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.

686 lines
21 KiB

11 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
11 years ago
11 years ago
  1. <?php
  2. // Included here for completeness, but this is a very dangerous operation.
  3. // It is the caller's responsibility to confirm the requestor's intent and
  4. // authorisation to do this.
  5. function user_remove($uid) {
  6. if(! $uid)
  7. return;
  8. $a = get_app();
  9. logger('Removing user: ' . $uid);
  10. $r = q("select * from user where uid = %d limit 1", intval($uid));
  11. call_hooks('remove_user',$r[0]);
  12. // save username (actually the nickname as it is guaranteed
  13. // unique), so it cannot be re-registered in the future.
  14. q("insert into userd ( username ) values ( '%s' )",
  15. $r[0]['nickname']
  16. );
  17. // don't delete yet, will be done later when contacts have deleted my stuff
  18. // q("DELETE FROM `contact` WHERE `uid` = %d", intval($uid));
  19. q("DELETE FROM `gcign` WHERE `uid` = %d", intval($uid));
  20. q("DELETE FROM `group` WHERE `uid` = %d", intval($uid));
  21. q("DELETE FROM `group_member` WHERE `uid` = %d", intval($uid));
  22. q("DELETE FROM `intro` WHERE `uid` = %d", intval($uid));
  23. q("DELETE FROM `event` WHERE `uid` = %d", intval($uid));
  24. q("DELETE FROM `item` WHERE `uid` = %d", intval($uid));
  25. q("DELETE FROM `item_id` WHERE `uid` = %d", intval($uid));
  26. q("DELETE FROM `mail` WHERE `uid` = %d", intval($uid));
  27. q("DELETE FROM `mailacct` WHERE `uid` = %d", intval($uid));
  28. q("DELETE FROM `manage` WHERE `uid` = %d", intval($uid));
  29. q("DELETE FROM `notify` WHERE `uid` = %d", intval($uid));
  30. q("DELETE FROM `photo` WHERE `uid` = %d", intval($uid));
  31. q("DELETE FROM `attach` WHERE `uid` = %d", intval($uid));
  32. q("DELETE FROM `profile` WHERE `uid` = %d", intval($uid));
  33. q("DELETE FROM `profile_check` WHERE `uid` = %d", intval($uid));
  34. q("DELETE FROM `pconfig` WHERE `uid` = %d", intval($uid));
  35. q("DELETE FROM `search` WHERE `uid` = %d", intval($uid));
  36. q("DELETE FROM `spam` WHERE `uid` = %d", intval($uid));
  37. // don't delete yet, will be done later when contacts have deleted my stuff
  38. // q("DELETE FROM `user` WHERE `uid` = %d", intval($uid));
  39. q("UPDATE `user` SET `account_removed` = 1, `account_expires_on` = UTC_TIMESTAMP() WHERE `uid` = %d", intval($uid));
  40. proc_run('php', "include/notifier.php", "removeme", $uid);
  41. // Send an update to the directory
  42. proc_run('php', "include/directory.php", $r[0]['url']);
  43. if($uid == local_user()) {
  44. unset($_SESSION['authenticated']);
  45. unset($_SESSION['uid']);
  46. goaway($a->get_baseurl());
  47. }
  48. }
  49. function contact_remove($id) {
  50. $r = q("select uid from contact where id = %d limit 1",
  51. intval($id)
  52. );
  53. if((! count($r)) || (! intval($r[0]['uid'])))
  54. return;
  55. $archive = get_pconfig($r[0]['uid'], 'system','archive_removed_contacts');
  56. if($archive) {
  57. q("update contact set `archive` = 1, `network` = 'none', `writable` = 0 where id = %d",
  58. intval($id)
  59. );
  60. return;
  61. }
  62. q("DELETE FROM `contact` WHERE `id` = %d",
  63. intval($id)
  64. );
  65. q("DELETE FROM `item` WHERE `contact-id` = %d ",
  66. intval($id)
  67. );
  68. q("DELETE FROM `photo` WHERE `contact-id` = %d ",
  69. intval($id)
  70. );
  71. q("DELETE FROM `mail` WHERE `contact-id` = %d ",
  72. intval($id)
  73. );
  74. q("DELETE FROM `event` WHERE `cid` = %d ",
  75. intval($id)
  76. );
  77. q("DELETE FROM `queue` WHERE `cid` = %d ",
  78. intval($id)
  79. );
  80. }
  81. // sends an unfriend message. Does not remove the contact
  82. function terminate_friendship($user,$self,$contact) {
  83. $a = get_app();
  84. require_once('include/datetime.php');
  85. if($contact['network'] === NETWORK_OSTATUS) {
  86. $slap = replace_macros(get_markup_template('follow_slap.tpl'), array(
  87. '$name' => $user['username'],
  88. '$profile_page' => $a->get_baseurl() . '/profile/' . $user['nickname'],
  89. '$photo' => $self['photo'],
  90. '$thumb' => $self['thumb'],
  91. '$published' => datetime_convert('UTC','UTC', 'now', ATOM_TIME),
  92. '$item_id' => 'urn:X-dfrn:' . $a->get_hostname() . ':unfollow:' . get_guid(32),
  93. '$title' => '',
  94. '$type' => 'text',
  95. '$content' => t('stopped following'),
  96. '$nick' => $user['nickname'],
  97. '$verb' => 'http://ostatus.org/schema/1.0/unfollow', // ACTIVITY_UNFOLLOW,
  98. '$ostat_follow' => '' // '<as:verb>http://ostatus.org/schema/1.0/unfollow</as:verb>' . "\r\n"
  99. ));
  100. if((x($contact,'notify')) && (strlen($contact['notify']))) {
  101. require_once('include/salmon.php');
  102. slapper($user,$contact['notify'],$slap);
  103. }
  104. }
  105. elseif($contact['network'] === NETWORK_DIASPORA) {
  106. require_once('include/diaspora.php');
  107. diaspora::send_unshare($user,$contact);
  108. }
  109. elseif($contact['network'] === NETWORK_DFRN) {
  110. require_once('include/dfrn.php');
  111. dfrn::deliver($user,$contact,'placeholder', 1);
  112. }
  113. }
  114. // Contact has refused to recognise us as a friend. We will start a countdown.
  115. // If they still don't recognise us in 32 days, the relationship is over,
  116. // and we won't waste any more time trying to communicate with them.
  117. // This provides for the possibility that their database is temporarily messed
  118. // up or some other transient event and that there's a possibility we could recover from it.
  119. if(! function_exists('mark_for_death')) {
  120. function mark_for_death($contact) {
  121. if($contact['archive'])
  122. return;
  123. if($contact['term-date'] == '0000-00-00 00:00:00') {
  124. q("UPDATE `contact` SET `term-date` = '%s' WHERE `id` = %d",
  125. dbesc(datetime_convert()),
  126. intval($contact['id'])
  127. );
  128. }
  129. else {
  130. /// @todo
  131. /// We really should send a notification to the owner after 2-3 weeks
  132. /// so they won't be surprised when the contact vanishes and can take
  133. /// remedial action if this was a serious mistake or glitch
  134. $expiry = $contact['term-date'] . ' + 32 days ';
  135. if(datetime_convert() > datetime_convert('UTC','UTC',$expiry)) {
  136. // relationship is really truly dead.
  137. // archive them rather than delete
  138. // though if the owner tries to unarchive them we'll start the whole process over again
  139. q("update contact set `archive` = 1 where id = %d",
  140. intval($contact['id'])
  141. );
  142. q("UPDATE `item` SET `private` = 2 WHERE `contact-id` = %d AND `uid` = %d", intval($contact['id']), intval($contact['uid']));
  143. //contact_remove($contact['id']);
  144. }
  145. }
  146. }}
  147. if(! function_exists('unmark_for_death')) {
  148. function unmark_for_death($contact) {
  149. // It's a miracle. Our dead contact has inexplicably come back to life.
  150. q("UPDATE `contact` SET `term-date` = '%s' WHERE `id` = %d",
  151. dbesc('0000-00-00 00:00:00'),
  152. intval($contact['id'])
  153. );
  154. }}
  155. /**
  156. * @brief Get contact data for a given profile link
  157. *
  158. * The function looks at several places (contact table and gcontact table) for the contact
  159. *
  160. * @param string $url The profile link
  161. * @param int $uid User id
  162. * @param array $default If not data was found take this data as default value
  163. *
  164. * @return array Contact data
  165. */
  166. function get_contact_details_by_url($url, $uid = -1, $default = array()) {
  167. if ($uid == -1)
  168. $uid = local_user();
  169. // Fetch contact data from the contact table for the given user
  170. $r = q("SELECT `id`, `id` AS `cid`, 0 AS `gid`, 0 AS `zid`, `uid`, `url`, `nurl`, `alias`, `network`, `name`, `nick`, `addr`, `location`, `about`,
  171. `keywords`, `gender`, `photo`, `thumb`, `micro`, `forum`, `prv`, (`forum` | `prv`) AS `community`, `bd` AS `birthday`, `self`
  172. FROM `contact` WHERE `nurl` = '%s' AND `uid` = %d",
  173. dbesc(normalise_link($url)), intval($uid));
  174. // Fetch the data from the contact table with "uid=0" (which is filled automatically)
  175. if (!$r)
  176. $r = q("SELECT `id`, 0 AS `cid`, `id` AS `zid`, 0 AS `gid`, `uid`, `url`, `nurl`, `alias`, `network`, `name`, `nick`, `addr`, `location`, `about`,
  177. `keywords`, `gender`, `photo`, `thumb`, `micro`, `forum`, `prv`, (`forum` | `prv`) AS `community`, `bd` AS `birthday`, 0 AS `self`
  178. FROM `contact` WHERE `nurl` = '%s' AND `uid` = 0",
  179. dbesc(normalise_link($url)));
  180. // Fetch the data from the gcontact table
  181. if (!$r)
  182. $r = q("SELECT 0 AS `id`, 0 AS `cid`, `id` AS `gid`, 0 AS `zid`, 0 AS `uid`, `url`, `nurl`, `alias`, `network`, `name`, `nick`, `addr`, `location`, `about`,
  183. `keywords`, `gender`, `photo`, `photo` AS `thumb`, `photo` AS `micro`, `community` AS `forum`, 0 AS `prv`, `community`, `birthday`, 0 AS `self`
  184. FROM `gcontact` WHERE `nurl` = '%s'",
  185. dbesc(normalise_link($url)));
  186. if ($r) {
  187. // If there is more than one entry we filter out the connector networks
  188. if (count($r) > 1)
  189. foreach ($r AS $id => $result)
  190. if ($result["network"] == NETWORK_STATUSNET)
  191. unset($r[$id]);
  192. $profile = array_shift($r);
  193. // "bd" always contains the upcoming birthday of a contact.
  194. // "birthday" might contain the birthday including the year of birth.
  195. if ($profile["birthday"] != "0000-00-00") {
  196. $bd_timestamp = strtotime($profile["birthday"]);
  197. $month = date("m", $bd_timestamp);
  198. $day = date("d", $bd_timestamp);
  199. $current_timestamp = time();
  200. $current_year = date("Y", $current_timestamp);
  201. $current_month = date("m", $current_timestamp);
  202. $current_day = date("d", $current_timestamp);
  203. $profile["bd"] = $current_year."-".$month."-".$day;
  204. $current = $current_year."-".$current_month."-".$current_day;
  205. if ($profile["bd"] < $current)
  206. $profile["bd"] = (++$current_year)."-".$month."-".$day;
  207. } else
  208. $profile["bd"] = "0000-00-00";
  209. } else
  210. $profile = $default;
  211. if (($profile["photo"] == "") AND isset($default["photo"]))
  212. $profile["photo"] = $default["photo"];
  213. if (($profile["name"] == "") AND isset($default["name"]))
  214. $profile["name"] = $default["name"];
  215. if (($profile["network"] == "") AND isset($default["network"]))
  216. $profile["network"] = $default["network"];
  217. if (($profile["thumb"] == "") AND isset($profile["photo"]))
  218. $profile["thumb"] = $profile["photo"];
  219. if (($profile["micro"] == "") AND isset($profile["thumb"]))
  220. $profile["micro"] = $profile["thumb"];
  221. if ((($profile["addr"] == "") OR ($profile["name"] == "")) AND ($profile["gid"] != 0) AND
  222. in_array($profile["network"], array(NETWORK_DFRN, NETWORK_DIASPORA, NETWORK_OSTATUS)))
  223. proc_run('php',"include/update_gcontact.php", $profile["gid"]);
  224. // Show contact details of Diaspora contacts only if connected
  225. if (($profile["cid"] == 0) AND ($profile["network"] == NETWORK_DIASPORA)) {
  226. $profile["location"] = "";
  227. $profile["about"] = "";
  228. $profile["gender"] = "";
  229. $profile["birthday"] = "0000-00-00";
  230. }
  231. return($profile);
  232. }
  233. if(! function_exists('contact_photo_menu')){
  234. function contact_photo_menu($contact, $uid = 0) {
  235. $a = get_app();
  236. $contact_url="";
  237. $pm_url="";
  238. $status_link="";
  239. $photos_link="";
  240. $posts_link="";
  241. $contact_drop_link = "";
  242. $poke_link="";
  243. if ($uid == 0)
  244. $uid = local_user();
  245. if ($contact["uid"] != $uid) {
  246. if ($uid == 0) {
  247. $profile_link = zrl($contact['url']);
  248. $menu = Array('profile' => array(t("View Profile"), $profile_link, true));
  249. return $menu;
  250. }
  251. $r = q("SELECT * FROM `contact` WHERE `nurl` = '%s' AND `network` = '%s' AND `uid` = %d",
  252. dbesc($contact["nurl"]), dbesc($contact["network"]), intval($uid));
  253. if ($r)
  254. return contact_photo_menu($r[0], $uid);
  255. else {
  256. $profile_link = zrl($contact['url']);
  257. $connlnk = 'follow/?url='.$contact['url'];
  258. $menu = Array(
  259. 'profile' => array(t("View Profile"), $profile_link, true),
  260. 'follow' => array(t("Connect/Follow"), $connlnk, true)
  261. );
  262. return $menu;
  263. }
  264. }
  265. $sparkle = false;
  266. if($contact['network'] === NETWORK_DFRN) {
  267. $sparkle = true;
  268. $profile_link = $a->get_baseurl() . '/redir/' . $contact['id'];
  269. }
  270. else
  271. $profile_link = $contact['url'];
  272. if($profile_link === 'mailbox')
  273. $profile_link = '';
  274. if($sparkle) {
  275. $status_link = $profile_link . "?url=status";
  276. $photos_link = $profile_link . "?url=photos";
  277. $profile_link = $profile_link . "?url=profile";
  278. }
  279. if (in_array($contact["network"], array(NETWORK_DFRN, NETWORK_DIASPORA)))
  280. $pm_url = $a->get_baseurl() . '/message/new/' . $contact['id'];
  281. if ($contact["network"] == NETWORK_DFRN)
  282. $poke_link = $a->get_baseurl() . '/poke/?f=&c=' . $contact['id'];
  283. $contact_url = $a->get_baseurl() . '/contacts/' . $contact['id'];
  284. $posts_link = $a->get_baseurl() . "/contacts/" . $contact['id'] . '/posts';
  285. $contact_drop_link = $a->get_baseurl() . "/contacts/" . $contact['id'] . '/drop?confirm=1';
  286. /**
  287. * menu array:
  288. * "name" => [ "Label", "link", (bool)Should the link opened in a new tab? ]
  289. */
  290. $menu = Array(
  291. 'status' => array(t("View Status"), $status_link, true),
  292. 'profile' => array(t("View Profile"), $profile_link, true),
  293. 'photos' => array(t("View Photos"), $photos_link,true),
  294. 'network' => array(t("Network Posts"), $posts_link,false),
  295. 'edit' => array(t("Edit Contact"), $contact_url, false),
  296. 'drop' => array(t("Drop Contact"), $contact_drop_link, false),
  297. 'pm' => array(t("Send PM"), $pm_url, false),
  298. 'poke' => array(t("Poke"), $poke_link, false),
  299. );
  300. $args = array('contact' => $contact, 'menu' => &$menu);
  301. call_hooks('contact_photo_menu', $args);
  302. $menucondensed = array();
  303. foreach ($menu AS $menuname=>$menuitem)
  304. if ($menuitem[1] != "")
  305. $menucondensed[$menuname] = $menuitem;
  306. return $menucondensed;
  307. }}
  308. function random_profile() {
  309. $r = q("SELECT `url` FROM `gcontact` WHERE `network` = '%s'
  310. AND `last_contact` >= `last_failure`
  311. AND `updated` > UTC_TIMESTAMP - INTERVAL 1 MONTH
  312. ORDER BY rand() LIMIT 1",
  313. dbesc(NETWORK_DFRN));
  314. if(count($r))
  315. return dirname($r[0]['url']);
  316. return '';
  317. }
  318. function contacts_not_grouped($uid,$start = 0,$count = 0) {
  319. if(! $count) {
  320. $r = q("select count(*) as total from contact where uid = %d and self = 0 and id not in (select distinct(`contact-id`) from group_member where uid = %d) ",
  321. intval($uid),
  322. intval($uid)
  323. );
  324. return $r;
  325. }
  326. $r = q("select * from contact where uid = %d and self = 0 and id not in (select distinct(`contact-id`) from group_member where uid = %d) and blocked = 0 and pending = 0 limit %d, %d",
  327. intval($uid),
  328. intval($uid),
  329. intval($start),
  330. intval($count)
  331. );
  332. return $r;
  333. }
  334. function get_contact($url, $uid = 0) {
  335. require_once("include/Scrape.php");
  336. $data = array();
  337. $contactid = 0;
  338. // is it an address in the format user@server.tld?
  339. /// @todo use gcontact and/or the addr field for a lookup
  340. if (!strstr($url, "http") OR strstr($url, "@")) {
  341. $data = probe_url($url);
  342. $url = $data["url"];
  343. if ($url == "")
  344. return 0;
  345. }
  346. $contact = q("SELECT `id`, `avatar-date` FROM `contact` WHERE `nurl` = '%s' AND `uid` = %d ORDER BY `id` LIMIT 2",
  347. dbesc(normalise_link($url)),
  348. intval($uid));
  349. if (!$contact)
  350. $contact = q("SELECT `id`, `avatar-date` FROM `contact` WHERE `alias` IN ('%s', '%s') AND `uid` = %d ORDER BY `id` LIMIT 1",
  351. dbesc($url),
  352. dbesc(normalise_link($url)),
  353. intval($uid));
  354. if ($contact) {
  355. $contactid = $contact[0]["id"];
  356. // Update the contact every 7 days
  357. $update_photo = ($contact[0]['avatar-date'] < datetime_convert('','','now -7 days'));
  358. //$update_photo = ($contact[0]['avatar-date'] < datetime_convert('','','now -12 hours'));
  359. if (!$update_photo)
  360. return($contactid);
  361. } elseif ($uid != 0)
  362. return 0;
  363. if (!count($data))
  364. $data = probe_url($url);
  365. // Does this address belongs to a valid network?
  366. if (!in_array($data["network"], array(NETWORK_DFRN, NETWORK_OSTATUS, NETWORK_DIASPORA))) {
  367. if ($uid != 0)
  368. return 0;
  369. // Get data from the gcontact table
  370. $r = q("SELECT `name`, `nick`, `url`, `photo`, `addr`, `alias`, `network` FROM `gcontact` WHERE `nurl` = '%s'",
  371. dbesc(normalise_link($url)));
  372. if (!$r)
  373. return 0;
  374. $data = $r[0];
  375. }
  376. $url = $data["url"];
  377. if ($contactid == 0) {
  378. q("INSERT INTO `contact` (`uid`, `created`, `url`, `nurl`, `addr`, `alias`, `notify`, `poll`,
  379. `name`, `nick`, `photo`, `network`, `pubkey`, `rel`, `priority`,
  380. `batch`, `request`, `confirm`, `poco`,
  381. `writable`, `blocked`, `readonly`, `pending`)
  382. VALUES (%d, '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', %d, %d, '%s', '%s', '%s', '%s', 1, 0, 0, 0)",
  383. intval($uid),
  384. dbesc(datetime_convert()),
  385. dbesc($data["url"]),
  386. dbesc(normalise_link($data["url"])),
  387. dbesc($data["addr"]),
  388. dbesc($data["alias"]),
  389. dbesc($data["notify"]),
  390. dbesc($data["poll"]),
  391. dbesc($data["name"]),
  392. dbesc($data["nick"]),
  393. dbesc($data["photo"]),
  394. dbesc($data["network"]),
  395. dbesc($data["pubkey"]),
  396. intval(CONTACT_IS_SHARING),
  397. intval($data["priority"]),
  398. dbesc($data["batch"]),
  399. dbesc($data["request"]),
  400. dbesc($data["confirm"]),
  401. dbesc($data["poco"])
  402. );
  403. $contact = q("SELECT `id` FROM `contact` WHERE `nurl` = '%s' AND `uid` = %d ORDER BY `id` LIMIT 2",
  404. dbesc(normalise_link($data["url"])),
  405. intval($uid));
  406. if (!$contact)
  407. return 0;
  408. $contactid = $contact[0]["id"];
  409. // Update the newly created contact from data in the gcontact table
  410. $r = q("SELECT `location`, `about`, `keywords`, `gender` FROM `gcontact` WHERE `nurl` = '%s'",
  411. dbesc(normalise_link($data["url"])));
  412. if ($r) {
  413. logger("Update contact ".$data["url"]);
  414. q("UPDATE `contact` SET `location` = '%s', `about` = '%s', `keywords` = '%s', `gender` = '%s' WHERE `id` = %d",
  415. dbesc($r["location"]), dbesc($r["about"]), dbesc($r["keywords"]),
  416. dbesc($r["gender"]), intval($contactid));
  417. }
  418. }
  419. if ((count($contact) > 1) AND ($uid == 0) AND ($contactid != 0) AND ($url != ""))
  420. q("DELETE FROM `contact` WHERE `nurl` = '%s' AND `id` != %d",
  421. dbesc(normalise_link($url)),
  422. intval($contactid));
  423. require_once("Photo.php");
  424. update_contact_avatar($data["photo"],$uid,$contactid);
  425. q("UPDATE `contact` SET `addr` = '%s', `alias` = '%s', `name` = '%s', `nick` = '%s',
  426. `name-date` = '%s', `uri-date` = '%s' WHERE `id` = %d",
  427. dbesc($data["addr"]),
  428. dbesc($data["alias"]),
  429. dbesc($data["name"]),
  430. dbesc($data["nick"]),
  431. dbesc(datetime_convert()),
  432. dbesc(datetime_convert()),
  433. intval($contactid)
  434. );
  435. return $contactid;
  436. }
  437. /**
  438. * @brief Returns posts from a given gcontact
  439. *
  440. * @param App $a argv application class
  441. * @param int $gcontact_id Global contact
  442. *
  443. * @return string posts in HTML
  444. */
  445. function posts_from_gcontact($a, $gcontact_id) {
  446. require_once('include/conversation.php');
  447. // There are no posts with "uid = 0" with connector networks
  448. // This speeds up the query a lot
  449. $r = q("SELECT `network` FROM `gcontact` WHERE `id` = %d", dbesc($gcontact_id));
  450. if (in_array($r[0]["network"], array(NETWORK_DFRN, NETWORK_DIASPORA, NETWORK_OSTATUS, "")))
  451. $sql = "(`item`.`uid` = 0 OR (`item`.`uid` = %d AND `item`.`private`))";
  452. else
  453. $sql = "`item`.`uid` = %d";
  454. if(get_config('system', 'old_pager')) {
  455. $r = q("SELECT COUNT(*) AS `total` FROM `item`
  456. WHERE `gcontact-id` = %d and $sql",
  457. intval($gcontact_id),
  458. intval(local_user()));
  459. $a->set_pager_total($r[0]['total']);
  460. }
  461. $r = q("SELECT `item`.`uri`, `item`.*, `item`.`id` AS `item_id`,
  462. `author-name` AS `name`, `owner-avatar` AS `photo`,
  463. `owner-link` AS `url`, `owner-avatar` AS `thumb`
  464. FROM `item` FORCE INDEX (`gcontactid_uid_created`)
  465. WHERE `gcontact-id` = %d AND $sql AND
  466. NOT `deleted` AND NOT `moderated` AND `visible`
  467. ORDER BY `item`.`created` DESC LIMIT %d, %d",
  468. intval($gcontact_id),
  469. intval(local_user()),
  470. intval($a->pager['start']),
  471. intval($a->pager['itemspage'])
  472. );
  473. $o = conversation($a,$r,'community',false);
  474. if(!get_config('system', 'old_pager')) {
  475. $o .= alt_pager($a,count($r));
  476. } else {
  477. $o .= paginate($a);
  478. }
  479. return $o;
  480. }
  481. /**
  482. * @brief Returns posts from a given contact
  483. *
  484. * @param App $a argv application class
  485. * @param int $contact_id contact
  486. *
  487. * @return string posts in HTML
  488. */
  489. function posts_from_contact($a, $contact_id) {
  490. require_once('include/conversation.php');
  491. $r = q("SELECT `url` FROM `contact` WHERE `id` = %d", intval($contact_id));
  492. if (!$r)
  493. return false;
  494. $contact = $r[0];
  495. if(get_config('system', 'old_pager')) {
  496. $r = q("SELECT COUNT(*) AS `total` FROM `item`
  497. WHERE `item`.`uid` = %d AND `author-link` IN ('%s', '%s')",
  498. intval(local_user()),
  499. dbesc(str_replace("https://", "http://", $contact["url"])),
  500. dbesc(str_replace("http://", "https://", $contact["url"])));
  501. $a->set_pager_total($r[0]['total']);
  502. }
  503. $r = q("SELECT `item`.`uri`, `item`.*, `item`.`id` AS `item_id`,
  504. `author-name` AS `name`, `owner-avatar` AS `photo`,
  505. `owner-link` AS `url`, `owner-avatar` AS `thumb`
  506. FROM `item` FORCE INDEX (`uid_contactid_created`)
  507. WHERE `item`.`uid` = %d AND `contact-id` = %d
  508. AND `author-link` IN ('%s', '%s')
  509. AND NOT `deleted` AND NOT `moderated` AND `visible`
  510. ORDER BY `item`.`created` DESC LIMIT %d, %d",
  511. intval(local_user()),
  512. intval($contact_id),
  513. dbesc(str_replace("https://", "http://", $contact["url"])),
  514. dbesc(str_replace("http://", "https://", $contact["url"])),
  515. intval($a->pager['start']),
  516. intval($a->pager['itemspage'])
  517. );
  518. $o .= conversation($a,$r,'community',false);
  519. if(!get_config('system', 'old_pager'))
  520. $o .= alt_pager($a,count($r));
  521. else
  522. $o .= paginate($a);
  523. return $o;
  524. }
  525. /**
  526. * @brief Returns a formatted location string from the given profile array
  527. *
  528. * @param array $profile Profile array (Generated from the "profile" table)
  529. *
  530. * @return string Location string
  531. */
  532. function formatted_location($profile) {
  533. $location = '';
  534. if($profile['locality'])
  535. $location .= $profile['locality'];
  536. if($profile['region'] AND ($profile['locality'] != $profile['region'])) {
  537. if($location)
  538. $location .= ', ';
  539. $location .= $profile['region'];
  540. }
  541. if($profile['country-name']) {
  542. if($location)
  543. $location .= ', ';
  544. $location .= $profile['country-name'];
  545. }
  546. return $location;
  547. }
  548. ?>