Friendica Communications Platform (please note that this is a clone of the repository at github, issues are handled there)
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.

boot.php 23KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826
  1. <?php
  2. /**
  3. * @file boot.php
  4. * This file defines some global constants and includes the central App class.
  5. */
  6. /**
  7. * Friendica
  8. *
  9. * Friendica is a communications platform for integrated social communications
  10. * utilising decentralised communications and linkage to several indie social
  11. * projects - as well as popular mainstream providers.
  12. *
  13. * Our mission is to free our friends and families from the clutches of
  14. * data-harvesting corporations, and pave the way to a future where social
  15. * communications are free and open and flow between alternate providers as
  16. * easily as email does today.
  17. */
  18. use Friendica\App;
  19. use Friendica\BaseObject;
  20. use Friendica\Core\Config;
  21. use Friendica\Core\PConfig;
  22. use Friendica\Core\Protocol;
  23. use Friendica\Core\System;
  24. use Friendica\Database\DBA;
  25. use Friendica\Model\Contact;
  26. use Friendica\Model\Term;
  27. use Friendica\Util\BasePath;
  28. use Friendica\Util\DateTimeFormat;
  29. define('FRIENDICA_PLATFORM', 'Friendica');
  30. define('FRIENDICA_CODENAME', 'Dalmatian Bellflower');
  31. define('FRIENDICA_VERSION', '2019.09-dev');
  32. define('DFRN_PROTOCOL_VERSION', '2.23');
  33. define('NEW_UPDATE_ROUTINE_VERSION', 1170);
  34. /**
  35. * @brief Constant with a HTML line break.
  36. *
  37. * Contains a HTML line break (br) element and a real carriage return with line
  38. * feed for the source.
  39. * This can be used in HTML and JavaScript where needed a line break.
  40. */
  41. define('EOL', "<br />\r\n");
  42. /**
  43. * @brief Image storage quality.
  44. *
  45. * Lower numbers save space at cost of image detail.
  46. * For ease of upgrade, please do not change here. Set system.jpegquality = n in config/local.config.php,
  47. * where n is between 1 and 100, and with very poor results below about 50
  48. */
  49. define('JPEG_QUALITY', 100);
  50. /**
  51. * system.png_quality = n where is between 0 (uncompressed) to 9
  52. */
  53. define('PNG_QUALITY', 8);
  54. /**
  55. * An alternate way of limiting picture upload sizes. Specify the maximum pixel
  56. * length that pictures are allowed to be (for non-square pictures, it will apply
  57. * to the longest side). Pictures longer than this length will be resized to be
  58. * this length (on the longest side, the other side will be scaled appropriately).
  59. * Modify this value using
  60. *
  61. * 'system' => [
  62. * 'max_image_length' => 'n',
  63. * ...
  64. * ],
  65. *
  66. * in config/local.config.php
  67. *
  68. * If you don't want to set a maximum length, set to -1. The default value is
  69. * defined by 'MAX_IMAGE_LENGTH' below.
  70. */
  71. define('MAX_IMAGE_LENGTH', -1);
  72. /**
  73. * Not yet used
  74. */
  75. define('DEFAULT_DB_ENGINE', 'InnoDB');
  76. /** @deprecated since version 2019.03, please use \Friendica\Module\Register::CLOSED instead */
  77. define('REGISTER_CLOSED', \Friendica\Module\Register::CLOSED);
  78. /** @deprecated since version 2019.03, please use \Friendica\Module\Register::APPROVE instead */
  79. define('REGISTER_APPROVE', \Friendica\Module\Register::APPROVE);
  80. /** @deprecated since version 2019.03, please use \Friendica\Module\Register::OPEN instead */
  81. define('REGISTER_OPEN', \Friendica\Module\Register::OPEN);
  82. /**
  83. * @name CP
  84. *
  85. * Type of the community page
  86. * @{
  87. */
  88. define('CP_NO_INTERNAL_COMMUNITY', -2);
  89. define('CP_NO_COMMUNITY_PAGE', -1);
  90. define('CP_USERS_ON_SERVER', 0);
  91. define('CP_GLOBAL_COMMUNITY', 1);
  92. define('CP_USERS_AND_GLOBAL', 2);
  93. /**
  94. * @}
  95. */
  96. /**
  97. * These numbers are used in stored permissions
  98. * and existing allocations MUST NEVER BE CHANGED
  99. * OR RE-ASSIGNED! You may only add to them.
  100. */
  101. $netgroup_ids = [
  102. Protocol::DFRN => (-1),
  103. Protocol::ZOT => (-2),
  104. Protocol::OSTATUS => (-3),
  105. Protocol::FEED => (-4),
  106. Protocol::DIASPORA => (-5),
  107. Protocol::MAIL => (-6),
  108. Protocol::FACEBOOK => (-8),
  109. Protocol::LINKEDIN => (-9),
  110. Protocol::XMPP => (-10),
  111. Protocol::MYSPACE => (-11),
  112. Protocol::GPLUS => (-12),
  113. Protocol::PUMPIO => (-13),
  114. Protocol::TWITTER => (-14),
  115. Protocol::DIASPORA2 => (-15),
  116. Protocol::STATUSNET => (-16),
  117. Protocol::NEWS => (-18),
  118. Protocol::ICALENDAR => (-19),
  119. Protocol::PNUT => (-20),
  120. Protocol::PHANTOM => (-127),
  121. ];
  122. /**
  123. * Maximum number of "people who like (or don't like) this" that we will list by name
  124. */
  125. define('MAX_LIKERS', 75);
  126. /**
  127. * @name Notify
  128. *
  129. * Email notification options
  130. * @{
  131. */
  132. define('NOTIFY_INTRO', 1);
  133. define('NOTIFY_CONFIRM', 2);
  134. define('NOTIFY_WALL', 4);
  135. define('NOTIFY_COMMENT', 8);
  136. define('NOTIFY_MAIL', 16);
  137. define('NOTIFY_SUGGEST', 32);
  138. define('NOTIFY_PROFILE', 64);
  139. define('NOTIFY_TAGSELF', 128);
  140. define('NOTIFY_TAGSHARE', 256);
  141. define('NOTIFY_POKE', 512);
  142. define('NOTIFY_SHARE', 1024);
  143. define('SYSTEM_EMAIL', 16384);
  144. define('NOTIFY_SYSTEM', 32768);
  145. /* @}*/
  146. /** @deprecated since 2019.03, use Term::UNKNOWN instead */
  147. define('TERM_UNKNOWN', Term::UNKNOWN);
  148. /** @deprecated since 2019.03, use Term::HASHTAG instead */
  149. define('TERM_HASHTAG', Term::HASHTAG);
  150. /** @deprecated since 2019.03, use Term::MENTION instead */
  151. define('TERM_MENTION', Term::MENTION);
  152. /** @deprecated since 2019.03, use Term::CATEGORY instead */
  153. define('TERM_CATEGORY', Term::CATEGORY);
  154. /** @deprecated since 2019.03, use Term::PCATEGORY instead */
  155. define('TERM_PCATEGORY', Term::PCATEGORY);
  156. /** @deprecated since 2019.03, use Term::FILE instead */
  157. define('TERM_FILE', Term::FILE);
  158. /** @deprecated since 2019.03, use Term::SAVEDSEARCH instead */
  159. define('TERM_SAVEDSEARCH', Term::SAVEDSEARCH);
  160. /** @deprecated since 2019.03, use Term::CONVERSATION instead */
  161. define('TERM_CONVERSATION', Term::CONVERSATION);
  162. /** @deprecated since 2019.03, use Term::OBJECT_TYPE_POST instead */
  163. define('TERM_OBJ_POST', Term::OBJECT_TYPE_POST);
  164. /** @deprecated since 2019.03, use Term::OBJECT_TYPE_PHOTO instead */
  165. define('TERM_OBJ_PHOTO', Term::OBJECT_TYPE_PHOTO);
  166. /**
  167. * @name Namespaces
  168. *
  169. * Various namespaces we may need to parse
  170. * @{
  171. */
  172. define('NAMESPACE_ZOT', 'http://purl.org/zot');
  173. define('NAMESPACE_DFRN', 'http://purl.org/macgirvin/dfrn/1.0');
  174. define('NAMESPACE_THREAD', 'http://purl.org/syndication/thread/1.0');
  175. define('NAMESPACE_TOMB', 'http://purl.org/atompub/tombstones/1.0');
  176. define('NAMESPACE_ACTIVITY2', 'https://www.w3.org/ns/activitystreams#');
  177. define('NAMESPACE_ACTIVITY', 'http://activitystrea.ms/spec/1.0/');
  178. define('NAMESPACE_ACTIVITY_SCHEMA', 'http://activitystrea.ms/schema/1.0/');
  179. define('NAMESPACE_MEDIA', 'http://purl.org/syndication/atommedia');
  180. define('NAMESPACE_SALMON_ME', 'http://salmon-protocol.org/ns/magic-env');
  181. define('NAMESPACE_OSTATUSSUB', 'http://ostatus.org/schema/1.0/subscribe');
  182. define('NAMESPACE_GEORSS', 'http://www.georss.org/georss');
  183. define('NAMESPACE_POCO', 'http://portablecontacts.net/spec/1.0');
  184. define('NAMESPACE_FEED', 'http://schemas.google.com/g/2010#updates-from');
  185. define('NAMESPACE_OSTATUS', 'http://ostatus.org/schema/1.0');
  186. define('NAMESPACE_STATUSNET', 'http://status.net/schema/api/1/');
  187. define('NAMESPACE_ATOM1', 'http://www.w3.org/2005/Atom');
  188. define('NAMESPACE_MASTODON', 'http://mastodon.social/schema/1.0');
  189. /* @}*/
  190. /**
  191. * @name Activity
  192. *
  193. * Activity stream defines
  194. * @{
  195. */
  196. define('ACTIVITY_LIKE', NAMESPACE_ACTIVITY_SCHEMA . 'like');
  197. define('ACTIVITY_DISLIKE', NAMESPACE_DFRN . '/dislike');
  198. define('ACTIVITY_ATTEND', NAMESPACE_ZOT . '/activity/attendyes');
  199. define('ACTIVITY_ATTENDNO', NAMESPACE_ZOT . '/activity/attendno');
  200. define('ACTIVITY_ATTENDMAYBE', NAMESPACE_ZOT . '/activity/attendmaybe');
  201. define('ACTIVITY_OBJ_HEART', NAMESPACE_DFRN . '/heart');
  202. define('ACTIVITY_FRIEND', NAMESPACE_ACTIVITY_SCHEMA . 'make-friend');
  203. define('ACTIVITY_REQ_FRIEND', NAMESPACE_ACTIVITY_SCHEMA . 'request-friend');
  204. define('ACTIVITY_UNFRIEND', NAMESPACE_ACTIVITY_SCHEMA . 'remove-friend');
  205. define('ACTIVITY_FOLLOW', NAMESPACE_ACTIVITY_SCHEMA . 'follow');
  206. define('ACTIVITY_UNFOLLOW', NAMESPACE_ACTIVITY_SCHEMA . 'stop-following');
  207. define('ACTIVITY_JOIN', NAMESPACE_ACTIVITY_SCHEMA . 'join');
  208. define('ACTIVITY_POST', NAMESPACE_ACTIVITY_SCHEMA . 'post');
  209. define('ACTIVITY_UPDATE', NAMESPACE_ACTIVITY_SCHEMA . 'update');
  210. define('ACTIVITY_TAG', NAMESPACE_ACTIVITY_SCHEMA . 'tag');
  211. define('ACTIVITY_FAVORITE', NAMESPACE_ACTIVITY_SCHEMA . 'favorite');
  212. define('ACTIVITY_UNFAVORITE', NAMESPACE_ACTIVITY_SCHEMA . 'unfavorite');
  213. define('ACTIVITY_SHARE', NAMESPACE_ACTIVITY_SCHEMA . 'share');
  214. define('ACTIVITY_DELETE', NAMESPACE_ACTIVITY_SCHEMA . 'delete');
  215. define('ACTIVITY2_ANNOUNCE', NAMESPACE_ACTIVITY2 . 'Announce');
  216. define('ACTIVITY_POKE', NAMESPACE_ZOT . '/activity/poke');
  217. define('ACTIVITY_OBJ_BOOKMARK', NAMESPACE_ACTIVITY_SCHEMA . 'bookmark');
  218. define('ACTIVITY_OBJ_COMMENT', NAMESPACE_ACTIVITY_SCHEMA . 'comment');
  219. define('ACTIVITY_OBJ_NOTE', NAMESPACE_ACTIVITY_SCHEMA . 'note');
  220. define('ACTIVITY_OBJ_PERSON', NAMESPACE_ACTIVITY_SCHEMA . 'person');
  221. define('ACTIVITY_OBJ_IMAGE', NAMESPACE_ACTIVITY_SCHEMA . 'image');
  222. define('ACTIVITY_OBJ_PHOTO', NAMESPACE_ACTIVITY_SCHEMA . 'photo');
  223. define('ACTIVITY_OBJ_VIDEO', NAMESPACE_ACTIVITY_SCHEMA . 'video');
  224. define('ACTIVITY_OBJ_P_PHOTO', NAMESPACE_ACTIVITY_SCHEMA . 'profile-photo');
  225. define('ACTIVITY_OBJ_ALBUM', NAMESPACE_ACTIVITY_SCHEMA . 'photo-album');
  226. define('ACTIVITY_OBJ_EVENT', NAMESPACE_ACTIVITY_SCHEMA . 'event');
  227. define('ACTIVITY_OBJ_GROUP', NAMESPACE_ACTIVITY_SCHEMA . 'group');
  228. define('ACTIVITY_OBJ_TAGTERM', NAMESPACE_DFRN . '/tagterm');
  229. define('ACTIVITY_OBJ_PROFILE', NAMESPACE_DFRN . '/profile');
  230. define('ACTIVITY_OBJ_QUESTION', 'http://activityschema.org/object/question');
  231. /* @}*/
  232. /**
  233. * @name Gravity
  234. *
  235. * Item weight for query ordering
  236. * @{
  237. */
  238. define('GRAVITY_PARENT', 0);
  239. define('GRAVITY_ACTIVITY', 3);
  240. define('GRAVITY_COMMENT', 6);
  241. define('GRAVITY_UNKNOWN', 9);
  242. /* @}*/
  243. /**
  244. * @name Priority
  245. *
  246. * Process priority for the worker
  247. * @{
  248. */
  249. define('PRIORITY_UNDEFINED', 0);
  250. define('PRIORITY_CRITICAL', 10);
  251. define('PRIORITY_HIGH', 20);
  252. define('PRIORITY_MEDIUM', 30);
  253. define('PRIORITY_LOW', 40);
  254. define('PRIORITY_NEGLIGIBLE', 50);
  255. /* @}*/
  256. /**
  257. * @name Social Relay settings
  258. *
  259. * See here: https://github.com/jaywink/social-relay
  260. * and here: https://wiki.diasporafoundation.org/Relay_servers_for_public_posts
  261. * @{
  262. */
  263. define('SR_SCOPE_NONE', '');
  264. define('SR_SCOPE_ALL', 'all');
  265. define('SR_SCOPE_TAGS', 'tags');
  266. /* @}*/
  267. // Normally this constant is defined - but not if "pcntl" isn't installed
  268. if (!defined("SIGTERM")) {
  269. define("SIGTERM", 15);
  270. }
  271. /**
  272. * Depending on the PHP version this constant does exist - or not.
  273. * See here: http://php.net/manual/en/curl.constants.php#117928
  274. */
  275. if (!defined('CURLE_OPERATION_TIMEDOUT')) {
  276. define('CURLE_OPERATION_TIMEDOUT', CURLE_OPERATION_TIMEOUTED);
  277. }
  278. /**
  279. * @brief Retrieve the App structure
  280. *
  281. * Useful in functions which require it but don't get it passed to them
  282. *
  283. * @deprecated since version 2018.09
  284. * @see BaseObject::getApp()
  285. * @return App
  286. */
  287. function get_app()
  288. {
  289. return BaseObject::getApp();
  290. }
  291. /**
  292. * Return the provided variable value if it exists and is truthy or the provided
  293. * default value instead.
  294. *
  295. * Works with initialized variables and potentially uninitialized array keys
  296. *
  297. * Usages:
  298. * - defaults($var, $default)
  299. * - defaults($array, 'key', $default)
  300. *
  301. * @param array $args
  302. * @brief Returns a defaut value if the provided variable or array key is falsy
  303. * @return mixed
  304. * @deprecated since version 2019.06, use native coalesce operator (??) instead
  305. */
  306. function defaults(...$args)
  307. {
  308. if (count($args) < 2) {
  309. throw new BadFunctionCallException('defaults() requires at least 2 parameters');
  310. }
  311. if (count($args) > 3) {
  312. throw new BadFunctionCallException('defaults() cannot use more than 3 parameters');
  313. }
  314. if (count($args) === 3 && is_null($args[1])) {
  315. throw new BadFunctionCallException('defaults($arr, $key, $def) $key is null');
  316. }
  317. // The default value always is the last argument
  318. $return = array_pop($args);
  319. if (count($args) == 2 && is_array($args[0]) && !empty($args[0][$args[1]])) {
  320. $return = $args[0][$args[1]];
  321. }
  322. if (count($args) == 1 && !empty($args[0])) {
  323. $return = $args[0];
  324. }
  325. return $return;
  326. }
  327. /**
  328. * @brief Used to end the current process, after saving session state.
  329. * @deprecated
  330. */
  331. function killme()
  332. {
  333. exit();
  334. }
  335. /**
  336. * @brief Returns the user id of locally logged in user or false.
  337. *
  338. * @return int|bool user id or false
  339. */
  340. function local_user()
  341. {
  342. if (!empty($_SESSION['authenticated']) && !empty($_SESSION['uid'])) {
  343. return intval($_SESSION['uid']);
  344. }
  345. return false;
  346. }
  347. /**
  348. * @brief Returns the public contact id of logged in user or false.
  349. *
  350. * @return int|bool public contact id or false
  351. */
  352. function public_contact()
  353. {
  354. static $public_contact_id = false;
  355. if (!$public_contact_id && !empty($_SESSION['authenticated'])) {
  356. if (!empty($_SESSION['my_address'])) {
  357. // Local user
  358. $public_contact_id = intval(Contact::getIdForURL($_SESSION['my_address'], 0, true));
  359. } elseif (!empty($_SESSION['visitor_home'])) {
  360. // Remote user
  361. $public_contact_id = intval(Contact::getIdForURL($_SESSION['visitor_home'], 0, true));
  362. }
  363. } elseif (empty($_SESSION['authenticated'])) {
  364. $public_contact_id = false;
  365. }
  366. return $public_contact_id;
  367. }
  368. /**
  369. * @brief Returns contact id of authenticated site visitor or false
  370. *
  371. * @return int|bool visitor_id or false
  372. */
  373. function remote_user()
  374. {
  375. // You cannot be both local and remote.
  376. // Unncommented by rabuzarus because remote authentication to local
  377. // profiles wasn't possible anymore (2018-04-12).
  378. // if (local_user()) {
  379. // return false;
  380. // }
  381. if (empty($_SESSION)) {
  382. return false;
  383. }
  384. if (!empty($_SESSION['authenticated']) && !empty($_SESSION['visitor_id'])) {
  385. return intval($_SESSION['visitor_id']);
  386. }
  387. return false;
  388. }
  389. /**
  390. * @brief Show an error message to user.
  391. *
  392. * This function save text in session, to be shown to the user at next page load
  393. *
  394. * @param string $s - Text of notice
  395. */
  396. function notice($s)
  397. {
  398. if (empty($_SESSION)) {
  399. return;
  400. }
  401. $a = \get_app();
  402. if (empty($_SESSION['sysmsg'])) {
  403. $_SESSION['sysmsg'] = [];
  404. }
  405. if ($a->interactive) {
  406. $_SESSION['sysmsg'][] = $s;
  407. }
  408. }
  409. /**
  410. * @brief Show an info message to user.
  411. *
  412. * This function save text in session, to be shown to the user at next page load
  413. *
  414. * @param string $s - Text of notice
  415. */
  416. function info($s)
  417. {
  418. $a = \get_app();
  419. if (local_user() && PConfig::get(local_user(), 'system', 'ignore_info')) {
  420. return;
  421. }
  422. if (empty($_SESSION['sysmsg_info'])) {
  423. $_SESSION['sysmsg_info'] = [];
  424. }
  425. if ($a->interactive) {
  426. $_SESSION['sysmsg_info'][] = $s;
  427. }
  428. }
  429. function feed_birthday($uid, $tz)
  430. {
  431. /**
  432. * Determine the next birthday, but only if the birthday is published
  433. * in the default profile. We _could_ also look for a private profile that the
  434. * recipient can see, but somebody could get mad at us if they start getting
  435. * public birthday greetings when they haven't made this info public.
  436. *
  437. * Assuming we are able to publish this info, we are then going to convert
  438. * the start time from the owner's timezone to UTC.
  439. *
  440. * This will potentially solve the problem found with some social networks
  441. * where birthdays are converted to the viewer's timezone and salutations from
  442. * elsewhere in the world show up on the wrong day. We will convert it to the
  443. * viewer's timezone also, but first we are going to convert it from the birthday
  444. * person's timezone to GMT - so the viewer may find the birthday starting at
  445. * 6:00PM the day before, but that will correspond to midnight to the birthday person.
  446. */
  447. $birthday = '';
  448. if (!strlen($tz)) {
  449. $tz = 'UTC';
  450. }
  451. $profile = DBA::selectFirst('profile', ['dob'], ['is-default' => true, 'uid' => $uid]);
  452. if (DBA::isResult($profile)) {
  453. $tmp_dob = substr($profile['dob'], 5);
  454. if (intval($tmp_dob)) {
  455. $y = DateTimeFormat::timezoneNow($tz, 'Y');
  456. $bd = $y . '-' . $tmp_dob . ' 00:00';
  457. $t_dob = strtotime($bd);
  458. $now = strtotime(DateTimeFormat::timezoneNow($tz));
  459. if ($t_dob < $now) {
  460. $bd = $y + 1 . '-' . $tmp_dob . ' 00:00';
  461. }
  462. $birthday = DateTimeFormat::convert($bd, 'UTC', $tz, DateTimeFormat::ATOM);
  463. }
  464. }
  465. return $birthday;
  466. }
  467. /**
  468. * @brief Check if current user has admin role.
  469. *
  470. * @return bool true if user is an admin
  471. */
  472. function is_site_admin()
  473. {
  474. $a = \get_app();
  475. $admin_email = Config::get('config', 'admin_email');
  476. $adminlist = explode(',', str_replace(' ', '', $admin_email));
  477. return local_user() && $admin_email && in_array(defaults($a->user, 'email', ''), $adminlist);
  478. }
  479. function explode_querystring($query)
  480. {
  481. $arg_st = strpos($query, '?');
  482. if ($arg_st !== false) {
  483. $base = substr($query, 0, $arg_st);
  484. $arg_st += 1;
  485. } else {
  486. $base = '';
  487. $arg_st = 0;
  488. }
  489. $args = explode('&', substr($query, $arg_st));
  490. foreach ($args as $k => $arg) {
  491. /// @TODO really compare type-safe here?
  492. if ($arg === '') {
  493. unset($args[$k]);
  494. }
  495. }
  496. $args = array_values($args);
  497. if (!$base) {
  498. $base = $args[0];
  499. unset($args[0]);
  500. $args = array_values($args);
  501. }
  502. return [
  503. 'base' => $base,
  504. 'args' => $args,
  505. ];
  506. }
  507. /**
  508. * Returns the complete URL of the current page, e.g.: http(s)://something.com/network
  509. *
  510. * Taken from http://webcheatsheet.com/php/get_current_page_url.php
  511. */
  512. function curPageURL()
  513. {
  514. $pageURL = 'http';
  515. if (!empty($_SERVER["HTTPS"]) && ($_SERVER["HTTPS"] == "on")) {
  516. $pageURL .= "s";
  517. }
  518. $pageURL .= "://";
  519. if ($_SERVER["SERVER_PORT"] != "80" && $_SERVER["SERVER_PORT"] != "443") {
  520. $pageURL .= $_SERVER["SERVER_NAME"] . ":" . $_SERVER["SERVER_PORT"] . $_SERVER["REQUEST_URI"];
  521. } else {
  522. $pageURL .= $_SERVER["SERVER_NAME"] . $_SERVER["REQUEST_URI"];
  523. }
  524. return $pageURL;
  525. }
  526. function get_server()
  527. {
  528. $server = Config::get("system", "directory");
  529. if ($server == "") {
  530. $server = "https://dir.friendica.social";
  531. }
  532. return $server;
  533. }
  534. function get_temppath()
  535. {
  536. $a = \get_app();
  537. $temppath = Config::get("system", "temppath");
  538. if (($temppath != "") && System::isDirectoryUsable($temppath)) {
  539. // We have a temp path and it is usable
  540. return BasePath::getRealPath($temppath);
  541. }
  542. // We don't have a working preconfigured temp path, so we take the system path.
  543. $temppath = sys_get_temp_dir();
  544. // Check if it is usable
  545. if (($temppath != "") && System::isDirectoryUsable($temppath)) {
  546. // Always store the real path, not the path through symlinks
  547. $temppath = BasePath::getRealPath($temppath);
  548. // To avoid any interferences with other systems we create our own directory
  549. $new_temppath = $temppath . "/" . $a->getHostName();
  550. if (!is_dir($new_temppath)) {
  551. /// @TODO There is a mkdir()+chmod() upwards, maybe generalize this (+ configurable) into a function/method?
  552. mkdir($new_temppath);
  553. }
  554. if (System::isDirectoryUsable($new_temppath)) {
  555. // The new path is usable, we are happy
  556. Config::set("system", "temppath", $new_temppath);
  557. return $new_temppath;
  558. } else {
  559. // We can't create a subdirectory, strange.
  560. // But the directory seems to work, so we use it but don't store it.
  561. return $temppath;
  562. }
  563. }
  564. // Reaching this point means that the operating system is configured badly.
  565. return '';
  566. }
  567. function get_cachefile($file, $writemode = true)
  568. {
  569. $cache = get_itemcachepath();
  570. if ((!$cache) || (!is_dir($cache))) {
  571. return "";
  572. }
  573. $subfolder = $cache . "/" . substr($file, 0, 2);
  574. $cachepath = $subfolder . "/" . $file;
  575. if ($writemode) {
  576. if (!is_dir($subfolder)) {
  577. mkdir($subfolder);
  578. chmod($subfolder, 0777);
  579. }
  580. }
  581. return $cachepath;
  582. }
  583. function clear_cache($basepath = "", $path = "")
  584. {
  585. if ($path == "") {
  586. $basepath = get_itemcachepath();
  587. $path = $basepath;
  588. }
  589. if (($path == "") || (!is_dir($path))) {
  590. return;
  591. }
  592. if (substr(realpath($path), 0, strlen($basepath)) != $basepath) {
  593. return;
  594. }
  595. $cachetime = (int) Config::get('system', 'itemcache_duration');
  596. if ($cachetime == 0) {
  597. $cachetime = 86400;
  598. }
  599. if (is_writable($path)) {
  600. if ($dh = opendir($path)) {
  601. while (($file = readdir($dh)) !== false) {
  602. $fullpath = $path . "/" . $file;
  603. if ((filetype($fullpath) == "dir") && ($file != ".") && ($file != "..")) {
  604. clear_cache($basepath, $fullpath);
  605. }
  606. if ((filetype($fullpath) == "file") && (filectime($fullpath) < (time() - $cachetime))) {
  607. unlink($fullpath);
  608. }
  609. }
  610. closedir($dh);
  611. }
  612. }
  613. }
  614. function get_itemcachepath()
  615. {
  616. // Checking, if the cache is deactivated
  617. $cachetime = (int) Config::get('system', 'itemcache_duration');
  618. if ($cachetime < 0) {
  619. return "";
  620. }
  621. $itemcache = Config::get('system', 'itemcache');
  622. if (($itemcache != "") && System::isDirectoryUsable($itemcache)) {
  623. return BasePath::getRealPath($itemcache);
  624. }
  625. $temppath = get_temppath();
  626. if ($temppath != "") {
  627. $itemcache = $temppath . "/itemcache";
  628. if (!file_exists($itemcache) && !is_dir($itemcache)) {
  629. mkdir($itemcache);
  630. }
  631. if (System::isDirectoryUsable($itemcache)) {
  632. Config::set("system", "itemcache", $itemcache);
  633. return $itemcache;
  634. }
  635. }
  636. return "";
  637. }
  638. /**
  639. * @brief Returns the path where spool files are stored
  640. *
  641. * @return string Spool path
  642. */
  643. function get_spoolpath()
  644. {
  645. $spoolpath = Config::get('system', 'spoolpath');
  646. if (($spoolpath != "") && System::isDirectoryUsable($spoolpath)) {
  647. // We have a spool path and it is usable
  648. return $spoolpath;
  649. }
  650. // We don't have a working preconfigured spool path, so we take the temp path.
  651. $temppath = get_temppath();
  652. if ($temppath != "") {
  653. // To avoid any interferences with other systems we create our own directory
  654. $spoolpath = $temppath . "/spool";
  655. if (!is_dir($spoolpath)) {
  656. mkdir($spoolpath);
  657. }
  658. if (System::isDirectoryUsable($spoolpath)) {
  659. // The new path is usable, we are happy
  660. Config::set("system", "spoolpath", $spoolpath);
  661. return $spoolpath;
  662. } else {
  663. // We can't create a subdirectory, strange.
  664. // But the directory seems to work, so we use it but don't store it.
  665. return $temppath;
  666. }
  667. }
  668. // Reaching this point means that the operating system is configured badly.
  669. return "";
  670. }
  671. if (!function_exists('exif_imagetype')) {
  672. function exif_imagetype($file)
  673. {
  674. $size = getimagesize($file);
  675. return $size[2];
  676. }
  677. }
  678. function validate_include(&$file)
  679. {
  680. $orig_file = $file;
  681. $file = realpath($file);
  682. if (strpos($file, getcwd()) !== 0) {
  683. return false;
  684. }
  685. $file = str_replace(getcwd() . "/", "", $file, $count);
  686. if ($count != 1) {
  687. return false;
  688. }
  689. if ($orig_file !== $file) {
  690. return false;
  691. }
  692. $valid = false;
  693. if (strpos($file, "include/") === 0) {
  694. $valid = true;
  695. }
  696. if (strpos($file, "addon/") === 0) {
  697. $valid = true;
  698. }
  699. // Simply return flag
  700. return $valid;
  701. }
  702. /**
  703. * PHP 5 compatible dirname() with count parameter
  704. *
  705. * @see http://php.net/manual/en/function.dirname.php#113193
  706. *
  707. * @deprecated with PHP 7
  708. * @param string $path
  709. * @param int $levels
  710. * @return string
  711. */
  712. function rdirname($path, $levels = 1)
  713. {
  714. if ($levels > 1) {
  715. return dirname(rdirname($path, --$levels));
  716. } else {
  717. return dirname($path);
  718. }
  719. }