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