Location: PHPKode > projects > chillyCMS > chillyCMS/core/frontendpage.class.php
<?php
//#################################################################################################
//	Frontendpage class
//#################################################################################################
//	chillyCMS - Content Management System
//	Copyright (C) 2008
//	Stefanie Wiegand <hide@address.com> & Johannes Cox <hide@address.com>
//	
//	This program is licensed under the GPL 3.0 license. For more information see LICENSE.txt.
//#################################################################################################
defined('DOIT') or die('Restricted access');

require_once('page.class.php');

$allpos = array("top","left","right","bottom","user0","user1","user2","user3",
"user4","user5","user6","user7","user8","user9");
		
class Frontendpage extends Page {
	//Class variables//////////////////////////////////////////////////////////////////////////
	public $language;			//active language
	protected $pagetitle;
	protected $author;
	protected $siteemail;
	protected $backendstyle;
	protected $template;			//active template
	protected $keywords;
	protected $description;
	protected $dofirst;			//a file to be executed before anything else
	protected $headlinks;			//something that is to be included in the html head
	protected $allpos;			//all possible positions
	
	protected $positions;			//all positions with the submodules they contain
	protected $id;				//the id to show
	protected $name;			//the name to show
	protected $mainmodname;			//the name of the mainmodule that is to be displayed
	protected $settings;			//the settings to show
	protected $access;			//the access to show
	protected $specialaccess;		//the specialaccess to show
	protected $content;			//the content to show
	protected $submodules;			//all modules on that site
	protected $stylesheets;			//the stylesheets of the modules used in the site
	protected $countme;			//count the visit of the pagecall?
	//Functions////////////////////////////////////////////////////////////////////////////////
	//Constructor
	function __construct($id=false) {
		global $settings,$allpos;
		
		parent::__construct();
		
		$this->allpos = $allpos;
				
		//Get settings from database
		if (isset($settings["language"])) { $this->language = $settings["language"]; }
		if (isset($settings["sitename"])) { $this->pagetitle = $settings["sitename"]; }
		if (isset($settings["author"])) { $this->author = $settings["author"]; }
		if (isset($settings["siteemail"])) { $this->siteemail = $settings["siteemail"]; }
		if (isset($settings["backendstyle"])) { $this->backendstyle = $settings["backendstyle"]; }
		if (isset($settings["template"])) { $this->template = $settings["template"]; }
		if (isset($settings["keywords"])) { $this->keywords = $settings["keywords"]; }
		if (isset($settings["description"])) { $this->description = $settings["description"]; }
		
		//Count site visits
		$this->countme=false;
		$cookievalue=num_text($this->pagetitle);
		$visited=false;
		if (isset($_COOKIE["chillyCMS"]) && is_array($_COOKIE["chillyCMS"])) {
			foreach ($_COOKIE["chillyCMS"] as $cookie=>$value) {
				if ($cookie==$cookievalue && $value=="visited") {
					$visited=true;
					break;
				}
			}
		}
		if (!$visited) {
			if (!headers_sent()) { setcookie("chillyCMS[$cookievalue]","visited"); }
			$this->countme=true;
		}
		$this->increment_stats();
		
		//Get all positions from template file
		if (file_exists(PATH."/templates/".$this->template."/config.php")) {
			require(PATH."/templates/".$this->template."/config.php");
			$this->positions = $positions;
		} else {
			$this->positions = false;
		}

		//Initialize class vars
		$this->submodules=array();
		$this->stylesheets=array();
		$this->stylesheets["normal"]=array();
		$this->stylesheets["ie6"]=array();
		$this->headlinks=array();
		$this->dofirst=array();
		
		///////////////////////////////////////////////////////////////////////////////////
		// Mainmodule                                                                    //
		///////////////////////////////////////////////////////////////////////////////////
		
		if (DB_HOST) { $request = escape($_REQUEST); }
		
		//get id from constructor
		if ($id!==false) {
			$this->id = $id;
			unset($request["id"]);
			$this->query("select c.*, m.name as modname from system_modules as m, site_content as c ".
			"where c.modid=m.modid and c.id=$this->id and c.active=1 limit 1");
			$result = $this->db->getdata();
		//get id from get
		} elseif (isset($request["id"])) {
			$this->id = intval($_REQUEST["id"]);
			$this->query("select c.*, m.name as modname from system_modules as m, site_content as c ".
			"where c.modid=m.modid and c.id=$this->id and c.active=1 limit 1");
			$result = $this->db->getdata();
		//get id from startpage
		} else {
			$this->id = 0;
			$this->query("select c.*, m.name as modname from site_content as c, system_modules as m ".
			"where c.modid=m.modid and c.startpage='1' and c.active=1 limit 1");
			$result = $this->db->getdata();
			if (!empty($result)) { $this->id = $result["id"]; }
		}
		
		$this->name = $result["name"];
		$this->mainmodname = $result["modname"];
		$this->settings = $result["settings"];
		$this->access = $result["access"];
		$this->specialaccess = $result["specialaccess"];
		$this->content = $result["content"];
		
		//get all mainmod types of the current page and all of the children
		$modandchildren = array();
		$sql = "select distinct m.name from system_modules as m ".
		"left join site_content as c on (c.id=$this->id or c.parentid=$this->id) ".
		"where c.modid=m.modid";
		$this->query($sql);
		$modandchildren = $this->db->getdata_array();
		
		if (is_array($modandchildren) && !empty($modandchildren)) {
			foreach ($modandchildren as $mc) {
				$mc = $mc['name'];
				
				//Get the stylesheets of the mainmodule if they exist
				if (file_exists(PATH."/modules/$mc/$mc.css")) {
					$this->stylesheets["normal"][] = "<link rel='stylesheet' href='".URL."/modules/$mc/".
					"$mc.css' type='text/css' />\n";
				}
				if (file_exists(PATH."/modules/$mc/ie6.css")) {
					$this->stylesheets["ie6"][]="<link rel='stylesheet' ".
					"href='".URL."/modules/$mc/ie6.css' type='text/css' />\n";
				}
				
				//Get dofirst action from mainmod if exists
				if (file_exists(PATH."/modules/$mc/dofirst.php")) {
					$this->dofirst[]=PATH."/modules/$mc/dofirst.php";
				}
		
				//Additional links
				if (file_exists(PATH."/modules/$mc/headlink.php")) {
					$this->headlinks[]=PATH."/modules/$mc/headlink.php";
				}
			}
		}
		
		
	
		///////////////////////////////////////////////////////////////////////////////////
		// Submodules                                                                    //
		///////////////////////////////////////////////////////////////////////////////////
		
		//Get Submodules
		$this->query("select m1.*, ifnull(m2.id,0) as prev, ifnull(m3.id,0) as next, ".
		"s.name as modname from site_modules as m1 ".
		"left join site_modules m2 on m2.order=m1.order-1 and m1.position=m2.position ".
		"left join site_modules m3 on m3.order=m1.order+1 and m1.position=m3.position ".
		"left join system_modules s on s.modid=m1.modid ".
		"order by m1.position,m1.order");
		$moddata = $this->db->getdata_array();
		
		//Stylesheets, dofirst acions and headlinks
		if (is_array($this->positions) && !empty($this->positions)) {
			foreach ($this->positions as $thispos) {
				if (in_array($thispos,$this->allpos)) {
				if (is_array($moddata) && !empty($moddata)) {
					foreach ($moddata as $mod) {
						if ($thispos==$mod['position']) {
							$this->submodules[$thispos][] = $mod;
							//Get the stylesheets of the submodule if it exists
							if (file_exists(PATH.'/modules/'.$mod['modname'].'/'.$mod['modname'].'.css')) {
								$newstylesheet='<link rel="stylesheet" '.
								'href="'.URL.'/modules/'.$mod['modname'].'/'.$mod['modname'].'.css" type="text/css" />';
								if (!in_array($newstylesheet,$this->stylesheets["normal"])) {
									$this->stylesheets["normal"][]=$newstylesheet;
								}
							}
							if (file_exists(PATH.'/modules/'.$mod['modname'].'/ie6.css')) {
								$newstylesheet='<link rel="stylesheet" '.
								'href="'.URL.'/modules/'.$mod['modname'].'/ie6.css" type="text/css" />';
								if (!in_array($newstylesheet,$this->stylesheets['ie6'])) {
									$this->stylesheets['ie6'][]=$newstylesheet;
								}
							}
							//Get dofirst action from submod if exists
							if (file_exists(PATH.'/modules/'.$mod['modname'].'/dofirst.php')) {
								$dofirst=PATH.'/modules/'.$mod['modname'].'/dofirst.php';
								if (!in_array($dofirst,$this->dofirst))
								$this->dofirst[]=$dofirst;
							}
							//Get things that are to be included in the html head
							if (file_exists(PATH.'/modules/'.$mod['modname'].'/headlink.php')) {
								$this->headlinks[]=PATH.'/modules/'.$mod['modname'].'/headlink.php';
							}
						}
					}
				}
				}
			}
		}
		
		///////////////////////////////////////////////////////////////////////////////////
		// HTML-Head                                                                     //
		///////////////////////////////////////////////////////////////////////////////////

		//title
		if ($this->name == "") { $this->title = $this->pagetitle; }
		else { $this->title = $this->pagetitle." - ".$this->name; }
	
		//doctype
		$this->doctype = "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" ".
		"\"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">\n".
		"<html xmlns=\"http://www.w3.org/1999/xhtml\" xml:lang=\"".$this->language."\" ".
		"lang=\"".$this->language."\">\n".
		"<head>\n";

		$mydescription = substr(strip_tags($this->content),0,150);

		//metatags
		$this->meta .= "\t<meta name=\"author\" content=\"".$this->author."\" />\n".
		"\t<meta name=\"keywords\" content=\"".$this->keywords."\" />\n".
		"\t<meta name=\"description\" content=\"".$mydescription."...\" />\n".
		"\t<meta name=\"robots\" content=\"index,follow\" />\n".
		"\t<meta name=\"revisit-after\" content=\"7 days\" />\n".
		
		//favicon
		"\t<link rel=\"icon\" href=\"".URL."/templates/".$this->template."/favicon.ico\" type=\"image/vnd.microsoft.icon\"/>\n".

		//general frontend stylesheet
		"\t<link rel='stylesheet' href='".URL."/style/css/frontend.css' type='text/css'/>\n";
	}
	
