Location: PHPKode > projects > Simple Way to Usenet > swun/libs/core/pgen/pagegen.class.php
<?php
/**
 *  
 * @author  Benjamin Gillissen <hide@address.com>
 * 
 *	**************************************************************

	Copyright (C) 2009  Benjamin Gillissen
	
	This program is free software; you can redistribute it and/or
	modify it under the terms of the GNU General Public License
	as published by the Free Software Foundation; either version 2
	of the License, or (at your option) any later version.
	
	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details at:
	http://www.gnu.org/copyleft/gpl.html

 *	**************************************************************
 */
class pagegen {
	
	private static $pageid;
	private static $events;
	
	public function __construct(){
		chrono::reqstart();
		browsing::special_events();
	}
	
	public function set_active($pageid){ self::$pageid = $pageid; }
	
	public function checks(){
		if ( ! pages::exists(self::$pageid) ){ return errors::getnew('Requested page do not exists !', CORE_LOG_ALERT, 'PGEN'); }
		$realm = pages::get_realm(self::$pageid);
		if ( empty($realm) ){ return TRUE; } //empty page realm, so is public
		$realm = new realm($realm);
		if ( FALSE === $realm->ispublic('pages', 'read', self::$pageid) ){
			//so page is not readable by everybody
			if ( ! $realm->isloged() ){ $realm->authenticate(); }
			while ( FALSE === $realm->checks('pages', 'read', self::$pageid) ){
				if ( ! $realm->isloged() ){
					$eid = self::add_event(langs::dico_translate('realmsg_noaccess', NULL, 'realm'));
				} else {
					$eid = self::add_event(langs::dico_translate('realmsg_lognoaccess', NULL, 'realm'));
				}
				if ( isset($_GET['ajax']) ){ ajax::EnforceReq('dummy'); } 
				$realm->login('html');
				self::del_event($eid); 
			}
		}
		//so user requested a valid page and can see it, we let index.php process the build.
		return TRUE;
	}
	
	
	public function build($pdata=NULL){
		langs::load();
		switch(pages::get_type(self::$pageid) ){
			case 'a' : 	$this->genby_intphp($pdata);
						break;
			case 'b' : 	$this->genby_intobj($pdata);
						break;
			case 'c' : 	$this->genby_extphp();
						break;
			case 'd' : 	$this->genby_inchtml();
						break;
			case 'e' : 	$this->genby_intredir($pdata);
						break;
			case 'f' : 	$this->genby_extredir();
						break;
			default : 	errors::raise('Unknow generation method for page "'.browsing::get_active().'"', CORE_LOG_ALERT, 'PGEN');
						break;
		}
	}
	
	private function genby_intphp($pdata){
		//php script that use CORE scripting scheme
		$script = PATH_PAGES.'/'.pages::get_conf(self::$pageid);
		$args = browsing::getargs();
		$hdata = $this->get_headerdata();
		if ( !file_exists($script) ){
			errors::raise('The script to include for this page was not found "'.$script.'"', CORE_LOG_ALERT, 'PGEN');
		}
		$content = include($script);
		if ( ! @constant('THEME_HEADER_ISSENT') ){ self::$theme->send_header($hdata); }
		if ( !$content ){ errors::raise('Something goes wrong with the included page', CORE_LOG_WARNING, 'PGEN');
		} elseif ( $content != 1 ) { echo $content;	}
		$hdata = $this->get_footerdata();
		if ( ! @constant('THEME_FOOTER_ISSENT') ){ self::$theme->send_footer($hdata); }
	}
	
	private function genby_intobj($pdata){
		//php object compatible with CORE pagegen
		$obj = pages::get_conf( self::$pageid );
		$class = 'pg_'.$obj;
		if ( FALSE === class_exists($class) ){
			errors::raise("The object defined to build this page was not found.", CORE_LOG_ALERT,'PGEN');return;
		}
		$pgobj = new $class(self::$pageid, browsing::getargs() );
		//FIXME gotinstance of pg_helper in pgobj ??
		$pgobj->send_header();
		$pgobj->send_content($pdata);
		$pgobj->send_footer();
	}
	
	private function genby_extphp(){
		//some php external scripts, we give them control.
		//forconf : script=path/to/script.php NOR toset=arg1,val1;arg2,val2
		//will probably output result via stdout, but need to check returned value too
	}
	
