3 require_once('include/datetime.php');
9 * Given a contact-id (minimum), load the PortableContacts friend list for that contact,
10 * and add the entries to the gcontact (Global Contact) table, or update existing entries
11 * if anything (name or photo) has changed.
12 * We use normalised urls for comparison which ignore http vs https and www.domain vs domain
14 * Once the global contact is stored add (if necessary) the contact linkage which associates
15 * the given uid, cid to the global contact entry. There can be many uid/cid combinations
16 * pointing to the same global contact id.
23 function poco_load($cid,$uid = 0,$url = null) {
25 if((! $url) || (! $uid)) {
26 $r = q("select `poco`, `uid` from `contact` where `id` = %d limit 1",
34 if((! $url) || (! $uid))
36 $s = fetch_url($url . '/@me/@all?fields=displayName,urls,photos');
38 if(($a->get_curl_code() > 299) || (! $s))
41 foreach($j->entry as $entry) {
47 $name = $entry->displayName;
49 foreach($entry->urls as $url) {
50 if($url->type == 'profile') {
51 $profile_url = $url->value;
55 foreach($entry->photos as $photo) {
56 if($photo->type == 'profile') {
57 $profile_photo = $photo->value;
62 if((! $name) || (! $profile_url) || (! $profile_photo))
65 $x = q("select * from `gcontact` where `nurl` = '%s' limit 1",
66 dbesc(normalise_link($profile_url))
71 if($x[0]['name'] != $name || $x[0]['photo'] != $profile_photo) {
72 q("update gcontact set `name` = '%s', `photo` = '%s' where
73 `nurl` = '%s' limit 1",
75 dbesc($profile_photo),
76 dbesc(normalise_link($profile_url))
81 q("insert into `gcontact` (`name`,`url`,`nurl`,`photo`)
82 values ( '%s', '%s', '%s', '%s') ",
85 dbesc(normalise_link($profile_url)),
88 $x = q("select * from `gcontact` where `nurl` = '%s' limit 1",
89 dbesc(normalise_link($profile_url))
97 $r = q("select * from glink where `cid` = %d and `uid` = %d and `gcid` = %d limit 1",
103 q("insert into glink ( `cid`,`uid`,`gcid`,`updated`) values (%d,%d,%d,'%s') ",
107 dbesc(datetime_convert())
111 q("update glink set updated = '%s' where `cid` = %d and `uid` = %d and `gcid` = %d limit 1",
112 dbesc(datetime_convert()),
121 q("delete from glink where `cid` = %d and `uid` = %d and `updated` < UTC_TIMESTAMP - INTERVAL 2 DAY",
129 function count_common_friends($uid,$cid) {
131 $r = q("SELECT count(*) as `total`
132 FROM `glink` left join `gcontact` on `glink`.`gcid` = `gcontact`.`id`
133 where `glink`.`cid` = %d and `glink`.`uid` = %d
134 and `gcontact`.`nurl` in (select nurl from contact where uid = %d and self = 0 and id != %d ) ",
142 return $r[0]['total'];
148 function common_friends($uid,$cid) {
150 $r = q("SELECT `gcontact`.*
151 FROM `glink` left join `gcontact` on `glink`.`gcid` = `gcontact`.`id`
152 where `glink`.`cid` = %d and `glink`.`uid` = %d
153 and `gcontact`.`nurl` in (select nurl from contact where uid = %d and self = 0 and id != %d ) ",
166 function suggestion_query($uid, $start = 0, $limit = 40) {
171 $r = q("SELECT count(glink.gcid) as `total`, gcontact.* from gcontact
172 left join glink on glink.gcid = gcontact.id
173 where uid = %d and not gcontact.nurl in ( select nurl from contact where uid = %d)
174 and not gcontact.id in ( select gcid from gcign where uid = %d )
175 group by glink.gcid order by total desc limit %d, %d ",