Frontend
[ class tree: Frontend ] [ index: Frontend ] [ all elements ]

Source for file UserController.php

Documentation is available at UserController.php

  1. <?php
  2. /**
  3. * DotBoost Technologies Inc.
  4. * DotKernel Application Framework
  5. *
  6. @category   DotKernel
  7. @package    Frontend
  8. @copyright  Copyright (c) 2009 DotBoost  Technologies (http://www.dotboost.com)
  9. @license    http://opensource.org/licenses/osl-3.0.php  Open Software License (OSL 3.0)
  10. @version    $Id: UserController.php 153 2010-06-23 10:06:25Z teo $
  11. */
  12.  
  13. /**
  14. * User Controller
  15. @author     DotKernel Team <team@dotkernel.com>
  16. */
  17.  
  18. // instantiate classes related to User module: model & view
  19. $userModel new User()
  20. $userView new User_View($tpl);
  21. // all actions MUST set  the variable  $pageTitle
  22. $pageTitle $option->pageTitle->action->{$requestAction};
  23. // switch based on the action, don't forget the default action
  24. switch ($requestAction)
  25. {
  26.     default:
  27.         // default action is login
  28.         $pageTitle $option->pageTitle->action->login;
  29.     case 'login':
  30.         if(!isset($session->user))
  31.         {
  32.             // display Login form
  33.             $userView->loginForm('login');
  34.         }
  35.         else
  36.         {            
  37.             header('Location: '.$config->website->params->url.'/user/account');
  38.             exit;
  39.         }
  40.     break;
  41.     case 'authorize':
  42.         // authorize user login 
  43.         if(array_key_exists('send'$_POST&& 'on' == $_POST['send'])
  44.         {    
  45.             // validate the authorization request paramethers 
  46.             $validate $userModel->validateLogin($_POST['username']$_POST['password']$_POST['send']);
  47.             if(!empty($validate['login']&& empty($validate['error']))
  48.             {
  49.                 // login info are VALID, we can see if is a valid user now 
  50.                 $user $userModel->checkLogin($validate['login']);
  51.                 if(!empty($user))
  52.                 {
  53.                     // user is valid and logged in
  54.                     $session->user $user;
  55.                     //prepare data for register the login
  56.                     $dataLogin array('ip' => Dot_Kernel::getUserIp()
  57.                               'userId' => $session->user['id']
  58.                               'username' => $session->user['username']
  59.                               'referer' => $_SERVER['HTTP_REFERER'],
  60.                               'userAgent' => $_SERVER["HTTP_USER_AGENT"]);
  61.                     $userModel->registerLogin($dataLogin);
  62.                     header('location: '.$config->website->params->url.'/user/account');
  63.                     exit;
  64.                 }
  65.                 else
  66.                 {
  67.                     unset($session->user);
  68.                     $session->message['txt'$option->errorMessage->login;
  69.                     $session->message['type''error';
  70.                 }
  71.             }
  72.             else
  73.             {
  74.                 // login info are NOT VALID
  75.                 $session->message['txt'array($validate['error']['username']$validate['error']['password']);
  76.                 $session->message['type''error';
  77.             }        
  78.         }
  79.         else
  80.         {
  81.             $session->message['txt'$option->warningMessage->userPermission;
  82.             $session->message['type''warning';
  83.         }
  84.         header('Location: '.$config->website->params->url.'/user/login');
  85.         exit;                
  86.             
  87.     break;
  88.     case 'account':
  89.         // display My Account page, if user is logged in 
  90.         Dot_Auth::checkIdentity();
  91.         $data array();
  92.         $error array();
  93.         if(array_key_exists('send'$_POST&& 'on' == $_POST['send'])
  94.         {                
  95.             // POST values that will be validated                
  96.             $values array('details' => 
  97.                                 array('firstName'=>$_POST['firstName'],
  98.                                       'lastName'=>$_POST['lastName']
  99.                                      ),
  100.                             'email' => array('email' => $_POST['email']),
  101.                             'password' => array('password' => $_POST['password'],
  102.                                                 'password2' =>  $_POST['password2']
  103.                                                )
  104.                           );
  105.             $valid $userModel->validateUser($values);
  106.             $data $valid['data'];
  107.             $error $valid['error'];
  108.             $data['id'$request['id'];        
  109.             if(empty($error))
  110.             {                
  111.                 // no error - then update user
  112.                 $userModel->updateUser($data);
  113.                 $session->message['txt'$option->infoMessage->update;
  114.                 $session->message['type''info';            
  115.             }
  116.             else
  117.             {
  118.                 $session->message['txt'$error;
  119.                 $session->message['type''error';
  120.             }            
  121.             $dataTmp $userModel->getUserInfo($session->user['id']);
  122.             $data['username'$dataTmp['username'];
  123.         }
  124.         else
  125.         {            
  126.             $data $userModel->getUserInfo($session->user['id']);
  127.         }
  128.         $userView->details('update',$data);    
  129.     break;
  130.     case 'register':
  131.         // display signup form and allow user to register 
  132.         $data array();
  133.         $error array();
  134.         if(array_key_exists('send'$_POST&& 'on' == $_POST['send'])
  135.         {        
  136.             // POST values that will be validated                
  137.             $values array('details' => 
  138.                                 array('firstName'=>$_POST['firstName'],
  139.                                       'lastName'=>$_POST['lastName']
  140.                                      ),
  141.                             'username' => array('username'=>$_POST['username']),
  142.                             'email' => array('email' => $_POST['email']),
  143.                             'password' => array('password' => $_POST['password'],
  144.                                                 'password2' =>  $_POST['password2']
  145.                                                )
  146.                           );
  147.             $valid $userModel->validateUser($values);
  148.             $data $valid['data'];
  149.             $error $valid['error'];
  150.             if(!isset($_POST['recaptcha_response_field']|| strlen($_POST['recaptcha_response_field']== 0)
  151.             {
  152.                 $error['Secure Image'$option->errorMessage->captcha;
  153.             }
  154.             else
  155.             {
  156.                 // validate secure image code
  157.                 $result $userView->getRecaptcha()->verify($_POST['recaptcha_challenge_field'],$_POST['recaptcha_response_field']);                
  158.                 if (!$result->isValid()) 
  159.                 {
  160.                     $error['Secure Image'$option->errorMessage->captcha;
  161.                 }
  162.             }    
  163.             if(empty($error))
  164.             {    
  165.                 //check if user already exists by $field ('username','email')
  166.                 $checkBy array('username','email');
  167.                 foreach ($checkBy as $field)
  168.                 {                    
  169.                        $userExists $userModel->getUserBy($field$data[$field]);
  170.                     if(!empty($userExists))
  171.                     {
  172.                         $error[$fielducfirst($field).$option->errorMessage->userExists;
  173.                     }
  174.                 }    
  175.             }
  176.             if(empty($error))
  177.             {                
  178.                    // no error - then add user
  179.                 $userModel->addUser($data);
  180.                 $session->message['txt'$option->infoMessage->add;
  181.                 $session->message['type''info';
  182.                 $validate $userModel->validateLogin($data['username']$data['password']'on');
  183.                 if(!empty($validate['login']&& empty($validate['error']))
  184.                 {
  185.                     // login info are VALID, we can see if is a valid user now 
  186.                     $user $userModel->checkLogin($validate['login']);
  187.                     if(!empty($user))
  188.                     {
  189.                         $session->user $user;
  190.                         $data array();
  191.                         $error array();
  192.                     }
  193.                     else
  194.                     {
  195.                         //this else case should never be reach
  196.                         unset($session->user);
  197.                         $error['Error Login'$option->errorMessage->login;
  198.                     }
  199.                 }
  200.             }
  201.             else
  202.             {    
  203.                 if(array_key_exists('password'$data))
  204.                 
  205.                     // do not display password in the add form
  206.                     unset($data['password']);                
  207.                 }                            
  208.             }
  209.             // add action and validation are made with ajax - dojo.xhrPost, so return json string  
  210.             echo Zend_Json::encode(array('data'=>$data'error'=>$error));
  211.             // return $data and $error as json
  212.             exit;            
  213.         }
  214.         $userView->details('add',$data);
  215.     break;
  216.     case 'forgot-password':
  217.         // send an emai with the forgotten password
  218.         $data array();
  219.         $error array();
  220.         if(array_key_exists('send'$_POST&& 'on' == $_POST['send'])
  221.         {                
  222.             $valid $userModel->validateEmail($_POST['email']);
  223.             $data $valid['data'];
  224.             $error $valid['error'];
  225.             if(empty($error))
  226.             {    
  227.                 // no error - then send password
  228.                 $userModel->forgotPassword($data['email']);                        
  229.             }
  230.             else
  231.             {
  232.                 $session->message['txt'$error;
  233.                 $session->message['type''error';
  234.             }            
  235.         }
  236.         $userView->details('forgot_password',$data);        
  237.     break;
  238.     case 'logout':
  239.         Dot_Auth::clearIdentity('user');
  240.         header('location: '.$config->website->params->url);
  241.         exit;
  242.     break;    
  243. }

Documentation generated on Wed, 21 Jul 2010 07:34:46 +0000 by phpDocumentor 1.4.3