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.
 
 
 
 
 
 

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