favicon.php 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  1. <?php
  2. if(!isset($_GET["s"])){
  3. header("X-Error: Missing parameter (s)ite");
  4. die();
  5. }
  6. new favicon($_GET["s"]);
  7. class favicon{
  8. public function __construct($url){
  9. header("Content-Type: image/png");
  10. if(substr_count($url, "/") !== 2){
  11. header("X-Error: Only provide the protocol and domain");
  12. $this->defaulticon();
  13. }
  14. $filename = str_replace(["https://", "http://"], "", $url);
  15. header("Content-Disposition: inline; filename=\"{$filename}.png\"");
  16. include "lib/curlproxy.php";
  17. $this->proxy = new proxy(false);
  18. $this->filename = parse_url($url, PHP_URL_HOST);
  19. /*
  20. Check if we have the favicon stored locally
  21. */
  22. if(file_exists("icons/" . $filename . ".png")){
  23. $handle = fopen("icons/" . $filename . ".png", "r");
  24. echo fread($handle, filesize("icons/" . $filename . ".png"));
  25. fclose($handle);
  26. return;
  27. }
  28. /*
  29. Scrape html
  30. */
  31. try{
  32. $payload = $this->proxy->get($url, $this->proxy::req_web, true);
  33. }catch(Exception $error){
  34. header("X-Error: Could not fetch HTML (" . $error->getMessage() . ")");
  35. $this->favicon404();
  36. }
  37. //$payload["body"] = '<link rel="manifest" id="MANIFEST_LINK" href="/data/manifest/" crossorigin="use-credentials" />';
  38. // get link tags
  39. preg_match_all(
  40. '/< *link +(.*)[\/]?>/Uixs',
  41. $payload["body"],
  42. $linktags
  43. );
  44. /*
  45. Get relevant tags
  46. */
  47. $linktags = $linktags[1];
  48. $attributes = [];
  49. /*
  50. header("Content-Type: text/plain");
  51. print_r($linktags);
  52. print_r($payload);
  53. die();*/
  54. for($i=0; $i<count($linktags); $i++){
  55. // get attributes
  56. preg_match_all(
  57. '/([A-Za-z0-9]+) *= *("[^"]*"|[^" ]+)/s',
  58. $linktags[$i],
  59. $tags
  60. );
  61. for($k=0; $k<count($tags[1]); $k++){
  62. $attributes[$i][] = [
  63. "name" => $tags[1][$k],
  64. "value" => trim($tags[2][$k], "\" \n\r\t\v\x00")
  65. ];
  66. }
  67. }
  68. unset($payload);
  69. unset($linktags);
  70. $href = [];
  71. // filter out the tags we want
  72. foreach($attributes as &$group){
  73. $tmp_href = null;
  74. $tmp_rel = null;
  75. $badtype = false;
  76. foreach($group as &$attribute){
  77. switch($attribute["name"]){
  78. case "rel":
  79. $attribute["value"] = strtolower($attribute["value"]);
  80. if(
  81. (
  82. $attribute["value"] == "icon" ||
  83. $attribute["value"] == "manifest" ||
  84. $attribute["value"] == "shortcut icon" ||
  85. $attribute["value"] == "apple-touch-icon" ||
  86. $attribute["value"] == "mask-icon"
  87. ) === false
  88. ){
  89. break;
  90. }
  91. $tmp_rel = $attribute["value"];
  92. break;
  93. case "type":
  94. $attribute["value"] = explode("/", $attribute["value"], 2);
  95. if(strtolower($attribute["value"][0]) != "image"){
  96. $badtype = true;
  97. break;
  98. }
  99. break;
  100. case "href":
  101. // must not contain invalid characters
  102. // must be bigger than 1
  103. if(
  104. filter_var($attribute["value"], FILTER_SANITIZE_URL) == $attribute["value"] &&
  105. strlen($attribute["value"]) > 0
  106. ){
  107. $tmp_href = $attribute["value"];
  108. break;
  109. }
  110. break;
  111. }
  112. }
  113. if(
  114. $badtype === false &&
  115. $tmp_rel !== null &&
  116. $tmp_href !== null
  117. ){
  118. $href[$tmp_rel] = $tmp_href;
  119. }
  120. }
  121. /*
  122. Priority list
  123. */
  124. /*
  125. header("Content-Type: text/plain");
  126. print_r($href);
  127. die();*/
  128. if(isset($href["icon"])){ $href = $href["icon"]; }
  129. elseif(isset($href["apple-touch-icon"])){ $href = $href["apple-touch-icon"]; }
  130. elseif(isset($href["manifest"])){
  131. // attempt to parse manifest, but fallback to []
  132. $href = $this->parsemanifest($href["manifest"], $url);
  133. }
  134. if(is_array($href)){
  135. if(isset($href["mask-icon"])){ $href = $href["mask-icon"]; }
  136. elseif(isset($href["shortcut icon"])){ $href = $href["shortcut icon"]; }
  137. else{
  138. $href = "/favicon.ico";
  139. }
  140. }
  141. $href = $this->proxy->getabsoluteurl($href, $url);
  142. /*
  143. header("Content-type: text/plain");
  144. echo $href;
  145. die();*/
  146. /*
  147. Download the favicon
  148. */
  149. //$href = "https://git.lolcat.ca/assets/img/logo.svg";
  150. try{
  151. $payload =
  152. $this->proxy->get(
  153. $href,
  154. $this->proxy::req_image,
  155. true,
  156. $url
  157. );
  158. }catch(Exception $error){
  159. header("X-Error: Could not fetch the favicon (" . $error->getMessage() . ")");
  160. $this->favicon404();
  161. }
  162. /*
  163. Parse the file format
  164. */
  165. $image = null;
  166. $format = $this->proxy->getimageformat($payload, $image);
  167. /*
  168. Convert the image
  169. */
  170. try{
  171. /*
  172. @todo: fix issues with avif+transparency
  173. maybe using GD as fallback?
  174. */
  175. if($format !== false){
  176. $image->setFormat($format);
  177. }
  178. $image->setBackgroundColor(new ImagickPixel("transparent"));
  179. $image->readImageBlob($payload["body"]);
  180. $image->resizeImage(16, 16, imagick::FILTER_LANCZOS, 1);
  181. $image->setFormat("png");
  182. $image = $image->getImageBlob();
  183. // save favicon
  184. $handle = fopen("icons/" . $this->filename . ".png", "w");
  185. fwrite($handle, $image, strlen($image));
  186. fclose($handle);
  187. echo $image;
  188. }catch(ImagickException $error){
  189. header("X-Error: Could not convert the favicon: (" . $error->getMessage() . ")");
  190. $this->favicon404();
  191. }
  192. return;
  193. }
  194. private function parsemanifest($href, $url){
  195. if(
  196. // check if base64-encoded JSON manifest
  197. preg_match(
  198. '/^data:application\/json;base64,([A-Za-z0-9=]*)$/',
  199. $href,
  200. $json
  201. )
  202. ){
  203. $json = base64_decode($json[1]);
  204. if($json === false){
  205. // could not decode the manifest regex
  206. return [];
  207. }
  208. }else{
  209. try{
  210. $json =
  211. $this->proxy->get(
  212. $this->proxy->getabsoluteurl($href, $url),
  213. $this->proxy::req_web,
  214. false,
  215. $url
  216. );
  217. $json = $json["body"];
  218. }catch(Exception $error){
  219. // could not fetch the manifest
  220. return [];
  221. }
  222. }
  223. $json = json_decode($json, true);
  224. if($json === null){
  225. // manifest did not return valid json
  226. return [];
  227. }
  228. if(
  229. isset($json["start_url"]) &&
  230. $this->proxy->validateurl($json["start_url"])
  231. ){
  232. $url = $json["start_url"];
  233. }
  234. if(!isset($json["icons"][0]["src"])){
  235. // manifest does not contain a path to the favicon
  236. return [];
  237. }
  238. // horay, return the favicon path
  239. return $json["icons"][0]["src"];
  240. }
  241. private function favicon404(){
  242. // fallback to google favicons
  243. // ... probably blocked by cuckflare
  244. try{
  245. $image =
  246. $this->proxy->get(
  247. "https://t0.gstatic.com/faviconV2?client=SOCIAL&type=FAVICON&fallback_opts=TYPE,SIZE,URL&url=http://{$this->filename}&size=16",
  248. $this->proxy::req_image
  249. );
  250. }catch(Exception $error){
  251. $this->defaulticon();
  252. }
  253. // write favicon from google
  254. $handle = fopen("icons/" . $this->filename . ".png", "w");
  255. fwrite($handle, $image["body"], strlen($image["body"]));
  256. fclose($handle);
  257. echo $image["body"];
  258. die();
  259. }
  260. private function defaulticon(){
  261. // give 404 and fuck off
  262. http_response_code(404);
  263. $handle = fopen("lib/favicon404.png", "r");
  264. echo fread($handle, filesize("lib/favicon404.png"));
  265. fclose($handle);
  266. die();
  267. }
  268. }