1 | <?php |
---|
2 | # -- BEGIN LICENSE BLOCK --------------------------------------- |
---|
3 | # |
---|
4 | # This file is part of Dotclear 2. |
---|
5 | # |
---|
6 | # Copyright (c) 2003-2011 Olivier Meunier & Association Dotclear |
---|
7 | # Licensed under the GPL version 2.0 license. |
---|
8 | # See LICENSE file or |
---|
9 | # http://www.gnu.org/licenses/old-licenses/gpl-2.0.html |
---|
10 | # |
---|
11 | # -- END LICENSE BLOCK ----------------------------------------- |
---|
12 | |
---|
13 | /* HTML page |
---|
14 | -------------------------------------------------------- */ |
---|
15 | require dirname(__FILE__).'/../inc/admin/prepend.php'; |
---|
16 | |
---|
17 | dcPage::check('media,media_admin'); |
---|
18 | |
---|
19 | $post_id = !empty($_GET['post_id']) ? (integer) $_GET['post_id'] : null; |
---|
20 | if ($post_id) { |
---|
21 | $post = $core->blog->getPosts(array('post_id'=>$post_id,'post_type'=>'')); |
---|
22 | if ($post->isEmpty()) { |
---|
23 | $post_id = null; |
---|
24 | } |
---|
25 | $post_title = $post->post_title; |
---|
26 | $post_type = $post->post_type; |
---|
27 | unset($post); |
---|
28 | } |
---|
29 | $d = isset($_REQUEST['d']) ? $_REQUEST['d'] : null; |
---|
30 | $dir = null; |
---|
31 | |
---|
32 | $page = !empty($_GET['page']) ? $_GET['page'] : 1; |
---|
33 | $nb_per_page = 30; |
---|
34 | |
---|
35 | # We are on home not comming from media manager |
---|
36 | if ($d === null && isset($_SESSION['media_manager_dir'])) { |
---|
37 | # We get session information |
---|
38 | $d = $_SESSION['media_manager_dir']; |
---|
39 | } |
---|
40 | |
---|
41 | if (!isset($_GET['page']) && isset($_SESSION['media_manager_page'])) { |
---|
42 | $page = $_SESSION['media_manager_page']; |
---|
43 | } |
---|
44 | |
---|
45 | # We set session information about directory and page |
---|
46 | if ($d) { |
---|
47 | $_SESSION['media_manager_dir'] = $d; |
---|
48 | } else { |
---|
49 | unset($_SESSION['media_manager_dir']); |
---|
50 | } |
---|
51 | if ($page != 1) { |
---|
52 | $_SESSION['media_manager_page'] = $page; |
---|
53 | } else { |
---|
54 | unset($_SESSION['media_manager_page']); |
---|
55 | } |
---|
56 | |
---|
57 | # Sort combo |
---|
58 | $sort_combo = array( |
---|
59 | __('By names, in ascending order') => 'name-asc', |
---|
60 | __('By names, in descending order') => 'name-desc', |
---|
61 | __('By dates, in ascending order') => 'date-asc', |
---|
62 | __('By dates, in descending order') => 'date-desc' |
---|
63 | ); |
---|
64 | |
---|
65 | if (!empty($_GET['file_sort']) && in_array($_GET['file_sort'],$sort_combo)) { |
---|
66 | $_SESSION['media_file_sort'] = $_GET['file_sort']; |
---|
67 | } |
---|
68 | $file_sort = !empty($_SESSION['media_file_sort']) ? $_SESSION['media_file_sort'] : null; |
---|
69 | |
---|
70 | $popup = (integer) !empty($_GET['popup']); |
---|
71 | |
---|
72 | $page_url = 'media.php?popup='.$popup.'&post_id='.$post_id; |
---|
73 | |
---|
74 | if ($popup) { |
---|
75 | $open_f = array('dcPage','openPopup'); |
---|
76 | $close_f = array('dcPage','closePopup'); |
---|
77 | } else { |
---|
78 | $open_f = array('dcPage','open'); |
---|
79 | $close_f = create_function('',"dcPage::helpBlock('core_media'); dcPage::close();"); |
---|
80 | } |
---|
81 | |
---|
82 | $core_media_writable = false; |
---|
83 | try { |
---|
84 | $core->media = new dcMedia($core); |
---|
85 | if ($file_sort) { |
---|
86 | $core->media->setFileSort($file_sort); |
---|
87 | } |
---|
88 | $core->media->chdir($d); |
---|
89 | $core->media->getDir(); |
---|
90 | $core_media_writable = $core->media->writable(); |
---|
91 | $dir =& $core->media->dir; |
---|
92 | if (!$core_media_writable) { |
---|
93 | throw new Exception('you do not have sufficient permissions to write to this folder: '); |
---|
94 | } |
---|
95 | } catch (Exception $e) { |
---|
96 | $core->error->add($e->getMessage()); |
---|
97 | } |
---|
98 | |
---|
99 | # Zip download |
---|
100 | if (!empty($_GET['zipdl']) && $core->auth->check('media_admin',$core->blog->id)) |
---|
101 | { |
---|
102 | try |
---|
103 | { |
---|
104 | @set_time_limit(300); |
---|
105 | $fp = fopen('php://output','wb'); |
---|
106 | $zip = new fileZip($fp); |
---|
107 | $zip->addExclusion('#(^|/).(.*?)_(m|s|sq|t).jpg$#'); |
---|
108 | $zip->addDirectory($core->media->root.'/'.$d,'',true); |
---|
109 | |
---|
110 | header('Content-Disposition: attachment;filename='.($d ? $d : 'media').'.zip'); |
---|
111 | header('Content-Type: application/x-zip'); |
---|
112 | $zip->write(); |
---|
113 | unset($zip); |
---|
114 | exit; |
---|
115 | } |
---|
116 | catch (Exception $e) |
---|
117 | { |
---|
118 | $core->error->add($e->getMessage()); |
---|
119 | } |
---|
120 | } |
---|
121 | |
---|
122 | # New directory |
---|
123 | if ($dir && !empty($_POST['newdir'])) |
---|
124 | { |
---|
125 | try { |
---|
126 | $core->media->makeDir($_POST['newdir']); |
---|
127 | http::redirect($page_url.'&d='.rawurlencode($d).'&mkdok=1'); |
---|
128 | } catch (Exception $e) { |
---|
129 | $core->error->add($e->getMessage()); |
---|
130 | } |
---|
131 | } |
---|
132 | |
---|
133 | # Adding a file |
---|
134 | if ($dir && !empty($_FILES['upfile'])) |
---|
135 | { |
---|
136 | $upfile = array('name' => $_FILES['upfile']['name'][0], |
---|
137 | 'type' => $_FILES['upfile']['type'][0], |
---|
138 | 'tmp_name' => $_FILES['upfile']['tmp_name'][0], |
---|
139 | 'error' => $_FILES['upfile']['error'][0], |
---|
140 | 'size' => $_FILES['upfile']['size'][0] |
---|
141 | ); |
---|
142 | |
---|
143 | try { |
---|
144 | files::uploadStatus($upfile); |
---|
145 | |
---|
146 | $core->media->uploadFile($upfile['tmp_name'],$upfile['name']); |
---|
147 | |
---|
148 | if (!empty($_SERVER['HTTP_X_REQUESTED_WITH'])) { |
---|
149 | header('Content-type: application/json'); |
---|
150 | $message = array(); |
---|
151 | $message['files'][] = array('name' => $upfile['name'], |
---|
152 | 'size' => $upfile['size'] |
---|
153 | ); |
---|
154 | |
---|
155 | echo json_encode($message); |
---|
156 | exit(); |
---|
157 | } else { |
---|
158 | http::redirect($page_url.'&d='.rawurlencode($d).'&upok=1'); |
---|
159 | } |
---|
160 | } catch (Exception $e) { |
---|
161 | if (!empty($_SERVER['HTTP_X_REQUESTED_WITH'])) { |
---|
162 | header('Content-type: application/json'); |
---|
163 | $message = array(); |
---|
164 | $message['files'][] = array('name' => $upfile['name'], |
---|
165 | 'error' => $e->getMessage() |
---|
166 | ); |
---|
167 | echo json_encode($message); |
---|
168 | exit(); |
---|
169 | } else { |
---|
170 | $core->error->add($e->getMessage()); |
---|
171 | } |
---|
172 | } |
---|
173 | } |
---|
174 | |
---|
175 | # Removing item |
---|
176 | if ($dir && !empty($_POST['rmyes']) && !empty($_POST['remove'])) |
---|
177 | { |
---|
178 | $_POST['remove'] = rawurldecode($_POST['remove']); |
---|
179 | |
---|
180 | try { |
---|
181 | $core->media->removeItem($_POST['remove']); |
---|
182 | http::redirect($page_url.'&d='.rawurlencode($d).'&rmfok=1'); |
---|
183 | } catch (Exception $e) { |
---|
184 | $core->error->add($e->getMessage()); |
---|
185 | } |
---|
186 | } |
---|
187 | |
---|
188 | # Rebuild directory |
---|
189 | if ($dir && $core->auth->isSuperAdmin() && !empty($_POST['rebuild'])) |
---|
190 | { |
---|
191 | try { |
---|
192 | $core->media->rebuild($d); |
---|
193 | http::redirect($page_url.'&d='.rawurlencode($d).'&rebuildok=1'); |
---|
194 | } catch (Exception $e) { |
---|
195 | $core->error->add($e->getMessage()); |
---|
196 | } |
---|
197 | } |
---|
198 | |
---|
199 | # DISPLAY confirm page for rmdir & rmfile |
---|
200 | if ($dir && !empty($_GET['remove']) && empty($_GET['noconfirm'])) |
---|
201 | { |
---|
202 | call_user_func($open_f,__('Media manager')); |
---|
203 | |
---|
204 | echo '<h2>'.html::escapeHTML($core->blog->name).' › '.__('Media manager').' › <span class="page-title">'.__('confirm removal').'</span></h2>'; |
---|
205 | |
---|
206 | echo |
---|
207 | '<form action="'.html::escapeURL($page_url).'" method="post">'. |
---|
208 | '<p>'.sprintf(__('Are you sure you want to remove %s?'), |
---|
209 | html::escapeHTML($_GET['remove'])).'</p>'. |
---|
210 | '<p><input type="submit" value="'.__('Cancel').'" /> '. |
---|
211 | ' <input type="submit" name="rmyes" value="'.__('Yes').'" />'. |
---|
212 | form::hidden('d',$d). |
---|
213 | $core->formNonce(). |
---|
214 | form::hidden('remove',html::escapeHTML($_GET['remove'])).'</p>'. |
---|
215 | '</form>'; |
---|
216 | |
---|
217 | call_user_func($close_f); |
---|
218 | exit; |
---|
219 | } |
---|
220 | |
---|
221 | /* DISPLAY Main page |
---|
222 | -------------------------------------------------------- */ |
---|
223 | $core->auth->user_prefs->addWorkspace('interface'); |
---|
224 | $user_ui_enhanceduploader = $core->auth->user_prefs->interface->enhanceduploader; |
---|
225 | |
---|
226 | call_user_func($open_f,__('Media manager'), |
---|
227 | dcPage::jsLoad('js/_media.js'). |
---|
228 | ($core_media_writable ? dcPage::jsUpload(array('d='.$d)) : '') |
---|
229 | ); |
---|
230 | |
---|
231 | if (!empty($_GET['mkdok'])) { |
---|
232 | dcPage::message(__('Directory has been successfully created.')); |
---|
233 | } |
---|
234 | |
---|
235 | if (!empty($_GET['upok'])) { |
---|
236 | dcPage::message(__('Files have been successfully uploaded.')); |
---|
237 | } |
---|
238 | |
---|
239 | if (!empty($_GET['rmfok'])) { |
---|
240 | dcPage::message(__('File has been successfully removed.')); |
---|
241 | } |
---|
242 | |
---|
243 | if (!empty($_GET['rmdok'])) { |
---|
244 | dcPage::message(__('Directory has been successfully removed.')); |
---|
245 | } |
---|
246 | |
---|
247 | if (!empty($_GET['rebuildok'])) { |
---|
248 | dcPage::message(__('Directory has been successfully rebuilt.')); |
---|
249 | } |
---|
250 | |
---|
251 | if (!empty($_GET['unzipok'])) { |
---|
252 | dcPage::message(__('Zip file has been successfully extracted.')); |
---|
253 | } |
---|
254 | |
---|
255 | echo '<h2>'.html::escapeHTML($core->blog->name).' › '; |
---|
256 | if (!isset($core->media)) { |
---|
257 | echo '<span class="page-title">'.__('Media manager').'</span></h2>'; |
---|
258 | } else { |
---|
259 | $breadcrumb = $core->media->breadCrumb(html::escapeURL($page_url).'&d=%s','<span class="page-title">%s</span>'); |
---|
260 | if ($breadcrumb == '') { |
---|
261 | echo '<span class="page-title">'.__('Media manager').'</span></h2>'; |
---|
262 | } else { |
---|
263 | echo '<a href="'.html::escapeURL($page_url.'&d=').'">'.__('Media manager').'</a>'.' / '.$breadcrumb.'</h2>'; |
---|
264 | } |
---|
265 | } |
---|
266 | |
---|
267 | if (!$dir) { |
---|
268 | call_user_func($close_f); |
---|
269 | exit; |
---|
270 | } |
---|
271 | |
---|
272 | if ($post_id) { |
---|
273 | echo '<p><strong>'.sprintf(__('Choose a file to attach to entry %s by clicking on %s.'), |
---|
274 | '<a href="'.$core->getPostAdminURL($post_type,$post_id).'">'.html::escapeHTML($post_title).'</a>', |
---|
275 | '<img src="images/plus.png" alt="'.__('Attach this file to entry').'" />').'</strong></p>'; |
---|
276 | } |
---|
277 | if ($popup) { |
---|
278 | echo '<p><strong>'.sprintf(__('Choose a file to insert into entry by clicking on %s.'), |
---|
279 | '<img src="images/plus.png" alt="'.__('Attach this file to entry').'" />').'</strong></p>'; |
---|
280 | } |
---|
281 | |
---|
282 | |
---|
283 | $items = array_values(array_merge($dir['dirs'],$dir['files'])); |
---|
284 | if (count($items) == 0) |
---|
285 | { |
---|
286 | echo '<p><strong>'.__('No file.').'</strong></p>'; |
---|
287 | } |
---|
288 | else |
---|
289 | { |
---|
290 | $pager = new pager($page,count($items),$nb_per_page,10); |
---|
291 | $pager->html_prev = __($pager->html_prev); |
---|
292 | $pager->html_next = __($pager->html_next); |
---|
293 | |
---|
294 | echo |
---|
295 | '<form action="media.php" method="get">'. |
---|
296 | '<p><label for="file_sort" class="classic">'.__('Sort files:').' '. |
---|
297 | form::combo('file_sort',$sort_combo,$file_sort).'</label>'. |
---|
298 | form::hidden(array('popup'),$popup). |
---|
299 | form::hidden(array('post_id'),$post_id). |
---|
300 | '<input type="submit" value="'.__('Sort').'" /></p>'. |
---|
301 | '</form>'. |
---|
302 | |
---|
303 | '<div class="media-list">'. |
---|
304 | '<p>'.__('Page(s)').' : '.$pager->getLinks().'</p>'; |
---|
305 | |
---|
306 | for ($i=$pager->index_start, $j=0; $i<=$pager->index_end; $i++, $j++) |
---|
307 | { |
---|
308 | echo mediaItemLine($items[$i],$j); |
---|
309 | } |
---|
310 | |
---|
311 | echo |
---|
312 | '<p class="clear">'.__('Page(s)').' : '.$pager->getLinks().'</p>'. |
---|
313 | '</div>'; |
---|
314 | } |
---|
315 | |
---|
316 | |
---|
317 | if ($core_media_writable) |
---|
318 | { |
---|
319 | echo '<div class="two-cols">'; |
---|
320 | |
---|
321 | echo |
---|
322 | '<div class="col">'. |
---|
323 | '<fieldset id="add-file-f"><legend>'.__('Add files').'</legend>'. |
---|
324 | '<p>'.__('Please take care to publish media that you own and that are not protected by copyright.').'</p>'. |
---|
325 | ' <form id="fileupload" action="'.html::escapeURL($page_url).'" method="POST" enctype="multipart/form-data">'. |
---|
326 | '<div>'.form::hidden(array('MAX_FILE_SIZE'),DC_MAX_UPLOAD_SIZE). |
---|
327 | $core->formNonce().'</div>'. |
---|
328 | '<div class="fileupload-buttonbar">'. |
---|
329 | '<label class="button-add button" for="upfile">'.__('Add files'). |
---|
330 | '<input type="file" id="upfile" name="upfile[]" multiple="multiple" data-url="'.html::escapeURL($page_url).'" />'. |
---|
331 | '</label>'. |
---|
332 | '</span>'. |
---|
333 | '<input class="button start" type="submit" value="'.__('Send').'"/>'. |
---|
334 | '</div>'. |
---|
335 | '<table role="presentation" class="table table-striped"><tbody class="files" data-toggle="modal-gallery" data-target="#modal-gallery"></tbody></table>'. |
---|
336 | form::hidden(array('d'),$d).'</p>'. |
---|
337 | '</fieldset>'. |
---|
338 | '</form>'. |
---|
339 | '</div>'; |
---|
340 | |
---|
341 | echo |
---|
342 | '<div class="col">'. |
---|
343 | '<form class="clear" action="'.html::escapeURL($page_url).'" method="post">'. |
---|
344 | '<fieldset id="new-dir-f">'. |
---|
345 | '<legend>'.__('New directory').'</legend>'. |
---|
346 | $core->formNonce(). |
---|
347 | '<p><label for="newdir">'.__('Directory Name:'). |
---|
348 | form::field(array('newdir','newdir'),35,255).'</label></p>'. |
---|
349 | '<p><input type="submit" value="'.__('Create').'" />'. |
---|
350 | form::hidden(array('d'),html::escapeHTML($d)).'</p>'. |
---|
351 | '</fieldset>'. |
---|
352 | '</form></div>'; |
---|
353 | |
---|
354 | echo '</div>'; |
---|
355 | } |
---|
356 | |
---|
357 | # Empty remove form (for javascript actions) |
---|
358 | echo |
---|
359 | '<form id="media-remove-hide" action="'.html::escapeURL($page_url).'" method="post"><div class="clear">'. |
---|
360 | form::hidden('rmyes',1).form::hidden('d',html::escapeHTML($d)). |
---|
361 | form::hidden('remove',''). |
---|
362 | $core->formNonce(). |
---|
363 | '</div></form>'; |
---|
364 | |
---|
365 | # Get zip directory |
---|
366 | if ($core->auth->check('media_admin',$core->blog->id) && |
---|
367 | !(count($items) == 0 || (count($items) == 1 && $items[0]->parent))) |
---|
368 | { |
---|
369 | echo |
---|
370 | '<p class="zip-dl"><a href="'.html::escapeURL($page_url).'&zipdl=1">'. |
---|
371 | __('Download this directory as a zip file').'</a></p>'; |
---|
372 | } |
---|
373 | |
---|
374 | call_user_func($close_f); |
---|
375 | |
---|
376 | /* ----------------------------------------------------- */ |
---|
377 | function mediaItemLine($f,$i) |
---|
378 | { |
---|
379 | global $core, $page_url, $popup, $post_id; |
---|
380 | |
---|
381 | $fname = $f->basename; |
---|
382 | |
---|
383 | if ($f->d) { |
---|
384 | $link = html::escapeURL($page_url).'&d='.html::sanitizeURL($f->relname); |
---|
385 | if ($f->parent) { |
---|
386 | $fname = '..'; |
---|
387 | } |
---|
388 | } else { |
---|
389 | $link = |
---|
390 | 'media_item.php?id='.$f->media_id.'&popup='.$popup.'&post_id='.$post_id; |
---|
391 | } |
---|
392 | |
---|
393 | $class = 'media-item media-col-'.($i%2); |
---|
394 | |
---|
395 | $res = |
---|
396 | '<div class="'.$class.'"><a class="media-icon media-link" href="'.$link.'">'. |
---|
397 | '<img src="'.$f->media_icon.'" alt="" /></a>'. |
---|
398 | '<ul>'. |
---|
399 | '<li><a class="media-link" href="'.$link.'">'.$fname.'</a></li>'; |
---|
400 | |
---|
401 | if (!$f->d) { |
---|
402 | $res .= |
---|
403 | '<li>'.$f->media_title.'</li>'. |
---|
404 | '<li>'. |
---|
405 | $f->media_dtstr.' - '. |
---|
406 | files::size($f->size).' - '. |
---|
407 | '<a href="'.$f->file_url.'">'.__('open').'</a>'. |
---|
408 | '</li>'; |
---|
409 | } |
---|
410 | |
---|
411 | $res .= '<li class="media-action"> '; |
---|
412 | |
---|
413 | if ($post_id && !$f->d) { |
---|
414 | $res .= '<form action="post_media.php" method="post">'. |
---|
415 | '<input type="image" src="images/plus.png" alt="'.__('Attach this file to entry').'" '. |
---|
416 | 'title="'.__('Attach this file to entry').'" /> '. |
---|
417 | form::hidden('media_id',$f->media_id). |
---|
418 | form::hidden('post_id',$post_id). |
---|
419 | form::hidden('attach',1). |
---|
420 | $core->formNonce(). |
---|
421 | '</form>'; |
---|
422 | } |
---|
423 | |
---|
424 | if ($popup && !$f->d) { |
---|
425 | $res .= '<a href="'.$link.'"><img src="images/plus.png" alt="'.__('Insert this file into entry').'" '. |
---|
426 | 'title="'.__('Insert this file into entry').'" /></a> '; |
---|
427 | } |
---|
428 | |
---|
429 | if ($f->del) { |
---|
430 | $res .= '<a class="media-remove" '. |
---|
431 | 'href="'.html::escapeURL($page_url).'&d='. |
---|
432 | rawurlencode($GLOBALS['d']).'&remove='.rawurlencode($f->basename).'">'. |
---|
433 | '<img src="images/trash.png" alt="'.__('Delete').'" title="'.__('delete').'" /></a>'; |
---|
434 | } |
---|
435 | |
---|
436 | $res .= '</li>'; |
---|
437 | |
---|
438 | if ($f->type == 'audio/mpeg3') { |
---|
439 | $res .= '<li>'.dcMedia::mp3player($f->file_url,'index.php?pf=player_mp3.swf').'</li>'; |
---|
440 | } |
---|
441 | |
---|
442 | $res .= '</ul></div>'; |
---|
443 | |
---|
444 | return $res; |
---|
445 | } |
---|
446 | ?> |
---|