The script Oppaitime uses to serve and manage images

index.php 2.5KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. <?php
  2. // Location where hosted images will be stored
  3. define('IMG_ROOT', '/var/images/');
  4. // Pre-shared key - must match IMAGE_PSK in gazelle config
  5. define('PSK', 'AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA');
  6. ini_set('memory_limit', '256M');
  7. function image_type($Data) {
  8. if (!strncmp($Data, pack('H*', '89504E47'), 4)) return 'png';
  9. if (!strncmp($Data, pack('H*', 'FFD8'), 2)) return 'jpeg';
  10. if (!strncmp($Data, 'GIF', 3)) return 'gif';
  11. if (strlen($Data) > 35 && !substr_compare($Data, 'webm', 31, 4)) return 'webm';
  12. if (!strncmp($Data, 'BM', 2)) return 'bmp';
  13. if (!strncmp($Data, 'II', 2) || !strncmp($Data, 'MM', 2)) return 'tiff';
  14. return '';
  15. }
  16. $ImgURL = $_GET['i'];
  17. $Auth = rawurldecode($_GET['h']);
  18. $ImgURLHash = hash('sha256', $ImgURL);
  19. // Deletion
  20. if (!empty($_GET['d'])) {
  21. $ImgURL = $_GET['d'];
  22. $ImgURLHash = hash('sha256', $ImgURL);
  23. if (base64_encode(hash_hmac('sha256', $ImgURL, strrev(PSK), true)) != $Auth) {
  24. echo 'Auth failure';
  25. die();
  26. }
  27. if (file_exists(IMG_ROOT.substr($ImgURLHash,0,2).'/'.$ImgURLHash)) {
  28. unlink(IMG_ROOT.substr($ImgURLHash,0,2).'/'.$ImgURLHash);
  29. echo 'Success';
  30. } else {
  31. echo 'File does not exist';
  32. }
  33. die();
  34. }
  35. // Normal use
  36. if (base64_encode(hash_hmac('sha256', $ImgURL, PSK, true)) != $Auth) {
  37. // Authentication is incorrect. Something other than the paired Gazelle instance is attempting to use the host.
  38. header('Content-type: image/png');
  39. echo file_get_contents('imgs/unauthorized.png');
  40. die();
  41. }
  42. if (file_exists(IMG_ROOT.substr($ImgURLHash,0,2).'/'.$ImgURLHash)) {
  43. // The file is cached. Serve it.
  44. $Img = file_get_contents(IMG_ROOT.substr($ImgURLHash,0,2).'/'.$ImgURLHash);
  45. $FileType = image_type($Img);
  46. header('Content-type: '.(($FileType=='webm')?'video':'image').'/'.$FileType);
  47. echo $Img;
  48. } else {
  49. // The file is not cached. Fetch it, cache it, and serve it.
  50. $Img = @file_get_contents($ImgURL, 0, stream_context_create(['http' => ['user_agent' => 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/62.0.2987.133 Safari/537.36'], 'ssl' => ['verify_peer' => false]]), 0, 134217728);
  51. $FileType = image_type($Img);
  52. if (!empty($FileType)) {
  53. if (!file_exists(IMG_ROOT.substr($ImgURLHash,0,2))) {
  54. mkdir(IMG_ROOT.substr($ImgURLHash,0,2));
  55. }
  56. file_put_contents(IMG_ROOT.substr($ImgURLHash,0,2).'/'.$ImgURLHash, $Img);
  57. header('Content-type: '.(($FileType=='webm')?'video':'image').'/'.$FileType);
  58. echo $Img;
  59. } else {
  60. header('Content-type: image/png');
  61. echo file_get_contents('imgs/invalid.png');
  62. }
  63. }
  64. ?>