<?php
/**
 * URI for N.u.C.o.K.e.
 * Requires N.u.C.o.K.e. v2.0.1664
 * © Hsilamot 2011
 * developed by Hidalgo Rionda
 * This library is intended to be used aside N.u.C.o.K.e. Class
 *
 * +----------------------------------------------------------------------+
 * |                           URI   v 2.0.531                            |
 * |             released under Creative Commons BY-NC-SA 3.0             |
 * |          http://creativecommons.org/licenses/by-nc-sa/3.0/           |
 * +----------------------------------------------------------------------+
 *
 * +----------------------------------------------------------------------+
 * |                              changelog                               |
 * | v1.0.1 (Hsilamot) first release                                      |
 * | v2.0.531 (Hsilamot) second release                                   |
 * +----------------------------------------------------------------------+
 *
 * @access public
 * @author Hsilamot <php@hsilamot.com>
 * @license http://creativecommons.org/licenses/by-nc-sa/3.0/
 * @version 2.0.531
 */
class URI extends NuCoKe {
	/**
	 * information about current URI
	 * @access public
	 * @var array information
	 */
	public $uri;
	/**
	 * will initialize the class
	 * @access public
	 * @version 2.0.42
	 * @param array $config array containing any extra configuration from the user
	 * @param object $nucoke previously initialized nucoke
	 * @return boolean if success
	 */
	public function __construct($config=array(),&$nucoke=null) {
		parent::__construct(null,$nucoke);
		$version = $this->version(); $this->user_agent_add('URI v'.$version[0].'.'.$version[1]); /* will add self to the user agent string */
		/* we will now set the default configuration */
		$this->config['db_uri'] = null; /* name of the NuCoKe Database */
		$this->config['table_uri'] = null; /* name of the NuCoKe Database */
		$this->config['uri_404'] = null; /* URI to request if the actual URI is not found */
		$this->config['index'] = array ('index.html'
								 ,'index.htm'
								 ,'homepage.html'
								 ,'homepage.htm'
								 ,'default.html'
								 ,'default.htm'
								 ); /* default Index pages */
		/* we will parse any configuration added from user end */
		if (!is_array($config)) { $config = array(); trigger_error('NuCoKe (URI): Unknown configuration type passed on __initialize($config)',E_USER_NOTICE); }
		foreach ($config as $name => $value) {
			switch ($name) {
				case 'db_uri':
					$this->config['db_uri'] = $value; break;
				case 'table_uri':
					$this->config['table_uri'] = $value; break;
				case 'uri_404':
					$this->config['uri_404'] = $value; break;
				default:
					trigger_error('NuCoKe (URI): Unknown configuration name \''.$name.'\'',E_USER_NOTICE); break;
			}
		}
		$uri = explode('?',$this->url);
		$uri = $uri[0];
		$this->url = $uri;
		$search = $this->info($uri);
		if ($search) {
			$search['httpcode'] = 200;
			$this->uri = $search;
			return true;
		}
		if (!$search) {
			$search = $this->info($this->config['uri_404']);
		}
		if ($search) {
			$search['httpcode'] = 404;
			$this->uri = $search;
			return true;
		}
		if (!$search) {
			return false;
			$search = array('ID'		=>	false
						,'URI'		=>	false
						,'file'		=>	false
						,'filepath'	=>	false
						,'visits'		=>	0
						,'created'	=>	time()
						,'modified'	=>	time()
						,'father'		=>	0
						,'title'		=>	'unknown'
						,'keywords'	=>	'unknown'
						,'description'	=>	'unknown'
						,'mime'		=>	'text/plain'
						,'method'		=>	array('GET','HEAD')
						,'ob'		=>	false
						,'cache'		=>	'none'
						,'expires'	=>	'0'
						,'expires_rfc' =>	date(DATE_RFC822,time())
						,'theme'		=>	false
						,'php'		=>	false
						,'wildcard'	=>	false
						,'httpcode'	=>	404
						);
		}
		return false;
	}
	/**
	 * retrieves information about a URI or false if not info is available
	 * @access public
	 * @version 1.0.10
	 * @return array ( main, minor, release )
	 */
	public function info($uri) {
		$sql = 'SELECT * FROM `'.$this->config['table_uri'].'` WHERE';
		if (substr($this->url,-1)=='/') {
			$sql .= ' `URI` = \''.$this->sql_vaccine($uri).'\'';
			foreach ($this->config['index'] as $index) {
				$sql .= ' OR `URI` = \''.$this->sql_vaccine($uri).$index.'\'';
			}
		} else {
			$sql .= ' `URI` = \''.$this->sql_vaccine($uri).'\'';
			foreach ($this->config['index'] as $index) {
				$sql .= ' OR `URI` = \''.$this->sql_vaccine($uri).'/'.$index.'\'';
			}
		}
		$sql .= ' OR `ID` = \''.$this->sql_vaccine($uri).'\'';
		$sql .= ' LIMIT 0,1';
		$search = $this->sql($sql,$this->config['db_uri']);
		if (!$search) {
			/* we now look for wildcarded URI's */
			$sql = 'SELECT * FROM `'.$this->config['table_uri'].'` WHERE `wildcard` = b\'1\'';
			$wildcards = $this->sql($sql,$this->config['db_uri']);
			if ($wildcards!==false) {
				foreach ($wildcards as $number => $item) {
					if ($item['URI']==substr($uri,0,strlen($item['URI']))) {
						$sql = 'SELECT * FROM `'.$this->config['table_uri'].'` WHERE `ID` = \''.$item['ID'].'\' LIMIT 0,1';
						$search = $this->sql($sql,$this->config['db_uri']);
					}
				}
			}
		}
		if ($search) {
			$search = $search[0];
			$search['created'] = strtotime($search['created']);
			$search['modified'] = strtotime($search['modified']);
			$search['expires_rfc'] = date(DATE_RFC822,(time()+$search['expires']));
			if ($search['ob']==chr(1)||$search['ob']=='1') { $search['ob'] = true; } else { $search['ob'] = false; }
			if ($search['theme']==chr(1)||$search['theme']=='1') { $search['theme'] = true; } else { $search['theme'] = false; }
			if ($search['php']==chr(1)||$search['php']=='1') { $search['php'] = true; } else { $search['php'] = false; }
			if ($search['wildcard']==chr(1)||$search['wildcard']=='1') { $search['wildcard'] = true; } else { $search['wildcard'] = false; }
			if (is_file($this->config['path'].$search['file'])) {
				$search['filepath'] = $this->config['path'].$search['file'];
			} else {
				$search['filepath'] = false;
			}
			$sql = 'UPDATE `'.$this->config['table_uri'].'` SET `visits` = (`visits`+1) WHERE `ID` = \''.$search['ID'].'\'';
			$this->sql($sql);
		} else {
			return false;
		}
		return $search;
	}
	/**
	 * will return the version of the class in Mayor, Minor and Release
	 * @access public
	 * @version 1.0.6
	 * @return array ( main, minor, release )
	 */
	public function version() {
		return array( 2, 0, 531 );
	}
}
?>
