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.

1678 lines
46 KiB

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