<?php
/* Ulyxex version 1.6.7.8 ***************/
/* code http://ulyxex.logz.org **********/
/* Andre Lozano http://andre-lozano.org */
//~ class Html extends Htmlz {
	//~ to keep retro-compatibility with special scripts
	//~ because name "Html" as a class produce a warning in php7 ?
//~ }
class XmlSitemap {
	public function urlset($val=null){
		$result = "<?xml version=\"1.0\" encoding=\"UTF-8\"?".">\n";
		$result .= "<urlset xmlns=\"http://www.sitemaps.org/schemas/sitemap/0.9\">\n";
		$result .= $val."\n</urlset>\n";
		return $result;
	}
	function tag($tag=null,$val=null){;
		if ($val) return "<".$tag.">".$val."</".$tag.">";
	}
	/* xml sitemap tags */
	public function url($val=null){
		return $this->tag("url",$val);
	}
	public function loc($val=null){
		return $this->tag("loc",$val);
	}
	public function lastmod($val=null){
		return $this->tag("lastmod",$val);
	}
	public function changefreq($val=null){
		return $this->tag("changefreq",$val);
	}
	public function priority($val=null){
		return $this->tag("priority",$val);
	}
}
class XmlRss {
	public function rss($val=null,$params=null){
		$result = "<?xml version=\"1.0\" encoding=\"UTF-8\"?".">\n";
		$result .= "<rss version=\"2.0\">\n<channel>\n";
		$result .= $val."\n</channel>\n</rss>\n";
		return $result;
	}
	function tag($tag=null,$val=null){
		if ($val) return "<".$tag.">".$val."</".$tag.">";
	}
	/* rss sitemap tags */
	public function item($val=null){
		return $this->tag("title",$val);
	}
	public function title($val=null){
		return $this->tag("title",$val);
	}
	public function link($val=null){
		return $this->tag("link",$val);
	}
	public function author($val=null){
		return $this->tag("lastmod",$val);
	}
	public function description($val=null){
		return $this->tag("description",$val);
	}
	public function pubdate($val=null){
		return $this->tag("pubdate",$val);
	}
}
class Htmlz {
	public int $xml;
	public int $htmlType;
	//~ SP_ is "&nbsp;"
	
