123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856 |
- <?php
-
- use core\basic\Config;
- use core\basic\Json;
- use core\view\View;
- use core\view\Paging;
- use core\basic\Response;
- use core\basic\Url;
- use core\basic\Basic;
- use core\basic\Smtp;
-
-
- function url($url, $suffix = false)
- {
- return Url::get($url, $suffix);
- }
-
-
- function homeurl($url, $suffix = null, $qs = null)
- {
- return Url::home($url, $suffix, $qs);
- }
-
-
- function error($string, $jump_url = null, $time = 2)
- {
- @ob_clean();
- http_response_code(404);
- if (! $string)
- $string = '未知错误!';
-
- if ($jump_url == '-1' && isset($_SERVER['HTTP_REFERER'])) {
- $jump_url = $_SERVER['HTTP_REFERER'];
- if (strpos($jump_url, get_http_url()) !== 0) {
- $jump_url = '/';
- }
- } elseif ($jump_url == '-1') {
- $jump_url = null;
- }
- if (Config::get('return_data_type') == 'json' || is_ajax()) {
- Response::json(0, strip_tags($string), $jump_url);
- } else {
- $err_tpl = CORE_PATH . '/template/error.html';
- echo parse_info_tpl($err_tpl, $string, $jump_url, $time);
- }
- exit();
- }
-
-
- function success($string, $jump_url = null, $time = 2)
- {
- if ($jump_url == '-1' && isset($_SERVER['HTTP_REFERER'])) {
- $jump_url = $_SERVER['HTTP_REFERER'];
- if (strpos($jump_url, get_http_url()) !== 0) {
- $jump_url = '/';
- }
- } elseif ($jump_url == '-1') {
- $jump_url = null;
- }
- if (Config::get('return_data_type') == 'json' || is_ajax()) {
- Response::json(1, strip_tags($string), $jump_url);
- } else {
- $err_tpl = CORE_PATH . '/template/success.html';
- echo parse_info_tpl($err_tpl, $string, $jump_url, $time);
- }
- exit();
- }
-
-
- function alert($info, $status = 0)
- {
- if (Config::get('return_data_type') == 'json' || is_ajax()) {
- Response::json($status, strip_tags($info));
- } else {
- echo '<script type="text/javascript">alert("' . clear_html_blank($info) . '");</script>';
- }
- }
-
-
- function alert_back($info, $status = 0)
- {
- if (Config::get('return_data_type') == 'json' || is_ajax()) {
- Response::json($status, strip_tags($info));
- } else {
- echo '<script type="text/javascript">alert("' . clear_html_blank($info) . '");window.history.go(-1);</script>';
- exit();
- }
- }
-
-
- function location($url)
- {
- if ($url == '-1' && isset($_SERVER['HTTP_REFERER'])) {
- $url = $_SERVER['HTTP_REFERER'];
- if (strpos($url, get_http_url()) !== 0) {
- $url = '/';
- }
- }
- header('Location:' . $url);
- exit();
- }
-
-
- function alert_location($info, $url, $status = 0)
- {
- if ($url == '-1' && isset($_SERVER['HTTP_REFERER'])) {
- $url = $_SERVER['HTTP_REFERER'];
- if (strpos($url, get_http_url()) !== 0) {
- $url = '/';
- }
- }
- if (Config::get('return_data_type') == 'json' || is_ajax()) {
- Response::json($status, strip_tags($info), $url);
- } else {
- echo '<script type="text/javascript">alert("' . clear_html_blank($info) . '");location.href="' . $url . '";</script>';
- exit();
- }
- }
-
-
- function alert_close($info, $status = 0)
- {
- if (Config::get('return_data_type') == 'json' || is_ajax()) {
- Response::json($status, strip_tags($info));
- } else {
- echo '<script type="text/javascript">alert("' . clear_html_blank($info) . '");window.close();</script>';
- exit();
- }
- }
-
-
- function model($name = null, $new = false)
- {
- return Basic::createModel($name, $new);
- }
-
-
- function api($args = null)
- {
- return Basic::createApi(func_get_args());
- }
-
-
- function display($tpl)
- {
- $view = View::getInstance();
- echo $view->parser($tpl);
- }
-
-
- function parser($tpl)
- {
- $view = View::getInstance();
- return $view->parser($tpl);
- }
-
-
- function set_theme($theme_name)
- {
- $view = View::getInstance();
- $view->assign('theme', $theme_name);
- }
-
-
- function assign($var, $value)
- {
- $view = View::getInstance();
- $view->assign($var, $value);
- }
-
-
- function get_var($var)
- {
- $view = View::getInstance();
- return $view->getVar($var);
- }
-
-
- function page($tatal, $morePageStr = false)
- {
- $page = Paging::getInstance();
- return $page->limit($tatal, $morePageStr);
- }
-
-
- function response($data)
- {
- return core\basic\Response::handle($data);
- }
-
-
- function json($code, $data, $tourl = null)
- {
- return core\basic\Response::json($code, $data, $tourl);
- }
-
-
- function filter($varname, $condition)
- {
-
- if (array_key_exists($varname, $condition) && $condition[$varname]) {
- $vartext = $condition[$varname];
- } else {
- $vartext = $varname;
- }
-
-
- if (array_key_exists('d_source', $condition)) {
- switch ($condition['d_source']) {
- case 'post':
- $data = @$_POST[$varname];
- break;
- case 'get':
- $data = @$_GET[$varname];
- break;
- case 'cookie':
- $data = @$_COOKIE[$varname];
- break;
- case 'session':
- $data = session($varname);
- break;
- case 'both':
- $data = @$_POST[$varname] ?: @$_GET[$varname];
- break;
- case 'string':
- $data = $varname;
- default:
- error($vartext . '数据获取方式设置错误!');
- }
-
- if (is_string($data))
- $data = trim($data);
- } else {
- $data = $varname;
- }
-
-
- if (! $data && array_key_exists('d_none', $condition) && $condition['d_none'] === false) {
- error($vartext . '不能为空!');
- }
-
-
- if (array_key_exists('d_require', $condition) && $condition['d_require'] == true) {
- $require = true;
- } else {
- $require = false;
- }
-
-
- if (array_key_exists('d_type', $condition)) {
- switch ($condition['d_type']) {
- case 'int':
- if (! preg_match('/^[0-9]+$/', $data)) {
- $err = '必须为整数!';
- }
- break;
- case 'float':
- if (! is_float($data)) {
- $err = '必须为浮点数!';
- }
- break;
- case 'num':
- if (! is_numeric($data)) {
- $err = '必须为数字!';
- }
- break;
- case 'letter':
- if (! preg_match('/^[a-zA-Z]+$/', $data)) {
- $err = '只能包含字母!';
- }
- break;
- case 'var':
- if (! preg_match('/^[\w\-\.]+$/', $data)) {
- $err = '只能包含字母、数字、划线、点!';
- }
- break;
- case 'bool':
- if (! is_bool($data)) {
- $err = '必须为布尔类型!';
- }
- break;
- case 'date':
- if (! strtotime($data)) {
- $err = '必须为日期类型!';
- }
- break;
- case 'array':
- if (! is_array($data)) {
- $err = '必须为数组类型!';
- }
- break;
- case 'object':
- if (! is_object($data)) {
- $err = '必须为对象类型!';
- }
- break;
- case 'vars':
- if (! preg_match('/^[\x{4e00}-\x{9fa5}\w\-\.,\s]+$/u', $data)) {
- $err = '只能包含中文、字母、数字、横线、点、逗号、空格!';
- }
- break;
- default:
- if ($condition['d_type'])
- error($vartext . '数据类型设置错误!');
- }
- }
-
-
- if ((! $require || ($require && ! isset($err)))) {
-
-
- if (array_key_exists('d_regular', $condition)) {
- if (! preg_match($condition['d_regular'], $data)) {
- $err = '不符合正则表达式规则!';
- }
- }
-
- if (array_key_exists('d_max', $condition)) {
- if (is_numeric($data)) {
- if ($data > $condition['d_max']) {
- $err = '不能大于' . $condition['d_max'];
- }
- } else {
- if (mb_strlen($data) > $condition['d_max']) {
- $err = '长度不能大于' . $condition['d_max'];
- }
- }
- }
-
- if (array_key_exists('d_min', $condition)) {
- if (is_numeric($data)) {
- if ($data < $condition['d_min']) {
- $err = '不能小于' . $condition['d_min'];
- }
- } else {
- if (mb_strlen($data) < $condition['d_min']) {
- $err = '长度不能小于' . $condition['d_min'];
- }
- }
- }
- }
-
-
- if ($require && isset($err)) {
- error($vartext . $err);
- } elseif (isset($err)) {
- $data = null;
- }
-
-
- if (array_key_exists('d_default', $condition)) {
- $data = (! is_null($data)) ? $data : $condition['d_default'];
- }
-
- if (is_string($data)) {
- $data = trim($data);
- $data = preg_replace_r('/(x3c)|(x3e)/', '', $data);
- $data = preg_replace_r('/pboot:if/i', 'pboot@if', $data);
- $data = preg_replace_r('/pboot:sql/i', 'pboot@sql', $data);
- $data = preg_replace_r('/GET\[/i', 'GET@[', $data);
- $data = preg_replace_r('/POST\[/i', 'POST@[', $data);
- }
-
-
- unset($err);
-
-
- return escape_string($data);
- }
-
-
- function get($name, $type = null, $require = false, $vartext = null, $default = null)
- {
- $condition = array(
- 'd_source' => 'get',
- 'd_type' => $type,
- 'd_require' => $require,
- $name => $vartext,
- 'd_default' => $default
-
- );
- return filter($name, $condition);
- }
-
-
- function post($name, $type = null, $require = false, $vartext = null, $default = null)
- {
- $condition = array(
- 'd_source' => 'post',
- 'd_type' => $type,
- 'd_require' => $require,
- $name => $vartext,
- 'd_default' => $default
-
- );
- return filter($name, $condition);
- }
-
-
- function request($name, $type = null, $require = false, $vartext = null, $default = null)
- {
- if (isset($_POST[$name])) {
- $d_source = 'post';
- } else {
- $d_source = 'get';
- }
- $condition = array(
- 'd_source' => $d_source,
- 'd_type' => $type,
- 'd_require' => $require,
- $name => $vartext,
- 'd_default' => $default
-
- );
- return filter($name, $condition);
- }
-
-
- function cookie($name, $value = null, $expire = null, $path = null, $domain = null, $secure = null, $httponly = false)
- {
- if (! is_null($value)) {
- $path = SITE_DIR . '/';
- if (is_string($value))
- $value = trim($value);
-
- $_COOKIE[$name] = $value;
- if (! is_null($expire)) {
- return setcookie($name, $value, time() + $expire, $path, $domain, $secure, $httponly);
- } else {
- return setcookie($name, $value, 0, $path, $domain, $secure, $httponly);
- }
- } else {
- if (isset($_COOKIE[$name])) {
- return escape_string($_COOKIE[$name]);
- } else {
- return null;
- }
- }
- }
-
-
- function session($name, $value = null)
- {
- if (! isset($_SESSION)) {
- session_start();
- }
-
- if (! is_null($value)) {
- if (isset($_SESSION[$name])) {
- if ($_SESSION[$name] != $value) {
- $_SESSION[$name] = $value;
- }
- } else {
- $_SESSION[$name] = $value;
- }
- return $value;
- } else {
- if (strpos($name, '.')) {
- if (isset($_SESSION[$name])) {
- return $_SESSION[$name];
- }
- $names = explode('.', $name);
- if (! isset($_SESSION[$names[0]])) {
- return null;
- }
- $var = $_SESSION[$names[0]];
- $len = count($names);
- for ($i = 1; $i < $len; $i ++) {
- if (is_array($var)) {
- if (isset($var[$names[$i]])) {
- $var = $var[$names[$i]];
- } else {
- return null;
- }
- } elseif (is_object($var)) {
- if (isset($var->{$names[$i]})) {
- $var = $var->{$names[$i]};
- } else {
- return null;
- }
- } else {
- break;
- }
- }
- return $var;
- } else {
- if (isset($_SESSION[$name])) {
- return $_SESSION[$name];
- } else {
- return null;
- }
- }
- }
- }
-
-
- function issetSession($name)
- {
- if (! isset($_SESSION)) {
- session_start();
- }
- return isset($_SESSION[$name]);
- }
-
-
- function sendmail(array $config, $to, $subject, $body)
- {
- $smtp = new Smtp($config['smtp_server'], $config['smtp_username'], $config['smtp_password'], $config['smtp_port'], $config['smtp_ssl']);
- if ($smtp->sendMail($to, $subject, $body)) {
- return true;
- } else {
- return $smtp->error();
- }
- }
-
-
- function sendsms(array $config, $to, $content)
- {
- if (! $to || ! $content) {
- return false;
- }
-
- if (! isset($config['sms_account']) || ! isset($config['sms_pwd']) || ! isset($config['sms_signid'])) {
- alert_back('短信发送参数配置有误');
- }
-
- $data['Account'] = $config['sms_account'];
- $data['Pwd'] = $config['sms_pwd'];
- $data['SignId'] = $config['sms_signid'];
- $data['Content'] = $content;
- $to = str_replace("\r\n", ",", $to);
- $to = str_replace(",", ",", $to);
- $data['Mobile'] = str_replace(" ", "", $to);
-
- $url = "http://api.feige.ee/SmsService/Send";
- if (! ! $res = get_url($url, $data)) {
- $result = json_decode($res);
- if (! ($result->Code === 0)) {
- error('短信发送失败,' . $result->Message);
- } else {
- return true;
- }
- }
- }
-
-
- function get_sms_balance(array $config)
- {
- $data['Account'] = $config['sms_account'];
- $data['Pwd'] = $config['sms_pwd'];
- $url = "http://api.feige.ee/Account/Balance";
- if (! ! $res = get_url($url, $data)) {
- $result = json_decode($res);
- if (! ($result->Code === 0)) {
- error('查询失败,' . $result->Message);
- } else {
- return $result->Balance;
- }
- }
- }
-
-
- function _404($string, $jump_url = null, $time = 2)
- {
- http_response_code(404);
- $file_404 = ROOT_PATH . '/404.html';
- if (file_exists($file_404)) {
- echo parse_info_tpl($file_404, $string, $jump_url, $time);
- exit();
- } else {
- error($string, $jump_url, $time);
- }
- }
-
- function _301(){
- $target_url = get_http_url();
- header("HTTP/1.1 301 Moved Permanently");
- header("Location: $target_url");
- exit;
- }
-
-
- function toArray($obj){
- if($obj === null){
- return [];
- }else{
- return json_decode(json_encode($obj),true);
- }
- }
-
-
- function symbol($matches): string
- {
- $flag = '';
- $symbol1 = ['&&','||'];
- foreach ($symbol1 as $items) {
- if (strpos($matches, $items) !== false) {
- $arr = explode($items, $matches);
- switch ($items) {
- case '&&':
- $bool1 = compareSymbol1($arr[0]);
- $bool2 = compareSymbol1($arr[1]);
- $flag = $bool1 && $bool2 ? 'if' : 'else';
- break;
- case '||':
- $bool1 = compareSymbol1($arr[0]);
- $bool2 = compareSymbol1($arr[1]);
-
- $flag = $bool1 || $bool2 ? 'if' : 'else';
- break;
- }
- break;
- }
- }
- if(!$flag){
- $compare = compareSymbol1($matches);
- if($compare === true){
- $flag = 'if';
- }else if($compare === false){
- $flag = 'else';
- }
- }
- return $flag;
- }
-
- function compareSymbol1($str){
- $bool = null;
- $symbol = ['>=','<=','!=','==','>','<'];
- foreach ($symbol as $items) {
- if (strpos($str, $items) !== false) {
- $arr = explode($items, $str);
- switch ($items) {
- case '>':
- $res1 = compareSymbol2($arr[0]);
- $res2 = compareSymbol2($arr[1]);
- $bool = $res1 > $res2;
- break;
- case '>=':
- $res1 = compareSymbol2($arr[0]);
- $res2 = compareSymbol2($arr[1]);
- $bool = $res1 >= $res2;
- break;
- case '!=':
- $res1 = compareSymbol2($arr[0]);
- $res2 = compareSymbol2($arr[1]);
- $bool = $res1 != $res2;
- break;
- case '==':
- $res1 = compareSymbol2($arr[0]);
- $res2 = compareSymbol2($arr[1]);
- $bool = $res1 == $res2;
- break;
- case '<=':
- $res1 = compareSymbol2($arr[0]);
- $res2 = compareSymbol2($arr[1]);
- $bool = $res1 <= $res2;
- break;
- case '<':
- $res1 = compareSymbol2($arr[0]);
- $res2 = compareSymbol2($arr[1]);
- $bool = $res1 < $res2;
- break;
- }
- break;
- }
- }
- if($bool === null){
- $res = compareSymbol2($str);
- if (trim($res)) {
- return true;
- } else {
- return false;
- }
- }
- return $bool;
- }
-
- function compareSymbol2($str){
- $res = null;
- $symbol2 = ['%'];
- foreach ($symbol2 as $items) {
- if (strpos($str, $items) !== false) {
- $arr = explode($items, $str);
- if ($items == '%') {
- $res = $arr[0] % $arr[1];
- }
- break;
- }
- }
- if($res === null) {
- $str = trim($str);
- $str = trim($str,"'");
- $res = (string)$str;
- }
- return $res;
- }
|