]> git.mxchange.org Git - friendica.git/blobdiff - src/Core/System.php
Merge branch 'develop' into new_image_presentation
[friendica.git] / src / Core / System.php
index eb27fa351775b3e90ec6be9d230d42701f2acee0..707649a49d3406cf2f64ee7f943925bd57992cff 100644 (file)
@@ -21,6 +21,8 @@
 
 namespace Friendica\Core;
 
+use Friendica\Content\Text\BBCode;
+use Friendica\Content\Text\HTML;
 use Friendica\Core\Config\Capability\IManageConfigValues;
 use Friendica\DI;
 use Friendica\Module\Response;
@@ -323,12 +325,12 @@ class System
         * After finishing the process is getting killed.
         *
         * @param string $content
-        * @param [type] $responce
+        * @param string $type
         * @param string|null $content_type
         * @return void
         */
-       public static function httpExit(string $content, string $responce = Response::TYPE_HTML, ?string $content_type = null) {
-               DI::apiResponse()->setType($responce, $content_type);
+       public static function httpExit(string $content, string $type = Response::TYPE_HTML, ?string $content_type = null) {
+               DI::apiResponse()->setType($type, $content_type);
                DI::apiResponse()->addContent($content);
                DI::page()->exit(DI::apiResponse()->generate());
 
@@ -438,21 +440,32 @@ class System
        /**
         * Fetch the load and number of processes
         *
+        * @param bool $get_processes
         * @return array
         */
-       public static function getLoadAvg(): array
+       public static function getLoadAvg(bool $get_processes = true): array
        {
-               $content = file_get_contents('/proc/loadavg');
-               if (empty($content)) {
-                       $content = shell_exec('cat /proc/loadavg');
-               }
-               if (empty($content)) {
-                       return [];
+               if ($get_processes && @is_readable('/proc/loadavg')) {
+                       $content = @file_get_contents('/proc/loadavg');
+                       if (empty($content)) {
+                               $content = shell_exec('uptime | sed "s/.*averages*: //" | sed "s/,//g"');
+                       }
                }
 
-               if (!preg_match("#([.\d]+)\s([.\d]+)\s([.\d]+)\s(\d+)/(\d+)#", $content, $matches)) {
-                       return [];
+               if (empty($content) || !preg_match("#([.\d]+)\s([.\d]+)\s([.\d]+)\s(\d+)/(\d+)#", $content, $matches)) {
+                       $load_arr = sys_getloadavg();
+                       if (empty($load_arr)) {
+                               return [];
+                       }
+                       return [
+                               'average1'  => $load_arr[0],
+                               'average5'  => $load_arr[1],
+                               'average15' => $load_arr[2],
+                               'runnable'  => 0,
+                               'scheduled' => 0
+                       ];
                }
+
                return [
                        'average1'  => (float)$matches[1],
                        'average5'  => (float)$matches[2],
@@ -649,4 +662,30 @@ class System
                // Reaching this point means that the operating system is configured badly.
                return "";
        }
+
+       /**
+        * Fetch the system rules
+        *
+        * @return array
+        */
+       public static function getRules(): array
+       {
+               $rules = [];
+               $id    = 0;
+
+               if (DI::config()->get('system', 'tosdisplay')) {
+                       $rulelist = DI::config()->get('system', 'tosrules') ?: DI::config()->get('system', 'tostext');
+                       $html = BBCode::convert($rulelist, false, BBCode::EXTERNAL);
+
+                       $msg = HTML::toPlaintext($html, 0, true);
+                       foreach (explode("\n", $msg) as $line) {
+                               $line = trim($line);
+                               if ($line) {
+                                       $rules[] = ['id' => (string)++$id, 'text' => $line];
+                               }
+                       }
+               }
+
+               return $rules;
+       }
 }