	function __construct(){
		$this->xml = false;
		if (defined('DOCUTYPE')) {
			if (DOCUTYPE == 1) {
				$this->htmlType = 1;
				$this->xml = true;
			} else if (DOCUTYPE == 4) {
				$this->htmlType = 4;
			} else {
				$this->htmlType = 5;
			}
		} else {
			$this->htmlType = 5;
		}
	}
	public function extUploadAllowed() {
		$arr = array("7z","aac","ai","aif","aifc","aiff","arj","asf","asr","asx","au","avi","bin","bmp","cda","dcr","doc","dot","eps","flv","gif","gz","hqx","html","ico","jfif","jpe","jpeg","jpg","m4a","m4b","m4v","mid","mov","mp2","mp3","mp4","mpa","mpe","mpeg","mpg","odb","odc","odf","odg","odi","odm","odp","ods","odt","ogg","pdf","png","pps","ppt","ps","psd","ra","ram","rar","rm","rmi","rtf","sea","sit","sla","stc","std","sti","stw","swf","sxc","sxd","sxg","sxi","sxm","sxw","tar","tgz","tif","tiff","torrent","txt","vcf","wav","wcm","wdb","webm","webp","wks","wma","wmf","wmv","wps","wri","wrl","wrz","xaf","xbm","xla","xlc","xlm","xls","xlt","xlw","xml","xof","xpm","xsd","xsl","xwd","xspf","zip","pict");
		return $arr;
	}
	/* ulyxex specific functions */
	public function ulyxCredits($userId=1,$firstLine=true,$secondLine=true){
		$t = new Translate();
		$version =  defined('VERSION')?VERSION:"1.6.6.6";
		$firstLine = ($firstLine)
			?
				$this->p(
					$this->span($this->ahref($this->root_url("index.php?k=contact&amp;v=".$userId),$t->w("Send email")),$this->v("class","contactsite"))
					." ".
					$this->span($this->ahref($this->root_url("login.php"),$t->w("Login")),$this->v("class","linklogin"))
				,$this->v("class","contact"))
			:
				"";
				
		$secondLine = ($secondLine)
			?
				$this->p(
					$this->ahref("http://ulyxex.logz.org",$t->wr("Made with Ulyxex").$version),$this->v("class","credits")
				)
			:
				"";
				
		return $firstLine.$secondLine;
	}
	public function countQ(){
		return $this->p("queries = ".$_SESSION['countqueries']);
    }
	/* html */
	public function html($val=null,$params=null){
		// ulyxex default use html version 5
		if ($this->htmlType == 1) {
			return $this->xhtml1($val,$params);
		} else if ($this->htmlType == 4){
			return $this->html4($val,$params);
		} else {
			return $this->html5($val,$params);
		}
	}
	public function html5($val=null,$params=null){
		$params = $this->_($params);
		$result = "<!DOCTYPE html>\n";
		$result .= "<html".$params.">";
		$result .= $val."</html>";
		return $result;
	}
	public function html4($val=null,$params=null){
		//~ no more supported and no UTF-8
		$params = $this->_($params);
		$result = "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\" \"http://www.w3.org/TR/1999/REC-html401-19991224/loose.dtd\">\n";
		$result .= "<html".$params.">";
		$result .= $val."</html>";
		return $result;
	}
	public function xhtml1($val=null,$params=null){
		$params = $this->_($params);
		$result = "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.1//EN\" \"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd\">\n";
		$result .= "<html xmlns=\"http://www.w3.org/1999/xhtml\" ";
		$result .= $this->v("xml:lang",$this->getLang());
		$result .= $this->v("xmlns:xsi","http://www.w3.org/2001/XMLSchema-instance");
		$result .= $this->v("xsi:schemaLocation","http://www.w3.org/1999/xhtml \nhttp://www.w3.org/MarkUp/SCHEMA/xhtml11.xsd");
		$result .= ">\n";
		$result .= $val."</html>\n";
		return $result;
	}
	/* regular tags */
	function v($name="",$value=""){
		//~ function v(alue) like value-name has this value...
		//~ value can be null or empty or 0 or "" or everything
		if (!empty($name)) {
			//~ write: name="value"+space (with double quote)
			$string = $name."=".$this->quot($value);
			$string = $this->end_($string);
			return $string;
		}
	}
	function _v($name="",$value=""){
		//~ add space before
		if (!empty($name)) {
			return $this->_($this->v($name,$value));
		}
	}
	function quot($val="",$single=false){
		//~ = "val" or 'val' double or single quote
		$string = ($single)?"'".$val."'":"\"".$val."\"";
		return $string;
	}
	function _($val=null){
		// add space at begin
		if ($val) {
			$val = trim($val);
			return " ".$val;
		}
	}
	function end_($val=null){
		// add space at end
		if ($val) {
			$val = trim($val);
			return $val." ";
		}
	}
	function _sp($val=null){ // old name
		return $this->_($val);
	}
	public function nbsp($str=""){
		//~ remplace all break space
		$str = preg_replace('/\s/', SP_, $str);
		return $str;
	}
	function tag($tag=null,$val=null,$params=null,$endline=false){
		$params = $this->_($params);
		$ln = ($endline)?$this->comment("\n"):"\n";// end line in comment like <!-- \n -->
		if ($val) return "<".rtrim($tag.$params).">".$val."</".$tag.">".$ln;
	}
	public function head($val=null){
		return $this->tag("head",$val);
	}
	public function body($val=null,$params=null){
		return $this->tag("body",$val,$params);
	}
	public function bodydata($val=null,$params=null){// ulyxex frontend body
		$content = $this->div($val,$this->v("id","page-content").$this->v("class","pagecontent"));
		return $this->body($content,$this->v("class","pagecontent").$params);
	}
	public function bodyadmin($val=null,$params=null){// ulyxex backend body
		$content = $this->div($val,$this->v("id","page-content").$this->v("class","admincontent"));
		return $this->body($content,$this->v("class","admincontent").$params);
	}
	public function title($val=null){
		return $this->tag("title",$val);
	}
	public function header($val=null,$params=null){
		return $this->tag("header",$val,$params);
	}
	public function main($val=null,$params=null){
		return $this->tag("main",$val,$params);
	}
	public function article($val=null,$params=null){
		return $this->tag("article",$val,$params);
	}
	public function articles($val=null,$params=null){
		$params = $this->v("class","articles").$params;
		return $this->main($val,$params);
	}
	public function aside($val=null,$params=null){
		return $this->tag("aside",$val,$params);
	}
	public function mark($val=null,$params=null){
		return $this->tag("mark",$val,$params);
	}
	public function nav($val=null,$params=null){
		return $this->tag("nav",$val,$params);
	}
	public function footer($val=null,$params=null){
		return $this->tag("footer",$val,$params);
	}
	public function h1($val=null,$params=null){
		return $this->tag("h1",$val,$params);
	}
	public function h2($val=null,$params=null){
		return $this->tag("h2",$val,$params);
	}
	public function h3($val=null,$params=null){
		return $this->tag("h3",$val,$params);
	}
	public function h4($val=null,$params=null){
		return $this->tag("h4",$val,$params);
	}
	public function h5($val=null,$params=null){
		return $this->tag("h5",$val,$params);
	}
	public function h6($val=null,$params=null){
		return $this->tag("h5",$val,$params);
	}
	public function p($val=null,$params=null){
		return $this->tag("p",$val,$params);
	}
	public function b($val=null,$params=null){
		return $this->tag("strong",$val,$params);
	}
	public function pre($val=null,$params=null){
		return $this->tag("pre",$val,$params);
	}
	public function code($val=null,$params=null){
		return $this->tag("code",$val,$params);
	}
	public function div($val=null,$params=null){
		return $this->tag("div",$val,$params);
	}
	public function ol($val=null,$params=null){
		return $this->tag("ol",$val,$params);
	}
	public function ul($val=null,$params=null){
		return $this->tag("ul",$val,$params);
	}
	public function li($val=null,$params=null){
		return $this->tag("li",$val,$params);
	}
	public function table($val=null,$params=null){
		return $this->tag("table",$val,$params);
	}
	public function thead($val=null,$params=null){
		return $this->tag("thead",$val,$params);
	}
	public function tbody($val=null,$params=null){
		return $this->tag("tbody",$val,$params);
	}
	public function tfoot($val=null,$params=null){
		return $this->tag("tfoot",$val,$params);
	}
	public function th($val=null,$params=null){
		return $this->tag("th",$val,$params);
	}
	public function tr($val=null,$params=null){
		return $this->tag("tr",$val,$params);
	}
	public function td($val=null,$params=null){
		return $this->tag("td",$val,$params);
	}
	public function fieldset($val=null){
		return $this->tag("fieldset",$val);
	}
	public function legend($val=null){
		return $this->tag("fieldset",$val);
	}
	/* unique tags or with no \n */
	public function tit($val=null) {
		$val = ($val)?$this->v("title",$this->safeVal($val)):null;
		if ($val) return $val;
	}
	public function alt($val=null) {
		return $this->tit($val);
	}
	public function label($val=null,$for=null,$params=null){
		$for = ($for)?$this->v("for",$for):"";
		return "<label ".rtrim($for.$params).">".$val."</label>";
	}
	public function lbl($val=null,$for=null,$params=null){// short name for label
		return $this->label($val,$for,$params);
	}
	public function span($val=null,$params=null){
		$params = $this->_($params);
		if ($val) return "<span".$params.">".$val."</span>";
	}
	/* unique tags head */
	public function meta($action=null,$content=null,$type="http-equiv"){
		$result = "<meta ";
		$result .= $this->v($type,$action);
		$result .= $this->v("content",$content);
		$result = rtrim($result);
		$result .= ($this->xml)?" />\n":">\n"; // xhtml
		return $result;
	}
	public function canonical($val){
		$val = $this->root_url($val);
		$result = "<link ";
		$result .= $this->v("rel","canonical");
		$result .= $this->v("href",$val);
		$result = rtrim($result);
		$result .= ($this->xml)?" />\n":">\n"; // xhtml
		if ($val) return $result;
	}
	public function ico($link=null,$type="image/png"){
		if ($link && file_exists($link)) {
			$result = "<link ";
			$result .= $this->v("rel","icon");
			$result .= $this->v("type",$type);
			$result .= $this->v("href",$link);
			$result .= $this->v("alt","No Favicon");
			$result = rtrim($result);
			$result .= ($this->xml)?" />\n":">\n"; // xhtml
			return $result;
		}
	}
	/* unique tags body */
	public function br($val=null){
		$result = $val."<br";
		$result .= ($this->xml)?" />\n":">\n"; // xhtml
		return $result;
	}
	public function ahref($target=null,$val=null,$params=null){
		$params = ($params)?$params:$this->alt($val); // if not params use val to title
		if ($val && $target) return "<a ".rtrim($this->v("href",$target).$params).">".$val."</a>";
	}
	public function audio($val=null,$type=null,$params=null){
		$params = $this->_($params);
		$result = "\n<audio".$params.">\n";
		$result .= "<source ".$this->v("src",$val).$this->v("type",$type);
		$result = rtrim($result);
		$result .= ($this->xml)?" />\n":">\n"; // xhtml
		$result .= "</audio>\n";
		if ($val) return $result;
	}
	public function video($val=null,$type=null,$params=null){
		$params = $this->_($params);
		$result = "\n<video".$params.">\n";
		$result .= "<source ".$this->v("src",$val).$this->v("type",$type);
		$result = rtrim($result);
		$result .= ($this->xml)?" />\n":">\n"; // xhtml
		$result .= "</video>\n";
		if ($val) return $result;
	}
	public function img($val=null,$params=null){
		$params .= str_contains($params,"alt=")?"":$this->v("alt",$val);// if not alt use val to alt
		if ($val) {
			$result = "<img ".$this->v("src",$val).$params;
			$result = rtrim($result);
			$result .= ($this->xml)?" />":">"; // xhtml
			return $result;
		}
	}
	public function object($data=null,$type=null,$params=null){
		if ($data) {
			$params = $this->v("data",$data).$this->v("type",$type).$params;
			$params = rtrim($params);
			$params .= str_contains($params,"width=")?"":$this->v("width",1024);
			$params .= str_contains($params,"height=")?"":$this->v("height",768);
			$result = "\n<object ".$params.">\n";
			$result .= $this->ahref($data,$data)."\n";
			$result .= "</object>\n";
			return $result;
		}
	}
	/* special tag */
	public function tag_by_fileType($val=null,$type=null,$params=null) {
		$dataType = explode("/",$type); // to extract only the first element *image*/jpeg of $_FILES["type"] post
		if ($dataType[0] == "audio") {
			return $this->audio($val,$type,$this->v("controls","controls").$params);
		} else if ($dataType[0] == "video") {
			return $this->video($val,$type,$this->v("controls","controls").$params);
		} else if ($dataType[0] == "image") {
			list($width, $height) = getimagesize($val);
			$params = ($params)?$params:$this->v("alt",$val); // if not params
			$params = $this->v("width",$width).$this->v("height",$height).$params;
			return $this->img($val,$params);
		} else {
			//~ try use objet
			return $this->object($val,$type,$params);
		}
	}
	public function text2ascii($text=null) {
		$text = strip_tags($text);
		$text = trim($text);
		$text = $this->replace_accent($text);
		$text = preg_replace("/[^[:alnum:]\.]/","-",$text);
		$text = preg_replace("/-+/","-",$text);
		return $text;
	}
	public function error404(){
		$head = $this->title("404 Not Found");
		$body = $this->h1("Not Found").$this->p("The requested URL ".$_SERVER['PHP_SELF']." was not found on this server.");
		return $this->html($this->head($head).$this->body($body));
	}
	public function alertPage($content="") {
		$head = $this->title("Alert !");
		$body = $this->p("Alert !",$this->v("style","color:red;"));
		$body .= $this->h1($content,$this->v("style","color:red;"));
		return $this->html($this->head($head).$this->body($body));
	}
	public function color($val=null,$color="black") {
		$params = $this->v("style","color:".$color.";");
		if ($val) return "<span ".rtrim($params).">".$val."</span>";
	}
	public function comment($val=null){ // old name
		if ($val) return "<!--".$val."-->";
	}
	public function com($val=null){ // short name
		if ($val) return $this->comment($val);
	}
	public function css($val=null,$external=null){
		$result = "";
		if ($val && $external==null){
			$params = $this->v("type","text/css");
			$result = "<style ".rtrim($params).">\n".$val."\n</style>\n";
			return $result;
		} elseif($val && $external && file_exists($val)){
			$result = "<link ";
			$result .= $this->v("href",$val);
			$result .= $this->v("rel","stylesheet");
			$result .= $this->v("type","text/css");
			$result = rtrim($result);
			$result .= ($this->xml)?" />\n":">\n"; // xhtml
			return $result;
		}
	}
	public function js($val=null,$external=null){
		$result = "";
		if ($val && $external == null){
			$params = $this->v("type","text/javascript");
			$result = "<script ".rtrim($params).">\n".$val."\n</script>\n";
		} elseif($val && $external && file_exists($val)){
			$params = $this->v("src",$val).$this->v("type","text/javascript");
			$result = "<script ".rtrim($params)."></script>\n";
		}
		if ($val) return $result;
	}
	public function script($val=null,$external=null) { // old name to maintain compatibility with older pages
		if ($val) return $this->js($val,$external);
	}
	public function noscript($val=null) {
		return $this->tag("noscript",$val);
	}
	/* form tags */
	public function button($val=null,$params=null,$type="button"){
		//~ types button|submit|reset
		$result = "<button ".$this->v("type",$type);
		if ($params) $result .= $params;
		$result = rtrim($result);
		$result .= ">";
		$result .= $val;
		$result .= "</button>";
		if ($val) return $result;
	}
	public function hrefbtn($link="",$text="ok",$params=null){
		$params = $this->v("type","button").$this->v("onclick","location.href='".$link."'").$params;
		$result = "<button ".rtrim($params);
		$result .= ">";
		$result .= $text;
		$result .= "</button>";
		if ($link) return $result;
	}
	public function form($val=null,$name=null,$method=null,$action=null,$enctype=null,$params=null){
		$result = "<form ";
		if ($name) $result .= $this->v("id",$name).$this->v("name",$name);
		if ($method) $result .= $this->v("method",$method);
		if ($action) $result .= $this->v("action",$action);
		if ($enctype) $result .= $this->v("enctype",$enctype);
		if ($params) $result .= $params;
		$result = rtrim($result);
		$result .= ">";
		$result .= $val;
		$result .= "</form>\n";
		if ($val) return $result;
	}
	public function textarea($val=null,$name=null,$sizes=array(4,80),$params=null){
		$result = "<textarea ";
		$result .= $this->v("rows",$sizes[0]).$this->v("cols",$sizes[1]);
		if ($name) $result .= $this->v("id",$name).$this->v("name",$name);
		if ($params) $result .= $params;
		$result = rtrim($result);
		$result .= ">";
		$result .= $val;
		$result .= "</textarea>";
		return $result;
	}
	public function input($type="submit",$name=null,$value="submit",$params=null){
		$result = "<input ";
		$result .= $this->v("type",$type);
		$result .= $this->v("value",$value); // don't check $value, it can be "0" or empty
		if ($name) $result .= $this->v("id",$name).$this->v("name",$name);
		if ($params) $result .= $params;
		$result = rtrim($result);
		$result .= ($this->xml)?" />":">"; // xhtml
		if ($type == "submit") $result = $this->span($result,$this->v("class","inputsubmit"));
		if ($type) return $result;
	}
	public function inputradio($id=null,$name=null,$value=null,$params=null){
		$result = "<input ".$this->v("type","radio");
		$result .= $this->v("id",$id);
		$result .= $this->v("name",$name); // name has to be the same for all inputs
		$result .= $this->v("value",$value);
		if ($params) $result .= $params;
		$result = rtrim($result);
		$result .= ($this->xml)?" />":">"; // xhtml
		return $result;
	}
    public function select($options=null,$name=null,$selected=null,$params=null){
		if ($name) $params = $this->v("id",$name).$this->v("name",$name).$params;
		$params = rtrim($params);
		return "<select ".$params.">\n".$this->options($options,$selected)."</select>\n";
	}
	function options($options=null,$selected=null){
		$optionsList = array();
		if ($options) {
			foreach($options as $item=>$value){
				if ($value == $selected){
					$params = $this->v("value",$value).$this->v("selected","selected");
					$optionsList[] =  "<option ".rtrim($params).">".$item."</option>"; 
				} else {
					$params = $this->v("value",$value);
					$optionsList[] =  "<option ".trim($params).">".$item."</option>"; 
				}
			}
			return implode("\n",$optionsList);
		}
	}
	/* utilities */
	public function crypt($word){
		if (function_exists('password_hash')){ // Only in PHP 5.5+
			return password_hash($word, PASSWORD_DEFAULT);
		} elseif (function_exists('sha1')){ // Only in PHP 4.3.0+
			return sha1($word);
		} elseif (function_exists('mhash')){ // Only if 2Mhash library is loaded
			return bin2hex(mhash(MHASH_SHA1, $word));
		} else {
			return md5($word);
		}
	}
	public function cryptOld($word){
		if (function_exists('sha1')){ // Only in PHP 4.3.0+
			return sha1($word);
		} elseif (function_exists('mhash')){ // Only if 2Mhash library is loaded
			return bin2hex(mhash(MHASH_SHA1, $word));
		} else {
			return md5($word);
		}
	}
	//~ safety
	public function num($num){
		if ($num && !preg_match("/^[0-9]+$/",$num)) exit("number entry incorrect ! \"".$num."\"");
		return $num;
	}
	public function word($word){
		if ($word && !preg_match("/^[A-Za-z0-9_\/\.\:]+$/",trim($word))) exit("word entry incorrect ! use only 'A-Z a-z 0-9 or _ / . :' chars");
		return $word;
	}
	public function safeSQL($val=null){
		return $this->safe($val);
	}
	public function safe($val=null){ // old name
		$val = stripslashes($val);
		$dbLink = mysqli_connect(HOSTMYSQL,USERMYSQL,PASSMYSQL,BASEMYSQL) or die("Error database connection aborted"); 
		$result = mysqli_real_escape_string($dbLink,$val);
		mysqli_close($dbLink);
		return $result;
	}
	public function safeVal($val=null){
		if ($val) {
			$val = stripslashes($val);
			$val = htmlspecialchars($val);
			return $val;
		}
	}
	public function safeCss($word){
		if ($word && !preg_match("/^[A-Za-z0-9_\.]+$/",trim($word))) exit("word entry incorrect ! use only 'A-Z a-z 0-9 or _ .' chars");
		return $word;
	}
	
