/usr/share/horde/trean/lib/Queue/Task/Favicon.php is in php-horde-trean 1.1.4-1build1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 | <?php
class Trean_Queue_Task_Favicon implements Horde_Queue_Task
{
/**
* Url to be crawled
* @var string
*/
protected $_url;
/**
* Bookmark id
* @var integer
*/
protected $_bookmarkId;
/**
* User id
* @var integer
*/
protected $_userId;
/**
* Page body
* @var string
*/
protected $_body;
/**
* Page body character set
* @var string
*/
protected $_charset;
/**
* Constructor
*
* @var string $url URL to crawl
* @var integer $bookmarkId Bookmark id
* @var integer $userId Horde integer user id
* @var string $body If already fetched, the body of $url
* @var string $charset If the body is already fetched, the string charset
*/
public function __construct($url, $bookmarkId, $userId, $body = null, $charset = null)
{
$this->_url = $url;
$this->_bookmarkId = $bookmarkId;
$this->_userId = $userId;
$this->_body = $body;
$this->_charset = $charset;
}
/**
*/
public function run()
{
$injector = $GLOBALS['injector'];
$client = $injector->getInstance('Horde_Http_Client');
if (!$this->_body) {
// Fetch full text of $url
try {
$page = $client->get($this->_url);
$this->_body = $page->getBody();
if ($type = $page->getHeader('Content-Type') &&
preg_match('/.*;\s*charset="?([^" ]*)/', $type, $match)) {
$this->_charset = $match[1];
}
} catch (Horde_Http_Exception $e) {
}
}
$url = parse_url($this->_url);
if ($favicon = $this->_findByRel($client, $url, $this->_body, $this->_charset)) {
$this->_storeFavicon($favicon);
} elseif ($favicon = $this->_findByRoot($client, $url)) {
$this->_storeFavicon($favicon);
} elseif ($favicon = $this->_findByPath($client, $url)) {
$this->_storeFavicon($favicon);
}
}
/**
* @param Horde_Http_Response_Base $response HTTP response; body of this is the favicon
*/
protected function _storeFavicon(Horde_Http_Response_Base $response)
{
global $injector;
$gateway = $injector->getInstance('Trean_Bookmarks');
$bookmark = $gateway->getBookmark($this->_bookmarkId);
if ($bookmark) {
$bookmark->favicon_url = $response->uri;
$bookmark->save(false);
}
// Initialize VFS
$vfs = $GLOBALS['injector']
->getInstance('Horde_Core_Factory_Vfs')
->create();
$vfs->writeData('.horde/trean/favicons/',
md5($bookmark->favicon_url),
$response->getBody(),
true);
}
protected function _findByRel($client, $url, $body, $charset)
{
try {
$dom = new Horde_Domhtml($body, $charset);
foreach ($dom as $node) {
if ($node instanceof DOMElement &&
Horde_String::lower($node->tagName) == 'link' &&
($rel = Horde_String::lower($node->getAttribute('rel'))) &&
($rel == 'shortcut icon' || $rel == 'icon')) {
$favicon = $node->getAttribute('href');
// Make sure $favicon is a full URL.
$favicon_url = parse_url($favicon);
if (empty($favicon_url['scheme'])) {
if (substr($favicon, 0, 1) == '/') {
$favicon = $url['scheme'] . '://' . $url['host'] . $favicon;
} else {
$path = pathinfo($url['path']);
$favicon = $url['scheme'] . '://' . $url['host'] . $path['dirname'] . '/' . $favicon;
}
}
try {
$response = $client->get($favicon);
if ($this->_isValidFavicon($response)) {
return $response;
}
} catch (Horde_Http_Exception $e) {
}
}
}
} catch (Exception $e) {
}
}
protected function _findByRoot($client, $url)
{
try {
$response = $client->get($url['scheme'] . '://' . $url['host'] . '/favicon.ico');
if ($this->_isValidFavicon($response)) {
return $response;
}
} catch (Horde_Http_Exception $e) {
}
}
protected function _findByPath($client, $url)
{
if (isset($url['path'])) {
$path = pathinfo($url['path']);
if (strlen($path['dirname'])) {
try {
$response = $client->get($url['scheme'] . '://' . $url['host'] . $path['dirname'] . '/favicon.ico');
if ($this->_isValidFavicon($response)) {
return $response;
}
} catch (Horde_Http_Exception $e) {
}
}
}
}
protected function _isValidFavicon($response)
{
return ($response->code == 200)
&& (substr($response->getHeader('content-type'), 0, 5) == 'image')
&& (strlen($response->getBody()) > 0);
}
}
|