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