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.
 
 
 
 
 
 

1527 lines
41 KiB

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