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.
 
 
 
 
 
 

781 lines
21 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. * @brief Used to end the current process, after saving session state.
  294. * @deprecated
  295. */
  296. function killme()
  297. {
  298. exit();
  299. }
  300. /**
  301. * @brief Returns the user id of locally logged in user or false.
  302. *
  303. * @return int|bool user id or false
  304. */
  305. function local_user()
  306. {
  307. if (!empty($_SESSION['authenticated']) && !empty($_SESSION['uid'])) {
  308. return intval($_SESSION['uid']);
  309. }
  310. return false;
  311. }
  312. /**
  313. * @brief Returns the public contact id of logged in user or false.
  314. *
  315. * @return int|bool public contact id or false
  316. */
  317. function public_contact()
  318. {
  319. static $public_contact_id = false;
  320. if (!$public_contact_id && !empty($_SESSION['authenticated'])) {
  321. if (!empty($_SESSION['my_address'])) {
  322. // Local user
  323. $public_contact_id = intval(Contact::getIdForURL($_SESSION['my_address'], 0, true));
  324. } elseif (!empty($_SESSION['visitor_home'])) {
  325. // Remote user
  326. $public_contact_id = intval(Contact::getIdForURL($_SESSION['visitor_home'], 0, true));
  327. }
  328. } elseif (empty($_SESSION['authenticated'])) {
  329. $public_contact_id = false;
  330. }
  331. return $public_contact_id;
  332. }
  333. /**
  334. * @brief Returns contact id of authenticated site visitor or false
  335. *
  336. * @return int|bool visitor_id or false
  337. */
  338. function remote_user()
  339. {
  340. if (empty($_SESSION['authenticated'])) {
  341. return false;
  342. }
  343. if (!empty($_SESSION['visitor_id'])) {
  344. return intval($_SESSION['visitor_id']);
  345. }
  346. return false;
  347. }
  348. /**
  349. * @brief Show an error message to user.
  350. *
  351. * This function save text in session, to be shown to the user at next page load
  352. *
  353. * @param string $s - Text of notice
  354. */
  355. function notice($s)
  356. {
  357. if (empty($_SESSION)) {
  358. return;
  359. }
  360. $a = \get_app();
  361. if (empty($_SESSION['sysmsg'])) {
  362. $_SESSION['sysmsg'] = [];
  363. }
  364. if ($a->interactive) {
  365. $_SESSION['sysmsg'][] = $s;
  366. }
  367. }
  368. /**
  369. * @brief Show an info message to user.
  370. *
  371. * This function save text in session, to be shown to the user at next page load
  372. *
  373. * @param string $s - Text of notice
  374. */
  375. function info($s)
  376. {
  377. $a = \get_app();
  378. if (local_user() && PConfig::get(local_user(), 'system', 'ignore_info')) {
  379. return;
  380. }
  381. if (empty($_SESSION['sysmsg_info'])) {
  382. $_SESSION['sysmsg_info'] = [];
  383. }
  384. if ($a->interactive) {
  385. $_SESSION['sysmsg_info'][] = $s;
  386. }
  387. }
  388. function feed_birthday($uid, $tz)
  389. {
  390. /**
  391. * Determine the next birthday, but only if the birthday is published
  392. * in the default profile. We _could_ also look for a private profile that the
  393. * recipient can see, but somebody could get mad at us if they start getting
  394. * public birthday greetings when they haven't made this info public.
  395. *
  396. * Assuming we are able to publish this info, we are then going to convert
  397. * the start time from the owner's timezone to UTC.
  398. *
  399. * This will potentially solve the problem found with some social networks
  400. * where birthdays are converted to the viewer's timezone and salutations from
  401. * elsewhere in the world show up on the wrong day. We will convert it to the
  402. * viewer's timezone also, but first we are going to convert it from the birthday
  403. * person's timezone to GMT - so the viewer may find the birthday starting at
  404. * 6:00PM the day before, but that will correspond to midnight to the birthday person.
  405. */
  406. $birthday = '';
  407. if (!strlen($tz)) {
  408. $tz = 'UTC';
  409. }
  410. $profile = DBA::selectFirst('profile', ['dob'], ['is-default' => true, 'uid' => $uid]);
  411. if (DBA::isResult($profile)) {
  412. $tmp_dob = substr($profile['dob'], 5);
  413. if (intval($tmp_dob)) {
  414. $y = DateTimeFormat::timezoneNow($tz, 'Y');
  415. $bd = $y . '-' . $tmp_dob . ' 00:00';
  416. $t_dob = strtotime($bd);
  417. $now = strtotime(DateTimeFormat::timezoneNow($tz));
  418. if ($t_dob < $now) {
  419. $bd = $y + 1 . '-' . $tmp_dob . ' 00:00';
  420. }
  421. $birthday = DateTimeFormat::convert($bd, 'UTC', $tz, DateTimeFormat::ATOM);
  422. }
  423. }
  424. return $birthday;
  425. }
  426. /**
  427. * @brief Check if current user has admin role.
  428. *
  429. * @return bool true if user is an admin
  430. */
  431. function is_site_admin()
  432. {
  433. $a = \get_app();
  434. $admin_email = Config::get('config', 'admin_email');
  435. $adminlist = explode(',', str_replace(' ', '', $admin_email));
  436. return local_user() && $admin_email && in_array($a->user['email'] ?? '', $adminlist);
  437. }
  438. function explode_querystring($query)
  439. {
  440. $arg_st = strpos($query, '?');
  441. if ($arg_st !== false) {
  442. $base = substr($query, 0, $arg_st);
  443. $arg_st += 1;
  444. } else {
  445. $base = '';
  446. $arg_st = 0;
  447. }
  448. $args = explode('&', substr($query, $arg_st));
  449. foreach ($args as $k => $arg) {
  450. /// @TODO really compare type-safe here?
  451. if ($arg === '') {
  452. unset($args[$k]);
  453. }
  454. }
  455. $args = array_values($args);
  456. if (!$base) {
  457. $base = $args[0];
  458. unset($args[0]);
  459. $args = array_values($args);
  460. }
  461. return [
  462. 'base' => $base,
  463. 'args' => $args,
  464. ];
  465. }
  466. /**
  467. * Returns the complete URL of the current page, e.g.: http(s)://something.com/network
  468. *
  469. * Taken from http://webcheatsheet.com/php/get_current_page_url.php
  470. */
  471. function curPageURL()
  472. {
  473. $pageURL = 'http';
  474. if (!empty($_SERVER["HTTPS"]) && ($_SERVER["HTTPS"] == "on")) {
  475. $pageURL .= "s";
  476. }
  477. $pageURL .= "://";
  478. if ($_SERVER["SERVER_PORT"] != "80" && $_SERVER["SERVER_PORT"] != "443") {
  479. $pageURL .= $_SERVER["SERVER_NAME"] . ":" . $_SERVER["SERVER_PORT"] . $_SERVER["REQUEST_URI"];
  480. } else {
  481. $pageURL .= $_SERVER["SERVER_NAME"] . $_SERVER["REQUEST_URI"];
  482. }
  483. return $pageURL;
  484. }
  485. function get_server()
  486. {
  487. $server = Config::get("system", "directory");
  488. if ($server == "") {
  489. $server = "https://dir.friendica.social";
  490. }
  491. return $server;
  492. }
  493. function get_temppath()
  494. {
  495. $a = \get_app();
  496. $temppath = Config::get("system", "temppath");
  497. if (($temppath != "") && System::isDirectoryUsable($temppath)) {
  498. // We have a temp path and it is usable
  499. return BasePath::getRealPath($temppath);
  500. }
  501. // We don't have a working preconfigured temp path, so we take the system path.
  502. $temppath = sys_get_temp_dir();
  503. // Check if it is usable
  504. if (($temppath != "") && System::isDirectoryUsable($temppath)) {
  505. // Always store the real path, not the path through symlinks
  506. $temppath = BasePath::getRealPath($temppath);
  507. // To avoid any interferences with other systems we create our own directory
  508. $new_temppath = $temppath . "/" . $a->getHostName();
  509. if (!is_dir($new_temppath)) {
  510. /// @TODO There is a mkdir()+chmod() upwards, maybe generalize this (+ configurable) into a function/method?
  511. mkdir($new_temppath);
  512. }
  513. if (System::isDirectoryUsable($new_temppath)) {
  514. // The new path is usable, we are happy
  515. Config::set("system", "temppath", $new_temppath);
  516. return $new_temppath;
  517. } else {
  518. // We can't create a subdirectory, strange.
  519. // But the directory seems to work, so we use it but don't store it.
  520. return $temppath;
  521. }
  522. }
  523. // Reaching this point means that the operating system is configured badly.
  524. return '';
  525. }
  526. function get_cachefile($file, $writemode = true)
  527. {
  528. $cache = get_itemcachepath();
  529. if ((!$cache) || (!is_dir($cache))) {
  530. return "";
  531. }
  532. $subfolder = $cache . "/" . substr($file, 0, 2);
  533. $cachepath = $subfolder . "/" . $file;
  534. if ($writemode) {
  535. if (!is_dir($subfolder)) {
  536. mkdir($subfolder);
  537. chmod($subfolder, 0777);
  538. }
  539. }
  540. return $cachepath;
  541. }
  542. function clear_cache($basepath = "", $path = "")
  543. {
  544. if ($path == "") {
  545. $basepath = get_itemcachepath();
  546. $path = $basepath;
  547. }
  548. if (($path == "") || (!is_dir($path))) {
  549. return;
  550. }
  551. if (substr(realpath($path), 0, strlen($basepath)) != $basepath) {
  552. return;
  553. }
  554. $cachetime = (int) Config::get('system', 'itemcache_duration');
  555. if ($cachetime == 0) {
  556. $cachetime = 86400;
  557. }
  558. if (is_writable($path)) {
  559. if ($dh = opendir($path)) {
  560. while (($file = readdir($dh)) !== false) {
  561. $fullpath = $path . "/" . $file;
  562. if ((filetype($fullpath) == "dir") && ($file != ".") && ($file != "..")) {
  563. clear_cache($basepath, $fullpath);
  564. }
  565. if ((filetype($fullpath) == "file") && (filectime($fullpath) < (time() - $cachetime))) {
  566. unlink($fullpath);
  567. }
  568. }
  569. closedir($dh);
  570. }
  571. }
  572. }
  573. function get_itemcachepath()
  574. {
  575. // Checking, if the cache is deactivated
  576. $cachetime = (int) Config::get('system', 'itemcache_duration');
  577. if ($cachetime < 0) {
  578. return "";
  579. }
  580. $itemcache = Config::get('system', 'itemcache');
  581. if (($itemcache != "") && System::isDirectoryUsable($itemcache)) {
  582. return BasePath::getRealPath($itemcache);
  583. }
  584. $temppath = get_temppath();
  585. if ($temppath != "") {
  586. $itemcache = $temppath . "/itemcache";
  587. if (!file_exists($itemcache) && !is_dir($itemcache)) {
  588. mkdir($itemcache);
  589. }
  590. if (System::isDirectoryUsable($itemcache)) {
  591. Config::set("system", "itemcache", $itemcache);
  592. return $itemcache;
  593. }
  594. }
  595. return "";
  596. }
  597. /**
  598. * @brief Returns the path where spool files are stored
  599. *
  600. * @return string Spool path
  601. */
  602. function get_spoolpath()
  603. {
  604. $spoolpath = Config::get('system', 'spoolpath');
  605. if (($spoolpath != "") && System::isDirectoryUsable($spoolpath)) {
  606. // We have a spool path and it is usable
  607. return $spoolpath;
  608. }
  609. // We don't have a working preconfigured spool path, so we take the temp path.
  610. $temppath = get_temppath();
  611. if ($temppath != "") {
  612. // To avoid any interferences with other systems we create our own directory
  613. $spoolpath = $temppath . "/spool";
  614. if (!is_dir($spoolpath)) {
  615. mkdir($spoolpath);
  616. }
  617. if (System::isDirectoryUsable($spoolpath)) {
  618. // The new path is usable, we are happy
  619. Config::set("system", "spoolpath", $spoolpath);
  620. return $spoolpath;
  621. } else {
  622. // We can't create a subdirectory, strange.
  623. // But the directory seems to work, so we use it but don't store it.
  624. return $temppath;
  625. }
  626. }
  627. // Reaching this point means that the operating system is configured badly.
  628. return "";
  629. }
  630. if (!function_exists('exif_imagetype')) {
  631. function exif_imagetype($file)
  632. {
  633. $size = getimagesize($file);
  634. return $size[2];
  635. }
  636. }
  637. function validate_include(&$file)
  638. {
  639. $orig_file = $file;
  640. $file = realpath($file);
  641. if (strpos($file, getcwd()) !== 0) {
  642. return false;
  643. }
  644. $file = str_replace(getcwd() . "/", "", $file, $count);
  645. if ($count != 1) {
  646. return false;
  647. }
  648. if ($orig_file !== $file) {
  649. return false;
  650. }
  651. $valid = false;
  652. if (strpos($file, "include/") === 0) {
  653. $valid = true;
  654. }
  655. if (strpos($file, "addon/") === 0) {
  656. $valid = true;
  657. }
  658. // Simply return flag
  659. return $valid;
  660. }
  661. /**
  662. * PHP 5 compatible dirname() with count parameter
  663. *
  664. * @see http://php.net/manual/en/function.dirname.php#113193
  665. *
  666. * @deprecated with PHP 7
  667. * @param string $path
  668. * @param int $levels
  669. * @return string
  670. */
  671. function rdirname($path, $levels = 1)
  672. {
  673. if ($levels > 1) {
  674. return dirname(rdirname($path, --$levels));
  675. } else {
  676. return dirname($path);
  677. }
  678. }