Passed
Push — master ( ade1c9...725b43 )
by kill
02:33
created

function.php ➔ dump()   B

Complexity

Conditions 5
Paths 12

Size

Total Lines 22
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 17
nc 12
nop 4
dl 0
loc 22
rs 8.6737
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * 获取客户端IP地址
5
 * @param integer $type 返回类型 0 返回IP地址 1 返回IPV4地址数字
6
 * @param boolean $adv 是否进行高级模式获取(有可能被伪装)
7
 * @return mixed
8
 */
9
function get_client_ip($type=0, $adv=true)
0 ignored issues
show
Coding Style introduced by
get_client_ip uses the super-global variable $_SERVER which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
10
{
11
    $type=$type ? 1 : 0;
12
    static $ip=null;
13
    if (null !== $ip) {
14
        return $ip[$type];
15
    }
16
    if ($adv) {
17
        if (isset($_SERVER['HTTP_X_FORWARDED_FOR'])) {
18
            $arr=explode(',', $_SERVER['HTTP_X_FORWARDED_FOR']);
19
            $pos=array_search('unknown', $arr);
20
            if (false !== $pos) {
21
                unset($arr[$pos]);
22
            }
23
            $ip=trim($arr[0]);
24
        } elseif (isset($_SERVER['HTTP_CLIENT_IP'])) {
25
            $ip=$_SERVER['HTTP_CLIENT_IP'];
26
        } elseif (isset($_SERVER['REMOTE_ADDR'])) {
27
            $ip=$_SERVER['REMOTE_ADDR'];
28
        }
29
    } elseif (isset($_SERVER['REMOTE_ADDR'])) {
30
        $ip=$_SERVER['REMOTE_ADDR'];
31
    }
32
    // IP地址合法验证
33
    $long=sprintf("%u", ip2long($ip));
34
    $ip=$long ? array($ip, $long) : array('0.0.0.0', 0);
35
    return $ip[$type];
36
}
37
//2为直接输出数组
38
function show_json(array $arr,$type=2)
39
{
40
41
    if(isset($arr['status']) && $type==2){
42
        $ret=$arr;
43
    }
44
    else{
45
        $ret['status'] = $type;
0 ignored issues
show
Coding Style Comprehensibility introduced by
$ret was never initialized. Although not strictly required by PHP, it is generally a good practice to add $ret = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
46
        $ret['data'] = $arr;
47
    }
48
49
    $obj = json_encode($ret);
50
    header('Content-Type: application/json');
51
    echo $obj;
52
    exit();
0 ignored issues
show
Coding Style Compatibility introduced by
The function show_json() contains an exit expression.

An exit expression should only be used in rare cases. For example, if you write a short command line script.

In most cases however, using an exit expression makes the code untestable and often causes incompatibilities with other libraries. Thus, unless you are absolutely sure it is required here, we recommend to refactor your code to avoid its usage.

Loading history...
53
}
54
55
function success($arr)
56
{
57
    show_json($arr,1);
58
}
59
60
function error($arr)
61
{
62
    show_json($arr,0);
63
}
64
function not_found($str='page not found,that is all we know!'){
65
    header('HTTP/1.1 404 Not Found');
66
    header("status: 404 Not Found");
67
    exit($str);
0 ignored issues
show
Coding Style Compatibility introduced by
The function not_found() contains an exit expression.

An exit expression should only be used in rare cases. For example, if you write a short command line script.

In most cases however, using an exit expression makes the code untestable and often causes incompatibilities with other libraries. Thus, unless you are absolutely sure it is required here, we recommend to refactor your code to avoid its usage.

Loading history...
68
}
69
70
71
/**
72
73
 * 数据签名认证
74
75
 * @param  array  $data 被认证的数据
76
77
 * @return string       签名
78
79
 * @author 麦当苗儿 <[email protected]>
80
81
 */
82
function data_auth_sign($data) {
83
    //数据类型检测
84
85
    if(!is_array($data)){
86
87
        $data = (array)$data;
88
    }
89
    ksort($data); //排序
90
    $code = http_build_query($data); //url编码并生成query字符串
91
    $sign = sha1($code); //生成签名
92
    return $sign;
93
}
94
/**
95
 * session管理函数
96
 * @param string|array $name session名称 如果为数组则表示进行session设置
97
 * @param mixed $value session值
98
 * @return mixed
99
 */
