Your IP : 216.73.216.201


Current Path : /home/balossw/www/temp/common/includes/
Upload File :
Current File : /home/balossw/www/temp/common/includes/document.inc.php

<?php
/*---------------------------------------------------------------------------
		File name ............ common/includes/document.inc.php
		Author ............... Antoine Lemoine
		Creation date ........ 02.09.2009
		Modification date .... 26.07.2010
---------------------------------------------------------------------------*/


checkLibraries('minify-2.1.3');
define('MINIFY_BASE_DIR',$global->realpath('root'));
set_include_path($global->path('lib/minify-2.1.3/lib') . PATH_SEPARATOR . get_include_path());

class DocumentHeadMeta
{
	var $charset;
	var $language;
	var $author;
	var $description;
	var $keywords;
	var $robots;
	var $cache;

	function DocumentHeadMeta() {
		$this->charset = 'ISO-8859-1';
		$this->language = 'fr';
		$this->robots = 'index,follow';
		$this->author = '';
		$this->description = '';
		$this->keywords = '';
		$this->cache = '';
	}

	function escape($value) {
		return htmlentities($value,ENT_COMPAT,$this->charset);
	}

	function toHTML() {
		if (!isset($this->robots)) $this->robots = 'noindex,nofollow';
		return ($this->charset!='' ? "<meta http-equiv=\"content-type\" content=\"text/html; charset=".$this->escape($this->charset)."\"/>\n" :'')
			. ($this->language!='' ? "<meta http-equiv=\"content-language\" content=\"".$this->escape($this->language)."\"/>\n" :'')
			. ($this->cache!='' ? "<meta http-equiv=\"cache-control\" content=\"".$this->escape($this->cache)."\">\n" : '')
			. ($this->robots!='' ? "<meta name=\"robots\" content=\"".$this->escape($this->robots)."\"/>\n" :'')
			. ($this->description!='' ? "<meta name=\"description\" content=\"".$this->escape($this->description)."\"/>\n" :'')
			. ($this->author!='' ? "<meta name=\"author\" content=\"".$this->escape($this->author)."\"/>\n" :'')
			. ($this->keywords!='' ? "<meta name=\"keywords\" content=\"".$this->escape($this->keywords)."\"/>\n" :'')
			. ($this->cache!='' ? "<meta name=\"pragma\" content=\"".$this->escape($this->cache)."\"/>\n" :'');
	}
}

function loadMinify()
{
	global $global;
	$_SERVER['DOCUMENT_ROOT'] = $global->realpath('root');
	include_once $global->realpath('lib/minify-2.1.3/lib/Minify.php');
	
	if (!class_exists('OMinify')) {
		class OMinify {
			static function minify($files, $extension, $minify=true) {
				$sources = array();
				$contents = array();
				foreach ($files as $file) {
					$filename = $_SERVER['DOCUMENT_ROOT'].$file;
					$content = @file_get_contents($filename);
					if ($extension=='css') $content = OMinify::rewriteCSSUrls($content,dirname($filename));
					$contents[] = $content;
					$sources[] = new Minify_Source(array(
						'id' => $file,
						'content' => $content,
						'contentType' => $extension=='css' ? 'text/css' : 'application/x-javascript',
					));
				}
				return $minify ? Minify::combine($sources) : implode("\n",$contents);
			}

			static function rewriteCSSUrls($css, $path) {
				global $global;
				$path_elems0 = explode('/',str_replace('\\','/',$global->realpath('var/min')));
				//$path_elems0 = explode('/',str_replace('\\','/',$global->realpath('root')/*$global->realpath('var/min')*/));
				$path_elems1 = explode('/',str_replace('\\','/',$path));
				$n = min(count($path_elems0),count($path_elems1));
				
				$new_path = '';
				while (count($path_elems0)>0 && count($path_elems1)>0) {
					if ($path_elems0[0]==$path_elems1[0]) {
						array_shift($path_elems0);
						array_shift($path_elems1);
					}
					else break;
				}
				
				$new_path = preg_replace('#/+#','/',preg_replace('#[^/]+#','..',implode('/',$path_elems0)).'/'.implode('/',$path_elems1));
				$new_path = preg_replace('/([\(\),\s\'"])/','\\\$1',$new_path);

				preg_match_all('/(url\(\s*[\'"]?\/?(.+?)[\'"]?\s*\))/i',$css,$result,PREG_SET_ORDER);
				foreach ($result as $result_i) {
					$css = str_replace($result_i[1],'url('.cleanPath($new_path.'/'.$result_i[2]).')',$css);
				}

				return $css;
			}
		}
	}
}

