-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
955514f
commit c051f21
Showing
9 changed files
with
310 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
<?php defined( 'SYSPATH' ) or die( 'No direct access allowed.' ); | ||
|
||
/** | ||
* @package KodiCMS/Reflink | ||
* @category Controller | ||
* @author butschster <[email protected]> | ||
* @link http://kodicms.ru | ||
* @copyright (c) 2012-2014 butschster | ||
* @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt | ||
*/ | ||
class Controller_Reflink extends Controller_System_Controller { | ||
|
||
public function action_index() | ||
{ | ||
$code = $this->request->param('code'); | ||
if ($code === NULL) | ||
{ | ||
Model_Page_Front::not_found(); | ||
} | ||
|
||
$reflink_model = ORM::factory('user_reflink', $code); | ||
|
||
if (!$reflink_model->loaded()) | ||
{ | ||
Messages::errors(__('Reflink not found')); | ||
$this->go_home(); | ||
} | ||
|
||
$next_url = Arr::get($reflink_model->data, 'next_url'); | ||
|
||
try | ||
{ | ||
Database::instance()->begin(); | ||
Reflink::factory($reflink_model)->confirm(); | ||
$reflink_model->delete(); | ||
Database::instance()->commit(); | ||
} | ||
catch (Kohana_Exception $e) | ||
{ | ||
Database::instance()->rollback(); | ||
Messages::errors($e->getMessage()); | ||
} | ||
|
||
if (Valid::url($next_url)) | ||
{ | ||
$this->go($next_url); | ||
} | ||
|
||
$this->go_home(); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
<?php defined('SYSPATH') or die('No direct script access.'); | ||
|
||
/** | ||
* @package KodiCMS/Reflink | ||
* @category Model | ||
* @author butschster <[email protected]> | ||
* @link http://kodicms.ru | ||
* @copyright (c) 2012-2014 butschster | ||
* @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt | ||
*/ | ||
class Model_User_Reflink extends ORM { | ||
|
||
protected $_primary_key = 'code'; | ||
|
||
protected $_created_column = array( | ||
'column' => 'created', | ||
'format' => 'Y-m-d H:i:s' | ||
); | ||
|
||
protected $_belongs_to = array( | ||
'user' => array(), | ||
); | ||
|
||
protected $_serialize_columns = array('data'); | ||
|
||
/** | ||
* Generate new reflink code | ||
* | ||
* @param Model_User $user | ||
* @param integer $type reflink type | ||
* @param string $data string stored to reflink in database | ||
* @return string | ||
*/ | ||
public function generate(Model_User $user, $type, $data = NULL) | ||
{ | ||
if (!$user->loaded()) | ||
{ | ||
throw new Reflink_Exception(' User not loaded '); | ||
} | ||
|
||
$type = URL::title($type, '_'); | ||
|
||
$reflink = $this | ||
->reset(FALSE) | ||
->where('user_id', '=', $user->id) | ||
->where('type', '=', $type) | ||
->where('created', '>', DB::expr('CURDATE() - INTERVAL 1 HOUR')) | ||
->find(); | ||
|
||
if (!$reflink->loaded()) | ||
{ | ||
$values = array( | ||
'user_id' => (int) $user->id, | ||
'code' => uniqid(TRUE) . sha1(microtime()), | ||
'type' => $type, | ||
'data' => $data | ||
); | ||
|
||
$reflink = ORM::factory('user_reflink') | ||
->values($values, array_keys($values)) | ||
->create(); | ||
} | ||
else | ||
{ | ||
$reflink | ||
->set('data', $data) | ||
->update(); | ||
} | ||
|
||
return $reflink->code; | ||
} | ||
|
||
/** | ||
* Delete reflinks from database | ||
* Model must be loaded | ||
* | ||
* @return integer | ||
*/ | ||
public function delete() | ||
{ | ||
if (!$this->loaded()) | ||
{ | ||
throw new Reflink_Exception('Model not loaded or not found.'); | ||
} | ||
|
||
return DB::delete($this->table_name()) | ||
->where('user_id', '=', $this->user_id) | ||
->where('type', '=', $this->type) | ||
->execute($this->_db); | ||
} | ||
|
||
/** | ||
* Delete old reflinks from database | ||
* | ||
* @return integer | ||
*/ | ||
public function clear_old() | ||
{ | ||
return DB::delete($this->table_name()) | ||
->where('created', '<', DB::expr('CURDATE() - INTERVAL 1 DAY')) | ||
->execute($this->_db); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
<?php defined( 'SYSPATH' ) or die( 'No direct access allowed.' ); | ||
|
||
/** | ||
* @package KodiCMS/Reflink | ||
* @author butschster <[email protected]> | ||
* @link http://kodicms.ru | ||
* @copyright (c) 2012-2014 butschster | ||
* @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt | ||
*/ | ||
abstract class Reflink { | ||
|
||
public static function factory(Model_User_Reflink $reflink) | ||
{ | ||
$class_name = 'Reflink_' . ucfirst($reflink->type); | ||
|
||
if (!class_exists($class_name)) | ||
{ | ||
throw new Reflink_Exception('Class :class not exists', array( | ||
':class' => $class_name)); | ||
} | ||
|
||
return new $class_name($reflink); | ||
} | ||
|
||
/** | ||
* | ||
* @var Model_User_Reflink | ||
*/ | ||
protected $_model = NULL; | ||
|
||
public function __construct(Model_User_Reflink $reflink) | ||
{ | ||
$this->_model = $reflink; | ||
} | ||
|
||
abstract public function confirm(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
<?php defined('SYSPATH') OR die('No direct access allowed.'); | ||
|
||
/** | ||
* @package KodiCMS/Reflink | ||
* @category Exception | ||
* @author butschster <[email protected]> | ||
* @link http://kodicms.ru | ||
* @copyright (c) 2012-2014 butschster | ||
* @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt | ||
*/ | ||
class Reflink_Exception extends Kohana_Exception {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
<?php defined( 'SYSPATH' ) or die( 'No direct access allowed.' ); | ||
|
||
/** | ||
* @package KodiCMS/Reflink | ||
* @category Drivers | ||
* @author butschster <[email protected]> | ||
* @link http://kodicms.ru | ||
* @copyright (c) 2012-2014 butschster | ||
* @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt | ||
*/ | ||
class Reflink_Forgot extends Reflink { | ||
|
||
public function confirm() | ||
{ | ||
$new_password = Text::random(); | ||
$this->_model->user->change_email( $new_password ); | ||
|
||
try | ||
{ | ||
Email_Type::get('user_new_password')->send(array( | ||
'username' => $this->_model->user->username, | ||
'email' => $this->_model->user->email, | ||
'password' => $new_password | ||
)); | ||
|
||
Messages::success(__('An email has been send with your new password!')); | ||
|
||
$this->_model->delete(); | ||
|
||
return TRUE; | ||
} | ||
catch (Kohana_Exception $e) | ||
{ | ||
throw new Reflink_Exception('Email :email not send', array( | ||
':email' => $this->_model->user->email)); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
<?php defined( 'SYSPATH' ) or die( 'No direct access allowed.' ); | ||
|
||
/** | ||
* @package KodiCMS/Reflink | ||
* @category Drivers | ||
* @author butschster <[email protected]> | ||
* @link http://kodicms.ru | ||
* @copyright (c) 2012-2014 butschster | ||
* @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt | ||
*/ | ||
class Reflink_Register extends Reflink { | ||
|
||
public function confirm() | ||
{ | ||
try | ||
{ | ||
$role = ORM::factory('role', array('name' => 'login')); | ||
$this->_model->user->add('roles', $role); | ||
|
||
Email_Type::get('user_registered')->send(array( | ||
'username' => $this->_model->user->username, | ||
'email' => $this->_model->user->email | ||
)); | ||
|
||
Messages::success(__('Thank you for registration!')); | ||
|
||
$this->_model->delete(); | ||
|
||
return TRUE; | ||
} | ||
catch (Kohana_Exception $e) | ||
{ | ||
throw new Reflink_Exception('Something went wrong'); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters