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

327 lines
9.5KB

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