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

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