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 29136, 5.5 kB (checked in by jp_morvan, 6 years ago)

add option to define gravatar's directories (by default :
sf_web_dir/sf_upload_dir/g_cache)

  • 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->setDirectories();
36     if (!is_dir($this->cache_dir))
37     {
38       if (false === @mkdir($this->cache_dir, 0777, true))
39       {
40         throw new sfCacheException(sprintf('Failed to make cache directory "%s" while generating gravatar image.', dirname($this->cache_dir)));
41       }
42     }
43
44     $this->default_image = sfConfig::get('app_gravatar_default_image', 'gravatar_default.png');
45     $this->expire_ago = sfConfig::get('app_gravatar_cache_expiration', '3 days');
46
47     if (is_null($image_size) || $image_size > 80 || $image_size < 1)
48     {
49       $this->image_size = sfConfig::get('app_gravatar_default_size', 80);
50     }
51     else
52     {
53       $this->image_size = $image_size;
54     }
55
56     if (is_null($rating) || !in_array($rating, $this->base_ratings))
57     {
58       $this->rating = sfConfig::get('app_gravatar_default_rating', 'G');
59     }
60     else
61     {
62       $this->rating = $rating;
63     }
64   }
65
66
67   /**
68    * constructs path to gravatar (with size, rating, md5 email and a default image to redirect to (if not found))
69    *
70    * @return String
71    * @author Mickael Kurmann
72    **/
73   protected function buildGravatarPath($md5_email)
74   {
75     return $this->base_url.'/avatar.php?gravatar_id='.$md5_email.
76                            '&size='.$this->image_size.
77                            '&rating='.$this->rating.
78                            '&default=http://www.default.com';
79   }
80
81   /**
82    * Check if a gravatar is avaible on gravatar.com
83    *
84    * @return boolean
85    * @author Mickael Kurmann
86    **/
87   public function hasGravatar($md5_email)
88   {
89     // TODO try cache !
90     $ch = curl_init($this->buildGravatarPath($md5_email));
91     curl_setopt($ch, CURLOPT_TIMEOUT, 30);
92
93     //--- Start buffering : HIDE CURL EXEC RETURN ...
94     ob_start();
95     curl_exec($ch);
96     ob_end_clean();
97     //--- End buffering and clean output
98
99     $session_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
100     curl_close($ch);
101
102     // 200 == page with no error, else 301 == redirect (no gravatar) or 404... or whatever
103     if ($session_code == 200)
104     {
105       return true;
106     }
107
108     return false;
109   }
110
111   /**
112    * check for a cache hit - if found check if file is within expiry time
113    *
114    * @return void
115    * @author Mickael Kurmann
116    **/
117   protected function isCacheValid($file_path)
118   {
119     if (file_exists($file_path))
120     {
121       if (filectime($file_path) < strtotime("+".$this->expire_ago))
122       {
123         // file exists and cache is valid
124         return true;
125       }
126       else
127       {
128         // file exists but cache has expired
129         unlink($file_path);
130       }
131     }
132
133     // no file
134     return false;
135   }
136
137   // get the gravatar to the cache, if email has a gravatar and it does not
138   // already exist (or has expired)
139   public function getGravatar($email)
140   {
141     $md5_email = md5($email);
142     $file = $this->cache_dir.$md5_email.'.png';
143
144     // the cache is valid, return the cached image
145     $to_return = $md5_email;
146
147     // check the cache
148     if (!$this->isCacheValid($file))
149     {
150       // no image in cache
151       if ($this->hasGravatar($md5_email))
152       {
153         $path = $this->buildGravatarPath($md5_email);
154       }
155       else
156       {
157         // no gravatar --> get the default one
158         $path = realpath(dirname(__FILE__).DIRECTORY_SEPARATOR.'..'.DIRECTORY_SEPARATOR.$this->default_image);
159       }
160
161       $new_file = fopen($file, 'w+b');
162       $gravatar_img = file_get_contents($path, 'rb');
163       // image on gravatar.com --> save it in cache
164       fwrite($new_file, $gravatar_img);
165     }
166
167     return str_replace(DIRECTORY_SEPARATOR, '/', $this->cache_dir_name).$to_return;
168   }
169
170   protected function setDirectories()
171   {
172     $gravatar_cache_dir_name = sfConfig::get('app_gravatar_cache_dir_name', 'g_cache');
173     
174     $this->cache_dir = $this->clearPath(sfConfig::get('sf_web_dir')
175                         .DIRECTORY_SEPARATOR
176                         .$this->getGravatarUploadDir()
177                         .DIRECTORY_SEPARATOR
178                         .$gravatar_cache_dir_name
179                         .DIRECTORY_SEPARATOR);
180     $this->cache_dir_name = str_replace(sfConfig::get('sf_web_dir'), '', $this->cache_dir);
181   }
182
183   protected function hasGravatarUploadDir()
184   {
185     return !is_null(sfConfig::get('app_gravatar_upload_dir'));
186   }
187
188   protected function getGravatarUploadDir()
189   {
190     return $this->clearPath(sfConfig::get('app_gravatar_upload_dir', str_replace(sfConfig::get('sf_web_dir'), '', sfConfig::get('sf_upload_dir'))));
191   }
192
193   protected function clearPath($path)
194   {
195     return str_replace("//", "/", $path);
196   }
197 }
Note: See TracBrowser for help on using the browser.