100
function session($name = '', $value = '') {
0 ignored issues
show
Coding Style introduced by
session uses the super-global variable $_SESSION which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
101
    if (is_array($name)) {
102
103
        if (isset($name['id'])) {
104
            session_id($name['id']);
105
        }
106
107
        if (isset($name['name'])) {
108
            session_name($name['name']);
109
        }
110
111
        if (isset($name['path'])) {
112
            session_save_path($name['path']);
113
        }
114
115
        if (isset($name['domain'])) {
116
            ini_set('session.cookie_domain', $name['domain']);
117
        }
118
119
        if (isset($name['expire'])) {
120
            ini_set('session.gc_maxlifetime', $name['expire']);
121
            ini_set('session.cookie_lifetime', $name['expire']);
122
        }
123
        if (isset($name['use_trans_sid'])) {
124
            ini_set('session.use_trans_sid', $name['use_trans_sid'] ? 1 : 0);
125
        }
126
127
        if (isset($name['use_cookies'])) {
128
            ini_set('session.use_cookies', $name['use_cookies'] ? 1 : 0);
129
        }
130
131
        if (isset($name['cache_limiter'])) {
132
            session_cache_limiter($name['cache_limiter']);
133
        }
134
135
        if (isset($name['cache_expire'])) {
136
            session_cache_expire($name['cache_expire']);
137
        }
138
        session_start();
139
    } elseif ('' === $value) {
140
        if ('' === $name) {
141
            // 获取全部的session
142
            return $_SESSION;
143
        } elseif (0 === strpos($name, '[')) {
144
            // session 操作
145
            if ('[pause]' == $name) {// 暂停session
146
                session_write_close();
147
            } elseif ('[start]' == $name) {
148
                // 启动session
149
                session_start();
150
            } elseif ('[destroy]' == $name) {
151
                // 销毁session
152
                $_SESSION = array();
153
                session_unset();
154
                session_destroy();
155
            } elseif ('[regenerate]' == $name) {
156
                // 重新生成id
157
                session_regenerate_id();
158
            }
159
        }else {
160
            if (strpos($name, '.')) {
161
                list($name1, $name2) = explode('.', $name);
162
                return isset($_SESSION[$name1][$name2]) ? $_SESSION[$name1][$name2] : null;
163
            } else {
164
                return isset($_SESSION[$name]) ? $_SESSION[$name] : null;
165
            }
166
        }
167
    } elseif (is_null($value)) {
168
        // 删除session
169
        if (strpos($name, '.')) {
170
            list($name1, $name2) = explode('.', $name);
171
            unset($_SESSION[$name1][$name2]);
172
        } else {
173
            unset($_SESSION[$name]);
174
        }
175
    } else {
176
        // 设置session
177
        if (strpos($name, '.')) {
178
            list($name1, $name2) = explode('.', $name);
179
            $_SESSION[$name1][$name2] = $value;
180
        } else {
181
            $_SESSION[$name] = $value;
182
        }
183
    }
184
    return null;
185
}
186
187
188
function admin_is_login(){
189
    $user = session('admin_user_auth');
190
    if (empty($user)) {
191
        return 0;
192
    } else {
193
        $auth_sign=session('admin_user_auth_sign');
194
        if(data_auth_sign($user)!=$auth_sign){
195
            return 0;
196
        }
197
        return $user['uid'];
198
    }
199
}
200
function json($str){
201
    $obj = json_encode($str,JSON_UNESCAPED_UNICODE);
202
    header('Content-Type: application/json');
203
    echo $obj;
204
}
205
/**
206
 * 浏览器友好的变量输出
207
 * @param mixed         $var 变量
208
 * @param boolean       $echo 是否输出 默认为true 如果为false 则返回输出字符串
209
 * @param string        $label 标签 默认为空
210
 * @param integer       $flags htmlspecialchars flags
211
 * @return void|string
212
 */
