Guest User

Untitled

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