]> git.mxchange.org Git - friendica.git/blob - src/Core/System.php
Don't show multiple calls from the "dba" class to show the essential parts of the...
[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()->get_baseurl($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()->remove_baseurl($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'] != '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                                 $previous = $func;
76                         }
77                 }
78
79                 $callstack2 = [];
80                 while ((count($callstack2) < $depth) && (count($callstack) > 0)) {
81                         $callstack2[] = array_pop($callstack);
82                 }
83
84                 return implode(', ', $callstack2);
85         }
86
87         /**
88          * @brief Called from db initialisation when db is dead.
89          */
90         static public function unavailable() {
91 echo <<< EOT
92 <html>
93         <head><title>System Unavailable</title></head>
94         <body>Apologies but this site is unavailable at the moment. Please try again later.</body>
95 </html>
96 EOT;
97
98                 killme();
99         }
100
101         /**
102          * Generic XML return
103          * Outputs a basic dfrn XML status structure to STDOUT, with a <status> variable
104          * of $st and an optional text <message> of $message and terminates the current process.
105          */
106         public static function xmlExit($st, $message = '')
107         {
108                 $result = ['status' => $st];
109
110                 if ($message != '') {
111                         $result['message'] = $message;
112                 }
113
114                 if ($st) {
115                         logger('xml_status returning non_zero: ' . $st . " message=" . $message);
116                 }
117
118                 header("Content-type: text/xml");
119
120                 $xmldata = ["result" => $result];
121
122                 echo XML::fromArray($xmldata, $xml);
123
124                 killme();
125         }
126
127         /**
128          * @brief Send HTTP status header and exit.
129          *
130          * @param integer $val         HTTP status result value
131          * @param array   $description optional message
132          *                             'title' => header title
133          *                             'description' => optional message
134          */
135         public static function httpExit($val, $description = [])
136         {
137                 $err = '';
138                 if ($val >= 400) {
139                         $err = 'Error';
140                         if (!isset($description["title"])) {
141                                 $description["title"] = $err." ".$val;
142                         }
143                 }
144
145                 if ($val >= 200 && $val < 300) {
146                         $err = 'OK';
147                 }
148
149                 logger('http_status_exit ' . $val);
150                 header($_SERVER["SERVER_PROTOCOL"] . ' ' . $val . ' ' . $err);
151
152                 if (isset($description["title"])) {
153                         $tpl = get_markup_template('http_status.tpl');
154                         echo replace_macros(
155                                 $tpl,
156                                 [
157                                         '$title' => $description["title"],
158                                         '$description' => $description["description"]]
159                         );
160                 }
161
162                 killme();
163         }
164
165         /**
166          * @brief Encodes content to json
167          *
168          * This function encodes an array to json format
169          * and adds an application/json HTTP header to the output.
170          * After finishing the process is getting killed.
171          *
172          * @param array $x The input content
173          */
174         public static function jsonExit($x)
175         {
176                 header("content-type: application/json");
177                 echo json_encode($x);
178                 killme();
179         }
180
181         /// @todo Move the following functions from boot.php
182         /*
183         function get_guid($size = 16, $prefix = "")
184         function killme()
185         function goaway($s)
186         function local_user()
187         function public_contact()
188         function remote_user()
189         function notice($s)
190         function info($s)
191         function is_site_admin()
192         function random_digits($digits)
193         function get_server()
194         function get_temppath()
195         function get_cachefile($file, $writemode = true)
196         function get_itemcachepath()
197         function get_spoolpath()
198         function current_load()
199         */
200 }