Old stuff/old_sites/ids/func.php
(Deskargatu)
<?php
function hex_encode ($str)
{
$encoded = bin2hex($str);
$encoded = chunk_split($encoded, 2, '%');
$encoded = '%'.substr($encoded, 0, strlen($encoded) - 1);
return $encoded;
}
class connection
{
var $con_id;
var $error;
var $errno;
function connection($db_user, $db_password , $db_host, $db_name)
{
$this->error = '';
$this->con_id = @mysql_connect($db_host, $db_user, $db_password);
if (!$this->con_id) {
$this->setError();
} else {
$this->database($db_name);
}
}
function database($db_name)
{
$db = @mysql_select_db($db_name);
if(!$db) {
$this->setError();
return false;
} else {
return true;
}
}
function close()
{
if ($this->con_id) {
mysql_close($this->con_id);
return true;
} else {
return false;
}
}
function selectQuery($query)
{
if (!$this->con_id) {
return false;
}
$cur = mysql_unbuffered_query($query, $this->con_id);
if ($cur)
{
$res = mysql_fetch_row($cur);
if (count($res) === 1)
{
$result = $res[0];
}
else
{
$result = array();
for($j=0; $j<count($res); $j++)
{
$result[strtolower(mysql_field_name($cur, $j))] = $res[$j];
}
}
return $result;
}
else
{
$this->setError();
return false;
}
}
function assocQuery($query)
{
if (!$this->con_id) {
return false;
}
$cur = mysql_unbuffered_query($query, $this->con_id);
if ($cur)
{
$i = 0;
$result = array();
while($res = mysql_fetch_row($cur))
{
for($j=0; $j<count($res); $j++)
{
$result[$i][strtolower(mysql_field_name($cur, $j))] = $res[$j];
}
$i++;
}
return $result;
}
else
{
$this->setError();
return false;
}
}
function execute($query)
{
if (!$this->con_id) {
return false;
}
$cur = mysql_query($query, $this->con_id);
if (!$cur) {
$this->setError();
return false;
} else {
return true;
}
}
function getLastID()
{
if ($this->con_id) {
return mysql_insert_id($this->con_id);
} else {
return false;
}
}
function setError()
{
if ($this->con_id) {
$this->error = mysql_error($this->con_id);
$this->errno = mysql_errno($this->con_id);
} else {
$this->error = mysql_error();
$this->errno = mysql_errno();
}
}
function error()
{
if ($this->error != '') {
return $this->errno.' - '.$this->error;
} else {
return false;
}
}
function escapeStr($str)
{
return mysql_escape_string($str);
}
}
function html2text($str)
{
$a = array(
"/<span style=\"text-decoration:line-through\">(.+?)<\/span>/",
"/<a target=\"_blank\" href=\"(.+?)\">(.+?)<\/a>/",
"/<img alt=\"Image sans description\" src=\"(.+?)\" \/>/"
);
$b = array (
"<n>$1</n>",
"[$1|$2]",
"[img=$1]"
);
return preg_replace($a,$b,$str);
}
function text2html($str)
{
$a=array(
"/<n>(.+?)<\/n>/",
"/<i>(.+?)<\/i>/",
"/<b>(.+?)<\/b>/",
"/\[(http|ftp):\/\/([^\s\n\|]+)\|([^\]]+)\]/",
"/\[img=([^\s\n\]]+)\]/"
);
$b=array(
"<span style=\"text-decoration:line-through\">$1</span>",
"<i>$1</i>",
"<b>$1</b>",
"<a href=\"$1://$2\">$3</a>",
"<img alt=\"Image sans description\" src=\"$1\" \/>",
);
$str = htmlentities($str);
$str= preg_replace($a,$b,$str);
$str = preg_replace("/(?:\n|\s|$){2}(http|ftp):\/\/([^\s\n\<]+)/","<a href=\"$1://$2\">$1://$2</a>",$str);
return preg_replace(
"/<a href=\"([^\"]+)\">(http|ftp):\/\/([^<]{30})([^<]{4,})<\/a>/",
"<a href=\"$1\">$2:\/\/$3(...)<\/a>",
$str);
}
function normalise($str)
{
$str = preg_replace('/[^a-z0-9_-\s\']/','',strtolower($str));
$str = preg_replace('/[\s\']+/',' ',trim($str));
$str = str_replace(' ','-',$str);
return $str;
}
function ip_read($art_f)
{
$ip_file = dirname(__FILE__)."/ips/$art_f";
$hd = fopen($ip_file,"r");
$contenu = fread($hd,filesize($ip_file));
fclose($hd);
$var_ip = unserialize(base64_decode($contenu));
#
if ($var_ip['date'] != date(Ymd))
{
unset($var_ip);
$var_ip = array('date' => date(Ymd));
foreach(array('account','article','breves','edito','mail','visites') as $key)
{
ip_write($key,$var_ip);
}
}
return $var_ip;
}
function ip_write($art_f,$var_f)
{
$ip_file = dirname(__FILE__)."/ips/$art_f";
$hd = fopen($ip_file,"w") or die("FATAL writing $art_f and ".$var_f['date']);
fwrite($hd,base64_encode(serialize($var_f)));
fclose($hd);
}
function affichage($var_a,$var_b,$var_c)
{
if($var_b == 0 ) { $var_b = 1000000; }
$i=0;
$li = 0;
$lignes = explode("\n",$var_a);
foreach($lignes as $key)
{
$key = preg_replace("/\_([\w\ ]+?)\_/","<span class=\"color\">$1</span>",$key);
$key = substr($key,0,$var_b);
if ($i>$var_b) { break; }
if (ereg("^\_{2}...$",$key)) { echo "<hr>"; }
if (!$code and ereg("^\[code\](.*)\[\/code\]",$key,$regs)) { echo "<pre>$regs[1]</pre>"; }
elseif(ereg("(.*)\[\/code\]",$key) and $pre) { echo preg_replace("\[code\]","",$key)."</pre>"; $pre=0;}
elseif(ereg("^\[code\]",$key)) { echo "<pre>".preg_replace("\[code\]","",$key); $pre=1;}
elseif(ereg("(.*)\[\/cadre\]",$key) and $cadre) { echo preg_replace("\[cadre\]","",$key)."</pre>"; $cadre=0;}
elseif(!$cadre and ereg("^\[cadre\]",$key)) { echo "<div class=\"cadre\">".preg_replace("\[cadre\]","",$key); $cadre=1;}
elseif (ereg("^-",$key) and !$pre)
{
$key = preg_replace("/^-(.*)/","$1",$key);
if($li == 0)
{
echo "<ul><li>$key</li>";
$li= 1;
}
else { echo "<li>$key</li>"; }
}
elseif($li==1) {
echo "</ul>";
$li=0;
print "<p>$key</p>\n";
}
elseif (!$pre) { print "<p>$key</p>\n"; }
else { print "$key\n"; }
$i = 1+$i + count(split(".",$key));
}
if ($li==1) { echo "</ul>"; }
if ($pre==1) { echo "</pre>"; }
if ($cadre==1) { echo "</div>"; }
if ($var_b != 1000000 and $i != count($lignes)) { echo "<p><a onclick=\"window.open('".$var_c."','Zoom sur l actualite','width=400,height=300,resizable=yes,statusbar=yes,scrollbar=yes')\" href=\"#\" title=\"Suite\">[Suite]</a></p>"; }
}
function getmicrotime()
{
list($usec,$sec) = explode(" ",microtime());
return ((float)$usec + (float)$sec);
}
function aide()
{
echo "<p>Aide formattage des messages:</p><ul>
<li>Seules les balises <i>(texte en italique) </i> et <b> (texte en gras) </b> sont acceptees. Toute autre balise sera converti en &lt;balise&gt;. </li>
<li>Pour donner des urls en specifiant un nom, vous pouvez utiliser la syntaxe <span class=\"underline\">[http://votre_url.org|le nom du lien]</span> ce qui donnera un affichage "le nom du lien".</li>
<li>Pour creer une liste, entrez les elements en debut de ligne en commençant par des \"-\"</li>
<li>Pour inserer une image, il suffit d'utiliser [img=url_de_l'image_voulue].
<li>Pour inserer du code: [code] votre source [/code]. </li>
<li>Pour inserer un encadré: [cadre] votre texte encadré [/cadre]. </li>
<li>Barrer du texte: <n>Texte</n></li>
<li>Colorer du texte : _Texte_</li>
</ul>
";
}
function miniature($picture,$dest) /* Tire de php.net */
{
$max=100;
$src_img=ImagecreateFromJpeg($picture);
$oh = imagesy($src_img); # original height
$ow = imagesx($src_img); # original width
$new_h = $oh;
$new_w = $ow;
if($oh > $max || $ow > $max){
$r = $oh/$ow;
$new_h = ($oh > $ow) ? $max : $max*$r;
$new_w = $new_h/$r;
}
// note TrueColor does 256 and not.. 8
$dst_img = ImageCreateTrueColor($new_w,$new_h);
ImageCopyResized($dst_img, $src_img, 0,0,0,0, $new_w, $new_h, ImageSX($src_img), ImageSY($src_img));
ImageJpeg($dst_img, $dest);
}
?>