favicon.php 7.1 KB

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