[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 ----------------------------------------- |
---|
| 12 | if (!defined('DC_RC_PATH')) { return; } |
---|
| 13 | |
---|
| 14 | /** |
---|
| 15 | @ingroup DC_CORE |
---|
| 16 | @brief Dotclear media manage |
---|
| 17 | |
---|
| 18 | This class handles Dotclear media items. |
---|
| 19 | */ |
---|
| 20 | class 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 | { |
---|
| 352 | switch ($this->file_sort) |
---|
| 353 | { |
---|
| 354 | case 'date-asc': |
---|
| 355 | if ($a->media_dt == $b->media_dt) { |
---|
| 356 | return 0; |
---|
| 357 | } |
---|
| 358 | return ($a->media_dt < $b->media_dt) ? -1 : 1; |
---|
| 359 | case 'date-desc': |
---|
| 360 | if ($a->media_dt == $b->media_dt) { |
---|
| 361 | return 0; |
---|
| 362 | } |
---|
| 363 | return ($a->media_dt > $b->media_dt) ? -1 : 1; |
---|
| 364 | case 'name-desc': |
---|
| 365 | return strcasecmp($b->basename,$a->basename); |
---|
| 366 | case 'name-asc': |
---|
| 367 | default: |
---|
| 368 | return strcasecmp($a->basename,$b->basename); |
---|
| 369 | } |
---|
| 370 | } |
---|
[2098] | 371 | |
---|
| 372 | /** |
---|
| 373 | Gets current working directory content (using filesystem) |
---|
| 374 | |
---|
| 375 | */ |
---|
| 376 | public function getFSDir() |
---|
| 377 | { |
---|
| 378 | parent::getDir(); |
---|
| 379 | } |
---|
| 380 | |
---|
[0] | 381 | /** |
---|
[2566] | 382 | Gets current working directory content. |
---|
| 383 | |
---|
[0] | 384 | @param type <b>string</b> Media type filter |
---|
| 385 | */ |
---|
| 386 | public function getDir($type=null) |
---|
| 387 | { |
---|
| 388 | if ($type) { |
---|
| 389 | $this->type = $type; |
---|
| 390 | } |
---|
[2566] | 391 | |
---|
[0] | 392 | $media_dir = $this->relpwd ? $this->relpwd : '.'; |
---|
[2566] | 393 | |
---|
[0] | 394 | $strReq = |
---|
| 395 | 'SELECT media_file, media_id, media_path, media_title, media_meta, media_dt, '. |
---|
| 396 | 'media_creadt, media_upddt, media_private, user_id '. |
---|
| 397 | 'FROM '.$this->table.' '. |
---|
| 398 | "WHERE media_path = '".$this->path."' ". |
---|
| 399 | "AND media_dir = '".$this->con->escape($media_dir)."' "; |
---|
[2566] | 400 | |
---|
[0] | 401 | if (!$this->core->auth->check('media_admin',$this->core->blog->id)) |
---|
| 402 | { |
---|
| 403 | $strReq .= 'AND (media_private <> 1 '; |
---|
[2566] | 404 | |
---|
[0] | 405 | if ($this->core->auth->userID()) { |
---|
| 406 | $strReq .= "OR user_id = '".$this->con->escape($this->core->auth->userID())."'"; |
---|
| 407 | } |
---|
| 408 | $strReq .= ') '; |
---|
| 409 | } |
---|
[2566] | 410 | |
---|
[0] | 411 | $rs = $this->con->select($strReq); |
---|
[2566] | 412 | |
---|
[0] | 413 | parent::getDir(); |
---|
[2566] | 414 | |
---|
[0] | 415 | $f_res = array(); |
---|
| 416 | $p_dir = $this->dir; |
---|
[2566] | 417 | |
---|
[0] | 418 | # If type is set, remove items from p_dir |
---|
| 419 | if ($this->type) |
---|
| 420 | { |
---|
| 421 | foreach ($p_dir['files'] as $k => $f) { |
---|
| 422 | if ($f->type_prefix != $this->type) { |
---|
| 423 | unset($p_dir['files'][$k]); |
---|
| 424 | } |
---|
| 425 | } |
---|
| 426 | } |
---|
[2566] | 427 | |
---|
[0] | 428 | $f_reg = array(); |
---|
[2566] | 429 | |
---|
[0] | 430 | while ($rs->fetch()) |
---|
| 431 | { |
---|
| 432 | # File in subdirectory, forget about it! |
---|
| 433 | if (dirname($rs->media_file) != '.' && dirname($rs->media_file) != $this->relpwd) { |
---|
| 434 | continue; |
---|
| 435 | } |
---|
[2566] | 436 | |
---|
[0] | 437 | if ($this->inFiles($rs->media_file)) |
---|
| 438 | { |
---|
| 439 | $f = $this->fileRecord($rs); |
---|
| 440 | if ($f !== null) { |
---|
| 441 | if (isset($f_reg[$rs->media_file])) |
---|
| 442 | { |
---|
[2566] | 443 | # That media is duplicated in the database, |
---|
[0] | 444 | # time to do a bit of house cleaning. |
---|
| 445 | $this->con->execute( |
---|
| 446 | 'DELETE FROM '.$this->table.' '. |
---|
| 447 | "WHERE media_id = ".$this->fileRecord($rs)->media_id |
---|
| 448 | ); |
---|
| 449 | } else { |
---|
| 450 | $f_res[] = $this->fileRecord($rs); |
---|
| 451 | $f_reg[$rs->media_file] = 1; |
---|
| 452 | } |
---|
| 453 | } |
---|
| 454 | } |
---|
| 455 | elseif (!empty($p_dir['files']) && $this->relpwd == '') |
---|
| 456 | { |
---|
| 457 | # Physical file does not exist remove it from DB |
---|
| 458 | # Because we don't want to erase everything on |
---|
| 459 | # dotclear upgrade, do it only if there are files |
---|
| 460 | # in directory and directory is root |
---|
| 461 | $this->con->execute( |
---|
| 462 | 'DELETE FROM '.$this->table.' '. |
---|
| 463 | "WHERE media_path = '".$this->con->escape($this->path)."' ". |
---|
| 464 | "AND media_file = '".$this->con->escape($rs->media_file)."' " |
---|
| 465 | ); |
---|
| 466 | $this->callFileHandler(files::getMimeType($rs->media_file),'remove',$this->pwd.'/'.$rs->media_file); |
---|
| 467 | } |
---|
| 468 | } |
---|
[2566] | 469 | |
---|
[0] | 470 | $this->dir['files'] = $f_res; |
---|
| 471 | foreach ($this->dir['dirs'] as $k => $v) { |
---|
[1855] | 472 | $v->media_icon = sprintf($this->icon_img,($v->parent ? 'folder-up' : 'folder')); |
---|
[0] | 473 | } |
---|
[2566] | 474 | |
---|
[0] | 475 | # Check files that don't exist in database and create them |
---|
| 476 | if ($this->core->auth->check('media,media_admin',$this->core->blog->id)) |
---|
| 477 | { |
---|
| 478 | foreach ($p_dir['files'] as $f) |
---|
| 479 | { |
---|
| 480 | if (!isset($f_reg[$f->relname])) { |
---|
| 481 | if (($id = $this->createFile($f->basename,null,false,null,false)) !== false) { |
---|
| 482 | $this->dir['files'][] = $this->getFile($id); |
---|
| 483 | } |
---|
| 484 | } |
---|
| 485 | } |
---|
| 486 | } |
---|
| 487 | usort($this->dir['files'],array($this,'sortFileHandler')); |
---|
| 488 | } |
---|
[2566] | 489 | |
---|
[0] | 490 | /** |
---|
| 491 | Gets file by its id. Returns a filteItem object. |
---|
[2566] | 492 | |
---|
[0] | 493 | @param id <b>integer</b> File ID |
---|
| 494 | @return <b>fileItem</b> |
---|
| 495 | */ |
---|
| 496 | public function getFile($id) |
---|
| 497 | { |
---|
| 498 | $strReq = |
---|
| 499 | 'SELECT media_id, media_path, media_title, '. |
---|
| 500 | 'media_file, media_meta, media_dt, media_creadt, '. |
---|
| 501 | 'media_upddt, media_private, user_id '. |
---|
| 502 | 'FROM '.$this->table.' '. |
---|
| 503 | "WHERE media_path = '".$this->path."' ". |
---|
| 504 | 'AND media_id = '.(integer) $id.' '; |
---|
[2566] | 505 | |
---|
[0] | 506 | if (!$this->core->auth->check('media_admin',$this->core->blog->id)) |
---|
| 507 | { |
---|
| 508 | $strReq .= 'AND (media_private <> 1 '; |
---|
[2566] | 509 | |
---|
[0] | 510 | if ($this->core->auth->userID()) { |
---|
| 511 | $strReq .= "OR user_id = '".$this->con->escape($this->core->auth->userID())."'"; |
---|
| 512 | } |
---|
| 513 | $strReq .= ') '; |
---|
| 514 | } |
---|
[2566] | 515 | |
---|
[0] | 516 | $rs = $this->con->select($strReq); |
---|
| 517 | return $this->fileRecord($rs); |
---|
| 518 | } |
---|
[2566] | 519 | |
---|
[0] | 520 | /** |
---|
[3099] | 521 | Search into media db (only). |
---|
| 522 | |
---|
| 523 | @param query <b>string</b> Search query |
---|
| 524 | @return boolean true or false if nothing found |
---|
| 525 | */ |
---|
| 526 | public function searchMedia($query) |
---|
| 527 | { |
---|
| 528 | if ($query == '') { |
---|
| 529 | return false; |
---|
| 530 | } |
---|
| 531 | |
---|
| 532 | $strReq = |
---|
| 533 | 'SELECT media_file, media_id, media_path, media_title, media_meta, media_dt, '. |
---|
| 534 | 'media_creadt, media_upddt, media_private, user_id '. |
---|
| 535 | 'FROM '.$this->table.' '. |
---|
| 536 | "WHERE media_path = '".$this->path."' ". |
---|
| 537 | "AND (media_title LIKE '%".$this->con->escape($query)."%' ". |
---|
| 538 | " OR media_file LIKE '%".$this->con->escape($query)."%' ". |
---|
| 539 | " OR media_meta LIKE '<Description>%".$this->con->escape($query)."%</Description>')"; |
---|
| 540 | |
---|
| 541 | if (!$this->core->auth->check('media_admin',$this->core->blog->id)) |
---|
| 542 | { |
---|
| 543 | $strReq .= 'AND (media_private <> 1 '; |
---|
| 544 | |
---|
| 545 | if ($this->core->auth->userID()) { |
---|
| 546 | $strReq .= "OR user_id = '".$this->con->escape($this->core->auth->userID())."'"; |
---|
| 547 | } |
---|
| 548 | $strReq .= ') '; |
---|
| 549 | } |
---|
| 550 | |
---|
| 551 | $rs = $this->con->select($strReq); |
---|
| 552 | |
---|
| 553 | $this->dir = array('dirs' => array(),'files' => array()); |
---|
| 554 | $f_res = array(); |
---|
| 555 | while ($rs->fetch()) |
---|
| 556 | { |
---|
| 557 | $fr = $this->fileRecord($rs); |
---|
| 558 | if ($fr) { |
---|
| 559 | $f_res[] = $fr; |
---|
| 560 | } |
---|
| 561 | } |
---|
| 562 | $this->dir['files'] = $f_res; |
---|
| 563 | |
---|
| 564 | try { |
---|
| 565 | usort($this->dir['files'],array($this,'sortFileHandler')); |
---|
| 566 | } catch (Exception $e) {} |
---|
| 567 | |
---|
| 568 | return (count($f_res) > 0 ? true : false); |
---|
| 569 | } |
---|
| 570 | |
---|
| 571 | /** |
---|
[0] | 572 | Returns media items attached to a blog post. Result is an array containing |
---|
| 573 | fileItems objects. |
---|
[2566] | 574 | |
---|
[0] | 575 | @param post_id <b>integer</b> Post ID |
---|
| 576 | @param media_id <b>integer</b> Optionnal media ID |
---|
[1280] | 577 | @return <b>array</b> Array of fileItems |
---|
[0] | 578 | */ |
---|
[1280] | 579 | public function getPostMedia($post_id,$media_id=null) |
---|
[0] | 580 | { |
---|
[407] | 581 | $params = array( |
---|
| 582 | 'post_id' => $post_id, |
---|
| 583 | 'media_path' => $this->path |
---|
| 584 | ); |
---|
[0] | 585 | if ($media_id) { |
---|
[407] | 586 | $params['media_id'] = (integer) $media_id; |
---|
[0] | 587 | } |
---|
[407] | 588 | $rs = $this->postmedia->getPostMedia($params); |
---|
[2566] | 589 | |
---|
[0] | 590 | $res = array(); |
---|
[2566] | 591 | |
---|
[0] | 592 | while ($rs->fetch()) { |
---|
| 593 | $f = $this->fileRecord($rs); |
---|
| 594 | if ($f !== null) { |
---|
[1280] | 595 | $res[] = $f; |
---|
[0] | 596 | } |
---|
| 597 | } |
---|
[2566] | 598 | |
---|
[1280] | 599 | return $res; |
---|
[0] | 600 | } |
---|
[2566] | 601 | |
---|
[0] | 602 | /** |
---|
[407] | 603 | @deprecated since version 2.4 |
---|
| 604 | @see dcPostMedia::addPostMedia |
---|
[0] | 605 | */ |
---|
| 606 | public function addPostMedia($post_id,$media_id) |
---|
| 607 | { |
---|
[407] | 608 | $this->postmedia->addPostMedia($post_id,$media_id); |
---|
[0] | 609 | } |
---|
[2566] | 610 | |
---|
[0] | 611 | /** |
---|
[407] | 612 | @deprecated since version 2.4 |
---|
| 613 | @see dcPostMedia::removePostMedia |
---|
[0] | 614 | */ |
---|
| 615 | public function removePostMedia($post_id,$media_id) |
---|
| 616 | { |
---|
[407] | 617 | $this->postmedia->removePostMedia($post_id,$media_id,"attachment"); |
---|
[0] | 618 | } |
---|
[2566] | 619 | |
---|
[0] | 620 | /** |
---|
| 621 | Rebuilds database items collection. Optional <var>$pwd</var> parameter is |
---|
| 622 | the path where to start rebuild. |
---|
[2566] | 623 | |
---|
[0] | 624 | @param pwd <b>string</b> Directory to rebuild |
---|
| 625 | */ |
---|
| 626 | public function rebuild($pwd='') |
---|
| 627 | { |
---|
| 628 | if (!$this->core->auth->isSuperAdmin()) { |
---|
| 629 | throw new Exception(__('You are not a super administrator.')); |
---|
| 630 | } |
---|
[2566] | 631 | |
---|
[0] | 632 | $this->chdir($pwd); |
---|
| 633 | parent::getDir(); |
---|
[2566] | 634 | |
---|
[0] | 635 | $dir = $this->dir; |
---|
[2566] | 636 | |
---|
[0] | 637 | foreach ($dir['dirs'] as $d) { |
---|
| 638 | if (!$d->parent) { |
---|
| 639 | $this->rebuild($d->relname,false); |
---|
| 640 | } |
---|
| 641 | } |
---|
[2566] | 642 | |
---|
[0] | 643 | foreach ($dir['files'] as $f) { |
---|
| 644 | $this->chdir(dirname($f->relname)); |
---|
| 645 | $this->createFile($f->basename); |
---|
| 646 | } |
---|
[2566] | 647 | |
---|
[0] | 648 | $this->rebuildDB($pwd); |
---|
| 649 | } |
---|
[2566] | 650 | |
---|
[0] | 651 | protected function rebuildDB($pwd) |
---|
| 652 | { |
---|
| 653 | $media_dir = $pwd ? $pwd : '.'; |
---|
[2566] | 654 | |
---|
[0] | 655 | $strReq = |
---|
| 656 | 'SELECT media_file, media_id '. |
---|
| 657 | 'FROM '.$this->table.' '. |
---|
| 658 | "WHERE media_path = '".$this->path."' ". |
---|
| 659 | "AND media_dir = '".$this->con->escape($media_dir)."' "; |
---|
[2566] | 660 | |
---|
[0] | 661 | $rs = $this->con->select($strReq); |
---|
[2566] | 662 | |
---|
[0] | 663 | $delReq = 'DELETE FROM '.$this->table.' '. |
---|
| 664 | 'WHERE media_id IN (%s) '; |
---|
| 665 | $del_ids = array(); |
---|
[2566] | 666 | |
---|
[0] | 667 | while ($rs->fetch()) |
---|
| 668 | { |
---|
| 669 | if (!is_file($this->root.'/'.$rs->media_file)) { |
---|
| 670 | $del_ids[] = (integer) $rs->media_id; |
---|
| 671 | } |
---|
| 672 | } |
---|
[2566] | 673 | |
---|
[0] | 674 | if (!empty($del_ids)) { |
---|
| 675 | $this->con->execute(sprintf($delReq,implode(',',$del_ids))); |
---|
| 676 | } |
---|
| 677 | } |
---|
[2566] | 678 | |
---|
[0] | 679 | public function makeDir($d) |
---|
| 680 | { |
---|
| 681 | $d = files::tidyFileName($d); |
---|
| 682 | parent::makeDir($d); |
---|
| 683 | } |
---|
[2566] | 684 | |
---|
[0] | 685 | /** |
---|
| 686 | Creates or updates a file in database. Returns new media ID or false if |
---|
| 687 | file does not exist. |
---|
[2566] | 688 | |
---|
[0] | 689 | @param name <b>string</b> File name (relative to working directory) |
---|
| 690 | @param title <b>string</b> File title |
---|
| 691 | @param private <b>boolean</b> File is private |
---|
| 692 | @param dt <b>string</b> File date |
---|
| 693 | @return <b>integer</b> New media ID |
---|
| 694 | */ |
---|
| 695 | public function createFile($name,$title=null,$private=false,$dt=null,$force=true) |
---|
| 696 | { |
---|
| 697 | if (!$this->core->auth->check('media,media_admin',$this->core->blog->id)) { |
---|
| 698 | throw new Exception(__('Permission denied.')); |
---|
| 699 | } |
---|
[2566] | 700 | |
---|
[0] | 701 | $file = $this->pwd.'/'.$name; |
---|
| 702 | if (!file_exists($file)) { |
---|
| 703 | return false; |
---|
| 704 | } |
---|
[2566] | 705 | |
---|
[0] | 706 | $media_file = $this->relpwd ? path::clean($this->relpwd.'/'.$name) : path::clean($name); |
---|
| 707 | $media_type = files::getMimeType($name); |
---|
[2566] | 708 | |
---|
[0] | 709 | $cur = $this->con->openCursor($this->table); |
---|
[2566] | 710 | |
---|
[0] | 711 | $strReq = 'SELECT media_id '. |
---|
| 712 | 'FROM '.$this->table.' '. |
---|
| 713 | "WHERE media_path = '".$this->con->escape($this->path)."' ". |
---|
| 714 | "AND media_file = '".$this->con->escape($media_file)."' "; |
---|
[2566] | 715 | |
---|
[0] | 716 | $rs = $this->con->select($strReq); |
---|
[2566] | 717 | |
---|
[0] | 718 | if ($rs->isEmpty()) |
---|
| 719 | { |
---|
| 720 | $this->con->writeLock($this->table); |
---|
| 721 | try |
---|
| 722 | { |
---|
| 723 | $rs = $this->con->select('SELECT MAX(media_id) FROM '.$this->table); |
---|
| 724 | $media_id = (integer) $rs->f(0) + 1; |
---|
[2566] | 725 | |
---|
[0] | 726 | $cur->media_id = $media_id; |
---|
| 727 | $cur->user_id = (string) $this->core->auth->userID(); |
---|
| 728 | $cur->media_path = (string) $this->path; |
---|
| 729 | $cur->media_file = (string) $media_file; |
---|
| 730 | $cur->media_dir = (string) dirname($media_file); |
---|
| 731 | $cur->media_creadt = date('Y-m-d H:i:s'); |
---|
| 732 | $cur->media_upddt = date('Y-m-d H:i:s'); |
---|
[2566] | 733 | |
---|
[0] | 734 | $cur->media_title = !$title ? (string) $name : (string) $title; |
---|
| 735 | $cur->media_private = (integer) (boolean) $private; |
---|
[2566] | 736 | |
---|
[0] | 737 | if ($dt) { |
---|
| 738 | $cur->media_dt = (string) $dt; |
---|
| 739 | } else { |
---|
| 740 | $cur->media_dt = strftime('%Y-%m-%d %H:%M:%S',filemtime($file)); |
---|
| 741 | } |
---|
[2566] | 742 | |
---|
[0] | 743 | try { |
---|
| 744 | $cur->insert(); |
---|
| 745 | } catch (Exception $e) { |
---|
| 746 | @unlink($name); |
---|
| 747 | throw $e; |
---|
| 748 | } |
---|
| 749 | $this->con->unlock(); |
---|
| 750 | } |
---|
| 751 | catch (Exception $e) |
---|
| 752 | { |
---|
| 753 | $this->con->unlock(); |
---|
| 754 | throw $e; |
---|
| 755 | } |
---|
| 756 | } |
---|
| 757 | else |
---|
| 758 | { |
---|
| 759 | $media_id = (integer) $rs->media_id; |
---|
[2566] | 760 | |
---|
[0] | 761 | $cur->media_upddt = date('Y-m-d H:i:s'); |
---|
[2566] | 762 | |
---|
[0] | 763 | $cur->update('WHERE media_id = '.$media_id); |
---|
| 764 | } |
---|
[2566] | 765 | |
---|
[0] | 766 | $this->callFileHandler($media_type,'create',$cur,$name,$media_id,$force); |
---|
[2566] | 767 | |
---|
[0] | 768 | return $media_id; |
---|
| 769 | } |
---|
[2566] | 770 | |
---|
[0] | 771 | /** |
---|
| 772 | Updates a file in database. |
---|
[2566] | 773 | |
---|
[0] | 774 | @param file <b>fileItem</b> Current fileItem object |
---|
| 775 | @param newFile <b>fileItem</b> New fileItem object |
---|
| 776 | */ |
---|
| 777 | public function updateFile($file,$newFile) |
---|
| 778 | { |
---|
| 779 | if (!$this->core->auth->check('media,media_admin',$this->core->blog->id)) { |
---|
| 780 | throw new Exception(__('Permission denied.')); |
---|
| 781 | } |
---|
[2566] | 782 | |
---|
[0] | 783 | $id = (integer) $file->media_id; |
---|
[2566] | 784 | |
---|
[0] | 785 | if (!$id) { |
---|
| 786 | throw new Exception('No file ID'); |
---|
| 787 | } |
---|
[2566] | 788 | |
---|
[0] | 789 | if (!$this->core->auth->check('media_admin',$this->core->blog->id) |
---|
| 790 | && $this->core->auth->userID() != $file->media_user) { |
---|
| 791 | throw new Exception(__('You are not the file owner.')); |
---|
| 792 | } |
---|
[2566] | 793 | |
---|
[0] | 794 | $cur = $this->con->openCursor($this->table); |
---|
[2566] | 795 | |
---|
[0] | 796 | # We need to tidy newFile basename. If dir isn't empty, concat to basename |
---|
| 797 | $newFile->relname = files::tidyFileName($newFile->basename); |
---|
| 798 | if ($newFile->dir) { |
---|
| 799 | $newFile->relname = $newFile->dir.'/'.$newFile->relname; |
---|
| 800 | } |
---|
[2566] | 801 | |
---|
[0] | 802 | if ($file->relname != $newFile->relname) { |
---|
| 803 | $newFile->file = $this->root.'/'.$newFile->relname; |
---|
[2566] | 804 | |
---|
[0] | 805 | if ($this->isFileExclude($newFile->relname)) { |
---|
| 806 | throw new Exception(__('This file is not allowed.')); |
---|
| 807 | } |
---|
[2566] | 808 | |
---|
[0] | 809 | if (file_exists($newFile->file)) { |
---|
| 810 | throw new Exception(__('New file already exists.')); |
---|
| 811 | } |
---|
[2566] | 812 | |
---|
[0] | 813 | $this->moveFile($file->relname,$newFile->relname); |
---|
[2566] | 814 | |
---|
[0] | 815 | $cur->media_file = (string) $newFile->relname; |
---|
| 816 | $cur->media_dir = (string) dirname($newFile->relname); |
---|
| 817 | } |
---|
[2566] | 818 | |
---|
[0] | 819 | $cur->media_title = (string) $newFile->media_title; |
---|
| 820 | $cur->media_dt = (string) $newFile->media_dtstr; |
---|
| 821 | $cur->media_upddt = date('Y-m-d H:i:s'); |
---|
| 822 | $cur->media_private = (integer) $newFile->media_priv; |
---|
[2566] | 823 | |
---|
[0] | 824 | $cur->update('WHERE media_id = '.$id); |
---|
[2566] | 825 | |
---|
[0] | 826 | $this->callFileHandler($file->type,'update',$file,$newFile); |
---|
| 827 | } |
---|
[2566] | 828 | |
---|
[0] | 829 | /** |
---|
| 830 | Uploads a file. |
---|
[2566] | 831 | |
---|
[0] | 832 | @param tmp <b>string</b> Full path of temporary uploaded file |
---|
| 833 | @param name <b>string</b> File name (relative to working directory) |
---|
| 834 | @param title <b>string</b> File title |
---|
| 835 | @param private <b>boolean</b> File is private |
---|
| 836 | */ |
---|
| 837 | public function uploadFile($tmp,$name,$title=null,$private=false,$overwrite=false) |
---|
| 838 | { |
---|
| 839 | if (!$this->core->auth->check('media,media_admin',$this->core->blog->id)) { |
---|
| 840 | throw new Exception(__('Permission denied.')); |
---|
| 841 | } |
---|
[2566] | 842 | |
---|
[0] | 843 | $name = files::tidyFileName($name); |
---|
[2566] | 844 | |
---|
[0] | 845 | parent::uploadFile($tmp,$name,$overwrite); |
---|
[2566] | 846 | |
---|
[0] | 847 | return $this->createFile($name,$title,$private); |
---|
| 848 | } |
---|
[2566] | 849 | |
---|
[0] | 850 | /** |
---|
| 851 | Creates a file from binary content. |
---|
[2566] | 852 | |
---|
[0] | 853 | @param name <b>string</b> File name (relative to working directory) |
---|
| 854 | @param bits <b>string</b> Binary file content |
---|
| 855 | */ |
---|
| 856 | public function uploadBits($name,$bits) |
---|
| 857 | { |
---|
| 858 | if (!$this->core->auth->check('media,media_admin',$this->core->blog->id)) { |
---|
| 859 | throw new Exception(__('Permission denied.')); |
---|
| 860 | } |
---|
[2566] | 861 | |
---|
[0] | 862 | $name = files::tidyFileName($name); |
---|
[2566] | 863 | |
---|
[0] | 864 | parent::uploadBits($name,$bits); |
---|
[2566] | 865 | |
---|
[0] | 866 | return $this->createFile($name,null,null); |
---|
| 867 | } |
---|
[2566] | 868 | |
---|
[0] | 869 | /** |
---|
| 870 | Removes a file. |
---|
[2566] | 871 | |
---|
[0] | 872 | @param f <b>fileItem</b> fileItem object |
---|
| 873 | */ |
---|
| 874 | public function removeFile($f) |
---|
| 875 | { |
---|
| 876 | if (!$this->core->auth->check('media,media_admin',$this->core->blog->id)) { |
---|
| 877 | throw new Exception(__('Permission denied.')); |
---|
| 878 | } |
---|
[2566] | 879 | |
---|
[0] | 880 | $media_file = $this->relpwd ? path::clean($this->relpwd.'/'.$f) : path::clean($f); |
---|
[2566] | 881 | |
---|
[0] | 882 | $strReq = 'DELETE FROM '.$this->table.' '. |
---|
| 883 | "WHERE media_path = '".$this->con->escape($this->path)."' ". |
---|
| 884 | "AND media_file = '".$this->con->escape($media_file)."' "; |
---|
[2566] | 885 | |
---|
[0] | 886 | if (!$this->core->auth->check('media_admin',$this->core->blog->id)) |
---|
| 887 | { |
---|
| 888 | $strReq .= "AND user_id = '".$this->con->escape($this->core->auth->userID())."'"; |
---|
| 889 | } |
---|
[2566] | 890 | |
---|
[0] | 891 | $this->con->execute($strReq); |
---|
[2566] | 892 | |
---|
[0] | 893 | if ($this->con->changes() == 0) { |
---|
| 894 | throw new Exception(__('File does not exist in the database.')); |
---|
| 895 | } |
---|
[2566] | 896 | |
---|
[0] | 897 | parent::removeFile($f); |
---|
[2566] | 898 | |
---|
[0] | 899 | $this->callFileHandler(files::getMimeType($media_file),'remove',$f); |
---|
| 900 | } |
---|
[1103] | 901 | |
---|
| 902 | /** |
---|
| 903 | * Root directories |
---|
| 904 | * |
---|
| 905 | * Returns an array of directory under {@link $root} directory. |
---|
| 906 | * |
---|
| 907 | * @uses fileItem |
---|
| 908 | * @return array |
---|
| 909 | */ |
---|
| 910 | public function getDBDirs() |
---|
| 911 | { |
---|
| 912 | $media_dir = $this->relpwd ? $this->relpwd : '.'; |
---|
[2566] | 913 | |
---|
[1103] | 914 | $strReq = |
---|
| 915 | 'SELECT distinct media_dir '. |
---|
| 916 | 'FROM '.$this->table.' '. |
---|
| 917 | "WHERE media_path = '".$this->path."'"; |
---|
| 918 | $rs = $this->con->select($strReq); |
---|
| 919 | while ($rs->fetch()) { |
---|
| 920 | if (is_dir($this->root.'/'.$rs->media_dir)) |
---|
[1123] | 921 | $dir[] = ($rs->media_dir == '.' ? '' : $rs->media_dir); |
---|
[1103] | 922 | } |
---|
[2566] | 923 | |
---|
[1103] | 924 | return $dir; |
---|
| 925 | } |
---|
[2566] | 926 | |
---|
[0] | 927 | /** |
---|
| 928 | Extract zip file in current location |
---|
[2566] | 929 | |
---|
[0] | 930 | @param f <b>fileRecord</b> fileRecord object |
---|
| 931 | */ |
---|
| 932 | public function inflateZipFile($f,$create_dir=true) |
---|
| 933 | { |
---|
| 934 | $zip = new fileUnzip($f->file); |
---|
[705] | 935 | $zip->setExcludePattern($this->exclude_pattern); |
---|
[0] | 936 | $zip->getList(false,'#(^|/)(__MACOSX|\.svn|\.DS_Store|\.directory|Thumbs\.db)(/|$)#'); |
---|
[2566] | 937 | |
---|
[0] | 938 | if ($create_dir) |
---|
| 939 | { |
---|
| 940 | $zip_root_dir = $zip->getRootDir(); |
---|
| 941 | if ($zip_root_dir != false) { |
---|
| 942 | $destination = $zip_root_dir; |
---|
| 943 | $target = $f->dir; |
---|
| 944 | } else { |
---|
| 945 | $destination = preg_replace('/\.([^.]+)$/','',$f->basename); |
---|
| 946 | $target = $f->dir.'/'.$destination; |
---|
| 947 | } |
---|
[2566] | 948 | |
---|
[0] | 949 | if (is_dir($f->dir.'/'.$destination)) { |
---|
[2566] | 950 | throw new Exception(sprintf(__('Extract destination directory %s already exists.'),dirname($f->relname).'/'.$destination)); |
---|
[0] | 951 | } |
---|
| 952 | } |
---|
| 953 | else |
---|
| 954 | { |
---|
| 955 | $target = $f->dir; |
---|
| 956 | $destination = ''; |
---|
| 957 | } |
---|
[2566] | 958 | |
---|
[0] | 959 | $zip->unzipAll($target); |
---|
| 960 | $zip->close(); |
---|
| 961 | return dirname($f->relname).'/'.$destination; |
---|
| 962 | } |
---|
[2566] | 963 | |
---|
[0] | 964 | /** |
---|
| 965 | Returns zip file content |
---|
[2566] | 966 | |
---|
[0] | 967 | @param f <b>fileRecord</b> fileRecord object |
---|
| 968 | @return <b>array</b> |
---|
| 969 | */ |
---|
| 970 | public function getZipContent($f) |
---|
| 971 | { |
---|
| 972 | $zip = new fileUnzip($f->file); |
---|
| 973 | $list = $zip->getList(false,'#(^|/)(__MACOSX|\.svn|\.DS_Store|\.directory|Thumbs\.db)(/|$)#'); |
---|
| 974 | $zip->close(); |
---|
| 975 | return $list; |
---|
| 976 | } |
---|
[2] | 977 | |
---|
| 978 | /** |
---|
| 979 | Calls file handlers registered for recreate event |
---|
[2566] | 980 | |
---|
[2] | 981 | @param f <b>fileItem</b> fileItem object |
---|
| 982 | */ |
---|
| 983 | public function mediaFireRecreateEvent($f) |
---|
| 984 | { |
---|
| 985 | $media_type = files::getMimeType($f->basename); |
---|
| 986 | $this->callFileHandler($media_type,'recreate',null,$f->basename); // Args list to be completed as necessary (Franck) |
---|
| 987 | } |
---|
[2566] | 988 | |
---|
[0] | 989 | /* Image handlers |
---|
| 990 | ------------------------------------------------------- */ |
---|
[3] | 991 | public function imageThumbCreate($cur,$f,$force=true) |
---|
[0] | 992 | { |
---|
| 993 | $file = $this->pwd.'/'.$f; |
---|
[2566] | 994 | |
---|
[0] | 995 | if (!file_exists($file)) { |
---|
| 996 | return false; |
---|
| 997 | } |
---|
[2566] | 998 | |
---|
[0] | 999 | $p = path::info($file); |
---|
[1381] | 1000 | $alpha = ($p['extension'] == 'png') || ($p['extension'] == 'PNG'); |
---|
| 1001 | $thumb = sprintf(($alpha ? $this->thumb_tp_alpha : $this->thumb_tp),$p['dirname'],$p['base'],'%s'); |
---|
[2566] | 1002 | |
---|
[0] | 1003 | try |
---|
| 1004 | { |
---|
| 1005 | $img = new imageTools(); |
---|
| 1006 | $img->loadImage($file); |
---|
[2566] | 1007 | |
---|
[0] | 1008 | $w = $img->getW(); |
---|
| 1009 | $h = $img->getH(); |
---|
[2566] | 1010 | |
---|
[0] | 1011 | if ($force) $this->imageThumbRemove($f); |
---|
[2566] | 1012 | |
---|
[0] | 1013 | foreach ($this->thumb_sizes as $suffix => $s) { |
---|
| 1014 | $thumb_file = sprintf($thumb,$suffix); |
---|
| 1015 | if (!file_exists($thumb_file) && $s[0] > 0 && |
---|
| 1016 | ($suffix == 'sq' || $w > $s[0] || $h > $s[0])) |
---|
| 1017 | { |
---|
[909] | 1018 | $rate = ($s[0] < 100 ? 95 : ($s[0] < 600 ? 90 : 85)); |
---|
[0] | 1019 | $img->resize($s[0],$s[0],$s[1]); |
---|
[1381] | 1020 | $img->output(($alpha ? 'png' : 'jpeg'),$thumb_file,$rate); |
---|
[1120] | 1021 | $img->loadImage($file); |
---|
[0] | 1022 | } |
---|
| 1023 | } |
---|
| 1024 | $img->close(); |
---|
| 1025 | } |
---|
| 1026 | catch (Exception $e) |
---|
| 1027 | { |
---|
| 1028 | if ($cur === null) { # Called only if cursor is null (public call) |
---|
| 1029 | throw $e; |
---|
| 1030 | } |
---|
| 1031 | } |
---|
| 1032 | } |
---|
[2566] | 1033 | |
---|
[397] | 1034 | protected function imageThumbUpdate($file,$newFile) |
---|
[0] | 1035 | { |
---|
| 1036 | if ($file->relname != $newFile->relname) |
---|
| 1037 | { |
---|
| 1038 | $p = path::info($file->relname); |
---|
[1381] | 1039 | $alpha = ($p['extension'] == 'png') || ($p['extension'] == 'PNG'); |
---|
| 1040 | $thumb_old = sprintf(($alpha ? $this->thumb_tp_alpha : $this->thumb_tp),$p['dirname'],$p['base'],'%s'); |
---|
[2566] | 1041 | |
---|
[0] | 1042 | $p = path::info($newFile->relname); |
---|
[1381] | 1043 | $alpha = ($p['extension'] == 'png') || ($p['extension'] == 'PNG'); |
---|
| 1044 | $thumb_new = sprintf(($alpha ? $this->thumb_tp_alpha : $this->thumb_tp),$p['dirname'],$p['base'],'%s'); |
---|
[2566] | 1045 | |
---|
[0] | 1046 | foreach ($this->thumb_sizes as $suffix => $s) { |
---|
| 1047 | try { |
---|
| 1048 | parent::moveFile(sprintf($thumb_old,$suffix),sprintf($thumb_new,$suffix)); |
---|
| 1049 | } catch (Exception $e) {} |
---|
| 1050 | } |
---|
| 1051 | } |
---|
| 1052 | } |
---|
[2566] | 1053 | |
---|
[2659] | 1054 | public function imageThumbRemove($f) |
---|
[0] | 1055 | { |
---|
| 1056 | $p = path::info($f); |
---|
[1381] | 1057 | $alpha = ($p['extension'] == 'png') || ($p['extension'] == 'PNG'); |
---|
| 1058 | $thumb = sprintf(($alpha ? $this->thumb_tp_alpha : $this->thumb_tp),'',$p['base'],'%s'); |
---|
[2566] | 1059 | |
---|
[0] | 1060 | foreach ($this->thumb_sizes as $suffix => $s) { |
---|
| 1061 | try { |
---|
| 1062 | parent::removeFile(sprintf($thumb,$suffix)); |
---|
| 1063 | } catch (Exception $e) {} |
---|
| 1064 | } |
---|
| 1065 | } |
---|
[2566] | 1066 | |
---|
[0] | 1067 | protected function imageMetaCreate($cur,$f,$id) |
---|
| 1068 | { |
---|
| 1069 | $file = $this->pwd.'/'.$f; |
---|
[2566] | 1070 | |
---|
[0] | 1071 | if (!file_exists($file)) { |
---|
| 1072 | return false; |
---|
| 1073 | } |
---|
[2566] | 1074 | |
---|
[0] | 1075 | $xml = new xmlTag('meta'); |
---|
| 1076 | $meta = imageMeta::readMeta($file); |
---|
| 1077 | $xml->insertNode($meta); |
---|
[2566] | 1078 | |
---|
[0] | 1079 | $c = $this->core->con->openCursor($this->table); |
---|
| 1080 | $c->media_meta = $xml->toXML(); |
---|
[2566] | 1081 | |
---|
[0] | 1082 | if ($cur->media_title !== null && $cur->media_title == basename($cur->media_file)) |
---|
| 1083 | { |
---|
| 1084 | if ($meta['Title']) { |
---|
| 1085 | $c->media_title = $meta['Title']; |
---|
| 1086 | } |
---|
| 1087 | } |
---|
[2566] | 1088 | |
---|
[0] | 1089 | if ($meta['DateTimeOriginal'] && $cur->media_dt === '') |
---|
| 1090 | { |
---|
| 1091 | # We set picture time to user timezone |
---|
| 1092 | $media_ts = strtotime($meta['DateTimeOriginal']); |
---|
| 1093 | if ($media_ts !== false) { |
---|
| 1094 | $o = dt::getTimeOffset($this->core->auth->getInfo('user_tz'),$media_ts); |
---|
| 1095 | $c->media_dt = dt::str('%Y-%m-%d %H:%M:%S',$media_ts+$o); |
---|
| 1096 | } |
---|
| 1097 | } |
---|
[2566] | 1098 | |
---|
[0] | 1099 | $c->update('WHERE media_id = '.$id); |
---|
| 1100 | } |
---|
[2566] | 1101 | |
---|
[0] | 1102 | /** |
---|
[2767] | 1103 | Returns HTML code for audio player (HTML5 and if possible fallback Flash player) |
---|
| 1104 | |
---|
| 1105 | @param type <b>string</b> audio mime type |
---|
| 1106 | @param url <b>string</b> audio URL to play |
---|
| 1107 | @param player <b>string</b> Player URL (flash player fallback) |
---|
| 1108 | @param args <b>array</b> Player parameters (flash player fallback) |
---|
| 1109 | @return <b>string</b> |
---|
| 1110 | */ |
---|
| 1111 | public static function audioPlayer($type,$url,$player=null,$args=null) |
---|
| 1112 | { |
---|
| 1113 | $audio = |
---|
[2768] | 1114 | '<audio controls preload="auto">'. |
---|
| 1115 | '<source src="'.$url.'">'; |
---|
[2767] | 1116 | |
---|
| 1117 | if ($type == 'audio/mpeg3') { |
---|
| 1118 | // Include Flash player fallback |
---|
| 1119 | if (!$player) { |
---|
| 1120 | $player = 'player_mp3.swf'; |
---|
| 1121 | } |
---|
| 1122 | |
---|
| 1123 | if (!is_array($args)) |
---|
| 1124 | { |
---|
| 1125 | $args = array( |
---|
| 1126 | 'showvolume' => 1, |
---|
| 1127 | 'loadingcolor' => 'ff9900', |
---|
| 1128 | 'bgcolor1' => 'eeeeee', |
---|
| 1129 | 'bgcolor2' => 'cccccc', |
---|
| 1130 | 'buttoncolor' => '0066cc', |
---|
| 1131 | 'buttonovercolor' => 'ff9900', |
---|
| 1132 | 'slidercolor1' => 'cccccc', |
---|
| 1133 | 'slidercolor2' => '999999', |
---|
| 1134 | 'sliderovercolor' => '0066cc' |
---|
| 1135 | ); |
---|
| 1136 | } |
---|
| 1137 | |
---|
| 1138 | $args['mp3'] = $url; |
---|
| 1139 | |
---|
| 1140 | if (empty($args['width'])) { |
---|
| 1141 | $args['width'] = 200; |
---|
| 1142 | } |
---|
| 1143 | if (empty($args['height'])) { |
---|
| 1144 | $args['height'] = 20; |
---|
| 1145 | } |
---|
| 1146 | |
---|
| 1147 | $vars = array(); |
---|
| 1148 | foreach ($args as $k => $v) { |
---|
| 1149 | $vars[] = $k.'='.$v; |
---|
| 1150 | } |
---|
| 1151 | |
---|
| 1152 | $audio .= |
---|
| 1153 | '<object type="application/x-shockwave-flash" '. |
---|
| 1154 | 'data="'.$player.'" '. |
---|
| 1155 | 'width="'.$args['width'].'" height="'.$args['height'].'">'. |
---|
| 1156 | '<param name="movie" value="'.$player.'" />'. |
---|
| 1157 | '<param name="wmode" value="transparent" />'. |
---|
| 1158 | '<param name="FlashVars" value="'.implode('&',$vars).'" />'. |
---|
| 1159 | __('Embedded Audio Player'). |
---|
[2768] | 1160 | '</object>'; |
---|
[2767] | 1161 | } |
---|
| 1162 | |
---|
| 1163 | $audio .= |
---|
[2768] | 1164 | '</audio>'; |
---|
[2767] | 1165 | |
---|
| 1166 | return $audio; |
---|
| 1167 | } |
---|
| 1168 | |
---|
| 1169 | /** |
---|
| 1170 | Returns HTML code for video player (HTML5 and if possible fallback Flash player) |
---|
| 1171 | |
---|
| 1172 | @param type <b>string</b> video mime type |
---|
| 1173 | @param url <b>string</b> video URL to play |
---|
| 1174 | @param player <b>string</b> Player URL (flash player fallback) |
---|
| 1175 | @param args <b>array</b> Player parameters (flash player fallback) |
---|
| 1176 | @return <b>string</b> |
---|
| 1177 | */ |
---|
| 1178 | public static function videoPlayer($type,$url,$player=null,$args=null) |
---|
| 1179 | { |
---|
| 1180 | $video = ''; |
---|
[2768] | 1181 | |
---|
| 1182 | // Cope with width and height, if given |
---|
| 1183 | $width = 400; |
---|
| 1184 | $height = 300; |
---|
| 1185 | if (is_array($args)) { |
---|
| 1186 | if (!empty($args['width']) && $args['width']) { |
---|
| 1187 | $width = (int) $args['width']; |
---|
| 1188 | } |
---|
| 1189 | if (!empty($args['height']) && $args['height']) { |
---|
| 1190 | $height = (int) $args['height']; |
---|
| 1191 | } |
---|
| 1192 | } |
---|
| 1193 | |
---|
[2767] | 1194 | if ($type != 'video/x-flv') { |
---|
| 1195 | $video = |
---|
[2768] | 1196 | '<video controls preload="auto"'.($width ? ' width="'.$width.'"' : '').($height ? ' height="'.$height.'"' : '').'>'. |
---|
| 1197 | '<source src="'.$url.'">'; |
---|
[2767] | 1198 | } |
---|
| 1199 | |
---|
| 1200 | if ($type == 'video/x-flv' || $type == 'video/mp4' || $type == 'video/x-m4v') |
---|
| 1201 | { |
---|
| 1202 | // Include Flash player fallback |
---|
| 1203 | if (!$player) { |
---|
| 1204 | $player = 'player_flv.swf'; |
---|
| 1205 | } |
---|
| 1206 | |
---|
| 1207 | if (!is_array($args)) |
---|
| 1208 | { |
---|
| 1209 | $args = array( |
---|
| 1210 | 'margin' => 1, |
---|
| 1211 | 'showvolume' => 1, |
---|
| 1212 | 'showtime' => 1, |
---|
| 1213 | 'showfullscreen' => 1, |
---|
| 1214 | 'buttonovercolor' => 'ff9900', |
---|
| 1215 | 'slidercolor1' => 'cccccc', |
---|
| 1216 | 'slidercolor2' => '999999', |
---|
| 1217 | 'sliderovercolor' => '0066cc' |
---|
| 1218 | ); |
---|
| 1219 | } |
---|
| 1220 | |
---|
| 1221 | $args['flv'] = $url; |
---|
| 1222 | |
---|
| 1223 | if (empty($args['width'])) { |
---|
| 1224 | $args['width'] = 400; |
---|
| 1225 | } |
---|
| 1226 | if (empty($args['height'])) { |
---|
| 1227 | $args['height'] = 300; |
---|
| 1228 | } |
---|
| 1229 | |
---|
| 1230 | $vars = array(); |
---|
| 1231 | foreach ($args as $k => $v) { |
---|
| 1232 | $vars[] = $k.'='.$v; |
---|
| 1233 | } |
---|
| 1234 | |
---|
| 1235 | $video .= |
---|
| 1236 | '<object type="application/x-shockwave-flash" '. |
---|
| 1237 | 'data="'.$player.'" '. |
---|
| 1238 | 'width="'.$args['width'].'" height="'.$args['height'].'">'. |
---|
| 1239 | '<param name="movie" value="'.$player.'" />'. |
---|
| 1240 | '<param name="wmode" value="transparent" />'. |
---|
| 1241 | '<param name="allowFullScreen" value="true" />'. |
---|
| 1242 | '<param name="FlashVars" value="'.implode('&',$vars).'" />'. |
---|
| 1243 | __('Embedded Video Player'). |
---|
[2768] | 1244 | '</object>'; |
---|
[2767] | 1245 | } |
---|
| 1246 | |
---|
| 1247 | if ($type != 'video/x-flv') { |
---|
| 1248 | $video .= |
---|
[2768] | 1249 | '</video>'; |
---|
[2767] | 1250 | } |
---|
| 1251 | |
---|
| 1252 | return $video; |
---|
| 1253 | } |
---|
| 1254 | |
---|
| 1255 | /** |
---|
[0] | 1256 | Returns HTML code for MP3 player |
---|
[2566] | 1257 | |
---|
[0] | 1258 | @param url <b>string</b> MP3 URL to play |
---|
| 1259 | @param player <b>string</b> Player URL |
---|
| 1260 | @param args <b>array</b> Player parameters |
---|
| 1261 | @return <b>string</b> |
---|
| 1262 | */ |
---|
| 1263 | public static function mp3player($url,$player=null,$args=null) |
---|
| 1264 | { |
---|
| 1265 | if (!$player) { |
---|
| 1266 | $player = 'player_mp3.swf'; |
---|
| 1267 | } |
---|
[2566] | 1268 | |
---|
[0] | 1269 | if (!is_array($args)) |
---|
| 1270 | { |
---|
| 1271 | $args = array( |
---|
| 1272 | 'showvolume' => 1, |
---|
| 1273 | 'loadingcolor' => 'ff9900', |
---|
| 1274 | 'bgcolor1' => 'eeeeee', |
---|
| 1275 | 'bgcolor2' => 'cccccc', |
---|
| 1276 | 'buttoncolor' => '0066cc', |
---|
| 1277 | 'buttonovercolor' => 'ff9900', |
---|
| 1278 | 'slidercolor1' => 'cccccc', |
---|
| 1279 | 'slidercolor2' => '999999', |
---|
| 1280 | 'sliderovercolor' => '0066cc' |
---|
| 1281 | ); |
---|
| 1282 | } |
---|
[2566] | 1283 | |
---|
[0] | 1284 | $args['mp3'] = $url; |
---|
[2566] | 1285 | |
---|
[0] | 1286 | if (empty($args['width'])) { |
---|
| 1287 | $args['width'] = 200; |
---|
| 1288 | } |
---|
| 1289 | if (empty($args['height'])) { |
---|
| 1290 | $args['height'] = 20; |
---|
| 1291 | } |
---|
[2566] | 1292 | |
---|
[0] | 1293 | $vars = array(); |
---|
| 1294 | foreach ($args as $k => $v) { |
---|
| 1295 | $vars[] = $k.'='.$v; |
---|
| 1296 | } |
---|
[2566] | 1297 | |
---|
[0] | 1298 | return |
---|
[2763] | 1299 | '<audio controls preload="auto">'. |
---|
| 1300 | '<source src="'.$url.'" type="audio/mpeg">'. |
---|
[0] | 1301 | '<object type="application/x-shockwave-flash" '. |
---|
| 1302 | 'data="'.$player.'" '. |
---|
| 1303 | 'width="'.$args['width'].'" height="'.$args['height'].'">'. |
---|
| 1304 | '<param name="movie" value="'.$player.'" />'. |
---|
| 1305 | '<param name="wmode" value="transparent" />'. |
---|
| 1306 | '<param name="FlashVars" value="'.implode('&',$vars).'" />'. |
---|
| 1307 | __('Embedded Audio Player'). |
---|
[2763] | 1308 | '</object>'. |
---|
| 1309 | '</audio>'; |
---|
[0] | 1310 | } |
---|
[2566] | 1311 | |
---|
[2767] | 1312 | /** |
---|
| 1313 | Returns HTML code for FLV player |
---|
| 1314 | |
---|
| 1315 | @param url <b>string</b> FLV URL to play |
---|
| 1316 | @param player <b>string</b> Player URL |
---|
| 1317 | @param args <b>array</b> Player parameters |
---|
| 1318 | @return <b>string</b> |
---|
| 1319 | */ |
---|
[0] | 1320 | public static function flvplayer($url,$player=null,$args=null) |
---|
| 1321 | { |
---|
| 1322 | if (!$player) { |
---|
| 1323 | $player = 'player_flv.swf'; |
---|
| 1324 | } |
---|
[2566] | 1325 | |
---|
[0] | 1326 | if (!is_array($args)) |
---|
| 1327 | { |
---|
| 1328 | $args = array( |
---|
| 1329 | 'margin' => 1, |
---|
| 1330 | 'showvolume' => 1, |
---|
| 1331 | 'showtime' => 1, |
---|
| 1332 | 'showfullscreen' => 1, |
---|
| 1333 | 'buttonovercolor' => 'ff9900', |
---|
| 1334 | 'slidercolor1' => 'cccccc', |
---|
| 1335 | 'slidercolor2' => '999999', |
---|
| 1336 | 'sliderovercolor' => '0066cc' |
---|
| 1337 | ); |
---|
| 1338 | } |
---|
[2566] | 1339 | |
---|
[0] | 1340 | $args['flv'] = $url; |
---|
[2566] | 1341 | |
---|
[0] | 1342 | if (empty($args['width'])) { |
---|
| 1343 | $args['width'] = 400; |
---|
| 1344 | } |
---|
| 1345 | if (empty($args['height'])) { |
---|
| 1346 | $args['height'] = 300; |
---|
| 1347 | } |
---|
[2566] | 1348 | |
---|
[0] | 1349 | $vars = array(); |
---|
| 1350 | foreach ($args as $k => $v) { |
---|
| 1351 | $vars[] = $k.'='.$v; |
---|
| 1352 | } |
---|
[2566] | 1353 | |
---|
[0] | 1354 | return |
---|
| 1355 | '<object type="application/x-shockwave-flash" '. |
---|
| 1356 | 'data="'.$player.'" '. |
---|
| 1357 | 'width="'.$args['width'].'" height="'.$args['height'].'">'. |
---|
| 1358 | '<param name="movie" value="'.$player.'" />'. |
---|
| 1359 | '<param name="wmode" value="transparent" />'. |
---|
| 1360 | '<param name="allowFullScreen" value="true" />'. |
---|
| 1361 | '<param name="FlashVars" value="'.implode('&',$vars).'" />'. |
---|
| 1362 | __('Embedded Video Player'). |
---|
| 1363 | '</object>'; |
---|
| 1364 | } |
---|
| 1365 | } |
---|