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.
 
 
 
 
 
 

1111 lines
29 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. define ( 'FRIENDIKA_VERSION', '2.2.1087' );
  9. define ( 'DFRN_PROTOCOL_VERSION', '2.21' );
  10. define ( 'DB_UPDATE_VERSION', 1084 );
  11. define ( 'EOL', "<br />\r\n" );
  12. define ( 'ATOM_TIME', 'Y-m-d\TH:i:s\Z' );
  13. /**
  14. *
  15. * Image storage quality. Lower numbers save space at cost of image detail.
  16. * For ease of upgrade, please do not change here. Change jpeg quality with
  17. * set_config('system','jpeg_quality',n) in .htconfig.php
  18. * where n is netween 1 and 100, and with very poor results below about 50
  19. *
  20. */
  21. define ( 'JPEG_QUALITY', 100 );
  22. /**
  23. * SSL redirection policies
  24. */
  25. define ( 'SSL_POLICY_NONE', 0 );
  26. define ( 'SSL_POLICY_FULL', 1 );
  27. define ( 'SSL_POLICY_SELFSIGN', 2 );
  28. /**
  29. * log levels
  30. */
  31. define ( 'LOGGER_NORMAL', 0 );
  32. define ( 'LOGGER_TRACE', 1 );
  33. define ( 'LOGGER_DEBUG', 2 );
  34. define ( 'LOGGER_DATA', 3 );
  35. define ( 'LOGGER_ALL', 4 );
  36. /**
  37. * registration policies
  38. */
  39. define ( 'REGISTER_CLOSED', 0 );
  40. define ( 'REGISTER_APPROVE', 1 );
  41. define ( 'REGISTER_OPEN', 2 );
  42. /**
  43. * relationship types
  44. */
  45. define ( 'CONTACT_IS_FOLLOWER', 1);
  46. define ( 'CONTACT_IS_SHARING', 2);
  47. define ( 'CONTACT_IS_FRIEND', 3);
  48. /**
  49. * Hook array order
  50. */
  51. define ( 'HOOK_HOOK', 0);
  52. define ( 'HOOK_FILE', 1);
  53. define ( 'HOOK_FUNCTION', 2);
  54. /**
  55. *
  56. * page/profile types
  57. *
  58. * PAGE_NORMAL is a typical personal profile account
  59. * PAGE_SOAPBOX automatically approves all friend requests as CONTACT_IS_SHARING, (readonly)
  60. * PAGE_COMMUNITY automatically approves all friend requests as CONTACT_IS_SHARING, but with
  61. * write access to wall and comments (no email and not included in page owner's ACL lists)
  62. * PAGE_FREELOVE automatically approves all friend requests as full friends (CONTACT_IS_FRIEND).
  63. *
  64. */
  65. define ( 'PAGE_NORMAL', 0 );
  66. define ( 'PAGE_SOAPBOX', 1 );
  67. define ( 'PAGE_COMMUNITY', 2 );
  68. define ( 'PAGE_FREELOVE', 3 );
  69. /**
  70. * Network and protocol family types
  71. */
  72. define ( 'NETWORK_ZOT', 'zot!'); // Zot!
  73. define ( 'NETWORK_DFRN', 'dfrn'); // Friendika, Mistpark, other DFRN implementations
  74. define ( 'NETWORK_OSTATUS', 'stat'); // status.net, identi.ca, GNU-social, other OStatus implementations
  75. define ( 'NETWORK_FEED', 'feed'); // RSS/Atom feeds with no known "post/notify" protocol
  76. define ( 'NETWORK_DIASPORA', 'dspr'); // Diaspora
  77. define ( 'NETWORK_MAIL', 'mail'); // IMAP/POP
  78. define ( 'NETWORK_FACEBOOK', 'face'); // Facebook API
  79. /**
  80. * Maximum number of "people who like (or don't like) this" that we will list by name
  81. */
  82. define ( 'MAX_LIKERS', 75);
  83. /**
  84. * Communication timeout
  85. */
  86. define ( 'ZCURL_TIMEOUT' , (-1));
  87. /**
  88. * email notification options
  89. */
  90. define ( 'NOTIFY_INTRO', 0x0001 );
  91. define ( 'NOTIFY_CONFIRM', 0x0002 );
  92. define ( 'NOTIFY_WALL', 0x0004 );
  93. define ( 'NOTIFY_COMMENT', 0x0008 );
  94. define ( 'NOTIFY_MAIL', 0x0010 );
  95. /**
  96. * various namespaces we may need to parse
  97. */
  98. define ( 'NAMESPACE_ZOT', 'http://purl.org/macgirvin/zot' );
  99. define ( 'NAMESPACE_DFRN' , 'http://purl.org/macgirvin/dfrn/1.0' );
  100. define ( 'NAMESPACE_THREAD' , 'http://purl.org/syndication/thread/1.0' );
  101. define ( 'NAMESPACE_TOMB' , 'http://purl.org/atompub/tombstones/1.0' );
  102. define ( 'NAMESPACE_ACTIVITY', 'http://activitystrea.ms/spec/1.0/' );
  103. define ( 'NAMESPACE_ACTIVITY_SCHEMA', 'http://activitystrea.ms/schema/1.0/' );
  104. define ( 'NAMESPACE_MEDIA', 'http://purl.org/syndication/atommedia' );
  105. define ( 'NAMESPACE_SALMON_ME', 'http://salmon-protocol.org/ns/magic-env' );
  106. define ( 'NAMESPACE_OSTATUSSUB', 'http://ostatus.org/schema/1.0/subscribe' );
  107. define ( 'NAMESPACE_GEORSS', 'http://www.georss.org/georss' );
  108. define ( 'NAMESPACE_POCO', 'http://portablecontacts.net/spec/1.0' );
  109. define ( 'NAMESPACE_FEED', 'http://schemas.google.com/g/2010#updates-from' );
  110. define ( 'NAMESPACE_OSTATUS', 'http://ostatus.org/schema/1.0' );
  111. define ( 'NAMESPACE_STATUSNET', 'http://status.net/schema/api/1/' );
  112. define ( 'NAMESPACE_ATOM1', 'http://www.w3.org/2005/Atom' );
  113. /**
  114. * activity stream defines
  115. */
  116. define ( 'ACTIVITY_LIKE', NAMESPACE_ACTIVITY_SCHEMA . 'like' );
  117. define ( 'ACTIVITY_DISLIKE', NAMESPACE_DFRN . '/dislike' );
  118. define ( 'ACTIVITY_OBJ_HEART', NAMESPACE_DFRN . '/heart' );
  119. define ( 'ACTIVITY_FRIEND', NAMESPACE_ACTIVITY_SCHEMA . 'make-friend' );
  120. define ( 'ACTIVITY_FOLLOW', NAMESPACE_ACTIVITY_SCHEMA . 'follow' );
  121. define ( 'ACTIVITY_UNFOLLOW', NAMESPACE_ACTIVITY_SCHEMA . 'stop-following' );
  122. define ( 'ACTIVITY_POST', NAMESPACE_ACTIVITY_SCHEMA . 'post' );
  123. define ( 'ACTIVITY_UPDATE', NAMESPACE_ACTIVITY_SCHEMA . 'update' );
  124. define ( 'ACTIVITY_TAG', NAMESPACE_ACTIVITY_SCHEMA . 'tag' );
  125. define ( 'ACTIVITY_OBJ_COMMENT', NAMESPACE_ACTIVITY_SCHEMA . 'comment' );
  126. define ( 'ACTIVITY_OBJ_NOTE', NAMESPACE_ACTIVITY_SCHEMA . 'note' );
  127. define ( 'ACTIVITY_OBJ_PERSON', NAMESPACE_ACTIVITY_SCHEMA . 'person' );
  128. define ( 'ACTIVITY_OBJ_PHOTO', NAMESPACE_ACTIVITY_SCHEMA . 'photo' );
  129. define ( 'ACTIVITY_OBJ_P_PHOTO', NAMESPACE_ACTIVITY_SCHEMA . 'profile-photo' );
  130. define ( 'ACTIVITY_OBJ_ALBUM', NAMESPACE_ACTIVITY_SCHEMA . 'photo-album' );
  131. define ( 'ACTIVITY_OBJ_EVENT', NAMESPACE_ACTIVITY_SCHEMA . 'event' );
  132. /**
  133. * item weight for query ordering
  134. */
  135. define ( 'GRAVITY_PARENT', 0);
  136. define ( 'GRAVITY_LIKE', 3);
  137. define ( 'GRAVITY_COMMENT', 6);
  138. /**
  139. *
  140. * Reverse the effect of magic_quotes_gpc if it is enabled.
  141. * Please disable magic_quotes_gpc so we don't have to do this.
  142. * See http://php.net/manual/en/security.magicquotes.disabling.php
  143. *
  144. */
  145. function startup() {
  146. error_reporting(E_ERROR | E_WARNING | E_PARSE);
  147. set_time_limit(0);
  148. ini_set('pcre.backtrack_limit', 250000);
  149. if (get_magic_quotes_gpc()) {
  150. $process = array(&$_GET, &$_POST, &$_COOKIE, &$_REQUEST);
  151. while (list($key, $val) = each($process)) {
  152. foreach ($val as $k => $v) {
  153. unset($process[$key][$k]);
  154. if (is_array($v)) {
  155. $process[$key][stripslashes($k)] = $v;
  156. $process[] = &$process[$key][stripslashes($k)];
  157. } else {
  158. $process[$key][stripslashes($k)] = stripslashes($v);
  159. }
  160. }
  161. }
  162. unset($process);
  163. }
  164. }
  165. /**
  166. *
  167. * class: App
  168. *
  169. * Our main application structure for the life of this page
  170. * Primarily deals with the URL that got us here
  171. * and tries to make some sense of it, and
  172. * stores our page contents and config storage
  173. * and anything else that might need to be passed around
  174. * before we spit the page out.
  175. *
  176. */
  177. if(! class_exists('App')) {
  178. class App {
  179. public $module_loaded = false;
  180. public $query_string;
  181. public $config;
  182. public $page;
  183. public $profile;
  184. public $user;
  185. public $cid;
  186. public $contact;
  187. public $contacts;
  188. public $page_contact;
  189. public $content;
  190. public $data;
  191. public $error = false;
  192. public $cmd;
  193. public $argv;
  194. public $argc;
  195. public $module;
  196. public $pager;
  197. public $strings;
  198. public $path;
  199. public $hooks;
  200. public $timezone;
  201. public $interactive = true;
  202. public $plugins;
  203. public $apps = Array();
  204. public $identities;
  205. private $scheme;
  206. private $hostname;
  207. private $baseurl;
  208. private $db;
  209. private $curl_code;
  210. private $curl_headers;
  211. function __construct() {
  212. $this->config = array();
  213. $this->page = array();
  214. $this->pager= array();
  215. $this->query_string = '';
  216. startup();
  217. $this->scheme = ((isset($_SERVER['HTTPS']) && ($_SERVER['HTTPS'])) ? 'https' : 'http' );
  218. if(x($_SERVER,'SERVER_NAME')) {
  219. $this->hostname = $_SERVER['SERVER_NAME'];
  220. if(x($_SERVER,'SERVER_PORT') && $_SERVER['SERVER_PORT'] != 80 && $_SERVER['SERVER_PORT'] != 443)
  221. $this->hostname .= ':' . $_SERVER['SERVER_PORT'];
  222. /**
  223. * Figure out if we are running at the top of a domain
  224. * or in a sub-directory and adjust accordingly
  225. */
  226. $path = trim(dirname($_SERVER['SCRIPT_NAME']),'/\\');
  227. if(isset($path) && strlen($path) && ($path != $this->path))
  228. $this->path = $path;
  229. }
  230. set_include_path(
  231. "include/$this->hostname" . PATH_SEPARATOR
  232. . 'include' . PATH_SEPARATOR
  233. . 'library' . PATH_SEPARATOR
  234. . 'library/phpsec' . PATH_SEPARATOR
  235. . '.' );
  236. if((x($_SERVER,'QUERY_STRING')) && substr($_SERVER['QUERY_STRING'],0,2) === "q=")
  237. $this->query_string = substr($_SERVER['QUERY_STRING'],2);
  238. if(x($_GET,'q'))
  239. $this->cmd = trim($_GET['q'],'/\\');
  240. /**
  241. *
  242. * Break the URL path into C style argc/argv style arguments for our
  243. * modules. Given "http://example.com/module/arg1/arg2", $this->argc
  244. * will be 3 (integer) and $this->argv will contain:
  245. * [0] => 'module'
  246. * [1] => 'arg1'
  247. * [2] => 'arg2'
  248. *
  249. *
  250. * There will always be one argument. If provided a naked domain
  251. * URL, $this->argv[0] is set to "home".
  252. *
  253. */
  254. $this->argv = explode('/',$this->cmd);
  255. $this->argc = count($this->argv);
  256. if((array_key_exists('0',$this->argv)) && strlen($this->argv[0])) {
  257. $this->module = str_replace(".", "_", $this->argv[0]);
  258. }
  259. else {
  260. $this->argc = 1;
  261. $this->argv = array('home');
  262. $this->module = 'home';
  263. }
  264. /**
  265. * Special handling for the webfinger/lrdd host XRD file
  266. */
  267. if($this->cmd === '.well-known/host-meta') {
  268. $this->argc = 1;
  269. $this->argv = array('hostxrd');
  270. $this->module = 'hostxrd';
  271. }
  272. /**
  273. * See if there is any page number information, and initialise
  274. * pagination
  275. */
  276. $this->pager['page'] = ((x($_GET,'page')) ? $_GET['page'] : 1);
  277. $this->pager['itemspage'] = 50;
  278. $this->pager['start'] = ($this->pager['page'] * $this->pager['itemspage']) - $this->pager['itemspage'];
  279. $this->pager['total'] = 0;
  280. }
  281. function get_baseurl($ssl = false) {
  282. $scheme = $this->scheme;
  283. if(x($this->config,'ssl_policy')) {
  284. if(($ssl) || ($this->config['ssl_policy'] == SSL_POLICY_FULL))
  285. $scheme = 'https';
  286. if(($this->config['ssl_policy'] == SSL_POLICY_SELFSIGN) && (local_user() || x($_POST,'auth-params')))
  287. $scheme = 'https';
  288. }
  289. $this->baseurl = $scheme . "://" . $this->hostname . ((isset($this->path) && strlen($this->path)) ? '/' . $this->path : '' );
  290. return $this->baseurl;
  291. }
  292. function set_baseurl($url) {
  293. $parsed = @parse_url($url);
  294. $this->baseurl = $url;
  295. if($parsed) {
  296. $this->scheme = $parsed['scheme'];
  297. $this->hostname = $parsed['host'];
  298. if(x($parsed,'port'))
  299. $this->hostname .= ':' . $parsed['port'];
  300. if(x($parsed,'path'))
  301. $this->path = trim($parsed['path'],'\\/');
  302. }
  303. }
  304. function get_hostname() {
  305. return $this->hostname;
  306. }
  307. function set_hostname($h) {
  308. $this->hostname = $h;
  309. }
  310. function set_path($p) {
  311. $this->path = trim(trim($p),'/');
  312. }
  313. function get_path() {
  314. return $this->path;
  315. }
  316. function set_pager_total($n) {
  317. $this->pager['total'] = intval($n);
  318. }
  319. function set_pager_itemspage($n) {
  320. $this->pager['itemspage'] = intval($n);
  321. $this->pager['start'] = ($this->pager['page'] * $this->pager['itemspage']) - $this->pager['itemspage'];
  322. }
  323. function init_pagehead() {
  324. $this->page['title'] = $this->config['sitename'];
  325. $tpl = file_get_contents('view/head.tpl');
  326. $this->page['htmlhead'] = replace_macros($tpl,array(
  327. '$baseurl' => $this->get_baseurl(), // FIXME for z_path!!!!
  328. '$generator' => 'Friendika' . ' ' . FRIENDIKA_VERSION,
  329. '$delitem' => t('Delete this item?'),
  330. '$comment' => t('Comment')
  331. ));
  332. }
  333. function set_curl_code($code) {
  334. $this->curl_code = $code;
  335. }
  336. function get_curl_code() {
  337. return $this->curl_code;
  338. }
  339. function set_curl_headers($headers) {
  340. $this->curl_headers = $headers;
  341. }
  342. function get_curl_headers() {
  343. return $this->curl_headers;
  344. }
  345. }}
  346. // retrieve the App structure
  347. // useful in functions which require it but don't get it passed to them
  348. if(! function_exists('get_app')) {
  349. function get_app() {
  350. global $a;
  351. return $a;
  352. }};
  353. // Multi-purpose function to check variable state.
  354. // Usage: x($var) or $x($array,'key')
  355. // returns false if variable/key is not set
  356. // if variable is set, returns 1 if has 'non-zero' value, otherwise returns 0.
  357. // e.g. x('') or x(0) returns 0;
  358. if(! function_exists('x')) {
  359. function x($s,$k = NULL) {
  360. if($k != NULL) {
  361. if((is_array($s)) && (array_key_exists($k,$s))) {
  362. if($s[$k])
  363. return (int) 1;
  364. return (int) 0;
  365. }
  366. return false;
  367. }
  368. else {
  369. if(isset($s)) {
  370. if($s) {
  371. return (int) 1;
  372. }
  373. return (int) 0;
  374. }
  375. return false;
  376. }
  377. }}
  378. // called from db initialisation if db is dead.
  379. if(! function_exists('system_unavailable')) {
  380. function system_unavailable() {
  381. include('system_unavailable.php');
  382. system_down();
  383. killme();
  384. }}
  385. function clean_urls() {
  386. global $a;
  387. // if($a->config['system']['clean_urls'])
  388. return true;
  389. // return false;
  390. }
  391. function z_path() {
  392. global $a;
  393. $base = $a->get_baseurl();
  394. if(! clean_urls())
  395. $base .= '/?q=';
  396. return $base;
  397. }
  398. function z_root() {
  399. global $a;
  400. return $a->get_baseurl();
  401. }
  402. function absurl($path) {
  403. if(strpos($path,'/') === 0)
  404. return z_path() . $path;
  405. return $path;
  406. }
  407. // Primarily involved with database upgrade, but also sets the
  408. // base url for use in cmdline programs which don't have
  409. // $_SERVER variables, and synchronising the state of installed plugins.
  410. if(! function_exists('check_config')) {
  411. function check_config(&$a) {
  412. $build = get_config('system','build');
  413. if(! x($build))
  414. $build = set_config('system','build',DB_UPDATE_VERSION);
  415. $url = get_config('system','url');
  416. // if the url isn't set or the stored url is radically different
  417. // than the currently visited url, store the current value accordingly.
  418. // "Radically different" ignores common variations such as http vs https
  419. // and www.example.com vs example.com.
  420. if((! x($url)) || (! link_compare($url,$a->get_baseurl())))
  421. $url = set_config('system','url',$a->get_baseurl());
  422. if($build != DB_UPDATE_VERSION) {
  423. $stored = intval($build);
  424. $current = intval(DB_UPDATE_VERSION);
  425. if(($stored < $current) && file_exists('update.php')) {
  426. // We're reporting a different version than what is currently installed.
  427. // Run any existing update scripts to bring the database up to current.
  428. require_once('update.php');
  429. // make sure that boot.php and update.php are the same release, we might be
  430. // updating right this very second and the correct version of the update.php
  431. // file may not be here yet. This can happen on a very busy site.
  432. if(DB_UPDATE_VERSION == UPDATE_VERSION) {
  433. for($x = $stored; $x < $current; $x ++) {
  434. if(function_exists('update_' . $x)) {
  435. $func = 'update_' . $x;
  436. $func($a);
  437. }
  438. }
  439. set_config('system','build', DB_UPDATE_VERSION);
  440. }
  441. }
  442. }
  443. /**
  444. *
  445. * Synchronise plugins:
  446. *
  447. * $a->config['system']['addon'] contains a comma-separated list of names
  448. * of plugins/addons which are used on this system.
  449. * Go through the database list of already installed addons, and if we have
  450. * an entry, but it isn't in the config list, call the uninstall procedure
  451. * and mark it uninstalled in the database (for now we'll remove it).
  452. * Then go through the config list and if we have a plugin that isn't installed,
  453. * call the install procedure and add it to the database.
  454. *
  455. */
  456. $r = q("SELECT * FROM `addon` WHERE `installed` = 1");
  457. if(count($r))
  458. $installed = $r;
  459. else
  460. $installed = array();
  461. $plugins = get_config('system','addon');
  462. $plugins_arr = array();
  463. if($plugins)
  464. $plugins_arr = explode(',',str_replace(' ', '',$plugins));
  465. $a->plugins = $plugins_arr;
  466. $installed_arr = array();
  467. if(count($installed)) {
  468. foreach($installed as $i) {
  469. if(! in_array($i['name'],$plugins_arr)) {
  470. uninstall_plugin($i['name']);
  471. }
  472. else
  473. $installed_arr[] = $i['name'];
  474. }
  475. }
  476. if(count($plugins_arr)) {
  477. foreach($plugins_arr as $p) {
  478. if(! in_array($p,$installed_arr)) {
  479. install_plugin($p);
  480. }
  481. }
  482. }
  483. load_hooks();
  484. return;
  485. }}
  486. function get_guid($size=16) {
  487. $exists = true; // assume by default that we don't have a unique guid
  488. do {
  489. $s = random_string($size);
  490. $r = q("select id from guid where guid = '%s' limit 1", dbesc($s));
  491. if(! count($r))
  492. $exists = false;
  493. } while($exists);
  494. q("insert into guid ( guid ) values ( '%s' ) ", dbesc($s));
  495. return $s;
  496. }
  497. // wrapper for adding a login box. If $register == true provide a registration
  498. // link. This will most always depend on the value of $a->config['register_policy'].
  499. // returns the complete html for inserting into the page
  500. if(! function_exists('login')) {
  501. function login($register = false) {
  502. $o = "";
  503. $register_tpl = (($register) ? get_markup_template("register-link.tpl") : "");
  504. $register_html = replace_macros($register_tpl,array(
  505. '$title' => t('Create a New Account'),
  506. '$desc' => t('Register')
  507. ));
  508. $noid = get_config('system','no_openid');
  509. if($noid) {
  510. $classname = 'no-openid';
  511. $namelabel = t('Nickname or Email address: ');
  512. $passlabel = t('Password: ');
  513. $login = t('Login');
  514. }
  515. else {
  516. $classname = 'openid';
  517. $namelabel = t('Nickname/Email/OpenID: ');
  518. $passlabel = t("Password \x28if not OpenID\x29: ");
  519. $login = t('Login');
  520. }
  521. $lostpass = t('Forgot your password?');
  522. $lostlink = t('Password Reset');
  523. if(local_user()) {
  524. $tpl = get_markup_template("logout.tpl");
  525. }
  526. else {
  527. $tpl = get_markup_template("login.tpl");
  528. }
  529. $o = '<script type="text/javascript"> $(document).ready(function() { $("#login-name").focus();} );</script>';
  530. $o .= replace_macros($tpl,array(
  531. '$logout' => t('Logout'),
  532. '$register_html' => $register_html,
  533. '$classname' => $classname,
  534. '$namelabel' => $namelabel,
  535. '$passlabel' => $passlabel,
  536. '$login' => $login,
  537. '$lostpass' => $lostpass,
  538. '$lostlink' => $lostlink
  539. ));
  540. return $o;
  541. }}
  542. // Used to end the current process, after saving session state.
  543. if(! function_exists('killme')) {
  544. function killme() {
  545. session_write_close();
  546. exit;
  547. }}
  548. // redirect to another URL and terminate this process.
  549. if(! function_exists('goaway')) {
  550. function goaway($s) {
  551. header("Location: $s");
  552. killme();
  553. }}
  554. // Returns the uid of locally logged in user or false.
  555. if(! function_exists('local_user')) {
  556. function local_user() {
  557. if((x($_SESSION,'authenticated')) && (x($_SESSION,'uid')))
  558. return intval($_SESSION['uid']);
  559. return false;
  560. }}
  561. // Returns contact id of authenticated site visitor or false
  562. if(! function_exists('remote_user')) {
  563. function remote_user() {
  564. if((x($_SESSION,'authenticated')) && (x($_SESSION,'visitor_id')))
  565. return intval($_SESSION['visitor_id']);
  566. return false;
  567. }}
  568. // contents of $s are displayed prominently on the page the next time
  569. // a page is loaded. Usually used for errors or alerts.
  570. if(! function_exists('notice')) {
  571. function notice($s) {
  572. $a = get_app();
  573. if($a->interactive)
  574. $_SESSION['sysmsg'] .= $s;
  575. }}
  576. if(! function_exists('info')) {
  577. function info($s) {
  578. $a = get_app();
  579. if($a->interactive)
  580. $_SESSION['sysmsg_info'] .= $s;
  581. }}
  582. // wrapper around config to limit the text length of an incoming message
  583. if(! function_exists('get_max_import_size')) {
  584. function get_max_import_size() {
  585. global $a;
  586. return ((x($a->config,'max_import_size')) ? $a->config['max_import_size'] : 0 );
  587. }}
  588. /**
  589. *
  590. * Function : profile_load
  591. * @parameter App $a
  592. * @parameter string $nickname
  593. * @parameter int $profile
  594. *
  595. * Summary: Loads a profile into the page sidebar.
  596. * The function requires a writeable copy of the main App structure, and the nickname
  597. * of a registered local account.
  598. *
  599. * If the viewer is an authenticated remote viewer, the profile displayed is the
  600. * one that has been configured for his/her viewing in the Contact manager.
  601. * Passing a non-zero profile ID can also allow a preview of a selected profile
  602. * by the owner.
  603. *
  604. * Profile information is placed in the App structure for later retrieval.
  605. * Honours the owner's chosen theme for display.
  606. *
  607. */
  608. if(! function_exists('profile_load')) {
  609. function profile_load(&$a, $nickname, $profile = 0) {
  610. if(remote_user()) {
  611. $r = q("SELECT `profile-id` FROM `contact` WHERE `id` = %d LIMIT 1",
  612. intval($_SESSION['visitor_id']));
  613. if(count($r))
  614. $profile = $r[0]['profile-id'];
  615. }
  616. $r = null;
  617. if($profile) {
  618. $profile_int = intval($profile);
  619. $r = q("SELECT `profile`.`uid` AS `profile_uid`, `profile`.* , `user`.* FROM `profile`
  620. LEFT JOIN `user` ON `profile`.`uid` = `user`.`uid`
  621. WHERE `user`.`nickname` = '%s' AND `profile`.`id` = %d LIMIT 1",
  622. dbesc($nickname),
  623. intval($profile_int)
  624. );
  625. }
  626. if(! count($r)) {
  627. $r = q("SELECT `profile`.`uid` AS `profile_uid`, `profile`.* , `user`.* FROM `profile`
  628. LEFT JOIN `user` ON `profile`.`uid` = `user`.`uid`
  629. WHERE `user`.`nickname` = '%s' AND `profile`.`is-default` = 1 LIMIT 1",
  630. dbesc($nickname)
  631. );
  632. }
  633. if(($r === false) || (! count($r))) {
  634. notice( t('No profile') . EOL );
  635. $a->error = 404;
  636. return;
  637. }
  638. $a->profile = $r[0];
  639. $a->page['title'] = $a->profile['name'] . " @ " . $a->config['sitename'];
  640. $_SESSION['theme'] = $a->profile['theme'];
  641. if(! (x($a->page,'aside')))
  642. $a->page['aside'] = '';
  643. $block = (((get_config('system','block_public')) && (! local_user()) && (! remote_user())) ? true : false);
  644. $a->page['aside'] .= profile_sidebar($a->profile, $block);
  645. /*if(! $block)
  646. $a->page['aside'] .= contact_block();*/
  647. return;
  648. }}
  649. /**
  650. *
  651. * Function: profile_sidebar
  652. *
  653. * Formats a profile for display in the sidebar.
  654. * It is very difficult to templatise the HTML completely
  655. * because of all the conditional logic.
  656. *
  657. * @parameter: array $profile
  658. *
  659. * Returns HTML string stuitable for sidebar inclusion
  660. * Exceptions: Returns empty string if passed $profile is wrong type or not populated
  661. *
  662. */
  663. if(! function_exists('profile_sidebar')) {
  664. function profile_sidebar($profile, $block = 0) {
  665. $a = get_app();
  666. $o = '';
  667. $location = false;
  668. $address = false;
  669. $pdesc = true;
  670. if((! is_array($profile)) && (! count($profile)))
  671. return $o;
  672. call_hooks('profile_sidebar_enter', $profile);
  673. // don't show connect link to yourself
  674. $connect = (($profile['uid'] != local_user()) ? t('Connect') : False);
  675. // don't show connect link to authenticated visitors either
  676. if((remote_user()) && ($_SESSION['visitor_visiting'] == $profile['uid']))
  677. $connect = False;
  678. if((x($profile,'address') == 1)
  679. || (x($profile,'locality') == 1)
  680. || (x($profile,'region') == 1)
  681. || (x($profile,'postal-code') == 1)
  682. || (x($profile,'country-name') == 1))
  683. $location = t('Location:');
  684. $gender = ((x($profile,'gender') == 1) ? t('Gender:') : False);
  685. $marital = ((x($profile,'marital') == 1) ? t('Status:') : False);
  686. $homepage = ((x($profile,'homepage') == 1) ? t('Homepage:') : False);
  687. if(($profile['hidewall'] || $block) && (! local_user()) && (! remote_user())) {
  688. $location = $pdesc = $connect = $gender = $marital = $homepage = False;
  689. }
  690. $diaspora = array(
  691. 'podloc' => $a->get_baseurl(),
  692. 'searchable' => (($profile['publish'] && $profile['net-publish']) ? 'true' : 'false' ),
  693. 'nickname ' => $profile['nickname'],
  694. 'fullname' => $profile['name'],
  695. 'photo300 ' => $a->get_baseurl() . '/photo/custom/300/' . $profile['uid'] . '.jpg',
  696. 'photo100 ' => $a->get_baseurl() . '/photo/custom/100/' . $profile['uid'] . '.jpg',
  697. 'photo50 ' => $a->get_baseurl() . '/photo/custom/50/' . $profile['uid'] . '.jpg',
  698. );
  699. if (!$block){
  700. $contact_block = contact_block();
  701. }
  702. $tpl = get_markup_template('profile_vcard.tpl');
  703. $o .= replace_macros($tpl, array(
  704. '$profile' => $profile,
  705. '$connect' => $connect,
  706. '$location' => $location,
  707. '$gender' => $gender,
  708. '$pdesc' => $pdesc,
  709. '$marital' => $marital,
  710. '$homepage' => $homepage,
  711. '$diaspora' => $diaspora,
  712. '$contact_block' => $contact_block,
  713. ));
  714. $arr = array('profile' => &$profile, 'entry' => &$o);
  715. call_hooks('profile_sidebar', $arr);
  716. return $o;
  717. }}
  718. if(! function_exists('get_birthdays')) {
  719. function get_birthdays() {
  720. $a = get_app();
  721. $o = '';
  722. if(! local_user())
  723. return $o;
  724. $bd_format = t('g A l F d') ; // 8 AM Friday January 18
  725. $r = q("SELECT `event`.*, `event`.`id` AS `eid`, `contact`.* FROM `event`
  726. LEFT JOIN `contact` ON `contact`.`id` = `event`.`cid`
  727. WHERE `event`.`uid` = %d AND `type` = 'birthday' AND `start` < '%s' AND `finish` > '%s'
  728. ORDER BY `start` DESC ",
  729. intval(local_user()),
  730. dbesc(datetime_convert('UTC','UTC','now + 6 days')),
  731. dbesc(datetime_convert('UTC','UTC','now'))
  732. );
  733. if($r && count($r)) {
  734. $total = 0;
  735. foreach($r as $rr)
  736. if(strlen($rr['name']))
  737. $total ++;
  738. if($total) {
  739. $o .= '<div id="birthday-notice" class="birthday-notice fakelink" onclick=openClose(\'birthday-wrapper\'); >' . t('Birthday Reminders') . ' ' . '(' . $total . ')' . '</div>';
  740. $o .= '<div id="birthday-wrapper" style="display: none;" ><div id="birthday-title">' . t('Birthdays this week:') . '</div>';
  741. $o .= '<div id="birthday-adjust">' . t("\x28Adjusted for local time\x29") . '</div>';
  742. $o .= '<div id="birthday-title-end"></div>';
  743. foreach($r as $rr) {
  744. if(! strlen($rr['name']))
  745. continue;
  746. $now = strtotime('now');
  747. $today = (((strtotime($rr['start'] . ' +00:00') < $now) && (strtotime($rr['finish'] . ' +00:00') > $now)) ? true : false);
  748. $o .= '<div class="birthday-list" id="birthday-' . $rr['eid'] . '"><a class="sparkle" href="'
  749. . $a->get_baseurl() . '/redir/' . $rr['cid'] . '">' . $rr['name'] . '</a> '
  750. . day_translate(datetime_convert('UTC', $a->timezone, $rr['start'], $bd_format)) . (($today) ? ' ' . t('[today]') : '')
  751. . '</div>' ;
  752. }
  753. $o .= '</div></div>';
  754. }
  755. }
  756. return $o;
  757. }}
  758. /**
  759. *
  760. * Wrap calls to proc_close(proc_open()) and call hook
  761. * so plugins can take part in process :)
  762. *
  763. * args:
  764. * $cmd program to run
  765. * next args are passed as $cmd command line
  766. *
  767. * e.g.: proc_run("ls","-la","/tmp");
  768. *
  769. * $cmd and string args are surrounded with ""
  770. */
  771. if(! function_exists('proc_run')) {
  772. function proc_run($cmd){
  773. $a = get_app();
  774. $args = func_get_args();
  775. $arr = array('args' => $args, 'run_cmd' => true);
  776. call_hooks("proc_run", $arr);
  777. if(! $arr['run_cmd'])
  778. return;
  779. if(count($args) && $args[0] === 'php')
  780. $args[0] = ((x($a->config,'php_path')) && (strlen($a->config['php_path'])) ? $a->config['php_path'] : 'php');
  781. foreach ($args as $arg){
  782. $arg = escapeshellarg($arg);
  783. }
  784. $cmdline = implode($args," ");
  785. proc_close(proc_open($cmdline." &",array(),$foo));
  786. }}
  787. if(! function_exists('current_theme')) {
  788. function current_theme(){
  789. $app_base_themes = array('duepuntozero', 'loozah');
  790. $a = get_app();
  791. $system_theme = ((isset($a->config['system']['theme'])) ? $a->config['system']['theme'] : '');
  792. $theme_name = ((is_array($_SESSION) && x($_SESSION,'theme')) ? $_SESSION['theme'] : $system_theme);
  793. if($theme_name && file_exists('view/theme/' . $theme_name . '/style.css'))
  794. return($theme_name);
  795. foreach($app_base_themes as $t) {
  796. if(file_exists('view/theme/' . $t . '/style.css'))
  797. return($t);
  798. }
  799. $fallback = glob('view/theme/*/style.css');
  800. if(count($fallback))
  801. return (str_replace('view/theme/','', str_replace("/style.css","",$fallback[0])));
  802. }}
  803. /*
  804. * Return full URL to theme which is currently in effect.
  805. * Provide a sane default if nothing is chosen or the specified theme does not exist.
  806. */
  807. if(! function_exists('current_theme_url')) {
  808. function current_theme_url() {
  809. global $a;
  810. $t = current_theme();
  811. return($a->get_baseurl() . '/view/theme/' . $t . '/style.css');
  812. }}
  813. if(! function_exists('feed_birthday')) {
  814. function feed_birthday($uid,$tz) {
  815. /**
  816. *
  817. * Determine the next birthday, but only if the birthday is published
  818. * in the default profile. We _could_ also look for a private profile that the
  819. * recipient can see, but somebody could get mad at us if they start getting
  820. * public birthday greetings when they haven't made this info public.
  821. *
  822. * Assuming we are able to publish this info, we are then going to convert
  823. * the start time from the owner's timezone to UTC.
  824. *
  825. * This will potentially solve the problem found with some social networks
  826. * where birthdays are converted to the viewer's timezone and salutations from
  827. * elsewhere in the world show up on the wrong day. We will convert it to the
  828. * viewer's timezone also, but first we are going to convert it from the birthday
  829. * person's timezone to GMT - so the viewer may find the birthday starting at
  830. * 6:00PM the day before, but that will correspond to midnight to the birthday person.
  831. *
  832. */
  833. $birthday = '';
  834. $p = q("SELECT `dob` FROM `profile` WHERE `is-default` = 1 AND `uid` = %d LIMIT 1",
  835. intval($uid)
  836. );
  837. if($p && count($p)) {
  838. $tmp_dob = substr($p[0]['dob'],5);
  839. if(intval($tmp_dob)) {
  840. $y = datetime_convert($tz,$tz,'now','Y');
  841. $bd = $y . '-' . $tmp_dob . ' 00:00';
  842. $t_dob = strtotime($bd);
  843. $now = strtotime(datetime_convert($tz,$tz,'now'));
  844. if($t_dob < $now)
  845. $bd = $y + 1 . '-' . $tmp_dob . ' 00:00';
  846. $birthday = datetime_convert($tz,'UTC',$bd,ATOM_TIME);
  847. }
  848. }
  849. return $birthday;
  850. }}
  851. if(! function_exists('is_site_admin')) {
  852. function is_site_admin() {
  853. $a = get_app();
  854. if(local_user() && x($a->user,'email') && x($a->config,'admin_email') && ($a->user['email'] === $a->config['admin_email']))
  855. return true;
  856. return false;
  857. }}
  858. if(! function_exists('load_contact_links')) {
  859. function load_contact_links($uid) {
  860. $a = get_app();
  861. $ret = array();
  862. if(! $uid || x($a->contacts,'empty'))
  863. return;
  864. $r = q("SELECT `id`,`network`,`url`,`thumb` FROM `contact` WHERE `uid` = %d AND `self` = 0 AND `blocked` = 0 ",
  865. intval($uid)
  866. );
  867. if(count($r)) {
  868. foreach($r as $rr){
  869. $url = normalise_link($rr['url']);
  870. $ret[$url] = $rr;
  871. }
  872. }
  873. else
  874. $ret['empty'] = true;
  875. $a->contacts = $ret;
  876. return;
  877. }}