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,$url = null) {
24 if((! $url) || (! $uid)) {
25 $r = q("select `poco`, `uid` from `contact` where `id` = %d limit 1",
33 if((! $url) || (! $uid))
35 $s = fetch_url($url . '/@me/@all?fields=displayName,urls,photos');
37 if(($a->get_curl_code() > 299) || (! $s))
40 foreach($j->entry as $entry) {
46 $name = $entry->displayName;
48 foreach($entry->urls as $url) {
49 if($url->type == 'profile') {
50 $profile_url = $url->value;
54 foreach($entry->photos as $photo) {
55 if($photo->type == 'profile') {
56 $profile_photo = $photo->value;
61 if((! $name) || (! $profile_url) || (! $profile_photo))
64 $x = q("select * from `gcontact` where `nurl` = '%s' limit 1",
65 dbesc(normalise_link($profile_url))
70 if($x[0]['name'] != $name || $x[0]['photo'] != $profile_photo) {
71 q("update gcontact set `name` = '%s', `photo` = '%s' where
72 `nurl` = '%s' limit 1",
74 dbesc($profile_photo),
75 dbesc(normalise_link($profile_url))
80 q("insert into `gcontact` (`name`,`url`,`nurl`,`photo`)
81 values ( '%s', '%s', '%s', '%s') ",
84 dbesc(normalise_link($profile_url)),
87 $x = q("select * from `gcontact` where `nurl` = '%s' limit 1",
88 dbesc(normalise_link($profile_url))
96 $r = q("select * from glink where `cid` = %d and `uid` = %d and `gcid` = %d limit 1",
102 q("insert into glink ( `cid`,`uid`,`gcid`,`updated`) values (%d,%d,%d,'%s') ",
106 dbesc(datetime_convert())
110 q("update glink set updated = '%s' where `cid` = %d and `uid` = %d and `gcid` = %d limit 1",
111 dbesc(datetime_convert()),
120 q("delete from gcid where `cid` = %d and `uid` = %d and `updated` < UTC_TIMESTAMP - INTERVAL 2 DAY",