Outils pour utilisateurs

Outils du site


php:cache_gravatar

Ceci est une ancienne révision du document !


Cache local de Gravatars

Code PHP pour faire un cache de Gravatar. Utilité ? Éviter que Gravatar puisse tracer les internautes, que le site soit bousillé si Gravatar est injoignable, … Les commentaires sont en anglais.

get.php
<?php 
/*
How to use it:
call it from your website like this:
http://www.yoursite.com/some_folder_for_gravatar_caching/get.php?g={md5_from_email}
*/
$expire = time() -604800 ;  // default: 604800 (7 days)
if (isset($_GET['g']))  // g given ? if yes...
{
if (strlen($_GET['g']) !== 32) { die; }  // g is 32 character long ? if no, die.
$hash = ereg_replace("[^a-f0-9]", "", $_GET['g'] );  // strip out anything that doesn't belong in a md5 hash
if (strlen($hash) != 32) { die; }  // still 32 characters ? if no, given hash wasn't genuine. die.
$newfile = $hash.'.png';
$file = 'https://secure.gravatar.com/avatar/'.$hash.'?s=60&d=monsterid';  // gravatar, 60px monsterid.
if(file_exists($newfile) && filemtime($newfile) < $expire)
{ unlink($newfile); }  // expired gravatar, out !
if (file_exists($newfile))
{ }  // 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 ($imagecheck['mime']!=='image/png')  // is it a PNG ?
  {   
imagepng(imagecreatefromjpeg($newfile),$newfile.'2');  // if no, creating PNG and replacing
unlink($newfile);
rename($newfile.'2', $newfile);
  }
}
header('Location: '.$newfile.''); }   // and finally let's redirect to the cached gravatar.
else
{ header("HTTP/1.0 404 Not Found"); echo "erreur"; }  // g not given, return error.
?>

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):

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;
}
?>

OPTIONNEL: vous pouvez placer le script suivant dans votre dossier de gravatars, il crée une galerie en supprimant les gravatars trop anciens:

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);
  }
 }
}
 
/*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
{ 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; } ?>
php/cache_gravatar.1425733321.txt.gz · Dernière modification : 2015-03-07 14:02 (modification externe)