	private function genby_inchtml(){
		//a simple html file to output
		//forconf : file=path/to/file.html
		//we send header
		//we output file (charsets ?? - local always utf8)
		//we send footer
	}
	
	private function genby_intredir($pdata){
		$pid = pages::get_conf(self::$pageid);
		if ( $pid == self::$pageid ){
			errors::raise('Internal Redirection to the same pageID !', CORE_LOG_ALERT, 'PGEN');
		}
		if ( ! pages::exists($pid) ){
			errors::raise('Internal Redirection to an invalid pageID !', CORE_LOG_ALERT, 'PGEN');
		}
		self::set_active($pid);
		if ( ! $this->checks() ){
			errors::raise('Internal Redirection error, destination page prechecks failure !', CORE_LOG_ALERT, 'PGEN');
		}
		errors::raise('Applying Internal Redirection to pageID '.$pid, CORE_LOG_ALERT, 'PGEN');
		return  $this->build($pdata);
	}
	
	private function genby_extredir(){
		//a redirection to a specified url using headers
		$url = pages::get_conf(self::$pageid);
		headers("Location: $url");
		exit;
	}

	private function get_headerdata(){
		$o['charset'] = langs::charset();
		$o['lang'] = langs::lang();
		$o['langs'] = langs::getlangs();
		$o['theme'] = themes::get_active();
		$o['themes'] = themes::gethemes();
		if ( FALSE != @constant('CORE_AUTH') ){
			$o['pagerealm'] = pages::get_realm(self::$pageid);
			$o['loginurl'] = 'login/'.$o['pagerealm'];
			$o['logouturl'] = "?logout=".$o['pagerealm'];
			$realm = new realm($o['pagerealm']);
			$o['isloged'] = $realm->isloged();
			if ( $o['isloged'] ){
				$o['logedas'] = $realm->loginbyuid($realm->logedas());
			}
		}
		$buf = str_replace(browsing::basedir(), '', browsing::activeurl());
		$buf = split('/', ereg_replace("^/", '', $buf));
		foreach($buf as $lvl => $page){
				$pid = pages::getby_namelvl($page, $lvl);
				$tmp['name'] = langs::dbtranslate('pages', $pid[0], 'title');
				if ( FALSE === $tmp['name'] ){ $tmp['name'] = $page; }
				if ( !isset($o['pagepath']) ){	
					$tmp['path'] = $page;
				} else {						
					$tmp['path'] = $o['pagepath'][$lvl-1]['path'].'/'.$page;
				}
				$o['pagepath'][$lvl] = $tmp;
		}
		$o['topmenu'] = browsing::topmenu_bydepth(self::$pageid);
		unset($buf, $tmp, $lvl);
		$o['title'] = SITE_TITLE.' - '.pages::get_title(self::$pageid);
		return $o;
	}
	
	private function get_footerdata(){
		$o['charset'] = langs::charset();
		$o['lang'] = langs::lang();
		$o['langs'] = langs::getlangs();
		$o['pagerealm'] = pages::get_realm(self::$pageid);
		$o['basedir'] = browsing::basedir();
		if ( FALSE != @constant('CORE_AUTH') ){
			$o['loginurl'] = 'login/'.$o['pagerealm'];
			$o['logouturl'] = "?logout=".$o['pagerealm'];
			$realm = new realm($o['pagerealm']);
			$o['isloged'] = $realm->isloged();
			if ( $o['isloged'] ){
				$o['profurl'] = 'profile/'.$o['pagerealm'].'/'.$realm->logedas();
				$o['logedas'] = $realm->loginbyuid($realm->logedas());
			}
		}
		$o['author'] = 'someone at somehost dot org';
		$o['contact_author'] = 'hide@address.com';
		$o['reqtimes'] = chrono::stopall();
		$ajax = new ajax();
		$o['JAVASCRIPT'] = $ajax->get_Javascript();
		return $o;
	}

	public static function add_event($event){
		$k = CORE::hash(NULL, 5); 
		self::$events[$k] = $event; 
		return $k;
	}
	
	public static function del_event($eid){
		unset(self::$events[$eid]); 
	}
	
	public static function getevents(){ 
		$o = self::$events;
		self::$events = Array();
		return $o;
	}

}
Return current item: Simple Way to Usenet