| 1 |
<?php |
| 2 |
|
| 3 |
// Get the client IP |
| 4 |
function lz_getip(){ |
| 5 |
if(isset($_SERVER["REMOTE_ADDR"])){ |
| 6 |
return $_SERVER["REMOTE_ADDR"]; |
| 7 |
}elseif(isset($_SERVER["HTTP_X_FORWARDED_FOR"])){ |
| 8 |
return $_SERVER["HTTP_X_FORWARDED_FOR"]; |
| 9 |
}elseif(isset($_SERVER["HTTP_CLIENT_IP"])){ |
| 10 |
return $_SERVER["HTTP_CLIENT_IP"]; |
| 11 |
} |
| 12 |
} |
| 13 |
|
| 14 |
// Execute a select query and return an array |
| 15 |
function lz_selectquery($query, $array = 0){ |
| 16 |
global $wpdb; |
| 17 |
|
| 18 |
$result = $wpdb->get_results($query, 'ARRAY_A'); |
| 19 |
|
| 20 |
if(empty($array)){ |
| 21 |
return current($result); |
| 22 |
}else{ |
| 23 |
return $result; |
| 24 |
} |
| 25 |
} |
| 26 |
|
| 27 |
// Check if an IP is valid |
| 28 |
function lz_valid_ip($ip){ |
| 29 |
|
| 30 |
if(!ip2long($ip)){ |
| 31 |
return false; |
| 32 |
} |
| 33 |
return true; |
| 34 |
} |
| 35 |
|
| 36 |
// Check if a field is posted via POST else return default value |
| 37 |
function lz_optpost($name, $default = ''){ |
| 38 |
|
| 39 |
if(!empty($_POST[$name])){ |
| 40 |
return lz_inputsec(lz_htmlizer(trim($_POST[$name]))); |
| 41 |
} |
| 42 |
|
| 43 |
return $default; |
| 44 |
} |
| 45 |
|
| 46 |
// Check if a field is posted via GET else return default value |
| 47 |
function lz_optget($name, $default = ''){ |
| 48 |
|
| 49 |
if(!empty($_GET[$name])){ |
| 50 |
return lz_inputsec(lz_htmlizer(trim($_GET[$name]))); |
| 51 |
} |
| 52 |
|
| 53 |
return $default; |
| 54 |
} |
| 55 |
|
| 56 |
// Check if a field is posted via GET or POST else return default value |
| 57 |
function lz_optreq($name, $default = ''){ |
| 58 |
|
| 59 |
if(!empty($_REQUEST[$name])){ |
| 60 |
return lz_inputsec(lz_htmlizer(trim($_REQUEST[$name]))); |
| 61 |
} |
| 62 |
|
| 63 |
return $default; |
| 64 |
} |
| 65 |
|
| 66 |
// For filling in posted values |
| 67 |
function lz_POSTval($name, $default = ''){ |
| 68 |
|
| 69 |
return (!empty($_POST) ? (!isset($_POST[$name]) ? '' : $_POST[$name]) : $default); |
| 70 |
|
| 71 |
} |
| 72 |
|
| 73 |
function lz_POSTchecked($name, $default = false){ |
| 74 |
|
| 75 |
return (!empty($_POST) ? (isset($_POST[$name]) ? 'checked="checked"' : '') : (!empty($default) ? 'checked="checked"' : '')); |
| 76 |
|
| 77 |
} |
| 78 |
|
| 79 |
function lz_POSTselect($name, $value, $default = false){ |
| 80 |
|
| 81 |
if(empty($_POST)){ |
| 82 |
if(!empty($default)){ |
| 83 |
return 'selected="selected"'; |
| 84 |
} |
| 85 |
}else{ |
| 86 |
if(isset($_POST[$name])){ |
| 87 |
if(trim($_POST[$name]) == $value){ |
| 88 |
return 'selected="selected"'; |
| 89 |
} |
| 90 |
} |
| 91 |
} |
| 92 |
|
| 93 |
} |
| 94 |
|
| 95 |
function lz_inputsec($string){ |
| 96 |
|
| 97 |
if(!get_magic_quotes_gpc()){ |
| 98 |
|
| 99 |
$string = addslashes($string); |
| 100 |
|
| 101 |
}else{ |
| 102 |
|
| 103 |
$string = stripslashes($string); |
| 104 |
$string = addslashes($string); |
| 105 |
|
| 106 |
} |
| 107 |
|
| 108 |
// This is to replace ` which can cause the command to be executed in exec() |
| 109 |
$string = str_replace('`', '\`', $string); |
| 110 |
|
| 111 |
return $string; |
| 112 |
|
| 113 |
} |
| 114 |
|
| 115 |
function lz_htmlizer($string){ |
| 116 |
|
| 117 |
$string = htmlentities($string, ENT_QUOTES, 'UTF-8'); |
| 118 |
|
| 119 |
preg_match_all('/(&#(\d{1,7}|x[0-9a-fA-F]{1,6});)/', $string, $matches);//r_print($matches); |
| 120 |
|
| 121 |
foreach($matches[1] as $mk => $mv){ |
| 122 |
$tmp_m = lz_entity_check($matches[2][$mk]); |
| 123 |
$string = str_replace($matches[1][$mk], $tmp_m, $string); |
| 124 |
} |
| 125 |
|
| 126 |
return $string; |
| 127 |
|
| 128 |
} |
| 129 |
|
| 130 |
function lz_entity_check($string){ |
| 131 |
|
| 132 |
//Convert Hexadecimal to Decimal |
| 133 |
$num = ((substr($string, 0, 1) === 'x') ? hexdec(substr($string, 1)) : (int) $string); |
| 134 |
|
| 135 |
//Squares and Spaces - return nothing |
| 136 |
$string = (($num > 0x10FFFF || ($num >= 0xD800 && $num <= 0xDFFF) || $num < 0x20) ? '' : '&#'.$num.';'); |
| 137 |
|
| 138 |
return $string; |
| 139 |
|
| 140 |
} |
| 141 |
|
| 142 |
// Check if a checkbox is selected |
| 143 |
function lz_is_checked($post){ |
| 144 |
|
| 145 |
if(!empty($_POST[$post])){ |
| 146 |
return true; |
| 147 |
} |
| 148 |
return false; |
| 149 |
} |
| 150 |
|
| 151 |
// Reoort an error |
| 152 |
function lz_report_error($error = array()){ |
| 153 |
|
| 154 |
if(empty($error)){ |
| 155 |
return true; |
| 156 |
} |
| 157 |
|
| 158 |
$error_string = '<b>Please fix the below error(s) :</b> <br />'; |
| 159 |
|
| 160 |
foreach($error as $ek => $ev){ |
| 161 |
$error_string .= '* '.$ev.'<br />'; |
| 162 |
} |
| 163 |
|
| 164 |
echo '<div id="message" class="error"><p>' |
| 165 |
. __($error_string, 'loginizer') |
| 166 |
. '</p></div>'; |
| 167 |
} |
| 168 |
|
| 169 |
// Report a notice |
| 170 |
function lz_report_notice($notice = array()){ |
| 171 |
|
| 172 |
global $wp_version; |
| 173 |
|
| 174 |
if(empty($notice)){ |
| 175 |
return true; |
| 176 |
} |
| 177 |
|
| 178 |
// Which class do we have to use ? |
| 179 |
if(version_compare($wp_version, '3.8', '<')){ |
| 180 |
$notice_class = 'updated'; |
| 181 |
}else{ |
| 182 |
$notice_class = 'updated'; |
| 183 |
} |
| 184 |
|
| 185 |
$notice_string = '<b>Please check the below notice(s) :</b> <br />'; |
| 186 |
|
| 187 |
foreach($notice as $ek => $ev){ |
| 188 |
$notice_string .= '* '.$ev.'<br />'; |
| 189 |
} |
| 190 |
|
| 191 |
echo '<div id="message" class="'.$notice_class.'"><p>' |
| 192 |
. __($notice_string, 'loginizer') |
| 193 |
. '</p></div>'; |
| 194 |
} |
| 195 |
|
| 196 |
// Convert an objext to array |
| 197 |
function lz_objectToArray($d){ |
| 198 |
|
| 199 |
if(is_object($d)){ |
| 200 |
$d = get_object_vars($d); |
| 201 |
} |
| 202 |
|
| 203 |
if(is_array($d)){ |
| 204 |
return array_map(__FUNCTION__, $d); // recursive |
| 205 |
}elseif(is_object($d)){ |
| 206 |
return lz_objectToArray($d); |
| 207 |
}else{ |
| 208 |
return $d; |
| 209 |
} |
| 210 |
} |
| 211 |
|
| 212 |
// Sanitize variables |
| 213 |
function lz_sanitize_variables($variables = array()){ |
| 214 |
|
| 215 |
if(is_array($variables)){ |
| 216 |
foreach($variables as $k => $v){ |
| 217 |
$variables[$k] = trim($v); |
| 218 |
$variables[$k] = escapeshellcmd($v); |
| 219 |
} |
| 220 |
}else{ |
| 221 |
$variables = escapeshellcmd(trim($variables)); |
| 222 |
} |
| 223 |
|
| 224 |
return $variables; |
| 225 |
} |
| 226 |
|
| 227 |
// Is multisite ? |
| 228 |
function lz_is_multisite() { |
| 229 |
|
| 230 |
if(function_exists('get_site_option') && function_exists('is_multisite') && is_multisite()){ |
| 231 |
return true; |
| 232 |
} |
| 233 |
|
| 234 |
return false; |
| 235 |
} |
| 236 |
|
| 237 |
// Generate a random string |
| 238 |
function lz_RandomString($length = 10){ |
| 239 |
$characters = '0123456789abcdefghijklmnopqrstuvwxyz'; |
| 240 |
$charactersLength = strlen($characters); |
| 241 |
$randomString = ''; |
| 242 |
for($i = 0; $i < $length; $i++){ |
| 243 |
$randomString .= $characters[rand(0, $charactersLength - 1)]; |
| 244 |
} |
| 245 |
return $randomString; |
| 246 |
} |
| 247 |
|
| 248 |
|
| 249 |
|