]> git.mxchange.org Git - friendica.git/blob - src/Core/System.php
Move index.php content to App->runFrontend
[friendica.git] / src / Core / System.php
1 <?php
2 /**
3  * @file src/Core/System.php
4  */
5 namespace Friendica\Core;
6
7 use Friendica\BaseObject;
8 use Friendica\Util\XML;
9
10 /**
11  * @file include/Core/System.php
12  *
13  * @brief Contains the class with system relevant stuff
14  */
15
16
17 /**
18  * @brief System methods
19  */
20 class System extends BaseObject
21 {
22         /**
23          * @brief Retrieves the Friendica instance base URL
24          *
25          * @param bool $ssl Whether to append http or https under SSL_POLICY_SELFSIGN
26          * @return string Friendica server base URL
27          */
28         public static function baseUrl($ssl = false)
29         {
30                 return self::getApp()->getBaseURL($ssl);
31         }
32
33         /**
34          * @brief Removes the baseurl from an url. This avoids some mixed content problems.
35          *
36          * @param string $orig_url The url to be cleaned
37          *
38          * @return string The cleaned url
39          */
40         public static function removedBaseUrl($orig_url)
41         {
42                 return self::getApp()->removeBaseURL($orig_url);
43         }
44
45         /**
46          * @brief Returns a string with a callstack. Can be used for logging.
47          * @param integer $depth optional, default 4
48          * @return string
49          */
50         public static function callstack($depth = 4)
51         {
52                 $trace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS);
53
54                 // We remove the first two items from the list since they contain data that we don't need.
55                 array_shift($trace);
56                 array_shift($trace);
57
58                 $callstack = [];
59                 $counter = 0;
60                 $previous = ['class' => '', 'function' => ''];
61
62                 // The ignore list contains all functions that are only wrapper functions
63                 $ignore = ['fetchUrl', 'call_user_func_array'];
64
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'];
71                                         $previous = $func;
72                                 }
73                         } elseif (!in_array($func['function'], $ignore)) {
74                                 $callstack[] = $func['function'];
75                                 $func['class'] = '';
76                                 $previous = $func;
77                         }
78                 }
79
80                 $callstack2 = [];
81                 while ((count($callstack2) < $depth) && (count($callstack) > 0)) {
82                         $callstack2[] = array_pop($callstack);
83                 }
84
85                 return implode(', ', $callstack2);
86         }
87
88         /**
89          * Generic XML return
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.
92          */
93         public static function xmlExit($st, $message = '')
94         {
95                 $result = ['status' => $st];
96
97                 if ($message != '') {
98                         $result['message'] = $message;
99                 }
100
101                 if ($st) {
102                         logger('xml_status returning non_zero: ' . $st . " message=" . $message);
103                 }
104
105                 header("Content-type: text/xml");
106
107                 $xmldata = ["result" => $result];
108
109                 echo XML::fromArray($xmldata, $xml);
110
111                 killme();
112         }
113
114         /**
115          * @brief Send HTTP status header and exit.
116          *
117          * @param integer $val         HTTP status result value
118          * @param array   $description optional message
119          *                             'title' => header title
120          *                             'description' => optional message
121          */
122         public static function httpExit($val, $description = [])
123         {
124                 $err = '';
125                 if ($val >= 400) {
126                         $err = 'Error';
127                         if (!isset($description["title"])) {
128                                 $description["title"] = $err." ".$val;
129                         }
130                 }
131
132                 if ($val >= 200 && $val < 300) {
133                         $err = 'OK';
134                 }
135
136                 logger('http_status_exit ' . $val);
137                 header($_SERVER["SERVER_PROTOCOL"] . ' ' . $val . ' ' . $err);
138
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', '')]);
143                 }
144
145                 exit();
146         }
147
148         /**
149          * @brief Encodes content to json.
150          *
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.
154          *
155          * @param array  $x The input content.
156          * @param string $content_type Type of the input (Default: 'application/json').
157          */
158         public static function jsonExit($x, $content_type = 'application/json') {
159                 header("Content-type: $content_type");
160                 echo json_encode($x);
161                 killme();
162         }
163
164         /**
165          * Generates a random string in the UUID format
166          *
167          * @param bool|string  $prefix   A given prefix (default is empty)
168          * @return string a generated UUID
169          */
170         public static function createUUID($prefix = '')
171         {
172                 $guid = System::createGUID(32, $prefix);
173                 return substr($guid, 0, 8). '-' . substr($guid, 8, 4) . '-' . substr($guid, 12, 4) . '-' . substr($guid, 16, 4) . '-' . substr($guid, 20, 12);
174         }
175
176         /**
177          * Generates a GUID with the given parameters
178          *
179          * @param int          $size     The size of the GUID (default is 16)
180          * @param bool|string  $prefix   A given prefix (default is empty)
181          * @return string a generated GUID
182          */
183         public static function createGUID($size = 16, $prefix = '')
184         {
185                 if (is_bool($prefix) && !$prefix) {
186                         $prefix = '';
187                 } elseif (empty($prefix)) {
188                         $prefix = hash('crc32', self::getApp()->getHostName());
189                 }
190
191                 while (strlen($prefix) < ($size - 13)) {
192                         $prefix .= mt_rand();
193                 }
194
195                 if ($size >= 24) {
196                         $prefix = substr($prefix, 0, $size - 22);
197                         return str_replace('.', '', uniqid($prefix, true));
198                 } else {
199                         $prefix = substr($prefix, 0, max($size - 13, 0));
200                         return uniqid($prefix);
201                 }
202         }
203
204         /**
205          * Generates a process identifier for the logging
206          *
207          * @param string $prefix A given prefix
208          *
209          * @return string a generated process identifier
210          */
211         public static function processID($prefix)
212         {
213                 // We aren't calling any other function here.
214                 // Doing so could easily create an endless loop
215                 $trailer = $prefix . ':' . getmypid() . ':';
216                 return substr($trailer . uniqid('') . mt_rand(), 0, 26);
217         }
218
219         /**
220          * Returns the current Load of the System
221          *
222          * @return integer
223          */
224         public static function currentLoad()
225         {
226                 if (!function_exists('sys_getloadavg')) {
227                         return false;
228                 }
229
230                 $load_arr = sys_getloadavg();
231
232                 if (!is_array($load_arr)) {
233                         return false;
234                 }
235
236                 return max($load_arr[0], $load_arr[1]);
237         }
238
239         /// @todo Move the following functions from boot.php
240         /*
241         function killme()
242         function goaway($s)
243         function local_user()
244         function public_contact()
245         function remote_user()
246         function notice($s)
247         function info($s)
248         function is_site_admin()
249         function random_digits($digits)
250         function get_server()
251         function get_temppath()
252         function get_cachefile($file, $writemode = true)
253         function get_itemcachepath()
254         function get_spoolpath()
255         */
256 }