Passed
Push — master ( 18eded...8ccda3 )
by Gaetano
12:44
created

slowLoris()   A

Complexity

Conditions 5
Paths 3

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 1
Metric Value
cc 5
eloc 7
nc 3
nop 2
dl 0
loc 12
rs 9.6111
c 1
b 1
f 1
1
<?php
2
3
// File accessed by http requests sent by the test suite, enabling testing of demo, debugger, extras files.
4
// It makes all errors visible, triggers generation of code-coverage information, and runs the target file,
5
// which is specified as GET param.
6
7
// In case this file is made available on an open-access server, avoid it being useable by anyone who can not also
8
// write a specific file to disk.
9
// NB: keep filename, cookie name in sync with the code within the TestCase classes sending http requests to this file
10
$idFile = sys_get_temp_dir() . '/phpunit_rand_id.txt';
11
$randId = isset($_COOKIE['PHPUNIT_RANDOM_TEST_ID']) ? $_COOKIE['PHPUNIT_RANDOM_TEST_ID'] : '';
12
$fileId = file_exists($idFile) ? file_get_contents($idFile) : '';
13
if ($randId == '' || $fileId == '' || $fileId !== $randId) {
14
    //die('This url can only be accessed by the test suite');
15
}
16
17
// Make errors visible
18
ini_set('display_errors', true);
0 ignored issues
show
Bug introduced by
true of type true is incompatible with the type string expected by parameter $value of ini_set(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

18
ini_set('display_errors', /** @scrutinizer ignore-type */ true);
Loading history...
19
error_reporting(E_ALL);
20
21
// Set up a constant which can be used by demo code to tell if the testuite is in action.
22
// We use a constant because it can not be injected via GET/POST/COOKIE/ENV
23
const TESTMODE = true;
24
25
// Out-of-band information: let the client manipulate the page operations
26
if (isset($_COOKIE['PHPUNIT_SELENIUM_TEST_ID']) && extension_loaded('xdebug')) {
27
    // NB: this has to be kept in sync with phunit_coverage.php
28
    $GLOBALS['PHPUNIT_COVERAGE_DATA_DIRECTORY'] = '/tmp/phpxmlrpc_coverage';
29
    if (!is_dir($GLOBALS['PHPUNIT_COVERAGE_DATA_DIRECTORY'])) {
30
        mkdir($GLOBALS['PHPUNIT_COVERAGE_DATA_DIRECTORY']);
31
        chmod($GLOBALS['PHPUNIT_COVERAGE_DATA_DIRECTORY'], 0777);
32
    }
33
34
    include_once __DIR__ . "/../vendor/phpunit/phpunit-selenium/PHPUnit/Extensions/SeleniumCommon/prepend.php";
35
}
36
37
$targetFile = null;
38
$rootDir = dirname(__DIR__);
39
if (isset($_GET['debugger'])) {
40
    if (strpos(realpath($rootDir.'/debugger/'.$_GET['debugger']), realpath($rootDir.'/debugger/')) === 0) {
41
        $targetFile = realpath($rootDir.'/debugger/'.$_GET['debugger']);
42
    }
43
} elseif (isset($_GET['demo'])) {
44
    if (strpos(realpath($rootDir.'/demo/'.$_GET['demo']), realpath($rootDir.'/demo/')) === 0) {
45
        $targetFile = realpath($rootDir.'/demo/'.$_GET['demo']);
46
    }
47
} elseif (isset($_GET['extras'])) {
48
    if (strpos(realpath($rootDir.'/extras/'.$_GET['extras']), realpath($rootDir.'/extras/')) === 0) {
49
        $targetFile = realpath($rootDir.'/extras/'.$_GET['extras']);
50
    }
51
}
52
if ($targetFile) {
53
    include $targetFile;
54
}
55
56
if (isset($_COOKIE['PHPUNIT_SELENIUM_TEST_ID']) && extension_loaded('xdebug')) {
57
    include_once __DIR__ . "/../vendor/phpunit/phpunit-selenium/PHPUnit/Extensions/SeleniumCommon/append.php";
58
}
59
60
/**
61
 * @param PhpXmlRpc\Server $s
62
 * @return void
63
 */
64
function preflight($s) {
65
    if (isset($_GET['FORCE_DEBUG'])) {
66
        $s->setOption(PhpXmlRpc\Server::OPT_DEBUG, $_GET['FORCE_DEBUG']);
67
    }
68
    if (isset($_GET['RESPONSE_ENCODING'])) {
69
        $s->setOption(PhpXmlRpc\Server::OPT_RESPONSE_CHARSET_ENCODING, $_GET['RESPONSE_ENCODING']);
70
    }
71
    if (isset($_GET['DETECT_ENCODINGS'])) {
72
        PhpXmlRpc\PhpXmlRpc::$xmlrpc_detectencodings = $_GET['DETECT_ENCODINGS'];
73
    }
74
    if (isset($_GET['EXCEPTION_HANDLING'])) {
75
        $s->setOption(PhpXmlRpc\Server::OPT_EXCEPTION_HANDLING, $_GET['EXCEPTION_HANDLING']);
76
    }
77
    if (isset($_GET['FORCE_AUTH'])) {
78
        // We implement both  Basic and Digest auth in php to avoid having to set it up in a vhost.
79
        // Code taken from php.net
80
        // NB: we do NOT check for valid credentials!
81
        if ($_GET['FORCE_AUTH'] == 'Basic') {
82
            if (!isset($_SERVER['PHP_AUTH_USER']) && !isset($_SERVER['REMOTE_USER']) && !isset($_SERVER['REDIRECT_REMOTE_USER'])) {
83
                header('HTTP/1.0 401 Unauthorized');
84
                header('WWW-Authenticate: Basic realm="Phpxmlrpc Basic Realm"');
85
                die('Text visible if user hits Cancel button');
86
            }
87
        } elseif ($_GET['FORCE_AUTH'] == 'Digest') {
88
            if (empty($_SERVER['PHP_AUTH_DIGEST'])) {
89
                header('HTTP/1.1 401 Unauthorized');
90
                header('WWW-Authenticate: Digest realm="Phpxmlrpc Digest Realm",qop="auth",nonce="' . uniqid() . '",opaque="' . md5('Phpxmlrpc Digest Realm') . '"');
91
                die('Text visible if user hits Cancel button');
92
            }
93
        }
94
    }
95
    if (isset($_GET['FORCE_REDIRECT'])) {
96
        header('HTTP/1.0 302 Found');
97
        unset($_GET['FORCE_REDIRECT']);
98
        header('Location: ' . $_SERVER['REQUEST_URI'] . (count($_GET) ? '?' . http_build_query($_GET) : ''));
99
        die();
100
    }
101
    if (isset($_GET['SLOW_LORIS']) && $_GET['SLOW_LORIS'] > 0) {
102
        slowLoris((int)$_GET['SLOW_LORIS'], $s);
103
        die();
0 ignored issues
show
Best Practice introduced by
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
104
    }
105
}
106
107
/**
108
 * Used to test timeouts: send out the payload one chunk every $secs second (10 chunks in total)
109
 * @param int $secs between 1 and 60
110
 * @param PhpXmlrpc\Server $s
111
 */
112
function slowLoris($secs, $s)
0 ignored issues
show
Unused Code introduced by
The parameter $s is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

112
function slowLoris($secs, /** @scrutinizer ignore-unused */ $s)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
113
{
114
    /// @todo as is, this method can not be used by eg. jsonrpc servers. We could look at the value $s::$responseClass
115
    ///       to improve that
116
    $strings = array('<?xml version="1.0"?>','<methodResponse>','<params>','<param>','<value>','<string></string>','</value>','</param>','</params>','</methodResponse>');
117
118
    header('Content-type: xml; charset=utf-8');
119
    foreach($strings as $i => $string) {
120
        echo $string;
121
        flush();
122
        if ($i < count($strings) && $secs > 0 && $secs <= 60) {
123
            sleep($secs);
124
        }
125
    }
126
}
127