<?php
/* All Right Reserved (C) 2010;
  MyTickets;
  Writed By Fahad Salem Al-ghudaier;
  faha1ad2@hotmail.com;
  www.phpx3.com;
*/
	function error($msg){
		echo '<html dir="rtl">
				<head><title>خطأ - Error - MyTickets</title>
				</head><body><center>
					<h1>حصل خطأ بالتشغيل</h1>
					<span style="font-family:Tahoma;font-size: 9pt;color: red">'.$msg.'<br /><br />
					<em style="color: #000000">إذا كنت زائراً للموقع فالرجاء منك معاودة الزيارة في وقت لاحق ونعتذر عن هذا الخلل</em>
					</span>
					<br /><br /><span style="font-family:Tahoma;color:silver;font-size:10pt">يعمل بواسطة <a href="http://phpx3.com/MyTickets.html">MyTickets</a></span>
				</center>
				</body>
			</html>
			';
		exit;
	}


	
	Function check($verb,$type,$count=0){
		if($type == 'email'){
				$patt = "^[a-zA-Z0-9_\-]+@[a-zA-Z0-9_\-]+\.[a-zA-Z0-9_\-]+$^";
				$patt2 = "^[a-zA-Z0-9_\-]+@[a-zA-Z0-9_\-]+\.[a-zA-Z0-9_\-]+\.[a-zA-Z0-9_\-]+$^";
				if(! preg_match ( $patt , $verb) AND ! preg_match ( $patt2 , $verb) ){
					return false;
				}else{
					return true;
				}

		}elseif($type == 'numeric'){
			//if(empty($verb)){ return true; }
		   if(is_numeric($verb)){ 
				return true; 
			}else{
				return false;
			}
		}elseif($type == 'count'){
			if(function_exists('mb_strlen')){
				if(mb_strlen($verb,'utf-8') < $count){ 
					return false;
				}else{
					return true;
				}
			}else{
				if(strlen($verb,'utf-8') < $count){ 
					return false;
				}else{
					return true;
				}
			}
		}elseif($type == 'url'){
			
			$urlregex .= "^(https?|ftp)\:\/\/";
			$urlregex .= "([a-z0-9+!*(),;?&=\$_.-]+(\:[a-z0-9+!*(),;?&=\$_.-]+)?@)?";
			$urlregex .= "[a-z0-9+\$_-]+(\.[a-z0-9+\$_-]+)*";
			$urlregex .= "([a-z0-9+\$_-]+\.)*[a-z0-9+\$_-]{2,3}";
			$urlregex .= "(\:[0-9]{2,5})?";
			$urlregex .= "(\/([a-z0-9+\$_-]\.?)+)*\/?";
			$urlregex .= "(\?[a-z+&\$_.-][a-z0-9;:@/&%=+\$_.-]*)?";
			$urlregex .= "(#[a-z_.-][a-z0-9+\$_.-]*)?\$^";
				if (preg_match($urlregex, $verb)) {return true; }else{ return false;}
		}
	}
	
	


	
	function redirect($url,$time=3){
		echo '<meta http-equiv = refresh content='.$time.';url='.$url.'>';
	}
	
	### I Take this from some one in php.net [wordwrap] function page ###
function breakLongWords($str, $maxLength, $char){ 
	$wordEndChars = array(" ", "\n", "\r", "\f", "\v", "\0"); 
	$count = 0; 
	$newStr = ""; 
	$openTag = false; 
	for($i=0; $i<strlen($str); $i++){ 
		$newStr .= $str{$i};    
        
		if($str{$i} == "<"){ 
		$openTag = true; 
		continue; 
		} 
		if(($openTag) && ($str{$i} == ">")){ 
		$openTag = false; 
		continue; 
		} 
        
		if(!$openTag){ 
		if(!in_array($str{$i}, $wordEndChars)){//If not word ending char 
		$count++; 
		if($count==$maxLength){//if current word max length is reached 
		$newStr .= $char;//insert word break char 
		$count = 0; 
		} 
		}else{
		$count = 0; 
		} 
		} 
        
	}
    return $newStr; 
} 



	function level_replacer($par){
	switch($par){
		case 1: return $par='مهمة'; break;
		case 2: return $par='متوسطة'; break;
		case 3: return $par='عادية'; break;
	}
	}
	function status_replacer($par){
	switch($par){
		case 1: return $par='مفتوحة'; break;
		case 2: return $par='مغلقة'; break;
		case 3: return $par='تنتظر الرد'; break;
	}
	}
	
	
	function text_handler($var){
		$var	= nl2br($var);
		$var	= str_replace(array('<br>','<br />','<br/>'),'[br]',$var);
		$var	= str_replace(array('<','>','"'),array('&lt;','&gt;','&quot;'),$var);
		$var	= str_replace('[br]','<br />',$var);
		return $var;
	}
	
	function input_handler($var){
		 $var	= trim($var);
		 $var	= addslashes($var);
		 $var	= strip_tags($var);
		 return $var;
	}
	
	function no_permission(){
		global $smarty;
		$smarty->display('no_permission.htm');
		include('content_foot.php');
		die();
	}
	
	
	
	function mytickets_mail($form_name,$from_email,$to_name,$to_email,$subject,$text,$in_admin=2){
	global $options;
	if($options['email_method'] == '1'){
			$aq			= mysql_fetch_array(mysql_query("SELECT `email` FROM `members` WHERE `id`='1'"));
			$AdminEmail	= $aq['email'];
			$headers  = 'MIME-Version: 1.0' . "\r\n";
			$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
			$headers .= 'To: '.$to_name.' <'.$to_email.'>' . "\r\n";
			$headers .= 'From: '.$options['site_name'].' <'.$AdminEmail.'>' . "\r\n";
			$headers .= 'Cc: '.$AdminEmail.'' . "\r\n";
			$headers .= 'Bcc: '.$AdminEmail.'' . "\r\n";
			if(@mail($to_email, $subject, $text, $headers)){
				global $SENT;
				$SENT	= 1;
			}else{
				global $SENT;
				$SENT	= 2;
			}
	}else{
		$m['from_name'] = $from_name;
		global $m;
		global $from_email;
		global $to_name;
		global $to_email;
		global $subject;
		global $text;
		if($in_admin == 2){
			include('include/phpmailer/smtp.php');
		}else{
			include('../include/phpmailer/smtp.php');
		}
	}
	}
	
	
	
	
	function email_codes($verb,$username,$ticket_code){
		if(! isset($options)){ $options	= $GLOBALS['options']; }
		$replace_it	=	 array('[site_name]','[br]','[username]','[ticket_code]','[site_url]','[date]');
		$replace_to	=	array($options['site_name'],'<br />',$username,$ticket_code,$options['site_url'],date('d-m-Y'));
		$verb		= str_replace($replace_it,$replace_to,$verb);
		return $verb;
	}
	
	
	
	//Calc Rate For Lessons By Saleh AlMatrafe [saphplesson script]
	function Rate($Votes,$Rate){
		if ($Votes <> 0){
			$Rating = round($Rate / $Votes,2);
			if($Rating > 5) {$Rating = 5;}
			if($Rating < 0) {$Rating = 0;}
		}else{
			$Rating = 0;
		}
		return floor($Rating);
	}
?>