<?php
// errors
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
// config
class config {
// DEFAULT CONFIG
// Only edit directly if it is a temporary installation. Settings added here will be lost when updating!
// Instead, add options from external config file in your storage_path [_files/config/config.php]
public static $default = array(
// paths
'root' => '', // root path relative to script.
'start_path' => false, // start path relative to script. If empty, root is start path
// login
'username' => '',
'password' => '', // Add password directly or use https://tinyfilemanager.github.io/docs/pwd.html to encrypt the password (encrypted password is more secure, as it prevents your password from being exposed directly in a file).
// images
'load_images' => true,
'load_files_proxy_php' => false,
'load_images_max_filesize' => 10000000, // maximum file size (bytes) for un-resized images loaded into list
'load_svg_max_filesize' => 100000, // 100k
'image_resize_enabled' => true,
'image_resize_cache' => true, // todo: remove this option and just use 'cache?
'image_resize_dimensions' => false,
'image_resize_dimensions_retina' => false,
'image_resize_quality' => 85,
'image_resize_function' => 'imagecopyresampled', // imagecopyresampled / imagecopyresized
'image_resize_sharpen' => true,
'image_resize_memory_limit' => 128, // 128 MB is suffient to resize images around 6000 px / 0 = ignore memory
'image_resize_max_pixels' => 30000000, // 30 MP equivalent to an image 6000 x 5000 / 0 = no limit
'image_resize_min_ratio' => 1.5, // min size diff original vs resize. Only resizes if ratio > min ratio
'image_resize_cache_direct' => false, // if enabled and delete cache, must increase cache_key
// menu
'menu_enabled' => true,
'menu_show' => true,
'menu_max_depth' => 5,
'menu_sort' => 'name_asc', // name_asc, name_desc, date_asc, date_desc
'menu_cache_validate' => true,
'menu_load_all' => false,
'menu_recursive_symlinks' => true, // List sub-directories of symlinks in the main menu. May cause menu loops and/or duplicate menu items
// files layout
'layout' => 'columns', // list, blocks, grid, rows, columns
'image_cover' => true, // scales image inside container for list, block, grid and rows layouts.
'sort' => 'name_asc', // name, date, filesize, kind
'sort_dirs_first' => true,
// cache
'cache' => true,
'cache_key' => 0,
'storage_path' => '_files',
// exclude files directories regex
'files_exclude' => '', // '/\.(pdf|jpe?g)$/i'
'dirs_exclude' => '', //'/\/Convert|\/football|\/node_modules(\/|$)/i',
'allow_symlinks' => true, // allow symlinks
// various
'history' => true,
'breadcrumbs' => true,
'transitions' => true,
'click' => 'popup', // popup, modal, download, window, menu
'code_max_load' => 100000,
'code_allow_edit' => false,
'popup_interval' => 5000,
'topbar_sticky' => 'scroll', // true, false, 'scroll'
'check_updates' => true,
'allow_tasks' => true,
'get_mime_type' => false, // get file mime type from server (slow) instead of from extension (fast)
'context_menu' => true, // disable context-menu button and right-click menu
'prevent_right_click' => false, // blocks browser right-click menu on sensitive items (images, list items, menu)
'license_key' => ''
);
// config (will popuplate)
public static $config = array();
// app vars
static $__dir__ = __DIR__;
static $__file__ = __FILE__;
static $assets;
static $prod = true;
static $version = '0.2.2';
static $root;
static $doc_root;
static $has_login = false;
static $storage_path;
static $storage_is_within_doc_root = false;
static $storage_config_realpath;
static $storage_config;
static $cache_path;
static $image_resize_cache_direct;
static $image_resize_dimensions_retina = false;
static $dirs_hash = false;
static $local_config_file = '_filesconfig.php';
// get config
private function get_config($path) {
if(empty($path) || !file_exists($path)) return array();
$config = include $path;
return empty($config) || !is_array($config) ? array() : array_map(function($v){
return is_string($v) ? trim($v) : $v;
}, $config);
}
// dump config
private function dump_config($local_config, $storage_path, $storage_config, $user_config, $user_valid){
// invalid and duplicate arrays
$user_invalid = array_diff_key($user_config, self::$default);
$user_duplicate = array_intersect_assoc($user_valid, self::$default);
// items
$items = array(
['arr' => $local_config, 'comment' => "// LOCAL CONFIG\n// " . self::$local_config_file],
['arr' => $storage_config, 'comment' => "// STORAGE CONFIG\n// " . rtrim($storage_path ?: '', '\/') . '/config/config.php'],
['arr' => $user_invalid, 'comment' => "// INVALID PARAMS\n// The following custom parameters will be ignored as they are not valid:", 'var' => '$invalid', 'hide' => empty($user_invalid)],
['arr' => $user_duplicate, 'comment' => "// DUPLICATE DEFAULT PARAMS\n// The following custom parameters will have no effect as they are identical to defaults:", 'var' => '$duplicate', 'hide' => empty($user_duplicate)],
['arr' => $user_valid, 'comment' => "// USER CONFIG\n// User config parameters.", 'var' => '$user', 'hide' => (empty($local_config) || empty($storage_config)) && empty($user_invalid)],
['arr' => self::$config, 'comment' => "// CONFIG\n// User parameters merged with default parameters.", 'var' => '$config'],
['arr' => self::$default, 'comment' => "// DEFAULT CONFIG\n// Default config parameters.", 'var' => '$default'],
['arr' => array_diff_key(get_class_vars('config'), array_flip(['default', 'config'])), 'comment' => "// STATIC VARS\n// Static app vars.", 'var' => '$static']
);
// loop
$output = '<?php' . PHP_EOL;
foreach ($items as $arr => $props) {
$is_empty = empty($props['arr']);
if(isset($props['hide']) && $props['hide']) continue;
foreach (['username', 'password', 'allow_tasks', '__dir__', '__file__'] as $prop) if(isset($props['arr'][$prop]) && !empty($props['arr'][$prop]) && is_string($props['arr'][$prop])) $props['arr'][$prop] = '***';
$export = $is_empty ? 'array ()' : var_export($props['arr'], true);
$comment = preg_replace('/\n/', " [" . count($props['arr']) . "]\n", $props['comment'], 1);
$var = isset($props['var']) ? $props['var'] . ' = ' : 'return ';
$output .= PHP_EOL . $comment . PHP_EOL . $var . $export . ';' . PHP_EOL;
}
highlight_string($output . PHP_EOL . ';?>');
exit;
}
//public static function helloWorld() {
public static function save_config($config = array()){
$save_config = array_intersect_key(array_replace(self::$storage_config, $config), self::$default);
$export = preg_replace("/ '/", " //'", var_export(array_replace(self::$default, $save_config), true));
foreach ($save_config as $key => $value) if($value !== self::$default[$key]) $export = str_replace("//'" . $key, "'" . $key, $export);
return @file_put_contents(config::$storage_config_realpath, '<?php ' . PHP_EOL . PHP_EOL . '// CONFIG / https://forum.photo.gallery/viewtopic.php?f=66&t=9964' . PHP_EOL . '// Uncomment the parameters you want to edit.' . PHP_EOL . 'return ' . $export . ';');
}
// construct
function __construct($is_doc = false) {
// normalize OS paths
self::$__dir__ = real_path(__DIR__);
self::$__file__ = real_path(__FILE__);
// local config
$local_config = self::get_config(self::$local_config_file);
// storage config
$storage_path = isset($local_config['storage_path']) ? $local_config['storage_path'] : self::$default['storage_path'];
$storage_realpath = !empty($storage_path) ? real_path($storage_path) : false;
if($is_doc && $sto