PluginProbe
SEO Redirection Plugin – 301 Redirect Manager / trunk
SEO Redirection Plugin – 301 Redirect Manager vtrunk
9.19 trunk 4.17 7.1 7.2 7.3 7.4 7.5 7.6 7.7 7.8 7.9 8.1 8.2 8.3 8.4 8.5 8.6 8.7 8.8 8.9 9.1 9.10 9.11 9.12 All 39 releases
seo-redirection / common / util.php

util.php in SEO Redirection Plugin – 301 Redirect Manager trunk, at common/util.php

646 lines 14.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 if(!class_exists('clogica_util_1')){
4 class clogica_util_1{
5
6 private $slug;
7 private $plugin_file;
8 private $plugin_path;
9 private $plugin_url;
10
11 private $option_group_name='clogica_option_group';
12 private $plugin_folder='plugin_folder_name';
13
14
15
16 public function init ($option_gruop='clogica_option_group',$plugin_file='')
17 {
18 $this->set_option_gruop($option_gruop);
19 $this->set_plugin_folder(basename(dirname($plugin_file)));
20 $this->plugin_file = $plugin_file;
21 $this->slug = basename($plugin_file);
22 $this->plugin_path = dirname($plugin_file) . '/';
23 $this->plugin_url =plugin_dir_url($plugin_file);
24
25 }
26 public function get($key,$type='text')
27 {
28
29 if(array_key_exists($key,$_GET))
30 {
31 $unsafe_val=$_GET[$key];
32 return $this->sanitize_req($unsafe_val,$type);
33 }
34 else
35 {
36 return '';
37 }
38 }
39
40 //----------------------------------------------------
41
42 public function post($key,$type='text')
43 {
44 if(array_key_exists($key,$_POST))
45 {
46 $unsafe_val=$_POST[$key];
47 return $this->sanitize_req($unsafe_val,$type);
48 }
49 else
50 {
51 return '';
52 }
53 }
54
55
56 //----------------------------------------------------
57
58 public function sanitize_req($unsafe_val,$type='text')
59 {
60 switch ($type) {
61 case 'text':
62 return htmlspecialchars($unsafe_val, ENT_QUOTES, 'UTF-8', false);
63 break;
64
65 case 'int': return intval($unsafe_val);
66 break;
67
68 case 'email': return filter_var($unsafe_val, FILTER_SANITIZE_EMAIL);//sanitize_email($unsafe_val);
69 break;
70
71 case 'filename': return sanitize_file_name($unsafe_val);
72 break;
73
74 case 'title':
75 return htmlspecialchars($unsafe_val, ENT_QUOTES, 'UTF-8', false);
76 break;
77
78 case 'url' : return filter_var($unsafe_val, FILTER_SANITIZE_SPECIAL_CHARS);//sanitize_title($unsafe_val);
79 break;
80
81 default:
82 return htmlspecialchars($unsafe_val, ENT_QUOTES, 'UTF-8', false);
83
84
85 // filter_var($var, FILTER_SANITIZE_URL)
86 // FILTER_SANITIZE_STRING
87 // FILTER_SANITIZE_EMAIL
88 // FILTER_SANITIZE_NUMBER_INT
89
90 }
91 }
92
93 //----------------------------------------------------
94
95 public function get_ref()
96 {
97 if(array_key_exists('HTTP_REFERER',$_SERVER))
98 {
99 return $this->sanitize_req(strip_tags($_SERVER['HTTP_REFERER']),"url");
100 }
101 else
102 {
103 return '';
104 }
105 }
106
107 //----------------------------------------------------
108
109 public function set_option_gruop($option_group_name)
110 {
111 $this->option_group_name=$option_group_name;
112 }
113
114 //----------------------------------------------------
115
116 public function get_option_gruop()
117 {
118 return $this->option_group_name;
119 }
120
121 //----------------------------------------------------
122
123 public function set_plugin_folder($folder)
124 {
125 $this->plugin_folder=$folder;
126 }
127
128
129 //----------------------------------------------------
130
131 public function get_plugin_folder()
132 {
133 return $this->plugin_folder;
134 }
135
136 //----------------------------------------------------
137
138
139 public function update_my_options($options)
140 {
141 update_option($this->get_option_gruop(),$options);
142 }
143
144 //----------------------------------------------------
145
146 public function get_my_options()
147 {
148 $options=get_option($this->get_option_gruop());
149 if(!is_array($options))
150 {
151 add_option($this->get_option_gruop());
152 $options= array();
153 }
154 return $options;
155 }
156
157 //---------------------------------------------------
158
159 public function get_option_value($key)
160 {
161 $options=$this->get_my_options();
162 if(array_key_exists($key,$options))
163 {
164 return $options[$key];
165 }else
166 {
167 return '';
168 }
169 }
170 //----------------------------------------------------
171
172 public function update_option($key,$value)
173 {
174 $options=$this->get_my_options();
175 $options[$key]=$value;
176 $this->update_my_options($options);
177 }
178
179 //----------------------------------------------------
180
181 public function update_post_option($key)
182 {
183 $options=$this->get_my_options();
184 $options[$key]=intval($this->post($key));
185 $this->update_my_options($options);
186 }
187
188
189 //----------------------------------------------------
190
191
192 public function delete_my_options()
193 {
194 delete_option($this->get_option_gruop());
195 }
196
197
198
199 /* get_current_URL ---------------------------------------------- */
200 public function get_current_URL()
201 {
202 $pageURL = 'http';
203 if ( array_key_exists("HTTPS",$_SERVER) && $_SERVER["HTTPS"] == "on")
204 {
205 $pageURL .= "s";
206 }
207 $pageURL .= "://";
208
209
210
211
212 $pageURL .= ($_SERVER["HTTP_HOST"].$_SERVER["REQUEST_URI"]);
213 return $this->sanitize_req($pageURL,"url");
214 }
215
216 //-----------------------------------------------------
217
218 public function remove_url_http_www($url)
219 {
220 $url = str_ireplace("http://www.",'',$url);
221 $url = str_ireplace("https://www.",'',$url);
222 $url = str_ireplace("http://",'',$url);
223 $url = str_ireplace("https://",'',$url);
224 return ($url);
225 }
226 //-----------------------------------------------------
227 public function make_relative_url($url)
228 {
229
230 if($url=="")
231 {
232 return "";
233 }
234 $site_url = $this->remove_url_http_www(site_url());
235
236 if(stripos($url,$site_url) !==false)
237 {
238 $url_no_www = $this->remove_url_http_www($url);
239 if(strtolower(substr($url_no_www,0,strlen($site_url))) == strtolower($site_url))
240 {
241 $url = str_ireplace($site_url,'',$url_no_www);
242 }
243 }
244 if($url=="")
245 {
246 $url="/";
247 }
248 return esc_url_raw($url);
249 }
250
251 //----------------------------------------------------
252 public function make_absolute_url($url)
253 {
254 if(substr($url,0,1)=='/')
255 {
256 $url = site_url() . $url;
257 }
258 return esc_url_raw($url);
259 }
260
261 //----------------------------------------------------
262
263 public function get_current_relative_url()
264 {
265 return $this->make_relative_url($this->get_current_URL());
266 }
267 //----------------------------------------------------
268 public function is_valid_url($url)
269 {
270 if(stripos($url,'://')!== false || substr($url,0, 1)=='/')
271 {
272 return true;
273 }else{
274 return false;
275 }
276 }
277 //----------------------------------------------------
278
279 public function WPSR_get_current_parameters($remove_parameter="")
280 {
281
282 if($_SERVER['QUERY_STRING']!='')
283 {
284 $qry = '?' . urldecode(sanitize_text_field($_SERVER['QUERY_STRING']));
285
286 if(is_array($remove_parameter))
287 {
288 for($i=0;$i<count($remove_parameter);$i++)
289 {
290 if(array_key_exists($remove_parameter[$i],$_GET)){
291 $string_remove = '&' . $remove_parameter[$i] . "=" . $this->get($remove_parameter[$i]);
292 $qry=str_replace($string_remove,"",$qry);
293 $string_remove = '?' . $remove_parameter[$i] . "=" . $this->get($remove_parameter[$i]);
294 $qry=str_replace($string_remove,"",$qry);
295 }
296 }
297
298 }else{
299 if($remove_parameter!='')
300 {
301 if(array_key_exists($remove_parameter,$_GET)){
302 $string_remove = '&' . $remove_parameter . "=" . $this->get($remove_parameter);
303 $qry=str_replace($string_remove,"",$qry);
304 $string_remove = '?' . $remove_parameter . "=" . $this->get($remove_parameter);
305 $qry=str_replace($string_remove,"",$qry);
306 }
307 }
308 }
309
310 return strip_tags($this->sanitize_req($qry,"url"));
311 }else
312 {
313 return "";
314 }
315 }
316
317
318 //---------------------------------------------------------------
319
320 public function get_plugin_path($folder='')
321 {
322 return WP_PLUGIN_DIR . '/' . $this->get_plugin_folder() . '/' . $folder;
323 }
324
325 /* get_plugin_path ---------------------------------------------
326 public function get_plugin_path()
327 {
328 return $this->plugin_path;
329 }*/
330
331 /* get plugin slug -------------------------------------------- */
332 public function get_plugin_slug()
333 {
334 return $this->slug;
335 }
336
337 /* get plugin slug -------------------------------------------- */
338 public function get_plugin_file()
339 {
340 return $this->plugin_file;
341 }
342 //-----------------------------------------------------
343
344
345 public function get_plugin_url($url='')
346 {
347 return $this->plugin_url;
348 }
349
350
351 //----------------------------------------------------
352
353 public function get_visitor_IP()
354 {
355 $ipaddress = isset($_SERVER['REMOTE_ADDR']) ? sanitize_text_field($_SERVER['REMOTE_ADDR']) : '';
356
357 if (!empty($_SERVER['HTTP_CLIENT_IP'])) {
358 $ipaddress = sanitize_text_field($_SERVER['HTTP_CLIENT_IP']);
359 } elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
360 // X-Forwarded-For may contain a comma-separated list; take the first entry.
361 $forwarded = explode(',', $_SERVER['HTTP_X_FORWARDED_FOR']);
362 $ipaddress = sanitize_text_field(trim($forwarded[0]));
363 }
364
365 // Security: validate that the value is actually an IP address.
366 // sanitize_text_field() does not strip HTML attribute breakers like
367 // double quotes or event-handler payloads, which previously allowed
368 // stored XSS when the IP was rendered in the admin dashboard.
369 if (!filter_var($ipaddress, FILTER_VALIDATE_IP)) {
370 $ipaddress = '';
371 }
372
373 return $ipaddress;
374 }
375
376 //----------------------------------------------------
377
378
379 public function get_visitor_OS()
380 {
381
382
383 $userAgent = isset($_SERVER['HTTP_USER_AGENT']) ? sanitize_text_field($_SERVER['HTTP_USER_AGENT']) : '';
384
385 $oses = array (
386 'iPhone' => '(iPhone)',
387 'Windows 3.11' => 'Win16',
388 'Windows 95' => '(Windows 95)|(Win95)|(Windows_95)',
389 'Windows 98' => '(Windows 98)|(Win98)',
390 'Windows 2000' => '(Windows NT 5.0)|(Windows 2000)',
391 'Windows XP' => '(Windows NT 5.1)|(Windows XP)',
392 'Windows 2003' => '(Windows NT 5.2)',
393 'Windows Vista' => '(Windows NT 6.0)|(Windows Vista)',
394 'Windows 7' => '(Windows NT 6.1)|(Windows 7)',
395 'Windows 8' => '(Windows NT 6.2)|(Windows 8)',
396 'Windows NT 4.0' => '(Windows NT 4.0)|(WinNT4.0)|(WinNT)|(Windows NT)',
397 'Windows ME' => 'Windows ME',
398 'Open BSD'=>'OpenBSD',
399 'Sun OS'=>'SunOS',
400 //'Linux'=>'(Linux)|(X11)', to detect if android or not
401 'Safari' => '(Safari)',
402 'Macintosh'=>'(Mac_PowerPC)|(Macintosh)',
403 'QNX'=>'QNX',
404 'BeOS'=>'BeOS',
405 'OS/2'=>'OS\/2',
406 'SearchBot'=>'(nuhk)|(Googlebot)|(Yammybot)|(Openbot)|(Slurp\/cat)|(msnbot)|(ia_archiver)'
407 );
408
409 foreach($oses as $os=>$pattern){
410
411 if(preg_match('/'.$pattern. '/i', $userAgent)) {
412 return $os;
413 }
414 }
415
416 // more tests
417
418 $ua = strtolower($userAgent);
419
420 if(stripos($ua,'android') !== false) {
421 return 'Android';
422 }
423
424 if(stripos($ua,'iphone') !== false) {
425 return 'iOS';
426 }
427
428
429 if(stripos($ua,'ipad') !== false) {
430 return 'iOS';
431 }
432
433 if(stripos($ua,'ipod') !== false) {
434 return 'iOS';
435 }
436
437 if(stripos($ua,'windows') !== false) {
438 return 'Windows';
439 }
440
441
442 if(stripos($ua,'linux') !== false) {
443 return 'Linux';
444 }
445
446
447 if(stripos($ua,'googlebot') !== false) {
448 return 'Googlebot';
449 }
450
451 if(stripos($ua,'bot') !== false) {
452 return 'SearchBot';
453 }
454
455
456 return 'Unknown';
457 }
458
459 //-----------------------------------------------------------------
460
461 public function get_visitor_Browser()
462 {
463
464 $u_agent= isset($_SERVER['HTTP_USER_AGENT']) ? sanitize_text_field($_SERVER['HTTP_USER_AGENT']) : '';
465 $bname = 'Unknown';
466
467 if(preg_match('/MSIE/i',$u_agent) && !preg_match('/Opera/i',$u_agent))
468 {
469 $bname = 'Internet Explorer';
470 }
471 elseif(preg_match('/Firefox/i',$u_agent))
472 {
473 $bname = 'Firefox';
474 }
475 elseif(preg_match('/Chrome/i',$u_agent))
476 {
477 $bname = 'Chrome';
478 }
479 elseif(preg_match('/Safari/i',$u_agent))
480 {
481 $bname = 'Safari';
482 }
483 elseif(preg_match('/Opera/i',$u_agent))
484 {
485 $bname = 'Opera';
486 }
487 elseif(preg_match('/Netscape/i',$u_agent))
488 {
489 $bname = 'Netscape';
490 }
491 elseif(preg_match('/googlebot/i',$u_agent))
492 {
493 $bname = 'GoogleBot';
494 }
495 elseif(preg_match('/bot/i',$u_agent))
496 {
497 $bname = 'SearchBot';
498 }
499
500
501 return sanitize_text_field($bname);
502
503 }
504
505
506 //----------------------------------------------------
507
508 public function option_msg($msg,$out='echo')
509 {
510
511 if($out=='echo')
512 echo '<div id="message" class="updated"><p>' . esc_html($msg) . '</p></div>';
513 elseif($out=='push')
514 $this->push_msg(esc_html($msg));
515 }
516
517 //----------------------------------------------------
518
519
520 public function info_option_msg($msg,$out='echo')
521 {
522
523
524 if($out=='echo')
525 echo '<div id="message" class="updated"><p><div class="info_icon"></div> ' . esc_html($msg) . '</p></div>';
526 elseif($out=='push')
527 $this->push_msg(esc_html($msg));
528
529
530 }
531
532 //----------------------------------------------------
533
534
535 public function warning_option_msg($msg,$out='echo')
536 {
537
538 if($out=='echo')
539 echo '<div id="message" class="error"><p><div class="warning_icon"></div> ' . esc_html($msg) . '</p></div>';
540 elseif($out=='push')
541 $this->push_msg(esc_html($msg));
542
543
544 }
545
546 //----------------------------------------------------
547
548 public function success_option_msg($msg,$out='echo')
549 {
550
551 if($out=='echo')
552 echo '<div id="message" class="updated"><p><div class="success_icon"></div> ' . esc_html($msg) . '</p></div>';
553 elseif($out=='push')
554 $this->push_msg(esc_html($msg));
555 }
556
557 //----------------------------------------------------
558
559 public function failure_option_msg($msg,$out='echo')
560 {
561
562 if($out=='echo')
563 echo '<div id="message" class="error"><p><div class="failure_icon"></div> ' . esc_html($msg) . '</p></div>';
564 elseif($out=='push')
565 $this->push_msg(esc_html($msg));
566
567 }
568
569 //----------------------------------------------------
570
571 private function push_msg($msg)
572 {
573 global $utilpro;
574 $msgs=$utilpro->get_option_value('admin_notices');
575 if(is_array($msgs))
576 {
577 $msgs[count($msgs)]=$msg;
578
579 }else
580 {
581 $msgs = array();
582 $msgs[0]=$msg;
583 }
584
585 $utilpro->update_option('admin_notices',$msgs);
586
587 }
588
589 //----------------------------------------------------
590
591
592 public function there_is_cache()
593 {
594
595 $plugins=get_option( 'active_plugins' );
596
597 for($i=0;$i<count($plugins);$i++)
598 {
599 if ( array_key_exists($i, $plugins) && stripos($plugins[$i],'cache')!==false)
600 {
601 return $plugins[$i];
602 }
603 }
604
605
606 return '';
607 }
608
609 //----------------------------------------------------
610
611
612 public function there_is_plugin($plugin)
613 {
614
615 $plugins=get_option( 'active_plugins' );
616
617 for($i=0;$i<count($plugins);$i++)
618 {
619 $phpfile = substr( $plugins[$i], strrpos( $plugins[$i], '/' )+1 );
620 $phpfile = explode(".", $phpfile);
621 $plugin_name = $phpfile[0];
622 if ($plugin_name==$plugin)
623 {
624 return true;
625 }
626 }
627
628
629 return false;
630 }
631
632 //---------------------------------------------------------------
633
634 public function regex_prepare($string)
635 {
636
637 $from= array('.', '+', '*', '?','[','^',']','$','(',')','{','}','=','!','<','>','|',':','-',')','/', '\\');
638 $to= array('\\.', '\\+', '\\*', '\\?','\\[','\\^','\\]','\\$','\\(','\\)','\\{','\\}','\\=','\\!','\\<','\\>','\\|','\\:','\\-','\\)','\\/','\\\\');
639 return str_replace($from,$to,$string);
640
641 }
642
643
644 }}
645
646 ?>