3 require_once('include/datetime.php');
4 require_once("include/Scrape.php");
8 - noscrape for updating contact fields and "last updated"
9 - use /poco/@global for discovering contacts from other servers
15 * Given a contact-id (minimum), load the PortableContacts friend list for that contact,
16 * and add the entries to the gcontact (Global Contact) table, or update existing entries
17 * if anything (name or photo) has changed.
18 * We use normalised urls for comparison which ignore http vs https and www.domain vs domain
20 * Once the global contact is stored add (if necessary) the contact linkage which associates
21 * the given uid, cid to the global contact entry. There can be many uid/cid combinations
22 * pointing to the same global contact id.
29 function poco_load($cid,$uid = 0,$zcid = 0,$url = null) {
31 require_once("include/html2bbcode.php");
36 if((! $url) || (! $uid)) {
37 $r = q("select `poco`, `uid` from `contact` where `id` = %d limit 1",
52 $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') ;
54 logger('poco_load: ' . $url, LOGGER_DEBUG);
58 logger('poco_load: returns ' . $s, LOGGER_DATA);
60 logger('poco_load: return code: ' . $a->get_curl_code(), LOGGER_DEBUG);
62 if(($a->get_curl_code() > 299) || (! $s))
67 logger('poco_load: json: ' . print_r($j,true),LOGGER_DATA);
69 if(! isset($j->entry))
73 foreach($j->entry as $entry) {
81 $updated = '0000-00-00 00:00:00';
88 $name = $entry->displayName;
90 if(isset($entry->urls)) {
91 foreach($entry->urls as $url) {
92 if($url->type == 'profile') {
93 $profile_url = $url->value;
96 if($url->type == 'webfinger') {
97 $connect_url = str_replace('acct:' , '', $url->value);
102 if(isset($entry->photos)) {
103 foreach($entry->photos as $photo) {
104 if($photo->type == 'profile') {
105 $profile_photo = $photo->value;
111 if(isset($entry->updated))
112 $updated = date("Y-m-d H:i:s", strtotime($entry->updated));
114 if(isset($entry->network))
115 $network = $entry->network;
117 if(isset($entry->currentLocation))
118 $location = $entry->currentLocation;
120 if(isset($entry->aboutMe))
121 $about = html2bbcode($entry->aboutMe);
123 if(isset($entry->gender))
124 $gender = $entry->gender;
126 if(isset($entry->generation) AND ($entry->generation > 0))
127 $generation = ++$entry->generation;
129 if(isset($entry->tags))
130 foreach($entry->tags as $tag)
131 $keywords = implode(", ", $tag);
133 // If you query a Friendica server for its profiles, the network has to be Friendica
134 // To-Do: It could also be a Redmatrix server
136 // $network = NETWORK_DFRN;
138 poco_check($profile_url, $name, $network, $profile_photo, $about, $location, $gender, $keywords, $connect_url, $updated, $generation, $cid, $uid, $zcid);
140 // Update the Friendica contacts. Diaspora is doing it via a message. (See include/diaspora.php)
141 if (($location != "") OR ($about != "") OR ($keywords != "") OR ($gender != ""))
142 q("UPDATE `contact` SET `location` = '%s', `about` = '%s', `keywords` = '%s', `gender` = '%s'
143 WHERE `nurl` = '%s' AND NOT `self` AND `network` = '%s'",
148 dbesc(normalise_link($profile_url)),
149 dbesc(NETWORK_DFRN));
151 logger("poco_load: loaded $total entries",LOGGER_DEBUG);
153 q("DELETE FROM `glink` WHERE `cid` = %d AND `uid` = %d AND `zcid` = %d AND `updated` < UTC_TIMESTAMP - INTERVAL 2 DAY",
161 function poco_check($profile_url, $name, $network, $profile_photo, $about, $location, $gender, $keywords, $connect_url, $updated, $generation, $cid = 0, $uid = 0, $zcid = 0) {
167 // 1: Profiles on this server
168 // 2: Contacts of profiles on this server
169 // 3: Contacts of contacts of profiles on this server
174 if ($profile_url == "")
177 $orig_updated = $updated;
179 // Don't store the statusnet connector as network
180 // We can't simply set this to NETWORK_OSTATUS since the connector could have fetched posts from friendica as well
181 if ($network == NETWORK_STATUSNET)
184 // The global contacts should contain the original picture, not the cached one
185 if (($generation != 1) AND stristr(normalise_link($profile_photo), normalise_link($a->get_baseurl()."/photo/")))
188 $r = q("SELECT `network` FROM `contact` WHERE `nurl` = '%s' AND `network` != '' AND `network` != '%s' LIMIT 1",
189 dbesc(normalise_link($profile_url)), dbesc(NETWORK_STATUSNET)
192 $network = $r[0]["network"];
194 if (($network == "") OR ($network == NETWORK_OSTATUS)) {
195 $r = q("SELECT `network`, `url` FROM `contact` WHERE `alias` IN ('%s', '%s') AND `network` != '' AND `network` != '%s' LIMIT 1",
196 dbesc($profile_url), dbesc(normalise_link($profile_url)), dbesc(NETWORK_STATUSNET)
199 $network = $r[0]["network"];
200 $profile_url = $r[0]["url"];
204 $x = q("SELECT * FROM `gcontact` WHERE `nurl` = '%s' LIMIT 1",
205 dbesc(normalise_link($profile_url))
209 if (($network == "") AND ($x[0]["network"] != NETWORK_STATUSNET))
210 $network = $x[0]["network"];
212 if ($updated == "0000-00-00 00:00:00")
213 $updated = $x[0]["updated"];
215 $last_contact = $x[0]["last_contact"];
216 $last_failure = $x[0]["last_failure"];
217 $server_url = $x[0]["server_url"];
219 $last_contact = "0000-00-00 00:00:00";
220 $last_failure = "0000-00-00 00:00:00";
224 if (($network == "") OR ($name == "") OR ($profile_photo == "") OR ($server_url == "")) {
225 $data = probe_url($profile_url);
226 $network = $data["network"];
227 $name = $data["name"];
228 $profile_url = $data["url"];
229 $profile_photo = $data["photo"];
230 $server_url = $data["baseurl"];
233 if (count($x) AND ($x[0]["network"] == "") AND ($network != "")) {
234 q("UPDATE `gcontact` SET `network` = '%s' WHERE `nurl` = '%s'",
236 dbesc(normalise_link($profile_url))
240 if (($name == "") OR ($profile_photo == ""))
243 if (!in_array($network, array(NETWORK_DFRN, NETWORK_OSTATUS, NETWORK_DIASPORA)))
246 logger("profile-check generation: ".$generation." Network: ".$network." URL: ".$profile_url." name: ".$name." avatar: ".$profile_photo, LOGGER_DEBUG);
248 // Only fetch last update manually if it wasn't provided and enabled in the system
249 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)) {
250 $last_updated = poco_last_updated($profile_url);
252 $updated = $last_updated;
253 $last_contact = datetime_convert();
254 logger("Last updated for profile ".$profile_url.": ".$updated, LOGGER_DEBUG);
257 q("UPDATE `gcontact` SET `last_contact` = '%s' WHERE `nurl` = '%s'", dbesc($last_contact), dbesc(normalise_link($profile_url)));
259 $last_failure = datetime_convert();
262 q("UPDATE `gcontact` SET `last_failure` = '%s' WHERE `nurl` = '%s'", dbesc($last_failure), dbesc(normalise_link($profile_url)));
266 poco_check_server($server_url, $network);
268 // Test - remove before flight
269 if ($last_contact > $last_failure)
270 q("UPDATE `gserver` SET `last_contact` = '%s' WHERE `nurl` = '%s'", dbesc($last_contact), dbesc(normalise_link($server_url)));
272 q("UPDATE `gserver` SET `last_failure` = '%s' WHERE `nurl` = '%s'", dbesc($last_failure), dbesc(normalise_link($server_url)));
277 if (($location == "") AND ($x[0]['location'] != ""))
278 $location = $x[0]['location'];
280 if (($about == "") AND ($x[0]['about'] != ""))
281 $about = $x[0]['about'];
283 if (($gender == "") AND ($x[0]['gender'] != ""))
284 $gender = $x[0]['gender'];
286 if (($keywords == "") AND ($x[0]['keywords'] != ""))
287 $keywords = $x[0]['keywords'];
289 if (($generation == 0) AND ($x[0]['generation'] > 0))
290 $generation = $x[0]['generation'];
292 if($x[0]['name'] != $name || $x[0]['photo'] != $profile_photo || $x[0]['updated'] < $updated) {
293 q("UPDATE `gcontact` SET `name` = '%s', `network` = '%s', `photo` = '%s', `connect` = '%s', `url` = '%s', `server_url` = '%s',
294 `updated` = '%s', `location` = '%s', `about` = '%s', `keywords` = '%s', `gender` = '%s', `generation` = %d
295 WHERE (`generation` >= %d OR `generation` = 0) AND `nurl` = '%s'",
298 dbesc($profile_photo),
309 dbesc(normalise_link($profile_url))
313 q("INSERT INTO `gcontact` (`name`,`network`, `url`,`nurl`,`photo`,`connect`, `server_url`, `updated`, `last_contact`, `last_failure`, `location`, `about`, `keywords`, `gender`, `generation`)
314 VALUES ('%s', '%s', '%s', '%s', '%s','%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', %d)",
318 dbesc(normalise_link($profile_url)),
319 dbesc($profile_photo),
323 dbesc($last_contact),
324 dbesc($last_failure),
331 $x = q("SELECT * FROM `gcontact` WHERE `nurl` = '%s' LIMIT 1",
332 dbesc(normalise_link($profile_url))
341 $r = q("SELECT * FROM `glink` WHERE `cid` = %d AND `uid` = %d AND `gcid` = %d AND `zcid` = %d LIMIT 1",
348 q("INSERT INTO `glink` (`cid`,`uid`,`gcid`,`zcid`, `updated`) VALUES (%d,%d,%d,%d, '%s') ",
353 dbesc(datetime_convert())
356 q("UPDATE `glink` SET `updated` = '%s' WHERE `cid` = %d AND `uid` = %d AND `gcid` = %d AND `zcid` = %d",
357 dbesc(datetime_convert()),
365 // For unknown reasons there are sometimes duplicates
366 q("DELETE FROM `gcontact` WHERE `nurl` = '%s' AND `id` != %d AND
367 NOT EXISTS (SELECT `gcid` FROM `glink` WHERE `gcid` = `gcontact`.`id`)",
368 dbesc(normalise_link($profile_url)),
375 function poco_last_updated($profile) {
376 $data = probe_url($profile);
378 if (($data["poll"] == "") OR ($data["network"] == NETWORK_FEED))
381 // To-Do: Use noscrape
383 $feedret = z_fetch_url($data["poll"]);
385 if (!$feedret["success"])
388 $doc = new DOMDocument();
389 @$doc->loadXML($feedret["body"]);
391 $xpath = new DomXPath($doc);
392 $xpath->registerNamespace('atom', "http://www.w3.org/2005/Atom");
394 $entries = $xpath->query('/atom:feed/atom:entry');
398 foreach ($entries AS $entry) {
399 $published = $xpath->query('atom:published/text()', $entry)->item(0)->nodeValue;
400 $updated = $xpath->query('atom:updated/text()', $entry)->item(0)->nodeValue;
402 if ($last_updated < $published)
403 $last_updated = $published;
405 if ($last_updated < $updated)
406 $last_updated = $updated;
409 // Maybe there aren't any entries. Then check if it is a valid feed
410 if ($last_updated == "")
411 if ($xpath->query('/atom:feed')->length > 0)
412 $last_updated = "0000-00-00 00:00:00";
414 return($last_updated);
417 function poco_do_update($updated, $last_contact, $last_failure) {
418 $now = strtotime(datetime_convert());
420 if ($updated > $last_contact)
421 $contact_time = strtotime($updated);
423 $contact_time = strtotime($last_contact);
425 $failure_time = strtotime($last_failure);
427 // If the last contact was less than 24 hours then don't update
428 if (($now - $contact_time) < (60 * 60 * 24))
431 // If the last failure was less than 24 hours then don't update
432 if (($now - $failure_time) < (60 * 60 * 24))
435 // If the last contact was less than a week ago and the last failure is older than a week then don't update
436 if ((($now - $contact_time) < (60 * 60 * 24 * 7)) AND ($contact_time > $failure_time))
439 // If the last contact time was more than a week ago, then only try once a week
440 if (($now - $contact_time) > (60 * 60 * 24 * 7) AND ($now - $failure_time) < (60 * 60 * 24 * 7))
443 // If the last contact time was more than a month ago, then only try once a month
444 if (($now - $contact_time) > (60 * 60 * 24 * 30) AND ($now - $failure_time) < (60 * 60 * 24 * 30))
450 function poco_to_boolean($val) {
451 if (($val == "true") OR ($val == 1))
453 if (($val == "false") OR ($val == 0))
459 function poco_check_server($server_url, $network = "") {
461 if ($server_url == "")
464 $servers = q("SELECT * FROM `gserver` WHERE `nurl` = '%s'", dbesc(normalise_link($server_url)));
466 $poco = $servers[0]["poco"];
467 $noscrape = $servers[0]["noscrape"];
470 $network = $servers[0]["network"];
472 $last_contact = $servers[0]["last_contact"];
473 $last_failure = $servers[0]["last_failure"];
474 $version = $servers[0]["version"];
475 $platform = $servers[0]["platform"];
476 $site_name = $servers[0]["site_name"];
477 $info = $servers[0]["info"];
478 $register_policy = $servers[0]["register_policy"];
480 // Only check the server once a week
481 if (strtotime(datetime_convert()) < (strtotime($last_contact) + (60 * 60 * 24 * 7)))
484 if (strtotime(datetime_convert()) < (strtotime($last_failure) + (60 * 60 * 24 * 7)))
493 $register_policy = -1;
495 $last_contact = "0000-00-00 00:00:00";
496 $last_failure = "0000-00-00 00:00:00";
500 $orig_last_failure = $last_failure;
502 // Check if the page is accessible via SSL.
503 $server_url = str_replace("http://", "https://", $server_url);
504 $serverret = z_fetch_url($server_url."/.well-known/host-meta");
506 // Maybe the page is unencrypted only?
507 $xmlobj = @simplexml_load_string($serverret["body"],'SimpleXMLElement',0, "http://docs.oasis-open.org/ns/xri/xrd-1.0");
508 if (!$serverret["success"] OR ($serverret["body"] == "") OR (@sizeof($xmlobj) == 0) OR !is_object($xmlobj)) {
509 $server_url = str_replace("https://", "http://", $server_url);
510 $serverret = z_fetch_url($server_url."/.well-known/host-meta");
512 $xmlobj = @simplexml_load_string($serverret["body"],'SimpleXMLElement',0, "http://docs.oasis-open.org/ns/xri/xrd-1.0");
515 if (!$serverret["success"] OR ($serverret["body"] == "") OR (sizeof($xmlobj) == 0) OR !is_object($xmlobj)) {
516 $last_failure = datetime_convert();
518 } elseif ($network == NETWORK_DIASPORA)
519 $last_contact = datetime_convert();
522 // Test for Statusnet
523 // Will also return data for Friendica and GNU Social - but it will be overwritten later
524 // The "not implemented" is a special treatment for really, really old Friendica versions
525 $serverret = z_fetch_url($server_url."/api/statusnet/version.json");
526 if ($serverret["success"] AND ($serverret["body"] != '{"error":"not implemented"}') AND ($serverret["body"] != '') AND (strlen($serverret["body"]) < 250)) {
527 $platform = "StatusNet";
528 $version = trim($serverret["body"], '"');
529 $network = NETWORK_OSTATUS;
532 // Test for GNU Social
533 $serverret = z_fetch_url($server_url."/api/gnusocial/version.json");
534 if ($serverret["success"] AND ($serverret["body"] != '{"error":"not implemented"}') AND ($serverret["body"] != '') AND (strlen($serverret["body"]) < 250)) {
535 $platform = "GNU Social";
536 $version = trim($serverret["body"], '"');
537 $network = NETWORK_OSTATUS;
540 $serverret = z_fetch_url($server_url."/api/statusnet/config.json");
541 if ($serverret["success"]) {
542 $data = json_decode($serverret["body"]);
544 if (isset($data->site->server)) {
545 $last_contact = datetime_convert();
547 if (isset($data->site->redmatrix)) {
548 if (isset($data->site->redmatrix->PLATFORM_NAME))
549 $platform = $data->site->redmatrix->PLATFORM_NAME;
550 elseif (isset($data->site->redmatrix->RED_PLATFORM))
551 $platform = $data->site->redmatrix->RED_PLATFORM;
553 $version = $data->site->redmatrix->RED_VERSION;
554 $network = NETWORK_DIASPORA;
556 if (isset($data->site->friendica)) {
557 $platform = $data->site->friendica->FRIENDICA_PLATFORM;
558 $version = $data->site->friendica->FRIENDICA_VERSION;
559 $network = NETWORK_DFRN;
562 $site_name = $data->site->name;
564 $data->site->closed = poco_to_boolean($data->site->closed);
565 $data->site->private = poco_to_boolean($data->site->private);
566 $data->site->inviteonly = poco_to_boolean($data->site->inviteonly);
568 if (!$data->site->closed AND !$data->site->private and $data->site->inviteonly)
569 $register_policy = REGISTER_APPROVE;
570 elseif (!$data->site->closed AND !$data->site->private)
571 $register_policy = REGISTER_OPEN;
573 $register_policy = REGISTER_CLOSED;
578 // Query statistics.json. Optional package for Diaspora, Friendica and Redmatrix
580 $serverret = z_fetch_url($server_url."/statistics.json");
581 if ($serverret["success"]) {
582 $data = json_decode($serverret["body"]);
584 $version = $data->version;
586 $site_name = $data->name;
588 if (isset($data->network) AND ($platform == ""))
589 $platform = $data->network;
591 if ($data->registrations_open)
592 $register_policy = REGISTER_OPEN;
594 $register_policy = REGISTER_CLOSED;
596 if (isset($data->version))
597 $last_contact = datetime_convert();
601 // Check for noscrape
602 // Friendica servers could be detected as OStatus servers
603 if (!$failure AND in_array($network, array(NETWORK_DFRN, NETWORK_OSTATUS))) {
604 $serverret = z_fetch_url($server_url."/friendica/json");
606 if ($serverret["success"]) {
607 $data = json_decode($serverret["body"]);
609 if (isset($data->version)) {
610 $last_contact = datetime_convert();
611 $network = NETWORK_DFRN;
613 $noscrape = $data->no_scrape_url;
614 $version = $data->version;
615 $site_name = $data->site_name;
617 $register_policy_str = $data->register_policy;
618 $platform = $data->platform;
620 switch ($register_policy_str) {
621 case "REGISTER_CLOSED":
622 $register_policy = REGISTER_CLOSED;
624 case "REGISTER_APPROVE":
625 $register_policy = REGISTER_APPROVE;
627 case "REGISTER_OPEN":
628 $register_policy = REGISTER_OPEN;
637 $serverret = z_fetch_url($server_url."/poco");
638 if ($serverret["success"]) {
639 $data = json_decode($serverret["body"]);
640 if (isset($data->totalResults)) {
641 $poco = $server_url."/poco";
642 $last_contact = datetime_convert();
648 q("UPDATE `gserver` SET `url` = '%s', `version` = '%s', `site_name` = '%s', `info` = '%s', `register_policy` = %d, `poco` = '%s', `noscrape` = '%s',
649 `network` = '%s', `platform` = '%s', `last_contact` = '%s', `last_failure` = '%s' WHERE `nurl` = '%s'",
654 intval($register_policy),
659 dbesc($last_contact),
660 dbesc($last_failure),
661 dbesc(normalise_link($server_url))
664 q("INSERT INTO `gserver` (`url`, `nurl`, `version`, `site_name`, `info`, `register_policy`, `poco`, `noscrape`, `network`, `platform`, `last_contact`)
665 VALUES ('%s', '%s', '%s', '%s', '%s', %d, '%s', '%s', '%s', '%s', '%s')",
667 dbesc(normalise_link($server_url)),
671 intval($register_policy),
676 dbesc(datetime_convert())
680 function poco_contact_from_body($body, $created, $cid, $uid) {
681 preg_replace_callback("/\[share(.*?)\].*?\[\/share\]/ism",
682 function ($match) use ($created, $cid, $uid){
683 return(sub_poco_from_share($match, $created, $cid, $uid));
687 function sub_poco_from_share($share, $created, $cid, $uid) {
689 preg_match("/profile='(.*?)'/ism", $share[1], $matches);
690 if ($matches[1] != "")
691 $profile = $matches[1];
693 preg_match('/profile="(.*?)"/ism', $share[1], $matches);
694 if ($matches[1] != "")
695 $profile = $matches[1];
700 logger("prepare poco_check for profile ".$profile, LOGGER_DEBUG);
701 poco_check($profile, "", "", "", "", "", "", "", "", $created, 3, $cid, $uid);
704 function poco_store($item) {
707 if ($item['private'])
710 // Or is it from a network where we don't store the global contacts?
711 if (!in_array($item["network"], array(NETWORK_DFRN, NETWORK_DIASPORA, NETWORK_OSTATUS, NETWORK_STATUSNET, "")))
714 // Is it a global copy?
715 $store_gcontact = ($item["uid"] == 0);
717 // Is it a comment on a global copy?
718 if (!$store_gcontact AND ($item["uri"] != $item["parent-uri"])) {
719 $q = q("SELECT `id` FROM `item` WHERE `uri`='%s' AND `uid` = 0", $item["parent-uri"]);
720 $store_gcontact = count($q);
723 if (!$store_gcontact)
726 // "3" means: We don't know this contact directly (Maybe a reshared item)
729 $profile_url = $item["author-link"];
731 // Is it a user from our server?
732 $q = q("SELECT `id` FROM `contact` WHERE `self` AND `nurl` = '%s' LIMIT 1",
733 dbesc(normalise_link($item["author-link"])));
735 logger("Our user (generation 1): ".$item["author-link"], LOGGER_DEBUG);
737 $network = NETWORK_DFRN;
738 } else { // Is it a contact from a user on our server?
739 $q = q("SELECT `network`, `url` FROM `contact` WHERE `uid` != 0 AND `network` != ''
740 AND (`nurl` = '%s' OR `alias` IN ('%s', '%s')) AND `network` != '%s' LIMIT 1",
741 dbesc(normalise_link($item["author-link"])),
742 dbesc(normalise_link($item["author-link"])),
743 dbesc($item["author-link"]),
744 dbesc(NETWORK_STATUSNET));
747 $network = $q[0]["network"];
748 $profile_url = $q[0]["url"];
749 logger("Known contact (generation 2): ".$profile_url, LOGGER_DEBUG);
753 if ($generation == 3)
754 logger("Unknown contact (generation 3): ".$item["author-link"], LOGGER_DEBUG);
756 poco_check($profile_url, $item["author-name"], $network, $item["author-avatar"], "", "", "", "", "", $item["received"], $generation, $item["contact-id"], $item["uid"]);
758 // Maybe its a body with a shared item? Then extract a global contact from it.
759 poco_contact_from_body($item["body"], $item["received"], $item["contact-id"], $item["uid"]);
762 function count_common_friends($uid,$cid) {
764 $r = q("SELECT count(*) as `total`
765 FROM `glink` INNER JOIN `gcontact` on `glink`.`gcid` = `gcontact`.`id`
766 where `glink`.`cid` = %d and `glink`.`uid` = %d
767 and `gcontact`.`nurl` in (select nurl from contact where uid = %d and self = 0 and blocked = 0 and hidden = 0 and id != %d ) ",
774 // logger("count_common_friends: $uid $cid {$r[0]['total']}");
776 return $r[0]['total'];
782 function common_friends($uid,$cid,$start = 0,$limit=9999,$shuffle = false) {
785 $sql_extra = " order by rand() ";
787 $sql_extra = " order by `gcontact`.`name` asc ";
789 $r = q("SELECT `gcontact`.*
790 FROM `glink` INNER JOIN `gcontact` on `glink`.`gcid` = `gcontact`.`id`
791 where `glink`.`cid` = %d and `glink`.`uid` = %d
792 and `gcontact`.`nurl` in (select nurl from contact where uid = %d and self = 0 and blocked = 0 and hidden = 0 and id != %d )
793 $sql_extra limit %d, %d",
807 function count_common_friends_zcid($uid,$zcid) {
809 $r = q("SELECT count(*) as `total`
810 FROM `glink` INNER JOIN `gcontact` on `glink`.`gcid` = `gcontact`.`id`
811 where `glink`.`zcid` = %d
812 and `gcontact`.`nurl` in (select nurl from contact where uid = %d and self = 0 and blocked = 0 and hidden = 0 ) ",
818 return $r[0]['total'];
823 function common_friends_zcid($uid,$zcid,$start = 0, $limit = 9999,$shuffle = false) {
826 $sql_extra = " order by rand() ";
828 $sql_extra = " order by `gcontact`.`name` asc ";
830 $r = q("SELECT `gcontact`.*
831 FROM `glink` INNER JOIN `gcontact` on `glink`.`gcid` = `gcontact`.`id`
832 where `glink`.`zcid` = %d
833 and `gcontact`.`nurl` in (select nurl from contact where uid = %d and self = 0 and blocked = 0 and hidden = 0 )
834 $sql_extra limit %d, %d",
846 function count_all_friends($uid,$cid) {
848 $r = q("SELECT count(*) as `total`
849 FROM `glink` INNER JOIN `gcontact` on `glink`.`gcid` = `gcontact`.`id`
850 where `glink`.`cid` = %d and `glink`.`uid` = %d ",
856 return $r[0]['total'];
862 function all_friends($uid,$cid,$start = 0, $limit = 80) {
864 $r = q("SELECT `gcontact`.*
865 FROM `glink` INNER JOIN `gcontact` on `glink`.`gcid` = `gcontact`.`id`
866 where `glink`.`cid` = %d and `glink`.`uid` = %d
867 order by `gcontact`.`name` asc LIMIT %d, %d ",
879 function suggestion_query($uid, $start = 0, $limit = 80) {
884 $network = array(NETWORK_DFRN);
886 if (get_config('system','diaspora_enabled'))
887 $network[] = NETWORK_DIASPORA;
889 if (!get_config('system','ostatus_disabled'))
890 $network[] = NETWORK_OSTATUS;
892 $sql_network = implode("', '", $network);
893 //$sql_network = "'".$sql_network."', ''";
894 $sql_network = "'".$sql_network."'";
896 $r = q("SELECT count(glink.gcid) as `total`, gcontact.* from gcontact
897 INNER JOIN glink on glink.gcid = gcontact.id
898 where uid = %d and not gcontact.nurl in ( select nurl from contact where uid = %d )
899 and not gcontact.name in ( select name from contact where uid = %d )
900 and not gcontact.id in ( select gcid from gcign where uid = %d )
901 AND `gcontact`.`updated` != '0000-00-00 00:00:00'
902 AND `gcontact`.`last_contact` >= `gcontact`.`last_failure`
903 AND `gcontact`.`network` IN (%s)
904 group by glink.gcid order by gcontact.updated desc,total desc limit %d, %d ",
914 if(count($r) && count($r) >= ($limit -1))
917 $r2 = q("SELECT gcontact.* from gcontact
918 INNER JOIN glink on glink.gcid = gcontact.id
919 where glink.uid = 0 and glink.cid = 0 and glink.zcid = 0 and not gcontact.nurl in ( select nurl from contact where uid = %d )
920 and not gcontact.name in ( select name from contact where uid = %d )
921 and not gcontact.id in ( select gcid from gcign where uid = %d )
922 AND `gcontact`.`updated` != '0000-00-00 00:00:00'
923 AND `gcontact`.`network` IN (%s)
924 order by rand() limit %d, %d ",
934 foreach ($r2 AS $suggestion)
935 $list[$suggestion["nurl"]] = $suggestion;
937 foreach ($r AS $suggestion)
938 $list[$suggestion["nurl"]] = $suggestion;
943 function update_suggestions() {
949 poco_load(0,0,0,$a->get_baseurl() . '/poco');
951 $done[] = $a->get_baseurl() . '/poco';
953 if(strlen(get_config('system','directory_submit_url'))) {
954 $x = fetch_url('http://dir.friendica.com/pubsites');
956 $j = json_decode($x);
958 foreach($j->entries as $entry) {
959 $url = $entry->url . '/poco';
960 if(! in_array($url,$done))
961 poco_load(0,0,0,$entry->url . '/poco');
967 $r = q("select distinct(poco) as poco from contact where network = '%s'",
973 $base = substr($rr['poco'],0,strrpos($rr['poco'],'/'));
974 if(! in_array($base,$done))
975 poco_load(0,0,0,$base);