Ejemplo n.º 1
0
 /**
  * signup to system
  * @return [type] [description]
  */
 public function post_signup()
 {
     // get parameters and set to local variables
     $mymobile = utility::post('mobile', 'filter');
     $mypass = utility::post('password', 'hash');
     $myperm = $this->option('account');
     if (!$myperm) {
         $myperm = 'NULL';
     }
     $user_id = \lib\db\users::signup($mymobile, $mypass, $myperm);
     if ($user_id) {
         // generate verification code
         // save in logs table
         // set SESSION verification_mobile
         $code = \lib\utility\filter::generate_verification_code($user_id, $mymobile);
         if ($code) {
             \lib\utility\sms::send($mymobile, 'signup', $code);
             debug::true(T_("Register successfully"));
             $this->redirector()->set_url('verification?from=signup&mobile=' . $mymobile);
             // $this->redirector()->set_url('login?from=signup&cp=1&mobile='.$mymobile);
         } else {
             debug::error(T_("Please contact to administrator!"));
         }
     } elseif ($user_id === false) {
         debug::error(T_("Mobile number exist!"));
     } else {
         debug::error(T_("Please contact to administrator!"));
     }
 }
Ejemplo n.º 2
0
 public function post_signup()
 {
     // get parameters and set to local variables
     $mymobile = utility::post('mobile', 'filter');
     $mypass = utility::post('password', 'hash');
     // check for mobile exist
     $tmp_result = $this->sql()->tableUsers()->whereUser_mobile($mymobile)->select();
     // if exist
     if ($tmp_result->num() == 1) {
         debug::error(T_("mobile number exist!"));
     } elseif ($tmp_result->num() == 0) {
         $qry = $this->sql()->tableUsers()->setUser_mobile($mymobile)->setUser_pass($mypass)->setUser_permission(3)->setUser_createdate(date('Y-m-d H:i:s'));
         $sql = $qry->insert();
         // ======================================================
         // 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 ($_mobile) {
             // \lib\utility\Sms::send($_mobile, 'signup', $_code);
             debug::true(T_("register successfully"));
             // $this->redirector()->set_url('verification?from=signup&mobile='.$_mobile.'&referer='.$myreferer);
             // $this->redirector()->set_url('login?from=signup&mobile='.$_mobile);
         }, $mymobile);
         // if a query has error or any error occour in any part of codes, run roolback
         $this->rollback(function () {
             debug::error(T_("register failed!"));
         });
     } else {
         debug::error(T_("please forward this message to administrator"));
     }
 }
Ejemplo n.º 3
0
 function post_changepass()
 {
     $myid = $this->login('id');
     $newpass = utility::post('password-new', 'hash');
     $oldpass = utility::post('password-old');
     $tmp_result = $this->sql()->tableUsers()->where('id', $myid)->and('user_status', 'active')->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($oldpass, $myhashedPassword)) {
             $newpass = utility::post('password-new', 'hash');
             $qry = $this->sql()->table('users')->where('id', $myid)->set('user_pass', $newpass);
             $sql = $qry->update();
             $this->commit(function () {
                 debug::true(T_("change password successfully"));
                 $this->redirector()->set_domain()->set_url();
                 // \lib\utility\Sms::send($_mobile, 'changepass');
             });
             // if a query has error or any error occour in any part of codes, run roolback
             $this->rollback(function () {
                 debug::error(T_("change password failed!"));
             });
         } else {
             debug::error(T_("Password is incorrect"));
         }
     } elseif ($tmp_result->num() == 0) {
         debug::error(T_("user is incorrect"));
     } else {
         debug::error(T_("Please forward this message to administrator"));
     }
     sleep(0.1);
 }
