<?php
/*
 * Google Favicon Proxy
 * By O. Lissenberg (http://lissenberg.org)
 * licensed under a Creative Commons License Attribution-ShareAlike 2.5
 * http://creativecommons.org/licenses/by-sa/2.5/
 *
 * july 9, 2009
 *
 * Table structure for table `portal_favicons`
 *
    CREATE TABLE `portal_favicons` (
      `host` varchar(512) COLLATE latin1_general_ci NOT NULL,
      `favicon` blob NOT NULL
    ) ENGINE=MyISAM DEFAULT CHARSET=latin1 COLLATE=latin1_general_ci;
 * 
 */

require_once 'Zend/Db.php';
require_once 'Portal/Connect.php';

if (isset($_GET["domain"])) {
$favurl = 'http://www.google.com/s2/favicons?domain='.$_GET["domain"];

$sql = "SELECT * FROM portal_favicons WHERE host = ?";
$result = $db->fetchAll($sql, $_GET["domain"]);

$favicon = null;
if ($result) {
    // From DB
    $favicon = $result[0]["favicon"];
} else {
    // From Google S2
    $favicon = file_get_contents($favurl);
    $data = array(
        'host' => $_GET["domain"],
        'favicon' => $favicon );
    $db->insert('portal_favicons', $data);
}
header("Content-Type: image/png");
echo $favicon;
}
?>