Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Libravatar2 #102

Merged
merged 2 commits into from
Nov 15, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 41 additions & 17 deletions admin/cache/get.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,28 @@
#
# *** LICENSE ***

function download_avatar($avatar_url, $newfile)
{
$curl_handle = curl_init();
curl_setopt($curl_handle, CURLOPT_URL, $avatar_url);
curl_setopt($curl_handle, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl_handle, CURLOPT_TIMEOUT, 5);
$file_content = curl_exec($curl_handle);
curl_close($curl_handle);

$fp = fopen($newfile, 'w+');
$ch = curl_init($avatar_url);
curl_setopt($ch, CURLOPT_FILE, $fp);
curl_setopt($ch, CURLOPT_ENCODING, 'gzip');
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 5);
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0');
curl_exec($ch);

curl_close($ch);
fclose($fp);
}

if (!isset($_GET['w'], $_GET['q'])) {
header("HTTP/1.0 400 Bad Request");
exit;
Expand All @@ -35,9 +57,9 @@
$target_file = $target_dir.'/'.md5($domain).'.png';
// expiration delay
$expire = time() -60*60*24*7*365 ; // default: 1 year
} elseif ($_GET['w'] == 'gravatar') {
} elseif ($_GET['w'] == 'avatar') {
// target dir
$target_dir = 'gravatar';
$target_dir = 'avatars';
// source file
if (strlen($_GET['q']) !== 32) {
header("HTTP/1.0 400 Bad Request");
Expand All @@ -51,7 +73,8 @@
$target_file = $hash.'.png';
$s = (isset($_GET['s']) and is_numeric($_GET['s'])) ? htmlspecialchars($_GET['s']) : 48; // try to get size
$d = (isset($_GET['d'])) ? htmlspecialchars($_GET['d']) : 'monsterid'; // try to get substitute image
$source_file = 'http://www.gravatar.com/avatar/'.$hash.'?s='.$s.'&d='.$d;
// First try with libravatar
$source_file = 'http://cdn.libravatar.org/avatar/'.$hash.'?s='.$s.'&d='.$d;
// dest file
$target_file = $target_dir.'/'.md5($hash).'.png';
// expiration delay
Expand All @@ -77,29 +100,30 @@

// no cached file or expired
if (!is_file($target_file) or $force_new === true) {
if (!is_dir($target_dir)) {
mkdir($target_dir);
}

// request
$curl_handle = curl_init();
curl_setopt($curl_handle, CURLOPT_URL, $source_file);
curl_setopt($curl_handle, CURLOPT_RETURNTRANSFER, 1);
$file_content = curl_exec($curl_handle);
curl_close($curl_handle);
if ($file_content == null) { // impossible request
download_avatar($source_file, $target_file);

if (!file_exists($target_file)) {
// try with gravatar
$source_file = 'http://www.gravatar.com/avatar/'.$hash.'?s='.$s.'&d='.$d;
$success = download_avatar($source_file, $target_file);
}
if (!file_exists($target_file)) {
// impossible request
header("HTTP/1.0 404 Not Found");
die('404');
exit;
}
// new request is a succes, delete old and save new file
if ($force_new === true) {
unlink($target_file);
}
if (!is_dir($target_dir)) {
mkdir($target_dir);
}
file_put_contents($target_file, $file_content);

// testing format
$imagecheck = getimagesize($target_file);
if ($imagecheck['mime'] !== 'image/png') {
imagepng(imagecreatefromjpeg($target_file), $target_file.'2'); // if not, creating PNG and replacing
unlink($target_file);
rename($target_file.'2', $target_file);
}
}
Expand Down
2 changes: 1 addition & 1 deletion admin/commentaires.php
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ function afficher_commentaire($comment, $with_link)
echo '<div class="commentbloc'.(!$comment['bt_statut'] ? ' privatebloc' : '').'" id="'.article_anchor($comment['bt_id']).'">'."\n";
echo '<div class="comm-side-icon">'."\n";
echo "\t".'<div class="comm-title">'."\n";
echo "\t\t".'<img class="author-icon" src="cache/get.php?w=gravatar&amp;q='.md5((!empty($comment['bt_email']) ? $comment['bt_email'] : $comment['bt_author'] )).'&amp;s=48&amp;d=monsterid"/>'."\n";
echo "\t\t".'<img class="author-icon" src="cache/get.php?w=avatar&amp;q='.md5((!empty($comment['bt_email']) ? $comment['bt_email'] : $comment['bt_author'] )).'&amp;s=48&amp;d=monsterid"/>'."\n";
echo "\t\t".'<span class="date">'.date_formate($comment['bt_id']).'<span>'.heure_formate($comment['bt_id']).'</span></span>'."\n" ;

echo "\t\t".'<span class="reply" onclick="reply(\'[b]@['.str_replace('\'', '\\\'', $comment['bt_author']).'|#'.article_anchor($comment['bt_id']).'] :[/b] \'); ">Reply</span> ';
Expand Down
53 changes: 38 additions & 15 deletions themes/default/gravatars/get.php → themes/default/avatars/get.php
Original file line number Diff line number Diff line change
@@ -1,11 +1,34 @@
<?php
/*
Local-gravatar cacher
http://www.yoursite.com/gravatar_caching_folder/get.php?g={md5_from_email}&s={size}&d={substitute}
returns icon and saves is localy
Local-gravatar cacher
http://www.yoursite.com/gravatar_caching_folder/get.php?g={md5_from_email}&s={size}&d={substitute}
returns icon and saves is localy
*/

$expire = time() -60*60*24*30 ; // default: 30 days

function download_avatar($avatar_url, $newfile)
{
$curl_handle = curl_init();
curl_setopt($curl_handle, CURLOPT_URL, $avatar_url);
curl_setopt($curl_handle, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl_handle, CURLOPT_TIMEOUT, 5);
$file_content = curl_exec($curl_handle);
curl_close($curl_handle);

$fp = fopen($newfile, 'w+');
$ch = curl_init($avatar_url);
curl_setopt($ch, CURLOPT_FILE, $fp);
curl_setopt($ch, CURLOPT_ENCODING, 'gzip');
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 5);
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0');
curl_exec($ch);

curl_close($ch);
fclose($fp);
}

// g given ? if yes...
if (isset($_GET['g'])) {
if (strlen($_GET['g']) !== 32) {
Expand All @@ -27,25 +50,25 @@
$s = (isset($_GET['s']) and is_numeric($_GET['s'])) ? htmlspecialchars($_GET['s']) : 48;
// try to get substitute image (d param)
$d = (isset($_GET['d'])) ? htmlspecialchars($_GET['d']) : 'monsterid';
$gravatar_url = 'http://www.gravatar.com/avatar/'.$hash.'?s='.$s.'&d='.$d;
// request
$curl_handle = curl_init();
// First try with libravatar
$avatar_url = 'http://cdn.libravatar.org/avatar/'.$hash.'?s='.$s.'&d='.$d;

curl_setopt($curl_handle, CURLOPT_ENCODING, 'gzip');
curl_setopt($curl_handle, CURLOPT_URL, $gravatar_url);
curl_setopt($curl_handle, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl_handle, CURLOPT_TIMEOUT, 5);
$file_content = curl_exec($curl_handle);
curl_close($curl_handle);
// request
download_avatar($avatar_url, $newfile);

if ($file_content == null) { // impossible request
if (!file_exists($newfile)) {
// try with gravatar
$gravatar_url = 'http://www.gravatar.com/avatar/'.$hash.'?s='.$s.'&d='.$d;
$success = download_avatar($avatar_url, $newfile);
}

if (!file_exists($newfile)) {
// impossible request
header("HTTP/1.0 404 Not Found");
die('404');
exit;
}

// saving
file_put_contents($newfile, $file_content);

$imagecheck = getimagesize($newfile);
if ($imagecheck['mime']!=='image/png') { // is it a PNG ?
Expand Down
2 changes: 1 addition & 1 deletion themes/default/template/commentaire.html
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<article class="comment" id="{commentaire_ancre}">
<div class="comm-icon-side">
<img class="com-gravatar" src="{style}/gravatars/get.php?g={commentaire_md5email}&amp;s=48&amp;d=monsterid" class="gravatar-icon" alt="gravatar" />
<img class="com-gravatar" src="{style}/avatars/get.php?g={commentaire_md5email}&amp;s=48&amp;d=monsterid" class="gravatar-icon" alt="avatar" />
<time class="com-date-time" datetime="{commentaire_date_iso}">{commentaire_date}</time>
</div>
<div class="comm-main-frame">
Expand Down
2 changes: 1 addition & 1 deletion themes/default/template/post.html
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ <h1 class="entry-title" itemprop="name"><a href="{article_lien}" itemprop="url">
{BOUCLE_commentaires}
<article class="comment" id="{commentaire_ancre}">
<div class="comm-icon-side">
<img class="com-gravatar" src="{style}/gravatars/get.php?g={commentaire_md5email}&amp;s=48&amp;d=monsterid" class="gravatar-icon" alt="gravatar" />
<img class="com-gravatar" src="{style}/avatars/get.php?g={commentaire_md5email}&amp;s=48&amp;d=monsterid" class="gravatar-icon" alt="avatar" />
<time class="com-date-time" datetime="{commentaire_date_iso}">{commentaire_date}</time>
</div>
<div class="comm-main-frame">
Expand Down