]> git.mxchange.org Git - friendica.git/blob - src/Core/System.php
058aca130dfc78b4a1bdf7d1878cf44bd868f198
[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                                 $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          * @brief Called from db initialisation when db is dead.
90          */
91         static public function unavailable() {
92 echo <<< EOT
93 <html>
94         <head><title>System Unavailable</title></head>
95         <body>Apologies but this site is unavailable at the moment. Please try again later.</body>
96 </html>
97 EOT;
98
99                 killme();
100         }
101
102         /**
103          * Generic XML return
104          * Outputs a basic dfrn XML status structure to STDOUT, with a <status> variable
105          * of $st and an optional text <message> of $message and terminates the current process.
106          */
107         public static function xmlExit($st, $message = '')
108         {
109                 $result = ['status' => $st];
110
111                 if ($message != '') {
112                         $result['message'] = $message;
113                 }
114
115                 if ($st) {
116                         logger('xml_status returning non_zero: ' . $st . " message=" . $message);
117                 }
118
119                 header("Content-type: text/xml");
120
121                 $xmldata = ["result" => $result];
122
123                 echo XML::fromArray($xmldata, $xml);
124
125                 killme();
126         }
127
128         /**
129          * @brief Send HTTP status header and exit.
130          *
131          * @param integer $val         HTTP status result value
132          * @param array   $description optional message
133          *                             'title' => header title
134          *                             'description' => optional message
135          */
136         public static function httpExit($val, $description = [])
137         {
138                 $err = '';
139                 if ($val >= 400) {
140                         $err = 'Error';
141                         if (!isset($description["title"])) {
142                                 $description["title"] = $err." ".$val;
143                         }
144                 }
145
146                 if ($val >= 200 && $val < 300) {
147                         $err = 'OK';
148                 }
149
150                 logger('http_status_exit ' . $val);
151                 header($_SERVER["SERVER_PROTOCOL"] . ' ' . $val . ' ' . $err);
152
153                 if (isset($description["title"])) {
154                         $tpl = get_markup_template('http_status.tpl');
155                         echo replace_macros($tpl, ['$title' => $description["title"],
156                                 '$description' => defaults($description, 'description', '')]);
157                 }
158
159                 killme();
160         }
161
162         /**
163          * @brief Encodes content to json.
164          *
165          * This function encodes an array to json format
166          * and adds an application/json HTTP header to the output.
167          * After finishing the process is getting killed.
168          *
169          * @param array  $x The input content.
170          * @param string $content_type Type of the input (Default: 'application/json').
171          */
172         public static function jsonExit($x, $content_type = 'application/json') {
173                 header("Content-type: $content_type");
174                 echo json_encode($x);
175                 killme();
176         }
177
178         /**
179          * Generates a GUID with the given parameters
180          *
181          * @param int          $size     The size of the GUID (default is 16)
182          * @param bool|string  $prefix   A given prefix (default is empty)
183          * @return string a generated GUID
184          */
185         public static function createGUID($size = 16, $prefix = '')
186         {
187                 if (is_bool($prefix) && !$prefix) {
188                         $prefix = '';
189                 } elseif (!isset($prefix)) {
190                         $prefix = hash('crc32', self::getApp()->get_hostname());
191                 }
192
193                 while (strlen($prefix) < ($size - 13)) {
194                         $prefix .= mt_rand();
195                 }
196
197                 if ($size >= 24) {
198                         $prefix = substr($prefix, 0, $size - 22);
199                         return str_replace('.', '', uniqid($prefix, true));
200                 } else {
201                         $prefix = substr($prefix, 0, max($size - 13, 0));
202                         return uniqid($prefix);
203                 }
204         }
205
206         /// @todo Move the following functions from boot.php
207         /*
208         function killme()
209         function goaway($s)
210         function local_user()
211         function public_contact()
212         function remote_user()
213         function notice($s)
214         function info($s)
215         function is_site_admin()
216         function random_digits($digits)
217         function get_server()
218         function get_temppath()
219         function get_cachefile($file, $writemode = true)
220         function get_itemcachepath()
221         function get_spoolpath()
222         function current_load()
223         */
224 }