Dotclear

source: inc/core/class.dc.media.php @ 3299:18dc878c1178

Revision 3299:18dc878c1178, 36.5 KB checked in by franck <carnet.franck.paul@…>, 9 years ago (diff)

Protect media sorting callback function from null arg(s)

RevLine 
[0]1<?php
2# -- BEGIN LICENSE BLOCK ---------------------------------------
3#
4# This file is part of Dotclear 2.
5#
[1179]6# Copyright (c) 2003-2013 Olivier Meunier & Association Dotclear
[0]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 -----------------------------------------
12if (!defined('DC_RC_PATH')) { return; }
13
14/**
15@ingroup DC_CORE
16@brief Dotclear media manage
17
18This class handles Dotclear media items.
19*/
20class dcMedia extends filemanager
21{
22     protected $core;         ///< <b>dcCore</b> dcCore instance
23     protected $con;          ///< <b>connection</b> Database connection
24     protected $table;        ///< <b>string</b> Media table name
25     protected $type;         ///< <b>string</b> Media type filter
[407]26     protected $postmedia;
[0]27     protected $file_sort = 'name-asc';
[2566]28
[0]29     protected $file_handler = array(); ///< <b>array</b> Array of callbacks
[2566]30
[0]31     public $thumb_tp = '%s/.%s_%s.jpg';     ///< <b>string</b> Thumbnail file pattern
[1381]32     public $thumb_tp_alpha = '%s/.%s_%s.png'; ///< <b>string</b> Thumbnail file pattern (with alpha layer)
[2566]33
[0]34     /**
35     <b>array</b> Tubmnail sizes:
36     - m: medium image
37     - s: small image
38     - t: thumbnail image
39     - sq: square image
40     */
[2566]41     public $thumb_sizes = array(
[0]42          'm' => array(448,'ratio','medium'),
43          's' => array(240,'ratio','small'),
44          't' => array(100,'ratio','thumbnail'),
45          'sq' => array(48,'crop','square')
46     );
[2566]47
[0]48     public $icon_img = 'images/media/%s.png';    ///< <b>string</b> Icon file pattern
[2566]49
[0]50     /**
51     Object constructor.
[2566]52
[0]53     @param    core      <b>dcCore</b>       dcCore instance
54     @param    type      <b>string</b>       Media type filter
55     */
56     public function __construct($core,$type='')
57     {
58          $this->core =& $core;
59          $this->con =& $core->con;
[407]60          $this->postmedia = new dcPostMedia($core);
[2566]61
[0]62          if ($this->core->blog == null) {
63               throw new Exception(__('No blog defined.'));
64          }
[2566]65
[0]66          $this->table = $this->core->prefix.'media';
67          $root = $this->core->blog->public_path;
[2566]68
[0]69          if (preg_match('#^http(s)?://#',$this->core->blog->settings->system->public_url)) {
70               $root_url = rawurldecode($this->core->blog->settings->system->public_url);
71          } else {
72               $root_url = rawurldecode($this->core->blog->host.path::clean($this->core->blog->settings->system->public_url));
73          }
[2566]74
[0]75          if (!is_dir($root)) {
[1705]76               # Check public directory
77               if ( $core->auth->isSuperAdmin() ) {
[1708]78                    throw new Exception(__("There is no writable directory /public/ at the location set in about:config \"public_path\". You must create this directory with sufficient rights (or change this setting)."));
[1705]79               } else {
[1708]80                    throw new Exception(__("There is no writable root directory for the media manager. You should contact your administrator."));
[1705]81               }
[0]82          }
[2566]83
[0]84          $this->type = $type;
[2566]85
[0]86          parent::__construct($root,$root_url);
87          $this->chdir('');
[2566]88
[0]89          $this->path = $this->core->blog->settings->system->public_path;
[2566]90
[0]91          $this->addExclusion(DC_RC_PATH);
92          $this->addExclusion(dirname(__FILE__).'/../');
[2566]93
[0]94          $this->exclude_pattern = $core->blog->settings->system->media_exclusion;
[2566]95
[0]96          # Event handlers
97          $this->addFileHandler('image/jpeg','create',array($this,'imageThumbCreate'));
98          $this->addFileHandler('image/png','create',array($this,'imageThumbCreate'));
99          $this->addFileHandler('image/gif','create',array($this,'imageThumbCreate'));
[2566]100
[0]101          $this->addFileHandler('image/png','update',array($this,'imageThumbUpdate'));
102          $this->addFileHandler('image/jpeg','update',array($this,'imageThumbUpdate'));
103          $this->addFileHandler('image/gif','update',array($this,'imageThumbUpdate'));
[2566]104
[0]105          $this->addFileHandler('image/png','remove',array($this,'imageThumbRemove'));
106          $this->addFileHandler('image/jpeg','remove',array($this,'imageThumbRemove'));
107          $this->addFileHandler('image/gif','remove',array($this,'imageThumbRemove'));
[2566]108
[0]109          $this->addFileHandler('image/jpeg','create',array($this,'imageMetaCreate'));
[2566]110
[2]111          $this->addFileHandler('image/jpeg','recreate',array($this,'imageThumbCreate'));
112          $this->addFileHandler('image/png','recreate',array($this,'imageThumbCreate'));
113          $this->addFileHandler('image/gif','recreate',array($this,'imageThumbCreate'));
[2566]114
[3]115          $this->addFileHandler('image/jpeg','recreate',array($this,'imageThumbCreate'));
116          $this->addFileHandler('image/png','recreate',array($this,'imageThumbCreate'));
117          $this->addFileHandler('image/gif','recreate',array($this,'imageThumbCreate'));
[2566]118
[0]119          # Thumbnails sizes
120          $this->thumb_sizes['m'][0] = abs($core->blog->settings->system->media_img_m_size);
121          $this->thumb_sizes['s'][0] = abs($core->blog->settings->system->media_img_s_size);
122          $this->thumb_sizes['t'][0] = abs($core->blog->settings->system->media_img_t_size);
[2566]123
[0]124          # Thumbnails sizes names
125          $this->thumb_sizes['m'][2] = __($this->thumb_sizes['m'][2]);
126          $this->thumb_sizes['s'][2] = __($this->thumb_sizes['s'][2]);
127          $this->thumb_sizes['t'][2] = __($this->thumb_sizes['t'][2]);
128          $this->thumb_sizes['sq'][2] = __($this->thumb_sizes['sq'][2]);
[2566]129
[0]130          # --BEHAVIOR-- coreMediaConstruct
[2566]131          $this->core->callBehavior('coreMediaConstruct',$this);
[0]132     }
[2566]133
[0]134     /**
135     Changes working directory.
[2566]136
[0]137     @param    dir       <b>string</b>       Directory name.
138     */
139     public function chdir($dir)
140     {
141          parent::chdir($dir);
142          $this->relpwd = preg_replace('/^'.preg_quote($this->root,'/').'\/?/','',$this->pwd);
143     }
[2566]144
[0]145     /**
146     Adds a new file handler for a given media type and event.
[2566]147
[0]148     Available events are:
149     - create: file creation
150     - update: file update
151     - remove: file deletion
[2566]152
[0]153     @param    type      <b>string</b>       Media type
154     @param    event     <b>string</b>       Event
155     @param    function  <b>callback</b>
156     */
157     public function addFileHandler($type,$event,$function)
158     {
159          if (is_callable($function)) {
160               $this->file_handler[$type][$event][] = $function;
161          }
162     }
[2566]163
[0]164     protected function callFileHandler($type,$event)
165     {
166          if (!empty($this->file_handler[$type][$event]))
167          {
168               $args = func_get_args();
169               array_shift($args);
170               array_shift($args);
[2566]171
[0]172               foreach ($this->file_handler[$type][$event] as $f)
173               {
174                    call_user_func_array($f,$args);
175               }
176          }
177     }
[2566]178
[0]179     /**
180     Returns HTML breadCrumb for media manager navigation.
[2566]181
[0]182     @param    href      <b>string</b>       URL pattern
[505]183     @param    last      <b>string</b>       Last item pattern
[0]184     @return   <b>string</b> HTML code
185     */
[505]186     public function breadCrumb($href,$last='')
[0]187     {
188          $res = '';
189          if ($this->relpwd && $this->relpwd != '.') {
190               $pwd = '';
[505]191               $arr = explode('/',$this->relpwd);
192               $count = count($arr);
193               foreach ($arr as $v) {
194                    if (($last != '') && (0 === --$count)) {
195                         $res .= sprintf($last,$v);
196                    } else {
197                         $pwd .= rawurlencode($v).'/';
198                         $res .= '<a href="'.sprintf($href,$pwd).'">'.$v.'</a> / ';
199                    }
[0]200               }
201          }
202          return $res;
[2566]203
[0]204     }
[2566]205
[0]206     protected function fileRecord($rs)
207     {
208          if ($rs->isEmpty()) { return null; }
[2566]209
[0]210          if (!$this->isFileExclude($this->root.'/'.$rs->media_file) && is_file($this->root.'/'.$rs->media_file))
211          {
212               $f = new fileItem($this->root.'/'.$rs->media_file,$this->root,$this->root_url);
[2566]213
[0]214               if ($this->type && $f->type_prefix != $this->type) {
215                    return null;
216               }
[2566]217
[0]218               $meta = @simplexml_load_string($rs->media_meta);
[2566]219
[0]220               $f->editable = true;
221               $f->media_id = $rs->media_id;
222               $f->media_title = $rs->media_title;
223               $f->media_meta =  $meta instanceof SimpleXMLElement ? $meta : simplexml_load_string('<meta></meta>');
224               $f->media_user = $rs->user_id;
225               $f->media_priv = (boolean) $rs->media_private;
226               $f->media_dt = strtotime($rs->media_dt);
227               $f->media_dtstr = dt::str('%Y-%m-%d %H:%M',$f->media_dt);
[2566]228
[0]229               $f->media_image = false;
[2566]230
[0]231               if (!$this->core->auth->check('media_admin',$this->core->blog->id)
232               && $this->core->auth->userID() != $f->media_user) {
233                    $f->del = false;
234                    $f->editable = false;
235               }
[2566]236
[0]237               $type_prefix = explode('/',$f->type);
238               $type_prefix = $type_prefix[0];
[2566]239
[0]240               switch ($type_prefix) {
241                    case 'image':
242                         $f->media_image = true;
243                         $f->media_icon = 'image';
244                         break;
245                    case 'audio':
246                         $f->media_icon = 'audio';
247                         break;
248                    case 'text':
249                         $f->media_icon = 'text';
250                         break;
251                    case 'video':
252                         $f->media_icon = 'video';
253                         break;
254                    default:
255                         $f->media_icon = 'blank';
256               }
257               switch ($f->type) {
258                    case 'application/msword':
259                    case 'application/vnd.oasis.opendocument.text':
260                    case 'application/vnd.sun.xml.writer':
261                    case 'application/pdf':
262                    case 'application/postscript':
263                         $f->media_icon = 'document';
264                         break;
265                    case 'application/msexcel':
266                    case 'application/vnd.oasis.opendocument.spreadsheet':
267                    case 'application/vnd.sun.xml.calc':
268                         $f->media_icon = 'spreadsheet';
269                         break;
270                    case 'application/mspowerpoint':
271                    case 'application/vnd.oasis.opendocument.presentation':
272                    case 'application/vnd.sun.xml.impress':
273                         $f->media_icon = 'presentation';
274                         break;
275                    case 'application/x-debian-package':
276                    case 'application/x-bzip':
277                    case 'application/x-gzip':
278                    case 'application/x-java-archive':
279                    case 'application/rar':
280                    case 'application/x-redhat-package-manager':
281                    case 'application/x-tar':
282                    case 'application/x-gtar':
283                    case 'application/zip':
284                         $f->media_icon = 'package';
285                         break;
286                    case 'application/octet-stream':
287                         $f->media_icon = 'executable';
288                         break;
289                    case 'application/x-shockwave-flash':
290                         $f->media_icon = 'video';
291                         break;
292                    case 'application/ogg':
293                         $f->media_icon = 'audio';
294                         break;
295                    case 'text/html':
296                         $f->media_icon = 'html';
297                         break;
298               }
[2566]299
[0]300               $f->media_type = $f->media_icon;
301               $f->media_icon = sprintf($this->icon_img,$f->media_icon);
[2566]302
[0]303               # Thumbnails
304               $f->media_thumb = array();
305               $p = path::info($f->relname);
[1677]306
[1381]307               $alpha = ($p['extension'] == 'png') || ($p['extension'] == 'PNG');
[1677]308
[1381]309               $thumb = sprintf(($alpha ? $this->thumb_tp_alpha : $this->thumb_tp),$this->root.'/'.$p['dirname'],$p['base'],'%s');
310               $thumb_url = sprintf(($alpha ? $this->thumb_tp_alpha : $this->thumb_tp),$this->root_url.$p['dirname'],$p['base'],'%s');
[2566]311
[0]312               # Cleaner URLs
313               $thumb_url = preg_replace('#\./#','/',$thumb_url);
314               $thumb_url = preg_replace('#(?<!:)/+#','/',$thumb_url);
[1677]315
316               if ($alpha) {
317                    $thumb_alt = sprintf($this->thumb_tp,$this->root.'/'.$p['dirname'],$p['base'],'%s');
318                    $thumb_url_alt = sprintf($this->thumb_tp,$this->root_url.$p['dirname'],$p['base'],'%s');
319                    # Cleaner URLs
320                    $thumb_url_alt = preg_replace('#\./#','/',$thumb_url_alt);
321                    $thumb_url_alt = preg_replace('#(?<!:)/+#','/',$thumb_url_alt);
322               }
[2566]323
[0]324               foreach ($this->thumb_sizes as $suffix => $s) {
325                    if (file_exists(sprintf($thumb,$suffix))) {
326                         $f->media_thumb[$suffix] = sprintf($thumb_url,$suffix);
[1677]327                    } elseif ($alpha && file_exists(sprintf($thumb_alt,$suffix))) {
328                         $f->media_thumb[$suffix] = sprintf($thumb_url_alt,$suffix);
[0]329                    }
330               }
[2566]331
[0]332               if (isset($f->media_thumb['sq']) && $f->media_type == 'image') {
333                    $f->media_icon = $f->media_thumb['sq'];
334               }
[2566]335
[0]336               return $f;
337          }
[2566]338
[0]339          return null;
340     }
[2566]341
342
[0]343     public function setFileSort($type='name')
344     {
345          if (in_array($type,array('name-asc','name-desc','date-asc','date-desc'))) {
346               $this->file_sort = $type;
347          }
348     }
[2566]349
[0]350     protected function sortFileHandler($a,$b)
351     {
[3299]352          if (is_null($a) || is_null($b)) {
353               return (is_null($a) ? 1 : -1);
354          }
[0]355          switch ($this->file_sort)
356          {
357               case 'date-asc':
358                    if ($a->media_dt == $b->media_dt) {
359                         return 0;
360                    }
361                    return ($a->media_dt < $b->media_dt) ? -1 : 1;
362               case 'date-desc':
363                    if ($a->media_dt == $b->media_dt) {
364                         return 0;
365                    }
366                    return ($a->media_dt > $b->media_dt) ? -1 : 1;
367               case 'name-desc':
368                    return strcasecmp($b->basename,$a->basename);
369               case 'name-asc':
370               default:
371                    return strcasecmp($a->basename,$b->basename);
372          }
373     }
[2098]374
375     /**
376     Gets current working directory content (using filesystem)
377
378     */
379     public function getFSDir()
380     {
381          parent::getDir();
382     }
383
[0]384     /**
[2566]385     Gets current working directory content.
386
[0]387     @param    type      <b>string</b>       Media type filter
388     */
389     public function getDir($type=null)
390     {
391          if ($type) {
392               $this->type = $type;
393          }
[2566]394
[0]395          $media_dir = $this->relpwd ? $this->relpwd : '.';
[2566]396
[0]397          $strReq =
398          'SELECT media_file, media_id, media_path, media_title, media_meta, media_dt, '.
399          'media_creadt, media_upddt, media_private, user_id '.
400          'FROM '.$this->table.' '.
401          "WHERE media_path = '".$this->path."' ".
402          "AND media_dir = '".$this->con->escape($media_dir)."' ";
[2566]403
[0]404          if (!$this->core->auth->check('media_admin',$this->core->blog->id))
405          {
406               $strReq .= 'AND (media_private <> 1 ';
[2566]407
[0]408               if ($this->core->auth->userID()) {
409                    $strReq .= "OR user_id = '".$this->con->escape($this->core->auth->userID())."'";
410               }
411               $strReq .= ') ';
412          }
[2566]413
[0]414          $rs = $this->con->select($strReq);
[2566]415
[0]416          parent::getDir();
[2566]417
[0]418          $f_res = array();
419          $p_dir = $this->dir;
[2566]420
[0]421          # If type is set, remove items from p_dir
422          if ($this->type)
423          {
424               foreach ($p_dir['files'] as $k => $f) {
425                    if ($f->type_prefix != $this->type) {
426                         unset($p_dir['files'][$k]);
427                    }
428               }
429          }
[2566]430
[0]431          $f_reg = array();
[2566]432
[0]433          while ($rs->fetch())
434          {
435               # File in subdirectory, forget about it!
436               if (dirname($rs->media_file) != '.' && dirname($rs->media_file) != $this->relpwd) {
437                    continue;
438               }
[2566]439
[0]440               if ($this->inFiles($rs->media_file))
441               {
442                    $f = $this->fileRecord($rs);
443                    if ($f !== null) {
444                         if (isset($f_reg[$rs->media_file]))
445                         {
[2566]446                              # That media is duplicated in the database,
[0]447                              # time to do a bit of house cleaning.
448                              $this->con->execute(
449                                   'DELETE FROM '.$this->table.' '.
450                                   "WHERE media_id = ".$this->fileRecord($rs)->media_id
451                              );
452                         } else {
453                              $f_res[] = $this->fileRecord($rs);
454                              $f_reg[$rs->media_file] = 1;
455                         }
456                    }
457               }
458               elseif (!empty($p_dir['files']) && $this->relpwd == '')
459               {
460                    # Physical file does not exist remove it from DB
461                    # Because we don't want to erase everything on
462                    # dotclear upgrade, do it only if there are files
463                    # in directory and directory is root
464                    $this->con->execute(
465                         'DELETE FROM '.$this->table.' '.
466                         "WHERE media_path = '".$this->con->escape($this->path)."' ".
467                         "AND media_file = '".$this->con->escape($rs->media_file)."' "
468                    );
469                    $this->callFileHandler(files::getMimeType($rs->media_file),'remove',$this->pwd.'/'.$rs->media_file);
470               }
471          }
[2566]472
[0]473          $this->dir['files'] = $f_res;
474          foreach ($this->dir['dirs'] as $k => $v) {
[1855]475               $v->media_icon = sprintf($this->icon_img,($v->parent ? 'folder-up' : 'folder'));
[0]476          }
[2566]477
[0]478          # Check files that don't exist in database and create them
479          if ($this->core->auth->check('media,media_admin',$this->core->blog->id))
480          {
481               foreach ($p_dir['files'] as $f)
482               {
483                    if (!isset($f_reg[$f->relname])) {
484                         if (($id = $this->createFile($f->basename,null,false,null,false)) !== false) {
485                              $this->dir['files'][] = $this->getFile($id);
486                         }
487                    }
488               }
489          }
[3299]490          try {
491               usort($this->dir['files'],array($this,'sortFileHandler'));
492          } catch (Exception $e) {}
[0]493     }
[2566]494
[0]495     /**
496     Gets file by its id. Returns a filteItem object.
[2566]497
[0]498     @param    id        <b>integer</b>      File ID
499     @return   <b>fileItem</b>
500     */
501     public function getFile($id)
502     {
503          $strReq =
504          'SELECT media_id, media_path, media_title, '.
505          'media_file, media_meta, media_dt, media_creadt, '.
506          'media_upddt, media_private, user_id '.
507          'FROM '.$this->table.' '.
508          "WHERE media_path = '".$this->path."' ".
509          'AND media_id = '.(integer) $id.' ';
[2566]510
[0]511          if (!$this->core->auth->check('media_admin',$this->core->blog->id))
512          {
513               $strReq .= 'AND (media_private <> 1 ';
[2566]514
[0]515               if ($this->core->auth->userID()) {
516                    $strReq .= "OR user_id = '".$this->con->escape($this->core->auth->userID())."'";
517               }
518               $strReq .= ') ';
519          }
[2566]520
[0]521          $rs = $this->con->select($strReq);
522          return $this->fileRecord($rs);
523     }
[2566]524
[0]525     /**
[3099]526     Search into media db (only).
527
528     @param    query          <b>string</b>       Search query
529     @return boolean     true or false if nothing found
530     */
531     public function searchMedia($query)
532     {
533          if ($query == '') {
534               return false;
535          }
536
537          $strReq =
538          'SELECT media_file, media_id, media_path, media_title, media_meta, media_dt, '.
539          'media_creadt, media_upddt, media_private, user_id '.
540          'FROM '.$this->table.' '.
541          "WHERE media_path = '".$this->path."' ".
542          "AND (media_title LIKE '%".$this->con->escape($query)."%' ".
543          "    OR media_file LIKE '%".$this->con->escape($query)."%' ".
544          "    OR media_meta LIKE '<Description>%".$this->con->escape($query)."%</Description>')";
545
546          if (!$this->core->auth->check('media_admin',$this->core->blog->id))
547          {
548               $strReq .= 'AND (media_private <> 1 ';
549
550               if ($this->core->auth->userID()) {
551                    $strReq .= "OR user_id = '".$this->con->escape($this->core->auth->userID())."'";
552               }
553               $strReq .= ') ';
554          }
555
556          $rs = $this->con->select($strReq);
557
558          $this->dir = array('dirs' => array(),'files' => array());
559          $f_res = array();
560          while ($rs->fetch())
561          {
562               $fr = $this->fileRecord($rs);
563               if ($fr) {
564                    $f_res[] = $fr;
565               }
566          }
567          $this->dir['files'] = $f_res;
568
569          try {
570               usort($this->dir['files'],array($this,'sortFileHandler'));
571          } catch (Exception $e) {}
572
573          return (count($f_res) > 0 ? true : false);
574     }
575
576     /**
[0]577     Returns media items attached to a blog post. Result is an array containing
578     fileItems objects.
[2566]579
[3167]580     @param    post_id        <b>integer</b>      Post ID
[0]581     @param    media_id  <b>integer</b>      Optionnal media ID
[3167]582     @param    link_type <b>string</b>       Optionnal link type
[1280]583     @return   <b>array</b> Array of fileItems
[0]584     */
[3167]585     public function getPostMedia($post_id,$media_id=null,$link_type=null)
[0]586     {
[407]587          $params = array(
588               'post_id' => $post_id,
589               'media_path' => $this->path
590          );
[0]591          if ($media_id) {
[407]592               $params['media_id'] = (integer) $media_id;
[0]593          }
[3167]594          if ($link_type) {
595               $params['link_type'] = $link_type;
596          }
[407]597          $rs = $this->postmedia->getPostMedia($params);
[2566]598
[0]599          $res = array();
[2566]600
[0]601          while ($rs->fetch()) {
602               $f = $this->fileRecord($rs);
603               if ($f !== null) {
[1280]604                    $res[] = $f;
[0]605               }
606          }
[2566]607
[1280]608          return $res;
[0]609     }
[2566]610
[0]611     /**
[407]612     @deprecated since version 2.4
613     @see dcPostMedia::addPostMedia
[0]614     */
[3167]615     public function addPostMedia($post_id,$media_id,$link_type='attachment')
[0]616     {
[3167]617          $this->postmedia->addPostMedia($post_id,$media_id,$link_type);
[0]618     }
[2566]619
[0]620     /**
[407]621     @deprecated since version 2.4
622     @see dcPostMedia::removePostMedia
[0]623     */
[3167]624     public function removePostMedia($post_id,$media_id,$link_type='attachment')
[0]625     {
[3167]626          $this->postmedia->removePostMedia($post_id,$media_id,$link_type);
[0]627     }
[2566]628
[0]629     /**
630     Rebuilds database items collection. Optional <var>$pwd</var> parameter is
631     the path where to start rebuild.
[2566]632
[0]633     @param    pwd       <b>string</b>       Directory to rebuild
634     */
635     public function rebuild($pwd='')
636     {
637          if (!$this->core->auth->isSuperAdmin()) {
638               throw new Exception(__('You are not a super administrator.'));
639          }
[2566]640
[0]641          $this->chdir($pwd);
642          parent::getDir();
[2566]643
[0]644          $dir = $this->dir;
[2566]645
[0]646          foreach ($dir['dirs'] as $d) {
647               if (!$d->parent) {
648                    $this->rebuild($d->relname,false);
649               }
650          }
[2566]651
[0]652          foreach ($dir['files'] as $f) {
653               $this->chdir(dirname($f->relname));
654               $this->createFile($f->basename);
655          }
[2566]656
[0]657          $this->rebuildDB($pwd);
658     }
[2566]659
[0]660     protected function rebuildDB($pwd)
661     {
662          $media_dir = $pwd ? $pwd : '.';
[2566]663
[0]664          $strReq =
665          'SELECT media_file, media_id '.
666          'FROM '.$this->table.' '.
667          "WHERE media_path = '".$this->path."' ".
668          "AND media_dir = '".$this->con->escape($media_dir)."' ";
[2566]669
[0]670          $rs = $this->con->select($strReq);
[2566]671
[0]672          $delReq = 'DELETE FROM '.$this->table.' '.
673                    'WHERE media_id IN (%s) ';
674          $del_ids = array();
[2566]675
[0]676          while ($rs->fetch())
677          {
678               if (!is_file($this->root.'/'.$rs->media_file)) {
679                    $del_ids[] = (integer) $rs->media_id;
680               }
681          }
[2566]682
[0]683          if (!empty($del_ids)) {
684               $this->con->execute(sprintf($delReq,implode(',',$del_ids)));
685          }
686     }
[2566]687
[0]688     public function makeDir($d)
689     {
690          $d = files::tidyFileName($d);
691          parent::makeDir($d);
692     }
[2566]693
[0]694     /**
695     Creates or updates a file in database. Returns new media ID or false if
696     file does not exist.
[2566]697
[0]698     @param    name      <b>string</b>       File name (relative to working directory)
699     @param    title     <b>string</b>       File title
700     @param    private   <b>boolean</b>      File is private
701     @param    dt        <b>string</b>       File date
702     @return   <b>integer</b> New media ID
703     */
704     public function createFile($name,$title=null,$private=false,$dt=null,$force=true)
705     {
706          if (!$this->core->auth->check('media,media_admin',$this->core->blog->id)) {
707               throw new Exception(__('Permission denied.'));
708          }
[2566]709
[0]710          $file = $this->pwd.'/'.$name;
711          if (!file_exists($file)) {
712               return false;
713          }
[2566]714
[0]715          $media_file = $this->relpwd ? path::clean($this->relpwd.'/'.$name) : path::clean($name);
716          $media_type = files::getMimeType($name);
[2566]717
[0]718          $cur = $this->con->openCursor($this->table);
[2566]719
[0]720          $strReq = 'SELECT media_id '.
721                    'FROM '.$this->table.' '.
722                    "WHERE media_path = '".$this->con->escape($this->path)."' ".
723                    "AND media_file = '".$this->con->escape($media_file)."' ";
[2566]724
[0]725          $rs = $this->con->select($strReq);
[2566]726
[0]727          if ($rs->isEmpty())
728          {
729               $this->con->writeLock($this->table);
730               try
731               {
732                    $rs = $this->con->select('SELECT MAX(media_id) FROM '.$this->table);
733                    $media_id = (integer) $rs->f(0) + 1;
[2566]734
[0]735                    $cur->media_id = $media_id;
736                    $cur->user_id = (string) $this->core->auth->userID();
737                    $cur->media_path = (string) $this->path;
738                    $cur->media_file = (string) $media_file;
739                    $cur->media_dir = (string) dirname($media_file);
740                    $cur->media_creadt = date('Y-m-d H:i:s');
741                    $cur->media_upddt = date('Y-m-d H:i:s');
[2566]742
[0]743                    $cur->media_title = !$title ? (string) $name : (string) $title;
744                    $cur->media_private = (integer) (boolean) $private;
[2566]745
[0]746                    if ($dt) {
747                         $cur->media_dt = (string) $dt;
748                    } else {
749                         $cur->media_dt = strftime('%Y-%m-%d %H:%M:%S',filemtime($file));
750                    }
[2566]751
[0]752                    try {
753                         $cur->insert();
754                    } catch (Exception $e) {
755                         @unlink($name);
756                         throw $e;
757                    }
758                    $this->con->unlock();
759               }
760               catch (Exception $e)
761               {
762                    $this->con->unlock();
763                    throw $e;
764               }
765          }
766          else
767          {
768               $media_id = (integer) $rs->media_id;
[2566]769
[0]770               $cur->media_upddt = date('Y-m-d H:i:s');
[2566]771
[0]772               $cur->update('WHERE media_id = '.$media_id);
773          }
[2566]774
[0]775          $this->callFileHandler($media_type,'create',$cur,$name,$media_id,$force);
[2566]776
[0]777          return $media_id;
778     }
[2566]779
[0]780     /**
781     Updates a file in database.
[2566]782
[0]783     @param    file      <b>fileItem</b>     Current fileItem object
784     @param    newFile   <b>fileItem</b>     New fileItem object
785     */
786     public function updateFile($file,$newFile)
787     {
788          if (!$this->core->auth->check('media,media_admin',$this->core->blog->id)) {
789               throw new Exception(__('Permission denied.'));
790          }
[2566]791
[0]792          $id = (integer) $file->media_id;
[2566]793
[0]794          if (!$id) {
795               throw new Exception('No file ID');
796          }
[2566]797
[0]798          if (!$this->core->auth->check('media_admin',$this->core->blog->id)
799          && $this->core->auth->userID() != $file->media_user) {
800               throw new Exception(__('You are not the file owner.'));
801          }
[2566]802
[0]803          $cur = $this->con->openCursor($this->table);
[2566]804
[0]805          # We need to tidy newFile basename. If dir isn't empty, concat to basename
806          $newFile->relname = files::tidyFileName($newFile->basename);
807          if ($newFile->dir) {
808               $newFile->relname = $newFile->dir.'/'.$newFile->relname;
809          }
[2566]810
[0]811          if ($file->relname != $newFile->relname) {
812               $newFile->file = $this->root.'/'.$newFile->relname;
[2566]813
[0]814               if ($this->isFileExclude($newFile->relname)) {
815                    throw new Exception(__('This file is not allowed.'));
816               }
[2566]817
[0]818               if (file_exists($newFile->file)) {
819                    throw new Exception(__('New file already exists.'));
820               }
[2566]821
[0]822               $this->moveFile($file->relname,$newFile->relname);
[2566]823
[0]824               $cur->media_file = (string) $newFile->relname;
825               $cur->media_dir = (string) dirname($newFile->relname);
826          }
[2566]827
[0]828          $cur->media_title = (string) $newFile->media_title;
829          $cur->media_dt = (string) $newFile->media_dtstr;
830          $cur->media_upddt = date('Y-m-d H:i:s');
831          $cur->media_private = (integer) $newFile->media_priv;
[2566]832
[0]833          $cur->update('WHERE media_id = '.$id);
[2566]834
[0]835          $this->callFileHandler($file->type,'update',$file,$newFile);
836     }
[2566]837
[0]838     /**
839     Uploads a file.
[2566]840
[0]841     @param    tmp       <b>string</b>       Full path of temporary uploaded file
842     @param    name      <b>string</b>       File name (relative to working directory)
843     @param    title     <b>string</b>       File title
844     @param    private   <b>boolean</b>      File is private
845     */
846     public function uploadFile($tmp,$name,$title=null,$private=false,$overwrite=false)
847     {
848          if (!$this->core->auth->check('media,media_admin',$this->core->blog->id)) {
849               throw new Exception(__('Permission denied.'));
850          }
[2566]851
[0]852          $name = files::tidyFileName($name);
[2566]853
[0]854          parent::uploadFile($tmp,$name,$overwrite);
[2566]855
[0]856          return $this->createFile($name,$title,$private);
857     }
[2566]858
[0]859     /**
860     Creates a file from binary content.
[2566]861
[0]862     @param    name      <b>string</b>       File name (relative to working directory)
863     @param    bits      <b>string</b>       Binary file content
864     */
865     public function uploadBits($name,$bits)
866     {
867          if (!$this->core->auth->check('media,media_admin',$this->core->blog->id)) {
868               throw new Exception(__('Permission denied.'));
869          }
[2566]870
[0]871          $name = files::tidyFileName($name);
[2566]872
[0]873          parent::uploadBits($name,$bits);
[2566]874
[0]875          return $this->createFile($name,null,null);
876     }
[2566]877
[0]878     /**
879     Removes a file.
[2566]880
[0]881     @param    f         <b>fileItem</b>     fileItem object
882     */
883     public function removeFile($f)
884     {
885          if (!$this->core->auth->check('media,media_admin',$this->core->blog->id)) {
886               throw new Exception(__('Permission denied.'));
887          }
[2566]888
[0]889          $media_file = $this->relpwd ? path::clean($this->relpwd.'/'.$f) : path::clean($f);
[2566]890
[0]891          $strReq = 'DELETE FROM '.$this->table.' '.
892                    "WHERE media_path = '".$this->con->escape($this->path)."' ".
893                    "AND media_file = '".$this->con->escape($media_file)."' ";
[2566]894
[0]895          if (!$this->core->auth->check('media_admin',$this->core->blog->id))
896          {
897               $strReq .= "AND user_id = '".$this->con->escape($this->core->auth->userID())."'";
898          }
[2566]899
[0]900          $this->con->execute($strReq);
[2566]901
[0]902          if ($this->con->changes() == 0) {
903               throw new Exception(__('File does not exist in the database.'));
904          }
[2566]905
[0]906          parent::removeFile($f);
[2566]907
[0]908          $this->callFileHandler(files::getMimeType($media_file),'remove',$f);
909     }
[1103]910
911     /**
912     * Root directories
913     *
914     * Returns an array of directory under {@link $root} directory.
915     *
916     * @uses fileItem
917     * @return array
918     */
919     public function getDBDirs()
920     {
921          $media_dir = $this->relpwd ? $this->relpwd : '.';
[2566]922
[1103]923          $strReq =
924          'SELECT distinct media_dir '.
925          'FROM '.$this->table.' '.
926          "WHERE media_path = '".$this->path."'";
927          $rs = $this->con->select($strReq);
928          while ($rs->fetch()) {
929               if (is_dir($this->root.'/'.$rs->media_dir))
[1123]930                    $dir[] = ($rs->media_dir == '.' ? '' : $rs->media_dir);
[1103]931          }
[2566]932
[1103]933          return $dir;
934     }
[2566]935
[0]936     /**
937     Extract zip file in current location
[2566]938
[0]939     @param    f         <b>fileRecord</b>   fileRecord object
940     */
941     public function inflateZipFile($f,$create_dir=true)
942     {
943          $zip = new fileUnzip($f->file);
[705]944          $zip->setExcludePattern($this->exclude_pattern);
[0]945          $zip->getList(false,'#(^|/)(__MACOSX|\.svn|\.DS_Store|\.directory|Thumbs\.db)(/|$)#');
[2566]946
[0]947          if ($create_dir)
948          {
949               $zip_root_dir = $zip->getRootDir();
950               if ($zip_root_dir != false) {
951                    $destination = $zip_root_dir;
952                    $target = $f->dir;
953               } else {
954                    $destination = preg_replace('/\.([^.]+)$/','',$f->basename);
955                    $target = $f->dir.'/'.$destination;
956               }
[2566]957
[0]958               if (is_dir($f->dir.'/'.$destination)) {
[2566]959                    throw new Exception(sprintf(__('Extract destination directory %s already exists.'),dirname($f->relname).'/'.$destination));
[0]960               }
961          }
962          else
963          {
964               $target = $f->dir;
965               $destination = '';
966          }
[2566]967
[0]968          $zip->unzipAll($target);
969          $zip->close();
970          return dirname($f->relname).'/'.$destination;
971     }
[2566]972
[0]973     /**
974     Returns zip file content
[2566]975
[0]976     @param    f         <b>fileRecord</b>   fileRecord object
977     @return <b>array</b>
978     */
979     public function getZipContent($f)
980     {
981          $zip = new fileUnzip($f->file);
982          $list = $zip->getList(false,'#(^|/)(__MACOSX|\.svn|\.DS_Store|\.directory|Thumbs\.db)(/|$)#');
983          $zip->close();
984          return $list;
985     }
[2]986
987     /**
988     Calls file handlers registered for recreate event
[2566]989
[2]990     @param    f    <b>fileItem</b>     fileItem object
991     */
992     public function mediaFireRecreateEvent($f)
993     {
994          $media_type = files::getMimeType($f->basename);
995          $this->callFileHandler($media_type,'recreate',null,$f->basename); // Args list to be completed as necessary (Franck)
996     }
[2566]997
[0]998     /* Image handlers
999     ------------------------------------------------------- */
[3]1000     public function imageThumbCreate($cur,$f,$force=true)
[0]1001     {
1002          $file = $this->pwd.'/'.$f;
[2566]1003
[0]1004          if (!file_exists($file)) {
1005               return false;
1006          }
[2566]1007
[0]1008          $p = path::info($file);
[1381]1009          $alpha = ($p['extension'] == 'png') || ($p['extension'] == 'PNG');
1010          $thumb = sprintf(($alpha ? $this->thumb_tp_alpha : $this->thumb_tp),$p['dirname'],$p['base'],'%s');
[2566]1011
[0]1012          try
1013          {
1014               $img = new imageTools();
1015               $img->loadImage($file);
[2566]1016
[0]1017               $w = $img->getW();
1018               $h = $img->getH();
[2566]1019
[0]1020               if ($force) $this->imageThumbRemove($f);
[2566]1021
[0]1022               foreach ($this->thumb_sizes as $suffix => $s) {
1023                    $thumb_file = sprintf($thumb,$suffix);
1024                    if (!file_exists($thumb_file) && $s[0] > 0 &&
1025                         ($suffix == 'sq' || $w > $s[0] || $h > $s[0]))
1026                    {
[909]1027                         $rate = ($s[0] < 100 ? 95 : ($s[0] < 600 ? 90 : 85));
[0]1028                         $img->resize($s[0],$s[0],$s[1]);
[1381]1029                         $img->output(($alpha ? 'png' : 'jpeg'),$thumb_file,$rate);
[1120]1030                         $img->loadImage($file);
[0]1031                    }
1032               }
1033               $img->close();
1034          }
1035          catch (Exception $e)
1036          {
1037               if ($cur === null) { # Called only if cursor is null (public call)
1038                    throw $e;
1039               }
1040          }
1041     }
[2566]1042
[397]1043     protected function imageThumbUpdate($file,$newFile)
[0]1044     {
1045          if ($file->relname != $newFile->relname)
1046          {
1047               $p = path::info($file->relname);
[1381]1048               $alpha = ($p['extension'] == 'png') || ($p['extension'] == 'PNG');
1049               $thumb_old = sprintf(($alpha ? $this->thumb_tp_alpha : $this->thumb_tp),$p['dirname'],$p['base'],'%s');
[2566]1050
[0]1051               $p = path::info($newFile->relname);
[1381]1052               $alpha = ($p['extension'] == 'png') || ($p['extension'] == 'PNG');
1053               $thumb_new = sprintf(($alpha ? $this->thumb_tp_alpha : $this->thumb_tp),$p['dirname'],$p['base'],'%s');
[2566]1054
[0]1055               foreach ($this->thumb_sizes as $suffix => $s) {
1056                    try {
1057                         parent::moveFile(sprintf($thumb_old,$suffix),sprintf($thumb_new,$suffix));
1058                    } catch (Exception $e) {}
1059               }
1060          }
1061     }
[2566]1062
[2659]1063     public function imageThumbRemove($f)
[0]1064     {
1065          $p = path::info($f);
[1381]1066          $alpha = ($p['extension'] == 'png') || ($p['extension'] == 'PNG');
1067          $thumb = sprintf(($alpha ? $this->thumb_tp_alpha : $this->thumb_tp),'',$p['base'],'%s');
[2566]1068
[0]1069          foreach ($this->thumb_sizes as $suffix => $s) {
1070               try {
1071                    parent::removeFile(sprintf($thumb,$suffix));
1072               } catch (Exception $e) {}
1073          }
1074     }
[2566]1075
[0]1076     protected function imageMetaCreate($cur,$f,$id)
1077     {
1078          $file = $this->pwd.'/'.$f;
[2566]1079
[0]1080          if (!file_exists($file)) {
1081               return false;
1082          }
[2566]1083
[0]1084          $xml = new xmlTag('meta');
1085          $meta = imageMeta::readMeta($file);
1086          $xml->insertNode($meta);
[2566]1087
[0]1088          $c = $this->core->con->openCursor($this->table);
1089          $c->media_meta = $xml->toXML();
[2566]1090
[0]1091          if ($cur->media_title !== null && $cur->media_title == basename($cur->media_file))
1092          {
1093               if ($meta['Title']) {
1094                    $c->media_title = $meta['Title'];
1095               }
1096          }
[2566]1097
[0]1098          if ($meta['DateTimeOriginal'] && $cur->media_dt === '')
1099          {
1100               # We set picture time to user timezone
1101               $media_ts = strtotime($meta['DateTimeOriginal']);
1102               if ($media_ts !== false) {
1103                    $o = dt::getTimeOffset($this->core->auth->getInfo('user_tz'),$media_ts);
1104                    $c->media_dt = dt::str('%Y-%m-%d %H:%M:%S',$media_ts+$o);
1105               }
1106          }
[2566]1107
[0]1108          $c->update('WHERE media_id = '.$id);
1109     }
[2566]1110
[0]1111     /**
[2767]1112     Returns HTML code for audio player (HTML5 and if possible fallback Flash player)
1113
[3138]1114     @param  type        <b>string</b>       audio mime type
1115     @param    url            <b>string</b>       audio URL to play
1116     @param    player         <b>string</b>       Player URL (flash player fallback)
1117     @param    args      <b>array</b>        Player parameters (flash player fallback)
1118     @param  fallback    <b>boolean</b>      Include Flash player fallback
[3205]1119     @param    preload        <b>boolean</b>      Add preload="auto" attribute if true, else preload="none"
[2767]1120     @return   <b>string</b>
1121     */
[3205]1122     public static function audioPlayer($type,$url,$player=null,$args=null,$fallback=true,$preload=true)
[2767]1123     {
1124          $audio =
[3205]1125               '<audio controls preload="'.($preload ? 'auto' : 'none').'">'.
[2768]1126               '<source src="'.$url.'">';
[2767]1127
[3138]1128          if ($fallback && $type == 'audio/mpeg3') {
[2767]1129               // Include Flash player fallback
1130               if (!$player) {
1131                    $player = 'player_mp3.swf';
1132               }
1133
1134               if (!is_array($args))
1135               {
1136                    $args = array(
1137                         'showvolume' => 1,
1138                         'loadingcolor' => 'ff9900',
1139                         'bgcolor1' => 'eeeeee',
1140                         'bgcolor2' => 'cccccc',
1141                         'buttoncolor' => '0066cc',
1142                         'buttonovercolor' => 'ff9900',
1143                         'slidercolor1' => 'cccccc',
1144                         'slidercolor2' => '999999',
1145                         'sliderovercolor' => '0066cc'
1146                    );
1147               }
1148
1149               $args['mp3'] = $url;
1150
1151               if (empty($args['width'])) {
1152                    $args['width'] = 200;
1153               }
1154               if (empty($args['height'])) {
1155                    $args['height'] = 20;
1156               }
1157
1158               $vars = array();
1159               foreach ($args as $k => $v) {
1160                    $vars[] = $k.'='.$v;
1161               }
1162
1163               $audio .=
1164                    '<object type="application/x-shockwave-flash" '.
1165                    'data="'.$player.'" '.
1166                    'width="'.$args['width'].'" height="'.$args['height'].'">'.
1167                    '<param name="movie" value="'.$player.'" />'.
1168                    '<param name="wmode" value="transparent" />'.
1169                    '<param name="FlashVars" value="'.implode('&amp;',$vars).'" />'.
1170                    __('Embedded Audio Player').
[2768]1171                    '</object>';
[2767]1172          }
1173
1174          $audio .=
[2768]1175               '</audio>';
[2767]1176
1177          return $audio;
1178     }
1179
1180     /**
1181     Returns HTML code for video player (HTML5 and if possible fallback Flash player)
1182
[3138]1183     @param  type        <b>string</b>       video mime type
1184     @param    url            <b>string</b>       video URL to play
1185     @param    player         <b>string</b>       Player URL (flash player fallback)
1186     @param    args      <b>array</b>        Player parameters (flash player fallback)
1187     @param  fallback    <b>boolean</b>      Include Flash player fallback (if not .flv)
[3205]1188     @param    preload        <b>boolean</b>      Add preload="auto" attribute if true, else preload="none"
[2767]1189     @return   <b>string</b>
1190     */
[3205]1191     public static function videoPlayer($type,$url,$player=null,$args=null,$fallback=true,$preload=true)
[2767]1192     {
1193          $video = '';
[2768]1194
1195          // Cope with width and height, if given
1196          $width = 400;
1197          $height = 300;
1198          if (is_array($args)) {
1199               if (!empty($args['width']) && $args['width']) {
1200                    $width = (int) $args['width'];
1201               }
1202               if (!empty($args['height']) && $args['height']) {
1203                    $height = (int) $args['height'];
1204               }
1205          }
1206
[2767]1207          if ($type != 'video/x-flv') {
1208               $video =
[3205]1209                    '<video controls preload="'.($preload ? 'auto' : 'none').'"'.
1210                    ($width ? ' width="'.$width.'"' : '').
1211                    ($height ? ' height="'.$height.'"' : '').'>'.
[2768]1212                    '<source src="'.$url.'">';
[2767]1213          }
1214
[3138]1215          if ($type == 'video/x-flv' || ($fallback && ($type == 'video/mp4' || $type == 'video/x-m4v')))
[2767]1216          {
1217               // Include Flash player fallback
1218               if (!$player) {
1219                    $player = 'player_flv.swf';
1220               }
1221
1222               if (!is_array($args))
1223               {
1224                    $args = array(
1225                         'margin' => 1,
1226                         'showvolume' => 1,
1227                         'showtime' => 1,
1228                         'showfullscreen' => 1,
1229                         'buttonovercolor' => 'ff9900',
1230                         'slidercolor1' => 'cccccc',
1231                         'slidercolor2' => '999999',
1232                         'sliderovercolor' => '0066cc'
1233                    );
1234               }
1235
1236               $args['flv'] = $url;
1237
1238               if (empty($args['width'])) {
1239                    $args['width'] = 400;
1240               }
1241               if (empty($args['height'])) {
1242                    $args['height'] = 300;
1243               }
1244
1245               $vars = array();
1246               foreach ($args as $k => $v) {
1247                    $vars[] = $k.'='.$v;
1248               }
1249
1250               $video .=
1251                    '<object type="application/x-shockwave-flash" '.
1252                    'data="'.$player.'" '.
1253                    'width="'.$args['width'].'" height="'.$args['height'].'">'.
1254                    '<param name="movie" value="'.$player.'" />'.
1255                    '<param name="wmode" value="transparent" />'.
1256                    '<param name="allowFullScreen" value="true" />'.
1257                    '<param name="FlashVars" value="'.implode('&amp;',$vars).'" />'.
1258                    __('Embedded Video Player').
[2768]1259                    '</object>';
[2767]1260          }
1261
1262          if ($type != 'video/x-flv') {
1263               $video .=
[2768]1264                    '</video>';
[2767]1265          }
1266
1267          return $video;
1268     }
1269
1270     /**
[0]1271     Returns HTML code for MP3 player
[2566]1272
[3138]1273     @param    url            <b>string</b>       MP3 URL to play
1274     @param    player         <b>string</b>       Player URL
[0]1275     @param    args      <b>array</b>        Player parameters
[3138]1276     @param  fallback    <b>boolean</b>      Include Flash player fallback
[3205]1277     @param    preload        <b>boolean</b>      Add preload="auto" attribute if true, else preload="none"
[0]1278     @return   <b>string</b>
1279     */
[3205]1280     public static function mp3player($url,$player=null,$args=null,$fallback=true,$preload=true)
[0]1281     {
1282          if (!$player) {
1283               $player = 'player_mp3.swf';
1284          }
[2566]1285
[0]1286          if (!is_array($args))
1287          {
1288               $args = array(
1289                    'showvolume' => 1,
1290                    'loadingcolor' => 'ff9900',
1291                    'bgcolor1' => 'eeeeee',
1292                    'bgcolor2' => 'cccccc',
1293                    'buttoncolor' => '0066cc',
1294                    'buttonovercolor' => 'ff9900',
1295                    'slidercolor1' => 'cccccc',
1296                    'slidercolor2' => '999999',
1297                    'sliderovercolor' => '0066cc'
1298               );
1299          }
[2566]1300
[0]1301          $args['mp3'] = $url;
[2566]1302
[0]1303          if (empty($args['width'])) {
1304               $args['width'] = 200;
1305          }
1306          if (empty($args['height'])) {
1307               $args['height'] = 20;
1308          }
[2566]1309
[0]1310          $vars = array();
1311          foreach ($args as $k => $v) {
1312               $vars[] = $k.'='.$v;
1313          }
[2566]1314
[0]1315          return
[3205]1316          '<audio controls preload="'.($preload ? 'auto' : 'none').'">'.
[2763]1317          '<source src="'.$url.'" type="audio/mpeg">'.
[3138]1318          ($fallback ?
1319               '<object type="application/x-shockwave-flash" '.
1320               'data="'.$player.'" '.
1321               'width="'.$args['width'].'" height="'.$args['height'].'">'.
1322               '<param name="movie" value="'.$player.'" />'.
1323               '<param name="wmode" value="transparent" />'.
1324               '<param name="FlashVars" value="'.implode('&amp;',$vars).'" />'.
1325               __('Embedded Audio Player').
1326               '</object>' : '').
[2763]1327          '</audio>';
[0]1328     }
[2566]1329
[2767]1330     /**
1331     Returns HTML code for FLV player
1332
1333     @param    url       <b>string</b>       FLV URL to play
1334     @param    player    <b>string</b>       Player URL
1335     @param    args      <b>array</b>        Player parameters
1336     @return   <b>string</b>
1337     */
[0]1338     public static function flvplayer($url,$player=null,$args=null)
1339     {
1340          if (!$player) {
1341               $player = 'player_flv.swf';
1342          }
[2566]1343
[0]1344          if (!is_array($args))
1345          {
1346               $args = array(
1347                    'margin' => 1,
1348                    'showvolume' => 1,
1349                    'showtime' => 1,
1350                    'showfullscreen' => 1,
1351                    'buttonovercolor' => 'ff9900',
1352                    'slidercolor1' => 'cccccc',
1353                    'slidercolor2' => '999999',
1354                    'sliderovercolor' => '0066cc'
1355               );
1356          }
[2566]1357
[0]1358          $args['flv'] = $url;
[2566]1359
[0]1360          if (empty($args['width'])) {
1361               $args['width'] = 400;
1362          }
1363          if (empty($args['height'])) {
1364               $args['height'] = 300;
1365          }
[2566]1366
[0]1367          $vars = array();
1368          foreach ($args as $k => $v) {
1369               $vars[] = $k.'='.$v;
1370          }
[2566]1371
[0]1372          return
1373          '<object type="application/x-shockwave-flash" '.
1374          'data="'.$player.'" '.
1375          'width="'.$args['width'].'" height="'.$args['height'].'">'.
1376          '<param name="movie" value="'.$player.'" />'.
1377          '<param name="wmode" value="transparent" />'.
1378          '<param name="allowFullScreen" value="true" />'.
1379          '<param name="FlashVars" value="'.implode('&amp;',$vars).'" />'.
1380          __('Embedded Video Player').
1381          '</object>';
1382     }
1383}
Note: See TracBrowser for help on using the repository browser.

Sites map