]> git.mxchange.org Git - friendica.git/blob - src/Core/System.php
Merge pull request #13704 from MrPetovan/bug/13693-infinite-indentation-level
[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]);
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          * @param array   $exclude 
234          * @return string
235          */
236         public static function callstack(int $depth = 4, int $offset = 0, bool $full = false, array $exclude = []): string
237         {
238                 $trace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS);
239
240                 // We remove at least the first two items from the list since they contain data that we don't need.
241                 $trace = array_slice($trace, 2 + $offset);
242
243                 $callstack = [];
244                 $previous = ['class' => '', 'function' => '', 'database' => false];
245
246                 // The ignore list contains all functions that are only wrapper functions
247                 $ignore = ['call_user_func_array'];
248
249                 while ($func = array_pop($trace)) {
250                         if (!empty($func['class'])) {
251                                 if (in_array($func['class'], $exclude)) {
252                                         continue;
253                                 }
254
255                                 if (!$full && in_array($previous['function'], ['insert', 'fetch', 'toArray', 'exists', 'count', 'selectFirst', 'selectToArray',
256                                         'select', 'update', 'delete', 'selectFirstForUser', 'selectForUser'])
257                                         && (substr($previous['class'], 0, 15) === 'Friendica\Model')) {
258                                         continue;
259                                 }
260
261                                 // Don't show multiple calls from the Database classes to show the essential parts of the callstack
262                                 $func['database'] = in_array($func['class'], ['Friendica\Database\DBA', 'Friendica\Database\Database']);
263                                 if ($full || !$previous['database'] || !$func['database']) {
264                                         $classparts = explode("\\", $func['class']);
265                                         $callstack[] = array_pop($classparts).'::'.$func['function'] . (isset($func['line']) ? ' (' . $func['line'] . ')' : '');
266                                         $previous = $func;
267                                 }
268                         } elseif (!in_array($func['function'], $ignore)) {
269                                 $func['database'] = ($func['function'] == 'q');
270                                 $callstack[] = $func['function'] . (isset($func['line']) ? ' (' . $func['line'] . ')' : '');
271                                 $func['class'] = '';
272                                 $previous = $func;
273                         }
274                 }
275
276                 $callstack2 = [];
277                 while ((count($callstack2) < $depth) && (count($callstack) > 0)) {
278                         $callstack2[] = array_pop($callstack);
279                 }
280
281                 return implode(', ', $callstack2);
282         }
283
284         /**
285          * Display current response, including setting all headers
286          *
287          * @param ResponseInterface $response
288          */
289         public static function echoResponse(ResponseInterface $response)
290         {
291                 header(sprintf("HTTP/%s %s %s",
292                                 $response->getProtocolVersion(),
293                                 $response->getStatusCode(),
294                                 $response->getReasonPhrase())
295                 );
296
297                 foreach ($response->getHeaders() as $key => $header) {
298                         if (is_array($header)) {
299                                 $header_str = implode(',', $header);
300                         } else {
301                                 $header_str = $header;
302                         }
303
304                         if (is_int($key)) {
305                                 header($header_str);
306                         } else {
307                                 header("$key: $header_str");
308                         }
309                 }
310
311                 echo $response->getBody();
312         }
313
314         /**
315          * Generic XML return
316          * Outputs a basic dfrn XML status structure to STDOUT, with a <status> variable
317          * of $st and an optional text <message> of $message and terminates the current process.
318          *
319          * @param mixed  $status
320          * @param string $message
321          * @throws \Exception
322          * @deprecated since 2023.09 Use BaseModule->httpExit() instead
323          */
324         public static function xmlExit($status, string $message = '')
325         {
326                 $result = ['status' => $status];
327
328                 if ($message != '') {
329                         $result['message'] = $message;
330                 }
331
332                 if ($status) {
333                         Logger::notice('xml_status returning non_zero: ' . $status . " message=" . $message);
334                 }
335
336                 self::httpExit(XML::fromArray(['result' => $result]), Response::TYPE_XML);
337         }
338
339         /**
340          * Send HTTP status header and exit.
341          *
342          * @param integer $val     HTTP status result value
343          * @param string  $message Error message. Optional.
344          * @param string  $content Response body. Optional.
345          * @throws \Exception
346          * @deprecated since 2023.09 Use BaseModule->httpError instead
347          */
348         public static function httpError($httpCode, $message = '', $content = '')
349         {
350                 if ($httpCode >= 400) {
351                         Logger::debug('Exit with error', ['code' => $httpCode, 'message' => $message, 'method' => DI::args()->getMethod(), 'agent' => $_SERVER['HTTP_USER_AGENT'] ?? '']);
352                 }
353                 DI::apiResponse()->setStatus($httpCode, $message);
354
355                 self::httpExit($content);
356         }
357
358         /**
359          * This function adds the content and a content-type HTTP header to the output.
360          * After finishing the process is getting killed.
361          *
362          * @param string      $content
363          * @param string      $type
364          * @param string|null $content_type
365          * @return void
366          * @throws InternalServerErrorException
367          * @deprecated since 2023.09 Use BaseModule->httpExit() instead
368          */
369         public static function httpExit(string $content, string $type = Response::TYPE_HTML, ?string $content_type = null)
370         {
371                 DI::apiResponse()->setType($type, $content_type);
372                 DI::apiResponse()->addContent($content);
373                 self::echoResponse(DI::apiResponse()->generate());
374
375                 self::exit();
376         }
377
378         /**
379          * @deprecated since 2023.09 Use BaseModule->jsonError instead
380          */
381         public static function jsonError($httpCode, $content, $content_type = 'application/json')
382         {
383                 if ($httpCode >= 400) {
384                         Logger::debug('Exit with error', ['code' => $httpCode, 'content_type' => $content_type, 'method' => DI::args()->getMethod(), 'agent' => $_SERVER['HTTP_USER_AGENT'] ?? '']);
385                 }
386                 DI::apiResponse()->setStatus($httpCode);
387                 self::jsonExit($content, $content_type);
388         }
389
390         /**
391          * Encodes content to json.
392          *
393          * This function encodes an array to json format
394          * and adds an application/json HTTP header to the output.
395          * After finishing the process is getting killed.
396          *
397          * @param mixed   $content      The input content
398          * @param string  $content_type Type of the input (Default: 'application/json')
399          * @param integer $options      JSON options
400          * @throws InternalServerErrorException
401          * @deprecated since 2023.09 Use BaseModule->jsonExit instead
402          */
403         public static function jsonExit($content, string $content_type = 'application/json', int $options = JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT)
404         {
405                 self::httpExit(json_encode($content, $options), Response::TYPE_JSON, $content_type);
406         }
407
408         /**
409          * Exit the program execution.
410          */
411         public static function exit()
412         {
413                 DI::page()->logRuntime(DI::config(), 'exit');
414                 exit();
415         }
416
417         /**
418          * Generates a random string in the UUID format
419          *
420          * @param bool|string $prefix A given prefix (default is empty)
421          * @return string a generated UUID
422          * @throws \Exception
423          */
424         public static function createUUID($prefix = '')
425         {
426                 $guid = System::createGUID(32, $prefix);
427                 return substr($guid, 0, 8) . '-' . substr($guid, 8, 4) . '-' . substr($guid, 12, 4) . '-' . substr($guid, 16, 4) . '-' . substr($guid, 20, 12);
428         }
429
430         /**
431          * Generates a GUID with the given parameters
432          *
433          * @param int         $size   The size of the GUID (default is 16)
434          * @param bool|string $prefix A given prefix (default is empty)
435          * @return string a generated GUID
436          * @throws \Exception
437          */
438         public static function createGUID($size = 16, $prefix = '')
439         {
440                 if (is_bool($prefix) && !$prefix) {
441                         $prefix = '';
442                 } elseif (empty($prefix)) {
443                         $prefix = hash('crc32', DI::baseUrl()->getHost());
444                 }
445
446                 while (strlen($prefix) < ($size - 13)) {
447                         $prefix .= mt_rand();
448                 }
449
450                 if ($size >= 24) {
451                         $prefix = substr($prefix, 0, $size - 22);
452                         return str_replace('.', '', uniqid($prefix, true));
453                 } else {
454                         $prefix = substr($prefix, 0, max($size - 13, 0));
455                         return uniqid($prefix);
456                 }
457         }
458
459         /**
460          * Returns the current Load of the System
461          *
462          * @return integer
463          */
464         public static function currentLoad()
465         {
466                 if (!function_exists('sys_getloadavg')) {
467                         return false;
468                 }
469
470                 $load_arr = sys_getloadavg();
471
472                 if (!is_array($load_arr)) {
473                         return false;
474                 }
475
476                 return max($load_arr[0], $load_arr[1]);
477         }
478
479         /**
480          * Fetch the load and number of processes
481          *
482          * @param bool $get_processes
483          * @return array
484          */
485         public static function getLoadAvg(bool $get_processes = true): array
486         {
487                 $load_arr = sys_getloadavg();
488                 if (empty($load_arr)) {
489                         return [];
490                 }
491
492                 $load = [
493                         'average1'  => $load_arr[0],
494                         'average5'  => $load_arr[1],
495                         'average15' => $load_arr[2],
496                         'runnable'  => 0,
497                         'scheduled' => 0
498                 ];
499
500                 if ($get_processes && @is_readable('/proc/loadavg')) {
501                         $content = @file_get_contents('/proc/loadavg');
502                         if (!empty($content) && preg_match("#([.\d]+)\s([.\d]+)\s([.\d]+)\s(\d+)/(\d+)#", $content, $matches)) {
503                                 $load['runnable']  = (float)$matches[4];
504                                 $load['scheduled'] = (float)$matches[5];
505                         }
506                 }
507
508                 return $load;
509         }
510
511         /**
512          * Redirects to an external URL (fully qualified URL)
513          * If you want to route relative to the current Friendica base, use App->internalRedirect()
514          *
515          * @param string $url  The new Location to redirect
516          * @param int    $code The redirection code, which is used (Default is 302)
517          *
518          * @throws FoundException
519          * @throws MovedPermanentlyException
520          * @throws TemporaryRedirectException
521          *
522          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
523          */
524         public static function externalRedirect($url, $code = 302)
525         {
526                 if (empty(parse_url($url, PHP_URL_SCHEME))) {
527                         Logger::warning('No fully qualified URL provided', ['url' => $url]);
528                         DI::baseUrl()->redirect($url);
529                 }
530
531                 header("Location: $url");
532
533                 switch ($code) {
534                         case 302:
535                                 throw new FoundException();
536                         case 301:
537                                 throw new MovedPermanentlyException();
538                         case 307:
539                                 throw new TemporaryRedirectException();
540                 }
541                 self::exit();
542         }
543
544         /**
545          * Returns the system user that is executing the script
546          *
547          * This mostly returns something like "www-data".
548          *
549          * @return string system username
550          */
551         public static function getUser()
552         {
553                 if (!function_exists('posix_getpwuid') || !function_exists('posix_geteuid')) {
554                         return '';
555                 }
556
557                 $processUser = posix_getpwuid(posix_geteuid());
558                 return $processUser['name'];
559         }
560
561         /**
562          * Checks if a given directory is usable for the system
563          *
564          * @param      $directory
565          *
566          * @return boolean the directory is usable
567          */
568         private static function isDirectoryUsable(string $directory): bool
569         {
570                 if (empty($directory)) {
571                         Logger::warning('Directory is empty. This shouldn\'t happen.');
572                         return false;
573                 }
574
575                 if (!file_exists($directory)) {
576                         Logger::info('Path does not exist', ['directory' => $directory, 'user' => static::getUser()]);
577                         return false;
578                 }
579
580                 if (is_file($directory)) {
581                         Logger::warning('Path is a file', ['directory' => $directory, 'user' => static::getUser()]);
582                         return false;
583                 }
584
585                 if (!is_dir($directory)) {
586                         Logger::warning('Path is not a directory', ['directory' => $directory, 'user' => static::getUser()]);
587                         return false;
588                 }
589
590                 if (!is_writable($directory)) {
591                         Logger::warning('Path is not writable', ['directory' => $directory, 'user' => static::getUser()]);
592                         return false;
593                 }
594
595                 return true;
596         }
597
598         /**
599          * Exit method used by asynchronous update modules
600          *
601          * @param string $o
602          */
603         public static function htmlUpdateExit($o)
604         {
605                 DI::apiResponse()->setType(Response::TYPE_HTML);
606                 echo "<!DOCTYPE html><html><body>\r\n";
607                 // We can remove this hack once Internet Explorer recognises HTML5 natively
608                 echo "<section>";
609                 // reportedly some versions of MSIE don't handle tabs in XMLHttpRequest documents very well
610                 echo str_replace("\t", "       ", $o);
611                 echo "</section>";
612                 echo "</body></html>\r\n";
613                 self::exit();
614         }
615
616         /**
617          * Fetch the temp path of the system
618          *
619          * @return string Path for temp files
620          */
621         public static function getTempPath()
622         {
623                 $temppath = DI::config()->get("system", "temppath");
624
625                 if (($temppath != "") && self::isDirectoryUsable($temppath)) {
626                         // We have a temp path and it is usable
627                         return BasePath::getRealPath($temppath);
628                 }
629
630                 // We don't have a working preconfigured temp path, so we take the system path.
631                 $temppath = sys_get_temp_dir();
632
633                 // Check if it is usable
634                 if (($temppath != "") && self::isDirectoryUsable($temppath)) {
635                         // Always store the real path, not the path through symlinks
636                         $temppath = BasePath::getRealPath($temppath);
637
638                         // To avoid any interferences with other systems we create our own directory
639                         $new_temppath = $temppath . "/" . DI::baseUrl()->getHost();
640                         if (!is_dir($new_temppath)) {
641                                 /// @TODO There is a mkdir()+chmod() upwards, maybe generalize this (+ configurable) into a function/method?
642                                 @mkdir($new_temppath);
643                         }
644
645                         if (self::isDirectoryUsable($new_temppath)) {
646                                 // The new path is usable, we are happy
647                                 DI::config()->set("system", "temppath", $new_temppath);
648                                 return $new_temppath;
649                         } else {
650                                 // We can't create a subdirectory, strange.
651                                 // But the directory seems to work, so we use it but don't store it.
652                                 return $temppath;
653                         }
654                 }
655
656                 // Reaching this point means that the operating system is configured badly.
657                 return '';
658         }
659
660         /**
661          * Returns the path where spool files are stored
662          *
663          * @return string Spool path
664          */
665         public static function getSpoolPath()
666         {
667                 $spoolpath = DI::config()->get('system', 'spoolpath');
668                 if (($spoolpath != "") && self::isDirectoryUsable($spoolpath)) {
669                         // We have a spool path and it is usable
670                         return $spoolpath;
671                 }
672
673                 // We don't have a working preconfigured spool path, so we take the temp path.
674                 $temppath = self::getTempPath();
675
676                 if ($temppath != "") {
677                         // To avoid any interferences with other systems we create our own directory
678                         $spoolpath = $temppath . "/spool";
679                         if (!is_dir($spoolpath)) {
680                                 mkdir($spoolpath);
681                         }
682
683                         if (self::isDirectoryUsable($spoolpath)) {
684                                 // The new path is usable, we are happy
685                                 DI::config()->set("system", "spoolpath", $spoolpath);
686                                 return $spoolpath;
687                         } else {
688                                 // We can't create a subdirectory, strange.
689                                 // But the directory seems to work, so we use it but don't store it.
690                                 return $temppath;
691                         }
692                 }
693
694                 // Reaching this point means that the operating system is configured badly.
695                 return "";
696         }
697
698         /**
699          * Fetch the system rules
700          * @param bool $numeric_id If set to "true", the rules are returned with a numeric id as key.
701          *
702          * @return array
703          */
704         public static function getRules(bool $numeric_id = false): array
705         {
706                 $rules = [];
707                 $id    = 0;
708
709                 if (DI::config()->get('system', 'tosdisplay')) {
710                         $rulelist = DI::config()->get('system', 'tosrules') ?: DI::config()->get('system', 'tostext');
711                         $msg = BBCode::toPlaintext($rulelist, false);
712                         foreach (explode("\n", trim($msg)) as $line) {
713                                 $line = trim($line);
714                                 if ($line) {
715                                         if ($numeric_id) {
716                                                 $rules[++$id] = $line;
717                                         } else {
718                                                 $rules[] = ['id' => (string)++$id, 'text' => $line];
719                                         }
720                                 }
721                         }
722                 }
723
724                 return $rules;
725         }
726 }