class DocumentHead
{
	var $default_title;
	var $title;
	var $base_href;
	var $meta;
	var $inner_html;
	var $style_includes_zone;
	var $js_filenames;
	var $external_js_filenames;
	var $css_filenames;

	function DocumentHead(&$document) {
		$this->document = &$document;
		$this->meta = new DocumentHeadMeta();
		$this->inner_html = '';
		$this->base_href = null;
		$this->style_includes_zone = '';
		$this->js_filenames = array();
		$this->external_js_filenames = array();
		$this->css_filenames = array();
	}

	function includeJsFile($filenames) {
		if (!is_array($filenames)) $filenames = array($filenames);
		foreach ($filenames as $filename) {
			if (substr($filename,0,7)=='http://' && !in_array($filename,$this->external_js_filenames)) $this->external_js_filenames[] = $filename;
			else if (file_exists(realpath($filename)) && !in_array($filename,$this->js_filenames)) $this->js_filenames[] = $filename;
		}
	}

	function includeCssFile($filenames, $prefix='', $suffix='', $media='all') {
		if (!is_array($filenames)) $filenames = array($filenames);
		if ($prefix!='' || $suffix!='' || $media!='all') {
			$links = '';
			foreach ($filenames as $filename) {
				if (file_exists(realpath($filename))) {
					$links .= "<link type=\"text/css\" href=\"".$filename."\" rel=\"stylesheet\" media=\"".$media."\"/>\n";
				}
			}
			if ($links!='') $this->style_includes_zone .= $prefix."\n".$links.$suffix."\n";
		} else {
			foreach ($filenames as $filename) {
				if (file_exists(realpath($filename)) && !in_array($filename,$this->css_filenames)) $this->css_filenames[] = $filename;
			}
		}
	}

	function includeCssFileForMedia($filename, $media) {
		$this->includeCssFile($filename,'','',$media);
	}

	function setFavicon($filename) {
		if (file_exists(realpath($filename))) $this->inner_html .= "<link href=\"".$filename."\" type=\"image/x-icon\" rel=\"shortcut icon\"/>\n";
	}

	function toHTML() {
		$this->document->minify($this->js_filenames,'js');
		$js_filenames = array_merge($this->js_filenames,$this->external_js_filenames);
		$script_includes = '';
		foreach ($js_filenames as $filename) {
			$script_includes .= "<script src=\"".$filename."\" type=\"text/javascript\"></script>\n";
		}

		$this->document->minify($this->css_filenames,'css');
		$style_includes = '';
		foreach ($this->css_filenames as $filename) {
			$style_includes .= "<link type=\"text/css\" href=\"".$filename."\" rel=\"stylesheet\" media=\"all\"/>\n";
		}

		return "<head>\n"
			. "<title>".$this->meta->escape($this->title)."</title>\n"
			. (isset($this->base_href) ? "<base href=\"".$this->base_href."\"/>\n" : '')
			. $this->meta->toHTML()
			. $style_includes
			. $this->style_includes_zone
			. $script_includes
			. $this->inner_html
			. "</head>\n";
	}
}

class DocumentBody
{
	var $inner_html;
	var $attributes;
	var $script_includes_zone;
	var $js_filenames;
	var $external_js_filenames;
	var $scripts;

	function DocumentBody(&$document) {
		$this->document = &$document;
		$this->inner_html = '';
		$this->attributes = array();
		$this->script_includes_zone = '';
		$this->js_filenames = array();
		$this->external_js_filenames = array();
		$this->scripts = '';
	}

	function setAttribute($key, $value) {
		$this->attributes[$key] = $value;
	}

	function getAttribute($key) {
		return isset($this->attributes[$key]) ? $this->attributes[$key] : null;
	}

