/[drupal]/contributions/modules/image_import_zip/image_import_zip.module
ViewVC logotype

Contents of /contributions/modules/image_import_zip/image_import_zip.module

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph


Revision 1.1 - (show annotations) (download) (as text)
Fri Jul 11 19:13:53 2008 UTC (16 months, 2 weeks ago) by attiks
Branch: MAIN
CVS Tags: DRUPAL-5--1-0, HEAD
Branch point for: DRUPAL-5
File MIME type: text/x-php
Initial release
1 <?php
2 // $Id: image_import_zip.module,v 1.1.2.8 2007/07/24 17:34:14 drewish Exp $
3
4 /**
5 * Implementation of hook_help().
6 */
7 function image_import_zip_help($section = '') {
8 switch ($section) {
9 case 'admin/content/image_import_zip':
10 return t("Import a zip file with images so you can process them using image import. ");
11 case 'admin/settings/image_import_zip':
12 return t("Configure the image import zip module's settings.");
13 default:
14 return null;
15 }
16 }
17
18 /**
19 * Implementation of hook_perm().
20 */
21 function image_import_zip_perm() {
22 return array('import images');
23 }
24
25 /**
26 * Implementation of hook_menu().
27 */
28 function image_import_zip_menu($may_cache) {
29 $items = array();
30 if ($may_cache) {
31 $items[] = array(
32 'path' => 'admin/settings/image_import_zip',
33 'title' => t('Image import from Zip'),
34 'callback' => 'drupal_get_form',
35 'callback arguments' => array('image_import_zip_admin_settings'),
36 'access' => user_access('administer site configuration'),
37 'type' => MENU_NORMAL_ITEM,
38 'description' => t('Change settings for the Image Import Zip module.')
39 );
40 $items[] = array(
41 'path' => 'admin/content/image_import_zip',
42 'title' => t('Image import from zip'),
43 'callback' => 'drupal_get_form',
44 'callback arguments' => array('image_import_zip_form'),
45 'access' => user_access('import images'),
46 'type' => MENU_NORMAL_ITEM,
47 'description' => t('Import images from the local filesystem by using a zip file.')
48 );
49 }
50 return $items;
51 }
52
53 /**
54 * Settings.
55 */
56 function image_import_zip_admin_settings() {
57 $form['image_import_zip'] = array(
58 '#type' => 'textfield',
59 '#title' => t('Allowed extensions in zip file'),
60 '#default_value' => variable_get('image_import_zip_extensions', 'gif jpg jpeg png'),
61 '#description' => t("The allowed file extensions inside the zip file, all other extensions will not be processed."),
62 '#required' => TRUE,
63 );
64
65 return system_settings_form($form);
66 }
67
68
69 function image_import_zip_form() {
70 $form = array();
71
72 $dirpath = variable_get('image_import_path', '');
73 if (!file_check_directory($dirpath)) {
74 drupal_set_message(t("You need to configure the import directory on the image import module's <a href='!admin-settings-image_import'>settings page</a>.", array('!admin-settings-image_import' => url('admin/settings/image_import'))), 'error');
75 return $form;
76 }
77
78 $form['#attributes'] = array("enctype" => "multipart/form-data");
79 $form['zipfile'] = array(
80 '#type' => 'file',
81 '#title' => t('Zip'),
82 '#size' => 40,
83 '#description' => t('Click "Browse..." to select a Zip file to upload.'),
84 '#weight' => -3,
85 );
86
87 $form['buttons']['submit'] = array(
88 '#type' => 'submit',
89 '#value' => t('Import zip'),
90 );
91
92 return $form;
93
94 }
95
96 function image_import_zip_form_submit($form_id, $form_values) {
97 $op = isset($form_values['op']) ? $form_values['op'] : '';
98 if ($op == t('Import zip')) {
99 $dirpath = variable_get('image_import_path', '');
100 if (file_check_directory($dirpath)) {
101 // try to avoid php's script timeout with large files or a slow machine
102 if (!ini_get('safe_mode')) {
103 set_time_limit(0);
104 }
105
106 if ($file = file_check_upload('zipfile')) {
107 // Save the file to the import directory.
108 $file = file_save_upload('zipfile', $dirpath . $file->filename);
109 if (!$file) {
110 drupal_set_message(t('Zip file not uploaded'), 'error');
111 } else {
112 drupal_set_message(t('Zip file uploaded'), 'succes');
113 image_import_zip_unzip_php_ext($file);
114 return;
115 }
116 }
117 }
118 }
119 }
120
121 function image_import_zip_unzip_php_ext($file) {
122
123 if ($file->filemime == 'application/zip') {
124
125 $allowed_extensions = array_unique(explode(' ', variable_get('image_import_zip_extensions', 'gif jpg jpeg png')));
126 $zip = zip_open(realpath($file->filepath));
127 $zip_dir = variable_get('image_import_path', '') . '/';
128
129 if (!is_resource($zip)) {
130 drupal_set_message("Error for $file->filepath = " . image_import_zip_err($zip), 'error');
131 return;
132 }
133
134 if ($zip) {
135 while ($zip_entry = zip_read($zip)) {
136
137 $filename = basename(zip_entry_name($zip_entry));
138 $ext = strtolower(array_pop(explode('.', $filename)));
139
140 // check file extension
141 if (!in_array($ext, $allowed_extensions)) {
142 drupal_set_message("Skipping $filename because extension isn't allowed", 'error');
143 $valid = false;
144 } else {
145 drupal_set_message("Extracting $filename", 'succes');
146 $fp = fopen($zip_dir.basename($filename), "w+");
147 if (zip_entry_open($zip, $zip_entry, "r")) {
148 $buf = zip_entry_read($zip_entry, zip_entry_filesize($zip_entry));
149 zip_entry_close($zip_entry);
150 }
151 fwrite($fp, $buf);
152 fclose($fp);
153 $node_field_unzip[] = $item;
154 }
155 }
156 zip_close($zip);
157 }
158
159 } else {
160 drupal_set_message("no zip", 'error');
161 }
162 unlink(realpath($file->filepath));
163 return;
164 }
165
166 function image_import_zip_err($errno) {
167 $zipFileFunctionsErrors = array(
168 'ZIPARCHIVE::ER_MULTIDISK' => 'Multi-disk zip archives not supported.',
169 'ZIPARCHIVE::ER_RENAME' => 'Renaming temporary file failed.',
170 'ZIPARCHIVE::ER_CLOSE' => 'Closing zip archive failed',
171 'ZIPARCHIVE::ER_SEEK' => 'Seek error',
172 'ZIPARCHIVE::ER_READ' => 'Read error',
173 'ZIPARCHIVE::ER_WRITE' => 'Write error',
174 'ZIPARCHIVE::ER_CRC' => 'CRC error',
175 'ZIPARCHIVE::ER_ZIPCLOSED' => 'Containing zip archive was closed',
176 'ZIPARCHIVE::ER_NOENT' => 'No such file.',
177 'ZIPARCHIVE::ER_EXISTS' => 'File already exists',
178 'ZIPARCHIVE::ER_OPEN' => 'Can\'t open file',
179 'ZIPARCHIVE::ER_TMPOPEN' => 'Failure to create temporary file.',
180 'ZIPARCHIVE::ER_ZLIB' => 'Zlib error',
181 'ZIPARCHIVE::ER_MEMORY' => 'Memory allocation failure',
182 'ZIPARCHIVE::ER_CHANGED' => 'Entry has been changed',
183 'ZIPARCHIVE::ER_COMPNOTSUPP' => 'Compression method not supported.',
184 'ZIPARCHIVE::ER_EOF' => 'Premature EOF',
185 'ZIPARCHIVE::ER_INVAL' => 'Invalid argument',
186 'ZIPARCHIVE::ER_NOZIP' => 'Not a zip archive',
187 'ZIPARCHIVE::ER_INTERNAL' => 'Internal error',
188 'ZIPARCHIVE::ER_INCONS' => 'Zip archive inconsistent',
189 'ZIPARCHIVE::ER_REMOVE' => 'Can\'t remove file',
190 'ZIPARCHIVE::ER_DELETED' => 'Entry has been deleted',
191 );
192 $errmsg = 'unknown';
193 foreach ($zipFileFunctionsErrors as $constName => $errorMessage) {
194 if (defined($constName) and constant($constName) === $errno) {
195 return 'Zip File Function error: '.$errorMessage;
196 }
197 }
198 return 'Zip File Function error: unknown - ' . $errno;
199 }

  ViewVC Help
Powered by ViewVC 1.1.2