Created
December 30, 2018 20:28
-
-
Save smarteist/62487e47d06332e4356184278cf8d961 to your computer and use it in GitHub Desktop.
php utils
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class utils | |
{ | |
public static function get_request_headers() | |
{ | |
$headers = array(); | |
foreach ($_SERVER as $name => $value) { | |
if (substr($name, 0, 5) == 'HTTP_') { | |
$name = str_replace(' ', '-', ucwords(strtolower(str_replace('_', ' ', substr($name, 5))))); | |
$headers[$name] = $value; | |
} else if ($name == "CONTENT_TYPE") { | |
$headers["Content-Type"] = $value; | |
} else if ($name == "CONTENT_LENGTH") { | |
$headers["Content-Length"] = $value; | |
} | |
} | |
return $headers; | |
} | |
public static function getPostBodyArray() | |
{ | |
return json_decode(self::getPostBodyString, true); | |
} | |
public static function getPostBodyString() | |
{ | |
return strval(file_get_contents('php://input')); | |
} | |
public static function getHashCode($length) | |
{ | |
$hexStr = ""; | |
for ($i = 0; $i < $length; $i++) { | |
$hexStr .= dechex(rand(0, 15)); | |
} | |
return $hexStr; | |
} | |
public static function sanitize($input) | |
{ | |
$search = array( | |
'@<script[^>]*?>.*?</script>@si', // Strip out javascript | |
'@<[\/\!]*?[^<>]*?>@si', // Strip out HTML tags | |
'@<![\s\S]*?--[ \t\n\r]*>@' // Strip multi-line comments | |
); | |
if (is_array($input)) { | |
foreach ($input as $var => $val) { | |
$output[$var] = sanitize($val); | |
} | |
} else { | |
if (get_magic_quotes_gpc()) { | |
$input = stripslashes($input); | |
} | |
$output = preg_replace($search, '', $input); | |
} | |
return $output; | |
} | |
public static function convertToPersianNumbers($text) | |
{ | |
$persianNumbers = array('۰', '۱', '۲', '۳', '۴', '۵', '۶', '۷', '۸', '۹'); | |
$latinNumbers = array('0', '1', '2', '3', '4', '5', '6', '7', '8', '9'); | |
return str_replace($latinNumbers, $persianNumbers, $text); | |
} | |
public static function getCurrentUrl() | |
{ | |
$protocol = strpos(strtolower($_SERVER['SERVER_PROTOCOL']), 'https') === false ? 'http' : 'https'; | |
$host = $_SERVER['HTTP_HOST']; | |
$script = $_SERVER['SCRIPT_NAME']; | |
$params = $_SERVER['QUERY_STRING'] == '' ? '' : '?' . $_SERVER['QUERY_STRING']; | |
return $protocol . '://' . $host . $script . $params; | |
} | |
public static function getWPCurrentUrl() | |
{ | |
$url = get_permalink(); | |
$params = $_SERVER['QUERY_STRING'] == '' ? '' : '?' . $_SERVER['QUERY_STRING']; | |
return $url . $params; | |
} | |
public static function getBaseUrl() | |
{ | |
$protocol = strpos(strtolower($_SERVER['SERVER_PROTOCOL']), 'https') === false ? 'http' : 'https'; | |
$host = $_SERVER['HTTP_HOST']; | |
return $protocol . '://' . $host . '/'; | |
} | |
public static function getUrlQuery($url) | |
{ | |
$vars = []; | |
parse_str(parse_url($url)['query'], $vars); | |
return $vars; | |
} | |
public static function getAllPhpFiles($dir) | |
{ | |
$files = array(); | |
foreach (new RecursiveIteratorIterator(new RecursiveDirectoryIterator($dir)) as $filename) { | |
if (is_file($filename) && pathinfo($filename, PATHINFO_EXTENSION) == 'php') { | |
$files[] = strval($filename); | |
} | |
} | |
return $files; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment