| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  * Pickle | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  * @license | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  * New BSD License | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  * Copyright © 2015-2015, Pickle community. All rights reserved. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  * Redistribution and use in source and binary forms, with or without | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |  * modification, are permitted provided that the following conditions are met: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |  *     * Redistributions of source code must retain the above copyright | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |  *       notice, this list of conditions and the following disclaimer. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |  *     * Redistributions in binary form must reproduce the above copyright | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |  *       notice, this list of conditions and the following disclaimer in the | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |  *       documentation and/or other materials provided with the distribution. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |  *     * Neither the name of the Hoa nor the names of its contributors may be | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |  *       used to endorse or promote products derived from this software without | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |  *       specific prior written permission. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS AND CONTRIBUTORS BE | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |  * POSSIBILITY OF SUCH DAMAGE. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  | namespace Pickle\Base\Archive; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  | use Pickle\Base\Interfaces; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  | use RuntimeException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  | use ZipArchive; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  | class PHPZipper extends PHP implements Interfaces\Archive\Zipper | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |      * {@inheritdoc} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |      * @see \Pickle\Base\Interfaces\Archive\Zipper::__construct() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |     public function __construct(string $path, int $flags) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |         parent::__construct(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |         switch ($flags) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |             case self::FLAG_OPEN: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |                 $this->open($path); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |                 break; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |             case self::FLAG_CREATE: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |                 $this->create($path, false); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |                 break; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |             case self::FLAG_CREATE_OVERWRITE: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |                 $this->create($path, true); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |                 break; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |             default: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |                 throw new RuntimeException('Invalid value of $flags in ' . __METHOD__); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |      * {@inheritdoc} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |      * @see \Pickle\Base\Interfaces\Archive\Zipper::addFromString($localname, $contents) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |     public function addFromString(string $localname, string $contents): void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |         $localname = ltrim(str_replace(DIRECTORY_SEPARATOR, '/', $localname)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |         if ($this->zipArchive->addFromString($localname, $contents) !== true) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |             $error = "Failed to add a file to the ZIP archive starting from its contents"; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |             $details = (string) $this->zipArchive->getStatusString(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |             if ($details !== '') { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |                 $error .= ": {$details}"; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |             throw new RuntimeException($error); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |      * {@inheritdoc} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |      * @see \Pickle\Base\Interfaces\Archive\Zipper::addFileWithoutPath() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |     public function addFileWithoutPath(string $path): void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |         if (!is_file($path)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |             throw new RuntimeException("Failed to find the file {$path}"); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |         if (!is_readable($path)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |             throw new RuntimeException("The file {$path} is not readable"); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |         if ($this->zipArchive->addFile($path, basename($path)) !== true) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |             $error = "Failed to add the file {$path} to the ZIP archive"; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |             $details = (string) $this->zipArchive->getStatusString(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |             if ($details !== '') { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |                 $error .= ": {$details}"; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |             throw new RuntimeException($error); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |      * {@inheritdoc} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |      * @see \Pickle\Base\Interfaces\Archive\Zipper::__destruct() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |     public function __destruct() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |         parent::__destruct(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |      * @throws \RuntimeException | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 121 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 122 |  |  |     private function create(string $path, bool $overwrite): void | 
            
                                                                        
                            
            
                                    
            
            
                | 123 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 124 |  |  |         $flags = ZipArchive::CREATE; | 
            
                                                                        
                            
            
                                    
            
            
                | 125 |  |  |         if ($overwrite === false) { | 
            
                                                                        
                            
            
                                    
            
            
                | 126 |  |  |             if (file_exists($path)) { | 
            
                                                                        
                            
            
                                    
            
            
                | 127 |  |  |                 throw new RuntimeException("The ZIP archive {$path} already exists"); | 
            
                                                                        
                            
            
                                    
            
            
                | 128 |  |  |             } | 
            
                                                                        
                            
            
                                    
            
            
                | 129 |  |  |         } else { | 
            
                                                                        
                            
            
                                    
            
            
                | 130 |  |  |             $flags |= ZipArchive::OVERWRITE; | 
            
                                                                        
                            
            
                                    
            
            
                | 131 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 132 |  |  |         if ($this->zipArchive->open($path, $flags) !== true) { | 
            
                                                                        
                            
            
                                    
            
            
                | 133 |  |  |             $error = "Failed to create the ZIP archive {$path}"; | 
            
                                                                        
                            
            
                                    
            
            
                | 134 |  |  |             $details = (string) $this->zipArchive->getStatusString(); | 
            
                                                                        
                            
            
                                    
            
            
                | 135 |  |  |             if ($details !== '') { | 
            
                                                                        
                            
            
                                    
            
            
                | 136 |  |  |                 $error .= ": {$details}"; | 
            
                                                                        
                            
            
                                    
            
            
                | 137 |  |  |             } | 
            
                                                                        
                            
            
                                    
            
            
                | 138 |  |  |             throw new RuntimeException($error); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 140 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 141 |  |  | } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 142 |  |  |  | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 143 |  |  | /* vim: set tabstop=4 shiftwidth=4 expandtab: fdm=marker */ | 
            
                                                        
            
                                    
            
            
                | 144 |  |  |  |