<?php
class HtmlInc {
public $uri;
public $html;
public $meta;
public $props;
public $name;
public $label;
public $title;
public $http;
public $error;
public function __construct($uri, $html=false) {
if($html) $this->html=$html;
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>';
return false;
}
}
public function html($html=false) {
if ($this->html) return $this->html;
if (!$this->uri) {
$this->error='<p class="error">Erreur interne (pas de fichier demandé).</p>';
return null;
}
$this->html=file_get_contents($this->uri);
return $this->html;
}
public function head() {
if ($this->meta) return $this->meta;
$head=self::headSub($this->html());
$this->props=array();
$title=array("");
preg_match('/<title>([^<]+)<\/title>/i', $head, $title);
if (isset($title[1])) $this->props['title'][]=array(0=>$title[1], "string"=>$title[1]);
preg_match_all("/<(meta|link)[^>]+>/i", $head, $meta, PREG_PATTERN_ORDER);
$meta=preg_grep( "/stylesheet|http-equiv|icon/", $meta[0], PREG_GREP_INVERT);
foreach ($meta as $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;
if ($pos=strpos($key[2], '.')) $key[2]=substr($key[2], $pos+1);
if ($pos=strpos($key[2], ':')) $key[2]=substr($key[2], $pos+1);
if(isset($uri[2]) && isset($string[2])) $this->props[$key[2]][]=array(0=>$string[2], "string"=>$string[2], 1=>$uri[2], "uri"=>$uri[2]);
else if(isset($uri[2])) $this->props[$key[2]][]=array(0=>$uri[2], "uri"=>$uri[2]);
else if(isset($string[2])) $this->props[$key[2]][]=array(0=>$string[2], "string"=>$string[2]);
}
$this->meta="\n " . @$title[0] . "\n " . implode("\n ", $meta);
return $this->meta;
}
public static function headSub($html) {
if (!$start=stripos($html, "<head")) return "";
$start=strpos($html, ">", $start)+1;
$to=stripos($html, "</head>");
if ($to) return substr($html, $start, $to - $start);
else return substr($html, $start);
}
public static function bodySub($html) {
if (!$start=stripos($html, "<body")) return $html;
$start=strpos($html, ">", $start)+1;
$to=stripos($html, "</body>");
if ($to) return substr($html, $start, $to - $start);
else return substr($html, $start);
}
public function props() {
$this->head();
return $this->props;
}
public function meta($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'=>'',
'/ class="western"/i'=>'',
'/ class="([^"]+)-western"/i' => ' class="$1"',
'/ /' => ' ',
'/(<sup><\/?a[^>]*>)<sup>/i' => '$1', '/<\/sup>(<\/a><\/sup>)/i' => '$1', );
public function body($out=false) {
if (is_string($out) && $out) return HtmlInc::bodySub( preg_replace(array_keys(self::$fixhtml), array_values(self::$fixhtml), $out));
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);
if (is_resource($out)) fwrite($out, $html);
return $html;
}
public function label() {
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;
}
}
?>