国内流行的内容管理系统(CMS)多端全媒体解决方案 https://www.dedebiz.com
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

400 lines
18KB

  1. <?php
  2. if (!defined('DEDEINC')) exit('dedebiz');
  3. /**
  4. * 系统核心函数存放文件
  5. * @version $Id: common.func.php 4 16:39 2010年7月6日Z tianya $
  6. * @package DedeBIZ.Libraries
  7. * @copyright Copyright (c) 2022, DedeBIZ.COM
  8. * @license https://www.dedebiz.com/license
  9. * @link https://www.dedebiz.com
  10. */
  11. if (version_compare(PHP_VERSION, '7.0.0', '>=')) {
  12. if (!function_exists('mysql_connect') and function_exists('mysqli_connect')) {
  13. function mysql_connect($server, $username, $password)
  14. {
  15. return mysqli_connect($server, $username, $password);
  16. }
  17. }
  18. if (!function_exists('mysql_query') and function_exists('mysqli_query')) {
  19. function mysql_query($query, $link)
  20. {
  21. return mysqli_query($link, $query);
  22. }
  23. }
  24. if (!function_exists('mysql_select_db') and function_exists('mysqli_select_db')) {
  25. function mysql_select_db($database_name, $link)
  26. {
  27. return mysqli_select_db($link, $database_name);
  28. }
  29. }
  30. if (!function_exists('mysql_fetch_array') and function_exists('mysqli_fetch_array')) {
  31. function mysql_fetch_array($result)
  32. {
  33. return mysqli_fetch_array($result);
  34. }
  35. }
  36. if (!function_exists('mysql_close') and function_exists('mysqli_close')) {
  37. function mysql_close($link)
  38. {
  39. return mysqli_close($link);
  40. }
  41. }
  42. if (!function_exists('split')) {
  43. function split($pattern, $string)
  44. {
  45. return explode($pattern, $string);
  46. }
  47. }
  48. }
  49. // 一个支持在PHP Cli Server打印的方法
  50. function var_dump_cli($val){
  51. ob_start();
  52. var_dump($val);
  53. error_log(ob_get_clean(), 4);
  54. }
  55. function get_mime_type($filename)
  56. {
  57. if (! function_exists('finfo_open'))
  58. {
  59. return 'unknow/octet-stream';
  60. }
  61. $finfo = finfo_open(FILEINFO_MIME_TYPE);
  62. $mimeType = finfo_file($finfo, $filename);
  63. finfo_close($finfo);
  64. return $mimeType;
  65. }
  66. function is_all_numeric(array $array){
  67. foreach($array as $item){
  68. if(!is_numeric($item)) return false;
  69. }
  70. return true;
  71. }
  72. function make_hash()
  73. {
  74. $rand = dede_random_bytes(16);
  75. $_SESSION['token'] = ($rand === FALSE)
  76. ? md5(uniqid(mt_rand(), TRUE))
  77. : bin2hex($rand);
  78. return $_SESSION['token'];
  79. }
  80. function dede_random_bytes($length)
  81. {
  82. if (empty($length) or !ctype_digit((string) $length)) {
  83. return FALSE;
  84. }
  85. if (function_exists('openssl_random_pseudo_bytes')) {
  86. return openssl_random_pseudo_bytes($length);
  87. }
  88. if (function_exists('random_bytes')) {
  89. try {
  90. return random_bytes((int) $length);
  91. } catch (Exception $e) {
  92. return FALSE;
  93. }
  94. }
  95. if (defined('MCRYPT_DEV_URANDOM') && ($output = mcrypt_create_iv($length, MCRYPT_DEV_URANDOM)) !== FALSE) {
  96. return $output;
  97. }
  98. if (is_readable('/dev/urandom') && ($fp = fopen('/dev/urandom', 'rb')) !== FALSE) {
  99. version_compare(PHP_VERSION, '5.4.0', '>=') && stream_set_chunk_size($fp, $length);
  100. $output = fread($fp, $length);
  101. fclose($fp);
  102. if ($output !== FALSE) {
  103. return $output;
  104. }
  105. }
  106. return FALSE;
  107. }
  108. /**
  109. * 载入小助手,系统默认载入小助手
  110. * 在/data/helper.inc.php中进行默认小助手初始化的设置
  111. * 使用示例:
  112. * 在开发中,首先需要创建一个小助手函数,目录在\include\helpers中
  113. * 例如,我们创建一个示例为test.helper.php,文件基本内容如下:
  114. * <code>
  115. * if ( ! function_exists('HelloDede'))
  116. * {
  117. * function HelloDede()
  118. * {
  119. * echo "Hello! Dede";
  120. * }
  121. * }
  122. * </code>
  123. * 则我们在开发中使用这个小助手的时候直接使用函数helper('test');初始化它
  124. * 然后在文件中就可以直接使用:HelloDede();来进行调用.
  125. *
  126. * @access public
  127. * @param mix $helpers 小助手名称,可以是数组,可以是单个字符串
  128. * @return void
  129. */
  130. $_helpers = array();
  131. function helper($helpers)
  132. {
  133. //如果是数组,则进行递归操作
  134. if (is_array($helpers)) {
  135. foreach ($helpers as $dede) {
  136. helper($dede);
  137. }
  138. return;
  139. }
  140. if (isset($_helpers[$helpers])) {
  141. return;
  142. }
  143. if (file_exists(DEDEINC.'/helpers/'.$helpers.'.helper.php')) {
  144. include_once(DEDEINC.'/helpers/'.$helpers.'.helper.php');
  145. $_helpers[$helpers] = TRUE;
  146. }
  147. //无法载入小助手
  148. if (!isset($_helpers[$helpers])) {
  149. exit('Unable to load the requested file: helpers/'.$helpers.'.helper.php');
  150. }
  151. }
  152. function dede_htmlspecialchars($str)
  153. {
  154. global $cfg_soft_lang;
  155. if (version_compare(PHP_VERSION, '5.4.0', '<')) return htmlspecialchars($str);
  156. if ($cfg_soft_lang == 'gb2312') return htmlspecialchars($str, ENT_COMPAT, 'ISO-8859-1');
  157. else return htmlspecialchars($str);
  158. }
  159. /**
  160. * 载入小助手,这里用户可能载入用helps载入多个小助手
  161. *
  162. * @access public
  163. * @param string
  164. * @return string
  165. */
  166. function helpers($helpers)
  167. {
  168. helper($helpers);
  169. }
  170. //兼容php4的file_put_contents
  171. if (!function_exists('file_put_contents')) {
  172. function file_put_contents($n, $d)
  173. {
  174. $f = @fopen($n, "w");
  175. if (!$f) {
  176. return FALSE;
  177. } else {
  178. fwrite($f, $d);
  179. fclose($f);
  180. return TRUE;
  181. }
  182. }
  183. }
  184. /**
  185. * 显示更新信息
  186. *
  187. * @return void
  188. */
  189. function UpdateStat()
  190. {
  191. include_once(DEDEINC."/inc/inc_stat.php");
  192. return SpUpdateStat();
  193. }
  194. $arrs1 = array();
  195. $arrs2 = array();
  196. /**
  197. * 短消息函数,可以在某个动作处理后友好的提示信息
  198. *
  199. * @param string $msg 消息提示信息
  200. * @param string $gourl 跳转地址
  201. * @param int $onlymsg 仅显示信息
  202. * @param int $limittime 限制时间
  203. * @return void
  204. */
  205. function ShowMsg($msg, $gourl, $onlymsg = 0, $limittime = 0)
  206. {
  207. global $cfg_soft_lang, $cfg_cmsurl;
  208. if(empty($GLOBALS['cfg_plus_dir'])) $GLOBALS['cfg_plus_dir'] = '..';
  209. $htmlhead = "<html><head><meta charset='utf-8'><title>提示信息</title><meta name='viewport' content='width=device-width,initial-scale=1'><base target='_self'></head>";
  210. $htmlhead .= "<body>".(isset($GLOBALS['ucsynlogin']) ? $GLOBALS['ucsynlogin'] : '')."<center><script>";
  211. $htmlfoot = "</script></center></body></html>";
  212. $litime = ($limittime == 0 ? 1000 : $limittime);
  213. $func = '';
  214. if ($gourl == '-1') {
  215. if ($limittime == 0) $litime = 5000;
  216. $gourl = "javascript:history.go(-1);";
  217. }
  218. if ($gourl == '' || $onlymsg == 1) {
  219. $msg = "<script>alert(\"".str_replace("\"", "“", $msg)."\");</script>";
  220. } else {
  221. //当网址为:close::objname 时, 关闭父框架的id=objname元素
  222. if (preg_match('/close::/', $gourl)) {
  223. $tgobj = trim(preg_replace('/close::/', '', $gourl));
  224. $gourl = 'javascript:;';
  225. $func .= "window.parent.document.getElementById('{$tgobj}').style.display='none';\r\n";
  226. }
  227. $func .= "var pgo=0;function JumpUrl(){if (pgo==0){location='$gourl'; pgo=1;}}";
  228. $rmsg = $func;
  229. $rmsg .= "document.write(\"<style>body{margin:0;line-height:1.5;font:14px Helvetica Neue,Helvetica,PingFang SC,Tahoma,Arial,sans-serif;color:#424b51;background:#f2f2f2}a{color:#28a745;text-decoration:none}.tips{margin:68px auto 0;padding:0;width:420px;height:auto;background:#fff;border-radius:.2rem}.tips-head{margin:0 20px;padding:16px 0;border-bottom:1px solid #f6f6f6}.tips-head p{margin:0;padding-left:10px;line-height:16px;text-align:left;border-left:3px solid #ff5722}.tips-box{padding:20px;min-height:120px;color:#666}.btn a{display:inline-block;margin:20px auto 0;padding:.375rem .75rem;font-size:12px;color:#fff;background:#28a745;border-radius:.2rem;text-align:center;transition:all .6s}.btn a:focus{background:#006829;border-color:#005b24;box-shadow:0 0 0 0.2rem rgba(38,159,86,.5)}@media (max-width:768px){body{padding:0 15px}.tips{width:100%}}</style>\");";
  230. $rmsg .= "document.write(\"<div class='tips'>";
  231. $rmsg .= "<div class='tips-head'><p>提示信息</p></div>\");";
  232. $rmsg .= "document.write(\"<div class='tips-box'>\");";
  233. $rmsg .= "document.write(\"".str_replace("\"","“",$msg)."\");";
  234. $rmsg .= "document.write(\"";
  235. if($onlymsg==0)
  236. {
  237. if( $gourl != 'javascript:;' && $gourl != '')
  238. {
  239. $rmsg .= "<div class='btn'><a href='{$gourl}'>点击反应</a></div>\");";
  240. $rmsg .= "setTimeout('JumpUrl()',$litime);";
  241. } else {
  242. $rmsg .= "</div>\");";
  243. }
  244. } else {
  245. $rmsg .= "</div>\");";
  246. }
  247. $msg = $htmlhead.$rmsg.$htmlfoot;
  248. }
  249. echo $msg;
  250. }
  251. /**
  252. * 获取验证码的session值
  253. *
  254. * @return string
  255. */
  256. function GetCkVdValue()
  257. {
  258. @session_id($_COOKIE['PHPSESSID']);
  259. @session_start();
  260. return isset($_SESSION['securimage_code_value']) ? $_SESSION['securimage_code_value'] : '';
  261. }
  262. /**
  263. * PHP某些版本有Bug,不能在同一作用域中同时读session并改注销它,因此调用后需执行本函数
  264. *
  265. * @return void
  266. */
  267. function ResetVdValue()
  268. {
  269. @session_start();
  270. $_SESSION['securimage_code_value'] = '';
  271. }
  272. function IndexSub($idx, $num)
  273. {
  274. return intval($idx) - intval($num) == 0 ? '0 ' : intval($idx) - intval($num);
  275. }
  276. //用来返回index的active
  277. function IndexActive($idx)
  278. {
  279. if ($idx == 1) {
  280. return ' active';
  281. } else {
  282. return '';
  283. }
  284. }
  285. //自定义函数接口
  286. //这里主要兼容早期的用户扩展,v5.7之后我们建议使用小助手helper进行扩展
  287. if (file_exists(DEDEINC.'/extend.func.php')) {
  288. require_once(DEDEINC.'/extend.func.php');
  289. }
  290. /**
  291. * 添加多选联动筛选
  292. *
  293. * @return string
  294. */
  295. function litimgurls($imgid=0)
  296. {
  297. global $lit_imglist,$dsql;
  298. $row = $dsql->GetOne("SELECT c.addtable FROM `#@__archives` AS a LEFT JOIN `#@__channeltype` AS c ON a.channel=c.id where a.id='$imgid'");
  299. $addtable = trim($row['addtable']);
  300. $row = $dsql->GetOne("Select imgurls From `$addtable` where aid='$imgid'");
  301. $ChannelUnit = new ChannelUnit(2,$imgid);
  302. $lit_imglist = $ChannelUnit->GetlitImgLinks($row['imgurls']);
  303. return $lit_imglist;
  304. }
  305. //字符过滤函数,用于安全
  306. function string_filter($str,$stype="inject") {
  307. if ($stype=="inject") {
  308. $str = str_replace (
  309. array ("select", "insert", "update", "delete", "alter", "cas", "union", "into", "load_file", "outfile", "create", "join", "where", "like", "drop", "modify", "rename", "'", "/*", "*", "../", "./"),
  310. array ("","","","","","","","","","","","","","","","","","","","","",""),
  311. $str);
  312. } else if ($stype=="xss") {
  313. $farr = array ("/\s+/" , "/<(\/?)(script|META|STYLE|HTML|HEAD|BODY|STYLE |i?frame|b|strong|style|html|img|P|o:p|iframe|u|em|strike|BR|div|a|TABLE|TBODY|object|tr|td|st1:chsdate|FONT|span|MARQUEE|body|title|\r\n|link|meta|\?|\%)([^>]*?)>/isU", "/(<[^>]*)on[a-zA-Z]+\s*=([^>]*>)/isU",);
  314. $tarr = array (" ","","\\1\\2",);
  315. $str = preg_replace ($farr, $tarr, $str);
  316. $str = str_replace (
  317. array( "<", ">", "'", "\"", ";", "/*", "*", "../", "./"),
  318. array("&lt;","&gt;","","","","","","",""),
  319. $str);
  320. }
  321. return $str;
  322. }
  323. //载入自定义表单,用于发布
  324. function AddFilter($channelid, $type=1, $fieldsnamef=array(), $defaulttid=0, $loadtype='autofield')
  325. {
  326. global $tid,$dsql,$id;
  327. $tid = $defaulttid ? $defaulttid : $tid;
  328. if ($id!="")
  329. {
  330. $tidsq = $dsql->GetOne("SELECT typeid FROM `#@__archives` WHERE id='$id' ");
  331. $tid = $tidsq["typeid"];
  332. }
  333. $nofilter = (isset($_REQUEST['TotalResult']) ? "&TotalResult=".$_REQUEST['TotalResult'] : '').(isset($_REQUEST['PageNo']) ? "&PageNo=".$_REQUEST['PageNo'] : '');
  334. $filterarr = string_filter(stripos($_SERVER['REQUEST_URI'], "list.php?tid=") ? str_replace($nofilter, '', $_SERVER['REQUEST_URI']) : $GLOBALS['cfg_cmsurl']."/apps/list.php?tid=".$tid);
  335. $cInfos = $dsql->GetOne("SELECT * FROM `#@__channeltype` WHERE id='$channelid' ");
  336. $fieldset=stripslashes($cInfos['fieldset']);
  337. $dtp = new DedeTagParse();
  338. $dtp->SetNameSpace('field','<','>');
  339. $dtp->LoadSource($fieldset);
  340. $dede_addonfields = '';
  341. if(is_array($dtp->CTags))
  342. {
  343. foreach($dtp->CTags as $tida=>$ctag)
  344. {
  345. $fieldsname = $fieldsnamef ? explode(",", $fieldsnamef) : explode(",", $ctag->GetName());
  346. if(($loadtype!='autofield' || ($loadtype=='autofield' && $ctag->GetAtt('autofield')==1)) && in_array($ctag->GetName(), $fieldsname) )
  347. {
  348. $href1 = explode($ctag->GetName().'=', $filterarr);
  349. $href2 = explode('&', $href1[1]);
  350. $fields_value = $href2[0];
  351. $fields_value1 = explode('|', $fields_value);
  352. $dede_addonfields .= '<b>'.$ctag->GetAtt('itemname').':</b>';
  353. switch ($type) {
  354. case 1:
  355. $dede_addonfields .= (preg_match("/&".$ctag->GetName()."=/is",$filterarr,$regm) ? '<a href="'.str_replace("&".$ctag->GetName()."=".$fields_value,"",$filterarr).'" style="display:inline-block;padding:.25rem .5rem;line-height:1.5;color:#fff;background:#008e38;border-color:#008e38;border-radius:.2rem">全部</a>' : '<span style="display:inline-block;padding:.25rem .5rem;line-height:1.5;color:#fff;background:#dc3545;border-color:#dc3545;border-radius:.2rem">全部</span>').'&nbsp;';
  356. $addonfields_items = explode(",",$ctag->GetAtt('default'));
  357. for ($i=0; $i<count($addonfields_items); $i++)
  358. {
  359. $href = stripos($filterarr,$ctag->GetName().'=') ? str_replace("=".$fields_value,"=".$fields_value."|".urlencode($addonfields_items[$i]),$filterarr) : $filterarr.'&'.$ctag->GetName().'='.urlencode($addonfields_items[$i]);
  360. $is_select = in_array(urlencode($addonfields_items[$i]), $fields_value1) ? 1 : 0;
  361. $fields_value2 = "";
  362. for ($j=0; $j<count($fields_value1); $j++)
  363. {
  364. $fields_value2 .= $fields_value1[$j] != urlencode($addonfields_items[$i]) ? $fields_value1[$j].($j<count($fields_value1)-1 ? "|" : "") : "";
  365. }
  366. $fields_value2 = rtrim($fields_value2, "|");
  367. $href3 = str_replace(array("&".$ctag->GetName()."=".$fields_value,$ctag->GetName()."=".$fields_value, "&".$ctag->GetName()."=&"), array("&".$ctag->GetName()."=".$fields_value2,$ctag->GetName()."=".$fields_value2, "&"), $filterarr);
  368. $href3 = !end(explode("=", $href3)) ? str_replace("&".end(explode("&", $href3)), "", $href3) : $href3;
  369. $dede_addonfields .= ($fields_value!=urlencode($addonfields_items[$i]) && $is_select!=1 ? '<a title="'.$addonfields_items[$i].'" href="'.$href.'" style="display:inline-block;padding:.25rem .5rem;line-height:1.5;color:#fff;background:#008e38;border-color:#008e38;border-radius:.2rem">'.$addonfields_items[$i].'</a>' : '<a title="'.$addonfields_items[$i].'" href="'.$href3.'" style="display:inline-block;padding:.25rem .5rem;line-height:1.5;color:#fff;background:#dc3545;border-color:#dc3545;border-radius:.2rem">'.$addonfields_items[$i].'<span style="margin-left:6px;color:#fff">×</span></a>')."&nbsp;";
  370. }
  371. $dede_addonfields .= '<br><br>';
  372. break;
  373. case 2:
  374. $dede_addonfields .= (preg_match("/&".$ctag->GetName()."=/is",$filterarr,$regm) ? '<a href="'.str_replace("&".$ctag->GetName()."=".$fields_value,"",$filterarr).'">全部</a>' : '<span>全部</span>').'&nbsp;';
  375. $addonfields_items = explode(",",$ctag->GetAtt('default'));
  376. for ($i=0; $i<count($addonfields_items); $i++)
  377. {
  378. $href = stripos($filterarr,$ctag->GetName().'=') ? str_replace("=".$fields_value,"=".$fields_value."|".urlencode($addonfields_items[$i]),$filterarr) : $filterarr.'&'.$ctag->GetName().'='.urlencode($addonfields_items[$i]);
  379. $is_select = in_array(urlencode($addonfields_items[$i]), $fields_value1) ? 1 : 0;
  380. $fields_value2 = "";
  381. for ($j=0; $j<count($fields_value1); $j++)
  382. {
  383. $fields_value2 .= $fields_value1[$j] != urlencode($addonfields_items[$i]) ? $fields_value1[$j].($j<count($fields_value1)-1 ? "|" : "") : "";
  384. }
  385. $fields_value2 = rtrim($fields_value2, "|");
  386. $href3 = str_replace(array("&".$ctag->GetName()."=".$fields_value,$ctag->GetName()."=".$fields_value, "&".$ctag->GetName()."=&"), array("&".$ctag->GetName()."=".$fields_value2,$ctag->GetName()."=".$fields_value2, "&"), $filterarr);
  387. $href3 = !end(explode("=", $href3)) ? str_replace("&".end(explode("&", $href3)), "", $href3) : $href3;
  388. $dede_addonfields .= ($fields_value!=urlencode($addonfields_items[$i]) && $is_select!=1 ? '<input type="checkbox" title="'.$addonfields_items[$i].'" value="'.$href.'" onclick="window.location=this.value">&nbsp;<a title="'.$addonfields_items[$i].'" href="'.$href.'">'.$addonfields_items[$i].'</a>' : '<input type="checkbox" checked="checked" title="'.$addonfields_items[$i].'" value="'.$href3.'" onclick="window.location=this.value">&nbsp;<a title="'.$addonfields_items[$i].'" href="'.$href3.'" class="cur">'.$addonfields_items[$i].'</a>')."&nbsp;";
  389. }
  390. $dede_addonfields .= '<br><br>';
  391. break;
  392. }
  393. }
  394. }
  395. }
  396. echo $dede_addonfields;
  397. }