Rename app to context.

This commit is contained in:
Lars Jung 2015-05-12 14:03:49 +02:00
parent 06b9232552
commit 057a5f05a5
12 changed files with 98 additions and 86 deletions

View file

@ -22,13 +22,13 @@ class Bootstrap {
$session = new Session($_SESSION);
$request = new Request($_REQUEST);
$setup = new Setup($request->query_boolean('refresh', false));
$app = new App($session, $request, $setup);
$context = new Context($session, $request, $setup);
if (strtolower($setup->get('REQUEST_METHOD')) === 'post') {
(new Api($app))->apply();
(new Api($context))->apply();
} else {
define('APP_HREF', $setup->get('APP_HREF'));
define('FALLBACK', (new Fallback($app))->get_html());
define('FALLBACK', (new Fallback($context))->get_html());
require __DIR__ . '/page.php';
}
}

View file

@ -2,15 +2,15 @@
class Api {
private $context;
private $request;
private $setup;
private $app;
public function __construct($app) {
public function __construct($context) {
$this->request = $app->get_request();
$this->setup = $app->get_setup();
$this->app = $app;
$this->context = $context;
$this->request = $context->get_request();
$this->setup = $context->get_setup();
}
public function apply() {
@ -25,14 +25,14 @@ class Api {
private function on_download() {
Util::json_fail(Util::ERR_DISABLED, 'download disabled', !$this->app->query_option('download.enabled', false));
Util::json_fail(Util::ERR_DISABLED, 'download disabled', !$this->context->query_option('download.enabled', false));
$as = $this->request->query('as');
$type = $this->request->query('type');
$base_href = $this->request->query('baseHref');
$hrefs = $this->request->query('hrefs');
$archive = new Archive($this->app);
$archive = new Archive($this->context);
set_time_limit(0);
header('Content-Type: application/octet-stream');
@ -52,18 +52,18 @@ class Api {
if ($this->request->query_boolean($name, false)) {
$methodname = 'get_' . $name;
$response[$name] = $this->app->$methodname();
$response[$name] = $this->context->$methodname();
}
}
if ($this->request->query_boolean('setup', false)) {
$response['setup'] = $this->setup->to_jsono($this->app->is_admin());
$response['setup'] = $this->setup->to_jsono($this->context->is_admin());
}
if ($this->request->query_boolean('theme', false)) {
$theme = new Theme($this->app);
$theme = new Theme($this->context);
$response['theme'] = $theme->get_icons();
}
@ -72,44 +72,44 @@ class Api {
$href = $this->request->query('items.href');
$what = $this->request->query_numeric('items.what');
$response['items'] = $this->app->get_items($href, $what);
$response['items'] = $this->context->get_items($href, $what);
}
if ($this->request->query('custom', false)) {
Util::json_fail(Util::ERR_DISABLED, 'custom disabled', !$this->app->query_option('custom.enabled', false));
Util::json_fail(Util::ERR_DISABLED, 'custom disabled', !$this->context->query_option('custom.enabled', false));
$href = $this->request->query('custom');
$custom = new Custom($this->app);
$custom = new Custom($this->context);
$response['custom'] = $custom->get_customizations($href);
}
if ($this->request->query('l10n', false)) {
Util::json_fail(Util::ERR_DISABLED, 'l10n disabled', !$this->app->query_option('l10n.enabled', false));
Util::json_fail(Util::ERR_DISABLED, 'l10n disabled', !$this->context->query_option('l10n.enabled', false));
$iso_codes = $this->request->query_array('l10n');
$iso_codes = array_filter($iso_codes);
$response['l10n'] = $this->app->get_l10n($iso_codes);
$response['l10n'] = $this->context->get_l10n($iso_codes);
}
if ($this->request->query('search', false)) {
Util::json_fail(Util::ERR_DISABLED, 'search disabled', !$this->app->query_option('search.enabled', false));
Util::json_fail(Util::ERR_DISABLED, 'search disabled', !$this->context->query_option('search.enabled', false));
$href = $this->request->query('search.href');
$pattern = $this->request->query('search.pattern');
$search = new Search($this->app);
$search = new Search($this->context);
$response['search'] = $search->get_items($href, $pattern);
}
if ($this->request->query('thumbs', false)) {
Util::json_fail(Util::ERR_DISABLED, 'thumbnails disabled', !$this->app->query_option('thumbnails.enabled', false));
Util::json_fail(Util::ERR_DISABLED, 'thumbnails disabled', !$this->context->query_option('thumbnails.enabled', false));
Util::json_fail(Util::ERR_UNSUPPORTED, 'thumbnails not supported', !$this->setup->get('HAS_PHP_JPEG'));
$thumbs = $this->request->query_array('thumbs');
$response['thumbs'] = $this->app->get_thumbs($thumbs);
$response['thumbs'] = $this->context->get_thumbs($thumbs);
}
Util::json_exit($response);
@ -118,11 +118,11 @@ class Api {
private function on_login() {
$pass = $this->request->query('pass');
Util::json_exit(['asAdmin' => $this->app->login_admin($pass)]);
Util::json_exit(['asAdmin' => $this->context->login_admin($pass)]);
}
private function on_logout() {
Util::json_exit(['asAdmin' => $this->app->logout_admin()]);
Util::json_exit(['asAdmin' => $this->context->logout_admin()]);
}
}

View file

@ -1,9 +1,10 @@
<?php
class App {
class Context {
private static $AS_ADMIN_SESSION_KEY = 'AS_ADMIN';
private $session;
private $request;
private $setup;
private $options;
@ -49,19 +50,19 @@ class App {
public function login_admin($pass) {
$hash = $this->setup->get('PASSHASH');
$this->session->set(App::$AS_ADMIN_SESSION_KEY, strcasecmp(hash('sha512', $pass), $hash) === 0);
return $this->session->get(App::$AS_ADMIN_SESSION_KEY);
$this->session->set(Context::$AS_ADMIN_SESSION_KEY, strcasecmp(hash('sha512', $pass), $hash) === 0);
return $this->session->get(Context::$AS_ADMIN_SESSION_KEY);
}
public function logout_admin() {
$this->session->set(App::$AS_ADMIN_SESSION_KEY, false);
return $this->session->get(App::$AS_ADMIN_SESSION_KEY);
$this->session->set(Context::$AS_ADMIN_SESSION_KEY, false);
return $this->session->get(Context::$AS_ADMIN_SESSION_KEY);
}
public function is_admin() {
return $this->session->get(App::$AS_ADMIN_SESSION_KEY);
return $this->session->get(Context::$AS_ADMIN_SESSION_KEY);
}
public function to_href($path, $trailing_slash = true) {

View file

@ -2,19 +2,19 @@
class Fallback {
private $context;
private $setup;
private $app;
public function __construct($app) {
public function __construct($context) {
$this->setup = $app->get_setup();
$this->app = $app;
$this->context = $context;
$this->setup = $context->get_setup();
}
private function get_current_path() {
$current_href = Util::normalize_path(parse_url($this->setup->get('REQUEST_URI'), PHP_URL_PATH), true);
$current_path = $this->app->to_path($current_href);
$current_path = $this->context->to_path($current_href);
if (!is_dir($current_path)) {
$current_path = Util::normalize_path(dirname($current_path), false);
@ -32,7 +32,7 @@ class Fallback {
$app_href = $this->setup->get('APP_HREF');
$cache = [];
$folder = Item::get($this->app, $path, $cache);
$folder = Item::get($this->context, $path, $cache);
$items = $folder->get_content($cache);
uasort($items, ['Item', 'cmp']);

View file

@ -14,9 +14,9 @@ class Item {
return strcasecmp($item1->path, $item2->path);
}
public static function get($app, $path, &$cache) {
public static function get($context, $path, &$cache) {
if (!Util::starts_with($path, $app->get_setup()->get('ROOT_PATH'))) {
if (!Util::starts_with($path, $context->get_setup()->get('ROOT_PATH'))) {
return null;
}
@ -24,7 +24,7 @@ class Item {
return $cache[$path];
}
$item = new Item($app, $path);
$item = new Item($context, $path);
if (is_array($cache)) {
$cache[$path] = $item;
@ -32,7 +32,7 @@ class Item {
return $item;
}
public $app;
public $context;
public $path;
public $href;
public $date;
@ -40,15 +40,15 @@ class Item {
public $is_folder;
public $is_content_fetched;
private function __construct($app, $path) {
private function __construct($context, $path) {
$this->app = $app;
$this->context = $context;
$this->path = Util::normalize_path($path, false);
$this->is_folder = is_dir($this->path);
$this->href = $app->to_href($this->path, $this->is_folder);
$this->href = $context->to_href($this->path, $this->is_folder);
$this->date = @filemtime($this->path);
$this->size = Util::filesize($app, $this->path);
$this->size = Util::filesize($context, $this->path);
$this->is_content_fetched = false;
}
@ -61,7 +61,7 @@ class Item {
];
if ($this->is_folder) {
$obj['managed'] = $this->app->is_managed_href($this->href);
$obj['managed'] = $this->context->is_managed_href($this->href);
$obj['fetched'] = $this->is_content_fetched;
}
@ -71,8 +71,8 @@ class Item {
public function get_parent(&$cache) {
$parent_path = Util::normalize_path(dirname($this->path), false);
if ($parent_path !== $this->path && Util::starts_with($parent_path, $this->app->get_setup()->get('ROOT_PATH'))) {
return Item::get($this->app, $parent_path, $cache);
if ($parent_path !== $this->path && Util::starts_with($parent_path, $this->context->get_setup()->get('ROOT_PATH'))) {
return Item::get($this->context, $parent_path, $cache);
}
return null;
}
@ -81,13 +81,13 @@ class Item {
$items = [];
if (!$this->app->is_managed_href($this->href)) {
if (!$this->context->is_managed_href($this->href)) {
return $items;
}
$files = $this->app->read_dir($this->path);
$files = $this->context->read_dir($this->path);
foreach ($files as $file) {
$item = Item::get($this->app, $this->path . '/' . $file, $cache);
$item = Item::get($this->context, $this->path . '/' . $file, $cache);
$items[$item->path] = $item;
}

View file

@ -5,6 +5,7 @@ class Setup {
private static $DEFAULT_PASSHASH = 'cf83e1357eefb8bdf1542850d66d8007d620e4050b5715dc83f4a921d36ce9ce47d0d13c5d85f2b0ff8318d2877eec2f63b931bd47417a81a538327af927da3e';
private $store;
private $refresh;
public function __construct($refresh = false) {

View file

@ -4,15 +4,17 @@ class Theme {
private static $EXTENSIONS = ['svg', 'png', 'jpg'];
public function __construct($app) {
private $context;
$this->app = $app;
public function __construct($context) {
$this->context = $context;
}
public function get_icons() {
$app_path = $this->app->get_setup()->get('APP_PATH');
$theme = $this->app->query_option('view.theme', '-NONE-');
$app_path = $this->context->get_setup()->get('APP_PATH');
$theme = $this->context->query_option('view.theme', '-NONE-');
$theme_path = $app_path . '/client/images/themes/' . $theme;
$icons = [];

View file

@ -113,7 +113,7 @@ class Util {
private static $size_cache = [];
public static function filesize($app, $path) {
public static function filesize($context, $path) {
if (array_key_exists($path, Util::$size_cache)) {
return Util::$size_cache[$path];
@ -153,14 +153,14 @@ class Util {
} else if (is_dir($path)) {
if ($app->query_option('foldersize.enabled', false)) {
if ($app->get_setup()->get('HAS_CMD_DU') && $app->query_option('foldersize.type', null) === 'shell-du') {
if ($context->query_option('foldersize.enabled', false)) {
if ($context->get_setup()->get('HAS_CMD_DU') && $context->query_option('foldersize.type', null) === 'shell-du') {
$cmdv = ['du', '-sk', $path];
$size = intval(preg_replace('#\s.*$#', '', Util::exec_cmdv($cmdv)), 10) * 1024;
} else {
$size = 0;
foreach ($app->read_dir($path) as $name) {
$size += Util::filesize($app, $path . '/' . $name);
foreach ($context->read_dir($path) as $name) {
$size += Util::filesize($context, $path . '/' . $name);
}
}
}

View file

@ -6,17 +6,20 @@ class Archive {
private static $TAR_PASSTHRU_CMD = 'cd [ROOTDIR] && tar --no-recursion -c -- [DIRS] [FILES]';
private static $ZIP_PASSTHRU_CMD = 'cd [ROOTDIR] && zip - -- [FILES]';
private $app, $base_path, $dirs, $files;
private $context;
private $base_path;
private $dirs;
private $files;
public function __construct($app) {
public function __construct($context) {
$this->app = $app;
$this->context = $context;
}
public function output($type, $base_href, $hrefs) {
$this->base_path = $this->app->to_path($base_href);
if (!$this->app->is_managed_path($this->base_path)) {
$this->base_path = $this->context->to_path($base_href);
if (!$this->context->is_managed_path($this->base_path)) {
return false;
}
@ -154,9 +157,9 @@ class Archive {
$d = Util::normalize_path(dirname($href), true);
$n = basename($href);
if ($this->app->is_managed_href($d) && !$this->app->is_hidden($n)) {
if ($this->context->is_managed_href($d) && !$this->context->is_hidden($n)) {
$real_file = $this->app->to_path($href);
$real_file = $this->context->to_path($href);
$archived_file = preg_replace('!^' . preg_quote(Util::normalize_path($this->base_path, true)) . '!', '', $real_file);
if (is_dir($real_file)) {
@ -177,10 +180,10 @@ class Archive {
private function add_dir($real_dir, $archived_dir) {
if ($this->app->is_managed_path($real_dir)) {
if ($this->context->is_managed_path($real_dir)) {
$this->dirs[] = $archived_dir;
$files = $this->app->read_dir($real_dir);
$files = $this->context->read_dir($real_dir);
foreach ($files as $file) {
$real_file = $real_dir . '/' . $file;

View file

@ -4,14 +4,16 @@ class Custom {
private static $EXTENSIONS = ['html', 'md'];
public function __construct($app) {
private $context;
$this->app = $app;
public function __construct($context) {
$this->context = $context;
}
private function read_custom_file($path, $name, &$content, &$type) {
$file_prefix = $this->app->get_setup()->get('FILE_PREFIX');
$file_prefix = $this->context->get_setup()->get('FILE_PREFIX');
foreach (Custom::$EXTENSIONS as $ext) {
$file = $path . '/' . $file_prefix . '.' . $name . '.' . $ext;
@ -25,15 +27,15 @@ class Custom {
public function get_customizations($href) {
if (!$this->app->query_option('custom.enabled', false)) {
if (!$this->context->query_option('custom.enabled', false)) {
return [
'header' => ['content' => null, 'type' => null],
'footer' => ['content' => null, 'type' => null]
];
}
$root_path = $this->app->get_setup()->get('FILE_PREFIX');
$path = $this->app->to_path($href);
$root_path = $this->context->get_setup()->get('FILE_PREFIX');
$path = $this->context->to_path($href);
$header = null;
$header_type = null;

View file

@ -2,17 +2,19 @@
class Search {
public function __construct($app) {
private $context;
$this->app = $app;
public function __construct($context) {
$this->context = $context;
}
public function get_paths($root, $pattern = null) {
$paths = [];
if ($pattern && $this->app->is_managed_path($root)) {
if ($pattern && $this->context->is_managed_path($root)) {
$re = Util::wrap_pattern($pattern);
$names = $this->app->read_dir($root);
$names = $this->context->read_dir($root);
foreach ($names as $name) {
$path = $root . '/' . $name;
if (preg_match($re, @basename($path))) {
@ -29,11 +31,11 @@ class Search {
public function get_items($href, $pattern = null) {
$cache = [];
$root = $this->app->to_path($href);
$root = $this->context->to_path($href);
$paths = $this->get_paths($root, $pattern);
$items = array_map(function ($path) {
return Item::get($this->app, $path, $cache)->to_json_object();
return Item::get($this->context, $path, $cache)->to_json_object();
}, $paths);
return $items;
}

View file

@ -7,14 +7,15 @@ class Thumb {
private static $CONVERT_CMDV = ['convert', '-density', '200', '-quality', '100', '-sharpen', '0x1.0', '-strip', '[SRC][0]', '[DEST]'];
private static $THUMB_CACHE = 'thumbs';
private $app;
private $context;
private $setup;
private $thumbs_path;
private $thumbs_href;
public function __construct($app) {
public function __construct($context) {
$this->setup = $app->get_setup();
$this->app = $app;
$this->context = $context;
$this->setup = $context->get_setup();
$this->thumbs_path = $this->setup->get('CACHE_PATH') . '/' . Thumb::$THUMB_CACHE;
$this->thumbs_href = $this->setup->get('CACHE_HREF') . Thumb::$THUMB_CACHE;
@ -25,7 +26,7 @@ class Thumb {
public function thumb($type, $source_href, $width, $height) {
$source_path = $this->app->to_path($source_href);
$source_path = $this->context->to_path($source_href);
if (!file_exists($source_path) || Util::starts_with($source_path, $this->setup->get('CACHE_PATH'))) {
return null;
}
@ -61,7 +62,7 @@ class Thumb {
$image = new Image();
$et = false;
if ($this->setup->get('HAS_PHP_EXIF') && $this->app->query_option('thumbnails.exif', false) === true && $height != 0) {
if ($this->setup->get('HAS_PHP_EXIF') && $this->context->query_option('thumbnails.exif', false) === true && $height != 0) {
$et = @exif_thumbnail($source_path);
}
if($et !== false) {