<?php

# lemur cms by Alec Gorge
#
#     This file contains the PHP functions used throught the CMS.
#
# updated: Nov. 13, 2008

class admin extends functions {
	public function __construct () {
		$this->db_connect();
		$this->register_site_info();
	}
	public function set_validation($user, $pass) {
		session_start();
		$_SESSION['username'] = sanitize_sql_string($user);
		$_SESSION['password'] = sha1(sanitize_sql_string($pass));
		if($this->validate() == false) {
			$_SESSION = array();
			session_destroy();
			return false;
		}
		else {
			return true;
		}
	}
	public function isAdmin () {
		$numArg = func_get_args();
		foreach($numArg as $key => $value) {
			$levels .= " level = '$value' or";
		}
		$sql = "SELECT * FROM ".DBTABLEPREFIX."users WHERE $levels level = 'aoiw'";
		$query = $this->db_query($sql) or die(mysql_error());
		$is_admin = false;
		while($row = mysql_fetch_array($query)) {
			if($this->page_info['user']['value'] == $row['username']) {
				$is_admin = true;
			}
		}
		return $is_admin;
	}
	public function adminNavigation () {
		$r = "\n<ul>\n";
		$contents = $this->openFile(BASE_URL."includes/admin/pages/main-page-list.inc");
		$menu = explode("\n", $contents);
		foreach ($menu as $key => $value) {
			$safe_value = make_clean_url($value);
			$r .= "\t<li><a href='admin.php?page=$safe_value'>$value</a></li>\n";
		}
		$contents = $this->openFile(BASE_URL."includes/admin/pages/user-page-list.inc");
		$menu = explode("\n", $contents);
		foreach ($menu as $key => $value) {
			$safe_value = make_clean_url($value);
			$r .= "\t<li><a href='admin.php?page=$safe_value'>$value</a></li>\n";
		}
		$r .= "</ul>";
		return $r;
	}
	public function adminPublicPageList ($ul_id ="ul-navigation", $sub_ul_class ="ul-sub-navigation", $lis = "selected", $link = true, $page = "edit-and-delete-pages") {
		$sql = "SELECT * FROM ".DBTABLEPREFIX."content_pages ORDER by orderingid, menutitle";
		$run = $this->db_query($sql);
			if($array == true) {
				while ($rec = mysql_fetch_assoc($run)) {
					$r[$rec['menutitle']] .= $rec['title']."&&||&&".$rec['content'];
					
				}
			}
		$r .= "\n<ul id=\"".$ul_id."\">";
		$xy = 0;
		while ($rec = mysql_fetch_assoc($run)) {
			if($rec['parent_id'] == "0") {
				$r .= "\n\t<li id='{$rec['id']}' name='{$rec['id']}'";
				if($page_id == $rec['id']) {
					$r .= " class='$lis'";
				}
				$r .= ">";
				//$r .= '<span>'.$rec['menutitle'].'</span>';
/*				$r .= "<select name='menu_item[]'>\n";
				for($col = -20; $col <= 20; $col++) {
					$r .= "\t<option value='$col'";
					if($col == $rec['orderingid']) {
						$r .= " selected='selected'";
					}
					$r .= ">$col</option>\n";
				}
				$r .= "</select>";*/
			if($link == true) {
					$r .= "<span class='rearrange-page'><a title='Move {$rec['menutitle']}' class='helplink' href='javascript:;' ><img src='".THEME_URL."includes/admin/images/icons/move.png' /></a></span>
					<span class='delete-page' ><a title='Delete {$rec['menutitle']}'class='helplink' href='javascript:;' ><img src='".THEME_URL."includes/admin/images/icons/delete-16x16.png' alt='Delete' /></a></span>
					<a class='admin-edit-link helplink' id='{$rec['id']}' href='?page=$page&id=".$rec['id']."' title='Edit {$rec['menutitle']}' ><img src='".THEME_URL."includes/admin/images/icons/edit-16x16.png' alt='Edit' />".$rec['menutitle'];
					$r .= "</a>";
			}
				$r .= $this->adminPublicPageListParent($rec['id'], $sub_ul_class, $lis, $link, false, $xy, $page).'<br class="clear" /></li>';
				$xy++;
			}
		}
		$r .= "\n</ul>\n";		
		return $r;
	}
	public function adminPublicPageListParent ($parentid = HOME_PAGE_ID, $class="ul-sub-navigation", $lis = "selected", $link = true, $array = false, $q, $page) {
		$sql = "SELECT * FROM ".DBTABLEPREFIX."content_pages WHERE parent_id = ".$parentid." ORDER by orderingid, menutitle";
		$run = $this->db_query($sql);
		$x = mysql_num_rows($run);
		if($x !== 0) {
			$r .= "\n\t\t<ul class='".$class."'>";
		}
		$qz = 0;
		while ($rec = mysql_fetch_assoc($run)) {
				$r .= "\n\t\t\t\t<li id='{$rec['id']}' name='{$rec['id']}'";
			if($page_id == $rec['id']) {
				$r .= " class='".$lis."'";
			}
			$r .= ">";
			//$r .= $rec['menutitle'];
/*			$r .= "<select name='menu_item[]'>\n";
			for($col = -20; $col <= 20; $col++) {
				$r .= "\t<option value='$col'";
				if($col == $rec['orderingid']) {
					$r .= " selected='selected'";
				}
				$r .= ">$col</option>\n";
			}
			$r .= "</select>";*/
			if($link == true) {
					$r .= "<span class='rearrange-page'><a title='Move {$rec['menutitle']}' class='helplink' href='javascript:;' ><img src='".THEME_URL."includes/admin/images/icons/move.png'  /></a></span>
					<span class='delete-page' ><a title='Delete {$rec['menutitle']}'class='helplink' href='javascript:;' ><img src='".THEME_URL."includes/admin/images/icons/delete-16x16.png' alt='Delete' title='Delete this page!' class='helplink' /></a></span>
					<a class='admin-edit-link helplink' id='{$rec['id']}' href='?page=$page&id=".$rec['id']."' title='Edit {$rec['menutitle']}' ><img src='".THEME_URL."includes/admin/images/icons/edit-16x16.png' alt='Edit' />".$rec['menutitle'];
					$r .= "</a>";
			}
			$qz++;
			$r .= $this->adminPublicPageListParent($rec['id'], $class, $lis, $link, false, $q, $page).'<br class="clear" /></li>';
		}
		if($x !== 0) {
			$r .= "\n\t\t</ul>";
		}
		return $r;
	}
	public function pageContent ($content, $append=0) {
		//$content = htmlspecialchars_decode(htmlentities($content, ENT_QUOTES, false)); 
		if($append == 1) {
			$this->page_info['content']['page_content'] .= $content;
		}
		if($append == -1) {
			$this->page_info['content']['page_content'] = $content.$this->page_info['content']['page_content'];
		}
		if($append == 0) {
			$this->page_info['content']['page_content'] = $content;
		}
	}
	public function pageTitle ($content, $append=0) {
		//$content = htmlspecialchars_decode(htmlentities($content, ENT_QUOTES, false)); 
		if($append == 1) {
			$this->page_info['content']['page_title'] .= $content;
		}
		if($append == -1) {
			$this->page_info['content']['page_title'] = $content.$this->page_info['content']['page_title'];
		}
		if($append == 0) {
			$this->page_info['content']['page_title'] = $content;
		}
	}
	public function displayPage ($page_id = 0, $set = 0) {
		$sql = "SELECT * FROM ".DBTABLEPREFIX."admin_content WHERE id ='$page_id'";
		$content = $this->db_query($sql) or die(mysql_error());
		while($row = mysql_fetch_array($content)) {
			$this->pageTitle(stripslashes($row['title']));
			$this->pageContent(stripslashes($row['title']));
			$this->page_info['content']['sidebar'] = stripslashes($row['sidebar']);
		}
		if($set == 0) {
			return $this->page_info['content']['page_content'];
		}
	}
	
}
?>