	function print_head() {
		global $mysession;
		
		echo //$this->doctype.
		"<title>$this->title</title>\n".
		$this->meta;

		//insert module stylesheets
		foreach ($this->stylesheets["normal"] as $css) {
			echo "\n\t$css";
		}
		if (!empty($this->stylesheets["ie6"])) {
			foreach ($this->stylesheets["ie6"] as $css) {
					echo "\n\t<!--[if lte IE 6]>$css<![endif]-->";
			}
		}
	
		//head links
		if (!empty($this->headlinks)) {
			foreach ($this->headlinks as $hl) {
				echo "\t";
				include($hl);
				echo "\n";
			}
		}
	}

	function print_position($position) {
		global $is_logged,$is_user,$is_admin,$mysession,$myuser;
		
		//is it a valid position for the used template?
		if (in_array($position,$this->positions)) {
			//find out what module is in that position
			$sortedmods=array();
			if (isset($this->submodules[$position]) && is_array($this->submodules[$position]) && !empty($this->submodules[$position])) {
				foreach ($this->submodules[$position] as $row) {
					$sortedmods[] = new Listitem($row["id"],$row["order"],$row["prev"],$row["next"],
					array("name"=>$row["name"],"modname"=>$row["modname"],"active"=>$row["active"],
					"settings"=>$row["settings"],"access"=>$row["access"],'conditionaldisplay'=>$row['conditionaldisplay'],
					'listtype'=>$row['listtype'],'list'=>explode(',',$row['list']),"specialaccess"=>$row["specialaccess"]));
				}
			}
			//sort modules
			if (!empty($sortedmods)) {
				foreach ($sortedmods as $thismod) {
					$modinstanceid=$thismod->id;
					$modinstancename=$thismod->data["name"];
					$modsettings=$thismod->data["settings"];
					$sitelanguage=$this->language;
					//if it can be accessed for everybody
					if ($thismod->data["active"]=="1") {
						//if it should be displayed on this specific page if:
						//--it has no conditional display OR
						if ($thismod->data["conditionaldisplay"]==0 or
						//--it is a whitelist and the id of this content id contained in it OR
						($thismod->data["conditionaldisplay"]==1 && $thismod->data["listtype"]==0 && 
						in_array($this->id,$thismod->data['list'])) or
						//--it has a blacklist and the id of this content is not contained in it
						($thismod->data["conditionaldisplay"]==1 && $thismod->data["listtype"]==1) &&
						!in_array($this->id,$thismod->data['list'])) {
							//special access
							$hasspecialaccess = false;
							$thisspecialaccess = explode(',',$thismod->data['specialaccess']);
							if (is_array($thisspecialaccess) && !empty($thisspecialaccess)) {
								foreach ($thisspecialaccess as $tsa) {
									if (isset($myuser->gids_assoc[$tsa]) && 
									($myuser->gids_assoc[$tsa]=='r' or $myuser->gids_assoc[$tsa]=='w')) {
										$hasspecialaccess = true;
										break;
									}
						
								}
							}
							//if the user is allowed to see the module
							if ($thismod->data["access"]=="0" or ($thismod->data["access"]=="1" && $is_user)
							or ($thismod->data["access"]=="2" && $is_admin) or $hasspecialaccess) {
								$modpath = PATH."/modules/".$thismod->data["modname"];
								//load the matching language file if it exists
								if (file_exists($modpath."/languages/$this->language.php")) {
									include($modpath."/languages/$this->language.php");
								//if no, try to load the "en" language file
								} elseif (file_exists($modpath."/languages/en.php")) {
									include($modpath."/languages/en.php");
								}
								//...load that very module
								include($modpath."/config.php");
								include($modpath."/$outputfile");					
							}
						}
					}
				}
			}		
		}
	}
	
