3 * @file src/Core/System.php
5 namespace Friendica\Core;
7 use Friendica\BaseObject;
8 use Friendica\Util\XML;
11 * @file include/Core/System.php
13 * @brief Contains the class with system relevant stuff
18 * @brief System methods
20 class System extends BaseObject
23 * @brief Retrieves the Friendica instance base URL
25 * @param bool $ssl Whether to append http or https under SSL_POLICY_SELFSIGN
26 * @return string Friendica server base URL
28 public static function baseUrl($ssl = false)
30 return self::getApp()->get_baseurl($ssl);
34 * @brief Removes the baseurl from an url. This avoids some mixed content problems.
36 * @param string $orig_url The url to be cleaned
38 * @return string The cleaned url
40 public static function removedBaseUrl($orig_url)
42 return self::getApp()->remove_baseurl($orig_url);
46 * @brief Returns a string with a callstack. Can be used for logging.
47 * @param integer $depth optional, default 4
50 public static function callstack($depth = 4)
52 $trace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS);
54 // We remove the first two items from the list since they contain data that we don't need.
60 $previous = ['class' => '', 'function' => ''];
62 // The ignore list contains all functions that are only wrapper functions
63 $ignore = ['fetchUrl', 'call_user_func_array'];
65 while ($func = array_pop($trace)) {
66 if (!empty($func['class'])) {
67 // Don't show multiple calls from the "dba" class to show the essential parts of the callstack
68 if ((($previous['class'] != $func['class']) || ($func['class'] != 'Friendica\Database\DBA')) && ($previous['function'] != 'q')) {
69 $classparts = explode("\\", $func['class']);
70 $callstack[] = array_pop($classparts).'::'.$func['function'];
73 } elseif (!in_array($func['function'], $ignore)) {
74 $callstack[] = $func['function'];
81 while ((count($callstack2) < $depth) && (count($callstack) > 0)) {
82 $callstack2[] = array_pop($callstack);
85 return implode(', ', $callstack2);
90 * Outputs a basic dfrn XML status structure to STDOUT, with a <status> variable
91 * of $st and an optional text <message> of $message and terminates the current process.
93 public static function xmlExit($st, $message = '')
95 $result = ['status' => $st];
98 $result['message'] = $message;
102 logger('xml_status returning non_zero: ' . $st . " message=" . $message);
105 header("Content-type: text/xml");
107 $xmldata = ["result" => $result];
109 echo XML::fromArray($xmldata, $xml);
115 * @brief Send HTTP status header and exit.
117 * @param integer $val HTTP status result value
118 * @param array $description optional message
119 * 'title' => header title
120 * 'description' => optional message
122 public static function httpExit($val, $description = [])
127 if (!isset($description["title"])) {
128 $description["title"] = $err." ".$val;
132 if ($val >= 200 && $val < 300) {
136 logger('http_status_exit ' . $val);
137 header($_SERVER["SERVER_PROTOCOL"] . ' ' . $val . ' ' . $err);
139 if (isset($description["title"])) {
140 $tpl = get_markup_template('http_status.tpl');
141 echo replace_macros($tpl, ['$title' => $description["title"],
142 '$description' => defaults($description, 'description', '')]);
149 * @brief Encodes content to json.
151 * This function encodes an array to json format
152 * and adds an application/json HTTP header to the output.
153 * After finishing the process is getting killed.
155 * @param array $x The input content.
156 * @param string $content_type Type of the input (Default: 'application/json').
158 public static function jsonExit($x, $content_type = 'application/json') {
159 header("Content-type: $content_type");
160 echo json_encode($x);
165 * Generates a GUID with the given parameters
167 * @param int $size The size of the GUID (default is 16)
168 * @param bool|string $prefix A given prefix (default is empty)
169 * @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', self::getApp()->get_hostname());
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 * Generates a process identifier for the logging
195 * @param string $prefix A given prefix
197 * @return string a generated process identifier
199 public static function processID($prefix)
201 // We aren't calling any other function here.
202 // Doing so could easily create an endless loop
203 $trailer = $prefix . ':' . getmypid() . ':';
204 return substr($trailer . uniqid('') . mt_rand(), 0, 26);
207 /// @todo Move the following functions from boot.php
211 function local_user()
212 function public_contact()
213 function remote_user()
216 function is_site_admin()
217 function random_digits($digits)
218 function get_server()
219 function get_temppath()
220 function get_cachefile($file, $writemode = true)
221 function get_itemcachepath()
222 function get_spoolpath()
223 function current_load()