]> git.mxchange.org Git - friendica.git/blob - boot.php
Move Notify::TYPE_WALL
[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_COMMENT',      8);
144 define('NOTIFY_MAIL',        16);
145 define('NOTIFY_SUGGEST',     32);
146 define('NOTIFY_PROFILE',     64);
147 define('NOTIFY_TAGSELF',    128);
148 define('NOTIFY_TAGSHARE',   256);
149 define('NOTIFY_POKE',       512);
150 define('NOTIFY_SHARE',     1024);
151
152 define('NOTIFY_SYSTEM',   32768);
153 /* @}*/
154
155
156 /** @deprecated since 2019.03, use Term::UNKNOWN instead */
157 define('TERM_UNKNOWN',   Term::UNKNOWN);
158 /** @deprecated since 2019.03, use Term::HASHTAG instead */
159 define('TERM_HASHTAG',   Term::HASHTAG);
160 /** @deprecated since 2019.03, use Term::MENTION instead */
161 define('TERM_MENTION',   Term::MENTION);
162 /** @deprecated since 2019.03, use Term::CATEGORY instead */
163 define('TERM_CATEGORY',  Term::CATEGORY);
164 /** @deprecated since 2019.03, use Term::PCATEGORY instead */
165 define('TERM_PCATEGORY', Term::PCATEGORY);
166 /** @deprecated since 2019.03, use Term::FILE instead */
167 define('TERM_FILE',      Term::FILE);
168 /** @deprecated since 2019.03, use Term::SAVEDSEARCH instead */
169 define('TERM_SAVEDSEARCH', Term::SAVEDSEARCH);
170 /** @deprecated since 2019.03, use Term::CONVERSATION instead */
171 define('TERM_CONVERSATION', Term::CONVERSATION);
172
173 /** @deprecated since 2019.03, use Term::OBJECT_TYPE_POST instead */
174 define('TERM_OBJ_POST',  Term::OBJECT_TYPE_POST);
175 /** @deprecated since 2019.03, use Term::OBJECT_TYPE_PHOTO instead */
176 define('TERM_OBJ_PHOTO', Term::OBJECT_TYPE_PHOTO);
177
178 /**
179  * @name Gravity
180  *
181  * Item weight for query ordering
182  * @{
183  */
184 define('GRAVITY_PARENT',       0);
185 define('GRAVITY_ACTIVITY',     3);
186 define('GRAVITY_COMMENT',      6);
187 define('GRAVITY_UNKNOWN',      9);
188 /* @}*/
189
190 /**
191  * @name Priority
192  *
193  * Process priority for the worker
194  * @{
195  */
196 define('PRIORITY_UNDEFINED',   0);
197 define('PRIORITY_CRITICAL',   10);
198 define('PRIORITY_HIGH',       20);
199 define('PRIORITY_MEDIUM',     30);
200 define('PRIORITY_LOW',        40);
201 define('PRIORITY_NEGLIGIBLE', 50);
202 /* @}*/
203
204 /**
205  * @name Social Relay settings
206  *
207  * See here: https://github.com/jaywink/social-relay
208  * and here: https://wiki.diasporafoundation.org/Relay_servers_for_public_posts
209  * @{
210  */
211 define('SR_SCOPE_NONE', '');
212 define('SR_SCOPE_ALL',  'all');
213 define('SR_SCOPE_TAGS', 'tags');
214 /* @}*/
215
216 // Normally this constant is defined - but not if "pcntl" isn't installed
217 if (!defined("SIGTERM")) {
218         define("SIGTERM", 15);
219 }
220
221 /**
222  * Depending on the PHP version this constant does exist - or not.
223  * See here: http://php.net/manual/en/curl.constants.php#117928
224  */
225 if (!defined('CURLE_OPERATION_TIMEDOUT')) {
226         define('CURLE_OPERATION_TIMEDOUT', CURLE_OPERATION_TIMEOUTED);
227 }
228
229 /**
230  * Returns the user id of locally logged in user or false.
231  *
232  * @return int|bool user id or false
233  */
234 function local_user()
235 {
236         if (!empty($_SESSION['authenticated']) && !empty($_SESSION['uid'])) {
237                 return intval($_SESSION['uid']);
238         }
239         return false;
240 }
241
242 /**
243  * Returns the public contact id of logged in user or false.
244  *
245  * @return int|bool public contact id or false
246  */
247 function public_contact()
248 {
249         static $public_contact_id = false;
250
251         if (!$public_contact_id && !empty($_SESSION['authenticated'])) {
252                 if (!empty($_SESSION['my_address'])) {
253                         // Local user
254                         $public_contact_id = intval(Contact::getIdForURL($_SESSION['my_address'], 0, true));
255                 } elseif (!empty($_SESSION['visitor_home'])) {
256                         // Remote user
257                         $public_contact_id = intval(Contact::getIdForURL($_SESSION['visitor_home'], 0, true));
258                 }
259         } elseif (empty($_SESSION['authenticated'])) {
260                 $public_contact_id = false;
261         }
262
263         return $public_contact_id;
264 }
265
266 /**
267  * Returns contact id of authenticated site visitor or false
268  *
269  * @return int|bool visitor_id or false
270  */
271 function remote_user()
272 {
273         if (empty($_SESSION['authenticated'])) {
274                 return false;
275         }
276
277         if (!empty($_SESSION['visitor_id'])) {
278                 return intval($_SESSION['visitor_id']);
279         }
280
281         return false;
282 }
283
284 /**
285  * Show an error message to user.
286  *
287  * This function save text in session, to be shown to the user at next page load
288  *
289  * @param string $s - Text of notice
290  */
291 function notice($s)
292 {
293         if (empty($_SESSION)) {
294                 return;
295         }
296
297         $a = DI::app();
298         if (empty($_SESSION['sysmsg'])) {
299                 $_SESSION['sysmsg'] = [];
300         }
301         if ($a->interactive) {
302                 $_SESSION['sysmsg'][] = $s;
303         }
304 }
305
306 /**
307  * Show an info message to user.
308  *
309  * This function save text in session, to be shown to the user at next page load
310  *
311  * @param string $s - Text of notice
312  */
313 function info($s)
314 {
315         $a = DI::app();
316
317         if (local_user() && DI::pConfig()->get(local_user(), 'system', 'ignore_info')) {
318                 return;
319         }
320
321         if (empty($_SESSION['sysmsg_info'])) {
322                 $_SESSION['sysmsg_info'] = [];
323         }
324         if ($a->interactive) {
325                 $_SESSION['sysmsg_info'][] = $s;
326         }
327 }
328
329 function feed_birthday($uid, $tz)
330 {
331         /**
332          * Determine the next birthday, but only if the birthday is published
333          * in the default profile. We _could_ also look for a private profile that the
334          * recipient can see, but somebody could get mad at us if they start getting
335          * public birthday greetings when they haven't made this info public.
336          *
337          * Assuming we are able to publish this info, we are then going to convert
338          * the start time from the owner's timezone to UTC.
339          *
340          * This will potentially solve the problem found with some social networks
341          * where birthdays are converted to the viewer's timezone and salutations from
342          * elsewhere in the world show up on the wrong day. We will convert it to the
343          * viewer's timezone also, but first we are going to convert it from the birthday
344          * person's timezone to GMT - so the viewer may find the birthday starting at
345          * 6:00PM the day before, but that will correspond to midnight to the birthday person.
346          */
347         $birthday = '';
348
349         if (!strlen($tz)) {
350                 $tz = 'UTC';
351         }
352
353         $profile = DBA::selectFirst('profile', ['dob'], ['uid' => $uid]);
354         if (DBA::isResult($profile)) {
355                 $tmp_dob = substr($profile['dob'], 5);
356                 if (intval($tmp_dob)) {
357                         $y = DateTimeFormat::timezoneNow($tz, 'Y');
358                         $bd = $y . '-' . $tmp_dob . ' 00:00';
359                         $t_dob = strtotime($bd);
360                         $now = strtotime(DateTimeFormat::timezoneNow($tz));
361                         if ($t_dob < $now) {
362                                 $bd = $y + 1 . '-' . $tmp_dob . ' 00:00';
363                         }
364                         $birthday = DateTimeFormat::convert($bd, 'UTC', $tz, DateTimeFormat::ATOM);
365                 }
366         }
367
368         return $birthday;
369 }
370
371 /**
372  * Check if current user has admin role.
373  *
374  * @return bool true if user is an admin
375  */
376 function is_site_admin()
377 {
378         $a = DI::app();
379
380         $admin_email = DI::config()->get('config', 'admin_email');
381
382         $adminlist = explode(',', str_replace(' ', '', $admin_email));
383
384         return local_user() && $admin_email && in_array($a->user['email'] ?? '', $adminlist);
385 }
386
387 function explode_querystring($query)
388 {
389         $arg_st = strpos($query, '?');
390         if ($arg_st !== false) {
391                 $base = substr($query, 0, $arg_st);
392                 $arg_st += 1;
393         } else {
394                 $base = '';
395                 $arg_st = 0;
396         }
397
398         $args = explode('&', substr($query, $arg_st));
399         foreach ($args as $k => $arg) {
400                 /// @TODO really compare type-safe here?
401                 if ($arg === '') {
402                         unset($args[$k]);
403                 }
404         }
405         $args = array_values($args);
406
407         if (!$base) {
408                 $base = $args[0];
409                 unset($args[0]);
410                 $args = array_values($args);
411         }
412
413         return [
414                 'base' => $base,
415                 'args' => $args,
416         ];
417 }
418
419 /**
420  * Returns the complete URL of the current page, e.g.: http(s)://something.com/network
421  *
422  * Taken from http://webcheatsheet.com/php/get_current_page_url.php
423  */
424 function curPageURL()
425 {
426         $pageURL = 'http';
427         if (!empty($_SERVER["HTTPS"]) && ($_SERVER["HTTPS"] == "on")) {
428                 $pageURL .= "s";
429         }
430
431         $pageURL .= "://";
432
433         if ($_SERVER["SERVER_PORT"] != "80" && $_SERVER["SERVER_PORT"] != "443") {
434                 $pageURL .= $_SERVER["SERVER_NAME"] . ":" . $_SERVER["SERVER_PORT"] . $_SERVER["REQUEST_URI"];
435         } else {
436                 $pageURL .= $_SERVER["SERVER_NAME"] . $_SERVER["REQUEST_URI"];
437         }
438         return $pageURL;
439 }
440
441 function get_temppath()
442 {
443         $temppath = DI::config()->get("system", "temppath");
444
445         if (($temppath != "") && System::isDirectoryUsable($temppath)) {
446                 // We have a temp path and it is usable
447                 return BasePath::getRealPath($temppath);
448         }
449
450         // We don't have a working preconfigured temp path, so we take the system path.
451         $temppath = sys_get_temp_dir();
452
453         // Check if it is usable
454         if (($temppath != "") && System::isDirectoryUsable($temppath)) {
455                 // Always store the real path, not the path through symlinks
456                 $temppath = BasePath::getRealPath($temppath);
457
458                 // To avoid any interferences with other systems we create our own directory
459                 $new_temppath = $temppath . "/" . DI::baseUrl()->getHostname();
460                 if (!is_dir($new_temppath)) {
461                         /// @TODO There is a mkdir()+chmod() upwards, maybe generalize this (+ configurable) into a function/method?
462                         mkdir($new_temppath);
463                 }
464
465                 if (System::isDirectoryUsable($new_temppath)) {
466                         // The new path is usable, we are happy
467                         DI::config()->set("system", "temppath", $new_temppath);
468                         return $new_temppath;
469                 } else {
470                         // We can't create a subdirectory, strange.
471                         // But the directory seems to work, so we use it but don't store it.
472                         return $temppath;
473                 }
474         }
475
476         // Reaching this point means that the operating system is configured badly.
477         return '';
478 }
479
480 function get_cachefile($file, $writemode = true)
481 {
482         $cache = get_itemcachepath();
483
484         if ((!$cache) || (!is_dir($cache))) {
485                 return "";
486         }
487
488         $subfolder = $cache . "/" . substr($file, 0, 2);
489
490         $cachepath = $subfolder . "/" . $file;
491
492         if ($writemode) {
493                 if (!is_dir($subfolder)) {
494                         mkdir($subfolder);
495                         chmod($subfolder, 0777);
496                 }
497         }
498
499         return $cachepath;
500 }
501
502 function clear_cache($basepath = "", $path = "")
503 {
504         if ($path == "") {
505                 $basepath = get_itemcachepath();
506                 $path = $basepath;
507         }
508
509         if (($path == "") || (!is_dir($path))) {
510                 return;
511         }
512
513         if (substr(realpath($path), 0, strlen($basepath)) != $basepath) {
514                 return;
515         }
516
517         $cachetime = (int) DI::config()->get('system', 'itemcache_duration');
518         if ($cachetime == 0) {
519                 $cachetime = 86400;
520         }
521
522         if (is_writable($path)) {
523                 if ($dh = opendir($path)) {
524                         while (($file = readdir($dh)) !== false) {
525                                 $fullpath = $path . "/" . $file;
526                                 if ((filetype($fullpath) == "dir") && ($file != ".") && ($file != "..")) {
527                                         clear_cache($basepath, $fullpath);
528                                 }
529                                 if ((filetype($fullpath) == "file") && (filectime($fullpath) < (time() - $cachetime))) {
530                                         unlink($fullpath);
531                                 }
532                         }
533                         closedir($dh);
534                 }
535         }
536 }
537
538 function get_itemcachepath()
539 {
540         // Checking, if the cache is deactivated
541         $cachetime = (int) DI::config()->get('system', 'itemcache_duration');
542         if ($cachetime < 0) {
543                 return "";
544         }
545
546         $itemcache = DI::config()->get('system', 'itemcache');
547         if (($itemcache != "") && System::isDirectoryUsable($itemcache)) {
548                 return BasePath::getRealPath($itemcache);
549         }
550
551         $temppath = get_temppath();
552
553         if ($temppath != "") {
554                 $itemcache = $temppath . "/itemcache";
555                 if (!file_exists($itemcache) && !is_dir($itemcache)) {
556                         mkdir($itemcache);
557                 }
558
559                 if (System::isDirectoryUsable($itemcache)) {
560                         DI::config()->set("system", "itemcache", $itemcache);
561                         return $itemcache;
562                 }
563         }
564         return "";
565 }
566
567 /**
568  * Returns the path where spool files are stored
569  *
570  * @return string Spool path
571  */
572 function get_spoolpath()
573 {
574         $spoolpath = DI::config()->get('system', 'spoolpath');
575         if (($spoolpath != "") && System::isDirectoryUsable($spoolpath)) {
576                 // We have a spool path and it is usable
577                 return $spoolpath;
578         }
579
580         // We don't have a working preconfigured spool path, so we take the temp path.
581         $temppath = get_temppath();
582
583         if ($temppath != "") {
584                 // To avoid any interferences with other systems we create our own directory
585                 $spoolpath = $temppath . "/spool";
586                 if (!is_dir($spoolpath)) {
587                         mkdir($spoolpath);
588                 }
589
590                 if (System::isDirectoryUsable($spoolpath)) {
591                         // The new path is usable, we are happy
592                         DI::config()->set("system", "spoolpath", $spoolpath);
593                         return $spoolpath;
594                 } else {
595                         // We can't create a subdirectory, strange.
596                         // But the directory seems to work, so we use it but don't store it.
597                         return $temppath;
598                 }
599         }
600
601         // Reaching this point means that the operating system is configured badly.
602         return "";
603 }
604
605 if (!function_exists('exif_imagetype')) {
606         function exif_imagetype($file)
607         {
608                 $size = getimagesize($file);
609                 return $size[2];
610         }
611 }
612
613 function validate_include(&$file)
614 {
615         $orig_file = $file;
616
617         $file = realpath($file);
618
619         if (strpos($file, getcwd()) !== 0) {
620                 return false;
621         }
622
623         $file = str_replace(getcwd() . "/", "", $file, $count);
624         if ($count != 1) {
625                 return false;
626         }
627
628         if ($orig_file !== $file) {
629                 return false;
630         }
631
632         $valid = false;
633         if (strpos($file, "include/") === 0) {
634                 $valid = true;
635         }
636
637         if (strpos($file, "addon/") === 0) {
638                 $valid = true;
639         }
640
641         // Simply return flag
642         return $valid;
643 }