Friendica Communications Platform (please note that this is a clone of the repository at github, issues are handled there) https://friendi.ca
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

522 lines
15 KiB

11 years ago
7 months ago
11 years ago
10 years ago
11 years ago
11 years ago
11 years ago
11 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
  1. <?php
  2. /**
  3. * @copyright Copyright (C) 2010-2021, the Friendica project
  4. *
  5. * @license GNU AGPL version 3 or any later version
  6. *
  7. * This program is free software: you can redistribute it and/or modify
  8. * it under the terms of the GNU Affero General Public License as
  9. * published by the Free Software Foundation, either version 3 of the
  10. * License, or (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU Affero General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Affero General Public License
  18. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  19. *
  20. * Friendica is a communications platform for integrated social communications
  21. * utilising decentralised communications and linkage to several indie social
  22. * projects - as well as popular mainstream providers.
  23. *
  24. * Our mission is to free our friends and families from the clutches of
  25. * data-harvesting corporations, and pave the way to a future where social
  26. * communications are free and open and flow between alternate providers as
  27. * easily as email does today.
  28. */
  29. use Friendica\Core\Protocol;
  30. use Friendica\Core\System;
  31. use Friendica\Database\DBA;
  32. use Friendica\DI;
  33. use Friendica\Model\Contact;
  34. use Friendica\Model\Notification;
  35. use Friendica\Util\BasePath;
  36. use Friendica\Util\DateTimeFormat;
  37. define('FRIENDICA_PLATFORM', 'Friendica');
  38. define('FRIENDICA_CODENAME', 'Siberian Iris');
  39. define('FRIENDICA_VERSION', '2021.12-dev');
  40. define('DFRN_PROTOCOL_VERSION', '2.23');
  41. define('NEW_TABLE_STRUCTURE_VERSION', 1288);
  42. /**
  43. * Constant with a HTML line break.
  44. *
  45. * Contains a HTML line break (br) element and a real carriage return with line
  46. * feed for the source.
  47. * This can be used in HTML and JavaScript where needed a line break.
  48. */
  49. define('EOL', "<br />\r\n");
  50. /**
  51. * Image storage quality.
  52. *
  53. * Lower numbers save space at cost of image detail.
  54. * For ease of upgrade, please do not change here. Set system.jpegquality = n in config/local.config.php,
  55. * where n is between 1 and 100, and with very poor results below about 50
  56. */
  57. define('JPEG_QUALITY', 100);
  58. /**
  59. * system.png_quality = n where is between 0 (uncompressed) to 9
  60. */
  61. define('PNG_QUALITY', 8);
  62. /**
  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
  68. *
  69. * 'system' => [
  70. * 'max_image_length' => 'n',
  71. * ...
  72. * ],
  73. *
  74. * in config/local.config.php
  75. *
  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.
  78. */
  79. define('MAX_IMAGE_LENGTH', -1);
  80. /**
  81. * Not yet used
  82. */
  83. define('DEFAULT_DB_ENGINE', 'InnoDB');
  84. /** @deprecated since version 2019.03, please use \Friendica\Module\Register::CLOSED instead */
  85. define('REGISTER_CLOSED', \Friendica\Module\Register::CLOSED);
  86. /** @deprecated since version 2019.03, please use \Friendica\Module\Register::APPROVE instead */
  87. define('REGISTER_APPROVE', \Friendica\Module\Register::APPROVE);
  88. /** @deprecated since version 2019.03, please use \Friendica\Module\Register::OPEN instead */
  89. define('REGISTER_OPEN', \Friendica\Module\Register::OPEN);
  90. /**
  91. * @name CP
  92. *
  93. * Type of the community page
  94. * @{
  95. */
  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);
  101. /**
  102. * @}
  103. */
  104. /**
  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.
  108. */
  109. $netgroup_ids = [
  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),
  128. Protocol::PHANTOM => (-127),
  129. ];
  130. /**
  131. * Maximum number of "people who like (or don't like) this" that we will list by name
  132. */
  133. define('MAX_LIKERS', 75);
  134. /**
  135. * @name Notification
  136. *
  137. * Email notification options
  138. * @{
  139. */
  140. /** @deprecated since 2020.03, use Notification\Type::INTRO instead */
  141. define('NOTIFY_INTRO', Notification\Type::INTRO);
  142. /** @deprecated since 2020.03, use Notification\Type::CONFIRM instead */
  143. define('NOTIFY_CONFIRM', Notification\Type::CONFIRM);
  144. /** @deprecated since 2020.03, use Notification\Type::WALL instead */
  145. define('NOTIFY_WALL', Notification\Type::WALL);
  146. /** @deprecated since 2020.03, use Notification\Type::COMMENT instead */
  147. define('NOTIFY_COMMENT', Notification\Type::COMMENT);
  148. /** @deprecated since 2020.03, use Notification\Type::MAIL instead */
  149. define('NOTIFY_MAIL', Notification\Type::MAIL);
  150. /** @deprecated since 2020.03, use Notification\Type::SUGGEST instead */
  151. define('NOTIFY_SUGGEST', Notification\Type::SUGGEST);
  152. /** @deprecated since 2020.03, use Notification\Type::PROFILE instead */
  153. define('NOTIFY_PROFILE', Notification\Type::PROFILE);
  154. /** @deprecated since 2020.03, use Notification\Type::TAG_SELF instead */
  155. define('NOTIFY_TAGSELF', Notification\Type::TAG_SELF);
  156. /** @deprecated since 2020.03, use Notification\Type::TAG_SHARE instead */
  157. define('NOTIFY_TAGSHARE', Notification\Type::TAG_SHARE);
  158. /** @deprecated since 2020.03, use Notification\Type::POKE instead */
  159. define('NOTIFY_POKE', Notification\Type::POKE);
  160. /** @deprecated since 2020.03, use Notification\Type::SHARE instead */
  161. define('NOTIFY_SHARE', Notification\Type::SHARE);
  162. /** @deprecated since 2020.12, use Notification\Type::SYSTEM instead */
  163. define('NOTIFY_SYSTEM', Notification\Type::SYSTEM);
  164. /* @}*/
  165. /**
  166. * @name Gravity
  167. *
  168. * Item weight for query ordering
  169. * @{
  170. */
  171. define('GRAVITY_PARENT', 0);
  172. define('GRAVITY_ACTIVITY', 3);
  173. define('GRAVITY_COMMENT', 6);
  174. define('GRAVITY_UNKNOWN', 9);
  175. /* @}*/
  176. /**
  177. * @name Priority
  178. *
  179. * Process priority for the worker
  180. * @{
  181. */
  182. define('PRIORITY_UNDEFINED', 0);
  183. define('PRIORITY_CRITICAL', 10);
  184. define('PRIORITY_HIGH', 20);
  185. define('PRIORITY_MEDIUM', 30);
  186. define('PRIORITY_LOW', 40);
  187. define('PRIORITY_NEGLIGIBLE', 50);
  188. define('PRIORITIES', [PRIORITY_CRITICAL, PRIORITY_HIGH, PRIORITY_MEDIUM, PRIORITY_LOW, PRIORITY_NEGLIGIBLE]);
  189. /* @}*/
  190. /**
  191. * @name Social Relay settings
  192. *
  193. * See here: https://github.com/jaywink/social-relay
  194. * and here: https://wiki.diasporafoundation.org/Relay_servers_for_public_posts
  195. * @{
  196. */
  197. define('SR_SCOPE_NONE', '');
  198. define('SR_SCOPE_ALL', 'all');
  199. define('SR_SCOPE_TAGS', 'tags');
  200. /* @}*/
  201. // Normally this constant is defined - but not if "pcntl" isn't installed
  202. if (!defined("SIGTERM")) {
  203. define("SIGTERM", 15);
  204. }
  205. /**
  206. * Depending on the PHP version this constant does exist - or not.
  207. * See here: http://php.net/manual/en/curl.constants.php#117928
  208. */
  209. if (!defined('CURLE_OPERATION_TIMEDOUT')) {
  210. define('CURLE_OPERATION_TIMEDOUT', CURLE_OPERATION_TIMEOUTED);
  211. }
  212. /**
  213. * Returns the user id of locally logged in user or false.
  214. *
  215. * @return int|bool user id or false
  216. */
  217. function local_user()
  218. {
  219. if (!empty($_SESSION['authenticated']) && !empty($_SESSION['uid'])) {
  220. return intval($_SESSION['uid']);
  221. }
  222. return false;
  223. }
  224. /**
  225. * Returns the public contact id of logged in user or false.
  226. *
  227. * @return int|bool public contact id or false
  228. */
  229. function public_contact()
  230. {
  231. static $public_contact_id = false;
  232. if (!$public_contact_id && !empty($_SESSION['authenticated'])) {
  233. if (!empty($_SESSION['my_address'])) {
  234. // Local user
  235. $public_contact_id = intval(Contact::getIdForURL($_SESSION['my_address'], 0, false));
  236. } elseif (!empty($_SESSION['visitor_home'])) {
  237. // Remote user
  238. $public_contact_id = intval(Contact::getIdForURL($_SESSION['visitor_home'], 0, false));
  239. }
  240. } elseif (empty($_SESSION['authenticated'])) {
  241. $public_contact_id = false;
  242. }
  243. return $public_contact_id;
  244. }
  245. /**
  246. * Returns public contact id of authenticated site visitor or false
  247. *
  248. * @return int|bool visitor_id or false
  249. */
  250. function remote_user()
  251. {
  252. if (empty($_SESSION['authenticated'])) {
  253. return false;
  254. }
  255. if (!empty($_SESSION['visitor_id'])) {
  256. return intval($_SESSION['visitor_id']);
  257. }
  258. return false;
  259. }
  260. /**
  261. * Show an error message to user.
  262. *
  263. * This function save text in session, to be shown to the user at next page load
  264. *
  265. * @param string $s - Text of notice
  266. */
  267. function notice($s)
  268. {
  269. if (empty($_SESSION)) {
  270. return;
  271. }
  272. if (empty($_SESSION['sysmsg'])) {
  273. $_SESSION['sysmsg'] = [];
  274. }
  275. $_SESSION['sysmsg'][] = $s;
  276. }
  277. /**
  278. * Show an info message to user.
  279. *
  280. * This function save text in session, to be shown to the user at next page load
  281. *
  282. * @param string $s - Text of notice
  283. */
  284. function info($s)
  285. {
  286. if (empty($_SESSION)) {
  287. return;
  288. }
  289. if (empty($_SESSION['sysmsg_info'])) {
  290. $_SESSION['sysmsg_info'] = [];
  291. }
  292. $_SESSION['sysmsg_info'][] = $s;
  293. }
  294. function feed_birthday($uid, $tz)
  295. {
  296. /**
  297. * Determine the next birthday, but only if the birthday is published
  298. * in the default profile. We _could_ also look for a private profile that the
  299. * recipient can see, but somebody could get mad at us if they start getting
  300. * public birthday greetings when they haven't made this info public.
  301. *
  302. * Assuming we are able to publish this info, we are then going to convert
  303. * the start time from the owner's timezone to UTC.
  304. *
  305. * This will potentially solve the problem found with some social networks
  306. * where birthdays are converted to the viewer's timezone and salutations from
  307. * elsewhere in the world show up on the wrong day. We will convert it to the
  308. * viewer's timezone also, but first we are going to convert it from the birthday
  309. * person's timezone to GMT - so the viewer may find the birthday starting at
  310. * 6:00PM the day before, but that will correspond to midnight to the birthday person.
  311. */
  312. $birthday = '';
  313. if (!strlen($tz)) {
  314. $tz = 'UTC';
  315. }
  316. $profile = DBA::selectFirst('profile', ['dob'], ['uid' => $uid]);
  317. if (DBA::isResult($profile)) {
  318. $tmp_dob = substr($profile['dob'], 5);
  319. if (intval($tmp_dob)) {
  320. $y = DateTimeFormat::timezoneNow($tz, 'Y');
  321. $bd = $y . '-' . $tmp_dob . ' 00:00';
  322. $t_dob = strtotime($bd);
  323. $now = strtotime(DateTimeFormat::timezoneNow($tz));
  324. if ($t_dob < $now) {
  325. $bd = $y + 1 . '-' . $tmp_dob . ' 00:00';
  326. }
  327. $birthday = DateTimeFormat::convert($bd, 'UTC', $tz, DateTimeFormat::ATOM);
  328. }
  329. }
  330. return $birthday;
  331. }
  332. /**
  333. * Check if current user has admin role.
  334. *
  335. * @return bool true if user is an admin
  336. */
  337. function is_site_admin()
  338. {
  339. $a = DI::app();
  340. $admin_email = DI::config()->get('config', 'admin_email');
  341. $adminlist = explode(',', str_replace(' ', '', $admin_email));
  342. return local_user() && $admin_email && DBA::exists('user', ['uid' => $a->getLoggedInUserId(), 'email' => $adminlist]);
  343. }
  344. /**
  345. * Returns the complete URL of the current page, e.g.: http(s)://something.com/network
  346. *
  347. * Taken from http://webcheatsheet.com/php/get_current_page_url.php
  348. */
  349. function curPageURL()
  350. {
  351. $pageURL = 'http';
  352. if (!empty($_SERVER["HTTPS"]) && ($_SERVER["HTTPS"] == "on")) {
  353. $pageURL .= "s";
  354. }
  355. $pageURL .= "://";
  356. if ($_SERVER["SERVER_PORT"] != "80" && $_SERVER["SERVER_PORT"] != "443") {
  357. $pageURL .= $_SERVER["SERVER_NAME"] . ":" . $_SERVER["SERVER_PORT"] . $_SERVER["REQUEST_URI"];
  358. } else {
  359. $pageURL .= $_SERVER["SERVER_NAME"] . $_SERVER["REQUEST_URI"];
  360. }
  361. return $pageURL;
  362. }
  363. function get_temppath()
  364. {
  365. $temppath = DI::config()->get("system", "temppath");
  366. if (($temppath != "") && System::isDirectoryUsable($temppath)) {
  367. // We have a temp path and it is usable
  368. return BasePath::getRealPath($temppath);
  369. }
  370. // We don't have a working preconfigured temp path, so we take the system path.
  371. $temppath = sys_get_temp_dir();
  372. // Check if it is usable
  373. if (($temppath != "") && System::isDirectoryUsable($temppath)) {
  374. // Always store the real path, not the path through symlinks
  375. $temppath = BasePath::getRealPath($temppath);
  376. // To avoid any interferences with other systems we create our own directory
  377. $new_temppath = $temppath . "/" . DI::baseUrl()->getHostname();
  378. if (!is_dir($new_temppath)) {
  379. /// @TODO There is a mkdir()+chmod() upwards, maybe generalize this (+ configurable) into a function/method?
  380. mkdir($new_temppath);
  381. }
  382. if (System::isDirectoryUsable($new_temppath)) {
  383. // The new path is usable, we are happy
  384. DI::config()->set("system", "temppath", $new_temppath);
  385. return $new_temppath;
  386. } else {
  387. // We can't create a subdirectory, strange.
  388. // But the directory seems to work, so we use it but don't store it.
  389. return $temppath;
  390. }
  391. }
  392. // Reaching this point means that the operating system is configured badly.
  393. return '';
  394. }
  395. /**
  396. * Returns the path where spool files are stored
  397. *
  398. * @return string Spool path
  399. */
  400. function get_spoolpath()
  401. {
  402. $spoolpath = DI::config()->get('system', 'spoolpath');
  403. if (($spoolpath != "") && System::isDirectoryUsable($spoolpath)) {
  404. // We have a spool path and it is usable
  405. return $spoolpath;
  406. }
  407. // We don't have a working preconfigured spool path, so we take the temp path.
  408. $temppath = get_temppath();
  409. if ($temppath != "") {
  410. // To avoid any interferences with other systems we create our own directory
  411. $spoolpath = $temppath . "/spool";
  412. if (!is_dir($spoolpath)) {
  413. mkdir($spoolpath);
  414. }
  415. if (System::isDirectoryUsable($spoolpath)) {
  416. // The new path is usable, we are happy
  417. DI::config()->set("system", "spoolpath", $spoolpath);
  418. return $spoolpath;
  419. } else {
  420. // We can't create a subdirectory, strange.
  421. // But the directory seems to work, so we use it but don't store it.
  422. return $temppath;
  423. }
  424. }
  425. // Reaching this point means that the operating system is configured badly.
  426. return "";
  427. }
  428. if (!function_exists('exif_imagetype')) {
  429. function exif_imagetype($file)
  430. {
  431. $size = getimagesize($file);
  432. return $size[2];
  433. }
  434. }
  435. function validate_include(&$file)
  436. {
  437. $orig_file = $file;
  438. $file = realpath($file);
  439. if (strpos($file, getcwd()) !== 0) {
  440. return false;
  441. }
  442. $file = str_replace(getcwd() . "/", "", $file, $count);
  443. if ($count != 1) {
  444. return false;
  445. }
  446. if ($orig_file !== $file) {
  447. return false;
  448. }
  449. $valid = false;
  450. if (strpos($file, "include/") === 0) {
  451. $valid = true;
  452. }
  453. if (strpos($file, "addon/") === 0) {
  454. $valid = true;
  455. }
  456. // Simply return flag
  457. return $valid;
  458. }