Files
AutoIndex-pl4/classes/FileItem.php

417 lines
18 KiB
PHP
Raw Permalink Normal View History

2019-09-17 10:16:07 +03:00
<?php
/**
* @package AutoIndex
*
* @copyright Copyright (C) 2002-2004 Justin Hagstrom, 2019-2025 Florin C Bodin aka orynider at github.com
2019-09-17 10:16:07 +03:00
* @license http://www.gnu.org/licenses/gpl.html GNU General Public License (GPL)
* @version $Id: FileItem.php, v 2.2.6 2025/09/08 19:25:08 orynider Exp $
2019-09-17 10:16:07 +03:00
* @link http://autoindex.sourceforge.net
*/
/*
AutoIndex PHP Script is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
AutoIndex PHP Script is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
if (!defined('IN_AUTOINDEX') || !IN_AUTOINDEX)
{
2023-11-09 19:44:24 +02:00
die('bad class init...');
2019-09-17 10:16:07 +03:00
}
/**
* Subclass of item that specifically represents a file.
*
* @author Justin Hagstrom <JustinHagstrom@yahoo.com>
* @version 1.0.1 (July 10, 2004)
* @package AutoIndex
*/
class FileItem extends Item
{
/**
* @param string $fn The filename
* @return string Everything after the list dot in the filename, not including the dot
*/
2023-11-09 19:44:24 +02:00
public static function ext($fn, $ext = true)
2019-09-17 10:16:07 +03:00
{
$fn = Item::get_basename($fn);
2023-11-09 19:44:24 +02:00
switch($ext)
{
case false:
2023-11-12 10:27:54 +02:00
return (strpos($fn, '.') ? substr($fn, 0, strrpos($fn, '.')) : $fn);
2023-11-09 19:44:24 +02:00
break;
default:
return (strpos($fn, '.') ? strtolower(substr(strrchr($fn, '.'), 1)) : '');
break;
}
2019-09-17 10:16:07 +03:00
}
/**
2023-11-09 19:44:24 +02:00
* @return string Returns the name of the filename
2019-09-17 10:16:07 +03:00
* @see FileItem::ext()
*/
2023-11-09 19:44:24 +02:00
public function file_name()
2019-09-17 10:16:07 +03:00
{
2023-11-09 19:44:24 +02:00
return self::ext($this->filename, false);
2019-09-17 10:16:07 +03:00
}
2023-11-09 19:44:24 +02:00
/**
* @return string Returns the extension of the filename
* @see FileItem::ext()
*/
public function file_ext()
{
return self::ext($this->filename);
}
2019-09-17 10:16:07 +03:00
/**
* @param string $parent_dir
* @param string $filename
*/
public function __construct($parent_dir, $filename)
{
parent::__construct($parent_dir, $filename);
2023-11-25 23:46:57 +02:00
if (!is_file($this->parent_dir . $filename))
2020-12-23 03:18:48 +02:00
{
2023-10-31 18:32:54 +02:00
throw new ExceptionDisplay('File <em>' . Url::html_output($this->parent_dir . $filename) . '</em> does not exist.');
2019-09-17 10:16:07 +03:00
}
2023-11-25 23:46:57 +02:00
global $config, $words, $downloads, $request;
$version_compare = explode(".", phpversion(), 3);
if ($version_compare[0] == '' || $version_compare[1] == '')
{
$phpversion = '5.4';
}
else
{
$phpversion = $version_compare[0] . '.' . $version_compare[1];
}
2023-10-31 18:32:54 +02:00
$this->filename = $filename;
$this->size = new Size(filesize($this->parent_dir . $filename));
2019-09-17 10:16:07 +03:00
if (ICON_PATH)
{
$file_icon = new Icon($filename);
2023-11-25 23:46:57 +02:00
$this->icon = $file_icon->__toString();
2019-09-17 10:16:07 +03:00
}
2023-11-25 23:46:57 +02:00
$this->downloads = (DOWNLOAD_COUNT && $downloads->is_set($parent_dir . $filename) ? (int)($downloads->__get($parent_dir . $filename)) : 0);
$this->link = Url::html_output($request->server('PHP_SELF')) . '?dir=' . Url::translate_uri(substr($this->parent_dir, strlen($config->__get('base_dir')))) . '&amp;file=' . Url::translate_uri($filename);
2020-06-19 20:56:35 +03:00
if (THUMBNAIL_HEIGHT && in_array(self::ext($filename), array('png', 'jpg', 'jpeg', 'jfif', 'gif', 'bmp')))
2019-09-17 10:16:07 +03:00
{
2023-11-25 23:46:57 +02:00
$this->thumb_link = ' <img src="' . Url::html_output($request->server('PHP_SELF'))
2023-10-31 18:32:54 +02:00
. '?thumbnail='. Url::translate_uri($this->parent_dir . $filename) . '"'
2023-11-25 23:46:57 +02:00
. ' alt="' . $words->__get('thumbnail of') . ' ' . $filename . '"'
2020-06-19 20:56:35 +03:00
. ' />';
2023-11-25 23:46:57 +02:00
$this->thumb_link .= ' <a href="' . Url::html_output($request->server('PHP_SELF'))
. '?thm='. Url::translate_uri($this->parent_dir . $filename) . '"'
. ' alt="' . $words->__get('thumbnail of') . ' ' . $filename . '"'
. ' >' . $words->__get('view') . ' ' . $words->__get('file') . '</a>';
2019-09-17 10:16:07 +03:00
}
if (in_array(self::ext($filename), array('svg', 'SVG')))
{
$svgcontent = false;
$svgcontent = file_get_contents(Url::translate_uri($this->parent_dir . $filename));
$width = $height = '32';
//<svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" viewBox="0 0 16 16" fill="context-fill">
$contentsvg = explode('<svg', $svgcontent);
//xmlns="http://www.w3.org/2000/svg" width="32" height="32" viewBox="0 0 32 32">
$content = explode('=', $contentsvg[1]);
//[0] => xmlns [1] => "http://www.w3.org/2000/svg" width [2] => "32" height [3] => "32" viewBox [4] => "0 0 32 32">
if(preg_match('/<svg\s[^>]*width=\"(.*)\"\/>/isU', '<svg '.$contentsvg[1], $width))
{
$width = explode('" ', $content[2]);
$width = !empty($width[0]) ? str_replace('"', '', $width[0]) : '32';
$width = ($width < '14') ? '32' : $width;
$width = ($width == '100%') ? '400' : $width;
}
else
{
$width = '200';
$height = '200';
}
$svgcontent = str_replace('width="100%"', 'width="'.$width.'"', $svgcontent);
$svgcontent = str_replace('height="100%"', 'height="'.$height.'"', $svgcontent);
if (version_compare($phpversion, "6.0", ">"))
{
//die("you're on ".$phpversion." or bellow".phpversion().' &'.print_R($version_compare, true));
$svgcontent = ($width > '400') ? str_replace($width, $width / 1.1, $svgcontent) : $svgcontent;
$svgcontent = ($width < '24') ? str_replace($width, $width * 1.5 * 2, $svgcontent) : $svgcontent;
}
else
{
//die("you're on ".$phpversion." or above: ".phpversion().' &'.print_R($version_compare, true));
$svgcontent = ($width > '400') ? str_replace($width, '200', $svgcontent) : $svgcontent;
$svgcontent = ($width < '24') ? str_replace($width, '24', $svgcontent) : $svgcontent;
}
$svgcontent = ('edit.svg' === basename($filename)) ? str_replace($width, '200', $svgcontent) : $svgcontent;
if(preg_match_all('/<svg\s[^>]*height=\"(.*)\"\/>/isU', '<svg '.$contentsvg[1], $height))
{
$height = explode('" ', $content[3]);
$height = !empty($height[0]) ? str_replace('"', '', $height[0]) : '32';
}
if (version_compare($phpversion, "6.0", ">"))
{
//die("you're on ".$phpversion." or bellow".phpversion().' &'.print_R($version_compare, true));
$svgcontent = ($width > '400') ? str_replace($width, $width / 1.1, $svgcontent) : $svgcontent;
$svgcontent = ($width < '24') ? str_replace($width, $width * 1.5 * 2, $svgcontent) : $svgcontent;
}
else
{
//die("you're on ".$phpversion." or above: ".phpversion().' &'.print_R($version_compare, true));
$svgcontent = ($width > '400') ? str_replace($width, '200', $svgcontent) : $svgcontent;
$svgcontent = ($width < '24') ? str_replace($width, '24', $svgcontent) : $svgcontent;
}
$svgcontent = ('<edit.svg>' === basename($filename)) ? str_replace($height, '200', $svgcontent) : $svgcontent;
if(preg_match_all('/<svg\s[^>]*viewBox=\"(.*)\"\/>/isU', '<svg '.$contentsvg[1], $viewBox))
{
$viewBox = explode('" ', $content[4]);
$viewBox = str_replace('"', '', $viewBox[0]);
}
$this->thumb_link = '<a title="'.trim(basename($filename)).'" href="'.Url::html_output($this->parent_dir . $filename).'">'.$svgcontent.'</a>';
}
if (THUMBNAIL_HEIGHT && in_array(self::ext($filename), array('thm', 'thm')))
2021-01-05 22:00:42 +02:00
{
2023-11-25 23:46:57 +02:00
$this->thumb_link = ' <img src="' . Url::html_output($request->server('PHP_SELF'))
2023-10-31 18:32:54 +02:00
. '?thm='. Url::translate_uri($this->parent_dir . $filename) . '"'
2023-11-25 23:46:57 +02:00
. ' alt="' . $words->__get('thumbnail of') . ' ' . $filename . '"'
2021-01-05 22:00:42 +02:00
. ' />';
2023-11-25 23:46:57 +02:00
$this->thumb_link .= ' <a href="' . Url::html_output($request->server('PHP_SELF'))
. '?thm='. Url::translate_uri($this->parent_dir . $filename) . '"'
. ' alt="' . $words->__get('thumbnail of') . ' ' . $filename . '"'
. ' >' . $words->__get('view') . ' ' . $words->__get('file') . '</a>';
2021-01-05 22:00:42 +02:00
}
2023-10-31 18:32:54 +02:00
if (THUMBNAIL_HEIGHT && in_array(self::ext($filename), array('avi', 'divx', 'xvid', 'mkv', 'asf', 'mov', 'wmv', '3gp', 'mp3', 'mp4', 'mpv', 'ogg', 'ogv','mpg', 'mpeg', 'flv', 'FLV', 'flvjs')))
2020-12-23 03:18:48 +02:00
{
$mime = new MimeType($filename);
$finfo = finfo_open(FILEINFO_MIME_TYPE);
//Display correct headers for media file
2023-11-25 23:46:57 +02:00
$mimetype = finfo_file($finfo, $this->parent_dir . $filename);
$file_size = function_exists('getvideosize') ? getvideosize($this->parent_dir . $filename) : array();
$file_mime = function_exists('getvideosize') ? $file_size['mime'] : $mime->__toString();
$this->thumb_link = '';
2023-10-31 18:32:54 +02:00
if (function_exists('imagecreatefromavi') && in_array(self::ext($filename), array('avi', 'divx', 'xvid')))
2020-12-23 03:18:48 +02:00
{
2023-10-31 18:32:54 +02:00
$this->thumb_link .= ' <video controls="play" src="' . Url::translate_uri($this->parent_dir . $filename) . '"'
2023-11-25 23:46:57 +02:00
. ' poster="' . Url::html_output($request->server('PHP_SELF')) . '"'
. ' type="' . $file_mime . ', ' . $mimetype . ', video/' . self::ext($filename) .'"'
. ' />Your browser does not support the <code>video</code> element.'
2023-11-25 23:46:57 +02:00
. '<source src="' . Url::html_output($request->server('PHP_SELF')) . '" type="video/' . self::ext($filename) . '" />'
. '</video> ';
2023-11-25 23:46:57 +02:00
$this->thumb_link .= '</br><img src="' . Url::html_output($request->server('PHP_SELF'))
2023-10-31 18:32:54 +02:00
. '?thumbnail='. Url::translate_uri($this->parent_dir . $filename) . '"'
. ' alt="' . $words->__get('thumbnail of') . ' ' . $filename . '"'
. ' />';
}
2023-10-31 18:32:54 +02:00
elseif (in_array(self::ext($filename), array('avi', 'divx', 'xvid', 'mkv', 'asf', 'mov', 'wmv', '3gp', 'mp4', 'mpv', 'ogv', 'mpg', 'mpeg')))
{
2023-11-25 23:46:57 +02:00
$video_href = Url::html_output($request->server('PHP_SELF')) . '?thm='. Url::translate_uri($this->parent_dir . $filename);
$thumbnail = Url::html_output($request->server('PHP_SELF')) . '?thumbnail='. Url::translate_uri($this->parent_dir . $filename);
2023-10-31 18:32:54 +02:00
$this->thumb_link .= ' <video id="'.$filename.'" controls />'
. '<source src="' . $video_href . '" type="video/'. self::ext($filename) .'" />'
. '<p>Your user agent does not support the HTML5 Video element.</p></video>';
2023-10-31 18:32:54 +02:00
// if (in_array(self::ext($filename), array('avi', 'divx', 'mp4', 'mpg'))) {
$this->thumb_link .='<button type="button" onclick="vid_play_pause()">Play/Pause</button>
<script>
function vid_play_pause()
{
2023-10-31 18:32:54 +02:00
var myVideo = document.getElementById('.$filename.');
if (myVideo.paused)
{
myVideo.play();
}
else
{
myVideo.pause();
}
}
</script>';
2023-10-31 18:32:54 +02:00
//}
2023-11-25 23:46:57 +02:00
$this->thumb_link .= '</br><img src="' . Url::html_output($request->server('PHP_SELF'))
2023-10-31 18:32:54 +02:00
. '?thumbnail='. Url::translate_uri($this->parent_dir . $filename) . '"'
2023-11-25 23:46:57 +02:00
. ' alt="' . $words->__get('thumbnail of') . ' ' . $filename . '"'
2020-12-23 03:18:48 +02:00
. ' />';
2023-11-25 23:46:57 +02:00
$this->thumb_link .= ' <a href="' . Url::html_output($request->server('PHP_SELF'))
2023-10-31 18:32:54 +02:00
. '?thm='. Url::translate_uri($this->parent_dir . $filename) . '"'
. ' alt="' . $words->__get('thumbnail of') . ' ' . $filename . '"'
. ' >' . $words->__get('view') . ' ' . $words->__get('file') . '</a>';
}
elseif (in_array(self::ext($filename), array('flv', 'FLV', 'flvjs')))
{
2023-11-25 23:46:57 +02:00
$video_href = Url::html_output($request->server('PHP_SELF')) . '?thm='. Url::translate_uri($this->parent_dir . $filename);
$thumbnail = Url::html_output($request->server('PHP_SELF')) . '?thumbnail='. Url::translate_uri($this->parent_dir . $filename);
2023-10-31 18:32:54 +02:00
$this->thumb_link .= '<script src="'.$config->__get('assets_path').'/javascript/flv.min.js"></script>';
$this->thumb_link .='<VIDEO controls="play" type="video/flv" id="videoElement" src="'.$video_href.'" loop="false" allowfullscreen="true" quality="high" width="425" height="360" scale="noscale" salign="lt" name="flvPlayer" align="center" bgcolor="#E3F0FB">
<script>
if (flvjs.isSupported())
{
var videoElement = document.getElementById(videoElement);
var flvPlayer = flvjs.createPlayer({
type: flv,
url: '.$video_href.'
});
flvPlayer.attachMediaElement(videoElement);
flvPlayer.load();
flvPlayer.play();
}
</script>
<source src="' . $video_href . '" type="video/'. self::ext($filename) .'" />
</VIDEO>';
2023-11-25 23:46:57 +02:00
$this->thumb_link .= ' <a href="' . Url::html_output($request->server('PHP_SELF'))
2023-10-31 18:32:54 +02:00
. '?thm='. Url::translate_uri($this->parent_dir . $filename) . '"'
2023-11-25 23:46:57 +02:00
. ' alt="' . $words->__get('thumbnail of') . ' ' . $filename . '"'
. ' >' . $words->__get('view') . ' ' . $words->__get('file') . '</a>';
}
elseif (in_array(self::ext($filename), array('MP3', 'mp3', 'ogg')))
{
//<!-- audio tag starts here -->
2023-11-25 23:46:57 +02:00
$this->thumb_link .= ' <audio controls="play" src="' . Url::html_output($request->server('PHP_SELF'))
2023-10-31 18:32:54 +02:00
. '?thm='. Url::translate_uri($this->parent_dir . $filename) . '"'
2023-11-25 23:46:57 +02:00
. ' poster="' . Url::html_output($request->server('PHP_SELF')) . '"'
. ' type="' . $file_mime . ', ' . $mimetype . ', audio/' . self::ext($filename) .'"'
. ' />Your browser does not support the <code>audio</code> element.'
2023-11-25 23:46:57 +02:00
. '<source src="' . Url::html_output($request->server('PHP_SELF')) . '" type="audio/' . self::ext($filename) . '" />'
. '</audio> ';
//<!-- audio tag ends here -->
2020-12-23 03:18:48 +02:00
}
else
{
2023-11-25 23:46:57 +02:00
$this->thumb_link .= ' <video controls="play" src="' . Url::html_output($request->server('PHP_SELF'))
2023-10-31 18:32:54 +02:00
. '?thm='. Url::translate_uri($this->parent_dir . $filename) . '"'
2023-11-25 23:46:57 +02:00
. ' poster="' . Url::html_output($request->server('PHP_SELF')) . '"'
2020-12-23 03:18:48 +02:00
. ' type="' . $file_mime . ', ' . $mimetype . ', application/octet-stream"'
. ' />Your browser does not support the <code>video</code> element.</video> ';
2023-11-25 23:46:57 +02:00
$this->thumb_link .= ' <a href="' . Url::html_output($request->server('PHP_SELF'))
2023-10-31 18:32:54 +02:00
. '?thm='. Url::translate_uri($this->parent_dir . $filename) . '"'
2023-11-25 23:46:57 +02:00
. ' alt="' . $words->__get('thumbnail of') . ' ' . $filename . '"'
2023-10-31 18:32:54 +02:00
. ' >' . $words->__get('view') . ' ' . $words->__get('file') . '</a>';
2020-12-23 03:18:48 +02:00
}
}
$size = $this->size->__get('bytes');
/* */
2025-09-09 04:39:56 +03:00
if (($size < 1048576000) && in_array(self::ext($filename), array('pdf', 'PDF')))
2020-06-19 20:56:35 +03:00
{
$icon_pdf = ICON_PATH ? Url::translate_uri($config->__get('icon_path') . 'pdf.png') : Url::translate_uri($this->parent_dir . $filename);
$heightwidth = in_array(self::ext($filename), array('pdf', 'PDF')) ? ' height="' . '150' . '" width="' . '150' . '" ' : ' ';
$this->thumb_link .= ' <img src="' . Url::html_output($request->server('PHP_SELF'))
. '?thumbnail='. Url::translate_uri($this->parent_dir . $filename) . '"'
. ' alt="' . $words->__get('thumbnail of') . ' ' . $filename . '"' . ' />';
2025-09-09 04:39:56 +03:00
$date = array();
$stringedPDF = $contentpdf = $creationdate = $str_time = $creation_time = $time = false;
$pfdcontent = file_get_contents(Url::html_output($this->parent_dir . $filename));
2025-09-09 04:39:56 +03:00
if (preg_match('/CreationDate\\s*\\(D:([0-9]{14})/', $pfdcontent, $date))
{
$creation_time = $date[1]; //echo(' date: ' . $creation_time); //date format: YYYYMMDDHHMMSS 2025 03 30 - 09 44 53
$split = date_parse_from_format('Ymdhis', $creation_time);
//echo(date ("F d Y H:i:s.", filemtime(Url::translate_uri($this->parent_dir . $filename))));
//echo(' ' . $split['year'] .' '. $split['month'] .' '. $split['day'] .' '. $split['hour'] .' '. $split['minute'] .' '. $split['second']);
$str_time = $split['year']."-".$split['month']."-".$split['day']." ".$split['hour'].":".$split['minute'].":".$split['second'];
$time = strtotime($split['year']."-".$split['month']."-".$split['day']." ".$split['hour'].":".$split['minute'].":".$split['second']);
if ($request->is_request('restorecdate', TYPE_NO_TAGS) && touch(Url::translate_uri($this->parent_dir . $filename), $time))
{
$creationdate = date("Y-m-d H:i:s", filemtime(Url::translate_uri($this->parent_dir . $filename)));
}
else
{
$creationdate = date("Y-m-d H:i:s", $time);
}
}
else if (preg_match('/ModDate\\s*\\(D:([0-9]{14})/', $pfdcontent, $date))
{
$creation_time = $date[1];
$split = date_parse_from_format('Ymdhis', $creation_time);
$str_time = $split['year']."-".$split['month']."-".$split['day']." ".$split['hour'].":".$split['minute'].":".$split['second'];
$time = strtotime($split['year']."-".$split['month']."-".$split['day']." ".$split['hour'].":".$split['minute'].":".$split['second']);
if ($request->is_request('restorecdate', TYPE_NO_TAGS) && touch(Url::translate_uri($this->parent_dir . $filename), $time))
{
$creationdate = date("Y-m-d H:i:s", filemtime(Url::translate_uri($this->parent_dir . $filename)));
}
else
{
$creationdate = date("Y-m-d H:i:s", $time);
}
//echo(' creation date: '.$creationdate);
}
else if (preg_match('/M\\s*\\(D:([0-9]{14})/', $pfdcontent, $date))
{
$creation_time = $date[1];
$split = date_parse_from_format('Ymdhis', $creation_time);
$str_time = $split['year']."-".$split['month']."-".$split['day']." ".$split['hour'].":".$split['minute'].":".$split['second'];
$time = strtotime($split['year']."-".$split['month']."-".$split['day']." ".$split['hour'].":".$split['minute'].":".$split['second']);
if ($request->is_request('restorecdate', TYPE_NO_TAGS) && touch(Url::translate_uri($this->parent_dir . $filename), $time))
{
$creationdate = date("Y-m-d H:i:s", filemtime(Url::translate_uri($this->parent_dir . $filename)));
}
else
{
$creationdate = date("Y-m-d H:i:s", $time);
}
//echo(' creation date: '.$creationdate);
}
2020-06-19 20:56:35 +03:00
}
/* */
2023-11-25 23:46:57 +02:00
if (MD5_SHOW && $size > 0 && $size / 1048576 <= $config->__get('md5_show'))
{
2023-11-25 23:46:57 +02:00
$this->md5_link = '<span class="autoindex_small">[<a class="autoindex_a" href="'
. Url::html_output($request->server('PHP_SELF')) . '?dir='
. Url::translate_uri(substr($this->parent_dir, strlen($config->__get('base_dir'))))
2019-09-17 10:16:07 +03:00
. '&amp;md5=' . Url::translate_uri($filename) . '">'
2023-11-25 23:46:57 +02:00
. $words->__get('calculate md5sum') . '</a>]</span>';
2019-09-17 10:16:07 +03:00
}
}
/**
* @param string $var The key to look for
* @return mixed The data stored at the key
*/
2020-12-23 03:18:48 +02:00
public function __get($var = '')
2019-09-17 10:16:07 +03:00
{
2023-11-12 10:27:54 +02:00
if (isset($this->$var))
2019-09-17 10:16:07 +03:00
{
2023-11-12 10:27:54 +02:00
return $this->$var;
2019-09-17 10:16:07 +03:00
}
2020-12-23 03:18:48 +02:00
throw new ExceptionDisplay('Variable <em>' . Url::html_output($var) . '</em> not set in FileItem class.');
2019-09-17 10:16:07 +03:00
}
}
?>