1: <?php
2:
3: /**
4: * This file contains the cZipArchive util class.
5: *
6: * @package Core
7: * @subpackage Util
8: * @author claus.schunk@4fb.de
9: * @copyright four for business AG <www.4fb.de>
10: * @license http://www.contenido.org/license/LIZENZ.txt
11: * @link http://www.4fb.de
12: * @link http://www.contenido.org
13: */
14: defined('CON_FRAMEWORK') || die('Illegal call: Missing framework initialization - request aborted.');
15:
16: /**
17: * This class contains the functionalities to handle zip archives.
18: *
19: * @author claus.schunk@4fb.de
20: */
21: class cZipArchive {
22: /**
23: * Read all files from given path excluding files which names start with a
24: * dot or are not valid according to CONTENIDO standards (validateFilename()).
25: *
26: * @see cFileHandler::validateFilename()
27: *
28: * @param string $dirPath
29: *
30: * @return array
31: * of files
32: *
33: * @throws cInvalidArgumentException
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: * @throws cInvalidArgumentException
94: */
95: public static function extractOverRide($file, $extractPath, $extractPathUserInput = NULL) {
96:
97: // validate user input
98: if (isset($extractPathUserInput)) {
99: $extractPath .= uplCreateFriendlyName($extractPathUserInput);
100: }
101:
102: $zip = new ZipArchive();
103:
104: // try to open archive
105: if (!$zip->open($file)) {
106: echo ('can not open zip file!');
107: return;
108: }
109:
110: for ($i = 0; $i < $zip->numFiles; $i++) {
111: $file = $zip->getNameIndex($i);
112: // remove '/' for validation -> directory names
113: $tmpFile = str_replace('/', '', $file);
114: // extract only file with valid filename
115: if (cFileHandler::validateFilename($tmpFile, FALSE) && (cString::getPartOfString($tmpFile, 0, 1) != '.') && (cString::getPartOfString($tmpFile, 0, 1) != '_')) {
116: $zip->extractTo($extractPath, $file);
117: }
118: }
119:
120: $zip->close();
121: }
122:
123: /**
124: * This function contains the functionality to extract archive.
125: *
126: * @param string $file
127: * zip file
128: * @param string $extractPath
129: * extraction path
130: * @param string $extractPathUserInput [optional]
131: * user specified extraction path
132: *
133: * @throws cInvalidArgumentException
134: */
135: public static function extract($file, $extractPath, $extractPathUserInput = NULL) {
136: if (isset($extractPathUserInput)) {
137:
138: // validate user input
139: $extractPath .= uplCreateFriendlyName($extractPathUserInput);
140: }
141:
142: if (file_exists($extractPath) && is_dir($extractPath)) {
143: $ar = cZipArchive::readExistingFiles($extractPath);
144: }
145:
146: $zip = new ZipArchive();
147:
148: // try to open archive
149: if (!$zip->open($file)) {
150: echo ('can not open zip file!');
151: return;
152: }
153:
154: // check if directory already exist
155: if (cZipArchive::isExtracted($extractPath)) {
156: for ($i = 0; $i < $zip->numFiles; $i++) {
157: $file = $zip->getNameIndex($i);
158: $tmpFile = str_replace('/', '', $file);
159: if (cFileHandler::validateFilename($tmpFile, FALSE) && (cString::getPartOfString($tmpFile, 0, 1) != '.') && (cString::getPartOfString($tmpFile, 0, 1) != '_')) {
160: if (!file_exists($extractPath . '/' . $file)) {
161: $zip->extractTo($extractPath, $file);
162: }
163: }
164: }
165: } else {
166: for ($i = 0; $i < $zip->numFiles; $i++) {
167: $file = $zip->getNameIndex($i);
168: // remove '/' for validation -> directory names
169: $tmpFile = str_replace('/', '', $file);
170: if (cFileHandler::validateFilename($tmpFile, FALSE) && (cString::getPartOfString($tmpFile, 0, 1) != '.') && (cString::getPartOfString($tmpFile, 0, 1) != '_')) {
171: $zip->extractTo($extractPath, $file);
172: }
173: }
174: }
175:
176: $zip->close();
177: }
178:
179: /**
180: * This function contains the functionality to create archives.
181: *
182: * @param string $zipFilePath
183: * file path
184: * @param string $dirPath
185: * directory path
186: * @param array $filePathes
187: * files to store in archive
188: */
189: public static function createZip($zipFilePath, $dirPath, array $filePathes) {
190: $zip = new ZipArchive();
191: if ($zip->open($dirPath . $zipFilePath, ZipArchive::CREATE) == TRUE) {
192: foreach ($filePathes as $key => $file) {
193: $zip->addFile($dirPath . $file, $file);
194: }
195: $zip->close();
196: }
197: }
198: }
199: