GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Branch master (124116)
by w3l
01:37
created

Misc::generatePassword()   B

Complexity

Conditions 5
Paths 10

Size

Total Lines 25
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 25
rs 8.439
cc 5
eloc 13
nc 10
nop 2
1
<?php
2
namespace w3l\Holt45;
3
trait Misc {
4
	
5
	/**
6
	 * Convert ISO 3166-1 alpha-2 code to (English) country name.
7
	 *
8
	 * @link https://gist.github.com/IngmarBoddington/5909709 Source
9
	 *
10
	 * @param string $key ISO 3166-1 alpha-2 code
11
	 * @return string Country name OR $key if no match.
12
	 */
13
	public static function iso3166ToName($key) {
14
	
15
		$countries = self::getCountriesList();
16
17
		return ((array_key_exists($key, $countries)) ? $countries[$key] : $key);
18
	}
19
		
20
	/**
21
	* To replace "Hallo [@var] world" with $value.
22
	*
23
	* @example replace_string($string, array("val1" => "foo", "val2" => "bar"))
24
	*
25
	* @param string $langString String containing placeholder.
26
	* @param array $dynamicContent key->value array.
27
	* @return string String with placeholder replaced.
28
	*/
29
	public static function replaceString($langString, $dynamicContent = array()) {
30
31
		foreach ($dynamicContent as $k => $v) {
32
			$langString = str_replace("[@".$k."]", $v, $langString);
33
		}
34
		return $langString;
35
	}
36
	
37
	/**
38
	 * Get client ip-address
39
	 *
40
	 * @return string User ip-address
41
	 */
42
	public static function getClientIpAddress() {
43
44
		if (getenv('HTTP_CLIENT_IP'))
45
			return getenv('HTTP_CLIENT_IP');
46
		else if(getenv('HTTP_X_FORWARDED_FOR'))
47
			return getenv('HTTP_X_FORWARDED_FOR');
48
		else if(getenv('HTTP_X_FORWARDED'))
49
			return getenv('HTTP_X_FORWARDED');
50
		else if(getenv('HTTP_FORWARDED_FOR'))
51
			return getenv('HTTP_FORWARDED_FOR');
52
		else if(getenv('HTTP_FORWARDED'))
53
			return getenv('HTTP_FORWARDED');
54
		else if(getenv('REMOTE_ADDR'))
55
			return getenv('REMOTE_ADDR');
56
57
			return '127.0.0.1'; // Unknown IP
58
	}
59
60
	/**
61
	 * Tries to auto-correct parse_url()-output.
62
	 *
63
	 * @param string $url
64
	 * @return string[]|false
65
	 */
0 ignored issues
show
Documentation introduced by
Should the return type not be array<string,string>|false?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
66
	
67
	private static function autoCorrectParseUrl($url) {
68
		// multiple /// messes up parse_url, replace 3 or more with 2
69
		$url = preg_replace('/(\/{2,})/','//',$url);
70
		
71
		$parseUrl = parse_url($url);
72
		
73
		if(empty($parseUrl["scheme"])) {
74
			$parseUrl["scheme"] = "http";
75
		}
76
		if(empty($parseUrl["host"]) && !empty($parseUrl["path"])) {
77
			// Strip slash from the beginning of path
78
			$parseUrl["host"] = ltrim($parseUrl["path"], '\/');
79
			$parseUrl["path"] = "";
80
		}
81
		return $parseUrl;
82
	}
83
84
	/**
85
	 * parse url, try to correct errors and return valid url + display-url.
86
	 *
87
	 * @example http:/wwww.example.com/lorum.html => http://www.example.com/lorum.html
88
	 * @example gopher:/ww.example.com => gopher://www.example.com
89
	 * @example http:/www3.example.com/?q=asd&f=#asd =>http://www3.example.com/?q=asd&f=#asd
90
	 * @example asd://.example.com/folder/folder/ =>http://example.com/folder/folder/
91
	 * @example .example.com/ => http://example.com/
92
	 * @example example.com =>http://example.com
93
	 * @example subdomain.example.com => http://subdomain.example.com
94
	 *
95
	 * @param string $url Any somewhat valid url.
96
	 * @return string[] "url" contains an auto-corrected url. "url_display" host.tld or subdomain.host.tld
0 ignored issues
show
Documentation introduced by
Should the return type not be array<string,string>?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
97
	 */
98
	public static function urlParser($url) {
99
		
100
		$parseUrl = self::autoCorrectParseUrl($url);
101
		
102
		// malformed URL, parse_url() returns false. Returns urls "as is".
103
		if($parseUrl === false) {
104
			return array("url" => $url, "url_display" => $url);
105
		}
106
		
107
		$urlArray = array("url" => "", "url_display" => "");
108
		
109
		// Check if scheme is correct
110
		if(!in_array($parseUrl["scheme"], array("http", "https", "gopher"))) {
111
			$urlArray["url"] .= 'http'.'://';
112
		} else {
113
			$urlArray["url"] .= $parseUrl["scheme"].'://';
114
		}
115
		
116
		// Check if the right amount of "www" is set.
117
		$explodeHost = explode(".", $parseUrl["host"]);
118
		
119
		// Remove empty entries
120
		$explodeHost = array_filter($explodeHost);
121
		// And reassign indexes
122
		$explodeHost = array_values($explodeHost);
123
		
124
		// Contains subdomain
125
		if(count($explodeHost) > 2) {
126
			// Check if subdomain only contains the letter w(then not any other subdomain).
127
			if(substr_count($explodeHost[0], 'w') == strlen($explodeHost[0])) {
128
				// Replace with "www" to avoid "ww" or "wwww", etc.
129
				$explodeHost[0] = "www";
130
				
131
			}
132
		}
133
134
		$urlArray["url"] .= implode(".",$explodeHost);
135
		$urlArray["url_display"] = trim(implode(".",$explodeHost), '\/'); // Removes trailing slash
136
		
137
		if(!empty($parseUrl["port"])) {
138
			$urlArray["url"] .= ":".$parseUrl["port"];
139
		}
140
		if(!empty($parseUrl["path"])) {
141
			$urlArray["url"] .= $parseUrl["path"];
142
		}
143
		if(!empty($parseUrl["query"])) {
144
			$urlArray["url"] .= '?'.$parseUrl["query"];
145
		}
146
		if(!empty($parseUrl["fragment"])) {
147
			$urlArray["url"] .= '#'.$parseUrl["fragment"];
148
		}
149
150
		
151
		return $urlArray;
152
	}
153
	
154
	/**
155
	 * Generate a password-suggestion.
156
	 *
157
	 * @param int $length Length of password
158
	 * @param string $passwordType "simple" limit character-set to first 33 characters. "long" uses 64 characters.
159
	 * @return string
160
	 */
161
	public static function generatePassword($length = 8, $passwordType = "long") {
162
		$characterSet = "23456789abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNPRSTUVWXYZ!#%+:=?@";
163
		$characterSetLenght = (($passwordType == "simple") ? 33 : 64);
164
		
165
		$counter = 0;
166
		$suggestedPassword = "";
167
		
168
		while($counter < 10) {
169
		
170
			$suggestedPassword = "";
171
			
172
			for($i = 0; $i < $length; $i++) {
173
				$suggestedPassword .= $characterSet[rand(0,($characterSetLenght-1))];
174
			}
175
176
			if(strlen(count_chars($suggestedPassword, 3)) > ($length-2)) {
177
				break;
178
			}
179
			
180
			$counter++;
181
		}
182
		
183
		return $suggestedPassword;
184
		
185
	}
186
	
187
	/**
188
	 * Obfuscate string (url-safe and somewhat hard to guess).
189
	 *
190
	 * @param string $input The text that should be obfuscated
191
	 * @return string Obfuscated string
192
	 */
193
	public static function obfuscateString($input) {
194
		return bin2hex(base64_encode(strrev($input)));
195
	}
196
197
	/**
198
	 * Deobfuscate string
199
	 *
200
	 * @param string $input Obfuscated string
201
	 * @return string Deobfuscated string
202
	 */
203
	public static function deobfuscateString($input) {
204
		return strrev(base64_decode(hex2bin($input)));
205
	}
206
	
207
	/**
208
	 * Convert <textarea> to [textarea].
209
	 *
210
	 * @param string $html
211
	 * @return string
212
	 */
213
	public static function textareaEncode($html) {
214
		return preg_replace("/<textarea(.*?)>(.*?)<\/textarea>/is", "[textarea$1]$2[/textarea]", $html);
215
	}
216
	
217
	/**
218
	 * Convert [textarea] to <textarea>.
219
	 *
220
	 * @param string $html
221
	 * @return string
222
	 */
223
	public static function textareaDecode($html) {
224
		return preg_replace("/\[textarea(.*?)\](.*?)\[\/textarea\]/is", "<textarea$1>$2</textarea>", $html);
225
	}
226
	
227
	/**
228
	 * Create range for pagination
229
	 *
230
	 * @param int $totalPages
231
	 * @param int $selectedPage
232
	 * @param int $numberOfResults
233
	 * @return array Array with all page-numbers limited by $number_of_results
234
	 */
235
	public static function generatePaginationRange($totalPages, $selectedPage = 1, $numberOfResults = 7) {
236
237
		// Get the numbers
238
		$tempArrayRange = range(1, $totalPages);
239
		
240
		if($totalPages <= $numberOfResults) {
241
			// all
242
			$arrayData = $tempArrayRange;
243 View Code Duplication
		} elseif($selectedPage <= (round(($numberOfResults / 2), 0, PHP_ROUND_HALF_UP))) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
244
			// 1-6+last
245
			$arrayData = array_slice($tempArrayRange, 0, ($numberOfResults-1));
246
			$arrayData[] = $totalPages;
247
			
248
		} elseif($selectedPage >= $totalPages-round(($numberOfResults / 2), 0, PHP_ROUND_HALF_DOWN)) {
249
			// first + $totalPages-5 - $totalPages
250
			$arrayData = array_slice($tempArrayRange, $totalPages-($numberOfResults-1));
251
			$arrayData[] = 1;
252 View Code Duplication
		} else {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
253
			// first + $totalPages-2 - $totalPages+2 + last
254
			$arrayData = array_slice(
255
									$tempArrayRange,
256
									$selectedPage-(round(($numberOfResults / 2), 0, PHP_ROUND_HALF_DOWN)), 
257
									($numberOfResults-2)
258
									);
259
			$arrayData[] = 1;
260
			$arrayData[] = $totalPages;
261
			
262
		}
263
		
264
		sort($arrayData);
265
		
266
		return $arrayData;
267
		
268
	}
269
270
	
271
}
272