123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205 |
- <?php
- if (basename ($_SERVER['SCRIPT_NAME']) == basename (__FILE__)) {
- die ("no direct access allowed");
- }
- class favicon {
- function favicon ($url) {
- global $settings, $convert_favicons;
- if ($settings['show_bookmark_icon']) {
- if ($this->parsed_url = $this->return_parse_url ($url)) {
- if ($this->favicon_url = $this->get_favicon_url ()) {
- $this->icon_name = rand () . basename ($this->favicon_url);
- if ($this->get_favicon_image ()) {
- if ($convert_favicons) {
- $this->favicon = $this->convert_favicon ();
- }
- else {
- $this->favicon = "./favicons/" . $this->icon_name;
- }
- }
- }
- }
- }
- }
-
-
-
-
- function convert_favicon () {
- global $convert, $identify;
- $tmp_file = "./favicons/" . $this->icon_name;
-
- if (@exec ("$identify $tmp_file", $output)) {
- $ident = explode (" ", $output[0]);
- if (count ($output) > 1) {
- $file_to_convert = $ident[0];
- }
- else {
- $file_to_convert = $tmp_file;
- }
-
- system ("$convert $file_to_convert -resize 16x16 $tmp_file.png");
- @unlink ($tmp_file);
- return $tmp_file . ".png";
- }
- else {
- @unlink ($tmp_file);
- return false;
- }
- }
-
-
-
- function get_favicon_image () {
-
-
- $httpparsed = $this->return_parse_url ($this->favicon_url);
-
- $httprequest = "GET ".$httpparsed['path']." HTTP/1.0\r\n".
- "Accept: */*\r\n".
- "Accept-Language: en\r\n".
- "Accept-Encoding: identity\r\n".
- "User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)\r\n".
- "Host: ".$httpparsed['host']."\r\n".
- "Connection: close\r\n\r\n";
-
- if ($httphandle = fsockopen($httpparsed['host'],$httpparsed['port'])) {
- fputs($httphandle, $httprequest);
-
- $answerdata = null;
- do {
- $answerdata .= fread($httphandle, 1024);
- } while (feof($httphandle) != true);
-
- fclose ($httphandle);
-
- $finalposi = strpos($answerdata, "\r\n\r\n") + 4;
- $finalfile = substr($answerdata, $finalposi, strlen($answerdata) - $finalposi);
-
- if ($fp = @fopen("./favicons/" . $this->icon_name, "w")) {
- fwrite($fp, $finalfile);
- fclose($fp);
- return true;
- }
- else {
- return false;
- }
- }
- else {
- return false;
- }
- }
-
-
-
-
- function get_favicon_url () {
- global $timeout;
-
- if ($socket = @fsockopen ($this->parsed_url['host'], $this->parsed_url['port'], $errno, $errstr, $timeout)) {
- fwrite ($socket, "HEAD /favicon.ico HTTP/1.0\r\nHost: " . $this->parsed_url['host'] . "\r\n\r\n");
- $http_response = fgets ($socket, 22);
- fclose ($socket);
- if (preg_match ("200 OK", $http_response)) {
-
- return $this->parsed_url['scheme'] . "://" . $this->parsed_url['host'] . ":" . $this->parsed_url['port'] . "/favicon.ico";
- }
- else {
-
- if ($socket = @fsockopen ($this->parsed_url['host'], $this->parsed_url['port'], $errno, $errstr, $timeout)) {
- fwrite ($socket, "GET " . $this->parsed_url['path'] . " HTTP/1.0\r\nHost: " . $this->parsed_url['host'] . "\r\n\r\n");
- while (!feof ($socket)) {
- $html = fgets ($socket, 1024);
- if ($html == null) {
- return false;
- }
-
- if (preg_match ('/.*Content-Type:.*/si', $html, $contenttype)) {
- if ( ! preg_match ('/text\/html/si', $contenttype[0])) {
- return false;
- }
- }
- if (preg_match ('/<link[^>]+rel="(?:shortcut )?icon".*>/si', $html, $tag)) {
-
- if (preg_match ('/<link[^>]+href="([^"]+)".*>/si', $tag[0], $location)) {
-
- if (substr($location[1], 0, 7) == 'http://') {
- $favicon = $location[1];
- }
-
- else if (substr ($location[1], 0, 1) == '/') {
- $favicon = $this->parsed_url['scheme'] . '://' . $this->parsed_url['host'] . ":" . $this->parsed_url['port'] . $location[1];
- }
-
-
-
-
- else {
-
-
- if (substr ($this->parsed_url['path'], strlen($this->parsed_url['path'])-1) == "/") {
- $favicon = $this->parsed_url['scheme'] . '://' . $this->parsed_url['host'] . ":" . $this->parsed_url['port'] . $this->parsed_url['path'] . $location[1];
- }
- else {
- $favicon = $this->parsed_url['scheme'] . '://' . $this->parsed_url['host'] . ":" . $this->parsed_url['port'] . dirname ($this->parsed_url['path']) . '/' . $location[1];
- }
- }
- return $favicon;
- }
- }
- else if (preg_match ('/.*<\/head.*>/si', $html)) {
-
- return false;
- }
- }
- fclose ($socket);
- }
- }
- }
- return false;
- }
-
-
-
- function return_parse_url ($url) {
- if ($parsed = @parse_url ($url)) {
- if (!isset ($parsed['scheme']) || $parsed['scheme'] == "") {
- $parsed['scheme'] = "http";
- }
- else if ($parsed['scheme'] != "http") {
- return false;
- }
- if (!isset ($parsed['host']) || $parsed['host'] == "") {
- return false;
- }
- if (!isset ($parsed['port']) || $parsed['port'] == "") {
- $parsed['port'] = 80;
- }
- if (!isset ($parsed['path']) || $parsed['path'] == "") {
- $parsed['path'] = "/";
- }
- return ($parsed);
- }
- else {
- return false;
- }
- }
- }
- ?>
|