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

300 lines
9.0KB

  1. <?php
  2. if (!defined('DEDEINC')) exit('dedebiz');
  3. /**
  4. * 核心小助手
  5. *
  6. * @version $id:util.helper.php 4 19:20 2010年7月6日 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. $realip = $_SERVER['HTTP_X_FORWARDED_FOR'];
  89. } else if (isset($_SERVER['HTTP_CLIENT_IP'])) {
  90. $realip = $_SERVER['HTTP_CLIENT_IP'];
  91. } else {
  92. $realip = @$_SERVER['REMOTE_ADDR'];
  93. }
  94. } else {
  95. if (getenv('HTTP_X_FORWARDED_FOR')) {
  96. $realip = getenv('HTTP_X_FORWARDED_FOR');
  97. } else if (getenv('HTTP_CLIENT_IP')) {
  98. $realip = getenv('HTTP_CLIENT_IP');
  99. } else {
  100. $realip = getenv('REMOTE_ADDR');
  101. }
  102. }
  103. if (filter_var($realip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4 | FILTER_FLAG_IPV6)) {
  104. return $realip;
  105. } else {
  106. return 'unknown';
  107. }
  108. }
  109. }
  110. /**
  111. * 获取修改器
  112. *
  113. * @param string $fname 表单名称
  114. * @param string $fvalue 如果表单中有默认值,则填入默认值
  115. * @param string $nheight 高度
  116. * @param string $etype 修改器类型
  117. * @param string $gtype 获取类型
  118. * @param string $isfullpage 是否全屏
  119. * @return string
  120. */
  121. if (!function_exists('GetEditor')) {
  122. function GetEditor($fname, $fvalue, $nheight = "350", $etype = "Basic", $gtype = "print", $isfullpage = "FALSE", $bbcode = false)
  123. {
  124. if (!function_exists('SpGetEditor')) {
  125. require_once(DEDEINC."/inc/inc_fun_funAdmin.php");
  126. }
  127. return SpGetEditor($fname, $fvalue, $nheight, $etype, $gtype, $isfullpage, $bbcode);
  128. }
  129. }
  130. /**
  131. * 获取模板
  132. *
  133. * @param string $filename 文件名称
  134. * @return string
  135. */
  136. if (!function_exists('GetTemplets')) {
  137. function GetTemplets($filename)
  138. {
  139. if (file_exists($filename)) {
  140. $fp = fopen($filename, "r");
  141. $rstr = fread($fp, filesize($filename));
  142. fclose($fp);
  143. return $rstr;
  144. } else {
  145. return '';
  146. }
  147. }
  148. }
  149. /**
  150. * 获取系统模板
  151. *
  152. * @param $filename 模板文件
  153. * @return string
  154. */
  155. if (!function_exists('GetSysTemplets')) {
  156. function GetSysTemplets($filename)
  157. {
  158. return GetTemplets($GLOBALS['cfg_basedir'].$GLOBALS['cfg_templets_dir'].'/system/'.$filename);
  159. }
  160. }
  161. /**
  162. * 获取新闻提示
  163. *
  164. * @return void
  165. */
  166. if (!function_exists('GetNewInfo')) {
  167. function GetNewInfo()
  168. {
  169. if (!function_exists('SpGetNewInfo')) {
  170. require_once(DEDEINC."/inc/inc_fun_funAdmin.php");
  171. }
  172. return SpGetNewInfo();
  173. }
  174. }
  175. /**
  176. * 生成一个随机字符
  177. *
  178. * @access public
  179. * @param string $ddnum
  180. * @return string
  181. */
  182. if (!function_exists('dd2char')) {
  183. function dd2char($ddnum)
  184. {
  185. $ddnum = strval($ddnum);
  186. $slen = strlen($ddnum);
  187. $okdd = '';
  188. $nn = '';
  189. for ($i = 0; $i < $slen; $i++) {
  190. if (isset($ddnum[$i + 1])) {
  191. $n = $ddnum[$i].$ddnum[$i + 1];
  192. if (($n > 96 && $n < 123) || ($n > 64 && $n < 91)) {
  193. $okdd .= chr($n);
  194. $i++;
  195. } else {
  196. $okdd .= $ddnum[$i];
  197. }
  198. } else {
  199. $okdd .= $ddnum[$i];
  200. }
  201. }
  202. return $okdd;
  203. }
  204. }
  205. /**
  206. * json_encode兼容函数
  207. *
  208. * @access public
  209. * @param string $data
  210. * @return string
  211. */
  212. if (!function_exists('json_encode')) {
  213. function format_json_value(&$value)
  214. {
  215. if (is_bool($value)) {
  216. $value = $value ? 'TRUE' : 'FALSE';
  217. } else if (is_int($value)) {
  218. $value = intval($value);
  219. } else if (is_float($value)) {
  220. $value = floatval($value);
  221. } else if (defined($value) && $value === NULL) {
  222. $value = strval(constant((string)$value));
  223. } else if (is_string($value)) {
  224. $value = '"'.addslashes($value).'"';
  225. }
  226. return $value;
  227. }
  228. function json_encode($data)
  229. {
  230. if (is_object($data)) {
  231. //对象转换成数组
  232. $data = get_object_vars($data);
  233. } else if (!is_array($data)) {
  234. //普通格式直接输出
  235. return format_json_value($data);
  236. }
  237. //判断是否关联数组
  238. if (empty($data) || is_numeric(implode('', array_keys($data)))) {
  239. $assoc = FALSE;
  240. } else {
  241. $assoc = TRUE;
  242. }
  243. //组装 Json字符串
  244. $json = $assoc ? '{' : '[';
  245. foreach ($data as $key => $val) {
  246. if (!is_NULL($val)) {
  247. if ($assoc) {
  248. $json .= "\"$key\":".json_encode($val).",";
  249. } else {
  250. $json .= json_encode($val).",";
  251. }
  252. }
  253. }
  254. if (strlen($json) > 1) { //加上判断 防止空数组
  255. $json = substr($json, 0, -1);
  256. }
  257. $json .= $assoc ? '}' : ']';
  258. return $json;
  259. }
  260. }
  261. /**
  262. * json_decode兼容函数
  263. *
  264. * @access public
  265. * @param string $json json数据
  266. * @param string $assoc 当该参数为TRUE时,返回array而非object
  267. * @return string
  268. */
  269. if (!function_exists('json_decode')) {
  270. function json_decode($json, $assoc = FALSE)
  271. {
  272. //目前不支持二维数组或对象
  273. $begin = substr($json, 0, 1);
  274. if (!in_array($begin, array('{', '[')))
  275. //不是对象或者数组直接返回
  276. return $json;
  277. $parse = substr($json, 1, -1);
  278. $data = explode(',', $parse);
  279. if ($flag = $begin == '{') {
  280. //转换成PHP对象
  281. $result = new stdClass();
  282. foreach ($data as $val) {
  283. $item = explode(':', $val);
  284. $key = substr($item[0], 1, -1);
  285. $result->$key = json_decode($item[1], $assoc);
  286. }
  287. if ($assoc)
  288. $result = get_object_vars($result);
  289. } else {
  290. //转换成PHP数组
  291. $result = array();
  292. foreach ($data as $val)
  293. $result[] = json_decode($val, $assoc);
  294. }
  295. return $result;
  296. }
  297. }
  298. ?>