/** * 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!")); } }
/** * 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") . ': '); }); }
/** * insert mulit tag * * @param <type> $_tags strign of tags * * @return <type> ( description_of_the_return_value ) */ public static function insert_multi($_tags) { //split tags $tags = preg_split("/\\,/", $_tags); // trim all value foreach ($tags as $key => $value) { $tags[$key] = trim($value); } // remove empty tags $tags = array_filter($tags); if (empty($tags)) { return null; } $result = []; foreach ($tags as $key => $value) { $result[] = ['term_type' => 'tag', 'term_title' => $value, 'term_url' => $value, 'term_slug' => \lib\utility\filter::slug($value)]; } return \lib\db\terms::insert_multi($result); }
public function query_search($_parameter = array()) { $search = array_key_exists('search', $_parameter) ? $_parameter['search'] : null; $image = array_key_exists('image', $_parameter) ? $_parameter['image'] : null; $video = array_key_exists('video', $_parameter) ? $_parameter['video'] : null; $audio = array_key_exists('audio', $_parameter) ? $_parameter['audio'] : null; $other = array_key_exists('other', $_parameter) ? $_parameter['other'] : null; $where = ''; if ($search) { $where .= "(post_title LIKE '%{$search}%' OR post_content LIKE '%{$search}%')"; } $_type = ['image', 'audio', 'video']; $type = array(); if ($image) { array_push($type, 'image'); } if ($video) { array_push($type, 'video'); } if ($audio) { array_push($type, 'audio'); } if ($other) { array_push($type, 'other'); } if (count($type) > 0 && count($type) < 4) { $where .= empty($where) ? '' : " AND "; if ($other) { if (count($type) == 1) { $_type = join("\"' ,'\"", $_type); $where .= "json_extract(post_meta, '\$.type') NOT IN ('\"{$_type}\"')"; } else { $_type = join("\"' ,'\"", array_diff($_type, $type)); $type = count($type) > 1 ? "\"" . join("\"' ,'\"", $type) . "\"" : $type[0]; $where .= "(json_extract(post_meta, '\$.type') IN ('{$type}')"; $where .= " OR json_extract(post_meta, '\$.type') NOT IN ('\"{$_type}\"'))"; } } else { $type = count($type) > 1 ? "\"" . join("\"' ,'\"", $type) . "\"" : $type[0]; $where .= "json_extract(post_meta, '\$.type') in ('{$type}')"; } } $where .= empty($where) ? '' : " AND "; $where .= "post_type = 'attachment'"; $length = 5; $start = 0; if ($_parameter['pagnation']) { list($start, $length) = $this->controller->pagnation_make_limit($length); } $query = "SELECT SQL_CALC_FOUND_ROWS posts.*, FOUND_ROWS() FROM posts WHERE {$where} LIMIT {$start}, {$length}"; $result = \lib\db::query($query); $query_rows = "SELECT FOUND_ROWS() as rows"; $result_rows = \lib\db::query($query_rows); $rows = $result_rows->fetch_assoc()['rows']; if ($_parameter['pagnation']) { $this->controller->pagnation_make($rows); $pagnation = $this->controller->pagnation; } else { $pagnation['total_pages'] = intval(ceil($rows / $length)); $pagnation['current'] = 1; $pagnation['next'] = $pagnation['current'] + 1 <= $pagnation['total_pages'] ? $pagnation['current'] + 1 : false; $pagnation['prev'] = $pagnation['current'] - 1 >= 1 ? $pagnation['current'] - 1 : false; $pagnation['count_link'] = 7; $pagnation['current_url'] = \lib\router::get_class() . '/attachments_data'; $pagnation['length'] = $length; } $decode_result = \lib\utility\filter::meta_decode(\lib\db::fetch_all($result)); return ['data' => $decode_result, 'pagnation' => $pagnation]; }
/** * 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; }
public static function get($_args) { if (empty($_args) || !is_array($_args)) { return false; } if (isset($_args['limit'])) { $limit = "LIMIT " . $_args['limit']; unset($_args['limit']); } else { $limit = null; } $where = []; foreach ($_args as $key => $value) { $where[] = "`{$key}` = '{$value}'"; } $where = "WHERE " . join($where, " AND "); $query = "\n\t\t\tSELECT\n\t\t\t\tid,\n\t\t\t\tuser_id AS 'user_id',\n\t\t\t\toption_cat AS 'cat',\n\t\t\t\toption_key AS 'key',\n\t\t\t\toption_value AS 'value',\n\t\t\t\toption_meta AS 'meta',\n\t\t\t\toption_status AS 'status'\n\t\t\tFROM\n\t\t\t\toptions\n\t\t\t{$where}\n\t\t\t{$limit}\n\t\t"; return \lib\utility\filter::meta_decode(self::select($query, "get")); }
/** * Check for invalid upload process * @param string self::$fieldName [description] * @return [type] [description] */ public static function invalid($_name = 'upfile', $_maxSize = null) { self::$fieldName = $_name; try { // Undefined | Multiple Files | $_FILES Corruption Attack // If this request falls under any of them, treat it invalid. if (!isset($_FILES[self::$fieldName]['error']) || is_array($_FILES[self::$fieldName]['error'])) { throw new \RuntimeException(T_('Invalid parameters')); } // Check $_FILES[self::$fieldName]['error'] value. switch ($_FILES[self::$fieldName]['error']) { case UPLOAD_ERR_OK: break; case UPLOAD_ERR_NO_FILE: throw new \RuntimeException(T_('No file sent')); case UPLOAD_ERR_INI_SIZE: case UPLOAD_ERR_FORM_SIZE: throw new \RuntimeException(T_('Exceeded filesize limit')); default: throw new \RuntimeException(T_('Unknown errors')); } $fileInfo = pathinfo($_FILES[self::$fieldName]['name']); self::$fileName = $fileInfo['filename']; self::$fileExt = strtolower($fileInfo['extension']); $extCheck = self::extCheck(self::$fileExt); self::$fileType = $extCheck['type']; self::$fileMime = $extCheck['mime']; self::$fileDisallow = $extCheck['disallow']; if (!$_maxSize) { $_maxSize = self::max_file_upload_in_bytes(true); } // Check filesize here. self::$fileSize = $_FILES[self::$fieldName]['size']; if (self::$fileSize > $_maxSize) { throw new \RuntimeException(T_('Exceeded filesize limit')); } //check file extention with allowed extention list // set file data like name, ext, mime // file with long name does not allowed in our system if (strlen(self::$fileName) > 200 || strpos(self::$fileName, 'htaccess') !== false) { throw new \RuntimeException(T_('Exceeded file name limit')); } // file with long extension does not allowed in our system if (strlen(self::$fileExt) > 10 || self::$fileDisallow) { throw new \RuntimeException(T_('Exceeded file extension limit')); } self::$fileFullName = \lib\utility\filter::slug(self::$fileName) . '.' . self::$fileExt; self::$fileMd5 = md5_file($_FILES[self::$fieldName]['tmp_name']); if (is_array(self::$extentions) && !in_array(self::$fileExt, self::$extentions)) { throw new \RuntimeException(T_("We don't support this type of file")); } // DO NOT TRUST $_FILES[self::$fieldName]['mime'] VALUE !! // Check MIME Type by yourself. // Alternative check if (function_exists('finfo')) { $finfo = new finfo(FILEINFO_MIME_TYPE); // var_dump($finfo); // if (false === $ext = array_search( $finfo->file($_FILES[self::$fieldName]['tmp_name']), self::$extentions ), true )) // { // throw new \RuntimeException(T_('Invalid file format.')); // } self::$fileMime = mime_content_type($fileInfo['basename']); } // it is not invalid, that's mean it's a valid upload return false; } catch (\RuntimeException $e) { return $e->getMessage(); } }
/** * try the sarshomar * generate mobile and password and register the Guset Session */ public static function signup_inspection() { $displayname = "Guest Session"; $mobile = \lib\utility\filter::temp_mobile(); $password = \lib\utility\filter::temp_password(); $user_id = self::signup($mobile, $password, true, $displayname); return $user_id; }
public static function get_post_meta($_post_id) { $query = "\n\t\t\tSELECT\n\t\t\t\t*\n\t\t\tFROM\n\t\t\t\toptions\n\t\t\tWHERE\n\t\t\t\toptions.post_id = {$_post_id} AND\n\t\t\t\toptions.option_cat = 'poll_{$_post_id}'\n\t\t"; $result = \lib\db\options::select($query, "get"); return \lib\utility\filter::meta_decode($result); }
/** * this function set custom operator for each custom module in cp * @param [type] $_id [description] * @return [type] [description] */ function cp_create_query($_id = null, $_data = null) { if (!$_id) { $_id = $this->childparam('edit'); } // set useful variables $cpModule = $this->cpModule(); $qry = $this->sql()->table('posts'); $datarow = null; $defaultCat = null; // if datarow is not sending from parameter give it form post if (!(is_array($_data) && $_data)) { $datarow = self::cp_getPosts($_id); $defaultCat = utility::post('cat'); } else { // if default cat isset then if (isset($_data['defaultCat'])) { $defaultCat = $_data['defaultCat']; unset($_data['defaultCat']); } $datarow = $_data; } // if don't set title return error if (!(isset($datarow['title']) && $datarow['title'])) { debug::warn(T_("Please enter title") . "!", 'title'); return false; } // set slug if is not set if (!$datarow['slug']) { $datarow['slug'] = utility\filter::slug($datarow['title']); } // fix post language $datarow['language'] = substr($datarow['language'], 0, 2); // start generate post url $url_slug = $datarow['slug']; $url_body = null; $url_prefix = null; switch ($cpModule['raw']) { case 'pages': case 'books': // calc and set url if ($datarow['parent']) { $url_body = $this->sql()->table('posts')->where('post_type', $cpModule['type'])->and('id', $datarow['parent'])->select()->assoc('post_url'); } else { $datarow['parent'] = '#NULL'; } if ($cpModule['raw'] === 'books') { $url_prefix = 'book/'; } 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 (!$defaultCat) { $defaultCat = 'file'; } $url_body = $defaultCat; // // 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']); break; case 'socialnetwork': $datarow['slug'] = 'social' . md5(time()); $url_slug = $datarow['slug']; $url_prefix = 'social/'; $datarow['status'] = 'draft'; break; // all other type of post // all other type of post default: unset($datarow['parent']); case 'polls': $url_body = $defaultCat; if (!$url_body) { // calc and set url $url_body = $this->sql()->table('terms')->where('id', 1)->select()->assoc('term_url'); } if ($cpModule['raw'] === 'polls') { $datarow['type'] = 'poll_sarshomar'; } break; } // generate posturl $datarow['url'] = self::sp_generateUrl($url_slug, $url_body, $url_prefix); // 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(); // $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; } // 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); } } $qry_tag->insert('IGNORE'); // get the list of tags id $tags_id = $this->cp_tag_id($mytags, false); 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 ($cpModule['raw'] === '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'); } $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 ($_postId, $_edit = null) { $_module = $this->cpModule('raw'); // if we are on create poll add into options table if ($_module === 'polls') { self::sp_savePoll($_postId); } 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); } }, $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") . ': '); }); }