Location: PHPKode > projects > Simple Way to Usenet > swun/libs/core/pgen/browsing.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 browsing {
	
	private static $active;
	private static $PARGS;
	
	
	public static function special_events(){
		self::is_captchar();
		self::is_logout();
		self::is_logswap();
		self::is_login();
		self::is_dload();
		self::is_qmedia();
	}
	
	/**
	 * Enforce antibot image output
	 * on any request if $_GET['captcha'] is defined
	 */
	private static function is_captchar(){
		@define('CAPTCHAR_URL', '?captcha=');
		if ( ! isset($_GET['captcha']) ){ return; }
		list($realm,$ticket) = split(';', $_GET['captcha']);
		$realm = new realm($realm);
		$key = $realm->ticket_chaldata($ticket);
		$img = new captcha();
		$img->sendimage($key);
		exit;
	}
	
	private static function is_logout(){
		if ( ! isset($_POST['logout']) AND !isset($_GET['logout']) ){ return; }
		if ( isset($_POST['logout']) ){
			$realm = $_POST['logout'];
			unset($_POST['logout']);
		} elseif ( isset($_GET['logout']) ){
			$realm = $_GET['logout'];
			unset($_GET['logout']);
		}
		$realm = new realm($realm);
		$realm->logout();
	}
	
	private static function is_logswap(){
		if ( isset($_POST['logswap']) AND !empty($_POST['switchto']) ){
			list($realm,$ruid) = split('=', $_POST['switchto']);
			$realm = new realm($realm);
			$err = $realm->switchto($ruid);
			//or add a page event maybe
			if ( TRUE !== $err ){ errors::raise('Switchto Failed : '.$err, CORE_LOG_ERROR, 'REALM'); }
			unset($_POST['switchto']);
		}
	}
	
	private static function is_login(){
		if ( isset($_GET['login']) ){
			$realm = new realm($_GET['login']);
			$chal=NULL;
			if ( isset($_GET['chal']) ){ $chal = $_GET['chal']; }
			unset($_GET['login'], $_GET['chal']);
			$realm->login($chal, NULL, TRUE);
		}
	}
		
	/**
	 * Enforce file download
	 * on any request if $_GET['file'] is defined
	 */
	private static function is_dload(){
		if ( isset($_GET['file']) ){
			die('FILE DLOADER');
			exit;
		}
	}
	
	 //TODO Enforce mediaq cache file output on any request if $_GET['qmedia'] is defined
	private static function is_qmedia(){
		if ( isset($_GET['qmedia']) ){
			die('QMEDIA DLOADER');
			exit;
		}
	}
	
	public static function get_active(){
		if (isset(self::$active) ){ return self::$active; }
		$pv = configs::get('pagegen', 'pagevar');
		if ( self::use_modrewrite() ){
			self::modrewrite();
			//echo 'MODREWRITE_RESULT = '.self::$active.'<br>';exit;
		} elseif ( isset($_GET[$pv]) ){ 
			self::$active = $_GET[$pv];
			self::detectargs(self::$active);
		} else {
			errors::raise('MODRewrite is disabled, and pagevar is not set, falling back to homepage', CORE_LOG_WARNING, 'PGEN');
			self::$active = configs::get('pagegen', 'homepid');
			self::detectargs(self::$active);
		}
		errors::raise('Browsing detected active page : '.self::$active, CORE_LOG_NOTICE, 'PGEN');
		return self::$active;
	}
	
	public static function use_modrewrite(){ return isset($_SERVER['SCRIPT_URL']); }
	
	public static function basedir(){
		$buf = pathinfo($_SERVER['SCRIPT_FILENAME']);
		$dc = ereg_replace("/$", '', $_SERVER['DOCUMENT_ROOT']);
		return str_replace($dc, '', $buf['dirname']);
	}
	
	
	public static function activeurl(){
		$base = self::basedir();
		$path = pages::get_pathto(self::get_active());
		if ( !empty($base) ){ return $base.'/'.$path; }
		return '/'.$path;
	}
	
	/**
	 * Pageid is not set lets try to detect it from
	 * the requested url.
	 */
	private static function modrewrite(){
		$rewrite = str_replace(browsing::basedir(), '', $_SERVER['SCRIPT_URL']);
		/*
		$buf = pathinfo($_SERVER['SCRIPT_FILENAME']);
		echo 'REQUEST_URI = '.$_SERVER['SCRIPT_URL'].'<br>';
		echo 'SCRIPT_FILE = '.$buf['basename'].'<br>';
		echo 'SCRIPT_DIRECTORY = '.$buf['dirname'].'<br>';
		echo 'VHOST_BASE = '.$_SERVER['DOCUMENT_ROOT'].'<br>';
		echo 'REWRITE_ARG = '.$rewrite.'<br>';
		*/
		$buf = split('/', ereg_replace('^/', '', $rewrite));
		foreach( $buf as $lvl => $name){
			//echo "Checking page $name on depth $lvl<br/>";
			if ( FALSE !== ( $pids = pages::getby_namelvl($name, $lvl)) ){
				if ( !is_array($pids) ){ $pids = Array($pids); }
				//echo "Page exists =>".print_r($pids, TRUE)."<br/>";
				if ( $lvl === 0 ){
					$o['pid'] = $pids[0];
					$o['lvl'] = $lvl;
				} else {
					foreach($pids as $c => $pid){
						$parent = pages::get_parent($pid);
						//echo 'PARENT OF '.$pid.' => '.$parent.'<br>';
						if ( !isset($buf[($lvl-1)]) ){
							echo 'prev lvl not set<br>';
							if ( isset($o) ){
								self::detectargs($o['pid'], $buf);
								self::$active = $o['pid'];
								return; 
							} else { 
								errors::raise('The parent\'s level "'.($lvl-1).'"of page "'.$name.'" is not defined in request path, 404', CORE_LOG_ALERT, 'PGEN');
							}
						} elseif ( pages::get_title($parent) == $buf[($lvl-1)] ){
							//echo 'parent name == prev lvl name <br>';
							$tmp = $pid;
							break;
						}
					}
					if ( !isset($tmp) AND !isset($o) ){
						errors::raise('Page '.$page.' with parent '.$parent.' could not be found, 404', CORE_LOG_ALERT, 'PGEN'); 
					} elseif ( isset($tmp) ){
						$o['pid'] = $tmp;
						$o['lvl'] = $lvl;
					} elseif ( isset($o) ){
						self::detectargs($o['pid'], $buf);
						self::$active = $o['pid'];
						return;
					}
				}
			} elseif ( isset($o) ){
				self::detectargs($o['pid'], $buf);
				self::$active = $o['pid'];
				return;
			} else {
				self::detectargs(configs::get('pagegen', 'homepid'), $buf);
				self::$active = configs::get('pagegen', 'homepid');
				errors::raise('Page "'.$name.'" was not found on first level, using default page', CORE_LOG_WARNING, 'PGEN');
				return;
			}
		}
		self::detectargs($o['pid'], $buf);
		self::$active = $o['pid'];
	}
	
	private static function detectargs($pid, $buf=NULL){
		$pargs = pages::get_args($pid);
		if( empty($pargs) ){ 
			errors::raise('This page take no argument.', CORE_LOG_NOTICE, 'PGEN');
			return; 
		}
		
		$lvl = pages::get_depth($pid);
		foreach($pargs as $k => $arg){
			$k++;
			if ( empty($arg) ){ break; }
			if ( ereg('\:', $arg) ){
				
				$arg = split(':', $arg);
				if ( self::use_modrewrite() ){
					if ( isset($buf[($lvl+$k)]) ){
						self::$PARGS[$arg[0]] = $buf[($lvl+$k)];
					} else {
						errors::raise('Missing Page Argument "'.$arg[0].'" Setting it to default "'.$arg[1].'"', CORE_LOG_NOTICE, 'PGEN');
						self::$PARGS[$arg[0]] = $arg[1];	
					}		
				} elseif ( ! isset($_GET[$arg[0]]) ){
					errors::raise('Missing Page Argument "'.$arg[0].'" Setting it to default "'.$arg[1].'"', CORE_LOG_NOTICE, 'PGEN');
					self::$PARGS[$arg[0]] = $arg[1];					
				} else {
					self::$PARGS[$arg[0]] = $_GET[$arg[0]];
				}
				
			} elseif ( ereg("=", $arg) ){
				
				$arg = split("=", $arg);
				errors::raise('Forced Page Argument "'.$arg[0].'" Setting it to "'.$arg[1].'"', CORE_LOG_NOTICE, 'PGEN');
				self::$PARGS[$arg[0]] = $arg[1];
				
			} elseif ( self::use_modrewrite() ){ 
					if ( isset($buf[($lvl+$k)]) ){
						self::$PARGS[$arg] = $buf[($lvl+$k)];
					} else {
						errors::raise('Missing Page Argument "'.$arg.'", expected on path level "'.($lvl+$k).'"', CORE_LOG_NOTICE, 'PGEN');
						self::$PARGS[$arg] = NULL;				
					}
			} elseif ( isset($_GET[$arg]) ){
				self::$PARGS[$arg] = $_GET[$arg];
			} else {
				errors::raise('Missing Page Argument "'.$arg.'", expected to be set on $_GET['.$arg.']', CORE_LOG_NOTICE, 'PGEN');
			}
		}
	}
	
	public static function setargs($var, $val){
		self::$PARGS[$var] = $val;
	}
	
	public static function getargs($var=NULL){
		if ( ! isset(self::$PARGS) ){ return FALSE; }
		if ( $var === NULL ){ return self::$PARGS; }
		if ( ! isset(self::$PARGS[$var]) ){ return FALSE; }
		return self::$PARGS[$var];
	}
	
	public static function topmenu_bydepth($active){
		$depth = pages::get_depth($active);
		$parent = pages::get_parent($active);
		//echo 'topmenu for page '.$active."\n";
		if ( $depth != 0 ){	//NOT ON ROOT
			if ( pages::got_childpage($active) ){
				//echo 'page got childs page and is not on root, we show active, child level';
				$o[] = self::getlevel($parent, $active);
				$o[] = self::getlevel($active, NULL);
			} else {
				//echo 'page got no childs page and is not on root, we show parent and active level';
				$o[] = self::getlevel(pages::get_parent($parent), $parent);
				$o[] = self::getlevel($parent, $active);
			}
		} else {				//ON ROOT
			//echo "page is on root, we show active level\n";
			$o[] = self::getlevel($parent, $active);
			//echo "and got childs we show them to";
			if ( pages::got_childpage($active) ){
				$o[] = self::getlevel($active, NULL);
			}
		}
		return $o;
	}
	
	private static function getlevel($parent, $active){
		$buf = pages::get_childs($parent);
		foreach($buf as $k => $pid){
			if ( !pages::is_hidden($pid) ){
				$tmp['title'] = pages::get_title($pid);
				$tmp['path'] = pages::get_pathto($pid);
				$tmp['pid'] =  $pid;
				$tmp['active'] =  ($pid == $active);
				$o[] = $tmp;
			}
		}
		if (!isset($o) ){ return FALSE; }
		return $o;
	}
	
}
Return current item: Simple Way to Usenet