| 1 |
<?php |
|---|
| 2 |
|
|---|
| 3 |
|
|---|
| 4 |
|
|---|
| 5 |
|
|---|
| 6 |
|
|---|
| 7 |
|
|---|
| 8 |
|
|---|
| 9 |
|
|---|
| 10 |
|
|---|
| 11 |
|
|---|
| 12 |
|
|---|
| 13 |
|
|---|
| 14 |
|
|---|
| 15 |
|
|---|
| 16 |
|
|---|
| 17 |
|
|---|
| 18 |
class GravatarApi |
|---|
| 19 |
{ |
|---|
| 20 |
|
|---|
| 21 |
protected $default_image; |
|---|
| 22 |
|
|---|
| 23 |
|
|---|
| 24 |
protected $expire_ago; |
|---|
| 25 |
|
|---|
| 26 |
protected $image_size, $rating, $cache_dir; |
|---|
| 27 |
protected $cache_dir_name; |
|---|
| 28 |
|
|---|
| 29 |
protected $base_url = "http://www.gravatar.com"; |
|---|
| 30 |
|
|---|
| 31 |
protected $base_ratings = array('G', 'PG', 'R', 'X'); |
|---|
| 32 |
|
|---|
| 33 |
public function __construct($image_size = null, $rating = null) |
|---|
| 34 |
{ |
|---|
| 35 |
$this->cache_dir = sfConfig::get('sf_upload_dir').DIRECTORY_SEPARATOR |
|---|
| 36 |
.sfConfig::get('app_gravatar_cache_dir_name', 'g_cache').DIRECTORY_SEPARATOR; |
|---|
| 37 |
$this->cache_dir_name = str_replace(sfConfig::get('sf_web_dir'), '', $this->cache_dir); |
|---|
| 38 |
if (!is_dir($this->cache_dir)) |
|---|
| 39 |
{ |
|---|
| 40 |
if (false === @mkdir($this->cache_dir, 0777, true)) |
|---|
| 41 |
{ |
|---|
| 42 |
throw new sfCacheException(sprintf('Failed to make cache directory "%s" while generating gravatar image.', dirname($this->cache_dir))); |
|---|
| 43 |
} |
|---|
| 44 |
} |
|---|
| 45 |
|
|---|
| 46 |
$this->default_image = sfConfig::get('app_gravatar_default_image', 'gravatar_default.png'); |
|---|
| 47 |
$this->expire_ago = sfConfig::get('app_gravatar_cache_expiration', '3 days'); |
|---|
| 48 |
|
|---|
| 49 |
if (is_null($image_size) || $image_size > 80 || $image_size < 1) |
|---|
| 50 |
{ |
|---|
| 51 |
$this->image_size = sfConfig::get('app_gravatar_default_size', 80); |
|---|
| 52 |
} |
|---|
| 53 |
else |
|---|
| 54 |
{ |
|---|
| 55 |
$this->image_size = $image_size; |
|---|
| 56 |
} |
|---|
| 57 |
|
|---|
| 58 |
if (is_null($rating) || !in_array($rating, $this->base_ratings)) |
|---|
| 59 |
{ |
|---|
| 60 |
$this->rating = sfConfig::get('app_gravatar_default_rating', 'G'); |
|---|
| 61 |
} |
|---|
| 62 |
else |
|---|
| 63 |
{ |
|---|
| 64 |
$this->rating = $rating; |
|---|
| 65 |
} |
|---|
| 66 |
} |
|---|
| 67 |
|
|---|
| 68 |
|
|---|
| 69 |
|
|---|
| 70 |
* constructs path to gravatar (with size, rating, md5 email and a default image to redirect to (if not found)) |
|---|
| 71 |
* |
|---|
| 72 |
* @return String |
|---|
| 73 |
* @author Mickael Kurmann |
|---|
| 74 |
**/ |
|---|
| 75 |
protected function buildGravatarPath($md5_email) |
|---|
| 76 |
{ |
|---|
| 77 |
return $this->base_url.'/avatar.php?gravatar_id='.$md5_email. |
|---|
| 78 |
'&size='.$this->image_size. |
|---|
| 79 |
'&rating='.$this->rating. |
|---|
| 80 |
'&default=http://www.default.com'; |
|---|
| 81 |
} |
|---|
| 82 |
|
|---|
| 83 |
|
|---|
| 84 |
* Check if a gravatar is avaible on gravatar.com |
|---|
| 85 |
* |
|---|
| 86 |
* @return boolean |
|---|
| 87 |
* @author Mickael Kurmann |
|---|
| 88 |
**/ |
|---|
| 89 |
public function hasGravatar($md5_email) |
|---|
| 90 |
{ |
|---|
| 91 |
|
|---|
| 92 |
$ch = curl_init($this->buildGravatarPath($md5_email)); |
|---|
| 93 |
curl_setopt($ch, CURLOPT_TIMEOUT, 30); |
|---|
| 94 |
|
|---|
| 95 |
|
|---|
| 96 |
ob_start(); |
|---|
| 97 |
curl_exec($ch); |
|---|
| 98 |
ob_end_clean(); |
|---|
| 99 |
|
|---|
| 100 |
|
|---|
| 101 |
$session_code = curl_getinfo($ch, CURLINFO_HTTP_CODE); |
|---|
| 102 |
curl_close($ch); |
|---|
| 103 |
|
|---|
| 104 |
|
|---|
| 105 |
if ($session_code == 200) |
|---|
| 106 |
{ |
|---|
| 107 |
return true; |
|---|
| 108 |
} |
|---|
| 109 |
|
|---|
| 110 |
return false; |
|---|
| 111 |
} |
|---|
| 112 |
|
|---|
| 113 |
|
|---|
| 114 |
* check for a cache hit - if found check if file is within expiry time |
|---|
| 115 |
* |
|---|
| 116 |
* @return void |
|---|
| 117 |
* @author Mickael Kurmann |
|---|
| 118 |
**/ |
|---|
| 119 |
protected function isCacheValid($file_path) |
|---|
| 120 |
{ |
|---|
| 121 |
if (file_exists($file_path)) |
|---|
| 122 |
{ |
|---|
| 123 |
if (filectime($file_path) < strtotime("+".$this->expire_ago)) |
|---|
| 124 |
{ |
|---|
| 125 |
|
|---|
| 126 |
return true; |
|---|
| 127 |
} |
|---|
| 128 |
else |
|---|
| 129 |
{ |
|---|
| 130 |
|
|---|
| 131 |
unlink($file_path); |
|---|
| 132 |
} |
|---|
| 133 |
} |
|---|
| 134 |
|
|---|
| 135 |
|
|---|
| 136 |
return false; |
|---|
| 137 |
} |
|---|
| 138 |
|
|---|
| 139 |
|
|---|
| 140 |
// already exist (or has expired) |
|---|
| 141 |
public function getGravatar($email) |
|---|
| 142 |
{ |
|---|
| 143 |
$md5_email = md5($email); |
|---|
| 144 |
$file = $this->cache_dir.$md5_email.'.png'; |
|---|
| 145 |
|
|---|
| 146 |
|
|---|
| 147 |
$to_return = $md5_email; |
|---|
| 148 |
|
|---|
| 149 |
|
|---|
| 150 |
if (!$this->isCacheValid($file)) |
|---|
| 151 |
{ |
|---|
| 152 |
|
|---|
| 153 |
if ($this->hasGravatar($md5_email)) |
|---|
| 154 |
{ |
|---|
| 155 |
$path = $this->buildGravatarPath($md5_email); |
|---|
| 156 |
} |
|---|
| 157 |
else |
|---|
| 158 |
{ |
|---|
| 159 |
|
|---|
| 160 |
$path = realpath(dirname(__FILE__).DIRECTORY_SEPARATOR.'..'.DIRECTORY_SEPARATOR.$this->default_image); |
|---|
| 161 |
} |
|---|
| 162 |
|
|---|
| 163 |
$new_file = fopen($file, 'w+b'); |
|---|
| 164 |
$gravatar_img = file_get_contents($path, 'rb'); |
|---|
| 165 |
|
|---|
| 166 |
fwrite($new_file, $gravatar_img); |
|---|
| 167 |
} |
|---|
| 168 |
|
|---|
| 169 |
return str_replace(DIRECTORY_SEPARATOR, '/', $this->cache_dir_name).$to_return; |
|---|
| 170 |
} |
|---|
| 171 |
} |
|---|