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