Passed
Push — master ( 1f470b...e71961 )
by Justin
03:43
created

PHPUtils::strEqs()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 2
rs 10
c 0
b 0
f 0
cc 1
eloc 1
nc 1
nop 2
1
<?php
2
3
class PHPUtils {
4
5
    public static function isMemcacheAvailable () {
6
        return class_exists('Memcache');
7
    }
8
9
    public static function isMemcachedAvailable () {
10
        return class_exists('Memcached');
11
    }
12
13
    public static function isModRewriteAvailable () {
14
    	if (function_exists("apache_get_modules")) {
15
    		if (in_array('mod_rewrite',apache_get_modules())) {
16
    			return true;
17
			}
18
19
			return false;
20
		}
21
22
		return false;
23
	}
24
25
	public static function startsWith ($haystack, $needle) : bool {
26
    	//https://stackoverflow.com/questions/834303/startswith-and-endswith-functions-in-php
27
28
		$length = strlen($needle);
29
		return (substr($haystack, 0, $length) === $needle);
30
	}
31
32
	public static function endsWith ($haystack, $needle) : bool {
33
		$length = strlen($needle);
34
35
		return $length === 0 || (substr($haystack, -$length) === $needle);
36
	}
37
38
	/**
39
	 * get IP address of client browser
40
	 *
41
	 * @return IPv4 / IPv6 address (up to 45 characters)
0 ignored issues
show
Bug introduced by
The type IPv4 was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
42
	 */
43
	public static function getClientIP () : string {
44
    	//https://stackoverflow.com/questions/3003145/how-to-get-the-client-ip-address-in-php
45
46
    	$ip = "";
47
48
		if (isset($_SERVER['HTTP_CLIENT_IP']) && !empty($_SERVER['HTTP_CLIENT_IP'])) {
49
			$ip = $_SERVER['HTTP_CLIENT_IP'];
50
		} elseif (isset($_SERVER['HTTP_X_FORWARDED_FOR']) && !empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
51
			$ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
52
		} else {
53
			$ip = $_SERVER['REMOTE_ADDR'];
54
		}
55
56
		return $ip;
57
	}
58
59
	public static function strEqs (string $str1, string $str2) : bool {
60
		return strcmp($str1, $str2) === 0;
61
	}
62
63
	/**
64
	 * Generate a random string, using a cryptographically secure
65
	 * pseudorandom number generator (random_int)
66
	 *
67
	 * For PHP 7, random_int is a PHP core function
68
	 * For PHP 5.x, depends on https://github.com/paragonie/random_compat
69
	 *
70
	 * @param int $length      How many characters do we want?
71
	 * @param string $keyspace A string of all possible characters
72
	 *                         to select from
73
	 *
74
	 * @link https://stackoverflow.com/questions/4356289/php-random-string-generator/31107425#31107425
75
	 *
76
	 * @return string
77
	 */
78
	public static function randomString(int $length, string $keyspace = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ') : string {
79
		$str = '';
80
		$max = mb_strlen($keyspace, '8bit') - 1;
81
82
		for ($i = 0; $i < $length; ++$i) {
83
			$str .= $keyspace[random_int(0, $max)];
84
		}
85
86
		return $str;
87
	}
88
89
	public static function getHostname () : string {
90
		if (function_exists("gethostname")) {
91
			return gethostname();
92
		} else {
93
			//Or, an option that also works before PHP 5.3
94
			return php_uname('n');
95
		}
96
	}
97
98
	public static function sendPOSTRequest (string $url, array $data = array()) {
99
		//check, if allow_url_fopen is enabled
100
		if (PHPUtils::isUrlfopenEnabled()) {
101
			// use key 'http' even if you send the request to https://...
102
			$options = array(
103
				'http' => array(
104
					'header'  => "Content-type: application/x-www-form-urlencoded\r\n",
105
					'method'  => 'POST',
106
					'content' => http_build_query($data)
107
				)
108
			);
109
			$context  = stream_context_create($options);
110
			$result = file_get_contents($url, false, $context);
111
112
			if ($result === FALSE) {
113
				return false;
114
			}
115
116
			return $result;
117
		} else {
118
			//try to use curl instead
119
120
			//https://stackoverflow.com/questions/2138527/php-curl-http-post-sample-code?utm_medium=organic&utm_source=google_rich_qa&utm_campaign=google_rich_qa
121
122
			//create a new curl session
123
			$ch = curl_init();
124
125
			curl_setopt($ch, CURLOPT_URL, $url);
126
			curl_setopt($ch, CURLOPT_POST, 1);
127
			curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));//"postvar1=value1&postvar2=value2&postvar3=value3"
128
129
			//receive server response
130
			curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
131
			$result = curl_exec ($ch);
132
133
			//close curl session
134
			curl_close ($ch);
135
136
			return $result;
137
		}
138
	}
139
140
	public static function isUrlfopenEnabled () : bool {
141
		$res = ini_get("allow_url_fopen");
142
143
		if ($res) {
144
			return true;
145
		} else {
146
			return false;
147
		}
148
	}
149
150
	public static function isCurlAvailable () : bool {
151
		/*if  (in_array  ('curl', get_loaded_extensions())) {
152
			return true;
153
		}
154
		else {
155
			return false;
156
		}*/
157
158
		return function_exists('curl_version');
159
	}
160
161
	public static function isGettextAvailable () : bool {
162
		return function_exists("gettext");
163
	}
164
165
	public static function clearGetTextCache () {
166
		//clear stats cache, often this clears also gettext cache
167
		clearstatcache();
168
	}
169
170
	public static function checkSessionStarted (bool $throw_exception = true) : bool {
171
		if (session_status() !== PHP_SESSION_ACTIVE) {
172
			if ($throw_exception) {
173
				throw new IllegalStateException("session wasnt started yet.");
174
			}
175
176
			return false;
177
		}
178
179
		return true;
180
	}
181
182
	public static function containsStr (string $haystack, string $needle) : bool {
183
		return strpos($haystack, $needle) !== FALSE;
184
	}
185
186
}
187