26 $this->site = $this->page->
getSite();
42 public function put($source, $name, $dir =
'', $extract =
false, $replace =
true, $types =
'', $save =
true, $original =
true) {
43 if ($original && ($original_page = $this->page->getCloneOriginal())) {
44 return $original_page->getMediapool()->put($source, $name, $dir, $extract, $replace, $types);
46 if ($extract || $this->page->validateFile($source, $name)) {
48 require_once(
'base/Ego_MimeType.php');
52 if (strpos($mime->autoDetect($source),
'image') !== 0) {
57 if (strpos($mime->autoDetect($source),
'image') === 0) {
64 $dir = $this->getDir($dir);
66 . rtrim($dir, DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR;
70 $name = strtr(mb_ereg_replace(
'[^a-zA-Z0-9\-äöüÄÖÜß()\[\]\.,_ ]',
'_', $name),
'#?&+',
'____');
73 $parts = explode(
'.', $name, 2);
74 $filename = $parts[0];
75 $suffix = $parts[1] ?
'.' . $parts[1] :
'';
77 while ((
bool)$this->
file($name, $dir)) {
78 $name = $filename .
'_' . ($n++) . $suffix;
82 if ($source !== $file) {
90 $data = $this->
get($name, $dir);
93 if ($data[
'isImage']) {
94 require_once(
'base/Ego_Image.php');
96 $imageTransform->load($file);
99 if (trim($copyright) !=
'') {
100 $data[
'copyright'] = $copyright;
103 $this->
setInfo($dir, $name,
'copyright', $copyright);
104 $this->page->update(array(),
true,
true);
123 if ($original_page = $this->page->getCloneOriginal()) {
124 return $original_page->getMediapool()->extract($name, $dir);
126 $dir = $this->getDir($dir);
127 $path = $this->
dir();
128 $file = $this->
file($name, $dir);
130 $tmp_file = tempnam($GLOBALS[
'egotec_conf'][
'tmp_dir'],
'ZIP_').
'.zip';
132 require_once(
'media/functions.php');
133 if (preg_match(
'/\.gz$/msi', $name)) {
134 require_once(
'Archive/Tar.php');
135 $zip =
new Archive_Tar($tmp_file);
137 require_once(
'Archive/Zip.php');
138 $zip =
new Archive_Zip($tmp_file);
143 $list = $zip->extract();
147 if (is_array($list)) {
149 foreach ($list as $info) {
151 .rtrim($dir, DIRECTORY_SEPARATOR)
152 .DIRECTORY_SEPARATOR.$info[
'stored_filename'];
153 if (!is_dir($info_dir)) {
154 if ($this->page->validateFile($info_dir)) {
159 . rtrim($dir, DIRECTORY_SEPARATOR)
160 . DIRECTORY_SEPARATOR . $info_name
165 } elseif ($main_dir ==
'' || strlen($info_dir) < strlen($main_dir)) {
166 $main_dir = $info_dir;
169 if ($main_dir !=
'') {
173 } elseif ($list ===
true) {
192 public function get($name, $dir =
'', $params = array(), $decoded =
false, $block =
'') {
193 $dir = $this->getDir($dir);
194 $params[
'pool'] = $name;
195 $file = $this->
file($name, $dir);
196 $sub_dir = $this->subDir($dir);
200 return $this->
get(urldecode($name), $dir, $params,
true);
206 require_once
'base/Ego_MimeType.php';
208 $mime_type = $mime->autoDetect($file);
211 if (preg_match(
'/\.([^\.]+)$/ims', $name, $match)) {
212 $suffix = strtolower($match[1]);
215 (!empty($suffix) && $ext = $suffix)
216 || ($ext = $mime->getExtension($mime_type))
224 $width = $height = null;
226 $width = $info[
'width'];
227 $height = $info[
'height'];
229 $params[
'width'] = $width;
230 $params[
'height'] = $height;
232 if (isset($info[
'duration'])) {
233 $sub_data[
'duration'] = $info[
'duration'];
236 if ((
string) $dir != (
string) $this->currentDir) {
237 $params[
'dir'] = $this->subDir($dir);
239 $params[
'site'] = $this->site->name;
240 $params[
'lang'] = $this->site->language;
241 $params[
'id'] = $this->page->field[
'id'];
244 $download_params = $params;
245 $download_params[
'download'] = 1;
246 unset($download_params[
'width'], $download_params[
'height']);
250 if ($extra_title = (
string) ($this->page->extra[
'mediapool'][md5($sub_dir)][md5($name)][
'title'])) {
251 $title = $extra_title;
252 } elseif ($pos = strrpos($name,
'.')) {
253 $title = substr($name, 0, $pos);
259 $extra_key =
'block.' . $block;
261 $extra_key = ($sub = strrchr($dir, DIRECTORY_SEPARATOR)) ? substr($sub, 1) :
'default';
263 if ($this->page->conf[
'mediapool'][$extra_key][
'extra']) {
264 foreach ($this->page->conf[
'mediapool'][$extra_key][
'extra'] as $field) {
265 $extra[$field[
'name']] = $this->page->extra[
'mediapool'][md5($sub_dir)][md5($name)][$field[
'name']];
275 $get_url =
function($params) {
276 unset($params[
'mime_type']);
277 return 'index.php?' . str_replace(
'%2F',
'/', http_build_query($params));
280 $data = array_merge(array(
283 'url' => $get_url($params),
284 'download' => $get_url($download_params),
285 'size' => @filesize($file),
286 'mime' => $mime_type,
290 'isImage' => $isImage,
291 'isArchive' => $isArchive,
292 'isVideo' => $isVideo,
293 'lastChange' => @filemtime($file),
295 'description' => (
string) $this->page->extra[
'mediapool'][md5($sub_dir)][md5($name)][
'description'],
296 'copyright' => (
string) $this->page->extra[
'mediapool'][md5($sub_dir)][md5($name)][
'copyright'],
297 'order' => (
int) $this->page->extra[
'mediapool'][md5($sub_dir)][md5($name)][
'order'],
298 'clip' => $this->page->extra[
'mediapool'][md5($sub_dir)][md5($name)][
'clip'],
299 'rotation' => (
int) $this->page->extra[
'mediapool'][md5($sub_dir)][md5($name)][
'rotation'],
300 'mirror' => (
string) $this->page->extra[
'mediapool'][md5($sub_dir)][md5($name)][
'mirror'],
313 public function file($name, $dir =
'') {
314 $dir = $this->getDir($dir);
316 .rtrim($dir, DIRECTORY_SEPARATOR).DIRECTORY_SEPARATOR.Ego_System::base64Encode($name);
322 .rtrim($dir, DIRECTORY_SEPARATOR).DIRECTORY_SEPARATOR.base64_encode($name);
337 public function delete($name, $dir =
'') {
338 if ($original_page = $this->page->getCloneOriginal()) {
339 return $original_page->getMediapool()->delete($name, $dir);
341 $dir = $this->getDir($dir);
343 .rtrim($dir, DIRECTORY_SEPARATOR).DIRECTORY_SEPARATOR.Ego_System::base64Encode($name);
347 .rtrim($dir, DIRECTORY_SEPARATOR).DIRECTORY_SEPARATOR.base64_encode($name);
354 if ($dir == $this->currentDir) {
355 $this->page->update();
370 public function edit($name, $params = array(), $dir =
'') {
371 if ($original_page = $this->page->getCloneOriginal()) {
372 return $original_page->getMediapool()->put($name, $params, $dir);
374 $dir = $this->getDir($dir);
375 $file = $this->
file($name, $dir);
376 $sub_dir = $this->subDir($dir);
377 $new_name = trim($params[
'new_name']);
382 && $name != $new_name
383 && ($data = $this->
put($file, $new_name, $dir,
false, (
bool) $params[
'replace']))
385 if ($this->
delete($name, $dir,
false)) {
387 $this->page->field[
'content'] = $this->renameRecursive($name, $new_name, $this->page->field[
'content']);
388 $this->page->field[
'short'] = $this->renameRecursive($name, $new_name, $this->page->field[
'short']);
389 $this->page->extra = $this->renameRecursive($name, $new_name, $this->page->extra);
390 $this->page->extra[
'mediapool'][md5($sub_dir)][md5($new_name)] = $this->page->extra[
'mediapool'][md5($sub_dir)][md5($name)];
391 unset($this->page->extra[
'mediapool'][md5($sub_dir)][md5($name)]);
395 $this->
delete($new_name, $dir);
400 if (empty($this->page->extra[
'mediapool'][md5($sub_dir)][md5($name)])) {
401 $this->page->extra[
'mediapool'][md5($sub_dir)][md5($name)] = array();
404 unset($params[
'new_name'], $params[
'replace']);
405 $this->page->extra[
'mediapool'][md5($sub_dir)][md5($name)] = array_merge($this->page->extra[
'mediapool'][md5($sub_dir)][md5($name)], $params);
407 $this->page->update(array(
409 'content' => $this->page->field[
'content'],
410 'short' => $this->page->field[
'short']
412 'extra' => $this->page->extra
425 private function renameRecursive($name, $new_name, $value) {
426 if (is_array($value)) {
427 foreach ($value as $key => $v) {
428 $value[$key] = $this->renameRecursive($name, $new_name, $value[$key]);
430 } elseif (preg_match_all(
'/index\.php\?[^>"]+/ims', $value, $matches)) {
431 foreach ($matches[0] as $match) {
432 $new_url = preg_replace(
'/([&?])pool='.preg_quote($name,
'/').
'/msi',
'$1pool='.$new_name, $match);
433 $value = str_replace($match, $new_url, $value);
460 public function list($dir =
'', $params = array(), $block =
'') {
461 $dir = $this->getDir($dir);
464 .rtrim($dir, DIRECTORY_SEPARATOR).DIRECTORY_SEPARATOR;
466 if ($handle = opendir($path)) {
469 $params2[
'sort_field'],
470 $params2[
'sort_reverse'],
472 $params2[
'only_image']
474 while (
false !== ($file = readdir($handle))) {
475 if ($file !=
'.' && $file !=
'..' && !is_dir($path.$file)) {
481 !$params[
'only_image']
482 && !$_REQUEST[
'session'][
'insert_image']
488 !$params[
'only_video']
489 && !$_REQUEST[
'session'][
'insert_video']
495 !$params[
'only_file']
496 && !$_REQUEST[
'session'][
'insert_file']
502 !$params[
'only_media']
503 && !$_REQUEST[
'session'][
'insert_media']
509 empty($params[
'mime_type'])
510 || preg_match(
'#'.$params[
'mime_type'].
'#i', $data[
'mime'])
513 empty($params[
'search'])
514 || stripos($data[
'name'], $params[
'search']) !==
false 515 || stripos($data[
'title'], $params[
'search']) !== false
527 foreach ($list as $file) {
528 $sort_field = ($params[
'sort_field'] && isset($file[$params[
'sort_field']]))
529 ? $file[$params[
'sort_field']]
531 $sort[] = is_string($sort_field)
532 ? mb_strtolower($sort_field)
534 $names[] = mb_strtolower($file[
'name']);
536 if (!$params[
'sort_field'] &&
sizeof(array_unique($sort)) == 1) {
540 $params[
'sort_reverse'] ? SORT_DESC : SORT_ASC,
548 $params[
'sort_reverse'] ? SORT_DESC : SORT_ASC,
549 is_string($sort_field) ? SORT_STRING : SORT_NUMERIC,
554 if (isset($params[
'start'])) {
556 $list = array_slice($list, $params[
'start'], $params[
'end']);
570 public function setInfo($dir, $name, $key, $value = null) {
571 $sub_dir = $this->subDir($dir);
572 if ($value === null) {
573 unset($this->page->extra[
'mediapool'][md5($sub_dir)][md5($name)][$key]);
575 $this->page->extra[
'mediapool'][md5($sub_dir)][md5($name)][$key] = $value;
577 return implode(
'.', array(
'mediapool', md5($sub_dir), md5($name), $key));
587 public function dir($relative =
false, $source = null) {
589 $site = $source->getSite();
591 $lang = $site->language;
592 $id = $source->field[
'id'];
594 $name = $this->site->name;
595 $lang = $this->site->language;
596 $id = $this->page->field[
'id'];
598 return (!$relative ? $GLOBALS[
'egotec_conf'][
'var_dir'] :
'').
'media'.
599 DIRECTORY_SEPARATOR.$name.
600 DIRECTORY_SEPARATOR.$lang.
601 DIRECTORY_SEPARATOR.
'pool' .
602 DIRECTORY_SEPARATOR.$id.
612 private function subDir($dir) {
614 if ($dirs = explode(DIRECTORY_SEPARATOR, $dir)) {
615 if ($dirs[0] == $this->currentDir) {
618 $sub_dir = implode(DIRECTORY_SEPARATOR, $dirs);
629 private function getDir($dir) {
630 if (preg_match(
'/^\d+($|' . preg_quote(DIRECTORY_SEPARATOR,
'/') .
'.*?$)/', $dir)) {
633 } elseif (empty($dir) || $dir == $this->currentDir) {
638 return $this->currentDir . DIRECTORY_SEPARATOR . $this->subDir($dir);
649 if ($original_page = $this->page->getCloneOriginal()) {
650 return $original_page->getMediapool()->archive($c_date, $dir);
652 $dir = $this->getDir($dir);
653 $path = $this->
dir();
655 $src = $path.rtrim($dir, DIRECTORY_SEPARATOR).DIRECTORY_SEPARATOR;
656 $dest = $path.($dir != $this->currentDir ? $dir :
'')
672 public function restore($c_date, $name =
'', $dir =
'') {
673 if ($original_page = $this->page->getCloneOriginal()) {
674 return $original_page->restore($c_date, $name, $dir);
676 if (!empty($c_date)) {
677 $dir = $this->getDir($dir);
681 .rtrim($dir, DIRECTORY_SEPARATOR).DIRECTORY_SEPARATOR;
682 $source = $this->
dir().$c_date.DIRECTORY_SEPARATOR;
687 $archive_page = $this->page->getArchivePage(is_numeric($c_date) ? date(
'Y-m-d H:i:s', $c_date) : $c_date);
688 $mediapool = $archive_page ? $archive_page->extra[
'mediapool'] : array();
689 $this->
clear($dir, $mediapool);
694 $path = $dir == $this->currentDir
696 : rtrim($dir, DIRECTORY_SEPARATOR).DIRECTORY_SEPARATOR.$c_date;
697 $source = $this->
get($name, $path);
698 return (
bool) $this->
put($source[
'file'], $name, $dir);
709 private function create() {
727 public function copy(
Page $target, $dir =
'', $clear =
false, $integrate =
false, $archive =
true, $asis =
true) {
728 $path = $this->
dir();
730 $path .= rtrim($dir, DIRECTORY_SEPARATOR).DIRECTORY_SEPARATOR;
734 $dest = $this->
dir(
false, $target);
736 $dest .= rtrim($dir, DIRECTORY_SEPARATOR).DIRECTORY_SEPARATOR;
744 $target->
getMediapool()->import(
true,
true, $dir,
true, $asis);
762 public function move($target, $dir =
'', $integrate =
false, $archive =
true) {
763 $this->
copy($target, $dir,
true, $integrate, $archive);
773 public function exists($dir =
'', $empty =
true) {
774 $dir = $this->getDir($dir);
776 .rtrim($dir, DIRECTORY_SEPARATOR).DIRECTORY_SEPARATOR;
777 return Ego_System::file_exists($path) && ($empty ||
sizeof(is_array($scanned_dir = scandir($path)) ? $scanned_dir : []) > 2);
788 public function clear($dir =
'', $mediapool = array(), $update =
true) {
789 if ($original_page = $this->page->getCloneOriginal()) {
790 return $original_page->getMediapool()->clear($dir);
792 $path = $this->
dir();
794 $path .= rtrim($dir, DIRECTORY_SEPARATOR).DIRECTORY_SEPARATOR;
796 $sub_dir = $this->subDir($dir);
799 if (!empty($mediapool)) {
800 $this->page->extra[
'mediapool'] = $mediapool;
802 unset($this->page->extra[
'mediapool'][md5($sub_dir)]);
804 $this->page->update(array(),
true,
true);
820 public function import($only_pool =
false, $replace =
false, $dir =
'', $update =
true, $asis =
true) {
821 $this->page->field[
'content'] = $this->importRecursive($this->page->field[
'content'], $only_pool, $replace, $dir);
822 $this->page->field[
'short'] = $this->importRecursive($this->page->field[
'short'], $only_pool, $replace, $dir);
823 $this->page->extra = $this->importRecursive($this->page->extra, $only_pool, $replace, $dir);
825 $this->page->update(array(
827 'content' => $this->page->field[
'content'],
828 'short' => $this->page->field[
'short']
830 'extra' => $this->page->extra
845 private function importRecursive($value, $only_pool =
false, $replace =
false, $dir =
'') {
846 if (is_array($value)) {
847 foreach ($value as $key => $v) {
848 $value[$key] = $this->importRecursive($v, $only_pool, $replace, $dir);
850 } elseif (preg_match_all(
'/index\.php\?[^>"]+/ims', $value, $matches)) {
851 $dir = $dir ==
'' ?
'' : rtrim($dir, DIRECTORY_SEPARATOR).DIRECTORY_SEPARATOR;
852 foreach ($matches[0] as $url) {
857 'deleted_or' =>
'1=1',
859 'only_active' =>
false 867 && in_array($page->field[
'type'], array(
'multimedia/file',
'multimedia/image'))
870 $site = $page->getSite();
871 $path = $GLOBALS[
'egotec_conf'][
'var_dir'].
'media/'.$site->name.
'/';
872 $file = $path.$page->getMediaFilename();
874 $name = $page->field[
'name'];
876 if ($page->extra[
'image_type']) {
877 $suffix =
'.'.$page->extra[
'image_type'];
880 while ($this->
file($name.$suffix, $dir)) {
882 if (preg_match(
'/_(\d+)$/ims', $name, $match)) {
883 $num = ((int) $match[1]) + 1;
884 $name = preg_replace(
'/_\d+$/ims',
'', $name);
891 $media = $this->
put($file, $name.$suffix, $dir);
892 if (!empty($media[
'url'])) {
893 $value = str_replace($url, $media[
'url'], $value);
896 } elseif (!empty($info[
'params'][
'pool'])) {
898 $media = $page->getMediapool()->get(urldecode($info[
'params'][
'pool']), $dir);
899 if ($media[
'file']) {
908 !$this->page->isClone()
911 if (!empty($media[
'url'])) {
912 $value = str_replace($url, $media[
'url'], $value);
static deldir($location, $del=true, $without='', $rename=true)
static urltopage($url, $params=array(), $only_site=false, $error_page=false, $commit_params=false)
static file_exists($file)
static mkdir($dir, $mode=0755, $recursive=true)
static getUrlInfo($url, $encode=false)
static copy($src, $dest, $except='', $useLinks=false)
static getFileInfo($file)
static dateEncode($string)