Development

/plugins/vjCommentPlugin/TRUNK/lib/tools/GravatarApi.class.php

You must first sign up to be able to contribute.

root/plugins/vjCommentPlugin/TRUNK/lib/tools/GravatarApi.class.php

Revision 28879, 4.8 kB (checked in by jp_morvan, 3 years ago)

initial import

  • Property svn:executable set to
Line 
1 <?php
2 /**
3  * This API Enable auto caching gravatar image
4  * It is inspired by these two scripts :
5  * - http://fucoder.com/code/gravatar-cache/
6  * - http://svn.hiddenloop.com/public/plugins/mephisto_gravatar_cache/
7  *
8  * TODO :
9  *  - automatically remove cached gravatar through a cron OR with phptask
10  *  - add unit tests
11  *
12  * @package  Symfony Plugin
13  * @author   Mickael Kurmann
14  * @author   Xavier Lacot <xavier@lacot.org>
15  * @see      http://www.symfony-project.com/trac/wiki/sfPropelActAsCommentableBehaviorPlugin
16  * @license  MIT
17  **/
18 class GravatarApi
19 {
20   // default cached gravatar img
21   protected $default_image;
22
23   // possible to put : 3 days, 1 week, and whatever you want according to php strtotime function
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   // gravatar ratings are only : G | PG | R | X
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     // TODO try cache !
92     $ch = curl_init($this->buildGravatarPath($md5_email));
93     curl_setopt($ch, CURLOPT_TIMEOUT, 30);
94
95     //--- Start buffering : HIDE CURL EXEC RETURN ...
96     ob_start();
97     curl_exec($ch);
98     ob_end_clean();
99     //--- End buffering and clean output
100
101     $session_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
102     curl_close($ch);
103
104     // 200 == page with no error, else 301 == redirect (no gravatar) or 404... or whatever
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         // file exists and cache is valid
126         return true;
127       }
128       else
129       {
130         // file exists but cache has expired
131         unlink($file_path);
132       }
133     }
134
135     // no file
136     return false;
137   }
138
139   // get the gravatar to the cache, if email has a gravatar and it does not
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     // the cache is valid, return the cached image
147     $to_return = $md5_email;
148
149     // check the cache
150     if (!$this->isCacheValid($file))
151     {
152       // no image in cache
153       if ($this->hasGravatar($md5_email))
154       {
155         $path = $this->buildGravatarPath($md5_email);
156       }
157       else
158       {
159         // no gravatar --> get the default one
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       // image on gravatar.com --> save it in cache
166       fwrite($new_file, $gravatar_img);
167     }
168
169     return str_replace(DIRECTORY_SEPARATOR, '/', $this->cache_dir_name).$to_return;
170   }
171 }
Note: See TracBrowser for help on using the browser.