View source
<?php
class DrupalFavicon {
const DEFAULT_URI = 'misc/favicon.ico';
protected $theme;
protected $file;
public function __construct($theme) {
$this->theme = $theme;
if (is_file(conf_path() . '/favicon.ico')) {
$uri = conf_path() . '/favicon.ico';
}
elseif ($favicon = theme_get_setting('favicon', $this->theme)) {
$uri = str_replace($GLOBALS['base_url'] . '/', '', $favicon);
}
elseif ($favicon_path = theme_get_setting('favicon_path', $this->theme)) {
$uri = $favicon_path;
}
else {
$uri = static::DEFAULT_URI;
}
$this->file = static::getFileFromUri($uri);
}
public function getFile() {
return $this->file;
}
public static function getFileFromUri($uri) {
drupal_alter('favicon_file_uri', $uri);
$file = new stdClass();
$file->uri = $uri;
$file->filemime = file_get_mimetype($uri);
$file->filesize = @filesize($uri);
static::validateFile($file);
drupal_alter('favicon_file', $file);
return $file;
}
public static function validateFile($file) {
if (!in_array($file->filemime, array(
'image/vnd.microsoft.icon',
'image/x-icon',
'image/png',
'image/gif',
'image/jpeg',
), TRUE)) {
throw new DrupalFaviconValidationException("The file {$file->uri} has an invalid MIME type of <em>{$file->filemime}</em> for use as a shortcut icon.");
}
}
public static function fetchFile($theme = NULL, $cached = TRUE) {
if ($uri = variable_get('favicon_uri')) {
return static::getFileFromUri($uri);
}
if (!isset($theme)) {
$theme = !empty($GLOBALS['theme_key']) ? $GLOBALS['theme_key'] : '';
}
$cid = FALSE;
if ($cached) {
$cache_data = array(
'theme' => $theme,
'base_url' => $GLOBALS['base_url'],
'conf_path' => conf_path(),
);
drupal_alter('favicon_cache_data', $cache_data);
$cid = 'favicon:' . md5(serialize($cache_data));
}
if ($cached && ($cache = cache_get($cid))) {
return $cache->data;
}
else {
$favicon = new static($theme);
if ($cached) {
cache_set($cid, $favicon
->getFile());
}
return $favicon
->getFile();
}
}
public static function canCacheFile($file) {
return $file->filesize <= variable_get('favicon_page_cache_maximum_size', DRUPAL_KILOBYTE * DRUPAL_KILOBYTE);
}
public static function deliverFileTransfer($file) {
if (is_int($file)) {
drupal_deliver_html_page($file);
return;
}
elseif (!is_object($file) || !is_file($file->uri) || !is_readable($file->uri)) {
drupal_deliver_html_page(MENU_NOT_FOUND);
return;
}
$headers = array(
'Content-Type' => mime_header_encode($file->filemime),
'Content-Disposition' => 'inline',
'Content-Length' => $file->filesize,
);
module_invoke_all('file_transfer', $file->uri, $headers);
foreach ($headers as $name => $value) {
drupal_add_http_header($name, $value);
}
$fd = fopen($file->uri, 'rb');
if ($fd !== FALSE) {
while (!feof($fd)) {
print fread($fd, DRUPAL_KILOBYTE);
}
fclose($fd);
}
else {
watchdog('favicon', 'Unable to open @uri for reading.', array(
'@uri' => $file->uri,
));
drupal_deliver_html_page(MENU_NOT_FOUND);
return;
}
if (static::canCacheFile($file)) {
drupal_page_footer();
}
else {
drupal_exit();
}
}
public static function deliverFileRedirect($file) {
if (is_int($file)) {
drupal_deliver_html_page($file);
return;
}
elseif (!is_object($file)) {
drupal_deliver_html_page(MENU_NOT_FOUND);
return;
}
$file->url = file_create_url($file->uri);
if (module_exists('redirect')) {
$redirect = new stdClass();
$redirect->redirect = $file->url;
redirect_redirect($redirect);
}
else {
drupal_goto($file->url, array(), 301);
}
}
}