| 1 | <?php |
|---|
| 2 | /** |
|---|
| 3 | * Chipotle Software TM |
|---|
| 4 | * Manuel Montoya 2002-2008 |
|---|
| 5 | * GPLv3 manuel<at>mononeurona<dot>org |
|---|
| 6 | * @version 0.3 |
|---|
| 7 | */ |
|---|
| 8 | |
|---|
| 9 | App::import('Sanitize'); |
|---|
| 10 | |
|---|
| 11 | class RecoversController extends AppController { |
|---|
| 12 | |
|---|
| 13 | public $helpers = array('Javascript', 'Ajax', 'Form', 'Fck', 'Gags'); |
|---|
| 14 | |
|---|
| 15 | public $components = array('Portal', 'Email', 'Adds'); |
|---|
| 16 | |
|---|
| 17 | public function beforeFilter() |
|---|
| 18 | { |
|---|
| 19 | parent::beforeFilter(); |
|---|
| 20 | $this->Auth->allow(array('confirm', 'newpwd', 'recover')); |
|---|
| 21 | } |
|---|
| 22 | |
|---|
| 23 | public function recover() |
|---|
| 24 | { |
|---|
| 25 | if ( $this->Auth->user() ): |
|---|
| 26 | $this->msgFlash(__('You are logged', true), '/'); |
|---|
| 27 | return false; |
|---|
| 28 | endif; |
|---|
| 29 | |
|---|
| 30 | $this->pageTitle = __('Recover password', true); |
|---|
| 31 | |
|---|
| 32 | $this->Portal->statics(); // Using Portal component |
|---|
| 33 | |
|---|
| 34 | $this->layout = 'portal'; |
|---|
| 35 | } |
|---|
| 36 | /*** Recover password check, method to check if email exist and send email ****/ |
|---|
| 37 | public function confirm() |
|---|
| 38 | { |
|---|
| 39 | if ( isset( $this->data['User']['email'] ) ): |
|---|
| 40 | Sanitize::paranoid($this->data['User']['email']); |
|---|
| 41 | $user_id = $this->Recover->User->field('User.id', array('User.email' => $this->data['User']['email'], 'User.active'=>1)); |
|---|
| 42 | if ($user_id == null): |
|---|
| 43 | $this->set('error_message', 'Error: email <b>' . $this->data['User']['email'] . '</b> '.__('does not exist on database', true)); |
|---|
| 44 | $this->render('check', 'ajax'); |
|---|
| 45 | else: // email exist |
|---|
| 46 | $this->Recover->deleteAll(array('Recover.user_id' => $user_id)); // remove previous |
|---|
| 47 | $this->data['Recover']['user_id'] = (int) $user_id; //the user id |
|---|
| 48 | $this->data['Recover']['random'] = $this->Adds->genPassword(20); |
|---|
| 49 | if ( $this->Recover->save($this->data) ): |
|---|
| 50 | if ( $this->__sendNewUserPwd($this->data['User']['email'], $this->data['Recover']['random'])): |
|---|
| 51 | $this->set('message', __('Success. An email has been sent to', true).": <b>".$this->data['User']['email']) . "</b>"; |
|---|
| 52 | $this->render('check', 'ajax'); |
|---|
| 53 | endif; |
|---|
| 54 | endif; |
|---|
| 55 | endif; |
|---|
| 56 | endif; |
|---|
| 57 | } |
|---|
| 58 | |
|---|
| 59 | public function newpwd($random = null) |
|---|
| 60 | { |
|---|
| 61 | if ( $random == null ): |
|---|
| 62 | redirect('/'); |
|---|
| 63 | endif; |
|---|
| 64 | |
|---|
| 65 | $this->layout = 'popup'; |
|---|
| 66 | |
|---|
| 67 | $this->pageTitle = __('Karamelo New Password', true); |
|---|
| 68 | |
|---|
| 69 | $conditions = array('random' => $random); |
|---|
| 70 | |
|---|
| 71 | $fields = array('id', 'user_id'); |
|---|
| 72 | |
|---|
| 73 | $data = $this->Recover->find($conditions, $fields); |
|---|
| 74 | |
|---|
| 75 | if ( $data == null ): |
|---|
| 76 | $this->redirect('/'); |
|---|
| 77 | else: |
|---|
| 78 | |
|---|
| 79 | $this->data['User']['id'] = $data['Recover']['user_id']; |
|---|
| 80 | $pwd = $this->Adds->genPassword(8); |
|---|
| 81 | $this->data['User']['pwd'] = $this->Auth->password($pwd); |
|---|
| 82 | |
|---|
| 83 | if ( $this->Recover->User->save($this->data) ): |
|---|
| 84 | $this->set('pwd', $pwd); |
|---|
| 85 | $this->Recover->del($data['Recover']['user_id']); //del the row |
|---|
| 86 | endif; |
|---|
| 87 | endif; |
|---|
| 88 | } |
|---|
| 89 | |
|---|
| 90 | private function __sendNewUserPwd($email, $random) |
|---|
| 91 | { |
|---|
| 92 | $this->Email->to = $email; |
|---|
| 93 | //$this->Email->bcc = array('noreply@karamelo.org'); // note |
|---|
| 94 | // this could be just a string too |
|---|
| 95 | $this->Email->subject = 'Karamelo e-Learning:: recover password'; |
|---|
| 96 | $this->Email->replyTo = 'support@karamelo.org'; |
|---|
| 97 | $this->Email->from = 'Chipotle-software.com'; |
|---|
| 98 | $this->Email->template = 'recover'; // note no '.ctp' |
|---|
| 99 | //Send as 'html', 'text' or 'both' (default is 'text') |
|---|
| 100 | $this->Email->sendAs = 'text'; // because we like to send pretty mail |
|---|
| 101 | //Set view variables as normal |
|---|
| 102 | $this->set('random', $random); |
|---|
| 103 | //Do not pass any args to send() |
|---|
| 104 | if ( $this->Email->send() ): |
|---|
| 105 | return true; |
|---|
| 106 | else: |
|---|
| 107 | return false; |
|---|
| 108 | endif; |
|---|
| 109 | } |
|---|
| 110 | } |
|---|
| 111 | ?> |
|---|