.
if (!defined('EG')) die('Direct access not allowed!');
function encodeUrl($url)
{
	$url = str_replace(' ','-',$url);
	$url = str_replace('[',null,$url);
	$url = str_replace(']',null,$url);
	$url = str_replace('(',null,$url);
	$url = str_replace(')',null,$url);
	$url = urlencode($url);
// 	$url = html_entity_decode($url, ENT_QUOTES);
// 	$url = xml_encode($url);
	return $url;
}
function smartDate($uglyDate = null, $lang = 'en')
{
	switch ($lang)
	{
		case 'en':
			$smDate =  date('H:i, d F Y',strtotime($uglyDate));
			break;
		default:
			$smDate = date('H:i, d F Y',strtotime($uglyDate));
	}
	return $smDate;
}
function sanitizeString($string)
{
	$string = preg_match('/^[a-zA-Z0-9\-\_\.\+\s]+$/',$string) ? sanitizeAll($string) : 'undef';
	return $string;
}
function sanitizeAlphanum($string)
{
	$string = ctype_alnum($string) ? sanitizeAll($string) : 'undef';
	return $string;
}
function getOrderByClause($string)
{
	switch ($string)
	{
		case 'last-inserted':
			$orderBy = 'hardware.id_hard desc';
			break;
		case 'alphabetically':
			$orderBy = 'model';
			break;
		case 'alphabetically-desc':
			$orderBy = 'model desc';
			break;
		case 'compatibility':
			$orderBy = 'compatibility';
			break;
		case 'undef':
			$orderBy = 'hardware.id_hard desc';
			break;
		default:
			$orderBy = 'hardware.id_hard desc';
	}
	
	return $orderBy;
}
function diff($old, $new){
		$maxlen = 0;
        foreach($old as $oindex => $ovalue){
// 				$nkeys = getNewKeys($new,$ovalue);
                $nkeys = array_keys($new, $ovalue);
                foreach($nkeys as $nindex){
                        $matrix[$oindex][$nindex] = isset($matrix[$oindex - 1][$nindex - 1]) ?
                                $matrix[$oindex - 1][$nindex - 1] + 1 : 1;
                        if($matrix[$oindex][$nindex] > $maxlen){
                                $maxlen = $matrix[$oindex][$nindex];
                                $omax = $oindex + 1 - $maxlen;
                                $nmax = $nindex + 1 - $maxlen;
                        }
                }       
        }
        if($maxlen == 0) return array(array('d'=>$old, 'i'=>$new));
        return array_merge(
                diff(array_slice($old, 0, $omax), array_slice($new, 0, $nmax)),
                array_slice($new, $nmax, $maxlen),
                diff(array_slice($old, $omax + $maxlen), array_slice($new, $nmax + $maxlen)));
}
function htmlDiff($old, $new){
		$old = str_replace("\r\n"," \r\n ",$old);
		$new = str_replace("\r\n"," \r\n ",$new);
		
		$ret = null;
        $diff = diff(explode(' ', $old), explode(' ', $new));
        foreach($diff as $k){
                if(is_array($k))
                        $ret .= (!empty($k['d'])?"".implode(' ',$k['d'])." ":'').
                                (!empty($k['i'])?"".implode(' ',$k['i'])." ":'');
                else $ret .= $k . ' ';
        }
        return $ret;
}
//a cosa serve?
function applyBreaks($values,$fields)
{
	$fieldsArray = explode(',',$fields);
	
	foreach ($fieldsArray as $field)
	{
		if (array_key_exists($field,$values))
		{
			$values[$field] = nl2br($values[$field]);
		}
	}
	return $values;
}
function getLinkToUser($user)
{
	if (strstr($user,'__'))
	{
		return str_replace('__',null,$user);
	}
	else
	{
		return "$user";
	}
}
//decode the text of the wiki
function decodeWikiText($string)
{
	
	$string = preg_replace('/(\[hr\])/', '
',$string);
	$string = preg_replace_callback('/(\[a\])(.*?)(\[\/a\])/', 'linkTo',$string);
	$string = preg_replace_callback('/(\[a\])(.*?)\|(.*?)(\[\/a\])/', 'linkToWithText',$string);
	$string = preg_replace_callback('/(\[notebook\])([0-9]*?)(\[\/notebook\])/s', 'linkToNotebook',$string);
	
	$string = preg_replace_callback('/(\[wifi\])([0-9]*?)(\[\/wifi\])/s', 'linkToWifi',$string);
	
	$string = preg_replace_callback('/(\[videocard\])([0-9]*?)(\[\/videocard\])/s', 'linkToVideocard',$string);
	
	$string = preg_replace('/(\[b\])(.*?)(\[\/b\])/s', '${2}',$string);
	
	$string = preg_replace('/(\[u\])(.*?)(\[\/u\])/s', '${2}',$string);
	
	$string = preg_replace('/(\[i\])(.*?)(\[\/i\])/s', '${2}',$string);
	
	$string = preg_replace('/(\[del\])(.*?)(\[\/del\])/s', '${2}',$string);
	
	$string = preg_replace('/(\[\*\])(.*?)(\[\/\*\])/s', '${2}',$string);
	
	$string = preg_replace('/(\[list\])(.*?)(\[\/list\])/s', '',$string);
	
	$string = preg_replace('/(\[enum\])(.*?)(\[\/enum\])/s', '${2}
',$string);
 	
	$string = preg_replace('/(\[code\])(.*?)(\[\/code\])/s', '${2}',$string);
	
	$string = preg_replace('/(\[p\])(.*?)(\[\/p\])/s', '${2}
',$string);
	
	$string = preg_replace('/(\[h1\])(.*?)(\[\/h1\])/s', '${2}
',$string);
	
	$string = preg_replace('/(\[h2\])(.*?)(\[\/h2\])/s', '${2}
',$string);
	
	$string = preg_replace('/(\[h3\])(.*?)(\[\/h3\])/s', '${2}
',$string);
	
	return $string;
}
function checkUrl($url)
{
// 	$match = '/^http\:\/\/(www\.)?[a-zA-Z0-9\-\_]+(\.[a-zA-Z0-9\-\_]+)?\.(com|net|it|info|org|eu|uk)((\/[a-zA-Z0-9\_\-\+]+)+[\/]?)?(\?([a-zA-Z0-9\_\-\+\s]+\=[a-zA-Z0-9\_\-\s\+\&]+)+)?(#[a-zA-Z0-9\_\-\+\s]+)?([\s]*)?$/';
	
	$match = '/^http\:\/\/(www\.)?[a-zA-Z0-9\-\_]+(\.[a-zA-Z0-9\-\_]+)?\.(com|net|it|info|org|eu|uk|ca)((\/[a-zA-Z0-9\_\-\+]+)*(\/([a-zA-Z0-9\_\-\.\+]+\.(php|html|htm|asp|aspx|jsp|cgi))?)?)?(\?([a-zA-Z0-9\_\-\+\s]+\=[a-zA-Z0-9\_\-\s\+\&]+)+)?(#[a-zA-Z0-9\_\-\+\s]+)?([\s]*)?$/';
	
	if (preg_match($match,$url))
	{
		return true;
	}
	else
	{
		return false;
	}
}
function vitalizeUrl($string)
{
	if (checkUrl($string))
	{
		return "".$string."";
	}
	return $string;
}
function linkTo($match)
{
	if (checkUrl($match[2]))
	{
		return "".$match[2]."";
	}
	else
	{
		return $match[0];
	}
}
function linkToWithText($match)
{
	if (checkUrl($match[2]))
	{
		
		return "".$match[3]."";
	}
	else
	{
		return $match[0];
	}
}
//create the link to the wiki page of the notebook
function linkToNotebook($match)
{
	$hardware = new HardwareModel();
	$clean['id_hard'] = (int)$match[2];
	$name = encodeUrl($hardware->getTheModelName($clean['id_hard']));
	$href = "HTTP://".DOMAIN_NAME."/notebooks/view/".Lang::$current."/".$clean['id_hard']."/$name";
	return (strcmp($name,'') !== 0) ? "".$name."" : $match[0];
}
//create the link to the wiki page of the wifi
function linkToWifi($match)
{
	$hardware = new HardwareModel();
	$clean['id_hard'] = (int)$match[2];
	$name = encodeUrl($hardware->getTheModelName($clean['id_hard']));
	$href = "HTTP://".DOMAIN_NAME."/wifi/view/".Lang::$current."/".$clean['id_hard']."/$name";
	return (strcmp($name,'') !== 0) ? "".$name."" : $match[0];
}
//create the link to the wiki page of the videocard
function linkToVideocard($match)
{
	$hardware = new HardwareModel();
	$clean['id_hard'] = (int)$match[2];
	$name = encodeUrl($hardware->getTheModelName($clean['id_hard']));
	$href = "HTTP://".DOMAIN_NAME."/videocards/view/".Lang::$current."/".$clean['id_hard']."/$name";
	return (strcmp($name,'') !== 0) ? "".$name."" : $match[0];
}
function getUserName($id_user = 0)
{
	$clean['id_user'] = (int)$id_user;
	$u = new UsersModel();
	return $u->getUser($clean['id_user']);
}
function getMotivation($row,$controller)
{
	if (strcmp($row['deletion']['object'],'duplication') === 0)
	{
		$clean['id_hard'] = (int)$row['deletion']['id_duplicate'];
		$hardware = new HardwareModel();
		$name = encodeUrl($hardware->getTheModelName($clean['id_hard']));
		return "duplication of the model having id ".$clean['id_hard']."";
	}
	else
	{
		return "".$row['deletion']['object']."";
	}
}
//get the text in the right language
function gtext($string)
{
	if (isset(Lang::$i18n[Lang::$current][$string]))
	{
		return Lang::$i18n[Lang::$current][$string];
	}
	return $string;	
}
//rewrite the vendor name
function betterVendor($string)
{
	$names = array(
		"AT-T"	=>	"AT&T",
		"Franklin-Wireless-Corporation"	=>	"Franklin Wireless Corporation",
		"Novatel-Wireless"	=>	"Novatel Wireless",
		"Sierra-Wireless"	=>	"Sierra Wireless",
	);
	
	return (array_key_exists($string,$names)) ? $names[$string] : $string;
}