File: faviconcache.inc.php

package info (click to toggle)
sitebar 3.3.8-7etch1
  • links: PTS
  • area: main
  • in suites: etch
  • size: 4,168 kB
  • ctags: 3,234
  • sloc: php: 52,374; sql: 570; sh: 331; makefile: 35
file content (363 lines) | stat: -rw-r--r-- 10,958 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
<?php
/******************************************************************************
 *  SiteBar 3 - The Bookmark Server for Personal and Team Use.                *
 *  Copyright (C) 2004  Gunnar Wrobel <sitebar@gunnarwrobel.de>               *
 *  Copyright (C) 2004-2006  Ondrej Brablc <http://brablc.com/mailto?o>       *
 *                                                                            *
 *  This program is free software; you can redistribute it and/or modify      *
 *  it under the terms of the GNU General Public License as published by      *
 *  the Free Software Foundation; either version 2 of the License, or         *
 *  (at your option) any later version.                                       *
 *                                                                            *
 *  This program is distributed in the hope that it will be useful,           *
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of            *
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the             *
 *  GNU General Public License for more details.                              *
 *                                                                            *
 *  You should have received a copy of the GNU General Public License         *
 *  along with this program; if not, write to the Free Software               *
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA *
 ******************************************************************************/

require_once('./inc/database.inc.php');
require_once('./inc/pageparser.inc.php');
require_once('./inc/usermanager.inc.php');
require_once('./inc/errorhandler.inc.php');

class SB_FaviconCache extends SB_ErrorHandler
{
    // The class needs the db as storage place
    var $db;
    // And the usermanager for user related functions
    var $um;

    function SB_FaviconCache()
    {
        $this->db =& SB_Database::staticInstance();
        $this->um =& SB_UserManager::staticInstance();
    }

    function & staticInstance()
    {
        static $cache;

        if (!$cache)
        {
            $cache = new SB_FaviconCache();
        }

        return $cache;
    }

    function purgeUsingCursor(&$res)
    {
        $count = 0;

        $records = $this->db->fetchRecords($res);

        foreach ($records as $rec)
        {
            // Delete it from cache anyway
            $this->db->delete('sitebar_cache', array
            (
                'type'=>'favicon',
                '^1'=>'AND',
                'ckey'=> $rec['ckey']
            ));

            if ($this->db->getAffectedRows())
            {
                $count++;
            }
        }

        return $count;
    }

    function hasSpace()
    {
        // Check how many icons we have
        $res = $this->db->select('count(*) count', 'sitebar_cache', "type = 'favicon'");
        $rec = $this->db->fetchRecord($res);

        // We have free place then return
        if ($rec['count']<$this->um->getParam('config','max_icon_cache'))
        {
            return true;
        }

        // NON BINARY: Delete orphans
        $res = $this->db->select
        (
            "ckey",
            "sitebar_cache LEFT OUTER  JOIN sitebar_link ON ( md5(favicon) = ckey )",
            "type='favicon' AND lid IS NULL AND ckey NOT LIKE 'binary:%'"
        );

        $purged = $this->purgeUsingCursor($res);

        // BINARY: Delete orphans
        $res = $this->db->select
        (
            "ckey",
            "sitebar_cache LEFT OUTER  JOIN sitebar_link ON ( favicon = ckey )",
            "type='favicon' AND lid IS NULL AND ckey LIKE 'binary:%'"
        );
        $purged += $this->purgeUsingCursor($res);

        if ($purged)
        {
            return true;
        }

        $maxAge = $this->um->getParam('config','max_icon_age');

        // NON BINARY: We have too many icons in total, purge the old icons
        for ($i=$maxAge; $i>0; $i--)
        {
            $result = $this->db->delete('sitebar_cache',
                "type = 'favicon' AND to_days(now()) - to_days(created) > $i AND ckey NOT LIKE 'binary:%'");

            // If we delete something, then hooray and return
            if ($this->db->getAffectedRows())
            {
                return true;
            }
        }

        $maxAge = $this->um->getParam('config','max_icon_age');

        // BINARY: We have too many icons in total, purge the old icons
        for ($i=$maxAge; $i>0; $i--)
        {
            $result = $this->db->delete('sitebar_cache',
                "type = 'favicon' AND to_days(now()) - to_days(created) > $i AND ckey LIKE 'binary:%'");

            // If we delete something, then hooray and return
            if ($this->db->getAffectedRows())
            {
                return true;
            }
        }

        return false;
    }

    function purge($lid = null)
    {
        if ($lid === null)
        {
            $this->db->delete('sitebar_cache', array
            (
                'type'=>'favicon',
            ));
        }
        else
        {
            // Get the url corresponding to the link id
            $select = $this->db->select( 'favicon', 'sitebar_link', array('lid' => $lid));
            $found = $this->db->fetchRecord($select);

            if (is_array($found) && isset($found['favicon']) && $found['favicon'])
            {
                // Delete it from cache anyway
                $this->db->delete('sitebar_cache', array
                (
                    'type'=>'favicon',
                    '^1'=>'AND',
                    'ckey'=> md5($found['favicon'])
                ));
            }
        }
    }

