Passed
Push — typo3v9 ( 2404ee...b9b5fa )
by Tomas Norre
05:51
created

isAbsPath()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 9
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
cc 3
eloc 3
c 1
b 1
f 0
nc 2
nop 1
dl 0
loc 9
rs 10
1
<?php
2
3
4
/**
5
 * Retrieve path (taken from cli_dispatch.phpsh)
6
 */
7
8
    // Get path to this script
9
$tempPathThisScript = isset($_SERVER['argv'][0]) ? $_SERVER['argv'][0] : (isset($_ENV['_']) ? $_ENV['_'] : $_SERVER['_']);
10
11
    // Resolve path
12
if (!isAbsPath($tempPathThisScript)) {
13
    $workingDirectory = $_SERVER['PWD'] ? $_SERVER['PWD'] : getcwd();
14
    if ($workingDirectory) {
15
        $tempPathThisScript = $workingDirectory . '/' . preg_replace('/\.\//', '', $tempPathThisScript);
16
        if (!@is_file($tempPathThisScript)) {
17
            die('Relative path found, but an error occured during resolving of the absolute path: ' . $tempPathThisScript . PHP_EOL);
18
        }
19
    } else {
20
        die('Relative path found, but resolving absolute path is not supported on this platform.' . PHP_EOL);
21
    }
22
}
23
24
$typo3Root = preg_replace('#typo3conf/ext/crawler/cli/bootstrap.php$#', '', $tempPathThisScript);
25
26
/**
27
 * Second parameter is a base64 encoded serialized array of header data
28
 */
29
$additionalHeaders = unserialize(base64_decode($_SERVER['argv'][3]));
30
if (is_array($additionalHeaders)) {
31
    foreach ($additionalHeaders as $additionalHeader) {
32
        if (strpos($additionalHeader, ':') !== false) {
33
            list($key, $value) = explode(':', $additionalHeader, 2);
34
            $key = str_replace('-', '_', strtoupper(trim($key)));
35
            if ($key != 'HOST') {
36
                $_SERVER['HTTP_' . $key] = $value;
37
            }
38
        }
39
    }
40
}
41
42
    // put parsed query parts into $_GET array
43
$urlParts = parse_url($_SERVER['argv'][2]);
44
    // Populating $_GET
45
parse_str($urlParts['query'], $_GET);
46
    // Populating $_REQUEST
47
parse_str($urlParts['query'], $_REQUEST);
48
    // Populating $_POST
49
$_POST = [];
50
    // Populating $_COOKIE
51
$_COOKIE = [];
52
53
    // Get the TYPO3_SITE_PATH of the website frontend:
54
$typo3SitePath = $_SERVER['argv'][1];
55
56
    // faking the environment
57
$_SERVER['DOCUMENT_ROOT'] = preg_replace('#' . preg_quote($typo3SitePath, '#') . '$#', '', $typo3Root);
58
$_SERVER['HTTP_USER_AGENT'] = 'CLI Mode';
59
$_SERVER['HTTP_HOST'] = $_SERVER['SERVER_NAME'] = $urlParts['host'];
60
$_SERVER['SCRIPT_NAME'] = $_SERVER['PHP_SELF'] = $typo3SitePath . 'index.php';
61
$_SERVER['SCRIPT_FILENAME'] = $_SERVER['PATH_TRANSLATED'] = $typo3Root . 'index.php';
62
$_SERVER['QUERY_STRING'] = (isset($urlParts['query']) ? $urlParts['query'] : '');
63
$_SERVER['REQUEST_URI'] = $urlParts['path'] . (isset($urlParts['query']) ? '?' . $urlParts['query'] : '');
64
$_SERVER['REQUEST_METHOD'] = 'GET';
65
66
    // Define a port if used in the URL:
67
if (isset($urlParts['port'])) {
68
    $_SERVER['HTTP_HOST'] .= ':' . $urlParts['port'];
69
    $_SERVER['SERVER_PORT'] = $urlParts['port'];
70
}
71
72
    // Define HTTPS disposal:
73
if ($urlParts['scheme'] === 'https') {
74
    $_SERVER['HTTPS'] = 'on';
75
}
76
77
chdir($typo3Root);
78
include($typo3Root . '/index.php');
79
80
/**
81
 * Checks if the $path is absolute or relative (detecting either '/' or 'x:/' as first part of string) and returns TRUE if so.
82
 *
83
 * @param string $path File path to evaluate
84
 * @return boolean
85
 */
86
function isAbsPath($path)
87
{
88
    // on Windows also a path starting with a drive letter is absolute: X:/
89
    if (stristr(PHP_OS, 'win') && substr($path, 1, 2) === ':/') {
90
        return true;
91
    }
92
93
    // path starting with a / is always absolute, on every system
94
    return (substr($path, 0, 1) === '/');
95
}
96