From 569cd459ec2e482ffff3de35877af8c7b3b02bfe Mon Sep 17 00:00:00 2001 From: Hypolite Petovan Date: Tue, 2 May 2017 22:42:42 -0400 Subject: [PATCH] Add basepath to App --- include/auth_ejabberd.php | 5 ++-- include/cli_startup.php | 2 +- include/dbstructure.php | 2 +- include/poller.php | 4 +-- include/shadowupdate.php | 2 +- index.php | 2 +- src/App.php | 52 +++++++++++++++++++++++++++------------ util/db_update.php | 2 +- util/maintenance.php | 2 +- util/typo.php | 2 +- 10 files changed, 48 insertions(+), 27 deletions(-) diff --git a/include/auth_ejabberd.php b/include/auth_ejabberd.php index 0b0f263ea5..9211c5baf0 100755 --- a/include/auth_ejabberd.php +++ b/include/auth_ejabberd.php @@ -49,8 +49,9 @@ require_once("boot.php"); global $a, $db; -if (is_null($a)) - $a = new App; +if (is_null($a)) { + $a = new App(dirname(__DIR__)); +} if (is_null($db)) { @include(".htconfig.php"); diff --git a/include/cli_startup.php b/include/cli_startup.php index e8cc9483db..01ee90f421 100644 --- a/include/cli_startup.php +++ b/include/cli_startup.php @@ -12,7 +12,7 @@ function cli_startup() { global $a, $db; if (is_null($a)) { - $a = new App; + $a = new App(dirname(__DIR__)); } if (is_null($db)) { diff --git a/include/dbstructure.php b/include/dbstructure.php index fc2424226d..e555138d48 100644 --- a/include/dbstructure.php +++ b/include/dbstructure.php @@ -1736,7 +1736,7 @@ function dbstructure_run(&$argv, &$argc) { global $a, $db; if (is_null($a)) { - $a = new App; + $a = new App(dirname(__DIR__)); } if (is_null($db)) { diff --git a/include/poller.php b/include/poller.php index e626a568ef..08e71fd486 100644 --- a/include/poller.php +++ b/include/poller.php @@ -19,8 +19,8 @@ require_once("boot.php"); function poller_run($argv, $argc){ global $a, $db; - if(is_null($a)) { - $a = new App; + if (is_null($a)) { + $a = new App(dirname(__DIR__)); } if(is_null($db)) { diff --git a/include/shadowupdate.php b/include/shadowupdate.php index f1888f92e5..756f220ec8 100644 --- a/include/shadowupdate.php +++ b/include/shadowupdate.php @@ -10,7 +10,7 @@ function shadowupdate_run(&$argv, &$argc){ global $a, $db; if (is_null($a)) { - $a = new App; + $a = new App(dirname(__DIR__)); } if (is_null($db)) { diff --git a/index.php b/index.php index 0b941d887d..639437017a 100644 --- a/index.php +++ b/index.php @@ -19,7 +19,7 @@ use Friendica\Core\Config; require_once 'boot.php'; require_once 'object/BaseObject.php'; -$a = new App; +$a = new App(__DIR__); BaseObject::set_app($a); // We assume that the index.php is called by a frontend process diff --git a/src/App.php b/src/App.php index c3926b622d..efb60c0386 100644 --- a/src/App.php +++ b/src/App.php @@ -40,6 +40,7 @@ class App { public $module; public $pager; public $strings; + public $basepath; public $path; public $hooks; public $timezone; @@ -112,8 +113,10 @@ class App { /** * @brief App constructor. + * + * @param string $basepath Path to the app base folder */ - function __construct() { + function __construct($basepath) { global $default_timezone; @@ -154,13 +157,6 @@ class App { startup(); - set_include_path( - get_include_path() . PATH_SEPARATOR - . 'include' . PATH_SEPARATOR - . 'library' . PATH_SEPARATOR - . 'library/langdet' . PATH_SEPARATOR - . '.'); - $this->scheme = 'http'; if ((x($_SERVER, 'HTTPS') && $_SERVER['HTTPS']) || @@ -195,6 +191,20 @@ class App { $this->hostname = $hostname; } + if (! static::directory_usable($basepath)) { + throw new Exception('Basepath ' . $basepath . ' isn\'t usable.'); + } + + $this->basepath = rtrim($basepath, DIRECTORY_SEPARATOR); + + set_include_path( + get_include_path() . PATH_SEPARATOR + . $this->basepath . DIRECTORY_SEPARATOR . 'include' . PATH_SEPARATOR + . $this->basepath . DIRECTORY_SEPARATOR . 'library' . PATH_SEPARATOR + . $this->basepath . DIRECTORY_SEPARATOR . 'library/langdet' . PATH_SEPARATOR + . $this->basepath); + + if (is_array($_SERVER['argv']) && $_SERVER['argc'] > 1 && substr(end($_SERVER['argv']), 0, 4) == 'http') { $this->set_baseurl(array_pop($_SERVER['argv'])); $_SERVER['argc'] --; @@ -284,18 +294,28 @@ class App { self::$a = $this; } + /** + * @brief Returns the base filesystem path of the App + * + * It first checks for the internal variable, then for DOCUMENT_ROOT and + * finally for PWD + * + * @return string + */ public static function get_basepath() { - $basepath = get_config('system', 'basepath'); + if (isset($this)) { + $basepath = $this->basepath; + } - if ($basepath == '') { - $basepath = dirname(__FILE__); + if (! $basepath) { + $basepath = Config::get('system', 'basepath'); } - if ($basepath == '') { + if (! $basepath && x($_SERVER, 'DOCUMENT_ROOT')) { $basepath = $_SERVER['DOCUMENT_ROOT']; } - if ($basepath == '') { + if (! $basepath && x($_SERVER, 'PWD')) { $basepath = $_SERVER['PWD']; } @@ -900,10 +920,10 @@ class App { return; } - if (get_config('system', 'proc_windows')) { - $resource = proc_open('cmd /c start /b ' . $cmdline, array(), $foo, dirname(__FILE__)); + if (Config::get('system', 'proc_windows')) { + $resource = proc_open('cmd /c start /b ' . $cmdline, array(), $foo, $this->get_basepath()); } else { - $resource = proc_open($cmdline . ' &', array(), $foo, dirname(__FILE__)); + $resource = proc_open($cmdline . ' &', array(), $foo, $this->get_basepath()); } if (!is_resource($resource)) { logger('We got no resource for command ' . $cmdline, LOGGER_DEBUG); diff --git a/util/db_update.php b/util/db_update.php index 5c71046d31..1e717e9875 100644 --- a/util/db_update.php +++ b/util/db_update.php @@ -11,7 +11,7 @@ use Friendica\App; */ require_once("boot.php"); -$a = new App; +$a = new App(dirname(__DIR__)); @include(".htconfig.php"); $lang = get_browser_language(); diff --git a/util/maintenance.php b/util/maintenance.php index 3b917aef19..40634e0d89 100644 --- a/util/maintenance.php +++ b/util/maintenance.php @@ -5,7 +5,7 @@ use Friendica\Core\Config; require_once("boot.php"); -$a = new App; +$a = new App(dirname(__DIR__)); @include(".htconfig.php"); $lang = get_browser_language(); diff --git a/util/typo.php b/util/typo.php index 2b60b319ed..522fe41001 100644 --- a/util/typo.php +++ b/util/typo.php @@ -12,7 +12,7 @@ ini_set('log_errors', '0'); include 'boot.php'; -$a = new App(); +$a = new App(dirname(__DIR__)); if (x($a->config, 'php_path')) { $phpath = $a->config['php_path']; -- 2.39.5