]> git.mxchange.org Git - friendica.git/blob - boot.php
pumped version to 2021.12-dev
[friendica.git] / boot.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2010-2021, the Friendica project
4  *
5  * @license GNU AGPL version 3 or any later version
6  *
7  * This program is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU Affero General Public License as
9  * published by the Free Software Foundation, either version 3 of the
10  * License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU Affero General Public License for more details.
16  *
17  * You should have received a copy of the GNU Affero General Public License
18  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
19  *
20  * Friendica is a communications platform for integrated social communications
21  * utilising decentralised communications and linkage to several indie social
22  * projects - as well as popular mainstream providers.
23  *
24  * Our mission is to free our friends and families from the clutches of
25  * data-harvesting corporations, and pave the way to a future where social
26  * communications are free and open and flow between alternate providers as
27  * easily as email does today.
28  */
29
30 use Friendica\Core\Protocol;
31 use Friendica\Core\System;
32 use Friendica\Database\DBA;
33 use Friendica\DI;
34 use Friendica\Model\Contact;
35 use Friendica\Model\Notification;
36 use Friendica\Util\BasePath;
37 use Friendica\Util\DateTimeFormat;
38
39 define('FRIENDICA_PLATFORM',     'Friendica');
40 define('FRIENDICA_CODENAME',     'Siberian Iris');
41 define('FRIENDICA_VERSION',      '2021.12-dev');
42 define('DFRN_PROTOCOL_VERSION',  '2.23');
43 define('NEW_TABLE_STRUCTURE_VERSION', 1288);
44
45 /**
46  * Constant with a HTML line break.
47  *
48  * Contains a HTML line break (br) element and a real carriage return with line
49  * feed for the source.
50  * This can be used in HTML and JavaScript where needed a line break.
51  */
52 define('EOL',                    "<br />\r\n");
53
54 /**
55  * Image storage quality.
56  *
57  * Lower numbers save space at cost of image detail.
58  * For ease of upgrade, please do not change here. Set system.jpegquality = n in config/local.config.php,
59  * where n is between 1 and 100, and with very poor results below about 50
60  */
61 define('JPEG_QUALITY',            100);
62
63 /**
64  * system.png_quality = n where is between 0 (uncompressed) to 9
65  */
66 define('PNG_QUALITY',             8);
67
68 /**
69  * An alternate way of limiting picture upload sizes. Specify the maximum pixel
70  * length that pictures are allowed to be (for non-square pictures, it will apply
71  * to the longest side). Pictures longer than this length will be resized to be
72  * this length (on the longest side, the other side will be scaled appropriately).
73  * Modify this value using
74  *
75  * 'system' => [
76  *      'max_image_length' => 'n',
77  *      ...
78  * ],
79  *
80  * in config/local.config.php
81  *
82  * If you don't want to set a maximum length, set to -1. The default value is
83  * defined by 'MAX_IMAGE_LENGTH' below.
84  */
85 define('MAX_IMAGE_LENGTH',        -1);
86
87 /**
88  * Not yet used
89  */
90 define('DEFAULT_DB_ENGINE',  'InnoDB');
91
92 /** @deprecated since version 2019.03, please use \Friendica\Module\Register::CLOSED instead */
93 define('REGISTER_CLOSED',        \Friendica\Module\Register::CLOSED);
94 /** @deprecated since version 2019.03, please use \Friendica\Module\Register::APPROVE instead */
95 define('REGISTER_APPROVE',       \Friendica\Module\Register::APPROVE);
96 /** @deprecated since version 2019.03, please use \Friendica\Module\Register::OPEN instead */
97 define('REGISTER_OPEN',          \Friendica\Module\Register::OPEN);
98
99 /**
100  * @name CP
101  *
102  * Type of the community page
103  * @{
104  */
105 define('CP_NO_INTERNAL_COMMUNITY', -2);
106 define('CP_NO_COMMUNITY_PAGE',     -1);
107 define('CP_USERS_ON_SERVER',        0);
108 define('CP_GLOBAL_COMMUNITY',       1);
109 define('CP_USERS_AND_GLOBAL',       2);
110 /**
111  * @}
112  */
113
114 /**
115  * These numbers are used in stored permissions
116  * and existing allocations MUST NEVER BE CHANGED
117  * OR RE-ASSIGNED! You may only add to them.
118  */
119 $netgroup_ids = [
120         Protocol::DFRN     => (-1),
121         Protocol::ZOT      => (-2),
122         Protocol::OSTATUS  => (-3),
123         Protocol::FEED     => (-4),
124         Protocol::DIASPORA => (-5),
125         Protocol::MAIL     => (-6),
126         Protocol::FACEBOOK => (-8),
127         Protocol::LINKEDIN => (-9),
128         Protocol::XMPP     => (-10),
129         Protocol::MYSPACE  => (-11),
130         Protocol::GPLUS    => (-12),
131         Protocol::PUMPIO   => (-13),
132         Protocol::TWITTER  => (-14),
133         Protocol::DIASPORA2 => (-15),
134         Protocol::STATUSNET => (-16),
135         Protocol::NEWS      => (-18),
136         Protocol::ICALENDAR => (-19),
137         Protocol::PNUT      => (-20),
138
139         Protocol::PHANTOM  => (-127),
140 ];
141
142 /**
143  * Maximum number of "people who like (or don't like) this"  that we will list by name
144  */
145 define('MAX_LIKERS',    75);
146
147 /**
148  * @name Notification
149  *
150  * Email notification options
151  * @{
152  */
153 /** @deprecated since 2020.03, use Notification\Type::INTRO instead */
154 define('NOTIFY_INTRO',        Notification\Type::INTRO);
155 /** @deprecated since 2020.03, use Notification\Type::CONFIRM instead */
156 define('NOTIFY_CONFIRM',      Notification\Type::CONFIRM);
157 /** @deprecated since 2020.03, use Notification\Type::WALL instead */
158 define('NOTIFY_WALL',         Notification\Type::WALL);
159 /** @deprecated since 2020.03, use Notification\Type::COMMENT instead */
160 define('NOTIFY_COMMENT',      Notification\Type::COMMENT);
161 /** @deprecated since 2020.03, use Notification\Type::MAIL instead */
162 define('NOTIFY_MAIL',        Notification\Type::MAIL);
163 /** @deprecated since 2020.03, use Notification\Type::SUGGEST instead */
164 define('NOTIFY_SUGGEST',     Notification\Type::SUGGEST);
165 /** @deprecated since 2020.03, use Notification\Type::PROFILE instead */
166 define('NOTIFY_PROFILE',     Notification\Type::PROFILE);
167 /** @deprecated since 2020.03, use Notification\Type::TAG_SELF instead */
168 define('NOTIFY_TAGSELF',     Notification\Type::TAG_SELF);
169 /** @deprecated since 2020.03, use Notification\Type::TAG_SHARE instead */
170 define('NOTIFY_TAGSHARE',    Notification\Type::TAG_SHARE);
171 /** @deprecated since 2020.03, use Notification\Type::POKE instead */
172 define('NOTIFY_POKE',        Notification\Type::POKE);
173 /** @deprecated since 2020.03, use Notification\Type::SHARE instead */
174 define('NOTIFY_SHARE',       Notification\Type::SHARE);
175
176 /** @deprecated since 2020.12, use Notification\Type::SYSTEM instead */
177 define('NOTIFY_SYSTEM',      Notification\Type::SYSTEM);
178 /* @}*/
179
180 /**
181  * @name Gravity
182  *
183  * Item weight for query ordering
184  * @{
185  */
186 define('GRAVITY_PARENT',       0);
187 define('GRAVITY_ACTIVITY',     3);
188 define('GRAVITY_COMMENT',      6);
189 define('GRAVITY_UNKNOWN',      9);
190 /* @}*/
191
192 /**
193  * @name Priority
194  *
195  * Process priority for the worker
196  * @{
197  */
198 define('PRIORITY_UNDEFINED',   0);
199 define('PRIORITY_CRITICAL',   10);
200 define('PRIORITY_HIGH',       20);
201 define('PRIORITY_MEDIUM',     30);
202 define('PRIORITY_LOW',        40);
203 define('PRIORITY_NEGLIGIBLE', 50);
204 define('PRIORITIES', [PRIORITY_CRITICAL, PRIORITY_HIGH, PRIORITY_MEDIUM, PRIORITY_LOW, PRIORITY_NEGLIGIBLE]);
205 /* @}*/
206
207 /**
208  * @name Social Relay settings
209  *
210  * See here: https://github.com/jaywink/social-relay
211  * and here: https://wiki.diasporafoundation.org/Relay_servers_for_public_posts
212  * @{
213  */
214 define('SR_SCOPE_NONE', '');
215 define('SR_SCOPE_ALL',  'all');
216 define('SR_SCOPE_TAGS', 'tags');
217 /* @}*/
218
219 // Normally this constant is defined - but not if "pcntl" isn't installed
220 if (!defined("SIGTERM")) {
221         define("SIGTERM", 15);
222 }
223
224 /**
225  * Depending on the PHP version this constant does exist - or not.
226  * See here: http://php.net/manual/en/curl.constants.php#117928
227  */
228 if (!defined('CURLE_OPERATION_TIMEDOUT')) {
229         define('CURLE_OPERATION_TIMEDOUT', CURLE_OPERATION_TIMEOUTED);
230 }
231
232 /**
233  * Returns the user id of locally logged in user or false.
234  *
235  * @return int|bool user id or false
236  */
237 function local_user()
238 {
239         if (!empty($_SESSION['authenticated']) && !empty($_SESSION['uid'])) {
240                 return intval($_SESSION['uid']);
241         }
242         return false;
243 }
244
245 /**
246  * Returns the public contact id of logged in user or false.
247  *
248  * @return int|bool public contact id or false
249  */
250 function public_contact()
251 {
252         static $public_contact_id = false;
253
254         if (!$public_contact_id && !empty($_SESSION['authenticated'])) {
255                 if (!empty($_SESSION['my_address'])) {
256                         // Local user
257                         $public_contact_id = intval(Contact::getIdForURL($_SESSION['my_address'], 0, false));
258                 } elseif (!empty($_SESSION['visitor_home'])) {
259                         // Remote user
260                         $public_contact_id = intval(Contact::getIdForURL($_SESSION['visitor_home'], 0, false));
261                 }
262         } elseif (empty($_SESSION['authenticated'])) {
263                 $public_contact_id = false;
264         }
265
266         return $public_contact_id;
267 }
268
269 /**
270  * Returns public contact id of authenticated site visitor or false
271  *
272  * @return int|bool visitor_id or false
273  */
274 function remote_user()
275 {
276         if (empty($_SESSION['authenticated'])) {
277                 return false;
278         }
279
280         if (!empty($_SESSION['visitor_id'])) {
281                 return intval($_SESSION['visitor_id']);
282         }
283
284         return false;
285 }
286
287 /**
288  * Show an error message to user.
289  *
290  * This function save text in session, to be shown to the user at next page load
291  *
292  * @param string $s - Text of notice
293  */
294 function notice($s)
295 {
296         if (empty($_SESSION)) {
297                 return;
298         }
299
300         if (empty($_SESSION['sysmsg'])) {
301                 $_SESSION['sysmsg'] = [];
302         }
303
304         $_SESSION['sysmsg'][] = $s;
305 }
306
307 /**
308  * Show an info message to user.
309  *
310  * This function save text in session, to be shown to the user at next page load
311  *
312  * @param string $s - Text of notice
313  */
314 function info($s)
315 {
316         if (empty($_SESSION)) {
317                 return;
318         }
319
320         if (empty($_SESSION['sysmsg_info'])) {
321                 $_SESSION['sysmsg_info'] = [];
322         }
323
324         $_SESSION['sysmsg_info'][] = $s;
325 }
326
327 function feed_birthday($uid, $tz)
328 {
329         /**
330          * Determine the next birthday, but only if the birthday is published
331          * in the default profile. We _could_ also look for a private profile that the
332          * recipient can see, but somebody could get mad at us if they start getting
333          * public birthday greetings when they haven't made this info public.
334          *
335          * Assuming we are able to publish this info, we are then going to convert
336          * the start time from the owner's timezone to UTC.
337          *
338          * This will potentially solve the problem found with some social networks
339          * where birthdays are converted to the viewer's timezone and salutations from
340          * elsewhere in the world show up on the wrong day. We will convert it to the
341          * viewer's timezone also, but first we are going to convert it from the birthday
342          * person's timezone to GMT - so the viewer may find the birthday starting at
343          * 6:00PM the day before, but that will correspond to midnight to the birthday person.
344          */
345         $birthday = '';
346
347         if (!strlen($tz)) {
348                 $tz = 'UTC';
349         }
350
351         $profile = DBA::selectFirst('profile', ['dob'], ['uid' => $uid]);
352         if (DBA::isResult($profile)) {
353                 $tmp_dob = substr($profile['dob'], 5);
354                 if (intval($tmp_dob)) {
355                         $y = DateTimeFormat::timezoneNow($tz, 'Y');
356                         $bd = $y . '-' . $tmp_dob . ' 00:00';
357                         $t_dob = strtotime($bd);
358                         $now = strtotime(DateTimeFormat::timezoneNow($tz));
359                         if ($t_dob < $now) {
360                                 $bd = $y + 1 . '-' . $tmp_dob . ' 00:00';
361                         }
362                         $birthday = DateTimeFormat::convert($bd, 'UTC', $tz, DateTimeFormat::ATOM);
363                 }
364         }
365
366         return $birthday;
367 }
368
369 /**
370  * Check if current user has admin role.
371  *
372  * @return bool true if user is an admin
373  */
374 function is_site_admin()
375 {
376         $a = DI::app();
377
378         $admin_email = DI::config()->get('config', 'admin_email');
379
380         $adminlist = explode(',', str_replace(' ', '', $admin_email));
381
382         return local_user() && $admin_email && DBA::exists('user', ['uid' => $a->getLoggedInUserId(), 'email' => $adminlist]);
383 }
384
385 /**
386  * Returns the complete URL of the current page, e.g.: http(s)://something.com/network
387  *
388  * Taken from http://webcheatsheet.com/php/get_current_page_url.php
389  */
390 function curPageURL()
391 {
392         $pageURL = 'http';
393         if (!empty($_SERVER["HTTPS"]) && ($_SERVER["HTTPS"] == "on")) {
394                 $pageURL .= "s";
395         }
396
397         $pageURL .= "://";
398
399         if ($_SERVER["SERVER_PORT"] != "80" && $_SERVER["SERVER_PORT"] != "443") {
400                 $pageURL .= $_SERVER["SERVER_NAME"] . ":" . $_SERVER["SERVER_PORT"] . $_SERVER["REQUEST_URI"];
401         } else {
402                 $pageURL .= $_SERVER["SERVER_NAME"] . $_SERVER["REQUEST_URI"];
403         }
404         return $pageURL;
405 }
406
407 function get_temppath()
408 {
409         $temppath = DI::config()->get("system", "temppath");
410
411         if (($temppath != "") && System::isDirectoryUsable($temppath)) {
412                 // We have a temp path and it is usable
413                 return BasePath::getRealPath($temppath);
414         }
415
416         // We don't have a working preconfigured temp path, so we take the system path.
417         $temppath = sys_get_temp_dir();
418
419         // Check if it is usable
420         if (($temppath != "") && System::isDirectoryUsable($temppath)) {
421                 // Always store the real path, not the path through symlinks
422                 $temppath = BasePath::getRealPath($temppath);
423
424                 // To avoid any interferences with other systems we create our own directory
425                 $new_temppath = $temppath . "/" . DI::baseUrl()->getHostname();
426                 if (!is_dir($new_temppath)) {
427                         /// @TODO There is a mkdir()+chmod() upwards, maybe generalize this (+ configurable) into a function/method?
428                         mkdir($new_temppath);
429                 }
430
431                 if (System::isDirectoryUsable($new_temppath)) {
432                         // The new path is usable, we are happy
433                         DI::config()->set("system", "temppath", $new_temppath);
434                         return $new_temppath;
435                 } else {
436                         // We can't create a subdirectory, strange.
437                         // But the directory seems to work, so we use it but don't store it.
438                         return $temppath;
439                 }
440         }
441
442         // Reaching this point means that the operating system is configured badly.
443         return '';
444 }
445
446 /**
447  * Returns the path where spool files are stored
448  *
449  * @return string Spool path
450  */
451 function get_spoolpath()
452 {
453         $spoolpath = DI::config()->get('system', 'spoolpath');
454         if (($spoolpath != "") && System::isDirectoryUsable($spoolpath)) {
455                 // We have a spool path and it is usable
456                 return $spoolpath;
457         }
458
459         // We don't have a working preconfigured spool path, so we take the temp path.
460         $temppath = get_temppath();
461
462         if ($temppath != "") {
463                 // To avoid any interferences with other systems we create our own directory
464                 $spoolpath = $temppath . "/spool";
465                 if (!is_dir($spoolpath)) {
466                         mkdir($spoolpath);
467                 }
468
469                 if (System::isDirectoryUsable($spoolpath)) {
470                         // The new path is usable, we are happy
471                         DI::config()->set("system", "spoolpath", $spoolpath);
472                         return $spoolpath;
473                 } else {
474                         // We can't create a subdirectory, strange.
475                         // But the directory seems to work, so we use it but don't store it.
476                         return $temppath;
477                 }
478         }
479
480         // Reaching this point means that the operating system is configured badly.
481         return "";
482 }
483
484 if (!function_exists('exif_imagetype')) {
485         function exif_imagetype($file)
486         {
487                 $size = getimagesize($file);
488                 return $size[2];
489         }
490 }
491
492 function validate_include(&$file)
493 {
494         $orig_file = $file;
495
496         $file = realpath($file);
497
498         if (strpos($file, getcwd()) !== 0) {
499                 return false;
500         }
501
502         $file = str_replace(getcwd() . "/", "", $file, $count);
503         if ($count != 1) {
504                 return false;
505         }
506
507         if ($orig_file !== $file) {
508                 return false;
509         }
510
511         $valid = false;
512         if (strpos($file, "include/") === 0) {
513                 $valid = true;
514         }
515
516         if (strpos($file, "addon/") === 0) {
517                 $valid = true;
518         }
519
520         // Simply return flag
521         return $valid;
522 }