favicon.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. <?php
  2. if (basename ($_SERVER['SCRIPT_NAME']) == basename (__FILE__)) {
  3. die ("no direct access allowed");
  4. }
  5. class favicon {
  6. function favicon ($url) {
  7. global $settings, $convert_favicons;
  8. if ($settings['show_bookmark_icon']) {
  9. if ($this->parsed_url = $this->return_parse_url ($url)) {
  10. if ($this->favicon_url = $this->get_favicon_url ()) {
  11. $this->icon_name = rand () . basename ($this->favicon_url);
  12. if ($this->get_favicon_image ()) {
  13. if ($convert_favicons) {
  14. $this->favicon = $this->convert_favicon ();
  15. }
  16. else {
  17. $this->favicon = "./favicons/" . $this->icon_name;
  18. }
  19. }
  20. }
  21. }
  22. }
  23. }
  24. ###
  25. ### check the image type and convert & resize it if required
  26. ### returns the absolute path of the (converted) .png file
  27. ###
  28. function convert_favicon () {
  29. global $convert, $identify;
  30. $tmp_file = "./favicons/" . $this->icon_name;
  31. # find out file type
  32. if (@exec ("$identify $tmp_file", $output)) {
  33. $ident = explode (" ", $output[0]);
  34. if (count ($output) > 1) {
  35. $file_to_convert = $ident[0];
  36. }
  37. else {
  38. $file_to_convert = $tmp_file;
  39. }
  40. # convert image in any case to 16x16 and .png
  41. system ("$convert $file_to_convert -resize 16x16 $tmp_file.png");
  42. @unlink ($tmp_file);
  43. return $tmp_file . ".png";
  44. }
  45. else {
  46. @unlink ($tmp_file);
  47. return false;
  48. }
  49. }
  50. ###
  51. ### download and save favicon
  52. ###
  53. function get_favicon_image () {
  54. //Selbstgebastelte, IIS-kompatible Version von Arne Haak (www.arnehaak.de)
  55. # HTTP-Url auswerten
  56. $httpparsed = $this->return_parse_url ($this->favicon_url);
  57. //HTTP-Request-Header erzeugen
  58. $httprequest = "GET ".$httpparsed['path']." HTTP/1.0\r\n".
  59. "Accept: */*\r\n".
  60. "Accept-Language: en\r\n".
  61. "Accept-Encoding: identity\r\n".
  62. "User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)\r\n".
  63. "Host: ".$httpparsed['host']."\r\n".
  64. "Connection: close\r\n\r\n";
  65. //Verbindung aufbauen und Request abschicken
  66. if ($httphandle = fsockopen($httpparsed['host'],$httpparsed['port'])) {
  67. fputs($httphandle, $httprequest);
  68. //Daten runterladen solange vorhanden
  69. $answerdata = null;
  70. do {
  71. $answerdata .= fread($httphandle, 1024);
  72. } while (feof($httphandle) != true);
  73. // Verbindung schliessen
  74. fclose ($httphandle);
  75. //Header finden und abtrennen
  76. $finalposi = strpos($answerdata, "\r\n\r\n") + 4; //Position des ersten Bytes nach dem Header bestimmen
  77. $finalfile = substr($answerdata, $finalposi, strlen($answerdata) - $finalposi); //Header abschneiden
  78. //Datei abspeichern
  79. if ($fp = @fopen("./favicons/" . $this->icon_name, "w")) {
  80. fwrite($fp, $finalfile);
  81. fclose($fp);
  82. return true;
  83. }
  84. else {
  85. return false;
  86. }
  87. }
  88. else {
  89. return false;
  90. }
  91. }
  92. ###
  93. ### checks for the existence of a favicon on a remote server
  94. ### and returns the url if one exist
  95. ###
  96. function get_favicon_url () {
  97. global $timeout;
  98. # search for favicon in document root first
  99. if ($socket = @fsockopen ($this->parsed_url['host'], $this->parsed_url['port'], $errno, $errstr, $timeout)) {
  100. fwrite ($socket, "HEAD /favicon.ico HTTP/1.0\r\nHost: " . $this->parsed_url['host'] . "\r\n\r\n");
  101. $http_response = fgets ($socket, 22);
  102. fclose ($socket);
  103. if (preg_match ("200 OK", $http_response)) {
  104. #echo "favicon found in document root\n";
  105. return $this->parsed_url['scheme'] . "://" . $this->parsed_url['host'] . ":" . $this->parsed_url['port'] . "/favicon.ico";
  106. }
  107. else {
  108. # if favicon was not found in document root, search in html header for it
  109. if ($socket = @fsockopen ($this->parsed_url['host'], $this->parsed_url['port'], $errno, $errstr, $timeout)) {
  110. fwrite ($socket, "GET " . $this->parsed_url['path'] . " HTTP/1.0\r\nHost: " . $this->parsed_url['host'] . "\r\n\r\n");
  111. while (!feof ($socket)) {
  112. $html = fgets ($socket, 1024);
  113. if ($html == null) {
  114. return false;
  115. }
  116. # we only want to search in HTML documents
  117. if (preg_match ('/.*Content-Type:.*/si', $html, $contenttype)) {
  118. if ( ! preg_match ('/text\/html/si', $contenttype[0])) {
  119. return false;
  120. }
  121. }
  122. if (preg_match ('/<link[^>]+rel="(?:shortcut )?icon".*>/si', $html, $tag)) {
  123. #echo "found favicon in html header\n";
  124. if (preg_match ('/<link[^>]+href="([^"]+)".*>/si', $tag[0], $location)) {
  125. # the favicon location is an url
  126. if (substr($location[1], 0, 7) == 'http://') {
  127. $favicon = $location[1];
  128. }
  129. # the favicon location is an absolute path
  130. else if (substr ($location[1], 0, 1) == '/') {
  131. $favicon = $this->parsed_url['scheme'] . '://' . $this->parsed_url['host'] . ":" . $this->parsed_url['port'] . $location[1];
  132. }
  133. # else the path can only be something useless
  134. # or a relative path, looking like this.
  135. # ./path/to/favicon.ico
  136. # path/to/favicon.ico
  137. else {
  138. # The location we called is either a file or a directory.
  139. # We have to guess. We assume it is a directory if there is a trailing slash
  140. if (substr ($this->parsed_url['path'], strlen($this->parsed_url['path'])-1) == "/") {
  141. $favicon = $this->parsed_url['scheme'] . '://' . $this->parsed_url['host'] . ":" . $this->parsed_url['port'] . $this->parsed_url['path'] . $location[1];
  142. }
  143. else {
  144. $favicon = $this->parsed_url['scheme'] . '://' . $this->parsed_url['host'] . ":" . $this->parsed_url['port'] . dirname ($this->parsed_url['path']) . '/' . $location[1];
  145. }
  146. }
  147. return $favicon;
  148. }
  149. }
  150. else if (preg_match ('/.*<\/head.*>/si', $html)) {
  151. #echo "html header end found, giving up\n";
  152. return false;
  153. }
  154. }
  155. fclose ($socket);
  156. }
  157. }
  158. }
  159. return false;
  160. }
  161. ###
  162. ### returns an array with parts of the given url
  163. ###
  164. function return_parse_url ($url) {
  165. if ($parsed = @parse_url ($url)) {
  166. if (!isset ($parsed['scheme']) || $parsed['scheme'] == "") {
  167. $parsed['scheme'] = "http";
  168. }
  169. else if ($parsed['scheme'] != "http") {
  170. return false;
  171. }
  172. if (!isset ($parsed['host']) || $parsed['host'] == "") {
  173. return false;
  174. }
  175. if (!isset ($parsed['port']) || $parsed['port'] == "") {
  176. $parsed['port'] = 80;
  177. }
  178. if (!isset ($parsed['path']) || $parsed['path'] == "") {
  179. $parsed['path'] = "/";
  180. }
  181. return ($parsed);
  182. }
  183. else {
  184. return false;
  185. }
  186. }
  187. }
  188. ?>