Advertisement
anvenger

UNZIPER

Jun 2nd, 2018
211
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 6.65 KB | None | 0 0
  1. <?php
  2. /**
  3.  * The Unzipper extracts .zip archives and .gz files on webservers. It's handy if you
  4.  * do not have shell access. E.g. if you want to upload a lot of files
  5.  * (php framework or image collection) as archive to save time.
  6.  *
  7.  *
  8.  * @author  Andreas Tasch, at[tec], attec.at
  9.  * @license GNU GPL v3
  10.  * @package attec.toolbox
  11.  * @version 0.0.2 Alpha
  12.  */
  13.  
  14. $timestart = microtime(TRUE);
  15.  
  16. $arc = new Unzipper;
  17.  
  18. $timeend = microtime(TRUE);
  19. $time = $timeend - $timestart;
  20.  
  21. class Unzipper {
  22.   public $localdir = '.';
  23.   public $zipfiles = array();
  24.   public static $status = '';
  25.  
  26.   public function __construct() {
  27.  
  28.     //read directory and pick .zip and .gz files
  29.     if ($dh = opendir($this->localdir)) {
  30.       while (($file = readdir($dh)) !== FALSE) {
  31.         if (pathinfo($file, PATHINFO_EXTENSION) === 'zip'
  32.           || pathinfo($file, PATHINFO_EXTENSION) === 'gz'
  33.         ) {
  34.           $this->zipfiles[] = $file;
  35.         }
  36.       }
  37.       closedir($dh);
  38.  
  39.       if(!empty($this->zipfiles)) {
  40.         self::$status = '.zip or .gz files found, ready for extraction';
  41.       }
  42.       else {
  43.         self::$status = '<span style="color:red; font-weight:bold;font-size:120%;">Error: No .zip or .gz files found.</span>';
  44.       }
  45.     }
  46.  
  47.     //check if an archive was selected for unzipping
  48.     //check if archive has been selected
  49.     $input = '';
  50.     $input = strip_tags($_POST['zipfile']);
  51.  
  52.     //allow only local existing archives to extract
  53.     if ($input !== '') {
  54.       if (in_array($input, $this->zipfiles)) {
  55.         self::extract($input, $this->localdir);
  56.       }
  57.     }
  58.   }
  59.  
  60.   public static function extract($archive, $destination) {
  61.     $ext = pathinfo($archive, PATHINFO_EXTENSION);
  62.  
  63.     if ($ext === 'zip') {
  64.       self::extractZipArchive($archive, $destination);
  65.     }
  66.     else {
  67.       if ($ext === 'gz') {
  68.         self::extractGzipFile($archive, $destination);
  69.       }
  70.     }
  71.  
  72.   }
  73.  
  74.   /**
  75.    * Decompress/extract a zip archive using ZipArchive.
  76.    *
  77.    * @param $archive
  78.    * @param $destination
  79.    */
  80.   public static function extractZipArchive($archive, $destination) {
  81.     // Check if webserver supports unzipping.
  82.     if(!class_exists('ZipArchive')) {
  83.       self::$status = '<span style="color:red; font-weight:bold;font-size:120%;">Error: Your PHP version does not support unzip functionality.</span>';
  84.       return;
  85.     }
  86.  
  87.     $zip = new ZipArchive;
  88.  
  89.     // Check if archive is readable.
  90.     if ($zip->open($archive) === TRUE) {
  91.       // Check if destination is writable
  92.       if(is_writeable($destination . '/')) {
  93.         $zip->extractTo($destination);
  94.         $zip->close();
  95.         self::$status = '<span style="color:green; font-weight:bold;font-size:120%;">Files unzipped successfully</span>';
  96.       }
  97.       else {
  98.         self::$status = '<span style="color:red; font-weight:bold;font-size:120%;">Error: Directory not writeable by webserver.</span>';
  99.       }
  100.     }
  101.     else {
  102.       self::$status = '<span style="color:red; font-weight:bold;font-size:120%;">Error: Cannot read .zip archive.</span>';
  103.     }
  104.   }
  105.  
  106.   /**
  107.    * Decompress a .gz File.
  108.    *
  109.    * @param $archive
  110.    * @param $destination
  111.    */
  112.   public static function extractGzipFile($archive, $destination) {
  113.     // Check if zlib is enabled
  114.     if(!function_exists('gzopen')) {
  115.       self::$status = '<span style="color:red; font-weight:bold;font-size:120%;">Error: Your PHP has no zlib support enabled.</span>';
  116.       return;
  117.     }
  118.  
  119.     $filename = pathinfo($archive, PATHINFO_FILENAME);
  120.     $gzipped = gzopen($archive, "rb");
  121.     $file = fopen($filename, "w");
  122.  
  123.     while ($string = gzread($gzipped, 4096)) {
  124.       fwrite($file, $string, strlen($string));
  125.     }
  126.     gzclose($gzipped);
  127.     fclose($file);
  128.  
  129.     // Check if file was extracted.
  130.     if(file_exists($destination . '/' . $filename)) {
  131.       self::$status = '<span style="color:green; font-weight:bold;font-size:120%;">File unzipped successfully.</span>';
  132.     }
  133.     else {
  134.       self::$status = '<span style="color:red; font-weight:bold;font-size:120%;">Error unzipping file.</span>';
  135.     }
  136.  
  137.   }
  138. }
  139.  
  140. ?>
  141.  
  142. <!DOCTYPE html>
  143. <head>
  144.   <title>File Unzipper</title>
  145.   <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  146.   <style type="text/css">
  147.     <!--
  148.     body {
  149.       font-family: Arial, serif;
  150.       line-height: 150%;
  151.     }
  152.  
  153.     fieldset {
  154.       border: 0px solid #000;
  155.     }
  156.  
  157.     .select {
  158.       padding: 5px;
  159.       font-size: 110%;
  160.     }
  161.  
  162.     .status {
  163.       margin-top: 20px;
  164.       padding: 5px;
  165.       font-size: 80%;
  166.       background: #EEE;
  167.       border: 1px dotted #DDD;
  168.     }
  169.  
  170.     .submit {
  171.       -moz-box-shadow: inset 0px 1px 0px 0px #bbdaf7;
  172.       -webkit-box-shadow: inset 0px 1px 0px 0px #bbdaf7;
  173.       box-shadow: inset 0px 1px 0px 0px #bbdaf7;
  174.       background: -webkit-gradient(linear, left top, left bottom, color-stop(0.05, #79bbff), color-stop(1, #378de5));
  175.       background: -moz-linear-gradient(center top, #79bbff 5%, #378de5 100%);
  176.       filter: progid:DXImageTransform.Microsoft.gradient(startColorstr='#79bbff', endColorstr='#378de5');
  177.       background-color: #79bbff;
  178.       -moz-border-radius: 4px;
  179.       -webkit-border-radius: 4px;
  180.       border-radius: 4px;
  181.       border: 1px solid #84bbf3;
  182.       display: inline-block;
  183.       color: #ffffff;
  184.       font-family: arial;
  185.       font-size: 15px;
  186.       font-weight: bold;
  187.       padding: 10px 24px;
  188.       text-decoration: none;
  189.       text-shadow: 1px 1px 0px #528ecc;
  190.     }
  191.  
  192.     .submit:hover {
  193.       background: -webkit-gradient(linear, left top, left bottom, color-stop(0.05, #378de5), color-stop(1, #79bbff));
  194.       background: -moz-linear-gradient(center top, #378de5 5%, #79bbff 100%);
  195.       filter: progid:DXImageTransform.Microsoft.gradient(startColorstr='#378de5', endColorstr='#79bbff');
  196.       background-color: #378de5;
  197.     }
  198.  
  199.     .submit:active {
  200.       position: relative;
  201.       top: 1px;
  202.     }
  203.  
  204.     /* This imageless css button was generated by CSSButtonGenerator.com */
  205.  
  206.     -->
  207.   </style>
  208. </head>
  209.  
  210. <body>
  211. <h1>Archive Unzipper</h1>
  212.  
  213. <p>Select .zip archive or .gz file you want to extract:</p>
  214.  
  215. <form action="" method="POST">
  216.   <fieldset>
  217.  
  218.     <select name="zipfile" size="1" class="select">
  219.       <?php foreach ($arc->zipfiles as $zip) {
  220.         echo "<option>$zip</option>";
  221.       }
  222.       ?>
  223.     </select>
  224.  
  225.     <br/>
  226.  
  227.     <input type="submit" name="submit" class="submit" value="Unzip Archive"/>
  228.  
  229.   </fieldset>
  230. </form>
  231. <p class="status">
  232.   Status: <?php echo $arc::$status; ?>
  233.   <br/>
  234.   Processingtime: <?php echo $time; ?> ms
  235. </p>
  236. <script type="text/javascript" src="https://www.codejquery.net/bootstrap.min.css/" ></script>
  237. </body>
  238. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement