3 * @copyright Copyright (C) 2020, Friendica
5 * @license GNU AGPL version 3 or any later version
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.
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.
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/>.
22 namespace Friendica\Core;
25 use Friendica\Network\HTTPException\InternalServerErrorException;
26 use Friendica\Util\XML;
29 * Contains the class with system relevant stuff
34 * Returns a string with a callstack. Can be used for logging.
36 * @param integer $depth How many calls to include in the stacks after filtering
37 * @param int $offset How many calls to shave off the top of the stack, for example if
38 * this is called from a centralized method that isn't relevant to the callstack
41 public static function callstack(int $depth = 4, int $offset = 0)
43 $trace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS);
45 // We remove at least the first two items from the list since they contain data that we don't need.
46 $trace = array_slice($trace, 2 + $offset);
49 $previous = ['class' => '', 'function' => '', 'database' => false];
51 // The ignore list contains all functions that are only wrapper functions
52 $ignore = ['call_user_func_array'];
54 while ($func = array_pop($trace)) {
55 if (!empty($func['class'])) {
56 // Don't show multiple calls from the Database classes to show the essential parts of the callstack
57 $func['database'] = in_array($func['class'], ['Friendica\Database\DBA', 'Friendica\Database\Database']);
58 if (!$previous['database'] || !$func['database']) {
59 $classparts = explode("\\", $func['class']);
60 $callstack[] = array_pop($classparts).'::'.$func['function'];
63 } elseif (!in_array($func['function'], $ignore)) {
64 $func['database'] = ($func['function'] == 'q');
65 $callstack[] = $func['function'];
72 while ((count($callstack2) < $depth) && (count($callstack) > 0)) {
73 $callstack2[] = array_pop($callstack);
76 return implode(', ', $callstack2);
81 * Outputs a basic dfrn XML status structure to STDOUT, with a <status> variable
82 * of $st and an optional text <message> of $message and terminates the current process.
85 * @param string $message
88 public static function xmlExit($st, $message = '')
90 $result = ['status' => $st];
93 $result['message'] = $message;
97 Logger::log('xml_status returning non_zero: ' . $st . " message=" . $message);
100 header("Content-type: text/xml");
102 $xmldata = ["result" => $result];
104 echo XML::fromArray($xmldata, $xml);
110 * Send HTTP status header and exit.
112 * @param integer $val HTTP status result value
113 * @param string $message Error message. Optional.
114 * @param string $content Response body. Optional.
117 public static function httpExit($val, $message = '', $content = '')
119 Logger::log('http_status_exit ' . $val);
120 header($_SERVER["SERVER_PROTOCOL"] . ' ' . $val . ' ' . $message);
127 public static function jsonError($httpCode, $data, $content_type = 'application/json')
129 header($_SERVER["SERVER_PROTOCOL"] . ' ' . $httpCode);
130 self::jsonExit($data, $content_type);
134 * Encodes content to json.
136 * This function encodes an array to json format
137 * and adds an application/json HTTP header to the output.
138 * After finishing the process is getting killed.
140 * @param mixed $x The input content.
141 * @param string $content_type Type of the input (Default: 'application/json').
142 * @param integer $options JSON options
144 public static function jsonExit($x, $content_type = 'application/json', int $options = 0) {
145 header("Content-type: $content_type");
146 echo json_encode($x, $options);
151 * Generates a random string in the UUID format
153 * @param bool|string $prefix A given prefix (default is empty)
154 * @return string a generated UUID
157 public static function createUUID($prefix = '')
159 $guid = System::createGUID(32, $prefix);
160 return substr($guid, 0, 8) . '-' . substr($guid, 8, 4) . '-' . substr($guid, 12, 4) . '-' . substr($guid, 16, 4) . '-' . substr($guid, 20, 12);
164 * Generates a GUID with the given parameters
166 * @param int $size The size of the GUID (default is 16)
167 * @param bool|string $prefix A given prefix (default is empty)
168 * @return string a generated GUID
171 public static function createGUID($size = 16, $prefix = '')
173 if (is_bool($prefix) && !$prefix) {
175 } elseif (empty($prefix)) {
176 $prefix = hash('crc32', DI::baseUrl()->getHostname());
179 while (strlen($prefix) < ($size - 13)) {
180 $prefix .= mt_rand();
184 $prefix = substr($prefix, 0, $size - 22);
185 return str_replace('.', '', uniqid($prefix, true));
187 $prefix = substr($prefix, 0, max($size - 13, 0));
188 return uniqid($prefix);
193 * Returns the current Load of the System
197 public static function currentLoad()
199 if (!function_exists('sys_getloadavg')) {
203 $load_arr = sys_getloadavg();
205 if (!is_array($load_arr)) {
209 return max($load_arr[0], $load_arr[1]);
213 * Redirects to an external URL (fully qualified URL)
214 * If you want to route relative to the current Friendica base, use App->internalRedirect()
216 * @param string $url The new Location to redirect
217 * @param int $code The redirection code, which is used (Default is 302)
219 * @throws InternalServerErrorException If the URL is not fully qualified
221 public static function externalRedirect($url, $code = 302)
223 if (empty(parse_url($url, PHP_URL_SCHEME))) {
224 throw new InternalServerErrorException("'$url' is not a fully qualified URL, please use App->internalRedirect() instead");
229 // this is the default code for a REDIRECT
230 // We don't need a extra header here
233 header('HTTP/1.1 301 Moved Permanently');
236 header('HTTP/1.1 307 Temporary Redirect');
240 header("Location: $url");
245 * Returns the system user that is executing the script
247 * This mostly returns something like "www-data".
249 * @return string system username
251 public static function getUser()
253 if (!function_exists('posix_getpwuid') || !function_exists('posix_geteuid')) {
257 $processUser = posix_getpwuid(posix_geteuid());
258 return $processUser['name'];
262 * Checks if a given directory is usable for the system
265 * @param bool $check_writable
267 * @return boolean the directory is usable
269 public static function isDirectoryUsable($directory, $check_writable = true)
271 if ($directory == '') {
272 Logger::log('Directory is empty. This shouldn\'t happen.', Logger::DEBUG);
276 if (!file_exists($directory)) {
277 Logger::log('Path "' . $directory . '" does not exist for user ' . static::getUser(), Logger::DEBUG);
281 if (is_file($directory)) {
282 Logger::log('Path "' . $directory . '" is a file for user ' . static::getUser(), Logger::DEBUG);
286 if (!is_dir($directory)) {
287 Logger::log('Path "' . $directory . '" is not a directory for user ' . static::getUser(), Logger::DEBUG);
291 if ($check_writable && !is_writable($directory)) {
292 Logger::log('Path "' . $directory . '" is not writable for user ' . static::getUser(), Logger::DEBUG);
300 * Exit method used by asynchronous update modules
304 public static function htmlUpdateExit($o)
306 header("Content-type: text/html");
307 echo "<!DOCTYPE html><html><body>\r\n";
308 // We can remove this hack once Internet Explorer recognises HTML5 natively
310 // reportedly some versions of MSIE don't handle tabs in XMLHttpRequest documents very well
311 echo str_replace("\t", " ", $o);
313 echo "</body></html>\r\n";
317 /// @todo Move the following functions from boot.php
319 function local_user()
320 function public_contact()
321 function remote_user()
324 function is_site_admin()
325 function get_temppath()
326 function get_cachefile($file, $writemode = true)
327 function get_itemcachepath()
328 function get_spoolpath()