Oppaitime's version of Gazelle
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

index.php 4.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. <?
  2. if (!check_perms('site_proxy_images')) {
  3. img_error('forbidden');
  4. }
  5. $URL = isset($_GET['i']) ? htmlspecialchars_decode($_GET['i']) : null;
  6. if (!extension_loaded('openssl') && strtoupper($URL[4]) == 'S') {
  7. img_error('badprotocol');
  8. }
  9. /*
  10. if (!(preg_match('/^'.IMAGE_REGEX.'/is', $URL, $Matches) || preg_match('/^'.VIDEO_REGEX.'/is', $URL, $Matches))) {
  11. img_error('invalid');
  12. }
  13. */
  14. if (isset($_GET['c'])) {
  15. list($Data, $FileType) = $Cache->get_value('image_cache_'.md5($URL));
  16. $Cached = true;
  17. }
  18. if (!isset($Data) || !$Data) {
  19. $Cached = false;
  20. $Data = @file_get_contents($URL, 0, stream_context_create(array('http' => array('timeout' => 15), 'ssl' => array('verify_peer' => false))));
  21. if (!$Data || empty($Data)) {
  22. img_error('timeout');
  23. }
  24. $FileType = image_type($Data);
  25. if ($FileType && function_exists("imagecreatefrom$FileType")) {
  26. $Image = imagecreatefromstring($Data);
  27. if (invisible($Image)) {
  28. img_error('invisible');
  29. }
  30. if (verysmall($Image)) {
  31. img_error('small');
  32. }
  33. }
  34. if (isset($_GET['c']) && strlen($Data) < 524288 && substr($Data,0,1) != '<') {
  35. $Cache->cache_value('image_cache_'.md5($URL), array($Data, $FileType), 3600 * 24 * 7);
  36. }
  37. }
  38. // Reset avatar, add mod note
  39. function reset_image($UserID, $Type, $AdminComment, $PrivMessage) {
  40. if ($Type === 'avatar') {
  41. $CacheKey = "user_info_$UserID";
  42. $DBTable = 'users_info';
  43. $DBColumn = 'Avatar';
  44. $PMSubject = 'Your avatar has been automatically reset';
  45. } elseif ($Type === 'avatar2') {
  46. $CacheKey = "donor_info_$UserID";
  47. $DBTable = 'donor_rewards';
  48. $DBColumn = 'SecondAvatar';
  49. $PMSubject = 'Your second avatar has been automatically reset';
  50. } elseif ($Type === 'donoricon') {
  51. $CacheKey = "donor_info_$UserID";
  52. $DBTable = 'donor_rewards';
  53. $DBColumn = 'CustomIcon';
  54. $PMSubject = 'Your donor icon has been automatically reset';
  55. }
  56. $UserInfo = G::$Cache->get_value($CacheKey, true);
  57. if ($UserInfo !== false) {
  58. if ($UserInfo[$DBColumn] === '') {
  59. // This image has already been reset
  60. return;
  61. }
  62. $UserInfo[$DBColumn] = '';
  63. G::$Cache->cache_value($CacheKey, $UserInfo, 2592000); // cache for 30 days
  64. }
  65. // reset the avatar or donor icon URL
  66. G::$DB->query("
  67. UPDATE $DBTable
  68. SET $DBColumn = ''
  69. WHERE UserID = '$UserID'");
  70. // write comment to staff notes
  71. G::$DB->query("
  72. UPDATE users_info
  73. SET AdminComment = CONCAT('".sqltime().' - '.db_string($AdminComment)."\n\n', AdminComment)
  74. WHERE UserID = '$UserID'");
  75. // clear cache keys
  76. G::$Cache->delete_value($CacheKey);
  77. Misc::send_pm($UserID, 0, $PMSubject, $PrivMessage);
  78. }
  79. // Enforce avatar rules
  80. if (isset($_GET['type']) && isset($_GET['userid'])) {
  81. $ValidTypes = array('avatar', 'avatar2', 'donoricon');
  82. if (!is_number($_GET['userid']) || !in_array($_GET['type'], $ValidTypes)) {
  83. die();
  84. }
  85. $UserID = $_GET['userid'];
  86. $Type = $_GET['type'];
  87. if ($Type === 'avatar' || $Type === 'avatar2') {
  88. $MaxFileSize = 512 * 1024; // 512 kiB
  89. $MaxImageHeight = 600; // pixels
  90. $TypeName = $Type === 'avatar' ? 'avatar' : 'second avatar';
  91. } elseif ($Type === 'donoricon') {
  92. $MaxFileSize = 128 * 1024; // 128 kiB
  93. $MaxImageHeight = 100; // pixels
  94. $TypeName = 'donor icon';
  95. }
  96. $Height = image_height($FileType, $Data);
  97. if (strlen($Data) > $MaxFileSize || $Height > $MaxImageHeight) {
  98. // Sometimes the cached image we have isn't the actual image
  99. if ($Cached) {
  100. $Data2 = file_get_contents($URL, 0, stream_context_create(array('http' => array('timeout' => 60), 'ssl' => array('verify_peer' => false))));
  101. } else {
  102. $Data2 = $Data;
  103. }
  104. if ((strlen($Data2) > $MaxFileSize || image_height($FileType, $Data2) > $MaxImageHeight) && $UserID != 1 && $UserID != 2) {
  105. require_once(SERVER_ROOT.'/classes/mysql.class.php');
  106. require_once(SERVER_ROOT.'/classes/time.class.php');
  107. $DBURL = db_string($URL);
  108. $AdminComment = ucfirst($TypeName)." reset automatically (Size: ".number_format((strlen($Data)) / 1024)." kB, Height: ".$Height."px). Used to be $DBURL";
  109. $PrivMessage = SITE_NAME." has the following requirements for {$TypeName}s:\n\n".
  110. "[b]".ucfirst($TypeName)."s must not exceed ".($MaxFileSize / 1024)." kB or be vertically longer than {$MaxImageHeight}px.[/b]\n\n".
  111. "Your $TypeName at $DBURL has been found to exceed these rules. As such, it has been automatically reset. You are welcome to reinstate your $TypeName once it has been resized down to an acceptable size.";
  112. reset_image($UserID, $Type, $AdminComment, $PrivMessage);
  113. }
  114. }
  115. }
  116. if (!isset($FileType)) {
  117. img_error('timeout');
  118. }
  119. if ($FileType == 'webm') {
  120. header("Content-type: video/$FileType");
  121. } else {
  122. header("Content-type: image/$FileType");
  123. }
  124. echo $Data;