	function print_content() {
		global $is_logged,$is_user,$is_admin,$mysession,$l_core,$l_gen,$l_log,$timeout,$settings;
	
		$showcontent=true;
		//load the language
		if (isset($_REQUEST["register"]) || isset($_REQUEST["forgotdata"]) || isset($_REQUEST["editprofile"])
		 || isset($_REQUEST["feedbackuser"]) || isset($_REQUEST["goodfeedback"]) || isset($_REQUEST["badfeedback"]) ||
		 (isset($_REQUEST["action"]) && $_REQUEST["action"]=="logout")) {
		 	$modlangfile = PATH."/modules/login/languages/$this->language.php";
		 	//include language file of the module if it exists
		 	if (file_exists($modlangfile)) {
				include($modlangfile);
			//english language file must always exist in a valid module
			} else {
				include(PATH."/modules/login/languages/en.php");
			}
		}
		//user wants to register
		if (isset($_REQUEST["register"])) {
			include_once(PATH."/modules/login/register.php");
			$showcontent=false;
		//user has forgotten his name and/or password
		} elseif (isset($_REQUEST["forgotdata"])) {
			include_once(PATH."/modules/login/forgotdata.php");
			$showcontent=false;
		//user was logged out
		} elseif (isset($_REQUEST["action"]) && $_REQUEST["action"]=="logout") {
			//--by himself
			if (isset($_POST["reason"]) && $_POST["reason"]=="logout") {
				echo msg($l_log["lbl_logout"],'good');
			//--by inactivity
			} else {
				$msg = $l_gen["lbl_sorry"].", ".escape_html($_GET["user"])."! ".$l_log["msg_timeout1_err"].
				round($settings["session_ltime"]/60,0).$l_log["msg_timeout2_err"]." ".$l_log["msg_sessionend"];
				echo msg($msg,'bad');
			}
		}
		if ($showcontent) {
			$specialaccess = explode(",",$this->specialaccess);
			$modsettings = $this->settings;
			//site is not for everybody
			//--users only
			if ($this->access == "1") {
				if (!($is_user or $is_admin)) {
					echo msg($l_core['lbl_uonly'],'info');
					$showcontent = false;
				}
			//--admins only
			} else if ($this->access == "2") {
				if (!$is_admin) {
					$ok = false;
					if (!empty($mysession)) {
						$gids = $mysession->user->gids;
					} else {
						$gids = false;
					}
					//is there any special access?
					if (!empty($gids)) {
						if (!empty($specialaccess)) {
							foreach ($specialaccess as $spac) {
								if (in_array($spac,$gids)) {
									$ok = true;
									break;
								}
							}
						}
					}
					if (!$ok) {
						echo msg($l_core['lbl_aonly'],'info');
						$showcontent = false;
					}
				}
			}
			if ($showcontent) {
				//Module found
				if ($this->mainmodname!="") {
					//increment views of the content
					$this->query("update site_content set `views`=`views`+1 where `id`=$this->id limit 1");
					$modpath=PATH."/modules/$this->mainmodname";
					include("$modpath/config.php");
					//load the matching language file if it exists
					if (file_exists($modpath."/languages/$this->language.php")) {
						include($modpath."/languages/$this->language.php");
					//if no, try to load the "en" language file
					} elseif (file_exists($modpath."/languages/en.php")) {
						include($modpath."/languages/en.php");
					}
					include("$modpath/$outputfile");
				} else {	
					include_once(PATH."/core/404.php");
				}
			}
		}
	}
	//Destructor
	public function __destruct() {
		$this->language=$this->pagetitle=$this->author=$this->siteemail=$this->backendstyle=$this->template=
		$this->keywords=$this->description=$this->positions=$this->id=$this->name=$this->mainmodname=$this->settings=
		$this->access=$this->specialaccess=$this->content=$this->submodules=$this->stylesheets=$this->countme=false;
		
		parent::__destruct();
	}
	//Getter
	public function __get($name) {
		if (isset($name, $this->$name)) { return $this->$name; }
		else { return false; }	
	}
	private function increment_stats () {
	if ($this->countme==true) {
		//collect visitor data for analysis tool
		$wu = new Webuser();
		if (!$wu->isbot) {
			$sql = "select `id`,`month`,`visits`,`browsers`,`oss`,`countries` from site_visitors ".
			"where `id`=(select max(`id`) from site_visitors) limit 1";
			
			$this->query($sql);
			$result = $this->db->getdata();
			$browsers = $oss = $countries = array();
			//still the same month?
			$now = date("m/Y",time());
			if ($now == $result["month"]) {
				//read from db:
				//--browsers
				$dbbrowsers = explode(",",$result["browsers"]);
				foreach ($dbbrowsers as $dbbrowser) {
					$browser=explode(":",$dbbrowser);
					if (isset($dbbrowser[0]) && $dbbrowser[0]!="") { $browsers[$browser[0]]=$browser[1]; }
				}
				//--oss
				$dboss = explode(",",$result["oss"]);
				foreach ($dboss as $dbos) {
					$os = explode(":",$dbos);
					if (isset($dbos[0]) && $dbos[0]!="") { $oss[$os[0]]=$os[1]; }
				}
				//--countries
				$dbcountries = explode(",",$result["countries"]);
				foreach ($dbcountries as $dbcountry) {
					$country=explode(":",$dbcountry);
					if (isset($dbcountry[0]) && $dbcountry[0]!="") { $countries[$country[0]]=$country[1]; }
				}
			}
			//set new values
			//--browser
			if (!array_key_exists($wu->browser, $browsers)) { $browsers[$wu->browser]=1; }
			else { $browsers[$wu->browser]++; }
			//--os
			if (!array_key_exists($wu->os, $oss)) { $oss[$wu->os]=1; }
			else { $oss[$wu->os]++; }
			//--country
			if (!array_key_exists($wu->country, $countries)) { $countries[$wu->country]=1; }
			else { $countries[$wu->country]++; }

			//transform to string again to save in db
			//--browsers
			$newbrowsers = array();
			foreach ($browsers as $browser=>$quantity) { $newbrowsers[] = "$browser:$quantity"; }
			$newbrowsers = implode(",",$newbrowsers);
			//--oss
			$newoss = array();
			foreach ($oss as $os=>$quantity) { $newoss[] = "$os:$quantity"; }
			$newoss = implode(",",$newoss);
			//--countries
			$newcountries = array();
			foreach ($countries as $country=>$quantity) { $newcountries[] = "$country:$quantity"; }
			$newcountries = implode(",",$newcountries);

			//Monatsformat in der DB: 09/2009
			if ($now == $result["month"]) {
				$sql = "update site_visitors set `visits`=`visits`+1,`browsers`='$newbrowsers'".
				",`oss`='$newoss',`countries`='$newcountries' where `month`='$result[month]'";
			} else {
				$sql = "insert into site_visitors (`month`,`visits`,`browsers`,`oss`,`countries`) ".
				"values (DATE_FORMAT(now(),'%m/%Y'),1,'$newbrowsers','$newoss','$newcountries')";
			}
			$this->query($sql);
		}
	}
}
} ?>
Return current item: chillyCMS