Ejemplo n.º 4
0
 /**
  * this function set custom operator for each custom module in cp
  * @param  [type] $_id [description]
  * @return [type]      [description]
  */
 function cp_create_query($_id = null)
 {
     if (!$_id) {
         $_id = $this->childparam('edit');
     }
     $cpModule = $this->cpModule();
     $mymodule = $this->cpModule('raw');
     $qry = $this->sql();
     $datarow = array();
     $datarow['slug'] = utility::post('slug', 'filter');
     $datarow['parent'] = utility::post('parent');
     if (!$datarow['slug']) {
         $datarow['slug'] = utility\filter::slug(utility::post('title'));
     }
     if ($datarow['parent']) {
         $datarow['url'] = $this->sql()->table('terms')->where('id', $datarow['parent'])->select()->assoc('term_url') . '/' . $datarow['slug'];
     } else {
         $datarow['parent'] = '#NULL';
         $datarow['url'] = $datarow['slug'];
     }
     if ($cpModule['raw'] === 'bookcategories') {
         $datarow['url'] = 'book-index/' . preg_replace("#^(book-index\\/)+#", "", $datarow['url']);
     }
     // var_dump($datarow['slug']);exit();
     if (utility::post('title')) {
         $qry = $qry->table('terms')->set('term_type', $cpModule['type'])->set('term_language', utility::post('language'))->set('term_title', utility::post('title'))->set('term_slug', $datarow['slug'])->set('term_desc', utility::post('desc'))->set('term_parent', $datarow['parent'])->set('term_url', $datarow['url']);
     } else {
         debug::error(T_("Please enter title!"));
         return false;
     }
     $post_new_id = null;
     if ($_id) {
         // on edit
         $qry = $qry->where('id', $_id)->update();
         $post_new_id = $_id;
     } else {
         // on add
         $qry = $qry->insert();
         $post_new_id = $qry->LAST_INSERT_ID();
     }
     // ======================================================
     // 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 ($_module, $_postId, $_edit = null) {
         if ($_edit) {
             debug::true(T_("Update Successfully"));
             // $this->redirector()->set_url($_module.'/edit='.$_postId);
         } else {
             debug::true(T_("Insert Successfully"));
             $this->redirector()->set_url($_module . '/add');
             // $this->redirector()->set_url($_module.'/edit='.$_postId);
         }
     }, $mymodule, $post_new_id, $_id);
     // if a query has error or any error occour in any part of codes, run roolback
     $this->rollback(function () {
         debug::title(T_("Transaction error") . ': ');
     });
 }
Ejemplo n.º 5
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);
 }
Ejemplo n.º 6
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_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"));
                 $referer = \lib\router::urlParser('referer', 'host');
                 /**
                  * temporary: after fix permissions below line must be delete
                  */
                 if ($referer == 'archiver.dev' || $referer == 'irancamera.ir') {
                     $this->redirector()->set_domain()->set_sub_domain('files')->set_url();
                 } elseif (\lib\router::get_storage('CMS')) {
                     $this->redirector()->set_domain()->set_sub_domain(\lib\router::get_storage('CMS'))->set_url();
                 } else {
                     $this->redirector()->set_domain()->set_url();
                 }
             });
             $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);
 }
Ejemplo n.º 7
0
 public function put_verification()
 {
     // get parameters and set to local variables
     $mycode = utility::post('code');
     $mymobile = utility::post('mobile', 'filter');
     if ($mymobile == '' && isset($_SESSION['verification_mobile'])) {
         $mymobile = $_SESSION['verification_mobile'];
     }
     $myuserid = $this->sql()->table('users')->field('id')->where('user_mobile', $mymobile)->select()->assoc('id');
     // check for mobile exist
     $tmp_result = $this->sql()->table('logs')->where('user_id', $myuserid)->and('log_data', $mycode)->and('log_status', 'enable')->select();
     if ($tmp_result->num()) {
         // mobile and code exist update the record and verify
         $qry = $this->sql()->table('logs')->set('log_status', 'expire')->where('user_id', $myuserid)->and('log_data', $mycode)->and('log_status', 'enable');
         $sql = $qry->update();
         $sql_users = $this->sql()->table('users')->where('id', $myuserid)->set('user_status', 'active')->update();
         // ======================================================
         // 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 ($_mobile, $_userid) {
             $myfrom = utility\cookie::read('from');
             if ($myfrom == 'signup') {
                 // login user to system
                 $this->model()->setLogin($_userid);
                 //Send SMS
                 \lib\utility\sms::send($_mobile, 'verification');
                 debug::true(T_("verify successfully."));
             } else {
                 // login user to system
                 $this->model()->setLogin($_userid, false);
                 $this->redirector()->set_url('changepass');
                 $myreferer = utility\cookie::write('mobile', $_mobile, 60 * 5);
                 $myreferer = utility\cookie::write('from', 'verification', 60 * 5);
                 debug::true(T_("verify successfully.") . ' ' . T_("please Input your new password"));
             }
         }, $mymobile, $myuserid);
         // if a query has error or any error occour in any part of codes, run roolback
         $this->rollback(function () {
             debug::error(T_("verify failed!"));
         });
     } elseif ($tmp_result->num() == 0) {
         debug::error(T_("this data is incorrect"));
     } else {
         debug::error(T_("please forward this message to administrator"));
     }
 }
