From 2c541afd47f775c92c4050fb1f04a17187167f8f Mon Sep 17 00:00:00 2001
From: Philipp Holzer <admin@philipp.info>
Date: Sat, 13 Oct 2018 18:57:31 +0200
Subject: [PATCH 1/3] Moved functions out of boot.php into class

- z_root() => $a->getBaseURL()
- absurl() => removed because no usage
- is_ajax() => $a->isAjax()
- current_load() => System::currentLoad()
- argc() => $a->argc
- argv($x) => $a->getArgumentValue($x)
---
 boot.php            | 78 ---------------------------------------------
 mod/admin.php       |  8 ++---
 mod/help.php        |  4 +--
 mod/item.php        |  4 +--
 mod/viewsrc.php     |  2 +-
 src/App.php         | 37 ++++++++++++++++++++-
 src/Core/System.php | 21 +++++++++++-
 src/Core/Worker.php |  2 +-
 8 files changed, 66 insertions(+), 90 deletions(-)

diff --git a/boot.php b/boot.php
index 64741b176..435de0923 100644
--- a/boot.php
+++ b/boot.php
@@ -475,44 +475,6 @@ function defaults() {
 	return $return;
 }
 
-/**
- * @brief Returns the baseurl.
- *
- * @see System::baseUrl()
- *
- * @return string
- * @TODO Function is deprecated and only used in some addons
- */
-function z_root()
-{
-	return System::baseUrl();
-}
-
-/**
- * @brief Return absolut URL for given $path.
- *
- * @param string $path given path
- *
- * @return string
- */
-function absurl($path)
-{
-	if (strpos($path, '/') === 0) {
-		return z_path() . $path;
-	}
-	return $path;
-}
-
-/**
- * @brief Function to check if request was an AJAX (xmlhttprequest) request.
- *
- * @return boolean
- */
-function is_ajax()
-{
-	return (!empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest');
-}
-
 /**
  * @brief Function to check if request was an AJAX (xmlhttprequest) request.
  *
@@ -1175,46 +1137,6 @@ function validate_include(&$file)
 	return $valid;
 }
 
-function current_load()
-{
-	if (!function_exists('sys_getloadavg')) {
-		return false;
-	}
-
-	$load_arr = sys_getloadavg();
-
-	if (!is_array($load_arr)) {
-		return false;
-	}
-
-	return max($load_arr[0], $load_arr[1]);
-}
-
-/**
- * @brief get c-style args
- *
- * @return int
- */
-function argc()
-{
-	return get_app()->argc;
-}
-
-/**
- * @brief Returns the value of a argv key
- *
- * @param int $x argv key
- * @return string Value of the argv key
- */
-function argv($x)
-{
-	if (array_key_exists($x, get_app()->argv)) {
-		return get_app()->argv[$x];
-	}
-
-	return '';
-}
-
 /**
  * @brief Get the data which is needed for infinite scroll
  *
diff --git a/mod/admin.php b/mod/admin.php
index 372210c40..ade6b0cad 100644
--- a/mod/admin.php
+++ b/mod/admin.php
@@ -77,7 +77,7 @@ function admin_post(App $a)
 				break;
 			case 'themes':
 				if ($a->argc < 2) {
-					if (is_ajax()) {
+					if ($a->isAjax()) {
 						return;
 					}
 					goaway('admin/');
@@ -107,7 +107,7 @@ function admin_post(App $a)
 				}
 
 				info(L10n::t('Theme settings updated.'));
-				if (is_ajax()) {
+				if ($a->isAjax()) {
 					return;
 				}
 				$return_path = 'admin/themes/' . $theme;
@@ -286,7 +286,7 @@ function admin_content(App $a)
 		$o = admin_page_summary($a);
 	}
 
-	if (is_ajax()) {
+	if ($a->isAjax()) {
 		echo $o;
 		killme();
 		return '';
@@ -2536,7 +2536,7 @@ function admin_page_features_post(App $a)
  */
 function admin_page_features(App $a)
 {
-	if ((argc() > 1) && (argv(1) === 'features')) {
+	if (($a->argc > 1) && ($a->argv[1] === 'features')) {
 		$arr = [];
 		$features = Feature::get(false);
 
diff --git a/mod/help.php b/mod/help.php
index 5db74c15e..53118544f 100644
--- a/mod/help.php
+++ b/mod/help.php
@@ -36,12 +36,12 @@ function help_content(App $a)
 		$path = '';
 		// looping through the argv keys bigger than 0 to build
 		// a path relative to /help
-		for ($x = 1; $x < argc(); $x ++) {
+		for ($x = 1; $x < $a->argc; $x ++) {
 			if (strlen($path)) {
 				$path .= '/';
 			}
 
-			$path .= argv($x);
+			$path .= $a->getArgumentValue($x);
 		}
 		$title = basename($path);
 		$filename = $path;
diff --git a/mod/item.php b/mod/item.php
index 5a0658b02..053a00c97 100644
--- a/mod/item.php
+++ b/mod/item.php
@@ -876,13 +876,13 @@ function item_content(App $a)
 	$o = '';
 
 	if (($a->argc == 3) && ($a->argv[1] === 'drop') && intval($a->argv[2])) {
-		if (is_ajax()) {
+		if ($a->isAjax()) {
 			$o = Item::deleteForUser(['id' => $a->argv[2]], local_user());
 		} else {
 			$o = drop_item($a->argv[2]);
 		}
 
-		if (is_ajax()) {
+		if ($a->isAjax()) {
 			// ajax return: [<item id>, 0 (no perm) | <owner id>]
 			echo json_encode([intval($a->argv[2]), intval($o)]);
 			killme();
diff --git a/mod/viewsrc.php b/mod/viewsrc.php
index afdcaada2..7ed029aa7 100644
--- a/mod/viewsrc.php
+++ b/mod/viewsrc.php
@@ -26,7 +26,7 @@ function viewsrc_content(App $a)
 	$item = Item::selectFirst(['body'], ['uid' => local_user(), 'id' => $item_id]);
 
 	if (DBA::isResult($item)) {
-		if (is_ajax()) {
+		if ($a->isAjax()) {
 			echo str_replace("\n", '<br />', $item['body']);
 			killme();
 		} else {
diff --git a/src/App.php b/src/App.php
index d763cfa43..968f07b0c 100644
--- a/src/App.php
+++ b/src/App.php
@@ -110,6 +110,11 @@ class App
 	 */
 	private $currentTheme;
 
+	/**
+	 * @var bool check if request was an AJAX (xmlhttprequest) request
+	 */
+	private $isAjax;
+
 	/**
 	 * Register a stylesheet file path to be included in the <head> tag of every page.
 	 * Inclusion is done in App->initHead().
@@ -322,6 +327,8 @@ class App
 		$this->is_mobile = $mobile_detect->isMobile();
 		$this->is_tablet = $mobile_detect->isTablet();
 
+		$this->isAjax = strtolower(defaults($_SERVER, 'HTTP_X_REQUESTED_WITH')) == 'xmlhttprequest';
+
 		// Register template engines
 		$this->registerTemplateEngine('Friendica\Render\FriendicaSmartyEngine');
 	}
@@ -1221,7 +1228,7 @@ class App
 			}
 		}
 
-		$load = current_load();
+		$load = System::currentLoad();
 		if ($load) {
 			if (intval($load) > $maxsysload) {
 				logger('system: load ' . $load . ' for ' . $process . ' tasks (' . $maxsysload . ') too high.');
@@ -1572,4 +1579,32 @@ class App
 	{
 		return Core\Theme::getStylesheetPath($this->getCurrentTheme());
 	}
+
+	/**
+	 * Check if request was an AJAX (xmlhttprequest) request.
+	 *
+	 * @return boolean true if it was an AJAX request
+	 */
+	public function isAjax()
+	{
+		return $this->isAjax;
+	}
+
+	/**
+	 * Returns the value of a argv key
+	 * TODO there are a lot of $a->argv usages in combination with defaults() which can be replaced with this method
+	 *
+	 * @param int $position the position of the argument
+	 * @param mixed $default the default value if not found
+	 *
+	 * @return mixed returns the value of the argument
+	 */
+	public function getArgumentValue($position, $default = '')
+	{
+		if (array_key_exists($position, $this->argv)) {
+			return $this->argv[$position];
+		}
+
+		return $default;
+	}
 }
diff --git a/src/Core/System.php b/src/Core/System.php
index cbffcdbef..5b5fac82f 100644
--- a/src/Core/System.php
+++ b/src/Core/System.php
@@ -216,6 +216,26 @@ class System extends BaseObject
 		return substr($trailer . uniqid('') . mt_rand(), 0, 26);
 	}
 
+	/**
+	 * Returns the current Load of the System
+	 *
+	 * @return integer
+	 */
+	public static function currentLoad()
+	{
+		if (!function_exists('sys_getloadavg')) {
+			return false;
+		}
+
+		$load_arr = sys_getloadavg();
+
+		if (!is_array($load_arr)) {
+			return false;
+		}
+
+		return max($load_arr[0], $load_arr[1]);
+	}
+
 	/// @todo Move the following functions from boot.php
 	/*
 	function killme()
@@ -232,6 +252,5 @@ class System extends BaseObject
 	function get_cachefile($file, $writemode = true)
 	function get_itemcachepath()
 	function get_spoolpath()
-	function current_load()
 	*/
 }
diff --git a/src/Core/Worker.php b/src/Core/Worker.php
index 870a5dfb2..bf7e9f3bc 100644
--- a/src/Core/Worker.php
+++ b/src/Core/Worker.php
@@ -618,7 +618,7 @@ class Worker
 		$active = self::activeWorkers();
 
 		// Decrease the number of workers at higher load
-		$load = current_load();
+		$load = System::currentLoad();
 		if ($load) {
 			$maxsysload = intval(Config::get("system", "maxloadavg", 50));
 

From 059bd1307f9590464975caa79b4b3815349dac7f Mon Sep 17 00:00:00 2001
From: Philipp Holzer <admin@philipp.info>
Date: Sat, 13 Oct 2018 19:05:54 +0200
Subject: [PATCH 2/3] rename argv to getArgumentValue

---
 mod/admin.php | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/mod/admin.php b/mod/admin.php
index ade6b0cad..d0e8ab0e3 100644
--- a/mod/admin.php
+++ b/mod/admin.php
@@ -2536,7 +2536,7 @@ function admin_page_features_post(App $a)
  */
 function admin_page_features(App $a)
 {
-	if (($a->argc > 1) && ($a->argv[1] === 'features')) {
+	if (($a->argc > 1) && ($a->getArgumentValue(1) === 'features')) {
 		$arr = [];
 		$features = Feature::get(false);
 

From 4f7cb049fb43800f4860a8d28e8d9b5c5d6ad0f3 Mon Sep 17 00:00:00 2001
From: Philipp Holzer <admin@philipp.info>
Date: Sat, 13 Oct 2018 19:10:46 +0200
Subject: [PATCH 3/3] isAjax bugfix

---
 src/App.php | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/src/App.php b/src/App.php
index 968f07b0c..97c193b3b 100644
--- a/src/App.php
+++ b/src/App.php
@@ -327,7 +327,7 @@ class App
 		$this->is_mobile = $mobile_detect->isMobile();
 		$this->is_tablet = $mobile_detect->isTablet();
 
-		$this->isAjax = strtolower(defaults($_SERVER, 'HTTP_X_REQUESTED_WITH')) == 'xmlhttprequest';
+		$this->isAjax = strtolower(defaults($_SERVER, 'HTTP_X_REQUESTED_WITH', '')) == 'xmlhttprequest';
 
 		// Register template engines
 		$this->registerTemplateEngine('Friendica\Render\FriendicaSmartyEngine');