	public function specialchars($val=null){
		if ($val) {
			$val = htmlspecialchars($val, ENT_HTML5);
			return $val;
		}
	}
	public function slash($val=null){
		if ($val) {
			$val = addslashes($val);
			return $val;
		}
	}
	public function entities($val=null){
		if ($val === null) {// htmlentities don't accept null
			return "htmties null ?";
		} else {
			return htmlentities($val,ENT_QUOTES,"UTF-8");
		}
	}	
	public function uly_htmlentities($val=null){// old name
		return $this->entities($val);
	}
	public function striptags($val=null){
		return strip_tags($val);
	}	
	public function uly_strip_tags($val=null){// old name
		return $this->striptags(trim($val));
	}
	//~ special
	public function strWidthLeft($val=null,$size=128){
		$val = trim($val);
		$stringLen = mb_strlen($val);
		if ($stringLen > $size){
			$result = mb_substr($val,0,$size);
			$words = array_filter(explode(" ",$result));
			// delete last word or fragment
			if (count($words) > 1) array_pop($words);
			$result = implode(" ",$words)."...";
		} else {
			$result = $val;
		}
		return $result;
	}
	public function strWidth($val=null,$size=64,$sp="&middot;"){
		//~ $sp = "&middot;"; // middle dot
		//~ $sp = "&nbsp;"; // non-breaking space
		//~ $sp = "&emsp;";
		//~ $sp = "&#32;";
		$val = trim($val);
		$stringLen = mb_strlen($val);
		if ( $stringLen >= $size ){
			$e = floor(($size-2) / 2);
			$result = mb_substr($val,0,$e).$sp." ".$sp.mb_substr($val,-$e);
		} else {
			$result = $val;
		}
		if (mb_strlen($result) < $size+1) $result = $result." ".str_repeat($sp,$size-mb_strlen($result));
		return $result;
	}
	public function url($val=null){
		return urlencode($val);
	}
	public function clean_short_url($val=null,$length=128){
		if ($val){
			$val = trim($val);
			$val = html_entity_decode($val);
			$val = $this->striptags($val);
			$val = $this->replace_accent($val);
			$val = preg_replace('/[^\w\s]/', '', $val);
			$val = preg_replace('/\s+/', ' ', $val);
			if (mb_strlen($val) > $length){
				$result = mb_substr($val,0,$length);
				$words = array_filter(explode(" ",$result));
				// delete last word fragment
				if (count($words) > 1) array_pop($words);
				$result = implode(" ",$words);
			} else {
				$result = $val;
			}
			return urlencode($result);
		}
	}
	public function f4d($val){
		return sprintf("%'-4d",$val);
	}
	public function echo_arr($arr=array()){
		echo "<pre>",print_r($arr,true),"</pre>";
	}
	public function root_url($val=null){
		$val = trim($val);
		if (!defined('HTTPSVAL')) {
			define("HTTPSVAL",null);
		} 
		$http = HTTPSVAL ? "https://":"http://";
		if (isset($_SERVER['REQUEST_URI'])) {
			$script = $_SERVER['REQUEST_URI'];
		} elseif (isset($_SERVER['SCRIPT_NAME'])) {
			$script = $_SERVER['SCRIPT_NAME'];
		} elseif (isset($_SERVER['PHP_SELF'])) {
			$script = $_SERVER['PHP_SELF'];
		} else {
			$script = null;
		}
		if ( preg_match("/\//",$script) ){
			$relativPath = array();
			$relativPath = explode("/",$script);
			array_pop($relativPath);
			$localDir = implode("/",$relativPath);
		} else {
			$localDir = "";
		}
		if (isset($_SERVER['HTTP_HOST'])){
			$result = $http.$_SERVER['HTTP_HOST'].$localDir."/"; 
		} elseif ( isset($_SERVER['SERVER_NAME']) ){
			$result = $http.$_SERVER['SERVER_NAME'].$localDir."/";
		} else {
			$result = $localDir."/";
		}
		if (!empty($val)) $result .= $val;
		return $result;
	}
	function replace_accent($val=null) {
		$a = array('À', 'Á', 'Â', 'Ã', 'Ä', 'Å', 'Æ', 'Ç', 'È', 'É', 'Ê', 'Ë', 'Ì', 'Í', 'Î', 'Ï', 'Ð', 'Ñ', 'Ò', 'Ó', 'Ô', 'Õ', 'Ö', 'Ø', 'Ù', 'Ú', 'Û', 'Ü', 'Ý', 'ß', 'à', 'á', 'â', 'ã', 'ä', 'å', 'æ', 'ç', 'è', 'é', 'ê', 'ë', 'ì', 'í', 'î', 'ï', 'ñ', 'ò', 'ó', 'ô', 'õ', 'ö', 'ø', 'ù', 'ú', 'û', 'ü', 'ý', 'ÿ', 'Ā', 'ā', 'Ă', 'ă', 'Ą', 'ą', 'Ć', 'ć', 'Ĉ', 'ĉ', 'Ċ', 'ċ', 'Č', 'č', 'Ď', 'ď', 'Đ', 'đ', 'Ē', 'ē', 'Ĕ', 'ĕ', 'Ė', 'ė', 'Ę', 'ę', 'Ě', 'ě', 'Ĝ', 'ĝ', 'Ğ', 'ğ', 'Ġ', 'ġ', 'Ģ', 'ģ', 'Ĥ', 'ĥ', 'Ħ', 'ħ', 'Ĩ', 'ĩ', 'Ī', 'ī', 'Ĭ', 'ĭ', 'Į', 'į', 'İ', 'ı', 'IJ', 'ij', 'Ĵ', 'ĵ', 'Ķ', 'ķ', 'Ĺ', 'ĺ', 'Ļ', 'ļ', 'Ľ', 'ľ', 'Ŀ', 'ŀ', 'Ł', 'ł', 'Ń', 'ń', 'Ņ', 'ņ', 'Ň', 'ň', 'ʼn', 'Ō', 'ō', 'Ŏ', 'ŏ', 'Ő', 'ő', 'Œ', 'œ', 'Ŕ', 'ŕ', 'Ŗ', 'ŗ', 'Ř', 'ř', 'Ś', 'ś', 'Ŝ', 'ŝ', 'Ş', 'ş', 'Š', 'š', 'Ţ', 'ţ', 'Ť', 'ť', 'Ŧ', 'ŧ', 'Ũ', 'ũ', 'Ū', 'ū', 'Ŭ', 'ŭ', 'Ů', 'ů', 'Ű', 'ű', 'Ų', 'ų', 'Ŵ', 'ŵ', 'Ŷ', 'ŷ', 'Ÿ', 'Ź', 'ź', 'Ż', 'ż', 'Ž', 'ž', 'ſ', 'ƒ', 'Ơ', 'ơ', 'Ư', 'ư', 'Ǎ', 'ǎ', 'Ǐ', 'ǐ', 'Ǒ', 'ǒ', 'Ǔ', 'ǔ', 'Ǖ', 'ǖ', 'Ǘ', 'ǘ', 'Ǚ', 'ǚ', 'Ǜ', 'ǜ', 'Ǻ', 'ǻ', 'Ǽ', 'ǽ', 'Ǿ', 'ǿ');
		$b = array('A', 'A', 'A', 'A', 'A', 'A', 'AE', 'C', 'E', 'E', 'E', 'E', 'I', 'I', 'I', 'I', 'D', 'N', 'O', 'O', 'O', 'O', 'O', 'O', 'U', 'U', 'U', 'U', 'Y', 's', 'a', 'a', 'a', 'a', 'a', 'a', 'ae', 'c', 'e', 'e', 'e', 'e', 'i', 'i', 'i', 'i', 'n', 'o', 'o', 'o', 'o', 'o', 'o', 'u', 'u', 'u', 'u', 'y', 'y', 'A', 'a', 'A', 'a', 'A', 'a', 'C', 'c', 'C', 'c', 'C', 'c', 'C', 'c', 'D', 'd', 'D', 'd', 'E', 'e', 'E', 'e', 'E', 'e', 'E', 'e', 'E', 'e', 'G', 'g', 'G', 'g', 'G', 'g', 'G', 'g', 'H', 'h', 'H', 'h', 'I', 'i', 'I', 'i', 'I', 'i', 'I', 'i', 'I', 'i', 'IJ', 'ij', 'J', 'j', 'K', 'k', 'L', 'l', 'L', 'l', 'L', 'l', 'L', 'l', 'l', 'l', 'N', 'n', 'N', 'n', 'N', 'n', 'n', 'O', 'o', 'O', 'o', 'O', 'o', 'OE', 'oe', 'R', 'r', 'R', 'r', 'R', 'r', 'S', 's', 'S', 's', 'S', 's', 'S', 's', 'T', 't', 'T', 't', 'T', 't', 'U', 'u', 'U', 'u', 'U', 'u', 'U', 'u', 'U', 'u', 'U', 'u', 'W', 'w', 'Y', 'y', 'Y', 'Z', 'z', 'Z', 'z', 'Z', 'z', 's', 'f', 'O', 'o', 'U', 'u', 'A', 'a', 'I', 'i', 'O', 'o', 'U', 'u', 'U', 'u', 'U', 'u', 'U', 'u', 'U', 'u', 'A', 'a', 'AE', 'ae', 'O', 'o');
		//~ encoding all stuff
		$val = mb_convert_encoding($val, "UTF-8", "auto");
		$a = mb_convert_encoding($a, "UTF-8", "auto");
		$b = mb_convert_encoding($b, "UTF-8", "auto");
		//~ replace
		return str_replace($a, $b, $val);
	}
	public function get_ext($fileName=null){
		$data = explode(".",$fileName);
		$ext = array_pop($data);
		$ext = strtolower($ext);
		return $ext;
	}
	public function format_file_size($octets) {// old name
		return $this->octets($octets);
	}
	public function octets($octets){
		$result = $octets;
		for ($i=0; $i < 8 && $result >= 1024; $i++) {
			$result = $result / 1024;
		}
		if ($i > 0) {
			return preg_replace('/,00$/', '', number_format($result, 2, ',', '')) . ' ' . substr('KMGTPEZY',$i-1,1) . 'o';
		} else {
			return $result . ' o';
		}
	}
	public function phpDate2Html($val=null){
		if ($val) {
			$val = str_replace(" ","T",$val);
			$val = substr($val, 0, -3);
		}
		return $val;
	}
	public function htmlDate2Php($val=null){
		if($val){
			$val = str_replace("T"," ",$val).":00";
		}
		return $val;
	}
	public function classicDate($val=null){
		if($val){
			$val = date("l j F Y [H:i:s]", strtotime($val));
		}
		return $val;
	}
	
	function getLang() {
		$lang = array("en","fr","es");
		return $lang[LANG];
	}
}
?>

page url