Skip to content

Commit

Permalink
Merge pull request #21 from douglas-88/dev
Browse files Browse the repository at this point in the history
Recovery Password
  • Loading branch information
douglas-88 authored Feb 21, 2020
2 parents fa159a8 + 1495f00 commit 01836ee
Show file tree
Hide file tree
Showing 7 changed files with 154 additions and 5 deletions.
9 changes: 6 additions & 3 deletions app/Controller/Admin/PasswordRecoveryController.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,12 @@
use Core\Controller;
use Core\Load;
use Core\Validate;
use Core\PasswordRecovery;

class PasswordRecoveryController extends Controller
{

CONST SECRET = "";
CONST SECRET = "DEUSNOCONTROLE!!";

public function forgot(){

Expand Down Expand Up @@ -41,8 +42,10 @@ public function enviarLinkRecuperarSenha(){
echo("Não achou o e-mail: {$data["email"]}");
return false;
}else{
$code = base64_encode(openssl_encrypt($dataRecovery["idrecovery"],"AES-128-ECB",User::SECRET));
dd($user);

$recovery = new PasswordRecovery();
dd($recovery->sendMessageLink($user));

}

}
Expand Down
11 changes: 11 additions & 0 deletions app/Functions/helpers.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?php

use App\Controller\Admin\PasswordRecoveryController;
use Core\Flash;
use Core\Redirect;

Expand Down Expand Up @@ -55,4 +56,14 @@ function back(){

function busca(){
return filter_input(INPUT_GET,"s",FILTER_SANITIZE_STRING);
}

function recoveryPasswordGenerate(){

$hash = md5(rand());
$code = base64_encode(openssl_encrypt($hash,"AES-128-ECB",PasswordRecoveryController::SECRET));
$root = (!empty($_SERVER['HTTPS']) ? 'https' : 'http') . '://' . $_SERVER['HTTP_HOST'] . '/';
$url = $root . "recovery-password/code={$code}";

return $url;
}
2 changes: 1 addition & 1 deletion phinx.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ environments:
host: localhost
name: twig_slim
user: root
pass: 'nokia5233'
pass: '1475'
port: 3306
charset: utf8

Expand Down
1 change: 0 additions & 1 deletion src/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ public function __construct()
try{

$this->connection = new PDO("mysql:host={$this->host};dbname={$this->dbname};charset=utf8", "{$this->userdb}", "$this->passworddb",$options);

}catch(\PDOException $e){
$this->erros = $e->getMessage();
}
Expand Down
37 changes: 37 additions & 0 deletions src/Email.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php


namespace Core;
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
use PHPMailer\PHPMailer\SMTP;


class Email extends PHPMailer
{

public function __construct($html,$exceptions = null)
{

$this->SMTPDebug = SMTP::DEBUG_OFF;
$this->isSMTP();
$this->Host = 'smtp.gmail.com'; // Set the SMTP server to send through
$this->SMTPAuth = true; // Enable SMTP authentication
$this->Username = '[email protected]'; // SMTP username
$this->Password = 'nokia5233'; // SMTP password
$this->SMTPSecure = "tls"; // Enable TLS encryption; `PHPMailer::ENCRYPTION_SMTPS` also accepted
$this->Port = 587;
$this->isHTML(true);
$this->CharSet = 'UTF-8';
$this->setLanguage("pt_br");
$this->setFrom("[email protected]");
$this->addAddress("[email protected]");
$this->addAddress("[email protected]");
$this->addAddress("[email protected]");
$this->Subject = "Recuperação de senha";
$this->Body = $html;

parent::__construct($exceptions);
}

}
56 changes: 56 additions & 0 deletions src/PasswordRecovery.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php


namespace Core;

use Core\Email;

class PasswordRecovery extends Model
{
protected $user;
protected $link;
protected $code;
protected $key = "DEUSNOCONTROLE!!";
protected $message;
protected $table = "password_recovery";


private function codeCreate():void{

$hash = md5(rand());
$this->code = base64_encode(openssl_encrypt($hash,"AES-128-ECB",$this->key));

}

private function linkCreate():void{
$root = (!empty($_SERVER['HTTPS']) ? 'https' : 'http') . '://' . $_SERVER['HTTP_HOST'] . '/';
$this->link = $root . "recovery-password/code={$this->code}";
}

private function messageCreate(){

}

private function recordAttempt(){
$data = [
"email" => $this->user->email,
"hash" => $this->code,
"status" => 0,
"user_id" => $this->user->id
];

$this->create($data);
}

public function sendMessageLink(object $user){
$this->user = $user;
$this->codeCreate();
$this->linkCreate();
$this->recordAttempt();
$email = new Email($this->link);
$email = $email->send();

return $email;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

use Phinx\Migration\AbstractMigration;

class CreateTablePasswordRecovery extends AbstractMigration
{
/**
* Change Method.
*
* Write your reversible migrations using this method.
*
* More information on writing migrations is available here:
* http://docs.phinx.org/en/latest/migrations.html#the-abstractmigration-class
*
* The following commands can be used in this method and Phinx will
* automatically reverse them when rolling back:
*
* createTable
* renameTable
* addColumn
* addCustomColumn
* renameColumn
* addIndex
* addForeignKey
*
* Any other destructive changes will result in an error when trying to
* rollback the migration.
*
* Remember to call "create()" or "update()" and NOT "save()" when working
* with the Table class.
*/
public function change()
{
$table = $this->table("password_recovery");
$table->addColumn("email","string",["limit" => 255]);
$table->addColumn("hash","string",["limit" => 88]);
$table->addColumn('status', 'integer', ['null' => true]);
$table->addColumn('user_id', 'integer', ['null' => true]);
$table->addForeignKey('user_id', 'users', 'id',['delete'=> 'SET_NULL']);
$table->addColumn("created",'timestamp', ['default' => 'CURRENT_TIMESTAMP']);
$table->create();
}
}

0 comments on commit 01836ee

Please sign in to comment.