<?php

# LemurCMS by Alec Gorge
#
#     This file contains the PHP functions used throught the CMS.
#
# updated: Jan. 12, 2009

class functions {
	public $page_info = array();
	public $adminLoginForm = '
	<form action="" method="post" id="cms-form-login">
		<fieldset>
			<div class="form-row">
				<label for="username">Username: </label><input type="text" name="username" />
			</div>
			<div class="form-row">
				<label for="password">Password: </label><input type="password" name="password" />
			</div>
			<div class="form-row-last">
				<input type="submit" name="login" value="Login" />
			</div>
		</fieldset>
	</form>
	';
	public function __contruct () {
		$this->db_connect();
	}
	public function db_connect () {
		$mysql_con = mysql_connect(DBHOST.":".DBPORT, DBUSER, DBPASS);
		mysql_select_db(DBNAME, $mysql_con);
	}
	public function db_query($sql) {
		$queryStart = round(microtime(), 4);
		
		$result = mysql_query($sql);
		
		$this->page_info['MYSQL_NUM_QUERIES']++;
		
		$queryEnd = round(microtime(), 4);
		
		$queryTime = $queryEnd - $queryStart;		
		
		$this->page_info['MYSQL_QUERY_TIME'] += $queryTime;
		
		return $result;
	}
	public function register_site_info () {
		$sql = "SELECT * FROM ".DBTABLEPREFIX."settings_site";
		$query = $this->db_query($sql) or die(mysql_error());
		while($row = mysql_fetch_array($query)) {
			$this->page_info['settings'][$row['settingname']]['id'] = $row['id'];
			$this->page_info['settings'][$row['settingname']]['description'] = $row['description'];
			$this->page_info['settings'][$row['settingname']]['value'] = $row['value'];
		}
		define('HOME_PAGE_ID', $this->page_info['settings']['Home Page Id']['value']);
		$sql = "SELECT * FROM ".DBTABLEPREFIX."settings_system";
		$query = $this->db_query($sql) or die(mysql_error());
		while($row = mysql_fetch_array($query)) {
			$this->page_info['settings']['system'][$row['settingname']]['id'] = $row['id'];
			$this->page_info['settings']['system'][$row['settingname']]['description'] = $row['description'];
			$this->page_info['settings']['system'][$row['settingname']]['value'] = $row['value'];
		}
	}
	public function create_temp_file ($file, $x = 'w', $content) {
		$x = true;
		$file = BASE_URL.'temp/'.$file;
		echo $file;
		$fh = fopen($file, $x) or die(
			$y = "can't open file $file"
		);
		$data = $content;
		fwrite($fh, $data);
		fclose($fh);
		return $y;
	}
	public function deleteFile ($file) {
		if($file != "") {
			unlink(BASE_URL.'temp/'.$path);
			return true;
		}
		else
			return 'can"t open file';
	}
	public function openFile ($path, $relative=1) {
		if($relative == 0) {
			$content = file_get_contents(BASE_URL.$path); 
		}
		else {
			$content = file_get_contents($path); 
		}
	    return mb_convert_encoding($content, 'UTF-8', mb_detect_encoding($content, 'UTF-8, ISO-8859-1', true)); 
	}
	public function adminLoginForm () {
		return $adminLoginForm;
	}
	public function getSetting($name) {
		$sql = "SELECT * FROM ".DBTABLEPREFIX."settings_site WHERE settingname = '{$name}'";
		$query = $this->db_query($sql);
		$query = mysql_fetch_assoc($query);
		return $query['value'];
	}
	public function prepareSettingArraysSmarty () {
		global $page_settings, $page_settings_system;
		//Remove spaces from settings arrays to be compatible with Smarty Templating System
		foreach($this->page_info['settings'] as $key => $value) {
			$key = str_replace(" ", "_", $key);
			$page_settings[$key] = $value;
		}
		foreach($this->page_info['settings']['system'] as $key => $value) {
			$key = str_replace(" ", "_", $key);
			$page_settings_system[$key] = $value;
		}
	}
	public function validate($if_fail_url="", $if_success_url="") {
		session_start();
		$username = sanitize_sql_string($_SESSION['username']);
		$qresult = $this->db_query("SELECT * FROM ".DBTABLEPREFIX."users WHERE username = '".$username."'") or die(mysql_error());
		if (mysql_num_rows($qresult)!="1"){
			if($if_fail_url!="") {
			header ("location: ".$if_fail_url."");
			exit();
			}
			return false;
		}
		$dinfo=mysql_fetch_array($qresult);
		if ($dinfo['password'] == $_SESSION['password']) {
			if($if_success_url!=="") {
				$this->page_info['user']['value'] = $_SESSION['username'];
				header ("location: ".$if_success_url."");
				exit();
			}
			$this->page_info['user']['value'] = $_SESSION['username'];
			return true;
		}
		else {
			if($if_fail_url!=="") {
				header ("location: ".$if_fail_url."");
				exit();
			}
			return false;
		}		
	}
	public function dump ($str, $var = false) {
		if($var == true) {
			echo '<pre>';
			var_dump($str);
			echo '</pre>';
		}
		else {
			echo '<pre>';
			print_r($str);
			echo '</pre>';
		}
	}
	/*
		Message Functions
	*/
		public function message($type = 'message', $header = "", $content = "") {
			switch ($type) {
				case 'message':
					return "<div class='message-message message'>\n\t<div class='message-message-header message-header'>".$header."</div>\n\t<div class='message-message-content message-content'>\n\t\t<p>".$content."</p>\n\t</div>\n</div>";
					break;
				case 'success':
					return "<div class='message-ok message'>\n\t<div class='message-ok-header message-header'>".$header."</div>\n\t<div class='message-ok-content message-content'>\n\t\t<p>".$content."</p>\n\t</div>\n</div>";
					break;
				case 'error':
					return "<div class='message-error message'>\n\t<div class='message-error-header message-header'>".$header."</div>\n\t<div class='message-error-content message-content'>\n\t\t<p>".$content."</p>\n\t</div>\n</div>";
					break;
				case 'warning':
					return "<div class='message-warning message'>\n\t<div class='message-warning-header message-header'>".$header."</div>\n\t<div class='message-warning-content message-content'>\n\t\t<p>".$content."</p>\n\t</div>\n</div>";
					break;
			}
		}
		
}

?>