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