<?php
/***************************************************************************
*
* Best Of IRC - Parseur de logs IRC
* Version 1.0 - 28/12/05
* Copyright (C) 2005 (HILT Guillaume aka Shadow AOK)
* <hide@address.com>
*
***************************************************************************
/***************************************************************************
*
* Ce programme est libre, vous pouvez le redistribuer et/ou le modifier
* selon les termes de la Licence Publique Générale GNU publiée par la
* Free Software Foundation (version 2 ou bien toute autre version
* ultérieure choisie par vous).
* Ce programme est distribué car potentiellement utile, mais SANS
* AUCUNE GARANTIE, ni explicite ni implicite, y compris les garanties
* de commercialisation ou d'adaptation dans un but spécifique.
* Reportez-vous à la Licence Publique Générale GNU pour plus de détails.
* Vous devez avoir reçu une copie de la Licence Publique Générale GNU
* en même temps que ce programme ; si ce n'est pas le cas, écrivez à
* la Free Software Foundation, Inc., 59 Temple Place, Suite 330,
* Boston, MA 02111-1307, États-Unis.
*
***************************************************************************/
class bestof
{
// Chemin vers le dossier contenant les images (avec un / à la fin)
private $path;
// Codes couleurs utilisés par les clients irc
private $ircColors;
// La chaine sur laquelle on travaille
private $stream;
// Pattern de recherche de la couleur
private $pattern_clr;
private $pattern_clr_src;
// Pattern de recherche du gras
private $pattern_bld;
// Pattern de recherche du souligné
private $pattern_udl;
######################################################
# Fonction: check
# Paramètres: texte
# Description : recherche et applique le gras/souligné/couleur
######################################################
function check($ligne)
{
$nbclrs = 0;
$nbg = 0;
$nbu = 0;
// Gras
$ligne = preg_replace($this->pattern_bld_src, "<b>\\1</b>", $ligne);
if(strpos($ligne, $this->pattern_bld) !== false)
{
$ligne = str_replace($this->pattern_bld, "<b>", $ligne)."</b>";
}
// Souligné
$ligne = preg_replace($this->pattern_udl_src, "<u>\\1</u>", $ligne);
if(strpos($ligne, $this->pattern_udl) !== false)
{
$ligne = str_replace($this->pattern_udl, "<u>", $ligne)."</u>";
}
// Couleur -> on peut faire plus court, en s'inspirant de la méthode ci-dessus mais j'ai la flemme
if(strpos($ligne, $this->pattern_clr) !== false)
{
while(preg_match($this->pattern_clr_src, $ligne, $tmp))
{
$tmpstr = $tmp[0];
$tmp[0] = substr($tmp[0], 1);
if(strpos($tmpstr, ",") !== false)
{
$tmp = explode(",", $tmp[0]);
}
$ligne = ereg_replace($tmpstr, $this->color($tmp[0], $tmp[1]), $ligne);
$nbclrs++;
}
while(strpos($ligne, $this->pattern_clr) !== false && $nbclrs > 0)
{
$ligne = ereg_replace($this->pattern_clr, "</span>", $ligne);
$nbclrs--;
}
if($nbclrs > 0)
{
$ligne .= "</span>";
}
}
return $ligne;
}
######################################################
# Fonction: color
# Paramètres: color, subcolor
# Description : renvoie le code html de la couleur
######################################################
function color($color, $subcolor)
{
if($color >= 0 && !isset($subcolor))
{
return "<span style=\"color: ".$this->ircColors[$color].";\">";
}
else if(!isset($color) && $subcolor >= 0)
{
return "<span style=\"background-color: ".$this->ircColors[$subcolor].";\">";
}
else
{
return "<span style=\"color: ".$this->ircColors[$color]."; background-color: ".$this->ircColors[$subcolor].";\">";
}
}
######################################################
# Fonction: read
# Paramètres: texte
# Description : prépare le texte au traitement
######################################################
function read($texte)
{
// Initialisation ni trop tôt ni trop tard -> merci ePoX ;)
$this->path = "./smileys/";
$this->ircColors = array(
'0' => '#FFFFFF', // blanc
'1' => '#000000', // noir
'2' => '#000080', // bleu foncé
'3' => '#008000', // vert foncé
'4' => '#FF0000', // rouge
'5' => '#800000', // rouge foncé
'6' => '#FF00FF', // violet
'7' => '#FF8000', // orange
'8' => '#FFFF00', // jaune
'9' => '#00FF00', // vert
'10' => '#008080', // cyan foncé
'11' => '#00FFFF', // cyan
'12' => '#0000FF', // bleu
'13' => '#800080', // violet foncé
'14' => '#808080', // gris foncé
'15' => '#C0C0C0' // gris clair
);
$this->stream = "";
$this->pattern_clr = chr(3);
$this->pattern_clr_src = "#".$this->pattern_clr."\d{1,2}(?:,\d{1,2})?#"; // -> merci ripat ;)
$this->pattern_bld = chr(2);
$this->pattern_bld_src = "!".$this->pattern_bld."(.*)".$this->pattern_bld."!";
$this->pattern_udl = chr(31);
$this->pattern_udl_src = "!".$this->pattern_udl."(.*)".$this->pattern_udl."!";
// Traitement par ligne
$stream = explode("\n", htmlentities($texte));
for($i = 0; $i < count($stream); $i++)
{
$stream[$i] = $this->check($stream[$i]);
$stream[$i] = $this->status($stream[$i]);
$stream[$i] = $this->smileys($stream[$i]);
}
return implode("<br />", $stream);
}
######################################################
# Fonction: smileys
# Paramètres: ligne
# Description : convertit les smileys ascii en images
######################################################
function smileys($ligne)
{
$smileys = array(
"0:)" => "<img src=\"".$this->path."ange.gif\" alt=\"0:)\" style=\"width: 31px; height: 18px;\" />",
":-)" => "<img src=\"".$this->path."smile.gif\" alt=\":-)\" style=\"width: 15px; height: 15px;\" />",
":)" => "<img src=\"".$this->path."smile.gif\" alt=\":)\" style=\"width: 15px; height: 15px;\" />",
"8)" => "<img src=\"".$this->path."cool.gif\" alt=\"8)\" style=\"width: 15px; height: 15px;\" />",
"8-)" => "<img src=\"".$this->path."cool.gif\" alt=\"8-)\" style=\"width: 15px; height: 15px;\" />",
";-)" => "<img src=\"".$this->path."wink.gif\" alt=\";-)\" style=\"width: 15px; height: 15px;\" />",
";)" => "<img src=\"".$this->path."wink.gif\" alt=\";)\" style=\"width: 15px; height: 15px;\" />",
":p" => "<img src=\"".$this->path."razz.gif\" alt=\":p\" style=\"width: 15px; height: 15px;\" />",
":P" => "<img src=\"".$this->path."razz.gif\" alt=\":P\" style=\"width: 15px; height: 15px;\" />",
";p" => "<img src=\"".$this->path."razz.gif\" alt=\";p\" style=\"width: 15px; height: 15px;\" />",
":-p" => "<img src=\"".$this->path."razz.gif\" alt=\":-p\" style=\"width: 15px; height: 15px;\" />",
":(((" => "<img src=\"".$this->path."pleure.gif\" alt=\":(((\" style=\"width: 40px; height: 15px;\" />",
":((" => "<img src=\"".$this->path."pleure.gif\" alt=\":((\" style=\"width: 40px; height: 15px;\" />",
":(" => "<img src=\"".$this->path."sad.gif\" alt=\":(\" style=\"width: 15px; height: 15px;\" />",
":/" => "<img src=\"".$this->path."sad.gif\" alt=\":/\" style=\"width: 15px; height: 15px;\" />",
":'(" => "<img src=\"".$this->path."pleure.gif\" alt=\":'(\" style=\"width: 40px; height: 15px;\" />",
":-(" => "<img src=\"".$this->path."sad.gif\" alt=\":-(\" style=\"width: 15px; height: 15px;\" />",
":o" => "<img src=\"".$this->path."surpris.gif\" alt=\":o\" style=\"width: 15px; height: 16px;\" />",
":-o" => "<img src=\"".$this->path."surpris.gif\" alt=\":-o\" style=\"width: 15px; height: 16px;\" />",
":O" => "<img src=\"".$this->path."surpris.gif\" alt=\":O\" style=\"width: 15px; height: 16px;\" />",
":-O" => "<img src=\"".$this->path."surpris.gif\" alt=\":-O\" style=\"width: 15px; height: 16px;\" />",
":D" => "<img src=\"".$this->path."biggrin.gif\" alt=\":D\" style=\"width: 15px; height: 16px;\" />",
":-D" => "<img src=\"".$this->path."biggrin.gif\" alt=\":-D\" style=\"width: 15px; height: 16px;\" />",
":/" => "<img src=\"".$this->path."confus.gif\" alt=\":/\" style=\"width: 15px; height: 22px;\" />",
":|" => "<img src=\"".$this->path."confus.gif\" alt=\":|\" style=\"width: 15px; height: 22px;\" />",
":!" => "<img src=\"".$this->path."confus.gif\" alt=\":!\" style=\"width: 15px; height: 22px;\" />",
"=)" => "<img src=\"".$this->path."ptdr.gif\" alt=\"=)\" style=\"width: 15px; height: 15px;\" />",
":lol:" => "<img src=\"".$this->path."ptdr.gif\" alt=\":lol:\" style=\"width: 15px; height: 15px;\" />",
"lol" => "<img src=\"".$this->path."ptdr.gif\" alt=\"lol\" style=\"width: 15px; height: 15px;\" />",
"LOL" => "<img src=\"".$this->path."ptdr.gif\" alt=\"LOL\" style=\"width: 15px; height: 15px;\" />",
"L O L" => "<img src=\"".$this->path."ptdr.gif\" alt=\"L O L\" style=\"width: 15px; height: 15px;\" />",
"mdr" => "<img src=\"".$this->path."ptdr.gif\" alt=\"mdr\" style=\"width: 15px; height: 15px;\" />",
":]" => "<img src=\"".$this->path."diablotin.gif\" alt=\":]\" style=\"width: 21px; height: 19px;\" />"
);
return strtr($ligne, $smileys);
}
######################################################
# Fonction: status
# Paramètres: ligne
# Description : regarde si la ligne a un statut particulier (emote, message serveur ...)
######################################################
function status($ligne)
{
// Chaînes à reconnaître (commençant par *)
// sous la forme chaîne => couleur
$spestr = array (
"Attempting to rejoin channel" => 2,
"has joined #" => 3,
"has quit" => 2,
"is now known as" => 3,
"Rejoigned channel #" => 3,
"Retrieving #" => 3,
"Set by" => 3,
"sets mode:" => 3,
"Topic is" => 3,
"* " => 13 // emote
);
if($ligne[0] == "*")
{
foreach($spestr as $src => $value)
{
if(strpos($ligne, $src) !== false)
{
return $this->color($value).$ligne."</span>";
}
}
}
else if($ligne[0] == "-")
{
return $this->color(5).$ligne."</span>";
}
else
{
return $ligne;
}
}
}
?>