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

380 lines
10.0KB

  1. <?php if(!defined('DEDEINC')) exit('dedecms');
  2. /**
  3. * 核心小助手
  4. *
  5. * @version $Id: util.helper.php 4 19:20 2010年7月6日Z tianya $
  6. * @package DedeCMS.Helpers
  7. * @copyright Copyright (c) 2020, DedeBIZ.COM
  8. * @license https://www.dedebiz.com/license
  9. * @link https://www.dedebiz.com
  10. */
  11. define('T_NEW_LINE', -1);
  12. if (!function_exists('token_get_all_nl'))
  13. {
  14. function token_get_all_nl($source)
  15. {
  16. $new_tokens = array();
  17. // Get the tokens
  18. $tokens = token_get_all($source);
  19. // Split newlines into their own tokens
  20. foreach ($tokens as $token)
  21. {
  22. $token_name = is_array($token) ? $token[0] : null;
  23. $token_data = is_array($token) ? $token[1] : $token;
  24. // Do not split encapsed strings or multiline comments
  25. if ($token_name == T_CONSTANT_ENCAPSED_STRING || substr($token_data, 0, 2) == '/*')
  26. {
  27. $new_tokens[] = array($token_name, $token_data);
  28. continue;
  29. }
  30. // Split the data up by newlines
  31. $split_data = preg_split('#(\r\n|\n)#', $token_data, -1, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY);
  32. foreach ($split_data as $data)
  33. {
  34. if ($data == "\r\n" || $data == "\n")
  35. {
  36. // This is a new line token
  37. $new_tokens[] = array(T_NEW_LINE, $data);
  38. }
  39. else
  40. {
  41. // Add the token under the original token name
  42. $new_tokens[] = is_array($token) ? array($token_name, $data) : $data;
  43. }
  44. }
  45. }
  46. return $new_tokens;
  47. }
  48. }
  49. if (!function_exists('token_name_nl'))
  50. {
  51. function token_name_nl($token)
  52. {
  53. if ($token === T_NEW_LINE)
  54. {
  55. return 'T_NEW_LINE';
  56. }
  57. return token_name($token);
  58. }
  59. }
  60. /**
  61. * 获得当前的脚本网址
  62. *
  63. * @return string
  64. */
  65. if ( ! function_exists('GetCurUrl'))
  66. {
  67. function GetCurUrl()
  68. {
  69. if(!empty($_SERVER["REQUEST_URI"]))
  70. {
  71. $scriptName = $_SERVER["REQUEST_URI"];
  72. $nowurl = $scriptName;
  73. }
  74. else
  75. {
  76. $scriptName = $_SERVER["PHP_SELF"];
  77. if(empty($_SERVER["QUERY_STRING"]))
  78. {
  79. $nowurl = $scriptName;
  80. }
  81. else
  82. {
  83. $nowurl = $scriptName."?".$_SERVER["QUERY_STRING"];
  84. }
  85. }
  86. return $nowurl;
  87. }
  88. }
  89. /**
  90. * 获取用户真实地址
  91. *
  92. * @return string 返回用户ip
  93. */
  94. if ( ! function_exists('GetIP'))
  95. {
  96. function GetIP()
  97. {
  98. static $realip = NULL;
  99. if ($realip !== NULL)
  100. {
  101. return $realip;
  102. }
  103. if (isset($_SERVER))
  104. {
  105. if (isset($_SERVER['HTTP_X_FORWARDED_FOR']))
  106. {
  107. $arr = explode(',', $_SERVER['HTTP_X_FORWARDED_FOR']);
  108. /* 取X-Forwarded-For中第x个非unknown的有效IP字符? */
  109. foreach ($arr as $ip)
  110. {
  111. $ip = trim($ip);
  112. if ($ip != 'unknown')
  113. {
  114. $realip = $ip;
  115. break;
  116. }
  117. }
  118. }
  119. elseif (isset($_SERVER['HTTP_CLIENT_IP']))
  120. {
  121. $realip = $_SERVER['HTTP_CLIENT_IP'];
  122. }
  123. else
  124. {
  125. if (isset($_SERVER['REMOTE_ADDR']))
  126. {
  127. $realip = $_SERVER['REMOTE_ADDR'];
  128. }
  129. else
  130. {
  131. $realip = '0.0.0.0';
  132. }
  133. }
  134. }
  135. else
  136. {
  137. if (getenv('HTTP_X_FORWARDED_FOR'))
  138. {
  139. $realip = getenv('HTTP_X_FORWARDED_FOR');
  140. }
  141. elseif (getenv('HTTP_CLIENT_IP'))
  142. {
  143. $realip = getenv('HTTP_CLIENT_IP');
  144. }
  145. else
  146. {
  147. $realip = getenv('REMOTE_ADDR');
  148. }
  149. }
  150. preg_match("/[\d\.]{7,15}/", $realip, $onlineip);
  151. $realip = ! empty($onlineip[0]) ? $onlineip[0] : '0.0.0.0';
  152. return $realip;
  153. }
  154. }
  155. /**
  156. * 获取编辑器
  157. *
  158. * @param string $fname 表单名称
  159. * @param string $fvalue 如果表单中有默认值,则填入默认值
  160. * @param string $nheight 高度
  161. * @param string $etype 编辑器类型
  162. * @param string $gtype 获取类型
  163. * @param string $isfullpage 是否全屏
  164. * @return string
  165. */
  166. if ( ! function_exists('GetEditor'))
  167. {
  168. function GetEditor($fname, $fvalue, $nheight="350", $etype="Basic", $gtype="print", $isfullpage="FALSE",$bbcode=false)
  169. {
  170. if(!function_exists('SpGetEditor'))
  171. {
  172. require_once(DEDEINC."/inc/inc_fun_funAdmin.php");
  173. }
  174. return SpGetEditor($fname, $fvalue, $nheight, $etype, $gtype, $isfullpage, $bbcode);
  175. }
  176. }
  177. /**
  178. * 获取模板
  179. *
  180. * @param string $filename 文件名称
  181. * @return string
  182. */
  183. if ( ! function_exists('GetTemplets'))
  184. {
  185. function GetTemplets($filename)
  186. {
  187. if(file_exists($filename))
  188. {
  189. $fp = fopen($filename,"r");
  190. $rstr = fread($fp,filesize($filename));
  191. fclose($fp);
  192. return $rstr;
  193. }
  194. else
  195. {
  196. return '';
  197. }
  198. }
  199. }
  200. /**
  201. * 获取系统模板
  202. *
  203. * @param $filename 模板文件
  204. * @return string
  205. */
  206. if ( ! function_exists('GetSysTemplets'))
  207. {
  208. function GetSysTemplets($filename)
  209. {
  210. return GetTemplets($GLOBALS['cfg_basedir'].$GLOBALS['cfg_templets_dir'].'/system/'.$filename);
  211. }
  212. }
  213. /**
  214. * 获取新闻提示
  215. *
  216. * @return void
  217. */
  218. if ( ! function_exists('GetNewInfo'))
  219. {
  220. function GetNewInfo()
  221. {
  222. if(!function_exists('SpGetNewInfo'))
  223. {
  224. require_once(DEDEINC."/inc/inc_fun_funAdmin.php");
  225. }
  226. return SpGetNewInfo();
  227. }
  228. }
  229. /**
  230. * 生成一个随机字符
  231. *
  232. * @access public
  233. * @param string $ddnum
  234. * @return string
  235. */
  236. if ( ! function_exists('dd2char'))
  237. {
  238. function dd2char($ddnum)
  239. {
  240. $ddnum = strval($ddnum);
  241. $slen = strlen($ddnum);
  242. $okdd = '';
  243. $nn = '';
  244. for($i=0;$i<$slen;$i++)
  245. {
  246. if(isset($ddnum[$i+1]))
  247. {
  248. $n = $ddnum[$i].$ddnum[$i+1];
  249. if( ($n>96 && $n<123) || ($n>64 && $n<91) )
  250. {
  251. $okdd .= chr($n);
  252. $i++;
  253. }
  254. else
  255. {
  256. $okdd .= $ddnum[$i];
  257. }
  258. }
  259. else
  260. {
  261. $okdd .= $ddnum[$i];
  262. }
  263. }
  264. return $okdd;
  265. }
  266. }
  267. /**
  268. * json_encode兼容函数
  269. *
  270. * @access public
  271. * @param string $data
  272. * @return string
  273. */
  274. if (!function_exists('json_encode')) {
  275. function format_json_value(&$value)
  276. {
  277. if(is_bool($value)) {
  278. $value = $value?'TRUE':'FALSE';
  279. } else if (is_int($value)) {
  280. $value = intval($value);
  281. } else if (is_float($value)) {
  282. $value = floatval($value);
  283. } else if (defined($value) && $value === NULL) {
  284. $value = strval(constant($value));
  285. } else if (is_string($value)) {
  286. $value = '"'.addslashes($value).'"';
  287. }
  288. return $value;
  289. }
  290. function json_encode($data)
  291. {
  292. if(is_object($data)) {
  293. //对象转换成数组
  294. $data = get_object_vars($data);
  295. }else if(!is_array($data)) {
  296. // 普通格式直接输出
  297. return format_json_value($data);
  298. }
  299. // 判断是否关联数组
  300. if(empty($data) || is_numeric(implode('',array_keys($data)))) {
  301. $assoc = FALSE;
  302. }else {
  303. $assoc = TRUE;
  304. }
  305. // 组装 Json字符串
  306. $json = $assoc ? '{' : '[' ;
  307. foreach($data as $key=>$val) {
  308. if(!is_NULL($val)) {
  309. if($assoc) {
  310. $json .= "\"$key\":".json_encode($val).",";
  311. }else {
  312. $json .= json_encode($val).",";
  313. }
  314. }
  315. }
  316. if(strlen($json)>1) {// 加上判断 防止空数组
  317. $json = substr($json,0,-1);
  318. }
  319. $json .= $assoc ? '}' : ']' ;
  320. return $json;
  321. }
  322. }
  323. /**
  324. * json_decode兼容函数
  325. *
  326. * @access public
  327. * @param string $json json数据
  328. * @param string $assoc 当该参数为 TRUE 时,将返回 array 而非 object
  329. * @return string
  330. */
  331. if (!function_exists('json_decode')) {
  332. function json_decode($json, $assoc=FALSE)
  333. {
  334. // 目前不支持二维数组或对象
  335. $begin = substr($json,0,1) ;
  336. if(!in_array($begin,array('{','[')))
  337. // 不是对象或者数组直接返回
  338. return $json;
  339. $parse = substr($json,1,-1);
  340. $data = explode(',',$parse);
  341. if($flag = $begin =='{' ) {
  342. // 转换成PHP对象
  343. $result = new stdClass();
  344. foreach($data as $val) {
  345. $item = explode(':',$val);
  346. $key = substr($item[0],1,-1);
  347. $result->$key = json_decode($item[1],$assoc);
  348. }
  349. if($assoc)
  350. $result = get_object_vars($result);
  351. }else {
  352. // 转换成PHP数组
  353. $result = array();
  354. foreach($data as $val)
  355. $result[] = json_decode($val,$assoc);
  356. }
  357. return $result;
  358. }
  359. }