Skip to content

Commit

Permalink
本地化gravatar头像请求,妈妈再也不用担心看不到我的头像了~
Browse files Browse the repository at this point in the history
+ 前提:需要服务器在国外
  • Loading branch information
movsb committed May 18, 2015
1 parent eae0f5c commit c2c2167
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 2 deletions.
31 changes: 31 additions & 0 deletions theme/avatar.php
@@ -0,0 +1,31 @@
<?php

$path = urldecode($_SERVER['QUERY_STRING']);
$url = 'http://www.gravatar.com/avatar/'.$path;

$headers = [];
if(isset($_SERVER['HTTP_IF_MODIFIED_SINCE']))
$headers[] = 'If-Modified-Since: '.$_SERVER['HTTP_IF_MODIFIED_SINCE'];
if(isset($_SERVER['HTTP_IF_NONE_MATCH']))
$headers[] = 'If-None-Match: '.$_SERVER['HTTP_IF_NONE_MATCH'];

$ch = curl_init($url);
if(count($headers)) {
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$headers = [];
}

curl_setopt($ch, CURLOPT_HEADERFUNCTION,
function($handle, $line) use (&$headers) {
$headers[] = substr($line, 0, -2);
if(strlen($line) === 2) {
for($i=0,$c=count($headers)-1; $i<$c; $i++) {
header($headers[$i]);
}
}
return strlen($line);
});

curl_exec($ch);
curl_close($ch);

3 changes: 1 addition & 2 deletions theme/scripts/comment.js
Expand Up @@ -157,8 +157,7 @@ function sanitize_content(c) {
}

function comment_avatar(eh,sz) {
var host = 'https://secure.gravatar.com/avatar/';
return host + eh + '?s=' + sz;
return '/theme/avatar.php?' + encodeURIComponent(eh + '?s=' + sz);
}

function comment_friendly_date(d) {
Expand Down

0 comments on commit c2c2167

Please sign in to comment.