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\Term;
26 use Friendica\Util\BasePath;
27 use Friendica\Util\DateTimeFormat;
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);
36 * Constant with a HTML line break.
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.
42 define('EOL', "<br />\r\n");
45 * Image storage quality.
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
51 define('JPEG_QUALITY', 100);
54 * system.png_quality = n where is between 0 (uncompressed) to 9
56 define('PNG_QUALITY', 8);
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
66 * 'max_image_length' => 'n',
70 * in config/local.config.php
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.
75 define('MAX_IMAGE_LENGTH', -1);
80 define('DEFAULT_DB_ENGINE', 'InnoDB');
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);
92 * Type of the community page
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);
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.
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),
129 Protocol::PHANTOM => (-127),
133 * Maximum number of "people who like (or don't like) this" that we will list by name
135 define('MAX_LIKERS', 75);
140 * Email notification options
143 define('NOTIFY_TAGSHARE', 256);
144 define('NOTIFY_POKE', 512);
145 define('NOTIFY_SHARE', 1024);
147 define('NOTIFY_SYSTEM', 32768);
151 /** @deprecated since 2019.03, use Term::UNKNOWN instead */
152 define('TERM_UNKNOWN', Term::UNKNOWN);
153 /** @deprecated since 2019.03, use Term::HASHTAG instead */
154 define('TERM_HASHTAG', Term::HASHTAG);
155 /** @deprecated since 2019.03, use Term::MENTION instead */
156 define('TERM_MENTION', Term::MENTION);
157 /** @deprecated since 2019.03, use Term::CATEGORY instead */
158 define('TERM_CATEGORY', Term::CATEGORY);
159 /** @deprecated since 2019.03, use Term::PCATEGORY instead */
160 define('TERM_PCATEGORY', Term::PCATEGORY);
161 /** @deprecated since 2019.03, use Term::FILE instead */
162 define('TERM_FILE', Term::FILE);
163 /** @deprecated since 2019.03, use Term::SAVEDSEARCH instead */
164 define('TERM_SAVEDSEARCH', Term::SAVEDSEARCH);
165 /** @deprecated since 2019.03, use Term::CONVERSATION instead */
166 define('TERM_CONVERSATION', Term::CONVERSATION);
168 /** @deprecated since 2019.03, use Term::OBJECT_TYPE_POST instead */
169 define('TERM_OBJ_POST', Term::OBJECT_TYPE_POST);
170 /** @deprecated since 2019.03, use Term::OBJECT_TYPE_PHOTO instead */
171 define('TERM_OBJ_PHOTO', Term::OBJECT_TYPE_PHOTO);
176 * Item weight for query ordering
179 define('GRAVITY_PARENT', 0);
180 define('GRAVITY_ACTIVITY', 3);
181 define('GRAVITY_COMMENT', 6);
182 define('GRAVITY_UNKNOWN', 9);
188 * Process priority for the worker
191 define('PRIORITY_UNDEFINED', 0);
192 define('PRIORITY_CRITICAL', 10);
193 define('PRIORITY_HIGH', 20);
194 define('PRIORITY_MEDIUM', 30);
195 define('PRIORITY_LOW', 40);
196 define('PRIORITY_NEGLIGIBLE', 50);
200 * @name Social Relay settings
202 * See here: https://github.com/jaywink/social-relay
203 * and here: https://wiki.diasporafoundation.org/Relay_servers_for_public_posts
206 define('SR_SCOPE_NONE', '');
207 define('SR_SCOPE_ALL', 'all');
208 define('SR_SCOPE_TAGS', 'tags');
211 // Normally this constant is defined - but not if "pcntl" isn't installed
212 if (!defined("SIGTERM")) {
213 define("SIGTERM", 15);
217 * Depending on the PHP version this constant does exist - or not.
218 * See here: http://php.net/manual/en/curl.constants.php#117928
220 if (!defined('CURLE_OPERATION_TIMEDOUT')) {
221 define('CURLE_OPERATION_TIMEDOUT', CURLE_OPERATION_TIMEOUTED);
225 * Returns the user id of locally logged in user or false.
227 * @return int|bool user id or false
229 function local_user()
231 if (!empty($_SESSION['authenticated']) && !empty($_SESSION['uid'])) {
232 return intval($_SESSION['uid']);
238 * Returns the public contact id of logged in user or false.
240 * @return int|bool public contact id or false
242 function public_contact()
244 static $public_contact_id = false;
246 if (!$public_contact_id && !empty($_SESSION['authenticated'])) {
247 if (!empty($_SESSION['my_address'])) {
249 $public_contact_id = intval(Contact::getIdForURL($_SESSION['my_address'], 0, true));
250 } elseif (!empty($_SESSION['visitor_home'])) {
252 $public_contact_id = intval(Contact::getIdForURL($_SESSION['visitor_home'], 0, true));
254 } elseif (empty($_SESSION['authenticated'])) {
255 $public_contact_id = false;
258 return $public_contact_id;
262 * Returns contact id of authenticated site visitor or false
264 * @return int|bool visitor_id or false
266 function remote_user()
268 if (empty($_SESSION['authenticated'])) {
272 if (!empty($_SESSION['visitor_id'])) {
273 return intval($_SESSION['visitor_id']);
280 * Show an error message to user.
282 * This function save text in session, to be shown to the user at next page load
284 * @param string $s - Text of notice
288 if (empty($_SESSION)) {
293 if (empty($_SESSION['sysmsg'])) {
294 $_SESSION['sysmsg'] = [];
296 if ($a->interactive) {
297 $_SESSION['sysmsg'][] = $s;
302 * Show an info message to user.
304 * This function save text in session, to be shown to the user at next page load
306 * @param string $s - Text of notice
312 if (local_user() && DI::pConfig()->get(local_user(), 'system', 'ignore_info')) {
316 if (empty($_SESSION['sysmsg_info'])) {
317 $_SESSION['sysmsg_info'] = [];
319 if ($a->interactive) {
320 $_SESSION['sysmsg_info'][] = $s;
324 function feed_birthday($uid, $tz)
327 * Determine the next birthday, but only if the birthday is published
328 * in the default profile. We _could_ also look for a private profile that the
329 * recipient can see, but somebody could get mad at us if they start getting
330 * public birthday greetings when they haven't made this info public.
332 * Assuming we are able to publish this info, we are then going to convert
333 * the start time from the owner's timezone to UTC.
335 * This will potentially solve the problem found with some social networks
336 * where birthdays are converted to the viewer's timezone and salutations from
337 * elsewhere in the world show up on the wrong day. We will convert it to the
338 * viewer's timezone also, but first we are going to convert it from the birthday
339 * person's timezone to GMT - so the viewer may find the birthday starting at
340 * 6:00PM the day before, but that will correspond to midnight to the birthday person.
348 $profile = DBA::selectFirst('profile', ['dob'], ['uid' => $uid]);
349 if (DBA::isResult($profile)) {
350 $tmp_dob = substr($profile['dob'], 5);
351 if (intval($tmp_dob)) {
352 $y = DateTimeFormat::timezoneNow($tz, 'Y');
353 $bd = $y . '-' . $tmp_dob . ' 00:00';
354 $t_dob = strtotime($bd);
355 $now = strtotime(DateTimeFormat::timezoneNow($tz));
357 $bd = $y + 1 . '-' . $tmp_dob . ' 00:00';
359 $birthday = DateTimeFormat::convert($bd, 'UTC', $tz, DateTimeFormat::ATOM);
367 * Check if current user has admin role.
369 * @return bool true if user is an admin
371 function is_site_admin()
375 $admin_email = DI::config()->get('config', 'admin_email');
377 $adminlist = explode(',', str_replace(' ', '', $admin_email));
379 return local_user() && $admin_email && in_array($a->user['email'] ?? '', $adminlist);
382 function explode_querystring($query)
384 $arg_st = strpos($query, '?');
385 if ($arg_st !== false) {
386 $base = substr($query, 0, $arg_st);
393 $args = explode('&', substr($query, $arg_st));
394 foreach ($args as $k => $arg) {
395 /// @TODO really compare type-safe here?
400 $args = array_values($args);
405 $args = array_values($args);
415 * Returns the complete URL of the current page, e.g.: http(s)://something.com/network
417 * Taken from http://webcheatsheet.com/php/get_current_page_url.php
419 function curPageURL()
422 if (!empty($_SERVER["HTTPS"]) && ($_SERVER["HTTPS"] == "on")) {
428 if ($_SERVER["SERVER_PORT"] != "80" && $_SERVER["SERVER_PORT"] != "443") {
429 $pageURL .= $_SERVER["SERVER_NAME"] . ":" . $_SERVER["SERVER_PORT"] . $_SERVER["REQUEST_URI"];
431 $pageURL .= $_SERVER["SERVER_NAME"] . $_SERVER["REQUEST_URI"];
436 function get_temppath()
438 $temppath = DI::config()->get("system", "temppath");
440 if (($temppath != "") && System::isDirectoryUsable($temppath)) {
441 // We have a temp path and it is usable
442 return BasePath::getRealPath($temppath);
445 // We don't have a working preconfigured temp path, so we take the system path.
446 $temppath = sys_get_temp_dir();
448 // Check if it is usable
449 if (($temppath != "") && System::isDirectoryUsable($temppath)) {
450 // Always store the real path, not the path through symlinks
451 $temppath = BasePath::getRealPath($temppath);
453 // To avoid any interferences with other systems we create our own directory
454 $new_temppath = $temppath . "/" . DI::baseUrl()->getHostname();
455 if (!is_dir($new_temppath)) {
456 /// @TODO There is a mkdir()+chmod() upwards, maybe generalize this (+ configurable) into a function/method?
457 mkdir($new_temppath);
460 if (System::isDirectoryUsable($new_temppath)) {
461 // The new path is usable, we are happy
462 DI::config()->set("system", "temppath", $new_temppath);
463 return $new_temppath;
465 // We can't create a subdirectory, strange.
466 // But the directory seems to work, so we use it but don't store it.
471 // Reaching this point means that the operating system is configured badly.
475 function get_cachefile($file, $writemode = true)
477 $cache = get_itemcachepath();
479 if ((!$cache) || (!is_dir($cache))) {
483 $subfolder = $cache . "/" . substr($file, 0, 2);
485 $cachepath = $subfolder . "/" . $file;
488 if (!is_dir($subfolder)) {
490 chmod($subfolder, 0777);
497 function clear_cache($basepath = "", $path = "")
500 $basepath = get_itemcachepath();
504 if (($path == "") || (!is_dir($path))) {
508 if (substr(realpath($path), 0, strlen($basepath)) != $basepath) {
512 $cachetime = (int) DI::config()->get('system', 'itemcache_duration');
513 if ($cachetime == 0) {
517 if (is_writable($path)) {
518 if ($dh = opendir($path)) {
519 while (($file = readdir($dh)) !== false) {
520 $fullpath = $path . "/" . $file;
521 if ((filetype($fullpath) == "dir") && ($file != ".") && ($file != "..")) {
522 clear_cache($basepath, $fullpath);
524 if ((filetype($fullpath) == "file") && (filectime($fullpath) < (time() - $cachetime))) {
533 function get_itemcachepath()
535 // Checking, if the cache is deactivated
536 $cachetime = (int) DI::config()->get('system', 'itemcache_duration');
537 if ($cachetime < 0) {
541 $itemcache = DI::config()->get('system', 'itemcache');
542 if (($itemcache != "") && System::isDirectoryUsable($itemcache)) {
543 return BasePath::getRealPath($itemcache);
546 $temppath = get_temppath();
548 if ($temppath != "") {
549 $itemcache = $temppath . "/itemcache";
550 if (!file_exists($itemcache) && !is_dir($itemcache)) {
554 if (System::isDirectoryUsable($itemcache)) {
555 DI::config()->set("system", "itemcache", $itemcache);
563 * Returns the path where spool files are stored
565 * @return string Spool path
567 function get_spoolpath()
569 $spoolpath = DI::config()->get('system', 'spoolpath');
570 if (($spoolpath != "") && System::isDirectoryUsable($spoolpath)) {
571 // We have a spool path and it is usable
575 // We don't have a working preconfigured spool path, so we take the temp path.
576 $temppath = get_temppath();
578 if ($temppath != "") {
579 // To avoid any interferences with other systems we create our own directory
580 $spoolpath = $temppath . "/spool";
581 if (!is_dir($spoolpath)) {
585 if (System::isDirectoryUsable($spoolpath)) {
586 // The new path is usable, we are happy
587 DI::config()->set("system", "spoolpath", $spoolpath);
590 // We can't create a subdirectory, strange.
591 // But the directory seems to work, so we use it but don't store it.
596 // Reaching this point means that the operating system is configured badly.
600 if (!function_exists('exif_imagetype')) {
601 function exif_imagetype($file)
603 $size = getimagesize($file);
608 function validate_include(&$file)
612 $file = realpath($file);
614 if (strpos($file, getcwd()) !== 0) {
618 $file = str_replace(getcwd() . "/", "", $file, $count);
623 if ($orig_file !== $file) {
628 if (strpos($file, "include/") === 0) {
632 if (strpos($file, "addon/") === 0) {
636 // Simply return flag