This repository has been archived by the owner on Aug 13, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpnsource_zip.php
258 lines (207 loc) · 8.46 KB
/
pnsource_zip.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
<?php
// $Id$
//
// Mediashare by Jorn Lind-Nielsen (C)
//
require_once 'modules/mediashare/common-edit.php';
require_once 'modules/mediashare/pnincludes/elfisk_common.php';
function mediashare_source_zip_view(& $args)
{
$albumId = mediashareGetIntUrl('aid', $args, 0);
if (isset($_POST['saveButton'])) {
return mediashareSourceZipUpload($args);
}
if (isset($_POST['moreButton']) || isset($_POST['continueButton'])) {
// After upload - update items and then continue to next page
if (!mediashareSourceZipUpdate()) {
return false;
}
}
if (isset($_POST['cancelButton']) || isset($_POST['continueButton'])) {
return pnRedirect(pnModURL('mediashare', 'edit', 'view', array('aid' => $albumId)));
}
if (isset($_POST['moreButton'])) {
return pnRedirect(pnModURL('mediashare', 'edit', 'addmedia', array('aid' => $albumId, 'source' => 'zip')));
}
// FIXME Required globals??
pnModAPILoad('mediashare', 'edit');
$uploadInfo = pnModAPIFunc('mediashare', 'source_zip', 'getUploadInfo');
$render = & pnRender::getInstance('mediashare', false);
$render->assign('imageNum', 1);
$render->assign('uploadFields', array(1));
$render->assign('post_max_size', $uploadInfo['post_max_size']);
$render->assign('upload_max_filesize', $uploadInfo['upload_max_filesize']);
return $render->fetch('mediashare_source_zip_view.html');
}
function mediashareSourceZipAddFile(& $zip, & $zipEntry, & $args)
{
$dom = ZLanguage::getModuleDomain('mediashare');
// Read zip info and file data into buffer
$zipSize = zip_entry_filesize($zipEntry);
$zipName = zip_entry_name($zipEntry);
if (!zip_entry_open($zip, $zipEntry, 'rb')) {
return array(array('ok' => false, 'message' => __f('Could not open the ZIP: %s', "$zipName", $dom)));
}
$buffer = zip_entry_read($zipEntry, $zipSize);
zip_entry_close($zipEntry);
// Ensure sub-folder exists
// Split name by slashes into folders and filename and create/verify the folders recursively
$folders = explode('/', $zipName);
$albumId = $args['albumId'];
if (!($subFolderID = mediashareEnsureFolderExists($albumId, $folders, 0))) {
return false;
}
$args['albumId'] = $subFolderID;
// Get actual filename from folderlist (last item in the array)
$imageName = $folders[sizeof($folders) - 1];
// Create tmp. file and copy image data into it
$tmpdir = pnModGetVar('mediashare', 'tmpDirName');
$tmpfilename = tempnam($tmpdir, 'IMG');
if (!($f = fopen($tmpfilename, 'wb'))) {
@ unlink($tmpfilename);
return false;
}
fwrite($f, $buffer);
fclose($f);
$args['mimeType'] = '';
if (function_exists('mime_content_type')) {
$args['mimeType'] = mime_content_type($tmpfilename);
if (empty($args['mimeType'])) {
$args['mimeType'] = mime_content_type($imageName);
}
}
if (empty($args['mimeType'])) {
$args['mimeType'] = mediashareGetMimeType($imageName);
}
$args['uploadFilename'] = $tmpfilename;
$args['fileSize'] = $zipSize;
$args['filename'] = $imageName;
$args['keywords'] = null;
$args['description'] = null;
// Create image (or add recursively zip archive)
$result = pnModAPIFunc('mediashare', 'source_zip', 'addMediaItem', $args);
if ($result === false) {
$status = array('ok' => false, 'message' => LogUtil::getErrorMessagesText());
} else {
$status = array('ok' => true, 'message' => $result['message'], 'mediaId' => $result['mediaId']);
}
$args['albumId'] = $albumId;
return $status;
}
function mediashareGetMimeType($filename)
{
$i = strpos($filename, '.');
if ($i >= 0) {
$ext = strtolower(substr($filename, $i + 1));
switch ($ext)
{
case 'gif':
return 'image/gif';
case 'jpg':
return 'image/jpeg';
case 'jpeg':
return 'image/jpeg';
case 'png':
return 'image/png';
}
}
return 'unknown';
}
function mediashareSourceZipUpload(& $args)
{
if (!SecurityUtil::confirmAuthKey()) {
return LogUtil::registerAuthidError();
}
$dom = ZLanguage::getModuleDomain('mediashare');
$albumId = mediashareGetIntUrl('aid', $args, 0);
// Check access
if (!mediashareAccessAlbum($albumId, mediashareAccessRequirementAddMedia, '')) {
return LogUtil::registerPermissionError();
}
// Get parent album information
if (!($album = pnModAPIFunc('mediashare', 'user', 'getAlbum', array('albumId' => $albumId)))) {
return false;
}
// Start fetching media items
$imageNum = (int)FormUtil::getPassedValue('imagenum');
$statusSet = array();
$args['albumId'] = $albumId;
for ($i = 1; $i <= $imageNum; ++$i)
{
$uploadInfo = $_FILES["upload$i"];
$args['width'] = FormUtil::getPassedValue("width$i");
$args['height'] = FormUtil::getPassedValue("height$i");
if (isset($uploadInfo['error']) && $uploadInfo['error'] != 0 && $uploadInfo['name'] != '') {
$statusSet[] = array('ok' => false, 'message' => $uploadInfo['name'] . ': ' . mediashareUploadErrorMsg($uploadInfo['error']));
} else if ($uploadInfo['size'] > 0) {
$zip = zip_open($uploadInfo['tmp_name']);
if (!$zip) {
return LogUtil::registerError(__('Could not open the ZIP.', $dom));
}
while ($zipEntry = zip_read($zip)) {
// echo "Name: ".zip_entry_name($zipEntry)."\n";
// echo "Actual Filesize: ".zip_entry_filesize($zipEntry)."\n";
// echo "Compressed Size: ".zip_entry_compressedsize($zipEntry)."\n";
// echo "Compression Method: ".zip_entry_compressionmethod($zipEntry)."\n";
// echo "<br>\n";
if (zip_entry_filesize($zipEntry) > 0) {
$result = mediashareSourceZipAddFile($zip, $zipEntry, $args);
if ($result === false) {
$status = array('ok' => false, 'message' => LogUtil::getErrorMessagesText());
} else {
$status = array('ok' => true, 'message' => $result['message'], 'mediaId' => $result['mediaId']);
}
$statusSet = array_merge($statusSet, array($status));
}
}
zip_close($zip);
}
}
// Quick count of uploaded images + getting IDs for further editing
$editMediaIds = array();
$acceptedImageNum = 0;
foreach ($statusSet as $status) {
if ($status['ok']) {
++$acceptedImageNum;
$editMediaIds[] = $status['mediaId'];
}
}
$album['imageCount'] += $acceptedImageNum; // Update for showing only
if ($acceptedImageNum == 0) {
$statusSet[] = array('ok' => false, 'message' => __('No media items', $dom));
}
if (($items = pnModAPIFunc('mediashare', 'user', 'getMediaItems', array('mediaIdList' => $editMediaIds))) === false) {
return false;
}
$render = & pnRender::getInstance('mediashare', false);
$render->assign('statusSet', $statusSet);
$render->assign('items', $items);
return $render->fetch('mediashare_source_zip_uploadet.html');
}
// Second page in upload sequence - user has entered media titles and such like, and it needs to be updated
function mediashareSourceZipUpdate()
{
if (!SecurityUtil::confirmAuthKey()) {
return LogUtil::registerAuthidError();
}
$mediaIds = FormUtil::getPassedValue('mediaId');
foreach ($mediaIds as $mediaId)
{
$mediaId = (int)$mediaId;
$title = FormUtil::getPassedValue("title-$mediaId");
$keywords = FormUtil::getPassedValue("keywords-$mediaId");
$description = FormUtil::getPassedValue("description-$mediaId");
// Check access
if (!mediashareAccessItem($mediaId, mediashareAccessRequirementEditMedia, '')) {
return LogUtil::registerPermissionError();
}
$args = array('mediaId' => $mediaId,
'title' => $title,
'keywords' => $keywords,
'description' => $description);
if (!pnModAPIFunc('mediashare', 'edit', 'updateItem', $args)) {
return false;
}
}
return true;
}