Merge branch 'QA_3_3'
[phpmyadmin/dkf.git] / libraries / zip_extension.lib.php
blobb20207662d07f393a263470a9af3b3035c53f1e5
1 <?php
2 /* vim: set expandtab sw=4 ts=4 sts=4: */
4 /**
5 * Interface for the zip extension
6 * @package phpMyAdmin
7 * @version $Id$
8 */
10 /**
11 * Gets zip file contents
13 * @param string $file
14 * @return array ($error_message, $file_data); $error_message
15 * is empty if no error
18 function PMA_getZipContents($file)
20 $error_message = '';
21 $file_data = '';
22 $zip_handle = zip_open($file);
23 if (is_resource($zip_handle)) {
24 $first_zip_entry = zip_read($zip_handle);
25 if (false === $first_zip_entry) {
26 $error_message = $GLOBALS['strNoFilesFoundInZip'];
27 } else {
28 /* Is the the zip really an ODS file? */
29 $read = zip_entry_read($first_zip_entry);
30 $ods_mime = 'application/vnd.oasis.opendocument.spreadsheet';
31 if (!strcmp($ods_mime, $read)) {
32 /* Return the correct contents, not just the first entry */
33 for ( ; ; ) {
34 $entry = zip_read($zip_handle);
35 if (is_resource($entry)) {
36 if (!strcmp('content.xml', zip_entry_name($entry))) {
37 zip_entry_open($zip_handle, $entry, 'r');
38 $file_data = zip_entry_read($entry, zip_entry_filesize($entry));
39 zip_entry_close($entry);
40 break;
42 } else {
43 /**
44 * Either we have reached the end of the zip and still
45 * haven't found 'content.xml' or there was a parsing
46 * error that we must display
48 if ($entry === FALSE) {
49 $error_message = $GLOBALS['strErrorInZipFile'] . ' Could not find "content.xml"';
50 } else {
51 $error_message = $GLOBALS['strErrorInZipFile'] . ' ' . PMA_getZipError($zip_handle);
54 break;
57 } else {
58 zip_entry_open($zip_handle, $first_zip_entry, 'r');
59 /* File pointer has already been moved, so include what was read above */
60 $file_data = $read;
61 $file_data .= zip_entry_read($first_zip_entry, zip_entry_filesize($first_zip_entry));
62 zip_entry_close($first_zip_entry);
65 } else {
66 $error_message = $GLOBALS['strErrorInZipFile'] . ' ' . PMA_getZipError($zip_handle);
68 zip_close($zip_handle);
69 return (array('error' => $error_message, 'data' => $file_data));
72 /**
73 * Gets zip error message
75 * @param integer error code
76 * @return string error message
78 function PMA_getZipError($code)
80 // I don't think this needs translation
81 switch ($code) {
82 case ZIPARCHIVE::ER_MULTIDISK:
83 $message = 'Multi-disk zip archives not supported';
84 break;
85 case ZIPARCHIVE::ER_READ:
86 $message = 'Read error';
87 break;
88 case ZIPARCHIVE::ER_CRC:
89 $message = 'CRC error';
90 break;
91 case ZIPARCHIVE::ER_NOZIP:
92 $message = 'Not a zip archive';
93 break;
94 case ZIPARCHIVE::ER_INCONS:
95 $message = 'Zip archive inconsistent';
96 break;
97 default:
98 $message = $code;
100 return $message;