'Déconnecté', 'available' => 'En ligne', 'dnd' => 'Occupé', 'xa' => 'Pas dispo.', 'chat' => 'Disponible', 'away' => 'Absent', ); public static function getUrl() { if (empty(self::$url)) { $path = dirname($_SERVER['SCRIPT_NAME']).'/'; $path = str_replace('//', '/', $path); self::$url = 'http://'.$_SERVER['HTTP_HOST'] . $path; } return self::$url; } public static function getStatusMsg($status) { if (isset(self::$status[$status])) return self::$status[$status]; else return '??'; } public static function encodeMail($mail) { $mail = strrev($mail); $mail = str_rot13($mail); $mail = str_replace('@', '..', $mail); return $mail; } public static function decodeMail($mail) { $mail = str_replace('..', '@', $mail); $mail = str_rot13($mail); $mail = strrev($mail); if (!preg_match('/^[a-z0-9]+[a-z0-9_.+@-]+$/', $mail)) { throw new Exception('JID invalide'); } return $mail; } public static function displayImage($image, $cache) { header("Content-type: image/png"); ob_start(); imagepng($image); $data = ob_get_contents(); ob_end_flush(); imagedestroy($image); file_put_contents(dirname(__FILE__) . '/cache/' . $cache, $data); return true; } public static function getDummyAvatar($size=96) { if (!in_array($size, array(32, 64, 80, 96))) $size = 64; $img = imagecreatefrompng(dirname(__FILE__) . '/dummy-'.(int)$size.'.png'); return $img; } public static function getAvatar($jid, $size=0) { $hash = md5($jid); $url = 'http://presence.jabberfr.org/'.$hash; switch ($size) { case 96: case 80: case 64: case 32: $url .= '/avatar-'.(int)$size.'.png'; break; default: $url .= '/avatar.png'; break; } // On fait une copie locale de l'image pour pas faire 2 requêtes sur la même url $tmp = dirname(__FILE__) . '/cache/tmp_avatar_' . md5($url); if (!@copy($url, $tmp)) { throw new Exception("Pas d'avatar trouvé."); } list(,,$type) = getimagesize($tmp); $img = false; switch ($type) { case IMAGETYPE_GIF: $img = imagecreatefromgif($tmp); break; case IMAGETYPE_JPEG: $img = imagecreatefromjpeg($tmp); break; case IMAGETYPE_PNG: $img = imagecreatefrompng($tmp); break; default: @unlink($tmp); throw new Exception("Format d'image de l'avatar inconnu."); break; } @unlink($tmp); return $img; } public static function getStatus($jid) { $url = 'http://presence.jabberfr.org/'.md5($jid).'/image.png'; $status = 'offline'; $img = imagecreatefrompng($url); if (empty($img)) { throw new Exception('Impossible de récupérer le statut'); } foreach ($http_response_header as $v) { if (preg_match('!Location: http://presence.jabberfr.org/.*/([a-z0-9]+)\.png!', $v, $match)) { $status = $match[1]; } } return array($status, $img); } public static function sendErrorImage($msg) { $msg = wordwrap($msg, 40); header("Content-type: image/png"); $image = imagecreatetruecolor(300, 64); $noir = imagecolorallocate($image, 0, 0, 0); $blanc = imagecolorallocate($image, 255, 255, 255); imagefill($image, 0, 0, $blanc); imagettftext($image, 9, 0, 2, 10, $noir, get_font('FreeSans'), 'Erreur : ' . $msg); imagepng($image); return; } public static function getCacheName() { $hash = ''; $args = func_get_args(); foreach ($args as $v) { $hash .= '-' . $v; } $hash = md5($hash); $file = $hash . '.png'; return $file; } public static function isCached($file, $expire=3600) { $cache = dirname(__FILE__) . '/cache/' . $file; if (file_exists($cache) && filemtime($cache) > (time() - (int)$expire)) return true; else return false; } public static function redirectImage($file) { $url = self::getUrl() . 'cache/' . $file; header('HTTP/1.1 302 Found'); header('Location: '.$url); header('Content-Type: image/png'); header('Connection: close'); exit; } public static function ImageBlur(&$image, $ratio) { $w = imagesx($image); $h = imagesy($image); $sum = pow(2, $ratio); $src = array(); $src['temp1'] = imagecreatetruecolor($w, $h); $src['temp2'] = imagecreatetruecolor($w, $h); $coeffs = array ( array ( 1), array ( 1, 1), array ( 1, 2, 1), array ( 1, 3, 3, 1), array ( 1, 4, 6, 4, 1), array ( 1, 5, 10, 10, 5, 1), array ( 1, 6, 15, 20, 15, 6, 1), array ( 1, 7, 21, 35, 35, 21, 7, 1), array ( 1, 8, 28, 56, 70, 56, 28, 8, 1), array ( 1, 9, 36, 84, 126, 126, 84, 36, 9, 1), array ( 1, 10, 45, 120, 210, 252, 210, 120, 45, 10, 1), array ( 1, 11, 55, 165, 330, 462, 462, 330, 165, 55, 11, 1) ); // Traitement for ( $i=0 ; $i < $w ; ++$i ) { for ( $j=0 ; $j < $h ; ++$j ) { $sumr=0; $sumg=0; $sumb=0; for ( $k=0 ; $k <= $ratio ; ++$k ) { $color = imagecolorat($image, $i-(($ratio)/2)+$k, $j); $r = ($color >> 16) & 0xFF; $g = ($color >> 8) & 0xFF; $b = ($color) & 0xFF; $sumr += $r*$coeffs[$ratio][$k]; $sumg += $g*$coeffs[$ratio][$k]; $sumb += $b*$coeffs[$ratio][$k]; } $color = imagecolorallocate ($src['temp1'], $sumr/$sum, $sumg/$sum, $sumb/$sum ); imagesetpixel($src['temp1'],$i,$j,$color); } } imagedestroy($image); for ( $i=0 ; $i < $w ; ++$i ) { for ( $j=0 ; $j < $h ; ++$j ) { $sumr=0; $sumg=0; $sumb=0; for ( $k=0 ; $k <= $ratio ; ++$k ) { $color=imagecolorat($src['temp1'], $i, $j-(($ratio)/2)+$k); $r=($color >> 16) & 0xFF; $g=($color >> 8) & 0xFF; $b=($color) & 0xFF; $sumr += $r*$coeffs[$ratio][$k]; $sumg += $g*$coeffs[$ratio][$k]; $sumb += $b*$coeffs[$ratio][$k]; } $color = imagecolorallocate ($src['temp2'], $sumr/$sum, $sumg/$sum, $sumb/$sum ); imagesetpixel($src['temp2'],$i,$j,$color); } } imagedestroy($src['temp1']); $image = $src['temp2']; } } process_path_info(); ?>