| 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 |
$ip = $_SERVER["HTTP_X_FORWARDED_FOR"]; |
| 32 |
} |
| 33 |
|
| 34 |
if(isset($_SERVER["HTTP_CLIENT_IP"]) && @$loginizer['ip_method'] == 2){ |
| 35 |
$ip = $_SERVER["HTTP_CLIENT_IP"]; |
| 36 |
} |
| 37 |
|
| 38 |
if(@$loginizer['ip_method'] == 3 && isset($_SERVER[@$loginizer['custom_ip_method']])){ |
| 39 |
$ip = $_SERVER[@$loginizer['custom_ip_method']]; |
| 40 |
} |
| 41 |
|
| 42 |
// Hacking fix for X-Forwarded-For |
| 43 |
if(!lz_valid_ip($ip)){ |
| 44 |
return ''; |
| 45 |
} |
| 46 |
|
| 47 |
return $ip; |
| 48 |
|
| 49 |
} |
| 50 |
|
| 51 |
// Execute a select query and return an array |
| 52 |
function lz_selectquery($query, $array = 0){ |
| 53 |
global $wpdb; |
| 54 |
|
| 55 |
$result = $wpdb->get_results($query, 'ARRAY_A'); |
| 56 |
|
| 57 |
if(empty($array)){ |
| 58 |
return current($result); |
| 59 |
}else{ |
| 60 |
return $result; |
| 61 |
} |
| 62 |
} |
| 63 |
|
| 64 |
// Check if an IP is valid |
| 65 |
function lz_valid_ip($ip){ |
| 66 |
|
| 67 |
// IPv6 |
| 68 |
if(lz_valid_ipv6($ip)){ |
| 69 |
return true; |
| 70 |
} |
| 71 |
|
| 72 |
// IPv4 |
| 73 |
if(!ip2long($ip)){ |
| 74 |
return false; |
| 75 |
} |
| 76 |
|
| 77 |
return true; |
| 78 |
} |
| 79 |
|
| 80 |
function lz_valid_ipv6($ip){ |
| 81 |
|
| 82 |
$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}:))$/'; |
| 83 |
|
| 84 |
if(!preg_match($pattern, $ip)){ |
| 85 |
return false; |
| 86 |
} |
| 87 |
|
| 88 |
return true; |
| 89 |
|
| 90 |
} |
| 91 |
|
| 92 |
// Check if a field is posted via POST else return default value |
| 93 |
function lz_optpost($name, $default = ''){ |
| 94 |
|
| 95 |
if(!empty($_POST[$name])){ |
| 96 |
return lz_inputsec(lz_htmlizer(trim($_POST[$name]))); |
| 97 |
} |
| 98 |
|
| 99 |
return $default; |
| 100 |
} |
| 101 |
|
| 102 |
// Check if a field is posted via GET else return default value |
| 103 |
function lz_optget($name, $default = ''){ |
| 104 |
|
| 105 |
if(!empty($_GET[$name])){ |
| 106 |
return lz_inputsec(lz_htmlizer(trim($_GET[$name]))); |
| 107 |
} |
| 108 |
|
| 109 |
return $default; |
| 110 |
} |
| 111 |
|
| 112 |
// Check if a field is posted via GET or POST else return default value |
| 113 |
function lz_optreq($name, $default = ''){ |
| 114 |
|
| 115 |
if(!empty($_REQUEST[$name])){ |
| 116 |
return lz_inputsec(lz_htmlizer(trim($_REQUEST[$name]))); |
| 117 |
} |
| 118 |
|
| 119 |
return $default; |
| 120 |
} |
| 121 |
|
| 122 |
// For filling in posted values |
| 123 |
function lz_POSTval($name, $default = ''){ |
| 124 |
|
| 125 |
return (!empty($_POST) ? (!isset($_POST[$name]) ? '' : $_POST[$name]) : $default); |
| 126 |
|
| 127 |
} |
| 128 |
|
| 129 |
function lz_POSTchecked($name, $default = false){ |
| 130 |
|
| 131 |
return (!empty($_POST) ? (isset($_POST[$name]) ? 'checked="checked"' : '') : (!empty($default) ? 'checked="checked"' : '')); |
| 132 |
|
| 133 |
} |
| 134 |
|
| 135 |
function lz_POSTselect($name, $value, $default = false){ |
| 136 |
|
| 137 |
if(empty($_POST)){ |
| 138 |
if(!empty($default)){ |
| 139 |
return 'selected="selected"'; |
| 140 |
} |
| 141 |
}else{ |
| 142 |
if(isset($_POST[$name])){ |
| 143 |
if(trim($_POST[$name]) == $value){ |
| 144 |
return 'selected="selected"'; |
| 145 |
} |
| 146 |
} |
| 147 |
} |
| 148 |
|
| 149 |
} |
| 150 |
|
| 151 |
function lz_inputsec($string){ |
| 152 |
|
| 153 |
if(!get_magic_quotes_gpc()){ |
| 154 |
|
| 155 |
$string = addslashes($string); |
| 156 |
|
| 157 |
}else{ |
| 158 |
|
| 159 |
$string = stripslashes($string); |
| 160 |
$string = addslashes($string); |
| 161 |
|
| 162 |
} |
| 163 |
|
| 164 |
// This is to replace ` which can cause the command to be executed in exec() |
| 165 |
$string = str_replace('`', '\`', $string); |
| 166 |
|
| 167 |
return $string; |
| 168 |
|
| 169 |
} |
| 170 |
|
| 171 |
function lz_htmlizer($string){ |
| 172 |
|
| 173 |
$string = htmlentities($string, ENT_QUOTES, 'UTF-8'); |
| 174 |
|
| 175 |
preg_match_all('/(&#(\d{1,7}|x[0-9a-fA-F]{1,6});)/', $string, $matches);//r_print($matches); |
| 176 |
|
| 177 |
foreach($matches[1] as $mk => $mv){ |
| 178 |
$tmp_m = lz_entity_check($matches[2][$mk]); |
| 179 |
$string = str_replace($matches[1][$mk], $tmp_m, $string); |
| 180 |
} |
| 181 |
|
| 182 |
return $string; |
| 183 |
|
| 184 |
} |
| 185 |
|
| 186 |
function lz_entity_check($string){ |
| 187 |
|
| 188 |
//Convert Hexadecimal to Decimal |
| 189 |
$num = ((substr($string, 0, 1) === 'x') ? hexdec(substr($string, 1)) : (int) $string); |
| 190 |
|
| 191 |
//Squares and Spaces - return nothing |
| 192 |
$string = (($num > 0x10FFFF || ($num >= 0xD800 && $num <= 0xDFFF) || $num < 0x20) ? '' : '&#'.$num.';'); |
| 193 |
|
| 194 |
return $string; |
| 195 |
|
| 196 |
} |
| 197 |
|
| 198 |
// Check if a checkbox is selected |
| 199 |
function lz_is_checked($post){ |
| 200 |
|
| 201 |
if(!empty($_POST[$post])){ |
| 202 |
return true; |
| 203 |
} |
| 204 |
return false; |
| 205 |
} |
| 206 |
|
| 207 |
// Reoort an error |
| 208 |
function lz_report_error($error = array()){ |
| 209 |
|
| 210 |
if(empty($error)){ |
| 211 |
return true; |
| 212 |
} |
| 213 |
|
| 214 |
$error_string = '<b>Please fix the below error(s) :</b> <br />'; |
| 215 |
|
| 216 |
foreach($error as $ek => $ev){ |
| 217 |
$error_string .= '* '.$ev.'<br />'; |
| 218 |
} |
| 219 |
|
| 220 |
echo '<div id="message" class="error"><p>' |
| 221 |
. __($error_string, 'loginizer') |
| 222 |
. '</p></div>'; |
| 223 |
} |
| 224 |
|
| 225 |
// Report a notice |
| 226 |
function lz_report_notice($notice = array()){ |
| 227 |
|
| 228 |
global $wp_version; |
| 229 |
|
| 230 |
if(empty($notice)){ |
| 231 |
return true; |
| 232 |
} |
| 233 |
|
| 234 |
// Which class do we have to use ? |
| 235 |
if(version_compare($wp_version, '3.8', '<')){ |
| 236 |
$notice_class = 'updated'; |
| 237 |
}else{ |
| 238 |
$notice_class = 'updated'; |
| 239 |
} |
| 240 |
|
| 241 |
$notice_string = '<b>Please check the below notice(s) :</b> <br />'; |
| 242 |
|
| 243 |
foreach($notice as $ek => $ev){ |
| 244 |
$notice_string .= '* '.$ev.'<br />'; |
| 245 |
} |
| 246 |
|
| 247 |
echo '<div id="message" class="'.$notice_class.'"><p>' |
| 248 |
. __($notice_string, 'loginizer') |
| 249 |
. '</p></div>'; |
| 250 |
} |
| 251 |
|
| 252 |
// Convert an objext to array |
| 253 |
function lz_objectToArray($d){ |
| 254 |
|
| 255 |
if(is_object($d)){ |
| 256 |
$d = get_object_vars($d); |
| 257 |
} |
| 258 |
|
| 259 |
if(is_array($d)){ |
| 260 |
return array_map(__FUNCTION__, $d); // recursive |
| 261 |
}elseif(is_object($d)){ |
| 262 |
return lz_objectToArray($d); |
| 263 |
}else{ |
| 264 |
return $d; |
| 265 |
} |
| 266 |
} |
| 267 |
|
| 268 |
// Sanitize variables |
| 269 |
function lz_sanitize_variables($variables = array()){ |
| 270 |
|
| 271 |
if(is_array($variables)){ |
| 272 |
foreach($variables as $k => $v){ |
| 273 |
$variables[$k] = trim($v); |
| 274 |
$variables[$k] = escapeshellcmd($v); |
| 275 |
} |
| 276 |
}else{ |
| 277 |
$variables = escapeshellcmd(trim($variables)); |
| 278 |
} |
| 279 |
|
| 280 |
return $variables; |
| 281 |
} |
| 282 |
|
| 283 |
// Is multisite ? |
| 284 |
function lz_is_multisite() { |
| 285 |
|
| 286 |
if(function_exists('get_site_option') && function_exists('is_multisite') && is_multisite()){ |
| 287 |
return true; |
| 288 |
} |
| 289 |
|
| 290 |
return false; |
| 291 |
} |
| 292 |
|
| 293 |
// Generate a random string |
| 294 |
function lz_RandomString($length = 10){ |
| 295 |
$characters = '0123456789abcdefghijklmnopqrstuvwxyz'; |
| 296 |
$charactersLength = strlen($characters); |
| 297 |
$randomString = ''; |
| 298 |
for($i = 0; $i < $length; $i++){ |
| 299 |
$randomString .= $characters[rand(0, $charactersLength - 1)]; |
| 300 |
} |
| 301 |
return $randomString; |
| 302 |
} |
| 303 |
|
| 304 |
function lz_print($array){ |
| 305 |
|
| 306 |
echo '<pre>'; |
| 307 |
print_r($array); |
| 308 |
echo '</pre>'; |
| 309 |
|
| 310 |
} |
| 311 |
|
| 312 |
function lz_cleanpath($path){ |
| 313 |
$path = str_replace('\\\\', '/', $path); |
| 314 |
$path = str_replace('\\', '/', $path); |
| 315 |
$path = str_replace('//', '/', $path); |
| 316 |
return rtrim($path, '/'); |
| 317 |
} |
| 318 |
|
| 319 |
// Returns the Numeric Value of results Per Page |
| 320 |
function lz_get_page($get = 'page', $resperpage = 50){ |
| 321 |
|
| 322 |
$resperpage = (!empty($_REQUEST['reslen']) && is_numeric($_REQUEST['reslen']) ? (int) lz_optreq('reslen') : $resperpage); |
| 323 |
|
| 324 |
if(lz_optget($get)){ |
| 325 |
$pg = (int) lz_optget($get); |
| 326 |
$pg = $pg - 1; |
| 327 |
$page = ($pg * $resperpage); |
| 328 |
$page = ($page <= 0 ? 0 : $page); |
| 329 |
}else{ |
| 330 |
$page = 0; |
| 331 |
} |
| 332 |
return $page; |
| 333 |
} |
| 334 |
|
| 335 |
// Replaces the Variables with the supplied ones |
| 336 |
function lz_lang_vars_name($str, $array){ |
| 337 |
|
| 338 |
foreach($array as $k => $v){ |
| 339 |
|
| 340 |
$str = str_replace('$'.$k, $v, $str); |
| 341 |
|
| 342 |
} |
| 343 |
|
| 344 |
return $str; |
| 345 |
|
| 346 |
} |