1: <?php
2:
3: /**
4: * This file contains the cZipArchive util class.
5: *
6: * @package Core
7: * @subpackage Util
8: * @version SVN Revision $Rev:$
9: *
10: * @author claus.schunk@4fb.de
11: * @copyright four for business AG <www.4fb.de>
12: * @license http://www.contenido.org/license/LIZENZ.txt
13: * @link http://www.4fb.de
14: * @link http://www.contenido.org
15: */
16: defined('CON_FRAMEWORK') || die('Illegal call: Missing framework initialization - request aborted.');
17:
18: /**
19: * This class contains the functionalities to handle zip archives.
20: *
21: * @author claus.schunk@4fb.de
22: */
23: class cZipArchive {
24:
25: /**
26: * Read all files from given path excluding files which names start with a
27: * dot or are not valid according to CONTENIDO standards
28: * (validateFilename()).
29: *
30: * @see cFileHandler::validateFilename()
31: * @param string $dirPath
32: * @return array
33: * of files
34: */
35: public static function readExistingFiles($dirPath) {
36:
37: // check if $dirPath is a dir
38: if (!is_dir($dirPath)) {
39: return array();
40: }
41:
42: // try to read $dirPath
43: if (false === ($handle = cDirHandler::read($dirPath))) {
44: return array();
45: }
46:
47: $array = array();
48: foreach ($handle as $file) {
49: if (cFileHandler::fileNameBeginsWithDot($file)) {
50: // exclude file if name starts with a dot
51: // hotfix : fileHandler returns filename '.' als valid filename
52: continue;
53: } else if (!cFileHandler::validateFilename($file, false)) {
54: // exclude file if name is not valid according to CONTENIDO
55: // standards
56: continue;
57: } else {
58: $array[] = $file;
59: }
60: }
61:
62: // return array of files
63: return $array;
64: }
65:
66: /**
67: * This function checks if the given path already exists.
68: *
69: * @param string $dirPath
70: * @return bool
71: */
72: public static function isExtracted($dirPath) {
73: if (!file_exists($dirPath)) {
74: return false;
75: } else if (!is_dir($dirPath)) {
76: return false;
77: } else {
78: return true;
79: }
80: }
81:
82: /**
83: * This function contains the functionality to extract archive and overwrite
84: * existing files.
85: *
86: * @param string $file
87: * zip file
88: * @param string $extractPath
89: * extraction path
90: * @param string $extractPathUserInput [optional]
91: * user specified extraction path
92: */
93: public static function extractOverRide($file, $extractPath, $extractPathUserInput = NULL) {
94:
95: // validate user input
96: if (isset($extractPathUserInput)) {
97: $extractPath .= uplCreateFriendlyName($extractPathUserInput);
98: }
99:
100: $zip = new ZipArchive();
101:
102: // try to open archive
103: if (!$zip->open($file)) {
104: echo ('can not open zip file!');
105: return;
106: }
107:
108: for ($i = 0; $i < $zip->numFiles; $i++) {
109: $file = $zip->getNameIndex($i);
110: // remove '/' for validation -> directory names
111: $tmpFile = str_replace('/', '', $file);
112: // extract only file with valid filename
113: if (cFileHandler::validateFilename($tmpFile, FALSE) && (substr($tmpFile, 0, 1) != '.') && (substr($tmpFile, 0, 1) != '_')) {
114: $zip->extractTo($extractPath, $file);
115: }
116: }
117:
118: $zip->close();
119: }
120:
121: /**
122: * This function contains the functionality to extract archive.
123: *
124: * @param string $file
125: * zip file
126: * @param string $extractPath
127: * extraction path
128: * @param string $extractPathUserInput [optional]
129: * user specified extraction path
130: */
131: public static function extract($file, $extractPath, $extractPathUserInput = NULL) {
132: if (isset($extractPathUserInput)) {
133:
134: // validate user input
135: $extractPath .= uplCreateFriendlyName($extractPathUserInput);
136: }
137:
138: if (file_exists($extractPath) and is_dir($extractPath)) {
139: $ar = cZipArchive::readExistingFiles($extractPath);
140: }
141: $zip = new ZipArchive();
142:
143: // try to open archive
144: if (!$zip->open($file)) {
145: echo ('can not open zip file!');
146: return;
147: }
148:
149: // check if directory already exist
150: if (cZipArchive::isExtracted($extractPath)) {
151: for ($i = 0; $i < $zip->numFiles; $i++) {
152: $file = $zip->getNameIndex($i);
153: $tmpFile = str_replace('/', '', $file);
154: if (cFileHandler::validateFilename($tmpFile, FALSE) && (substr($tmpFile, 0, 1) != '.') && (substr($tmpFile, 0, 1) != '_')) {
155: if (!file_exists($extractPath . '/' . $file)) {
156: $zip->extractTo($extractPath, $file);
157: }
158: }
159: }
160: } else {
161: for ($i = 0; $i < $zip->numFiles; $i++) {
162: $file = $zip->getNameIndex($i);
163: // remove '/' for validation -> directory names
164: $tmpFile = str_replace('/', '', $file);
165: if (cFileHandler::validateFilename($tmpFile, FALSE) && (substr($tmpFile, 0, 1) != '.') && (substr($tmpFile, 0, 1) != '_')) {
166: $zip->extractTo($extractPath, $file);
167: }
168: }
169: }
170: $zip->close();
171: }
172:
173: /**
174: * This function contains the functionality to create archives.
175: *
176: * @param string $zipFilePath
177: * file path
178: * @param string $dirPath
179: * directory path
180: * @param array $filePathes
181: * files to store in archive
182: */
183: public static function createZip($zipFilePath, $dirPath, array $filePathes) {
184: $zip = new ZipArchive();
185: if ($zip->open($dirPath . $zipFilePath, ZipArchive::CREATE) == TRUE) {
186: foreach ($filePathes as $key => $file) {
187: $zip->addFile($dirPath . $file, $file);
188: }
189: $zip->close();
190: }
191: }
192: }
193: