Completed
Push — master ( fdda97...b0b47a )
by Arman
17s queued 11s
created

src/Helpers/functions/server.php (2 issues)

1
<?php
2
3
/**
4
 * Quantum PHP Framework
5
 *
6
 * An open source software development framework for PHP
7
 *
8
 * @package Quantum
9
 * @author Arman Ag. <[email protected]>
10
 * @copyright Copyright (c) 2018 Softberg LLC (https://softberg.org)
11
 * @link http://quantum.softberg.org/
12
 * @since 2.0.0
13
 */
14
use Quantum\Environment\Server;
15
16
if (!function_exists('get_user_ip')) {
17
18
    /**
19
     * Gets user IP
20
     * @return string|null
21
     */
22
    function get_user_ip()
23
    {
24
        $server = new Server();
25
26
        $user_ip = null;
27
28
        if ($server->get('HTTP_CLIENT_IP')) {
29
            $user_ip = $server->get('HTTP_CLIENT_IP');
30
        } elseif ($server->get('HTTP_X_FORWARDED_FOR')) {
31
            $user_ip = $server->get('HTTP_X_FORWARDED_FOR');
32
        } else {
33
            $user_ip = $server->get('REMOTE_ADDR');
34
        }
35
36
        return $user_ip;
37
    }
38
39
}
40
41
if (!function_exists('getallheaders')) {
42
43
    /** 	
44
     * Get all headers	
45
     * Built in PHP function synonym of apache_request_headers() 
46
     * Declaring here for Nginx server
47
     * @return array	
48
     */
49
    function getallheaders()
50
    {
51
        $server = new Server();
52
53
        $data = $server->all();
54
55
        if (!empty($data)) {
56
            return [];
57
        }
58
59
        $headers = [];
60
61
        foreach ($data as $key => $value) {
62
            if (substr($key, 0, 5) == 'HTTP_') {
63
                $headers[str_replace(' ', '-', ucwords(strtolower(str_replace('_', ' ', substr($key, 5)))))] = $value;
64
            }
65
        }
66
67
        return $headers;
68
    }
69
70
}
71
72
if (!function_exists('parse_raw_http_request')) {
73
74
    /**
75
     * Parses raw http request
76
     * @param mixed $input
77
     * @return mixed
78
     */
79
    function parse_raw_http_request($input)
80
    {
81
        $server = new Server();
82
83
        $encoded_data = [];
84
85
        preg_match('/boundary=(.*)$/', $server->get('CONTENT_TYPE'), $matches);
0 ignored issues
show
It seems like $server->get('CONTENT_TYPE') can also be of type null; however, parameter $subject of preg_match() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

85
        preg_match('/boundary=(.*)$/', /** @scrutinizer ignore-type */ $server->get('CONTENT_TYPE'), $matches);
Loading history...
86
87
        if (count($matches) > 0) {
88
            $boundary = $matches[1];
89
            $blocks = preg_split("/-+$boundary/", $input);
90
91
            if (is_array($blocks)) {
0 ignored issues
show
The condition is_array($blocks) is always true.
Loading history...
92
                array_pop($blocks);
93
                
94
                foreach ($blocks as $id => $block) {
95
                    if (empty($block))
96
                        continue;
97
                    if (strpos($block, 'application/octet-stream') !== false) {
98
                        preg_match("/name=\"([^\"]*)\".*stream[\n|\r]+([^\n\r].*)?$/s", $block, $matches);
99
                        if (count($matches) > 0) {
100
                            $encoded_data['files'][$matches[1]] = isset($matches[2]) ? $matches[2] : '';
101
                        }
102
                    } else {
103
                        preg_match('/name=\"([^\"]*)\"[\n|\r]+([^\n\r].*)?\r$/s', $block, $matches);
104
                        if (count($matches) > 0) {
105
                            $encoded_data[$matches[1]] = isset($matches[2]) ? $matches[2] : '';
106
                        }
107
                    }
108
                }
109
            }
110
        }
111
112
        return $encoded_data;
113
    }
114
115
}
116