<?php

/**-----------------------------------------------
*
* Rooty, 2018 <rooty@rooty.me>
*
*
* This software is protected by copyright, please
* read the file COPYRIGHT.
* This program is distributed in the hope that it
* will be useful, but without any warranty; without
* even the implied warranty of merchantability or
* fitness for a particular purpose. Please
* read the file LICENCE.
*
* Fichier highlight.php
*
* Ce fichier contient la classe highlight
*<BR>
* @author Initiance <www.initiance.com|martial@initiance.com>
* @copyright Copyright &copy; 2004-2005, Initiance 
* @since 2004/06/07
* @version 0.3
* @link www.rooty.me
* @package object_plugin
* @subpackage Highlight
* @docreview Tristan <tristan@initiance.com> | 30-10-2005
*/

if ( !defined('SYSTEM_IN') )
{
 die("Hacking attempt");
}

/**
* TODOC
*/
define("USEAUTOGENERATEDCOLORS", TRUE);

/**
* Classe Highlight
*<P>Permet de surligner des mots sur la page courante</P>
* @package object_plugin
* @subpackage Highlight
*/
class Highlight extends Plugin{
	var $defaultBackColor = null;
	var $defaultBackColors = null;
	var $defaultTextColor = null;
	var $defaultTextColors = null;
	var $highlightFromCode = false;
	var $word = null;
	var $backcolor = null;
	var $textcolor = null;
/**

* Constructeur de la classe Plugin Debug
* @access public
*/
	function Highlight(){
		$this->__construct();
	}

/**
* Constructeur de la classe Plugin Debug
* @access public
*/
	function __construct(){
		parent::__construct("1.0", "", "");
		$this->defaultBackColor = "yellow";
		$this->defaultTextColor = "blue";
		$this->defaultBackColors = array("yellow", "blue", "red", "green", "orange", "pink");
		$this->defaultTextColors = array("blue", "yellow", "green", "red", "pink", "orange");
	}

/**
* Surligne un mot en backColor et change la couleur du mot en textColor.
* @access private
*/	
	function doHighlight($word, $backColor, $textColor){
		static $num=0;
		if(empty($backColor)) $backColor=$this->defaultBackColor;
		if(empty($textColor)) $textColor=$this->defaultTextColor;
		$GLOBALS["SYSTEM_CONTROL"]->data = eregi_replace("</head>", "
<style language=\"text/css\">
.highlight".$num." {
	background-color:#".$backColor.";
	color:#".$textColor.";
	font-style:solid;
	font-weight:bold;
}	
</style>
</head>
		", $GLOBALS["SYSTEM_CONTROL"]->data);
		$GLOBALS["SYSTEM_CONTROL"]->data = $this->html_replace($word, "<span class=\"highlight".$num."\">".$word."</span>", $GLOBALS["SYSTEM_CONTROL"]->data);
		$num++;
	}

/**
* Appelle doHighlight pour chaque éléments des tableaux
* @access private
*/		
	function doHighlightFromArray($words, $backColors=NULL, $textColors=NULL){
		if(!empty($words) && is_array($words) && count($words)){
			$nbwords = count($words);
			if(!empty($backColors) && is_array($backColors) && count($backColors) && !empty($textColors) && is_array($textColors) && count($textColors)){
				for($i=0 ; $i<$nbwords ; $i++){
					$this->doHighlight($words[$i], $backColors[$i], $textColors[$i]);
				}
			} else {
				$colors = $this->getRandomColor($nbwords);
				for($i=0 ; $i<$nbwords ; $i++){
					$this->doHighlight($words[$i], $colors['backcolor'][$i], $colors['textcolor'][$i]);
				}
			}
			return true;
		} return false;
	}

/**
* Renvoie un tableau de couleurs aléatoire [backColor=>xxx, textColor=>xxx]
* @access private
*/	
	function getRandomColor($nb=1){
		if($nb){
			$j=0;
			$tab = array();
			$tab['backcolor'] = array();
			$tab['textcolor'] = array();
			for($i=0 ; $i<$nb ; $i++){
				srand($this->make_seed());
				$random = rand(100,200);
				$color = dechex($random);
				$negatif = dechex(255-$random);
				switch($j){
					case 0 :
						$tab['backcolor'][] = "00".$color."FF";
						$tab['textcolor'][] = "FF".$negatif."00";
					break;
					case 1 :
						$tab['backcolor'][] = "FF00".$color;
						$tab['textcolor'][] = "00FF".$negatif;
					break;
					case 2 :
						$tab['backcolor'][] = $color."FF00";
						$tab['textcolor'][] = $negatif."00FF";
					break;	
				}
				if($j==2) $j=0;
				else $j++;
			}
			return $tab;
		}
	}

	function doHighlightFromCode($word, $backColor=NULL, $textColor=NULL){
		$this->highlightFromCode = true;
		if(!empty($word)){
			$this->word = $word;
			$this->backColor = $backColor;
			$this->textColor = $textColor;
			return true;
		} else {
			return false;
		}
	}

	function make_seed(){
		list($usec, $sec) = explode(' ', microtime());
		return (float) $sec + ((float) $usec * 100000);
	}

	function html_replace ($from, $to, $txt, $func = 'str_replace'){
		$pos = strpos($txt, '<');
		if (FALSE !== $pos) {
			$txt1 = substr($txt, 0, $pos);
			$txt2 = substr($txt, $pos);
			$txt1 = $func("$from", "$to", $txt1);
		}
		else {
			$txt1 = '';
			$txt2 = $txt;
		}
		$from = str_replace('\\', '\\\\', addslashes($from));
		$to = str_replace('\\', '\\\\', addslashes($to));
		$rplc = $func.'("'.$from.'", "'.$to.'", "\\';
		$txt2 = preg_replace('/<([^>]*)>([^<]*)/ie', '"<\1>".'.$rplc.'2")', $txt2);
		return stripslashes($txt1.$txt2);
	}

/**
* Méthode héritée
* @access private
*/
	function __afterProcess(){
 		if(isset($_GET['highlight'])){
 			$this->doHighlight($_GET['highlight'], $this->defaultBackColor, $this->defaultTextColor);
 		}
		if(isset($_GET['hwords']) && is_array($_GET['hwords']) && count($_GET['hwords'])){
			$colors = $this->getRandomColor(count($_GET['hwords']));
			$this->doHighlightFromArray($_GET['hwords'], $colors['backcolor'], $colors['textcolor']);
		}
		if($this->highlightFromCode){
			if(is_array($this->word)){
				$this->doHighlightFromArray($this->word, $this->backcolor, $this->textcolor);
			} else {
				$this->doHighlight($this->word, $this->backcolor, $this->textcolor);
			}
		}
	}

}
?>
