Location: PHPKode > scripts > Noostr > _engine/classes/Template.php
<?php defined('SYSPATH') OR die('No direct access to this file is allowed.');
/**
 * This is the main templating class.
 *
 * @package Noostr
 * @subpackage Classes
 */

class Template {
	public $filecount;
	public $filefail;
	public $httpstatus;
	private $data = array();
	private $startpage;
	private $served_footer;
	private $served_header;
	private $showing_login;

/**
 * Constructor for the class. We set a few initial values and deduce the
 * filename we will need for this page's template.
 *
 * @global Site $site The Site object
 */
	public function __construct() {
		global $site;
		$this->served_footer = false;
		$this->served_header = false;
		$this->showing_login = false;
		$this->httpstatus = 200;
		$this->filecount = 0;
		$this->filefail = 0;
		$this->data['page'] = '';
		// First build template names like "path1-path2-path3.php".
		for ($i = 0, $c = count($site->uri); $i < $c; $i++) {
			if ($site->uri[$i] != '') {
				$this->data['page'] .= $site->uri[$i].'-';
			}
		}
		// "admin-" is special, it needs to be removed from the name
		if (strtolower(substr($this->data['page'], 0, 6)) == strtolower(ADMIN).'-') {
			$this->data['page'] = substr($this->data['page'], 6);
		}
		// If the filename ends with a hyphen, remove it.
		if (substr($this->data['page'], -1) == '-' || $this->data['page'] == '-') {
			$this->data['page'] = substr($this->data['page'], 0, strlen($this->data['page']) - 1);
		}
		// If the filename is empty, use the default site start page.
		if ($this->data['page'] == '') {
			$this->data['page'] = $site->startpage;
		}
	}

	public function start() {
		global $page;
		$file = $page->find('uid', $page->find('url', '/'.$this->data['page'], 'tid'), 'url');
		if ($file == '') {
			// If no page template is set, use the normal "page" var
			$file = $this->data['page'];
		}
		// Strip the leading slash, if any.
		if (substr($file, 0, 1) == '/') {
			$file = substr($file, 1);
		}
		$this->get_template_file('functions');
		$this->data['parentname'] = null;
		$this->get_template_file(strtolower($file));
	}
	public function get_header($name = '') {
		if (!$this->served_header) {
			$this->served_header = true;
			$this->get_template_file('header');
		}
	}
	public function get_footer($name = '') {
		if (!$this->served_footer) {
			$this->served_footer = true;
			$this->get_template_file('footer');
		}
	}
	public function get_sidebar($name = '') {
		$this->get_template_file('sidebar');
	}
	public function get_submission_form($type) {
		global $db;
		$legal = array();
		$sql = 'select stub from '.PREFIX.'classes';
		$result = $db->query($sql);
		// Convert $result into a better array.
		for ($i = 0, $c = count($result); $i < $c; $i++) {
			$legal[] = trim(strtolower($result[$i]['stub']));
		}
		if (in_array(trim(strtolower($type)), $legal)) {
			$this->get_template_file('form-'.$type);
		}
	}
	public function get_template_file($file, $default = false) {
		global $ns, $error, $formfields;
		$get_template_file_return = false;
		$fullname = TEMPLATEFOLDER.'/'.$file.'.php';
		if ($default) {
			$fullname = TEMPLATEDEFAULTFOLDER.'/'.$file.'.php';
		}
		//echo "FILE: $fullname\n<br />";
		if (file_exists($fullname)) {
			$this->filecount++;
			if (substr(strtolower($file), 0, 6) != 'header' && substr(strtolower($file), 0, 6) != 'footer' && substr(strtolower($file), 0, 9) != 'functions') {
				$this->data['pagename'] = $file;
				if ($this->data['parentname'] == null) {
					$this->data['parentname'] = $file;
				}
			}
			$ns->load($this->get_template_vars());
			include ($fullname);
			$get_template_file_return = true;
		} else {
			if (strrpos($file, '-') !== false) {
				// recursive loader, as long as $file is hyphenated
				$file = substr($file, 0, strrpos($file, '-'));
				$get_template_file_return = $this->get_template_file($file);
			} else {
				// Special cases here
				switch ($file) {
					case 'logout':
						global $user, $site;
						$user->logout($site->startpage);
						break;
					default:
						if (!$default) {
							if ($file != 'index' && !$this->get_template_file('index')) {
								$this->get_template_file($file, true);
							}
						} else {
							$this->filefail++;
							$this->httpstatus = 404;
							header($_SERVER["SERVER_PROTOCOL"]." 404 Not Found");
							$this->get_template_file('404');
						}
						break;
				}
			}
		}
		return $get_template_file_return;
	}

	public function show($item) {
		echo $this->get($item);
	}
	public function get($item) {
		$return = '';
		if (is_array($this->data)) {
			if (array_key_exists($item, $this->data)) {
				$return = $this->data[$item];
			}
		}
		if ($return == '') {
			// Special cases, since they may change during execution
			switch (strtolower($item)) {
				case 'querycount':
					global $db;
					$return = $db->count;
					break;
				case 'querylist':
					global $db;
					unset($return);
					$return = array();
					foreach ($db->querylist as $key => $value) {
						$return[] = $key.' :: '.$value;
					}
					break;
				case 'timer':
					$return = timer(microtime(true), true);
					break;
				case 'filecount':
					$return = $this->filecount;
					break;
				case 'filefail':
					$return = $this->filefail;
					break;
			}
		}
		return $return;
	}

	private function get_template_vars() {
		global $acl, $page, $site, $user;
		$return = array();
		$return['page'] = $page->tpl_getinfo();
		$return['querystring'] = $site->tpl_getquerystringinfo();
		$return['site'] = $site->tpl_getinfo();
		$return['template'] = $this->tpl_getinfo();
		$return['template']['querycount'] = $this->get('querycount');
		$return['template']['timer'] = $this->get('timer');
		$return['template']['filecount'] = $this->get('filecount');
		$return['template']['filefail'] = $this->get('filefail');
		$return['user'] = $user->tpl_getinfo();
		$return['user']['loggedin'] = $user->loggedin;
		if ($user->roleid == $acl->admin) {
			$return['pages'] = $page->tpl_getinfo(true);
		}
		return $return;
	}

	public function tpl_getinfo() {
		return $this->data;
	}
}
Return current item: Noostr