<?php
/* MyTickets [   by = www.phpx3.com   ]
* Faha1ad2@hotmail.com
* Writed By : Fahad S. Al-ghudaier
* All Right Reseved (C) 2011
*/

	$Dir		= 'rtl';
	$Titlex		= 'لوحة الإدارة // MyTickets';
	global $Version;
	$Version	= '1.1.1';

	function head($dir='rtl',$extra='',$Title='',$ShowHeader=1){
		$HEAD = "<html dir='".$GLOBALS['Dir']."'>
		<meta content='ar-sa' http-equiv='Content-Language' />
		<meta content='text/html; charset=utf-8' http-equiv='Content-Type' />
		<LINK REL='STYLESHEET' TYPE='text/css' HREF='style/css/mestyle.css'>
		<link rel='stylesheet' type='text/css' href='style/css/theme4.css' />
		<link rel='stylesheet' type='text/css' href='style/css/style.css' />
	<!--[if IE]>
	<link rel='stylesheet' type='text/css' href='style/css/ie-sucks.css' />
	<![endif]-->
		".
		$extra."
		<title>".$GLOBALS['Titlex']."</title>
		</head><body><center>";
		print $HEAD;
		if($ShowHeader==1){
		include('style/temp/header.php');
		}
	}



	# Include Important Files
	function includes(){
	global $db;
		require("../include/connect.php");
		require('../include/system/Functions.php');
	}

	
	# is he logged in ? is he allowed to see the control panel ?
	function check_session(){
		$db	= $GLOBALS['db'];
	
		# check if the session is exist
		if(!isset($_SESSION['tickets_session'])){
			include("style/temp/login.htm");
			exit;
		}
		
		$user_name 		= $_SESSION['member_username'];
		$user_password 	= $_SESSION['member_password'];
		$user_id		= $_SESSION['member_id'];
		$Query 			= $db->query("SELECT `id`,`username`,`password`,`group_id` FROM `members` WHERE `id`='".$user_id."' AND `username`='".$user_name."' AND `password`='".$user_password."'") or die("Fail Check The Session;");
		
		if(mysql_num_rows($Query) == 1){
			$member		= $db->fetch($Query);
			$group		= $db->query("SELECT * FROM `groups` WHERE `id`='".$member['group_id']."'") or die("Fail Check The Session");
			global $permission;
			$permission	= $db->fetch($group);
		}else{
			$ACCESS = 'UNACCEPT';
		}
		if($permission['allow_cp'] == 1 OR $permission['top_man'] == 1){
			$ACCESS = 'allow';
		}else{
			$ACCESS = 'UNACCEPT';
		}
		if($ACCESS == 'UNACCEPT'){ 
			exit('المعذره , إما انك لم تسجيل الدخول او ليس لديك الترخيص اللازم لدخول هذه المنطقة ...
			<hr />
			<a href="logout.php">تسجيل الخروج وحذف الجلسة</a>'); 
			die;
		}
	}
		
		
	# to show templates
	function show($par){
		include('style/temp/'.$par.'');
	}
	

	# redirect with successful message
	function ok($TO,$time=3){
		$move_to = $TO;
		include("style/temp/ok.htm");
	}

	
	
	# querys used alot
	function querys(){
	global $languages_query;
	global $groups_query;
		$languages_query	= $GLOBALS['db']->query("SELECT * FROM `languages` ORDER BY `id` DESC");
		$groups_query		= $GLOBALS['db']->query("SELECT * FROM `groups` ORDER BY `id` DESC");
	}
	
	
	
	# check the given id's
	function id_check($par,$type){
		$par	= trim($par);
		$par	= addslashes($par);
		if(empty($par)){
			return false;
		}elseif(! is_numeric($par)){
			return false;
		}elseif($GLOBALS['db']->id_exist($type,$par)){
			return true;
		}else{
			return false;
		}
	}
	
	
	# proccess the 'to' redirect
	function replace_to($to){
		$to	= str_replace('[and]','&',$to);
		return $to;
	}
	
	
	# get any member informations
	function get_userinfo($userID,$fields='*'){
	global $minfo;
	$q1		= $GLOBALS['db']->query("SELECT ".$fields." FROM `members` WHERE `id`='".$userID."'");
	if(mysql_num_rows($q1) > 0){
	$minfo	= mysql_fetch_array($q1);
	return $minfo;
	}
	}
	
	# get any group informations
	function get_groupinfo($GID,$fields='*'){
	global $ginfo;
	$q1		= $GLOBALS['db']->query("SELECT ".$fields." FROM `groups` WHERE `id`='".$GID."'");
	if(mysql_num_rows($q1) > 0){
	$ginfo	= $GLOBALS['db']->fetch($q1);
	return $ginfo;
	}
	}
	

	function sections_list($selected='',$multi=2){
		$query	= $GLOBALS['db']->query("SELECT `id`,`title` FROM `section` ORDER BY `level` ASC");
		echo '<select name="section_id"';
		if($multi==1){ echo 'multiple="multiple"'; }
		echo '>';
		while($srows = $GLOBALS['db']->fetch($query)){
			echo '<option value="'.$srows['id'].'" ';
			if($srows['id'] == $selected){ echo 'SELECTED'; }
			echo '>',$srows['title'],'</option>';
		}
		echo '</select>';
	}
	
	# show the footer
	function down($ShowFooter=1){
	if($ShowFooter == 1){
		include('style/temp/footer.php');
	}else{
		echo '</center><!-- MyTickets --></body></html>';
	}
	}


?>