1
0

favicon.php 7.1 KB

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