Passed
Push — master ( 1282f8...92a01c )
by Schlaefer
03:31
created

Utility::getFiles()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 20
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 20
ccs 10
cts 10
cp 1
rs 9.4285
c 1
b 0
f 0
cc 2
eloc 10
nc 2
nop 2
crap 2
1
<?php
2
/**
3
 * The Phile Utility class
4
 */
5
namespace Phile\Core;
6
7
/**
8
 * Utility class
9
 *
10
 * @author  PhileCMS
11
 * @link    https://philecms.com
12
 * @license http://opensource.org/licenses/MIT
13
 * @package Phile
14
 */
15
class Utility
16
{
17
18
    /**
19
     * method to get the current http protocol
20
     *
21
     * @return     string the current protocol
22
     * @deprecated since 1.5 will be removed
23
     */
24
    public static function getProtocol()
25
    {
26
        return (new Router)->getProtocol();
27
    }
28
29
    /**
30
     * detect base url
31
     *
32
     * @return     string
33
     * @deprecated since 1.5 will be removed
34
     */
35 2
    public static function getBaseUrl()
36
    {
37 2
        return (new Router)->getBaseUrl();
38
    }
39
40
    /**
41
     * detect install path
42
     *
43
     * @return     string
44
     * @deprecated since 1.5 will be removed
45
     */
46
    public static function getInstallPath()
47
    {
48
        $path = self::getBaseUrl();
0 ignored issues
show
Deprecated Code introduced by
The method Phile\Core\Utility::getBaseUrl() has been deprecated with message: since 1.5 will be removed

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
49
        $path = substr($path, strpos($path, '://') + 3);
50
        $path = substr($path, strpos($path, '/') + 1);
51
52
        return $path;
53
    }
54
55
    /**
56
     * resolve a file path by replace the mod: prefix
57
     *
58
     * @param $path
59
     *
60
     * @return string|null the full filepath or null if file does not exists
61
     */
62
    public static function resolveFilePath($path)
63
    {
64
        // resolve MOD: prefix
65
        if (strtoupper(substr($path, 0, 3)) === 'MOD') {
66
            $path = str_ireplace('mod:', PLUGINS_DIR, $path);
67
        }
68
        // check if file exists
69
        if (file_exists($path)) {
70
            return $path;
71
        }
72
73
        return null;
74
    }
75
76
    /**
77
     * load files e.g. config files
78
     *
79
     * @param $file
80
     *
81
     * @return mixed|null
82
     */
83 7
    public static function load($file)
84
    {
85 7
        if (file_exists($file)) {
86 7
            return include $file;
87
        }
88
89 2
        return null;
90
    }
91
92
    /**
93
     * check if a plugin is loaded
94
     *
95
     * @param      $plugin
96
     * @return     bool
97
     * @deprecated since 1.5 will be removed
98
     * @use        'plugins_loaded' event
99
     */
100
    public static function isPluginLoaded($plugin)
101
    {
102
        $config = Registry::get('Phile_Settings');
103
        if (!isset($config['plugins'])) {
104
            return false;
105
        }
106
        $plugins = $config['plugins'];
107
        return (isset($plugins[$plugin]['active']) && $plugins[$plugin]['active'] === true);
108
    }
109
110
    /**
111
     * static method to get files by directory and file filter
112
     *
113
     * @param $directory
114
     * @param string    $filter
115
     *
116
     * @return array
117
     */
118 6
    public static function getFiles($directory, $filter = '\Phile\FilterIterator\GeneralFileFilterIterator')
119
    {
120 6
        $files = new $filter(
121 6
        new \RecursiveIteratorIterator(
122 6
            new \RecursiveDirectoryIterator(
123 6
                $directory,
124 6
                \RecursiveDirectoryIterator::FOLLOW_SYMLINKS
125
            )
126
        )
127
        );
128 6
        $result = array();
129 6
        foreach ($files as $file) {
130
            /**
131
 * @var \SplFileInfo $file
132
*/
133 6
            $result[] = $file->getPathname();
134
        }
135
136 6
        return $result;
137
    }
138
139
    /**
140
     * redirect to an url
141
     *
142
     * @param      $url        the url to redirect to
143
     * @param      int                               $statusCode the http status code
144
     * @deprecated since 1.5 will be removed
145
     */
146
    public static function redirect($url, $statusCode = 302)
147
    {
148
        (new Response)->redirect($url, $statusCode);
149
    }
150
151
    /**
152
     * generate secure md5 hash
153
     *
154
     * @param $value
155
     *
156
     * @return string
157
     */
158
    public static function getSecureMD5Hash($value)
159
    {
160
        $config = Registry::get('Phile_Settings');
161
162
        return md5($config['encryptionKey'] . $value);
163
    }
164
165
    /**
166
     * method to generate a secure token
167
     * code from http://stackoverflow.com/a/13733588/1372085
168
     * modified by Frank Nägler
169
     *
170
     * @param int  $length
171
     * @param bool $widthSpecialChars
172
     * @param null $additionalChars
173
     *
174
     * @return string
175
     */
176 2
    public static function generateSecureToken($length = 32, $widthSpecialChars = true, $additionalChars = null)
177
    {
178 2
        $token        = "";
0 ignored issues
show
Coding Style Comprehensibility introduced by
The string literal does not require double quotes, as per coding-style, please use single quotes.

PHP provides two ways to mark string literals. Either with single quotes 'literal' or with double quotes "literal". The difference between these is that string literals in double quotes may contain variables with are evaluated at run-time as well as escape sequences.

String literals in single quotes on the other hand are evaluated very literally and the only two characters that needs escaping in the literal are the single quote itself (\') and the backslash (\\). Every other character is displayed as is.

Double quoted string literals may contain other variables or more complex escape sequences.

<?php

$singleQuoted = 'Value';
$doubleQuoted = "\tSingle is $singleQuoted";

print $doubleQuoted;

will print an indented: Single is Value

If your string literal does not contain variables or escape sequences, it should be defined using single quotes to make that fact clear.

For more information on PHP string literals and available escape sequences see the PHP core documentation.

Loading history...
179 2
        $codeAlphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
0 ignored issues
show
Coding Style Comprehensibility introduced by
The string literal ABCDEFGHIJKLMNOPQRSTUVWXYZ does not require double quotes, as per coding-style, please use single quotes.

PHP provides two ways to mark string literals. Either with single quotes 'literal' or with double quotes "literal". The difference between these is that string literals in double quotes may contain variables with are evaluated at run-time as well as escape sequences.

String literals in single quotes on the other hand are evaluated very literally and the only two characters that needs escaping in the literal are the single quote itself (\') and the backslash (\\). Every other character is displayed as is.

Double quoted string literals may contain other variables or more complex escape sequences.

<?php

$singleQuoted = 'Value';
$doubleQuoted = "\tSingle is $singleQuoted";

print $doubleQuoted;

will print an indented: Single is Value

If your string literal does not contain variables or escape sequences, it should be defined using single quotes to make that fact clear.

For more information on PHP string literals and available escape sequences see the PHP core documentation.

Loading history...
180 2
        $codeAlphabet .= "abcdefghijklmnopqrstuvwxyz";
0 ignored issues
show
Coding Style Comprehensibility introduced by
The string literal abcdefghijklmnopqrstuvwxyz does not require double quotes, as per coding-style, please use single quotes.

PHP provides two ways to mark string literals. Either with single quotes 'literal' or with double quotes "literal". The difference between these is that string literals in double quotes may contain variables with are evaluated at run-time as well as escape sequences.

String literals in single quotes on the other hand are evaluated very literally and the only two characters that needs escaping in the literal are the single quote itself (\') and the backslash (\\). Every other character is displayed as is.

Double quoted string literals may contain other variables or more complex escape sequences.

<?php

$singleQuoted = 'Value';
$doubleQuoted = "\tSingle is $singleQuoted";

print $doubleQuoted;

will print an indented: Single is Value

If your string literal does not contain variables or escape sequences, it should be defined using single quotes to make that fact clear.

For more information on PHP string literals and available escape sequences see the PHP core documentation.

Loading history...
181 2
        $codeAlphabet .= "0123456789";
0 ignored issues
show
Coding Style Comprehensibility introduced by
The string literal 0123456789 does not require double quotes, as per coding-style, please use single quotes.

PHP provides two ways to mark string literals. Either with single quotes 'literal' or with double quotes "literal". The difference between these is that string literals in double quotes may contain variables with are evaluated at run-time as well as escape sequences.

String literals in single quotes on the other hand are evaluated very literally and the only two characters that needs escaping in the literal are the single quote itself (\') and the backslash (\\). Every other character is displayed as is.

Double quoted string literals may contain other variables or more complex escape sequences.

<?php

$singleQuoted = 'Value';
$doubleQuoted = "\tSingle is $singleQuoted";

print $doubleQuoted;

will print an indented: Single is Value

If your string literal does not contain variables or escape sequences, it should be defined using single quotes to make that fact clear.

For more information on PHP string literals and available escape sequences see the PHP core documentation.

Loading history...
182 2
        if ($widthSpecialChars) {
183 2
            $codeAlphabet .= "!/()=?[]|{}";
0 ignored issues
show
Coding Style Comprehensibility introduced by
The string literal !/()=?[]|{} does not require double quotes, as per coding-style, please use single quotes.

PHP provides two ways to mark string literals. Either with single quotes 'literal' or with double quotes "literal". The difference between these is that string literals in double quotes may contain variables with are evaluated at run-time as well as escape sequences.

String literals in single quotes on the other hand are evaluated very literally and the only two characters that needs escaping in the literal are the single quote itself (\') and the backslash (\\). Every other character is displayed as is.

Double quoted string literals may contain other variables or more complex escape sequences.

<?php

$singleQuoted = 'Value';
$doubleQuoted = "\tSingle is $singleQuoted";

print $doubleQuoted;

will print an indented: Single is Value

If your string literal does not contain variables or escape sequences, it should be defined using single quotes to make that fact clear.

For more information on PHP string literals and available escape sequences see the PHP core documentation.

Loading history...
184
        }
185 2
        if ($additionalChars !== null) {
186
            $codeAlphabet .= $additionalChars;
187
        }
188 2
        for ($i = 0; $i < $length; $i++) {
189 2
            $token .= $codeAlphabet[Utility::crypto_rand_secure(0, strlen($codeAlphabet))];
190
        }
191
192 2
        return $token;
193
    }
194
195
    /**
196
     * method to get a more secure random value
197
     * code from http://stackoverflow.com/a/13733588/1372085
198
     *
199
     * @param $min
200
     * @param $max
201
     *
202
     * @return mixed
203
     */
204
    // @codingStandardsIgnoreStart
205 2
    public static function crypto_rand_secure($min, $max)
206
    {
207
        // @codingStandardsIgnoreEnd
208 2
        $range = $max - $min;
209 2
        if ($range < 0) {
210
            return $min;
211
        } // not so random...
212 2
        $log    = log($range, 2);
213 2
        $bytes  = (int)($log / 8) + 1; // length in bytes
214 2
        $bits   = (int)$log + 1; // length in bits
215 2
        $filter = (int)(1 << $bits) - 1; // set all lower bits to 1
216
        do {
217 2
            $rnd = hexdec(bin2hex(openssl_random_pseudo_bytes($bytes)));
218 2
            $rnd = $rnd & $filter; // discard irrelevant bits
219 2
        } while ($rnd >= $range);
220
221 2
        return $min + $rnd;
222
    }
223
}
224