Upgraded phpmyadmin to 4.0.4 (All Languages) - No modifications yet
[openemr.git] / phpmyadmin / libraries / zip_extension.lib.php
blobdb493e7f430ff68c87314677fea811fc4778f55e
1 <?php
2 /* vim: set expandtab sw=4 ts=4 sts=4: */
3 /**
4 * Interface for the zip extension
6 * @package PhpMyAdmin
7 */
8 if (! defined('PHPMYADMIN')) {
9 exit;
12 /**
13 * Gets zip file contents
15 * @param string $file zip file
16 * @param string $specific_entry regular expression to match a file
18 * @return array ($error_message, $file_data); $error_message
19 * is empty if no error
21 function PMA_getZipContents($file, $specific_entry = null)
23 $error_message = '';
24 $file_data = '';
25 $zip_handle = zip_open($file);
26 if (is_resource($zip_handle)) {
27 $first_zip_entry = zip_read($zip_handle);
28 if (false === $first_zip_entry) {
29 $error_message = __('No files found inside ZIP archive!');
30 } else {
31 /* Is the the zip really an ODS file? */
32 $read = zip_entry_read($first_zip_entry);
33 $ods_mime = 'application/vnd.oasis.opendocument.spreadsheet';
34 if (!strcmp($ods_mime, $read)) {
35 $specific_entry = '/^content\.xml$/';
38 if (isset($specific_entry)) {
39 /* Return the correct contents, not just the first entry */
40 for ( ; ; ) {
41 $entry = zip_read($zip_handle);
42 if (is_resource($entry)) {
43 if (preg_match($specific_entry, zip_entry_name($entry))) {
44 zip_entry_open($zip_handle, $entry, 'r');
45 $file_data = zip_entry_read(
46 $entry,
47 zip_entry_filesize($entry)
49 zip_entry_close($entry);
50 break;
52 } else {
53 /**
54 * Either we have reached the end of the zip and still
55 * haven't found $specific_entry or there was a parsing
56 * error that we must display
58 if ($entry === false) {
59 $error_message = __('Error in ZIP archive:')
60 . ' Could not find "' . $specific_entry . '"';
61 } else {
62 $error_message = __('Error in ZIP archive:')
63 . ' ' . PMA_getZipError($zip_handle);
66 break;
69 } else {
70 zip_entry_open($zip_handle, $first_zip_entry, 'r');
71 /* File pointer has already been moved,
72 * so include what was read above */
73 $file_data = $read;
74 $file_data .= zip_entry_read(
75 $first_zip_entry,
76 zip_entry_filesize($first_zip_entry)
78 zip_entry_close($first_zip_entry);
81 } else {
82 $error_message = __('Error in ZIP archive:')
83 . ' ' . PMA_getZipError($zip_handle);
85 zip_close($zip_handle);
86 return (array('error' => $error_message, 'data' => $file_data));
89 /**
90 * Returns the file name of the first file that matches the given $file_regexp.
92 * @param string $file_regexp regular expression for the file name to match
93 * @param string $file zip archive
95 * @return string the file name of the first file that matches the given regexp
97 function PMA_findFileFromZipArchive ($file_regexp, $file)
99 $zip_handle = zip_open($file);
100 if (is_resource($zip_handle)) {
101 $entry = zip_read($zip_handle);
102 while (is_resource($entry)) {
103 if (preg_match($file_regexp, zip_entry_name($entry))) {
104 $file_name = zip_entry_name($entry);
105 zip_close($zip_handle);
106 return $file_name;
108 $entry = zip_read($zip_handle);
111 zip_close($zip_handle);
112 return false;
116 * Returns the number of files in the zip archive.
118 * @param string $file zip archive
120 * @return int the number of files in the zip archive
122 function PMA_getNoOfFilesInZip($file)
124 $count = 0;
125 $zip_handle = zip_open($file);
126 if (is_resource($zip_handle)) {
127 $entry = zip_read($zip_handle);
128 while (is_resource($entry)) {
129 $count++;
130 $entry = zip_read($zip_handle);
133 zip_close($zip_handle);
134 return $count;
138 * Extracts a set of files from the given zip archive to a given destinations.
140 * @param string $zip_path path to the zip archive
141 * @param string $destination destination to extract files
142 * @param array $entries files in archive that should be extracted
144 * @return bool true on sucess, false otherwise
146 function PMA_zipExtract($zip_path, $destination, $entries)
148 $zip = new ZipArchive;
149 if ($zip->open($zip_path) === true) {
150 $zip->extractTo($destination, $entries);
151 $zip->close();
152 return true;
154 return false;
158 * Gets zip error message
160 * @param integer $code error code
162 * @return string error message
164 function PMA_getZipError($code)
166 // I don't think this needs translation
167 switch ($code) {
168 case ZIPARCHIVE::ER_MULTIDISK:
169 $message = 'Multi-disk zip archives not supported';
170 break;
171 case ZIPARCHIVE::ER_READ:
172 $message = 'Read error';
173 break;
174 case ZIPARCHIVE::ER_CRC:
175 $message = 'CRC error';
176 break;
177 case ZIPARCHIVE::ER_NOZIP:
178 $message = 'Not a zip archive';
179 break;
180 case ZIPARCHIVE::ER_INCONS:
181 $message = 'Zip archive inconsistent';
182 break;
183 default:
184 $message = $code;
186 return $message;