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