| 1 |
<?php |
| 2 |
|
| 3 |
include_once(LOGINIZER_DIR.'/IPv6/IPv6.php'); |
| 4 |
|
| 5 |
// Get the client IP |
| 6 |
function _lz_getip(){ |
| 7 |
if(isset($_SERVER["REMOTE_ADDR"])){ |
| 8 |
return $_SERVER["REMOTE_ADDR"]; |
| 9 |
}elseif(isset($_SERVER["HTTP_X_FORWARDED_FOR"])){ |
| 10 |
return $_SERVER["HTTP_X_FORWARDED_FOR"]; |
| 11 |
}elseif(isset($_SERVER["HTTP_CLIENT_IP"])){ |
| 12 |
return $_SERVER["HTTP_CLIENT_IP"]; |
| 13 |
} |
| 14 |
} |
| 15 |
|
| 16 |
// Get the client IP |
| 17 |
function lz_getip(){ |
| 18 |
|
| 19 |
global $loginizer; |
| 20 |
|
| 21 |
// Just so that we have something |
| 22 |
$ip = _lz_getip(); |
| 23 |
|
| 24 |
$loginizer['ip_method'] = (int) @$loginizer['ip_method']; |
| 25 |
|
| 26 |
if(isset($_SERVER["REMOTE_ADDR"])){ |
| 27 |
$ip = $_SERVER["REMOTE_ADDR"]; |
| 28 |
} |
| 29 |
|
| 30 |
if(isset($_SERVER["HTTP_X_FORWARDED_FOR"]) && @$loginizer['ip_method'] == 1){ |
| 31 |
if(strpos($_SERVER["HTTP_X_FORWARDED_FOR"], ',')){ |
| 32 |
$temp_ip = explode(',', $_SERVER["HTTP_X_FORWARDED_FOR"]); |
| 33 |
$ip = trim($temp_ip[0]); |
| 34 |
}else{ |
| 35 |
$ip = $_SERVER["HTTP_X_FORWARDED_FOR"]; |
| 36 |
} |
| 37 |
} |
| 38 |
|
| 39 |
if(isset($_SERVER["HTTP_CLIENT_IP"]) && @$loginizer['ip_method'] == 2){ |
| 40 |
$ip = $_SERVER["HTTP_CLIENT_IP"]; |
| 41 |
} |
| 42 |
|
| 43 |
if(@$loginizer['ip_method'] == 3 && isset($_SERVER[@$loginizer['custom_ip_method']])){ |
| 44 |
$ip = $_SERVER[@$loginizer['custom_ip_method']]; |
| 45 |
} |
| 46 |
|
| 47 |
// Hacking fix for X-Forwarded-For |
| 48 |
if(!lz_valid_ip($ip)){ |
| 49 |
return ''; |
| 50 |
} |
| 51 |
|
| 52 |
return $ip; |
| 53 |
|
| 54 |
} |
| 55 |
|
| 56 |
// Execute a select query and return an array |
| 57 |
function lz_selectquery($query, $array = 0){ |
| 58 |
global $wpdb; |
| 59 |
|
| 60 |
$result = $wpdb->get_results($query, 'ARRAY_A'); |
| 61 |
|
| 62 |
if(empty($array)){ |
| 63 |
return current($result); |
| 64 |
}else{ |
| 65 |
return $result; |
| 66 |
} |
| 67 |
} |
| 68 |
|
| 69 |
// Check if an IP is valid |
| 70 |
function lz_valid_ip($ip){ |
| 71 |
|
| 72 |
// IPv6 |
| 73 |
if(lz_valid_ipv6($ip)){ |
| 74 |
return true; |
| 75 |
} |
| 76 |
|
| 77 |
// IPv4 |
| 78 |
if(!ip2long($ip) || !lz_valid_ipv4($ip)){ |
| 79 |
return false; |
| 80 |
} |
| 81 |
|
| 82 |
return true; |
| 83 |
} |
| 84 |
|
| 85 |
function lz_valid_ipv4($ip){ |
| 86 |
if(!preg_match('/^(\d){1,3}\.(\d){1,3}\.(\d){1,3}\.(\d){1,3}$/is', $ip) || substr_count($ip, '.') != 3){ |
| 87 |
return false; |
| 88 |
} |
| 89 |
|
| 90 |
$r = explode('.', $ip); |
| 91 |
|
| 92 |
foreach($r as $v){ |
| 93 |
$v = (int) $v; |
| 94 |
if($v > 255 || $v < 0){ |
| 95 |
return false; |
| 96 |
} |
| 97 |
} |
| 98 |
|
| 99 |
return true; |
| 100 |
|
| 101 |
} |
| 102 |
|
| 103 |
function lz_valid_ipv6($ip){ |
| 104 |
|
| 105 |
$pattern = '/^((([0-9A-Fa-f]{1,4}:){7}[0-9A-Fa-f]{1,4})|(([0-9A-Fa-f]{1,4}:){6}:[0-9A-Fa-f]{1,4})|(([0-9A-Fa-f]{1,4}:){5}:([0-9A-Fa-f]{1,4}:)?[0-9A-Fa-f]{1,4})|(([0-9A-Fa-f]{1,4}:){4}:([0-9A-Fa-f]{1,4}:){0,2}[0-9A-Fa-f]{1,4})|(([0-9A-Fa-f]{1,4}:){3}:([0-9A-Fa-f]{1,4}:){0,3}[0-9A-Fa-f]{1,4})|(([0-9A-Fa-f]{1,4}:){2}:([0-9A-Fa-f]{1,4}:){0,4}[0-9A-Fa-f]{1,4})|(([0-9A-Fa-f]{1,4}:){6}((\b((25[0-5])|(1\d{2})|(2[0-4]\d)|(\d{1,2}))\b)\.){3}(\b((25[0-5])|(1\d{2})|(2[0-4]\d)|(\d{1,2}))\b))|(([0-9A-Fa-f]{1,4}:){0,5}:((\b((25[0-5])|(1\d{2})|(2[0-4]\d)|(\d{1,2}))\b)\.){3}(\b((25[0-5])|(1\d{2})|(2[0-4]\d)|(\d{1,2}))\b))|(::([0-9A-Fa-f]{1,4}:){0,5}((\b((25[0-5])|(1\d{2})|(2[0-4]\d)|(\d{1,2}))\b)\.){3}(\b((25[0-5])|(1\d{2})|(2[0-4]\d)|(\d{1,2}))\b))|([0-9A-Fa-f]{1,4}::([0-9A-Fa-f]{1,4}:){0,5}[0-9A-Fa-f]{1,4})|(::([0-9A-Fa-f]{1,4}:){0,6}[0-9A-Fa-f]{1,4})|(([0-9A-Fa-f]{1,4}:){1,7}:))$/'; |
| 106 |
|
| 107 |
if(!preg_match($pattern, $ip)){ |
| 108 |
return false; |
| 109 |
} |
| 110 |
|
| 111 |
return true; |
| 112 |
|
| 113 |
} |
| 114 |
|
| 115 |
// Check if a field is posted via POST else return default value |
| 116 |
function lz_optpost($name, $default = ''){ |
| 117 |
|
| 118 |
if(!empty($_POST[$name])){ |
| 119 |
return lz_inputsec(lz_htmlizer(trim($_POST[$name]))); |
| 120 |
} |
| 121 |
|
| 122 |
return $default; |
| 123 |
} |
| 124 |
|
| 125 |
// Check if a field is posted via GET else return default value |
| 126 |
function lz_optget($name, $default = ''){ |
| 127 |
|
| 128 |
if(!empty($_GET[$name])){ |
| 129 |
return lz_inputsec(lz_htmlizer(trim($_GET[$name]))); |
| 130 |
} |
| 131 |
|
| 132 |
return $default; |
| 133 |
} |
| 134 |
|
| 135 |
// Check if a field is posted via GET or POST else return default value |
| 136 |
function lz_optreq($name, $default = ''){ |
| 137 |
|
| 138 |
if(!empty($_REQUEST[$name])){ |
| 139 |
return lz_inputsec(lz_htmlizer(trim($_REQUEST[$name]))); |
| 140 |
} |
| 141 |
|
| 142 |
return $default; |
| 143 |
} |
| 144 |
|
| 145 |
// For filling in posted values |
| 146 |
function lz_POSTval($name, $default = ''){ |
| 147 |
|
| 148 |
return (!empty($_POST) ? (!isset($_POST[$name]) ? '' : $_POST[$name]) : $default); |
| 149 |
|
| 150 |
} |
| 151 |
|
| 152 |
function lz_POSTchecked($name, $default = false, $submit_name = ''){ |
| 153 |
|
| 154 |
if(!empty($submit_name)){ |
| 155 |
$post_to_check = isset($_POST[$submit_name]) ? $_POST[$submit_name] : ''; |
| 156 |
}else{ |
| 157 |
$post_to_check = $_POST; |
| 158 |
} |
| 159 |
|
| 160 |
return (!empty($post_to_check) ? (isset($_POST[$name]) ? 'checked="checked"' : '') : (!empty($default) ? 'checked="checked"' : '')); |
| 161 |
|
| 162 |
} |
| 163 |
|
| 164 |
function lz_POSTselect($name, $value, $default = false){ |
| 165 |
|
| 166 |
if(empty($_POST)){ |
| 167 |
if(!empty($default)){ |
| 168 |
return 'selected="selected"'; |
| 169 |
} |
| 170 |
}else{ |
| 171 |
if(isset($_POST[$name])){ |
| 172 |
if(trim($_POST[$name]) == $value){ |
| 173 |
return 'selected="selected"'; |
| 174 |
} |
| 175 |
} |
| 176 |
} |
| 177 |
|
| 178 |
} |
| 179 |
|
| 180 |
function lz_POSTradio($name, $val, $default = null){ |
| 181 |
|
| 182 |
return (!empty($_POST) ? (@$_POST[$name] == $val ? 'checked="checked"' : '') : (!is_null($default) && $default == $val ? 'checked="checked"' : '')); |
| 183 |
|
| 184 |
} |
| 185 |
|
| 186 |
function lz_inputsec($string){ |
| 187 |
|
| 188 |
$string = addslashes($string); |
| 189 |
|
| 190 |
// This is to replace ` which can cause the command to be executed in exec() |
| 191 |
$string = str_replace('`', '\`', $string); |
| 192 |
|
| 193 |
return $string; |
| 194 |
|
| 195 |
} |
| 196 |
|
| 197 |
function lz_htmlizer($string){ |
| 198 |
|
| 199 |
$string = htmlentities($string, ENT_QUOTES, 'UTF-8'); |
| 200 |
|
| 201 |
preg_match_all('/(&#(\d{1,7}|x[0-9a-fA-F]{1,6});)/', $string, $matches);//r_print($matches); |
| 202 |
|
| 203 |
foreach($matches[1] as $mk => $mv){ |
| 204 |
$tmp_m = lz_entity_check($matches[2][$mk]); |
| 205 |
$string = str_replace($matches[1][$mk], $tmp_m, $string); |
| 206 |
} |
| 207 |
|
| 208 |
return $string; |
| 209 |
|
| 210 |
} |
| 211 |
|
| 212 |
function lz_entity_check($string){ |
| 213 |
|
| 214 |
//Convert Hexadecimal to Decimal |
| 215 |
$num = ((substr($string, 0, 1) === 'x') ? hexdec(substr($string, 1)) : (int) $string); |
| 216 |
|
| 217 |
//Squares and Spaces - return nothing |
| 218 |
$string = (($num > 0x10FFFF || ($num >= 0xD800 && $num <= 0xDFFF) || $num < 0x20) ? '' : '&#'.$num.';'); |
| 219 |
|
| 220 |
return $string; |
| 221 |
|
| 222 |
} |
| 223 |
|
| 224 |
// Check if a checkbox is selected |
| 225 |
function lz_is_checked($post){ |
| 226 |
|
| 227 |
if(!empty($_POST[$post])){ |
| 228 |
return true; |
| 229 |
} |
| 230 |
return false; |
| 231 |
} |
| 232 |
|
| 233 |
// Reoort an error |
| 234 |
function lz_report_error($error = array()){ |
| 235 |
|
| 236 |
if(empty($error)){ |
| 237 |
return true; |
| 238 |
} |
| 239 |
|
| 240 |
$error_string = '<b>Please fix the below error(s) :</b> <br />'; |
| 241 |
|
| 242 |
foreach($error as $ek => $ev){ |
| 243 |
$error_string .= '* '.$ev.'<br />'; |
| 244 |
} |
| 245 |
|
| 246 |
echo '<div id="message" class="error"><p>' |
| 247 |
. __($error_string, 'loginizer') |
| 248 |
. '</p></div>'; |
| 249 |
} |
| 250 |
|
| 251 |
// Report a notice |
| 252 |
function lz_report_notice($notice = array()){ |
| 253 |
|
| 254 |
global $wp_version; |
| 255 |
|
| 256 |
if(empty($notice)){ |
| 257 |
return true; |
| 258 |
} |
| 259 |
|
| 260 |
// Which class do we have to use ? |
| 261 |
if(version_compare($wp_version, '3.8', '<')){ |
| 262 |
$notice_class = 'updated'; |
| 263 |
}else{ |
| 264 |
$notice_class = 'updated'; |
| 265 |
} |
| 266 |
|
| 267 |
$notice_string = '<b>Please check the below notice(s) :</b> <br />'; |
| 268 |
|
| 269 |
foreach($notice as $ek => $ev){ |
| 270 |
$notice_string .= '* '.$ev.'<br />'; |
| 271 |
} |
| 272 |
|
| 273 |
echo '<div id="message" class="'.$notice_class.'"><p>' |
| 274 |
. __($notice_string, 'loginizer') |
| 275 |
. '</p></div>'; |
| 276 |
} |
| 277 |
|
| 278 |
// Convert an objext to array |
| 279 |
function lz_objectToArray($d){ |
| 280 |
|
| 281 |
if(is_object($d)){ |
| 282 |
$d = get_object_vars($d); |
| 283 |
} |
| 284 |
|
| 285 |
if(is_array($d)){ |
| 286 |
return array_map(__FUNCTION__, $d); // recursive |
| 287 |
}elseif(is_object($d)){ |
| 288 |
return lz_objectToArray($d); |
| 289 |
}else{ |
| 290 |
return $d; |
| 291 |
} |
| 292 |
} |
| 293 |
|
| 294 |
// Sanitize variables |
| 295 |
function lz_sanitize_variables($variables = array()){ |
| 296 |
|
| 297 |
if(is_array($variables)){ |
| 298 |
foreach($variables as $k => $v){ |
| 299 |
$variables[$k] = trim($v); |
| 300 |
$variables[$k] = escapeshellcmd($v); |
| 301 |
} |
| 302 |
}else{ |
| 303 |
$variables = escapeshellcmd(trim($variables)); |
| 304 |
} |
| 305 |
|
| 306 |
return $variables; |
| 307 |
} |
| 308 |
|
| 309 |
// Is multisite ? |
| 310 |
function lz_is_multisite() { |
| 311 |
|
| 312 |
if(function_exists('get_site_option') && function_exists('is_multisite') && is_multisite()){ |
| 313 |
return true; |
| 314 |
} |
| 315 |
|
| 316 |
return false; |
| 317 |
} |
| 318 |
|
| 319 |
// Generate a random string |
| 320 |
function lz_RandomString($length = 10){ |
| 321 |
$characters = '0123456789abcdefghijklmnopqrstuvwxyz'; |
| 322 |
$charactersLength = strlen($characters); |
| 323 |
$randomString = ''; |
| 324 |
for($i = 0; $i < $length; $i++){ |
| 325 |
$randomString .= $characters[rand(0, $charactersLength - 1)]; |
| 326 |
} |
| 327 |
return $randomString; |
| 328 |
} |
| 329 |
|
| 330 |
function lz_print($array){ |
| 331 |
|
| 332 |
echo '<pre>'; |
| 333 |
print_r($array); |
| 334 |
echo '</pre>'; |
| 335 |
|
| 336 |
} |
| 337 |
|
| 338 |
function lz_cleanpath($path){ |
| 339 |
$path = str_replace('\\\\', '/', $path); |
| 340 |
$path = str_replace('\\', '/', $path); |
| 341 |
$path = str_replace('//', '/', $path); |
| 342 |
return rtrim($path, '/'); |
| 343 |
} |
| 344 |
|
| 345 |
// Returns the Numeric Value of results Per Page |
| 346 |
function lz_get_page($get = 'page', $resperpage = 50){ |
| 347 |
|
| 348 |
$resperpage = (!empty($_REQUEST['reslen']) && is_numeric($_REQUEST['reslen']) ? (int) lz_optreq('reslen') : $resperpage); |
| 349 |
|
| 350 |
if(lz_optget($get)){ |
| 351 |
$pg = (int) lz_optget($get); |
| 352 |
$pg = $pg - 1; |
| 353 |
$page = ($pg * $resperpage); |
| 354 |
$page = ($page <= 0 ? 0 : $page); |
| 355 |
}else{ |
| 356 |
$page = 0; |
| 357 |
} |
| 358 |
return $page; |
| 359 |
} |
| 360 |
|
| 361 |
// Replaces the Variables with the supplied ones |
| 362 |
function lz_lang_vars_name($str, $array){ |
| 363 |
|
| 364 |
foreach($array as $k => $v){ |
| 365 |
|
| 366 |
$str = str_replace('$'.$k, $v, $str); |
| 367 |
|
| 368 |
} |
| 369 |
|
| 370 |
return $str; |
| 371 |
|
| 372 |
} |
| 373 |
|
| 374 |
// Check if Loginizer is premium |
| 375 |
function loginizer_is_premium(){ |
| 376 |
return defined('LOGINIZER_PREMIUM'); |
| 377 |
} |
| 378 |
|
| 379 |
function loginizer_feature_available($feature = '', $return = 0){ |
| 380 |
|
| 381 |
if(loginizer_is_premium()){ |
| 382 |
return true; |
| 383 |
} |
| 384 |
|
| 385 |
$msg = ''; |
| 386 |
|
| 387 |
if(!empty($feature)){ |
| 388 |
$msg .= '<b>'.$feature.'</b> is available in the Pro version of Loginizer. '; |
| 389 |
} |
| 390 |
|
| 391 |
$msg .= '<a href="'.LOGINIZER_PRICING_URL.'" target="_blank" style="text-decoration:none; color:green;"><b>Upgrade to Pro</b></a>'; |
| 392 |
|
| 393 |
if(!empty($return)){ |
| 394 |
return $msg; |
| 395 |
}else{ |
| 396 |
echo '<div style="color:#a94442; background-color:#f2dede; border-color:#ebccd1; padding:15px; margin-bottom:20px; border:1px solid transparent; border-radius:4px;">'.$msg.'</div>'; |
| 397 |
} |
| 398 |
|
| 399 |
} |