3 * @file src/Core/System.php
5 namespace Friendica\Core;
7 use Friendica\BaseObject;
8 use Friendica\Core\Logger;
9 use Friendica\Core\Renderer;
10 use Friendica\Network\HTTPException\InternalServerErrorException;
11 use Friendica\Util\XML;
14 * @file include/Core/System.php
16 * @brief Contains the class with system relevant stuff
21 * @brief System methods
23 class System extends BaseObject
26 * @brief Retrieves the Friendica instance base URL
28 * @param bool $ssl Whether to append http or https under SSL_POLICY_SELFSIGN
29 * @return string Friendica server base URL
31 public static function baseUrl($ssl = false)
33 return self::getApp()->getBaseURL($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
43 public static function removedBaseUrl($orig_url)
45 return self::getApp()->removeBaseURL($orig_url);
49 * @brief Returns a string with a callstack. Can be used for logging.
50 * @param integer $depth optional, default 4
53 public static function callstack($depth = 4)
55 $trace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS);
57 // 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.
96 public static function xmlExit($st, $message = '')
98 $result = ['status' => $st];
100 if ($message != '') {
101 $result['message'] = $message;
105 Logger::log('xml_status returning non_zero: ' . $st . " message=" . $message);
108 header("Content-type: text/xml");
110 $xmldata = ["result" => $result];
112 echo XML::fromArray($xmldata, $xml);
118 * @brief Send HTTP status header and exit.
120 * @param integer $val HTTP status result value
121 * @param array $description optional message
122 * 'title' => header title
123 * 'description' => optional message
125 public static function httpExit($val, $description = [])
130 if (!isset($description["title"])) {
131 $description["title"] = $err." ".$val;
135 if ($val >= 200 && $val < 300) {
139 Logger::log('http_status_exit ' . $val);
140 header($_SERVER["SERVER_PROTOCOL"] . ' ' . $val . ' ' . $err);
142 if (isset($description["title"])) {
143 $tpl = Renderer::getMarkupTemplate('http_status.tpl');
144 echo Renderer::replaceMacros($tpl, ['$title' => $description["title"],
145 '$description' => defaults($description, 'description', '')]);
152 * @brief Encodes content to json.
154 * This function encodes an array to json format
155 * and adds an application/json HTTP header to the output.
156 * After finishing the process is getting killed.
158 * @param array $x The input content.
159 * @param string $content_type Type of the input (Default: 'application/json').
161 public static function jsonExit($x, $content_type = 'application/json') {
162 header("Content-type: $content_type");
163 echo json_encode($x);
168 * Generates a random string in the UUID format
170 * @param bool|string $prefix A given prefix (default is empty)
171 * @return string a generated UUID
173 public static function createUUID($prefix = '')
175 $guid = System::createGUID(32, $prefix);
176 return substr($guid, 0, 8). '-' . substr($guid, 8, 4) . '-' . substr($guid, 12, 4) . '-' . substr($guid, 16, 4) . '-' . substr($guid, 20, 12);
180 * Generates a GUID with the given parameters
182 * @param int $size The size of the GUID (default is 16)
183 * @param bool|string $prefix A given prefix (default is empty)
184 * @return string a generated GUID
186 public static function createGUID($size = 16, $prefix = '')
188 if (is_bool($prefix) && !$prefix) {
190 } elseif (empty($prefix)) {
191 $prefix = hash('crc32', self::getApp()->getHostName());
194 while (strlen($prefix) < ($size - 13)) {
195 $prefix .= mt_rand();
199 $prefix = substr($prefix, 0, $size - 22);
200 return str_replace('.', '', uniqid($prefix, true));
202 $prefix = substr($prefix, 0, max($size - 13, 0));
203 return uniqid($prefix);
208 * Generates a process identifier for the logging
210 * @param string $prefix A given prefix
212 * @return string a generated process identifier
214 public static function processID($prefix)
216 // We aren't calling any other function here.
217 // Doing so could easily create an endless loop
218 $trailer = $prefix . ':' . getmypid() . ':';
219 return substr($trailer . uniqid('') . mt_rand(), 0, 26);
223 * Returns the current Load of the System
227 public static function currentLoad()
229 if (!function_exists('sys_getloadavg')) {
233 $load_arr = sys_getloadavg();
235 if (!is_array($load_arr)) {
239 return max($load_arr[0], $load_arr[1]);
243 * Redirects to an external URL (fully qualified URL)
244 * If you want to route relative to the current Friendica base, use App->internalRedirect()
246 * @param string $url The new Location to redirect
247 * @throws InternalServerErrorException If the URL is not fully qualified
249 public static function externalRedirect($url)
251 if (!filter_var($url, FILTER_VALIDATE_URL)) {
252 throw new InternalServerErrorException("'$url' is not a fully qualified URL, please use App->internalRedirect() instead");
255 header("Location: $url");
259 /// @todo Move the following functions from boot.php
262 function local_user()
263 function public_contact()
264 function remote_user()
267 function is_site_admin()
268 function get_server()
269 function get_temppath()
270 function get_cachefile($file, $writemode = true)
271 function get_itemcachepath()
272 function get_spoolpath()