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.

1607 lines
42KB

  1. <?php
  2. /** @file boot.php
  3. *
  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. require_once(__DIR__ . DIRECTORY_SEPARATOR . 'vendor' . DIRECTORY_SEPARATOR . 'autoload.php');
  19. use Friendica\App;
  20. use Friendica\Core\System;
  21. use Friendica\Core\Config;
  22. use Friendica\Core\PConfig;
  23. use Friendica\Core\Worker;
  24. use Friendica\Database\DBM;
  25. use Friendica\Util\Lock;
  26. require_once 'include/network.php';
  27. require_once 'include/plugin.php';
  28. require_once 'include/text.php';
  29. require_once 'include/datetime.php';
  30. require_once 'include/pgettext.php';
  31. require_once 'include/nav.php';
  32. require_once 'include/cache.php';
  33. require_once 'include/features.php';
  34. require_once 'include/identity.php';
  35. require_once 'update.php';
  36. require_once 'include/dbstructure.php';
  37. require_once 'include/poller.php';
  38. define ( 'FRIENDICA_PLATFORM', 'Friendica');
  39. define ( 'FRIENDICA_CODENAME', 'Asparagus');
  40. define ( 'FRIENDICA_VERSION', '3.6-dev' );
  41. define ( 'DFRN_PROTOCOL_VERSION', '2.23' );
  42. define ( 'DB_UPDATE_VERSION', 1235 );
  43. /**
  44. * @brief Constant with a HTML line break.
  45. *
  46. * Contains a HTML line break (br) element and a real carriage return with line
  47. * feed for the source.
  48. * This can be used in HTML and JavaScript where needed a line break.
  49. */
  50. define ( 'EOL', "<br />\r\n" );
  51. define ( 'ATOM_TIME', 'Y-m-d\TH:i:s\Z' );
  52. /**
  53. * @brief Image storage quality.
  54. *
  55. * Lower numbers save space at cost of image detail.
  56. * For ease of upgrade, please do not change here. Change jpeg quality with
  57. * $a->config['system']['jpeg_quality'] = n;
  58. * in .htconfig.php, where n is netween 1 and 100, and with very poor results
  59. * below about 50
  60. *
  61. */
  62. define ( 'JPEG_QUALITY', 100 );
  63. /**
  64. * $a->config['system']['png_quality'] from 0 (uncompressed) to 9
  65. */
  66. define ( 'PNG_QUALITY', 8 );
  67. /**
  68. *
  69. * An alternate way of limiting picture upload sizes. Specify the maximum pixel
  70. * length that pictures are allowed to be (for non-square pictures, it will apply
  71. * to the longest side). Pictures longer than this length will be resized to be
  72. * this length (on the longest side, the other side will be scaled appropriately).
  73. * Modify this value using
  74. *
  75. * $a->config['system']['max_image_length'] = n;
  76. *
  77. * in .htconfig.php
  78. *
  79. * If you don't want to set a maximum length, set to -1. The default value is
  80. * defined by 'MAX_IMAGE_LENGTH' below.
  81. *
  82. */
  83. define ( 'MAX_IMAGE_LENGTH', -1 );
  84. /**
  85. * Not yet used
  86. */
  87. define ( 'DEFAULT_DB_ENGINE', 'InnoDB' );
  88. /**
  89. * @name SSL Policy
  90. *
  91. * SSL redirection policies
  92. * @{
  93. */
  94. define ( 'SSL_POLICY_NONE', 0 );
  95. define ( 'SSL_POLICY_FULL', 1 );
  96. define ( 'SSL_POLICY_SELFSIGN', 2 );
  97. /* @}*/
  98. /**
  99. * @name Logger
  100. *
  101. * log levels
  102. * @{
  103. */
  104. define ( 'LOGGER_NORMAL', 0 );
  105. define ( 'LOGGER_TRACE', 1 );
  106. define ( 'LOGGER_DEBUG', 2 );
  107. define ( 'LOGGER_DATA', 3 );
  108. define ( 'LOGGER_ALL', 4 );
  109. /* @}*/
  110. /**
  111. * @name Cache
  112. *
  113. * Cache levels
  114. * @{
  115. */
  116. define ( 'CACHE_MONTH', 0 );
  117. define ( 'CACHE_WEEK', 1 );
  118. define ( 'CACHE_DAY', 2 );
  119. define ( 'CACHE_HOUR', 3 );
  120. define ( 'CACHE_HALF_HOUR', 4 );
  121. define ( 'CACHE_QUARTER_HOUR', 5 );
  122. define ( 'CACHE_FIVE_MINUTES', 6 );
  123. define ( 'CACHE_MINUTE', 7 );
  124. /* @}*/
  125. /**
  126. * @name Register
  127. *
  128. * Registration policies
  129. * @{
  130. */
  131. define ( 'REGISTER_CLOSED', 0 );
  132. define ( 'REGISTER_APPROVE', 1 );
  133. define ( 'REGISTER_OPEN', 2 );
  134. /** @}*/
  135. /**
  136. * @name Contact_is
  137. *
  138. * Relationship types
  139. * @{
  140. */
  141. define ( 'CONTACT_IS_FOLLOWER', 1);
  142. define ( 'CONTACT_IS_SHARING', 2);
  143. define ( 'CONTACT_IS_FRIEND', 3);
  144. /** @}*/
  145. /**
  146. * @name Update
  147. *
  148. * DB update return values
  149. * @{
  150. */
  151. define ( 'UPDATE_SUCCESS', 0);
  152. define ( 'UPDATE_FAILED', 1);
  153. /** @}*/
  154. /**
  155. * @name page/profile types
  156. *
  157. * PAGE_NORMAL is a typical personal profile account
  158. * PAGE_SOAPBOX automatically approves all friend requests as CONTACT_IS_SHARING, (readonly)
  159. * PAGE_COMMUNITY automatically approves all friend requests as CONTACT_IS_SHARING, but with
  160. * write access to wall and comments (no email and not included in page owner's ACL lists)
  161. * PAGE_FREELOVE automatically approves all friend requests as full friends (CONTACT_IS_FRIEND).
  162. *
  163. * @{
  164. */
  165. define ( 'PAGE_NORMAL', 0 );
  166. define ( 'PAGE_SOAPBOX', 1 );
  167. define ( 'PAGE_COMMUNITY', 2 );
  168. define ( 'PAGE_FREELOVE', 3 );
  169. define ( 'PAGE_BLOG', 4 );
  170. define ( 'PAGE_PRVGROUP', 5 );
  171. /** @}*/
  172. /**
  173. * @name account types
  174. *
  175. * ACCOUNT_TYPE_PERSON - the account belongs to a person
  176. * Associated page types: PAGE_NORMAL, PAGE_SOAPBOX, PAGE_FREELOVE
  177. *
  178. * ACCOUNT_TYPE_ORGANISATION - the account belongs to an organisation
  179. * Associated page type: PAGE_SOAPBOX
  180. *
  181. * ACCOUNT_TYPE_NEWS - the account is a news reflector
  182. * Associated page type: PAGE_SOAPBOX
  183. *
  184. * ACCOUNT_TYPE_COMMUNITY - the account is community forum
  185. * Associated page types: PAGE_COMMUNITY, PAGE_PRVGROUP
  186. * @{
  187. */
  188. define ( 'ACCOUNT_TYPE_PERSON', 0 );
  189. define ( 'ACCOUNT_TYPE_ORGANISATION',1 );
  190. define ( 'ACCOUNT_TYPE_NEWS', 2 );
  191. define ( 'ACCOUNT_TYPE_COMMUNITY', 3 );
  192. /** @}*/
  193. /**
  194. * @name CP
  195. *
  196. * Type of the community page
  197. * @{
  198. */
  199. define ( 'CP_NO_COMMUNITY_PAGE', -1 );
  200. define ( 'CP_USERS_ON_SERVER', 0 );
  201. define ( 'CP_GLOBAL_COMMUNITY', 1 );
  202. /** @}*/
  203. /**
  204. * @name Protocols
  205. *
  206. * Different protocols that we are storing
  207. * @{
  208. */
  209. define('PROTOCOL_UNKNOWN', 0);
  210. define('PROTOCOL_DFRN', 1);
  211. define('PROTOCOL_DIASPORA', 2);
  212. define('PROTOCOL_OSTATUS_SALMON', 3);
  213. define('PROTOCOL_OSTATUS_FEED', 4); // Deprecated
  214. define('PROTOCOL_GS_CONVERSATION', 5); // Deprecated
  215. define('PROTOCOL_SPLITTED_CONV', 6);
  216. /** @}*/
  217. /**
  218. * @name Network
  219. *
  220. * Network and protocol family types
  221. * @{
  222. */
  223. define ( 'NETWORK_DFRN', 'dfrn'); // Friendica, Mistpark, other DFRN implementations
  224. define ( 'NETWORK_ZOT', 'zot!'); // Zot!
  225. define ( 'NETWORK_OSTATUS', 'stat'); // status.net, identi.ca, GNU-social, other OStatus implementations
  226. define ( 'NETWORK_FEED', 'feed'); // RSS/Atom feeds with no known "post/notify" protocol
  227. define ( 'NETWORK_DIASPORA', 'dspr'); // Diaspora
  228. define ( 'NETWORK_MAIL', 'mail'); // IMAP/POP
  229. define ( 'NETWORK_MAIL2', 'mai2'); // extended IMAP/POP
  230. define ( 'NETWORK_FACEBOOK', 'face'); // Facebook API
  231. define ( 'NETWORK_LINKEDIN', 'lnkd'); // LinkedIn
  232. define ( 'NETWORK_XMPP', 'xmpp'); // XMPP
  233. define ( 'NETWORK_MYSPACE', 'mysp'); // MySpace
  234. define ( 'NETWORK_GPLUS', 'goog'); // Google+
  235. define ( 'NETWORK_PUMPIO', 'pump'); // pump.io
  236. define ( 'NETWORK_TWITTER', 'twit'); // Twitter
  237. define ( 'NETWORK_DIASPORA2', 'dspc'); // Diaspora connector
  238. define ( 'NETWORK_STATUSNET', 'stac'); // Statusnet connector
  239. define ( 'NETWORK_APPNET', 'apdn'); // app.net
  240. define ( 'NETWORK_NEWS', 'nntp'); // Network News Transfer Protocol
  241. define ( 'NETWORK_ICALENDAR', 'ical'); // iCalendar
  242. define ( 'NETWORK_PNUT', 'pnut'); // pnut.io
  243. define ( 'NETWORK_PHANTOM', 'unkn'); // Place holder
  244. /** @}*/
  245. /**
  246. * These numbers are used in stored permissions
  247. * and existing allocations MUST NEVER BE CHANGED
  248. * OR RE-ASSIGNED! You may only add to them.
  249. */
  250. $netgroup_ids = array(
  251. NETWORK_DFRN => (-1),
  252. NETWORK_ZOT => (-2),
  253. NETWORK_OSTATUS => (-3),
  254. NETWORK_FEED => (-4),
  255. NETWORK_DIASPORA => (-5),
  256. NETWORK_MAIL => (-6),
  257. NETWORK_MAIL2 => (-7),
  258. NETWORK_FACEBOOK => (-8),
  259. NETWORK_LINKEDIN => (-9),
  260. NETWORK_XMPP => (-10),
  261. NETWORK_MYSPACE => (-11),
  262. NETWORK_GPLUS => (-12),
  263. NETWORK_PUMPIO => (-13),
  264. NETWORK_TWITTER => (-14),
  265. NETWORK_DIASPORA2 => (-15),
  266. NETWORK_STATUSNET => (-16),
  267. NETWORK_APPNET => (-17),
  268. NETWORK_NEWS => (-18),
  269. NETWORK_ICALENDAR => (-19),
  270. NETWORK_PNUT => (-20),
  271. NETWORK_PHANTOM => (-127),
  272. );
  273. /**
  274. * Maximum number of "people who like (or don't like) this" that we will list by name
  275. */
  276. define ( 'MAX_LIKERS', 75);
  277. /**
  278. * Communication timeout
  279. */
  280. define ( 'ZCURL_TIMEOUT' , (-1));
  281. /**
  282. * @name Notify
  283. *
  284. * Email notification options
  285. * @{
  286. */
  287. define ( 'NOTIFY_INTRO', 0x0001 );
  288. define ( 'NOTIFY_CONFIRM', 0x0002 );
  289. define ( 'NOTIFY_WALL', 0x0004 );
  290. define ( 'NOTIFY_COMMENT', 0x0008 );
  291. define ( 'NOTIFY_MAIL', 0x0010 );
  292. define ( 'NOTIFY_SUGGEST', 0x0020 );
  293. define ( 'NOTIFY_PROFILE', 0x0040 );
  294. define ( 'NOTIFY_TAGSELF', 0x0080 );
  295. define ( 'NOTIFY_TAGSHARE', 0x0100 );
  296. define ( 'NOTIFY_POKE', 0x0200 );
  297. define ( 'NOTIFY_SHARE', 0x0400 );
  298. define ( 'SYSTEM_EMAIL', 0x4000 );
  299. define ( 'NOTIFY_SYSTEM', 0x8000 );
  300. /* @}*/
  301. /**
  302. * @name Term
  303. *
  304. * Tag/term types
  305. * @{
  306. */
  307. define ( 'TERM_UNKNOWN', 0 );
  308. define ( 'TERM_HASHTAG', 1 );
  309. define ( 'TERM_MENTION', 2 );
  310. define ( 'TERM_CATEGORY', 3 );
  311. define ( 'TERM_PCATEGORY', 4 );
  312. define ( 'TERM_FILE', 5 );
  313. define ( 'TERM_SAVEDSEARCH', 6 );
  314. define ( 'TERM_CONVERSATION', 7 );
  315. define ( 'TERM_OBJ_POST', 1 );
  316. define ( 'TERM_OBJ_PHOTO', 2 );
  317. /**
  318. * @name Namespaces
  319. *
  320. * Various namespaces we may need to parse
  321. * @{
  322. */
  323. define ( 'NAMESPACE_ZOT', 'http://purl.org/zot' );
  324. define ( 'NAMESPACE_DFRN' , 'http://purl.org/macgirvin/dfrn/1.0' );
  325. define ( 'NAMESPACE_THREAD' , 'http://purl.org/syndication/thread/1.0' );
  326. define ( 'NAMESPACE_TOMB' , 'http://purl.org/atompub/tombstones/1.0' );
  327. define ( 'NAMESPACE_ACTIVITY', 'http://activitystrea.ms/spec/1.0/' );
  328. define ( 'NAMESPACE_ACTIVITY_SCHEMA', 'http://activitystrea.ms/schema/1.0/' );
  329. define ( 'NAMESPACE_MEDIA', 'http://purl.org/syndication/atommedia' );
  330. define ( 'NAMESPACE_SALMON_ME', 'http://salmon-protocol.org/ns/magic-env' );
  331. define ( 'NAMESPACE_OSTATUSSUB', 'http://ostatus.org/schema/1.0/subscribe' );
  332. define ( 'NAMESPACE_GEORSS', 'http://www.georss.org/georss' );
  333. define ( 'NAMESPACE_POCO', 'http://portablecontacts.net/spec/1.0' );
  334. define ( 'NAMESPACE_FEED', 'http://schemas.google.com/g/2010#updates-from' );
  335. define ( 'NAMESPACE_OSTATUS', 'http://ostatus.org/schema/1.0' );
  336. define ( 'NAMESPACE_STATUSNET', 'http://status.net/schema/api/1/' );
  337. define ( 'NAMESPACE_ATOM1', 'http://www.w3.org/2005/Atom' );
  338. define ( 'NAMESPACE_MASTODON', 'http://mastodon.social/schema/1.0' );
  339. /* @}*/
  340. /**
  341. * @name Activity
  342. *
  343. * Activity stream defines
  344. * @{
  345. */
  346. define ( 'ACTIVITY_LIKE', NAMESPACE_ACTIVITY_SCHEMA . 'like' );
  347. define ( 'ACTIVITY_DISLIKE', NAMESPACE_DFRN . '/dislike' );
  348. define ( 'ACTIVITY_ATTEND', NAMESPACE_ZOT . '/activity/attendyes' );
  349. define ( 'ACTIVITY_ATTENDNO', NAMESPACE_ZOT . '/activity/attendno' );
  350. define ( 'ACTIVITY_ATTENDMAYBE', NAMESPACE_ZOT . '/activity/attendmaybe' );
  351. define ( 'ACTIVITY_OBJ_HEART', NAMESPACE_DFRN . '/heart' );
  352. define ( 'ACTIVITY_FRIEND', NAMESPACE_ACTIVITY_SCHEMA . 'make-friend' );
  353. define ( 'ACTIVITY_REQ_FRIEND', NAMESPACE_ACTIVITY_SCHEMA . 'request-friend' );
  354. define ( 'ACTIVITY_UNFRIEND', NAMESPACE_ACTIVITY_SCHEMA . 'remove-friend' );
  355. define ( 'ACTIVITY_FOLLOW', NAMESPACE_ACTIVITY_SCHEMA . 'follow' );
  356. define ( 'ACTIVITY_UNFOLLOW', NAMESPACE_ACTIVITY_SCHEMA . 'stop-following' );
  357. define ( 'ACTIVITY_JOIN', NAMESPACE_ACTIVITY_SCHEMA . 'join' );
  358. define ( 'ACTIVITY_POST', NAMESPACE_ACTIVITY_SCHEMA . 'post' );
  359. define ( 'ACTIVITY_UPDATE', NAMESPACE_ACTIVITY_SCHEMA . 'update' );
  360. define ( 'ACTIVITY_TAG', NAMESPACE_ACTIVITY_SCHEMA . 'tag' );
  361. define ( 'ACTIVITY_FAVORITE', NAMESPACE_ACTIVITY_SCHEMA . 'favorite' );
  362. define ( 'ACTIVITY_UNFAVORITE', NAMESPACE_ACTIVITY_SCHEMA . 'unfavorite' );
  363. define ( 'ACTIVITY_SHARE', NAMESPACE_ACTIVITY_SCHEMA . 'share' );
  364. define ( 'ACTIVITY_DELETE', NAMESPACE_ACTIVITY_SCHEMA . 'delete' );
  365. define ( 'ACTIVITY_POKE', NAMESPACE_ZOT . '/activity/poke' );
  366. define ( 'ACTIVITY_MOOD', NAMESPACE_ZOT . '/activity/mood' );
  367. define ( 'ACTIVITY_OBJ_BOOKMARK', NAMESPACE_ACTIVITY_SCHEMA . 'bookmark' );
  368. define ( 'ACTIVITY_OBJ_COMMENT', NAMESPACE_ACTIVITY_SCHEMA . 'comment' );
  369. define ( 'ACTIVITY_OBJ_NOTE', NAMESPACE_ACTIVITY_SCHEMA . 'note' );
  370. define ( 'ACTIVITY_OBJ_PERSON', NAMESPACE_ACTIVITY_SCHEMA . 'person' );
  371. define ( 'ACTIVITY_OBJ_IMAGE', NAMESPACE_ACTIVITY_SCHEMA . 'image' );
  372. define ( 'ACTIVITY_OBJ_PHOTO', NAMESPACE_ACTIVITY_SCHEMA . 'photo' );
  373. define ( 'ACTIVITY_OBJ_VIDEO', NAMESPACE_ACTIVITY_SCHEMA . 'video' );
  374. define ( 'ACTIVITY_OBJ_P_PHOTO', NAMESPACE_ACTIVITY_SCHEMA . 'profile-photo' );
  375. define ( 'ACTIVITY_OBJ_ALBUM', NAMESPACE_ACTIVITY_SCHEMA . 'photo-album' );
  376. define ( 'ACTIVITY_OBJ_EVENT', NAMESPACE_ACTIVITY_SCHEMA . 'event' );
  377. define ( 'ACTIVITY_OBJ_GROUP', NAMESPACE_ACTIVITY_SCHEMA . 'group' );
  378. define ( 'ACTIVITY_OBJ_TAGTERM', NAMESPACE_DFRN . '/tagterm' );
  379. define ( 'ACTIVITY_OBJ_PROFILE', NAMESPACE_DFRN . '/profile' );
  380. define ( 'ACTIVITY_OBJ_QUESTION', 'http://activityschema.org/object/question' );
  381. /* @}*/
  382. /**
  383. * @name Gravity
  384. *
  385. * Item weight for query ordering
  386. * @{
  387. */
  388. define ( 'GRAVITY_PARENT', 0);
  389. define ( 'GRAVITY_LIKE', 3);
  390. define ( 'GRAVITY_COMMENT', 6);
  391. /* @}*/
  392. /**
  393. * @name Priority
  394. *
  395. * Process priority for the worker
  396. * @{
  397. */
  398. define('PRIORITY_UNDEFINED', 0);
  399. define('PRIORITY_CRITICAL', 10);
  400. define('PRIORITY_HIGH', 20);
  401. define('PRIORITY_MEDIUM', 30);
  402. define('PRIORITY_LOW', 40);
  403. define('PRIORITY_NEGLIGIBLE',50);
  404. /* @}*/
  405. /**
  406. * @name Social Relay settings
  407. *
  408. * See here: https://github.com/jaywink/social-relay
  409. * and here: https://wiki.diasporafoundation.org/Relay_servers_for_public_posts
  410. * @{
  411. */
  412. define('SR_SCOPE_NONE', '');
  413. define('SR_SCOPE_ALL', 'all');
  414. define('SR_SCOPE_TAGS', 'tags');
  415. /* @}*/
  416. /**
  417. * Lowest possible date time value
  418. */
  419. define ('NULL_DATE', '0001-01-01 00:00:00');
  420. // Normally this constant is defined - but not if "pcntl" isn't installed
  421. if (!defined("SIGTERM")) {
  422. define("SIGTERM", 15);
  423. }
  424. /**
  425. * Depending on the PHP version this constant does exist - or not.
  426. * See here: http://php.net/manual/en/curl.constants.php#117928
  427. */
  428. if (!defined('CURLE_OPERATION_TIMEDOUT')) {
  429. define('CURLE_OPERATION_TIMEDOUT', CURLE_OPERATION_TIMEOUTED);
  430. }
  431. /**
  432. *
  433. * Reverse the effect of magic_quotes_gpc if it is enabled.
  434. * Please disable magic_quotes_gpc so we don't have to do this.
  435. * See http://php.net/manual/en/security.magicquotes.disabling.php
  436. *
  437. */
  438. function startup() {
  439. error_reporting(E_ERROR | E_WARNING | E_PARSE);
  440. set_time_limit(0);
  441. // This has to be quite large to deal with embedded private photos
  442. ini_set('pcre.backtrack_limit', 500000);
  443. if (get_magic_quotes_gpc()) {
  444. $process = array(&$_GET, &$_POST, &$_COOKIE, &$_REQUEST);
  445. while (list($key, $val) = each($process)) {
  446. foreach ($val as $k => $v) {
  447. unset($process[$key][$k]);
  448. if (is_array($v)) {
  449. $process[$key][stripslashes($k)] = $v;
  450. $process[] = &$process[$key][stripslashes($k)];
  451. } else {
  452. $process[$key][stripslashes($k)] = stripslashes($v);
  453. }
  454. }
  455. }
  456. unset($process);
  457. }
  458. }
  459. /**
  460. * @brief Retrieve the App structure
  461. *
  462. * Useful in functions which require it but don't get it passed to them
  463. */
  464. function get_app() {
  465. global $a;
  466. if (empty($a)) {
  467. $a = new App(dirname(__DIR__));
  468. }
  469. return $a;
  470. }
  471. /**
  472. * @brief Multi-purpose function to check variable state.
  473. *
  474. * Usage: x($var) or $x($array, 'key')
  475. *
  476. * returns false if variable/key is not set
  477. * if variable is set, returns 1 if has 'non-zero' value, otherwise returns 0.
  478. * e.g. x('') or x(0) returns 0;
  479. *
  480. * @param string|array $s variable to check
  481. * @param string $k key inside the array to check
  482. *
  483. * @return bool|int
  484. */
  485. function x($s, $k = NULL) {
  486. if ($k != NULL) {
  487. if ((is_array($s)) && (array_key_exists($k, $s))) {
  488. if ($s[$k]) {
  489. return (int) 1;
  490. }
  491. return (int) 0;
  492. }
  493. return false;
  494. } else {
  495. if (isset($s)) {
  496. if ($s) {
  497. return (int) 1;
  498. }
  499. return (int) 0;
  500. }
  501. return false;
  502. }
  503. }
  504. /**
  505. * @brief Called from db initialisation if db is dead.
  506. */
  507. function system_unavailable() {
  508. include('system_unavailable.php');
  509. system_down();
  510. killme();
  511. }
  512. /**
  513. * @brief Returns the baseurl.
  514. *
  515. * @see System::baseUrl()
  516. *
  517. * @return string
  518. * @TODO Function is deprecated and only used in some addons
  519. */
  520. function z_root() {
  521. return System::baseUrl();
  522. }
  523. /**
  524. * @brief Return absolut URL for given $path.
  525. *
  526. * @param string $path
  527. *
  528. * @return string
  529. */
  530. function absurl($path) {
  531. if (strpos($path, '/') === 0) {
  532. return z_path() . $path;
  533. }
  534. return $path;
  535. }
  536. /**
  537. * @brief Function to check if request was an AJAX (xmlhttprequest) request.
  538. *
  539. * @return boolean
  540. */
  541. function is_ajax() {
  542. return (!empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest');
  543. }
  544. /**
  545. * @brief Function to check if request was an AJAX (xmlhttprequest) request.
  546. *
  547. * @param $via_worker boolean Is the check run via the poller?
  548. */
  549. function check_db($via_worker) {
  550. $build = Config::get('system', 'build');
  551. if (!x($build)) {
  552. Config::set('system', 'build', DB_UPDATE_VERSION);
  553. $build = DB_UPDATE_VERSION;
  554. }
  555. if ($build != DB_UPDATE_VERSION) {
  556. // When we cannot execute the database update via the worker, we will do it directly
  557. if (!Worker::add(PRIORITY_CRITICAL, 'dbupdate') && $via_worker) {
  558. update_db(get_app());
  559. }
  560. }
  561. }
  562. /**
  563. * Sets the base url for use in cmdline programs which don't have
  564. * $_SERVER variables
  565. */
  566. function check_url(App $a) {
  567. $url = Config::get('system', 'url');
  568. // if the url isn't set or the stored url is radically different
  569. // than the currently visited url, store the current value accordingly.
  570. // "Radically different" ignores common variations such as http vs https
  571. // and www.example.com vs example.com.
  572. // We will only change the url to an ip address if there is no existing setting
  573. if (!x($url)) {
  574. $url = Config::set('system', 'url', System::baseUrl());
  575. }
  576. if ((!link_compare($url, System::baseUrl())) && (!preg_match("/^(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})$/", $a->get_hostname))) {
  577. $url = Config::set('system', 'url', System::baseUrl());
  578. }
  579. return;
  580. }
  581. /**
  582. * @brief Automatic database updates
  583. */
  584. function update_db(App $a) {
  585. $build = Config::get('system', 'build');
  586. if (!x($build)) {
  587. $build = Config::set('system', 'build', DB_UPDATE_VERSION);
  588. }
  589. if ($build != DB_UPDATE_VERSION) {
  590. $stored = intval($build);
  591. $current = intval(DB_UPDATE_VERSION);
  592. if ($stored < $current) {
  593. Config::load('database');
  594. // We're reporting a different version than what is currently installed.
  595. // Run any existing update scripts to bring the database up to current.
  596. // make sure that boot.php and update.php are the same release, we might be
  597. // updating right this very second and the correct version of the update.php
  598. // file may not be here yet. This can happen on a very busy site.
  599. if (DB_UPDATE_VERSION == UPDATE_VERSION) {
  600. // Compare the current structure with the defined structure
  601. $t = Config::get('database', 'dbupdate_' . DB_UPDATE_VERSION);
  602. if ($t !== false) {
  603. return;
  604. }
  605. Config::set('database', 'dbupdate_' . DB_UPDATE_VERSION, time());
  606. // run old update routine (wich could modify the schema and
  607. // conflits with new routine)
  608. for ($x = $stored; $x < NEW_UPDATE_ROUTINE_VERSION; $x++) {
  609. $r = run_update_function($x);
  610. if (!$r) {
  611. break;
  612. }
  613. }
  614. if ($stored < NEW_UPDATE_ROUTINE_VERSION) {
  615. $stored = NEW_UPDATE_ROUTINE_VERSION;
  616. }
  617. // run new update routine
  618. // it update the structure in one call
  619. $retval = update_structure(false, true);
  620. if ($retval) {
  621. update_fail(
  622. DB_UPDATE_VERSION,
  623. $retval
  624. );
  625. return;
  626. } else {
  627. Config::set('database', 'dbupdate_' . DB_UPDATE_VERSION, 'success');
  628. }
  629. // run any left update_nnnn functions in update.php
  630. for ($x = $stored; $x < $current; $x ++) {
  631. $r = run_update_function($x);
  632. if (!$r) {
  633. break;
  634. }
  635. }
  636. }
  637. }
  638. }
  639. return;
  640. }
  641. function run_update_function($x) {
  642. if (function_exists('update_' . $x)) {
  643. // There could be a lot of processes running or about to run.
  644. // We want exactly one process to run the update command.
  645. // So store the fact that we're taking responsibility
  646. // after first checking to see if somebody else already has.
  647. // If the update fails or times-out completely you may need to
  648. // delete the config entry to try again.
  649. $t = Config::get('database', 'update_' . $x);
  650. if ($t !== false) {
  651. return false;
  652. }
  653. Config::set('database', 'update_' . $x, time());
  654. // call the specific update
  655. $func = 'update_' . $x;
  656. $retval = $func();
  657. if ($retval) {
  658. //send the administrator an e-mail
  659. update_fail(
  660. $x,
  661. sprintf(t('Update %s failed. See error logs.'), $x)
  662. );
  663. return false;
  664. } else {
  665. Config::set('database', 'update_' . $x, 'success');
  666. Config::set('system', 'build', $x + 1);
  667. return true;
  668. }
  669. } else {
  670. Config::set('database', 'update_' . $x, 'success');
  671. Config::set('system', 'build', $x + 1);
  672. return true;
  673. }
  674. return true;
  675. }
  676. /**
  677. * @brief Synchronise plugins:
  678. *
  679. * $a->config['system']['addon'] contains a comma-separated list of names
  680. * of plugins/addons which are used on this system.
  681. * Go through the database list of already installed addons, and if we have
  682. * an entry, but it isn't in the config list, call the uninstall procedure
  683. * and mark it uninstalled in the database (for now we'll remove it).
  684. * Then go through the config list and if we have a plugin that isn't installed,
  685. * call the install procedure and add it to the database.
  686. *
  687. * @param App $a
  688. *
  689. */
  690. function check_plugins(App $a) {
  691. $r = q("SELECT * FROM `addon` WHERE `installed` = 1");
  692. if (DBM::is_result($r)) {
  693. $installed = $r;
  694. } else {
  695. $installed = array();
  696. }
  697. $plugins = Config::get('system', 'addon');
  698. $plugins_arr = array();
  699. if ($plugins) {
  700. $plugins_arr = explode(',', str_replace(' ', '', $plugins));
  701. }
  702. $a->plugins = $plugins_arr;
  703. $installed_arr = array();
  704. if (count($installed)) {
  705. foreach ($installed as $i) {
  706. if (!in_array($i['name'], $plugins_arr)) {
  707. uninstall_plugin($i['name']);
  708. } else {
  709. $installed_arr[] = $i['name'];
  710. }
  711. }
  712. }
  713. if (count($plugins_arr)) {
  714. foreach ($plugins_arr as $p) {
  715. if (!in_array($p, $installed_arr)) {
  716. install_plugin($p);
  717. }
  718. }
  719. }
  720. load_hooks();
  721. return;
  722. }
  723. function get_guid($size = 16, $prefix = "") {
  724. if ($prefix == "") {
  725. $a = get_app();
  726. $prefix = hash("crc32", $a->get_hostname());
  727. }
  728. while (strlen($prefix) < ($size - 13)) {
  729. $prefix .= mt_rand();
  730. }
  731. if ($size >= 24) {
  732. $prefix = substr($prefix, 0, $size - 22);
  733. return(str_replace(".", "", uniqid($prefix, true)));
  734. } else {
  735. $prefix = substr($prefix, 0, max($size - 13, 0));
  736. return(uniqid($prefix));
  737. }
  738. }
  739. /**
  740. * @brief Wrapper for adding a login box.
  741. *
  742. * @param bool $register
  743. * If $register == true provide a registration link.
  744. * This will most always depend on the value of $a->config['register_policy'].
  745. * @param bool $hiddens
  746. *
  747. * @return string
  748. * Returns the complete html for inserting into the page
  749. *
  750. * @hooks 'login_hook'
  751. * string $o
  752. */
  753. function login($register = false, $hiddens = false) {
  754. $a = get_app();
  755. $o = "";
  756. $reg = false;
  757. if ($register) {
  758. $reg = array(
  759. 'title' => t('Create a New Account'),
  760. 'desc' => t('Register')
  761. );
  762. }
  763. $noid = Config::get('system', 'no_openid');
  764. $dest_url = $a->query_string;
  765. if (local_user()) {
  766. $tpl = get_markup_template("logout.tpl");
  767. } else {
  768. $a->page['htmlhead'] .= replace_macros(get_markup_template("login_head.tpl"), array(
  769. '$baseurl' => $a->get_baseurl(true)
  770. ));
  771. $tpl = get_markup_template("login.tpl");
  772. $_SESSION['return_url'] = $a->query_string;
  773. $a->module = 'login';
  774. }
  775. $o .= replace_macros($tpl, array(
  776. '$dest_url' => $dest_url,
  777. '$logout' => t('Logout'),
  778. '$login' => t('Login'),
  779. '$lname' => array('username', t('Nickname or Email: ') , '', ''),
  780. '$lpassword' => array('password', t('Password: '), '', ''),
  781. '$lremember' => array('remember', t('Remember me'), 0, ''),
  782. '$openid' => !$noid,
  783. '$lopenid' => array('openid_url', t('Or login using OpenID: '),'',''),
  784. '$hiddens' => $hiddens,
  785. '$register' => $reg,
  786. '$lostpass' => t('Forgot your password?'),
  787. '$lostlink' => t('Password Reset'),
  788. '$tostitle' => t('Website Terms of Service'),
  789. '$toslink' => t('terms of service'),
  790. '$privacytitle' => t('Website Privacy Policy'),
  791. '$privacylink' => t('privacy policy'),
  792. ));
  793. call_hooks('login_hook', $o);
  794. return $o;
  795. }
  796. /**
  797. * @brief Used to end the current process, after saving session state.
  798. */
  799. function killme() {
  800. global $session_exists;
  801. if (!get_app()->is_backend()) {
  802. if (!$session_exists) {
  803. session_abort();
  804. } else {
  805. session_write_close();
  806. }
  807. }
  808. exit();
  809. }
  810. /**
  811. * @brief Redirect to another URL and terminate this process.
  812. */
  813. function goaway($s) {
  814. if (!strstr(normalise_link($s), "http://")) {
  815. $s = System::baseUrl() . "/" . $s;
  816. }
  817. header("Location: $s");
  818. killme();
  819. }
  820. /**
  821. * @brief Returns the user id of locally logged in user or false.
  822. *
  823. * @return int|bool user id or false
  824. */
  825. function local_user() {
  826. if (x($_SESSION, 'authenticated') && x($_SESSION, 'uid')) {
  827. return intval($_SESSION['uid']);
  828. }
  829. return false;
  830. }
  831. /**
  832. * @brief Returns the public contact id of logged in user or false.
  833. *
  834. * @return int|bool public contact id or false
  835. */
  836. function public_contact() {
  837. static $public_contact_id = false;
  838. if (!$public_contact_id && x($_SESSION, 'authenticated')) {
  839. if (x($_SESSION, 'my_address')) {
  840. // Local user
  841. $public_contact_id = intval(get_contact($_SESSION['my_address'], 0));
  842. } elseif (x($_SESSION, 'visitor_home')) {
  843. // Remote user
  844. $public_contact_id = intval(get_contact($_SESSION['visitor_home'], 0));
  845. }
  846. } elseif (!x($_SESSION, 'authenticated')) {
  847. $public_contact_id = false;
  848. }
  849. return $public_contact_id;
  850. }
  851. /**
  852. * @brief Returns contact id of authenticated site visitor or false
  853. *
  854. * @return int|bool visitor_id or false
  855. */
  856. function remote_user() {
  857. // You cannot be both local and remote
  858. if (local_user()) {
  859. return false;
  860. }
  861. if ((x($_SESSION, 'authenticated')) && (x($_SESSION, 'visitor_id'))) {
  862. return intval($_SESSION['visitor_id']);
  863. }
  864. return false;
  865. }
  866. /**
  867. * @brief Show an error message to user.
  868. *
  869. * This function save text in session, to be shown to the user at next page load
  870. *
  871. * @param string $s - Text of notice
  872. */
  873. function notice($s) {
  874. $a = get_app();
  875. if (!x($_SESSION, 'sysmsg')) {
  876. $_SESSION['sysmsg'] = array();
  877. }
  878. if ($a->interactive) {
  879. $_SESSION['sysmsg'][] = $s;
  880. }
  881. }
  882. /**
  883. * @brief Show an info message to user.
  884. *
  885. * This function save text in session, to be shown to the user at next page load
  886. *
  887. * @param string $s - Text of notice
  888. */
  889. function info($s) {
  890. $a = get_app();
  891. if (local_user() && PConfig::get(local_user(), 'system', 'ignore_info')) {
  892. return;
  893. }
  894. if (!x($_SESSION, 'sysmsg_info')) {
  895. $_SESSION['sysmsg_info'] = array();
  896. }
  897. if ($a->interactive) {
  898. $_SESSION['sysmsg_info'][] = $s;
  899. }
  900. }
  901. /**
  902. * @brief Wrapper around config to limit the text length of an incoming message
  903. *
  904. * @return int
  905. */
  906. function get_max_import_size() {
  907. $a = get_app();
  908. return ((x($a->config, 'max_import_size')) ? $a->config['max_import_size'] : 0 );
  909. }
  910. /**
  911. * @brief compatibilty wrapper for Worker::add function
  912. *
  913. * @param (integer|array) priority or parameter array, strings are deprecated and are ignored
  914. *
  915. * @return boolean "false" if proc_run couldn't be executed
  916. */
  917. function proc_run() {
  918. $proc_args = func_get_args();
  919. call_user_func_array('Friendica\Core\Worker::add', $proc_args);
  920. }
  921. function current_theme() {
  922. $app_base_themes = array('duepuntozero', 'dispy', 'quattro');
  923. $a = get_app();
  924. $page_theme = null;
  925. // Find the theme that belongs to the user whose stuff we are looking at
  926. if ($a->profile_uid && ($a->profile_uid != local_user())) {
  927. $r = q("select theme from user where uid = %d limit 1",
  928. intval($a->profile_uid)
  929. );
  930. if (DBM::is_result($r)) {
  931. $page_theme = $r[0]['theme'];
  932. }
  933. }
  934. // Allow folks to over-rule user themes and always use their own on their own site.
  935. // This works only if the user is on the same server
  936. if ($page_theme && local_user() && (local_user() != $a->profile_uid)) {
  937. if (PConfig::get(local_user(), 'system', 'always_my_theme')) {
  938. $page_theme = null;
  939. }
  940. }
  941. // $mobile_detect = new Mobile_Detect();
  942. // $is_mobile = $mobile_detect->isMobile() || $mobile_detect->isTablet();
  943. $is_mobile = $a->is_mobile || $a->is_tablet;
  944. $standard_system_theme = Config::get('system', 'theme', '');
  945. $standard_theme_name = ((isset($_SESSION) && x($_SESSION, 'theme')) ? $_SESSION['theme'] : $standard_system_theme);
  946. if ($is_mobile) {
  947. if (isset($_SESSION['show-mobile']) && !$_SESSION['show-mobile']) {
  948. $system_theme = $standard_system_theme;
  949. $theme_name = $standard_theme_name;
  950. } else {
  951. $system_theme = Config::get('system', 'mobile-theme', '');
  952. if ($system_theme == '') {
  953. $system_theme = $standard_system_theme;
  954. }
  955. $theme_name = ((isset($_SESSION) && x($_SESSION, 'mobile-theme')) ? $_SESSION['mobile-theme'] : $system_theme);
  956. if ($theme_name === '---') {
  957. // user has selected to have the mobile theme be the same as the normal one
  958. $system_theme = $standard_system_theme;
  959. $theme_name = $standard_theme_name;
  960. if ($page_theme) {
  961. $theme_name = $page_theme;
  962. }
  963. }
  964. }
  965. } else {
  966. $system_theme = $standard_system_theme;
  967. $theme_name = $standard_theme_name;
  968. if ($page_theme) {
  969. $theme_name = $page_theme;
  970. }
  971. }
  972. if ($theme_name &&
  973. (file_exists('view/theme/' . $theme_name . '/style.css') ||
  974. file_exists('view/theme/' . $theme_name . '/style.php'))) {
  975. return($theme_name);
  976. }
  977. foreach ($app_base_themes as $t) {
  978. if (file_exists('view/theme/' . $t . '/style.css') ||
  979. file_exists('view/theme/' . $t . '/style.php')) {
  980. return($t);
  981. }
  982. }
  983. $fallback = array_merge(glob('view/theme/*/style.css'), glob('view/theme/*/style.php'));
  984. if (count($fallback)) {
  985. return (str_replace('view/theme/', '', substr($fallback[0], 0, -10)));
  986. }
  987. /// @TODO No final return statement?
  988. }
  989. /**
  990. * @brief Return full URL to theme which is currently in effect.
  991. *
  992. * Provide a sane default if nothing is chosen or the specified theme does not exist.
  993. *
  994. * @return string
  995. */
  996. function current_theme_url() {
  997. $a = get_app();
  998. $t = current_theme();
  999. $opts = (($a->profile_uid) ? '?f=&puid=' . $a->profile_uid : '');
  1000. if (file_exists('view/theme/' . $t . '/style.php')) {
  1001. return('view/theme/' . $t . '/style.pcss' . $opts);
  1002. }
  1003. return('view/theme/' . $t . '/style.css');
  1004. }
  1005. function feed_birthday($uid, $tz) {
  1006. /**
  1007. *
  1008. * Determine the next birthday, but only if the birthday is published
  1009. * in the default profile. We _could_ also look for a private profile that the
  1010. * recipient can see, but somebody could get mad at us if they start getting
  1011. * public birthday greetings when they haven't made this info public.
  1012. *
  1013. * Assuming we are able to publish this info, we are then going to convert
  1014. * the start time from the owner's timezone to UTC.
  1015. *
  1016. * This will potentially solve the problem found with some social networks
  1017. * where birthdays are converted to the viewer's timezone and salutations from
  1018. * elsewhere in the world show up on the wrong day. We will convert it to the
  1019. * viewer's timezone also, but first we are going to convert it from the birthday
  1020. * person's timezone to GMT - so the viewer may find the birthday starting at
  1021. * 6:00PM the day before, but that will correspond to midnight to the birthday person.
  1022. *
  1023. */
  1024. $birthday = '';
  1025. if (!strlen($tz)) {
  1026. $tz = 'UTC';
  1027. }
  1028. $p = q("SELECT `dob` FROM `profile` WHERE `is-default` = 1 AND `uid` = %d LIMIT 1",
  1029. intval($uid)
  1030. );
  1031. if (DBM::is_result($p)) {
  1032. $tmp_dob = substr($p[0]['dob'], 5);
  1033. if (intval($tmp_dob)) {
  1034. $y = datetime_convert($tz, $tz, 'now', 'Y');
  1035. $bd = $y . '-' . $tmp_dob . ' 00:00';
  1036. $t_dob = strtotime($bd);
  1037. $now = strtotime(datetime_convert($tz, $tz, 'now'));
  1038. if ($t_dob < $now) {
  1039. $bd = $y + 1 . '-' . $tmp_dob . ' 00:00';
  1040. }
  1041. $birthday = datetime_convert($tz, 'UTC', $bd, ATOM_TIME);
  1042. }
  1043. }
  1044. return $birthday;
  1045. }
  1046. /**
  1047. * @brief Check if current user has admin role.
  1048. *
  1049. * @return bool true if user is an admin
  1050. */
  1051. function is_site_admin() {
  1052. $a = get_app();
  1053. $adminlist = explode(",", str_replace(" ", "", $a->config['admin_email']));
  1054. //if(local_user() && x($a->user,'email') && x($a->config,'admin_email') && ($a->user['email'] === $a->config['admin_email']))
  1055. if (local_user() && x($a->user, 'email') && x($a->config, 'admin_email') && in_array($a->user['email'], $adminlist)) {
  1056. return true;
  1057. }
  1058. return false;
  1059. }
  1060. /**
  1061. * @brief Returns querystring as string from a mapped array.
  1062. *
  1063. * @param array $params mapped array with query parameters
  1064. * @param string $name of parameter, default null
  1065. *
  1066. * @return string
  1067. */
  1068. function build_querystring($params, $name = null) {
  1069. $ret = "";
  1070. foreach ($params as $key => $val) {
  1071. if (is_array($val)) {
  1072. /// @TODO maybe not compare against null, use is_null()
  1073. if ($name == null) {
  1074. $ret .= build_querystring($val, $key);
  1075. } else {
  1076. $ret .= build_querystring($val, $name . "[$key]");
  1077. }
  1078. } else {
  1079. $val = urlencode($val);
  1080. /// @TODO maybe not compare against null, use is_null()
  1081. if ($name != null) {
  1082. /// @TODO two string concated, can be merged to one
  1083. $ret .= $name . "[$key]" . "=$val&";
  1084. } else {
  1085. $ret .= "$key=$val&";
  1086. }
  1087. }
  1088. }
  1089. return $ret;
  1090. }
  1091. function explode_querystring($query) {
  1092. $arg_st = strpos($query, '?');
  1093. if ($arg_st !== false) {
  1094. $base = substr($query, 0, $arg_st);
  1095. $arg_st += 1;
  1096. } else {
  1097. $base = '';
  1098. $arg_st = 0;
  1099. }
  1100. $args = explode('&', substr($query, $arg_st));
  1101. foreach ($args as $k => $arg) {
  1102. /// @TODO really compare type-safe here?
  1103. if ($arg === '') {
  1104. unset($args[$k]);
  1105. }
  1106. }
  1107. $args = array_values($args);
  1108. if (!$base) {
  1109. $base = $args[0];
  1110. unset($args[0]);
  1111. $args = array_values($args);
  1112. }
  1113. return array(
  1114. 'base' => $base,
  1115. 'args' => $args,
  1116. );
  1117. }
  1118. /**
  1119. * Returns the complete URL of the current page, e.g.: http(s)://something.com/network
  1120. *
  1121. * Taken from http://webcheatsheet.com/php/get_current_page_url.php
  1122. */
  1123. function curPageURL() {
  1124. $pageURL = 'http';
  1125. if ($_SERVER["HTTPS"] == "on") {
  1126. $pageURL .= "s";
  1127. }
  1128. $pageURL .= "://";
  1129. if ($_SERVER["SERVER_PORT"] != "80" && $_SERVER["SERVER_PORT"] != "443") {
  1130. $pageURL .= $_SERVER["SERVER_NAME"] . ":" . $_SERVER["SERVER_PORT"] . $_SERVER["REQUEST_URI"];
  1131. } else {
  1132. $pageURL .= $_SERVER["SERVER_NAME"] . $_SERVER["REQUEST_URI"];
  1133. }
  1134. return $pageURL;
  1135. }
  1136. function random_digits($digits) {
  1137. $rn = '';
  1138. for ($i = 0; $i < $digits; $i++) {
  1139. /// @TODO rand() is different to mt_rand() and maybe lesser "random"
  1140. $rn .= rand(0, 9);
  1141. }
  1142. return $rn;
  1143. }
  1144. function get_server() {
  1145. $server = Config::get("system", "directory");
  1146. if ($server == "") {
  1147. $server = "http://dir.friendica.social";
  1148. }
  1149. return($server);
  1150. }
  1151. function get_temppath() {
  1152. $a = get_app();
  1153. $temppath = Config::get("system", "temppath");
  1154. if (($temppath != "") && App::directory_usable($temppath)) {
  1155. // We have a temp path and it is usable
  1156. return App::realpath($temppath);
  1157. }
  1158. // We don't have a working preconfigured temp path, so we take the system path.
  1159. $temppath = sys_get_temp_dir();
  1160. // Check if it is usable
  1161. if (($temppath != "") && App::directory_usable($temppath)) {
  1162. // Always store the real path, not the path through symlinks
  1163. $temppath = App::realpath($temppath);
  1164. // To avoid any interferences with other systems we create our own directory
  1165. $new_temppath = $temppath . "/" . $a->get_hostname();
  1166. if (!is_dir($new_temppath)) {
  1167. /// @TODO There is a mkdir()+chmod() upwards, maybe generalize this (+ configurable) into a function/method?
  1168. mkdir($new_temppath);
  1169. }
  1170. if (App::directory_usable($new_temppath)) {
  1171. // The new path is usable, we are happy
  1172. Config::set("system", "temppath", $new_temppath);
  1173. return $new_temppath;
  1174. } else {
  1175. // We can't create a subdirectory, strange.
  1176. // But the directory seems to work, so we use it but don't store it.
  1177. return $temppath;
  1178. }
  1179. }
  1180. // Reaching this point means that the operating system is configured badly.
  1181. return '';
  1182. }
  1183. function get_cachefile($file, $writemode = true) {
  1184. $cache = get_itemcachepath();
  1185. if ((!$cache) || (!is_dir($cache))) {
  1186. return("");
  1187. }
  1188. $subfolder = $cache . "/" . substr($file, 0, 2);
  1189. $cachepath = $subfolder . "/" . $file;
  1190. if ($writemode) {
  1191. if (!is_dir($subfolder)) {
  1192. mkdir($subfolder);
  1193. chmod($subfolder, 0777);
  1194. }
  1195. }
  1196. /// @TODO no need to put braces here
  1197. return $cachepath;
  1198. }
  1199. function clear_cache($basepath = "", $path = "") {
  1200. if ($path == "") {
  1201. $basepath = get_itemcachepath();
  1202. $path = $basepath;
  1203. }
  1204. if (($path == "") || (!is_dir($path))) {
  1205. return;
  1206. }
  1207. if (substr(realpath($path), 0, strlen($basepath)) != $basepath) {
  1208. return;
  1209. }
  1210. $cachetime = (int) Config::get('system', 'itemcache_duration');
  1211. if ($cachetime == 0) {
  1212. $cachetime = 86400;
  1213. }
  1214. if (is_writable($path)) {
  1215. if ($dh = opendir($path)) {
  1216. while (($file = readdir($dh)) !== false) {
  1217. $fullpath = $path . "/" . $file;
  1218. if ((filetype($fullpath) == "dir") && ($file != ".") && ($file != "..")) {
  1219. clear_cache($basepath, $fullpath);
  1220. }
  1221. if ((filetype($fullpath) == "file") && (filectime($fullpath) < (time() - $cachetime))) {
  1222. unlink($fullpath);
  1223. }
  1224. }
  1225. closedir($dh);
  1226. }
  1227. }
  1228. }
  1229. function get_itemcachepath() {
  1230. // Checking, if the cache is deactivated
  1231. $cachetime = (int) Config::get('system', 'itemcache_duration');
  1232. if ($cachetime < 0) {
  1233. return "";
  1234. }
  1235. $itemcache = Config::get('system', 'itemcache');
  1236. if (($itemcache != "") && App::directory_usable($itemcache)) {
  1237. return App::realpath($itemcache);
  1238. }
  1239. $temppath = get_temppath();
  1240. if ($temppath != "") {
  1241. $itemcache = $temppath . "/itemcache";
  1242. if (!file_exists($itemcache) && !is_dir($itemcache)) {
  1243. mkdir($itemcache);
  1244. }
  1245. if (App::directory_usable($itemcache)) {
  1246. Config::set("system", "itemcache", $itemcache);
  1247. return $itemcache;
  1248. }
  1249. }
  1250. return "";
  1251. }
  1252. /**
  1253. * @brief Returns the path where spool files are stored
  1254. *
  1255. * @return string Spool path
  1256. */
  1257. function get_spoolpath() {
  1258. $spoolpath = Config::get('system', 'spoolpath');
  1259. if (($spoolpath != "") && App::directory_usable($spoolpath)) {
  1260. // We have a spool path and it is usable
  1261. return $spoolpath;
  1262. }
  1263. // We don't have a working preconfigured spool path, so we take the temp path.
  1264. $temppath = get_temppath();
  1265. if ($temppath != "") {
  1266. // To avoid any interferences with other systems we create our own directory
  1267. $spoolpath = $temppath . "/spool";
  1268. if (!is_dir($spoolpath)) {
  1269. mkdir($spoolpath);
  1270. }
  1271. if (App::directory_usable($spoolpath)) {
  1272. // The new path is usable, we are happy
  1273. Config::set("system", "spoolpath", $spoolpath);
  1274. return $spoolpath;
  1275. } else {
  1276. // We can't create a subdirectory, strange.
  1277. // But the directory seems to work, so we use it but don't store it.
  1278. return $temppath;
  1279. }
  1280. }
  1281. // Reaching this point means that the operating system is configured badly.
  1282. return "";
  1283. }
  1284. /// @deprecated
  1285. function set_template_engine(App $a, $engine = 'internal') {
  1286. /// @note This function is no longer necessary, but keep it as a wrapper to the class method
  1287. /// to avoid breaking themes again unnecessarily
  1288. /// @TODO maybe output a warning here so the theme developer can see it? PHP won't show such warnings like Java does.
  1289. $a->set_template_engine($engine);
  1290. }
  1291. if (!function_exists('exif_imagetype')) {
  1292. function exif_imagetype($file) {
  1293. $size = getimagesize($file);
  1294. return $size[2];
  1295. }
  1296. }
  1297. function validate_include(&$file) {
  1298. $orig_file = $file;
  1299. $file = realpath($file);
  1300. if (strpos($file, getcwd()) !== 0) {
  1301. return false;
  1302. }
  1303. $file = str_replace(getcwd() . "/", "", $file, $count);
  1304. if ($count != 1) {
  1305. return false;
  1306. }
  1307. if ($orig_file !== $file) {
  1308. return false;
  1309. }
  1310. $valid = false;
  1311. if (strpos($file, "include/") === 0) {
  1312. $valid = true;
  1313. }
  1314. if (strpos($file, "addon/") === 0) {
  1315. $valid = true;
  1316. }
  1317. // Simply return flag
  1318. return ($valid);
  1319. }
  1320. function current_load() {
  1321. if (!function_exists('sys_getloadavg')) {
  1322. return false;
  1323. }
  1324. $load_arr = sys_getloadavg();
  1325. if (!is_array($load_arr)) {
  1326. return false;
  1327. }
  1328. return max($load_arr[0], $load_arr[1]);
  1329. }
  1330. /**
  1331. * @brief get c-style args
  1332. *
  1333. * @return int
  1334. */
  1335. function argc() {
  1336. return get_app()->argc;
  1337. }
  1338. /**
  1339. * @brief Returns the value of a argv key
  1340. *
  1341. * @param int $x argv key
  1342. * @return string Value of the argv key
  1343. */
  1344. function argv($x) {
  1345. if (array_key_exists($x, get_app()->argv)) {
  1346. return get_app()->argv[$x];
  1347. }
  1348. return '';
  1349. }
  1350. /**
  1351. * @brief Get the data which is needed for infinite scroll
  1352. *
  1353. * For invinite scroll we need the page number of the actual page
  1354. * and the the URI where the content of the next page comes from.
  1355. * This data is needed for the js part in main.js.
  1356. * Note: infinite scroll does only work for the network page (module)
  1357. *
  1358. * @param string $module The name of the module (e.g. "network")
  1359. * @return array Of infinite scroll data
  1360. * 'pageno' => $pageno The number of the actual page
  1361. * 'reload_uri' => $reload_uri The URI of the content we have to load
  1362. */
  1363. function infinite_scroll_data($module) {
  1364. if (PConfig::get(local_user(), 'system', 'infinite_scroll')
  1365. && ($module == "network") && ($_GET["mode"] != "minimal")) {
  1366. // get the page number
  1367. if (is_string($_GET["page"])) {
  1368. $pageno = $_GET["page"];
  1369. } else {
  1370. $pageno = 1;
  1371. }
  1372. $reload_uri = "";
  1373. // try to get the uri from which we load the content
  1374. foreach ($_GET AS $param => $value) {
  1375. if (($param != "page") && ($param != "q")) {
  1376. $reload_uri .= "&" . $param . "=" . urlencode($value);
  1377. }
  1378. }
  1379. if (($a->page_offset != "") && ! strstr($reload_uri, "&offset=")) {
  1380. $reload_uri .= "&offset=" . urlencode($a->page_offset);
  1381. }
  1382. $arr = array("pageno" => $pageno, "reload_uri" => $reload_uri);
  1383. return $arr;
  1384. }
  1385. }