| 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 |
function lz_inputsec($string){ |
| 67 |
|
| 68 |
if(!get_magic_quotes_gpc()){ |
| 69 |
|
| 70 |
$string = addslashes($string); |
| 71 |
|
| 72 |
}else{ |
| 73 |
|
| 74 |
$string = stripslashes($string); |
| 75 |
$string = addslashes($string); |
| 76 |
|
| 77 |
} |
| 78 |
|
| 79 |
// This is to replace ` which can cause the command to be executed in exec() |
| 80 |
$string = str_replace('`', '\`', $string); |
| 81 |
|
| 82 |
return $string; |
| 83 |
|
| 84 |
} |
| 85 |
|
| 86 |
function lz_htmlizer($string){ |
| 87 |
|
| 88 |
$string = htmlentities($string, ENT_QUOTES, 'UTF-8'); |
| 89 |
|
| 90 |
$string = preg_replace('/(&#(\d{1,7}|x[0-9a-fA-F]{1,6});)/e', 'entity_check(\\2);', $string); |
| 91 |
|
| 92 |
return $string; |
| 93 |
|
| 94 |
} |
| 95 |
|
| 96 |
// Check if a checkbox is selected |
| 97 |
function lz_is_checked($post){ |
| 98 |
|
| 99 |
if(!empty($_POST[$post])){ |
| 100 |
return true; |
| 101 |
} |
| 102 |
return false; |
| 103 |
} |
| 104 |
|
| 105 |
// Reoort an error |
| 106 |
function lz_report_error($error = array()){ |
| 107 |
|
| 108 |
if(empty($error)){ |
| 109 |
return true; |
| 110 |
} |
| 111 |
|
| 112 |
$error_string = '<b>Please fix the below error(s) :</b> <br />'; |
| 113 |
|
| 114 |
foreach($error as $ek => $ev){ |
| 115 |
$error_string .= '* '.$ev.'<br />'; |
| 116 |
} |
| 117 |
|
| 118 |
echo '<div id="message" class="error"><p>' |
| 119 |
. __($error_string, 'loginizer') |
| 120 |
. '</p></div>'; |
| 121 |
} |
| 122 |
|
| 123 |
// Report a notice |
| 124 |
function lz_report_notice($notice = array()){ |
| 125 |
|
| 126 |
global $wp_version; |
| 127 |
|
| 128 |
if(empty($notice)){ |
| 129 |
return true; |
| 130 |
} |
| 131 |
|
| 132 |
// Which class do we have to use ? |
| 133 |
if(version_compare($wp_version, '3.8', '<')){ |
| 134 |
$notice_class = 'updated'; |
| 135 |
}else{ |
| 136 |
$notice_class = 'updated'; |
| 137 |
} |
| 138 |
|
| 139 |
$notice_string = '<b>Please check the below notice(s) :</b> <br />'; |
| 140 |
|
| 141 |
foreach($notice as $ek => $ev){ |
| 142 |
$notice_string .= '* '.$ev.'<br />'; |
| 143 |
} |
| 144 |
|
| 145 |
echo '<div id="message" class="'.$notice_class.'"><p>' |
| 146 |
. __($notice_string, 'loginizer') |
| 147 |
. '</p></div>'; |
| 148 |
} |
| 149 |
|
| 150 |
// Convert an objext to array |
| 151 |
function lz_objectToArray($d){ |
| 152 |
|
| 153 |
if(is_object($d)){ |
| 154 |
$d = get_object_vars($d); |
| 155 |
} |
| 156 |
|
| 157 |
if(is_array($d)){ |
| 158 |
return array_map(__FUNCTION__, $d); // recursive |
| 159 |
}elseif(is_object($d)){ |
| 160 |
return lz_objectToArray($d); |
| 161 |
}else{ |
| 162 |
return $d; |
| 163 |
} |
| 164 |
} |
| 165 |
|
| 166 |
// Sanitize variables |
| 167 |
function lz_sanitize_variables($variables = array()){ |
| 168 |
|
| 169 |
if(is_array($variables)){ |
| 170 |
foreach($variables as $k => $v){ |
| 171 |
$variables[$k] = trim($v); |
| 172 |
$variables[$k] = escapeshellcmd($v); |
| 173 |
} |
| 174 |
}else{ |
| 175 |
$variables = escapeshellcmd(trim($variables)); |
| 176 |
} |
| 177 |
|
| 178 |
return $variables; |
| 179 |
} |
| 180 |
|
| 181 |
// Is multisite ? |
| 182 |
function lz_is_multisite() { |
| 183 |
|
| 184 |
if(function_exists('get_site_option') && function_exists('is_multisite') && is_multisite()){ |
| 185 |
return true; |
| 186 |
} |
| 187 |
|
| 188 |
return false; |
| 189 |
} |
| 190 |
|
| 191 |
|