Dotclear

source: admin/media_item.php @ 1121:bcb6a083baa7

Revision 1121:bcb6a083baa7, 18.7 KB checked in by franck <carnet.franck.paul@…>, 11 years ago (diff)

Avoid null date (1970-01-01) in media title

Line 
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
13require dirname(__FILE__).'/../inc/admin/prepend.php';
14
15dcPage::check('media,media_admin');
16
17$tab = empty($_REQUEST['tab']) ? '' : $_REQUEST['tab'];
18
19$post_id = !empty($_GET['post_id']) ? (integer) $_GET['post_id'] : null;
20if ($post_id) {
21     $post = $core->blog->getPosts(array('post_id'=>$post_id));
22     if ($post->isEmpty()) {
23          $post_id = null;
24     }
25     $post_title = $post->post_title;
26     unset($post);
27}
28
29$file = null;
30$popup = (integer) !empty($_GET['popup']);
31$page_url = 'media_item.php?popup='.$popup.'&post_id='.$post_id;
32$media_page_url = 'media.php?popup='.$popup.'&post_id='.$post_id;
33
34$id = !empty($_REQUEST['id']) ? (integer) $_REQUEST['id'] : '';
35
36if ($popup) {
37     $open_f = array('dcPage','openPopup');
38     $close_f = array('dcPage','closePopup');
39} else {
40     $open_f = array('dcPage','open');
41     $close_f = create_function('',"dcPage::helpBlock('core_media'); dcPage::close();");
42}
43
44$core_media_writable = false;
45try
46{
47     $core->media = new dcMedia($core);
48     
49     if ($id) {
50          $file = $core->media->getFile($id);
51     }
52     
53     if ($file === null) {
54          throw new Exception(__('Not a valid file'));
55     }
56     
57     $core->media->chdir(dirname($file->relname));
58     $core_media_writable = $core->media->writable();
59     
60     # Prepare directories combo box
61     $dirs_combo = array();
62     foreach ($core->media->getDBDirs() as $v) {
63          $dirs_combo['/'.$v] = $v;
64     }
65     ksort($dirs_combo);
66}
67catch (Exception $e)
68{
69     $core->error->add($e->getMessage());
70}
71
72# Upload a new file
73if ($file && !empty($_FILES['upfile']) && $file->editable && $core_media_writable)
74{
75     try {
76          files::uploadStatus($_FILES['upfile']);
77          $core->media->uploadFile($_FILES['upfile']['tmp_name'],$file->basename,null,false,true);
78          http::redirect($page_url.'&id='.$id.'&fupl=1');
79     } catch (Exception $e) {
80          $core->error->add($e->getMessage());
81     }
82}
83
84# Update file
85if ($file && !empty($_POST['media_file']) && $file->editable && $core_media_writable)
86{
87     $newFile = clone $file;
88     
89     $newFile->basename = $_POST['media_file'];
90     
91     if ($_POST['media_path']) {
92          $newFile->dir = $_POST['media_path'];
93          $newFile->relname = $_POST['media_path'].'/'.$newFile->basename;
94     } else {
95          $newFile->dir = '';
96          $newFile->relname = $newFile->basename;
97     }
98     $newFile->media_title = $_POST['media_title'];
99     $newFile->media_dt = strtotime($_POST['media_dt']);
100     $newFile->media_dtstr = $_POST['media_dt'];
101     $newFile->media_priv = !empty($_POST['media_private']);
102     
103     try {
104          $core->media->updateFile($file,$newFile);
105          http::redirect($page_url.'&id='.$id.'&fupd=1&tab=media-details-tab');
106     } catch (Exception $e) {
107          $core->error->add($e->getMessage());
108     }
109}
110
111# Update thumbnails
112if (!empty($_POST['thumbs']) && $file->media_type == 'image' && $file->editable && $core_media_writable)
113{
114     try {
115          $foo = null;
116          $core->media->mediaFireRecreateEvent($file);
117          http::redirect($page_url.'&id='.$id.'&thumbupd=1&tab=media-details-tab');
118     } catch (Exception $e) {
119          $core->error->add($e->getMessage());
120     }
121}
122
123# Unzip file
124if (!empty($_POST['unzip']) && $file->type == 'application/zip' && $file->editable && $core_media_writable)
125{
126     try {
127          $unzip_dir = $core->media->inflateZipFile($file,$_POST['inflate_mode'] == 'new');
128          http::redirect($media_page_url.'&d='.$unzip_dir.'&unzipok=1');
129     } catch (Exception $e) {
130          $core->error->add($e->getMessage());
131     }
132}
133
134# Function to get image title based on meta
135function dcGetImageTitle($file,$pattern,$dto_first=false)
136{
137     $res = array();
138     $pattern = preg_split('/\s*;;\s*/',$pattern);
139     $sep = ', ';
140     
141     foreach ($pattern as $v) {
142          if ($v == 'Title') {
143               $res[] = $file->media_title;
144          } elseif ($file->media_meta->{$v}) {
145               $res[] = (string) $file->media_meta->{$v};
146          } elseif (preg_match('/^Date\((.+?)\)$/u',$v,$m)) {
147               if ($dto_first && ($file->media_meta->DateTimeOriginal != 0)) {
148                    $res[] = dt::dt2str($m[1],(string) $file->media_meta->DateTimeOriginal);
149               } else {
150                    $res[] = dt::str($m[1],$file->media_dt);
151               }
152          } elseif (preg_match('/^DateTimeOriginal\((.+?)\)$/u',$v,$m) && $file->media_meta->DateTimeOriginal) {
153               $res[] = dt::dt2str($m[1],(string) $file->media_meta->DateTimeOriginal);
154          } elseif (preg_match('/^separator\((.*?)\)$/u',$v,$m)) {
155               $sep = $m[1];
156          }
157     }
158     return implode($sep,$res);
159}
160
161/* DISPLAY Main page
162-------------------------------------------------------- */
163$starting_scripts = 
164     '<script type="text/javascript">'."\n".
165     "//<![CDATA["."\n".
166     dcPage::jsVar('dotclear.msg.confirm_delete_media',__('Are you sure to delete this media?'))."\n".
167     "//]]>".
168     "</script>".
169     dcPage::jsLoad('js/_media_item.js');
170if ($popup) {
171     $starting_scripts .=
172     dcPage::jsLoad('js/jsToolBar/popup_media.js');
173}
174call_user_func($open_f,__('Media manager'),
175     $starting_scripts.
176     dcPage::jsDatePicker().
177     dcPage::jsPageTabs($tab)
178);
179
180if ($file === null) {
181     call_user_func($close_f);
182     exit;
183}
184
185if (!empty($_GET['fupd']) || !empty($_GET['fupl'])) {
186     dcPage::message(__('File has been successfully updated.'));
187}
188if (!empty($_GET['thumbupd'])) {
189     dcPage::message(__('Thumbnails have been successfully updated.'));
190}
191
192echo '<h2><a href="'.html::escapeURL($media_page_url).'">'.__('Media manager').'</a>'.
193' / '.$core->media->breadCrumb(html::escapeURL($media_page_url).'&amp;d=%s').
194'<span class="page-title">'.$file->basename.'</span></h2>';
195
196# Insertion popup
197if ($popup)
198{
199     $media_desc = $file->media_title;
200     
201     echo
202     '<div id="media-insert" class="multi-part" title="'.__('Insert media item').'">'.
203     '<form id="media-insert-form" action="" method="get">';
204     
205     $media_img_default_size = $core->blog->settings->system->media_img_default_size;
206     if ($media_img_default_size == '') {
207          $media_img_default_size = 'm';
208     }
209     $media_img_default_alignment = $core->blog->settings->system->media_img_default_alignment;
210     if ($media_img_default_alignment == '') {
211          $media_img_default_alignment = 'none';
212     }
213     $media_img_default_link = (boolean)$core->blog->settings->system->media_img_default_link;
214
215     if ($file->media_type == 'image')
216     {
217          $media_type = 'image';
218          $media_desc = dcGetImageTitle($file,
219               $core->blog->settings->system->media_img_title_pattern,
220               $core->blog->settings->system->media_img_use_dto_first);
221          if ($media_desc == $file->basename) {
222               $media_desc = '';
223          }
224
225          echo
226          '<h3>'.__('Image size:').'</h3> ';
227         
228          $s_checked = false;
229          echo '<p>';
230          foreach (array_reverse($file->media_thumb) as $s => $v) {
231               $s_checked = ($s == $media_img_default_size);
232               echo '<label class="classic">'.
233               form::radio(array('src'),html::escapeHTML($v),$s_checked).' '.
234               $core->media->thumb_sizes[$s][2].'</label><br /> ';
235          }
236          $s_checked = (!isset($file->media_thumb[$media_img_default_size]));
237          echo '<label class="classic">'.
238          form::radio(array('src'),$file->file_url,$s_checked).' '.__('original').'</label><br /> ';
239          echo '</p>';
240         
241         
242          echo '<h3>'.__('Image alignment').'</h3>';
243          $i_align = array(
244               'none' => array(__('None'),($media_img_default_alignment == 'none' ? 1 : 0)),
245               'left' => array(__('Left'),($media_img_default_alignment == 'left' ? 1 : 0)),
246               'right' => array(__('Right'),($media_img_default_alignment == 'right' ? 1 : 0)),
247               'center' => array(__('Center'),($media_img_default_alignment == 'center' ? 1 : 0))
248          );
249         
250          echo '<p>';
251          foreach ($i_align as $k => $v) {
252               echo '<label class="classic">'.
253               form::radio(array('alignment'),$k,$v[1]).' '.$v[0].'</label><br /> ';
254          }
255          echo '</p>';
256         
257          echo
258          '<h3>'.__('Image insertion').'</h3>'.
259          '<p>'.
260          '<label for="insert1" class="classic">'.form::radio(array('insertion','insert1'),'simple',!$media_img_default_link).
261          __('As a single image').'</label><br />'.
262          '<label for="insert2" class="classic">'.form::radio(array('insertion','insert2'),'link',$media_img_default_link).
263          __('As a link to original image').'</label>'.
264          '</p>';
265     }
266     elseif ($file->type == 'audio/mpeg3')
267     {
268          $media_type = 'mp3';
269         
270          echo '<h3>'.__('MP3 disposition').'</h3>';
271          dcPage::message(__("Please note that you cannot insert mp3 files with visual editor."),false);
272         
273          $i_align = array(
274               'none' => array(__('None'),($media_img_default_alignment == 'none' ? 1 : 0)),
275               'left' => array(__('Left'),($media_img_default_alignment == 'left' ? 1 : 0)),
276               'right' => array(__('Right'),($media_img_default_alignment == 'right' ? 1 : 0)),
277               'center' => array(__('Center'),($media_img_default_alignment == 'center' ? 1 : 0))
278          );
279         
280          echo '<p>';
281          foreach ($i_align as $k => $v) {
282               echo '<label for="alignment" class="classic">'.
283               form::radio(array('alignment'),$k,$v[1]).' '.$v[0].'</label><br /> ';
284          }
285         
286          $public_player_style = unserialize($core->blog->settings->themes->mp3player_style);
287          $public_player = dcMedia::mp3player($file->file_url,$core->blog->getQmarkURL().'pf=player_mp3.swf',$public_player_style);
288          echo form::hidden('public_player',html::escapeHTML($public_player));
289          echo '</p>';
290     }
291     elseif ($file->type == 'video/x-flv' || $file->type == 'video/mp4' || $file->type == 'video/x-m4v')
292     {
293          $media_type = 'flv';
294         
295          dcPage::message(__("Please note that you cannot insert video files with visual editor."),false);
296         
297          echo
298          '<h3>'.__('Video size').'</h3>'.
299          '<p><label for="video_w" class="classic">'.__('Width:').'</label> '.
300          form::field('video_w',3,4,400).'  '.
301          '<label for="video_h" class="classic">'.__('Height:').'</label> '.
302          form::field('video_h',3,4,300).
303          '</p>';
304         
305          echo '<h3>'.__('Video disposition').'</h3>';
306         
307          $i_align = array(
308               'none' => array(__('None'),($media_img_default_alignment == 'none' ? 1 : 0)),
309               'left' => array(__('Left'),($media_img_default_alignment == 'left' ? 1 : 0)),
310               'right' => array(__('Right'),($media_img_default_alignment == 'right' ? 1 : 0)),
311               'center' => array(__('Center'),($media_img_default_alignment == 'center' ? 1 : 0))
312          );
313         
314          echo '<p>';
315          foreach ($i_align as $k => $v) {
316               echo '<label for="alignment" class="classic">'.
317               form::radio(array('alignment'),$k,$v[1]).' '.$v[0].'</label><br /> ';
318          }
319         
320          $public_player_style = unserialize($core->blog->settings->themes->flvplayer_style);
321          $public_player = dcMedia::flvplayer($file->file_url,$core->blog->getQmarkURL().'pf=player_flv.swf',$public_player_style);
322          echo form::hidden('public_player',html::escapeHTML($public_player));
323          echo '</p>';
324     }
325     else
326     {
327          $media_type = 'default';
328          echo '<p>'.__('Media item will be inserted as a link.').'</p>';
329     }
330     
331     echo
332     '<p><a id="media-insert-cancel" class="button" href="#">'.__('Cancel').'</a> - '.
333     '<strong><a id="media-insert-ok" class="button" href="#">'.__('Insert').'</a></strong>'.
334     form::hidden(array('type'),html::escapeHTML($media_type)).
335     form::hidden(array('title'),html::escapeHTML($file->media_title)).
336     form::hidden(array('description'),html::escapeHTML($media_desc)).
337     form::hidden(array('url'),$file->file_url).
338     '</p>';
339     
340     echo '</form></div>';
341}
342
343echo
344'<div class="multi-part" title="'.__('Media details').'" id="media-details-tab">'.
345'<p id="media-icon"><img src="'.$file->media_icon.'?'.time()*rand().'" alt="" /></p>';
346
347echo
348'<div id="media-details">';
349
350if ($file->media_image)
351{
352     $thumb_size = !empty($_GET['size']) ? $_GET['size'] : 's';
353     
354     if (!isset($core->media->thumb_sizes[$thumb_size]) && $thumb_size != 'o') {
355          $thumb_size = 's';
356     }
357     
358     echo '<p>'.__('Available sizes:').' ';
359     foreach (array_reverse($file->media_thumb) as $s => $v)
360     {
361          $strong_link = ($s == $thumb_size) ? '<strong>%s</strong>' : '%s';
362          printf($strong_link,'<a href="'.html::escapeURL($page_url).
363          '&amp;id='.$id.'&amp;size='.$s.'&amp;tab=media-details-tab">'.$core->media->thumb_sizes[$s][2].'</a> | ');
364     }
365     echo '<a href="'.html::escapeURL($page_url).'&amp;id='.$id.'&amp;size=o&amp;tab=media-details-tab">'.__('original').'</a>';
366     echo '</p>';
367     
368     if (isset($file->media_thumb[$thumb_size])) {
369          echo '<p><img src="'.$file->media_thumb[$thumb_size].'?'.time()*rand().'" alt="" /></p>';
370     } elseif ($thumb_size == 'o') {
371          $S = getimagesize($file->file);
372          $class = ($S[1] > 500) ? ' class="overheight"' : '';
373          unset($S);
374          echo '<p id="media-original-image"'.$class.'><img src="'.$file->file_url.'?'.time()*rand().'" alt="" /></p>';
375     }
376}
377
378if ($file->type == 'audio/mpeg3')
379{
380     echo dcMedia::mp3player($file->file_url,'index.php?pf=player_mp3.swf');
381}
382
383if ($file->type == 'video/x-flv' || $file->type == 'video/mp4' || $file->type == 'video/x-m4v')
384{
385     echo dcMedia::flvplayer($file->file_url,'index.php?pf=player_flv.swf');
386}
387
388echo
389'<h3>'.__('Media details').'</h3>'.
390'<ul>'.
391     '<li><strong>'.__('File owner:').'</strong> '.$file->media_user.'</li>'.
392     '<li><strong>'.__('File type:').'</strong> '.$file->type.'</li>'.
393     '<li><strong>'.__('File size:').'</strong> '.files::size($file->size).'</li>'.
394     '<li><strong>'.__('File URL:').'</strong> <a href="'.$file->file_url.'">'.$file->file_url.'</a></li>'.
395'</ul>';
396
397if (empty($_GET['find_posts']))
398{
399     echo
400     '<p><strong><a href="'.html::escapeHTML($page_url).'&amp;id='.$id.'&amp;find_posts=1&amp;tab=media-details-tab">'.
401     __('Show entries containing this media').'</a></strong></p>';
402}
403else
404{
405     echo '<h3>'.__('Entries containing this media').'</h3>';
406     $params = array(
407          'post_type' => '',
408          'from' => 'LEFT OUTER JOIN '.$core->prefix.'post_media PM ON P.post_id = PM.post_id ',
409          'sql' => 'AND ('.
410               'PM.media_id = '.(integer) $id.' '.
411               "OR post_content_xhtml LIKE '%".$core->con->escape($file->relname)."%' ".
412               "OR post_excerpt_xhtml LIKE '%".$core->con->escape($file->relname)."%' "
413     );
414     
415     if ($file->media_image)
416     { # We look for thumbnails too
417          if (preg_match('#^http(s)?://#',$core->blog->settings->system->public_url)) {
418               $media_root = $core->blog->settings->system->public_url;
419          } else {
420               $media_root = $core->blog->host.path::clean($core->blog->settings->system->public_url).'/';
421          }
422          foreach ($file->media_thumb as $v) {
423               $v = preg_replace('/^'.preg_quote($media_root,'/').'/','',$v);
424               $params['sql'] .= "OR post_content_xhtml LIKE '%".$core->con->escape($v)."%' ";
425               $params['sql'] .= "OR post_excerpt_xhtml LIKE '%".$core->con->escape($v)."%' ";
426          }
427     }
428     
429     $params['sql'] .= ') ';
430     
431     $rs = $core->blog->getPosts($params);
432     
433     if ($rs->isEmpty())
434     {
435          echo '<p>'.__('No entry seems contain this media.').'</p>';
436     }
437     else
438     {
439          echo '<ul>';
440          while ($rs->fetch()) {
441               $img = '<img alt="%1$s" title="%1$s" src="images/%2$s" />';
442               switch ($rs->post_status) {
443                    case 1:
444                         $img_status = sprintf($img,__('published'),'check-on.png');
445                         break;
446                    case 0:
447                         $img_status = sprintf($img,__('unpublished'),'check-off.png');
448                         break;
449                    case -1:
450                         $img_status = sprintf($img,__('scheduled'),'scheduled.png');
451                         break;
452                    case -2:
453                         $img_status = sprintf($img,__('pending'),'check-wrn.png');
454                         break;
455               }
456               echo '<li>'.$img_status.' '.'<a href="'.$core->getPostAdminURL($rs->post_type,$rs->post_id).'">'.
457                    $rs->post_title.'</a>'.
458                    ($rs->post_type != 'post' ? ' ('.html::escapeHTML($rs->post_type).')' : '').
459                    ' - '.dt::dt2str(__('%Y-%m-%d %H:%M'),$rs->post_dt).'</li>';
460          }
461          echo '</ul>';
462     }
463}
464
465if ($file->type == 'image/jpeg')
466{
467     echo '<h3>'.__('Image details').'</h3>';
468     
469     if (count($file->media_meta) == 0)
470     {
471          echo '<p>'.__('No detail').'</p>';
472     }
473     else
474     {
475          echo '<ul>';
476          foreach ($file->media_meta as $k => $v)
477          {
478               if ((string) $v) {
479                    echo '<li><strong>'.$k.':</strong> '.html::escapeHTML($v).'</li>';
480               }
481          }
482          echo '</ul>';
483     }
484}
485
486if ($file->editable && $core_media_writable)
487{
488     if ($file->media_type == 'image')
489     {
490          echo
491          '<form class="clear" action="'.html::escapeURL($page_url).'" method="post">'.
492          '<fieldset><legend>'.__('Update thumbnails').'</legend>'.
493          '<p>'.__('This will create or update thumbnails for this image.').'</p>'.
494          '<p><input type="submit" name="thumbs" value="'.__('Update thumbnails').'" />'.
495          form::hidden(array('id'),$id).
496          $core->formNonce().'</p>'.
497          '</fieldset></form>';
498     }
499     
500     if ($file->type == 'application/zip')
501     {
502          $inflate_combo = array(
503               __('Extract in a new directory') => 'new',
504               __('Extract in current directory') => 'current'
505          );
506         
507          echo
508          '<form class="clear" id="file-unzip" action="'.html::escapeURL($page_url).'" method="post">'.
509          '<fieldset><legend>'.__('Extract archive').'</legend>'.
510          '<ul>'.
511          '<li><strong>'.__('Extract in a new directory').'</strong> : '.
512          __('This will extract archive in a new directory that should not exist yet.').'</li>'.
513          '<li><strong>'.__('Extract in current directory').'</strong> : '.
514          __('This will extract archive in current directory and will overwrite existing files or directory.').'</li>'.
515          '</ul>'.
516          '<p><label for="inflate_mode" class="classic">'.__('Extract mode:').' '.
517          form::combo('inflate_mode',$inflate_combo,'new').'</label> '.
518          '<input type="submit" name="unzip" value="'.__('Extract').'" />'.
519          form::hidden(array('id'),$id).
520          $core->formNonce().'</p>'.
521          '</fieldset></form>';
522     }
523     
524     echo
525     '<form class="clear" action="'.html::escapeURL($page_url).'" method="post">'.
526     '<fieldset><legend>'.__('Change media properties').'</legend>'.
527     '<p><label for="media_file">'.__('File name:').
528     form::field('media_file',30,255,html::escapeHTML($file->basename)).'</label></p>'.
529     '<p><label for="media_title">'.__('File title:').
530     form::field('media_title',30,255,html::escapeHTML($file->media_title)).'</label></p>'.
531     '<p><label for="media_dt">'.__('File date:').
532     form::field('media_dt',16,16,html::escapeHTML($file->media_dtstr)).'</label></p>'.
533     '<p><label for="media_private" class="classic">'.form::checkbox('media_private',1,$file->media_priv).' '.
534     __('Private').'</label></p>'.
535     '<p><label for="media_path">'.__('New directory:').
536     form::combo('media_path',$dirs_combo,dirname($file->relname)).'</label></p>'.
537     '<p><input type="submit" accesskey="s" value="'.__('Save').'" />'.
538     form::hidden(array('id'),$id).
539     $core->formNonce().'</p>'.
540     '</fieldset></form>';
541     
542     echo
543     '<form class="clear" action="'.html::escapeURL($page_url).'" method="post" enctype="multipart/form-data">'.
544     '<fieldset><legend>'.__('Change file').'</legend>'.
545     '<div>'.form::hidden(array('MAX_FILE_SIZE'),DC_MAX_UPLOAD_SIZE).'</div>'.
546     '<p><label for="upfile">'.__('Choose a file:').
547     ' ('.sprintf(__('Maximum size %s'),files::size(DC_MAX_UPLOAD_SIZE)).') '.
548     '<input type="file" id="upfile" name="upfile" size="35" />'.
549     '</label></p>'.
550     '<p><input type="submit" value="'.__('Send').'" />'.
551     form::hidden(array('id'),$id).
552     $core->formNonce().'</p>'.
553     '</fieldset></form>';
554
555     if ($file->del) {
556          echo
557          '<form id="delete-form" method="post" action="'.html::escapeURL($media_page_url).
558          '&amp;d='.rawurlencode(dirname($file->relname)).
559          '&amp;remove='.rawurlencode($file->basename).'">'.
560          '<p><input name="delete" type="submit" class="delete" value="'.__('Delete this media').'" />'.
561          form::hidden('remove',rawurlencode($file->basename)).
562          form::hidden('rmyes',1).
563          $core->formNonce().'</p>'.
564          '</form>';
565     }
566
567
568     # --BEHAVIOR-- adminMediaItemForm
569     $core->callBehavior('adminMediaItemForm',$file);
570}
571
572echo
573'</div>'.
574'</div>';
575
576call_user_func($close_f);
577?>
Note: See TracBrowser for help on using the repository browser.

Sites map