Class PHP Type Agent
Description
S'occupe de la détection du navigateur internet et de retourner le DOCTYPE correct, et la gestion de code (x)HTML proprement.
Voici le code source de la classe "Type Agent".
<?php
/**
Class set_agent :
Sert a gerer le mime type supporte par les navigateurs,
a retourner la DTD correcte dans un prologue,
a compresser les donnees par bufferisation de sortie si la compression est supportee par le navigateur,
a corriger le code XHTML en HTML, si le navigateur n'est pas capable d'afficher du XHTML avec le bon mime type
Auteur : Stephane HUC
Licence : GNU/LGPL
*/
class set_agent {
public $agent; // define the user agent
public $accept; // define the code accepted
public $accept_encoding; //define the encoding accepted
public $charsets; // array to define the charset to return
private $code;
public $mimes; // array to define the mime type to return
private $mtime; // define the microtime for calcul time to execution
private $websrc; // array to define the code html to return
public function __construct() {
$this->agent = $_SERVER["HTTP_USER_AGENT"];
if(!empty($_SERVER["HTTP_ACCEPT"])) $this->accept = $_SERVER["HTTP_ACCEPT"];
if(!empty($_SERVER["HTTP_ACCEPT_ENCODING"])) $this->accept_encoding = $_SERVER["HTTP_ACCEPT_ENCODING"];
if(!empty($_SERVER["REQUEST_URI"])) $this->request = $_SERVER["REQUEST_URI"];
$this->agent_name = array('Chrome','MSIE','Mozilla','Opera');
$this->mimes = array ("text/html", "application/xhtml+xml", "application/xml", "application/atom+xml", "application/rss+xml","text/xml",);
$this->charsets = array ("ISO-8859-1", "UTF-8", "ISO-8859-15" );
$this->websrc = array ("HTML 4.01", "XHTML 1.0", /*"XHTML 1.1",*/ );
//$this->get_user_agent(); //var_dump($this->user_agent);
if(ereg('atom.php$',$this->request)) $this->mime = $this->mimes[3];
elseif(ereg('rss.php$', $this->request)) $this->mime = $this->mimes[4];
elseif(ereg('sitemap.php$', $this->request)) $this->mime = $this->mimes[5];
//elseif(stristr($this->accept, $this->mimes[2]) || stristr($this->agent, "W3C_Validator")) $this->mime = $this->mimes[2];
elseif(stristr($this->accept, $this->mimes[1]) || stristr($this->agent, "W3C_Validator")) $this->mime = $this->mimes[1];
//elseif($this->user_agent == "MSIE+") $this->mime = $this->mimes[1];
//elseif($this->user_agent == "MSIE6") $this->mime = $this->mimes[1];
else $this->mime = $this->mimes[0];
return $this->mime;
}
public function get_prologue() {
$this->get_charset();
switch($this->mime) {
case $this->mimes[5] :
case $this->mimes[4] :
case $this->mimes[3] :
$this->buffering();
$this->prologue = '<?xml version="1.0" encoding="'.$this->charset.'"?>'."\n";
break;
case $this->mimes[2] :/*
$this->buffering();
$this->prologue = '<?xml version="1.0" encoding="'.$this->charset.'"?>'."\n";
$this->prologue .= '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">';
break;*/
case $this->mimes[1] :
$this->buffering();
ob_start(array('set_agent','compress_html'));
/*$this->prologue = '<?xml version="1.0" encoding="'.$this->charset.'"?>'."\n";*/
$this->prologue = '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">'."\n";
break;
case $this->mimes[0] :
ob_start(array('set_agent','fix_code'));
$this->prologue = '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">'."\n";
break;
}
header("Content-Type: ".$this->mime.";charset=".$this->charset);
header("Vary: Accept");
echo $this->prologue;
}
public function get_user_agent() {
if(ereg('MSIE', $this->agent) && !ereg('Opera', $this->agent)) {
$xploz = explode(';',$this->agent);
$this->msie = trim($xploz[1]);
unset($xploz);
switch($this->msie) {
case "MSIE 8.0":
case "MSIE 7.0":
$this->user_agent = 'MSIE+';
break;
case "MSIE 6.0";
$this->user_agent = 'MSIE6';
break;
default:
$this->user_agent = 'MSIE';
break;
}
//$this->user_agent = 'MSIE';
}
elseif(ereg('Opera', $this->agent)) $this->user_agent = 'Opera';
elseif(ereg('Chrome', $this->agent)) $this->user_agent = 'Chrome';
else $this->user_agent = 'MOZ';
//return $this->user_agent;
}
public function get_code() {
foreach($this->mimes as $k => $v) {
if(strCmp($this->mime, $v) == 0 && !empty($this->websrc[$k])) {
$this->code = $this->websrc[$k];
}
}
unSet($k, $v);
if(!empty($this->code)) return $this->code;
else return $this->code='';
}
public function get_charset() {
$this->get_code();
switch($this->code) {
case $this->websrc[2] :
case $this->websrc[1] :
$this->charset = $this->charsets[1];
break;
case $this->websrc[0] : $this->charset = $this->charsets[0]; break;
default: $this->charset = $this->charsets[2];
}
return $this->charset;
}
public function get_mime() {
return $this->mime;
}
public function get_microtime() {
$this->mtime = microtime();
$this->mtime = explode(" ",$this->mtime);
$this->mtime = $this->mtime[1] + $this->mtime[0];
return $this->mtime;
}
public function fix_code($buffer) {
$search = array (
"!html xmlns=\"http://www.w3.org/1999/xhtml\"!",
"!\s*/>!",
"!xml:!",
"!xmlns:xlink=\"http://www.w3.org/1999/xlink\"!",
);
$replace = array (
"html",
">",
"",
"",
);
return (preg_replace($search, $replace, $buffer));
}
public function compress_html($buffer) {
/* remove comments */
$buffer = preg_replace('!/\*[^*]*\*+([^/][^*]*\*+)*/!', '', $buffer);
/* remove tabs, spaces, newlines, etc. */
$buffer = str_replace(array("\r\n", "\r", "\n", "\t", ' ', ' ', ' '), '', $buffer);
return $buffer;
}
private function buffering() {
if(!empty($this->accept_encoding) && eregi("gzip,deflate", $this->accept_encoding)) {
ini_set('zlib.output_compression', 'On');
ini_set('zlib.output_compression_level', '1');
ob_start('ob_gzhandler');
}
}
public function empty_out() {
ob_end_flush();
}
}
?>
<<| Page : PHP : class : type_agent : |