]> git.mxchange.org Git - friendica.git/blob - src/Core/System.php
Move System::jsonError to BaseModule->jsonError
[friendica.git] / src / Core / System.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2010-2023, the Friendica project
4  *
5  * @license GNU AGPL version 3 or any later version
6  *
7  * This program is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU Affero General Public License as
9  * published by the Free Software Foundation, either version 3 of the
10  * License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU Affero General Public License for more details.
16  *
17  * You should have received a copy of the GNU Affero General Public License
18  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
19  *
20  */
21
22 namespace Friendica\Core;
23
24 use Friendica\Content\Text\BBCode;
25 use Friendica\Content\Text\HTML;
26 use Friendica\Core\Config\Capability\IManageConfigValues;
27 use Friendica\DI;
28 use Friendica\Model\User;
29 use Friendica\Module\Response;
30 use Friendica\Network\HTTPException\FoundException;
31 use Friendica\Network\HTTPException\InternalServerErrorException;
32 use Friendica\Network\HTTPException\MovedPermanentlyException;
33 use Friendica\Network\HTTPException\TemporaryRedirectException;
34 use Friendica\Util\BasePath;
35 use Friendica\Util\XML;
36 use Psr\Http\Message\ResponseInterface;
37 use Psr\Log\LoggerInterface;
38
39 /**
40  * Contains the class with system relevant stuff
41  */
42 class System
43 {
44         /**
45          * @var LoggerInterface
46          */
47         private $logger;
48
49         /**
50          * @var IManageConfigValues
51          */
52         private $config;
53
54         /**
55          * @var string
56          */
57         private $basePath;
58
59         public function __construct(LoggerInterface $logger, IManageConfigValues $config, string $basepath)
60         {
61                 $this->logger   = $logger;
62                 $this->config   = $config;
63                 $this->basePath = $basepath;
64         }
65
66         /**
67          * Checks if the maximum number of database processes is reached
68          *
69          * @return bool Is the limit reached?
70          */
71         public function isMaxProcessesReached(): bool
72         {
73                 // Deactivated, needs more investigating if this check really makes sense
74                 return false;
75
76                 /*
77                  * Commented out to suppress static analyzer issues
78                  *
79                 if ($this->mode->isBackend()) {
80                         $process = 'backend';
81                         $max_processes = $this->config->get('system', 'max_processes_backend');
82                         if (intval($max_processes) == 0) {
83                                 $max_processes = 5;
84                         }
85                 } else {
86                         $process = 'frontend';
87                         $max_processes = $this->config->get('system', 'max_processes_frontend');
88                         if (intval($max_processes) == 0) {
89                                 $max_processes = 20;
90                         }
91                 }
92
93                 $processlist = DBA::processlist();
94                 if ($processlist['list'] != '') {
95                         $this->logger->debug('Processcheck: Processes: ' . $processlist['amount'] . ' - Processlist: ' . $processlist['list']);
96
97                         if ($processlist['amount'] > $max_processes) {
98                                 $this->logger->debug('Processcheck: Maximum number of processes for ' . $process . ' tasks (' . $max_processes . ') reached.');
99                                 return true;
100                         }
101                 }
102                 return false;
103                  */
104         }
105
106         /**
107          * Checks if the minimal memory is reached
108          *
109          * @return bool Is the memory limit reached?
110          */
111         public function isMinMemoryReached(): bool
112         {
113                 // Deactivated, needs more investigating if this check really makes sense
114                 return false;
115
116                 /*
117                  * Commented out to suppress static analyzer issues
118                  *
119                 $min_memory = $this->config->get('system', 'min_memory', 0);
120                 if ($min_memory == 0) {
121                         return false;
122                 }
123
124                 if (!is_readable('/proc/meminfo')) {
125                         return false;
126                 }
127
128                 $memdata = explode("\n", file_get_contents('/proc/meminfo'));
129
130                 $meminfo = [];
131                 foreach ($memdata as $line) {
132                         $data = explode(':', $line);
133                         if (count($data) != 2) {
134                                 continue;
135                         }
136                         [$key, $val]     = $data;
137                         $meminfo[$key]   = (int)trim(str_replace('kB', '', $val));
138                         $meminfo[$key]   = (int)($meminfo[$key] / 1024);
139                 }
140
141                 if (!isset($meminfo['MemFree'])) {
142                         return false;
143                 }
144
145                 $free = $meminfo['MemFree'];
146
147                 $reached = ($free < $min_memory);
148
149                 if ($reached) {
150                         $this->logger->warning('Minimal memory reached.', ['free' => $free, 'memtotal' => $meminfo['MemTotal'], 'limit' => $min_memory]);
151                 }
152
153                 return $reached;
154                  */
155         }
156
157         /**
158          * Checks if the maximum load is reached
159          *
160          * @return bool Is the load reached?
161          */
162         public function isMaxLoadReached(): bool
163         {
164                 $maxsysload = intval($this->config->get('system', 'maxloadavg'));
165                 if ($maxsysload < 1) {
166                         $maxsysload = 50;
167                 }
168
169                 $load = System::currentLoad();
170                 if ($load) {
171                         if (intval($load) > $maxsysload) {
172                                 $this->logger->notice('system load for process too high.', ['load' => $load, 'process' => 'backend', 'maxsysload' => $maxsysload]);
173                                 return true;
174                         }
175                 }
176                 return false;
177         }
178
179         /**
180          * Executes a child process with 'proc_open'
181          *
182          * @param string $command The command to execute
183          * @param array  $args    Arguments to pass to the command ( [ 'key' => value, 'key2' => value2, ... ]
184          */
185         public function run(string $command, array $args)
186         {
187                 if (!function_exists('proc_open')) {
188                         $this->logger->warning('"proc_open" not available - quitting');
189                         return;
190                 }
191
192                 $cmdline = $this->config->get('config', 'php_path', 'php') . ' ' . escapeshellarg($command);
193
194                 foreach ($args as $key => $value) {
195                         if (!is_null($value) && is_bool($value) && !$value) {
196                                 continue;
197                         }
198
199                         $cmdline .= ' --' . $key;
200                         if (!is_null($value) && !is_bool($value)) {
201                                 $cmdline .= ' ' . $value;
202                         }
203                 }
204
205                 if ($this->isMinMemoryReached()) {
206                         $this->logger->warning('Memory limit reached - quitting');
207                         return;
208                 }
209
210                 if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') {
211                         $resource = proc_open('cmd /c start /b ' . $cmdline, [], $foo, $this->basePath);
212                 } else {
213                         $resource = proc_open($cmdline . ' &', [], $foo, $this->basePath);
214                 }
215
216                 if (!is_resource($resource)) {
217                         $this->logger->warning('We got no resource for command.', ['command' => $cmdline]);
218                         return;
219                 }
220
221                 proc_close($resource);
222
223                 $this->logger->info('Executed "proc_open"', ['command' => $cmdline, 'callstack' => System::callstack(10)]);
224         }
225
226         /**
227          * Returns a string with a callstack. Can be used for logging.
228          *
229          * @param integer $depth  How many calls to include in the stacks after filtering
230          * @param int     $offset How many calls to shave off the top of the stack, for example if
231          *                        this is called from a centralized method that isn't relevant to the callstack
232          * @param bool    $full   If enabled, the callstack is not compacted
233          * @return string
234          */
235         public static function callstack(int $depth = 4, int $offset = 0, bool $full = false): string
236         {
237                 $trace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS);
238
239                 // We remove at least the first two items from the list since they contain data that we don't need.
240                 $trace = array_slice($trace, 2 + $offset);
241
242                 $callstack = [];
243                 $previous = ['class' => '', 'function' => '', 'database' => false];
244
245                 // The ignore list contains all functions that are only wrapper functions
246                 $ignore = ['call_user_func_array'];
247
248                 while ($func = array_pop($trace)) {
249                         if (!empty($func['class'])) {
250                                 if (!$full && in_array($previous['function'], ['insert', 'fetch', 'toArray', 'exists', 'count', 'selectFirst', 'selectToArray',
251                                         'select', 'update', 'delete', 'selectFirstForUser', 'selectForUser'])
252                                         && (substr($previous['class'], 0, 15) === 'Friendica\Model')) {
253                                         continue;
254                                 }
255
256                                 // Don't show multiple calls from the Database classes to show the essential parts of the callstack
257                                 $func['database'] = in_array($func['class'], ['Friendica\Database\DBA', 'Friendica\Database\Database']);
258                                 if ($full || !$previous['database'] || !$func['database']) {
259                                         $classparts = explode("\\", $func['class']);
260                                         $callstack[] = array_pop($classparts).'::'.$func['function'] . (isset($func['line']) ? ' (' . $func['line'] . ')' : '');
261                                         $previous = $func;
262                                 }
263                         } elseif (!in_array($func['function'], $ignore)) {
264                                 $func['database'] = ($func['function'] == 'q');
265                                 $callstack[] = $func['function'] . (isset($func['line']) ? ' (' . $func['line'] . ')' : '');
266                                 $func['class'] = '';
267                                 $previous = $func;
268                         }
269                 }
270
271                 $callstack2 = [];
272                 while ((count($callstack2) < $depth) && (count($callstack) > 0)) {
273                         $callstack2[] = array_pop($callstack);
274                 }
275
276                 return implode(', ', $callstack2);
277         }
278
279         /**
280          * Display current response, including setting all headers
281          *
282          * @param ResponseInterface $response
283          */
284         public static function echoResponse(ResponseInterface $response)
285         {
286                 header(sprintf("HTTP/%s %s %s",
287                                 $response->getProtocolVersion(),
288                                 $response->getStatusCode(),
289                                 $response->getReasonPhrase())
290                 );
291
292                 foreach ($response->getHeaders() as $key => $header) {
293                         if (is_array($header)) {
294                                 $header_str = implode(',', $header);
295                         } else {
296                                 $header_str = $header;
297                         }
298
299                         if (is_int($key)) {
300                                 header($header_str);
301                         } else {
302                                 header("$key: $header_str");
303                         }
304                 }
305
306                 echo $response->getBody();
307         }
308
309         /**
310          * Generic XML return
311          * Outputs a basic dfrn XML status structure to STDOUT, with a <status> variable
312          * of $st and an optional text <message> of $message and terminates the current process.
313          *
314          * @param        $st
315          * @param string $message
316          * @throws \Exception
317          */
318         public static function xmlExit($st, $message = '')
319         {
320                 $result = ['status' => $st];
321
322                 if ($message != '') {
323                         $result['message'] = $message;
324                 }
325
326                 if ($st) {
327                         Logger::notice('xml_status returning non_zero: ' . $st . " message=" . $message);
328                 }
329
330                 DI::apiResponse()->setType(Response::TYPE_XML);
331                 DI::apiResponse()->addContent(XML::fromArray(['result' => $result]));
332                 self::echoResponse(DI::apiResponse()->generate());
333
334                 self::exit();
335         }
336
337         /**
338          * Send HTTP status header and exit.
339          *
340          * @param integer $val     HTTP status result value
341          * @param string  $message Error message. Optional.
342          * @param string  $content Response body. Optional.
343          * @throws \Exception
344          * @deprecated since 2023.09 Use BaseModule->httpError instead
345          */
346         public static function httpError($httpCode, $message = '', $content = '')
347         {
348                 if ($httpCode >= 400) {
349                         Logger::debug('Exit with error', ['code' => $httpCode, 'message' => $message, 'callstack' => System::callstack(20), 'method' => DI::args()->getMethod(), 'agent' => $_SERVER['HTTP_USER_AGENT'] ?? '']);
350                 }
351                 DI::apiResponse()->setStatus($httpCode, $message);
352
353                 self::httpExit($content);
354         }
355
356         /**
357          * This function adds the content and a content-type HTTP header to the output.
358          * After finishing the process is getting killed.
359          *
360          * @param string      $content
361          * @param string      $type
362          * @param string|null $content_type
363          * @return void
364          * @throws InternalServerErrorException
365          * @deprecated since 2023.09 Use BaseModule->httpExit() instead
366          */
367         public static function httpExit(string $content, string $type = Response::TYPE_HTML, ?string $content_type = null)
368         {
369                 DI::apiResponse()->setType($type, $content_type);
370                 DI::apiResponse()->addContent($content);
371                 self::echoResponse(DI::apiResponse()->generate());
372
373                 self::exit();
374         }
375
376         /**
377          * @deprecated since 2023.09 Use BaseModule->jsonError instead
378          */
379         public static function jsonError($httpCode, $content, $content_type = 'application/json')
380         {
381                 if ($httpCode >= 400) {
382                         Logger::debug('Exit with error', ['code' => $httpCode, 'content_type' => $content_type, 'callstack' => System::callstack(20), 'method' => DI::args()->getMethod(), 'agent' => $_SERVER['HTTP_USER_AGENT'] ?? '']);
383                 }
384                 DI::apiResponse()->setStatus($httpCode);
385                 self::jsonExit($content, $content_type);
386         }
387
388         /**
389          * Encodes content to json.
390          *
391          * This function encodes an array to json format
392          * and adds an application/json HTTP header to the output.
393          * After finishing the process is getting killed.
394          *
395          * @param mixed   $content      The input content
396          * @param string  $content_type Type of the input (Default: 'application/json')
397          * @param integer $options      JSON options
398          * @throws InternalServerErrorException
399          * @deprecated since 2023.09 Use BaseModule->jsonExit instead
400          */
401         public static function jsonExit($content, string $content_type = 'application/json', int $options = JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT)
402         {
403                 self::httpExit(json_encode($content, $options), Response::TYPE_JSON, $content_type);
404         }
405
406         /**
407          * Exit the program execution.
408          */
409         public static function exit()
410         {
411                 DI::page()->logRuntime(DI::config(), 'exit');
412                 exit();
413         }
414
415         /**
416          * Generates a random string in the UUID format
417          *
418          * @param bool|string $prefix A given prefix (default is empty)
419          * @return string a generated UUID
420          * @throws \Exception
421          */
422         public static function createUUID($prefix = '')
423         {
424                 $guid = System::createGUID(32, $prefix);
425                 return substr($guid, 0, 8) . '-' . substr($guid, 8, 4) . '-' . substr($guid, 12, 4) . '-' . substr($guid, 16, 4) . '-' . substr($guid, 20, 12);
426         }
427
428         /**
429          * Generates a GUID with the given parameters
430          *
431          * @param int         $size   The size of the GUID (default is 16)
432          * @param bool|string $prefix A given prefix (default is empty)
433          * @return string a generated GUID
434          * @throws \Exception
435          */
436         public static function createGUID($size = 16, $prefix = '')
437         {
438                 if (is_bool($prefix) && !$prefix) {
439                         $prefix = '';
440                 } elseif (empty($prefix)) {
441                         $prefix = hash('crc32', DI::baseUrl()->getHost());
442                 }
443
444                 while (strlen($prefix) < ($size - 13)) {
445                         $prefix .= mt_rand();
446                 }
447
448                 if ($size >= 24) {
449                         $prefix = substr($prefix, 0, $size - 22);
450                         return str_replace('.', '', uniqid($prefix, true));
451                 } else {
452                         $prefix = substr($prefix, 0, max($size - 13, 0));
453                         return uniqid($prefix);
454                 }
455         }
456
457         /**
458          * Returns the current Load of the System
459          *
460          * @return integer
461          */
462         public static function currentLoad()
463         {
464                 if (!function_exists('sys_getloadavg')) {
465                         return false;
466                 }
467
468                 $load_arr = sys_getloadavg();
469
470                 if (!is_array($load_arr)) {
471                         return false;
472                 }
473
474                 return max($load_arr[0], $load_arr[1]);
475         }
476
477         /**
478          * Fetch the load and number of processes
479          *
480          * @param bool $get_processes
481          * @return array
482          */
483         public static function getLoadAvg(bool $get_processes = true): array
484         {
485                 $load_arr = sys_getloadavg();
486                 if (empty($load_arr)) {
487                         return [];
488                 }
489
490                 $load = [
491                         'average1'  => $load_arr[0],
492                         'average5'  => $load_arr[1],
493                         'average15' => $load_arr[2],
494                         'runnable'  => 0,
495                         'scheduled' => 0
496                 ];
497
498                 if ($get_processes && @is_readable('/proc/loadavg')) {
499                         $content = @file_get_contents('/proc/loadavg');
500                         if (!empty($content) && preg_match("#([.\d]+)\s([.\d]+)\s([.\d]+)\s(\d+)/(\d+)#", $content, $matches)) {
501                                 $load['runnable']  = (float)$matches[4];
502                                 $load['scheduled'] = (float)$matches[5];
503                         }
504                 }
505
506                 return $load;
507         }
508
509         /**
510          * Redirects to an external URL (fully qualified URL)
511          * If you want to route relative to the current Friendica base, use App->internalRedirect()
512          *
513          * @param string $url  The new Location to redirect
514          * @param int    $code The redirection code, which is used (Default is 302)
515          *
516          * @throws FoundException
517          * @throws MovedPermanentlyException
518          * @throws TemporaryRedirectException
519          *
520          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
521          */
522         public static function externalRedirect($url, $code = 302)
523         {
524                 if (empty(parse_url($url, PHP_URL_SCHEME))) {
525                         Logger::warning('No fully qualified URL provided', ['url' => $url, 'callstack' => self::callstack(20)]);
526                         DI::baseUrl()->redirect($url);
527                 }
528
529                 header("Location: $url");
530
531                 switch ($code) {
532                         case 302:
533                                 throw new FoundException();
534                         case 301:
535                                 throw new MovedPermanentlyException();
536                         case 307:
537                                 throw new TemporaryRedirectException();
538                 }
539                 self::exit();
540         }
541
542         /**
543          * Returns the system user that is executing the script
544          *
545          * This mostly returns something like "www-data".
546          *
547          * @return string system username
548          */
549         public static function getUser()
550         {
551                 if (!function_exists('posix_getpwuid') || !function_exists('posix_geteuid')) {
552                         return '';
553                 }
554
555                 $processUser = posix_getpwuid(posix_geteuid());
556                 return $processUser['name'];
557         }
558
559         /**
560          * Checks if a given directory is usable for the system
561          *
562          * @param      $directory
563          *
564          * @return boolean the directory is usable
565          */
566         private static function isDirectoryUsable(string $directory): bool
567         {
568                 if (empty($directory)) {
569                         Logger::warning('Directory is empty. This shouldn\'t happen.');
570                         return false;
571                 }
572
573                 if (!file_exists($directory)) {
574                         Logger::info('Path does not exist', ['directory' => $directory, 'user' => static::getUser()]);
575                         return false;
576                 }
577
578                 if (is_file($directory)) {
579                         Logger::warning('Path is a file', ['directory' => $directory, 'user' => static::getUser()]);
580                         return false;
581                 }
582
583                 if (!is_dir($directory)) {
584                         Logger::warning('Path is not a directory', ['directory' => $directory, 'user' => static::getUser()]);
585                         return false;
586                 }
587
588                 if (!is_writable($directory)) {
589                         Logger::warning('Path is not writable', ['directory' => $directory, 'user' => static::getUser()]);
590                         return false;
591                 }
592
593                 return true;
594         }
595
596         /**
597          * Exit method used by asynchronous update modules
598          *
599          * @param string $o
600          */
601         public static function htmlUpdateExit($o)
602         {
603                 DI::apiResponse()->setType(Response::TYPE_HTML);
604                 echo "<!DOCTYPE html><html><body>\r\n";
605                 // We can remove this hack once Internet Explorer recognises HTML5 natively
606                 echo "<section>";
607                 // reportedly some versions of MSIE don't handle tabs in XMLHttpRequest documents very well
608                 echo str_replace("\t", "       ", $o);
609                 echo "</section>";
610                 echo "</body></html>\r\n";
611                 self::exit();
612         }
613
614         /**
615          * Fetch the temp path of the system
616          *
617          * @return string Path for temp files
618          */
619         public static function getTempPath()
620         {
621                 $temppath = DI::config()->get("system", "temppath");
622
623                 if (($temppath != "") && self::isDirectoryUsable($temppath)) {
624                         // We have a temp path and it is usable
625                         return BasePath::getRealPath($temppath);
626                 }
627
628                 // We don't have a working preconfigured temp path, so we take the system path.
629                 $temppath = sys_get_temp_dir();
630
631                 // Check if it is usable
632                 if (($temppath != "") && self::isDirectoryUsable($temppath)) {
633                         // Always store the real path, not the path through symlinks
634                         $temppath = BasePath::getRealPath($temppath);
635
636                         // To avoid any interferences with other systems we create our own directory
637                         $new_temppath = $temppath . "/" . DI::baseUrl()->getHost();
638                         if (!is_dir($new_temppath)) {
639                                 /// @TODO There is a mkdir()+chmod() upwards, maybe generalize this (+ configurable) into a function/method?
640                                 @mkdir($new_temppath);
641                         }
642
643                         if (self::isDirectoryUsable($new_temppath)) {
644                                 // The new path is usable, we are happy
645                                 DI::config()->set("system", "temppath", $new_temppath);
646                                 return $new_temppath;
647                         } else {
648                                 // We can't create a subdirectory, strange.
649                                 // But the directory seems to work, so we use it but don't store it.
650                                 return $temppath;
651                         }
652                 }
653
654                 // Reaching this point means that the operating system is configured badly.
655                 return '';
656         }
657
658         /**
659          * Returns the path where spool files are stored
660          *
661          * @return string Spool path
662          */
663         public static function getSpoolPath()
664         {
665                 $spoolpath = DI::config()->get('system', 'spoolpath');
666                 if (($spoolpath != "") && self::isDirectoryUsable($spoolpath)) {
667                         // We have a spool path and it is usable
668                         return $spoolpath;
669                 }
670
671                 // We don't have a working preconfigured spool path, so we take the temp path.
672                 $temppath = self::getTempPath();
673
674                 if ($temppath != "") {
675                         // To avoid any interferences with other systems we create our own directory
676                         $spoolpath = $temppath . "/spool";
677                         if (!is_dir($spoolpath)) {
678                                 mkdir($spoolpath);
679                         }
680
681                         if (self::isDirectoryUsable($spoolpath)) {
682                                 // The new path is usable, we are happy
683                                 DI::config()->set("system", "spoolpath", $spoolpath);
684                                 return $spoolpath;
685                         } else {
686                                 // We can't create a subdirectory, strange.
687                                 // But the directory seems to work, so we use it but don't store it.
688                                 return $temppath;
689                         }
690                 }
691
692                 // Reaching this point means that the operating system is configured badly.
693                 return "";
694         }
695
696         /**
697          * Fetch the system rules
698          * @param bool $numeric_id If set to "true", the rules are returned with a numeric id as key.
699          *
700          * @return array
701          */
702         public static function getRules(bool $numeric_id = false): array
703         {
704                 $rules = [];
705                 $id    = 0;
706
707                 if (DI::config()->get('system', 'tosdisplay')) {
708                         $rulelist = DI::config()->get('system', 'tosrules') ?: DI::config()->get('system', 'tostext');
709                         $msg = BBCode::toPlaintext($rulelist, false);
710                         foreach (explode("\n", trim($msg)) as $line) {
711                                 $line = trim($line);
712                                 if ($line) {
713                                         if ($numeric_id) {
714                                                 $rules[++$id] = $line;
715                                         } else {
716                                                 $rules[] = ['id' => (string)++$id, 'text' => $line];
717                                         }
718                                 }
719                         }
720                 }
721
722                 return $rules;
723         }
724 }