501 lines
12 KiB
PHP
Executable File

<?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 Chaine.php
*
*Ce fichier contient la classe Chaine
*<BR>
* @author Rooty <www.rooty.me|rooty@rooty.me>
* @link www.rooty.me
* @since 2004/04/09
* @version 0.3
* @copyright Copyright &copy; 2009-2018, Rooty
* @package system_utils
*/
/**
* Classe Chaine
*
* Permet la gestion de chaines
* @package system_utils
*/
class Chaine
{
/**#@+
* @access public
*/
/**
* @var string
* @desc contient la chaine d'origine
*/
var $src = null;
/**#@-*/
//
// Constructor
//
/**
*Constructeur de la classe Chaine
*
*<p>Initialise la donnée</p>
*
* @param chaine source de données
*/
function __construct($src)
{
$this->src=$src;
}
/**
*String to html
*
*<p>Transforme une chaine de caractère au format HTML à partir de $src</p>
*
*/
function strToHtml()
{
$tmp=htmlspecialchars($this->src, ENT_QUOTES);
$tmp=str_replace(chr(13).chr(10),"<br>",$tmp);
return $tmp;
}
/**
*html to String
*
*<p>Transforme du code HTML en chaine de caractère à partir de $src</p>
*
*/
function htmlToStr()
{
$tmp=$this->src;
$tmp=unhtmlentities($tmp);
$tmp=str_replace("<br>",chr(13).chr(10),$tmp);
//$tmp = addslashes(stripslashes($tmp));
return $tmp; // retourne une chaine
}
/**
*StrCut
*
*<p>Récupere le debut d'une chaine de caractere (nbChar)</p>
*
* @param entier nombre de caractère à récuperer
*/
function strCut($nbChar)
{
$tmp=$this->src;
# On enleve les retours à la ligne
$tmp=str_replace(chr(13).chr(10),"",$tmp);
$strlen=strlen($tmp);
# Si la taille n'est pas valide
if ($strlen>$nbChar)
{
$tmp=substr($tmp,0,$nbChar);
}
return $tmp;
}
/**
*HtmlCut
*
*<p>Récupere le debut du texte d'une chaine html (nbChar)</p>
*
* @param entier nombre de caractère à récuperer
*/
function htmlCut($nbChar)
{
$tmp=$this->src;
# On enlève les espaces superflux
while (!(strpos($tmp, " ")===false))
{
$tmp=str_replace(" "," ",$tmp);
}
# on enlève les caractères spéciaux
$tmp=unhtmlentities($tmp);
// $tmp = mb_eregi_replace("^[[:space:]]*[a-z]+[.)".chr(176)."]([[:space:]]*)", "",$tmp);
# on a maintenant une chaine propre à l'utilisation...
# On enleve les retours à la ligne
$tmp=str_replace(chr(13).chr(10),"",$tmp);
$strlen=strlen($tmp);
# Si la taille n'est pas valide
if ($strlen>$nbChar)
{
$tmp=substr($tmp,0,$nbChar)."&nbsp;...";
}
//$tmp = addslashes(stripslashes($tmp));
return $tmp;
}
function truncateHtml($length = 500, $ending = '...', $exact = false, $considerHtml = true)
{
$text=$this->src;
if ($considerHtml) {
// if the plain text is shorter than the maximum length, return the whole text
if (strlen(preg_replace('/<.*?>/', '', $text)) <= $length) {
return $text;
}
// splits all html-tags to scanable lines
preg_match_all('/(<.+?>)?([^<>]*)/s', $text, $lines, PREG_SET_ORDER);
$total_length = strlen($ending);
$open_tags = array();
$truncate = '';
foreach ($lines as $line_matchings) {
// if there is any html-tag in this line, handle it and add it (uncounted) to the output
if (!empty($line_matchings[1])) {
// if it's an "empty element" with or without xhtml-conform closing slash
if (preg_match('/^<(\s*.+?\/\s*|\s*(img|br|input|hr|area|base|basefont|col|frame|isindex|link|meta|param)(\s.+?)?)>$/is', $line_matchings[1])) {
// do nothing
// if tag is a closing tag
} else if (preg_match('/^<\s*\/([^\s]+?)\s*>$/s', $line_matchings[1], $tag_matchings)) {
// delete tag from $open_tags list
$pos = array_search($tag_matchings[1], $open_tags);
if ($pos !== false) {
unset($open_tags[$pos]);
}
// if tag is an opening tag
} else if (preg_match('/^<\s*([^\s>!]+).*?>$/s', $line_matchings[1], $tag_matchings)) {
// add tag to the beginning of $open_tags list
array_unshift($open_tags, strtolower($tag_matchings[1]));
}
// add html-tag to $truncate'd text
$truncate .= $line_matchings[1];
}
// calculate the length of the plain text part of the line; handle entities as one character
$content_length = strlen(preg_replace('/&[0-9a-z]{2,8};|&#[0-9]{1,7};|[0-9a-f]{1,6};/i', ' ', $line_matchings[2]));
if ($total_length+$content_length> $length) {
// the number of characters which are left
$left = $length - $total_length;
$entities_length = 0;
// search for html entities
if (preg_match_all('/&[0-9a-z]{2,8};|&#[0-9]{1,7};|[0-9a-f]{1,6};/i', $line_matchings[2], $entities, PREG_OFFSET_CAPTURE)) {
// calculate the real length of all entities in the legal range
foreach ($entities[0] as $entity) {
if ($entity[1]+1-$entities_length <= $left) {
$left--;
$entities_length += strlen($entity[0]);
} else {
// no more characters left
break;
}
}
}
$truncate .= substr($line_matchings[2], 0, $left+$entities_length);
// maximum lenght is reached, so get off the loop
break;
} else {
$truncate .= $line_matchings[2];
$total_length += $content_length;
}
// if the maximum length is reached, get off the loop
if($total_length>= $length) {
break;
}
}
} else {
if (strlen($text) <= $length) {
return $text;
} else {
$truncate = substr($text, 0, $length - strlen($ending));
}
}
// if the words shouldn't be cut in the middle...
if (!$exact) {
// ...search the last occurance of a space...
$spacepos = strrpos($truncate, ' ');
if (isset($spacepos)) {
// ...and cut the text in this position
$truncate = substr($truncate, 0, $spacepos);
}
}
// add the defined ending to the text
$truncate .= $ending;
if($considerHtml) {
// close all unclosed html-tags
foreach ($open_tags as $tag) {
$truncate .= '</' . $tag . '>';
}
}
return $truncate;
}
function chainCut($nbChar)
{
$tmp=$this->src;
// # On enlève les espaces superflux
// while (!(strpos($tmp, " ")===false))
// {
// $tmp=str_replace(" "," ",$tmp);
// }
# on enlève les caractères spéciaux
$tmp=unhtmlentities($tmp);
// $tmp = mb_eregi_replace("^[[:space:]]*[a-z]+[.)".chr(176)."]([[:space:]]*)", "",$tmp);
# on a maintenant une chaine propre à l'utilisation...
# On enleve les retours à la ligne
$tmp=str_replace(chr(13).chr(10),"",$tmp);
$strlen=strlen($tmp);
# Si la taille n'est pas valide
if ($strlen>$nbChar)
{
$tmp=substr($tmp,0,$nbChar)."&nbsp;...";
}
//$tmp = addslashes(stripslashes($tmp));
return $tmp;
}
/**
*Str To Sql
*
*<p>Transforme une chaine de caractères en chaine Sql à partir de $src</p>
*
*<p>Utilisation de htmlentities</p>
*
*/
function strToSql()
{
$tmp=$this->src;
$tmp=htmlentitiesconv($tmp); //stockés en htmlentities
return $tmp;
}
/**
*Html To Sql
*
*<p>Transforme une chaine de caractères au format HTML à partir de $src</p>
*
*/
function htmlToSql()
{
$tmp=$this->src;
return $tmp;
}
/**
*SQL To Str
*
*<p>Transforme une chaine de caractères à partir de $src</p>
*
*/
function sqlToStr()
{
$tmp=$this->src;
return $tmp;
}
/**
*SQL To Html
*
*<p>Transforme une chaine de caractères au format HTML à partir de $src</p>
*
*/
function sqlToHtml()
{
$tmp=$this->src;
return $tmp;
}
/**
* bytexor
* @private
*/
function bytexor($a,$b,$l)
{
$c="";
for($i=0;$i<$l;$i++) {
$c.=$a[$i]^$b[$i];
}
return($c);
}
/**
* binmd5
* @private
*/
function binmd5($val)
{
return(pack("H*",md5($val)));
}
/*
*Decrypt
*<p>Decryptage de la source de donnée à partir d'une clé</p>
*
* @param chaine
* @desc clé de l'encryptage
* @return chaine
* @desc chaine décryptée à partir de la source de l'objet (source cryptée)
*/
function decrypt($paramKey)
{
$msg=$this->src;
$key=$paramKey;$sifra="";
$key1=$this->binmd5($key);
while($msg) {
$m=substr($msg,0,16);
$msg=substr($msg,16);
$sifra.=$m=$this->bytexor($m, $key1, strlen($m));
$key1=$this->binmd5($key.$key1.$m);
}
return($sifra);
}
/*
*Encryptage
*<p>Encryptage de la source de donnée à partir d'une clé</p>
*
* @param chaine
* @desc clé de l'encryptage
* @return chaine
* @desc chaine cryptée à partir de la source de l'objet'
*/
function encrypt($paramKey)
{
$msg=$this->src;
$key=$paramKey;$sifra="";
$key1=$this->binmd5($key);
while($msg) {
$m=substr($msg,0,16);
$msg=substr($msg,16);
$sifra.=$this->bytexor($m, $key1, strlen($m));
$key1=$this->binmd5($key.$key1.$m);
}
return($sifra);
}
/*
* formatNameClean
*<p>Enléve tous les characteres spéciaux</p>
*
* @return chaine
* @desc chaine chaine sans caractéres spéciaux
*/
function formatNameClean()
{
$ch = $this->src;
/*Fonction permettant de supprimer les numeros, le point et l'espace à l'affichage*/
$ch = eregi_replace("^[[:space:]]*[a-z]+[.)".chr(176)."][[:space:]]+", "", $ch);
$s = strtolower($ch);
# Gestion des char remplacables
$s=ereg_replace("à|À|â|Â|ä|Ä","a",$s);
$s=ereg_replace("ç|Ç","c",$s);
$s=ereg_replace("è|È|é|É|ê|Ê|ë|Ë","e",$s);
$s=ereg_replace("î|Î|ï|Ï","i",$s);
$s=ereg_replace("ô|Ô","o",$s);
$s=ereg_replace("ù|Ù|û|Û","u",$s);
# Gestion des espaces
$s=ereg_replace(" | |-|/|,","_",$s);
$nb=strlen($s);
$i=0;
while($i<$nb)
{
// Suppression de tous les chars spèciaux!!!
if ( ((ord(substr($s,$i,1))<97) || (ord(substr($s,$i,1))>122)) && ((ord(substr($s,$i,1))!=95)) ){
$s=substr($s,0,$i).substr($s,($i+1));
$i=0;
$nb=strlen($s);
}
$i++;
}
if (strlen($s)>25)
$s=substr($s,0,25);
return $s;
}
/*
* formatNameClean
*<p>Enléve tous les characteres spéciaux</p>
*
* @return chaine
* @desc chaine chaine sans caractéres spéciaux
*/
function formatNameWidthNumberClean()
{
$ch = $this->src;
/*Fonction permettant de supprimer les numeros, le point et l'espace à l'affichage*/
$ch = eregi_replace("^[[:space:]]*[a-z][0-9]+[.)".chr(176)."][[:space:]]+", "", $ch);
$s = strtolower($ch);
# Gestion des char remplacables
$s=ereg_replace("à|À|â|Â|ä|Ä","a",$s);
$s=ereg_replace("ç|Ç","c",$s);
$s=ereg_replace("è|È|é|É|ê|Ê|ë|Ë","e",$s);
$s=ereg_replace("î|Î|ï|Ï","i",$s);
$s=ereg_replace("ô|Ô","o",$s);
$s=ereg_replace("ù|Ù|û|Û","u",$s);
# Gestion des espaces
$s=ereg_replace(" | |-|/|,","_",$s);
$nb=strlen($s);
$i=0;
while($i<$nb)
{
// Suppression de tous les chars spèciaux!!!
if ( ((ord(substr($s,$i,1))<48) || (ord(substr($s,$i,1))>57)) && ((ord(substr($s,$i,1))<97) || (ord(substr($s,$i,1))>122)) && ((ord(substr($s,$i,1))!=95)) ){
$s=substr($s,0,$i).substr($s,($i+1));
$i=0;
$nb=strlen($s);
}
$i++;
}
if (strlen($s)>25)
$s=substr($s,0,25);
return $s;
}
/*
* NoSpecialChar
*<p>Enléve tous les characteres spéciaux</p>
*
* @return chaine
* @desc chaine chaine sans caractéres spéciaux
*/
function noSpecialChar()
{
$tmp=$this->src;
$tmp=strtoupper($tmp);
$i=1;
$retourValue="";
while ($i <= strlen($tmp))
{
$testValue=substr($tmp,$i-1,1);
if (($testValue>="A" && $testValue<="Z") || ($testValue>="0" && $testValue<="9"))
{
$retourValue.=$testValue;
}
$i++;
}
return $retourValue;
}
/*
* StripHtmlTags
*<p>Enléve toutes les tags HTML</p>
*
* @return chaine
* @desc chaine chaine sans caractéres spéciaux
*/
function stripHtmlTags()
{
$tmp=strip_tags($this->src);
$tmp=unhtmlentities($tmp);
return $tmp;
}
}
?>