	function appendAttribute($key, $value) {
		if (!isset($this->attributes[$key])) $this->attributes[$key] = '';
		$this->attributes[$key] .= ' '.$value;
	}

	function addScript($script) {
		$this->scripts .= $script;
	}

	function includeJsFile($filenames) {
		if (!is_array($filenames)) $filenames = array($filenames);
		foreach ($filenames as $filename) {
			if (substr($filename,0,7)=='http://' && !in_array($filename,$this->external_js_filenames)) $this->external_js_filenames[] = $filename;
			else if (file_exists(realpath($filename)) && !in_array($filename,$this->js_filenames)) $this->js_filenames[] = $filename;
		}
	}

	function toHTML() {
		$this->document->minify($this->js_filenames,'js');
		$script_includes = '';
		$js_filenames = array_merge($this->js_filenames,$this->external_js_filenames);
		foreach ($js_filenames as $filename) {
			$script_includes .= "<script src=\"".$filename."\" type=\"text/javascript\"></script>\n";
		}

		$output = "<body";
		foreach ($this->attributes as $attribute => $value) {
			$output .= ' '.$attribute.'="'.trim(str_replace('"','\"',$value)).'"';
		}
		$output .= ">\n".$this->inner_html."\n"
			. $script_includes.$this->script_includes_zone
			. ($this->scripts!= '' ? "<script type=\"text/javascript\">\n//<![CDATA[\n".trim($this->scripts)."\n//]]>\n</script>\n" : '')
			. "</body>\n";
		return $output;
	}
}

class Document
{
	var $head;
	var $body;
	var $minify;
	var $attributes;

	function Document() {
		$this->head = new DocumentHead($this);
		$this->body = new DocumentBody($this);
		$this->attributes = array();
		$this->minify = true;
	}

	function setAttribute($key, $value) {
		$this->attributes[$key] = $value;
	}

	function getAttribute($key) {
		return isset($this->attributes[$key]) ? $this->attributes[$key] : null;
	}

	function appendAttribute($key, $value) {
		if (!isset($this->attributes[$key])) $this->attributes[$key] = '';
		$this->attributes[$key] .= ' '.$value;
	}

	function includeCssFileForMedia($filename, $media) {
		$this->includeCssFile($filename,'','',$media);
	}

	function includeCssFile($filenames, $prefix='', $suffix='', $media='all') {
		$this->head->includeCssFile($filenames,$prefix,$suffix,$media);
	}

	function includeJsFile($filenames) {
		$this->body->includeJsFile($filenames);
	}

	function addScript($script) {
		$this->body->addScript($script);
	}

	function minify(&$filenames, $extension) {
		global $global;

		if ($this->minify && count($filenames)>=1) {
			$tmp_path = $global->path('var/min');
			$tmp_realpath = addEndingSlash(realpath($tmp_path));

			$infos = array();
			foreach ($filenames as $filename) {
				$infos[] = array(
					'filename' => realpath($filename),
					'filesize' => filesize($filename),
					'filemtime' => filemtime($filename),
				);
			}

			$merge_filename = md5(serialize($infos)).'.'.$extension;

			if (!file_exists($tmp_realpath.$merge_filename)) {
				loadMinify();
				$sources = array();
				foreach ($filenames as $filename) {
					$sources[] = '/'.$filename;
				}
				$minified = OMinify::minify($sources,$extension,$this->minify);
				@file_put_contents($tmp_realpath.$merge_filename,$minified);
				//@file_put_contents($tmp_realpath.$merge_filename.'.gz',gzencode($minified,9));
			}
			if (file_exists($tmp_realpath.$merge_filename)) {
				//$filenames = array('.?minified='.$merge_filename);
				$filenames = array($global->path('var/min/'.$merge_filename));
			}
		}
	}

	function toHTML() {
		$output = "<!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\"";
		foreach ($this->attributes as $attribute => $value) {
			$output .= ' '.$attribute.'="'.trim(str_replace('"','\"',$value)).'"';
		}
		$output .= " lang=\"".$this->head->meta->language."\" xml:lang=\"".$this->head->meta->language."\">\n"
			. $this->head->toHTML()
			. $this->body->toHTML()
			. "</html>\n";
		return $output;
	}
}

?>