Advertisement
Guest User

Untitled

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