Ejemplo n.º 8
0
 function put_changeSmsStatus($mymobile)
 {
     $qry = $this->sql()->tableSmss()->setSms_status('expire')->whereSms_from($mymobile)->andSms_type('receive')->andSms_status('enable');
     $sql = $qry->update();
     // ======================================================
     // 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 () {
         debug::true(T_('we receive your message and your account is now verifited.'));
     });
     // if a query has error or any error occour in any part of codes, run roolback
     $this->rollback(function () {
         debug::error(T_('error on verify your code!'));
     });
 }
Ejemplo n.º 9
0
 /**
  * [delete_commit description]
  * @param  [type] $_qry [description]
  * @return [type]       [description]
  */
 protected function delete_commit($_qry)
 {
     $_qry = $_qry->delete();
     // var_dump($_qry);exit();
     // ======================================================
     // 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 () {
         debug::true(T_("Delete Successfully"));
     });
     // if a query has error or any error occour in any part of codes, run roolback
     $this->rollback(function () {
         debug::error(T_("Delete Failed!"));
     });
 }
Ejemplo n.º 10
0
 public function post_recovery()
 {
     // get parameters and set to local variables
     $mymobile = utility::post('mobile', 'filter');
     // check for mobile exist
     $tmp_result = $this->sql()->table('users')->where('user_mobile', $mymobile)->select();
     if ($tmp_result->num() == 1) {
         $myuserid = $tmp_result->assoc('id');
         $mylogitem = $this->sql()->table('logitems')->field('id')->where('logitem_title', 'account/recovery')->select()->assoc('id');
         if (!isset($mylogitem)) {
             return;
         }
         $mycode = utility::randomCode();
         $qry = $this->sql()->table('logs')->set('logitem_id', $mylogitem)->set('user_id', $myuserid)->set('log_data', $mycode)->set('log_status', 'enable')->set('log_createdate', date('Y-m-d H:i:s'));
         // var_dump($qry->insertString());
         // return;
         $sql = $qry->insert();
         // ======================================================
         // 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 ($_mobile, $_code) {
             $myreferer = utility\Cookie::read('referer');
             //Send SMS
             \lib\utility\Sms::send($_mobile, 'recovery', $_code);
             debug::true(T_("we send a verification code for you"));
             $myreferer = utility\Cookie::write('mobile', $_mobile, 60 * 5);
             $myreferer = utility\Cookie::write('from', 'recovery', 60 * 5);
             $this->redirector()->set_url('verification?from=recovery&mobile=' . $_mobile . '&referer=' . $myreferer);
         }, $mymobile, $mycode);
         // if a query has error or any error occour in any part of codes, run roolback
         $this->rollback(function () {
             debug::error(T_("recovery failed!"));
         });
     } elseif ($tmp_result->num() == 0) {
         debug::error(T_("Mobile number is incorrect"));
     } else {
         debug::error(T_("please forward this message to administrator"));
     }
 }
