Passed
Push — develop ( 7a79ec...b37dd3 )
by Jens
06:50
created

Request::isSecure()   B

Complexity

Conditions 5
Paths 7

Size

Total Lines 5
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 4
nc 7
nop 0
dl 0
loc 5
rs 8.8571
c 0
b 0
f 0
1
<?php
2
3
namespace CloudControl\Cms\cc {
4
    /**
5
     * Class Request
6
     * @package CloudControl\Cms\cc
7
     */
8
    class Request
9
    {
10
11
        /**
12
         * @var string
13
         */
14
        public static $subfolders;
15
        /**
16
         * @var string
17
         */
18
        public static $requestUri;
19
        /**
20
         * @var string
21
         */
22
        public static $relativeUri;
23
        /**
24
         * @var string
25
         */
26
        public static $queryString;
27
        /**
28
         * @var array
29
         */
30
        public static $requestParameters;
31
        /**
32
         * @var array
33
         */
34
        public static $post = array();
35
        /**
36
         * @var array
37
         */
38
        public static $get = array();
39
        /**
40
         * @var array
41
         */
42
        private $statics;
43
44
        /**
45
         * Request constructor.
46
         */
47
        public function __construct()
48
        {
49
            $rootPath = str_replace('\\', '/', realpath(str_replace('\\', '/', dirname(__FILE__)) . '/../../') . '/');
50
51
            if (PHP_SAPI === 'cli-server') {
52
                self::$subfolders = '/';
53
            } else {
54
                self::$subfolders = '/' . str_replace('//', '/', str_replace(str_replace('\\', '/', $_SERVER['DOCUMENT_ROOT']), "", $rootPath));
55
                self::$subfolders = str_replace('//', '/', self::$subfolders);
56
                self::$subfolders = str_replace('vendor/getcloudcontrol/cloudcontrol/', '', self::$subfolders);
57
            }
58
59
            if (PHP_SAPI === 'cli') {
60
                global $argv;
0 ignored issues
show
Compatibility Best Practice introduced by
Use of global functionality is not recommended; it makes your code harder to test, and less reusable.

Instead of relying on global state, we recommend one of these alternatives:

1. Pass all data via parameters

function myFunction($a, $b) {
    // Do something
}

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

    public function __construct($a, $b) {
        $this->a = $a;
        $this->b = $b;
    }

    public function myFunction() {
        // Do something
    }
}
Loading history...
61
                array_shift($argv);
62
                self::$queryString = '';
63
                self::$requestUri = self::$subfolders . implode('/', $argv);
64
            } else {
65
                self::$requestUri = $_SERVER['REQUEST_URI'];
66
                self::$queryString = isset($_SERVER['QUERY_STRING']) ? $_SERVER['QUERY_STRING'] : '';
67
            }
68
            if (self::$subfolders === '/') {
69
                self::$relativeUri = str_replace('?' . self::$queryString, '', substr(self::$requestUri, 1));
70
            } else {
71
                self::$relativeUri = str_replace('?' . self::$queryString, '', str_replace(self::$subfolders, '', self::$requestUri));
72
            }
73
74
            self::$requestParameters = explode('/', self::$relativeUri);
75
76
            self::$get = $_GET;
77
            self::$post = $_POST;
78
79
            $this->statics = array(
80
                'subfolders' => self::$subfolders,
81
                'requestUri' => self::$requestUri,
82
                'relativeUri' => self::$relativeUri,
83
                'queryString' => self::$queryString,
84
                'requestParameters' => self::$requestParameters,
85
                'post' => self::$post,
86
                'get' => self::$get
87
            );
88
        }
89
90
        public static function isSecure() {
91
            return
92
                (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off')
93
                || $_SERVER['SERVER_PORT'] == 443
94
                || (isset($_SERVER['HTTP_X_FORWARDED_PROTO']) && $_SERVER['HTTP_X_FORWARDED_PROTO'] === 'https');
95
        }
96
    }
97
}