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

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