| 1 |
<?php |
| 2 |
|
| 3 |
class RabbitLoader_21_Util_Core{ |
| 4 |
|
| 5 |
private static $request_uri = ""; |
| 6 |
private static $request_url = ""; |
| 7 |
private static $is_no_optimization = false; |
| 8 |
|
| 9 |
public static function get_param($name, $allowCase=false){ |
| 10 |
$val = ''; |
| 11 |
if(isset($_GET[$name])){ |
| 12 |
$val = $_GET[$name]; |
| 13 |
}else if(isset($_POST[$name])){ |
| 14 |
$val = $_POST[$name]; |
| 15 |
} |
| 16 |
|
| 17 |
if(!empty($val)){ |
| 18 |
if($allowCase){ |
| 19 |
$val = preg_replace( '/[^A-Za-z0-9_\-.]/', '', $val); |
| 20 |
}else{ |
| 21 |
$val = sanitize_key($val); |
| 22 |
} |
| 23 |
} |
| 24 |
|
| 25 |
return $val; |
| 26 |
} |
| 27 |
public static function move(string $source, string $destination){ |
| 28 |
if(!file_exists($source)){ |
| 29 |
return false; |
| 30 |
} |
| 31 |
if(rename($source, $destination)) { |
| 32 |
@unlink($source); |
| 33 |
return true; |
| 34 |
}else if(copy($source, $destination)) |
| 35 |
{ |
| 36 |
@unlink($source); |
| 37 |
return true; |
| 38 |
} |
| 39 |
return false; |
| 40 |
} |
| 41 |
/** |
| 42 |
* Same as file_put_contents with debug mode wrapper |
| 43 |
*/ |
| 44 |
public static function fpc($fp, &$data, $debug){ |
| 45 |
$cache_dir = RabbitLoader_21_Util_WP::get_cache_dir(''); |
| 46 |
if(!file_exists($cache_dir)){ |
| 47 |
@mkdir($cache_dir, 0755, true); |
| 48 |
} |
| 49 |
if ($debug) { |
| 50 |
$file_updated = file_put_contents($fp, $data); |
| 51 |
} else { |
| 52 |
$file_updated = @file_put_contents($fp, $data); |
| 53 |
} |
| 54 |
return $file_updated; |
| 55 |
} |
| 56 |
|
| 57 |
/** |
| 58 |
* Same as file_append_contents with debug mode wrapper |
| 59 |
*/ |
| 60 |
public static function fac($fn, &$data, $debug){ |
| 61 |
RabbitLoader_21_Core::get_log_file($fn, $fp); |
| 62 |
|
| 63 |
if ($debug) { |
| 64 |
$file_updated = file_put_contents($fp, $data, FILE_APPEND | LOCK_EX); |
| 65 |
} else { |
| 66 |
$file_updated = @file_put_contents($fp, $data, FILE_APPEND | LOCK_EX); |
| 67 |
} |
| 68 |
|
| 69 |
if(!$file_updated){ |
| 70 |
//if file writing fails, ensure dir is created if missing |
| 71 |
$cache_dir = RabbitLoader_21_Util_WP::get_cache_dir(''); |
| 72 |
if(!file_exists($cache_dir)){ |
| 73 |
@mkdir($cache_dir, 0755, true); |
| 74 |
} |
| 75 |
} |
| 76 |
|
| 77 |
return $file_updated; |
| 78 |
} |
| 79 |
|
| 80 |
public static function get_request_type(){ |
| 81 |
return empty($_SERVER['REQUEST_METHOD']) ? '' : strtolower($_SERVER['REQUEST_METHOD']); |
| 82 |
} |
| 83 |
|
| 84 |
private static function isHTTPS(){ |
| 85 |
return (isset($_SERVER['SERVER_PORT']) && $_SERVER['SERVER_PORT'] == 443) || (isset($_SERVER['HTTPS']) && strcmp($_SERVER['HTTPS'], "off")!==0); |
| 86 |
} |
| 87 |
|
| 88 |
public static function isNoOptimization(){ |
| 89 |
return self::$is_no_optimization || isset($_SERVER['HTTP_RL_NO_OPTIMIZATION']) || isset($_GET['rl-no-optimization']); |
| 90 |
} |
| 91 |
|
| 92 |
public static function reset_requested_uri(){ |
| 93 |
self::$request_uri = ''; |
| 94 |
self::$request_url = ''; |
| 95 |
} |
| 96 |
|
| 97 |
public static function get_requested_uri(&$uri, &$url){ |
| 98 |
if(!empty(self::$request_url)){ |
| 99 |
$uri = self::$request_uri; |
| 100 |
$url = self::$request_url; |
| 101 |
return; |
| 102 |
} |
| 103 |
//process request |
| 104 |
if(isset($_SERVER['REQUEST_URI'])){ |
| 105 |
$replace_count = 0; |
| 106 |
$_SERVER['REQUEST_URI'] = str_ireplace('rl-no-optimization=1','', $_SERVER['REQUEST_URI'], $replace_count); |
| 107 |
if($replace_count){ |
| 108 |
$_SERVER['REQUEST_URI'] = rtrim($_SERVER['REQUEST_URI'], '?'); |
| 109 |
self::$is_no_optimization = true; |
| 110 |
} |
| 111 |
} |
| 112 |
$request_uri = isset($_SERVER['REQUEST_URI']) ? $_SERVER['REQUEST_URI'] : ''; |
| 113 |
|
| 114 |
$http_host = isset($_SERVER['HTTP_HOST']) ? $_SERVER['HTTP_HOST'] : ''; |
| 115 |
$raw_link = (self::isHTTPS() ? "https" : "http") . "://$http_host$request_uri"; |
| 116 |
|
| 117 |
$parsed_url = parse_url($raw_link); |
| 118 |
$query = empty($parsed_url['query']) ? '': trim($parsed_url['query']); |
| 119 |
if(!empty($query)){ |
| 120 |
try{ |
| 121 |
parse_str($query, $qs_vars); |
| 122 |
|
| 123 |
#remove default ignore params |
| 124 |
$ig_params = ['_gl', 'epik', 'fbclid', 'gbraid','gclid', 'msclkid', 'utm_source', 'utm_medium', 'utm_campaign','utm_content','utm_term','vgo_ee', 'wbraid', 'zenid' ,'rltest']; |
| 125 |
foreach($ig_params as $p){ |
| 126 |
unset($qs_vars[$p]); |
| 127 |
} |
| 128 |
|
| 129 |
#remove user defined ignore params |
| 130 |
RabbitLoader_21_Core::getWpUserOption($user_options); |
| 131 |
if(!empty($user_options['ignore_params'])){ |
| 132 |
$ignore_params = explode("\n", $user_options['ignore_params']); |
| 133 |
if(!empty($ignore_params)){ |
| 134 |
foreach($ignore_params as $p){ |
| 135 |
unset($qs_vars[trim($p)]); |
| 136 |
} |
| 137 |
} |
| 138 |
} |
| 139 |
|
| 140 |
$query = http_build_query($qs_vars); |
| 141 |
}catch(Throwable $e){ |
| 142 |
RabbitLoader_21_Core::on_exception($e); |
| 143 |
} |
| 144 |
} |
| 145 |
|
| 146 |
$uri = trim(@$parsed_url['path']).(empty($query)?'':'?'.$query);; |
| 147 |
$host = trim(@$parsed_url['host']); |
| 148 |
$scheme = trim(@$parsed_url['scheme']); |
| 149 |
$url = $scheme.'://'.$host.$uri; |
| 150 |
|
| 151 |
self::$request_uri = $uri; |
| 152 |
self::$request_url = $url; |
| 153 |
} |
| 154 |
|
| 155 |
public static function send_headers($headers){ |
| 156 |
$headers_sent = 0; |
| 157 |
if(empty($headers)){ |
| 158 |
return $headers_sent; |
| 159 |
} |
| 160 |
if(!is_array($headers)){ |
| 161 |
$headers_decoded = json_decode($headers, true); |
| 162 |
if($headers_decoded===false){ |
| 163 |
$e = new WP_Error( 'header_json_decode', json_last_error_msg(), $headers); |
| 164 |
RabbitLoader_21_Core::on_exception($e); |
| 165 |
}else{ |
| 166 |
$headers = $headers_decoded; |
| 167 |
} |
| 168 |
} |
| 169 |
if(!empty($headers)){ |
| 170 |
foreach($headers as $key=>$values){ |
| 171 |
foreach($values as $val){ |
| 172 |
header($key.':'.$val, false); |
| 173 |
$headers_sent++; |
| 174 |
} |
| 175 |
} |
| 176 |
} |
| 177 |
return $headers_sent; |
| 178 |
} |
| 179 |
|
| 180 |
public static function isDev(){ |
| 181 |
return ((defined('RABBITLOADER_PLUG_ENV') && RABBITLOADER_PLUG_ENV=='DEV') || (defined('RABBITLOADER_AC_PLUG_ENV') && RABBITLOADER_AC_PLUG_ENV=='DEV')); |
| 182 |
} |
| 183 |
|
| 184 |
public static function serverURINoGet(){ |
| 185 |
return strtok($_SERVER["REQUEST_URI"],'?'); |
| 186 |
} |
| 187 |
} |
| 188 |
|
| 189 |
?> |