Ejemplo n.º 11
0
 /**
  * optimize sql table, fields and value
  * @param  [string] $table [set table name]
  * @param  [string] $field [set field name]
  * @param  [string] $value [set value]
  * @return [string]        [optimize of string]
  * @example
  * 	oSting(users)			return #users#
  * 	oSting(users, id)		return #users.id#
  * 	oSting(users, id, 150)	return #users.id 150#
  */
 public function oString($table, $field = null, $value = null, $checkCondition = true)
 {
     if ($value !== null) {
         $cInt = false;
         // for insert or update multiple row
         if (is_array($value)) {
         } elseif (preg_match("/^#(.*)\$/", $value, $v)) {
             $value = $v[1];
             $cInt = true;
         } elseif (substr($value, 0, 1) == '#') {
             $value = substr($value, 1);
             $cInt = true;
         } else {
             $sTable = "get" . ucfirst(dbconnection::get_db_name());
             $cTable = sql\table::$sTable($table);
             if (isset($cTable->{$field})) {
                 $type = $cTable->{$field}->type;
                 $int = array("int", "tinyint", "smallint", "decimal");
                 preg_match("/^([^@]*)@/", $type, $tp);
                 if (preg_grep("/^" . $tp[1] . "\$/", $int)) {
                     $cInt = true;
                 }
                 if ($this->auto_validate) {
                     $status = $this->auto_validate($field, $cTable->{$field}, $value);
                     if (!is_bool($status)) {
                         \lib\debug::error($status, $field, 'form');
                     }
                 }
             }
             if (isset($cTable->{$field}->closure) && $checkCondition) {
                 $gTable = $cTable->{$field}->closure;
                 $value = preg_replace("/^\\\\#/", "#", $value);
                 $v = new validator(array($field, $value), $gTable->validate, 'form');
                 $value = $v->compile();
                 $value = $value == '' && is_string($value) && $value === false ? "NULL" : $value;
             }
             // switch by type of field and encode data if needed
             // var_dump($cTable->$field->type);
             if (isset($cTable->{$field}->type)) {
                 $atPos = strpos($cTable->{$field}->type, '@');
             } else {
                 // return false;
                 \lib\error::page("Field {$field} does not exist!");
             }
             if ($atPos !== false) {
                 switch (substr($cTable->{$field}->type, 0, $atPos)) {
                     // if the type of field is int do nothing
                     case 'tinyint':
                     case 'smallint':
                     case 'mediumint':
                     case 'int':
                     case 'bigint':
                     case 'decimal':
                     case 'float':
                         break;
                         // else doing entities
                     // else doing entities
                     case 'tinytext':
                     case 'text':
                     case 'mediumtext':
                     case 'longtext':
                     default:
                         // if does not contain meta doing nothing and encode value
                         if (strpos($field, '_meta') === false) {
                             $value = htmlentities($value, ENT_QUOTES, "UTF-8");
                         }
                         break;
                 }
             }
             // if(!$cInt)
             // {
             // 	$value = htmlentities($value, ENT_QUOTES, "UTF-8");
             // }
         }
         if (is_array($value)) {
             $optimize = $value;
         } else {
             $optimize = $cInt ? "{$value}" : "'{$value}'";
         }
     } else {
         $optimize = "`{$table}`";
         if ($field) {
             if (preg_match("/^#/", $field)) {
                 $optimize = preg_replace("/^#/", "", $field);
             } else {
                 // $optimize .= $field ? ($field === "*") ? ".$field" : ".`$field`" : "";
                 if ($field) {
                     if ($field === "*") {
                         $optimize .= ".{$field}";
                     } else {
                         $optimize .= ".`{$field}`";
                     }
                 } else {
                     $optimize .= "";
                 }
             }
         }
     }
     return $optimize;
 }
Ejemplo n.º 12
0
 /**
  * return
  * @param  string $_loc  location
  * @param  string $_type type of permission needed
  * @return [type]        [description]
  */
 public static function access($_content = null, $_loc = null, $_type = null, $_block = null)
 {
     $myStatus = null;
     $su = null;
     // if user is superviser then set su to true
     // permission id 1 is supervisior of system
     if (isset($_SESSION['user']['permission']) && $_SESSION['user']['permission'] === "1") {
         $su = true;
         $suStatus = self::permListFill('su');
     }
     // if programmer not set content, give it automatically from address
     if ($_content === 'all') {
         $myStatus = [];
         if ($su) {
             foreach ($suStatus as $key => $value) {
                 if (isset($value['enable'])) {
                     $myStatus[$key] = $value['enable'];
                 }
             }
         } elseif (isset($_SESSION['permission'])) {
             foreach ($_SESSION['permission'] as $key => $value) {
                 if (isset($value['enable'])) {
                     $myStatus[$key] = $value['enable'];
                 }
             }
         }
         return $myStatus;
     } elseif (!$_content) {
         $_content = \lib\router::get_repository_name();
         if ($_content !== "content") {
             $_content = substr($_content, strpos($_content, '_') + 1);
         }
     }
     if (!isset($suStatus[$_content]) || !isset($suStatus[$_content]['modules'])) {
         $su = false;
     }
     // if user want specefic location
     if ($_loc == 'all') {
         if ($su) {
             $myStatus = $suStatus[$_content]['modules'];
         } elseif (isset($_SESSION['permission'][$_content]['modules'])) {
             $myStatus = $_SESSION['permission'][$_content]['modules'];
         }
     } elseif ($_loc) {
         if ($_type) {
             if ($su) {
                 if (isset($suStatus[$_content]['modules'][$_loc][$_type])) {
                     $myStatus = $suStatus[$_content]['modules'][$_loc][$_type];
                 }
             } elseif (isset($_SESSION['permission'][$_content]['modules'][$_loc][$_type])) {
                 $myStatus = $_SESSION['permission'][$_content]['modules'][$_loc][$_type];
             }
         } else {
             if ($su) {
                 $myStatus = $suStatus[$_content]['modules'][$_loc];
             } elseif (isset($_SESSION['permission'][$_content]['modules'][$_loc])) {
                 $myStatus = $_SESSION['permission'][$_content]['modules'][$_loc];
             }
         }
     } else {
         if ($su) {
             $myStatus = $suStatus[$_content]['enable'];
         } elseif (isset($_SESSION['permission'][$_content]['enable'])) {
             $myStatus = $_SESSION['permission'][$_content]['enable'];
         }
     }
     if (!$myStatus) {
         if ($_block === "notify" && $_type && $_loc) {
             $msg = null;
             switch ($_type) {
                 case 'view':
                     $msg = "You can't view this part of system";
                     break;
                 case 'add':
                     $msg = T_("You can't add new") . ' ' . T_($_loc);
                     break;
                 case 'edit':
                     $msg = T_("You can't edit") . ' ' . T_($_loc);
                     break;
                 case 'delete':
                     $msg = T_("You can't delete") . ' ' . T_($_loc);
                     break;
                 default:
                     $msg = "You can't access to this part of system";
                     break;
             }
             $msg = $msg . "<br/> " . T_("Because of your permission");
             \lib\debug::error(T_($msg));
             // exit();
         } elseif ($_block) {
             \lib\error::access(T_("You can't access to this page!"));
         } else {
             // do nothing!
         }
     }
     return $myStatus;
 }
