3 * @file src/Core/System.php
5 namespace Friendica\Core;
7 use Friendica\App\BaseURL;
8 use Friendica\BaseObject;
9 use Friendica\Network\HTTPException\InternalServerErrorException;
10 use Friendica\Util\XML;
13 * @file include/Core/System.php
15 * @brief Contains the class with system relevant stuff
20 * @brief System methods
22 class System extends BaseObject
25 * @brief Retrieves the Friendica instance base URL
27 * @param bool $ssl Whether to append http or https under BaseURL::SSL_POLICY_SELFSIGN
28 * @return string Friendica server base URL
29 * @throws InternalServerErrorException
31 public static function baseUrl($ssl = false)
33 return self::getClass(BaseURL::class)->get($ssl);
37 * @brief Removes the baseurl from an url. This avoids some mixed content problems.
39 * @param string $orig_url The url to be cleaned
41 * @return string The cleaned url
44 public static function removedBaseUrl(string $orig_url)
46 return self::getApp()->removeBaseURL($orig_url);
50 * @brief Returns a string with a callstack. Can be used for logging.
51 * @param integer $depth optional, default 4
54 public static function callstack($depth = 4)
56 $trace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS);
58 // We remove the first two items from the list since they contain data that we don't need.
63 $previous = ['class' => '', 'function' => ''];
65 // The ignore list contains all functions that are only wrapper functions
66 $ignore = ['fetchUrl', 'call_user_func_array'];
68 while ($func = array_pop($trace)) {
69 if (!empty($func['class'])) {
70 // Don't show multiple calls from the "dba" class to show the essential parts of the callstack
71 if ((($previous['class'] != $func['class']) || ($func['class'] != 'Friendica\Database\DBA')) && ($previous['function'] != 'q')) {
72 $classparts = explode("\\", $func['class']);
73 $callstack[] = array_pop($classparts).'::'.$func['function'];
76 } elseif (!in_array($func['function'], $ignore)) {
77 $callstack[] = $func['function'];
84 while ((count($callstack2) < $depth) && (count($callstack) > 0)) {
85 $callstack2[] = array_pop($callstack);
88 return implode(', ', $callstack2);
93 * Outputs a basic dfrn XML status structure to STDOUT, with a <status> variable
94 * of $st and an optional text <message> of $message and terminates the current process.
97 * @param string $message
100 public static function xmlExit($st, $message = '')
102 $result = ['status' => $st];
104 if ($message != '') {
105 $result['message'] = $message;
109 Logger::log('xml_status returning non_zero: ' . $st . " message=" . $message);
112 header("Content-type: text/xml");
114 $xmldata = ["result" => $result];
116 echo XML::fromArray($xmldata, $xml);
122 * @brief Send HTTP status header and exit.
124 * @param integer $val HTTP status result value
125 * @param string $message Error message. Optional.
126 * @param string $content Response body. Optional.
129 public static function httpExit($val, $message = '', $content = '')
131 Logger::log('http_status_exit ' . $val);
132 header($_SERVER["SERVER_PROTOCOL"] . ' ' . $val . ' ' . $message);
139 public static function jsonError($httpCode, $data, $content_type = 'application/json')
141 header($_SERVER["SERVER_PROTOCOL"] . ' ' . $httpCode);
142 self::jsonExit($data, $content_type);
146 * @brief Encodes content to json.
148 * This function encodes an array to json format
149 * and adds an application/json HTTP header to the output.
150 * After finishing the process is getting killed.
152 * @param mixed $x The input content.
153 * @param string $content_type Type of the input (Default: 'application/json').
155 public static function jsonExit($x, $content_type = 'application/json') {
156 header("Content-type: $content_type");
157 echo json_encode($x);
162 * Generates a random string in the UUID format
164 * @param bool|string $prefix A given prefix (default is empty)
165 * @return string a generated UUID
168 public static function createUUID($prefix = '')
170 $guid = System::createGUID(32, $prefix);
171 return substr($guid, 0, 8) . '-' . substr($guid, 8, 4) . '-' . substr($guid, 12, 4) . '-' . substr($guid, 16, 4) . '-' . substr($guid, 20, 12);
175 * Generates a GUID with the given parameters
177 * @param int $size The size of the GUID (default is 16)
178 * @param bool|string $prefix A given prefix (default is empty)
179 * @return string a generated GUID
182 public static function createGUID($size = 16, $prefix = '')
184 if (is_bool($prefix) && !$prefix) {
186 } elseif (empty($prefix)) {
187 $prefix = hash('crc32', self::getApp()->getHostName());
190 while (strlen($prefix) < ($size - 13)) {
191 $prefix .= mt_rand();
195 $prefix = substr($prefix, 0, $size - 22);
196 return str_replace('.', '', uniqid($prefix, true));
198 $prefix = substr($prefix, 0, max($size - 13, 0));
199 return uniqid($prefix);
204 * Returns the current Load of the System
208 public static function currentLoad()
210 if (!function_exists('sys_getloadavg')) {
214 $load_arr = sys_getloadavg();
216 if (!is_array($load_arr)) {
220 return max($load_arr[0], $load_arr[1]);
224 * Redirects to an external URL (fully qualified URL)
225 * If you want to route relative to the current Friendica base, use App->internalRedirect()
227 * @param string $url The new Location to redirect
228 * @param int $code The redirection code, which is used (Default is 302)
230 * @throws InternalServerErrorException If the URL is not fully qualified
232 public static function externalRedirect($url, $code = 302)
234 if (empty(parse_url($url, PHP_URL_SCHEME))) {
235 throw new InternalServerErrorException("'$url' is not a fully qualified URL, please use App->internalRedirect() instead");
240 // this is the default code for a REDIRECT
241 // We don't need a extra header here
244 header('HTTP/1.1 301 Moved Permanently');
247 header('HTTP/1.1 307 Temporary Redirect');
251 header("Location: $url");
256 * @brief Returns the system user that is executing the script
258 * This mostly returns something like "www-data".
260 * @return string system username
262 public static function getUser()
264 if (!function_exists('posix_getpwuid') || !function_exists('posix_geteuid')) {
268 $processUser = posix_getpwuid(posix_geteuid());
269 return $processUser['name'];
273 * @brief Checks if a given directory is usable for the system
276 * @param bool $check_writable
278 * @return boolean the directory is usable
280 public static function isDirectoryUsable($directory, $check_writable = true)
282 if ($directory == '') {
283 Logger::log('Directory is empty. This shouldn\'t happen.', Logger::DEBUG);
287 if (!file_exists($directory)) {
288 Logger::log('Path "' . $directory . '" does not exist for user ' . static::getUser(), Logger::DEBUG);
292 if (is_file($directory)) {
293 Logger::log('Path "' . $directory . '" is a file for user ' . static::getUser(), Logger::DEBUG);
297 if (!is_dir($directory)) {
298 Logger::log('Path "' . $directory . '" is not a directory for user ' . static::getUser(), Logger::DEBUG);
302 if ($check_writable && !is_writable($directory)) {
303 Logger::log('Path "' . $directory . '" is not writable for user ' . static::getUser(), Logger::DEBUG);
310 /// @todo Move the following functions from boot.php
313 function local_user()
314 function public_contact()
315 function remote_user()
318 function is_site_admin()
319 function get_server()
320 function get_temppath()
321 function get_cachefile($file, $writemode = true)
322 function get_itemcachepath()
323 function get_spoolpath()