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

387 lines
11KB

  1. <?php
  2. /**
  3. * 系统核心函数存放文件
  4. * @version $Id: common.func.php 4 16:39 2010年7月6日Z tianya $
  5. * @package DedeCMS.Libraries
  6. * @copyright Copyright (c) 2007 - 2018, DesDev, Inc.
  7. * @copyright Copyright (c) 2020, DedeBIZ.COM
  8. * @license https://www.dedebiz.com/license/v6
  9. * @link https://www.dedebiz.com
  10. */
  11. if(!defined('DEDEINC')) exit('dedecms');
  12. if (version_compare(PHP_VERSION, '7.0.0', '>='))
  13. {
  14. if (!function_exists('mysql_connect') AND function_exists('mysqli_connect')) {
  15. function mysql_connect($server, $username, $password)
  16. {
  17. return mysqli_connect($server, $username, $password);
  18. }
  19. }
  20. if (!function_exists('mysql_query') AND function_exists('mysqli_query')) {
  21. function mysql_query($query, $link)
  22. {
  23. return mysqli_query($link, $query);
  24. }
  25. }
  26. if (!function_exists('mysql_select_db') AND function_exists('mysqli_select_db')) {
  27. function mysql_select_db($database_name, $link)
  28. {
  29. return mysqli_select_db($link, $database_name);
  30. }
  31. }
  32. if (!function_exists('mysql_fetch_array') AND function_exists('mysqli_fetch_array')) {
  33. function mysql_fetch_array($result)
  34. {
  35. return mysqli_fetch_array($result);
  36. }
  37. }
  38. if (!function_exists('mysql_close') AND function_exists('mysqli_close')) {
  39. function mysql_close($link)
  40. {
  41. return mysqli_close($link);
  42. }
  43. }
  44. if (!function_exists('split')) {
  45. function split($pattern, $string){
  46. return explode($pattern, $string);
  47. }
  48. }
  49. }
  50. function make_hash()
  51. {
  52. $rand = dede_random_bytes(16);
  53. $_SESSION['token'] = ($rand === FALSE)
  54. ? md5(uniqid(mt_rand(), TRUE))
  55. : bin2hex($rand);
  56. return $_SESSION['token'];
  57. }
  58. function dede_random_bytes($length)
  59. {
  60. if (empty($length) OR ! ctype_digit((string) $length))
  61. {
  62. return FALSE;
  63. }
  64. if (function_exists('openssl_random_pseudo_bytes'))
  65. {
  66. return openssl_random_pseudo_bytes($length);
  67. }
  68. if (function_exists('random_bytes'))
  69. {
  70. try
  71. {
  72. return random_bytes((int) $length);
  73. }
  74. catch (Exception $e)
  75. {
  76. return FALSE;
  77. }
  78. }
  79. if (defined('MCRYPT_DEV_URANDOM') && ($output = mcrypt_create_iv($length, MCRYPT_DEV_URANDOM)) !== FALSE)
  80. {
  81. return $output;
  82. }
  83. if (is_readable('/dev/urandom') && ($fp = fopen('/dev/urandom', 'rb')) !== FALSE)
  84. {
  85. version_compare(PHP_VERSION, '5.4.0', '>=') && stream_set_chunk_size($fp, $length);
  86. $output = fread($fp, $length);
  87. fclose($fp);
  88. if ($output !== FALSE)
  89. {
  90. return $output;
  91. }
  92. }
  93. return FALSE;
  94. }
  95. /**
  96. * 载入小助手,系统默认载入小助手
  97. * 在/data/helper.inc.php中进行默认小助手初始化的设置
  98. * 使用示例:
  99. * 在开发中,首先需要创建一个小助手函数,目录在\include\helpers中
  100. * 例如,我们创建一个示例为test.helper.php,文件基本内容如下:
  101. * <code>
  102. * if ( ! function_exists('HelloDede'))
  103. * {
  104. * function HelloDede()
  105. * {
  106. * echo "Hello! Dede...";
  107. * }
  108. * }
  109. * </code>
  110. * 则我们在开发中使用这个小助手的时候直接使用函数helper('test');初始化它
  111. * 然后在文件中就可以直接使用:HelloDede();来进行调用.
  112. *
  113. * @access public
  114. * @param mix $helpers 小助手名称,可以是数组,可以是单个字符串
  115. * @return void
  116. */
  117. $_helpers = array();
  118. function helper($helpers)
  119. {
  120. //如果是数组,则进行递归操作
  121. if (is_array($helpers))
  122. {
  123. foreach($helpers as $dede)
  124. {
  125. helper($dede);
  126. }
  127. return;
  128. }
  129. if (isset($_helpers[$helpers]))
  130. {
  131. return;
  132. }
  133. if (file_exists(DEDEINC.'/helpers/'.$helpers.'.helper.php'))
  134. {
  135. include_once(DEDEINC.'/helpers/'.$helpers.'.helper.php');
  136. $_helpers[$helpers] = TRUE;
  137. }
  138. // 无法载入小助手
  139. if ( ! isset($_helpers[$helpers]))
  140. {
  141. exit('Unable to load the requested file: helpers/'.$helpers.'.helper.php');
  142. }
  143. }
  144. function dede_htmlspecialchars($str) {
  145. global $cfg_soft_lang;
  146. if (version_compare(PHP_VERSION, '5.4.0', '<')) return htmlspecialchars($str);
  147. if ($cfg_soft_lang=='gb2312') return htmlspecialchars($str,ENT_COMPAT,'ISO-8859-1');
  148. else return htmlspecialchars($str);
  149. }
  150. /**
  151. * 控制器调用函数
  152. *
  153. * @access public
  154. * @param string $ct 控制器
  155. * @param string $ac 操作事件
  156. * @param string $path 指定控制器所在目录
  157. * @return string
  158. */
  159. function RunApp($ct, $ac = '',$directory = '')
  160. {
  161. $ct = preg_replace("/[^0-9a-z_]/i", '', $ct);
  162. $ac = preg_replace("/[^0-9a-z_]/i", '', $ac);
  163. $ac = empty ( $ac ) ? $ac = 'index' : $ac;
  164. if(!empty($directory)) $path = DEDECONTROL.'/'.$directory. '/' . $ct . '.php';
  165. else $path = DEDECONTROL . '/' . $ct . '.php';
  166. if (file_exists ( $path ))
  167. {
  168. require $path;
  169. } else {
  170. if (DEBUG_LEVEL === TRUE)
  171. {
  172. trigger_error("Load Controller false!");
  173. }
  174. //生产环境中,找不到控制器的情况不需要记录日志
  175. else
  176. {
  177. header ( "location:/404.html" );
  178. die ();
  179. }
  180. }
  181. $action = 'ac_'.$ac;
  182. $loaderr = FALSE;
  183. $instance = new $ct ( );
  184. if (method_exists ( $instance, $action ) === TRUE)
  185. {
  186. $instance->$action();
  187. unset($instance);
  188. } else $loaderr = TRUE;
  189. if ($loaderr)
  190. {
  191. if (DEBUG_LEVEL === TRUE)
  192. {
  193. trigger_error("Load Method false!");
  194. }
  195. //生产环境中,找不到控制器的情况不需要记录日志
  196. else
  197. {
  198. header ( "location:/404.html" );
  199. die ();
  200. }
  201. }
  202. }
  203. /**
  204. * 载入小助手,这里用户可能载入用helps载入多个小助手
  205. *
  206. * @access public
  207. * @param string
  208. * @return string
  209. */
  210. function helpers($helpers)
  211. {
  212. helper($helpers);
  213. }
  214. //兼容php4的file_put_contents
  215. if(!function_exists('file_put_contents'))
  216. {
  217. function file_put_contents($n, $d)
  218. {
  219. $f=@fopen($n, "w");
  220. if (!$f)
  221. {
  222. return FALSE;
  223. }
  224. else
  225. {
  226. fwrite($f, $d);
  227. fclose($f);
  228. return TRUE;
  229. }
  230. }
  231. }
  232. /**
  233. * 显示更新信息
  234. *
  235. * @return void
  236. */
  237. function UpdateStat()
  238. {
  239. include_once(DEDEINC."/inc/inc_stat.php");
  240. return SpUpdateStat();
  241. }
  242. $arrs1 = array(0x63,0x66,0x67,0x5f,0x70,0x6f,0x77,0x65,0x72,0x62,0x79);
  243. $arrs2 = array(0x20,0x3c,0x61,0x20,0x68,0x72,0x65,0x66,0x3d,0x68,0x74,0x74,0x70,0x3a,0x2f,0x2f,
  244. 0x77,0x77,0x77,0x2e,0x64,0x65,0x64,0x65,0x63,0x6d,0x73,0x2e,0x63,0x6f,0x6d,0x20,0x74,0x61,0x72,
  245. 0x67,0x65,0x74,0x3d,0x27,0x5f,0x62,0x6c,0x61,0x6e,0x6b,0x27,0x3e,0x50,0x6f,0x77,0x65,0x72,0x20,
  246. 0x62,0x79,0x20,0x44,0x65,0x64,0x65,0x43,0x6d,0x73,0x3c,0x2f,0x61,0x3e);
  247. /**
  248. * 短消息函数,可以在某个动作处理后友好的提示信息
  249. *
  250. * @param string $msg 消息提示信息
  251. * @param string $gourl 跳转地址
  252. * @param int $onlymsg 仅显示信息
  253. * @param int $limittime 限制时间
  254. * @return void
  255. */
  256. function ShowMsg($msg, $gourl, $onlymsg=0, $limittime=0)
  257. {
  258. global $cfg_soft_lang,$cfg_cmsurl;
  259. if(empty($GLOBALS['cfg_plus_dir'])) $GLOBALS['cfg_plus_dir'] = '..';
  260. $htmlhead = "<html>\r\n<head>\r\n<title>DedeCMS提示信息</title>\r\n<meta http-equiv=\"Content-Type\" content=\"text/html; charset={$cfg_soft_lang}\" />\r\n<meta name=\"viewport\" content=\"width=device-width, initial-scale=1, shrink-to-fit=no\">";
  261. $htmlhead .= "<link rel=\"stylesheet\" href=\"{$cfg_cmsurl}/static/css/bootstrap.min.css\"><style>.modal {position: static;}</style><link href=\"{$cfg_cmsurl}/static/font-awesome/css/font-awesome.min.css\" rel=\"stylesheet\">";
  262. $htmlhead .= "<base target='_self'/></head>\r\n<body leftmargin='0' topmargin='0' bgcolor='#FFFFFF'>".(isset($GLOBALS['ucsynlogin']) ? $GLOBALS['ucsynlogin'] : '')."\r\n<center>\r\n<script>\r\n";
  263. $htmlfoot = "</script>\r\n</center>\r\n</body>\r\n</html>\r\n";
  264. $litime = ($limittime==0 ? 1000 : $limittime);
  265. $func = '';
  266. if($gourl=='-1')
  267. {
  268. if($limittime==0) $litime = 5000;
  269. $gourl = "javascript:history.go(-1);";
  270. }
  271. if($gourl=='' || $onlymsg==1)
  272. {
  273. $msg = "<script>alert(\"".str_replace("\"","“",$msg)."\");</script>";
  274. }
  275. else
  276. {
  277. //当网址为:close::objname 时, 关闭父框架的id=objname元素
  278. if(preg_match('/close::/',$gourl))
  279. {
  280. $tgobj = trim(preg_replace('/close::/', '', $gourl));
  281. $gourl = 'javascript:;';
  282. $func .= "window.parent.document.getElementById('{$tgobj}').style.display='none';\r\n";
  283. }
  284. $func .= " var pgo=0;
  285. function JumpUrl(){
  286. if(pgo==0){ location='$gourl'; pgo=1; }
  287. }\r\n";
  288. $rmsg = $func;
  289. $rmsg .= "document.write(\"<main class='container'><div class='modal' tabindex='-1' role='dialog' style='display:block'><div class='modal-dialog'><div class='modal-content'><div class='modal-header'><h6 class='modal-title'>";
  290. $rmsg .= "DedeCMS 提示信息!</h6></div><div class='modal-body'>\");\r\n";
  291. $rmsg .= "document.write(\"".str_replace("\"","“",$msg)."\");\r\n";
  292. $rmsg .= "document.write(\"";
  293. if($onlymsg==0)
  294. {
  295. if( $gourl != 'javascript:;' && $gourl != '')
  296. {
  297. $rmsg .= "<br /><a href='{$gourl}'>如果你的浏览器没反应,请点击这里...</a>";
  298. $rmsg .= "</div></div></div></div></main>\");\r\n";
  299. $rmsg .= "setTimeout('JumpUrl()',$litime);";
  300. }
  301. else
  302. {
  303. $rmsg .= "</div></div></div></div></main>\");\r\n";
  304. }
  305. }
  306. else
  307. {
  308. $rmsg .= "</div></div></div></div></main>\");\r\n";
  309. }
  310. $msg = $htmlhead.$rmsg.$htmlfoot;
  311. }
  312. echo $msg;
  313. }
  314. /**
  315. * 获取验证码的session值
  316. *
  317. * @return string
  318. */
  319. function GetCkVdValue()
  320. {
  321. @session_id($_COOKIE['PHPSESSID']);
  322. @session_start();
  323. return isset($_SESSION['securimage_code_value']) ? $_SESSION['securimage_code_value'] : '';
  324. }
  325. /**
  326. * PHP某些版本有Bug,不能在同一作用域中同时读session并改注销它,因此调用后需执行本函数
  327. *
  328. * @return void
  329. */
  330. function ResetVdValue()
  331. {
  332. @session_start();
  333. $_SESSION['securimage_code_value'] = '';
  334. }
  335. function IndexSub($idx, $num) {
  336. return intval($idx - $num) == 0? '0 ' : intval($idx - $num);
  337. }
  338. // 用来返回index的active
  339. function IndexActive($idx){
  340. if ($idx == 1) {
  341. return ' active';
  342. } else {
  343. return '';
  344. }
  345. }
  346. // 自定义函数接口
  347. // 这里主要兼容早期的用户扩展,v5.7之后我们建议使用小助手helper进行扩展
  348. if( file_exists(DEDEINC.'/extend.func.php') )
  349. {
  350. require_once(DEDEINC.'/extend.func.php');
  351. }