示例#1
0
 public function post_login()
 {
     // get parameters and set to local variables
     $mymobile = utility::post('mobile', 'filter');
     $mypass = utility::post('password');
     // check for mobile exist
     $tmp_result = $this->sql()->tableUsers()->whereUser_mobile($mymobile)->and('user_status', 'active')->select();
     // $tmp_result =  $this->sql()->tableUsers()->select();
     // if exist
     if ($tmp_result->num() == 1) {
         $tmp_result = $tmp_result->assoc();
         $myhashedPassword = $tmp_result['user_pass'];
         // if password is correct. go for login:)
         if (isset($myhashedPassword) && utility::hasher($mypass, $myhashedPassword)) {
             // you can change the code way easily at any time!
             // $qry		= $this->sql()->tableUsers ()
             // 				->setUser_logincounter  ($tmp_result['user_logincounter'] +1)
             // 				->whereId               ($tmp_result['id']);
             // $sql		= $qry->update();
             $myfields = array('id', 'user_displayname', 'user_mobile', 'user_meta', 'user_status');
             $this->setLoginSession($tmp_result, $myfields);
             // ======================================================
             // you can manage next event with one of these variables,
             // commit for successfull and rollback for failed
             // if query run without error means commit
             $this->commit(function () {
                 // $this->logger('login');
                 // create code for pass with get to service home page
                 debug::true(T_("Login Successfully"));
                 \lib\utility\session::save();
                 $referer = \lib\router::urlParser('referer', 'host');
                 // set redirect to homepage
                 $this->redirector()->set_domain()->set_url();
                 if (\lib\utility\option::get('account', 'status')) {
                     $_redirect_sub = \lib\utility\option::get('account', 'meta', 'redirect');
                     if ($_redirect_sub !== 'home') {
                         if (\lib\utility\option::get('config', 'meta', 'fakeSub')) {
                             $this->redirector()->set_url($_redirect_sub);
                         } else {
                             $this->redirector()->set_sub_domain($_redirect_sub);
                         }
                     }
                 }
                 // do not use pushstate and run link direct
                 debug::msg('direct', true);
             });
             $this->rollback(function () {
                 debug::error(T_("Login failed!"));
             });
         } else {
             debug::error(T_("Mobile or password is incorrect"));
         }
     } elseif ($tmp_result->num() == 0) {
         debug::error(T_("Mobile or password is incorrect"));
     } else {
         debug::error(T_("Please forward this message to administrator"));
     }
     // sleep(0.1);
 }
示例#2
0
 /**
  * create a related query and run it
  * @param  [type] $_type [description]
  * @return [type]        [description]
  */
 protected function qryPermission($_type)
 {
     $newPerm = utility::post('pName');
     switch ($_type) {
         case 'add':
             if (!$newPerm) {
                 debug::warn(T_("First you must enter name of permission"));
                 return;
             }
             // check permission exist or not
             $qryExist = $this->qryCreator($_type);
             $qryExist = $qryExist->select()->num();
             // if exist show related message
             if ($qryExist) {
                 debug::warn(T_("This permission name exist!") . " " . T_("You can edit this permission"));
                 return;
             }
             // get last id in permissions
             $qryMaxID = $this->qryCreator();
             $qryMaxID = $qryMaxID->field('#max(option_key) as id')->select()->assoc('id');
             // permission id start from 2 because id 1 used for superuser
             $qryMaxID += $qryMaxID == 0 ? 2 : 1;
             $qryAdd = $this->qryCreator();
             $qryAdd = $qryAdd->set('option_cat', 'permissions')->set('option_key', $qryMaxID)->set('option_value', $newPerm)->set('option_status', 'enable')->insert();
             $qryAdd = $qryAdd;
             break;
         case 'delete':
             $delParam = $this->childparam('delete');
             // if user pass child param, get this param and update status of permission
             if ($delParam) {
                 $qryDel = $this->qryCreator($_type, $delParam);
                 $qryDel = $qryDel->set('option_status', 'disable')->update();
                 \lib\utility\session::deleteByPerm($editParam);
             }
             break;
         case 'edit':
             $editParam = $this->childparam('edit');
             if ($editParam) {
                 $permResult = \lib\utility\permission::permListFill(true);
                 $permResult = json_encode($permResult, JSON_FORCE_OBJECT | JSON_UNESCAPED_UNICODE);
                 $qryEdit = $this->qryCreator($_type, $editParam);
                 $qryEdit = $qryEdit->set('option_meta', $permResult)->update();
                 \lib\utility\session::deleteByPerm($editParam);
                 $this->setPermissionSession();
             }
             break;
         default:
             break;
     }
     $this->commit(function ($_type, $_permName) {
         switch ($_type) {
             case 'add':
                 debug::true(T_("Insert Successfully"));
                 $this->redirector()->set_url('permissions/' . $_permName);
                 break;
             case 'delete':
                 debug::true(T_("Delete Successfully"));
                 break;
             case 'edit':
                 debug::true(T_("Update Successfully"));
                 break;
             default:
                 break;
         }
     }, $_type, $newPerm);
     // if a query has error or any error occour in any part of codes, run roolback
     $this->rollback(function () {
         debug::title(T_("Transaction error") . ': ');
     });
 }
示例#3
0
 /**
  * save once telegram user details
  * @param  [type] $_telegram_id [description]
  * @param  [type] $_fromDetail  [description]
  * @return [type]               [description]
  */
 private static function catchTelegramUser($_telegram_id, $_fromDetail = null)
 {
     // if user_id is not set try to give user_id from database
     // search in db to find user_id
     $qry = "SELECT `user_id`\n\t\t\tFROM options\n\t\t\tWHERE\n\t\t\t\t`option_cat` LIKE 'telegram\\_%' AND\n\t\t\t\t`option_key` LIKE 'user\\_%' AND\n\t\t\t\t`option_value` = {$_telegram_id}\n\t\t";
     $my_user_id = \lib\db::get($qry, 'user_id', true);
     if (is_numeric($my_user_id)) {
         self::$user_id = $my_user_id;
     }
     // if user does not exist in db, signup it
     if (!self::$user_id) {
         // calc full_name of user
         $fullName = trim(self::response('from', 'first_name') . ' ' . self::response('from', 'last_name'));
         $mobile = 'tg_' . $_telegram_id;
         // generate password
         $password = \lib\utility\filter::temp_password();
         \lib\db\users::signup($mobile, $password, true, $fullName);
         self::$user_id = \lib\db\users::$user_id;
         // save telegram user detail like name and username into options
         $userDetail = ['cat' => 'telegram_' . self::$user_id, 'key' => 'user_' . self::response('from', 'username'), 'value' => $_telegram_id, 'meta' => $_fromDetail];
         if (isset(self::$user_id)) {
             $userDetail['user'] = self::$user_id;
             $userDetail['status'] = 'enable';
         } else {
             $userDetail['status'] = 'disable';
         }
         // save in options table
         \lib\utility\option::set($userDetail, true);
     }
     // save session id database only one time
     // if exist use old one else insert new one to database
     \lib\utility\session::save_once(self::$user_id, 'telegram_' . $_telegram_id);
     if (!array_key_exists('tg', $_SESSION) || !is_array($_SESSION['tg'])) {
         $_SESSION['tg'] = array();
     }
     if (self::$user_id) {
         return true;
     }
     return false;
 }