7 require_once(
'base/Ego_System.php');
8 require_once(
'base/functions.php');
55 private $info = array();
69 private $mime_type =
'';
83 private $generator =
'Server-File';
104 private $is_image =
false;
111 private $is_valid =
false;
118 private $web_format =
false;
125 private $temporary =
false;
132 private $quality = 100;
139 private $multipart_support =
false;
146 private $error =
false;
155 if (is_a($source,
'Page')) {
157 $this->page = $source;
158 $this->site = $this->page->getSite();
162 if ($_REQUEST[
'dir'] || $_REQUEST[
'c_date']) {
163 $dir = $_REQUEST[
'dir'] ? $_REQUEST[
'dir'] : $_REQUEST[
'c_date'];
164 if ($_REQUEST[
'c_date'] && !is_numeric($dir) && strpos($dir, DIRECTORY_SEPARATOR) ===
false) {
166 $dir = strtotime($dir);
170 if ($_REQUEST[
'thumbnail']) {
172 list($width, $height, $index) = explode(
'x', $_REQUEST[
'thumbnail'], 3);
173 $this->file = $this->getThumbnail((
int) $width, (
int) ($height ?? $width), $pool, $dir, [
174 'index' => (
int) $index
176 $this->detectMimeType();
177 $this->is_image =
true;
183 $file = $this->page->getMediapool()->get($this->pool, $dir);
185 $this->file = $file[
'file'];
187 $this->detectMimeType();
188 $type = $file[
'suffix'];
191 if ($this->page->field[
'type'] ==
'multimedia/image' && $this->page->extra[
'crop_image']) {
193 'edit' => $this->page->extra[
'edit'],
196 $this->page = $this->page->getParents()->nextPage();
197 $this->page->extra = array_merge($this->page->extra, $extra);
200 $this->file = $GLOBALS[
'egotec_conf'][
'var_dir'].
'media/'.$this->site->name.
'/';
201 if ($_REQUEST[
'c_date']) {
203 $archive_file = $this->file . $this->page->getMediaFilename(
208 $this->file = $archive_file;
211 $this->file .= $this->page->getMediaFilename();
214 $this->file .= $this->page->getMediaFilename();
216 $this->mime_type = $this->page->extra[
'mime_type'];
217 $type = $this->page->extra[
'image_type'];
220 if ($this->site->admin[
'video'][
'compress'] && !$GLOBALS[
'is_index']) {
221 require_once
'base/Ego_Mobile.php';
224 '360p' => $this->page->getMediapool()->get(
'360p.mp4'),
225 '720p' => $this->page->getMediapool()->get(
'720p.mp4'),
226 '1080p' => $this->page->getMediapool()->get(
'1080p.mp4')
228 $is_mobile = $mobile->isMobile();
229 $is_tablet = $mobile->isTablet();
231 ($is_mobile && !$is_tablet && ($data = $files[
'360p']))
232 || ($is_tablet && ($data = $files[
'720p']))
233 || ($data = $files[
'1080p'])
235 $this->file = $data[
'file'];
236 $this->mime_type = $data[
'mime'];
239 $existing_files = array_filter($files);
240 if ($data = array_pop($existing_files)) {
241 $this->file = $data[
'file'];
242 $this->mime_type = $data[
'mime'];
251 'docx' =>
'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
252 'pptx' =>
'application/vnd.openxmlformats-officedocument.presentationml.presentation',
253 'pptm' =>
'application/vnd.ms-powerpoint.presentation.macroEnabled.12',
254 'xlsx' =>
'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
255 'xlsm' =>
'application/vnd.ms-excel.sheet.macroEnabled.12',
256 'xps' =>
'application/vnd.ms-xpsdocument',
257 'svg' =>
'image/svg+xml' 259 if (array_key_exists($type, $formats)) {
260 $this->mime_type = $formats[$type];
263 } elseif (is_string($source)) {
264 if (strpos($source,
'string:') === 0) {
266 $file = tempnam($GLOBALS[
'egotec_conf'][
'tmp_dir'],
'output');
274 if (!empty($GLOBALS[
'site']) && is_a($GLOBALS[
'site'],
'Site')) {
275 $this->site = $GLOBALS[
'site'];
276 } elseif (class_exists(
'Site')) {
277 $this->site =
new Site();
282 $ignore_image_mimes = array(
'image/svg+xml',
'image/x-icon',
'image/vnd.microsoft.icon');
283 $ignore_image_types = array(
'svg',
'ico');
285 if (!$this->is_image) {
286 if ($this->page && $this->page->field[
'type'] ==
'multimedia/image') {
287 $this->is_image = !in_array($this->page->extra[
'mime_type'], $ignore_image_mimes);
289 $this->is_image = preg_match(
294 function ($type) use ($ignore_image_types) {
295 return !in_array($type, $ignore_image_types);
301 } elseif ($this->file) {
302 $this->is_image = !(strpos($this->mime_type,
'image') ===
false || in_array($this->mime_type, $ignore_image_mimes));
303 if ($this->page && $this->page->extra[
'mime_type'] && $this->page->extra[
'image_type']) {
304 $this->is_valid =
true;
309 if ($this->is_image) {
310 $this->web_format = in_array(
314 if (is_numeric($_REQUEST[
'quality'])) {
316 $this->quality = $_REQUEST[
'quality'];
317 }
else if (is_numeric($GLOBALS[
'quality'])) {
319 $this->quality = $GLOBALS[
'quality'];
320 } elseif (is_numeric($GLOBALS[
'egotec_conf'][
'image'][
'quality'])) {
322 $this->quality = $GLOBALS[
'egotec_conf'][
'image'][
'quality'];
333 private function isValid($download =
false) {
338 in_array($this->page->field[
'type'], array(
'multimedia/file',
'multimedia/image',
'multimedia/category'))
344 && $this->page->field[
'type'] ==
'multimedia/category' 348 && $this->page->hasRights(
'view')
358 private function detectMimeType() {
359 require_once(
'base/Ego_MimeType.php');
361 $this->mime_type = $mime->autoDetect($this->file);
370 public function error($image =
false) {
372 if (($this->is_image || $image) && !$this->
error) {
374 if (class_exists(
'Imagick')) {
375 $width = $this->page ? $this->page->extra[
'origImgWidth'] : 0;
376 if (!is_numeric($width) || $_REQUEST[
'width']) {
377 $width = (int) $_REQUEST[
'width'];
379 $height = $this->page ? $this->page->extra[
'origImgHeight'] : 0;
380 if (!is_numeric($height) || $_REQUEST[
'height']) {
381 $height = (int) $_REQUEST[
'height'];
384 if ($width && $height) {
385 $this->file = tempnam($GLOBALS[
'egotec_conf'][
'tmp_dir'],
'transform');
387 $image =
new Imagick();
388 $draw =
new ImagickDraw();
389 $image->newImage($width, $height,
new ImagickPixel(
'grey'));
391 $font_size = ceil(min(array($width, $height)) / 6);
392 $draw->setFillColor(
'lightgrey');
393 $draw->setFontSize($font_size);
394 $draw->setTextAlignment(Imagick::ALIGN_CENTER);
395 $image->annotateImage(
398 ceil($height / 2) + ceil($font_size / 2),
402 $image->setFormat(
'png');
407 $error_image =
false;
411 $this->file = $GLOBALS[
'egotec_conf'][
'egotec_dir'] .
'pub' .
412 DIRECTORY_SEPARATOR .
'missing_file.gif';
414 $this->detectMimeType();
415 $this->is_valid =
true;
417 $error = $this->
read();
418 } elseif ($this->site) {
419 $error = $this->site->getErrorPage();
422 $error = $site->getErrorPage();
436 private function prepend(&$page, &$site, &$auth, &$smarty) {
441 && ($file = $site->getSiteFile($page->field[
'type'].DIRECTORY_SEPARATOR.
'index.php'))
462 private function getName() {
463 if ($this->name !=
'') {
465 } elseif ($this->pool) {
467 } elseif ($this->page) {
468 $suffix =
'.'.$this->page->extra[
'image_type'];
470 '/'.preg_quote($suffix,
'/').
'$/i',
472 strtr($this->page->field[
'name'],
'/\:*?"<>|',
'_________')
475 return basename($this->file);
485 private function setDefaultHeaders($length = 0) {
486 if (!empty($this->etag)) {
490 $t = $this->page ? $this->site->getCacheExpire() : 0;
502 $_SERVER[
'HTTPS'] ==
'on' 504 mb_strtolower($this->page->extra[
'image_type']),
505 array(
'pdf',
'doc',
'docx',
'dot')
511 $this->page->field[
'cache']&2
512 && $GLOBALS[
'auth']->isNobody()
521 if (!$GLOBALS[
'egotec_conf'][
'no_content_length_header']) {
523 $length = $this->getFilesize($this->file);
529 if ($this->page->extra[
'meta_robots'] && $this->page->extra[
'meta_robots'] !=
'index') {
540 private function setSingleHeaders() {
546 $this->setDefaultHeaders();
549 readfile_chunked($this->file);
557 private function setRangeHeaders() {
558 $fp = @fopen($this->file,
'rb');
559 $size = $this->getFilesize($this->file);
563 if (isset($_SERVER[
'HTTP_RANGE'])) {
565 list(, $range) = explode(
'=', $_SERVER[
'HTTP_RANGE'], 2);
566 if (strpos($range,
',') !==
false) {
568 if ($this->multipart_support) {
569 $boundary = md5(
'multipart_byteranges');
573 foreach (explode(
',', $range) as $multi_range) {
574 $multi_range = explode(
'-', trim($multi_range));
575 if ($multi_range[0] ==
'-') {
576 $start = $size - substr($multi_range, 1);
579 $start = $multi_range[0];
580 $end = (isset($multi_range[1]) && is_numeric($multi_range[1])) ? $multi_range[1] : $size;
584 echo
"--{$boundary}\r\n";
585 echo
"Content-Type: {$this->mime_type}\r\n";
586 echo
"Content-Range: bytes $start-$end/$size\r\n\r\n";
587 while (!feof($fp) && ($p = ftell($fp)) <= $end) {
588 if ($p + $buffer > $end) {
589 $buffer = $end - $p + 1;
592 echo fread($fp, $buffer);
597 echo
"--{$boundary}--\r\n\r\n";
601 $this->setDownloadHeaders();
606 if ($range[0] ==
'-') {
607 $c_start = $size - substr($range, 1);
609 $range = explode(
'-', $range);
610 $c_start = $range[0];
611 $c_end = (isset($range[1]) && is_numeric($range[1])) ? $range[1] : $size;
613 $c_end = $c_end > $end ? $end : $c_end;
614 if ($c_start > $c_end || $c_start > $size - 1 || $c_end >= $size) {
621 $length = $end - $start + 1;
627 $this->setDefaultHeaders($length);
629 while (!feof($fp) && ($p = ftell($fp)) <= $end) {
630 if ($p + $buffer > $end) {
631 $buffer = $end - $p + 1;
634 echo fread($fp, $buffer);
645 private function setDownloadHeaders() {
647 Ego_System::header(
'Content-Disposition: attachment; filename="'.$this->getName().
'";');
650 $this->setDefaultHeaders();
653 readfile_chunked($this->file);
662 public function read($download =
false) {
666 && $_SERVER[
'HTTP_IF_NONE_MATCH'] ==
'"' . $this->etag .
'"' 667 && $GLOBALS[
'auth']->isNobody()
674 if ($this->isValid($download)) {
675 $this->prepend($this->page, $this->site, $GLOBALS[
'auth'], $GLOBALS[
'smarty']);
679 $GLOBALS[
'egotec_conf'][
'dev'][
'images']
688 $modify_keys = array(
'clip',
'rotation',
'mirror');
689 if ($_REQUEST[
'original'] && !$GLOBALS[
'auth']->isNobody()) {
691 unset($_REQUEST[
'width'], $_REQUEST[
'height']);
694 $modify_keys = array(
'rotation',
'mirror');
697 foreach ($modify_keys as $param) {
698 if (isset($_REQUEST[$param])) {
699 $modify[$param] = $_REQUEST[$param];
702 if (isset($this->info[$param])) {
703 $modify[$param] = $this->info[$param];
705 } elseif ($this->page->field[
'type'] ==
'multimedia/image') {
706 if (isset($this->page->extra[
'edit'][$param])) {
707 $modify[$param] = $this->page->extra[
'edit'][$param];
714 (!$download || !empty($modify))
716 && ($_REQUEST[
'width']
717 || $_REQUEST[
'height']
718 || $this->quality != 100
719 || !$this->web_format
723 $this->
transform((
int) $_REQUEST[
'width'], (
int) $_REQUEST[
'height'], $modify);
728 && @is_dir($this->file)
735 && $this->page->field[
'type'] ==
'multimedia/category' 736 && !empty($this->site->admin[
'allow_download'])
737 && !empty($this->page->extra[
'allow_download'])
740 $this->file = $this->page->export();
741 $this->detectMimeType();
743 $this->page->extra[
'image_type'] =
'tar.gz';
746 $this->setDownloadHeaders();
752 isset($_SERVER[
'HTTP_RANGE'])
753 || strpos($this->mime_type,
'video/') === 0
756 !preg_match(
'/MSIE (.*?);/', $_SERVER[
'HTTP_USER_AGENT'], $matches)
761 $this->setRangeHeaders();
765 $this->setSingleHeaders();
770 && !empty($this->cache)
771 && ($this->page->field[
'cache']&3) != 0
772 && !$GLOBALS[
'no_cache']
776 'headers' => $GLOBALS[
'egotec'][
'response_headers'],
777 'stats' => $GLOBALS[
'stats'],
778 'type' => $this->page->field[
'type']
780 $this->site->setCacheEntry($this->cache .
'.info', $info);
781 $this->site->setCacheEntry($this->cache, $this->file);
784 if ($this->temporary) {
785 @unlink($this->file);
789 return $this->
error($this->page && strpos($this->page->extra[
'mime_type'],
'image/') === 0);
799 return $this->
read(
true);
813 public function pdf($name =
'', $temporary =
true, $cookie =
true, $return = 1) {
814 if ($this->isValid()) {
816 $smarty = $GLOBALS[
'smarty'];
820 if ($cookie && !empty($_COOKIE[EGOTEC])) {
821 $context = stream_context_create(array(
824 'header' =>
"Cookie: ".EGOTEC.
"=".$_COOKIE[EGOTEC].
"\r\n" 832 $_REQUEST[
'title'] = $this->getName();
835 $no_pdf_header =
true;
836 include(
'pdf/pdf_create.php');
839 $pdf_file = $GLOBALS[
'egotec_conf'][
'tmp_dir'].
'output'.md5(microtime()).
'.pdf';
854 return $this->
read();
860 return $this->
error();
871 public function transform($width, $height, $modify = array()) {
872 if ($this->is_image) {
874 $output_transform = $this->page && method_exists($this->page,
'outputTransform');
877 $output_watermark =
'';
880 $list = md5($_REQUEST[
'dir']);
881 $key = md5($this->pool);
882 if ($this->page->extra[
'mediapool'][$list][$key][
'watermark']) {
883 $output_watermark = $this->page->extra[
'mediapool'][$list][$key][
'watermark'];
885 } elseif ($this->page) {
887 $inherit = $this->page->inheritExtra(array(
'watermark'));
888 if ($inherit[
'watermark']) {
889 $output_watermark = $inherit[
'watermark'];
894 $quality = in_array($this->mime_type, array(
'image/jpeg',
'image/jpg')) ? $this->quality : 0;
896 if ($width > 0 || $height > 0 || !empty($modify) || $output_transform || $output_watermark || $quality) {
897 $size = getimagesize($this->file);
900 if ($width > $size[0] || $height > $size[1]) {
905 if (!$_REQUEST[
'max'] || $size[0] > $width || $size[1] > $height || !empty($modify)) {
906 $dir = $GLOBALS[
'egotec_conf'][
'cachemedia_dir'];
908 $dir .= $this->site->name . DIRECTORY_SEPARATOR . $this->site->language . DIRECTORY_SEPARATOR . $this->page->field[
'id'] . DIRECTORY_SEPARATOR;
909 $dir .=
Ego_System::dateEncode($_REQUEST[
'c_date'] ? $_REQUEST[
'c_date'] : $this->page->field[
'c_date']) .
912 $dir .=
'_output' . DIRECTORY_SEPARATOR;
914 if (!file_exists($dir)) {
917 $file = $dir . $width .
'x' . $height .
'x' . $this->quality . ($this->web_format ?
'' :
'_jpg');
919 $file .=
'_' . str_replace(
'.',
'_', base64_encode($this->pool));
920 } elseif (!$this->page) {
921 $file .=
'_' . md5($this->file);
923 if ($output_watermark) {
924 $file .=
'_' . md5($output_watermark);
926 if (!empty($modify)) {
927 $file .=
'_' . md5(serialize($modify));
931 $output_cache = $this->page && method_exists($this->page,
'outputCache');
935 && !$_REQUEST[
'c_date']
937 && (!$output_cache || $this->page->outputCache($width, $height))
946 && $size[0] == $width
947 && $size[1] == $height
949 && (!in_array($this->mime_type, array(
'image/jpeg',
'image/jpg'))
950 || $this->quality == 100)
951 && !$output_transform
952 && !$output_watermark
958 require_once(
'base/Ego_Image.php');
960 $tmp = tempnam($GLOBALS[
'egotec_conf'][
'tmp_dir'],
'transform');
963 $imageTransform->load($tmp);
966 if (!empty($modify)) {
968 if ($modify[
'mirror']) {
969 if (strpos($modify[
'mirror'],
'v') !==
false) {
970 $imageTransform->mirror(
true,
false);
972 if (strpos($modify[
'mirror'],
'h') !==
false) {
973 $imageTransform->mirror(
false,
true);
978 if ($modify[
'rotation']) {
979 $imageTransform->rotate((
int) $modify[
'rotation']);
983 if ($modify[
'clip']) {
984 list($x1, $y1, $x2, $y2) = explode(
',', $modify[
'clip'], 4);
986 $cheight = $y2 - $y1;
988 $imageTransform->crop($x1, $y1, $x2, $y2);
994 if ($width > $cwidth) {
997 $width = (int)round($cwidth * ($height / $cheight));
999 if ($height > $cheight) {
1001 } elseif (!$height) {
1002 $height = (int)round($cheight * ($width / $cwidth));
1007 if ($_REQUEST[
'max']) {
1011 $h = (int)round($size[1] * ($width / $size[0]));
1013 if ($height && $h > $height) {
1015 $w = (int)round($w * ($height / $h));
1018 $imageTransform->resize($w, $h);
1020 if ($width && $height) {
1022 $imageTransform->resize($width, $height);
1023 }
else if ($width) {
1025 $imageTransform->scaleByX($width);
1026 }
else if ($height) {
1028 $imageTransform->scaleByY($height);
1033 if ($output_transform) {
1034 $this->page->outputTransform($imageTransform, $tmp, $width, $height, $modify);
1038 if ($output_watermark) {
1039 $imageTransform->watermark($output_watermark);
1044 && $imageTransform->save($file, ($this->web_format ?
'' :
'jpeg'), $quality)
1046 $imageTransform->free();
1047 $this->file = $file;
1067 $this->file = $file;
1068 $this->detectMimeType();
1069 $this->is_valid =
true;
1078 @unlink($this->file);
1080 $this->is_valid =
false;
1091 $this->cache = $cache;
1092 $this->etag = $etag;
1102 $this->temporary = $temporary;
1112 if (@is_dir($path)) {
1113 $dir = $GLOBALS[
'egotec_conf'][
'tmp_dir'];
1114 $name = basename($path);
1115 $file = $name.
'.tar.gz';
1118 require_once(
'Archive/Tar.php');
1121 $zip =
new Archive_Tar($file,
'gz');
1122 $zip->_separator =
',';
1123 $zip->setIgnoreList(array($dir));
1124 $zip->createModify(rtrim($path, DIRECTORY_SEPARATOR),
'', $name);
1142 require_once(
'workflow/Ego_Workflow.php');
1143 $workflow =
new Ego_Workflow($key);
1144 $source = $workflow->export();
1147 require_once(
'base/template/Ego_Template.php');
1148 $template =
new Ego_Template($key);
1149 $source = $template->export();
1151 if (!empty($source)) {
1153 $output->setTemporary(
true);
1154 $output->download();
1168 private function getThumbnail($width, $height = 0, $pool =
'', $dir =
'', $params = []) {
1170 return $this->page->getThumbnail($width, $height, $pool, $dir, $params);
1183 private function getFilesize($file) {
1184 return (
int) @filesize($file);
static export($key, $type)
static file_put_contents($filename, $data, $flags=0, $context=null)
setCache($cache, $etag='')
static file_exists($file)
transform($width, $height, $modify=array())
static mkdir($dir, $mode=0755, $recursive=true)
static file_get_contents($filename, $utf8=true, $context=null)
static copy($src, $dest, $except='', $useLinks=false)
static dateEncode($string)
static header($header, $replace=true)
__construct($source=null, $pool='')
pdf($name='', $temporary=true, $cookie=true, $return=1)