1
|
|
|
<?php |
2
|
|
|
namespace Holt45; |
3
|
|
|
trait Misc { |
4
|
|
|
/** |
5
|
|
|
* Get client ip-address |
6
|
|
|
* |
7
|
|
|
* @return string User ip-address |
8
|
|
|
*/ |
9
|
|
|
public static function get_client_ip_address() { |
10
|
|
|
|
11
|
|
|
if (getenv('HTTP_CLIENT_IP')) |
12
|
|
|
return getenv('HTTP_CLIENT_IP'); |
13
|
|
|
else if(getenv('HTTP_X_FORWARDED_FOR')) |
14
|
|
|
return getenv('HTTP_X_FORWARDED_FOR'); |
15
|
|
|
else if(getenv('HTTP_X_FORWARDED')) |
16
|
|
|
return getenv('HTTP_X_FORWARDED'); |
17
|
|
|
else if(getenv('HTTP_FORWARDED_FOR')) |
18
|
|
|
return getenv('HTTP_FORWARDED_FOR'); |
19
|
|
|
else if(getenv('HTTP_FORWARDED')) |
20
|
|
|
return getenv('HTTP_FORWARDED'); |
21
|
|
|
else if(getenv('REMOTE_ADDR')) |
22
|
|
|
return getenv('REMOTE_ADDR'); |
23
|
|
|
else |
24
|
|
|
return '127.0.0.1'; // Unknown IP |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* parse url, try to correct errors and return valid url + display-url. |
29
|
|
|
* |
30
|
|
|
* @example http:/wwww.example.com/lorum.html => http://www.example.com/lorum.html |
31
|
|
|
* @example gopher:/ww.example.com => gopher://www.example.com |
32
|
|
|
* @example http:/www3.example.com/?q=asd&f=#asd =>http://www3.example.com/?q=asd&f=#asd |
33
|
|
|
* @example asd://.example.com/folder/folder/ =>http://example.com/folder/folder/ |
34
|
|
|
* @example .example.com/ => http://example.com/ |
35
|
|
|
* @example example.com =>http://example.com |
36
|
|
|
* @example subdomain.example.com => http://subdomain.example.com |
37
|
|
|
* |
38
|
|
|
* @param string $url Any somewhat valid url. |
39
|
|
|
* @return string[] "url" contains an auto-corrected url. "url_display" host.tld or subdomain.host.tld |
|
|
|
|
40
|
|
|
*/ |
41
|
|
|
public static function url_parser($url) { |
42
|
|
|
|
43
|
|
|
// multiple /// messes up parse_url, replace 3 or more with 2 |
44
|
|
|
$url = preg_replace('/(\/{2,})/','//',$url); |
45
|
|
|
|
46
|
|
|
$parse_url = parse_url($url); |
47
|
|
|
|
48
|
|
|
if(empty($parse_url["scheme"])) { |
49
|
|
|
$parse_url["scheme"] = "http"; |
50
|
|
|
} |
51
|
|
|
if(empty($parse_url["host"]) && !empty($parse_url["path"])) { |
52
|
|
|
// Strip slash from the beginning of path |
53
|
|
|
$parse_url["host"] = ltrim($parse_url["path"], '\/'); |
54
|
|
|
$parse_url["path"] = ""; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
$url_array = array("url" => "", "url_display" => ""); |
58
|
|
|
|
59
|
|
|
// Check if scheme is correct |
60
|
|
|
if(!in_array($parse_url["scheme"], array("http", "https", "gopher"))) { |
61
|
|
|
$url_array["url"] .= 'http'.'://'; |
62
|
|
|
} else { |
63
|
|
|
$url_array["url"] .= $parse_url["scheme"].'://'; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
// Check if the right amount of "www" is set. |
67
|
|
|
$explode_host = explode(".", $parse_url["host"]); |
68
|
|
|
|
69
|
|
|
// Remove empty entries |
70
|
|
|
$explode_host = array_filter($explode_host); |
71
|
|
|
// And reassign indexes |
72
|
|
|
$explode_host = array_values($explode_host); |
73
|
|
|
|
74
|
|
|
// Contains subdomain |
75
|
|
|
if(count($explode_host) > 2) { |
76
|
|
|
// Check if subdomain only contains the letter w(then not any other subdomain). |
77
|
|
|
if(substr_count($explode_host[0], 'w') == strlen($explode_host[0])) { |
78
|
|
|
// Replace with "www" to avoid "ww" or "wwww", etc. |
79
|
|
|
$explode_host[0] = "www"; |
80
|
|
|
|
81
|
|
|
} |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
$url_array["url"] .= implode(".",$explode_host); |
|
|
|
|
85
|
|
|
$url_array["url_display"] = trim(implode(".",$explode_host), '\/'); // Removes trailing slash |
86
|
|
|
|
87
|
|
|
if(!empty($parse_url["port"])) { |
88
|
|
|
$url_array["url"] .= ":".$parse_url["port"]; |
89
|
|
|
} |
90
|
|
|
if(!empty($parse_url["path"])) { |
91
|
|
|
$url_array["url"] .= $parse_url["path"]; |
92
|
|
|
} |
93
|
|
|
if(!empty($parse_url["query"])) { |
94
|
|
|
$url_array["url"] .= '?'.$parse_url["query"]; |
95
|
|
|
} |
96
|
|
|
if(!empty($parse_url["fragment"])) { |
97
|
|
|
$url_array["url"] .= '#'.$parse_url["fragment"]; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
|
101
|
|
|
return $url_array; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* Generate a password-suggestion. |
106
|
|
|
* |
107
|
|
|
* @param int $length Length of password |
108
|
|
|
* @param bool $simple Limit character-set to first 33 characters. |
109
|
|
|
* @return string |
110
|
|
|
*/ |
111
|
|
|
public static function generate_password($length = 8, $simple = false) { |
112
|
|
|
$character_set = "23456789abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNPRSTUVWXYZ!#%+:=?@"; |
|
|
|
|
113
|
|
|
$character_set_lenght = (($simple) ? 33 : 64); |
114
|
|
|
|
115
|
|
|
$i = 0; |
116
|
|
|
|
117
|
|
|
while($i < 10) { |
118
|
|
|
|
119
|
|
|
$suggested_password = ""; |
120
|
|
|
|
121
|
|
|
for($i = 0; $i < $length; $i++) { |
122
|
|
|
$suggested_password .= $character_set[rand(0,($character_set_lenght-1))]; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
if(strlen(count_chars($suggested_password, 3)) > ($length-2)) { |
126
|
|
|
break; |
127
|
|
|
} |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
return $suggested_password; |
|
|
|
|
131
|
|
|
|
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* Convert <textarea> to [textarea]. |
136
|
|
|
* |
137
|
|
|
* @param string $html |
138
|
|
|
* @return string |
139
|
|
|
*/ |
140
|
|
|
public static function textarea_encode($html) { |
141
|
|
|
return preg_replace("/<textarea(.*?)>(.*?)<\/textarea>/is", "[textarea$1]$2[/textarea]", $html); |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* Convert [textarea] to <textarea>. |
146
|
|
|
* |
147
|
|
|
* @param string $html |
148
|
|
|
* @return string |
149
|
|
|
*/ |
150
|
|
|
public static function textarea_decode($html) { |
151
|
|
|
return preg_replace("/\[textarea(.*?)\](.*?)\[\/textarea\]/is", "<textarea$1>$2</textarea>", $html); |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
} |
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.