File: D:/HostingSpaces/SBogers10/ilysium.komma.pro/wwwroot/lib/general/functions.class.php
<?php
/**
* functions.class.php
* Created by Komma Mediadesign.
* Author: mike
* Date: 3/20/13
*/
class Fn
{
public function __construct(){ }
// Converts an string so it can be used in the url
public static function encodeUrl($input)
{
// Remove whitespace
$output = trim($input);
// Lowercase
$output = strtolower($output);
// Replace & with "en" or "and"
switch(URL_LANG)
{
case 'en':
$output = str_replace('&', 'and', $output);
break;
case 'nl':
$output = str_replace('&', 'en', $output);
break;
case 'de':
$output = str_replace('&', 'und', $output);
break;
default:
$output = str_replace('&', 'and', $output);
}
// Replace special letters with normal letters
$output = preg_replace( "`&([a-z])(acute|uml|circ|grave|ring|cedil|slash|tilde|caron|lig|quot|rsquo);`i","\\1", $output );
// Remove html entities
$pattern = '#(&)([a-z]*)([;$])#';
$output = preg_replace($pattern,'',$output);
// Remove all special characters
$output = preg_replace('/[^a-zA-Z0-9_ %\[\]\.\(\)%&-]/s', '', $output);
// Remove ( )
$output = str_replace('(','',$output);
$output = str_replace(')','',$output);
$output = str_replace('.','',$output);
// Replace spaces with a dash
$output = str_replace(' ','-',$output);
// Replace multiple dashes with one dash
$output = preg_replace('/-{2,}/','-',$output);
return $output;
}
/*
* Headers a 404 error
*/
public static function notFound()
{
header("HTTP/1.0 404 Not Found");
header('location:' . LANG_ROOT . '404/');
exit;
}
/*
* Redirect to a page
*/
public static function redirect($url)
{
header('location:' . $url);
exit;
}
/*
* Return a camelCase string
*/
public static function camelCase($label)
{
$label = str_replace('-',' ',$label);
$label = str_replace('_',' ',$label);
$words = explode(' ',$label);
$output = '';
foreach($words as $i => $word)
{
$word = strtolower($word);
if($i != 0) $word = ucfirst($word);
$output .= $word;
}
return $output;
}
/*
* Check if a string can be an e-mail address.
*/
public static function checkMail($email) {
// First, we check that there's one @ symbol, and that the lengths are right
if (!preg_match("/^[^@]{1,64}@[^@]{1,255}$/", $email)) {
// Email invalid because wrong number of characters in one section, or wrong number of @ symbols.
return false;
}
// Split it into sections to make life easier
$email_array = explode("@", $email);
$local_array = explode(".", $email_array[0]);
for ($i = 0; $i < sizeof($local_array); $i++) {
if (!preg_match("/^(([A-Za-z0-9!#$%&'*+\/=?^_`{|}~-][A-Za-z0-9!#$%&'*+\/=?^_`{|}~\.-]{0,63})|(\"[^(\\|\")]{0,62}\"))$/", $local_array[$i])) {
return false;
}
}
if (!preg_match("/^\[?[0-9\.]+\]?$/", $email_array[1])) { // Check if domain is IP. If not, it should be valid domain name
$domain_array = explode(".", $email_array[1]);
if (sizeof($domain_array) < 2) {
return false; // Not enough parts to domain
}
for ($i = 0; $i < sizeof($domain_array); $i++) {
if (!preg_match("/^(([A-Za-z0-9][A-Za-z0-9-]{0,61}[A-Za-z0-9])|([A-Za-z0-9]+))$/", $domain_array[$i])) {
return false;
}
}
}
return true;
}
/*
* Print
*/
public static function pre_print($array)
{
if($_SERVER['REMOTE_ADDR'] == '212.61.130.133')
{
echo '<pre>';
print_r($array);
echo '</pre>';
exit;
}
}
/*
* Checks whether input is a 2-dimensional or 1-dimensional array
* If 1-dimensional it gets converted to a 2-dimensional array
*/
public static function convert2D($array)
{
if( ! is_array($array[key($array)])) $array = array($array);
return $array;
}
public static function detectMobile($justMobileOrTablet = '')
{
$browser['mobile'] = 0;
$browser['tablet'] = 0;
// Detect tablet
if (preg_match('/(tablet|ipad|playbook)|(android(?!.*(mobi|opera mini)))/i', strtolower($_SERVER['HTTP_USER_AGENT']))) {
$browser['tablet']++;
}
// Detect mobile
if (preg_match('/(up.browser|up.link|mmp|symbian|smartphone|midp|wap|phone|android|iemobile)/i', strtolower($_SERVER['HTTP_USER_AGENT']))) {
$browser['mobile']++;
}
if ((strpos(strtolower($_SERVER['HTTP_ACCEPT']),'application/vnd.wap.xhtml+xml') > 0) or ((isset($_SERVER['HTTP_X_WAP_PROFILE']) or isset($_SERVER['HTTP_PROFILE'])))) {
$browser['mobile']++;
}
$mobile_ua = strtolower(substr($_SERVER['HTTP_USER_AGENT'], 0, 4));
$mobile_agents = array(
'w3c ','acs-','alav','alca','amoi','audi','avan','benq','bird','blac',
'blaz','brew','cell','cldc','cmd-','dang','doco','eric','hipt','inno',
'ipaq','java','jigs','kddi','keji','leno','lg-c','lg-d','lg-g','lge-',
'maui','maxo','midp','mits','mmef','mobi','mot-','moto','mwbp','nec-',
'newt','noki','palm','pana','pant','phil','play','port','prox',
'qwap','sage','sams','sany','sch-','sec-','send','seri','sgh-','shar',
'sie-','siem','smal','smar','sony','sph-','symb','t-mo','teli','tim-',
'tosh','tsm-','upg1','upsi','vk-v','voda','wap-','wapa','wapi','wapp',
'wapr','webc','winw','winw','xda ','xda-');
if (in_array($mobile_ua,$mobile_agents)) {
$browser['mobile']++;
}
if (strpos(strtolower($_SERVER['HTTP_USER_AGENT']),'opera mini') > 0) {
$browser['mobile']++;
//Check for tablets on opera mini alternative headers
$stock_ua = strtolower(isset($_SERVER['HTTP_X_OPERAMINI_PHONE_UA'])?$_SERVER['HTTP_X_OPERAMINI_PHONE_UA']:(isset($_SERVER['HTTP_DEVICE_STOCK_UA'])?$_SERVER['HTTP_DEVICE_STOCK_UA']:''));
if (preg_match('/(tablet|ipad|playbook)|(android(?!.*mobile))/i', $stock_ua)) {
$browser['tablet']++;
}
}
if(empty($justMobileOrTablet))
{
return $browser['mobile'] || $browser['tablet'];
}
else
{
return $browser[$justMobileOrTablet];
}
}
}