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:
- 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&r=G&s=500"><img class="gravatar" src="http://www.votre.site/wp-content/gravatars/get.php?g='.$email_hash.'" alt="'.$alt.'"></a>'; return $return; } ?>
php/cache_gravatar.1341179010.txt.gz · Dernière modification : 2013-02-19 20:28 (modification externe)