Advertisement
Guest User

Untitled

a guest
Mar 4th, 2019
150
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 16.12 KB | None | 0 0
  1. <?php
  2.     if ( ! defined('ABS_PATH')) exit('ABS_PATH is not loaded. Direct access is not allowed.');
  3.  
  4.     class CWebLogin extends BaseModel
  5.     {
  6.         function __construct()
  7.         {
  8.             parent::__construct();
  9.             if( !osc_users_enabled() ) {
  10.                 osc_add_flash_error_message( _m('Users not enabled') );
  11.                 $this->redirectTo(osc_base_url());
  12.             }
  13.             osc_run_hook( 'init_login' );
  14.         }
  15.  
  16.         //Business Layer...
  17.         function doModel()
  18.         {
  19.             switch( $this->action ) {
  20.                 case('login_post'):     //post execution for the login
  21.                                         if(!osc_users_enabled()) {
  22.                                             osc_add_flash_error_message(_m('Users are not enabled'));
  23.                                             $this->redirectTo(osc_base_url());
  24.                                         }
  25.                                         osc_csrf_check();
  26.                                         osc_run_hook('before_validating_login');
  27.  
  28.                                         // e-mail or/and password is/are empty or incorrect
  29.                                         $wrongCredentials = false;
  30.                                         $email = trim(Params::getParam('email'));
  31.                                         $captcha = trim(Params::getParam('captcha'));
  32.                                         $password = Params::getParam('password', false, false);
  33.                                         if ( $email == '' ) {
  34.                                             osc_add_flash_error_message( _m('Please provide an email address.') );
  35.                                             $wrongCredentials = true;
  36.                                         }
  37.                                         if ( $captcha == '' ) {
  38.                                             osc_add_flash_error_message( _m('Please provide a captcha code.') );
  39.                                             $wrongCredentials = true;
  40.                                         }
  41.                                         if ($_SESSION['captcha'] ==!  $captcha ) {
  42.                                             osc_add_flash_error_message( _m('Captcha code is incorrect.') );
  43.                                             $wrongCredentials = true;
  44.                                         }
  45.                                         if ( $password == '' ) {
  46.                                             osc_add_flash_error_message( _m('Empty passwords are not allowed. Please provide a password') );
  47.                                             $wrongCredentials = true;
  48.                                         }
  49.                                         if ( $wrongCredentials ) {
  50.                                             $this->redirectTo( osc_user_login_url() );
  51.                                         }
  52.  
  53.                                         if(osc_validate_email($email)) {
  54.                                             $user = User::newInstance()->findByEmail( $email );
  55.                                         }
  56.                                         if ( empty($user) ) {
  57.                                             $user = User::newInstance()->findByUsername( $email );
  58.                                         }
  59.                                         if ( empty($user) ) {
  60.                                             osc_add_flash_error_message(_m("The user doesn't exist"));
  61.                                             $this->redirectTo( osc_user_login_url() );
  62.                                         }
  63.                                         if ( ! osc_verify_password($password, (isset($user['s_password'])?$user['s_password']:'') )) {
  64.                                             osc_add_flash_error_message( _m('The password is incorrect'));
  65.                                             $this->redirectTo( osc_user_login_url() ); // @TODO if valid user, send email parameter back to the login form
  66.                                         } else {
  67.                                             if (@$user['s_password']!='') {
  68.                                                 if (preg_match('|\$2y\$([0-9]{2})\$|', $user['s_password'], $cost)) {
  69.                                                     if ($cost[1] != BCRYPT_COST) {
  70.                                                          User::newInstance()->update(
  71.                                                          array( 's_password' => osc_hash_password($password))
  72.                                                          ,array( 'pk_i_id' => $user['pk_i_id'] ) );
  73.                                                     }
  74.                                                 } else {
  75.                                                     User::newInstance()->update(
  76.                                                         array( 's_password' => osc_hash_password($password))
  77.                                                         ,array( 'pk_i_id' => $user['pk_i_id'] ) );
  78.                                                 }
  79.                                             }
  80.                                         }
  81.                                         // e-mail or/and IP is/are banned
  82.                                         $banned = osc_is_banned($email); // int 0: not banned or unknown, 1: email is banned, 2: IP is banned, 3: both email & IP are banned
  83.                                         if($banned & 1) {
  84.                                             osc_add_flash_error_message( _m('Your current email is not allowed'));
  85.                                         }
  86.                                         if($banned & 2) {
  87.                                             osc_add_flash_error_message( _m('Your current IP is not allowed'));
  88.                                         }
  89.                                         if($banned !== 0) {
  90.                                             $this->redirectTo( osc_user_login_url() );
  91.                                         }
  92.  
  93.                                         osc_run_hook('before_login');
  94.  
  95.                                         $url_redirect = osc_get_http_referer();
  96.                                         if(osc_rewrite_enabled() && $url_redirect!='') {
  97.                                             // if comes from oc-admin/
  98.                                             if(strpos($url_redirect,'oc-admin')!==false) {
  99.                                                 $url_redirect = osc_user_dashboard_url();
  100.                                             } else {
  101.                                                 $request_uri = urldecode(preg_replace('@^' . osc_base_url() . '@', "", $url_redirect));
  102.                                                 $tmp_ar = explode("?", $request_uri);
  103.                                                 $request_uri = $tmp_ar[0];
  104.                                                 $rules = Rewrite::newInstance()->listRules();
  105.                                                 foreach($rules as $match => $uri) {
  106.                                                     if(preg_match('#'.$match.'#', $request_uri, $m)) {
  107.                                                         $request_uri = preg_replace('#'.$match.'#', $uri, $request_uri);
  108.                                                         if(preg_match('|([&?]{1})page=([^&]*)|', '&'.$request_uri.'&', $match)) {
  109.                                                             $page_redirect = $match[2];
  110.                                                             if($page_redirect=='' || $page_redirect=='login') {
  111.                                                                 $url_redirect = osc_user_dashboard_url();
  112.                                                             }
  113.                                                         }
  114.                                                         break;
  115.                                                     }
  116.                                                 }
  117.                                             }
  118.                                         }
  119.  
  120.                                         require_once LIB_PATH . 'osclass/UserActions.php';
  121.                                         $uActions = new UserActions(false);
  122.                                         $logged = $uActions->bootstrap_login($user['pk_i_id']);
  123.  
  124.                                         if($logged==0) {
  125.                                             osc_add_flash_error_message(_m("The user doesn't exist"));
  126.                                         } else if($logged==1) {
  127.                                             if((time()-strtotime($user['dt_access_date']))>1200) { // EACH 20 MINUTES
  128.                                                 osc_add_flash_error_message(sprintf(_m('The user has not been validated yet. Would you like to re-send your <a href="%s">activation?</a>'), osc_user_resend_activation_link($user['pk_i_id'], $user['s_email'])));
  129.                                             } else {
  130.                                                 osc_add_flash_error_message(_m('The user has not been validated yet'));
  131.                                             }
  132.                                         } else if($logged==2) {
  133.                                             osc_add_flash_error_message(_m('The user has been suspended'));
  134.                                         } else if($logged==3) {
  135.                                             if ( Params::getParam('remember') == 1 ) {
  136.  
  137.                                                 //this include contains de osc_genRandomPassword function
  138.                                                 require_once osc_lib_path() . 'osclass/helpers/hSecurity.php';
  139.                                                 $secret = osc_genRandomPassword();
  140.  
  141.                                                 User::newInstance()->update(
  142.                                                     array('s_secret' => $secret)
  143.                                                     ,array('pk_i_id' => $user['pk_i_id'])
  144.                                                 );
  145.  
  146.                                                 Cookie::newInstance()->set_expires( osc_time_cookie() );
  147.                                                 Cookie::newInstance()->push('oc_userId', $user['pk_i_id']);
  148.                                                 Cookie::newInstance()->push('oc_userSecret', $secret);
  149.                                                 Cookie::newInstance()->set();
  150.                                             }
  151.  
  152.                                             if($url_redirect=='') {
  153.                                                 $url_redirect = osc_user_dashboard_url();
  154.                                             }
  155.  
  156.                                             osc_run_hook("after_login", $user, $url_redirect);
  157.  
  158.                                             $this->redirectTo( osc_apply_filter('correct_login_url_redirect', $url_redirect) );
  159.  
  160.                                         } else {
  161.                                             osc_add_flash_error_message(_m('This should never happen'));
  162.                                         }
  163.  
  164.                                         if( ! $user['b_enabled']) {
  165.                                             $this->redirectTo(osc_user_login_url());
  166.                                         }
  167.  
  168.                                         $this->redirectTo(osc_user_login_url());
  169.                                         break;
  170.                 case('resend'):
  171.                                         $id = Params::getParam('id');
  172.                                         $email = Params::getParam('email');
  173.                                         $user = User::newInstance()->findByPrimaryKey($id);
  174.                                         if($id=='' || $email=='' || !isset($user) || $user['b_active']==1 || $email!=$user['s_email']) {
  175.                                             osc_add_flash_error_message(_m('Incorrect link'));
  176.                                             $this->redirectTo(osc_user_login_url());
  177.                                         }
  178.                                         if((time()-strtotime($user['dt_access_date']))>1200) { // EACH 20 MINUTES
  179.                                             if(osc_notify_new_user()) {
  180.                                                 osc_run_hook('hook_email_admin_new_user', $user);
  181.                                             }
  182.                                             if(osc_user_validation_enabled()) {
  183.                                                 osc_run_hook('hook_email_user_validation', $user, $user);
  184.                                             }
  185.                                             User::newInstance()->update(array('dt_access_date' => date('Y-m-d H:i:s')), array('pk_i_id'  => $user['pk_i_id']));
  186.                                             osc_add_flash_ok_message(_m('Validation email re-sent'));
  187.                                         } else {
  188.                                             osc_add_flash_warning_message(_m('We have just sent you an email to validate your account, you will have to wait a few minutes to resend it again'));
  189.                                         }
  190.                                         $this->redirectTo(osc_user_login_url());
  191.                                         break;
  192.                 case('recover'):        //form to recover the password (in this case we have the form in /gui/)
  193.                                         $this->doView( 'user-recover.php' );
  194.                 break;
  195.                 case('recover_post'):   //post execution to recover the password
  196.                                         osc_csrf_check();
  197.                                         require_once LIB_PATH . 'osclass/UserActions.php';
  198.  
  199.                                         // e-mail is incorrect
  200.                                         if( !osc_validate_email(Params::getParam('s_email')) ) {
  201.                                             osc_add_flash_error_message( _m('Invalid email address') );
  202.                                             $this->redirectTo( osc_recover_user_password_url() );
  203.                                         }
  204.  
  205.                                         $userActions = new UserActions(false);
  206.                                         $success = $userActions->recover_password();
  207.  
  208.                                         switch ($success) {
  209.                                             case(0): // recover ok
  210.                                                      osc_add_flash_ok_message( _m('We have sent you an email with the instructions to reset your password'));
  211.                                                      $this->redirectTo( osc_base_url() );
  212.                                                      break;
  213.                                             case(1): // e-mail does not exist
  214.                                                      osc_add_flash_error_message( _m('We were not able to identify you given the information provided'));
  215.                                                      $this->redirectTo( osc_recover_user_password_url() );
  216.                                                      break;
  217.                                             case(2): // recaptcha wrong
  218.                                                      osc_add_flash_error_message( _m('The recaptcha code is wrong'));
  219.                                                      $this->redirectTo( osc_recover_user_password_url() );
  220.                                                      break;
  221.                                         }
  222.                 break;
  223.                 case('forgot'):         //form to recover the password (in this case we have the form in /gui/)
  224.                                         $user = User::newInstance()->findByIdPasswordSecret(Params::getParam('userId'), Params::getParam('code'));
  225.                                         if($user) {
  226.                                             $this->doView( 'user-forgot_password.php' );
  227.                                         } else {
  228.                                             osc_add_flash_error_message( _m('Sorry, the link is not valid'));
  229.                                             $this->redirectTo( osc_base_url() );
  230.                                         }
  231.                 break;
  232.                 case('forgot_post'):
  233.                                         osc_csrf_check();
  234.                                         if( (Params::getParam('new_password', false, false) == '') || (Params::getParam('new_password2', false, false) == '') ) {
  235.                                             osc_add_flash_warning_message( _m('Password cannot be blank'));
  236.                                             $this->redirectTo(osc_forgot_user_password_confirm_url(Params::getParam('userId'), Params::getParam('code')));
  237.                                         }
  238.  
  239.                                         $user = User::newInstance()->findByIdPasswordSecret(Params::getParam('userId'), Params::getParam('code'));
  240.                                         if($user['b_enabled'] == 1) {
  241.                                             if(Params::getParam('new_password', false, false)==Params::getParam('new_password2', false, false)) {
  242.                                                 User::newInstance()->update(
  243.                                                     array('s_pass_code' => osc_genRandomPassword(50)
  244.                                                         , 's_pass_date' => date('Y-m-d H:i:s', 0)
  245.                                                         , 's_pass_ip' => Params::getServerParam('REMOTE_ADDR')
  246.                                                         , 's_password' => osc_hash_password(Params::getParam('new_password', false, false))
  247.                                                     ), array('pk_i_id' => $user['pk_i_id'])
  248.                                                 );
  249.                                                 osc_add_flash_ok_message( _m('The password has been changed'));
  250.                                                 $this->redirectTo(osc_user_login_url());
  251.                                             } else {
  252.                                                 osc_add_flash_error_message( _m("Error, the password don't match"));
  253.                                                 $this->redirectTo(osc_forgot_user_password_confirm_url(Params::getParam('userId'), Params::getParam('code')));
  254.                                             }
  255.                                         } else {
  256.                                             osc_add_flash_error_message( _m('Sorry, the link is not valid'));
  257.                                         }
  258.                                         $this->redirectTo( osc_base_url() );
  259.                 break;
  260.                 default:                //login
  261.                                         Session::newInstance()->_setReferer(osc_get_http_referer());
  262.                                         if( osc_logged_user_id() != '') {
  263.                                             $this->redirectTo(osc_user_dashboard_url());
  264.                                         }
  265.                                         $this->doView( 'user-login.php' );
  266.             }
  267.         }
  268.  
  269.         //hopefully generic...
  270.         function doView($file)
  271.         {
  272.             osc_run_hook("before_html");
  273.             osc_current_web_theme_path($file);
  274.             osc_run_hook("after_html");
  275.         }
  276.     }
  277.  
  278.     /* file end: ./login.php */
  279. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement