<?php
function neat_trim($str, $n, $delim='...') {
   $len = strlen($str);
   if ($len > $n) {
       preg_match('/(.{' . $n . '}.*?)\b/', $str, $matches);
       return rtrim($matches[1]) . $delim;
   }
   else {
       return $str;
   }
}


function cleanstr($str,$uppercase=false){
	$newstr = addslashes(stripslashes(strip_tags(trim($str))));
	if($uppercase)
		$newstr = mb_strtoupper($newstr);
	return $newstr;
}

function is_mail($str){
	if(!preg_match("/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,6})$/",$str))
		return false;
	else
		return true;
}
function clean($str,$with){
	$str = ereg_replace("[^A-Za-z0-9]", "", $str);
	return $str;
}
function is_clean($str){
	if(!preg_match("/^[a-zA-Z0-9]+$/",$str))
		return false;
	else
		return true;
}
function is_password($str){
	return preg_match("/^[\w-.!@#$%]{6,20}$/iu", $str);
}

function is_date($ita_date){
	list($day,$month,$year) = explode("/",$ita_date);
	if(empty($day) || empty($month) || empty($year))
		return false;
	if(checkdate($month,$day,$year) && $year > 1970)
		return date('Y-m-d', mktime(0, 0, 0, $month, $day, $year));
	else
		return false;

}
function getDateIta($date_eng){
	return strftime('%d/%m/%Y',strtotime($date_eng));
}

function is_import($n){
	$n = str_replace(",",".",$n);
	if(!preg_match('/^[0-9]+(?:\.[0-9]{0,2})?$/im',$n)){
		return false;
	}else{
		return $n;
	}
}

function is_intnumber($n){
	if(!preg_match('/^[0-9]+$/im',$n)){
		return false;
	}else{
		return $n;
	}
}

function date_ita($eng_date){
	list($yyyy,$mm,$dd) = explode("-",substr($eng_date,0,10));
	if(!checkdate((int)$mm,(int)$dd,(int)$yyyy))
		return NULL;
	else
		return strftime('%d/%m/%Y', strtotime($eng_date));
}

function import_ita($eng_import){
	return number_format($eng_import,2,',','.');
}


function generatePassword($length=8, $strength=0) {
	$vowels = 'aeuy';
	$consonants = 'bdghjmnpqrstvz23456789';

	$password = '';
	$alt = time() % 2;
	for ($i = 0; $i < $length; $i++) {
		if ($alt == 1) {
			$password .= $consonants[(rand() % strlen($consonants))];
			$alt = 0;
		} else {
			$password .= $vowels[(rand() % strlen($vowels))];
			$alt = 1;
		}
	}
	return $password;
}

function naviga($url,$tot,$nstart,$nris){

	if(!empty($_REQUEST)){
		foreach($_REQUEST as $k=>$v)
			if($k <> 'from')
				$qs[] = $k.'='.$v;
		if($qs)
			$qs = implode("&",$qs);
	}

	if(!empty($qs))
		$qs .= '&';

	$url .= '?'.$qs;

	$npag = ceil($tot / $nris);	//numero totale pagine

	$vis_da = intval($nstart);
	$vis_a = $vis_da + $nris -1;
  $go_back = '';
  $go_forward = '';

	if($vis_da > 1){
		$go_back = '<a title="indietro" href="'.$url.'from='. ($vis_da-$nris) .'">&laquo; indietro</a>';
	}


	if(($vis_a+1) >= $tot){
		$vis_a = $tot;
	}else{
		$vis_a = $vis_da + $nris;
		$go_forward = '<a title="avanti" href="'.$url.'from='.$vis_a.'">avanti &raquo;</a>';
	}


	$navigation = '<div class="naviga">visualizzati '.($vis_da+1).' - '.$vis_a.' di '.$tot.' '.$go_back.' '.$go_forward.'</div>';

	return $navigation;
}

?>
