Guest User

Untitled

a guest
Apr 15th, 2017
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.33 KB | None | 0 0
  1. <?php
  2. $game = 'd2';
  3. $zipName = "test2.zip";
  4.  
  5. $zip = new ZipArchive;
  6. $res = $zip->open($zipName);
  7.  
  8. if ($res !== true) {
  9. header('400 Bad Request');
  10. echo 'Uploaded file not a valid Zip.';
  11. exit;
  12. }
  13.  
  14. if ($game == 'd2') {
  15.  
  16. $mapData = null;
  17. $isValidMapData = 0;
  18. $iniData = null;
  19. $isValidIniData = 0;
  20. $misData = null;
  21. $isValidMisData = 0;
  22.  
  23. for ($i = 0; $i < 3; $i++) {
  24. $tmp = $zip->statIndex($i);
  25.  
  26. if ($tmp['size'] > 128 * 128 * 8) {
  27. header('400 Bad Request');
  28. echo 'Map file larger than expected.';
  29. exit;
  30. }
  31.  
  32. if (is_array($tmp) && preg_match('/\.map$/i', $tmp['name'])) {
  33.  
  34. $mapData = $zip->getFromIndex($i);
  35.  
  36. if ($mapData !== null) {
  37. $isValidMapData = isDuneMapFileValid('zip://'.$zipName.'#'.$tmp['name'], $tmp['size']);
  38. }
  39. } else if (is_array($tmp) && preg_match('/\.ini$/i', $tmp['name'])) {
  40.  
  41. $iniData = $zip->getFromIndex($i);
  42.  
  43. if ($iniData !== null) {
  44. $isValidIniData = isDuneIniFileValid('zip://'.$zipName.'#'.$tmp['name'], $tmp['size']);
  45. }
  46. } else if (is_array($tmp) && preg_match('/\.mis$/i', $tmp['name'])) {
  47.  
  48. $misData = $zip->getFromIndex($i);
  49.  
  50. if ($misData !== null) {
  51. $isValidMisData= isDuneMisFileValid('zip://'.$zipName.'#'.$tmp['name'], $tmp['size']);
  52. }
  53. }
  54. }
  55.  
  56. if ($mapData === null || $isValidMapData === 0) {
  57. header('400 Bad Request');
  58. echo 'Valid map file not found in Zip.';
  59. exit;
  60. }
  61.  
  62. if ($iniData === null || $isValidIniData === 0) {
  63. header('400 Bad Request');
  64. echo 'Valid map ini file not found in Zip.';
  65. exit;
  66. }
  67.  
  68. if ($misData !== null && $isValidMisData === 0) {
  69. header('400 Bad Request');
  70. echo 'Valid map mis file not found in Zip.';
  71. exit;
  72. }
  73.  
  74. // if ($sha1 != sha1($mapData . $iniData . $misData)) {
  75. // header('400 Bad Request');
  76. // echo 'Map file checksum differs from Zip name, rejected.';
  77. // exit;
  78. // }
  79.  
  80. $res = $zip->open($zipName, ZipArchive::CREATE);
  81. if ($res !== true) {
  82. header('500 Internal Server Error');
  83. echo 'Server failed to save map zip, sorry.';
  84. exit;
  85. }
  86.  
  87. $zip->addFromString($sha1 . '.map', $mapData);
  88.  
  89. $zip->addFromString($sha1 . '.ini', $iniData);
  90.  
  91. if ($misData)
  92. $zip->addFromString('_' . $sha1 . '.mis', $misData);
  93.  
  94. $zip->close();
  95.  
  96. echo 'Upload succeeded!';
  97. exit;
  98. }
  99.  
  100. /**
  101. * This function validates Dune map file.
  102. *
  103. * @param $filePath Path to the map file.
  104. * @param $fileSize Size of the map file.
  105. * @return number 1 if map is valid, 0 otherwise.
  106. */
  107. function isDuneMapFileValid($filePath, $fileSize) {
  108.  
  109. $returnVal = 1;
  110.  
  111. $handle = fopen($filePath, "rb");
  112.  
  113. $height = unpack('s', fread($handle, 2))[1];
  114. $width = unpack('s', fread($handle, 2))[1];
  115.  
  116. // Check if height is valid.
  117. if ($height > 128)
  118. {
  119. $returnVal = 0;
  120. }
  121.  
  122. // Check if width is valid.
  123. if ($width > 128)
  124. {
  125. $returnVal = 0;
  126. }
  127.  
  128. // Check if file size is valid.
  129. if (($height * $width * 4) + 4 != $fileSize)
  130. {
  131. $returnVal = 0;
  132. }
  133.  
  134. $cellCount = $height * $width;
  135.  
  136. if ($returnVal == 1)
  137. {
  138. // Check if all cells are valid.
  139. for ($iter = 1; $iter<= $cellCount; $iter++)
  140. {
  141. $tile = unpack('s', fread($handle, 2))[1];
  142.  
  143. //Check tile index value of the cell.
  144. if ($tile >= 800)
  145. {
  146. $returnVal = 0;
  147. break;
  148. }
  149.  
  150. $special = unpack('s', fread($handle, 2))[1];
  151.  
  152. //Check special index value of the cell.
  153. if ($special >= 1000)
  154. {
  155. $returnVal = 0;
  156. break;
  157. }
  158. }
  159. }
  160.  
  161. return $returnVal;
  162. }
  163.  
  164. /**
  165. * This function validates Dune ini file.
  166. *
  167. * @param $iniPath Path to the ini file.
  168. * @param $fileSize Size of the ini file.
  169. * @return number number 1 if ini file is valid, 0 otherwise.
  170. */
  171. function isDuneIniFileValid($filePath, $fileSize) {
  172.  
  173. $returnVal = 1;
  174.  
  175. if (strcmp(mime_content_type($filePath),"text/plain") != 0) {
  176.  
  177. $returnVal = 0;
  178. }
  179.  
  180. if ($fileSize > 2000) {
  181.  
  182. $returnVal = 0;
  183. }
  184.  
  185. return $returnVal;
  186. }
  187.  
  188. /**
  189. * This function validates Dune mis file.
  190. *
  191. * @param $filePath Path to the ini file.
  192. * @param $fileSize Size of the ini file.
  193. * @return number number 1 if ini file is valid, 0 otherwise.
  194. */
  195. function isDuneMisFileValid($filePath, $fileSize) {
  196.  
  197. $returnVal = 1;
  198.  
  199. // Check if .mis file size is 68066.
  200. if ($fileSize!= 68066) {
  201.  
  202. $returnVal = 0;
  203. }
  204.  
  205. return $returnVal;
  206. }
  207.  
  208. ?>
Add Comment
Please, Sign In to add comment