Ejemplo n.º 13
0
 public function delete($_qry = null, $_id = null, $_table = null)
 {
     // if user pass the qry use it else use our automatic creator
     // $myqry = $_qry? $_qry: null;
     if (!$_qry) {
         $tmp_table = $_table ? $_table : 'table' . ucfirst($this->module());
         $tmp_id = $_id ? $_id : $this->childparam('delete');
         $tmp_id = $tmp_id ? $tmp_id : \lib\utility::post('id');
         $_qry = $this->sql()->{$tmp_table}()->whereId($tmp_id);
         // var_dump($_qry);
     }
     if (!$_qry->select()->num()) {
         debug::error(T_("id does not exist!"));
         return false;
     }
     return $this->delete_commit($_qry);
 }
Ejemplo n.º 14
0
 /**
  * return
  * @param  string $_loc  location
  * @param  string $_type type of permission needed
  * @return [type]        [description]
  */
 public function access($_content = null, $_loc = null, $_type = null, $_block = null)
 {
     $myStatus = null;
     // if programmer not set content, give it automatically from address
     if ($_content === 'all') {
         $myStatus = [];
         if (isset($_SESSION['permission'])) {
             foreach ($_SESSION['permission'] as $key => $value) {
                 if (isset($value['enable'])) {
                     $myStatus[$key] = $value['enable'];
                 }
             }
         }
         return $myStatus;
     } elseif (!$_content) {
         $_content = router::get_repository_name();
         $_content = substr($_content, strpos($_content, '_') + 1);
     }
     // if user want specefic location
     if ($_loc == 'all') {
         if (isset($_SESSION['permission'][$_content]['modules'])) {
             $myStatus = $_SESSION['permission'][$_content]['modules'];
         }
     } elseif ($_loc) {
         if ($_type) {
             if (isset($_SESSION['permission'][$_content]['modules'][$_loc][$_type])) {
                 $myStatus = $_SESSION['permission'][$_content]['modules'][$_loc][$_type];
             }
         } else {
             if (isset($_SESSION['permission'][$_content]['modules'][$_loc])) {
                 $myStatus = $_SESSION['permission'][$_content]['modules'][$_loc];
             }
         }
     } else {
         if (isset($_SESSION['permission'][$_content]['enable'])) {
             $myStatus = $_SESSION['permission'][$_content]['enable'];
         }
     }
     if (!$myStatus) {
         if ($_block === "notify" && $_type && $_loc) {
             $msg = null;
             switch ($_type) {
                 case 'view':
                     $msg = "You can't view this part of system";
                     break;
                 case 'add':
                     $msg = T_("you can't add new") . ' ' . T_($_loc);
                     break;
                 case 'edit':
                     $msg = T_("you can't edit") . ' ' . T_($_loc);
                     break;
                 case 'delete':
                     $msg = T_("you can't delete") . ' ' . T_($_loc);
                     break;
                 default:
                     $msg = "you can't access to this part of system";
                     break;
             }
             $msg = $msg . "<br/>" . T_(" Because of your permission");
             \lib\debug::error(T_($msg));
             $this->model()->_processor(object(array("force_json" => true, "force_stop" => true)));
         } elseif ($_block) {
             \lib\error::access(T_("you can't access to this page!"));
         }
     }
     return $myStatus;
 }