    function faviconGetAll()
    {
        $select = $this->db->select('ckey', 'sitebar_cache',
            array('type'=>'favicon'), 'length(cvalue) asc, cvalue asc');
        $found  = $this->db->fetchRecords($select);
        return $found;
    }

    function isFaviconCached($favicon)
    {
        $select = $this->db->select('count(*) count', 'sitebar_cache',
            array('^1'=>"type='favicon' AND", 'ckey' => md5($favicon)));
        $found  = $this->db->fetchRecord($select);
        return $found['count'];
    }

    function saveFavicon($favicon, $ico)
    {
        $this->db->insert('sitebar_cache', array
        (
            'type'=>'favicon',
            'ckey' => md5($favicon),
            'cvalue' => $ico,
            'created' => array('now' => ''),
        ), array(1062));
    }

    function saveFaviconBase64($base64)
    {
        if (!$this->hasSpace())
        {
            return '';
        }

        // Create shorter key, will be stored in the link
        $key = 'binary:'.md5($base64);

        $this->db->insert('sitebar_cache', array
        (
            'type'=>'favicon',
            'ckey' => $key,
            'cvalue' => base64_decode($base64),
            'created' => array('now' => ''),
        ),array(1062));

        return $key;
    }

    function faviconGet($favicon_md5, $lid, $refresh=false)
    {
        $maxAge = $this->um->getParam('config','max_icon_age');

        $select = $this->db->select(
            'cvalue, to_days(now()) - to_days(created) as age',
            'sitebar_cache',
            array('^1'=>"type='favicon' AND", 'ckey' => $favicon_md5));

        $found  = $this->db->fetchRecord($select, true);
        $oldIcon = null;

        if (is_array($found))
        {
            $oldIcon = $found['cvalue'];
            if (!$refresh && ($found['age']<=$maxAge || !$lid))
            {
                return $oldIcon;
            }
        }

        // if lid is not specified, the favicon wont be fetched into the cache
        if ($lid)
        {
            $favurl = '';

            // If we are numeric
            if ($lid == "".intval($lid))
            {
                // Get the url corresponding to the link id
                $rset = $this->db->select( null, 'sitebar_link', array('lid' => $lid));

                // Fetch the link properties
                $rlink = $this->db->fetchRecord($rset);

                // No such link? Return empty?
                if (!$rlink || !$rlink['favicon'])
                {
                    return null;
                }

                $favurl = $rlink['favicon'];
            }
            else
            {
                $favurl = $lid;
            }

            $newIcon = '';

            // Retrieve and test icon as binary string
            $page = new SB_PageParser($favurl);
            $errorCode = $page->retrieveFAVICON($newIcon);

            // We have unrecoverable error
            if ($errorCode >= 500)
            {
                return null;
            }

            // We have probably connection problem
            if ($errorCode >= 400)
            {
                if ($oldIcon)
                {
                    $this->db->update('sitebar_cache',
                        array('created' => array('now' => '')),
                        array('^1'=>"type='favicon' AND", 'ckey' => $favicon_md5));
                }
                return $oldIcon;
            }

            if ($oldIcon) // We had a records
            {
                $this->db->update('sitebar_cache',
                    array('cvalue' => $newIcon),
                    array('^1'=>"type='favicon' AND", 'ckey' => $favicon_md5));
            }
            else if ($this->hasSpace()) // New record
            {
                $this->saveFavicon($favurl, $newIcon);
            }

            return $newIcon;
        }
    }

    // For backward compatibility PHP < 4.3.0
    function file_get_contents($filename)
    {
       $fd = fopen($filename, "rb");
       $content = fread($fd, filesize($filename));
       fclose($fd);
       return $content;
    }

    // If lid is not specified, the cache wont be updated
    function faviconReturn($favicon_md5, $lid = null, $refresh=false)
    {
        $ico = $this->faviconGet($favicon_md5, $lid, $refresh);

        // 30 days keep cached in browser
        $age = 60*60*24*30;

        if (!$ico)
        {
            // Sent wrong icon image
            SB_Skin::set($this->um->getParam('user','skin'));
            $ico = $this->file_get_contents(SB_Skin::imgsrc('link_wrong_favicon'));
            $age = 60*60*24; // Try tomorrow
        }

        if ($refresh)
        {
            $age = -1;
        }

        header('Accept-Ranges: bytes');
        header('Cache-Control: max-age='.$age);
        header('Content-Length: '. strlen($ico));

        $type = 'image/x-icon';

        if (substr($ico,0,3)=='GIF')
        {
            $type = 'image/gif';
        }
        else if (substr($ico,6,4)=='JFIF')
        {
            $type = 'image/jpeg';
        }
        else if (substr($ico,1,3)=='PNG')
        {
            $type = 'image/png';
        }

        header('Content-Type: '.$type);

        print $ico;
    }
}
?>