GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Pull Request — master (#2606)
by
unknown
05:01
created

func.utilities.php ➔ getcwd_safe()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * @package boot
5
 */
6
7
/**
8
 * Redirects the browser to a specified location. Safer than using a
9
 * direct header() call
10
 *
11
 *  @param string $url
12
 */
13
function redirect($url)
14
{
15
    // Just make sure.
16
    $url = str_replace('Location:', null, $url);
17
18
    if (headers_sent($filename, $line)) {
19
        echo "<h1>Error: Cannot redirect to <a href=\"$url\">$url</a></h1><p>Output has already started in $filename on line $line</p>";
20
        exit;
0 ignored issues
show
Coding Style Compatibility introduced by
The function redirect() 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...
21
    }
22
23
    // convert idn back to ascii for redirect
24
25
    if (function_exists('idn_to_ascii')) {
26
        $root = parse_url(URL);
27
        $host = $root['host'];
28
        $url  = str_replace($host, idn_to_ascii($host), $url);
29
    }
30
31
    cleanup_session_cookies();
32
    header('Status: 302 Found');
33
    header('Expires: Mon, 12 Dec 1982 06:00:00 GMT');
34
    header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT');
35
    header('Cache-Control: no-cache, must-revalidate, max-age=0');
36
    header('Pragma: no-cache');
37
    header("Location: $url");
38
39
    exit;
0 ignored issues
show
Coding Style Compatibility introduced by
The function redirect() 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...
40
}
41
42
/**
43
 * Returns the current working directory, replacing any \
44
 *  with /. Use for Windows compatibility.
45
 *
46
 *  @return string
47
 */
48
function getcwd_safe()
49
{
50
    return str_replace('\\', '/', getcwd());
51
}
52
53
/**
54
 * Checks that a constant has not been defined before defining
55
 * it. If the constant is already defined, this function will do
56
 * nothing, otherwise, it will set the constant
57
 *
58
 * @param string $name
59
 *  The name of the constant to set
60
 * @param string $value
61
 *  The value of the desired constant
62
 */
63
function define_safe($name, $value)
64
{
65
    if (!defined($name)) {
66
        define($name, $value);
67
    }
68
}
69
70
/**
71
 * Retrieve a value from the $_SERVER array. Makes sure the key exists.
72
 * Returns null otherwise.
73
 *
74
 * This function is an extension point. We could check other storage for
75
 * specific values or enforce some security restrictions.
76
 *
77
 * @param string $name
78
 *  The name of the value to retrieve
79
 * @return mixed
80
 *  The value, is it exists
81
 */
82
function server_safe($name)
0 ignored issues
show
Coding Style introduced by
server_safe 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...
83
{
84
    if (isset($_SERVER[$name])) {
85
        return $_SERVER[$name];
86
    }
87
    return null;
88
}
89
90
/**
91
 * Returns the current URL string from within the Administration
92
 * context. It omits the Symphony directory from the current URL.
93
 *
94
 *  @return string
95
 */
96
function getCurrentPage()
97
{
98
    if (!isset($_GET['symphony-page']) || !is_string($_GET['symphony-page'])) {
99
        return null;
100
    }
101
102
    return '/' . filter_var(trim($_GET['symphony-page'], '/'), FILTER_SANITIZE_STRING) . '/';
103
}
104
105
/**
106
 * Used as a basic stopwatch for profiling. The default `$action`
107
 * starts the timer. Setting `$action` to 'stop' and passing the
108
 * start time returns the difference between now and that time.
109
 *
110
 *  @param string $action (optional)
111
 *  @param integer $start_time (optional)
112
 *  @return integer
113
 */
114
function precision_timer($action = 'start', $start_time = null)
115
{
116
    $currtime = microtime(true);
117
118
    if ($action == 'stop') {
119
        return $currtime - $start_time;
120
    }
121
122
    return $currtime;
123
}
124
125
/**
126
 * Convert php.ini size format to bytes
127
 *
128
 *  @param string $val (optional)
129
 *  @return integer
130
 */
131
function ini_size_to_bytes($val)
132
{
133
    $val = trim($val);
134
    $last = strtolower($val[strlen($val)-1]);
135
136 View Code Duplication
    switch ($last) {
137
        case 'g':
0 ignored issues
show
Coding Style introduced by
There must be a comment when fall-through is intentional in a non-empty case body
Loading history...
138
            $val *= 1024;
139
        case 'm':
0 ignored issues
show
Coding Style introduced by
There must be a comment when fall-through is intentional in a non-empty case body
Loading history...
140
            $val *= 1024;
141
        case 'k':
142
            $val *= 1024;
143
    }
144
145
    return $val;
146
}
147
148
/**
149
 * Cleans up Session Cookies. When there is no data in the session the cookie will be unset.
150
 * If there is data, the cookie will be renewed, expiring it in two weeks from now.
151
 * This will improve the interoperability with caches like Varnish and Squid.
152
 *
153
 * @since 2.3.3
154
 * @author creativedutchmen (Huib Keemink)
155
 * @return void
156
 */
157
function cleanup_session_cookies()
0 ignored issues
show
Coding Style introduced by
cleanup_session_cookies 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...
158
{
159
    /*
160
    Unfortunately there is no way to delete a specific previously set cookie from PHP.
161
    The only way seems to be the method employed here: store all the cookie we need to keep, then delete every cookie and add the stored cookies again.
162
    Luckily we can just store the raw header and output them again, so we do not need to actively parse the header string.
163
    */
164
    $cookie_params = session_get_cookie_params();
165
    $list = headers_list();
166
    $custom_cookies = array();
167
168
    foreach ($list as $hdr) {
169
        if ((stripos($hdr, 'Set-Cookie') !== false) && (stripos($hdr, session_id()) === false)) {
170
            $custom_cookies[] = $hdr;
171
        }
172
    }
173
174
    header_remove('Set-Cookie');
175
176
    foreach ($custom_cookies as $custom_cookie) {
177
        header($custom_cookie);
178
    }
179
180
    $session_is_empty = is_session_empty();
181
182
    if ($session_is_empty && !empty($_COOKIE[session_name()])) {
183
        setcookie(
184
            session_name(),
185
            session_id(),
186
            time() - 3600,
187
            $cookie_params['path'],
188
            $cookie_params['domain'],
189
            $cookie_params['secure'],
190
            $cookie_params['httponly']
191
        );
192
    } elseif (!$session_is_empty) {
193
        setcookie(
194
            session_name(),
195
            session_id(),
196
            time() + TWO_WEEKS,
197
            $cookie_params['path'],
198
            $cookie_params['domain'],
199
            $cookie_params['secure'],
200
            $cookie_params['httponly']
201
        );
202
    }
203
}
204
205
/**
206
 * Function will loop over the $_SESSION and find out if it is empty or not
207
 *
208
 * @since Symphony 2.4
209
 * @return boolean
210
 */
211
function is_session_empty()
0 ignored issues
show
Coding Style introduced by
is_session_empty 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...
212
{
213
    $session_is_empty = true;
214
    if (is_array($_SESSION)) {
215
        foreach ($_SESSION as $contents) {
216
            if (!empty($contents)) {
217
                $session_is_empty = false;
218
            }
219
        }
220
    }
221
222
    return $session_is_empty;
223
}
224
225
/**
226
 * Responsible for picking the launcher function and starting it.
227
 *
228
 *  @param string $mode (optional)
229
 */
230
function symphony($mode)
231
{
232
    $launcher = SYMPHONY_LAUNCHER;
233
    $launcher($mode);
234
}
235
236
/**
237
 * Responsible for launching a standard symphony instance and
238
 * sending output to the browser.
239
 *
240
 *  @param string $mode (optional)
241
 *  @return integer
242
 */
243
function symphony_launcher($mode)
244
{
245
    if (is_string($mode) && strtolower($mode) == 'administration') {
246
        $renderer = Administration::instance();
247
    } else {
248
        $renderer = Frontend::instance();
249
    }
250
251
    $output = $renderer->display(getCurrentPage());
252
253
    // #1808
254
    if (server_safe('HTTP_MOD_REWRITE') != null) {
255
        $output = file_get_contents(GenericExceptionHandler::getTemplate('fatalerror.rewrite'));
256
        $output = str_replace('{ASSETS_URL}', ASSETS_URL, $output);
257
        $output = str_replace('{SYMPHONY_URL}', SYMPHONY_URL, $output);
258
        $output = str_replace('{URL}', URL, $output);
259
        echo $output;
260
        exit;
0 ignored issues
show
Coding Style Compatibility introduced by
The function symphony_launcher() 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...
261
    }
262
263
    cleanup_session_cookies();
264
265
    echo $output;
266
267
    return $renderer;
268
}
269
270
271
/**
272
 * The translation function accepts an English string and returns its translation
273
 * to the active system language. If the given string is not available in the
274
 * current dictionary the original English string will be returned. Given an optional
275
 * `$inserts` array, the function will replace translation placeholders using `vsprintf()`.
276
 * Since Symphony 2.3, it is also possible to have multiple translation of the same string
277
 * according to the page namespace (i.e. the value returned by Symphony's `getPageNamespace()`
278
 * method). In your lang file, use the `$dictionary` key as namespace and its value as an array
279
 * of context-aware translations, as shown below:
280
 *
281
 * $dictionary = array(
282
 *        [...]
283
 *
284
 *        'Create new' => 'Translation for Create New',
285
 *
286
 *        '/blueprints/datasources' => array(
287
 *            'Create new' =>
288
 *            'If we are inside a /blueprints/datasources/* page, this translation will be returned for the string'
289
 *        ),
290
 *
291
 *        [...]
292
 *  );
293
 *
294
 * @see core.Symphony#getPageNamespace()
295
 * @param string $string
296
 *  The string that should be translated
297
 * @param array $inserts (optional)
298
 *  Optional array used to replace translation placeholders, defaults to NULL
299
 * @return string
300
 *  Returns the translated string
301
 */
302
function __($string, $inserts = null)
303
{
304
    return Lang::translate($string, $inserts);
305
}
306