心理咨询网
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

helper.php 24KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856
  1. <?php
  2. /**
  3. * @copyright (C)2016-2099 Hnaoyun Inc.
  4. * @author XingMeng
  5. * @email hnxsh@foxmail.com
  6. * @date 2017年11月5日
  7. * 助手函数
  8. */
  9. use core\basic\Config;
  10. use core\basic\Json;
  11. use core\view\View;
  12. use core\view\Paging;
  13. use core\basic\Response;
  14. use core\basic\Url;
  15. use core\basic\Basic;
  16. use core\basic\Smtp;
  17. /**
  18. * 生成实际跳转路径
  19. *
  20. * @param string $url
  21. * 接收控制器方法访问完整路径,如:/home/index/index
  22. * @return mixed
  23. */
  24. function url($url, $suffix = false)
  25. {
  26. return Url::get($url, $suffix);
  27. }
  28. /**
  29. * 生成前端路径
  30. *
  31. * @param string $url
  32. * 前端地址参数
  33. * @return mixed
  34. */
  35. function homeurl($url, $suffix = null, $qs = null)
  36. {
  37. return Url::home($url, $suffix, $qs);
  38. }
  39. /**
  40. * 自定义错误页面
  41. *
  42. * @param string $_string内容
  43. * @param string $_url跳转地址
  44. * @param number $_time时间
  45. */
  46. function error($string, $jump_url = null, $time = 2)
  47. {
  48. @ob_clean();
  49. http_response_code(404);
  50. if (! $string)
  51. $string = '未知错误!';
  52. if ($jump_url == '-1' && isset($_SERVER['HTTP_REFERER'])) {
  53. $jump_url = $_SERVER['HTTP_REFERER'];
  54. if (strpos($jump_url, get_http_url()) !== 0) {
  55. $jump_url = '/';
  56. }
  57. } elseif ($jump_url == '-1') {
  58. $jump_url = null;
  59. }
  60. if (Config::get('return_data_type') == 'json' || is_ajax()) { // 接口模型返回格式数据
  61. Response::json(0, strip_tags($string), $jump_url);
  62. } else {
  63. $err_tpl = CORE_PATH . '/template/error.html';
  64. echo parse_info_tpl($err_tpl, $string, $jump_url, $time);
  65. }
  66. exit();
  67. }
  68. /**
  69. * 自定义错误页面
  70. *
  71. * @param string $_string内容
  72. * @param string $_url跳转地址
  73. * @param number $_time时间
  74. */
  75. function success($string, $jump_url = null, $time = 2)
  76. {
  77. if ($jump_url == '-1' && isset($_SERVER['HTTP_REFERER'])) {
  78. $jump_url = $_SERVER['HTTP_REFERER'];
  79. if (strpos($jump_url, get_http_url()) !== 0) {
  80. $jump_url = '/';
  81. }
  82. } elseif ($jump_url == '-1') {
  83. $jump_url = null;
  84. }
  85. if (Config::get('return_data_type') == 'json' || is_ajax()) { // 接口模型返回格式数据
  86. Response::json(1, strip_tags($string), $jump_url);
  87. } else {
  88. $err_tpl = CORE_PATH . '/template/success.html';
  89. echo parse_info_tpl($err_tpl, $string, $jump_url, $time);
  90. }
  91. exit();
  92. }
  93. /**
  94. * 弹窗
  95. *
  96. * @param string $info信息
  97. */
  98. function alert($info, $status = 0)
  99. {
  100. if (Config::get('return_data_type') == 'json' || is_ajax()) { // 接口模型返回格式数据
  101. Response::json($status, strip_tags($info));
  102. } else {
  103. echo '<script type="text/javascript">alert("' . clear_html_blank($info) . '");</script>';
  104. }
  105. }
  106. /**
  107. * 弹窗并返回前页
  108. *
  109. * @param string $info信息
  110. */
  111. function alert_back($info, $status = 0)
  112. {
  113. if (Config::get('return_data_type') == 'json' || is_ajax()) { // 接口模型返回格式数据
  114. Response::json($status, strip_tags($info));
  115. } else {
  116. echo '<script type="text/javascript">alert("' . clear_html_blank($info) . '");window.history.go(-1);</script>';
  117. exit();
  118. }
  119. }
  120. /**
  121. * 跳转
  122. *
  123. * @param string $url跳转地址
  124. */
  125. function location($url)
  126. {
  127. if ($url == '-1' && isset($_SERVER['HTTP_REFERER'])) {
  128. $url = $_SERVER['HTTP_REFERER'];
  129. if (strpos($url, get_http_url()) !== 0) {
  130. $url = '/';
  131. }
  132. }
  133. header('Location:' . $url);
  134. exit();
  135. }
  136. /**
  137. * 弹窗并跳转
  138. *
  139. * @param string $info信息
  140. * @param string $url跳转地址
  141. */
  142. function alert_location($info, $url, $status = 0)
  143. {
  144. if ($url == '-1' && isset($_SERVER['HTTP_REFERER'])) {
  145. $url = $_SERVER['HTTP_REFERER'];
  146. if (strpos($url, get_http_url()) !== 0) {
  147. $url = '/';
  148. }
  149. }
  150. if (Config::get('return_data_type') == 'json' || is_ajax()) { // 接口模型返回格式数据
  151. Response::json($status, strip_tags($info), $url);
  152. } else {
  153. echo '<script type="text/javascript">alert("' . clear_html_blank($info) . '");location.href="' . $url . '";</script>';
  154. exit();
  155. }
  156. }
  157. /**
  158. * 弹窗并关闭
  159. *
  160. * @param string $info信息
  161. */
  162. function alert_close($info, $status = 0)
  163. {
  164. if (Config::get('return_data_type') == 'json' || is_ajax()) { // 接口模型返回格式数据
  165. Response::json($status, strip_tags($info));
  166. } else {
  167. echo '<script type="text/javascript">alert("' . clear_html_blank($info) . '");window.close();</script>';
  168. exit();
  169. }
  170. }
  171. /**
  172. * 实例化模型对象助手
  173. *
  174. * @param string $name
  175. * 需要实力化的模型名称
  176. * @param string $new
  177. * 是否强制新建对象
  178. * @return mixed
  179. */
  180. function model($name = null, $new = false)
  181. {
  182. return Basic::createModel($name, $new);
  183. }
  184. /**
  185. * api读取数据
  186. *
  187. * @param string $name
  188. * 接口名称,如:add或 admin.user.addd
  189. * @param array $param
  190. * 参数
  191. * @param string $rsOriginal
  192. * 结果不处理直接返回
  193. * @param string $jsonRsArray
  194. * 返回Json数组方式
  195. * @return mixed
  196. */
  197. function api($args = null)
  198. {
  199. return Basic::createApi(func_get_args());
  200. }
  201. // 输出模板内容
  202. function display($tpl)
  203. {
  204. $view = View::getInstance();
  205. echo $view->parser($tpl);
  206. }
  207. // 解析模板内容
  208. function parser($tpl)
  209. {
  210. $view = View::getInstance();
  211. return $view->parser($tpl);
  212. }
  213. // 设置模板
  214. function set_theme($theme_name)
  215. {
  216. $view = View::getInstance();
  217. $view->assign('theme', $theme_name);
  218. }
  219. // 注入模板变量
  220. function assign($var, $value)
  221. {
  222. $view = View::getInstance();
  223. $view->assign($var, $value);
  224. }
  225. // 变量获取接口
  226. function get_var($var)
  227. {
  228. $view = View::getInstance();
  229. return $view->getVar($var);
  230. }
  231. // 手动生成分页信息,返回限制语句
  232. function page($tatal, $morePageStr = false)
  233. {
  234. $page = Paging::getInstance();
  235. return $page->limit($tatal, $morePageStr);
  236. }
  237. // 内容输出助手函数
  238. function response($data)
  239. {
  240. return core\basic\Response::handle($data);
  241. }
  242. // Json内容输出助手函数
  243. function json($code, $data, $tourl = null)
  244. {
  245. return core\basic\Response::json($code, $data, $tourl);
  246. }
  247. /**
  248. * 数据过滤
  249. *
  250. * @param mixed $varname
  251. * 字符串或参数名称
  252. * @param array $condition
  253. * array('d_source'=>'post','d_none'=>true,'d_require'=>true,'d_type'=>'int','d_max'=>5,'d_min'=2,'d_default'=>'')
  254. * 字段名称:$varname,用字段名称做key传递文本
  255. * 数据源:d_source,[post、get、cookie、session、both、string]
  256. * 是否允许空:d_none,[true、false],如果为false接受的收据为空,则直接报错
  257. * 是否必须:d_require,[true、false],如果为true意味着如果数据不满足要求直接报错,否则返回null
  258. * 数据类型:d_type,[int、float、num、letter、var、bool、date、array、object]
  259. * 正则表达:d_regular,接受正则表达式
  260. * 最大值|最大长度:d_max
  261. * 最小值|最小长度:d_min
  262. * 默认值:d_default
  263. * @return mixed
  264. */
  265. function filter($varname, $condition)
  266. {
  267. // 变量名称文本
  268. if (array_key_exists($varname, $condition) && $condition[$varname]) {
  269. $vartext = $condition[$varname];
  270. } else {
  271. $vartext = $varname;
  272. }
  273. // 数据源
  274. if (array_key_exists('d_source', $condition)) {
  275. switch ($condition['d_source']) {
  276. case 'post':
  277. $data = @$_POST[$varname];
  278. break;
  279. case 'get':
  280. $data = @$_GET[$varname];
  281. break;
  282. case 'cookie':
  283. $data = @$_COOKIE[$varname];
  284. break;
  285. case 'session':
  286. $data = session($varname);
  287. break;
  288. case 'both':
  289. $data = @$_POST[$varname] ?: @$_GET[$varname];
  290. break;
  291. case 'string':
  292. $data = $varname;
  293. default:
  294. error($vartext . '数据获取方式设置错误!');
  295. }
  296. // 去空格
  297. if (is_string($data))
  298. $data = trim($data);
  299. } else {
  300. $data = $varname; // 没有数据源指定时直接按照字符串过滤处理
  301. }
  302. // 数据为空时,进行是否允许空检测
  303. if (! $data && array_key_exists('d_none', $condition) && $condition['d_none'] === false) {
  304. error($vartext . '不能为空!');
  305. }
  306. // 判断是否强制检测,为true时,意味着如果数据不满足要求直接报错,否则返回null
  307. if (array_key_exists('d_require', $condition) && $condition['d_require'] == true) {
  308. $require = true;
  309. } else {
  310. $require = false;
  311. }
  312. // 数据类型检测
  313. if (array_key_exists('d_type', $condition)) {
  314. switch ($condition['d_type']) {
  315. case 'int':
  316. if (! preg_match('/^[0-9]+$/', $data)) {
  317. $err = '必须为整数!';
  318. }
  319. break;
  320. case 'float':
  321. if (! is_float($data)) {
  322. $err = '必须为浮点数!';
  323. }
  324. break;
  325. case 'num':
  326. if (! is_numeric($data)) {
  327. $err = '必须为数字!';
  328. }
  329. break;
  330. case 'letter':
  331. if (! preg_match('/^[a-zA-Z]+$/', $data)) {
  332. $err = '只能包含字母!';
  333. }
  334. break;
  335. case 'var':
  336. if (! preg_match('/^[\w\-\.]+$/', $data)) {
  337. $err = '只能包含字母、数字、划线、点!';
  338. }
  339. break;
  340. case 'bool':
  341. if (! is_bool($data)) {
  342. $err = '必须为布尔类型!';
  343. }
  344. break;
  345. case 'date':
  346. if (! strtotime($data)) {
  347. $err = '必须为日期类型!';
  348. }
  349. break;
  350. case 'array':
  351. if (! is_array($data)) {
  352. $err = '必须为数组类型!';
  353. }
  354. break;
  355. case 'object':
  356. if (! is_object($data)) {
  357. $err = '必须为对象类型!';
  358. }
  359. break;
  360. case 'vars':
  361. if (! preg_match('/^[\x{4e00}-\x{9fa5}\w\-\.,\s]+$/u', $data)) {
  362. $err = '只能包含中文、字母、数字、横线、点、逗号、空格!';
  363. }
  364. break;
  365. default:
  366. if ($condition['d_type'])
  367. error($vartext . '数据类型设置错误!');
  368. }
  369. }
  370. // 非必须或必须但无错误时执行
  371. if ((! $require || ($require && ! isset($err)))) {
  372. // 正则匹配
  373. if (array_key_exists('d_regular', $condition)) {
  374. if (! preg_match($condition['d_regular'], $data)) {
  375. $err = '不符合正则表达式规则!';
  376. }
  377. }
  378. // 最大值匹配
  379. if (array_key_exists('d_max', $condition)) {
  380. if (is_numeric($data)) {
  381. if ($data > $condition['d_max']) {
  382. $err = '不能大于' . $condition['d_max'];
  383. }
  384. } else {
  385. if (mb_strlen($data) > $condition['d_max']) {
  386. $err = '长度不能大于' . $condition['d_max'];
  387. }
  388. }
  389. }
  390. // 最小值匹配
  391. if (array_key_exists('d_min', $condition)) {
  392. if (is_numeric($data)) {
  393. if ($data < $condition['d_min']) {
  394. $err = '不能小于' . $condition['d_min'];
  395. }
  396. } else {
  397. if (mb_strlen($data) < $condition['d_min']) {
  398. $err = '长度不能小于' . $condition['d_min'];
  399. }
  400. }
  401. }
  402. }
  403. // 如果为必须且有错误,则显示错误,如果非必须,但有错误,则设置数据为null
  404. if ($require && isset($err)) {
  405. error($vartext . $err);
  406. } elseif (isset($err)) {
  407. $data = null;
  408. }
  409. // 如果设置有默认值,默认值
  410. if (array_key_exists('d_default', $condition)) {
  411. $data = (! is_null($data)) ? $data : $condition['d_default'];
  412. }
  413. if (is_string($data)) {
  414. $data = trim($data); // 去空格
  415. $data = preg_replace_r('/(x3c)|(x3e)/', '', $data); // 去十六进制括号
  416. $data = preg_replace_r('/pboot:if/i', 'pboot@if', $data); // 过滤插入cms条件语句
  417. $data = preg_replace_r('/pboot:sql/i', 'pboot@sql', $data); // 过滤插入cms条件语句
  418. $data = preg_replace_r('/GET\[/i', 'GET@[', $data);
  419. $data = preg_replace_r('/POST\[/i', 'POST@[', $data);
  420. }
  421. // 销毁错误
  422. unset($err);
  423. // 返回收据
  424. return escape_string($data);
  425. }
  426. /**
  427. * 获取GET参数
  428. *
  429. * @param string $name
  430. * 参数名称
  431. * @param mixed $type
  432. * 数据类型
  433. * @param string $require
  434. * 是否为必须,为true是,如果不满足条件直接错误
  435. * @param string $vartext
  436. * 变量描述文本
  437. * @param string $default
  438. * 在非必需情况下默认值
  439. * @return mixed
  440. */
  441. function get($name, $type = null, $require = false, $vartext = null, $default = null)
  442. {
  443. $condition = array(
  444. 'd_source' => 'get',
  445. 'd_type' => $type,
  446. 'd_require' => $require,
  447. $name => $vartext,
  448. 'd_default' => $default
  449. );
  450. return filter($name, $condition);
  451. }
  452. /**
  453. * * 获取POST参数
  454. *
  455. * @param string $name
  456. * 参数名称
  457. * @param mixed $type
  458. * 数据类型
  459. * @param string $require
  460. * 是否为必须,为true是,如果不满足条件直接错误
  461. * @param string $vartext
  462. * 变量描述文本
  463. * @param string $default
  464. * 在非必需情况下默认值
  465. * @return mixed
  466. */
  467. function post($name, $type = null, $require = false, $vartext = null, $default = null)
  468. {
  469. $condition = array(
  470. 'd_source' => 'post',
  471. 'd_type' => $type,
  472. 'd_require' => $require,
  473. $name => $vartext,
  474. 'd_default' => $default
  475. );
  476. return filter($name, $condition);
  477. }
  478. /**
  479. * * 获取参数,post或get
  480. *
  481. * @param string $name
  482. * 参数名称
  483. * @param mixed $type
  484. * 数据类型
  485. * @param string $require
  486. * 是否为必须,为true是,如果不满足条件直接错误
  487. * @param string $vartext
  488. * 变量描述文本
  489. * @param string $default
  490. * 在非必需情况下默认值
  491. * @return mixed
  492. */
  493. function request($name, $type = null, $require = false, $vartext = null, $default = null)
  494. {
  495. if (isset($_POST[$name])) {
  496. $d_source = 'post';
  497. } else {
  498. $d_source = 'get';
  499. }
  500. $condition = array(
  501. 'd_source' => $d_source,
  502. 'd_type' => $type,
  503. 'd_require' => $require,
  504. $name => $vartext,
  505. 'd_default' => $default
  506. );
  507. return filter($name, $condition);
  508. }
  509. /**
  510. * 读取或写入Cookie信息
  511. *
  512. * @param string $name
  513. * 名称
  514. * @param string $value
  515. * 值
  516. * @param int $expire
  517. * 秒数
  518. * @param string $path
  519. * 路径,默认站点目录
  520. */
  521. function cookie($name, $value = null, $expire = null, $path = null, $domain = null, $secure = null, $httponly = false)
  522. {
  523. if (! is_null($value)) {
  524. $path = SITE_DIR . '/';
  525. if (is_string($value))
  526. $value = trim($value);
  527. $_COOKIE[$name] = $value; // 让cookie立即生效
  528. if (! is_null($expire)) {
  529. return setcookie($name, $value, time() + $expire, $path, $domain, $secure, $httponly);
  530. } else {
  531. return setcookie($name, $value, 0, $path, $domain, $secure, $httponly);
  532. }
  533. } else {
  534. if (isset($_COOKIE[$name])) {
  535. return escape_string($_COOKIE[$name]);
  536. } else {
  537. return null;
  538. }
  539. }
  540. }
  541. /**
  542. * 读取或写入session信息
  543. *
  544. * @param string $name
  545. * 支持点分多级获取
  546. * @param mixed $value
  547. * 设置值
  548. * @return string|NULL|unknown
  549. */
  550. function session($name, $value = null)
  551. {
  552. if (! isset($_SESSION)) {
  553. session_start(); // 自动启动会话
  554. }
  555. if (! is_null($value)) {
  556. if (isset($_SESSION[$name])) {
  557. if ($_SESSION[$name] != $value) {
  558. $_SESSION[$name] = $value;
  559. }
  560. } else {
  561. $_SESSION[$name] = $value;
  562. }
  563. return $value;
  564. } else {
  565. if (strpos($name, '.')) {
  566. if (isset($_SESSION[$name])) {
  567. return $_SESSION[$name];
  568. }
  569. $names = explode('.', $name);
  570. if (! isset($_SESSION[$names[0]])) {
  571. return null;
  572. }
  573. $var = $_SESSION[$names[0]];
  574. $len = count($names);
  575. for ($i = 1; $i < $len; $i ++) {
  576. if (is_array($var)) {
  577. if (isset($var[$names[$i]])) {
  578. $var = $var[$names[$i]];
  579. } else {
  580. return null;
  581. }
  582. } elseif (is_object($var)) {
  583. if (isset($var->{$names[$i]})) {
  584. $var = $var->{$names[$i]};
  585. } else {
  586. return null;
  587. }
  588. } else {
  589. break;
  590. }
  591. }
  592. return $var;
  593. } else {
  594. if (isset($_SESSION[$name])) {
  595. return $_SESSION[$name];
  596. } else {
  597. return null;
  598. }
  599. }
  600. }
  601. }
  602. // 检查会话参数是否存在
  603. function issetSession($name)
  604. {
  605. if (! isset($_SESSION)) {
  606. session_start(); // 自动启动会话
  607. }
  608. return isset($_SESSION[$name]);
  609. }
  610. /**
  611. * 快速发送邮件函数
  612. *
  613. * @param array $config
  614. * 邮件服务器连接数组,需包含 smtp_server、smtp_username、smtp_password、smtp_port、smtp_port
  615. * @param string $to
  616. * 邮件接收人
  617. * @param string $subject
  618. * 邮件主题
  619. * @param string $body
  620. * 邮件正文
  621. */
  622. function sendmail(array $config, $to, $subject, $body)
  623. {
  624. $smtp = new Smtp($config['smtp_server'], $config['smtp_username'], $config['smtp_password'], $config['smtp_port'], $config['smtp_ssl']);
  625. if ($smtp->sendMail($to, $subject, $body)) {
  626. return true;
  627. } else {
  628. return $smtp->error();
  629. }
  630. }
  631. // 发送短信
  632. function sendsms(array $config, $to, $content)
  633. {
  634. if (! $to || ! $content) {
  635. return false;
  636. }
  637. if (! isset($config['sms_account']) || ! isset($config['sms_pwd']) || ! isset($config['sms_signid'])) {
  638. alert_back('短信发送参数配置有误');
  639. }
  640. $data['Account'] = $config['sms_account'];
  641. $data['Pwd'] = $config['sms_pwd'];
  642. $data['SignId'] = $config['sms_signid'];
  643. $data['Content'] = $content;
  644. $to = str_replace("\r\n", ",", $to); // 替换回车
  645. $to = str_replace(",", ",", $to); // 替换中文逗号分割符
  646. $data['Mobile'] = str_replace(" ", "", $to); // 替换空格
  647. $url = "http://api.feige.ee/SmsService/Send";
  648. if (! ! $res = get_url($url, $data)) {
  649. $result = json_decode($res);
  650. if (! ($result->Code === 0)) {
  651. error('短信发送失败,' . $result->Message);
  652. } else {
  653. return true;
  654. }
  655. }
  656. }
  657. // 查询短信余额
  658. function get_sms_balance(array $config)
  659. {
  660. $data['Account'] = $config['sms_account'];
  661. $data['Pwd'] = $config['sms_pwd'];
  662. $url = "http://api.feige.ee/Account/Balance";
  663. if (! ! $res = get_url($url, $data)) {
  664. $result = json_decode($res);
  665. if (! ($result->Code === 0)) {
  666. error('查询失败,' . $result->Message);
  667. } else {
  668. return $result->Balance;
  669. }
  670. }
  671. }
  672. // 返回404页面,文件中可使用{info}替换提示信息
  673. function _404($string, $jump_url = null, $time = 2)
  674. {
  675. http_response_code(404);
  676. $file_404 = ROOT_PATH . '/404.html';
  677. if (file_exists($file_404)) {
  678. echo parse_info_tpl($file_404, $string, $jump_url, $time);
  679. exit();
  680. } else {
  681. error($string, $jump_url, $time);
  682. }
  683. }
  684. function _301(){
  685. $target_url = get_http_url();
  686. header("HTTP/1.1 301 Moved Permanently");
  687. header("Location: $target_url");
  688. exit;
  689. }
  690. // php对象转为数组
  691. function toArray($obj){
  692. if($obj === null){
  693. return [];
  694. }else{
  695. return json_decode(json_encode($obj),true);
  696. }
  697. }
  698. //if标签比较符处理
  699. function symbol($matches): string
  700. {
  701. $flag = '';
  702. $symbol1 = ['&&','||'];
  703. foreach ($symbol1 as $items) {
  704. if (strpos($matches, $items) !== false) {
  705. $arr = explode($items, $matches);
  706. switch ($items) {
  707. case '&&':
  708. $bool1 = compareSymbol1($arr[0]);
  709. $bool2 = compareSymbol1($arr[1]);
  710. $flag = $bool1 && $bool2 ? 'if' : 'else';
  711. break;
  712. case '||':
  713. $bool1 = compareSymbol1($arr[0]);
  714. $bool2 = compareSymbol1($arr[1]);
  715. $flag = $bool1 || $bool2 ? 'if' : 'else';
  716. break;
  717. }
  718. break;
  719. }
  720. }
  721. if(!$flag){
  722. $compare = compareSymbol1($matches);
  723. if($compare === true){
  724. $flag = 'if';
  725. }else if($compare === false){
  726. $flag = 'else';
  727. }
  728. }
  729. return $flag;
  730. }
  731. function compareSymbol1($str){
  732. $bool = null;
  733. $symbol = ['>=','<=','!=','==','>','<'];
  734. foreach ($symbol as $items) {
  735. if (strpos($str, $items) !== false) {
  736. $arr = explode($items, $str);
  737. switch ($items) {
  738. case '>':
  739. $res1 = compareSymbol2($arr[0]);
  740. $res2 = compareSymbol2($arr[1]);
  741. $bool = $res1 > $res2;
  742. break;
  743. case '>=':
  744. $res1 = compareSymbol2($arr[0]);
  745. $res2 = compareSymbol2($arr[1]);
  746. $bool = $res1 >= $res2;
  747. break;
  748. case '!=':
  749. $res1 = compareSymbol2($arr[0]);
  750. $res2 = compareSymbol2($arr[1]);
  751. $bool = $res1 != $res2;
  752. break;
  753. case '==':
  754. $res1 = compareSymbol2($arr[0]);
  755. $res2 = compareSymbol2($arr[1]);
  756. $bool = $res1 == $res2;
  757. break;
  758. case '<=':
  759. $res1 = compareSymbol2($arr[0]);
  760. $res2 = compareSymbol2($arr[1]);
  761. $bool = $res1 <= $res2;
  762. break;
  763. case '<':
  764. $res1 = compareSymbol2($arr[0]);
  765. $res2 = compareSymbol2($arr[1]);
  766. $bool = $res1 < $res2;
  767. break;
  768. }
  769. break;
  770. }
  771. }
  772. if($bool === null){
  773. $res = compareSymbol2($str);
  774. if (trim($res)) {
  775. return true;
  776. } else {
  777. return false;
  778. }
  779. }
  780. return $bool;
  781. }
  782. function compareSymbol2($str){
  783. $res = null;
  784. $symbol2 = ['%'];
  785. foreach ($symbol2 as $items) {
  786. if (strpos($str, $items) !== false) {
  787. $arr = explode($items, $str);
  788. if ($items == '%') {
  789. $res = $arr[0] % $arr[1];
  790. }
  791. break;
  792. }
  793. }
  794. if($res === null) {
  795. $str = trim($str);
  796. $str = trim($str,"'");
  797. $res = (string)$str;
  798. }
  799. return $res;
  800. }