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.
 
 
 
 
 
 

822 lines
22 KiB

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