Projet

Général

Profil

Paste
Statistiques
| Branche: | Révision:

ryxeo-glpi-git / htdocs / epack / lib / zip / PclZipProxy.php @ 1c14bcc4

Historique | Voir | Annoter | Télécharger (5,01 ko)

1 1c14bcc4 Eric Seigne
<?php
2
require_once 'pclzip/pclzip.lib.php';
3
require_once 'ZipInterface.php';
4
class PclZipProxyException extends Exception
5
{ }
6
/**
7
 * Proxy class for the PclZip library
8
 * You need PHP 5.2 at least
9
 * You need Zip Extension or PclZip library
10
 * Encoding : ISO-8859-1
11
 * Last commit by $Author: neveldo $
12
 * Date - $Date: 2009-05-29 10:05:11 +0200 (ven., 29 mai 2009) $
13
 * SVN Revision - $Rev: 28 $
14
 * Id : $Id: odf.php 28 2009-05-29 08:05:11Z neveldo $
15
 *
16
 * @copyright  GPL License 2008 - Julien Pauli - Cyril PIERRE de GEYER - Anaska (http://www.anaska.com)
17
 * @license    http://www.gnu.org/copyleft/gpl.html  GPL License
18
 * @version 1.3
19
 */
20
class PclZipProxy implements ZipInterface
21
{
22
        const TMP_DIR = './tmp';
23
        protected $openned = false;
24
        protected $filename;
25
        protected $pclzip;
26
    /**
27
     * Constructeur de classe
28
     *
29
     * @throws PclZipProxyException
30
     */
31
        public function __construct()
32
        {
33
                if (! class_exists('PclZip')) {
34
                        throw new PclZipProxyException('PclZip class not loaded - PclZip library
35
                         is required for using PclZipProxy'); ;
36
                }
37
        }        
38
        /**
39
         * Ouvrir une archive au format Zip
40
         * 
41
         * @param string $filename le nom de l'archive à ouvrir
42
         * @return true si l'ouverture à réussi
43
         */
44
        public function open($filename)
45
        {
46
                if (true === $this->openned) {
47
                        $this->close();
48
                }                
49
                if (!file_exists(self::TMP_DIR)) {
50
                        mkdir(self::TMP_DIR);
51
                }
52
                $this->filename = $filename;
53
                $this->pclzip = new PclZip($this->filename);
54
                $this->openned = true;
55
                return true;
56
        }
57
        /**
58
         * Récupérer le contenu d'un fichier de l'archive à partir de son nom
59
         * 
60
         * @param string $name le nom du fichier à extraire
61
         * @return le contenu du fichier dans une chaine de caractères
62
         */
63
        public function getFromName($name)
64
        {
65
                if (false === $this->openned) {
66
                        return false;
67
                }
68
                $name = preg_replace("/(?:\.|\/)*(.*)/", "\\1", $name);
69
                $extraction = $this->pclzip->extract(PCLZIP_OPT_BY_NAME, $name, 
70
                        PCLZIP_OPT_EXTRACT_AS_STRING);
71
                if (!empty($extraction)) {
72
                        return $extraction[0]['content'];
73
                } 
74
                return false;
75
        }
76
        /**
77
         * Ajouter un fichier à l'archive à partir d'une chaine de caractères
78
         * 
79
         * @param string $localname le chemin local du fichier dans l'archive
80
         * @param string $contents le contenu du fichier
81
         * @return true si le fichier a été ajouté avec succès
82
         */
83
        public function addFromString($localname, $contents)
84
        {
85
                if (false === $this->openned) {
86
                        return false;
87
                }
88
                if (file_exists($this->filename) && !is_writable($this->filename)) {
89
                        return false;
90
                }
91
                $localname = preg_replace("/(?:\.|\/)*(.*)/", "\\1", $localname);
92
                $localpath = dirname($localname);
93
                $tmpfilename = self::TMP_DIR . '/' . basename($localname);
94
                if (false !== file_put_contents($tmpfilename, $contents)) {
95
                        $this->pclzip->delete(PCLZIP_OPT_BY_NAME, $localname);
96
                        $add = $this->pclzip->add($tmpfilename,
97
                                PCLZIP_OPT_REMOVE_PATH, self::TMP_DIR,
98
                                PCLZIP_OPT_ADD_PATH, $localpath);
99
                        unlink($tmpfilename);
100
                        if (!empty($add)) {
101
                                return true;
102
                        } 
103
                }
104
                return false;
105
        }
106
        /**
107
         * Ajouter un fichier à l'archive à partir d'un fichier
108
         * 
109
         * @param string $filename le chemin vers le fichier à ajouter
110
         * @param string $localname le chemin local du fichier dans l'archive
111
         * @return true si le fichier a été ajouté avec succès
112
         */
113
        public function addFile($filename, $localname = null)
114
        {
115
                if (false === $this->openned) {
116
                        return false;
117
                }
118
                if (file_exists($this->filename) && !is_writable($this->filename)) {
119
                        return false;
120
                }                
121
                if (isSet($localname)) {
122
                        $localname = preg_replace("/(?:\.|\/)*(.*)/", "\\1", $localname);
123
                        $localpath = dirname($localname);
124
                        $tmpfilename = self::TMP_DIR . '/' . basename($localname);
125
                } else {
126
                        $localname = basename($filename);
127
                        $tmpfilename = self::TMP_DIR . '/' . $localname;
128
                        $localpath = '';
129
                }
130
                if (file_exists($filename)) {
131
                        copy($filename, $tmpfilename);
132
                        $this->pclzip->delete(PCLZIP_OPT_BY_NAME, $localname);
133
                        $this->pclzip->add($tmpfilename,
134
                                PCLZIP_OPT_REMOVE_PATH, self::TMP_DIR,
135
                                PCLZIP_OPT_ADD_PATH, $localpath);
136
                        unlink($tmpfilename);
137
                        return true;
138
                }
139
                return false;
140
        }
141
        /**
142
         * ferme l'archive Zip
143
         * @return true
144
         */
145
        public function close()
146
        {
147
                if (false === $this->openned) {
148
                        return false;
149
                }                
150
                $this->pclzip = $this->filename = null;
151
                $this->openned = false;
152
                if (file_exists(self::TMP_DIR)) {
153
                        $this->_rrmdir(self::TMP_DIR);
154
                        rmdir(self::TMP_DIR);
155
                }
156
                return true;
157
        }
158
        /**
159
         * Vide le répertoire temporaire de travail récursivement
160
         * @param $dir le répertoire temporaire de travail
161
         * @return void
162
         */
163
        private function _rrmdir($dir)
164
        {
165
                if ($handle = opendir($dir)) { 
166
                        while (false !== ($file = readdir($handle))) { 
167
                                if ($file != '.' && $file != '..') {
168
                                        if (is_dir($dir . '/' . $file)) {
169
                                                $this->_rrmdir($dir . '/' . $file);
170
                                                rmdir($dir . '/' . $file);
171
                                        } else {
172
                                                unlink($dir . '/' . $file);
173
                                        }
174
                                } 
175
                        } 
176
                        closedir($handle); 
177
                } 
178
        }                
179
}
180
181
?>
Redmine Appliance - Powered by TurnKey Linux