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