3 require_once('include/datetime.php');
8 * Given a contact-id (minimum), load the PortableContacts friend list for that contact,
9 * and add the entries to the gcontact (Global Contact) table, or update existing entries
10 * if anything (name or photo) has changed.
11 * We use normalised urls for comparison which ignore http vs https and www.domain vs domain
13 * Once the global contact is stored add (if necessary) the contact linkage which associates
14 * the given uid, cid to the global contact entry. There can be many uid/cid combinations
15 * pointing to the same global contact id.
22 function poco_load($cid,$uid = 0,$zcid = 0,$url = null) {
26 if((! $url) || (! $uid)) {
27 $r = q("select `poco`, `uid` from `contact` where `id` = %d limit 1",
42 $url = $url . (($uid) ? '/@me/@all?fields=displayName,urls,photos,updated,network' : '?fields=displayName,urls,photos,updated,network') ;
44 logger('poco_load: ' . $url, LOGGER_DEBUG);
48 logger('poco_load: returns ' . $s, LOGGER_DATA);
50 logger('poco_load: return code: ' . $a->get_curl_code(), LOGGER_DEBUG);
52 if(($a->get_curl_code() > 299) || (! $s))
57 logger('poco_load: json: ' . print_r($j,true),LOGGER_DATA);
59 if(! isset($j->entry))
63 foreach($j->entry as $entry) {
71 $updated = '0000-00-00 00:00:00';
73 $name = $entry->displayName;
75 if(isset($entry->urls)) {
76 foreach($entry->urls as $url) {
77 if($url->type == 'profile') {
78 $profile_url = $url->value;
81 if($url->type == 'webfinger') {
82 $connect_url = str_replace('acct:' , '', $url->value);
87 if(isset($entry->photos)) {
88 foreach($entry->photos as $photo) {
89 if($photo->type == 'profile') {
90 $profile_photo = $photo->value;
96 if(isset($entry->updated))
97 $updated = date("Y-m-d H:i:s", strtotime($entry->updated));
99 if(isset($entry->network))
100 $network = $entry->network;
102 poco_check($profile_url, $name, $network, $profile_photo, $connect_url, $updated, $cid, $uid, $zcid);
105 logger("poco_load: loaded $total entries",LOGGER_DEBUG);
107 q("DELETE FROM `glink` WHERE `cid` = %d AND `uid` = %d AND `zcid` = %d AND `updated` < UTC_TIMESTAMP - INTERVAL 2 DAY",
115 function poco_check($profile_url, $name, $network, $profile_photo, $connect_url, $updated, $cid = 0, $uid = 0, $zcid = 0) {
118 if ($profile_url == "")
121 if (($network == "") OR ($name == "") OR ($profile_photo == "")) {
122 require_once("include/Scrape.php");
124 $data = probe_url($profile_url, PROBE_DIASPORA);
125 $network = $data["network"];
126 $name = $data["name"];
127 $profile_photo = $data["photo"];
130 if (!in_array($network, array(NETWORK_DFRN, NETWORK_OSTATUS, NETWORK_DIASPORA, NETWORK_STATUSNET)))
133 if (($name == "") OR ($profile_photo == ""))
136 logger("profile-check URL: ".$profile_url." name: ".$name." avatar: ".$profile_photo, LOGGER_DEBUG);
138 $x = q("SELECT * FROM `gcontact` WHERE `nurl` = '%s' LIMIT 1",
139 dbesc(normalise_link($profile_url))
145 if($x[0]['name'] != $name || $x[0]['photo'] != $profile_photo || $x[0]['updated'] < $updated) {
146 q("update gcontact set `name` = '%s', `network` = '%s', `photo` = '%s', `connect` = '%s', `url` = '%s', `updated` = '%s'
147 where `nurl` = '%s'",
150 dbesc($profile_photo),
154 dbesc(normalise_link($profile_url))
158 q("insert into `gcontact` (`name`,`network`, `url`,`nurl`,`photo`,`connect`, `updated`)
159 values ('%s', '%s', '%s', '%s', '%s','%s', '%s')",
163 dbesc(normalise_link($profile_url)),
164 dbesc($profile_photo),
168 $x = q("SELECT * FROM `gcontact` WHERE `nurl` = '%s' LIMIT 1",
169 dbesc(normalise_link($profile_url))
178 $r = q("SELECT * FROM `glink` WHERE `cid` = %d AND `uid` = %d AND `gcid` = %d AND `zcid` = %d LIMIT 1",
185 q("INSERT INTO `glink` (`cid`,`uid`,`gcid`,`zcid`, `updated`) VALUES (%d,%d,%d,%d, '%s') ",
190 dbesc(datetime_convert())
193 q("UPDATE `glink` SET `updated` = '%s' WHERE `cid` = %d AND `uid` = %d AND `gcid` = %d AND `zcid` = %d",
194 dbesc(datetime_convert()),
202 // For unknown reasons there are sometimes duplicates
203 q("DELETE FROM `gcontact` WHERE `nurl` = '%s' AND `id` != %d AND
204 NOT EXISTS (SELECT `gcid` FROM `glink` WHERE `gcid` = `gcontact`.`id`)",
205 dbesc(normalise_link($profile_url)),
212 function poco_contact_from_body($body, $created, $cid, $uid) {
213 preg_replace_callback("/\[share(.*?)\].*?\[\/share\]/ism",
214 function ($match) use ($created, $cid, $uid){
215 return(sub_poco_from_share($match, $created, $cid, $uid));
219 function sub_poco_from_share($share, $created, $cid, $uid) {
221 preg_match("/profile='(.*?)'/ism", $share[1], $matches);
222 if ($matches[1] != "")
223 $profile = $matches[1];
225 preg_match('/profile="(.*?)"/ism', $share[1], $matches);
226 if ($matches[1] != "")
227 $profile = $matches[1];
232 logger("prepare poco_check for profile ".$profile, LOGGER_DEBUG);
233 poco_check($profile, "", "", "", "", $created, $cid, $uid);
236 function count_common_friends($uid,$cid) {
238 $r = q("SELECT count(*) as `total`
239 FROM `glink` INNER JOIN `gcontact` on `glink`.`gcid` = `gcontact`.`id`
240 where `glink`.`cid` = %d and `glink`.`uid` = %d
241 and `gcontact`.`nurl` in (select nurl from contact where uid = %d and self = 0 and blocked = 0 and hidden = 0 and id != %d ) ",
248 // logger("count_common_friends: $uid $cid {$r[0]['total']}");
250 return $r[0]['total'];
256 function common_friends($uid,$cid,$start = 0,$limit=9999,$shuffle = false) {
259 $sql_extra = " order by rand() ";
261 $sql_extra = " order by `gcontact`.`name` asc ";
263 $r = q("SELECT `gcontact`.*
264 FROM `glink` INNER JOIN `gcontact` on `glink`.`gcid` = `gcontact`.`id`
265 where `glink`.`cid` = %d and `glink`.`uid` = %d
266 and `gcontact`.`nurl` in (select nurl from contact where uid = %d and self = 0 and blocked = 0 and hidden = 0 and id != %d )
267 $sql_extra limit %d, %d",
281 function count_common_friends_zcid($uid,$zcid) {
283 $r = q("SELECT count(*) as `total`
284 FROM `glink` INNER JOIN `gcontact` on `glink`.`gcid` = `gcontact`.`id`
285 where `glink`.`zcid` = %d
286 and `gcontact`.`nurl` in (select nurl from contact where uid = %d and self = 0 and blocked = 0 and hidden = 0 ) ",
292 return $r[0]['total'];
297 function common_friends_zcid($uid,$zcid,$start = 0, $limit = 9999,$shuffle = false) {
300 $sql_extra = " order by rand() ";
302 $sql_extra = " order by `gcontact`.`name` asc ";
304 $r = q("SELECT `gcontact`.*
305 FROM `glink` INNER JOIN `gcontact` on `glink`.`gcid` = `gcontact`.`id`
306 where `glink`.`zcid` = %d
307 and `gcontact`.`nurl` in (select nurl from contact where uid = %d and self = 0 and blocked = 0 and hidden = 0 )
308 $sql_extra limit %d, %d",
320 function count_all_friends($uid,$cid) {
322 $r = q("SELECT count(*) as `total`
323 FROM `glink` INNER JOIN `gcontact` on `glink`.`gcid` = `gcontact`.`id`
324 where `glink`.`cid` = %d and `glink`.`uid` = %d ",
330 return $r[0]['total'];
336 function all_friends($uid,$cid,$start = 0, $limit = 80) {
338 $r = q("SELECT `gcontact`.*
339 FROM `glink` INNER JOIN `gcontact` on `glink`.`gcid` = `gcontact`.`id`
340 where `glink`.`cid` = %d and `glink`.`uid` = %d
341 order by `gcontact`.`name` asc LIMIT %d, %d ",
353 function suggestion_query($uid, $start = 0, $limit = 80) {
358 $network = array(NETWORK_DFRN);
360 if (get_config('system','diaspora_enabled'))
361 $network[] = NETWORK_DIASPORA;
363 if (!get_config('system','ostatus_disabled'))
364 $network[] = NETWORK_OSTATUS;
366 $sql_network = implode("', '", $network);
367 //$sql_network = "'".$sql_network."', ''";
368 $sql_network = "'".$sql_network."'";
370 $r = q("SELECT count(glink.gcid) as `total`, gcontact.* from gcontact
371 INNER JOIN glink on glink.gcid = gcontact.id
372 where uid = %d and not gcontact.nurl in ( select nurl from contact where uid = %d )
373 and not gcontact.name in ( select name from contact where uid = %d )
374 and not gcontact.id in ( select gcid from gcign where uid = %d )
375 AND `gcontact`.`updated` != '0000-00-00 00:00:00'
376 AND `gcontact`.`network` IN (%s)
377 group by glink.gcid order by gcontact.updated desc,total desc limit %d, %d ",
387 if(count($r) && count($r) >= ($limit -1))
390 $r2 = q("SELECT gcontact.* from gcontact
391 INNER JOIN glink on glink.gcid = gcontact.id
392 where glink.uid = 0 and glink.cid = 0 and glink.zcid = 0 and not gcontact.nurl in ( select nurl from contact where uid = %d )
393 and not gcontact.name in ( select name from contact where uid = %d )
394 and not gcontact.id in ( select gcid from gcign where uid = %d )
395 AND `gcontact`.`updated` != '0000-00-00 00:00:00'
396 AND `gcontact`.`network` IN (%s)
397 order by rand() limit %d, %d ",
407 foreach ($r2 AS $suggestion)
408 $list[$suggestion["nurl"]] = $suggestion;
410 foreach ($r AS $suggestion)
411 $list[$suggestion["nurl"]] = $suggestion;
416 function update_suggestions() {
422 poco_load(0,0,0,$a->get_baseurl() . '/poco');
424 $done[] = $a->get_baseurl() . '/poco';
426 if(strlen(get_config('system','directory_submit_url'))) {
427 $x = fetch_url('http://dir.friendica.com/pubsites');
429 $j = json_decode($x);
431 foreach($j->entries as $entry) {
432 $url = $entry->url . '/poco';
433 if(! in_array($url,$done))
434 poco_load(0,0,0,$entry->url . '/poco');
440 $r = q("select distinct(poco) as poco from contact where network = '%s'",
446 $base = substr($rr['poco'],0,strrpos($rr['poco'],'/'));
447 if(! in_array($base,$done))
448 poco_load(0,0,0,$base);