Removing old documentation
[openemr.git] / phpmyadmin / libraries / zip_extension.lib.php
blobc14a8a05f1504fe1ed911701f687c70b5a1c32c5
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 $error_message = __('Error in ZIP archive:')
28 . ' ' . PMA_getZipError($zip_handle);
29 return (array('error' => $error_message, 'data' => $file_data));
32 $first_zip_entry = zip_read($zip_handle);
33 if (false === $first_zip_entry) {
34 $error_message = __('No files found inside ZIP archive!');
35 zip_close($zip_handle);
36 return (array('error' => $error_message, 'data' => $file_data));
39 /* Is the the zip really an ODS file? */
40 $read = zip_entry_read($first_zip_entry);
41 $ods_mime = 'application/vnd.oasis.opendocument.spreadsheet';
42 if (!strcmp($ods_mime, $read)) {
43 $specific_entry = '/^content\.xml$/';
46 if (!isset($specific_entry)) {
47 zip_entry_open($zip_handle, $first_zip_entry, 'r');
48 /* File pointer has already been moved,
49 * so include what was read above */
50 $file_data = $read;
51 $file_data .= zip_entry_read(
52 $first_zip_entry,
53 zip_entry_filesize($first_zip_entry)
55 zip_entry_close($first_zip_entry);
56 zip_close($zip_handle);
57 return (array('error' => $error_message, 'data' => $file_data));
60 /* Return the correct contents, not just the first entry */
61 for ( ; ; ) {
62 $entry = zip_read($zip_handle);
63 if (is_resource($entry)) {
64 if (preg_match($specific_entry, zip_entry_name($entry))) {
65 zip_entry_open($zip_handle, $entry, 'r');
66 $file_data = zip_entry_read(
67 $entry,
68 zip_entry_filesize($entry)
70 zip_entry_close($entry);
71 break;
73 } else {
74 /**
75 * Either we have reached the end of the zip and still
76 * haven't found $specific_entry or there was a parsing
77 * error that we must display
79 if ($entry === false) {
80 $error_message = __('Error in ZIP archive:')
81 . ' Could not find "' . $specific_entry . '"';
82 } else {
83 $error_message = __('Error in ZIP archive:')
84 . ' ' . PMA_getZipError($zip_handle);
87 break;
91 zip_close($zip_handle);
92 return (array('error' => $error_message, 'data' => $file_data));
95 /**
96 * Returns the file name of the first file that matches the given $file_regexp.
98 * @param string $file_regexp regular expression for the file name to match
99 * @param string $file zip archive
101 * @return string the file name of the first file that matches the given regexp
103 function PMA_findFileFromZipArchive($file_regexp, $file)
105 $zip_handle = zip_open($file);
106 if (is_resource($zip_handle)) {
107 $entry = zip_read($zip_handle);
108 while (is_resource($entry)) {
109 if (preg_match($file_regexp, zip_entry_name($entry))) {
110 $file_name = zip_entry_name($entry);
111 zip_close($zip_handle);
112 return $file_name;
114 $entry = zip_read($zip_handle);
117 zip_close($zip_handle);
118 return false;
122 * Returns the number of files in the zip archive.
124 * @param string $file zip archive
126 * @return int the number of files in the zip archive
128 function PMA_getNoOfFilesInZip($file)
130 $count = 0;
131 $zip_handle = zip_open($file);
132 if (is_resource($zip_handle)) {
133 $entry = zip_read($zip_handle);
134 while (is_resource($entry)) {
135 $count++;
136 $entry = zip_read($zip_handle);
139 zip_close($zip_handle);
140 return $count;
144 * Extracts a set of files from the given zip archive to a given destinations.
146 * @param string $zip_path path to the zip archive
147 * @param string $destination destination to extract files
148 * @param array $entries files in archive that should be extracted
150 * @return bool true on success, false otherwise
152 function PMA_zipExtract($zip_path, $destination, $entries)
154 $zip = new ZipArchive;
155 if ($zip->open($zip_path) === true) {
156 $zip->extractTo($destination, $entries);
157 $zip->close();
158 return true;
160 return false;
164 * Gets zip error message
166 * @param resource $code error code
168 * @return string error message
170 function PMA_getZipError($code)
172 // I don't think this needs translation
173 switch ($code) {
174 case ZIPARCHIVE::ER_MULTIDISK:
175 $message = 'Multi-disk zip archives not supported';
176 break;
177 case ZIPARCHIVE::ER_READ:
178 $message = 'Read error';
179 break;
180 case ZIPARCHIVE::ER_CRC:
181 $message = 'CRC error';
182 break;
183 case ZIPARCHIVE::ER_NOZIP:
184 $message = 'Not a zip archive';
185 break;
186 case ZIPARCHIVE::ER_INCONS:
187 $message = 'Zip archive inconsistent';
188 break;
189 default:
190 $message = $code;
192 return $message;