Ejemplo n.º 15
0
 /**
  * this function set custom operator for each custom module in cp
  * @param  [type] $_id [description]
  * @return [type]      [description]
  */
 function cp_create_query($_id = null)
 {
     if (!$_id) {
         $_id = $this->childparam('edit');
     }
     // if don't set title return error
     if (!utility::post('title')) {
         debug::error(T_("Please enter title!"));
         return false;
     }
     // remove this line!
     $mymodule = $this->cpModule('raw');
     // set useful variables
     $datarow = array();
     $cpModule = $this->cpModule();
     $qry = $this->sql()->table('posts');
     // set all variable get form all type of forms
     $datarow['language'] = utility::post('language');
     $datarow['title'] = utility::post('title');
     $datarow['slug'] = utility::post('slug', 'filter');
     $datarow['content'] = utility::post('desc');
     $datarow['type'] = $cpModule['type'];
     $datarow['url'] = null;
     $datarow['status'] = utility::post('status');
     $datarow['parent'] = utility::post('parent');
     $datarow['user_id'] = $this->login('id');
     $datarow['publishdate'] = date('Y-m-d H:i:s');
     // read post meta and rewrite it
     $datarow['meta'] = $this->sql()->table('posts')->where('id', $_id)->select()->assoc('post_meta');
     $datarow['meta'] = json_decode($datarow['meta'], true);
     // meta fields
     $datarow['meta']['thumbid'] = utility::post('thumbid');
     $datarow['meta']['slug'] = $datarow['slug'];
     $datarow['meta'] = json_encode($datarow['meta']);
     // set slug if is not set
     if (!$datarow['slug']) {
         $datarow['slug'] = utility\Filter::slug($datarow['title']);
     }
     switch ($cpModule['raw']) {
         case 'pages':
         case 'books':
             // calc and set url
             if ($datarow['parent']) {
                 $datarow['url'] = $this->sql()->table('posts')->where('post_type', $cpModule['type'])->and('id', $datarow['parent'])->select()->assoc('post_url') . '/' . $datarow['slug'];
             } else {
                 $datarow['parent'] = '#NULL';
                 $datarow['url'] = $datarow['slug'];
             }
             if ($cpModule['raw'] === 'books') {
                 $datarow['url'] = 'book/' . preg_replace("#^(book\\/)+#", "", $datarow['url']);
             }
             break;
             // only on edit
         // only on edit
         case 'attachments':
             // remove unuse fields like slug, url, data, status, ...
             // commented row not deleted and check
             unset($datarow['language']);
             // unset($datarow['title']);
             // unset($datarow['slug']);
             // unset($datarow['content']);
             unset($datarow['type']);
             unset($datarow['url']);
             // unset($datarow['status']);
             unset($datarow['parent']);
             // unset($datarow['user_id']);
             unset($datarow['publishdate']);
             if (utility::post('cat')) {
                 $cat = utility::post('cat');
             } else {
                 $cat = 'file';
             }
             $datarow['url'] = $cat . '/' . $datarow['slug'];
             $datarow['url'] = trim($datarow['url'], '/');
             // // read post meta and rewrite it
             // $datarow['meta'] = $this->sql()->table('posts')
             // 		->where('post_type', 'attachment')->and('id', $_id)
             // 		->select()->assoc('post_meta');
             // $datarow['meta'] = json_decode($datarow['meta'], true);
             // $datarow['meta']['slug'] = $datarow['slug'];
             // $datarow['meta'] = json_encode($datarow['meta']);
             unset($datarow['slug']);
             // var_dump(utility::post('cat'));
             // var_dump($datarow['meta']);
             // exit();
             break;
         case 'socialnetwork':
             $datarow['slug'] = 'social' . md5(time());
             $datarow['url'] = 'social/' . $datarow['slug'];
             $datarow['status'] = 'draft';
             // print_r($datarow);
             // exit();
             break;
             // all other type of post
         // all other type of post
         default:
             unset($datarow['parent']);
             $datarow['url'] = utility::post('cat');
             // create url with selected cat
             if ($cpModule['raw'] === 'books') {
                 $datarow['url'] = 'books';
             } elseif (!$datarow['url']) {
                 // calc and set url
                 $datarow['url'] = $this->sql()->table('terms')->where('id', 1)->select()->assoc('term_url');
             }
             if ($datarow['url']) {
                 $datarow['url'] = $datarow['url'] . '/';
             }
             $datarow['url'] = $datarow['url'] . $datarow['slug'];
             break;
     }
     // if in edit get this record data
     if ($_id) {
         $record = $this->sql()->table('posts')->where('id', $_id)->select()->assoc();
         $record_meta = $this->sql()->table('options')->where('post_id', $_id)->order('id', 'asc')->select()->allassoc();
         // fill options value like posts field
         foreach ($record_meta as $key => $value) {
             $record[$record_meta[$key]['option_key']] = $record_meta[$key]['option_value'];
         }
     }
     $changed = false;
     // set values if exist
     foreach ($datarow as $key => $value) {
         $key = $key === 'user_id' ? 'user_id' : 'post_' . $key;
         if ($_id) {
             // check with old data and if change then set it
             if ($record[$key] !== $value) {
                 $qry = $qry->set($key, $value);
                 $changed = true;
             }
         } elseif ($value) {
             $qry = $qry->set($key, $value);
         }
     }
     $post_new_id = $_id;
     if ($_id) {
         // on edit
         if ($changed) {
             $qry = $qry->where('id', $_id)->update();
         }
     } else {
         // on add
         $qry = $qry->insert();
         $post_new_id = $qry->LAST_INSERT_ID();
     }
     if ($post_new_id === 0 || !$post_new_id) {
         return;
     }
     // if publish post share it on twitter and save in options
     // before share check db for share before
     // if on add or in edit and staus exist and status !== 400
     // then if status == publish and changed from old position
     $post_status = isset($record['post_status']) ? $record['post_status'] : null;
     $post_type = isset($record['post_type']) ? $record['post_type'] : null;
     $post_type = $post_type ? $post_type : $cpModule['type'];
     if ($datarow['status'] === 'publish' && $datarow['status'] !== $post_status && $post_type === 'post') {
         $url_main = $this->url('MainProtocol') . '://' . $this->url('MainSite');
         if (!(isset($record['twitter']['status']) && $record['twitter']['status'] === 400)) {
             $mytwitte = $datarow['title'] . ' ' . $url_main . '/' . $datarow['url'];
             $twitte_result = \lib\utility\SocialNetwork::twitter($mytwitte);
             if (isset($twitte_result) && isset($twitte_result['status'])) {
                 $twitte_result = json_encode($twitte_result);
                 $qry_twitter = $this->sql()->table('options')->set('post_id', $post_new_id)->set('option_cat', 'post' . $post_new_id . '_SocialNetwork')->set('option_key', 'twitter')->set('option_value', $twitte_result);
                 // $qry_twitter = $qry_twitter->insertString();
                 // var_dump($qry_twitter);
                 $qry_twitter = $qry_twitter->insert();
             }
         }
         $telegram = \lib\utility\SocialNetwork::telegram($datarow['title'] . "\n" . $url_main . '/' . $datarow['url']);
         $facebook_content = html_entity_decode($datarow['content']);
         $facebook_content = preg_replace("/<\\/p>/", "\n", $facebook_content);
         $facebook_content = preg_replace("/<[^>]+>/", "", $facebook_content);
         $facebook_content = preg_replace("/^[\\s\n\r\t]+/", "", $facebook_content);
         $facebook_url = $url_main . '/' . $datarow['url'];
         $result_fb = \lib\utility\SocialNetwork::facebook($facebook_url, $facebook_content);
         if (isset($result_fb)) {
             // $result_fb = json_encode($result_fb);
             $qry_facebook = $this->sql()->table('options')->set('post_id', $post_new_id)->set('option_cat', 'post' . $post_new_id . '_SocialNetwork')->set('option_key', 'facebook')->set('option_value', $result_fb);
             // $qry_facebook = $qry_facebook->insertString();
             $qry_facebook = $qry_facebook->insert();
         }
     }
     // add tags to terms table
     $mycats = utility::post('categories');
     // if(!$mycats)
     // 	$mycats = [1];
     $mytags = utility::post('tags');
     $mytags = explode(',', $mytags);
     foreach ($mytags as $key => $value) {
         $value = trim($value, " ");
         $value = trim($value, "'");
         if ($value) {
             $mytags[$key] = $value;
         } else {
             unset($mytags[$key]);
         }
     }
     // --------------------------------------------------- check new tag and cats with old one on edit
     if ($_id) {
         $myterms_del = null;
         // get old tags and diff of it with new one by title of tags
         $old_tags = $this->sp_term_list('tag', false);
         $tags_diff = array_diff($old_tags, $mytags);
         if (count($tags_diff) > 0) {
             // get the list of tags id
             $tags_id = $this->cp_tag_id($tags_diff);
             $myterms_del = $tags_id;
         }
         // get old cats and diff of it with new one by id
         if ($cpModule['raw'] === 'attachments') {
             $old_cats = $this->sp_term_list('filecat', false);
             if (!is_array($mycats)) {
                 $mycats = null;
             }
         } elseif ($cpModule['raw'] === 'books') {
             $old_cats = $this->sp_term_list('bookcat', false);
             if (!is_array($mycats)) {
                 $mycats = null;
             }
         } else {
             $old_cats = $this->sp_term_list('cat', false);
             if (!is_array($mycats)) {
                 $mycats = [1];
             }
         }
         if (is_array($old_cats) && count($old_cats) && is_array($mycats) && count($mycats)) {
             $cats_diff = array_diff($old_cats, $mycats);
         } elseif (is_array($mycats) && count($mycats)) {
             $cats_diff = $mycats;
         } else {
             $cats_diff = $old_cats;
         }
         if (is_array($cats_diff) && count($cats_diff) > 0) {
             $cats_diff = implode(",", $cats_diff);
             if ($myterms_del) {
                 $myterms_del .= ',';
             }
             $myterms_del .= $cats_diff;
         }
         // var_dump($myterms_del);
         // exit();
         // delete deleted tags and cats together in one query
         if ($myterms_del) {
             $qry_term_del = $this->sql()->table('termusages')->where('termusage_id', $post_new_id);
             if (count(explode(',', $myterms_del)) === 1) {
                 $qry_term_del = $qry_term_del->and('term_id', '=', $myterms_del)->delete();
             } else {
                 $qry_term_del = $qry_term_del->and('term_id', 'in', "(" . $myterms_del . ")")->delete();
             }
         }
     }
     // ------------------------------------------------- if user enter new tag
     $tags_id = array();
     if (count($mytags) > 0) {
         $qry_tag = $this->sql()->table('terms');
         // add each tag to sql syntax
         foreach ($mytags as $value) {
             if ($value) {
                 $qry_tag = $qry_tag->set('term_type', 'tag')->set('term_title', $value)->set('term_slug', $value)->set('term_url', $value);
             }
         }
         // var_dump($qry_tag->insertString('IGNORE'));exit();
         $qry_tag->insert('IGNORE');
         // get the list of tags id
         $tags_id = $this->cp_tag_id($mytags, false);
         // var_dump($tags_id);
         if (!is_array($tags_id)) {
             $tags_id = array();
         }
     }
     // add selected tag to term usages table
     // on pages dont need cats and only add tags
     if ($mymodule === 'pages') {
         $myterms = $tags_id;
     } elseif (is_array($mycats) && count($mycats)) {
         $myterms = array_merge($tags_id, $mycats);
     } else {
         $myterms = $tags_id;
     }
     // ---------------------------------------------- set termusage table
     // if terms exist go to foreach
     if (isset($myterms) && count($myterms) > 0) {
         $qry_tagusages = $this->sql()->table('termusages');
         foreach ($myterms as $value) {
             $qry_tagusages = $qry_tagusages->set('term_id', $value)->set('termusage_id', $post_new_id)->set('termusage_foreign', 'posts');
         }
         // var_dump($qry_tagusages->insertString());exit();
         $qry_tagusages->insert('IGNORE');
     }
     // update post url
     // $post_url = utility::post('slug', 'filter');
     // $this->sql()->table('posts')->set('post_url', $post_url)
     // ->where('id', $post_new_id)->update();
     // ======================================================
     // you can manage next event with one of these variables,
     // commit for successfull and rollback for failed
     // if query run without error means commit
     if ($cpModule['raw'] == 'socialnetwork') {
         $twitte_result = \lib\utility\SocialNetwork::telegram($datarow['content']);
     }
     $this->commit(function ($_module, $_postId, $_edit = null) {
         if ($_edit) {
             debug::true(T_("Update Successfully"));
             $this->redirector()->set_url($_module . '/edit=' . $_postId);
         } else {
             debug::true(T_("Insert Successfully"));
             $this->redirector()->set_url($_module . '/edit=' . $_postId);
         }
     }, $mymodule, $post_new_id, $_id);
     // if a query has error or any error occour in any part of codes, run roolback
     $this->rollback(function () {
         debug::title(T_("Transaction error") . ': ');
     });
 }