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\BaseObject;
22 use Friendica\Core\Config;
23 use Friendica\Core\PConfig;
24 use Friendica\Core\Protocol;
25 use Friendica\Core\System;
26 use Friendica\Core\Session;
27 use Friendica\Database\DBA;
28 use Friendica\Model\Contact;
29 use Friendica\Model\Term;
30 use Friendica\Util\BasePath;
31 use Friendica\Util\DateTimeFormat;
33 define('FRIENDICA_PLATFORM', 'Friendica');
34 define('FRIENDICA_CODENAME', 'Dalmatian Bellflower');
35 define('FRIENDICA_VERSION', '2020.03-dev');
36 define('DFRN_PROTOCOL_VERSION', '2.23');
37 define('NEW_UPDATE_ROUTINE_VERSION', 1170);
40 * @brief Constant with a HTML line break.
42 * Contains a HTML line break (br) element and a real carriage return with line
43 * feed for the source.
44 * This can be used in HTML and JavaScript where needed a line break.
46 define('EOL', "<br />\r\n");
49 * @brief Image storage quality.
51 * Lower numbers save space at cost of image detail.
52 * For ease of upgrade, please do not change here. Set system.jpegquality = n in config/local.config.php,
53 * where n is between 1 and 100, and with very poor results below about 50
55 define('JPEG_QUALITY', 100);
58 * system.png_quality = n where is between 0 (uncompressed) to 9
60 define('PNG_QUALITY', 8);
63 * An alternate way of limiting picture upload sizes. Specify the maximum pixel
64 * length that pictures are allowed to be (for non-square pictures, it will apply
65 * to the longest side). Pictures longer than this length will be resized to be
66 * this length (on the longest side, the other side will be scaled appropriately).
67 * Modify this value using
70 * 'max_image_length' => 'n',
74 * in config/local.config.php
76 * If you don't want to set a maximum length, set to -1. The default value is
77 * defined by 'MAX_IMAGE_LENGTH' below.
79 define('MAX_IMAGE_LENGTH', -1);
84 define('DEFAULT_DB_ENGINE', 'InnoDB');
86 /** @deprecated since version 2019.03, please use \Friendica\Module\Register::CLOSED instead */
87 define('REGISTER_CLOSED', \Friendica\Module\Register::CLOSED);
88 /** @deprecated since version 2019.03, please use \Friendica\Module\Register::APPROVE instead */
89 define('REGISTER_APPROVE', \Friendica\Module\Register::APPROVE);
90 /** @deprecated since version 2019.03, please use \Friendica\Module\Register::OPEN instead */
91 define('REGISTER_OPEN', \Friendica\Module\Register::OPEN);
96 * Type of the community page
99 define('CP_NO_INTERNAL_COMMUNITY', -2);
100 define('CP_NO_COMMUNITY_PAGE', -1);
101 define('CP_USERS_ON_SERVER', 0);
102 define('CP_GLOBAL_COMMUNITY', 1);
103 define('CP_USERS_AND_GLOBAL', 2);
109 * These numbers are used in stored permissions
110 * and existing allocations MUST NEVER BE CHANGED
111 * OR RE-ASSIGNED! You may only add to them.
114 Protocol::DFRN => (-1),
115 Protocol::ZOT => (-2),
116 Protocol::OSTATUS => (-3),
117 Protocol::FEED => (-4),
118 Protocol::DIASPORA => (-5),
119 Protocol::MAIL => (-6),
120 Protocol::FACEBOOK => (-8),
121 Protocol::LINKEDIN => (-9),
122 Protocol::XMPP => (-10),
123 Protocol::MYSPACE => (-11),
124 Protocol::GPLUS => (-12),
125 Protocol::PUMPIO => (-13),
126 Protocol::TWITTER => (-14),
127 Protocol::DIASPORA2 => (-15),
128 Protocol::STATUSNET => (-16),
129 Protocol::NEWS => (-18),
130 Protocol::ICALENDAR => (-19),
131 Protocol::PNUT => (-20),
133 Protocol::PHANTOM => (-127),
137 * Maximum number of "people who like (or don't like) this" that we will list by name
139 define('MAX_LIKERS', 75);
144 * Email notification options
147 define('NOTIFY_INTRO', 1);
148 define('NOTIFY_CONFIRM', 2);
149 define('NOTIFY_WALL', 4);
150 define('NOTIFY_COMMENT', 8);
151 define('NOTIFY_MAIL', 16);
152 define('NOTIFY_SUGGEST', 32);
153 define('NOTIFY_PROFILE', 64);
154 define('NOTIFY_TAGSELF', 128);
155 define('NOTIFY_TAGSHARE', 256);
156 define('NOTIFY_POKE', 512);
157 define('NOTIFY_SHARE', 1024);
159 define('SYSTEM_EMAIL', 16384);
161 define('NOTIFY_SYSTEM', 32768);
165 /** @deprecated since 2019.03, use Term::UNKNOWN instead */
166 define('TERM_UNKNOWN', Term::UNKNOWN);
167 /** @deprecated since 2019.03, use Term::HASHTAG instead */
168 define('TERM_HASHTAG', Term::HASHTAG);
169 /** @deprecated since 2019.03, use Term::MENTION instead */
170 define('TERM_MENTION', Term::MENTION);
171 /** @deprecated since 2019.03, use Term::CATEGORY instead */
172 define('TERM_CATEGORY', Term::CATEGORY);
173 /** @deprecated since 2019.03, use Term::PCATEGORY instead */
174 define('TERM_PCATEGORY', Term::PCATEGORY);
175 /** @deprecated since 2019.03, use Term::FILE instead */
176 define('TERM_FILE', Term::FILE);
177 /** @deprecated since 2019.03, use Term::SAVEDSEARCH instead */
178 define('TERM_SAVEDSEARCH', Term::SAVEDSEARCH);
179 /** @deprecated since 2019.03, use Term::CONVERSATION instead */
180 define('TERM_CONVERSATION', Term::CONVERSATION);
182 /** @deprecated since 2019.03, use Term::OBJECT_TYPE_POST instead */
183 define('TERM_OBJ_POST', Term::OBJECT_TYPE_POST);
184 /** @deprecated since 2019.03, use Term::OBJECT_TYPE_PHOTO instead */
185 define('TERM_OBJ_PHOTO', Term::OBJECT_TYPE_PHOTO);
190 * Item weight for query ordering
193 define('GRAVITY_PARENT', 0);
194 define('GRAVITY_ACTIVITY', 3);
195 define('GRAVITY_COMMENT', 6);
196 define('GRAVITY_UNKNOWN', 9);
202 * Process priority for the worker
205 define('PRIORITY_UNDEFINED', 0);
206 define('PRIORITY_CRITICAL', 10);
207 define('PRIORITY_HIGH', 20);
208 define('PRIORITY_MEDIUM', 30);
209 define('PRIORITY_LOW', 40);
210 define('PRIORITY_NEGLIGIBLE', 50);
214 * @name Social Relay settings
216 * See here: https://github.com/jaywink/social-relay
217 * and here: https://wiki.diasporafoundation.org/Relay_servers_for_public_posts
220 define('SR_SCOPE_NONE', '');
221 define('SR_SCOPE_ALL', 'all');
222 define('SR_SCOPE_TAGS', 'tags');
225 // Normally this constant is defined - but not if "pcntl" isn't installed
226 if (!defined("SIGTERM")) {
227 define("SIGTERM", 15);
231 * Depending on the PHP version this constant does exist - or not.
232 * See here: http://php.net/manual/en/curl.constants.php#117928
234 if (!defined('CURLE_OPERATION_TIMEDOUT')) {
235 define('CURLE_OPERATION_TIMEDOUT', CURLE_OPERATION_TIMEOUTED);
239 * @brief Retrieve the App structure
241 * Useful in functions which require it but don't get it passed to them
243 * @deprecated since version 2018.09
244 * @see BaseObject::getApp()
249 return BaseObject::getApp();
253 * @brief Used to end the current process, after saving session state.
262 * @brief Returns the user id of locally logged in user or false.
264 * @return int|bool user id or false
266 function local_user()
268 if (!empty($_SESSION['authenticated']) && !empty($_SESSION['uid'])) {
269 return intval($_SESSION['uid']);
275 * @brief Returns the public contact id of logged in user or false.
277 * @return int|bool public contact id or false
279 function public_contact()
281 static $public_contact_id = false;
283 if (!$public_contact_id && !empty($_SESSION['authenticated'])) {
284 if (!empty($_SESSION['my_address'])) {
286 $public_contact_id = intval(Contact::getIdForURL($_SESSION['my_address'], 0, true));
287 } elseif (!empty($_SESSION['visitor_home'])) {
289 $public_contact_id = intval(Contact::getIdForURL($_SESSION['visitor_home'], 0, true));
291 } elseif (empty($_SESSION['authenticated'])) {
292 $public_contact_id = false;
295 return $public_contact_id;
299 * @brief Returns contact id of authenticated site visitor or false
301 * @return int|bool visitor_id or false
303 function remote_user()
305 if (empty($_SESSION['authenticated'])) {
309 if (!empty($_SESSION['visitor_id'])) {
310 return intval($_SESSION['visitor_id']);
317 * @brief Show an error message to user.
319 * This function save text in session, to be shown to the user at next page load
321 * @param string $s - Text of notice
325 if (empty($_SESSION)) {
330 if (empty($_SESSION['sysmsg'])) {
331 $_SESSION['sysmsg'] = [];
333 if ($a->interactive) {
334 $_SESSION['sysmsg'][] = $s;
339 * @brief Show an info message to user.
341 * This function save text in session, to be shown to the user at next page load
343 * @param string $s - Text of notice
349 if (local_user() && PConfig::get(local_user(), 'system', 'ignore_info')) {
353 if (empty($_SESSION['sysmsg_info'])) {
354 $_SESSION['sysmsg_info'] = [];
356 if ($a->interactive) {
357 $_SESSION['sysmsg_info'][] = $s;
361 function feed_birthday($uid, $tz)
364 * Determine the next birthday, but only if the birthday is published
365 * in the default profile. We _could_ also look for a private profile that the
366 * recipient can see, but somebody could get mad at us if they start getting
367 * public birthday greetings when they haven't made this info public.
369 * Assuming we are able to publish this info, we are then going to convert
370 * the start time from the owner's timezone to UTC.
372 * This will potentially solve the problem found with some social networks
373 * where birthdays are converted to the viewer's timezone and salutations from
374 * elsewhere in the world show up on the wrong day. We will convert it to the
375 * viewer's timezone also, but first we are going to convert it from the birthday
376 * person's timezone to GMT - so the viewer may find the birthday starting at
377 * 6:00PM the day before, but that will correspond to midnight to the birthday person.
385 $profile = DBA::selectFirst('profile', ['dob'], ['is-default' => true, 'uid' => $uid]);
386 if (DBA::isResult($profile)) {
387 $tmp_dob = substr($profile['dob'], 5);
388 if (intval($tmp_dob)) {
389 $y = DateTimeFormat::timezoneNow($tz, 'Y');
390 $bd = $y . '-' . $tmp_dob . ' 00:00';
391 $t_dob = strtotime($bd);
392 $now = strtotime(DateTimeFormat::timezoneNow($tz));
394 $bd = $y + 1 . '-' . $tmp_dob . ' 00:00';
396 $birthday = DateTimeFormat::convert($bd, 'UTC', $tz, DateTimeFormat::ATOM);
404 * @brief Check if current user has admin role.
406 * @return bool true if user is an admin
408 function is_site_admin()
412 $admin_email = Config::get('config', 'admin_email');
414 $adminlist = explode(',', str_replace(' ', '', $admin_email));
416 return local_user() && $admin_email && in_array($a->user['email'] ?? '', $adminlist);
419 function explode_querystring($query)
421 $arg_st = strpos($query, '?');
422 if ($arg_st !== false) {
423 $base = substr($query, 0, $arg_st);
430 $args = explode('&', substr($query, $arg_st));
431 foreach ($args as $k => $arg) {
432 /// @TODO really compare type-safe here?
437 $args = array_values($args);
442 $args = array_values($args);
452 * Returns the complete URL of the current page, e.g.: http(s)://something.com/network
454 * Taken from http://webcheatsheet.com/php/get_current_page_url.php
456 function curPageURL()
459 if (!empty($_SERVER["HTTPS"]) && ($_SERVER["HTTPS"] == "on")) {
465 if ($_SERVER["SERVER_PORT"] != "80" && $_SERVER["SERVER_PORT"] != "443") {
466 $pageURL .= $_SERVER["SERVER_NAME"] . ":" . $_SERVER["SERVER_PORT"] . $_SERVER["REQUEST_URI"];
468 $pageURL .= $_SERVER["SERVER_NAME"] . $_SERVER["REQUEST_URI"];
473 function get_server()
475 $server = Config::get("system", "directory");
478 $server = "https://dir.friendica.social";
484 function get_temppath()
488 $temppath = Config::get("system", "temppath");
490 if (($temppath != "") && System::isDirectoryUsable($temppath)) {
491 // We have a temp path and it is usable
492 return BasePath::getRealPath($temppath);
495 // We don't have a working preconfigured temp path, so we take the system path.
496 $temppath = sys_get_temp_dir();
498 // Check if it is usable
499 if (($temppath != "") && System::isDirectoryUsable($temppath)) {
500 // Always store the real path, not the path through symlinks
501 $temppath = BasePath::getRealPath($temppath);
503 // To avoid any interferences with other systems we create our own directory
504 $new_temppath = $temppath . "/" . $a->getHostName();
505 if (!is_dir($new_temppath)) {
506 /// @TODO There is a mkdir()+chmod() upwards, maybe generalize this (+ configurable) into a function/method?
507 mkdir($new_temppath);
510 if (System::isDirectoryUsable($new_temppath)) {
511 // The new path is usable, we are happy
512 Config::set("system", "temppath", $new_temppath);
513 return $new_temppath;
515 // We can't create a subdirectory, strange.
516 // But the directory seems to work, so we use it but don't store it.
521 // Reaching this point means that the operating system is configured badly.
525 function get_cachefile($file, $writemode = true)
527 $cache = get_itemcachepath();
529 if ((!$cache) || (!is_dir($cache))) {
533 $subfolder = $cache . "/" . substr($file, 0, 2);
535 $cachepath = $subfolder . "/" . $file;
538 if (!is_dir($subfolder)) {
540 chmod($subfolder, 0777);
547 function clear_cache($basepath = "", $path = "")
550 $basepath = get_itemcachepath();
554 if (($path == "") || (!is_dir($path))) {
558 if (substr(realpath($path), 0, strlen($basepath)) != $basepath) {
562 $cachetime = (int) Config::get('system', 'itemcache_duration');
563 if ($cachetime == 0) {
567 if (is_writable($path)) {
568 if ($dh = opendir($path)) {
569 while (($file = readdir($dh)) !== false) {
570 $fullpath = $path . "/" . $file;
571 if ((filetype($fullpath) == "dir") && ($file != ".") && ($file != "..")) {
572 clear_cache($basepath, $fullpath);
574 if ((filetype($fullpath) == "file") && (filectime($fullpath) < (time() - $cachetime))) {
583 function get_itemcachepath()
585 // Checking, if the cache is deactivated
586 $cachetime = (int) Config::get('system', 'itemcache_duration');
587 if ($cachetime < 0) {
591 $itemcache = Config::get('system', 'itemcache');
592 if (($itemcache != "") && System::isDirectoryUsable($itemcache)) {
593 return BasePath::getRealPath($itemcache);
596 $temppath = get_temppath();
598 if ($temppath != "") {
599 $itemcache = $temppath . "/itemcache";
600 if (!file_exists($itemcache) && !is_dir($itemcache)) {
604 if (System::isDirectoryUsable($itemcache)) {
605 Config::set("system", "itemcache", $itemcache);
613 * @brief Returns the path where spool files are stored
615 * @return string Spool path
617 function get_spoolpath()
619 $spoolpath = Config::get('system', 'spoolpath');
620 if (($spoolpath != "") && System::isDirectoryUsable($spoolpath)) {
621 // We have a spool path and it is usable
625 // We don't have a working preconfigured spool path, so we take the temp path.
626 $temppath = get_temppath();
628 if ($temppath != "") {
629 // To avoid any interferences with other systems we create our own directory
630 $spoolpath = $temppath . "/spool";
631 if (!is_dir($spoolpath)) {
635 if (System::isDirectoryUsable($spoolpath)) {
636 // The new path is usable, we are happy
637 Config::set("system", "spoolpath", $spoolpath);
640 // We can't create a subdirectory, strange.
641 // But the directory seems to work, so we use it but don't store it.
646 // Reaching this point means that the operating system is configured badly.
650 if (!function_exists('exif_imagetype')) {
651 function exif_imagetype($file)
653 $size = getimagesize($file);
658 function validate_include(&$file)
662 $file = realpath($file);
664 if (strpos($file, getcwd()) !== 0) {
668 $file = str_replace(getcwd() . "/", "", $file, $count);
673 if ($orig_file !== $file) {
678 if (strpos($file, "include/") === 0) {
682 if (strpos($file, "addon/") === 0) {
686 // Simply return flag
691 * PHP 5 compatible dirname() with count parameter
693 * @see http://php.net/manual/en/function.dirname.php#113193
695 * @deprecated with PHP 7
696 * @param string $path
700 function rdirname($path, $levels = 1)
703 return dirname(rdirname($path, --$levels));
705 return dirname($path);