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_POKE', 512);
144 define('NOTIFY_SHARE', 1024);
146 define('NOTIFY_SYSTEM', 32768);
150 /** @deprecated since 2019.03, use Term::UNKNOWN instead */
151 define('TERM_UNKNOWN', Term::UNKNOWN);
152 /** @deprecated since 2019.03, use Term::HASHTAG instead */
153 define('TERM_HASHTAG', Term::HASHTAG);
154 /** @deprecated since 2019.03, use Term::MENTION instead */
155 define('TERM_MENTION', Term::MENTION);
156 /** @deprecated since 2019.03, use Term::CATEGORY instead */
157 define('TERM_CATEGORY', Term::CATEGORY);
158 /** @deprecated since 2019.03, use Term::PCATEGORY instead */
159 define('TERM_PCATEGORY', Term::PCATEGORY);
160 /** @deprecated since 2019.03, use Term::FILE instead */
161 define('TERM_FILE', Term::FILE);
162 /** @deprecated since 2019.03, use Term::SAVEDSEARCH instead */
163 define('TERM_SAVEDSEARCH', Term::SAVEDSEARCH);
164 /** @deprecated since 2019.03, use Term::CONVERSATION instead */
165 define('TERM_CONVERSATION', Term::CONVERSATION);
167 /** @deprecated since 2019.03, use Term::OBJECT_TYPE_POST instead */
168 define('TERM_OBJ_POST', Term::OBJECT_TYPE_POST);
169 /** @deprecated since 2019.03, use Term::OBJECT_TYPE_PHOTO instead */
170 define('TERM_OBJ_PHOTO', Term::OBJECT_TYPE_PHOTO);
175 * Item weight for query ordering
178 define('GRAVITY_PARENT', 0);
179 define('GRAVITY_ACTIVITY', 3);
180 define('GRAVITY_COMMENT', 6);
181 define('GRAVITY_UNKNOWN', 9);
187 * Process priority for the worker
190 define('PRIORITY_UNDEFINED', 0);
191 define('PRIORITY_CRITICAL', 10);
192 define('PRIORITY_HIGH', 20);
193 define('PRIORITY_MEDIUM', 30);
194 define('PRIORITY_LOW', 40);
195 define('PRIORITY_NEGLIGIBLE', 50);
199 * @name Social Relay settings
201 * See here: https://github.com/jaywink/social-relay
202 * and here: https://wiki.diasporafoundation.org/Relay_servers_for_public_posts
205 define('SR_SCOPE_NONE', '');
206 define('SR_SCOPE_ALL', 'all');
207 define('SR_SCOPE_TAGS', 'tags');
210 // Normally this constant is defined - but not if "pcntl" isn't installed
211 if (!defined("SIGTERM")) {
212 define("SIGTERM", 15);
216 * Depending on the PHP version this constant does exist - or not.
217 * See here: http://php.net/manual/en/curl.constants.php#117928
219 if (!defined('CURLE_OPERATION_TIMEDOUT')) {
220 define('CURLE_OPERATION_TIMEDOUT', CURLE_OPERATION_TIMEOUTED);
224 * Returns the user id of locally logged in user or false.
226 * @return int|bool user id or false
228 function local_user()
230 if (!empty($_SESSION['authenticated']) && !empty($_SESSION['uid'])) {
231 return intval($_SESSION['uid']);
237 * Returns the public contact id of logged in user or false.
239 * @return int|bool public contact id or false
241 function public_contact()
243 static $public_contact_id = false;
245 if (!$public_contact_id && !empty($_SESSION['authenticated'])) {
246 if (!empty($_SESSION['my_address'])) {
248 $public_contact_id = intval(Contact::getIdForURL($_SESSION['my_address'], 0, true));
249 } elseif (!empty($_SESSION['visitor_home'])) {
251 $public_contact_id = intval(Contact::getIdForURL($_SESSION['visitor_home'], 0, true));
253 } elseif (empty($_SESSION['authenticated'])) {
254 $public_contact_id = false;
257 return $public_contact_id;
261 * Returns contact id of authenticated site visitor or false
263 * @return int|bool visitor_id or false
265 function remote_user()
267 if (empty($_SESSION['authenticated'])) {
271 if (!empty($_SESSION['visitor_id'])) {
272 return intval($_SESSION['visitor_id']);
279 * Show an error message to user.
281 * This function save text in session, to be shown to the user at next page load
283 * @param string $s - Text of notice
287 if (empty($_SESSION)) {
292 if (empty($_SESSION['sysmsg'])) {
293 $_SESSION['sysmsg'] = [];
295 if ($a->interactive) {
296 $_SESSION['sysmsg'][] = $s;
301 * Show an info 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
311 if (local_user() && DI::pConfig()->get(local_user(), 'system', 'ignore_info')) {
315 if (empty($_SESSION['sysmsg_info'])) {
316 $_SESSION['sysmsg_info'] = [];
318 if ($a->interactive) {
319 $_SESSION['sysmsg_info'][] = $s;
323 function feed_birthday($uid, $tz)
326 * Determine the next birthday, but only if the birthday is published
327 * in the default profile. We _could_ also look for a private profile that the
328 * recipient can see, but somebody could get mad at us if they start getting
329 * public birthday greetings when they haven't made this info public.
331 * Assuming we are able to publish this info, we are then going to convert
332 * the start time from the owner's timezone to UTC.
334 * This will potentially solve the problem found with some social networks
335 * where birthdays are converted to the viewer's timezone and salutations from
336 * elsewhere in the world show up on the wrong day. We will convert it to the
337 * viewer's timezone also, but first we are going to convert it from the birthday
338 * person's timezone to GMT - so the viewer may find the birthday starting at
339 * 6:00PM the day before, but that will correspond to midnight to the birthday person.
347 $profile = DBA::selectFirst('profile', ['dob'], ['uid' => $uid]);
348 if (DBA::isResult($profile)) {
349 $tmp_dob = substr($profile['dob'], 5);
350 if (intval($tmp_dob)) {
351 $y = DateTimeFormat::timezoneNow($tz, 'Y');
352 $bd = $y . '-' . $tmp_dob . ' 00:00';
353 $t_dob = strtotime($bd);
354 $now = strtotime(DateTimeFormat::timezoneNow($tz));
356 $bd = $y + 1 . '-' . $tmp_dob . ' 00:00';
358 $birthday = DateTimeFormat::convert($bd, 'UTC', $tz, DateTimeFormat::ATOM);
366 * Check if current user has admin role.
368 * @return bool true if user is an admin
370 function is_site_admin()
374 $admin_email = DI::config()->get('config', 'admin_email');
376 $adminlist = explode(',', str_replace(' ', '', $admin_email));
378 return local_user() && $admin_email && in_array($a->user['email'] ?? '', $adminlist);
381 function explode_querystring($query)
383 $arg_st = strpos($query, '?');
384 if ($arg_st !== false) {
385 $base = substr($query, 0, $arg_st);
392 $args = explode('&', substr($query, $arg_st));
393 foreach ($args as $k => $arg) {
394 /// @TODO really compare type-safe here?
399 $args = array_values($args);
404 $args = array_values($args);
414 * Returns the complete URL of the current page, e.g.: http(s)://something.com/network
416 * Taken from http://webcheatsheet.com/php/get_current_page_url.php
418 function curPageURL()
421 if (!empty($_SERVER["HTTPS"]) && ($_SERVER["HTTPS"] == "on")) {
427 if ($_SERVER["SERVER_PORT"] != "80" && $_SERVER["SERVER_PORT"] != "443") {
428 $pageURL .= $_SERVER["SERVER_NAME"] . ":" . $_SERVER["SERVER_PORT"] . $_SERVER["REQUEST_URI"];
430 $pageURL .= $_SERVER["SERVER_NAME"] . $_SERVER["REQUEST_URI"];
435 function get_temppath()
437 $temppath = DI::config()->get("system", "temppath");
439 if (($temppath != "") && System::isDirectoryUsable($temppath)) {
440 // We have a temp path and it is usable
441 return BasePath::getRealPath($temppath);
444 // We don't have a working preconfigured temp path, so we take the system path.
445 $temppath = sys_get_temp_dir();
447 // Check if it is usable
448 if (($temppath != "") && System::isDirectoryUsable($temppath)) {
449 // Always store the real path, not the path through symlinks
450 $temppath = BasePath::getRealPath($temppath);
452 // To avoid any interferences with other systems we create our own directory
453 $new_temppath = $temppath . "/" . DI::baseUrl()->getHostname();
454 if (!is_dir($new_temppath)) {
455 /// @TODO There is a mkdir()+chmod() upwards, maybe generalize this (+ configurable) into a function/method?
456 mkdir($new_temppath);
459 if (System::isDirectoryUsable($new_temppath)) {
460 // The new path is usable, we are happy
461 DI::config()->set("system", "temppath", $new_temppath);
462 return $new_temppath;
464 // We can't create a subdirectory, strange.
465 // But the directory seems to work, so we use it but don't store it.
470 // Reaching this point means that the operating system is configured badly.
474 function get_cachefile($file, $writemode = true)
476 $cache = get_itemcachepath();
478 if ((!$cache) || (!is_dir($cache))) {
482 $subfolder = $cache . "/" . substr($file, 0, 2);
484 $cachepath = $subfolder . "/" . $file;
487 if (!is_dir($subfolder)) {
489 chmod($subfolder, 0777);
496 function clear_cache($basepath = "", $path = "")
499 $basepath = get_itemcachepath();
503 if (($path == "") || (!is_dir($path))) {
507 if (substr(realpath($path), 0, strlen($basepath)) != $basepath) {
511 $cachetime = (int) DI::config()->get('system', 'itemcache_duration');
512 if ($cachetime == 0) {
516 if (is_writable($path)) {
517 if ($dh = opendir($path)) {
518 while (($file = readdir($dh)) !== false) {
519 $fullpath = $path . "/" . $file;
520 if ((filetype($fullpath) == "dir") && ($file != ".") && ($file != "..")) {
521 clear_cache($basepath, $fullpath);
523 if ((filetype($fullpath) == "file") && (filectime($fullpath) < (time() - $cachetime))) {
532 function get_itemcachepath()
534 // Checking, if the cache is deactivated
535 $cachetime = (int) DI::config()->get('system', 'itemcache_duration');
536 if ($cachetime < 0) {
540 $itemcache = DI::config()->get('system', 'itemcache');
541 if (($itemcache != "") && System::isDirectoryUsable($itemcache)) {
542 return BasePath::getRealPath($itemcache);
545 $temppath = get_temppath();
547 if ($temppath != "") {
548 $itemcache = $temppath . "/itemcache";
549 if (!file_exists($itemcache) && !is_dir($itemcache)) {
553 if (System::isDirectoryUsable($itemcache)) {
554 DI::config()->set("system", "itemcache", $itemcache);
562 * Returns the path where spool files are stored
564 * @return string Spool path
566 function get_spoolpath()
568 $spoolpath = DI::config()->get('system', 'spoolpath');
569 if (($spoolpath != "") && System::isDirectoryUsable($spoolpath)) {
570 // We have a spool path and it is usable
574 // We don't have a working preconfigured spool path, so we take the temp path.
575 $temppath = get_temppath();
577 if ($temppath != "") {
578 // To avoid any interferences with other systems we create our own directory
579 $spoolpath = $temppath . "/spool";
580 if (!is_dir($spoolpath)) {
584 if (System::isDirectoryUsable($spoolpath)) {
585 // The new path is usable, we are happy
586 DI::config()->set("system", "spoolpath", $spoolpath);
589 // We can't create a subdirectory, strange.
590 // But the directory seems to work, so we use it but don't store it.
595 // Reaching this point means that the operating system is configured badly.
599 if (!function_exists('exif_imagetype')) {
600 function exif_imagetype($file)
602 $size = getimagesize($file);
607 function validate_include(&$file)
611 $file = realpath($file);
613 if (strpos($file, getcwd()) !== 0) {
617 $file = str_replace(getcwd() . "/", "", $file, $count);
622 if ($orig_file !== $file) {
627 if (strpos($file, "include/") === 0) {
631 if (strpos($file, "addon/") === 0) {
635 // Simply return flag