[PHP] thinksaas 函数库 →→→→→进入此内容的聊天室

来自 Idiotic Motmot, 2024-05-29, 写在 PHP, 查看 166 次.
URL http://www.code666.cn/view/c1f5350c
  1. <?php
  2. defined('IN_TS') or die('Access Denied.');
  3. /*
  4.  * ThinkSAAS core function
  5.  * @copyright (c) ThinkSAAS All Rights Reserved
  6.  * @code by QiuJun
  7.  * @Email:thinksaas@qq.com
  8.  * @TIME:2010-12-18
  9.  */
  10. use Intervention\Image\ImageManagerStatic as Image;
  11. use OSS\OssClient;
  12. use OSS\Core\OssException;
  13.  
  14. /**
  15.  * 加载某一APP类
  16.  *
  17.  * @param string $app
  18.  * @return object
  19.  */
  20. function aac($app) {
  21.         spl_autoload_register(function ($app) {
  22.                 require_once 'app/'.$app.'/'.'class.'.$app.'.php';
  23.         });
  24.         $obj = new $app($GLOBALS['db']);
  25.         return $obj;
  26. }
  27.  
  28. /**
  29.  * 打印输出(右键查看源代码浏览输出结果,不影响前端网页运行效果)
  30.  */
  31. function tsPrint($value){
  32.         echo '<!--';
  33.         echo "\n";
  34.         var_dump($value);
  35.         echo '-->';
  36.         echo "\n";
  37. }
  38.  
  39. /**
  40.  * 二维数组的根据不同键值来排序。 第一个参数是二位数组名,第二个是依据键,第三个是升序还是降序,默认是升序
  41.  * @param $arr
  42.  * @param $keys
  43.  * @param $type
  44.  * @return array
  45.  */
  46. function array2sort($arr, $keys, $type = 'asc') {
  47.         $keysvalue = $new_array = array();
  48.         foreach ($arr as $k => $v) {
  49.                 $keysvalue[$k] = $v[$keys];
  50.         }
  51.         if ($type == 'asc') {
  52.                 asort($keysvalue);
  53.         } else {
  54.                 arsort($keysvalue);
  55.         }
  56.         reset($keysvalue);
  57.         foreach ($keysvalue as $k => $v) {
  58.                 $new_array[$k] = $arr[$k];
  59.         }
  60.         return $new_array;
  61. }
  62.  
  63. /**
  64.  * ThinkSAAS Notice
  65.  * @param $notice
  66.  * @param $button
  67.  * @param $url
  68.  * @param $isAutoGo
  69.  * @return void
  70.  */
  71. function tsNotice($notice, $button = '点击返回', $url = 'javascript:history.back(-1);', $isAutoGo = false) {
  72.         global $runTime;
  73.         $title = '提示:';
  74.         include  pubTemplate('notice');
  75.         exit();
  76. }
  77.  
  78. /**
  79.  * 系统消息
  80.  * @param $msg
  81.  * @param $button
  82.  * @param $url
  83.  * @param $isAutoGo
  84.  * @return void
  85.  */
  86. function qiMsg($msg, $button = '点击返回>>', $url = 'javascript:history.back(-1);', $isAutoGo = false) {
  87.     echo <<<EOT
  88. <html>
  89. <head>
  90. EOT;
  91.         if ($isAutoGo) {
  92.                 echo "<meta http-equiv=\"refresh\" content=\"2;url=$url\" />";
  93.         }
  94.         echo <<<EOT
  95. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  96. <title>ThinkSAAS 提示</title>
  97. <style type="text/css">
  98. <!--
  99. body {
  100. font-family: Arial;
  101. font-size: 12px;
  102. line-height:150%;
  103. text-align:center;
  104. }
  105. a{color:#555555;}
  106. .main {
  107. width:500px;
  108. background-color:#FFFFFF;
  109. font-size: 12px;
  110. color: #666666;
  111. margin:100px auto 0;
  112. list-style:none;
  113. padding:20px;
  114. }
  115. .main p {
  116. line-height: 18px;
  117. margin: 5px 20px;
  118. font-size:14px;
  119. }
  120. -->
  121. </style>
  122. </head>
  123. <body>
  124. <div class="main">
  125. <p>$msg</p>
  126. <p><a href="$url">$button</a></p>
  127. </div>
  128. </body>
  129. </html>
  130. EOT;
  131.         exit();
  132. }
  133.  
  134. /**
  135.  * 分页函数
  136.  * @param int $count
  137.  * @param int $perlogs
  138.  * @param int $page
  139.  * @param string $url
  140.  * @param string $suffix
  141.  * @return string
  142.  */
  143. function pagination($count, $perlogs, $page, $url ,$suffix = '') {
  144.  
  145.     $urlset = $GLOBALS['TS_SITE']['site_urltype'];
  146.     if ($urlset == 3 && !strpos($url,'index.php')) {
  147.         $suffix = '.html';
  148.     }
  149.  
  150.     $pnums = ceil($count / $perlogs);
  151.     $res = '<nav aria-label="Page navigation example"><ul class="pagination justify-content-center">';
  152.     $re = '';
  153.     for ($i = $page - 5; $i <= $page + 5 && $i <= $pnums; $i++) {
  154.         if ($i > 0) {
  155.             if ($i == $page) {
  156.                 $re .= '<li class="page-item active"><a class="page-link">' . $i . '</a></li>';
  157.             } else {
  158.                 $re .= '<li class="page-item"><a class="page-link" href="' . $url . $i . $suffix . '">' . $i . '</a></li>';
  159.             }
  160.         }
  161.     }
  162.     if ($page > 6)
  163.         $re = '<li class="page-item"><a class="page-link" href="' . $url . '1' . $suffix . '" title="首页">&laquo;</a></li>' . $re;
  164.     if ($page + 5 < $pnums)
  165.         $re .= '<li class="page-item"><a class="page-link" href="' . $url . $pnums . $suffix . '" title="尾页">&raquo;</a></li>';
  166.  
  167.     $re .= '</ul></nav>';
  168.  
  169.     $res .= $re;
  170.  
  171.     if ($pnums <= 1)
  172.         $res = '';
  173.     return $res;
  174. }
  175.  
  176. /**
  177.  * 验证Email
  178.  * @param string $email
  179.  * @return boolean
  180.  */
  181. function valid_email($email) {
  182.         if (preg_match('/^[A-Za-z0-9]+([._\-\+]*[A-Za-z0-9]+)*@([A-Za-z0-9-]+\.)+[A-Za-z0-9]+$/', $email)) {
  183.                 return true;
  184.         } else {
  185.                 return false;
  186.         }
  187. }
  188.  
  189. /**
  190.  * 处理时间的函数
  191.  * @param unknown $btime
  192.  * @param unknown $etime
  193.  * @return string
  194.  */
  195. function getTime($btime, $etime = null) {
  196.         if ($etime == null)
  197.                 $etime = time();
  198.         if ($btime < $etime) {
  199.                 $stime = $btime;
  200.                 $endtime = $etime;
  201.         } else {
  202.                 $stime = $etime;
  203.                 $endtime = $btime;
  204.         }
  205.         $timec = $endtime - $stime;
  206.         $days = intval($timec / 86400);
  207.         $rtime = $timec % 86400;
  208.         $hours = intval($rtime / 3600);
  209.         $rtime = $rtime % 3600;
  210.         $mins = intval($rtime / 60);
  211.         $secs = $rtime % 60;
  212.         if ($days >= 1) {
  213.                 return $days . ' 天前';
  214.         }
  215.         if ($hours >= 1) {
  216.                 return $hours . ' 小时前';
  217.         }
  218.  
  219.         if ($mins >= 1) {
  220.                 return $mins . ' 分钟前';
  221.         }
  222.         if ($secs >= 1) {
  223.                 return $secs . ' 秒前';
  224.         }
  225. }
  226.  
  227. /**
  228.  * 获取用户IP
  229.  * @return Ambigous <string, unknown>
  230.  */
  231. function getIp() {
  232.         if (getenv('HTTP_CLIENT_IP') && strcasecmp(getenv('HTTP_CLIENT_IP'), 'unknown')) {
  233.                 $PHP_IP = getenv('HTTP_CLIENT_IP');
  234.         } elseif (getenv('HTTP_X_FORWARDED_FOR') && strcasecmp(getenv('HTTP_X_FORWARDED_FOR'), 'unknown')) {
  235.                 $PHP_IP = getenv('HTTP_X_FORWARDED_FOR');
  236.         } elseif (getenv('REMOTE_ADDR') && strcasecmp(getenv('REMOTE_ADDR'), 'unknown')) {
  237.                 $PHP_IP = getenv('REMOTE_ADDR');
  238.         } elseif (isset($_SERVER['REMOTE_ADDR']) && $_SERVER['REMOTE_ADDR'] && strcasecmp($_SERVER['REMOTE_ADDR'], 'unknown')) {
  239.                 $PHP_IP = $_SERVER['REMOTE_ADDR'];
  240.         }
  241.         preg_match("/[\d\.]{7,15}/", $PHP_IP, $ipmatches);
  242.         $PHP_IP = $ipmatches[0] ? $ipmatches[0] : 'unknown';
  243.         return $PHP_IP;
  244. }
  245.  
  246. /**
  247.  * 纯文本输入
  248.  * @param unknown $text
  249.  * @return mixed
  250.  */
  251. function t($text) {
  252.         if($text!='' && $text!=null){
  253.                 $text = tsDecode($text);
  254.                 $text = preg_replace('/\[.*?\]/is', '', $text);
  255.                
  256.                 $text = cleanJs($text);
  257.                 // 彻底过滤空格BY QINIAO
  258.                 $text = preg_replace('/\s(?=\s)/', '', $text);
  259.                 $text = preg_replace('/[\n\r\t]/', ' ', $text);
  260.                 $text = str_replace('  ', ' ', $text);
  261.                 // $text = str_replace ( ' ', '', $text );
  262.                 $text = str_replace('&nbsp;', '', $text);
  263.                 $text = str_replace('&', '', $text);
  264.                 $text = str_replace('=', '', $text);
  265.                 $text = str_replace('-', '', $text);
  266.                 $text = str_replace('#', '', $text);
  267.                 $text = str_replace('%', '', $text);
  268.                 $text = str_replace('!', '', $text);
  269.                 $text = str_replace('@', '', $text);
  270.                 $text = str_replace('^', '', $text);
  271.                 $text = str_replace('*', '', $text);
  272.                 $text = str_replace('amp;', '', $text);
  273.        
  274.                 $text = str_replace('position', '', $text);
  275.        
  276.                 $text = strip_tags($text);
  277.                 $text = htmlspecialchars($text);
  278.                 $text = str_replace("'", "", $text);
  279.                 return $text;
  280.         }else{
  281.                 return '';
  282.         }
  283.        
  284.        
  285. }
  286.  
  287. /**
  288.  * 输入安全的html,针对存入数据库中的数据进行的过滤和转义
  289.  * @param string $text
  290.  * @return string
  291.  */
  292. function h($text) {
  293.         $text = tsTrim($text);
  294.         $text = htmlspecialchars($text);
  295.         $text = addslashes($text);
  296.         return $text;
  297. }
  298.  
  299. /**
  300.  * utf-8截取
  301.  * @param string $string
  302.  * @param number $start
  303.  * @param unknown $sublen
  304.  * @param string $append
  305.  * @return string
  306.  */
  307. function cututf8($string, $start = 0, $sublen=100, $append = true) {
  308.         if($string!='' && $string!=null){
  309.                 $pa = "/[\x01-\x7f]|[\xc2-\xdf][\x80-\xbf]|\xe0[\xa0-\xbf][\x80-\xbf]|[\xe1-\xef][\x80-\xbf][\x80-\xbf]|\xf0[\x90-\xbf][\x80-\xbf][\x80-\xbf]|[\xf1-\xf7][\x80-\xbf][\x80-\xbf][\x80-\xbf]/";
  310.                 preg_match_all($pa, $string, $t_string);
  311.                 if (count($t_string[0]) - $start > $sublen && $append == true) {
  312.                         return join('', array_slice($t_string[0], $start, $sublen)) . "...";
  313.                 } else {
  314.                         return join('', array_slice($t_string[0], $start, $sublen));
  315.                 }
  316.         }else{
  317.                 return '';
  318.         }
  319. }
  320.  
  321. /**
  322.  * 计算时间
  323.  * @return number
  324.  */
  325. function getmicrotime() {
  326.         list($usec, $sec) = explode(" ", microtime());
  327.         return (( float )$usec + ( float )$sec);
  328. }
  329.  
  330. /**
  331.  * 写入文件,支持Memcache缓存
  332.  * @param unknown $file 缓存文件
  333.  * @param string $dir 缓存目录
  334.  * @param unknown $data 缓存内容
  335.  * @param number $isphp
  336.  * @return boolean
  337.  */
  338. function fileWrite($file, $dir, $data, $isphp = 1) {
  339.  
  340.         $dfile = $dir . '/' . $file;
  341.  
  342.         // 支持memcache
  343.         if ($GLOBALS['TS_CF']['memcache'] && extension_loaded('memcache')) {
  344.                 $GLOBALS['TS_MC'] -> delete(md5($dfile));
  345.         $GLOBALS['TS_MC'] -> set(md5($dfile), $data, 0, 172800);
  346.         }
  347.  
  348.         // 同时保存文件
  349.         !is_dir($dir) ? mkdir($dir, 0777) : '';
  350.         if (is_file($dfile))
  351.                 unlink($dfile);
  352.         if ($isphp == 1) {
  353.                 $data = "<?php\ndefined('IN_TS') or die('Access Denied.');\nreturn " . var_export($data, true) . ";";
  354.         }
  355.  
  356.         file_put_contents($dfile, $data);
  357.  
  358.         return true;
  359. }
  360.  
  361. /**
  362.  * 读取文件 支持Memcache缓存 $dfile 文件
  363.  * @param string $dfile
  364.  */
  365. function fileRead($dfile) {
  366.  
  367.         // 支持memcache
  368.         if ($GLOBALS['TS_CF']['memcache'] && extension_loaded('memcache')) {
  369.  
  370.                 if ($GLOBALS['TS_MC'] -> get(md5($dfile))) {
  371.                         return $GLOBALS['TS_MC'] -> get(md5($dfile));
  372.                 } else {
  373.                         if (is_file($dfile))
  374.                                 return
  375.                                 include $dfile;
  376.                 }
  377.         } else {
  378.  
  379.                 if (is_file($dfile))
  380.                         return
  381.                         include $dfile;
  382.         }
  383. }
  384.  
  385. /**
  386.  * 把数组转换为,号分割的字符串
  387.  * @param 一维数组 $arr
  388.  * @param 分割符号,默认,号
  389.  * @return Ambigous <string, unknown>
  390.  */
  391. function arr2str($arr,$fg=',') {
  392.         $str = '';
  393.         $count = 1;
  394.         if (is_array($arr)) {
  395.                 foreach ($arr as $a) {
  396.                         if ($count == 1) {
  397.                                 $str .= $a;
  398.                         } else {
  399.                                 $str .= $fg . $a;
  400.                         }
  401.                         $count++;
  402.                 }
  403.         }
  404.         return $str;
  405. }
  406.  
  407. /**
  408.  * 生成随机数(1数字,0字母数字组合)
  409.  * @param unknown $length
  410.  * @param number $numeric
  411.  * @return string
  412.  */
  413. function random($length, $numeric = 0) {
  414.         PHP_VERSION < '4.2.0' ? mt_srand(( double ) microtime() * 1000000) : mt_srand();
  415.         $seed = base_convert(md5(print_r($_SERVER, 1) . microtime()), 16, $numeric ? 10 : 35);
  416.         $seed = $numeric ? (str_replace('0', '', $seed) . '012340567890') : ($seed . 'zZ' . strtoupper($seed));
  417.         $hash = '';
  418.         $max = strlen($seed) - 1;
  419.         for ($i = 0; $i < $length; $i++) {
  420.                 $hash .= $seed[mt_rand(0, $max)];
  421.         }
  422.         return $hash;
  423. }
  424.  
  425. /**
  426.  * 封装一个采集函数
  427.  * @param unknown $url  网址
  428.  * @param unknown $proxy        代理
  429.  * @param unknown $timeout      跳出时间
  430.  * @return mixed
  431.  */
  432. function getHtmlByCurl($url, $proxy, $timeout) {
  433.         $ch = curl_init();
  434.         curl_setopt($ch, CURLOPT_PROXY, $proxy);
  435.         curl_setopt($ch, CURLOPT_URL, $url);
  436.         curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  437.         curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
  438.         $file_contents = curl_exec($ch);
  439.         return $file_contents;
  440. }
  441.  
  442. /**
  443.  * 计算文件大小
  444.  * @param int $size
  445.  * @return string
  446.  */
  447. function format_bytes($size) {
  448.         $units = array(' B', ' KB', ' MB', ' GB', ' TB');
  449.         for ($i = 0; $size >= 1024 && $i < 4; $i++)
  450.                 $size /= 1024;
  451.         return round($size, 2) . $units[$i];
  452. }
  453.  
  454. /**
  455.  * 对象转数组
  456.  * @param unknown $array
  457.  * @return array
  458.  */
  459. function object2array($array) {
  460.         if (is_object($array)) {
  461.                 $array = ( array )$array;
  462.         }
  463.         if (is_array($array)) {
  464.                 foreach ($array as $key => $value) {
  465.                         $array[$key] = object2array($value);
  466.                 }
  467.         }
  468.         return $array;
  469. }
  470.  
  471. /**
  472.  * 写文件
  473.  *
  474.  * @param string $file
  475.  *              - 需要写入的文件,系统的绝对路径加文件名
  476.  * @param string $content
  477.  *              - 需要写入的内容
  478.  * @param string $mod
  479.  *              - 写入模式,默认为w
  480.  * @param boolean $exit
  481.  *              - 不能写入是否中断程序,默认为中断
  482.  * @return boolean 返回是否写入成功
  483.  *
  484.  */
  485. function isWriteFile($file, $content, $mod = 'w', $exit = TRUE) {
  486.         if (!@$fp = @fopen($file, $mod)) {
  487.                 if ($exit) {
  488.                         exit('ThinkSAAS File :<br>' . $file . '<br>Have no access to write!');
  489.                 } else {
  490.                         return false;
  491.                 }
  492.         } else {
  493.                 @flock($fp, 2);
  494.                 @fwrite($fp, $content);
  495.                 @fclose($fp);
  496.                 return true;
  497.         }
  498. }
  499.  
  500. /**
  501.  * 创建目录
  502.  * @param string $dir
  503.  * @return boolean
  504.  */
  505. function makedir($dir) {
  506.         return is_dir($dir) or (makedir(dirname($dir)) and mkdir($dir, 0777));
  507. }
  508.  
  509. /**
  510.  * 加载模板
  511.  * @param string $file
  512.  *              - 模板文件名
  513.  * @return string 返回编译后模板的系统绝对路径
  514.  * @param array $self
  515.  *              自定义路径,必须是数组格式
  516.  *
  517.  */
  518. function template($file, $plugin = '', $self = '') {
  519.  
  520.         if ($plugin) {
  521.                 $tplfile = 'plugins/' . $GLOBALS['TS_URL']['app'] . '/' . $plugin . '/' . $file . '.html';
  522.                 if (!is_file($tplfile)) {
  523.                         $tplfile = 'plugins/pubs/' . $plugin . '/' . $file . '.html';
  524.                 }
  525.                 $objfile = 'cache/template/' . $plugin . '.' . $file . '.tpl.php';
  526.         } else if ($self) {
  527.         $tplfile ='';
  528.                 foreach ($self as $v) {
  529.             $tplfile .= $v . '/';
  530.                         $cache = $v . '_';
  531.                 }
  532.                 $tplfile = $tplfile . $file . '.html';
  533.                 $objfile = 'cache/template/self/' . $self[3] . '.' . $file . '.tpl.php';
  534.         } else {
  535.                 $tplfile = 'app/' . $GLOBALS['TS_URL']['app'] . '/html/' . $file . '.html';
  536.                 $objfile = 'cache/template/' . $GLOBALS['TS_URL']['app'] . '.' . $file . '.tpl.php';
  537.         }
  538.  
  539.         if (@filemtime($tplfile) > @filemtime($objfile)) {
  540.                 // note 加载模板类文件
  541.                 require_once 'thinksaas/tsTemplate.php';
  542.                 $T = new tsTemplate();
  543.  
  544.                 $T -> complie($tplfile, $objfile);
  545.         }
  546.  
  547.         return $objfile;
  548. }
  549.  
  550. /**
  551.  * 加载公用html模板文件
  552.  * @param unknown $file
  553.  * @return string
  554.  */
  555. function pubTemplate($file) {
  556.         $tplfile = 'public/html/' . $file . '.html';
  557.         $objfile = 'cache/template/public.' . $file . '.tpl.php';
  558.         if (filemtime($tplfile) > filemtime($objfile)) {
  559.                 // note 加载模板类文件
  560.                 require_once 'thinksaas/tsTemplate.php';
  561.                 $T = new tsTemplate();
  562.                 $T -> complie($tplfile, $objfile);
  563.         }
  564.         return $objfile;
  565. }
  566.  
  567. /**
  568.  * 该函数在插件中调用,挂载插件函数到预留的钩子上,针对app各个的插件部分,修改自Emlog
  569.  *
  570.  * @param string $hook
  571.  * @param string $actionFunc
  572.  * @return boolearn
  573.  *
  574.  */
  575. function addAction($hook, $actionFunc) {
  576.         global $tsHooks;
  577.         $tsHooks[$hook][] = $actionFunc;
  578.         return true;
  579. }
  580.  
  581. /**
  582.  * 执行挂在钩子上的函数,支持多参数 eg:doAction('post_comment', $author, $email, $url, $comment);
  583.  * @param string $hook
  584.  */
  585. function doAction($hook) {
  586.         global $tsHooks;
  587.         $args = array_slice(func_get_args(), 1);
  588.         if (isset($tsHooks[$hook])) {
  589.                 foreach ($tsHooks [$hook] as $function) {
  590.                         $string = call_user_func_array($function, $args);
  591.                 }
  592.         }
  593.         #打印钩子信息
  594.         if ($GLOBALS['TS_CF']['hook']) var_dump($hook);
  595. }
  596.  
  597. function createFolders($path) {
  598.         // 递归创建
  599.         if (!file_exists($path)) {
  600.                 createFolders(dirname($path));
  601.                 // 取得最后一个文件夹的全路径返回开始的地方
  602.                 mkdir($path, 0777);
  603.         }
  604. }
  605.  
  606. /**
  607.  * 删除文件夹和文件夹下所有的文件
  608.  * @param string $dir
  609.  * @return boolean
  610.  */
  611. function delDir($dir = '') {
  612.         if (empty($dir)) {
  613.                 return false;
  614.         }
  615.         if (substr($dir, -1) == '/') {
  616.                 $dir = rtrim($dir, '/');
  617.         }
  618.         if (!file_exists($dir))
  619.                 return true;
  620.         if (!is_dir($dir) || is_link($dir))
  621.                 return unlink($dir);
  622.         foreach (scandir ( $dir ) as $item) {
  623.                 if ($item == '.' || $item == '..')
  624.                         continue;
  625.                 if (!delDir($dir . "/" . $item)) {
  626.                         chmod($dir . "/" . $item, 0777);
  627.                         if (!delDir($dir . "/" . $item))
  628.                                 return false;
  629.                 };
  630.         }
  631.         return rmdir($dir);
  632. }
  633.  
  634. /**
  635.  * 获取带http的网站域名
  636.  * @return string
  637.  */
  638. function getHttpUrl() {
  639.         $arrUri = explode('index.php', $_SERVER['REQUEST_URI']);
  640.         $site_url = 'http://' . $_SERVER['HTTP_HOST'] . $arrUri[0];
  641.         return $site_url;
  642. }
  643.  
  644. /**
  645.  * 10位MD5值
  646.  * @param string $str
  647.  * @return string
  648.  */
  649. function md10($str = '') {
  650.         return substr(md5($str), 10, 10);
  651. }
  652.  
  653. /**
  654.  * ThinkSAAS专用图片截图函数
  655.  * @param unknown $file 数据库里的图片url
  656.  * @param unknown $app  app名称
  657.  * @param unknown $w    缩略图片宽度
  658.  * @param unknown $h    缩略图片高度
  659.  * @param string $path
  660.  * @param string $c     1裁切,0不裁切
  661.  * @return void|string
  662.  */
  663. /**
  664.  * Undocumented function
  665.  *
  666.  * @param [type] $file          数据库里的图片url
  667.  * @param [type] $app           app名称
  668.  * @param [type] $w             缩略图片宽度
  669.  * @param [type] $h             缩略图片高度
  670.  * @param string $path
  671.  * @param string $c             1裁切,0不裁切
  672.  * @param string $sy            水印图片
  673.  * @param string $position      水印位置
  674.  * @param integer $x            水印X轴
  675.  * @param integer $y            水印Y轴
  676.  * @return void
  677.  */
  678. function tsXimg($file, $app, $w='', $h='', $path = '', $c = '0',$sy='sy.png',$position='bottom-left',$x=10,$y=10) {
  679.  
  680.         if($GLOBALS['TS_SITE']['file_upload_type']==1){
  681.                 #阿里云oss图片输出
  682.                 $result = tsXimgAliOss($file, $app, $w, $h);
  683.         }else{
  684.                 #本地图片输出
  685.                 $result = tsXimgLocal($file, $app, $w, $h, $path, $c,$sy,$position,$x,$y);
  686.         }
  687.         return $result;
  688. }
  689.  
  690. /**
  691.  * 本地图片输出
  692.  */
  693. function tsXimgLocal($file, $app, $w, $h, $path = '', $c = '0',$sy='sy.png',$position='bottom-left',$x=10,$y=10){
  694.         if (!$file) {
  695.         return false;
  696.     } else {
  697.  
  698.         $arrInfo = explode('/', $file);
  699.         $name = end($arrInfo);
  700.  
  701.         $arrType = explode('.',$name);
  702.         $type = end($arrType);
  703.                
  704.                
  705.                 if($w=='' && $h==''){
  706.                         #原图输出
  707.                         $cpath = 'uploadfile/'.$app.'/'.$file;
  708.  
  709.                 }else{
  710.  
  711.                         if($type!='gif'){
  712.                                 $cpath = 'cache/' . $app . '/' . $path . '/' . md5($w . $h . $app . $name) . '.jpg';
  713.                         }else{
  714.                                 $cpath = 'uploadfile/'.$app.'/'.$file;
  715.                         }
  716.        
  717.        
  718.                         if (!is_file($cpath) && $type!='gif') {
  719.        
  720.                                 $driver = $GLOBALS['TS_SITE']['photo_driver'];
  721.                                 if($driver==''){
  722.                                         $driver = 'gd';
  723.                                 }
  724.        
  725.                                 Image::configure(array('driver' => $driver));//gd or imagick
  726.        
  727.                                 createFolders('cache/' . $app . '/' . $path);
  728.                                 $dest = 'uploadfile/' . $app . '/' . $file;
  729.                                 $arrImg = getimagesize($dest);
  730.        
  731.                                 try{
  732.                                         $img = Image::make($dest);
  733.                                 }catch (Exception $e){
  734.                                         //$e->getMessage();
  735.                                         return SITE_URL . 'public/images/nopic.jpg';
  736.                                         exit();
  737.                                 }
  738.        
  739.                                 if ($arrImg[0] <= $w) {
  740.        
  741.                                         if($c){
  742.                                                 if($w && $h){
  743.                                                         $img->fit($w, $h);
  744.                                                 }elseif($w && $h==''){
  745.                                                         $img->resize($w, null, function ($constraint) {
  746.                                                                 $constraint->aspectRatio();
  747.                                                         });
  748.                                                 }
  749.                                         }
  750.        
  751.                                 } else {
  752.        
  753.                                         if($w && $h){
  754.                                                 $img->fit($w, $h);
  755.                                         }elseif($w && $h==''){
  756.                                                 $img->resize($w, null, function ($constraint) {
  757.                                                         $constraint->aspectRatio();
  758.                                                 });
  759.                                         }
  760.        
  761.                                 }
  762.        
  763.        
  764.                                 if($arrImg[0]>320 && $w>320 && in_array($type,array('jpg','jpeg','png'))){
  765.                                         #图片大于320px加水印
  766.                                         //echo $sy;exit;
  767.                                         $watermark = Image::make('public/images/'.$sy);
  768.                                         $img->insert($watermark, $position,$x,$y);
  769.                                 }
  770.        
  771.        
  772.                                 $img->save($cpath);
  773.        
  774.                         }
  775.  
  776.                 }
  777.  
  778.        
  779.  
  780.         return SITE_URL . $cpath;
  781.  
  782.         }
  783. }
  784.  
  785. /**
  786.  * 阿里云oss图片输出
  787.  *
  788.  * @param [type] $file
  789.  * @param [type] $app
  790.  * @param [type] $w
  791.  * @param [type] $h
  792.  * @return void
  793.  */
  794. function tsXimgAliOss($file, $app, $w, $h){
  795.         $cpath = 'uploadfile/'.$app.'/'.$file;
  796.         $url = $GLOBALS['TS_SITE']['alioss_bucket_url'].'/'.$cpath;
  797.         if($w && $h){
  798.                 $photo = $url.'?x-oss-process=image/resize,m_fill,h_'.$h.',w_'.$w;
  799.         }elseif($w && $h==''){
  800.                 $photo = $url.'?x-oss-process=image/resize,w_'.$w;
  801.         }elseif($w=='' && $h){
  802.                 $photo = $url.'?x-oss-process=image/resize,h_'.$h;
  803.         }elseif($w=='' && $h==''){
  804.                 #原图输出
  805.                 $photo = $url;
  806.         }
  807.         return $photo;
  808. }
  809.  
  810. /**
  811.  * TS专用删除缓存图片
  812.  * @param $file
  813.  * @param $app
  814.  * @param $w
  815.  * @param $h
  816.  * @param $path
  817.  * @return bool
  818.  */
  819. function tsDimg($file, $app, $w, $h, $path) {
  820.  
  821.         $info = explode('/', $file);
  822.         $name = $info[2];
  823.  
  824.     $cpath = 'cache/' . $app . '/' . $path . '/' . md5($w . $h . $app . $name) . '.jpg';
  825.  
  826.         unlink($cpath);
  827.  
  828.         return true;
  829. }
  830.  
  831. /**
  832.  * tsUrl提供至少4种的url展示方式
  833.  * (1)index.php?app=group&ac=topic&topicid=1 //标准默认模式
  834.  * (2)index.php/group/topic/topicid-1 //path_info模式
  835.  * (3)group-topic-topicid-1.html //rewrite模式1
  836.  * (4)group/topic/topicid-1 //rewrite模式2
  837.  * @param unknown $app
  838.  * @param string $ac
  839.  * @param unknown $params
  840.  * @return string
  841.  */
  842. function tsUrl($app, $ac = '', $params = array()) {
  843.  
  844.         $urlset = $GLOBALS['TS_SITE']['site_urltype'];
  845.     $str ='';
  846.         if ($urlset == 1) {
  847.                 foreach ($params as $k => $v) {
  848.                         $str .= '&' . $k . '=' . $v;
  849.                 }
  850.                 if ($ac == '') {
  851.                         $ac = '';
  852.                 } else {
  853.                         if ($GLOBALS['TS_CF']['subdomain'] && in_array($app, $GLOBALS['TS_CF']['subdomain']['app']) || $GLOBALS['TS_CF']['appdomain'][$app]) {
  854.                                 $ac = '?ac=' . $ac;
  855.                         } else {
  856.                                 $ac = '&ac=' . $ac;
  857.                         }
  858.                 }
  859.                 if ($GLOBALS['TS_CF']['subdomain'] && in_array($app, $GLOBALS['TS_CF']['subdomain']['app']) || $GLOBALS['TS_CF']['appdomain'][$app]) {
  860.                         $url = 'index.php' . $ac . $str;
  861.                 } else {
  862.                         $url = 'index.php?app=' . $app . $ac . $str;
  863.                 }
  864.         } elseif ($urlset == 2) {
  865.  
  866.                 foreach ($params as $k => $v) {
  867.             $str .= '/' . $k . '-' . $v;
  868.                 }
  869.                 if ($ac == '') {
  870.                         $ac = '';
  871.                 } else {
  872.                         $ac = '/' . $ac;
  873.                 }
  874.  
  875.                 if ($GLOBALS['TS_CF']['subdomain'] && in_array($app, $GLOBALS['TS_CF']['subdomain']['app']) || $GLOBALS['TS_CF']['appdomain'][$app]) {
  876.                         $url = 'index.php' . $ac . $str;
  877.                 } else {
  878.                         $url = 'index.php/' . $app . $ac . $str;
  879.                 }
  880.         } elseif ($urlset == 3) {
  881.                 foreach ($params as $k => $v) {
  882.                         $str .= '-' . $k . '-' . $v;
  883.                 }
  884.  
  885.                 if ($ac == '') {
  886.                         $ac = '';
  887.                 } else {
  888.                         if ($GLOBALS['TS_CF']['subdomain'] && in_array($app, $GLOBALS['TS_CF']['subdomain']['app']) || $GLOBALS['TS_CF']['appdomain'][$app]) {
  889.                                 $ac = $ac;
  890.                         } else {
  891.                                 $ac = '-' . $ac;
  892.                         }
  893.                 }
  894.  
  895.                 $page = strpos($str, 'page');
  896.  
  897.                 if ($page) {
  898.                         if ($GLOBALS['TS_CF']['subdomain'] && in_array($app, $GLOBALS['TS_CF']['subdomain']['app']) || $GLOBALS['TS_CF']['appdomain'][$app]) {
  899.                                 $url = $ac . $str;
  900.                         } else {
  901.                                 $url = $app . $ac . $str;
  902.                         }
  903.                 } else {
  904.                         if ($GLOBALS['TS_CF']['subdomain'] && in_array($app, $GLOBALS['TS_CF']['subdomain']['app']) || $GLOBALS['TS_CF']['appdomain'][$app]) {
  905.                                 $url = $ac . $str . '.html';
  906.                         } else {
  907.                                 $url = $app . $ac . $str . '.html';
  908.                         }
  909.                 }
  910.         } elseif ($urlset == 4) {
  911.                 foreach ($params as $k => $v) {
  912.                         $str .= '/' . $k . '-' . $v;
  913.                 }
  914.                 if ($ac == '') {
  915.                         $ac = '';
  916.                 } else {
  917.                         if ($GLOBALS['TS_CF']['subdomain'] && in_array($app, $GLOBALS['TS_CF']['subdomain']['app']) || $GLOBALS['TS_CF']['appdomain'][$app]) {
  918.                                 $ac = $ac;
  919.                         } else {
  920.                                 $ac = '/' . $ac;
  921.                         }
  922.                 }
  923.                 if ($GLOBALS['TS_CF']['subdomain'] && in_array($app, $GLOBALS['TS_CF']['subdomain']['app']) || $GLOBALS['TS_CF']['appdomain'][$app]) {
  924.                         $url = $ac . $str;
  925.                 } else {
  926.                         $url = $app . $ac . $str;
  927.                 }
  928.         } elseif ($urlset == 5) {
  929.                 foreach ($params as $k => $v) {
  930.                         $str .= '/' . $k . '/' . $v;
  931.                 }
  932.                 $str = str_replace('/id', '', $str);
  933.                 if ($ac == '') {
  934.                         $ac = '';
  935.                 } else {
  936.                         if ($GLOBALS['TS_CF']['subdomain'] && in_array($app, $GLOBALS['TS_CF']['subdomain']['app']) || $GLOBALS['TS_CF']['appdomain'][$app]) {
  937.                                 $ac = $ac;
  938.                         } else {
  939.                                 $ac = '/' . $ac;
  940.                         }
  941.                 }
  942.                 if ($GLOBALS['TS_CF']['subdomain'] && in_array($app, $GLOBALS['TS_CF']['subdomain']['app']) || $GLOBALS['TS_CF']['appdomain'][$app]) {
  943.                         $url = $ac . $str;
  944.                 } else {
  945.                         $url = $app . $ac . $str;
  946.                 }
  947.         } elseif ($urlset == 6) {
  948.                 foreach ($params as $k => $v) {
  949.                         $str .= '/' . $k . '/' . $v;
  950.                 }
  951.  
  952.                 if ($ac == '') {
  953.                         $ac = '';
  954.                 } else {
  955.                         if ($GLOBALS['TS_CF']['subdomain'] && in_array($app, $GLOBALS['TS_CF']['subdomain']['app']) || $GLOBALS['TS_CF']['appdomain'][$app]) {
  956.                                 $ac = $ac;
  957.                         } else {
  958.                                 $ac = '/' . $ac;
  959.                         }
  960.                 }
  961.                 if ($GLOBALS['TS_CF']['subdomain'] && in_array($app, $GLOBALS['TS_CF']['subdomain']['app']) || $GLOBALS['TS_CF']['appdomain'][$app]) {
  962.                         $url = $ac . $str;
  963.                 } else {
  964.                         $url = $app . $ac . $str;
  965.                 }
  966.         } elseif ($urlset == 7) {
  967.                 foreach ($params as $k => $v) {
  968.                         $str .= '/' . $k . '/' . $v;
  969.                 }
  970.                 $str = str_replace('/id', '', $str);
  971.                 if ($ac == '') {
  972.                         $ac = '';
  973.                 } else {
  974.                         if ($GLOBALS['TS_CF']['subdomain'] && in_array($app, $GLOBALS['TS_CF']['subdomain']['app']) || $GLOBALS['TS_CF']['appdomain'][$app]) {
  975.                                 $ac = $ac;
  976.                         } else {
  977.                                 $ac = '/' . $ac;
  978.                         }
  979.                 }
  980.  
  981.                 $page = strpos($str, 'page');
  982.  
  983.                 if ($page) {
  984.                         if ($GLOBALS['TS_CF']['subdomain'] && in_array($app, $GLOBALS['TS_CF']['subdomain']['app']) || $GLOBALS['TS_CF']['appdomain'][$app]) {
  985.                                 $url = $ac . $str;
  986.                         } else {
  987.                                 $url = $app . $ac . $str;
  988.                         }
  989.                 } else {
  990.                         if ($GLOBALS['TS_CF']['subdomain'] && in_array($app, $GLOBALS['TS_CF']['subdomain']['app']) || $GLOBALS['TS_CF']['appdomain'][$app]) {
  991.                                 if ($ac == '') {
  992.                                         $url = '';
  993.                                 } else {
  994.                                         $url = $ac . $str . '/';
  995.                                 }
  996.                         } else {
  997.                                 $url = $app . $ac . $str . '/';
  998.                         }
  999.                 }
  1000.         }
  1001.         if ($GLOBALS['TS_CF']['subdomain'] && in_array($app, $GLOBALS['TS_CF']['subdomain']['app'])) {
  1002.                 return 'http://' . $app . '.' . $GLOBALS['TS_CF']['subdomain']['domain'] . '/' . $url;
  1003.         } elseif ($GLOBALS['TS_CF']['appdomain'][$app]) {
  1004.                 return 'http://' . $GLOBALS['TS_CF']['appdomain'][$app] . '/' . $url;
  1005.         } else {
  1006.                 return SITE_URL . $url;
  1007.         }
  1008. }
  1009.  
  1010. /**
  1011.  * reurl
  1012.  */
  1013. function reurl() {
  1014.         global $tsMySqlCache;
  1015.  
  1016.         $arrSuffix = array(
  1017.                 '?from=message',
  1018.             '?from=singlemessage',
  1019.             '?from=singlemessage&from=singlemessage',
  1020.         '?from=groupmessage',
  1021.         '?from=timeline',
  1022.         '?tdsourcetag=s_pctim_aiomsg',
  1023.         '?_wv=1031',
  1024.         '?tdsourcetag=s_pcqq_aiomsg',
  1025.         '?from=groupmessage&isappinstalled=0',
  1026.         '?from=groupmessage&isappinstalled=1',
  1027.         '?from=singlemessage&isappinstalled=0',
  1028.         '?from=singlemessage&isappinstalled=1',
  1029.     );
  1030.  
  1031.  
  1032.         $options = fileRead('data/system_options.php');
  1033.  
  1034.         if ($options == '') {
  1035.                 $options = $tsMySqlCache -> get('system_options');
  1036.         }
  1037.  
  1038.         $scriptName = explode('index.php', $_SERVER['SCRIPT_NAME']);
  1039.  
  1040.         //获取到网站目录
  1041.         $rurl = substr($_SERVER['REQUEST_URI'], strlen($scriptName[0]));
  1042.         //过滤掉网站目录剩下的就是URL部分
  1043.  
  1044.         if (strpos($rurl, 'index.php?') === false  || strpos ( $rurl, '?openid=' ) == true  || strpos ( $rurl, '?from=' ) == true){
  1045.  
  1046.                 if (preg_match('/index.php/i', $rurl)) {
  1047.                         $rurl = str_replace('index.php', '', $rurl);
  1048.  
  1049.                         $rurl = substr($rurl, 1);
  1050.                         $params = $rurl;
  1051.                 } else {
  1052.                         $params = $rurl;
  1053.                 }
  1054.  
  1055.                 if ($rurl) {
  1056.  
  1057.             if($options['site_urltype'] == 2) {
  1058.                 //形式:index.php/group/topic/id-1
  1059.                 $params = explode('/', $params);
  1060.  
  1061.                 foreach ($params as $p => $v) {
  1062.                     switch ($p) {
  1063.                         case 0 :
  1064.                             $_GET['app'] = $v;
  1065.                             break;
  1066.                         case 1 :
  1067.                             $_GET['ac'] = $v;
  1068.                             break;
  1069.  
  1070.                             // 处理TAG
  1071.                             if ($_GET['ac'] == 'tag') {
  1072.                                 $_GET['id'] = $v;
  1073.                                 break;
  1074.                             }
  1075.  
  1076.                         default :
  1077.                             $kv = explode('-', $v);
  1078.                             if (count($kv) > 1) {
  1079.                                 $_GET[$kv[0]] = $kv[1];
  1080.                             } else {
  1081.                                 $_GET['params' . $p] = $kv[0];
  1082.                             }
  1083.  
  1084.                             break;
  1085.                     }
  1086.                 }
  1087.             }elseif ($options['site_urltype'] == 3) {
  1088.                                 // http://localhost/group-topic-id-1.html
  1089.                                 $params = explode('.', $params);
  1090.  
  1091.                                 $params = explode('-', $params[0]);
  1092.  
  1093.                                 foreach ($params as $p => $v) {
  1094.                                         switch ($p) {
  1095.                                                 case 0 :
  1096.                             if(in_array($v,$arrSuffix)) $v='home';
  1097.                                                         $_GET['app'] = $v;
  1098.                                                         break;
  1099.                                                 case 1 :
  1100.                                                         $_GET['ac'] = $v;
  1101.                                                         break;
  1102.                                                 default :
  1103.                                                         if ($v)
  1104.                                                                 $kv[] = $v;
  1105.  
  1106.                                                         break;
  1107.                                         }
  1108.                                 }
  1109.  
  1110.                                 $ck = count($kv) / 2;
  1111.  
  1112.                                 if ($ck >= 2) {
  1113.                                         //$arrKv = array_chunk($kv, $ck);
  1114.                                         $arrKv = array_chunk($kv, 2);
  1115.                                         foreach ($arrKv as $key => $item) {
  1116.                                                 $_GET[$item[0]] = $item[1];
  1117.                                         }
  1118.                                 } elseif ($ck == 1) {
  1119.                                         $_GET[$kv[0]] = $kv[1];
  1120.                                 } else {
  1121.                                 }
  1122.                         } elseif ($options['site_urltype'] == 4) {
  1123.                                 // http://localhost/group/topic/id-1
  1124.                                 $params = explode('/', $params);
  1125.  
  1126.                                 foreach ($params as $p => $v) {
  1127.                                         switch ($p) {
  1128.                                                 case 0 :
  1129.                                                         $_GET['app'] = $v;
  1130.                                                         break;
  1131.                                                 case 1 :
  1132.                                                         $_GET['ac'] = $v;
  1133.                                                         break;
  1134.                                                 default :
  1135.                                                         $kv = explode('-', $v);
  1136.  
  1137.                                                         if (count($kv) > 1) {
  1138.                                                                 $_GET[$kv[0]] = $kv[1];
  1139.                                                         } else {
  1140.                                                                 $_GET['params' . $p] = $kv[0];
  1141.                                                         }
  1142.                                                         break;
  1143.                                         }
  1144.                                 }
  1145.                         } elseif ($options['site_urltype'] == 5) {
  1146.                                 // http://localhost/group/topic/1<后面可以继续跟参数:?a=b&c=d>
  1147.  
  1148.                 $params = explode('?',$params);
  1149.                 $otherParams = $params[1];
  1150.                 $params = explode('/', $params[0]);
  1151.                 $arrOther = explode('&',$otherParams);
  1152.  
  1153.                                 foreach ($params as $p => $v) {
  1154.                                         switch ($p) {
  1155.                                                 case 0 :
  1156.                                                         $_GET['app'] = $v;
  1157.                                                         break;
  1158.                                                 case 1 :
  1159.                                                         $_GET['ac'] = $v;
  1160.                                                         if (empty($_GET['ac']))
  1161.                                                                 $_GET['ac'] = 'index';
  1162.                                                         break;
  1163.                                                 case 2 :
  1164.                                                         if ((( int )$v) > 0) {
  1165.                                                                 $_GET['id'] = $v;
  1166.                                                                 break;
  1167.                                                         }
  1168.                                                         // 处理TAG
  1169.                                                         if ($_GET['ac'] == 'tag') {
  1170.                                                                 $_GET['id'] = $v;
  1171.                                                                 break;
  1172.                                                         }
  1173.  
  1174.                                                 default :
  1175.                                                         $_GET[$v] = $params[$p + 1];
  1176.                                                         break;
  1177.                                         }
  1178.                                 }
  1179.  
  1180.  
  1181.                 if($arrOther){
  1182.                     foreach($arrOther as $key=>$item){
  1183.                         $arrKv = explode('=',$item);
  1184.                         $_GET[$arrKv[0]] = $arrKv[1];
  1185.                     }
  1186.                 }
  1187.  
  1188.  
  1189.                         } elseif ($options['site_urltype'] == 6) {
  1190.                                 // http://localhost/group/topic/id/1
  1191.                                 $params = explode('/', $params);
  1192.  
  1193.                                 foreach ($params as $p => $v) {
  1194.                                         switch ($p) {
  1195.                                                 case 0 :
  1196.                                                         $_GET['app'] = $v;
  1197.                                                         break;
  1198.                                                 case 1 :
  1199.                                                         $_GET['ac'] = $v;
  1200.                                                         break;
  1201.                                                 default :
  1202.                                                         $_GET[$v] = $params[$p + 1];
  1203.                                                         break;
  1204.                                         }
  1205.                                 }
  1206.                         } elseif ($options['site_urltype'] == 7) {
  1207.                                 // http://localhost/group/topic/1/
  1208.                                 $params = explode('/', $params);
  1209.                                 //var_dump($params);exit;
  1210.                                 foreach ($params as $p => $v) {
  1211.                                         switch ($p) {
  1212.                                                 case 0 :
  1213.                             if(in_array($v,$arrSuffix)) $v='home';
  1214.                                                         $_GET['app'] = $v;
  1215.                                                         break;
  1216.                                                 case 1 :
  1217.                             if(in_array($v,$arrSuffix)) $v='index';
  1218.                                                         $_GET['ac'] = $v;
  1219.                                                         if (empty($_GET['ac']))
  1220.                                                                 $_GET['ac'] = 'index';
  1221.                                                         break;
  1222.                                                 case 2 :
  1223.                                                         if ((( int )$v) > 0) {
  1224.                                                                 $_GET['id'] = $v;
  1225.                                                                 break;
  1226.                                                         }
  1227.                                                         // 处理TAG
  1228.                                                         if ($_GET['ac'] == 'tag') {
  1229.                                                                 $_GET['id'] = $v;
  1230.                                                                 break;
  1231.                                                         }
  1232.  
  1233.                                                 default :
  1234.                                                         $_GET[$v] = $params[$p + 1];
  1235.  
  1236.                                                         break;
  1237.                                         }
  1238.                                 }
  1239.                         }
  1240.                 }
  1241.         }
  1242.  
  1243.         //规划化URL规则,对跳转到首页不符合规则的进行404处理
  1244.     /*
  1245.         if ($_GET['app'] == '' && $_GET['ac'] == '' && $rurl) {
  1246.                 header("HTTP/1.1 404 Not Found");
  1247.                 header("Status: 404 Not Found");
  1248.                 echo '404 page by <a href="http://www.thinksaas.cn/">www.thinksaas.cn</a>';
  1249.                 exit ;
  1250.         }
  1251.     */
  1252.  
  1253. }
  1254.  
  1255. /**
  1256.  * 辅助APP二级域名
  1257.  */
  1258. function reurlsubdomain() {
  1259.         global $tsMySqlCache;
  1260.         $options = fileRead('data/system_options.php');
  1261.         if ($options == '') {
  1262.                 $options = $tsMySqlCache -> get('system_options');
  1263.         }
  1264.  
  1265.         $scriptName = explode('index.php', $_SERVER['SCRIPT_NAME']);
  1266.         $rurl = substr($_SERVER['REQUEST_URI'], strlen($scriptName[0]));
  1267.  
  1268.         if (strpos($rurl, '?') == false) {
  1269.  
  1270.                 if (preg_match('/index.php/i', $rurl)) {
  1271.                         $rurl = str_replace('index.php', '', $rurl);
  1272.                         $rurl = substr($rurl, 1);
  1273.                         $params = $rurl;
  1274.                 } else {
  1275.                         $params = $rurl;
  1276.                 }
  1277.  
  1278.                 if ($rurl) {
  1279.  
  1280.                         if ($options['site_urltype'] == 3) {
  1281.                                 // http://group.thinksaas.cn/topic-id-1.html
  1282.                                 $params = explode('.', $params);
  1283.  
  1284.                                 $params = explode('-', $params[0]);
  1285.  
  1286.                                 foreach ($params as $p => $v) {
  1287.                                         switch ($p) {
  1288.                                                 case 0 :
  1289.                                                         $_GET['ac'] = $v;
  1290.                                                         break;
  1291.                                                 default :
  1292.                                                         if ($v)
  1293.                                                                 $kv[] = $v;
  1294.  
  1295.                                                         break;
  1296.                                         }
  1297.                                 }
  1298.  
  1299.                                 $ck = count($kv) / 2;
  1300.  
  1301.                                 if ($ck >= 2) {
  1302.                                         $arrKv = array_chunk($kv, $ck);
  1303.                                         foreach ($arrKv as $key => $item) {
  1304.                                                 $_GET[$item[0]] = $item[1];
  1305.                                         }
  1306.                                 } elseif ($ck == 1) {
  1307.                                         $_GET[$kv[0]] = $kv[1];
  1308.                                 } else {
  1309.                                 }
  1310.                         } elseif ($options['site_urltype'] == 4) {
  1311.                                 // http://group.thinksaas.cn/topic/id-1
  1312.                                 $params = explode('/', $params);
  1313.  
  1314.                                 foreach ($params as $p => $v) {
  1315.                                         switch ($p) {
  1316.                                                 case 0 :
  1317.                                                         $_GET['ac'] = $v;
  1318.                                                         break;
  1319.                                                 default :
  1320.                                                         $kv = explode('-', $v);
  1321.  
  1322.                                                         if (count($kv) > 1) {
  1323.                                                                 $_GET[$kv[0]] = $kv[1];
  1324.                                                         } else {
  1325.                                                                 $_GET['params' . $p] = $kv[0];
  1326.                                                         }
  1327.                                                         break;
  1328.                                         }
  1329.                                 }
  1330.                         } elseif ($options['site_urltype'] == 5) {
  1331.                                 // http://group.thinksaas.cn/topic/1
  1332.                                 $params = explode('/', $params);
  1333.  
  1334.                                 foreach ($params as $p => $v) {
  1335.                                         switch ($p) {
  1336.                                                 case 0 :
  1337.                                                         $_GET['ac'] = $v;
  1338.                                                         if (empty($_GET['ac']))
  1339.                                                                 $_GET['ac'] = 'index';
  1340.                                                         break;
  1341.                                                 case 1 :
  1342.                                                         if ((( int )$v) > 0) {
  1343.                                                                 $_GET['id'] = $v;
  1344.                                                                 break;
  1345.                                                         }
  1346.                                                 default :
  1347.                                                         $_GET[$v] = $params[$p + 1];
  1348.                                                         break;
  1349.                                         }
  1350.                                 }
  1351.                         } elseif ($options['site_urltype'] == 6) {
  1352.                                 // http://group.thinksaas.cn/topic/id/1
  1353.                                 $params = explode('/', $params);
  1354.  
  1355.                                 foreach ($params as $p => $v) {
  1356.                                         switch ($p) {
  1357.                                                 case 0 :
  1358.                                                         $_GET['ac'] = $v;
  1359.                                                         break;
  1360.                                                 default :
  1361.                                                         $_GET[$v] = $params[$p + 1];
  1362.                                                         break;
  1363.                                         }
  1364.                                 }
  1365.                         } elseif ($options['site_urltype'] == 7) {
  1366.                                 // http://group.thinksaas.cn/topic/1/
  1367.                                 $params = explode('/', $params);
  1368.  
  1369.                                 foreach ($params as $p => $v) {
  1370.                                         switch ($p) {
  1371.                                                 case 0 :
  1372.                                                         $_GET['ac'] = $v;
  1373.                                                         if (empty($_GET['ac']))
  1374.                                                                 $_GET['ac'] = 'index';
  1375.                                                         break;
  1376.                                                 case 1 :
  1377.                                                         if ((( int )$v) > 0) {
  1378.                                                                 $_GET['id'] = $v;
  1379.                                                                 break;
  1380.                                                         }
  1381.                                                 default :
  1382.                                                         $_GET[$v] = $params[$p + 1];
  1383.                                                         break;
  1384.                                         }
  1385.                                 }
  1386.                         } else {
  1387.  
  1388.                                 $params = explode('/', $params);
  1389.  
  1390.                                 foreach ($params as $p => $v) {
  1391.                                         switch ($p) {
  1392.                                                 case 0 :
  1393.                                                         $_GET['ac'] = $v;
  1394.                                                         break;
  1395.                                                 default :
  1396.                                                         $kv = explode('-', $v);
  1397.                                                         if (count($kv) > 1) {
  1398.                                                                 $_GET[$kv[0]] = $kv[1];
  1399.                                                         } else {
  1400.                                                                 $_GET['params' . $p] = $kv[0];
  1401.                                                         }
  1402.                                                         break;
  1403.                                         }
  1404.                                 }
  1405.                         }
  1406.                 }
  1407.         }
  1408. }
  1409.  
  1410. /**
  1411.  * 检测目录是否可写1可写,0不可写
  1412.  * @param string $file
  1413.  * @return number
  1414.  */
  1415. function iswriteable($file) {
  1416.         if (is_dir($file)) {
  1417.                 $dir = $file;
  1418.                 if ($fp = fopen("$dir/test.txt", 'w')) {
  1419.                         fclose($fp);
  1420.                         unlink("$dir/test.txt");
  1421.                         $writeable = 1;
  1422.                 } else {
  1423.                         $writeable = 0;
  1424.                 }
  1425.         } else {
  1426.                 if ($fp = fopen($file, 'a+')) {
  1427.                         fclose($fp);
  1428.                         $writeable = 1;
  1429.                 } else {
  1430.                         $writeable = 0;
  1431.                 }
  1432.         }
  1433.         return $writeable;
  1434. }
  1435.  
  1436. /**
  1437.  * 删除目录下文件
  1438.  * @param unknown $dir
  1439.  */
  1440. function delDirFile($dir) {
  1441.         $arrFiles = tsScanDir($dir, 'files');
  1442.         foreach ($arrFiles as $item) {
  1443.                 unlink($dir . '/' . $item);
  1444.         }
  1445. }
  1446.  
  1447.  
  1448. /**
  1449.  * ThinkSAAS专用上传函数
  1450.  *
  1451.  * @param [type] $files                 要上传的文件 如$_FILES['photo']
  1452.  * @param [type] $projectid             上传针对的项目id 如$userid
  1453.  * @param [type] $dir                   上传到目录 如 user
  1454.  * @param array $uptypes                上传类型,数组 array('jpg','png','gif')
  1455.  * @param string $sy                    图片水印文件sy.png,文件请放到public/images/目录下
  1456.  * @return void                                 失败返回false,成功返回数组:array('name'=>'','path'=>'','url'=>'','path'=>'','size'=>'')
  1457.  */
  1458. function tsUpload($files, $projectid, $dir, $uptypes=array(),$sy='') {
  1459.         if($GLOBALS['TS_SITE']['file_upload_type']==1){
  1460.                 #阿里云oss上传存储
  1461.                 $result = tsUploadAliOss($files, $projectid, $dir, $uptypes);
  1462.         }else{
  1463.                 #本地上传存储
  1464.                 $result = tsUploadLocal($files, $projectid, $dir, $uptypes,$sy);
  1465.         }
  1466.         return $result;
  1467. }
  1468.  
  1469. /**
  1470.  * 本地上传存储
  1471.  */
  1472. function tsUploadLocal($files, $projectid, $dir, $uptypes=array(),$sy=''){
  1473.         if ($files['size'] > 0) {
  1474.  
  1475.         $upload_max_filesize = ini_get('upload_max_filesize')*1048576;
  1476.  
  1477.         if($upload_max_filesize<$files['size']){
  1478.             getJson('PHP允许上传文件的最大尺寸为'.ini_get('upload_max_filesize'),1);
  1479.         }
  1480.  
  1481.         $arrType = explode('.', strtolower($files['name']));
  1482.         $type = end($arrType);
  1483.  
  1484.                 //上传图片大小控制
  1485.                 if(in_array($type,array('jpg','jpeg','png','gif'))) {
  1486.  
  1487.             $type = getImagetype($files['tmp_name']);
  1488.  
  1489.             if (!in_array($type, $uptypes)) {
  1490.                 getJson('图片错误!',1);
  1491.             }
  1492.  
  1493.             if ($GLOBALS['TS_SITE']['photo_size']) {
  1494.                 $upsize = $GLOBALS['TS_SITE']['photo_size'] * 1048576;
  1495.  
  1496.                 if ($files ['size'] > $upsize) {
  1497.                     //tsNotice('上传图片不能超过' . $GLOBALS['TS_SITE']['photo_size'] . 'M,请修改小点后再上传!');
  1498.  
  1499.                     $img = Image::make($files['tmp_name']);
  1500.  
  1501.                 }
  1502.  
  1503.             }
  1504.  
  1505.         }
  1506.  
  1507.         $path = getDirPath($projectid);
  1508.         $dest_dir = 'uploadfile/' . $dir . '/' . $path;
  1509.         createFolders($dest_dir);
  1510.  
  1511.                 //$ext = pathinfo($files['name'],PATHINFO_EXTENSION);
  1512.  
  1513.                 if (in_array($type, $uptypes)) {
  1514.  
  1515.                         $name = $projectid . '.' . $type;
  1516.  
  1517.                         $dest = $dest_dir . '/' . $name;
  1518.  
  1519.                         // 先删除
  1520.                         unlink($dest);
  1521.                         // 后上传
  1522.             if($img){
  1523.                 //处理大图统一为800宽度,高度自适应
  1524.                 $img->resize(800, null, function ($constraint) {
  1525.                     $constraint->aspectRatio();
  1526.                                 });
  1527.                                
  1528.                                 #加水印
  1529.                                 if($sy){
  1530.                                         $watermark = Image::make('public/images/'.$sy);
  1531.                                         $img->insert($watermark, 'bottom-left',10,10);
  1532.                                 }
  1533.  
  1534.                 $img->save($dest);
  1535.             }else{
  1536.                 move_uploaded_file($files['tmp_name'], mb_convert_encoding($dest, "gb2312", "UTF-8"));
  1537.             }
  1538.  
  1539.                         chmod($dest, 0777);
  1540.  
  1541.                         $filesize = filesize($dest);
  1542.                         if (intval($filesize) > 0) {
  1543.  
  1544.                 #继续验证图片
  1545.                if(in_array($type,array('jpg','jpeg','png','gif'))) {
  1546.  
  1547.                     try{
  1548.                         Image::make($dest);
  1549.                     }catch (Exception $e){
  1550.                         echo 'Message: ' .$e->getMessage();
  1551.                         unlink($dest);
  1552.                         return false;
  1553.                         exit();
  1554.                     }
  1555.  
  1556.                 }
  1557.  
  1558.                                 return array(
  1559.                                         'name' => tsFilter($files['name']),
  1560.                                         'path' => $path,
  1561.                                         'url' => $path . '/' . $name,
  1562.                                         'type' => $type,
  1563.                                         'size' => tsFilter($files['size'])
  1564.                                 );
  1565.  
  1566.                         } else {
  1567.                                 return false;
  1568.                         }
  1569.                 } else {
  1570.                         return false;
  1571.                 }
  1572.  
  1573.         }
  1574. }
  1575.  
  1576. /**
  1577.  * 阿里云oss上传存储
  1578.  */
  1579. function tsUploadAliOss($files, $projectid, $dir, $uptypes=array()){
  1580.         if ($files['size'] > 0) {
  1581.                 $path = getDirPath($projectid);
  1582.                 $dest_dir = 'uploadfile/' . $dir . '/' . $path;
  1583.  
  1584.                 $arrType = explode('.', strtolower($files['name']));
  1585.         $type = end($arrType);
  1586.  
  1587.                 //上传图片大小控制
  1588.                 if(in_array($type,array('jpg','jpeg','png','gif'))) {
  1589.             $type = getImagetype($files['tmp_name']);
  1590.             if (!in_array($type, $uptypes)) {
  1591.                 getJson('图片错误!',1);
  1592.             }
  1593.         }
  1594.        
  1595.                 if (in_array($type, $uptypes)) {
  1596.                        
  1597.                         $name = $projectid . '.' . $type;
  1598.                         $dest = $dest_dir . '/' . $name;
  1599.                         // 阿里云主账号AccessKey拥有所有API的访问权限,风险很高。强烈建议您创建并使用RAM账号进行API访问或日常运维,请登录RAM控制台创建RAM账号。
  1600.                         $accessKeyId = $GLOBALS['TS_SITE']['alioss_accesskey_id'];
  1601.                         $accessKeySecret = $GLOBALS['TS_SITE']['alioss_accesskey_secret'];
  1602.                         // Endpoint以杭州为例,其它Region请按实际情况填写。
  1603.                         $endpoint = $GLOBALS['TS_SITE']['alioss_endpoint'];
  1604.                         // 设置存储空间名称。
  1605.                         $bucket= $GLOBALS['TS_SITE']['alioss_bucket'];
  1606.                         // 设置文件名称。
  1607.                         $object = $dest;
  1608.                         // <yourLocalFile>由本地文件路径加文件名包括后缀组成,例如/users/local/myfile.txt。
  1609.                         $filePath = $files['tmp_name'];
  1610.  
  1611.                         try{
  1612.                                
  1613.                                 $ossClient = new OssClient($accessKeyId, $accessKeySecret, $endpoint);
  1614.                                 $ossClient->uploadFile($bucket, $object, $filePath);
  1615.  
  1616.                                 return array(
  1617.                                         'name' => tsFilter($files['name']),
  1618.                                         'path' => $path,
  1619.                                         'url' => $path . '/' . $name,
  1620.                                         'type' => $type,
  1621.                                         'size' => tsFilter($files['size'])
  1622.                                 );
  1623.  
  1624.                         } catch(OssException $e) {
  1625.                                
  1626.                                 /*
  1627.                                 printf(__FUNCTION__ . ": FAILED\n");
  1628.                                 printf($e->getMessage() . "\n");
  1629.                                 return;
  1630.                                 */
  1631.                                 return false;
  1632.                         }
  1633.                         //print(__FUNCTION__ . ": OK" . "\n");
  1634.                 }else{
  1635.                         return false;
  1636.                 }
  1637.         }else{
  1638.                 return false;
  1639.         }
  1640. }
  1641.  
  1642. /**
  1643.  * 阿里云分片上传
  1644.  *
  1645.  * @param [type] $projectid
  1646.  * @param [type] $dir
  1647.  * @param [type] $type
  1648.  * @return void
  1649.  */
  1650. function AliOssChunkUpload($projectid,$dir,$type){
  1651.  
  1652.         $path = getDirPath($projectid);
  1653.         $dest_dir = 'uploadfile/' . $dir . '/' . $path;
  1654.         $name = $projectid . '.' . $type;
  1655.         $dest = $dest_dir . '/' . $name;
  1656.  
  1657.         // 阿里云主账号AccessKey拥有所有API的访问权限,风险很高。强烈建议您创建并使用RAM账号进行API访问或日常运维,请登录RAM控制台创建RAM账号。
  1658.         $accessKeyId = $GLOBALS['TS_SITE']['alioss_accesskey_id'];
  1659.         $accessKeySecret = $GLOBALS['TS_SITE']['alioss_accesskey_secret'];
  1660.         // Endpoint以杭州为例,其它Region请按实际情况填写。
  1661.         $endpoint = $GLOBALS['TS_SITE']['alioss_endpoint'];
  1662.         $bucket= $GLOBALS['TS_SITE']['alioss_bucket'];
  1663.         $object = $dest;
  1664.         $file = $dest;
  1665.  
  1666.         $options = array(
  1667.                 OssClient::OSS_CHECK_MD5 => true,
  1668.                 OssClient::OSS_PART_SIZE => 2, //2M
  1669.         );
  1670.         try{
  1671.                 $ossClient = new OssClient($accessKeyId, $accessKeySecret, $endpoint);
  1672.                 $ossClient->multiuploadFile($bucket, $object, $file, $options);
  1673.  
  1674.                 return true;
  1675.  
  1676.         } catch(OssException $e) {
  1677.                 #printf(__FUNCTION__ . ": FAILED\n");
  1678.                 #printf($e->getMessage() . "\n");
  1679.                 #return;
  1680.  
  1681.                 return false;
  1682.  
  1683.         }
  1684.         #print(__FUNCTION__ . ":  OK" . "\n");
  1685.  
  1686. }
  1687.  
  1688. /**
  1689.  * 阿里云oss网络文件存储
  1690.  *
  1691.  * @param [type] $fileurl
  1692.  * @param [type] $project
  1693.  * @param [type] $dir
  1694.  * @param [type] $filetype
  1695.  * @return void
  1696.  */
  1697. function tsUploadAliOssUrl($fileurl,$projectid,$dir,$filetype){
  1698.         $path = getDirPath($projectid);
  1699.         $dest_dir = 'uploadfile/' . $dir . '/' . $path;
  1700.         $name = $projectid . '.' . $filetype;
  1701.         $dest = $dest_dir . '/' . $name;
  1702.         // 阿里云主账号AccessKey拥有所有API的访问权限,风险很高。强烈建议您创建并使用RAM账号进行API访问或日常运维,请登录RAM控制台创建RAM账号。
  1703.         $accessKeyId = $GLOBALS['TS_SITE']['alioss_accesskey_id'];
  1704.         $accessKeySecret = $GLOBALS['TS_SITE']['alioss_accesskey_secret'];
  1705.         // Endpoint以杭州为例,其它Region请按实际情况填写。
  1706.         $endpoint = $GLOBALS['TS_SITE']['alioss_endpoint'];
  1707.         // 设置存储空间名称。
  1708.         $bucket= $GLOBALS['TS_SITE']['alioss_bucket'];
  1709.         // 设置文件名称。
  1710.         $object = $dest;
  1711.         // 配置文件内容。
  1712.         $content = file_get_contents($fileurl);
  1713.         try{
  1714.                 $ossClient = new OssClient($accessKeyId, $accessKeySecret, $endpoint);
  1715.  
  1716.                 $ossClient->putObject($bucket, $object, $content);
  1717.  
  1718.                 return array(
  1719.                         'name' => $name,
  1720.                         'path' => $path,
  1721.                         'url' => $path . '/' . $name,
  1722.                         'type' => $filetype,
  1723.                 );
  1724.  
  1725.         } catch(OssException $e) {
  1726.                 /*
  1727.                 printf(__FUNCTION__ . ": FAILED\n");
  1728.                 printf($e->getMessage() . "\n");
  1729.                 return;
  1730.                 */
  1731.  
  1732.                 return false;
  1733.  
  1734.         }
  1735.         //print(__FUNCTION__ . ": OK" . "\n");
  1736. }
  1737.  
  1738. /**
  1739.  * 删除阿里云oss文件
  1740.  *
  1741.  * @param [type] $file
  1742.  * @return void
  1743.  */
  1744. function deleteAliOssFile($file){
  1745.         $accessKeyId = $GLOBALS['TS_SITE']['alioss_accesskey_id'];
  1746.         $accessKeySecret = $GLOBALS['TS_SITE']['alioss_accesskey_secret'];
  1747.         $endpoint = $GLOBALS['TS_SITE']['alioss_endpoint'];
  1748.         $bucket= $GLOBALS['TS_SITE']['alioss_bucket'];
  1749.         $object = $file;
  1750.         try{
  1751.                 $ossClient = new OssClient($accessKeyId, $accessKeySecret, $endpoint);
  1752.                 $ossClient->deleteObject($bucket, $object);
  1753.                 return true;
  1754.         } catch(OssException $e) {
  1755.                 /*
  1756.                 printf(__FUNCTION__ . ": FAILED\n");
  1757.                 printf($e->getMessage() . "\n");
  1758.                 return;
  1759.                 */
  1760.                 return false;
  1761.         }
  1762.         //print(__FUNCTION__ . ": OK" . "\n");
  1763. }
  1764.  
  1765. function gmt_iso8601($time) {
  1766.     $dtStr = date("c", $time);
  1767.     $mydatetime = new DateTime($dtStr);
  1768.     $expiration = $mydatetime->format(DateTime::ISO8601);
  1769.     $pos = strpos($expiration, '+');
  1770.     $expiration = substr($expiration, 0, $pos);
  1771.     return $expiration."Z";
  1772. }
  1773.  
  1774. /**
  1775.  * 阿里云oss直传签名
  1776.  */
  1777. function AliOssSIgn(){
  1778.         $id= $GLOBALS['TS_SITE']['alioss_accesskey_id'];          // 请填写您的AccessKeyId。
  1779.         $key= $GLOBALS['TS_SITE']['alioss_accesskey_secret'];     // 请填写您的AccessKeySecret。
  1780.         // $host的格式为 bucketname.endpoint,请替换为您的真实信息。
  1781.         $host = $GLOBALS['TS_SITE']['alioss_endpoint'];  
  1782.         // $callbackUrl为上传回调服务器的URL,请将下面的IP和Port配置为您自己的真实URL信息。
  1783.         $callbackUrl = SITE_URL.'index.php?app=pubs&ac=aliyun&ts=callback';
  1784.         //$dir = 'user-dir-prefix/';          // 用户上传文件时指定的前缀。
  1785.         $dir = '';
  1786.        
  1787.         $callback_param = array('callbackUrl'=>$callbackUrl,
  1788.                                 'callbackBody'=>'filename=${object}&size=${size}&mimeType=${mimeType}&height=${imageInfo.height}&width=${imageInfo.width}',
  1789.                                 'callbackBodyType'=>"application/x-www-form-urlencoded");
  1790.         $callback_string = json_encode($callback_param);
  1791.  
  1792.         $base64_callback_body = base64_encode($callback_string);
  1793.         $now = time();
  1794.         $expire = 30;  //设置该policy超时时间是10s. 即这个policy过了这个有效时间,将不能访问。
  1795.         $end = $now + $expire;
  1796.         $expiration = gmt_iso8601($end);
  1797.  
  1798.         //最大文件大小.用户可以自己设置
  1799.         $condition = array(0=>'content-length-range', 1=>0, 2=>1048576000);
  1800.         $conditions[] = $condition;
  1801.  
  1802.         // 表示用户上传的数据,必须是以$dir开始,不然上传会失败,这一步不是必须项,只是为了安全起见,防止用户通过policy上传到别人的目录。
  1803.         $start = array(0=>'starts-with', 1=>'$key', 2=>$dir);
  1804.         $conditions[] = $start;
  1805.  
  1806.         $arr = array('expiration'=>$expiration,'conditions'=>$conditions);
  1807.         $policy = json_encode($arr);
  1808.         $base64_policy = base64_encode($policy);
  1809.         $string_to_sign = $base64_policy;
  1810.         $signature = base64_encode(hash_hmac('sha1', $string_to_sign, $key, true));
  1811.  
  1812.         $response = array();
  1813.         $response['accessid'] = $id;
  1814.         $response['host'] = $host;
  1815.         $response['policy'] = $base64_policy;
  1816.         $response['signature'] = $signature;
  1817.         $response['expire'] = $end;
  1818.         $response['callback'] = $base64_callback_body;
  1819.         $response['dir'] = $dir;  // 这个参数是设置用户上传文件时指定的前缀。
  1820.         //echo json_encode($response);
  1821.  
  1822.         return $response;
  1823.  
  1824. }
  1825.  
  1826. /**
  1827.  * 网络文件上传(非图片)
  1828.  *
  1829.  * @param [type] $fileurl
  1830.  * @param [type] $projectid
  1831.  * @param [type] $dir
  1832.  * @param array $uptypes
  1833.  * @return void
  1834.  */
  1835. function tsUploadUrl($fileurl, $projectid, $dir, $uptypes=array()) {
  1836.  
  1837.         $arrType = explode('.', $fileurl);
  1838.         //转小写一下
  1839.         $type = array_pop($arrType);
  1840.  
  1841.                 if (in_array($type, $uptypes)) {
  1842.  
  1843.                         if($GLOBALS['TS_SITE']['file_upload_type']==1){
  1844.                                 $result = tsUploadAliOssUrl($fileurl,$projectid,$dir,$type);
  1845.                                 return $result;
  1846.                         }else{
  1847.  
  1848.                                 $menu2 = intval($projectid / 1000);
  1849.                                 $menu1 = intval($menu2 / 1000);
  1850.                                 $path = $menu1 . '/' . $menu2;
  1851.                                 $dest_dir = 'uploadfile/' . $dir . '/' . $path;
  1852.                                 createFolders($dest_dir);
  1853.        
  1854.                                 $name = $projectid . '.' . $type;
  1855.                                 $dest = $dest_dir . '/' . $name;
  1856.                                 //先删除
  1857.                                 unlink($dest);
  1858.                                 //后上传
  1859.                                 $img = file_get_contents($fileurl);
  1860.                                 file_put_contents($dest, $img);
  1861.                                 chmod($dest, 0777);
  1862.                                 $filesize = filesize($dest);
  1863.                                 if (intval($filesize) > 0) {
  1864.                                         return array('name' => $name, 'path' => $path, 'url' => $path . '/' . $name, 'type' => $type, 'size' => $filesize, );
  1865.                                 } else {
  1866.                                         return false;
  1867.                                 }
  1868.  
  1869.                         }
  1870.  
  1871.                 } else {
  1872.  
  1873.                         return false;
  1874.  
  1875.                 }
  1876.        
  1877.  
  1878.        
  1879. }
  1880.  
  1881.  
  1882. /**
  1883.  * 拷贝已经上传的文件
  1884.  */
  1885. function tsUploadCopy($dfile,$projectid, $dir){
  1886.         $menu2 = intval($projectid / 1000);
  1887.         $menu1 = intval($menu2 / 1000);
  1888.         $path = $menu1 . '/' . $menu2;
  1889.         $dest_dir = 'uploadfile/' . $dir . '/' . $path;
  1890.         createFolders($dest_dir);
  1891.         $arrType = explode('.', strtolower($dfile));
  1892.         // 转小写一下
  1893.         $type = array_pop($arrType);
  1894.         $name = $projectid . '.' . $type;
  1895.         $dest = $dest_dir . '/' . $name;
  1896.  
  1897.         unlink($dest);
  1898.         copy($dfile, $dest);
  1899.         unlink($dfile);
  1900.  
  1901.         chmod($dest, 0777);
  1902.         return array(
  1903.                 'path' => $path,
  1904.                 'url' => $path . '/' . $name
  1905.         );
  1906. }
  1907.  
  1908.  
  1909. /**
  1910.  * 扫描目录
  1911.  * @param unknown $dir
  1912.  * @param string $isDir
  1913.  * @return mixed
  1914.  */
  1915. function tsScanDir($dir, $isDir = null) {
  1916.         if ($isDir == null) {
  1917.                 $dirs = array_filter(glob($dir . '/' . '*'), 'is_dir');
  1918.         } else {
  1919.                 $dirs = array_filter(glob($dir . '/' . '*'), 'is_file');
  1920.         }
  1921.  
  1922.         foreach ($dirs as $key => $item) {
  1923.                 $y = explode('/', $item);
  1924.                 $arrDirs[] = array_pop($y);
  1925.         }
  1926.  
  1927.         return $arrDirs;
  1928. }
  1929.  
  1930. /**
  1931.  * 删除目录下所有文件
  1932.  * @param string $dir
  1933.  */
  1934. function rmrf($dir) {
  1935.         foreach (glob ( $dir ) as $file) {
  1936.                 if (is_dir($file)) {
  1937.                         rmrf("$file/*");
  1938.                         rmdir($file);
  1939.                 } else {
  1940.                         unlink($file);
  1941.                 }
  1942.         }
  1943. }
  1944.  
  1945. /**
  1946.  * 内容url解析
  1947.  * @param string $content
  1948.  * @return mixed
  1949.  */
  1950. function urlcontent($content) {
  1951.         $pattern = '/(http:\/\/|https:\/\/|ftp:\/\/)([\w:\/\.\?=&-_#]+)/is';
  1952.         $content = @preg_replace($pattern, '<a rel="nofollow" target="_blank" href="\1\2">\1\2</a>', $content);
  1953.         return $content;
  1954. }
  1955.  
  1956. /**
  1957.  * 反序列化为UTF-8
  1958.  * @param string $serial_str
  1959.  * @return mixed
  1960.  */
  1961. function mb_unserialize($serial_str, $t = NULL) {
  1962.     $serial_str = @preg_replace_callback('!s:(\d+):"(.*?)";!s', function( $m ){
  1963.         return 's:'.strlen($m[2]).':"'.$m[2].'";';
  1964.     }, $serial_str);
  1965.     $serial_str = str_replace("\r", "", $serial_str);
  1966.     return unserialize($serial_str);
  1967. }
  1968. /**
  1969.  * 反序列化为ASC
  1970.  * @param string $serial_str
  1971.  * @return mixed
  1972.  */
  1973. function asc_unserialize($serial_str) {
  1974.     $serial_str = @preg_replace_callback('!s:(\d+):"(.*?)";!s', function( $m ){
  1975.         return 's:'.strlen($m[2]).':"'.$m[2].';"';
  1976.     }, $serial_str);
  1977.     $serial_str = str_replace("\r", "", $serial_str);
  1978.     return unserialize($serial_str);
  1979. }
  1980.  
  1981. /**
  1982.  * 二进制上传
  1983.  * @param int $projectid
  1984.  * @param unknown $dir
  1985.  * @param unknown $type
  1986.  * @return multitype:string unknown
  1987.  */
  1988. function tsXupload($projectid, $dir, $type) {
  1989.         $menu2 = intval($projectid / 1000);
  1990.         $menu1 = intval($menu2 / 1000);
  1991.         $path = $menu1 . '/' . $menu2;
  1992.         $dest_dir = 'uploadfile/' . $dir . '/' . $path;
  1993.         createFolders($dest_dir);
  1994.  
  1995.         $name = $projectid . '.' . $type;
  1996.         // 要生成的图片名字
  1997.  
  1998.         $dest = $dest_dir . '/' . $name;
  1999.  
  2000.         // 先删除
  2001.         unlink($dest);
  2002.  
  2003.         $xmlstr = file_get_contents('php://input');
  2004.                
  2005.         $jpg = $xmlstr;
  2006.         // 得到post过来的二进制原始数据
  2007.         $file = fopen($dest, "w");
  2008.         // 打开文件准备写入
  2009.         fwrite($file, $jpg);
  2010.         // 写入
  2011.         fclose($file);
  2012.         // 关闭
  2013.  
  2014.         chmod($dest, 0777);
  2015.  
  2016.         return array('name' => $name, 'path' => $path, 'url' => $path . '/' . $name, 'type' => $type);
  2017. }
  2018.  
  2019. /**
  2020.  * 记录日志
  2021.  * @param unknown $file
  2022.  * @param string $data
  2023.  */
  2024. function logging($file, $data) {
  2025.         !is_dir('tslogs') ? mkdir('tslogs', 0777) : '';
  2026.         $dfile = 'tslogs/' . $file;
  2027.  
  2028.         $filesize = abs(filesize($dfile));
  2029.  
  2030.         // 文件重命名
  2031.         if ($filesize > 1024000) {
  2032.                 rename($dfile, $dfile . date('His'));
  2033.         }
  2034.  
  2035.         $fd = fopen($dfile, "a+");
  2036.         fputs($fd, $data);
  2037.         fclose($fd);
  2038. }
  2039.  
  2040. /**
  2041.  * 记录用户日志
  2042.  * @param unknown $array
  2043.  * @param unknown $userid
  2044.  */
  2045. function userlog(&$array, $userid) {
  2046.         if (is_array($array)) {
  2047.                 foreach ($array as $key => $value) {
  2048.                         if (!is_array($value)) {
  2049.                                 $data = "UserId:" . $userid . "\n";
  2050.                                 $data .= "IP:" . getIp() . "\n";
  2051.                                 $data .= "TIME:" . date('Y-m-d H:i:s') . "\n";
  2052.                                 $data .= "URL:" . $_SERVER['REQUEST_URI'] . "\n";
  2053.                                 $data .= "DATA:" . $data . "\n";
  2054.                                 $data .= "--------------------------------------\n";
  2055.                                 logging(date('Ymd') . '-' . $userid . '.txt', $data);
  2056.                         } else {
  2057.                 userlog($array[$key],$userid);
  2058.                         }
  2059.                 }
  2060.         }
  2061. }
  2062.  
  2063. /**
  2064.  * 过滤脚本代码
  2065.  * @param string $text
  2066.  * @return mixed
  2067.  */
  2068. function cleanJs($text) {
  2069.         $text = tsTrim($text);
  2070.         //$text = stripslashes ( $text );
  2071.         // 完全过滤注释
  2072.         $text = @preg_replace('/<!--?.*-->/', '', $text);
  2073.         // 完全过滤动态代码
  2074.         $text = @preg_replace('/<\?|\?>/', '', $text);
  2075.         // 完全过滤js
  2076.         $text = @preg_replace('/<script?.*\/script>/', '', $text);
  2077.         // 过滤多余html
  2078.         $text = @preg_replace('/<\/?(html|head|meta|link|base|body|title|style|script|form|iframe|frame|frameset|math|maction|marquee)[^><]*>/i', '', $text);
  2079.         // 过滤on事件lang js
  2080.         while (preg_match('/(<[^><]+)(data|onmouse|onexit|onclick|onkey|onsuspend|onabort|onactivate|onafterprint|onafterupdate|onbeforeactivate|onbeforecopy|onbeforecut|onbeforedeactivate|onbeforeeditfocus|onbeforepaste|onbeforeprint|onbeforeunload|onbeforeupdate|onblur|onbounce|oncellchange|onchange|onclick|oncontextmenu|oncontrolselect|oncopy|oncut|ondataavailable|ondatasetchanged|ondatasetcomplete|ondblclick|ondeactivate|ondrag|ondragend|ondragenter|ondragleave|ondragover|ondragstart|ondrop|onerror|onerrorupdate|onfilterchange|onfinish|onfocus|onfocusin|onfocusout|onhelp|onkeydown|onkeypress|onkeyup|onlayoutcomplete|onload|onlosecapture|onmousedown|onmouseenter|onmouseleave|onmousemove|onmouseout|onmouseover|onmouseup|onmousewheel|onmove|onmoveend|onmovestart|onpaste|onpropertychange|onreadystatechange|onreset|onresize|onresizeend|onresizestart|onrowenter|onrowexit|onrowsdelete|onrowsinserted|onscroll|onselect|onselectionchange|onselectstart|onstart|onstop|onsubmit|onunload)[^><]+/i', $text, $mat)) {
  2081.                 $text = str_replace($mat[0], $mat[1], $text);
  2082.         }
  2083.         while (preg_match('/(<[^><]+)(window\.|javascript:|js:|about:|file:|document\.|vbs:|cookie)([^><]*)/i', $text, $mat)) {
  2084.                 $text = str_replace($mat[0], $mat[1] . $mat[3], $text);
  2085.         }
  2086.         return $text;
  2087. }
  2088.  
  2089. /**
  2090.  * 输入安全过滤
  2091.  * @param string $text
  2092.  * @return mixed
  2093.  */
  2094. function tsClean($text,$js=0) {
  2095.         $text = stripslashes(tsTrim($text));
  2096.         //去除前后空格,并去除反斜杠
  2097.         //$text = br2nl($text); //将br转换成/n
  2098.  
  2099.         //处理正文图片
  2100.         if($GLOBALS['TS_SITE']['photo_check']==1){
  2101.                 preg_match_all('/<img[^>]*src="([^"]*)"[^>]*>/i',$text, $matchs);   //主要
  2102.                 $arrImage = $matchs[1];
  2103.                 foreach($arrImage as $key=>$item){
  2104.                         if(substr( $item, 0, 1 )=='/'){
  2105.                                 $item = substr($item,1);
  2106.                         }
  2107.                         if(getimagesize($item)==false){
  2108.                                 getJson('内容中存在非法图片:'.$item,$js,0);
  2109.                         }
  2110.                 }
  2111.         }
  2112.  
  2113.         ///////XSS start
  2114.         require_once 'thinksaas/xsshtml.class.php';
  2115.         $xss = new XssHtml($text);
  2116.         $text = $xss -> getHtml();
  2117.         //$text = substr ($text, 4);//去除左边<p>标签
  2118.         //$text = substr ($text, 0,-5);//去除右边</p>标签
  2119.         ///////XSS end
  2120.  
  2121.         //$text = html_entity_decode($text,ENT_NOQUOTES,"utf-8");//把 HTML 实体转换为字符
  2122.         //$text = strip_tags($text); //去掉HTML及PHP标记
  2123.         //$text = cleanJs ( $text );
  2124.  
  2125.         $text = htmlentities($text, ENT_NOQUOTES, "utf-8");
  2126.         //把字符转换为 HTML 实体
  2127.  
  2128.         return $text;
  2129. }
  2130.  
  2131. /*
  2132.  * 针对tsClean函数会过滤很多html标签的补充函数
  2133.  */
  2134. function tsCleanContent($text){
  2135.     $text = stripslashes(tsTrim($text));
  2136.     $text = htmlentities($text, ENT_NOQUOTES, "utf-8");
  2137.     return $text;
  2138. }
  2139.  
  2140. /*
  2141.  * @text 内容
  2142.  * @tp 内容分页
  2143.  * @url URL
  2144.  */
  2145. function tsDecode($text, $tp = 1) {
  2146.         if($text){
  2147.  
  2148.                 $text = tsTrim($text);
  2149.                 //$text = html_entity_decode(stripslashes($text), ENT_NOQUOTES, "utf-8");
  2150.                 $text = html_entity_decode($text, ENT_NOQUOTES, "utf-8");
  2151.                 $text = str_replace('<br /><br />', '<br />', $text);
  2152.  
  2153.                 //分页处理
  2154.                 /*
  2155.                 $arrText = explode('_ueditor_page_break_tag_', $text);
  2156.  
  2157.                 if ($arrText) {
  2158.                         $tp = $tp - 1;
  2159.                         $text = $arrText[$tp];
  2160.                 }
  2161.                 */
  2162.  
  2163.         }
  2164.    
  2165.  
  2166.         return $text;
  2167. }
  2168.  
  2169. /*
  2170.  * 输出标题处理
  2171.  */
  2172. function tsTitle($title) {
  2173.         $title = stripslashes($title);
  2174.         $title = htmlspecialchars($title);
  2175.         return $title;
  2176. }
  2177.  
  2178. /*
  2179.  * 输出内容截取
  2180.  */
  2181. function tsCutContent($text, $length = 50) {
  2182.         $text = cututf8(t(tsDecode($text)), 0, $length);
  2183.         return $text;
  2184. }
  2185.  
  2186. /**
  2187.  * 敏感词直接替换成***
  2188.  *
  2189.  * @param [type] $text
  2190.  * @return void
  2191.  */
  2192. function antiWord($text,$isjs=0){
  2193.         $strWord = fileRead('system_anti_word.php');
  2194.     if($strWord==''){
  2195.         $strWord = $GLOBALS['tsMySqlCache']->get('system_anti_word');
  2196.         }
  2197.         $arrWord = explode('|',$strWord);
  2198.         $arrWords = array_combine($arrWord, array_fill(0, count($arrWord), '***'));
  2199.  
  2200.         $result = strtr($text, $arrWords);
  2201.  
  2202.         #敏感词处理方式
  2203.         if($GLOBALS['TS_SITE']['anti_word_type']==1){
  2204.                 return $result;
  2205.         }else{
  2206.                 if(stristr($result, '***') === FALSE) {
  2207.                         return $result;
  2208.                 }else{
  2209.                         getJson('非法操作!',$isjs,0);
  2210.                 }
  2211.         }
  2212.  
  2213.        
  2214.  
  2215. }
  2216.  
  2217. /*
  2218.  * tpCount()
  2219.  */
  2220. function tpCount($text) {
  2221.         $arrText = explode('_ueditor_page_break_tag_', $text);
  2222.         return count($arrText);
  2223. }
  2224.  
  2225. /*
  2226.  * 内容分页
  2227.  */
  2228. function tpPage($text, $app, $ac, $arr) {
  2229.         $tpCount = tpCount($text);
  2230.         $tpUrl = '';
  2231.         if ($tpCount > 1) {
  2232.                 $tpUrl = '<div class="page">';
  2233.                 for ($i = 1; $i <= $tpCount; $i++) {
  2234.                         $arr['tp'] = $i;
  2235.                         $tpUrl .= '<a rel="nofollow" href="' . tsUrl($app, $ac, $arr) . '">' . $i . '</a>';
  2236.                 }
  2237.                 $tpUrl .= '</div>';
  2238.         }
  2239.         return $tpUrl;
  2240. }
  2241.  
  2242. /**
  2243.  * 统计字符长度
  2244.  * @param string $str
  2245.  * @return number
  2246.  */
  2247. function count_string_len($str) {
  2248.         // return (strlen($str)+mb_strlen($str,'utf-8'))/2; //开启了php_mbstring.dll扩展
  2249.         $name_len = strlen($str);
  2250.         $temp_len = 0;
  2251.         for ($i = 0; $i < $name_len; ) {
  2252.                 if (strpos('abcdefghijklmnopqrstvuwxyz0123456789', $str[$i]) === false) {
  2253.                         $i = $i + 3;
  2254.                         $temp_len += 2;
  2255.                 } else {
  2256.                         $i = $i + 1;
  2257.                         $temp_len += 1;
  2258.                 }
  2259.         }
  2260.         return $temp_len;
  2261. }
  2262.  
  2263. /**
  2264.  * 针对特殊字符或者内容的特殊过滤
  2265.  * @param string $value
  2266.  * @return Ambigous <string, mixed>
  2267.  */
  2268. function tsFilter($value) {
  2269.         $value = tsTrim($value);
  2270.         //定义不允许提交的SQl命令和关键字
  2271.         $words = array();
  2272.         $words[] = "add ";
  2273.         $words[] = "and ";
  2274.         $words[] = "count ";
  2275.         $words[] = "order ";
  2276.         $words[] = "table ";
  2277.         $words[] = "by ";
  2278.         $words[] = "create ";
  2279.         $words[] = "delete ";
  2280.         $words[] = "drop ";
  2281.         $words[] = "from ";
  2282.         $words[] = "grant ";
  2283.         $words[] = "insert ";
  2284.         $words[] = "select ";
  2285.         $words[] = "truncate ";
  2286.         $words[] = "update ";
  2287.         $words[] = "use ";
  2288.         $words[] = "--";
  2289.         $words[] = "#";
  2290.         $words[] = "group_concat";
  2291.         $words[] = "column_name";
  2292.         $words[] = "information_schema.columns";
  2293.         $words[] = "table_schema";
  2294.         $words[] = "union ";
  2295.         $words[] = "where ";
  2296.         $words[] = "alert";
  2297.         $value = strtolower($value);
  2298.         //转换为小写
  2299.         foreach ($words as $word) {
  2300.                 while (strstr($value, $word)) {
  2301.                         $value = str_replace($word, '', $value);
  2302.                 }
  2303.         }
  2304.  
  2305.         return $value;
  2306. }
  2307.  
  2308. function tsgpc(&$array) {
  2309.         //如果是数组,遍历数组,递归调用
  2310.         if (is_array($array)) {
  2311.                 foreach ($array as $k => $v) {
  2312.                         $array[$k] = tsgpc($v);
  2313.                 }
  2314.         } else if (is_string($array)) {
  2315.                 //使用addslashes函数来处理
  2316.                 //$array = addslashes ( closetags($array) );
  2317.                 $array = addslashes($array);
  2318.         } else if (is_numeric($array)) {
  2319.                 $array = intval($array);
  2320.         }
  2321.         return $array;
  2322. }
  2323.  
  2324. /**
  2325.  * 检查标签是否闭合
  2326.  */
  2327. function closetags($html) {
  2328.         preg_match_all('#<(?!meta|img|br|hr|input\b)\b([a-z]+)(?: .*)?(?<![/|/ ])>#iU', $html, $result);
  2329.         $openedtags = $result[1];
  2330.         preg_match_all('#</([a-z]+)>#iU', $html, $result);
  2331.         $closedtags = $result[1];
  2332.         $len_opened = count($openedtags);
  2333.         $len_closed = count($closedtags);
  2334.         if ($len_closed == $len_opened) {
  2335.                 return $html;
  2336.         }
  2337.         $openedtags = array_reverse($openedtags);
  2338.         for ($i = 0; $i < $len_opened; $i++) {
  2339.                 if (!in_array($openedtags[$i], $closedtags)) {
  2340.                         $html .= '</' . $openedtags[$i] . '>';
  2341.                 } else {
  2342.                         unset($closedtags[array_search($openedtags[$i], $closedtags)]);
  2343.                 }
  2344.         }
  2345.         return $html;
  2346. }
  2347.  
  2348. /*
  2349.  * url参数检测过滤
  2350.  * @parameter  例如$app,$ac,$ts等的参数
  2351.  */
  2352. function tsUrlCheck($parameter) {
  2353.        
  2354.         $parameter = tsTrim($parameter);
  2355.         if($parameter){
  2356.         $arrStr = str_split($parameter);
  2357.         $strOk = '%-_1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
  2358.         foreach ($arrStr as $key => $item) {
  2359.             if (stripos($strOk, $item) === false) {
  2360.                 header ( "HTTP/1.1 404 Not Found" );
  2361.                 header ( "Status: 404 Not Found" );
  2362.                 exit;
  2363.             }
  2364.         }
  2365.  
  2366.         //return strtolower($parameter);//转小写
  2367.         return $parameter;
  2368.     }
  2369.  
  2370. }
  2371.  
  2372. function ludou_width_height($content) {
  2373.         $images = array();
  2374.         preg_match_all('/<img (.*?)\/>/', $content, $images);
  2375.         if (!empty($images)) {
  2376.                 foreach ($images[1] as $index => $value) {
  2377.                         $img = array();
  2378.                         preg_match_all('/(width)=("[^"]*")/i', $images[1][$index], $img);
  2379.  
  2380.                         if (!empty($img[2])) {
  2381.                                 $width = tsTrim($img[2][0], '"');
  2382.                                 if ($width > 630) {
  2383.                                         $new_img = @preg_replace('/(width)=("[^"]*")/i', 'width="630"', $images[0][$index]);
  2384.                                         $content = str_replace($images[0][$index], $new_img, $content);
  2385.  
  2386.                                         $new_img2 = @preg_replace('/(height)=("[^"]*")/i', 'height="100%"', $new_img);
  2387.                                         $content = str_replace($new_img, $new_img2, $content);
  2388.                                 }
  2389.                         }
  2390.                 }
  2391.         }
  2392.         return $content;
  2393. }
  2394.  
  2395. /**
  2396.  * Convert BR tags to nl
  2397.  *
  2398.  * @param string The string to convert
  2399.  * @return string The converted string
  2400.  */
  2401. function br2nl($string) {
  2402.         return @preg_replace('/\<br(\s*)?\/?\>/i', "\n", $string);
  2403. }
  2404.  
  2405. /*
  2406.  * 判断手机访问
  2407.  */
  2408. function isMobile() {
  2409.         // 如果有HTTP_X_WAP_PROFILE则一定是移动设备
  2410.         if (isset($_SERVER['HTTP_X_WAP_PROFILE'])) {
  2411.                 return true;
  2412.         }
  2413.         // 如果via信息含有wap则一定是移动设备,部分服务商会屏蔽该信息
  2414.         if (isset($_SERVER['HTTP_VIA'])) {
  2415.                 // 找不到为flase,否则为true
  2416.                 return stristr($_SERVER['HTTP_VIA'], "wap") ? true : false;
  2417.         }
  2418.         // 脑残法,判断手机发送的客户端标志,兼容性有待提高
  2419.         if (isset($_SERVER['HTTP_USER_AGENT'])) {
  2420.                 $clientkeywords = array('nokia', 'sony', 'ericsson', 'mot', 'samsung', 'htc', 'sgh', 'lg', 'sharp', 'sie-', 'philips', 'panasonic', 'alcatel', 'lenovo', 'iphone', 'ipod', 'blackberry', 'meizu', 'android', 'netfront', 'symbian', 'ucweb', 'windowsce', 'palm', 'operamini', 'operamobi', 'openwave', 'nexusone', 'cldc', 'midp', 'wap', 'mobile');
  2421.                 // 从HTTP_USER_AGENT中查找手机浏览器的关键字
  2422.                 if (preg_match("/(" . implode('|', $clientkeywords) . ")/i", strtolower($_SERVER['HTTP_USER_AGENT']))) {
  2423.                         return true;
  2424.                 }
  2425.         }
  2426.         // 协议法,因为有可能不准确,放到最后判断
  2427.         if (isset($_SERVER['HTTP_ACCEPT'])) {
  2428.                 // 如果只支持wml并且不支持html那一定是移动设备
  2429.                 // 如果支持wml和html但是wml在html之前则是移动设备
  2430.                 if ((strpos($_SERVER['HTTP_ACCEPT'], 'vnd.wap.wml') !== false) && (strpos($_SERVER['HTTP_ACCEPT'], 'text/html') === false || (strpos($_SERVER['HTTP_ACCEPT'], 'vnd.wap.wml') < strpos($_SERVER['HTTP_ACCEPT'], 'text/html')))) {
  2431.                         return true;
  2432.                 }
  2433.         }
  2434.         return false;
  2435. }
  2436.  
  2437. /**
  2438.  * 判断是否微信浏览器访问
  2439.  */
  2440. function isWeixin(){
  2441.     if ( strpos($_SERVER['HTTP_USER_AGENT'], 'MicroMessenger') !== false ) {
  2442.         return true;
  2443.     }else{
  2444.                 return false;
  2445.         }
  2446. }
  2447.  
  2448. /**
  2449.  * @param $msg          返回的数据或者提示语
  2450.  * @param int $js       判断是否是通过json方式处理的
  2451.  * @param int $status   状态:0不正常、1正常
  2452.  * @param string $url   当status=2要跳转的url页面
  2453.  * @param string $data  返回的数据
  2454.  * @param int $isview   是否有html模版文件加载0无1有
  2455.  */
  2456. function getJson($msg, $js = 1, $status = 1, $url = '', $data='',$isview=0) {
  2457.     if ($js) {
  2458.         header("Content-type: application/json;charset=utf-8");
  2459.         if ($url) {
  2460.             echo json_encode(array(
  2461.                 'status' => $status,
  2462.                 'msg'=>$msg,
  2463.                 'data' => $data,
  2464.                 'url' => $url,
  2465.             ));
  2466.         } else {
  2467.             echo json_encode(array(
  2468.                 'status' => $status,
  2469.                 'msg'=>$msg,
  2470.                 'data' => $data,
  2471.                                 'url' => '',
  2472.             ));
  2473.         }
  2474.         exit ;
  2475.     }
  2476.     if($isview==0){
  2477.         if($js == 0 && $url) {
  2478.             header('Location: ' . $url);
  2479.             exit ;
  2480.         } else {
  2481.             tsNotice($msg);
  2482.         }
  2483.     }
  2484. }
  2485.  
  2486. /**
  2487.  * 为项目上传绝对路径的图片(只针对图片)
  2488.  *
  2489.  * @param [type] $photourl
  2490.  * @param [type] $projectid
  2491.  * @param [type] $dir
  2492.  * @param string $type
  2493.  * @return void
  2494.  */
  2495. function tsUploadPhotoUrl($photourl, $projectid, $dir,$type='') {
  2496.  
  2497.         if($type==''){
  2498.                 $arrType = explode('.', strtolower($photourl));
  2499.                 $type = array_pop($arrType);
  2500.         }
  2501.  
  2502.         if($GLOBALS['TS_SITE']['file_upload_type']==1){
  2503.  
  2504.                 $result = tsUploadAliOssUrl($photourl,$projectid,$dir,$type);
  2505.                 return $result;
  2506.  
  2507.         }else{
  2508.  
  2509.                 $menu2 = intval($projectid / 1000);
  2510.                 $menu1 = intval($menu2 / 1000);
  2511.                 $path = $menu1 . '/' . $menu2;
  2512.                 $dest_dir = 'uploadfile/' . $dir . '/' . $path;
  2513.                 createFolders($dest_dir);
  2514.        
  2515.                 $name = $projectid . '.' . $type;
  2516.        
  2517.                 $dest = $dest_dir . '/' . $name;
  2518.        
  2519.                 $fgc = file_get_contents($photourl);
  2520.                 if($fgc) {
  2521.                         $fpc = file_put_contents($dest, $fgc);
  2522.                         if ($fpc){
  2523.                                 return array(
  2524.                                         'path' => $path,
  2525.                                         'url' => $path . '/' . $name,
  2526.                                         'type' => $type,
  2527.                                 );
  2528.                         }else{
  2529.                                 return false;
  2530.                         }
  2531.                 }else{
  2532.                         return false;
  2533.                 }
  2534.  
  2535.         }
  2536.  
  2537. }
  2538.  
  2539.  
  2540. /**
  2541.  * 获取域名的根域名
  2542.  * @param $url
  2543.  * @return string
  2544.  */
  2545. function getdomain($url) {
  2546.     $host = strtolower ( $url );
  2547.     if (strpos ( $host, '/' ) !== false) {
  2548.         $parse = @parse_url ( $host );
  2549.         $host = $parse ['host'];
  2550.     }
  2551.     $topleveldomaindb = array ('com', 'edu', 'gov', 'int', 'mil', 'net', 'org', 'biz', 'info', 'pro', 'name', 'museum', 'coop', 'aero', 'xxx', 'idv', 'mobi', 'cc', 'me','in','io','gg','co' );
  2552.     $str = '';
  2553.     foreach ( $topleveldomaindb as $v ) {
  2554.         $str .= ($str ? '|' : '') . $v;
  2555.     }
  2556.  
  2557.     $matchstr = "[^\.]+\.(?:(" . $str . ")|\w{2}|((" . $str . ")\.\w{2}))$";
  2558.     if (preg_match ( "/" . $matchstr . "/ies", $host, $matchs )) {
  2559.         $domain = $matchs ['0'];
  2560.     } else {
  2561.         $domain = $host;
  2562.     }
  2563.     return $domain;
  2564. }
  2565.  
  2566.  
  2567. /**
  2568.  * 验证手机号,支持13、15、18、17、19号段
  2569.  *
  2570.  * @param $phone
  2571.  * @return bool
  2572.  */
  2573. function isPhone($phone){
  2574.         //if(preg_match("/^13[0-9]{1}[0-9]{8}$|15[0-9]{1}[0-9]{8}$|18[0-9]{1}[0-9]{8}$|17[0-9]{1}[0-9]{8}$|19[0-9]{1}[0-9]{8}$/",$phone)){
  2575.         if(preg_match("/^1[0-9]{10}$/",$phone)){
  2576.                 return true;
  2577.         }else{
  2578.                 return false;
  2579.         }
  2580. }
  2581.  
  2582.  
  2583. /**
  2584.  * 将字符串转换为数组
  2585.  *
  2586.  * @param string $data         
  2587.  * @return array
  2588.  */
  2589. function string2array($data) {
  2590.         if ($data == '') {
  2591.                 return array ();
  2592.         }
  2593.         if (is_array ( $data )) {
  2594.                 return $data;
  2595.         }
  2596.         if (strpos ( $data, 'array' ) !== false && strpos ( $data, 'array' ) === 0) {
  2597.                 //eval ( "\$data = $data;" );
  2598.                 eval("\$data = \"$data\";");
  2599.                 return $data;
  2600.         }
  2601.         return unserialize ( ($data) ); //unserialize ( new_stripslashes ( $data ) );
  2602. }
  2603.  
  2604. /**
  2605.  * 将数组转换为字符串
  2606.  *
  2607.  * @param array $data          
  2608.  * @param bool $isformdata             
  2609.  * @return string
  2610.  *
  2611.  */
  2612. function array2string($data, $isformdata = 1) {
  2613.         if ($data == '') {
  2614.                 return '';
  2615.         }
  2616.         if ($isformdata) {
  2617.                 $data = ($data); //new_stripslashes ( $data );
  2618.         }
  2619.         return serialize ( $data );
  2620. }
  2621.  
  2622.  
  2623. /**
  2624.  * 返回404提示
  2625.  */
  2626. function ts404(){
  2627.     header ( "HTTP/1.1 404 Not Found" );
  2628.     header ( "Status: 404 Not Found" );
  2629.     $title = '404';
  2630.     include pubTemplate ( "404" );
  2631.     exit ();
  2632. }
  2633.  
  2634.  
  2635. /**
  2636.  * URL跳转
  2637.  *
  2638.  * @param $url
  2639.  */
  2640. function tsHeaderUrl($url){
  2641.         header('Location: '.$url);
  2642.         exit;
  2643. }
  2644.  
  2645.  
  2646. /**
  2647.  * curl get 方式请求url
  2648.  * @param $URL
  2649.  * @return bool|mixed
  2650.  */
  2651. function curl_get_file_contents($URL){
  2652.     $c = curl_init();
  2653.     curl_setopt($c, CURLOPT_RETURNTRANSFER, 1);
  2654. //curl_setopt($c, CURLOPT_HEADER, 1);//输出远程服务器的header信息
  2655.     curl_setopt($c, CURLOPT_USERAGENT, 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 2.0.50727;http://www.thinksaas.cn)');
  2656.     curl_setopt($c, CURLOPT_URL, $URL);
  2657.     $contents = curl_exec($c);
  2658.     curl_close($c);
  2659.     if ($contents) {return $contents;}
  2660.     else {return FALSE;}
  2661. }
  2662.  
  2663. /**
  2664.  * 获取客户端IP地址
  2665.  * @return string
  2666.  */
  2667. function get_client_ip() {
  2668.     if(getenv('HTTP_CLIENT_IP')){
  2669.         $client_ip = getenv('HTTP_CLIENT_IP');
  2670.     } elseif(getenv('HTTP_X_FORWARDED_FOR')) {
  2671.         $client_ip = getenv('HTTP_X_FORWARDED_FOR');
  2672.     } elseif(getenv('REMOTE_ADDR')) {
  2673.         $client_ip = getenv('REMOTE_ADDR');
  2674.     } else {
  2675.         $client_ip = $_SERVER['REMOTE_ADDR'];
  2676.     }
  2677.     return $client_ip;
  2678. }
  2679.  
  2680.  
  2681. /*
  2682.  * 通过curl模拟post的请求
  2683.  */
  2684. function sendDataByCurl($url,$data=array()){
  2685.     //对空格进行转义
  2686.     $url = str_replace(' ','+',$url);
  2687.     $ch = curl_init();
  2688.     //设置选项,包括URL
  2689.     curl_setopt($ch, CURLOPT_URL, "$url");
  2690.     curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  2691.     curl_setopt($ch, CURLOPT_HEADER, 0);
  2692.     curl_setopt($ch,CURLOPT_TIMEOUT,60);  //定义超时3秒钟
  2693.     // POST数据
  2694.     curl_setopt($ch, CURLOPT_POST, 1);
  2695.     // 把post的变量加上
  2696.     curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));    //所需传的数组用http_bulid_query()函数处理一下,就ok了
  2697.  
  2698.     //执行并获取url地址的内容
  2699.     $output = curl_exec($ch);
  2700.     $errorCode = curl_errno($ch);
  2701.     //释放curl句柄
  2702.     curl_close($ch);
  2703.     if(0 !== $errorCode) {
  2704.         return false;
  2705.     }
  2706.     return $output;
  2707.  
  2708. }
  2709.  
  2710. /*
  2711.  * 获取毫秒时间
  2712.  */
  2713. function getTimestamp(){
  2714.     $time = explode ( " ", microtime () );
  2715.     $time = $time [1] . ($time [0] * 1000);
  2716.     $time2 = explode ( ".", $time );
  2717.     $time = $time2 [0];
  2718.     return $time;
  2719. }
  2720.  
  2721. /**
  2722.  * 取得根域名
  2723.  * @param string $domain 域名
  2724.  * @return string 返回根域名
  2725.  */
  2726. function GetUrlToDomain($domain) {
  2727.  
  2728.     $arrDomain= parse_url($domain);
  2729.  
  2730.         //print_r($arrDomain);
  2731.  
  2732.     $domain = $arrDomain['path'];
  2733.  
  2734.     $re_domain = '';
  2735.     $domain_postfix_cn_array = array('com', 'net', 'org', 'gov', 'edu', 'com.cn', 'cn','cc','me','tv','la','net.cn','org.cn','top','wang','hk','co','pw','ren','asia','biz','gov.cn','tw','com.tw','us','tel','info','website','host','io','press','mobi','wiki','io');
  2736.  
  2737.     $domain = str_replace('http://','',$domain);
  2738.     $domain = str_replace('https://','',$domain);
  2739.  
  2740.     $array_domain = explode('.', $domain);
  2741.     $array_num = count($array_domain) - 1;
  2742.     if ($array_domain[$array_num] == 'cn') {
  2743.         if (in_array($array_domain[$array_num - 1], $domain_postfix_cn_array)) {
  2744.             $re_domain = $array_domain[$array_num - 2] . '.' . $array_domain[$array_num - 1] . '.' . $array_domain[$array_num];
  2745.         } else {
  2746.             $re_domain = $array_domain[$array_num - 1] . '.' . $array_domain[$array_num];
  2747.         }
  2748.     } else {
  2749.         $re_domain = $array_domain[$array_num - 1] . '.' . $array_domain[$array_num];
  2750.     }
  2751.  
  2752.     $re_domain = str_replace('/','',$re_domain);
  2753.  
  2754.     return $re_domain;
  2755. }
  2756.  
  2757. /*
  2758.  * 对抓去到的内容做简单过滤(过滤空白字符,便于正则匹配)
  2759.  */
  2760. function _prefilter($output) {
  2761.         $output=preg_replace("/\/\/[\S\f\t\v ]*?;[\r|\n]/", "", $output);
  2762.         $output=preg_replace("/\<\!\-\-[\s\S]*?\-\-\>/", "", $output);
  2763.         $output=preg_replace("/\>[\s]+\</", "><", $output);
  2764.         $output=preg_replace("/;[\s]+/", ";", $output);
  2765.         $output=preg_replace("/[\s]+\}/", "}", $output);
  2766.         $output=preg_replace("/}[\s]+/", "}", $output);
  2767.         $output=preg_replace("/\{[\s]+/", "{", $output);
  2768.         $output=preg_replace("/([\s]){2,}/", "$1", $output);
  2769.         $output=preg_replace("/[\s]+\=[\s]+/", "=", $output);
  2770.         return $output;
  2771. }
  2772.  
  2773.  
  2774. /*
  2775.  * 去除文本内容中图片的高度和宽度
  2776.  */
  2777. function cleanContentImgWH($content){
  2778.     $search = '/(<img.*?)width=(["\'])?.*?(?(2)\2|\s)([^>]+>)/is';
  2779.     $content = preg_replace($search,'$1$3',$content);
  2780.     $search1 = '/(<img.*?)height=(["\'])?.*?(?(2)\2|\s)([^>]+>)/is';   //去除图片的高度
  2781.     $style = '/(<img.*?)style=(["\'])?.*?(?(2)\2|\s)([^>]+>)/is';
  2782.     $content =  preg_replace($search1,'$1$3',$content);
  2783.     $content =  preg_replace($style,'$1$3',$content);
  2784.     return $content;
  2785. }
  2786.  
  2787.  
  2788. /**
  2789.  * 将正文转换成手机端支持的html正文
  2790.  *
  2791.  * @param [type] $html
  2792.  * @return void
  2793.  */
  2794. function mobileHtml($html){
  2795.         $html = strip_tags($html,'<div><p><img><br>');
  2796.         $html = preg_replace( '/(<div.*?)(style=.+?[\'|"])|((width)=[\'"]+[0-9]+[\'"]+)|((height)=[\'"]+[0-9]+[\'"]+)/i', '$1' , $html);
  2797.         $html = preg_replace( '/(<p.*?)(style=.+?[\'|"])|((width)=[\'"]+[0-9]+[\'"]+)|((height)=[\'"]+[0-9]+[\'"]+)/i', '$1' , $html);
  2798.         $html = preg_replace( '/(<img.*?)(style=.+?[\'|"])|((width)=[\'"]+[0-9]+[\'"]+)|((height)=[\'"]+[0-9]+[\'"]+)/i', '$1' , $html);
  2799.         $html = str_replace('<img ','<img style="max-width:100%;height:auto" ',$html);
  2800.         return $html;
  2801. }
  2802.  
  2803.  
  2804. //获取正文图片
  2805. function getTextPhotos($text,$num=0){
  2806.     $pattern="/<[img|IMG].*?src=[\'|\"](.*?(?:[\.gif|\.jpg|\.png]))[\'|\"].*?[\/]?>/";
  2807.     preg_match_all($pattern,$text,$match);
  2808.     $arrPhoto = $match[1];
  2809.  
  2810.     $count = count($arrPhoto);
  2811.  
  2812.     if($count>$num && $num){
  2813.  
  2814.         for($i=0;$i<=$num-1;$i++){
  2815.             $arrPhotos[$i]=$arrPhoto[$i];
  2816.         }
  2817.  
  2818.         return $arrPhotos;
  2819.  
  2820.     }else{
  2821.         return $arrPhoto;
  2822.     }
  2823. }
  2824.  
  2825. /**
  2826.  * 获取时区数组
  2827.  */
  2828. function getArrTimezone(){
  2829.     return array (
  2830.         'Pacific/Kwajalein' => '(GMT -12:00) International Date Line West',
  2831.         'Pacific/Samoa' => '(GMT -11:00) Midway Island, Samoa',
  2832.         'Pacific/Honolulu' => '(GMT -10:00) Hawaii',
  2833.         'US/Alaska' => '(GMT -9:00) Alaska',
  2834.         'US/Pacific' => '(GMT -8:00) Pacific Time (US &amp; Canada); Tijuana',
  2835.         'US/Mountain' => '(GMT -7:00) Mountain Time (US &amp; Canada)',
  2836.         'US/Arizona' => '(GMT -7:00) Arizona',
  2837.         'Mexico/BajaNorte' => '(GMT -7:00) Chihuahua, La Paz, Mazatlan',
  2838.         'US/Central' => '(GMT -6:00) Central Time (US &amp; Canada)',
  2839.         'America/Costa_Rica' => '(GMT -6:00) Central America',
  2840.         'Mexico/General' => '(GMT -6:00) Guadalajara, Mexico City, Monterrey',
  2841.         'Canada/Saskatchewan' => '(GMT -6:00) Saskatchewan',
  2842.         'US/Eastern' => '(GMT -5:00) Eastern Time (US &amp; Canada)',
  2843.         'America/Bogota' => '(GMT -5:00) Bogota, Lima, Quito',
  2844.         'US/East-Indiana' => '(GMT -5:00) Indiana (East)',
  2845.         'Canada/Eastern' => '(GMT -4:00) Atlantic Time (Canada)',
  2846.         'America/Caracas' => '(GMT -4:00) Caracas, La Paz',
  2847.         'America/Santiago' => '(GMT -4:00) Santiago',
  2848.         'Canada/Newfoundland' => '(GMT -3:30) Newfoundland',
  2849.         'Canada/Atlantic' => '(GMT -3:00) Brasilia, Greenland',
  2850.         'America/Buenos_Aires' => '(GMT -3:00) Buenos Aires, Georgetown',
  2851.         'Atlantic/Cape_Verde' => '(GMT -1:00) Cape Verde Is.',
  2852.         'Atlantic/Azores' => '(GMT -1:00) Azores',
  2853.         'Africa/Casablanca' => '(GMT 0) Casablanca, Monrovia',
  2854.         'Europe/Dublin' => '(GMT 0) Greenwich Mean Time : Dublin, Edinburgh, London',
  2855.         'Europe/Amsterdam' => '(GMT +1:00) Amsterdam, Berlin, Rome, Stockholm, Vienna',
  2856.         'Europe/Prague' => '(GMT +1:00) Belgrade, Bratislava, Budapest, Prague',
  2857.         'Europe/Paris' => '(GMT +1:00) Brussels, Copenhagen, Madrid, Paris',
  2858.         'Europe/Warsaw' => '(GMT +1:00) Sarajevo, Skopje, Warsaw, Zagreb',
  2859.         'Africa/Bangui' => '(GMT +1:00) West Central Africa',
  2860.         'Europe/Istanbul' => '(GMT +2:00) Athens, Beirut, Bucharest, Cairo, Istanbul    ',
  2861.         'Asia/Jerusalem' => '(GMT +2:00) Harare, Jerusalem, Pretoria',
  2862.         'Europe/Kiev' => '(GMT +2:00) Helsinki, Kiev, Riga, Sofia, Tallinn, Vilnius',
  2863.         'Asia/Riyadh' => '(GMT +3:00) Kuwait, Nairobi, Riyadh',
  2864.         'Europe/Moscow' => '(GMT +3:00) Baghdad, Moscow, St. Petersburg, Volgograd',
  2865.         'Asia/Tehran' => '(GMT +3:30) Tehran',
  2866.         'Asia/Muscat' => '(GMT +4:00) Abu Dhabi, Muscat',
  2867.         'Asia/Baku' => '(GMT +4:00) Baku, Tbilsi, Yerevan',
  2868.         'Asia/Kabul' => '(GMT +4:30) Kabul',
  2869.         'Asia/Yekaterinburg' => '(GMT +5:00) Yekaterinburg',
  2870.         'Asia/Karachi' => '(GMT +5:00) Islamabad, Karachi, Tashkent',
  2871.         'Asia/Calcutta' => '(GMT +5:30) Chennai, Calcutta, Mumbai, New Delhi',
  2872.         'Asia/Katmandu' => '(GMT +5:45) Katmandu',
  2873.         'Asia/Almaty' => '(GMT +6:00) Almaty, Novosibirsk',
  2874.         'Asia/Dhaka' => '(GMT +6:00) Astana, Dhaka, Sri Jayawardenepura',
  2875.         'Asia/Rangoon' => '(GMT +6:30) Rangoon',
  2876.         'Asia/Bangkok' => '(GMT +7:00) Bangkok, Hanoi, Jakarta',
  2877.         'Asia/Krasnoyarsk' => '(GMT +7:00) Krasnoyarsk',
  2878.         'Asia/Hong_Kong' => '(GMT +8:00) 北京, 重庆, 香港, 乌鲁木齐',
  2879.         'Asia/Irkutsk' => '(GMT +8:00) Irkutsk, Ulaan Bataar',
  2880.         'Asia/Singapore' => '(GMT +8:00) Kuala Lumpar, Perth, Singapore, Taipei',
  2881.         'Asia/Tokyo' => '(GMT +9:00) Osaka, Sapporo, Tokyo',
  2882.         'Asia/Seoul' => '(GMT +9:00) Seoul',
  2883.         'Asia/Yakutsk' => '(GMT +9:00) Yakutsk',
  2884.         'Australia/Adelaide' => '(GMT +9:30) Adelaide',
  2885.         'Australia/Darwin' => '(GMT +9:30) Darwin',
  2886.         'Australia/Brisbane' => '(GMT +10:00) Brisbane, Guam, Port Moresby',
  2887.         'Australia/Canberra' => '(GMT +10:00) Canberra, Hobart, Melbourne, Sydney, Vladivostok',
  2888.         'Asia/Magadan' => '(GMT +11:00) Magadan, Soloman Is., New Caledonia',
  2889.         'Pacific/Auckland' => '(GMT +12:00) Auckland, Wellington',
  2890.         'Pacific/Fiji' => '(GMT +12:00) Fiji, Kamchatka, Marshall Is.',
  2891.     );
  2892. }
  2893.  
  2894.  
  2895. /**
  2896.  * 判断图片上传格式是否为图片 return返回文件后缀
  2897.  */
  2898. function getImagetype($filename){
  2899.     $file = fopen($filename, 'rb');
  2900.     $bin  = fread($file, 2); //只读2字节
  2901.     fclose($file);
  2902.     $strInfo  = unpack('C2chars', $bin);
  2903.     $typeCode = intval($strInfo['chars1'].$strInfo['chars2']);
  2904.     // dd($typeCode);
  2905.     $fileType = '';
  2906.     switch ($typeCode) {
  2907.         case 255216:
  2908.             $fileType = 'jpg';
  2909.             break;
  2910.         case 7173:
  2911.             $fileType = 'gif';
  2912.             break;
  2913.         case 6677:
  2914.             $fileType = 'bmp';
  2915.             break;
  2916.         case 13780:
  2917.             $fileType = 'png';
  2918.             break;
  2919.         default:
  2920.             $fileType = '只能上传图片类型格式';
  2921.     }
  2922.     // if ($strInfo['chars1']=='-1' AND $strInfo['chars2']=='-40' ) return 'jpg';
  2923.     // if ($strInfo['chars1']=='-119' AND $strInfo['chars2']=='80' ) return 'png';
  2924.     return $fileType;
  2925. }
  2926.  
  2927. /**
  2928.  * 根据ID获取目录形式。例如 0/0
  2929.  */
  2930. function getDirPath($projectid){
  2931.     $menu2 = intval($projectid / 1000);
  2932.     $menu1 = intval($menu2 / 1000);
  2933.     $path = $menu1 . '/' . $menu2;
  2934.     return $path;
  2935. }
  2936.  
  2937. /**
  2938.  * 等比计算
  2939.  */
  2940. function dengBi($width,$height,$maxX=1280,$maxY=1280){
  2941.     //计算缩放比例
  2942.     $scale = ($maxX/$width)>($maxY/$height)?$maxY/$height:$maxX/$width;
  2943.     //计算缩放后的尺寸
  2944.     $sWidth = floor($width*$scale);
  2945.     $sHeight = floor($height*$scale);
  2946.  
  2947.     return array(
  2948.         'w'=>$sWidth,
  2949.         'h'=>$sHeight,
  2950.     );
  2951. }
  2952.  
  2953. /**
  2954.  * 编辑器存在有html标签的空内容
  2955.  */
  2956. function emptyText($text=''){
  2957.     $text = tsTrim($text);
  2958.     $text = str_replace('<p>','',$text);
  2959.     $text = str_replace('<br>','',$text);
  2960.     $text = str_replace('</p>','',$text);
  2961.     $text = tsTrim($text);
  2962.     return $text;
  2963. }
  2964.  
  2965.  
  2966. /**
  2967.  * 更新app导航和我的导航
  2968.  * @param $appkey
  2969.  * @param $appname
  2970.  */
  2971. function upAppNav($appkey,$appname){
  2972.     if($appkey && $appname){
  2973.         $strAbout = require_once 'app/'.$appkey.'/about.php';
  2974.         if($strAbout['isappnav']==1){
  2975.             #更新APP导航名称
  2976.                         $arrNav = include 'data/system_appnav.php';
  2977.                         if($arrNav[$appkey]){
  2978.                                 if(is_array($arrNav)){
  2979.                                         $arrNav[$appkey] = $appname;
  2980.                                 }else{
  2981.                                         $arrNav = array(
  2982.                                                 $appkey=>$appname,
  2983.                                         );
  2984.                                 }
  2985.                                 fileWrite('system_appnav.php','data',$arrNav);
  2986.                                 $GLOBALS['tsMySqlCache']->set('system_appnav',$arrNav);
  2987.                         }
  2988.         }
  2989.  
  2990.         if($strAbout['ismy']==1){
  2991.             #更新我的社区导航
  2992.                         $arrMy = include 'data/system_mynav.php';
  2993.                         if($arrMy[$appkey]){
  2994.                                 if(is_array($arrMy)){
  2995.                                         $arrMy[$appkey] = $appname;
  2996.                                 }else{
  2997.                                         $arrMy = array(
  2998.                                                 $appkey=>$appname,
  2999.                                         );
  3000.                                 }
  3001.                                 fileWrite('system_mynav.php','data',$arrMy);
  3002.                                 $GLOBALS['tsMySqlCache']->set('system_mynav',$arrMy);
  3003.                         }
  3004.         }
  3005.     }
  3006. }
  3007.  
  3008. /**
  3009.  * 更新app配置选项
  3010.  * @param $app
  3011.  * @param array $option
  3012.  */
  3013. function upAppOptions($app,array $option){
  3014.     fileWrite($app.'_options.php','data',$option);
  3015.     $GLOBALS['tsMySqlCache']->set($app.'_options',$option);
  3016. }
  3017.  
  3018. /**
  3019.  * 获取app配置选项
  3020.  * @param $app
  3021.  * @return mixed
  3022.  */
  3023. function getAppOptions($app){
  3024.     $strOption = fileRead($app.'_options.php');
  3025.     if($strOption==''){
  3026.         $strOption = $GLOBALS['tsMySqlCache']->get($app.'_options');
  3027.     }
  3028.     return $strOption;
  3029. }
  3030.  
  3031. /**
  3032.  * 获取项目PC网址
  3033.  *
  3034.  * @param [type] $ptable
  3035.  * @param [type] $pid
  3036.  * @return void
  3037.  */
  3038. function getProjectUrl($ptable,$pid){
  3039.         $arrProjectUrl = array(
  3040.                 'topic'=>tsUrl('topic','show',array('id'=>$pid)),//帖子
  3041.                 'article'=>tsUrl('article','show',array('id'=>$pid)),//文章
  3042.                 'photo'=>tsUrl('photo','show',array('id'=>$pid)),//图片
  3043.                 'weibo'=>tsUrl('weibo','show',array('id'=>$pid)),//唠叨
  3044.                 'video'=>tsUrl('video','show',array('id'=>$pid)),//视频
  3045.                 'audio'=>tsUrl('audio','show',array('id'=>$pid)),//音频
  3046.                 'event'=>tsUrl('event','show',array('id'=>$pid)),//活动
  3047.                 'vote'=>tsUrl('vote','show',array('id'=>$pid)),//投票
  3048.                 'study_lesson'=>tsUrl('study','lesson',array('id'=>$pid)),//课时
  3049.                 'shop_goods'=>tsUrl('shop','goods',array('id'=>$pid)),//商品
  3050.                 'review'=>tsUrl('review','show',array('id'=>$pid)),//点评1
  3051.                 'review_comment'=>tsUrl('review','comment',array('id'=>$pid)),//点评2
  3052.         );
  3053.         return $arrProjectUrl[$ptable];
  3054. }
  3055.  
  3056. /**
  3057.  * 获取uniapp页面路径
  3058.  *
  3059.  * @param [type] $ptable
  3060.  * @param [type] $pid
  3061.  * @return void
  3062.  */
  3063. function getUniappPage($ptable,$pid){
  3064.         $arrProjectUrl = array(
  3065.                 'topic'=>'../topic/show?topicid='.$pid,//帖子
  3066.                 'article'=>'../article/show?articleid='.$pid,//文章
  3067.                 'video'=>'../video/show?videoid='.$pid,//视频
  3068.                 'event'=>'../event/show?eventid='.$pid,//活动
  3069.                 'ask'=>'../ask/show?askid='.$pid,//问答
  3070.                 'weibo'=>'../weibo/show?weiboid='.$pid,//唠叨
  3071.                 'attach'=>'../attach/show?attachid='.$pid,//附件
  3072.                 'vote'=>'../vote/show?voteid='.$pid,//投票
  3073.                 'audio'=>'../audio/show?audioid='.$pid,//音频
  3074.         );
  3075.         return $arrProjectUrl[$ptable];
  3076. }
  3077.  
  3078. /**
  3079.  * 取0以上的整数,GET数字变量专用
  3080.  *
  3081.  * @param [type] $number
  3082.  * @param integer $min
  3083.  * @param integer $default_number
  3084.  * @return void
  3085.  */
  3086. function tsIntval($number,$min=0,$default_number=0){
  3087.         #判断是否空变量
  3088.         if(empty($number)==true){
  3089.                 $number = 0;
  3090.         }
  3091.         #判断是否为数字,或者判断数字中是否有小数点
  3092.         if(is_numeric($number)==false || (is_numeric($number)==true && strpos($number,".")==true)){
  3093.                 ts404();
  3094.         }
  3095.         #转为整型
  3096.         $number = intval($number);
  3097.         #如果是负整数
  3098.         if($number<0){
  3099.                 ts404();
  3100.         }
  3101.  
  3102.         //最小值
  3103.         if($number==0 && $min>0){
  3104.                 $number = $min;
  3105.         }
  3106.  
  3107.         //默认值
  3108.         if($default_number>0 && $number==0){
  3109.                 $number = $default_number;
  3110.         }
  3111.  
  3112.         return $number;
  3113. }
  3114.  
  3115. /**
  3116.  * 只保留中文,英文以及数字
  3117.  */
  3118. function match_chinese($chars,$encoding='utf8'){
  3119.     $pattern =($encoding=='utf8')?'/[\x{4e00}-\x{9fa5}a-zA-Z0-9]/u':'/[\x80-\xFF]/';
  3120.     preg_match_all($pattern,$chars,$result);
  3121.     return join('',$result[0]);
  3122. }
  3123.  
  3124. /**
  3125.  * 返回13位时间戳
  3126.  */
  3127. function getUnixTimestamp (){
  3128.     list($s1, $s2) = explode(' ', microtime());
  3129.     return (float)sprintf('%.0f',(floatval($s1) + floatval($s2)) * 1000);
  3130. }
  3131.  
  3132. /**
  3133.  * vaptcha人机验证
  3134.  *
  3135.  * @param string $token
  3136.  * @param integer $scene
  3137.  * @param [type] $url
  3138.  * @return void
  3139.  */
  3140. function vaptcha(string $token,$scene,$url){
  3141.  
  3142.         $json = sendDataByCurl($url,array(
  3143.                 'id'=>$GLOBALS['TS_SITE']['vaptcha_vid'],
  3144.                 'secretkey'=>$GLOBALS['TS_SITE']['vaptcha_key'],
  3145.                 'scene'=>$scene,
  3146.                 'token'=>$token,
  3147.                 'ip'=>getIp(),
  3148.         ));
  3149.  
  3150.         $res = json_decode($json,true);
  3151.  
  3152.         return $res;
  3153.        
  3154.  
  3155. }
  3156.  
  3157. /**
  3158.  * 根据省份获取快递费用
  3159.  *
  3160.  * @param [type] $province
  3161.  * @return void
  3162.  */
  3163. function getPostPrice($province){
  3164.     if(in_array($province,array('新疆维吾尔自治区','西藏自治区','青海省','宁夏回族自治区','甘肃省','云南省','贵州省','海南省'))){
  3165.         $postprice = 20;
  3166.     }elseif(in_array($province,array('台湾省','香港特别行政区','澳门特别行政区'))){
  3167.         $postprice = 300;
  3168.     }else{
  3169.         $postprice = 0;
  3170.     }
  3171.     return $postprice;
  3172. }
  3173.  
  3174. /**
  3175.  * 替代trim
  3176.  *
  3177.  * @param [type] $text
  3178.  * @return void
  3179.  */
  3180. function tsTrim($text){
  3181.         return trim((string) $text);
  3182. }

回复 "thinksaas 函数库"

这儿你可以回复上面这条便签

captcha