Files
h5ai/src/_h5ai/server/php/inc/class-bootstrap.php

155 lines
5.0 KiB
PHP
Raw Normal View History

2014-09-06 01:02:38 +02:00
<?php
2014-09-20 22:21:13 +02:00
class Bootstrap {
2015-05-06 23:51:12 +02:00
private static $classpaths = ["/inc", "/inc/core", "/inc/ext"];
2014-09-20 22:21:13 +02:00
2015-05-06 23:51:12 +02:00
private $basepath;
2015-05-06 13:11:37 +02:00
2015-05-06 23:51:12 +02:00
public function __construct($basepath) {
$this->basepath = $basepath;
}
public function run() {
spl_autoload_register([$this, "autoload"]);
$this->once("config");
$this->setup_php();
$this->setup_app();
$this->setup_admin();
$this->setup_server();
$this->setup_paths();
$this->setup_cache();
$this->setup_ext_cmds();
2014-09-20 22:21:13 +02:00
$app = new App();
2015-05-01 16:16:31 +02:00
if (Util::is_post_request()) {
2014-09-20 22:21:13 +02:00
$api = new Api($app);
$api->apply();
} else {
2015-05-06 20:57:18 +02:00
$fallback = new Fallback($app);
define("FALLBACK", $fallback->get_html());
2015-05-06 23:51:12 +02:00
$this->once("inc/page");
}
}
public function autoload($class_name) {
$filename = "class-" . strtolower($class_name) . ".php";
foreach (Bootstrap::$classpaths as $path) {
$file = $this->basepath . $path . "/" . $filename;
if (file_exists($file)) {
require_once($file);
return true;
}
2014-09-20 22:21:13 +02:00
}
2015-05-06 23:51:12 +02:00
return false;
2014-09-20 22:21:13 +02:00
}
2015-05-06 23:51:12 +02:00
private function once($lib) {
require_once($this->basepath . "/" . $lib . ".php");
}
2014-09-20 22:21:13 +02:00
2015-05-02 18:01:03 +02:00
private function setup_php() {
2014-09-20 22:21:13 +02:00
2014-09-06 01:02:38 +02:00
putenv("LANG=en_US.UTF-8");
setlocale(LC_CTYPE, "en_US.UTF-8");
2015-05-05 23:20:37 +02:00
date_default_timezone_set(@date_default_timezone_get());
2014-09-06 01:02:38 +02:00
2015-05-02 18:01:03 +02:00
define("HAS_PHP_EXIF", function_exists("exif_thumbnail"));
$has_php_jpeg = false;
if (function_exists("gd_info")) {
$infos = gd_info();
$has_php_jpeg = array_key_exists("JPEG Support", $infos) && $infos["JPEG Support"];
}
define("HAS_PHP_JPEG", $has_php_jpeg);
}
private function setup_app() {
2014-09-06 01:02:38 +02:00
define("NAME", "{{pkg.name}}");
define("VERSION", "{{pkg.version}}");
define("FILE_PREFIX", "_{{pkg.name}}");
2015-05-02 16:52:50 +02:00
}
2015-05-02 18:01:03 +02:00
private function setup_admin() {
2014-09-06 01:02:38 +02:00
session_start();
define("AS_ADMIN_SESSION_KEY", "__H5AI_AS_ADMIN__");
define("AS_ADMIN", isset($_SESSION[AS_ADMIN_SESSION_KEY]) && $_SESSION[AS_ADMIN_SESSION_KEY] === true);
2015-05-06 14:12:17 +02:00
define("HAS_CUSTOM_PASSHASH", strcasecmp(PASSHASH, "cf83e1357eefb8bdf1542850d66d8007d620e4050b5715dc83f4a921d36ce9ce47d0d13c5d85f2b0ff8318d2877eec2f63b931bd47417a81a538327af927da3e") !== 0);
2015-05-02 16:52:50 +02:00
}
2015-05-02 18:01:03 +02:00
private function setup_server() {
2014-09-06 01:02:38 +02:00
$server_name = null;
$server_version = null;
$server_software = getenv("SERVER_SOFTWARE");
if ($server_software && preg_match("#^(.*?)(?:/(.*?))?(?: |$)#", strtolower($server_software), $matches)) {
$server_name = $matches[1];
$server_version = count($matches) > 2 ? $matches[2] : '';
}
define("SERVER_NAME", $server_name);
define("SERVER_VERSION", $server_version);
2015-05-05 23:55:35 +02:00
define("HAS_SERVER", in_array($server_name, ["apache", "lighttpd", "nginx", "cherokee"]));
2015-05-02 16:52:50 +02:00
}
2015-05-02 18:01:03 +02:00
private function setup_paths() {
2014-09-06 01:02:38 +02:00
$script_name = getenv("SCRIPT_NAME");
if (SERVER_NAME === "lighttpd") {
$script_name = preg_replace("#^.*?//#", "/", $script_name);
}
define("APP_HREF", Util::normalize_path(dirname(dirname(dirname($script_name))), true));
define("APP_PATH", Util::normalize_path(dirname(dirname(dirname(dirname(__FILE__)))), false));
define("ROOT_HREF", Util::normalize_path(dirname(APP_HREF), true));
define("ROOT_PATH", Util::normalize_path(dirname(APP_PATH), false));
$index_href = null;
if (@is_readable(Util::normalize_path(APP_PATH . "/server/php/index.php", false))) {
$index_href = Util::normalize_path(APP_HREF . "/server/php/index.php", false);
}
define("INDEX_HREF", $index_href);
2015-05-02 16:52:50 +02:00
}
2015-05-02 18:01:03 +02:00
private function setup_cache() {
2014-09-06 01:02:38 +02:00
define("CACHE_HREF", Util::normalize_path(APP_HREF . "/cache", true));
define("CACHE_PATH", Util::normalize_path(APP_PATH . "/cache", false));
define("HAS_WRITABLE_CACHE", @is_writable(CACHE_PATH));
2015-05-02 16:52:50 +02:00
}
2015-05-02 18:01:03 +02:00
private function setup_ext_cmds() {
2015-05-02 16:52:50 +02:00
define("CMDS_PATH", Util::normalize_path(CACHE_PATH . "/cmds.json", false));
2014-09-06 01:02:38 +02:00
$cmds = Util::load_commented_json(CMDS_PATH);
2015-05-06 17:18:08 +02:00
if (sizeof($cmds) === 0 || Util::query_boolean_request_param("updatecmds", false)) {
2014-09-06 01:02:38 +02:00
$cmds["command"] = Util::exec_0("command -v command");
$cmds["which"] = Util::exec_0("which which");
$cmd = false;
if ($cmds["command"]) {
$cmd = "command -v";
} else if ($cmds["which"]) {
$cmd = "which";
}
2015-05-05 23:55:35 +02:00
foreach (["tar", "zip", "convert", "ffmpeg", "avconv", "du"] as $c) {
2014-09-06 01:02:38 +02:00
$cmds[$c] = ($cmd !== false) && Util::exec_0($cmd . " " . $c);
}
2014-09-10 16:59:51 +02:00
Util::save_json(CMDS_PATH, $cmds);
2014-09-06 01:02:38 +02:00
}
foreach ($cmds as $c => $has) {
define("HAS_CMD_" . strtoupper($c), $has);
}
}
}