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.

871 lines
22KB

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