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.
 
 
 
 
 
 

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