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: * @param string $dirPath
31: * @return array of files
32: * @see cFileHandler::validateFilename()
33: */
34: public static function readExistingFiles($dirPath) {
35:
36: // check if $dirPath is a dir
37: if (!is_dir($dirPath)) {
38: return array();
39: }
40:
41: // try to open $dirPath
42: if (false === $handle = opendir($dirPath)) {
43: return array();
44: }
45:
46: // gather all files in $dirPath that are "valid" filenames
47: $array = array();
48: while (false !== $file = readdir($handle)) {
49: if ('.' === $file[0]) {
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: // close dir handle
63: closedir($handle);
64:
65: // return array of files
66: return $array;
67: }
68:
69: /**
70: * This function checks if the given path already exists.
71: *
72: * @param string $dirPath
73: * @return boolean
74: */
75: public static function isExtracted($dirPath) {
76: if (!file_exists($dirPath)) {
77: return false;
78: } else if (!is_dir($dirPath)) {
79: return false;
80: } else {
81: return true;
82: }
83: }
84:
85: /**
86: * This function contains the functionality to extract archive and overwrite
87: * existing files.
88: *
89: * @param string $file zip file
90: * @param string $extractPath extraction path
91: * @param string $extractPathUserInput 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 zip file
125: * @param string $extractPath extraction path
126: * @param string $extractPathUserInput user specified extraction path
127: */
128: public static function extract($file, $extractPath, $extractPathUserInput = NULL) {
129: if (isset($extractPathUserInput)) {
130:
131: // validate user input
132: $extractPath .= uplCreateFriendlyName($extractPathUserInput);
133: }
134:
135: if (file_exists($extractPath) and is_dir($extractPath)) {
136: $ar = cZipArchive::readExistingFiles($extractPath);
137: }
138: $zip = new ZipArchive();
139:
140: // try to open archive
141: if (!$zip->open($file)) {
142: echo ('can not open zip file!');
143: return;
144: }
145:
146: // check if directory already exist
147: if (cZipArchive::isExtracted($extractPath)) {
148: for ($i = 0; $i < $zip->numFiles; $i++) {
149: $file = $zip->getNameIndex($i);
150: $tmpFile = str_replace('/', '', $file);
151: if (cFileHandler::validateFilename($tmpFile, FALSE) && (substr($tmpFile, 0, 1) != '.') && (substr($tmpFile, 0, 1) != '_')) {
152: if (!file_exists($extractPath . '/' . $file)) {
153: $zip->extractTo($extractPath, $file);
154: }
155: }
156: }
157: } else {
158: for ($i = 0; $i < $zip->numFiles; $i++) {
159: $file = $zip->getNameIndex($i);
160: // remove '/' for validation -> directory names
161: $tmpFile = str_replace('/', '', $file);
162: if (cFileHandler::validateFilename($tmpFile, FALSE) && (substr($tmpFile, 0, 1) != '.') && (substr($tmpFile, 0, 1) != '_')) {
163: $zip->extractTo($extractPath, $file);
164: }
165: }
166: }
167: $zip->close();
168: }
169:
170: /**
171: * This function contains the functionality to create archives.
172: *
173: * @param string $zipFilePath file path
174: * @param string $dirPath directory path
175: * @param array $filePathes files to store in archive
176: */
177: public static function createZip($zipFilePath, $dirPath, array $filePathes) {
178: $zip = new ZipArchive();
179: if ($zip->open($dirPath . $zipFilePath, ZipArchive::CREATE) == TRUE) {
180: foreach ($filePathes as $key => $file) {
181: $zip->addFile($dirPath . $file, $file);
182: }
183: $zip->close();
184: }
185: }
186: }
187: