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
|
|
|
* Get client ip-address |
22
|
|
|
* |
23
|
|
|
* @return string User ip-address |
24
|
|
|
*/ |
25
|
|
|
public static function getClientIpAddress() { |
26
|
|
|
|
27
|
|
|
if (getenv('HTTP_CLIENT_IP')) |
28
|
|
|
return getenv('HTTP_CLIENT_IP'); |
29
|
|
|
else if(getenv('HTTP_X_FORWARDED_FOR')) |
30
|
|
|
return getenv('HTTP_X_FORWARDED_FOR'); |
31
|
|
|
else if(getenv('HTTP_X_FORWARDED')) |
32
|
|
|
return getenv('HTTP_X_FORWARDED'); |
33
|
|
|
else if(getenv('HTTP_FORWARDED_FOR')) |
34
|
|
|
return getenv('HTTP_FORWARDED_FOR'); |
35
|
|
|
else if(getenv('HTTP_FORWARDED')) |
36
|
|
|
return getenv('HTTP_FORWARDED'); |
37
|
|
|
else if(getenv('REMOTE_ADDR')) |
38
|
|
|
return getenv('REMOTE_ADDR'); |
39
|
|
|
|
40
|
|
|
return '127.0.0.1'; // Unknown IP |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Tries to auto-correct parse_url()-output. |
45
|
|
|
* |
46
|
|
|
* @param string $url |
47
|
|
|
* @return string[]|false |
48
|
|
|
*/ |
|
|
|
|
49
|
|
|
|
50
|
|
|
private static function autoCorrectParseUrl($url) { |
51
|
|
|
// multiple /// messes up parse_url, replace 3 or more with 2 |
52
|
|
|
$url = preg_replace('/(\/{2,})/','//',$url); |
53
|
|
|
|
54
|
|
|
$parseUrl = parse_url($url); |
55
|
|
|
|
56
|
|
|
if(empty($parseUrl["scheme"])) { |
57
|
|
|
$parseUrl["scheme"] = "http"; |
58
|
|
|
} |
59
|
|
|
if(empty($parseUrl["host"]) && !empty($parseUrl["path"])) { |
60
|
|
|
// Strip slash from the beginning of path |
61
|
|
|
$parseUrl["host"] = ltrim($parseUrl["path"], '\/'); |
62
|
|
|
$parseUrl["path"] = ""; |
63
|
|
|
} |
64
|
|
|
return $parseUrl; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* parse url, try to correct errors and return valid url + display-url. |
69
|
|
|
* |
70
|
|
|
* @example http:/wwww.example.com/lorum.html => http://www.example.com/lorum.html |
71
|
|
|
* @example gopher:/ww.example.com => gopher://www.example.com |
72
|
|
|
* @example http:/www3.example.com/?q=asd&f=#asd =>http://www3.example.com/?q=asd&f=#asd |
73
|
|
|
* @example asd://.example.com/folder/folder/ =>http://example.com/folder/folder/ |
74
|
|
|
* @example .example.com/ => http://example.com/ |
75
|
|
|
* @example example.com =>http://example.com |
76
|
|
|
* @example subdomain.example.com => http://subdomain.example.com |
77
|
|
|
* |
78
|
|
|
* @param string $url Any somewhat valid url. |
79
|
|
|
* @return string[] "url" contains an auto-corrected url. "url_display" host.tld or subdomain.host.tld |
|
|
|
|
80
|
|
|
*/ |
81
|
|
|
public static function urlParser($url) { |
82
|
|
|
|
83
|
|
|
$parseUrl = self::autoCorrectParseUrl($url); |
84
|
|
|
|
85
|
|
|
// malformed URL, parse_url() returns false. Returns urls "as is". |
86
|
|
|
if($parseUrl === false) { |
87
|
|
|
return array("url" => $url, "url_display" => $url); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
$urlArray = array("url" => "", "url_display" => ""); |
91
|
|
|
|
92
|
|
|
// Check if scheme is correct |
93
|
|
|
if(!in_array($parseUrl["scheme"], array("http", "https", "gopher"))) { |
94
|
|
|
$urlArray["url"] .= 'http'.'://'; |
95
|
|
|
} else { |
96
|
|
|
$urlArray["url"] .= $parseUrl["scheme"].'://'; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
// Check if the right amount of "www" is set. |
100
|
|
|
$explodeHost = explode(".", $parseUrl["host"]); |
101
|
|
|
|
102
|
|
|
// Remove empty entries |
103
|
|
|
$explodeHost = array_filter($explodeHost); |
104
|
|
|
// And reassign indexes |
105
|
|
|
$explodeHost = array_values($explodeHost); |
106
|
|
|
|
107
|
|
|
// Contains subdomain |
108
|
|
|
if(count($explodeHost) > 2) { |
109
|
|
|
// Check if subdomain only contains the letter w(then not any other subdomain). |
110
|
|
|
if(substr_count($explodeHost[0], 'w') == strlen($explodeHost[0])) { |
111
|
|
|
// Replace with "www" to avoid "ww" or "wwww", etc. |
112
|
|
|
$explodeHost[0] = "www"; |
113
|
|
|
|
114
|
|
|
} |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
$urlArray["url"] .= implode(".",$explodeHost); |
118
|
|
|
$urlArray["url_display"] = trim(implode(".",$explodeHost), '\/'); // Removes trailing slash |
119
|
|
|
|
120
|
|
|
if(!empty($parseUrl["port"])) { |
121
|
|
|
$urlArray["url"] .= ":".$parseUrl["port"]; |
122
|
|
|
} |
123
|
|
|
if(!empty($parseUrl["path"])) { |
124
|
|
|
$urlArray["url"] .= $parseUrl["path"]; |
125
|
|
|
} |
126
|
|
|
if(!empty($parseUrl["query"])) { |
127
|
|
|
$urlArray["url"] .= '?'.$parseUrl["query"]; |
128
|
|
|
} |
129
|
|
|
if(!empty($parseUrl["fragment"])) { |
130
|
|
|
$urlArray["url"] .= '#'.$parseUrl["fragment"]; |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
|
134
|
|
|
return $urlArray; |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* Generate a password-suggestion. |
139
|
|
|
* |
140
|
|
|
* @param int $length Length of password |
141
|
|
|
* @param string $passwordType "simple" limit character-set to first 33 characters. "long" uses 64 characters. |
142
|
|
|
* @return string |
143
|
|
|
*/ |
144
|
|
|
public static function generatePassword($length = 8, $passwordType = "long") { |
145
|
|
|
$characterSet = "23456789abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNPRSTUVWXYZ!#%+:=?@"; |
146
|
|
|
$characterSetLenght = (($passwordType == "simple") ? 33 : 64); |
147
|
|
|
|
148
|
|
|
$counter = 0; |
149
|
|
|
$suggestedPassword = ""; |
150
|
|
|
|
151
|
|
|
while($counter < 10) { |
152
|
|
|
|
153
|
|
|
$suggestedPassword = ""; |
154
|
|
|
|
155
|
|
|
for($i = 0; $i < $length; $i++) { |
156
|
|
|
$suggestedPassword .= $characterSet[rand(0,($characterSetLenght-1))]; |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
if(strlen(count_chars($suggestedPassword, 3)) > ($length-2)) { |
160
|
|
|
break; |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
$counter++; |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
return $suggestedPassword; |
167
|
|
|
|
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
} |
171
|
|
|
|
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.