classHtmlInc{// the requested uri (maybe file)
public $uri;// all html
public $html;// just meta tags,
public $meta;// metas as an array
public $props;// the filename
public $name;// A short label for the resource
public $label;// A longer title
public $title;// boolean, if remote
public $http;// if an error when instantiate
public $error;
public function__construct($uri,$html=false){if($html)$this->html=$html;// if no uri, let user instantiate object and use it like he wants
if(!$uri)return;$this->uri=$uri;$this->name=$this->label=current(explode('.',basename($uri)));if(strpos($uri,"http://")===0)return$this->http=true;if(!is_file($uri)){$this->error='<p class="error">Page momentanément indisponible. <!-- '.$uri.' --></p>';returnfalse;}}
public functionhtml($html=false){if($this->html)return$this->html;if(!$this->uri){$this->error='<p class="error">Erreur interne (pas de fichier demandé).</p>';returnnull;}$this->html=file_get_contents($this->uri);return$this->html;}
public functionhead(){if($this->meta)return$this->meta;$head=self::headSub($this->html());$this->props=array();// keep title in memory
$title=array("");preg_match('/<title>([^<]+)<\/title>/i',$head,$title);if(isset($title[1]))$this->props['title'][]=array(0=>$title[1],"string"=>$title[1]);// grab all tags candidates
preg_match_all("/<(meta|link)[^>]+>/i",$head,$meta,PREG_PATTERN_ORDER);// filter tags kown to not be metas
$meta=preg_grep("/stylesheet|http-equiv|icon/",$meta[0],PREG_GREP_INVERT);// loop on meta to populate the array
foreach($metaas$line){preg_match('/(name|rel)="([^"]+)"/i',$line,$key);preg_match('/(content|title)="([^"]+)"/i',$line,$string);preg_match('/(scheme|href)="([^"]+)"/i',$line,$uri);if(!isset($key[2]))continue;// strip namespace prefix of property
if($pos=strpos($key[2],'.'))$key[2]=substr($key[2],$pos+1);if($pos=strpos($key[2],':'))$key[2]=substr($key[2],$pos+1);// all props supposed repeat
if(isset($uri[2])&&isset($string[2]))$this->props[$key[2]][]=array(0=>$string[2],"string"=>$string[2],1=>$uri[2],"uri"=>$uri[2]);elseif(isset($uri[2]))$this->props[$key[2]][]=array(0=>$uri[2],"uri"=>$uri[2]);elseif(isset($string[2]))$this->props[$key[2]][]=array(0=>$string[2],"string"=>$string[2]);}// rebuild a clean meta block ready to include in HTML
$this->meta="\n ".@$title[0]."\n ".implode("\n ",$meta);return$this->meta;}
public static functionheadSub($html){if(!$start=stripos($html,"<head"))return"";$start=strpos($html,">",$start)+1;$to=stripos($html,"</head>");if($to)returnsubstr($html,$start,$to-$start);elsereturnsubstr($html,$start);}
public static functionbodySub($html){if(!$start=stripos($html,"<body"))return$html;$start=strpos($html,">",$start)+1;$to=stripos($html,"</body>");if($to)returnsubstr($html,$start,$to-$start);elsereturnsubstr($html,$start);}
public functionprops(){$this->head();return$this->props;}
public functionmeta($out=null){$this->head();if(is_string($out))return$this->meta;if(!is_resource($out))$out=fopen("php://output","w");fwrite($out,$this->meta);}
public static $fixhtml=array('/<\/?font[^>]*>/i'=>'','//i'=>'','/ class="([^"]+)-western"/i' => ' class="$1"','/ /' => ' ','/(<sup><\/?a[^>]*>)<sup>/i' => '$1',// fix specifique OOo
'/<\/sup>(<\/a><\/sup>)/i' => '$1',// fix specifique OOo
// '/ style="[^"]*"/i' => '', // pb for width
);
public functionbody($out=false){// static call with an html string to cut
if(is_string($out)&&$out)returnHtmlInc::bodySub(preg_replace(array_keys(self::$fixhtml),array_values(self::$fixhtml),$out));// error already given, return it
if($this->error){echo$this->error,' <!--',$this->uri,'-->';return;}$html=self::bodySub($this->html());$html=preg_replace(array_keys(self::$fixhtml),array_values(self::$fixhtml),$html);// imprévisible et non maîtrisé
// if(class_exists("Diple")) $html=Diple::rewrite($html);
// if caller wants a string, like body(""), send back a string
// if (!is_resource($out)) $out=fopen("php://output", "w");
if(is_resource($out))fwrite($out,$html);return$html;}
public functionlabel(){if(isset($this->props['label']))return$this->props['label'][0][0];if(isset($this->props['title']))return$this->props['title'][0][0];return$this->label;}}?>