<?php
/*
	PHPbase web framework
	copyright (c) 2003 @authors

	$Id: name.php,v 0.0.1 dd/mm/yyyy hh:mm:ss author Exp $
	description
*/

// dependencies

/**
* description
*
* @library	
* @author	
* @since	
*/
class CAdministrators extends CPlugin{
	/**
	* description
	*
	* @param
	*
	* @return
	*
	* @access
	*/
	function CAdministrators() {
		//parent::CModule("auth");
	}

	/**
	* description
	*
	* @param
	*
	* @return
	*
	* @access
	*/
	function DoEvents() {
		global $_CONF, $base , $_TSM , $_USER , $_SESS , $_PAGE;

		parent::DoEvents();
		//$this->GetInfoBlock();
		//$this->__protect();

		if ($_GET["mod"] == "administrators") {

			$this->tpl_module = array(
				"mod_name"	=> $_USER["user_level"] ? "My Profile" : "CMS Administrators",
			);

		
			switch ($_GET["sub"]) {
				
				case "profile":
				case "myprofile":
					URLRedirect($_SERVER["PHP_SCRIPT"] . "?mod={$this->name}&sub=users&action=details&user_id={$_USER[user_id]}");
				break;

				case "users":

					$data = new CSQLAdmin($_GET["sub"], $_CONF["forms"]["admintemplate"],$this->db,$this->private->tables,$extra);

					$this->PrepareFields($data->forms["forms"], $sub);

					if (!strlen($_POST["user_password"]))
						unset($_POST["user_password"]);

					$data->functions = array( 
							"onstore" => array(&$this , "__call_user_store"),
					);

					return $data->DoEvents();
				break;

				case "logs":

					$data = new CSQLAdmin($_GET["sub"], $_CONF["forms"]["admintemplate"],$this->db,$this->private->tables,$extra);
					$this->PrepareFields($data->forms["forms"], $sub);

					return $data->DoEvents();
				break;

			}
		}		
	}	

	
	/**
	* description
	*
	* @param
	*
	* @return
	*
	* @access
	*/
	function __call_user_store($record , $type) {
		if ($_POST["user_password"]) {			
			$this->db->Query("UPDATE {$this->private->tables[users]} SET user_password=md5('$_POST[user_password]') WHERE user_id='{$record[user_id]}'");
		}		
	}

	function LogAction() {
		global $_SESS , $_PAGE;

		if (!$_SESS["minibase"]) {
			return "";
		}

		if ($_PAGE == "ajax") {
			return "";
		}
		
		

		

		$log = array(
			"log_date"			=> time(),
			"log_user"			=> $_SESS["minibase"]["raw"]["user_id"],
			"log_ip"			=> $_SERVER["REMOTE_ADDR"],
			"log_url"			=> $_SERVER["REQUEST_URI"],
			"log_url_referer"	=> $_SERVER["HTTP_REFERER"],

			"log_vars_get"		=> serialize($_GET),
			"log_vars_post"		=> serialize($_POST),
			"log_vars_files"	=> serialize($_FILES),

		);

		$this->db->QueryInsert(
			$this->private->tables['users_log'],
			$log
		);
	}


	/**
	* description
	*
	* @param
	*
	* @return
	*
	* @access
	*/
	function CheckGlobalPermission($module_name, $acl = false) {
		global $_SESS , $_USER;

		if (!$_USER["user_level"]) {
			return true;
		}

		if ($_USER["user_perm2"]) {

			if ($this->tables["core:modules"]) {

				$modules = $this->db->Linear($this->db->QFetchRowArray("SELECT module_code FROM {$this->tables['core:modules']} WHERE module_id in ({$_USER[user_perm2]})"));

				
				if (in_array( $module_name , $modules)) {
					return true;
				}

			}
		}
		

		
		return false;		
	}
	

}

?>