Outils pour utilisateurs

Outils du site


php:cache_gravatar

Différences

Ci-dessous, les différences entre deux révisions de la page.

Lien vers cette vue comparative

Prochaine révision
Révision précédente
php:cache_gravatar [2012-05-28 21:56]
mitsu créée
php:cache_gravatar [2015-07-04 11:05] (Version actuelle)
mitsu
Ligne 11: Ligne 11:
 */ */
 $expire = time() -604800 ;  // default: 604800 (7 days) $expire = time() -604800 ;  // default: 604800 (7 days)
-if (isset($_GET['g']))  // g given ? if yes... +$size = 65; 
-{ + 
-if (strlen($_GET['g']) !== 32) { die; }  // g is 32 character long ? if no, die. +if(isset($_GET['s'])) { 
-$hash = ereg_replace("[^a-f0-9]", "", $_GET['g'] );  // strip out anything that doesn't belong in a md5 hash + switch ($_GET['s']) { 
-if (strlen($hash) != 32) { die; }  // still 32 characters ? if no, given hash wasn't genuine. die. + case 0: 
-$newfile = $hash.'.png'; + $size = 32; break; 
-$file = 'https://secure.gravatar.com/avatar/'.$hash.'?s=60&d=monsterid';  // gravatar, 60px monsterid. + case 1: 
-if(file_exists($newfile) && filemtime($newfile) < $expire) + $size = 120; break; 
-unlink($newfile); // expired gravatar, out ! + default: 
-if (file_exists($newfile)) + $size = 65; 
- // the gravatar wasn't removed before: it's valid and doesn't need refreshment +
-else +
-copy($file, $newfile);   // gravatar deleted, getting new. + 
-$imagecheck = getimagesize($newfile); +if (isset($_GET['g'])) {   // g given ? if yes... 
-if ($imagecheck['mime']!=='image/png' // is it a PNG ? + if (strlen($_GET['g']) !== 32) { header("HTTP/1.0 404 Not Found");die; } 
-     + $hash = preg_replace("/[^a-f0-9]/", "", $_GET['g'] );  
-imagepng(imagecreatefromjpeg($newfile),$newfile.'2');  // if no, creating PNG and replacing +  if (strlen($hash) != 32) { header("HTTP/1.0 404 Not Found");die; }   
-unlink($newfile); + 
-rename($newfile.'2', $newfile);+ $newfile = $hash.'_'.$size.'.png'; 
 + $file = 'http://gravatar.com/avatar/'.$hash.'?s='.$size.'&d=mm&r=x';   
 + if(file_exists($newfile) && filemtime($newfile) < $expire) { 
 + unlink($newfile); 
 +
 + if (file_exists($newfile)) { 
 +
 + else { 
 + copy($file, $newfile); 
 + $imagecheck = getimagesize($newfile); 
 + if ($imagecheck['mime']!=='image/png'
 + imagepng(imagecreatefromjpeg($newfile),$newfile.'2'); 
 + unlink($newfile); 
 + rename($newfile.'2', $newfile); 
 +
 +
 + header('Content-Type: image/png'); 
 + header('Last-Modified: '.gmdate('D, d M Y H:i:s', filemtime($newfile)).' GMT'); 
 +   header('Content-Length: ' . filesize($newfile)); 
 +   header('Cache-Control: public, max-age=86400'); 
 +   readfile($newfile); 
 + exit; 
 +
 + 
 +else { 
 + $seconds_old = 2592000; // 30 days 
 + $dirhandle = opendir('.'); 
 + while($fileclean = readdir($dirhandle)) { 
 + if( $fileclean != "." && $fileclean != ".." && $fileclean != "index.php" ) { 
 + if(filemtime($fileclean) < (time()-$seconds_old) ) { 
 + unlink($fileclean); 
 +
 +
 +
 + header('Location: /'); 
 + exit; 
 +
 + 
 +?> 
 +</file> 
 + 
 + 
 +Pour passer les avatars Wordpress, envoyez ce fichier dans le dossier des extensions et activez-le (après avoir crée le dossier "gravatars" dans wp-content et y avoir placé le "get.php" ci-dessus): 
 +<file php gravatar.php> 
 +<?php 
 +add_filter('get_avatar', 'be_gravatar_filter', 10, 5); 
 +function be_gravatar_filter($avatar, $id_or_email, $size, $default, $alt) { 
 +$email = 'a@b.c'; 
 +if ( is_numeric($id_or_email) ) { 
 +$id = (int) $id_or_email; 
 +$user = get_userdata($id); 
 +if ( $user ) 
 + $email = $user->user_email; 
 + } elseif ( is_object($id_or_email) ) { 
 + // No avatar for pingbacks or trackbacks 
 + $allowed_comment_types = apply_filters( 'get_avatar_comment_types', array( 'comment' ) ); 
 + if ( ! empty( $id_or_email->comment_type ) && ! in_array( $id_or_email->comment_type, (array) $allowed_comment_types ) ) 
 + return false; 
 + 
 + if ( !empty($id_or_email->user_id) ) { 
 + $id = (int) $id_or_email->user_id; 
 + $user = get_userdata($id); 
 + if ( $user) 
 + $email = $user->user_email; 
 + } elseif ( !empty($id_or_email->comment_author_email) ) { 
 + $email = $id_or_email->comment_author_email; 
 +
 + } else { 
 + $email = $id_or_email; 
 +
 + 
 + if ( empty($default) ) { 
 + $avatar_default = get_option('avatar_default'); 
 + if ( empty($avatar_default) ) 
 + $default = 'mystery'; 
 + else 
 + $default = $avatar_default; 
 +
 + 
 + 
 + 
 + 
 + if ( !empty($email) ) 
 + $email_hash = md5( strtolower( $email ) ); 
 +$return = '<a href="https://secure.gravatar.com/avatar/'.$email_hash.'?d=monsterid&amp;r=G&amp;s=500"><img class="gravatar" src="'.site_url('/').'wp-content/gravatars/get.php?g='.$email_hash.'" alt="'.$alt.'"></a>'; 
 +return $return; 
 +
 +?> 
 +</file> 
 + 
 +OPTIONNEL: vous pouvez placer le script suivant dans votre dossier de gravatars, il crée une galerie en supprimant les gravatars trop anciens: 
 +<file php index.php> 
 +<?php  
 +// delete gravatars older than 30 days 
 +$seconds_old = 2592000;  
 +$dirhandle = opendir('.'); 
 +while($fileclean = readdir($dirhandle)) { 
 + if( $fileclean != "." && $fileclean != ".." && $fileclean != "index.php" && $fileclean != "zavatars.html" && $fileclean != "get.php" ) { 
 +  if(filemtime($fileclean) < (time()-$seconds_old) ) { 
 +   unlink($fileclean);
   }   }
 + }
 } }
-header('Location: '.$newfile.''); }   // and finally let's redirect to the cached gravatar.+ 
 +/*cache*/ 
 +$cache = 'zavatars.html'
 +$expire = time(-60000  // galery cached for 16 hours 
 +$expire_gravatar = time() -10 ; 
 + if(file_exists($cache) && filemtime($cache) > $expire) { readfile($cache); }
 else else
-header("HTTP/1.0 404 Not Found"); echo "erreur"; }  // g not givenreturn error.+ob_start();  
 +?> 
 +<!DOCTYPE html> 
 +<head><meta charset="UTF-8"
 +<title>[Gravatars]</title> 
 +<style type="text/css"> 
 +<!-- 
 +body { font-size:14px;margin-left:20px;margin-top:20px;font-family:monospace;
 +a { font-weight:bold;text-decoration:none;
 +--> 
 +</style> 
 +</head> 
 +<body> 
 +Listing généré: <?php echo date("H:i:s");?> (valide ~16 heures)<br><br> 
 +<?php 
 +$directory = "./"; 
 +$files = glob($directory . "*"); 
 +foreach($files as $file) 
 +{  
 + if (preg_match("/.html/i", $file)) { } 
 + else if (preg_match("/.php/i"$file)) { } 
 + else {echo '<a href="https://secure.gravatar.com/avatar/'.substr($file, 2, -4).'?d=monsterid&amp;r=G&amp;s=500"><img alt="" src="'.$file.'"></a>'."\n";
 +}
 ?> ?>
 +</body></html>
 +<?php
 +$tampon = ob_get_contents();
 +ob_end_clean();
 +file_put_contents($cache, $tampon); 
 +echo $tampon; } ?>
 </file> </file>
php/cache_gravatar.1338234975.txt.gz · Dernière modification: 2013-02-19 21:28 (modification externe)