Completed
Push — master ( 00ed35...63fbb3 )
by Oscar
10:20
created

Helpers::fixPath()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
c 1
b 1
f 0
dl 0
loc 11
rs 9.4286
cc 2
eloc 7
nc 1
nop 1
1
<?php
2
3
namespace Psr7Middlewares\Utils;
4
5
use Psr\Http\Message\RequestInterface;
6
use Psr\Http\Message\ResponseInterface;
7
8
/**
9
 * Helper functions.
10
 */
11
class Helpers
12
{
13
    private static $hash_equals;
14
15
    /**
16
     * helper function to fix paths '//' or '/./' or '/foo/../' in a path.
17
     *
18
     * @param string $path Path to resolve
19
     *
20
     * @return string
21
     */
22
    public static function fixPath($path)
23
    {
24
        $path = str_replace('\\', '/', $path); //windows paths
25
        $replace = ['#(/\.?/)#', '#/(?!\.\.)[^/]+/\.\./#'];
26
27
        do {
28
            $path = preg_replace($replace, '/', $path, -1, $n);
29
        } while ($n > 0);
30
31
        return $path;
32
    }
33
34
    /**
35
     * Join several pieces into a path.
36
     * 
37
     * @param string
38
     *               ...
39
     * 
40
     * @return string
41
     */
42
    public static function joinPath()
43
    {
44
        return self::fixPath(implode('/', func_get_args()));
45
    }
46
47
    /**
48
     * Check whether a request is or not ajax.
49
     * 
50
     * @param RequestInterface $request
51
     * 
52
     * @return bool
53
     */
54
    public static function isAjax(RequestInterface $request)
55
    {
56
        return strtolower($request->getHeaderLine('X-Requested-With')) === 'xmlhttprequest';
57
    }
58
59
    /**
60
     * Check if a request is post or any similar method.
61
     * 
62
     * @param RequestInterface $request
63
     * 
64
     * @return bool
65
     */
66
    public static function isPost(RequestInterface $request)
67
    {
68
        switch (strtoupper($request->getMethod())) {
69
            case 'GET':
70
            case 'HEAD':
71
            case 'CONNECT':
72
            case 'TRACE':
73
            case 'OPTIONS':
74
                return false;
75
        }
76
77
        return true;
78
    }
79
80
    /**
81
     * Check whether a response is a redirection.
82
     * 
83
     * @param ResponseInterface $response
84
     * 
85
     * @return bool
86
     */
87
    public static function isRedirect(ResponseInterface $response)
88
    {
89
        return in_array($response->getStatusCode(), [302, 301]);
90
    }
91
92
    /**
93
     * Return the output buffer.
94
     * 
95
     * @param int $level
96
     * 
97
     * @return string
98
     */
99
    public static function getOutput($level)
100
    {
101
        $output = '';
102
103
        while (ob_get_level() >= $level) {
104
            $output .= ob_get_clean();
105
        }
106
107
        return $output;
108
    }
109
110
    /**
111
     * Very short timing attack safe string comparison for PHP < 5.6
112
     * http://php.net/manual/en/function.hash-equals.php#118384
113
     * 
114
     * @param string $a
115
     * @param string $b
116
     * 
117
     * @return bool
118
     */
119
    public static function hash_equals($a, $b)
120
    {
121
        if (self::$hash_equals === null) {
122
            self::$hash_equals = function_exists('hash_equals');
123
        }
124
125
        if (self::$hash_equals) {
126
            return hash_equals($a, $b);
127
        }
128
129
        return substr_count($a ^ $b, "\0") * 2 === strlen($a . $b);
130
    }
131
}
132