These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php |
||
2 | class holt45 { |
||
0 ignored issues
–
show
|
|||
3 | |||
4 | const DATA_URI_TRANSPARENT_GIF = 'data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7'; |
||
5 | const DATA_URI_TRANSPARENT_PNG = 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVQYV2NgYAAAAAMAAWgmWQ0AAAAASUVORK5CYII='; |
||
6 | |||
7 | |||
8 | /** |
||
9 | * Check $_GET |
||
10 | * |
||
11 | * @example if(chk_get("s") == "a") instead of if(isset($_GET["s"]) && $_GET["s"] == "a") |
||
12 | * |
||
13 | * @param string $key Get-key... |
||
14 | * @return bool |
||
15 | */ |
||
16 | public static function chk_get($key) { |
||
0 ignored issues
–
show
chk_get uses the super-global variable $_GET which is generally not recommended.
Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable: // Bad
class Router
{
public function generate($path)
{
return $_SERVER['HOST'].$path;
}
}
// Better
class Router
{
private $host;
public function __construct($host)
{
$this->host = $host;
}
public function generate($path)
{
return $this->host.$path;
}
}
class Controller
{
public function myAction(Request $request)
{
// Instead of
$page = isset($_GET['page']) ? intval($_GET['page']) : 1;
// Better (assuming you use the Symfony2 request)
$page = $request->query->get('page', 1);
}
}
![]() |
|||
17 | if (!isset($_GET[$key])) { |
||
18 | return false; |
||
19 | } |
||
20 | return $_GET[$key]; |
||
21 | } |
||
22 | |||
23 | /** |
||
24 | * Check $_POST |
||
25 | * |
||
26 | * @example if(chk_post("s") == "a") instead of if(isset($_POST["s"]) && $_POST["s"] == "a") |
||
27 | * |
||
28 | * @param string $key Post-key... |
||
29 | * @return bool |
||
30 | */ |
||
31 | public static function chk_post($key) { |
||
0 ignored issues
–
show
chk_post uses the super-global variable $_POST which is generally not recommended.
Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable: // Bad
class Router
{
public function generate($path)
{
return $_SERVER['HOST'].$path;
}
}
// Better
class Router
{
private $host;
public function __construct($host)
{
$this->host = $host;
}
public function generate($path)
{
return $this->host.$path;
}
}
class Controller
{
public function myAction(Request $request)
{
// Instead of
$page = isset($_GET['page']) ? intval($_GET['page']) : 1;
// Better (assuming you use the Symfony2 request)
$page = $request->query->get('page', 1);
}
}
![]() |
|||
32 | if (!isset($_POST[$key])) { |
||
33 | return false; |
||
34 | } |
||
35 | return $_POST[$key]; |
||
36 | } |
||
37 | |||
38 | /** |
||
39 | * Check multiple $_GET-keys |
||
40 | * |
||
41 | * @example if(chk_get_all(array("a","b"))) instead of if(!empty($_GET["a"]) && !empty($_GET["b"])) |
||
42 | * |
||
43 | * @param array $keys |
||
44 | * @return bool |
||
45 | */ |
||
46 | View Code Duplication | public static function chk_get_all($keys) { |
|
0 ignored issues
–
show
chk_get_all uses the super-global variable $_GET which is generally not recommended.
Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable: // Bad
class Router
{
public function generate($path)
{
return $_SERVER['HOST'].$path;
}
}
// Better
class Router
{
private $host;
public function __construct($host)
{
$this->host = $host;
}
public function generate($path)
{
return $this->host.$path;
}
}
class Controller
{
public function myAction(Request $request)
{
// Instead of
$page = isset($_GET['page']) ? intval($_GET['page']) : 1;
// Better (assuming you use the Symfony2 request)
$page = $request->query->get('page', 1);
}
}
![]() This method seems to be duplicated in 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. ![]() |
|||
47 | $s = true; |
||
48 | |||
49 | foreach($keys AS $key) { |
||
50 | |||
51 | if (empty($_GET[$key])) { |
||
52 | $s = false; |
||
53 | } |
||
54 | } |
||
55 | return $s; |
||
56 | } |
||
57 | |||
58 | /** |
||
59 | * Check multiple $_POST-keys |
||
60 | * |
||
61 | * @example if(chk_post_all(array("a","b"))) instead of if(!empty($_POST["a"]) && !empty($_POST["b"])) |
||
62 | * |
||
63 | * @param array $keys |
||
64 | * @return bool |
||
65 | */ |
||
66 | View Code Duplication | public static function chk_post_all($keys) { |
|
0 ignored issues
–
show
chk_post_all uses the super-global variable $_POST which is generally not recommended.
Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable: // Bad
class Router
{
public function generate($path)
{
return $_SERVER['HOST'].$path;
}
}
// Better
class Router
{
private $host;
public function __construct($host)
{
$this->host = $host;
}
public function generate($path)
{
return $this->host.$path;
}
}
class Controller
{
public function myAction(Request $request)
{
// Instead of
$page = isset($_GET['page']) ? intval($_GET['page']) : 1;
// Better (assuming you use the Symfony2 request)
$page = $request->query->get('page', 1);
}
}
![]() This method seems to be duplicated in 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. ![]() |
|||
67 | $s = true; |
||
68 | |||
69 | foreach($keys AS $key) { |
||
70 | |||
71 | if (empty($_POST[$key])) { |
||
72 | $s = false; |
||
73 | } |
||
74 | } |
||
75 | return $s; |
||
76 | } |
||
77 | |||
78 | /** |
||
79 | * Convert timestamp to HTTP-date (RFC2616) |
||
80 | * |
||
81 | * For use in "Last-Modified" headers. |
||
82 | * |
||
83 | * @param string $timestamp |
||
84 | * @return string |
||
0 ignored issues
–
show
|
|||
85 | */ |
||
86 | public static function timestamp_to_http_date($timestamp) { |
||
87 | if($timestamp == NULL) { return NULL; } |
||
88 | return gmdate("D, d M Y H:i:s T", strtotime($timestamp)); |
||
89 | } |
||
90 | |||
91 | /** |
||
92 | * Get client ip-address |
||
93 | * |
||
94 | * @return string User ip-address |
||
95 | */ |
||
96 | public static function get_client_ip_address() { |
||
97 | |||
98 | if (getenv('HTTP_CLIENT_IP')) |
||
99 | return getenv('HTTP_CLIENT_IP'); |
||
100 | else if(getenv('HTTP_X_FORWARDED_FOR')) |
||
101 | return getenv('HTTP_X_FORWARDED_FOR'); |
||
102 | else if(getenv('HTTP_X_FORWARDED')) |
||
103 | return getenv('HTTP_X_FORWARDED'); |
||
104 | else if(getenv('HTTP_FORWARDED_FOR')) |
||
105 | return getenv('HTTP_FORWARDED_FOR'); |
||
106 | else if(getenv('HTTP_FORWARDED')) |
||
107 | return getenv('HTTP_FORWARDED'); |
||
108 | else if(getenv('REMOTE_ADDR')) |
||
109 | return getenv('REMOTE_ADDR'); |
||
110 | else |
||
111 | return '127.0.0.1'; // Unknown IP |
||
112 | } |
||
113 | |||
114 | /** |
||
115 | * parse url, try to correct errors and return valid url + display-url. |
||
116 | * |
||
117 | * @example http:/wwww.example.com/lorum.html => http://www.example.com/lorum.html |
||
118 | * @example gopher:/ww.example.com => gopher://www.example.com |
||
119 | * @example http:/www3.example.com/?q=asd&f=#asd =>http://www3.example.com/?q=asd&f=#asd |
||
120 | * @example asd://.example.com/folder/folder/ =>http://example.com/folder/folder/ |
||
121 | * @example .example.com/ => http://example.com/ |
||
122 | * @example example.com =>http://example.com |
||
123 | * @example subdomain.example.com => http://subdomain.example.com |
||
124 | * |
||
125 | * @param string $url Any somewhat valid url. |
||
126 | * @return string[] "url" contains an auto-corrected url. "url_display" host.tld or subdomain.host.tld |
||
0 ignored issues
–
show
|
|||
127 | */ |
||
128 | public static function url_parser($url) { |
||
129 | |||
130 | // multiple /// messes up parse_url, replace 3 or more with 2 |
||
131 | $url = preg_replace('/(\/{2,})/','//',$url); |
||
132 | |||
133 | $parse_url = parse_url($url); |
||
134 | |||
135 | if(empty($parse_url["scheme"])) { |
||
136 | $parse_url["scheme"] = "http"; |
||
137 | } |
||
138 | if(empty($parse_url["host"]) && !empty($parse_url["path"])) { |
||
139 | // Strip slash from the beginning of path |
||
140 | $parse_url["host"] = ltrim($parse_url["path"], '\/'); |
||
141 | $parse_url["path"] = ""; |
||
142 | } |
||
143 | |||
144 | $url_array = array("url" => "", "url_display" => ""); |
||
145 | |||
146 | // Check if scheme is correct |
||
147 | if(!in_array($parse_url["scheme"], array("http", "https", "gopher"))) { |
||
148 | $url_array["url"] .= 'http'.'://'; |
||
149 | } else { |
||
150 | $url_array["url"] .= $parse_url["scheme"].'://'; |
||
151 | } |
||
152 | |||
153 | // Check if the right amount of "www" is set. |
||
154 | $explode_host = explode(".", $parse_url["host"]); |
||
155 | |||
156 | // Remove empty entries |
||
157 | $explode_host = array_filter($explode_host); |
||
158 | // And reassign indexes |
||
159 | $explode_host = array_values($explode_host); |
||
160 | |||
161 | // Contains subdomain |
||
162 | if(count($explode_host) > 2) { |
||
163 | // Check if subdomain only contains the letter w(then not any other subdomain). |
||
164 | if(substr_count($explode_host[0], 'w') == strlen($explode_host[0])) { |
||
165 | // Replace with "www" to avoid "ww" or "wwww", etc. |
||
166 | $explode_host[0] = "www"; |
||
167 | |||
168 | } |
||
169 | } |
||
170 | |||
171 | $url_array["url"] .= implode(".",$explode_host); |
||
172 | $url_array["url_display"] = trim(implode(".",$explode_host), '\/'); // Removes trailing slash |
||
173 | |||
174 | if(!empty($parse_url["port"])) { |
||
175 | $url_array["url"] .= ":".$parse_url["port"]; |
||
176 | } |
||
177 | if(!empty($parse_url["path"])) { |
||
178 | $url_array["url"] .= $parse_url["path"]; |
||
179 | } |
||
180 | if(!empty($parse_url["query"])) { |
||
181 | $url_array["url"] .= '?'.$parse_url["query"]; |
||
182 | } |
||
183 | if(!empty($parse_url["fragment"])) { |
||
184 | $url_array["url"] .= '#'.$parse_url["fragment"]; |
||
185 | } |
||
186 | |||
187 | |||
188 | return $url_array; |
||
189 | } |
||
190 | |||
191 | /** |
||
192 | * Generate a password-suggestion. |
||
193 | * |
||
194 | * @param int $length Length of password |
||
195 | * @param bool $simple Limit character-set to first 33 characters. |
||
196 | * @return string |
||
197 | */ |
||
198 | public static function generate_password($length = 8, $simple = false) { |
||
199 | $character_set = "23456789abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNPRSTUVWXYZ!#%+:=?@"; |
||
200 | $character_set_lenght = (($simple) ? 33 : 64); |
||
201 | |||
202 | $i = 0; |
||
203 | |||
204 | while($i < 10) { |
||
205 | |||
206 | $suggested_password = ""; |
||
207 | |||
208 | for($i = 0; $i < $length; $i++) { |
||
209 | $suggested_password .= $character_set[rand(0,($character_set_lenght-1))]; |
||
210 | } |
||
211 | |||
212 | if(strlen(count_chars($suggested_password, 3)) > ($length-2)) { |
||
213 | break; |
||
214 | } |
||
215 | } |
||
216 | |||
217 | return $suggested_password; |
||
0 ignored issues
–
show
The variable
$suggested_password does not seem to be defined for all execution paths leading up to this point.
If you define a variable conditionally, it can happen that it is not defined for all execution paths. Let’s take a look at an example: function myFunction($a) {
switch ($a) {
case 'foo':
$x = 1;
break;
case 'bar':
$x = 2;
break;
}
// $x is potentially undefined here.
echo $x;
}
In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined. Available Fixes
![]() |
|||
218 | |||
219 | } |
||
220 | |||
221 | /** |
||
222 | * Convert <textarea> to [textarea]. |
||
223 | * |
||
224 | * @param string $html |
||
225 | * @return string |
||
226 | */ |
||
227 | public static function textarea_encode($html) { |
||
228 | return preg_replace("/<textarea(.*?)>(.*?)<\/textarea>/is", "[textarea$1]$2[/textarea]", $html); |
||
229 | } |
||
230 | |||
231 | /** |
||
232 | * Convert [textarea] to <textarea>. |
||
233 | * |
||
234 | * @param string $html |
||
235 | * @return string |
||
236 | */ |
||
237 | public static function textarea_decode($html) { |
||
238 | return preg_replace("/\[textarea(.*?)\](.*?)\[\/textarea\]/is", "<textarea$1>$2</textarea>", $html); |
||
239 | } |
||
240 | |||
241 | /** |
||
242 | * Convert timestamp to "x unit" |
||
243 | * |
||
244 | * @param string $timestamp |
||
245 | * @return string |
||
246 | */ |
||
247 | public static function time_elapsed($timestamp) { |
||
248 | $seconds = max((time() - strtotime($timestamp)),0); |
||
249 | |||
250 | if($seconds < 60) { |
||
251 | $number = $seconds; |
||
252 | $text = "second"; |
||
253 | } elseif($seconds < (60 * 60)) { |
||
254 | $number = $seconds / 60; |
||
255 | $text = "minute"; |
||
256 | } elseif($seconds < (60 * 60 * 24)) { |
||
257 | $number = $seconds / (60 * 60); |
||
258 | $text = "hour"; |
||
259 | } else { |
||
260 | $number = $seconds / (60 * 60 * 24); |
||
261 | $text = "day"; |
||
262 | } |
||
263 | |||
264 | $number = floor($number); |
||
265 | |||
266 | if($number > 1) { |
||
267 | $text.="s"; |
||
268 | } |
||
269 | |||
270 | return "$number $text"; |
||
271 | } |
||
272 | } |
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.