Passed
Push — develop ( 91c1d2...446210 )
by Schlaefer
41s
created

Utility::getBaseUrl()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 7

Duplication

Lines 11
Ratio 100 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 11
loc 11
ccs 0
cts 6
cp 0
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 7
nc 2
nop 0
crap 6
1
<?php
2
/**
3
 * The Phile Utility class
4
 */
5
namespace Phile\Core;
6
7
use Phile\Core\Container;
8
9
/**
10
 * Utility class
11
 *
12
 * @author  PhileCMS
13
 * @link    https://philecms.com
14
 * @license http://opensource.org/licenses/MIT
15
 * @package Phile
16
 */
17
class Utility
18
{
19
20
    /**
21
     * method to get the current http protocol
22
     *
23
     * @return     string the current protocol
24
     * @deprecated since 1.5 will be removed
25
     */
26
    public static function getProtocol()
27
    {
28
        return Container::getInstance()->get('Phile_Router')->getProtocol();
29
    }
30
31
    /**
32
     * detect base url
33
     *
34
     * @return     string
35
     * @deprecated since 1.5 will be removed
36
     */
37 View Code Duplication
    public static function getBaseUrl()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
38
    {
39
        $container = Container::getInstance();
40
        if ($container->has('Phile_Router')) {
41
            $router = $container->get('Phile_Router');
42
        } else {
43
            // BC: some old 1.x plugins may call this before the core is initialized
44
            $router = new Router;
45
        }
46
        return $router->getBaseUrl();
47
    }
48
49
    /**
50
     * detect install path
51
     *
52
     * @return     string
53
     * @deprecated since 1.5 will be removed
54
     */
55
    public static function getInstallPath()
56
    {
57
        $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...
58
        $path = substr($path, strpos($path, '://') + 3);
59
        $path = substr($path, strpos($path, '/') + 1);
60
61
        return $path;
62
    }
63
64
    /**
65
     * resolve a file path by replace the mod: prefix
66
     *
67
     * @param $path
68
     *
69
     * @return string|null the full filepath or null if file does not exists
70
     */
71
    public static function resolveFilePath($path)
72
    {
73
        // resolve MOD: prefix
74
        if (strtoupper(substr($path, 0, 3)) === 'MOD') {
75
            $path = str_ireplace('mod:', PLUGINS_DIR, $path);
76
        }
77
        // check if file exists
78
        if (file_exists($path)) {
79
            return $path;
80
        }
81
82
        return null;
83
    }
84
85
    /**
86
     * load files e.g. config files
87
     *
88
     * @param $file
89
     *
90
     * @return mixed|null
91
     */
92 32
    public static function load($file)
93
    {
94 32
        if (file_exists($file)) {
95 32
            return include $file;
96
        }
97
98 26
        return null;
99
    }
100
101
    /**
102
     * check if a plugin is loaded
103
     *
104
     * @param      $plugin
105
     * @return     bool
106
     * @deprecated since 1.5 will be removed
107
     * @use        'plugins_loaded' event
108
     */
109
    public static function isPluginLoaded($plugin)
110
    {
111
        $config = Container::getInstance()->get('Phile_Config');
112
        if ($config->get('plugins')) {
113
            return false;
114
        }
115
        $plugins = $config->get('plugins');
116
        return (isset($plugins[$plugin]['active']) && $plugins[$plugin]['active'] === true);
117
    }
118
119
    /**
120
     * static method to get files by directory and file filter
121
     *
122
     * @param $directory
123
     * @param string    $filter
124
     *
125
     * @return array
126
     */
127 7
    public static function getFiles($directory, $filter = '\Phile\FilterIterator\GeneralFileFilterIterator')
128
    {
129 7
        $files = new $filter(
130 7
            new \RecursiveIteratorIterator(
131 7
                new \RecursiveDirectoryIterator(
132 7
                    $directory,
133 7
                    \RecursiveDirectoryIterator::FOLLOW_SYMLINKS
134
                )
135
            )
136
        );
137 7
        $result = array();
138 7
        foreach ($files as $file) {
139
            /**
140
 * @var \SplFileInfo $file
141
*/
142 7
            $result[] = $file->getPathname();
143
        }
144
145 7
        return $result;
146
    }
147
148
    /**
149
     * redirect to an url
150
     *
151
     * @param      $url        the url to redirect to
152
     * @param      int                               $statusCode the http status code
153
     * @deprecated since 1.5 will be removed
154
     */
155
    public static function redirect($url, $statusCode = 302)
156
    {
157
        (new Response)->redirect($url, $statusCode);
158
    }
159
160
    /**
161
     * generate secure md5 hash
162
     *
163
     * @param $value
164
     *
165
     * @return string
166
     */
167
    public static function getSecureMD5Hash($value)
168
    {
169
        $config = Container::getInstance()->get('Phile_Config');
170
171
        return md5($config->get('encryptionKey') . $value);
172
    }
173
174
    /**
175
     * method to generate a secure token
176
     * code from http://stackoverflow.com/a/13733588/1372085
177
     * modified by Frank Nägler
178
     *
179
     * @param int  $length
180
     * @param bool $widthSpecialChars
181
     * @param null $additionalChars
182
     *
183
     * @return string
184
     */
185 1
    public static function generateSecureToken($length = 32, $widthSpecialChars = true, $additionalChars = null)
186
    {
187 1
        $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...
188 1
        $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...
189 1
        $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...
190 1
        $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...
191 1
        if ($widthSpecialChars) {
192 1
            $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...
193
        }
194 1
        if ($additionalChars !== null) {
195
            $codeAlphabet .= $additionalChars;
196
        }
197 1
        for ($i = 0; $i < $length; $i++) {
198 1
            $token .= $codeAlphabet[Utility::crypto_rand_secure(0, strlen($codeAlphabet))];
199
        }
200
201 1
        return $token;
202
    }
203
204
    /**
205
     * method to get a more secure random value
206
     * code from http://stackoverflow.com/a/13733588/1372085
207
     *
208
     * @param $min
209
     * @param $max
210
     *
211
     * @return mixed
212
     */
213
    // @codingStandardsIgnoreStart
214 1
    public static function crypto_rand_secure($min, $max)
215
    {
216
        // @codingStandardsIgnoreEnd
217 1
        $range = $max - $min;
218 1
        if ($range < 0) {
219
            return $min;
220
        } // not so random...
221 1
        $log    = log($range, 2);
222 1
        $bytes  = (int)($log / 8) + 1; // length in bytes
223 1
        $bits   = (int)$log + 1; // length in bits
224 1
        $filter = (int)(1 << $bits) - 1; // set all lower bits to 1
225
        do {
226 1
            $rnd = hexdec(bin2hex(openssl_random_pseudo_bytes($bytes)));
227 1
            $rnd = $rnd & $filter; // discard irrelevant bits
228 1
        } while ($rnd >= $range);
229
230 1
        return $min + $rnd;
231
    }
232
}
233