<?php

/**
 * Goweb Sitemanager 5 Framework
 * 
 * Classe de manipulação de ficheiros ZIP
 *
 * @version    5.0.0
 * @category   Goweb
 * @package    Goweb_Zip
 * @author     I&D <dev@goweb.pt>, Pedro Pinto <ppinto@goweb.pt>
 * @copyright  2009 (c) Goweb (http://www.goweb.pt)
 */

/**
 *
 * @version    5.0.0
 * @category   Goweb
 * @package    Goweb_Zip
 * @author     I&D <dev@goweb.pt>, Pedro Pinto <ppinto@goweb.pt>
 * @copyright  2009 (c) Goweb (http://www.goweb.pt)
 * @todo       create() : Creates the Zip archive
 */
class Goweb_Zip {
    
    /**
     * Nome do ficheiro zip
     *
     * @var String
     */
    protected $_zip;
    
    /**
     * Nome do Directorio onde se encontra o ficheiro ZIP
     *
     * @var String
     */
    protected $_zipDir;
    
    /**
     * Constructor
     *
     * @param String $filename
     */
    public function __construct($filename){
        $this->_zip = $filename;
        $this->_zipDir = dirname($filename);
        if (substr($this->_zipDir, -1) != '/' && substr($this->_zipDir, -1) != DIRECTORY_SEPARATOR) $this->_zipDir .= '/';
    }
    
    /**
     * Singleton instance
     *
     * @param String $filename
     * @return PclZip|Goweb_Zip
     */
    public static function getInstance($filename){
        if (!extension_loaded('zip')){
            require_once LIBRARY_PATH . '/External/pclzip/pclzip.lib.php';
            return new PclZip($filename);
        }
        return new self($filename);
    }
    
    /**
     * List of the files and directories in the zip archive
     *
     * @return array
     */
    public function listContent(){
        $zip = zip_open($this->_zip);
        if (is_resource($zip)) {
            $list = array();
            while ($zip_entry = zip_read($zip)) {
              $list[] = array('filename' => zip_entry_name($zip_entry),
              'size' => zip_entry_filesize($zip_entry),
              'compressed_size' => zip_entry_compressedsize($zip_entry));
            }
            zip_close($zip);
            return $list;
        }
    }

    /**
     * extract all the files / directories from the archive to the specified folder
     * baseado em http://pt.php.net/manual/en/function.zip-open.php#85585
     *
     * @param String $destination
     * @return array
     */
    public function extract($destination = ''){
        $zip = zip_open($this->_zip);
        if (substr($destination, -1) != '/' && substr($destination, -1) != DIRECTORY_SEPARATOR) $destination .= '/';
        if (is_resource($zip)){
            while ($zip_entry = zip_read($zip)){
                $completePath = $destination . dirname(zip_entry_name($zip_entry));
                $completeName = $destination . zip_entry_name($zip_entry);

                if(!file_exists($completePath)){
                    $tmp = '';
                    foreach(explode('/',$completePath) AS $k){
                        $tmp .= $k.'/';
                        if(!file_exists($tmp) ){
                            @mkdir($tmp, 0777);
                            @chmod($tmp, 0777);
                        }
                    }
                }
                if (substr($completeName, -1) != '/' && substr($completeName, -1) != DIRECTORY_SEPARATOR){
                    if (zip_entry_open($zip, $zip_entry, "r")){
                            if ($fd = @fopen($completeName, 'w+')){
                                fwrite($fd, zip_entry_read($zip_entry, zip_entry_filesize($zip_entry)));
                                fclose($fd);
                            } else {
                                // We think this was an empty directory
                                @mkdir($completeName, 0777);
                            }
                            @chmod($completeName, 0777);
                            zip_entry_close($zip_entry);
                    }
                }
            }
            zip_close($zip);
            return $this->listContent();
        }
    }
    
}