国内流行的内容管理系统(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.

397 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 (is_readable('/dev/urandom') && ($fp = fopen('/dev/urandom', 'rb')) !== FALSE) {
  96. version_compare(PHP_VERSION, '5.4.0', '>=') && stream_set_chunk_size($fp, $length);
  97. $output = fread($fp, $length);
  98. fclose($fp);
  99. if ($output !== FALSE) {
  100. return $output;
  101. }
  102. }
  103. return FALSE;
  104. }
  105. /**
  106. * 载入小助手,系统默认载入小助手
  107. * 在/data/helper.inc.php中进行默认小助手初始化的设置
  108. * 使用示例:
  109. * 在开发中,首先需要创建一个小助手函数,目录在\include\helpers中
  110. * 例如,我们创建一个示例为test.helper.php,文件基本内容如下:
  111. * <code>
  112. * if ( ! function_exists('HelloDede'))
  113. * {
  114. * function HelloDede()
  115. * {
  116. * echo "Hello! Dede";
  117. * }
  118. * }
  119. * </code>
  120. * 则我们在开发中使用这个小助手的时候直接使用函数helper('test');初始化它
  121. * 然后在文件中就可以直接使用:HelloDede();来进行调用.
  122. *
  123. * @access public
  124. * @param mix $helpers 小助手名称,可以是数组,可以是单个字符串
  125. * @return void
  126. */
  127. $_helpers = array();
  128. function helper($helpers)
  129. {
  130. //如果是数组,则进行递归操作
  131. if (is_array($helpers)) {
  132. foreach ($helpers as $dede) {
  133. helper($dede);
  134. }
  135. return;
  136. }
  137. if (isset($_helpers[$helpers])) {
  138. return;
  139. }
  140. if (file_exists(DEDEINC.'/helpers/'.$helpers.'.helper.php')) {
  141. include_once(DEDEINC.'/helpers/'.$helpers.'.helper.php');
  142. $_helpers[$helpers] = TRUE;
  143. }
  144. //无法载入小助手
  145. if (!isset($_helpers[$helpers])) {
  146. exit('Unable to load the requested file: helpers/'.$helpers.'.helper.php');
  147. }
  148. }
  149. function dede_htmlspecialchars($str)
  150. {
  151. global $cfg_soft_lang;
  152. if (version_compare(PHP_VERSION, '5.4.0', '<')) return htmlspecialchars($str);
  153. if ($cfg_soft_lang == 'gb2312') return htmlspecialchars($str, ENT_COMPAT, 'ISO-8859-1');
  154. else return htmlspecialchars($str);
  155. }
  156. /**
  157. * 载入小助手,这里用户可能载入用helps载入多个小助手
  158. *
  159. * @access public
  160. * @param string
  161. * @return string
  162. */
  163. function helpers($helpers)
  164. {
  165. helper($helpers);
  166. }
  167. //兼容php4的file_put_contents
  168. if (!function_exists('file_put_contents')) {
  169. function file_put_contents($n, $d)
  170. {
  171. $f = @fopen($n, "w");
  172. if (!$f) {
  173. return FALSE;
  174. } else {
  175. fwrite($f, $d);
  176. fclose($f);
  177. return TRUE;
  178. }
  179. }
  180. }
  181. /**
  182. * 显示更新信息
  183. *
  184. * @return void
  185. */
  186. function UpdateStat()
  187. {
  188. include_once(DEDEINC."/inc/inc_stat.php");
  189. return SpUpdateStat();
  190. }
  191. $arrs1 = array();
  192. $arrs2 = array();
  193. /**
  194. * 短消息函数,可以在某个动作处理后友好的提示信息
  195. *
  196. * @param string $msg 消息提示信息
  197. * @param string $gourl 跳转地址
  198. * @param int $onlymsg 仅显示信息
  199. * @param int $limittime 限制时间
  200. * @return void
  201. */
  202. function ShowMsg($msg, $gourl, $onlymsg = 0, $limittime = 0)
  203. {
  204. global $cfg_soft_lang, $cfg_cmsurl;
  205. if(empty($GLOBALS['cfg_plus_dir'])) $GLOBALS['cfg_plus_dir'] = '..';
  206. $htmlhead = "<html><head><meta charset='utf-8'><title>提示信息</title><meta name='viewport' content='width=device-width,initial-scale=1'><base target='_self'></head>";
  207. $htmlhead .= "<body><center><script>";
  208. $htmlfoot = "</script></center></body></html>";
  209. $litime = ($limittime == 0 ? 1000 : $limittime);
  210. $func = '';
  211. if ($gourl == '-1') {
  212. if ($limittime == 0) $litime = 5000;
  213. $gourl = "javascript:history.go(-1);";
  214. }
  215. if ($gourl == '' || $onlymsg == 1) {
  216. $msg = "<script>alert(\"".str_replace("\"", "“", $msg)."\");</script>";
  217. } else {
  218. //当网址为:close::objname 时, 关闭父框架的id=objname元素
  219. if (preg_match('/close::/', $gourl)) {
  220. $tgobj = trim(preg_replace('/close::/', '', $gourl));
  221. $gourl = 'javascript:;';
  222. $func .= "window.parent.document.getElementById('{$tgobj}').style.display='none';\r\n";
  223. }
  224. $func .= "var pgo=0;function JumpUrl(){if (pgo==0){location='$gourl'; pgo=1;}}";
  225. $rmsg = $func;
  226. $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:#f6f6f6}a{color:#28a745;text-decoration:none}.tips{margin:50px auto 0;padding:0;width:430px;height:auto;background:#fff;border-radius:.2rem}.tips-head{margin:0 20px;padding:16px 0;border-bottom:1px solid #f8f8f8}.tips-head p{margin:0;padding-left:10px;line-height:16px;text-align:left;border-left:3px solid #dc3545}.tips-box{padding:20px;min-height:120px;color:#424b51}.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>\");";
  227. $rmsg .= "document.write(\"<div class='tips'>";
  228. $rmsg .= "<div class='tips-head'><p>提示信息</p></div>\");";
  229. $rmsg .= "document.write(\"<div class='tips-box'>\");";
  230. $rmsg .= "document.write(\"".str_replace("\"","“",$msg)."\");";
  231. $rmsg .= "document.write(\"";
  232. if($onlymsg==0)
  233. {
  234. if( $gourl != 'javascript:;' && $gourl != '')
  235. {
  236. $rmsg .= "<div class='btn'><a href='{$gourl}'>点击反应</a></div>\");";
  237. $rmsg .= "setTimeout('JumpUrl()',$litime);";
  238. } else {
  239. $rmsg .= "</div>\");";
  240. }
  241. } else {
  242. $rmsg .= "</div>\");";
  243. }
  244. $msg = $htmlhead.$rmsg.$htmlfoot;
  245. }
  246. echo $msg;
  247. }
  248. /**
  249. * 获取验证码的session值
  250. *
  251. * @return string
  252. */
  253. function GetCkVdValue()
  254. {
  255. @session_id($_COOKIE['PHPSESSID']);
  256. @session_start();
  257. return isset($_SESSION['securimage_code_value']) ? $_SESSION['securimage_code_value'] : '';
  258. }
  259. /**
  260. * PHP某些版本有Bug,不能在同一作用域中同时读session并改注销它,因此调用后需执行本函数
  261. *
  262. * @return void
  263. */
  264. function ResetVdValue()
  265. {
  266. @session_start();
  267. $_SESSION['securimage_code_value'] = '';
  268. }
  269. function IndexSub($idx, $num)
  270. {
  271. return intval($idx) - intval($num) == 0 ? '0 ' : intval($idx) - intval($num);
  272. }
  273. //用来返回index的active
  274. function IndexActive($idx)
  275. {
  276. if ($idx == 1) {
  277. return ' active';
  278. } else {
  279. return '';
  280. }
  281. }
  282. //自定义函数接口
  283. //这里主要兼容早期的用户扩展,v5.7之后我们建议使用小助手helper进行扩展
  284. if (file_exists(DEDEINC.'/extend.func.php')) {
  285. require_once(DEDEINC.'/extend.func.php');
  286. }
  287. /**
  288. * 添加多选联动筛选
  289. *
  290. * @return string
  291. */
  292. function litimgurls($imgid=0)
  293. {
  294. global $lit_imglist,$dsql;
  295. $row = $dsql->GetOne("SELECT c.addtable FROM `#@__archives` AS a LEFT JOIN `#@__channeltype` AS c ON a.channel=c.id where a.id='$imgid'");
  296. $addtable = trim($row['addtable']);
  297. $row = $dsql->GetOne("Select imgurls From `$addtable` where aid='$imgid'");
  298. $ChannelUnit = new ChannelUnit(2,$imgid);
  299. $lit_imglist = $ChannelUnit->GetlitImgLinks($row['imgurls']);
  300. return $lit_imglist;
  301. }
  302. //字符过滤函数,用于安全
  303. function string_filter($str,$stype="inject") {
  304. if ($stype=="inject") {
  305. $str = str_replace (
  306. array ("select", "insert", "update", "delete", "alter", "cas", "union", "into", "load_file", "outfile", "create", "join", "where", "like", "drop", "modify", "rename", "'", "/*", "*", "../", "./"),
  307. array ("","","","","","","","","","","","","","","","","","","","","",""),
  308. $str);
  309. } else if ($stype=="xss") {
  310. $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",);
  311. $tarr = array (" ","","\\1\\2",);
  312. $str = preg_replace ($farr, $tarr, $str);
  313. $str = str_replace (
  314. array( "<", ">", "'", "\"", ";", "/*", "*", "../", "./"),
  315. array("&lt;","&gt;","","","","","","",""),
  316. $str);
  317. }
  318. return $str;
  319. }
  320. //载入自定义表单,用于发布
  321. function AddFilter($channelid, $type=1, $fieldsnamef="", $defaulttid=0, $loadtype='autofield')
  322. {
  323. global $tid,$dsql,$id;
  324. $tid = $defaulttid ? $defaulttid : $tid;
  325. if ($id!="")
  326. {
  327. $tidsq = $dsql->GetOne("SELECT typeid FROM `#@__archives` WHERE id='$id' ");
  328. $tid = $tidsq["typeid"];
  329. }
  330. $nofilter = (isset($_REQUEST['TotalResult']) ? "&TotalResult=".$_REQUEST['TotalResult'] : '').(isset($_REQUEST['PageNo']) ? "&PageNo=".$_REQUEST['PageNo'] : '');
  331. $filterarr = string_filter(stripos($_SERVER['REQUEST_URI'], "list.php?tid=") ? str_replace($nofilter, '', $_SERVER['REQUEST_URI']) : $GLOBALS['cfg_cmsurl']."/apps/list.php?tid=".$tid);
  332. $cInfos = $dsql->GetOne("SELECT * FROM `#@__channeltype` WHERE id='$channelid' ");
  333. $fieldset=stripslashes($cInfos['fieldset']);
  334. $dtp = new DedeTagParse();
  335. $dtp->SetNameSpace('field','<','>');
  336. $dtp->LoadSource($fieldset);
  337. $dede_addonfields = '';
  338. if(is_array($dtp->CTags))
  339. {
  340. foreach($dtp->CTags as $tida=>$ctag)
  341. {
  342. $fieldsname = $fieldsnamef ? explode(",", $fieldsnamef) : explode(",", $ctag->GetName());
  343. if(($loadtype!='autofield' || ($loadtype=='autofield' && $ctag->GetAtt('autofield')==1)) && in_array($ctag->GetName(), $fieldsname) )
  344. {
  345. $href1 = explode($ctag->GetName().'=', $filterarr);
  346. $href2 = explode('&', $href1[1]);
  347. $fields_value = $href2[0];
  348. $fields_value1 = explode('|', $fields_value);
  349. $dede_addonfields .= ''.$ctag->GetAtt('itemname').':';
  350. switch ($type) {
  351. case 1:
  352. $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:#28a745;border-color:#28a745;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;';
  353. $addonfields_items = explode(",",$ctag->GetAtt('default'));
  354. for ($i=0; $i<count($addonfields_items); $i++)
  355. {
  356. $href = stripos($filterarr,$ctag->GetName().'=') ? str_replace("=".$fields_value,"=".$fields_value."|".urlencode($addonfields_items[$i]),$filterarr) : $filterarr.'&'.$ctag->GetName().'='.urlencode($addonfields_items[$i]);
  357. $is_select = in_array(urlencode($addonfields_items[$i]), $fields_value1) ? 1 : 0;
  358. $fields_value2 = "";
  359. for ($j=0; $j<count($fields_value1); $j++)
  360. {
  361. $fields_value2 .= $fields_value1[$j] != urlencode($addonfields_items[$i]) ? $fields_value1[$j].($j<count($fields_value1)-1 ? "|" : "") : "";
  362. }
  363. $fields_value2 = rtrim($fields_value2, "|");
  364. $href3 = str_replace(array("&".$ctag->GetName()."=".$fields_value,$ctag->GetName()."=".$fields_value, "&".$ctag->GetName()."=&"), array("&".$ctag->GetName()."=".$fields_value2,$ctag->GetName()."=".$fields_value2, "&"), $filterarr);
  365. $href3 = !end(explode("=", $href3)) ? str_replace("&".end(explode("&", $href3)), "", $href3) : $href3;
  366. $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:#28a745;border-color:#28a745;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;";
  367. }
  368. $dede_addonfields .= '<br><br>';
  369. break;
  370. case 2:
  371. $dede_addonfields .= (preg_match("/&".$ctag->GetName()."=/is",$filterarr,$regm) ? '<a href="'.str_replace("&".$ctag->GetName()."=".$fields_value,"",$filterarr).'">全部</a>' : '<span>全部</span>').'&nbsp;';
  372. $addonfields_items = explode(",",$ctag->GetAtt('default'));
  373. for ($i=0; $i<count($addonfields_items); $i++)
  374. {
  375. $href = stripos($filterarr,$ctag->GetName().'=') ? str_replace("=".$fields_value,"=".$fields_value."|".urlencode($addonfields_items[$i]),$filterarr) : $filterarr.'&'.$ctag->GetName().'='.urlencode($addonfields_items[$i]);
  376. $is_select = in_array(urlencode($addonfields_items[$i]), $fields_value1) ? 1 : 0;
  377. $fields_value2 = "";
  378. for ($j=0; $j<count($fields_value1); $j++)
  379. {
  380. $fields_value2 .= $fields_value1[$j] != urlencode($addonfields_items[$i]) ? $fields_value1[$j].($j<count($fields_value1)-1 ? "|" : "") : "";
  381. }
  382. $fields_value2 = rtrim($fields_value2, "|");
  383. $href3 = str_replace(array("&".$ctag->GetName()."=".$fields_value,$ctag->GetName()."=".$fields_value, "&".$ctag->GetName()."=&"), array("&".$ctag->GetName()."=".$fields_value2,$ctag->GetName()."=".$fields_value2, "&"), $filterarr);
  384. $href3 = !end(explode("=", $href3)) ? str_replace("&".end(explode("&", $href3)), "", $href3) : $href3;
  385. $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;";
  386. }
  387. $dede_addonfields .= '<br><br>';
  388. break;
  389. }
  390. }
  391. }
  392. }
  393. echo $dede_addonfields;
  394. }