]> git.mxchange.org Git - friendica.git/blob - boot.php
Move Notify::TYPE_POKE
[friendica.git] / boot.php
1 <?php
2 /**
3  * @file boot.php
4  * This file defines some global constants and includes the central App class.
5  */
6
7 /**
8  * Friendica
9  *
10  * Friendica is a communications platform for integrated social communications
11  * utilising decentralised communications and linkage to several indie social
12  * projects - as well as popular mainstream providers.
13  *
14  * Our mission is to free our friends and families from the clutches of
15  * data-harvesting corporations, and pave the way to a future where social
16  * communications are free and open and flow between alternate providers as
17  * easily as email does today.
18  */
19
20 use Friendica\Core\Protocol;
21 use Friendica\Core\System;
22 use Friendica\Database\DBA;
23 use Friendica\DI;
24 use Friendica\Model\Contact;
25 use Friendica\Model\Term;
26 use Friendica\Util\BasePath;
27 use Friendica\Util\DateTimeFormat;
28
29 define('FRIENDICA_PLATFORM',     'Friendica');
30 define('FRIENDICA_CODENAME',     'Dalmatian Bellflower');
31 define('FRIENDICA_VERSION',      '2020.03-dev');
32 define('DFRN_PROTOCOL_VERSION',  '2.23');
33 define('NEW_UPDATE_ROUTINE_VERSION', 1170);
34
35 /**
36  * Constant with a HTML line break.
37  *
38  * Contains a HTML line break (br) element and a real carriage return with line
39  * feed for the source.
40  * This can be used in HTML and JavaScript where needed a line break.
41  */
42 define('EOL',                    "<br />\r\n");
43
44 /**
45  * Image storage quality.
46  *
47  * Lower numbers save space at cost of image detail.
48  * For ease of upgrade, please do not change here. Set system.jpegquality = n in config/local.config.php,
49  * where n is between 1 and 100, and with very poor results below about 50
50  */
51 define('JPEG_QUALITY',            100);
52
53 /**
54  * system.png_quality = n where is between 0 (uncompressed) to 9
55  */
56 define('PNG_QUALITY',             8);
57
58 /**
59  * An alternate way of limiting picture upload sizes. Specify the maximum pixel
60  * length that pictures are allowed to be (for non-square pictures, it will apply
61  * to the longest side). Pictures longer than this length will be resized to be
62  * this length (on the longest side, the other side will be scaled appropriately).
63  * Modify this value using
64  *
65  * 'system' => [
66  *      'max_image_length' => 'n',
67  *      ...
68  * ],
69  *
70  * in config/local.config.php
71  *
72  * If you don't want to set a maximum length, set to -1. The default value is
73  * defined by 'MAX_IMAGE_LENGTH' below.
74  */
75 define('MAX_IMAGE_LENGTH',        -1);
76
77 /**
78  * Not yet used
79  */
80 define('DEFAULT_DB_ENGINE',  'InnoDB');
81
82 /** @deprecated since version 2019.03, please use \Friendica\Module\Register::CLOSED instead */
83 define('REGISTER_CLOSED',        \Friendica\Module\Register::CLOSED);
84 /** @deprecated since version 2019.03, please use \Friendica\Module\Register::APPROVE instead */
85 define('REGISTER_APPROVE',       \Friendica\Module\Register::APPROVE);
86 /** @deprecated since version 2019.03, please use \Friendica\Module\Register::OPEN instead */
87 define('REGISTER_OPEN',          \Friendica\Module\Register::OPEN);
88
89 /**
90  * @name CP
91  *
92  * Type of the community page
93  * @{
94  */
95 define('CP_NO_INTERNAL_COMMUNITY', -2);
96 define('CP_NO_COMMUNITY_PAGE',     -1);
97 define('CP_USERS_ON_SERVER',        0);
98 define('CP_GLOBAL_COMMUNITY',       1);
99 define('CP_USERS_AND_GLOBAL',       2);
100 /**
101  * @}
102  */
103
104 /**
105  * These numbers are used in stored permissions
106  * and existing allocations MUST NEVER BE CHANGED
107  * OR RE-ASSIGNED! You may only add to them.
108  */
109 $netgroup_ids = [
110         Protocol::DFRN     => (-1),
111         Protocol::ZOT      => (-2),
112         Protocol::OSTATUS  => (-3),
113         Protocol::FEED     => (-4),
114         Protocol::DIASPORA => (-5),
115         Protocol::MAIL     => (-6),
116         Protocol::FACEBOOK => (-8),
117         Protocol::LINKEDIN => (-9),
118         Protocol::XMPP     => (-10),
119         Protocol::MYSPACE  => (-11),
120         Protocol::GPLUS    => (-12),
121         Protocol::PUMPIO   => (-13),
122         Protocol::TWITTER  => (-14),
123         Protocol::DIASPORA2 => (-15),
124         Protocol::STATUSNET => (-16),
125         Protocol::NEWS      => (-18),
126         Protocol::ICALENDAR => (-19),
127         Protocol::PNUT      => (-20),
128
129         Protocol::PHANTOM  => (-127),
130 ];
131
132 /**
133  * Maximum number of "people who like (or don't like) this"  that we will list by name
134  */
135 define('MAX_LIKERS',    75);
136
137 /**
138  * @name Notify
139  *
140  * Email notification options
141  * @{
142  */
143 define('NOTIFY_SHARE',     1024);
144
145 define('NOTIFY_SYSTEM',   32768);
146 /* @}*/
147
148
149 /** @deprecated since 2019.03, use Term::UNKNOWN instead */
150 define('TERM_UNKNOWN',   Term::UNKNOWN);
151 /** @deprecated since 2019.03, use Term::HASHTAG instead */
152 define('TERM_HASHTAG',   Term::HASHTAG);
153 /** @deprecated since 2019.03, use Term::MENTION instead */
154 define('TERM_MENTION',   Term::MENTION);
155 /** @deprecated since 2019.03, use Term::CATEGORY instead */
156 define('TERM_CATEGORY',  Term::CATEGORY);
157 /** @deprecated since 2019.03, use Term::PCATEGORY instead */
158 define('TERM_PCATEGORY', Term::PCATEGORY);
159 /** @deprecated since 2019.03, use Term::FILE instead */
160 define('TERM_FILE',      Term::FILE);
161 /** @deprecated since 2019.03, use Term::SAVEDSEARCH instead */
162 define('TERM_SAVEDSEARCH', Term::SAVEDSEARCH);
163 /** @deprecated since 2019.03, use Term::CONVERSATION instead */
164 define('TERM_CONVERSATION', Term::CONVERSATION);
165
166 /** @deprecated since 2019.03, use Term::OBJECT_TYPE_POST instead */
167 define('TERM_OBJ_POST',  Term::OBJECT_TYPE_POST);
168 /** @deprecated since 2019.03, use Term::OBJECT_TYPE_PHOTO instead */
169 define('TERM_OBJ_PHOTO', Term::OBJECT_TYPE_PHOTO);
170
171 /**
172  * @name Gravity
173  *
174  * Item weight for query ordering
175  * @{
176  */
177 define('GRAVITY_PARENT',       0);
178 define('GRAVITY_ACTIVITY',     3);
179 define('GRAVITY_COMMENT',      6);
180 define('GRAVITY_UNKNOWN',      9);
181 /* @}*/
182
183 /**
184  * @name Priority
185  *
186  * Process priority for the worker
187  * @{
188  */
189 define('PRIORITY_UNDEFINED',   0);
190 define('PRIORITY_CRITICAL',   10);
191 define('PRIORITY_HIGH',       20);
192 define('PRIORITY_MEDIUM',     30);
193 define('PRIORITY_LOW',        40);
194 define('PRIORITY_NEGLIGIBLE', 50);
195 /* @}*/
196
197 /**
198  * @name Social Relay settings
199  *
200  * See here: https://github.com/jaywink/social-relay
201  * and here: https://wiki.diasporafoundation.org/Relay_servers_for_public_posts
202  * @{
203  */
204 define('SR_SCOPE_NONE', '');
205 define('SR_SCOPE_ALL',  'all');
206 define('SR_SCOPE_TAGS', 'tags');
207 /* @}*/
208
209 // Normally this constant is defined - but not if "pcntl" isn't installed
210 if (!defined("SIGTERM")) {
211         define("SIGTERM", 15);
212 }
213
214 /**
215  * Depending on the PHP version this constant does exist - or not.
216  * See here: http://php.net/manual/en/curl.constants.php#117928
217  */
218 if (!defined('CURLE_OPERATION_TIMEDOUT')) {
219         define('CURLE_OPERATION_TIMEDOUT', CURLE_OPERATION_TIMEOUTED);
220 }
221
222 /**
223  * Returns the user id of locally logged in user or false.
224  *
225  * @return int|bool user id or false
226  */
227 function local_user()
228 {
229         if (!empty($_SESSION['authenticated']) && !empty($_SESSION['uid'])) {
230                 return intval($_SESSION['uid']);
231         }
232         return false;
233 }
234
235 /**
236  * Returns the public contact id of logged in user or false.
237  *
238  * @return int|bool public contact id or false
239  */
240 function public_contact()
241 {
242         static $public_contact_id = false;
243
244         if (!$public_contact_id && !empty($_SESSION['authenticated'])) {
245                 if (!empty($_SESSION['my_address'])) {
246                         // Local user
247                         $public_contact_id = intval(Contact::getIdForURL($_SESSION['my_address'], 0, true));
248                 } elseif (!empty($_SESSION['visitor_home'])) {
249                         // Remote user
250                         $public_contact_id = intval(Contact::getIdForURL($_SESSION['visitor_home'], 0, true));
251                 }
252         } elseif (empty($_SESSION['authenticated'])) {
253                 $public_contact_id = false;
254         }
255
256         return $public_contact_id;
257 }
258
259 /**
260  * Returns contact id of authenticated site visitor or false
261  *
262  * @return int|bool visitor_id or false
263  */
264 function remote_user()
265 {
266         if (empty($_SESSION['authenticated'])) {
267                 return false;
268         }
269
270         if (!empty($_SESSION['visitor_id'])) {
271                 return intval($_SESSION['visitor_id']);
272         }
273
274         return false;
275 }
276
277 /**
278  * Show an error message to user.
279  *
280  * This function save text in session, to be shown to the user at next page load
281  *
282  * @param string $s - Text of notice
283  */
284 function notice($s)
285 {
286         if (empty($_SESSION)) {
287                 return;
288         }
289
290         $a = DI::app();
291         if (empty($_SESSION['sysmsg'])) {
292                 $_SESSION['sysmsg'] = [];
293         }
294         if ($a->interactive) {
295                 $_SESSION['sysmsg'][] = $s;
296         }
297 }
298
299 /**
300  * Show an info message to user.
301  *
302  * This function save text in session, to be shown to the user at next page load
303  *
304  * @param string $s - Text of notice
305  */
306 function info($s)
307 {
308         $a = DI::app();
309
310         if (local_user() && DI::pConfig()->get(local_user(), 'system', 'ignore_info')) {
311                 return;
312         }
313
314         if (empty($_SESSION['sysmsg_info'])) {
315                 $_SESSION['sysmsg_info'] = [];
316         }
317         if ($a->interactive) {
318                 $_SESSION['sysmsg_info'][] = $s;
319         }
320 }
321
322 function feed_birthday($uid, $tz)
323 {
324         /**
325          * Determine the next birthday, but only if the birthday is published
326          * in the default profile. We _could_ also look for a private profile that the
327          * recipient can see, but somebody could get mad at us if they start getting
328          * public birthday greetings when they haven't made this info public.
329          *
330          * Assuming we are able to publish this info, we are then going to convert
331          * the start time from the owner's timezone to UTC.
332          *
333          * This will potentially solve the problem found with some social networks
334          * where birthdays are converted to the viewer's timezone and salutations from
335          * elsewhere in the world show up on the wrong day. We will convert it to the
336          * viewer's timezone also, but first we are going to convert it from the birthday
337          * person's timezone to GMT - so the viewer may find the birthday starting at
338          * 6:00PM the day before, but that will correspond to midnight to the birthday person.
339          */
340         $birthday = '';
341
342         if (!strlen($tz)) {
343                 $tz = 'UTC';
344         }
345
346         $profile = DBA::selectFirst('profile', ['dob'], ['uid' => $uid]);
347         if (DBA::isResult($profile)) {
348                 $tmp_dob = substr($profile['dob'], 5);
349                 if (intval($tmp_dob)) {
350                         $y = DateTimeFormat::timezoneNow($tz, 'Y');
351                         $bd = $y . '-' . $tmp_dob . ' 00:00';
352                         $t_dob = strtotime($bd);
353                         $now = strtotime(DateTimeFormat::timezoneNow($tz));
354                         if ($t_dob < $now) {
355                                 $bd = $y + 1 . '-' . $tmp_dob . ' 00:00';
356                         }
357                         $birthday = DateTimeFormat::convert($bd, 'UTC', $tz, DateTimeFormat::ATOM);
358                 }
359         }
360
361         return $birthday;
362 }
363
364 /**
365  * Check if current user has admin role.
366  *
367  * @return bool true if user is an admin
368  */
369 function is_site_admin()
370 {
371         $a = DI::app();
372
373         $admin_email = DI::config()->get('config', 'admin_email');
374
375         $adminlist = explode(',', str_replace(' ', '', $admin_email));
376
377         return local_user() && $admin_email && in_array($a->user['email'] ?? '', $adminlist);
378 }
379
380 function explode_querystring($query)
381 {
382         $arg_st = strpos($query, '?');
383         if ($arg_st !== false) {
384                 $base = substr($query, 0, $arg_st);
385                 $arg_st += 1;
386         } else {
387                 $base = '';
388                 $arg_st = 0;
389         }
390
391         $args = explode('&', substr($query, $arg_st));
392         foreach ($args as $k => $arg) {
393                 /// @TODO really compare type-safe here?
394                 if ($arg === '') {
395                         unset($args[$k]);
396                 }
397         }
398         $args = array_values($args);
399
400         if (!$base) {
401                 $base = $args[0];
402                 unset($args[0]);
403                 $args = array_values($args);
404         }
405
406         return [
407                 'base' => $base,
408                 'args' => $args,
409         ];
410 }
411
412 /**
413  * Returns the complete URL of the current page, e.g.: http(s)://something.com/network
414  *
415  * Taken from http://webcheatsheet.com/php/get_current_page_url.php
416  */
417 function curPageURL()
418 {
419         $pageURL = 'http';
420         if (!empty($_SERVER["HTTPS"]) && ($_SERVER["HTTPS"] == "on")) {
421                 $pageURL .= "s";
422         }
423
424         $pageURL .= "://";
425
426         if ($_SERVER["SERVER_PORT"] != "80" && $_SERVER["SERVER_PORT"] != "443") {
427                 $pageURL .= $_SERVER["SERVER_NAME"] . ":" . $_SERVER["SERVER_PORT"] . $_SERVER["REQUEST_URI"];
428         } else {
429                 $pageURL .= $_SERVER["SERVER_NAME"] . $_SERVER["REQUEST_URI"];
430         }
431         return $pageURL;
432 }
433
434 function get_temppath()
435 {
436         $temppath = DI::config()->get("system", "temppath");
437
438         if (($temppath != "") && System::isDirectoryUsable($temppath)) {
439                 // We have a temp path and it is usable
440                 return BasePath::getRealPath($temppath);
441         }
442
443         // We don't have a working preconfigured temp path, so we take the system path.
444         $temppath = sys_get_temp_dir();
445
446         // Check if it is usable
447         if (($temppath != "") && System::isDirectoryUsable($temppath)) {
448                 // Always store the real path, not the path through symlinks
449                 $temppath = BasePath::getRealPath($temppath);
450
451                 // To avoid any interferences with other systems we create our own directory
452                 $new_temppath = $temppath . "/" . DI::baseUrl()->getHostname();
453                 if (!is_dir($new_temppath)) {
454                         /// @TODO There is a mkdir()+chmod() upwards, maybe generalize this (+ configurable) into a function/method?
455                         mkdir($new_temppath);
456                 }
457
458                 if (System::isDirectoryUsable($new_temppath)) {
459                         // The new path is usable, we are happy
460                         DI::config()->set("system", "temppath", $new_temppath);
461                         return $new_temppath;
462                 } else {
463                         // We can't create a subdirectory, strange.
464                         // But the directory seems to work, so we use it but don't store it.
465                         return $temppath;
466                 }
467         }
468
469         // Reaching this point means that the operating system is configured badly.
470         return '';
471 }
472
473 function get_cachefile($file, $writemode = true)
474 {
475         $cache = get_itemcachepath();
476
477         if ((!$cache) || (!is_dir($cache))) {
478                 return "";
479         }
480
481         $subfolder = $cache . "/" . substr($file, 0, 2);
482
483         $cachepath = $subfolder . "/" . $file;
484
485         if ($writemode) {
486                 if (!is_dir($subfolder)) {
487                         mkdir($subfolder);
488                         chmod($subfolder, 0777);
489                 }
490         }
491
492         return $cachepath;
493 }
494
495 function clear_cache($basepath = "", $path = "")
496 {
497         if ($path == "") {
498                 $basepath = get_itemcachepath();
499                 $path = $basepath;
500         }
501
502         if (($path == "") || (!is_dir($path))) {
503                 return;
504         }
505
506         if (substr(realpath($path), 0, strlen($basepath)) != $basepath) {
507                 return;
508         }
509
510         $cachetime = (int) DI::config()->get('system', 'itemcache_duration');
511         if ($cachetime == 0) {
512                 $cachetime = 86400;
513         }
514
515         if (is_writable($path)) {
516                 if ($dh = opendir($path)) {
517                         while (($file = readdir($dh)) !== false) {
518                                 $fullpath = $path . "/" . $file;
519                                 if ((filetype($fullpath) == "dir") && ($file != ".") && ($file != "..")) {
520                                         clear_cache($basepath, $fullpath);
521                                 }
522                                 if ((filetype($fullpath) == "file") && (filectime($fullpath) < (time() - $cachetime))) {
523                                         unlink($fullpath);
524                                 }
525                         }
526                         closedir($dh);
527                 }
528         }
529 }
530
531 function get_itemcachepath()
532 {
533         // Checking, if the cache is deactivated
534         $cachetime = (int) DI::config()->get('system', 'itemcache_duration');
535         if ($cachetime < 0) {
536                 return "";
537         }
538
539         $itemcache = DI::config()->get('system', 'itemcache');
540         if (($itemcache != "") && System::isDirectoryUsable($itemcache)) {
541                 return BasePath::getRealPath($itemcache);
542         }
543
544         $temppath = get_temppath();
545
546         if ($temppath != "") {
547                 $itemcache = $temppath . "/itemcache";
548                 if (!file_exists($itemcache) && !is_dir($itemcache)) {
549                         mkdir($itemcache);
550                 }
551
552                 if (System::isDirectoryUsable($itemcache)) {
553                         DI::config()->set("system", "itemcache", $itemcache);
554                         return $itemcache;
555                 }
556         }
557         return "";
558 }
559
560 /**
561  * Returns the path where spool files are stored
562  *
563  * @return string Spool path
564  */
565 function get_spoolpath()
566 {
567         $spoolpath = DI::config()->get('system', 'spoolpath');
568         if (($spoolpath != "") && System::isDirectoryUsable($spoolpath)) {
569                 // We have a spool path and it is usable
570                 return $spoolpath;
571         }
572
573         // We don't have a working preconfigured spool path, so we take the temp path.
574         $temppath = get_temppath();
575
576         if ($temppath != "") {
577                 // To avoid any interferences with other systems we create our own directory
578                 $spoolpath = $temppath . "/spool";
579                 if (!is_dir($spoolpath)) {
580                         mkdir($spoolpath);
581                 }
582
583                 if (System::isDirectoryUsable($spoolpath)) {
584                         // The new path is usable, we are happy
585                         DI::config()->set("system", "spoolpath", $spoolpath);
586                         return $spoolpath;
587                 } else {
588                         // We can't create a subdirectory, strange.
589                         // But the directory seems to work, so we use it but don't store it.
590                         return $temppath;
591                 }
592         }
593
594         // Reaching this point means that the operating system is configured badly.
595         return "";
596 }
597
598 if (!function_exists('exif_imagetype')) {
599         function exif_imagetype($file)
600         {
601                 $size = getimagesize($file);
602                 return $size[2];
603         }
604 }
605
606 function validate_include(&$file)
607 {
608         $orig_file = $file;
609
610         $file = realpath($file);
611
612         if (strpos($file, getcwd()) !== 0) {
613                 return false;
614         }
615
616         $file = str_replace(getcwd() . "/", "", $file, $count);
617         if ($count != 1) {
618                 return false;
619         }
620
621         if ($orig_file !== $file) {
622                 return false;
623         }
624
625         $valid = false;
626         if (strpos($file, "include/") === 0) {
627                 $valid = true;
628         }
629
630         if (strpos($file, "addon/") === 0) {
631                 $valid = true;
632         }
633
634         // Simply return flag
635         return $valid;
636 }