<?php
/*
* Ideally this module should including as few functions as possible.
* Functions should mostly be hooks to be included into modules.
*/
//require_once("minify.inc");
/**
* Return first occurrence of HTML / XML element in a string.
* @param $string
* @param $tag
* @return string
*/
function parsetag($string, $tag) {
$pattern = "/<" . $tag . ">(.*?)<\/" . $tag . ">/s";
preg_match_all($pattern, $string, $matches);
return isset($matches[1][0]) ? $matches[1][0] : '';
}
/**
* Generate a password.
*
* @param $pass
* @return bool|string
*/
function pwd($pass) {
return substr((md5(substr($pass, 0, 5) . 'ABC' . substr($pass, 5, 2) . 'nginE')) . md5($pass), 0, 50);
}
/**
* Redirect the current user to another page.
* // TODO: make this stuff better.
*
* @param string $where
* Where to redirect the user.
*/
function redirect($where) {
print '<script>top.location.href="' . $where . '";</script>';
die();
}
/**
* Check if a string is alphanumeric.
*
* @param $string
* The string.
*
* @return boolean
* TRUE if the string is alphanumeric.
*/
function is_alphanumeric($string) {
return preg_match('/^[A-Za-z0-9_]+$/', $string);
}
/**
* Check if a string represents a valid email address.
*
* @param $email
* The email address to check.
*
* @return bool
* TRUE if the argument is a valid email address.
*/
function valid_email($email) {
return (!filter_var($email, FILTER_VALIDATE_EMAIL) === FALSE);
}
/**
* Check if a string represents a valid URL.
*
* @param $url
* The URL to check.
*
* @return bool
* Returns True if the URL is valid.
*/
function valid_url($url) {
return (!filter_var($url, FILTER_VALIDATE_URL) === FALSE);
}
/**
* Normalize a string by removing all quanta-related characters.
*
* @param string $string
* The string to normalize.
*
* @return string
* The normalized string.
*/
function string_normalize($string, $nl2br = FALSE) {
if (!is_string($string)) {
return '';
}
$string = str_replace('[', '[', $string);
$string = str_replace(']', ']', $string);
$string = str_replace('{', '{', $string);
$string = str_replace('}', '}', $string);
$string = str_replace(':', ':', $string);
$string = str_replace('|', '|', $string);
if ($nl2br) {
$string = nl2br($string);
}
// Remove tabs and newlines.
// TODO: was necessary as it broke INPUT tags by weirdly printing the value...
return preg_replace('~[\r\n\t]+~', '', $string);
}
/**
* Normalize a path by replacing all special characters.
*
* @param string $s
* The string to normalize.
*
* @return string
* The normalized string.
*/
function normalizePath($s) {
$s = remove_accents($s);
$s = strtolower($s);
$s = str_replace(' ', '-', $s);
$s = str_replace('_', '-', $s);
$s = preg_replace("/[^A-Za-z0-9\-]/", '', $s);;
while (strpos($s, '--') > 0) {
$s = str_replace('--', '-', $s);
}
$s = trim($s, '-');
return $s;
}
/**
* Normalize a path by replacing all special characters for files.
*
* @param string $f
* The file to normalize.
*
* @return string
* The normalized file.
*/
function normalizeFilePath($f) {
$file_arr = explode('.', $f);
$file_ext = array_pop($file_arr);
$file_name = implode('.', $file_arr);
$file_name = normalizePath($file_name);
return $file_name . '.' . $file_ext;
}
/**
* Converts all accent characters to ASCII characters.
*
* If there are no accent characters, then the string given is just returned.
*
* @param string $string
* Text that might have accent characters
*
* @return string
* Filtered string with replaced "nice" characters.
*/
function remove_accents($string) {
$unwanted_array = array(
'?'=>'','?'=>'','?'=>'','?'=>'','?'=>'','?' => '',
'Á'=>'a','á'=>'a','À'=>'a','à'=>'a','?'=>'a','?'=>'a','?'=>'a','?'=>'A','?'=>'A',
'?'=>'a','?'=>'a','?'=>'A','?'=>'a','?'=>'A','Â'=>'a','â'=>'a','?'=>'a','?'=>'A',
'?'=>'a','?'=>'a','?'=>'a','?'=>'A','?'=>'a','?'=>'a','Å'=>'a','å'=>'a','?'=>'a',
'?'=>'a','Ä'=>'a','ä'=>'a','ã'=>'a','Ã'=>'A','?'=>'a','?'=>'a','?'=>'a','?'=>'a',
'?'=>'a','?'=>'a','?'=>'A','?'=>'a','?'=>'a','?'=>'A','?'=>'a','?'=>'A','Æ'=>'ae',
'æ'=>'ae','?'=>'ae','?'=>'ae','?'=>'a','?'=>'A',
'?'=>'c','?'=>'c','?'=>'c','?'=>'c','?'=>'c','?'=>'c','?'=>'c','?'=>'c','Ç'=>'c','ç'=>'c',
'?'=>'d','?'=>'d','?'=>'D','?'=>'d','?'=>'d','?'=>'d','?'=>'D','?'=>'d','?'=>'D','?'=>'d','ð'=>'d','Ð'=>'D',
'É'=>'e','é'=>'e','È'=>'e','è'=>'e','?'=>'e','?'=>'e','ê'=>'e','?'=>'e','?'=>'E','?'=>'e',
'?'=>'E','?'=>'e','?'=>'e','Ë'=>'e','ë'=>'e','?'=>'e','?'=>'e','?'=>'e','?'=>'e','?'=>'e',
'?'=>'e','?'=>'e','?'=>'E','?'=>'e','?'=>'e','?'=>'e','?'=>'E','?'=>'e',
'?'=>'E','?'=>'e','?'=>'E','?'=>'e','?'=>'E','?'=>'e','?'=>'E',
'?'=>'f',
'?'=>'g','?'=>'g','?'=>'g','?'=>'g','?'=>'G','?'=>'g','?'=>'g','?'=>'g','?'=>'g','?'=>'g',
'H?'=>'H','h?'=>'h','?'=>'h','?'=>'h','?'=>'H','?'=>'h','?'=>'H','?'=>'h','?'=>'h','?'=>'h','?'=>'H','?'=>'h',
'?'=>'I','Í'=>'i','í'=>'i','Ì'=>'i','ì'=>'i','?'=>'i','?'=>'i','Î'=>'i','î'=>'i','?'=>'i','?'=>'i',
'Ï'=>'i','ï'=>'i','?'=>'I','?'=>'i','?'=>'i','?'=>'i','?'=>'i','?'=>'i','?'=>'i','?'=>'i','?'=>'i',
'?'=>'I','?'=>'I','?'=>'i','?'=>'ij','?'=>'ij','?'=>'i',
'?'=>'j','?'=>'j',
'?'=>'k','?'=>'k','?'=>'K','?'=>'k',
'?'=>'l','?'=>'l','?'=>'l','?'=>'l','?'=>'l','?'=>'l','?'=>'l','?'=>'l','?'=>'l','?'=>'l',
'?'=>'n','?'=>'n','?'=>'n','?'=>'n','Ñ'=>'N','ñ'=>'n','?'=>'n','?'=>'n','?'=>'N','?'=>'n','?'=>'n','?'=>'n',
'Ó'=>'o','ó'=>'o','Ò'=>'o','ò'=>'o','?'=>'o','?'=>'o','Ô'=>'o','ô'=>'o','?'=>'o','?'=>'O','?'=>'o',
'?'=>'O','?'=>'o','?'=>'O','?'=>'o','?'=>'o','Ö'=>'o','ö'=>'o','?'=>'o','?'=>'o','Õ'=>'o','õ'=>'o',
'Ø'=>'o','ø'=>'o','?'=>'o','?'=>'o','?'=>'O','?'=>'o','?'=>'O','?'=>'o','?'=>'o','?'=>'o','?'=>'o',
'?'=>'O','?'=>'o','?'=>'o','?'=>'o','?'=>'O','?'=>'o','?'=>'O','?'=>'o','?'=>'O','?'=>'o','?'=>'O',
'?'=>'o','?'=>'O','?'=>'o','?'=>'O','?'=>'o','?'=>'O','?'=>'o','?'=>'O',
'?'=>'oe','?'=>'oe',
'?'=>'k',
'?'=>'r','?'=>'r','?'=>'r','?'=>'r','?'=>'r','?'=>'r','?'=>'r','?'=>'R','?'=>'r','?'=>'R','?'=>'r',
'S?'=>'S','s?'=>'s','?'=>'s','?'=>'s','?'=>'s','?'=>'s','?'=>'s','?'=>'s','?'=>'s','?'=>'s',
'?'=>'S','?'=>'s','?'=>'S','?'=>'s',
'?'=>'z','ß'=>'ss','?'=>'t','?'=>'t','?'=>'t','?'=>'t','?'=>'T','?'=>'t','?'=>'T',
'?'=>'t','?'=>'T','?'=>'t','?'=>'tm','?'=>'t','?'=>'t',
'Ú'=>'u','ú'=>'u','Ù'=>'u','ù'=>'u','?'=>'u','?'=>'u','Û'=>'u','û'=>'u','?'=>'u','?'=>'u','?'=>'u','?'=>'u',
'Ü'=>'u','ü'=>'u','?'=>'u','?'=>'u','?'=>'u','?'=>'u','?'=>'u','?'=>'u','?'=>'u','?'=>'u','?'=>'u','?'=>'u',
'?'=>'u','?'=>'u','?'=>'u','?'=>'u','?'=>'u','?'=>'u','?'=>'u','?'=>'u','?'=>'u','?'=>'U','?'=>'u','?'=>'U',
'?'=>'u','?'=>'U','?'=>'u','?'=>'U','?'=>'u','?'=>'U','?'=>'u','?'=>'U','?'=>'u','?'=>'U',
'?'=>'w','?'=>'w',
'Ý'=>'y','ý'=>'y','?'=>'y','?'=>'Y','?'=>'y','?'=>'y','ÿ'=>'y','?'=>'y','?'=>'y','?'=>'Y','?'=>'y','?'=>'Y',
'Z?'=>'Z','z?'=>'z','?'=>'z','?'=>'z','?'=>'z','?'=>'z','?'=>'z','?'=>'z','?'=>'Z','?'=>'z',
'þ'=>'p','?'=>'n','?'=>'a','?'=>'a','?'=>'b','?'=>'b','?'=>'v','?'=>'v','?'=>'g','?'=>'g','?'=>'g','?'=>'g',
'?'=>'d','?'=>'d','?'=>'e','?'=>'e','?'=>'jo','?'=>'jo','?'=>'e','?'=>'e','?'=>'zh','?'=>'zh','?'=>'z','?'=>'z',
'?'=>'i','?'=>'i','?'=>'i','?'=>'i','?'=>'i','?'=>'i','?'=>'j','?'=>'j','?'=>'k','?'=>'k','?'=>'l','?'=>'l',
'?'=>'m','?'=>'m','?'=>'n','?'=>'n','?'=>'o','?'=>'o','?'=>'p','?'=>'p','?'=>'r','?'=>'r','?'=>'s','?'=>'s',
'?'=>'t','?'=>'t','?'=>'u','?'=>'u','?'=>'f','?'=>'f','?'=>'h','?'=>'h','?'=>'c','?'=>'c','?'=>'ch','?'=>'ch',
'?'=>'sh','?'=>'sh','?'=>'sch','?'=>'sch','?'=>'-',
'?'=>'-','?'=>'y','?'=>'y','?'=>'-','?'=>'-',
'?'=>'je','?'=>'je','?'=>'ju','?'=>'ju','?'=>'ja','?'=>'ja','?'=>'a','?'=>'b','?'=>'g','?'=>'d','?'=>'h','?'=>'v',
'?'=>'z','?'=>'h','?'=>'t','?'=>'i','?'=>'k','?'=>'k','?'=>'l','?'=>'m','?'=>'m','?'=>'n','?'=>'n','?'=>'s','?'=>'e',
'?'=>'p','?'=>'p','?'=>'C','?'=>'c','?'=>'q','?'=>'r','?'=>'w','?'=>'t'
);
return strtr( $string , $unwanted_array );
}
/**
* Minify a JS / CSS file.
* //TODO: move elsewhere.
*
* @param Environment $env
* The Environment.
*
* @param string $file
* The JS / CSS file.
*
* @return string
* The minified JS / CSS file.
*/
function minify($env, $file) {
$exp = explode('.', $file);
$ext = $exp[count($exp) - 1];
$command = $env->dir['vendor'] . '/matthiasmullie/minify/bin/minify' . $ext . ' ' . $file;
exec($command, $arr);
return $arr;
}
/**
* Filter a string to make it xss-safe.
*/
function filter_xss($string) {
$filtered_string = htmlspecialchars($string, ENT_QUOTES,'utf-8');
return $filtered_string;
}
/**
* Get Browser (user agent) info.
*
* @return array
* array with user agent info.
*/
function getBrowser() {
$u_agent = !empty($_SERVER['HTTP_USER_AGENT']) ? $_SERVER['HTTP_USER_AGENT'] : NULL;
if (empty($u_agent)) {
return NULL;
}
$bname = 'Unknown';
$platform = 'Unknown';
$version= "";
//First get the platform?
if (preg_match('/linux/i', $u_agent)) {
$platform = 'linux';
} elseif (preg_match('/macintosh|mac os x/i', $u_agent)) {
$platform = 'mac';
} elseif (preg_match('/windows|win32/i', $u_agent)) {
$platform = 'windows';
}
// Next get the name of the useragent yes seperately and for good reason
if(preg_match('/MSIE/i',$u_agent) && !preg_match('/Opera/i',$u_agent)) {
$bname = 'Internet Explorer';
$ub = "MSIE";
} elseif(preg_match('/Trident/i',$u_agent)) {
// this condition is for IE11
$bname = 'Internet Explorer';
$ub = "rv";
} elseif(preg_match('/Firefox/i',$u_agent)) {
$bname = 'Mozilla Firefox';
$ub = "Firefox";
} elseif(preg_match('/Chrome/i',$u_agent)) {
$bname = 'Google Chrome';
$ub = "Chrome";
} elseif(preg_match('/Safari/i',$u_agent)) {
$bname = 'Apple Safari';
$ub = "Safari";
} elseif(preg_match('/Opera/i',$u_agent)) {
$bname = 'Opera';
$ub = "Opera";
} elseif(preg_match('/Netscape/i',$u_agent)) {
$bname = 'Netscape';
$ub = "Netscape";
}
// finally get the correct version number
// Added "|:"
$known = array('Version', $ub, 'other');
$pattern = '#(?<browser>' . join('|', $known) .
')[/|: ]+(?<version>[0-9.|a-zA-Z.]*)#';
if (!preg_match_all($pattern, $u_agent, $matches)) {
// we have no matching number just continue
// TODO: useless condition?
}
// see how many we have
$i = count($matches['browser']);
if ($i != 1) {
//we will have two since we are not using 'other' argument yet
//see if version is before or after the name
if (strripos($u_agent,"Version") < strripos($u_agent,$ub)) {
$version= $matches['version'][0];
} else {
$version= $matches['version'][1];
}
} else {
$version= $matches['version'][0];
}
// check if we have a number
if ($version==null || $version=="") {
$version="?";
}
return array(
'userAgent' => $u_agent,
'name' => $bname,
'version' => $version,
'platform' => $platform,
'pattern' => $pattern
);
}
|