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.

1724 lines
47 KiB

11 years ago
10 years ago
9 years ago
11 years ago
11 years ago
10 years ago
11 years ago
11 years ago
11 years ago
10 years ago
11 years ago
10 years ago
9 years ago
11 years ago
10 years ago
11 years ago
9 years ago
11 years ago
9 years ago
11 years ago
10 years ago
11 years ago
11 years ago
11 years ago
9 years ago
9 years ago
11 years ago
10 years ago
10 years ago
10 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
10 years ago
11 years ago
9 years ago
9 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
10 years ago
10 years ago
10 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
10 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
9 years ago
10 years ago
  1. <?php
  2. require_once('include/config.php');
  3. require_once('include/network.php');
  4. require_once('include/plugin.php');
  5. require_once('include/text.php');
  6. require_once('include/datetime.php');
  7. require_once('include/pgettext.php');
  8. require_once('include/nav.php');
  9. require_once('include/cache.php');
  10. require_once('library/Mobile_Detect/Mobile_Detect.php');
  11. define ( 'FRIENDICA_PLATFORM', 'Friendica');
  12. define ( 'FRIENDICA_VERSION', '3.0.1426' );
  13. define ( 'DFRN_PROTOCOL_VERSION', '2.23' );
  14. define ( 'DB_UPDATE_VERSION', 1154 );
  15. define ( 'EOL', "<br />\r\n" );
  16. define ( 'ATOM_TIME', 'Y-m-d\TH:i:s\Z' );
  17. /**
  18. *
  19. * Image storage quality. Lower numbers save space at cost of image detail.
  20. * For ease of upgrade, please do not change here. Change jpeg quality with
  21. * $a->config['system']['jpeg_quality'] = n;
  22. * in .htconfig.php, where n is netween 1 and 100, and with very poor results
  23. * below about 50
  24. *
  25. */
  26. define ( 'JPEG_QUALITY', 100 );
  27. /**
  28. * $a->config['system']['png_quality'] from 0 (uncompressed) to 9
  29. */
  30. define ( 'PNG_QUALITY', 8 );
  31. /**
  32. *
  33. * An alternate way of limiting picture upload sizes. Specify the maximum pixel
  34. * length that pictures are allowed to be (for non-square pictures, it will apply
  35. * to the longest side). Pictures longer than this length will be resized to be
  36. * this length (on the longest side, the other side will be scaled appropriately).
  37. * Modify this value using
  38. *
  39. * $a->config['system']['max_image_length'] = n;
  40. *
  41. * in .htconfig.php
  42. *
  43. * If you don't want to set a maximum length, set to -1. The default value is
  44. * defined by 'MAX_IMAGE_LENGTH' below.
  45. *
  46. */
  47. define ( 'MAX_IMAGE_LENGTH', -1 );
  48. /**
  49. * Not yet used
  50. */
  51. define ( 'DEFAULT_DB_ENGINE', 'MyISAM' );
  52. /**
  53. * SSL redirection policies
  54. */
  55. define ( 'SSL_POLICY_NONE', 0 );
  56. define ( 'SSL_POLICY_FULL', 1 );
  57. define ( 'SSL_POLICY_SELFSIGN', 2 );
  58. /**
  59. * log levels
  60. */
  61. define ( 'LOGGER_NORMAL', 0 );
  62. define ( 'LOGGER_TRACE', 1 );
  63. define ( 'LOGGER_DEBUG', 2 );
  64. define ( 'LOGGER_DATA', 3 );
  65. define ( 'LOGGER_ALL', 4 );
  66. /**
  67. * registration policies
  68. */
  69. define ( 'REGISTER_CLOSED', 0 );
  70. define ( 'REGISTER_APPROVE', 1 );
  71. define ( 'REGISTER_OPEN', 2 );
  72. /**
  73. * relationship types
  74. */
  75. define ( 'CONTACT_IS_FOLLOWER', 1);
  76. define ( 'CONTACT_IS_SHARING', 2);
  77. define ( 'CONTACT_IS_FRIEND', 3);
  78. /**
  79. * DB update return values
  80. */
  81. define ( 'UPDATE_SUCCESS', 0);
  82. define ( 'UPDATE_FAILED', 1);
  83. /**
  84. *
  85. * page/profile types
  86. *
  87. * PAGE_NORMAL is a typical personal profile account
  88. * PAGE_SOAPBOX automatically approves all friend requests as CONTACT_IS_SHARING, (readonly)
  89. * PAGE_COMMUNITY automatically approves all friend requests as CONTACT_IS_SHARING, but with
  90. * write access to wall and comments (no email and not included in page owner's ACL lists)
  91. * PAGE_FREELOVE automatically approves all friend requests as full friends (CONTACT_IS_FRIEND).
  92. *
  93. */
  94. define ( 'PAGE_NORMAL', 0 );
  95. define ( 'PAGE_SOAPBOX', 1 );
  96. define ( 'PAGE_COMMUNITY', 2 );
  97. define ( 'PAGE_FREELOVE', 3 );
  98. define ( 'PAGE_BLOG', 4 );
  99. define ( 'PAGE_PRVGROUP', 5 );
  100. /**
  101. * Network and protocol family types
  102. */
  103. define ( 'NETWORK_DFRN', 'dfrn'); // Friendica, Mistpark, other DFRN implementations
  104. define ( 'NETWORK_ZOT', 'zot!'); // Zot!
  105. define ( 'NETWORK_OSTATUS', 'stat'); // status.net, identi.ca, GNU-social, other OStatus implementations
  106. define ( 'NETWORK_FEED', 'feed'); // RSS/Atom feeds with no known "post/notify" protocol
  107. define ( 'NETWORK_DIASPORA', 'dspr'); // Diaspora
  108. define ( 'NETWORK_MAIL', 'mail'); // IMAP/POP
  109. define ( 'NETWORK_MAIL2', 'mai2'); // extended IMAP/POP
  110. define ( 'NETWORK_FACEBOOK', 'face'); // Facebook API
  111. define ( 'NETWORK_LINKEDIN', 'lnkd'); // LinkedIn
  112. define ( 'NETWORK_XMPP', 'xmpp'); // XMPP
  113. define ( 'NETWORK_MYSPACE', 'mysp'); // MySpace
  114. define ( 'NETWORK_GPLUS', 'goog'); // Google+
  115. define ( 'NETWORK_PHANTOM', 'unkn'); // Place holder
  116. /**
  117. * These numbers are used in stored permissions
  118. * and existing allocations MUST NEVER BE CHANGED
  119. * OR RE-ASSIGNED! You may only add to them.
  120. */
  121. $netgroup_ids = array(
  122. NETWORK_DFRN => (-1),
  123. NETWORK_ZOT => (-2),
  124. NETWORK_OSTATUS => (-3),
  125. NETWORK_FEED => (-4),
  126. NETWORK_DIASPORA => (-5),
  127. NETWORK_MAIL => (-6),
  128. NETWORK_MAIL2 => (-7),
  129. NETWORK_FACEBOOK => (-8),
  130. NETWORK_LINKEDIN => (-9),
  131. NETWORK_XMPP => (-10),
  132. NETWORK_MYSPACE => (-11),
  133. NETWORK_GPLUS => (-12),
  134. NETWORK_PHANTOM => (-127),
  135. );
  136. /**
  137. * Maximum number of "people who like (or don't like) this" that we will list by name
  138. */
  139. define ( 'MAX_LIKERS', 75);
  140. /**
  141. * Communication timeout
  142. */
  143. define ( 'ZCURL_TIMEOUT' , (-1));
  144. /**
  145. * email notification options
  146. */
  147. define ( 'NOTIFY_INTRO', 0x0001 );
  148. define ( 'NOTIFY_CONFIRM', 0x0002 );
  149. define ( 'NOTIFY_WALL', 0x0004 );
  150. define ( 'NOTIFY_COMMENT', 0x0008 );
  151. define ( 'NOTIFY_MAIL', 0x0010 );
  152. define ( 'NOTIFY_SUGGEST', 0x0020 );
  153. define ( 'NOTIFY_PROFILE', 0x0040 );
  154. define ( 'NOTIFY_TAGSELF', 0x0080 );
  155. define ( 'NOTIFY_TAGSHARE', 0x0100 );
  156. define ( 'NOTIFY_POKE', 0x0200 );
  157. define ( 'NOTIFY_SYSTEM', 0x8000 );
  158. /**
  159. * Tag/term types
  160. */
  161. define ( 'TERM_UNKNOWN', 0 );
  162. define ( 'TERM_HASHTAG', 1 );
  163. define ( 'TERM_MENTION', 2 );
  164. define ( 'TERM_CATEGORY', 3 );
  165. define ( 'TERM_PCATEGORY', 4 );
  166. define ( 'TERM_FILE', 5 );
  167. define ( 'TERM_OBJ_POST', 1 );
  168. define ( 'TERM_OBJ_PHOTO', 2 );
  169. /**
  170. * various namespaces we may need to parse
  171. */
  172. define ( 'NAMESPACE_ZOT', 'http://purl.org/zot' );
  173. define ( 'NAMESPACE_DFRN' , 'http://purl.org/macgirvin/dfrn/1.0' );
  174. define ( 'NAMESPACE_THREAD' , 'http://purl.org/syndication/thread/1.0' );
  175. define ( 'NAMESPACE_TOMB' , 'http://purl.org/atompub/tombstones/1.0' );
  176. define ( 'NAMESPACE_ACTIVITY', 'http://activitystrea.ms/spec/1.0/' );
  177. define ( 'NAMESPACE_ACTIVITY_SCHEMA', 'http://activitystrea.ms/schema/1.0/' );
  178. define ( 'NAMESPACE_MEDIA', 'http://purl.org/syndication/atommedia' );
  179. define ( 'NAMESPACE_SALMON_ME', 'http://salmon-protocol.org/ns/magic-env' );
  180. define ( 'NAMESPACE_OSTATUSSUB', 'http://ostatus.org/schema/1.0/subscribe' );
  181. define ( 'NAMESPACE_GEORSS', 'http://www.georss.org/georss' );
  182. define ( 'NAMESPACE_POCO', 'http://portablecontacts.net/spec/1.0' );
  183. define ( 'NAMESPACE_FEED', 'http://schemas.google.com/g/2010#updates-from' );
  184. define ( 'NAMESPACE_OSTATUS', 'http://ostatus.org/schema/1.0' );
  185. define ( 'NAMESPACE_STATUSNET', 'http://status.net/schema/api/1/' );
  186. define ( 'NAMESPACE_ATOM1', 'http://www.w3.org/2005/Atom' );
  187. /**
  188. * activity stream defines
  189. */
  190. define ( 'ACTIVITY_LIKE', NAMESPACE_ACTIVITY_SCHEMA . 'like' );
  191. define ( 'ACTIVITY_DISLIKE', NAMESPACE_DFRN . '/dislike' );
  192. define ( 'ACTIVITY_OBJ_HEART', NAMESPACE_DFRN . '/heart' );
  193. define ( 'ACTIVITY_FRIEND', NAMESPACE_ACTIVITY_SCHEMA . 'make-friend' );
  194. define ( 'ACTIVITY_REQ_FRIEND', NAMESPACE_ACTIVITY_SCHEMA . 'request-friend' );
  195. define ( 'ACTIVITY_UNFRIEND', NAMESPACE_ACTIVITY_SCHEMA . 'remove-friend' );
  196. define ( 'ACTIVITY_FOLLOW', NAMESPACE_ACTIVITY_SCHEMA . 'follow' );
  197. define ( 'ACTIVITY_UNFOLLOW', NAMESPACE_ACTIVITY_SCHEMA . 'stop-following' );
  198. define ( 'ACTIVITY_JOIN', NAMESPACE_ACTIVITY_SCHEMA . 'join' );
  199. define ( 'ACTIVITY_POST', NAMESPACE_ACTIVITY_SCHEMA . 'post' );
  200. define ( 'ACTIVITY_UPDATE', NAMESPACE_ACTIVITY_SCHEMA . 'update' );
  201. define ( 'ACTIVITY_TAG', NAMESPACE_ACTIVITY_SCHEMA . 'tag' );
  202. define ( 'ACTIVITY_FAVORITE', NAMESPACE_ACTIVITY_SCHEMA . 'favorite' );
  203. define ( 'ACTIVITY_POKE', NAMESPACE_ZOT . '/activity/poke' );
  204. define ( 'ACTIVITY_OBJ_COMMENT', NAMESPACE_ACTIVITY_SCHEMA . 'comment' );
  205. define ( 'ACTIVITY_OBJ_NOTE', NAMESPACE_ACTIVITY_SCHEMA . 'note' );
  206. define ( 'ACTIVITY_OBJ_PERSON', NAMESPACE_ACTIVITY_SCHEMA . 'person' );
  207. define ( 'ACTIVITY_OBJ_PHOTO', NAMESPACE_ACTIVITY_SCHEMA . 'photo' );
  208. define ( 'ACTIVITY_OBJ_P_PHOTO', NAMESPACE_ACTIVITY_SCHEMA . 'profile-photo' );
  209. define ( 'ACTIVITY_OBJ_ALBUM', NAMESPACE_ACTIVITY_SCHEMA . 'photo-album' );
  210. define ( 'ACTIVITY_OBJ_EVENT', NAMESPACE_ACTIVITY_SCHEMA . 'event' );
  211. define ( 'ACTIVITY_OBJ_GROUP', NAMESPACE_ACTIVITY_SCHEMA . 'group' );
  212. define ( 'ACTIVITY_OBJ_TAGTERM', NAMESPACE_DFRN . '/tagterm' );
  213. define ( 'ACTIVITY_OBJ_PROFILE', NAMESPACE_DFRN . '/profile' );
  214. /**
  215. * item weight for query ordering
  216. */
  217. define ( 'GRAVITY_PARENT', 0);
  218. define ( 'GRAVITY_LIKE', 3);
  219. define ( 'GRAVITY_COMMENT', 6);
  220. /**
  221. *
  222. * Reverse the effect of magic_quotes_gpc if it is enabled.
  223. * Please disable magic_quotes_gpc so we don't have to do this.
  224. * See http://php.net/manual/en/security.magicquotes.disabling.php
  225. *
  226. */
  227. function startup() {
  228. error_reporting(E_ERROR | E_WARNING | E_PARSE);
  229. set_time_limit(0);
  230. // This has to be quite large to deal with embedded private photos
  231. ini_set('pcre.backtrack_limit', 500000);
  232. if (get_magic_quotes_gpc()) {
  233. $process = array(&$_GET, &$_POST, &$_COOKIE, &$_REQUEST);
  234. while (list($key, $val) = each($process)) {
  235. foreach ($val as $k => $v) {
  236. unset($process[$key][$k]);
  237. if (is_array($v)) {
  238. $process[$key][stripslashes($k)] = $v;
  239. $process[] = &$process[$key][stripslashes($k)];
  240. } else {
  241. $process[$key][stripslashes($k)] = stripslashes($v);
  242. }
  243. }
  244. }
  245. unset($process);
  246. }
  247. }
  248. /**
  249. *
  250. * class: App
  251. *
  252. * Our main application structure for the life of this page
  253. * Primarily deals with the URL that got us here
  254. * and tries to make some sense of it, and
  255. * stores our page contents and config storage
  256. * and anything else that might need to be passed around
  257. * before we spit the page out.
  258. *
  259. */
  260. if(! class_exists('App')) {
  261. class App {
  262. public $module_loaded = false;
  263. public $query_string;
  264. public $config;
  265. public $page;
  266. public $profile;
  267. public $user;
  268. public $cid;
  269. public $contact;
  270. public $contacts;
  271. public $page_contact;
  272. public $content;
  273. public $data = array();
  274. public $error = false;
  275. public $cmd;
  276. public $argv;
  277. public $argc;
  278. public $module;
  279. public $pager;
  280. public $strings;
  281. public $path;
  282. public $hooks;
  283. public $timezone;
  284. public $interactive = true;
  285. public $plugins;
  286. public $apps = array();
  287. public $identities;
  288. public $nav_sel;
  289. public $category;
  290. // Allow themes to control internal parameters
  291. // by changing App values in theme.php
  292. //
  293. // Possibly should make these part of the plugin
  294. // system, but it seems like overkill to invoke
  295. // all the plugin machinery just to change a couple
  296. // of values
  297. public $sourcename = '';
  298. public $videowidth = 425;
  299. public $videoheight = 350;
  300. private $scheme;
  301. private $hostname;
  302. private $baseurl;
  303. private $db;
  304. private $curl_code;
  305. private $curl_headers;
  306. private $cached_profile_image;
  307. private $cached_profile_picdate;
  308. function __construct() {
  309. global $default_timezone;
  310. $this->timezone = ((x($default_timezone)) ? $default_timezone : 'UTC');
  311. date_default_timezone_set($this->timezone);
  312. $this->config = array();
  313. $this->page = array();
  314. $this->pager= array();
  315. $this->query_string = '';
  316. startup();
  317. $this->scheme = 'http';
  318. if(x($_SERVER,'HTTPS') && $_SERVER['HTTPS'])
  319. $this->scheme = 'https';
  320. elseif(x($_SERVER,'SERVER_PORT') && (intval($_SERVER['SERVER_PORT']) == 443))
  321. $this->scheme = 'https';
  322. if(x($_SERVER,'SERVER_NAME')) {
  323. $this->hostname = $_SERVER['SERVER_NAME'];
  324. // See bug 437 - this didn't work so disabling it
  325. //if(stristr($this->hostname,'xn--')) {
  326. // PHP or webserver may have converted idn to punycode, so
  327. // convert punycode back to utf-8
  328. // require_once('library/simplepie/idn/idna_convert.class.php');
  329. // $x = new idna_convert();
  330. // $this->hostname = $x->decode($_SERVER['SERVER_NAME']);
  331. //}
  332. if(x($_SERVER,'SERVER_PORT') && $_SERVER['SERVER_PORT'] != 80 && $_SERVER['SERVER_PORT'] != 443)
  333. $this->hostname .= ':' . $_SERVER['SERVER_PORT'];
  334. /**
  335. * Figure out if we are running at the top of a domain
  336. * or in a sub-directory and adjust accordingly
  337. */
  338. $path = trim(dirname($_SERVER['SCRIPT_NAME']),'/\\');
  339. if(isset($path) && strlen($path) && ($path != $this->path))
  340. $this->path = $path;
  341. }
  342. set_include_path(
  343. "include/$this->hostname" . PATH_SEPARATOR
  344. . 'include' . PATH_SEPARATOR
  345. . 'library' . PATH_SEPARATOR
  346. . 'library/phpsec' . PATH_SEPARATOR
  347. . 'library/langdet' . PATH_SEPARATOR
  348. . '.' );
  349. if((x($_SERVER,'QUERY_STRING')) && substr($_SERVER['QUERY_STRING'],0,2) === "q=") {
  350. $this->query_string = substr($_SERVER['QUERY_STRING'],2);
  351. // removing trailing / - maybe a nginx problem
  352. if (substr($this->query_string, 0, 1) == "/")
  353. $this->query_string = substr($this->query_string, 1);
  354. }
  355. if(x($_GET,'q'))
  356. $this->cmd = trim($_GET['q'],'/\\');
  357. // unix style "homedir"
  358. if(substr($this->cmd,0,1) === '~')
  359. $this->cmd = 'profile/' . substr($this->cmd,1);
  360. // Diaspora style profile url
  361. if(substr($this->cmd,0,2) === 'u/')
  362. $this->cmd = 'profile/' . substr($this->cmd,2);
  363. /**
  364. *
  365. * Break the URL path into C style argc/argv style arguments for our
  366. * modules. Given "http://example.com/module/arg1/arg2", $this->argc
  367. * will be 3 (integer) and $this->argv will contain:
  368. * [0] => 'module'
  369. * [1] => 'arg1'
  370. * [2] => 'arg2'
  371. *
  372. *
  373. * There will always be one argument. If provided a naked domain
  374. * URL, $this->argv[0] is set to "home".
  375. *
  376. */
  377. $this->argv = explode('/',$this->cmd);
  378. $this->argc = count($this->argv);
  379. if((array_key_exists('0',$this->argv)) && strlen($this->argv[0])) {
  380. $this->module = str_replace(".", "_", $this->argv[0]);
  381. }
  382. else {
  383. $this->argc = 1;
  384. $this->argv = array('home');
  385. $this->module = 'home';
  386. }
  387. /**
  388. * Special handling for the webfinger/lrdd host XRD file
  389. */
  390. if($this->cmd === '.well-known/host-meta') {
  391. $this->argc = 1;
  392. $this->argv = array('hostxrd');
  393. $this->module = 'hostxrd';
  394. }
  395. /**
  396. * See if there is any page number information, and initialise
  397. * pagination
  398. */
  399. $this->pager['page'] = ((x($_GET,'page') && intval($_GET['page']) > 0) ? intval($_GET['page']) : 1);
  400. $this->pager['itemspage'] = 50;
  401. $this->pager['start'] = ($this->pager['page'] * $this->pager['itemspage']) - $this->pager['itemspage'];
  402. if($this->pager['start'] < 0)
  403. $this->pager['start'] = 0;
  404. $this->pager['total'] = 0;
  405. }
  406. function get_baseurl($ssl = false) {
  407. $scheme = $this->scheme;
  408. if((x($this->config,'system')) && (x($this->config['system'],'ssl_policy'))) {
  409. if(intval($this->config['system']['ssl_policy']) === intval(SSL_POLICY_FULL))
  410. $scheme = 'https';
  411. // Basically, we have $ssl = true on any links which can only be seen by a logged in user
  412. // (and also the login link). Anything seen by an outsider will have it turned off.
  413. if($this->config['system']['ssl_policy'] == SSL_POLICY_SELFSIGN) {
  414. if($ssl)
  415. $scheme = 'https';
  416. else
  417. $scheme = 'http';
  418. }
  419. }
  420. $this->baseurl = $scheme . "://" . $this->hostname . ((isset($this->path) && strlen($this->path)) ? '/' . $this->path : '' );
  421. return $this->baseurl;
  422. }
  423. function set_baseurl($url) {
  424. $parsed = @parse_url($url);
  425. $this->baseurl = $url;
  426. if($parsed) {
  427. $this->scheme = $parsed['scheme'];
  428. $this->hostname = $parsed['host'];
  429. if(x($parsed,'port'))
  430. $this->hostname .= ':' . $parsed['port'];
  431. if(x($parsed,'path'))
  432. $this->path = trim($parsed['path'],'\\/');
  433. }
  434. }
  435. function get_hostname() {
  436. return $this->hostname;
  437. }
  438. function set_hostname($h) {
  439. $this->hostname = $h;
  440. }
  441. function set_path($p) {
  442. $this->path = trim(trim($p),'/');
  443. }
  444. function get_path() {
  445. return $this->path;
  446. }
  447. function set_pager_total($n) {
  448. $this->pager['total'] = intval($n);
  449. }
  450. function set_pager_itemspage($n) {
  451. $this->pager['itemspage'] = ((intval($n) > 0) ? intval($n) : 0);
  452. $this->pager['start'] = ($this->pager['page'] * $this->pager['itemspage']) - $this->pager['itemspage'];
  453. }
  454. function init_pagehead() {
  455. $interval = ((local_user()) ? get_pconfig(local_user(),'system','update_interval') : 40000);
  456. if($interval < 10000)
  457. $interval = 40000;
  458. $this->page['title'] = $this->config['sitename'];
  459. $tpl = get_markup_template('head.tpl');
  460. $this->page['htmlhead'] = replace_macros($tpl,array(
  461. '$baseurl' => $this->get_baseurl(), // FIXME for z_path!!!!
  462. '$local_user' => local_user(),
  463. '$generator' => 'Friendica' . ' ' . FRIENDICA_VERSION,
  464. '$delitem' => t('Delete this item?'),
  465. '$comment' => t('Comment'),
  466. '$showmore' => t('show more'),
  467. '$showfewer' => t('show fewer'),
  468. '$update_interval' => $interval
  469. ));
  470. }
  471. function init_page_end() {
  472. $tpl = get_markup_template('end.tpl');
  473. $this->page['end'] = replace_macros($tpl,array(
  474. '$baseurl' => $this->get_baseurl() // FIXME for z_path!!!!
  475. ));
  476. }
  477. function set_curl_code($code) {
  478. $this->curl_code = $code;
  479. }
  480. function get_curl_code() {
  481. return $this->curl_code;
  482. }
  483. function set_curl_headers($headers) {
  484. $this->curl_headers = $headers;
  485. }
  486. function get_curl_headers() {
  487. return $this->curl_headers;
  488. }
  489. function get_cached_avatar_image($avatar_image){
  490. if($this->cached_profile_image[$avatar_image])
  491. return $this->cached_profile_image[$avatar_image];
  492. $path_parts = explode("/",$avatar_image);
  493. $common_filename = $path_parts[count($path_parts)-1];
  494. if($this->cached_profile_picdate[$common_filename]){
  495. $this->cached_profile_image[$avatar_image] = $avatar_image . $this->cached_profile_picdate[$common_filename];
  496. } else {
  497. $r = q("SELECT `contact`.`avatar-date` AS picdate FROM `contact` WHERE `contact`.`thumb` like \"%%/%s\"",
  498. $common_filename);
  499. if(! count($r)){
  500. $this->cached_profile_image[$avatar_image] = $avatar_image;
  501. } else {
  502. $this->cached_profile_picdate[$common_filename] = "?rev=" . urlencode($r[0]['picdate']);
  503. $this->cached_profile_image[$avatar_image] = $avatar_image . $this->cached_profile_picdate[$common_filename];
  504. }
  505. }
  506. return $this->cached_profile_image[$avatar_image];
  507. }
  508. }
  509. }
  510. // retrieve the App structure
  511. // useful in functions which require it but don't get it passed to them
  512. if(! function_exists('get_app')) {
  513. function get_app() {
  514. global $a;
  515. return $a;
  516. }
  517. };
  518. // Multi-purpose function to check variable state.
  519. // Usage: x($var) or $x($array,'key')
  520. // returns false if variable/key is not set
  521. // if variable is set, returns 1 if has 'non-zero' value, otherwise returns 0.
  522. // e.g. x('') or x(0) returns 0;
  523. if(! function_exists('x')) {
  524. function x($s,$k = NULL) {
  525. if($k != NULL) {
  526. if((is_array($s)) && (array_key_exists($k,$s))) {
  527. if($s[$k])
  528. return (int) 1;
  529. return (int) 0;
  530. }
  531. return false;
  532. }
  533. else {
  534. if(isset($s)) {
  535. if($s) {
  536. return (int) 1;
  537. }
  538. return (int) 0;
  539. }
  540. return false;
  541. }
  542. }
  543. }
  544. // called from db initialisation if db is dead.
  545. if(! function_exists('system_unavailable')) {
  546. function system_unavailable() {
  547. include('system_unavailable.php');
  548. system_down();
  549. killme();
  550. }
  551. }
  552. function clean_urls() {
  553. global $a;
  554. // if($a->config['system']['clean_urls'])
  555. return true;
  556. // return false;
  557. }
  558. function z_path() {
  559. global $a;
  560. $base = $a->get_baseurl();
  561. if(! clean_urls())
  562. $base .= '/?q=';
  563. return $base;
  564. }
  565. function z_root() {
  566. global $a;
  567. return $a->get_baseurl();
  568. }
  569. function absurl($path) {
  570. if(strpos($path,'/') === 0)
  571. return z_path() . $path;
  572. return $path;
  573. }
  574. function is_ajax() {
  575. return (!empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest');
  576. }
  577. // Primarily involved with database upgrade, but also sets the
  578. // base url for use in cmdline programs which don't have
  579. // $_SERVER variables, and synchronising the state of installed plugins.
  580. if(! function_exists('check_config')) {
  581. function check_config(&$a) {
  582. $build = get_config('system','build');
  583. if(! x($build))
  584. $build = set_config('system','build',DB_UPDATE_VERSION);
  585. $url = get_config('system','url');
  586. // if the url isn't set or the stored url is radically different
  587. // than the currently visited url, store the current value accordingly.
  588. // "Radically different" ignores common variations such as http vs https
  589. // and www.example.com vs example.com.
  590. if((! x($url)) || (! link_compare($url,$a->get_baseurl())))
  591. $url = set_config('system','url',$a->get_baseurl());
  592. if($build != DB_UPDATE_VERSION) {
  593. $stored = intval($build);
  594. $current = intval(DB_UPDATE_VERSION);
  595. if(($stored < $current) && file_exists('update.php')) {
  596. load_config('database');
  597. // We're reporting a different version than what is currently installed.
  598. // Run any existing update scripts to bring the database up to current.
  599. require_once('update.php');
  600. // make sure that boot.php and update.php are the same release, we might be
  601. // updating right this very second and the correct version of the update.php
  602. // file may not be here yet. This can happen on a very busy site.
  603. if(DB_UPDATE_VERSION == UPDATE_VERSION) {
  604. for($x = $stored; $x < $current; $x ++) {
  605. if(function_exists('update_' . $x)) {
  606. // There could be a lot of processes running or about to run.
  607. // We want exactly one process to run the update command.
  608. // So store the fact that we're taking responsibility
  609. // after first checking to see if somebody else already has.
  610. // If the update fails or times-out completely you may need to
  611. // delete the config entry to try again.
  612. $t = get_config('database','update_' . $x);
  613. if($t !== false)
  614. break;
  615. set_config('database','update_' . $x, time());
  616. // call the specific update
  617. $func = 'update_' . $x;
  618. $retval = $func();
  619. if($retval) {
  620. //send the administrator an e-mail
  621. $email_tpl = get_intltext_template("update_fail_eml.tpl");
  622. $email_msg = replace_macros($email_tpl, array(
  623. '$sitename' => $a->config['sitename'],
  624. '$siteurl' => $a->get_baseurl(),
  625. '$update' => $x,
  626. '$error' => sprintf( t('Update %s failed. See error logs.'), $x)
  627. ));
  628. $subject=sprintf(t('Update Error at %s'), $a->get_baseurl());
  629. mail($a->config['admin_email'], $subject, $email_msg,
  630. 'From: ' . t('Administrator') . '@' . $_SERVER['SERVER_NAME'] . "\n"
  631. . 'Content-type: text/plain; charset=UTF-8' . "\n"
  632. . 'Content-transfer-encoding: 8bit' );
  633. //try the logger
  634. logger('CRITICAL: Update Failed: '. $x);
  635. break;
  636. }
  637. else {
  638. set_config('database','update_' . $x, 'success');
  639. set_config('system','build', $x + 1);
  640. }
  641. }
  642. }
  643. }
  644. }
  645. }
  646. /**
  647. *
  648. * Synchronise plugins:
  649. *
  650. * $a->config['system']['addon'] contains a comma-separated list of names
  651. * of plugins/addons which are used on this system.
  652. * Go through the database list of already installed addons, and if we have
  653. * an entry, but it isn't in the config list, call the uninstall procedure
  654. * and mark it uninstalled in the database (for now we'll remove it).
  655. * Then go through the config list and if we have a plugin that isn't installed,
  656. * call the install procedure and add it to the database.
  657. *
  658. */
  659. $r = q("SELECT * FROM `addon` WHERE `installed` = 1");
  660. if(count($r))
  661. $installed = $r;
  662. else
  663. $installed = array();
  664. $plugins = get_config('system','addon');
  665. $plugins_arr = array();
  666. if($plugins)
  667. $plugins_arr = explode(',',str_replace(' ', '',$plugins));
  668. $a->plugins = $plugins_arr;
  669. $installed_arr = array();
  670. if(count($installed)) {
  671. foreach($installed as $i) {
  672. if(! in_array($i['name'],$plugins_arr)) {
  673. uninstall_plugin($i['name']);
  674. }
  675. else {
  676. $installed_arr[] = $i['name'];
  677. }
  678. }
  679. }
  680. if(count($plugins_arr)) {
  681. foreach($plugins_arr as $p) {
  682. if(! in_array($p,$installed_arr)) {
  683. install_plugin($p);
  684. }
  685. }
  686. }
  687. load_hooks();
  688. return;
  689. }
  690. }
  691. function get_guid($size=16) {
  692. $exists = true; // assume by default that we don't have a unique guid
  693. do {
  694. $s = random_string($size);
  695. $r = q("select id from guid where guid = '%s' limit 1", dbesc($s));
  696. if(! count($r))
  697. $exists = false;
  698. } while($exists);
  699. q("insert into guid ( guid ) values ( '%s' ) ", dbesc($s));
  700. return $s;
  701. }
  702. // wrapper for adding a login box. If $register == true provide a registration
  703. // link. This will most always depend on the value of $a->config['register_policy'].
  704. // returns the complete html for inserting into the page
  705. if(! function_exists('login')) {
  706. function login($register = false, $hiddens=false) {
  707. $a = get_app();
  708. $o = "";
  709. $reg = false;
  710. if ($register) {
  711. $reg = array(
  712. 'title' => t('Create a New Account'),
  713. 'desc' => t('Register')
  714. );
  715. }
  716. $noid = get_config('system','no_openid');
  717. $dest_url = $a->get_baseurl(true) . '/' . $a->query_string;
  718. if(local_user()) {
  719. $tpl = get_markup_template("logout.tpl");
  720. }
  721. else {
  722. $tpl = get_markup_template("login.tpl");
  723. $_SESSION['return_url'] = $a->query_string;
  724. }
  725. $o .= replace_macros($tpl,array(
  726. '$dest_url' => $dest_url,
  727. '$logout' => t('Logout'),
  728. '$login' => t('Login'),
  729. '$lname' => array('username', t('Nickname or Email address: ') , '', ''),
  730. '$lpassword' => array('password', t('Password: '), '', ''),
  731. '$openid' => !$noid,
  732. '$lopenid' => array('openid_url', t('Or login using OpenID: '),'',''),
  733. '$hiddens' => $hiddens,
  734. '$register' => $reg,
  735. '$lostpass' => t('Forgot your password?'),
  736. '$lostlink' => t('Password Reset'),
  737. ));
  738. call_hooks('login_hook',$o);
  739. return $o;
  740. }
  741. }
  742. // Used to end the current process, after saving session state.
  743. if(! function_exists('killme')) {
  744. function killme() {
  745. session_write_close();
  746. exit;
  747. }
  748. }
  749. // redirect to another URL and terminate this process.
  750. if(! function_exists('goaway')) {
  751. function goaway($s) {
  752. header("Location: $s");
  753. killme();
  754. }
  755. }
  756. // Returns the uid of locally logged in user or false.
  757. if(! function_exists('local_user')) {
  758. function local_user() {
  759. if((x($_SESSION,'authenticated')) && (x($_SESSION,'uid')))
  760. return intval($_SESSION['uid']);
  761. return false;
  762. }
  763. }
  764. // Returns contact id of authenticated site visitor or false
  765. if(! function_exists('remote_user')) {
  766. function remote_user() {
  767. if((x($_SESSION,'authenticated')) && (x($_SESSION,'visitor_id')))
  768. return intval($_SESSION['visitor_id']);
  769. return false;
  770. }
  771. }
  772. // contents of $s are displayed prominently on the page the next time
  773. // a page is loaded. Usually used for errors or alerts.
  774. if(! function_exists('notice')) {
  775. function notice($s) {
  776. $a = get_app();
  777. if(! x($_SESSION,'sysmsg')) $_SESSION['sysmsg'] = array();
  778. if($a->interactive)
  779. $_SESSION['sysmsg'][] = $s;
  780. }
  781. }
  782. if(! function_exists('info')) {
  783. function info($s) {
  784. $a = get_app();
  785. if(! x($_SESSION,'sysmsg_info')) $_SESSION['sysmsg_info'] = array();
  786. if($a->interactive)
  787. $_SESSION['sysmsg_info'][] = $s;
  788. }
  789. }
  790. // wrapper around config to limit the text length of an incoming message
  791. if(! function_exists('get_max_import_size')) {
  792. function get_max_import_size() {
  793. global $a;
  794. return ((x($a->config,'max_import_size')) ? $a->config['max_import_size'] : 0 );
  795. }
  796. }
  797. /**
  798. *
  799. * Function : profile_load
  800. * @parameter App $a
  801. * @parameter string $nickname
  802. * @parameter int $profile
  803. *
  804. * Summary: Loads a profile into the page sidebar.
  805. * The function requires a writeable copy of the main App structure, and the nickname
  806. * of a registered local account.
  807. *
  808. * If the viewer is an authenticated remote viewer, the profile displayed is the
  809. * one that has been configured for his/her viewing in the Contact manager.
  810. * Passing a non-zero profile ID can also allow a preview of a selected profile
  811. * by the owner.
  812. *
  813. * Profile information is placed in the App structure for later retrieval.
  814. * Honours the owner's chosen theme for display.
  815. *
  816. */
  817. if(! function_exists('profile_load')) {
  818. function profile_load(&$a, $nickname, $profile = 0) {
  819. if(remote_user()) {
  820. $r = q("SELECT `profile-id` FROM `contact` WHERE `id` = %d LIMIT 1",
  821. intval($_SESSION['visitor_id']));
  822. if(count($r))
  823. $profile = $r[0]['profile-id'];
  824. }
  825. $r = null;
  826. if($profile) {
  827. $profile_int = intval($profile);
  828. $r = q("SELECT `profile`.`uid` AS `profile_uid`, `profile`.* , `contact`.`avatar-date` AS picdate, `user`.* FROM `profile`
  829. left join `contact` on `contact`.`uid` = `profile`.`uid` LEFT JOIN `user` ON `profile`.`uid` = `user`.`uid`
  830. WHERE `user`.`nickname` = '%s' AND `profile`.`id` = %d and `contact`.`self` = 1 LIMIT 1",
  831. dbesc($nickname),
  832. intval($profile_int)
  833. );
  834. }
  835. if((! $r) && (! count($r))) {
  836. $r = q("SELECT `profile`.`uid` AS `profile_uid`, `profile`.* , `contact`.`avatar-date` AS picdate, `user`.* FROM `profile`
  837. left join `contact` on `contact`.`uid` = `profile`.`uid` LEFT JOIN `user` ON `profile`.`uid` = `user`.`uid`
  838. WHERE `user`.`nickname` = '%s' AND `profile`.`is-default` = 1 and `contact`.`self` = 1 LIMIT 1",
  839. dbesc($nickname)
  840. );
  841. }
  842. if(($r === false) || (! count($r))) {
  843. logger('profile error: ' . $a->query_string, LOGGER_DEBUG);
  844. notice( t('Requested profile is not available.') . EOL );
  845. $a->error = 404;
  846. return;
  847. }
  848. // fetch user tags if this isn't the default profile
  849. if(! $r[0]['is-default']) {
  850. $x = q("select `pub_keywords` from `profile` where uid = %d and `is-default` = 1 limit 1",
  851. intval($profile_uid)
  852. );
  853. if($x && count($x))
  854. $r[0]['pub_keywords'] = $x[0]['pub_keywords'];
  855. }
  856. $a->profile = $r[0];
  857. $a->page['title'] = $a->profile['name'] . " @ " . $a->config['sitename'];
  858. $_SESSION['theme'] = $a->profile['theme'];
  859. /**
  860. * load/reload current theme info
  861. */
  862. $theme_info_file = "view/theme/".current_theme()."/theme.php";
  863. if (file_exists($theme_info_file)){
  864. require_once($theme_info_file);
  865. }
  866. if(! (x($a->page,'aside')))
  867. $a->page['aside'] = '';
  868. if(local_user() && local_user() == $a->profile['uid']) {
  869. $a->page['aside'] .= replace_macros(get_markup_template('profile_edlink.tpl'),array(
  870. '$editprofile' => t('Edit profile'),
  871. '$profid' => $a->profile['id']
  872. ));
  873. }
  874. $block = (((get_config('system','block_public')) && (! local_user()) && (! remote_user())) ? true : false);
  875. $a->page['aside'] .= profile_sidebar($a->profile, $block);
  876. /*if(! $block)
  877. $a->page['aside'] .= contact_block();*/
  878. return;
  879. }
  880. }
  881. /**
  882. *
  883. * Function: profile_sidebar
  884. *
  885. * Formats a profile for display in the sidebar.
  886. * It is very difficult to templatise the HTML completely
  887. * because of all the conditional logic.
  888. *
  889. * @parameter: array $profile
  890. *
  891. * Returns HTML string stuitable for sidebar inclusion
  892. * Exceptions: Returns empty string if passed $profile is wrong type or not populated
  893. *
  894. */
  895. if(! function_exists('profile_sidebar')) {
  896. function profile_sidebar($profile, $block = 0) {
  897. $a = get_app();
  898. $o = '';
  899. $location = false;
  900. $address = false;
  901. $pdesc = true;
  902. if((! is_array($profile)) && (! count($profile)))
  903. return $o;
  904. $profile['picdate'] = urlencode($profile['picdate']);
  905. call_hooks('profile_sidebar_enter', $profile);
  906. // don't show connect link to yourself
  907. $connect = (($profile['uid'] != local_user()) ? t('Connect') : False);
  908. // don't show connect link to authenticated visitors either
  909. if((remote_user()) && ($_SESSION['visitor_visiting'] == $profile['uid']))
  910. $connect = False;
  911. if(get_my_url() && $profile['unkmail'])
  912. $wallmessage = t('Message');
  913. else
  914. $wallmessage = false;
  915. // show edit profile to yourself
  916. if ($profile['uid'] == local_user()) {
  917. $profile['edit'] = array($a->get_baseurl(). '/profiles', t('Profiles'),"", t('Manage/edit profiles'));
  918. $r = q("SELECT * FROM `profile` WHERE `uid` = %d",
  919. local_user());
  920. $profile['menu'] = array(
  921. 'chg_photo' => t('Change profile photo'),
  922. 'cr_new' => t('Create New Profile'),
  923. 'entries' => array(),
  924. );
  925. if(count($r)) {
  926. foreach($r as $rr) {
  927. $profile['menu']['entries'][] = array(
  928. 'photo' => $rr['thumb'],
  929. 'id' => $rr['id'],
  930. 'alt' => t('Profile Image'),
  931. 'profile_name' => $rr['profile-name'],
  932. 'isdefault' => $rr['is-default'],
  933. 'visibile_to_everybody' => t('visible to everybody'),
  934. 'edit_visibility' => t('Edit visibility'),
  935. );
  936. }
  937. }
  938. }
  939. if((x($profile,'address') == 1)
  940. || (x($profile,'locality') == 1)
  941. || (x($profile,'region') == 1)
  942. || (x($profile,'postal-code') == 1)
  943. || (x($profile,'country-name') == 1))
  944. $location = t('Location:');
  945. $gender = ((x($profile,'gender') == 1) ? t('Gender:') : False);
  946. $marital = ((x($profile,'marital') == 1) ? t('Status:') : False);
  947. $homepage = ((x($profile,'homepage') == 1) ? t('Homepage:') : False);
  948. if(($profile['hidewall'] || $block) && (! local_user()) && (! remote_user())) {
  949. $location = $pdesc = $gender = $marital = $homepage = False;
  950. }
  951. $firstname = ((strpos($profile['name'],' '))
  952. ? trim(substr($profile['name'],0,strpos($profile['name'],' '))) : $profile['name']);
  953. $lastname = (($firstname === $profile['name']) ? '' : trim(substr($profile['name'],strlen($firstname))));
  954. $diaspora = array(
  955. 'podloc' => $a->get_baseurl(),
  956. 'searchable' => (($profile['publish'] && $profile['net-publish']) ? 'true' : 'false' ),
  957. 'nickname' => $profile['nickname'],
  958. 'fullname' => $profile['name'],
  959. 'firstname' => $firstname,
  960. 'lastname' => $lastname,
  961. 'photo300' => $a->get_cached_avatar_image($a->get_baseurl() . '/photo/custom/300/' . $profile['uid'] . '.jpg'),
  962. 'photo100' => $a->get_cached_avatar_image($a->get_baseurl() . '/photo/custom/100/' . $profile['uid'] . '.jpg'),
  963. 'photo50' => $a->get_cached_avatar_image($a->get_baseurl() . '/photo/custom/50/' . $profile['uid'] . '.jpg'),
  964. );
  965. if (!$block){
  966. $contact_block = contact_block();
  967. }
  968. $tpl = get_markup_template('profile_vcard.tpl');
  969. $o .= replace_macros($tpl, array(
  970. '$profile' => $profile,
  971. '$connect' => $connect,
  972. '$wallmessage' => $wallmessage,
  973. '$location' => template_escape($location),
  974. '$gender' => $gender,
  975. '$pdesc' => $pdesc,
  976. '$marital' => $marital,
  977. '$homepage' => $homepage,
  978. '$diaspora' => $diaspora,
  979. '$contact_block' => $contact_block,
  980. ));
  981. $arr = array('profile' => &$profile, 'entry' => &$o);
  982. call_hooks('profile_sidebar', $arr);
  983. return $o;
  984. }
  985. }
  986. if(! function_exists('get_birthdays')) {
  987. function get_birthdays() {
  988. $a = get_app();
  989. $o = '';
  990. if(! local_user())
  991. return $o;
  992. $mobile_detect = new Mobile_Detect();
  993. $is_mobile = $mobile_detect->isMobile() || $mobile_detect->isTablet();
  994. if($is_mobile)
  995. return $o;
  996. $bd_format = t('g A l F d') ; // 8 AM Friday January 18
  997. $bd_short = t('F d');
  998. $r = q("SELECT `event`.*, `event`.`id` AS `eid`, `contact`.* FROM `event`
  999. LEFT JOIN `contact` ON `contact`.`id` = `event`.`cid`
  1000. WHERE `event`.`uid` = %d AND `type` = 'birthday' AND `start` < '%s' AND `finish` > '%s'
  1001. ORDER BY `start` ASC ",
  1002. intval(local_user()),
  1003. dbesc(datetime_convert('UTC','UTC','now + 6 days')),
  1004. dbesc(datetime_convert('UTC','UTC','now'))
  1005. );
  1006. if($r && count($r)) {
  1007. $total = 0;
  1008. $now = strtotime('now');
  1009. $cids = array();
  1010. $istoday = false;
  1011. foreach($r as $rr) {
  1012. if(strlen($rr['name']))
  1013. $total ++;
  1014. if((strtotime($rr['start'] . ' +00:00') < $now) && (strtotime($rr['finish'] . ' +00:00') > $now))
  1015. $istoday = true;
  1016. }
  1017. $classtoday = $istoday ? ' birthday-today ' : '';
  1018. if($total) {
  1019. foreach($r as &$rr) {
  1020. if(! strlen($rr['name']))
  1021. continue;
  1022. // avoid duplicates
  1023. if(in_array($rr['cid'],$cids))
  1024. continue;
  1025. $cids[] = $rr['cid'];
  1026. $today = (((strtotime($rr['start'] . ' +00:00') < $now) && (strtotime($rr['finish'] . ' +00:00') > $now)) ? true : false);
  1027. $sparkle = '';
  1028. $url = $rr['url'];
  1029. if($rr['network'] === NETWORK_DFRN) {
  1030. $sparkle = " sparkle";
  1031. $url = $a->get_baseurl() . '/redir/' . $rr['cid'];
  1032. }
  1033. $rr['link'] = $url;
  1034. $rr['title'] = $rr['name'];
  1035. $rr['date'] = day_translate(datetime_convert('UTC', $a->timezone, $rr['start'], $rr['adjust'] ? $bd_format : $bd_short)) . (($today) ? ' ' . t('[today]') : '');
  1036. $rr['startime'] = Null;
  1037. $rr['today'] = $today;
  1038. }
  1039. }
  1040. }
  1041. $tpl = get_markup_template("birthdays_reminder.tpl");
  1042. return replace_macros($tpl, array(
  1043. '$baseurl' => $a->get_baseurl(),
  1044. '$classtoday' => $classtoday,
  1045. '$count' => $total,
  1046. '$event_reminders' => t('Birthday Reminders'),
  1047. '$event_title' => t('Birthdays this week:'),
  1048. '$events' => $r,
  1049. '$lbr' => '{', // raw brackets mess up if/endif macro processing
  1050. '$rbr' => '}'
  1051. ));
  1052. }
  1053. }
  1054. if(! function_exists('get_events')) {
  1055. function get_events() {
  1056. require_once('include/bbcode.php');
  1057. $a = get_app();
  1058. if(! local_user())
  1059. return $o;
  1060. $mobile_detect = new Mobile_Detect();
  1061. $is_mobile = $mobile_detect->isMobile() || $mobile_detect->isTablet();
  1062. if($is_mobile)
  1063. return $o;
  1064. $bd_format = t('g A l F d') ; // 8 AM Friday January 18
  1065. $bd_short = t('F d');
  1066. $r = q("SELECT `event`.* FROM `event`
  1067. WHERE `event`.`uid` = %d AND `type` != 'birthday' AND `start` < '%s' AND `start` > '%s'
  1068. ORDER BY `start` ASC ",
  1069. intval(local_user()),
  1070. dbesc(datetime_convert('UTC','UTC','now + 6 days')),
  1071. dbesc(datetime_convert('UTC','UTC','now - 1 days'))
  1072. );
  1073. if($r && count($r)) {
  1074. $now = strtotime('now');
  1075. $istoday = false;
  1076. foreach($r as $rr) {
  1077. if(strlen($rr['name']))
  1078. $total ++;
  1079. $strt = datetime_convert('UTC',$rr['convert'] ? $a->timezone : 'UTC',$rr['start'],'Y-m-d');
  1080. if($strt === datetime_convert('UTC',$a->timezone,'now','Y-m-d'))
  1081. $istoday = true;
  1082. }
  1083. $classtoday = (($istoday) ? 'event-today' : '');
  1084. foreach($r as &$rr) {
  1085. if($rr['adjust'])
  1086. $md = datetime_convert('UTC',$a->timezone,$rr['start'],'Y/m');
  1087. else
  1088. $md = datetime_convert('UTC','UTC',$rr['start'],'Y/m');
  1089. $md .= "/#link-".$rr['id'];
  1090. $title = substr(strip_tags(bbcode($rr['desc'])),0,32) . '... ';
  1091. if(! $title)
  1092. $title = t('[No description]');
  1093. $strt = datetime_convert('UTC',$rr['convert'] ? $a->timezone : 'UTC',$rr['start']);
  1094. $today = ((substr($strt,0,10) === datetime_convert('UTC',$a->timezone,'now','Y-m-d')) ? true : false);
  1095. $rr['link'] = $md;
  1096. $rr['title'] = $title;
  1097. $rr['date'] = day_translate(datetime_convert('UTC', $rr['adjust'] ? $a->timezone : 'UTC', $rr['start'], $bd_format)) . (($today) ? ' ' . t('[today]') : '');
  1098. $rr['startime'] = $strt;
  1099. $rr['today'] = $today;
  1100. }
  1101. }
  1102. $tpl = get_markup_template("events_reminder.tpl");
  1103. return replace_macros($tpl, array(
  1104. '$baseurl' => $a->get_baseurl(),
  1105. '$classtoday' => $classtoday,
  1106. '$count' => count($r),
  1107. '$event_reminders' => t('Event Reminders'),
  1108. '$event_title' => t('Events this week:'),
  1109. '$events' => $r,
  1110. ));
  1111. }
  1112. }
  1113. /**
  1114. *
  1115. * Wrap calls to proc_close(proc_open()) and call hook
  1116. * so plugins can take part in process :)
  1117. *
  1118. * args:
  1119. * $cmd program to run
  1120. * next args are passed as $cmd command line
  1121. *
  1122. * e.g.: proc_run("ls","-la","/tmp");
  1123. *
  1124. * $cmd and string args are surrounded with ""
  1125. */
  1126. if(! function_exists('proc_run')) {
  1127. function proc_run($cmd){
  1128. $a = get_app();
  1129. $args = func_get_args();
  1130. $newargs = array();
  1131. if(! count($args))
  1132. return;
  1133. // expand any arrays
  1134. foreach($args as $arg) {
  1135. if(is_array($arg)) {
  1136. foreach($arg as $n) {
  1137. $newargs[] = $n;
  1138. }
  1139. }
  1140. else
  1141. $newargs[] = $arg;
  1142. }
  1143. $args = $newargs;
  1144. $arr = array('args' => $args, 'run_cmd' => true);
  1145. call_hooks("proc_run", $arr);
  1146. if(! $arr['run_cmd'])
  1147. return;
  1148. if(count($args) && $args[0] === 'php')
  1149. $args[0] = ((x($a->config,'php_path')) && (strlen($a->config['php_path'])) ? $a->config['php_path'] : 'php');
  1150. for($x = 0; $x < count($args); $x ++)
  1151. $args[$x] = escapeshellarg($args[$x]);
  1152. $cmdline = implode($args," ");
  1153. proc_close(proc_open($cmdline." &",array(),$foo));
  1154. }
  1155. }
  1156. if(! function_exists('current_theme')) {
  1157. function current_theme(){
  1158. $app_base_themes = array('duepuntozero', 'dispy', 'quattro');
  1159. $a = get_app();
  1160. $mobile_detect = new Mobile_Detect();
  1161. $is_mobile = $mobile_detect->isMobile() || $mobile_detect->isTablet();
  1162. if($is_mobile) {
  1163. $system_theme = ((isset($a->config['system']['mobile-theme'])) ? $a->config['system']['mobile-theme'] : '');
  1164. $theme_name = ((isset($_SESSION) && x($_SESSION,'mobile-theme')) ? $_SESSION['mobile-theme'] : $system_theme);
  1165. }
  1166. if(!$is_mobile || ($system_theme === '' && $theme_name === '')) {
  1167. $system_theme = ((isset($a->config['system']['theme'])) ? $a->config['system']['theme'] : '');
  1168. $theme_name = ((isset($_SESSION) && x($_SESSION,'theme')) ? $_SESSION['theme'] : $system_theme);
  1169. }
  1170. if($theme_name &&
  1171. (file_exists('view/theme/' . $theme_name . '/style.css') ||
  1172. file_exists('view/theme/' . $theme_name . '/style.php')))
  1173. return($theme_name);
  1174. foreach($app_base_themes as $t) {
  1175. if(file_exists('view/theme/' . $t . '/style.css')||
  1176. file_exists('view/theme/' . $t . '/style.php'))
  1177. return($t);
  1178. }
  1179. $fallback = array_merge(glob('view/theme/*/style.css'),glob('view/theme/*/style.php'));
  1180. if(count($fallback))
  1181. return (str_replace('view/theme/','', substr($fallback[0],0,-10)));
  1182. }
  1183. }
  1184. /*
  1185. * Return full URL to theme which is currently in effect.
  1186. * Provide a sane default if nothing is chosen or the specified theme does not exist.
  1187. */
  1188. if(! function_exists('current_theme_url')) {
  1189. function current_theme_url() {
  1190. global $a;
  1191. $t = current_theme();
  1192. if (file_exists('view/theme/' . $t . '/style.php'))
  1193. return($a->get_baseurl() . '/view/theme/' . $t . '/style.pcss');
  1194. return($a->get_baseurl() . '/view/theme/' . $t . '/style.css');
  1195. }
  1196. }
  1197. if(! function_exists('feed_birthday')) {
  1198. function feed_birthday($uid,$tz) {
  1199. /**
  1200. *
  1201. * Determine the next birthday, but only if the birthday is published
  1202. * in the default profile. We _could_ also look for a private profile that the
  1203. * recipient can see, but somebody could get mad at us if they start getting
  1204. * public birthday greetings when they haven't made this info public.
  1205. *
  1206. * Assuming we are able to publish this info, we are then going to convert
  1207. * the start time from the owner's timezone to UTC.
  1208. *
  1209. * This will potentially solve the problem found with some social networks
  1210. * where birthdays are converted to the viewer's timezone and salutations from
  1211. * elsewhere in the world show up on the wrong day. We will convert it to the
  1212. * viewer's timezone also, but first we are going to convert it from the birthday
  1213. * person's timezone to GMT - so the viewer may find the birthday starting at
  1214. * 6:00PM the day before, but that will correspond to midnight to the birthday person.
  1215. *
  1216. */
  1217. $birthday = '';
  1218. if(! strlen($tz))
  1219. $tz = 'UTC';
  1220. $p = q("SELECT `dob` FROM `profile` WHERE `is-default` = 1 AND `uid` = %d LIMIT 1",
  1221. intval($uid)
  1222. );
  1223. if($p && count($p)) {
  1224. $tmp_dob = substr($p[0]['dob'],5);
  1225. if(intval($tmp_dob)) {
  1226. $y = datetime_convert($tz,$tz,'now','Y');
  1227. $bd = $y . '-' . $tmp_dob . ' 00:00';
  1228. $t_dob = strtotime($bd);
  1229. $now = strtotime(datetime_convert($tz,$tz,'now'));
  1230. if($t_dob < $now)
  1231. $bd = $y + 1 . '-' . $tmp_dob . ' 00:00';
  1232. $birthday = datetime_convert($tz,'UTC',$bd,ATOM_TIME);
  1233. }
  1234. }
  1235. return $birthday;
  1236. }
  1237. }
  1238. if(! function_exists('is_site_admin')) {
  1239. function is_site_admin() {
  1240. $a = get_app();
  1241. if(local_user() && x($a->user,'email') && x($a->config,'admin_email') && ($a->user['email'] === $a->config['admin_email']))
  1242. return true;
  1243. return false;
  1244. }
  1245. }
  1246. if(! function_exists('load_contact_links')) {
  1247. function load_contact_links($uid) {
  1248. $a = get_app();
  1249. $ret = array();
  1250. if(! $uid || x($a->contacts,'empty'))
  1251. return;
  1252. $r = q("SELECT `id`,`network`,`url`,`thumb` FROM `contact` WHERE `uid` = %d AND `self` = 0 AND `blocked` = 0 ",
  1253. intval($uid)
  1254. );
  1255. if(count($r)) {
  1256. foreach($r as $rr){
  1257. $url = normalise_link($rr['url']);
  1258. $ret[$url] = $rr;
  1259. }
  1260. }
  1261. else
  1262. $ret['empty'] = true;
  1263. $a->contacts = $ret;
  1264. return;
  1265. }
  1266. }
  1267. if(! function_exists('profile_tabs')){
  1268. function profile_tabs($a, $is_owner=False, $nickname=Null){
  1269. //echo "<pre>"; var_dump($a->user); killme();
  1270. if (is_null($nickname))
  1271. $nickname = $a->user['nickname'];
  1272. if(x($_GET,'tab'))
  1273. $tab = notags(trim($_GET['tab']));
  1274. $url = $a->get_baseurl() . '/profile/' . $nickname;
  1275. $tabs = array(
  1276. array(
  1277. 'label'=>t('Status'),
  1278. 'url' => $url,
  1279. 'sel' => ((!isset($tab)&&$a->argv[0]=='profile')?'active':''),
  1280. 'title' => t('Status Messages and Posts'),
  1281. 'id' => 'status-tab',
  1282. ),
  1283. array(
  1284. 'label' => t('Profile'),
  1285. 'url' => $url.'/?tab=profile',
  1286. 'sel' => ((isset($tab) && $tab=='profile')?'active':''),
  1287. 'title' => t('Profile Details'),
  1288. 'id' => 'profile-tab',
  1289. ),
  1290. array(
  1291. 'label' => t('Photos'),
  1292. 'url' => $a->get_baseurl() . '/photos/' . $nickname,
  1293. 'sel' => ((!isset($tab)&&$a->argv[0]=='photos')?'active':''),
  1294. 'title' => t('Photo Albums'),
  1295. 'id' => 'photo-tab',
  1296. ),
  1297. );
  1298. if ($is_owner){
  1299. $tabs[] = array(
  1300. 'label' => t('Events'),
  1301. 'url' => $a->get_baseurl() . '/events',
  1302. 'sel' =>((!isset($tab)&&$a->argv[0]=='events')?'active':''),
  1303. 'title' => t('Events and Calendar'),
  1304. 'id' => 'events-tab',
  1305. );
  1306. $tabs[] = array(
  1307. 'label' => t('Personal Notes'),
  1308. 'url' => $a->get_baseurl() . '/notes',
  1309. 'sel' =>((!isset($tab)&&$a->argv[0]=='notes')?'active':''),
  1310. 'title' => t('Only You Can See This'),
  1311. 'id' => 'notes-tab',
  1312. );
  1313. }
  1314. $arr = array('is_owner' => $is_owner, 'nickname' => $nickname, 'tab' => (($tab) ? $tab : false), 'tabs' => $tabs);
  1315. call_hooks('profile_tabs', $arr);
  1316. $tpl = get_markup_template('common_tabs.tpl');
  1317. return replace_macros($tpl,array('$tabs' => $arr['tabs']));
  1318. }
  1319. }
  1320. function get_my_url() {
  1321. if(x($_SESSION,'my_url'))
  1322. return $_SESSION['my_url'];
  1323. return false;
  1324. }
  1325. function zrl_init(&$a) {
  1326. $tmp_str = get_my_url();
  1327. if(validate_url($tmp_str)) {
  1328. proc_run('php','include/gprobe.php',bin2hex($tmp_str));
  1329. $arr = array('zrl' => $tmp_str, 'url' => $a->cmd);
  1330. call_hooks('zrl_init',$arr);
  1331. }
  1332. }
  1333. function zrl($s,$force = false) {
  1334. if(! strlen($s))
  1335. return $s;
  1336. if((! strpos($s,'/profile/')) && (! $force))
  1337. return $s;
  1338. if($force && substr($s,-1,1) !== '/')
  1339. $s = $s . '/';
  1340. $achar = strpos($s,'?') ? '&' : '?';
  1341. $mine = get_my_url();
  1342. if($mine and ! link_compare($mine,$s))
  1343. return $s . $achar . 'zrl=' . urlencode($mine);
  1344. return $s;
  1345. }
  1346. /**
  1347. * returns querystring as string from a mapped array
  1348. *
  1349. * @param params Array
  1350. * @return string
  1351. */
  1352. function build_querystring($params, $name=null) {
  1353. $ret = "";
  1354. foreach($params as $key=>$val) {
  1355. if(is_array($val)) {
  1356. if($name==null) {
  1357. $ret .= build_querystring($val, $key);
  1358. } else {
  1359. $ret .= build_querystring($val, $name."[$key]");
  1360. }
  1361. } else {
  1362. $val = urlencode($val);
  1363. if($name!=null) {
  1364. $ret.=$name."[$key]"."=$val&";
  1365. } else {
  1366. $ret.= "$key=$val&";
  1367. }
  1368. }
  1369. }
  1370. return $ret;
  1371. }