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.
 
 
 
 
 
 

736 lines
20 KiB

  1. <?php
  2. require_once('include/datetime.php');
  3. require_once("include/Scrape.php");
  4. /*
  5. To-Do:
  6. - noscrape for updating contact fields and "last updated"
  7. - use /poco/@global for discovering contacts from other servers
  8. */
  9. /*
  10. * poco_load
  11. *
  12. * Given a contact-id (minimum), load the PortableContacts friend list for that contact,
  13. * and add the entries to the gcontact (Global Contact) table, or update existing entries
  14. * if anything (name or photo) has changed.
  15. * We use normalised urls for comparison which ignore http vs https and www.domain vs domain
  16. *
  17. * Once the global contact is stored add (if necessary) the contact linkage which associates
  18. * the given uid, cid to the global contact entry. There can be many uid/cid combinations
  19. * pointing to the same global contact id.
  20. *
  21. */
  22. function poco_load($cid,$uid = 0,$zcid = 0,$url = null) {
  23. require_once("include/html2bbcode.php");
  24. $a = get_app();
  25. if($cid) {
  26. if((! $url) || (! $uid)) {
  27. $r = q("select `poco`, `uid` from `contact` where `id` = %d limit 1",
  28. intval($cid)
  29. );
  30. if(count($r)) {
  31. $url = $r[0]['poco'];
  32. $uid = $r[0]['uid'];
  33. }
  34. }
  35. if(! $uid)
  36. return;
  37. }
  38. if(! $url)
  39. return;
  40. $url = $url . (($uid) ? '/@me/@all?fields=displayName,urls,photos,updated,network,aboutMe,currentLocation,tags,gender,generation' : '?fields=displayName,urls,photos,updated,network,aboutMe,currentLocation,tags,gender,generation') ;
  41. logger('poco_load: ' . $url, LOGGER_DEBUG);
  42. $s = fetch_url($url);
  43. logger('poco_load: returns ' . $s, LOGGER_DATA);
  44. logger('poco_load: return code: ' . $a->get_curl_code(), LOGGER_DEBUG);
  45. if(($a->get_curl_code() > 299) || (! $s))
  46. return;
  47. $j = json_decode($s);
  48. logger('poco_load: json: ' . print_r($j,true),LOGGER_DATA);
  49. if(! isset($j->entry))
  50. return;
  51. $total = 0;
  52. foreach($j->entry as $entry) {
  53. $total ++;
  54. $profile_url = '';
  55. $profile_photo = '';
  56. $connect_url = '';
  57. $name = '';
  58. $network = '';
  59. $updated = '0000-00-00 00:00:00';
  60. $location = '';
  61. $about = '';
  62. $keywords = '';
  63. $gender = '';
  64. $generation = 0;
  65. $name = $entry->displayName;
  66. if(isset($entry->urls)) {
  67. foreach($entry->urls as $url) {
  68. if($url->type == 'profile') {
  69. $profile_url = $url->value;
  70. continue;
  71. }
  72. if($url->type == 'webfinger') {
  73. $connect_url = str_replace('acct:' , '', $url->value);
  74. continue;
  75. }
  76. }
  77. }
  78. if(isset($entry->photos)) {
  79. foreach($entry->photos as $photo) {
  80. if($photo->type == 'profile') {
  81. $profile_photo = $photo->value;
  82. continue;
  83. }
  84. }
  85. }
  86. if(isset($entry->updated))
  87. $updated = date("Y-m-d H:i:s", strtotime($entry->updated));
  88. if(isset($entry->network))
  89. $network = $entry->network;
  90. if(isset($entry->currentLocation))
  91. $location = $entry->currentLocation;
  92. if(isset($entry->aboutMe))
  93. $about = html2bbcode($entry->aboutMe);
  94. if(isset($entry->gender))
  95. $gender = $entry->gender;
  96. if(isset($entry->generation) AND ($entry->generation > 0))
  97. $generation = ++$entry->generation;
  98. if(isset($entry->tags))
  99. foreach($entry->tags as $tag)
  100. $keywords = implode(", ", $tag);
  101. // If you query a Friendica server for its profiles, the network has to be Friendica
  102. // To-Do: It could also be a Redmatrix server
  103. //if ($uid == 0)
  104. // $network = NETWORK_DFRN;
  105. poco_check($profile_url, $name, $network, $profile_photo, $about, $location, $gender, $keywords, $connect_url, $updated, $generation, $cid, $uid, $zcid);
  106. // Update the Friendica contacts. Diaspora is doing it via a message. (See include/diaspora.php)
  107. if (($location != "") OR ($about != "") OR ($keywords != "") OR ($gender != ""))
  108. q("UPDATE `contact` SET `location` = '%s', `about` = '%s', `keywords` = '%s', `gender` = '%s'
  109. WHERE `nurl` = '%s' AND NOT `self` AND `network` = '%s'",
  110. dbesc($location),
  111. dbesc($about),
  112. dbesc($keywords),
  113. dbesc($gender),
  114. dbesc(normalise_link($profile_url)),
  115. dbesc(NETWORK_DFRN));
  116. }
  117. logger("poco_load: loaded $total entries",LOGGER_DEBUG);
  118. q("DELETE FROM `glink` WHERE `cid` = %d AND `uid` = %d AND `zcid` = %d AND `updated` < UTC_TIMESTAMP - INTERVAL 2 DAY",
  119. intval($cid),
  120. intval($uid),
  121. intval($zcid)
  122. );
  123. }
  124. function poco_check($profile_url, $name, $network, $profile_photo, $about, $location, $gender, $keywords, $connect_url, $updated, $generation, $cid = 0, $uid = 0, $zcid = 0) {
  125. $a = get_app();
  126. // Generation:
  127. // 0: No definition
  128. // 1: Profiles on this server
  129. // 2: Contacts of profiles on this server
  130. // 3: Contacts of contacts of profiles on this server
  131. // 4: ...
  132. $gcid = "";
  133. if ($profile_url == "")
  134. return $gcid;
  135. $orig_updated = $updated;
  136. // Don't store the statusnet connector as network
  137. // We can't simply set this to NETWORK_OSTATUS since the connector could have fetched posts from friendica as well
  138. if ($network == NETWORK_STATUSNET)
  139. $network = "";
  140. // The global contacts should contain the original picture, not the cached one
  141. if (($generation != 1) AND stristr(normalise_link($profile_photo), normalise_link($a->get_baseurl()."/photo/")))
  142. $profile_photo = "";
  143. $r = q("SELECT `network` FROM `contact` WHERE `nurl` = '%s' AND `network` != '' AND `network` != '%s' LIMIT 1",
  144. dbesc(normalise_link($profile_url)), dbesc(NETWORK_STATUSNET)
  145. );
  146. if(count($r))
  147. $network = $r[0]["network"];
  148. if (($network == "") OR ($network == NETWORK_OSTATUS)) {
  149. $r = q("SELECT `network`, `url` FROM `contact` WHERE `alias` IN ('%s', '%s') AND `network` != '' AND `network` != '%s' LIMIT 1",
  150. dbesc($profile_url), dbesc(normalise_link($profile_url)), dbesc(NETWORK_STATUSNET)
  151. );
  152. if(count($r)) {
  153. $network = $r[0]["network"];
  154. $profile_url = $r[0]["url"];
  155. }
  156. }
  157. $x = q("SELECT * FROM `gcontact` WHERE `nurl` = '%s' LIMIT 1",
  158. dbesc(normalise_link($profile_url))
  159. );
  160. if (count($x)) {
  161. if (($network == "") AND ($x[0]["network"] != NETWORK_STATUSNET))
  162. $network = $x[0]["network"];
  163. if ($updated == "0000-00-00 00:00:00")
  164. $updated = $x[0]["updated"];
  165. $last_contact = $x[0]["last_contact"];
  166. $last_failure = $x[0]["last_failure"];
  167. } else {
  168. $last_contact = "0000-00-00 00:00:00";
  169. $last_failure = "0000-00-00 00:00:00";
  170. }
  171. if (($network == "") OR ($name == "") OR ($profile_photo == "")) {
  172. $data = probe_url($profile_url);
  173. $network = $data["network"];
  174. $name = $data["name"];
  175. $profile_url = $data["url"];
  176. $profile_photo = $data["photo"];
  177. }
  178. if (count($x) AND ($x[0]["network"] == "") AND ($network != "")) {
  179. q("UPDATE `gcontact` SET `network` = '%s' WHERE `nurl` = '%s'",
  180. dbesc($network),
  181. dbesc(normalise_link($profile_url))
  182. );
  183. }
  184. if (($name == "") OR ($profile_photo == ""))
  185. return $gcid;
  186. if (!in_array($network, array(NETWORK_DFRN, NETWORK_OSTATUS, NETWORK_DIASPORA)))
  187. return $gcid;
  188. logger("profile-check generation: ".$generation." Network: ".$network." URL: ".$profile_url." name: ".$name." avatar: ".$profile_photo, LOGGER_DEBUG);
  189. // Only fetch last update manually if it wasn't provided and enabled in the system
  190. if (get_config('system','ld_discover_activity') AND ($orig_updated == "0000-00-00 00:00:00") AND poco_do_update($updated, $last_contact, $last_failure)) {
  191. $last_updated = poco_last_updated($profile_url);
  192. if ($last_updated) {
  193. $updated = $last_updated;
  194. $last_contact = datetime_convert();
  195. logger("Last updated for profile ".$profile_url.": ".$updated, LOGGER_DEBUG);
  196. if (count($x))
  197. q("UPDATE `gcontact` SET `last_contact` = '%s' WHERE `nurl` = '%s'", dbesc($last_contact), dbesc(normalise_link($profile_url)));
  198. } else {
  199. $last_failure = datetime_convert();
  200. if (count($x))
  201. q("UPDATE `gcontact` SET `last_failure` = '%s' WHERE `nurl` = '%s'", dbesc($last_failure), dbesc(normalise_link($profile_url)));
  202. }
  203. }
  204. if(count($x)) {
  205. $gcid = $x[0]['id'];
  206. if (($location == "") AND ($x[0]['location'] != ""))
  207. $location = $x[0]['location'];
  208. if (($about == "") AND ($x[0]['about'] != ""))
  209. $about = $x[0]['about'];
  210. if (($gender == "") AND ($x[0]['gender'] != ""))
  211. $gender = $x[0]['gender'];
  212. if (($keywords == "") AND ($x[0]['keywords'] != ""))
  213. $keywords = $x[0]['keywords'];
  214. if (($generation == 0) AND ($x[0]['generation'] > 0))
  215. $generation = $x[0]['generation'];
  216. if($x[0]['name'] != $name || $x[0]['photo'] != $profile_photo || $x[0]['updated'] < $updated) {
  217. q("UPDATE `gcontact` SET `name` = '%s', `network` = '%s', `photo` = '%s', `connect` = '%s', `url` = '%s',
  218. `updated` = '%s', `location` = '%s', `about` = '%s', `keywords` = '%s', `gender` = '%s', `generation` = %d
  219. WHERE (`generation` >= %d OR `generation` = 0) AND `nurl` = '%s'",
  220. dbesc($name),
  221. dbesc($network),
  222. dbesc($profile_photo),
  223. dbesc($connect_url),
  224. dbesc($profile_url),
  225. dbesc($updated),
  226. dbesc($location),
  227. dbesc($about),
  228. dbesc($keywords),
  229. dbesc($gender),
  230. intval($generation),
  231. intval($generation),
  232. dbesc(normalise_link($profile_url))
  233. );
  234. }
  235. } else {
  236. q("INSERT INTO `gcontact` (`name`,`network`, `url`,`nurl`,`photo`,`connect`, `updated`, `last_contact`, `last_failure`, `location`, `about`, `keywords`, `gender`, `generation`)
  237. VALUES ('%s', '%s', '%s', '%s', '%s','%s', '%s', '%s', '%s', '%s', '%s', %d)",
  238. dbesc($name),
  239. dbesc($network),
  240. dbesc($profile_url),
  241. dbesc(normalise_link($profile_url)),
  242. dbesc($profile_photo),
  243. dbesc($connect_url),
  244. dbesc($updated),
  245. dbesc($last_contact),
  246. dbesc($last_failure),
  247. dbesc($location),
  248. dbesc($about),
  249. dbesc($keywords),
  250. dbesc($gender),
  251. intval($generation)
  252. );
  253. $x = q("SELECT * FROM `gcontact` WHERE `nurl` = '%s' LIMIT 1",
  254. dbesc(normalise_link($profile_url))
  255. );
  256. if(count($x))
  257. $gcid = $x[0]['id'];
  258. }
  259. if(! $gcid)
  260. return $gcid;
  261. $r = q("SELECT * FROM `glink` WHERE `cid` = %d AND `uid` = %d AND `gcid` = %d AND `zcid` = %d LIMIT 1",
  262. intval($cid),
  263. intval($uid),
  264. intval($gcid),
  265. intval($zcid)
  266. );
  267. if(! count($r)) {
  268. q("INSERT INTO `glink` (`cid`,`uid`,`gcid`,`zcid`, `updated`) VALUES (%d,%d,%d,%d, '%s') ",
  269. intval($cid),
  270. intval($uid),
  271. intval($gcid),
  272. intval($zcid),
  273. dbesc(datetime_convert())
  274. );
  275. } else {
  276. q("UPDATE `glink` SET `updated` = '%s' WHERE `cid` = %d AND `uid` = %d AND `gcid` = %d AND `zcid` = %d",
  277. dbesc(datetime_convert()),
  278. intval($cid),
  279. intval($uid),
  280. intval($gcid),
  281. intval($zcid)
  282. );
  283. }
  284. // For unknown reasons there are sometimes duplicates
  285. q("DELETE FROM `gcontact` WHERE `nurl` = '%s' AND `id` != %d AND
  286. NOT EXISTS (SELECT `gcid` FROM `glink` WHERE `gcid` = `gcontact`.`id`)",
  287. dbesc(normalise_link($profile_url)),
  288. intval($gcid)
  289. );
  290. return $gcid;
  291. }
  292. function poco_last_updated($profile) {
  293. $data = probe_url($profile);
  294. if (($data["poll"] == "") OR ($data["network"] == NETWORK_FEED))
  295. return false;
  296. // To-Do: Use noscrape
  297. $feedret = z_fetch_url($data["poll"]);
  298. if (!$feedret["success"])
  299. return false;
  300. $doc = new DOMDocument();
  301. @$doc->loadXML($feedret["body"]);
  302. $xpath = new DomXPath($doc);
  303. $xpath->registerNamespace('atom', "http://www.w3.org/2005/Atom");
  304. $entries = $xpath->query('/atom:feed/atom:entry');
  305. $last_updated = "";
  306. foreach ($entries AS $entry) {
  307. $published = $xpath->query('atom:published/text()', $entry)->item(0)->nodeValue;
  308. $updated = $xpath->query('atom:updated/text()', $entry)->item(0)->nodeValue;
  309. if ($last_updated < $published)
  310. $last_updated = $published;
  311. if ($last_updated < $updated)
  312. $last_updated = $updated;
  313. }
  314. // Maybe there aren't any entries. Then check if it is a valid feed
  315. if ($last_updated == "")
  316. if ($xpath->query('/atom:feed')->length > 0)
  317. $last_updated = "0000-00-00 00:00:00";
  318. return($last_updated);
  319. }
  320. function poco_do_update($updated, $last_contact, $last_failure) {
  321. $now = strtotime(datetime_convert());
  322. if ($updated > $last_contact)
  323. $contact_time = strtotime($updated);
  324. else
  325. $contact_time = strtotime($last_contact);
  326. $failure_time = strtotime($last_failure);
  327. // If the last contact was less than 24 hours then don't update
  328. if (($now - $contact_time) < (60 * 60 * 24))
  329. return false;
  330. // If the last failure was less than 24 hours then don't update
  331. if (($now - $failure_time) < (60 * 60 * 24))
  332. return false;
  333. // If the last contact was less than a week ago and the last failure is older than a week then don't update
  334. if ((($now - $contact_time) < (60 * 60 * 24 * 7)) AND ($contact_time > $failure_time))
  335. return false;
  336. // If the last contact time was more than a week ago, then only try once a week
  337. if (($now - $contact_time) > (60 * 60 * 24 * 7) AND ($now - $failure_time) < (60 * 60 * 24 * 7))
  338. return false;
  339. // If the last contact time was more than a month ago, then only try once a month
  340. if (($now - $contact_time) > (60 * 60 * 24 * 30) AND ($now - $failure_time) < (60 * 60 * 24 * 30))
  341. return false;
  342. return true;
  343. }
  344. function poco_contact_from_body($body, $created, $cid, $uid) {
  345. preg_replace_callback("/\[share(.*?)\].*?\[\/share\]/ism",
  346. function ($match) use ($created, $cid, $uid){
  347. return(sub_poco_from_share($match, $created, $cid, $uid));
  348. }, $body);
  349. }
  350. function sub_poco_from_share($share, $created, $cid, $uid) {
  351. $profile = "";
  352. preg_match("/profile='(.*?)'/ism", $share[1], $matches);
  353. if ($matches[1] != "")
  354. $profile = $matches[1];
  355. preg_match('/profile="(.*?)"/ism', $share[1], $matches);
  356. if ($matches[1] != "")
  357. $profile = $matches[1];
  358. if ($profile == "")
  359. return;
  360. logger("prepare poco_check for profile ".$profile, LOGGER_DEBUG);
  361. poco_check($profile, "", "", "", "", "", "", "", "", $created, 3, $cid, $uid);
  362. }
  363. function poco_store($item) {
  364. // Isn't it public?
  365. if ($item['private'])
  366. return;
  367. // Or is it from a network where we don't store the global contacts?
  368. if (!in_array($item["network"], array(NETWORK_DFRN, NETWORK_DIASPORA, NETWORK_OSTATUS, NETWORK_STATUSNET, "")))
  369. return;
  370. // Is it a global copy?
  371. $store_gcontact = ($item["uid"] == 0);
  372. // Is it a comment on a global copy?
  373. if (!$store_gcontact AND ($item["uri"] != $item["parent-uri"])) {
  374. $q = q("SELECT `id` FROM `item` WHERE `uri`='%s' AND `uid` = 0", $item["parent-uri"]);
  375. $store_gcontact = count($q);
  376. }
  377. if (!$store_gcontact)
  378. return;
  379. // "3" means: We don't know this contact directly (Maybe a reshared item)
  380. $generation = 3;
  381. $network = "";
  382. $profile_url = $item["author-link"];
  383. // Is it a user from our server?
  384. $q = q("SELECT `id` FROM `contact` WHERE `self` AND `nurl` = '%s' LIMIT 1",
  385. dbesc(normalise_link($item["author-link"])));
  386. if (count($q)) {
  387. logger("Our user (generation 1): ".$item["author-link"], LOGGER_DEBUG);
  388. $generation = 1;
  389. $network = NETWORK_DFRN;
  390. } else { // Is it a contact from a user on our server?
  391. $q = q("SELECT `network`, `url` FROM `contact` WHERE `uid` != 0 AND `network` != ''
  392. AND (`nurl` = '%s' OR `alias` IN ('%s', '%s')) AND `network` != '%s' LIMIT 1",
  393. dbesc(normalise_link($item["author-link"])),
  394. dbesc(normalise_link($item["author-link"])),
  395. dbesc($item["author-link"]),
  396. dbesc(NETWORK_STATUSNET));
  397. if (count($q)) {
  398. $generation = 2;
  399. $network = $q[0]["network"];
  400. $profile_url = $q[0]["url"];
  401. logger("Known contact (generation 2): ".$profile_url, LOGGER_DEBUG);
  402. }
  403. }
  404. if ($generation == 3)
  405. logger("Unknown contact (generation 3): ".$item["author-link"], LOGGER_DEBUG);
  406. poco_check($profile_url, $item["author-name"], $network, $item["author-avatar"], "", "", "", "", "", $item["received"], $generation, $item["contact-id"], $item["uid"]);
  407. // Maybe its a body with a shared item? Then extract a global contact from it.
  408. poco_contact_from_body($item["body"], $item["received"], $item["contact-id"], $item["uid"]);
  409. }
  410. function count_common_friends($uid,$cid) {
  411. $r = q("SELECT count(*) as `total`
  412. FROM `glink` INNER JOIN `gcontact` on `glink`.`gcid` = `gcontact`.`id`
  413. where `glink`.`cid` = %d and `glink`.`uid` = %d
  414. and `gcontact`.`nurl` in (select nurl from contact where uid = %d and self = 0 and blocked = 0 and hidden = 0 and id != %d ) ",
  415. intval($cid),
  416. intval($uid),
  417. intval($uid),
  418. intval($cid)
  419. );
  420. // logger("count_common_friends: $uid $cid {$r[0]['total']}");
  421. if(count($r))
  422. return $r[0]['total'];
  423. return 0;
  424. }
  425. function common_friends($uid,$cid,$start = 0,$limit=9999,$shuffle = false) {
  426. if($shuffle)
  427. $sql_extra = " order by rand() ";
  428. else
  429. $sql_extra = " order by `gcontact`.`name` asc ";
  430. $r = q("SELECT `gcontact`.*
  431. FROM `glink` INNER JOIN `gcontact` on `glink`.`gcid` = `gcontact`.`id`
  432. where `glink`.`cid` = %d and `glink`.`uid` = %d
  433. and `gcontact`.`nurl` in (select nurl from contact where uid = %d and self = 0 and blocked = 0 and hidden = 0 and id != %d )
  434. $sql_extra limit %d, %d",
  435. intval($cid),
  436. intval($uid),
  437. intval($uid),
  438. intval($cid),
  439. intval($start),
  440. intval($limit)
  441. );
  442. return $r;
  443. }
  444. function count_common_friends_zcid($uid,$zcid) {
  445. $r = q("SELECT count(*) as `total`
  446. FROM `glink` INNER JOIN `gcontact` on `glink`.`gcid` = `gcontact`.`id`
  447. where `glink`.`zcid` = %d
  448. and `gcontact`.`nurl` in (select nurl from contact where uid = %d and self = 0 and blocked = 0 and hidden = 0 ) ",
  449. intval($zcid),
  450. intval($uid)
  451. );
  452. if(count($r))
  453. return $r[0]['total'];
  454. return 0;
  455. }
  456. function common_friends_zcid($uid,$zcid,$start = 0, $limit = 9999,$shuffle = false) {
  457. if($shuffle)
  458. $sql_extra = " order by rand() ";
  459. else
  460. $sql_extra = " order by `gcontact`.`name` asc ";
  461. $r = q("SELECT `gcontact`.*
  462. FROM `glink` INNER JOIN `gcontact` on `glink`.`gcid` = `gcontact`.`id`
  463. where `glink`.`zcid` = %d
  464. and `gcontact`.`nurl` in (select nurl from contact where uid = %d and self = 0 and blocked = 0 and hidden = 0 )
  465. $sql_extra limit %d, %d",
  466. intval($zcid),
  467. intval($uid),
  468. intval($start),
  469. intval($limit)
  470. );
  471. return $r;
  472. }
  473. function count_all_friends($uid,$cid) {
  474. $r = q("SELECT count(*) as `total`
  475. FROM `glink` INNER JOIN `gcontact` on `glink`.`gcid` = `gcontact`.`id`
  476. where `glink`.`cid` = %d and `glink`.`uid` = %d ",
  477. intval($cid),
  478. intval($uid)
  479. );
  480. if(count($r))
  481. return $r[0]['total'];
  482. return 0;
  483. }
  484. function all_friends($uid,$cid,$start = 0, $limit = 80) {
  485. $r = q("SELECT `gcontact`.*
  486. FROM `glink` INNER JOIN `gcontact` on `glink`.`gcid` = `gcontact`.`id`
  487. where `glink`.`cid` = %d and `glink`.`uid` = %d
  488. order by `gcontact`.`name` asc LIMIT %d, %d ",
  489. intval($cid),
  490. intval($uid),
  491. intval($start),
  492. intval($limit)
  493. );
  494. return $r;
  495. }
  496. function suggestion_query($uid, $start = 0, $limit = 80) {
  497. if(! $uid)
  498. return array();
  499. $network = array(NETWORK_DFRN);
  500. if (get_config('system','diaspora_enabled'))
  501. $network[] = NETWORK_DIASPORA;
  502. if (!get_config('system','ostatus_disabled'))
  503. $network[] = NETWORK_OSTATUS;
  504. $sql_network = implode("', '", $network);
  505. //$sql_network = "'".$sql_network."', ''";
  506. $sql_network = "'".$sql_network."'";
  507. $r = q("SELECT count(glink.gcid) as `total`, gcontact.* from gcontact
  508. INNER JOIN glink on glink.gcid = gcontact.id
  509. where uid = %d and not gcontact.nurl in ( select nurl from contact where uid = %d )
  510. and not gcontact.name in ( select name from contact where uid = %d )
  511. and not gcontact.id in ( select gcid from gcign where uid = %d )
  512. AND `gcontact`.`updated` != '0000-00-00 00:00:00'
  513. AND `gcontact`.`last_contact` >= `gcontact`.`last_failure`
  514. AND `gcontact`.`network` IN (%s)
  515. group by glink.gcid order by gcontact.updated desc,total desc limit %d, %d ",
  516. intval($uid),
  517. intval($uid),
  518. intval($uid),
  519. intval($uid),
  520. $sql_network,
  521. intval($start),
  522. intval($limit)
  523. );
  524. if(count($r) && count($r) >= ($limit -1))
  525. return $r;
  526. $r2 = q("SELECT gcontact.* from gcontact
  527. INNER JOIN glink on glink.gcid = gcontact.id
  528. where glink.uid = 0 and glink.cid = 0 and glink.zcid = 0 and not gcontact.nurl in ( select nurl from contact where uid = %d )
  529. and not gcontact.name in ( select name from contact where uid = %d )
  530. and not gcontact.id in ( select gcid from gcign where uid = %d )
  531. AND `gcontact`.`updated` != '0000-00-00 00:00:00'
  532. AND `gcontact`.`network` IN (%s)
  533. order by rand() limit %d, %d ",
  534. intval($uid),
  535. intval($uid),
  536. intval($uid),
  537. $sql_network,
  538. intval($start),
  539. intval($limit)
  540. );
  541. $list = array();
  542. foreach ($r2 AS $suggestion)
  543. $list[$suggestion["nurl"]] = $suggestion;
  544. foreach ($r AS $suggestion)
  545. $list[$suggestion["nurl"]] = $suggestion;
  546. return $list;
  547. }
  548. function update_suggestions() {
  549. $a = get_app();
  550. $done = array();
  551. poco_load(0,0,0,$a->get_baseurl() . '/poco');
  552. $done[] = $a->get_baseurl() . '/poco';
  553. if(strlen(get_config('system','directory_submit_url'))) {
  554. $x = fetch_url('http://dir.friendica.com/pubsites');
  555. if($x) {
  556. $j = json_decode($x);
  557. if($j->entries) {
  558. foreach($j->entries as $entry) {
  559. $url = $entry->url . '/poco';
  560. if(! in_array($url,$done))
  561. poco_load(0,0,0,$entry->url . '/poco');
  562. }
  563. }
  564. }
  565. }
  566. $r = q("select distinct(poco) as poco from contact where network = '%s'",
  567. dbesc(NETWORK_DFRN)
  568. );
  569. if(count($r)) {
  570. foreach($r as $rr) {
  571. $base = substr($rr['poco'],0,strrpos($rr['poco'],'/'));
  572. if(! in_array($base,$done))
  573. poco_load(0,0,0,$base);
  574. }
  575. }
  576. }