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.
21 use Friendica\Core\Config;
22 use Friendica\Core\PConfig;
23 use Friendica\Core\Protocol;
24 use Friendica\Core\System;
25 use Friendica\Database\DBA;
27 use Friendica\Model\Contact;
28 use Friendica\Model\Term;
29 use Friendica\Util\BasePath;
30 use Friendica\Util\DateTimeFormat;
32 define('FRIENDICA_PLATFORM', 'Friendica');
33 define('FRIENDICA_CODENAME', 'Dalmatian Bellflower');
34 define('FRIENDICA_VERSION', '2020.03-dev');
35 define('DFRN_PROTOCOL_VERSION', '2.23');
36 define('NEW_UPDATE_ROUTINE_VERSION', 1170);
39 * @brief Constant with a HTML line break.
41 * Contains a HTML line break (br) element and a real carriage return with line
42 * feed for the source.
43 * This can be used in HTML and JavaScript where needed a line break.
45 define('EOL', "<br />\r\n");
48 * @brief Image storage quality.
50 * Lower numbers save space at cost of image detail.
51 * For ease of upgrade, please do not change here. Set system.jpegquality = n in config/local.config.php,
52 * where n is between 1 and 100, and with very poor results below about 50
54 define('JPEG_QUALITY', 100);
57 * system.png_quality = n where is between 0 (uncompressed) to 9
59 define('PNG_QUALITY', 8);
62 * An alternate way of limiting picture upload sizes. Specify the maximum pixel
63 * length that pictures are allowed to be (for non-square pictures, it will apply
64 * to the longest side). Pictures longer than this length will be resized to be
65 * this length (on the longest side, the other side will be scaled appropriately).
66 * Modify this value using
69 * 'max_image_length' => 'n',
73 * in config/local.config.php
75 * If you don't want to set a maximum length, set to -1. The default value is
76 * defined by 'MAX_IMAGE_LENGTH' below.
78 define('MAX_IMAGE_LENGTH', -1);
83 define('DEFAULT_DB_ENGINE', 'InnoDB');
85 /** @deprecated since version 2019.03, please use \Friendica\Module\Register::CLOSED instead */
86 define('REGISTER_CLOSED', \Friendica\Module\Register::CLOSED);
87 /** @deprecated since version 2019.03, please use \Friendica\Module\Register::APPROVE instead */
88 define('REGISTER_APPROVE', \Friendica\Module\Register::APPROVE);
89 /** @deprecated since version 2019.03, please use \Friendica\Module\Register::OPEN instead */
90 define('REGISTER_OPEN', \Friendica\Module\Register::OPEN);
95 * Type of the community page
98 define('CP_NO_INTERNAL_COMMUNITY', -2);
99 define('CP_NO_COMMUNITY_PAGE', -1);
100 define('CP_USERS_ON_SERVER', 0);
101 define('CP_GLOBAL_COMMUNITY', 1);
102 define('CP_USERS_AND_GLOBAL', 2);
108 * These numbers are used in stored permissions
109 * and existing allocations MUST NEVER BE CHANGED
110 * OR RE-ASSIGNED! You may only add to them.
113 Protocol::DFRN => (-1),
114 Protocol::ZOT => (-2),
115 Protocol::OSTATUS => (-3),
116 Protocol::FEED => (-4),
117 Protocol::DIASPORA => (-5),
118 Protocol::MAIL => (-6),
119 Protocol::FACEBOOK => (-8),
120 Protocol::LINKEDIN => (-9),
121 Protocol::XMPP => (-10),
122 Protocol::MYSPACE => (-11),
123 Protocol::GPLUS => (-12),
124 Protocol::PUMPIO => (-13),
125 Protocol::TWITTER => (-14),
126 Protocol::DIASPORA2 => (-15),
127 Protocol::STATUSNET => (-16),
128 Protocol::NEWS => (-18),
129 Protocol::ICALENDAR => (-19),
130 Protocol::PNUT => (-20),
132 Protocol::PHANTOM => (-127),
136 * Maximum number of "people who like (or don't like) this" that we will list by name
138 define('MAX_LIKERS', 75);
143 * Email notification options
146 define('NOTIFY_INTRO', 1);
147 define('NOTIFY_CONFIRM', 2);
148 define('NOTIFY_WALL', 4);
149 define('NOTIFY_COMMENT', 8);
150 define('NOTIFY_MAIL', 16);
151 define('NOTIFY_SUGGEST', 32);
152 define('NOTIFY_PROFILE', 64);
153 define('NOTIFY_TAGSELF', 128);
154 define('NOTIFY_TAGSHARE', 256);
155 define('NOTIFY_POKE', 512);
156 define('NOTIFY_SHARE', 1024);
158 define('SYSTEM_EMAIL', 16384);
160 define('NOTIFY_SYSTEM', 32768);
164 /** @deprecated since 2019.03, use Term::UNKNOWN instead */
165 define('TERM_UNKNOWN', Term::UNKNOWN);
166 /** @deprecated since 2019.03, use Term::HASHTAG instead */
167 define('TERM_HASHTAG', Term::HASHTAG);
168 /** @deprecated since 2019.03, use Term::MENTION instead */
169 define('TERM_MENTION', Term::MENTION);
170 /** @deprecated since 2019.03, use Term::CATEGORY instead */
171 define('TERM_CATEGORY', Term::CATEGORY);
172 /** @deprecated since 2019.03, use Term::PCATEGORY instead */
173 define('TERM_PCATEGORY', Term::PCATEGORY);
174 /** @deprecated since 2019.03, use Term::FILE instead */
175 define('TERM_FILE', Term::FILE);
176 /** @deprecated since 2019.03, use Term::SAVEDSEARCH instead */
177 define('TERM_SAVEDSEARCH', Term::SAVEDSEARCH);
178 /** @deprecated since 2019.03, use Term::CONVERSATION instead */
179 define('TERM_CONVERSATION', Term::CONVERSATION);
181 /** @deprecated since 2019.03, use Term::OBJECT_TYPE_POST instead */
182 define('TERM_OBJ_POST', Term::OBJECT_TYPE_POST);
183 /** @deprecated since 2019.03, use Term::OBJECT_TYPE_PHOTO instead */
184 define('TERM_OBJ_PHOTO', Term::OBJECT_TYPE_PHOTO);
189 * Item weight for query ordering
192 define('GRAVITY_PARENT', 0);
193 define('GRAVITY_ACTIVITY', 3);
194 define('GRAVITY_COMMENT', 6);
195 define('GRAVITY_UNKNOWN', 9);
201 * Process priority for the worker
204 define('PRIORITY_UNDEFINED', 0);
205 define('PRIORITY_CRITICAL', 10);
206 define('PRIORITY_HIGH', 20);
207 define('PRIORITY_MEDIUM', 30);
208 define('PRIORITY_LOW', 40);
209 define('PRIORITY_NEGLIGIBLE', 50);
213 * @name Social Relay settings
215 * See here: https://github.com/jaywink/social-relay
216 * and here: https://wiki.diasporafoundation.org/Relay_servers_for_public_posts
219 define('SR_SCOPE_NONE', '');
220 define('SR_SCOPE_ALL', 'all');
221 define('SR_SCOPE_TAGS', 'tags');
224 // Normally this constant is defined - but not if "pcntl" isn't installed
225 if (!defined("SIGTERM")) {
226 define("SIGTERM", 15);
230 * Depending on the PHP version this constant does exist - or not.
231 * See here: http://php.net/manual/en/curl.constants.php#117928
233 if (!defined('CURLE_OPERATION_TIMEDOUT')) {
234 define('CURLE_OPERATION_TIMEDOUT', CURLE_OPERATION_TIMEOUTED);
238 * @brief Returns the user id of locally logged in user or false.
240 * @return int|bool user id or false
242 function local_user()
244 if (!empty($_SESSION['authenticated']) && !empty($_SESSION['uid'])) {
245 return intval($_SESSION['uid']);
251 * @brief Returns the public contact id of logged in user or false.
253 * @return int|bool public contact id or false
255 function public_contact()
257 static $public_contact_id = false;
259 if (!$public_contact_id && !empty($_SESSION['authenticated'])) {
260 if (!empty($_SESSION['my_address'])) {
262 $public_contact_id = intval(Contact::getIdForURL($_SESSION['my_address'], 0, true));
263 } elseif (!empty($_SESSION['visitor_home'])) {
265 $public_contact_id = intval(Contact::getIdForURL($_SESSION['visitor_home'], 0, true));
267 } elseif (empty($_SESSION['authenticated'])) {
268 $public_contact_id = false;
271 return $public_contact_id;
275 * @brief Returns contact id of authenticated site visitor or false
277 * @return int|bool visitor_id or false
279 function remote_user()
281 if (empty($_SESSION['authenticated'])) {
285 if (!empty($_SESSION['visitor_id'])) {
286 return intval($_SESSION['visitor_id']);
293 * @brief Show an error message to user.
295 * This function save text in session, to be shown to the user at next page load
297 * @param string $s - Text of notice
301 if (empty($_SESSION)) {
306 if (empty($_SESSION['sysmsg'])) {
307 $_SESSION['sysmsg'] = [];
309 if ($a->interactive) {
310 $_SESSION['sysmsg'][] = $s;
315 * @brief Show an info message to user.
317 * This function save text in session, to be shown to the user at next page load
319 * @param string $s - Text of notice
325 if (local_user() && PConfig::get(local_user(), 'system', 'ignore_info')) {
329 if (empty($_SESSION['sysmsg_info'])) {
330 $_SESSION['sysmsg_info'] = [];
332 if ($a->interactive) {
333 $_SESSION['sysmsg_info'][] = $s;
337 function feed_birthday($uid, $tz)
340 * Determine the next birthday, but only if the birthday is published
341 * in the default profile. We _could_ also look for a private profile that the
342 * recipient can see, but somebody could get mad at us if they start getting
343 * public birthday greetings when they haven't made this info public.
345 * Assuming we are able to publish this info, we are then going to convert
346 * the start time from the owner's timezone to UTC.
348 * This will potentially solve the problem found with some social networks
349 * where birthdays are converted to the viewer's timezone and salutations from
350 * elsewhere in the world show up on the wrong day. We will convert it to the
351 * viewer's timezone also, but first we are going to convert it from the birthday
352 * person's timezone to GMT - so the viewer may find the birthday starting at
353 * 6:00PM the day before, but that will correspond to midnight to the birthday person.
361 $profile = DBA::selectFirst('profile', ['dob'], ['is-default' => true, 'uid' => $uid]);
362 if (DBA::isResult($profile)) {
363 $tmp_dob = substr($profile['dob'], 5);
364 if (intval($tmp_dob)) {
365 $y = DateTimeFormat::timezoneNow($tz, 'Y');
366 $bd = $y . '-' . $tmp_dob . ' 00:00';
367 $t_dob = strtotime($bd);
368 $now = strtotime(DateTimeFormat::timezoneNow($tz));
370 $bd = $y + 1 . '-' . $tmp_dob . ' 00:00';
372 $birthday = DateTimeFormat::convert($bd, 'UTC', $tz, DateTimeFormat::ATOM);
380 * @brief Check if current user has admin role.
382 * @return bool true if user is an admin
384 function is_site_admin()
388 $admin_email = Config::get('config', 'admin_email');
390 $adminlist = explode(',', str_replace(' ', '', $admin_email));
392 return local_user() && $admin_email && in_array($a->user['email'] ?? '', $adminlist);
395 function explode_querystring($query)
397 $arg_st = strpos($query, '?');
398 if ($arg_st !== false) {
399 $base = substr($query, 0, $arg_st);
406 $args = explode('&', substr($query, $arg_st));
407 foreach ($args as $k => $arg) {
408 /// @TODO really compare type-safe here?
413 $args = array_values($args);
418 $args = array_values($args);
428 * Returns the complete URL of the current page, e.g.: http(s)://something.com/network
430 * Taken from http://webcheatsheet.com/php/get_current_page_url.php
432 function curPageURL()
435 if (!empty($_SERVER["HTTPS"]) && ($_SERVER["HTTPS"] == "on")) {
441 if ($_SERVER["SERVER_PORT"] != "80" && $_SERVER["SERVER_PORT"] != "443") {
442 $pageURL .= $_SERVER["SERVER_NAME"] . ":" . $_SERVER["SERVER_PORT"] . $_SERVER["REQUEST_URI"];
444 $pageURL .= $_SERVER["SERVER_NAME"] . $_SERVER["REQUEST_URI"];
449 function get_temppath()
451 $temppath = Config::get("system", "temppath");
453 if (($temppath != "") && System::isDirectoryUsable($temppath)) {
454 // We have a temp path and it is usable
455 return BasePath::getRealPath($temppath);
458 // We don't have a working preconfigured temp path, so we take the system path.
459 $temppath = sys_get_temp_dir();
461 // Check if it is usable
462 if (($temppath != "") && System::isDirectoryUsable($temppath)) {
463 // Always store the real path, not the path through symlinks
464 $temppath = BasePath::getRealPath($temppath);
466 // To avoid any interferences with other systems we create our own directory
467 $new_temppath = $temppath . "/" . DI::baseUrl()->getHostname();
468 if (!is_dir($new_temppath)) {
469 /// @TODO There is a mkdir()+chmod() upwards, maybe generalize this (+ configurable) into a function/method?
470 mkdir($new_temppath);
473 if (System::isDirectoryUsable($new_temppath)) {
474 // The new path is usable, we are happy
475 Config::set("system", "temppath", $new_temppath);
476 return $new_temppath;
478 // We can't create a subdirectory, strange.
479 // But the directory seems to work, so we use it but don't store it.
484 // Reaching this point means that the operating system is configured badly.
488 function get_cachefile($file, $writemode = true)
490 $cache = get_itemcachepath();
492 if ((!$cache) || (!is_dir($cache))) {
496 $subfolder = $cache . "/" . substr($file, 0, 2);
498 $cachepath = $subfolder . "/" . $file;
501 if (!is_dir($subfolder)) {
503 chmod($subfolder, 0777);
510 function clear_cache($basepath = "", $path = "")
513 $basepath = get_itemcachepath();
517 if (($path == "") || (!is_dir($path))) {
521 if (substr(realpath($path), 0, strlen($basepath)) != $basepath) {
525 $cachetime = (int) Config::get('system', 'itemcache_duration');
526 if ($cachetime == 0) {
530 if (is_writable($path)) {
531 if ($dh = opendir($path)) {
532 while (($file = readdir($dh)) !== false) {
533 $fullpath = $path . "/" . $file;
534 if ((filetype($fullpath) == "dir") && ($file != ".") && ($file != "..")) {
535 clear_cache($basepath, $fullpath);
537 if ((filetype($fullpath) == "file") && (filectime($fullpath) < (time() - $cachetime))) {
546 function get_itemcachepath()
548 // Checking, if the cache is deactivated
549 $cachetime = (int) Config::get('system', 'itemcache_duration');
550 if ($cachetime < 0) {
554 $itemcache = Config::get('system', 'itemcache');
555 if (($itemcache != "") && System::isDirectoryUsable($itemcache)) {
556 return BasePath::getRealPath($itemcache);
559 $temppath = get_temppath();
561 if ($temppath != "") {
562 $itemcache = $temppath . "/itemcache";
563 if (!file_exists($itemcache) && !is_dir($itemcache)) {
567 if (System::isDirectoryUsable($itemcache)) {
568 Config::set("system", "itemcache", $itemcache);
576 * @brief Returns the path where spool files are stored
578 * @return string Spool path
580 function get_spoolpath()
582 $spoolpath = Config::get('system', 'spoolpath');
583 if (($spoolpath != "") && System::isDirectoryUsable($spoolpath)) {
584 // We have a spool path and it is usable
588 // We don't have a working preconfigured spool path, so we take the temp path.
589 $temppath = get_temppath();
591 if ($temppath != "") {
592 // To avoid any interferences with other systems we create our own directory
593 $spoolpath = $temppath . "/spool";
594 if (!is_dir($spoolpath)) {
598 if (System::isDirectoryUsable($spoolpath)) {
599 // The new path is usable, we are happy
600 Config::set("system", "spoolpath", $spoolpath);
603 // We can't create a subdirectory, strange.
604 // But the directory seems to work, so we use it but don't store it.
609 // Reaching this point means that the operating system is configured badly.
613 if (!function_exists('exif_imagetype')) {
614 function exif_imagetype($file)
616 $size = getimagesize($file);
621 function validate_include(&$file)
625 $file = realpath($file);
627 if (strpos($file, getcwd()) !== 0) {
631 $file = str_replace(getcwd() . "/", "", $file, $count);
636 if ($orig_file !== $file) {
641 if (strpos($file, "include/") === 0) {
645 if (strpos($file, "addon/") === 0) {
649 // Simply return flag