1 <?php # vim:ts=2:sw=2:et:
2 /* For licensing and copyright terms, see the file named LICENSE */
4 require_once 'MTrackWeb.php';
6 class MTrackWeb_Avatar extends MTrackWeb
8 var $cache_duration = 3600;
11 return parent::getAuth();
16 if (empty($_GET['u']) || empty($_GET['s'])) {
21 $u = DB_DataObject::factory('userinfo');
22 $u->get('userid', $_GET['u']);
24 $this->cachedir = ini_get('session.save_path').'/mtrack_cache';
25 if (!file_exists($this->cachedir)) {
26 mkdir($this->cachedir, 0700,true);
29 if (empty($u->avatar) && !empty($u->email))
31 $u->avatar ="http://www.gravatar.com/avatar/" .
32 md5(strtolower($u->email)) . "?s=$size&d=wavatar";
37 $hint = basename($u->avatar );
38 list($tosend) = $this->cache_get_url_and_operate( $u->avatar);
41 header("Content-Type: image/png");
42 header("Content-Disposition: inline; filename=\"$hint\"");
48 $cache = dirname(__FILE__) . "/images/default_avatar.png";
49 header('Content-Type:image/png');
50 header("Content-Disposition: inline");
54 // removed favatar option....
57 * Fetches the contents of the URL $source using a cache.
58 * Optionally runs a callback specified by $funcname on the
59 * data while it is under a lock (to ensure a consistent view).
60 * $funcname is passed the local cache filename as its first parameter.
61 * Any additional parameters passed to this function will be passed
62 * to $funcname as parameters after the cache filename.
65 * 0 => data from the url
66 * 1 => return value of optional funcname
69 function cache_get_url_and_operate($source, $funcname = null /* args */)
73 $args = func_get_args();
74 if (count($args) > 2) {
80 $cache = $this->cachedir . "/" . md5($source);
81 array_unshift($args, $cache);
83 // cache file population, avoiding thundering herd and maintaining
84 // consistency under concurrency.
90 while ($tries-- > 0) {
91 //logit("tries=$tries");
92 // Can we open the file for read?
93 $fp = @fopen($cache, 'r+b');
95 $fp = @fopen($cache, 'x+');
98 // Yes; get a lock for consistency
100 // logit("got shared lock");
101 // What do we need to do?
103 if ($st['size'] == 0) {
104 // No data in the file, let's see if we can do something about that
105 //logit("zero size; getting ex lock");
108 if ($st['size'] == 0) {
110 //logit("zero sized; we're fixing it, reading from $source");
111 $tosend = file_get_contents($source);
112 fwrite($fp, $tosend);
114 //if ($funcname !== null) {
115 // $dat = call_user_func_array($funcname, $args);
119 // Someone else fixed it
120 //logit("Someone else fixed it, size is now $st[size]");
121 } else if (time() - $st['mtime'] > $this->cache_duration) {
122 // Someone needs to re-fetch the data
123 //logit("Past cache period, getting ex lock");
126 if (time() - $st['mtime'] > $this->cache_duration) {
128 //logit("cache expired; reading from $source, truncating");
131 $tosend = file_get_contents($source);
132 //logit("read " . strlen($tosend) . " from $source");
133 $x = fwrite($fp, $tosend);
134 //logit("wrote $x to local cache file");
135 //if ($funcname !== null) {
136 // $dat = call_user_func_array($funcname, $args);
140 // Someone else fixed it
141 //logit("Someone fixed it, mtime now $st[mtime]");
143 // Good to read through
144 //logit("Reading through cache");
145 $tosend = stream_get_contents($fp);
146 //if ($funcname !== null) {
147 // $dat = call_user_func_array($funcname, $args);
151 //logit("Couldn't get data, sleeping and retrying");
158 return array($tosend, $dat);