213
function dump($var, $echo = true, $label = null, $flags = ENT_SUBSTITUTE)
214
{
215
    $label = (null === $label) ? '' : rtrim($label) . ':';
216
    ob_start();
217
    var_dump($var);
0 ignored issues
show
Security Debugging Code introduced by
var_dump($var); looks like debug code. Are you sure you do not want to remove it? This might expose sensitive data.
Loading history...
218
    $output = ob_get_clean();
219
    $output = preg_replace('/\]\=\>\n(\s+)/m', '] => ', $output);
220
    if (IS_CLI) {
221
        $output = PHP_EOL . $label . $output . PHP_EOL;
222
    } else {
223
        if (!extension_loaded('xdebug')) {
224
            $output = htmlspecialchars($output, $flags);
225
        }
226
        $output = '<pre>' . $label . $output . '</pre>';
227
    }
228
    if ($echo) {
229
        echo($output);
230
        return;
231
    } else {
232
        return $output;
233
    }
234
}
235
236
237
/**
238
 * 获取输入参数 支持过滤和默认值
239
 * 使用方法:
240
 * <code>
241
 * I('id',0); 获取id参数 自动判断get或者post
242
 * I('post.name','','htmlspecialchars'); 获取$_POST['name']
243
 * I('get.'); 获取$_GET
244
 * </code>
245
 * @param string $name 变量的名称 支持指定类型
246
 * @param mixed $default 不存在的时候默认值
247
 * @param mixed $filter 参数过滤方法
248
 * @param mixed $datas 要获取的额外数据源
249
 * @return mixed
250
 */
