mirror of
https://github.com/lrsjng/h5ai.git
synced 2025-05-28 14:04:45 -04:00
Clean PHP code.
This commit is contained in:
parent
34f86997cb
commit
788f4f047c
9 changed files with 22 additions and 55 deletions
|
@ -5,7 +5,7 @@ class Fallback {
|
||||||
private $setup;
|
private $setup;
|
||||||
private $app;
|
private $app;
|
||||||
|
|
||||||
function __construct($app) {
|
public function __construct($app) {
|
||||||
|
|
||||||
$this->setup = $app->get_setup();
|
$this->setup = $app->get_setup();
|
||||||
$this->app = $app;
|
$this->app = $app;
|
||||||
|
|
|
@ -14,7 +14,6 @@ class Item {
|
||||||
return strcasecmp($item1->path, $item2->path);
|
return strcasecmp($item1->path, $item2->path);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public static function get($app, $path, &$cache) {
|
public static function get($app, $path, &$cache) {
|
||||||
|
|
||||||
if (!Util::starts_with($path, $app->get_setup()->get('ROOT_PATH'))) {
|
if (!Util::starts_with($path, $app->get_setup()->get('ROOT_PATH'))) {
|
||||||
|
@ -33,11 +32,13 @@ class Item {
|
||||||
return $item;
|
return $item;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public $app;
|
public $app;
|
||||||
public $path, $href, $date, $size;
|
public $path;
|
||||||
public $is_folder, $is_content_fetched;
|
public $href;
|
||||||
|
public $date;
|
||||||
|
public $size;
|
||||||
|
public $is_folder;
|
||||||
|
public $is_content_fetched;
|
||||||
|
|
||||||
private function __construct($app, $path) {
|
private function __construct($app, $path) {
|
||||||
|
|
||||||
|
@ -51,7 +52,6 @@ class Item {
|
||||||
$this->is_content_fetched = false;
|
$this->is_content_fetched = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public function to_json_object() {
|
public function to_json_object() {
|
||||||
|
|
||||||
$obj = [
|
$obj = [
|
||||||
|
@ -68,7 +68,6 @@ class Item {
|
||||||
return $obj;
|
return $obj;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public function get_parent(&$cache) {
|
public function get_parent(&$cache) {
|
||||||
|
|
||||||
$parent_path = Util::normalize_path(dirname($this->path), false);
|
$parent_path = Util::normalize_path(dirname($this->path), false);
|
||||||
|
@ -78,7 +77,6 @@ class Item {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public function get_content(&$cache) {
|
public function get_content(&$cache) {
|
||||||
|
|
||||||
$items = [];
|
$items = [];
|
||||||
|
|
|
@ -4,7 +4,7 @@ class Theme {
|
||||||
|
|
||||||
private static $extensions = ['svg', 'png', 'jpg'];
|
private static $extensions = ['svg', 'png', 'jpg'];
|
||||||
|
|
||||||
function __construct($app) {
|
public function __construct($app) {
|
||||||
|
|
||||||
$this->app = $app;
|
$this->app = $app;
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,7 +2,6 @@
|
||||||
|
|
||||||
class Util {
|
class Util {
|
||||||
|
|
||||||
|
|
||||||
const ERR_MISSING_PARAM = 'ERR_MISSING_PARAM';
|
const ERR_MISSING_PARAM = 'ERR_MISSING_PARAM';
|
||||||
const ERR_ILLIGAL_PARAM = 'ERR_ILLIGAL_PARAM';
|
const ERR_ILLIGAL_PARAM = 'ERR_ILLIGAL_PARAM';
|
||||||
const ERR_FAILED = 'ERR_FAILED';
|
const ERR_FAILED = 'ERR_FAILED';
|
||||||
|
@ -11,14 +10,12 @@ class Util {
|
||||||
const NO_DEFAULT = 'NO_*@+#?!_DEFAULT';
|
const NO_DEFAULT = 'NO_*@+#?!_DEFAULT';
|
||||||
const RE_DELIMITER = '@';
|
const RE_DELIMITER = '@';
|
||||||
|
|
||||||
|
|
||||||
public static function normalize_path($path, $trailing_slash = false) {
|
public static function normalize_path($path, $trailing_slash = false) {
|
||||||
|
|
||||||
$path = preg_replace('#[\\\\/]+#', '/', $path);
|
$path = preg_replace('#[\\\\/]+#', '/', $path);
|
||||||
return preg_match('#^(\w:)?/$#', $path) ? $path : (rtrim($path, '/') . ($trailing_slash ? '/' : ''));
|
return preg_match('#^(\w:)?/$#', $path) ? $path : (rtrim($path, '/') . ($trailing_slash ? '/' : ''));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public static function json_exit($obj = []) {
|
public static function json_exit($obj = []) {
|
||||||
|
|
||||||
header('Content-type: application/json;charset=utf-8');
|
header('Content-type: application/json;charset=utf-8');
|
||||||
|
@ -26,7 +23,6 @@ class Util {
|
||||||
exit;
|
exit;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public static function json_fail($err, $msg = '', $cond = true) {
|
public static function json_fail($err, $msg = '', $cond = true) {
|
||||||
|
|
||||||
if ($cond) {
|
if ($cond) {
|
||||||
|
@ -34,7 +30,6 @@ class Util {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public static function array_query($array, $keypath = '', $default = Util::NO_DEFAULT) {
|
public static function array_query($array, $keypath = '', $default = Util::NO_DEFAULT) {
|
||||||
|
|
||||||
$value = $array;
|
$value = $array;
|
||||||
|
@ -50,25 +45,21 @@ class Util {
|
||||||
return $value;
|
return $value;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public static function starts_with($sequence, $head) {
|
public static function starts_with($sequence, $head) {
|
||||||
|
|
||||||
return substr($sequence, 0, strlen($head)) === $head;
|
return substr($sequence, 0, strlen($head)) === $head;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public static function ends_with($sequence, $tail) {
|
public static function ends_with($sequence, $tail) {
|
||||||
|
|
||||||
return substr($sequence, -strlen($tail)) === $tail;
|
return substr($sequence, -strlen($tail)) === $tail;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public static function wrap_pattern($pattern) {
|
public static function wrap_pattern($pattern) {
|
||||||
|
|
||||||
return Util::RE_DELIMITER . str_replace(Util::RE_DELIMITER, '\\' . Util::RE_DELIMITER, $pattern) . Util::RE_DELIMITER;
|
return Util::RE_DELIMITER . str_replace(Util::RE_DELIMITER, '\\' . Util::RE_DELIMITER, $pattern) . Util::RE_DELIMITER;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public static function load_commented_json($path) {
|
public static function load_commented_json($path) {
|
||||||
|
|
||||||
if (!file_exists($path)) {
|
if (!file_exists($path)) {
|
||||||
|
@ -83,14 +74,12 @@ class Util {
|
||||||
return json_decode($content, true);
|
return json_decode($content, true);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public static function save_json($path, $obj) {
|
public static function save_json($path, $obj) {
|
||||||
|
|
||||||
$json = json_encode($obj);
|
$json = json_encode($obj);
|
||||||
return file_put_contents($path, $json) !== false;
|
return file_put_contents($path, $json) !== false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public static function passthru_cmd($cmd) {
|
public static function passthru_cmd($cmd) {
|
||||||
|
|
||||||
$rc = null;
|
$rc = null;
|
||||||
|
@ -98,7 +87,6 @@ class Util {
|
||||||
return $rc;
|
return $rc;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public static function exec_cmdv($cmdv) {
|
public static function exec_cmdv($cmdv) {
|
||||||
|
|
||||||
if (!is_array($cmdv)) {
|
if (!is_array($cmdv)) {
|
||||||
|
@ -112,7 +100,6 @@ class Util {
|
||||||
return implode("\n", $lines);
|
return implode("\n", $lines);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public static function exec_0($cmd) {
|
public static function exec_0($cmd) {
|
||||||
|
|
||||||
$lines = [];
|
$lines = [];
|
||||||
|
@ -124,7 +111,6 @@ class Util {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
private static $size_cache = [];
|
private static $size_cache = [];
|
||||||
|
|
||||||
public static function filesize($app, $path) {
|
public static function filesize($app, $path) {
|
||||||
|
|
|
@ -8,13 +8,11 @@ class Archive {
|
||||||
|
|
||||||
private $app, $base_path, $dirs, $files;
|
private $app, $base_path, $dirs, $files;
|
||||||
|
|
||||||
|
|
||||||
public function __construct($app) {
|
public function __construct($app) {
|
||||||
|
|
||||||
$this->app = $app;
|
$this->app = $app;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public function output($type, $base_href, $hrefs) {
|
public function output($type, $base_href, $hrefs) {
|
||||||
|
|
||||||
$this->base_path = $this->app->to_path($base_href);
|
$this->base_path = $this->app->to_path($base_href);
|
||||||
|
@ -50,7 +48,6 @@ class Archive {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
private function shell_cmd($cmd) {
|
private function shell_cmd($cmd) {
|
||||||
|
|
||||||
$cmd = str_replace('[ROOTDIR]', escapeshellarg($this->base_path), $cmd);
|
$cmd = str_replace('[ROOTDIR]', escapeshellarg($this->base_path), $cmd);
|
||||||
|
@ -64,7 +61,6 @@ class Archive {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
private function php_tar($dirs, $files) {
|
private function php_tar($dirs, $files) {
|
||||||
|
|
||||||
$filesizes = [];
|
$filesizes = [];
|
||||||
|
@ -101,7 +97,6 @@ class Archive {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
private function php_tar_header($filename, $size, $mtime, $type) {
|
private function php_tar_header($filename, $size, $mtime, $type) {
|
||||||
|
|
||||||
$name = substr(basename($filename), -99);
|
$name = substr(basename($filename), -99);
|
||||||
|
@ -135,7 +130,6 @@ class Archive {
|
||||||
return $header;
|
return $header;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
private function print_file($file) {
|
private function print_file($file) {
|
||||||
|
|
||||||
// Send file content in segments to not hit PHP's memory limit (default: 128M)
|
// Send file content in segments to not hit PHP's memory limit (default: 128M)
|
||||||
|
@ -149,7 +143,6 @@ class Archive {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
private function add_hrefs($hrefs) {
|
private function add_hrefs($hrefs) {
|
||||||
|
|
||||||
foreach ($hrefs as $href) {
|
foreach ($hrefs as $href) {
|
||||||
|
@ -175,7 +168,6 @@ class Archive {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
private function add_file($real_file, $archived_file) {
|
private function add_file($real_file, $archived_file) {
|
||||||
|
|
||||||
if (is_readable($real_file)) {
|
if (is_readable($real_file)) {
|
||||||
|
@ -183,7 +175,6 @@ class Archive {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
private function add_dir($real_dir, $archived_dir) {
|
private function add_dir($real_dir, $archived_dir) {
|
||||||
|
|
||||||
if ($this->app->is_managed_path($real_dir)) {
|
if ($this->app->is_managed_path($real_dir)) {
|
||||||
|
|
|
@ -4,7 +4,7 @@ class Custom {
|
||||||
|
|
||||||
private static $extensions = ['html', 'md'];
|
private static $extensions = ['html', 'md'];
|
||||||
|
|
||||||
function __construct($app) {
|
public function __construct($app) {
|
||||||
|
|
||||||
$this->app = $app;
|
$this->app = $app;
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,12 +2,12 @@
|
||||||
|
|
||||||
class Search {
|
class Search {
|
||||||
|
|
||||||
function __construct($app) {
|
public function __construct($app) {
|
||||||
|
|
||||||
$this->app = $app;
|
$this->app = $app;
|
||||||
}
|
}
|
||||||
|
|
||||||
function get_paths($root, $pattern = null) {
|
public function get_paths($root, $pattern = null) {
|
||||||
|
|
||||||
$paths = [];
|
$paths = [];
|
||||||
if ($pattern && $this->app->is_managed_path($root)) {
|
if ($pattern && $this->app->is_managed_path($root)) {
|
||||||
|
@ -26,7 +26,7 @@ class Search {
|
||||||
return $paths;
|
return $paths;
|
||||||
}
|
}
|
||||||
|
|
||||||
function get_items($href, $pattern = null) {
|
public function get_items($href, $pattern = null) {
|
||||||
|
|
||||||
$cache = [];
|
$cache = [];
|
||||||
$root = $this->app->to_path($href);
|
$root = $this->app->to_path($href);
|
||||||
|
|
|
@ -7,9 +7,9 @@ class Thumb {
|
||||||
private static $CONVERT_CMDV = ['convert', '-density', '200', '-quality', '100', '-sharpen', '0x1.0', '-strip', '[SRC][0]', '[DEST]'];
|
private static $CONVERT_CMDV = ['convert', '-density', '200', '-quality', '100', '-sharpen', '0x1.0', '-strip', '[SRC][0]', '[DEST]'];
|
||||||
private static $THUMB_CACHE = 'thumbs';
|
private static $THUMB_CACHE = 'thumbs';
|
||||||
|
|
||||||
|
private $app;
|
||||||
private $app, $thumbs_path, $thumbs_href;
|
private $thumbs_path;
|
||||||
|
private $thumbs_href;
|
||||||
|
|
||||||
public function __construct($app) {
|
public function __construct($app) {
|
||||||
|
|
||||||
|
@ -23,7 +23,6 @@ class Thumb {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public function thumb($type, $source_href, $width, $height) {
|
public function thumb($type, $source_href, $width, $height) {
|
||||||
|
|
||||||
$source_path = $this->app->to_path($source_href);
|
$source_path = $this->app->to_path($source_href);
|
||||||
|
@ -47,7 +46,6 @@ class Thumb {
|
||||||
return $this->thumb_href($capture_path, $width, $height);
|
return $this->thumb_href($capture_path, $width, $height);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
private function thumb_href($source_path, $width, $height) {
|
private function thumb_href($source_path, $width, $height) {
|
||||||
|
|
||||||
if (!file_exists($source_path)) {
|
if (!file_exists($source_path)) {
|
||||||
|
@ -81,7 +79,6 @@ class Thumb {
|
||||||
return file_exists($thumb_path) ? $thumb_href : null;
|
return file_exists($thumb_path) ? $thumb_href : null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
private function capture($cmdv, $source_path) {
|
private function capture($cmdv, $source_path) {
|
||||||
|
|
||||||
if (!file_exists($source_path)) {
|
if (!file_exists($source_path)) {
|
||||||
|
@ -104,11 +101,14 @@ class Thumb {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
class Image {
|
class Image {
|
||||||
|
|
||||||
private $source_file, $source, $width, $height, $type, $dest;
|
private $source_file;
|
||||||
|
private $source;
|
||||||
|
private $width;
|
||||||
|
private $height;
|
||||||
|
private $type;
|
||||||
|
private $dest;
|
||||||
|
|
||||||
public function __construct($filename = null) {
|
public function __construct($filename = null) {
|
||||||
|
|
||||||
|
@ -123,14 +123,12 @@ class Image {
|
||||||
$this->set_source($filename);
|
$this->set_source($filename);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public function __destruct() {
|
public function __destruct() {
|
||||||
|
|
||||||
$this->release_source();
|
$this->release_source();
|
||||||
$this->release_dest();
|
$this->release_dest();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public function set_source($filename) {
|
public function set_source($filename) {
|
||||||
|
|
||||||
$this->release_source();
|
$this->release_source();
|
||||||
|
@ -155,7 +153,6 @@ class Image {
|
||||||
$this->source = imagecreatefromstring(file_get_contents($this->source_file));
|
$this->source = imagecreatefromstring(file_get_contents($this->source_file));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public function save_dest_jpeg($filename, $quality = 80) {
|
public function save_dest_jpeg($filename, $quality = 80) {
|
||||||
|
|
||||||
if (!is_null($this->dest)) {
|
if (!is_null($this->dest)) {
|
||||||
|
@ -164,7 +161,6 @@ class Image {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public function release_dest() {
|
public function release_dest() {
|
||||||
|
|
||||||
if (!is_null($this->dest)) {
|
if (!is_null($this->dest)) {
|
||||||
|
@ -173,7 +169,6 @@ class Image {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public function release_source() {
|
public function release_source() {
|
||||||
|
|
||||||
if (!is_null($this->source)) {
|
if (!is_null($this->source)) {
|
||||||
|
@ -186,7 +181,6 @@ class Image {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public function thumb($width, $height) {
|
public function thumb($width, $height) {
|
||||||
|
|
||||||
if (is_null($this->source)) {
|
if (is_null($this->source)) {
|
||||||
|
@ -232,7 +226,6 @@ class Image {
|
||||||
imagecopyresampled($this->dest, $this->source, 0, 0, $src_x, 0, $width, $height, $src_w, $src_h);
|
imagecopyresampled($this->dest, $this->source, 0, 0, $src_x, 0, $width, $height, $src_w, $src_h);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public function rotate($angle) {
|
public function rotate($angle) {
|
||||||
|
|
||||||
if (is_null($this->source) || ($angle !== 90 && $angle !== 180 && $angle !== 270)) {
|
if (is_null($this->source) || ($angle !== 90 && $angle !== 180 && $angle !== 270)) {
|
||||||
|
@ -245,7 +238,6 @@ class Image {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public function normalize_exif_orientation($exif_source_file = null) {
|
public function normalize_exif_orientation($exif_source_file = null) {
|
||||||
|
|
||||||
if (is_null($this->source) || !function_exists('exif_read_data')) {
|
if (is_null($this->source) || !function_exists('exif_read_data')) {
|
||||||
|
|
|
@ -2,7 +2,7 @@
|
||||||
|
|
||||||
define('MIN_PHP_VERSION', '5.4.0');
|
define('MIN_PHP_VERSION', '5.4.0');
|
||||||
|
|
||||||
if (version_compare(PHP_VERSION, MIN_PHP_VERSION) < 0) {
|
if (version_compare(PHP_VERSION, MIN_PHP_VERSION, '<')) {
|
||||||
header('Content-type: application/json;charset=utf-8');
|
header('Content-type: application/json;charset=utf-8');
|
||||||
echo '{"err":"ERR_PHP","msg":"PHP ' . MIN_PHP_VERSION . '+ required","ver":"' . PHP_VERSION . '"}';
|
echo '{"err":"ERR_PHP","msg":"PHP ' . MIN_PHP_VERSION . '+ required","ver":"' . PHP_VERSION . '"}';
|
||||||
exit;
|
exit;
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue