4 * This file defines some global constants and includes the central App class.
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.
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.
20 use Friendica\Core\Protocol;
21 use Friendica\Core\System;
22 use Friendica\Database\DBA;
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;
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);
37 * Constant with a HTML line break.
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.
43 define('EOL', "<br />\r\n");
46 * Image storage quality.
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
52 define('JPEG_QUALITY', 100);
55 * system.png_quality = n where is between 0 (uncompressed) to 9
57 define('PNG_QUALITY', 8);
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
67 * 'max_image_length' => 'n',
71 * in config/local.config.php
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.
76 define('MAX_IMAGE_LENGTH', -1);
81 define('DEFAULT_DB_ENGINE', 'InnoDB');
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);
93 * Type of the community page
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);
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.
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),
130 Protocol::PHANTOM => (-127),
134 * Maximum number of "people who like (or don't like) this" that we will list by name
136 define('MAX_LIKERS', 75);
141 * Email notification options
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);
167 /** @deprecated since 2020.12, use Notify\Type::SYSTEM instead */
168 define('NOTIFY_SYSTEM', Notify\Type::SYSTEM);
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);
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);
197 * Item weight for query ordering
200 define('GRAVITY_PARENT', 0);
201 define('GRAVITY_ACTIVITY', 3);
202 define('GRAVITY_COMMENT', 6);
203 define('GRAVITY_UNKNOWN', 9);
209 * Process priority for the worker
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);
221 * @name Social Relay settings
223 * See here: https://github.com/jaywink/social-relay
224 * and here: https://wiki.diasporafoundation.org/Relay_servers_for_public_posts
227 define('SR_SCOPE_NONE', '');
228 define('SR_SCOPE_ALL', 'all');
229 define('SR_SCOPE_TAGS', 'tags');
232 // Normally this constant is defined - but not if "pcntl" isn't installed
233 if (!defined("SIGTERM")) {
234 define("SIGTERM", 15);
238 * Depending on the PHP version this constant does exist - or not.
239 * See here: http://php.net/manual/en/curl.constants.php#117928
241 if (!defined('CURLE_OPERATION_TIMEDOUT')) {
242 define('CURLE_OPERATION_TIMEDOUT', CURLE_OPERATION_TIMEOUTED);
246 * Returns the user id of locally logged in user or false.
248 * @return int|bool user id or false
250 function local_user()
252 if (!empty($_SESSION['authenticated']) && !empty($_SESSION['uid'])) {
253 return intval($_SESSION['uid']);
259 * Returns the public contact id of logged in user or false.
261 * @return int|bool public contact id or false
263 function public_contact()
265 static $public_contact_id = false;
267 if (!$public_contact_id && !empty($_SESSION['authenticated'])) {
268 if (!empty($_SESSION['my_address'])) {
270 $public_contact_id = intval(Contact::getIdForURL($_SESSION['my_address'], 0, true));
271 } elseif (!empty($_SESSION['visitor_home'])) {
273 $public_contact_id = intval(Contact::getIdForURL($_SESSION['visitor_home'], 0, true));
275 } elseif (empty($_SESSION['authenticated'])) {
276 $public_contact_id = false;
279 return $public_contact_id;
283 * Returns contact id of authenticated site visitor or false
285 * @return int|bool visitor_id or false
287 function remote_user()
289 if (empty($_SESSION['authenticated'])) {
293 if (!empty($_SESSION['visitor_id'])) {
294 return intval($_SESSION['visitor_id']);
301 * Show an error message to user.
303 * This function save text in session, to be shown to the user at next page load
305 * @param string $s - Text of notice
309 if (empty($_SESSION)) {
314 if (empty($_SESSION['sysmsg'])) {
315 $_SESSION['sysmsg'] = [];
317 if ($a->interactive) {
318 $_SESSION['sysmsg'][] = $s;
323 * Show an info message to user.
325 * This function save text in session, to be shown to the user at next page load
327 * @param string $s - Text of notice
333 if (local_user() && DI::pConfig()->get(local_user(), 'system', 'ignore_info')) {
337 if (empty($_SESSION['sysmsg_info'])) {
338 $_SESSION['sysmsg_info'] = [];
340 if ($a->interactive) {
341 $_SESSION['sysmsg_info'][] = $s;
345 function feed_birthday($uid, $tz)
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.
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.
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.
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));
378 $bd = $y + 1 . '-' . $tmp_dob . ' 00:00';
380 $birthday = DateTimeFormat::convert($bd, 'UTC', $tz, DateTimeFormat::ATOM);
388 * Check if current user has admin role.
390 * @return bool true if user is an admin
392 function is_site_admin()
396 $admin_email = DI::config()->get('config', 'admin_email');
398 $adminlist = explode(',', str_replace(' ', '', $admin_email));
400 return local_user() && $admin_email && in_array($a->user['email'] ?? '', $adminlist);
403 function explode_querystring($query)
405 $arg_st = strpos($query, '?');
406 if ($arg_st !== false) {
407 $base = substr($query, 0, $arg_st);
414 $args = explode('&', substr($query, $arg_st));
415 foreach ($args as $k => $arg) {
416 /// @TODO really compare type-safe here?
421 $args = array_values($args);
426 $args = array_values($args);
436 * Returns the complete URL of the current page, e.g.: http(s)://something.com/network
438 * Taken from http://webcheatsheet.com/php/get_current_page_url.php
440 function curPageURL()
443 if (!empty($_SERVER["HTTPS"]) && ($_SERVER["HTTPS"] == "on")) {
449 if ($_SERVER["SERVER_PORT"] != "80" && $_SERVER["SERVER_PORT"] != "443") {
450 $pageURL .= $_SERVER["SERVER_NAME"] . ":" . $_SERVER["SERVER_PORT"] . $_SERVER["REQUEST_URI"];
452 $pageURL .= $_SERVER["SERVER_NAME"] . $_SERVER["REQUEST_URI"];
457 function get_temppath()
459 $temppath = DI::config()->get("system", "temppath");
461 if (($temppath != "") && System::isDirectoryUsable($temppath)) {
462 // We have a temp path and it is usable
463 return BasePath::getRealPath($temppath);
466 // We don't have a working preconfigured temp path, so we take the system path.
467 $temppath = sys_get_temp_dir();
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);
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);
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;
486 // We can't create a subdirectory, strange.
487 // But the directory seems to work, so we use it but don't store it.
492 // Reaching this point means that the operating system is configured badly.
496 function get_cachefile($file, $writemode = true)
498 $cache = get_itemcachepath();
500 if ((!$cache) || (!is_dir($cache))) {
504 $subfolder = $cache . "/" . substr($file, 0, 2);
506 $cachepath = $subfolder . "/" . $file;
509 if (!is_dir($subfolder)) {
511 chmod($subfolder, 0777);
518 function clear_cache($basepath = "", $path = "")
521 $basepath = get_itemcachepath();
525 if (($path == "") || (!is_dir($path))) {
529 if (substr(realpath($path), 0, strlen($basepath)) != $basepath) {
533 $cachetime = (int) DI::config()->get('system', 'itemcache_duration');
534 if ($cachetime == 0) {
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);
545 if ((filetype($fullpath) == "file") && (filectime($fullpath) < (time() - $cachetime))) {
554 function get_itemcachepath()
556 // Checking, if the cache is deactivated
557 $cachetime = (int) DI::config()->get('system', 'itemcache_duration');
558 if ($cachetime < 0) {
562 $itemcache = DI::config()->get('system', 'itemcache');
563 if (($itemcache != "") && System::isDirectoryUsable($itemcache)) {
564 return BasePath::getRealPath($itemcache);
567 $temppath = get_temppath();
569 if ($temppath != "") {
570 $itemcache = $temppath . "/itemcache";
571 if (!file_exists($itemcache) && !is_dir($itemcache)) {
575 if (System::isDirectoryUsable($itemcache)) {
576 DI::config()->set("system", "itemcache", $itemcache);
584 * Returns the path where spool files are stored
586 * @return string Spool path
588 function get_spoolpath()
590 $spoolpath = DI::config()->get('system', 'spoolpath');
591 if (($spoolpath != "") && System::isDirectoryUsable($spoolpath)) {
592 // We have a spool path and it is usable
596 // We don't have a working preconfigured spool path, so we take the temp path.
597 $temppath = get_temppath();
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)) {
606 if (System::isDirectoryUsable($spoolpath)) {
607 // The new path is usable, we are happy
608 DI::config()->set("system", "spoolpath", $spoolpath);
611 // We can't create a subdirectory, strange.
612 // But the directory seems to work, so we use it but don't store it.
617 // Reaching this point means that the operating system is configured badly.
621 if (!function_exists('exif_imagetype')) {
622 function exif_imagetype($file)
624 $size = getimagesize($file);
629 function validate_include(&$file)
633 $file = realpath($file);
635 if (strpos($file, getcwd()) !== 0) {
639 $file = str_replace(getcwd() . "/", "", $file, $count);
644 if ($orig_file !== $file) {
649 if (strpos($file, "include/") === 0) {
653 if (strpos($file, "addon/") === 0) {
657 // Simply return flag