251
function I($name, $default = '', $filter = null, $datas = null)
0 ignored issues
show
Coding Style introduced by
I uses the super-global variable $_GET which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
Coding Style introduced by
I uses the super-global variable $_POST which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
Coding Style introduced by
I uses the super-global variable $_SERVER which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
Coding Style introduced by
I uses the super-global variable $_REQUEST which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
Coding Style introduced by
I uses the super-global variable $_SESSION which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
Coding Style introduced by
I uses the super-global variable $_COOKIE which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
Coding Style introduced by
I uses the super-global variable $GLOBALS which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
252
{
253
    static $_PUT = null;
254
    if (strpos($name, '/')) {
255
        // 指定修饰符
256
        list($name, $type) = explode('/', $name, 2);
257
    } else{
258
        // 默认强制转换为字符串
259
        $type = 's';
260
    }
261
    if (strpos($name, '.')) {
262
        // 指定参数来源
263
        list($method, $name) = explode('.', $name, 2);
264
    } else {
265
        // 默认为自动判断
266
        $method = 'param';
267
    }
268
    switch (strtolower($method)) {
269
        case 'get':
270
            $input = &$_GET;
271
            break;
272
        case 'post':
273
            $input = &$_POST;
274
            break;
275
        case 'put':
276
            if (is_null($_PUT)) {
277
                parse_str(file_get_contents('php://input'), $_PUT);
278
            }
279
            $input = $_PUT;
280
            break;
281
        case 'param':
282
            switch ($_SERVER['REQUEST_METHOD']) {
283
                case 'POST':
284
                    $input = $_POST;
285
                    break;
286
                case 'PUT':
287
                    if (is_null($_PUT)) {
288
                        parse_str(file_get_contents('php://input'), $_PUT);
289
                    }
290
                    $input = $_PUT;
291
                    break;
292
                default:
293
                    $input = $_GET;
294
            }
295
            break;
296
        case 'path':
297
            $input = array();
298
            if (!empty($_SERVER['PATH_INFO'])) {
299
                $depr  = C('URL_PATHINFO_DEPR');
300
                $input = explode($depr, trim($_SERVER['PATH_INFO'], $depr));
301
            }
302
            break;
303
        case 'request':
304
            $input = &$_REQUEST;
305
            break;
306
        case 'session':
307
            $input = &$_SESSION;
308
            break;
309
        case 'cookie':
310
            $input = &$_COOKIE;
311
            break;
312
        case 'server':
313
            $input = &$_SERVER;
314
            break;
315
        case 'globals':
316
            $input = &$GLOBALS;
317
            break;
318
        case 'data':
319
            $input = &$datas;
320
            break;
321
        default:
322
            return null;
323
    }
324
    if ('' == $name) {
325
        // 获取全部变量
326
        $data    = $input;
327
        $filters = isset($filter) ? $filter : 'htmlspecialchars';
328
        if ($filters) {
329
            if (is_string($filters)) {
330
                $filters = explode(',', $filters);
0 ignored issues
show
Unused Code introduced by
$filters is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
331
            }
332
            else if (is_array($filters)){
333
                foreach ($filters as $filter) {
334
                    $data = array_map_recursive($filter, $data); // 参数过滤
335
                }
336
            }else{
337
                throw new \RuntimeException('$filters must be an array or string.');
338
            }
339
        }
340
    } elseif (isset($input[$name])) {
341
        // 取值操作
342
        $data    = $input[$name];
343
        $filters = isset($filter) ? $filter : 'htmlspecialchars';
344
        if ($filters) {
345
            if (is_string($filters)) {
346
                if (0 === strpos($filters, '/')) {
347
                    if (1 !== preg_match($filters, (string) $data)) {
348
                        // 支持正则验证
349
                        return isset($default) ? $default : null;
350
                    }
351
                } else {
352
                    $filters = explode(',', $filters);
353
                }
354
            } elseif (is_int($filters)) {
355
                $filters = array($filters);
356
            }
357
358
            if (is_array($filters)) {
359
                foreach ($filters as $filter) {
360
                    $filter = trim($filter);
361
                    if (function_exists($filter)) {
362
                        $data = is_array($data) ? array_map_recursive($filter, $data) : $filter($data); // 参数过滤
363
                    } else {
364
                        $data = filter_var($data, is_int($filter) ? $filter : filter_id($filter));
365
                        if (false === $data) {
366
                            return isset($default) ? $default : null;
367
                        }
368
                    }
369
                }
370
            }
371
        }
372
        if (!empty($type)) {
373
            switch (strtolower($type)) {
374
                case 'a':    // 数组
375
                    $data = (array) $data;
376
                    break;
377
                case 'd':    // 数字
378
                    $data = (int) $data;
379
                    break;
380
                case 'f':    // 浮点
381
                    $data = (float) $data;
382
                    break;
383
                case 'b':    // 布尔
384
                    $data = (boolean) $data;
385
                    break;
386
                case 's':// 字符串
387
                default:
388
                    $data = (string) $data;
389
            }
390
        }
391
    } else {
392
        // 变量默认值
393
        $data = isset($default) ? $default : null;
394
    }
395
    is_array($data) && array_walk_recursive($data, 'think_filter');
396
    return $data;
397
}
398
function array_map_recursive($filter, $data)
399
{
400
    $result = array();
401
    foreach ($data as $key => $val) {
402
        $result[$key] = is_array($val)
403
            ? array_map_recursive($filter, $val)
404
            : call_user_func($filter, $val);
405
    }
406
    return $result;
407
}
408
function think_filter(&$value)
409
{
410
    // TODO 其他安全过滤
411
412
    // 过滤查询特殊字符
413
    if (preg_match('/^(EXP|NEQ|GT|EGT|LT|ELT|OR|XOR|LIKE|NOTLIKE|NOT BETWEEN|NOTBETWEEN|BETWEEN|NOTIN|NOT IN|IN)$/i', $value)) {
414
        $value .= ' ';
415
    }
416
}
417
function config($name=null,$value=null,$default=null){
418
    $config=\puck\Conf::load();
419
    if ($name===null){
420
        return $config->all();
421
    }
422
    if ($value===null){
423
        return $config->get($name,$default);
424
    }
425
    $config->set($name,$value);
426
}
427
/**
428
 * 字符串命名风格转换
429
 * type 0 将Java风格转换为C的风格 1 将C风格转换为Java的风格
430
 * @param string $name 字符串
431
 * @param integer $type 转换类型
432
 * @return string
433
 */
434
function parse_name($name, $type = 0) {
435
    if ($type) {
436
        return ucfirst(preg_replace_callback('/_([a-zA-Z])/', function ($match) {return strtoupper($match[1]);}, $name));
437
    } else {
438
        return strtolower(trim(preg_replace("/[A-Z]/", "_\\0", $name), "_"));
439
    }
440
}