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

328 lines
9.4KB

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