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) {
27 if((! $url) || (! $uid)) {
28 $r = q("select `poco`, `uid` from `contact` where `id` = %d limit 1",
43 logger('poco_load: ' . $url, LOGGER_DATA);
46 $s = fetch_url($url . ($uid) ? '/@me/@all?fields=displayName,urls,photos' : '?fields=displayName,urls,photos' );
48 if(($a->get_curl_code() > 299) || (! $s))
51 foreach($j->entry as $entry) {
57 $name = $entry->displayName;
59 foreach($entry->urls as $url) {
60 if($url->type == 'profile') {
61 $profile_url = $url->value;
65 foreach($entry->photos as $photo) {
66 if($photo->type == 'profile') {
67 $profile_photo = $photo->value;
72 if((! $name) || (! $profile_url) || (! $profile_photo))
75 $x = q("select * from `gcontact` where `nurl` = '%s' limit 1",
76 dbesc(normalise_link($profile_url))
82 if($x[0]['name'] != $name || $x[0]['photo'] != $profile_photo) {
83 q("update gcontact set `name` = '%s', `photo` = '%s' where
84 `nurl` = '%s' limit 1",
86 dbesc($profile_photo),
87 dbesc(normalise_link($profile_url))
92 q("insert into `gcontact` (`name`,`url`,`nurl`,`photo`)
93 values ( '%s', '%s', '%s', '%s') ",
96 dbesc(normalise_link($profile_url)),
99 $x = q("select * from `gcontact` where `nurl` = '%s' limit 1",
100 dbesc(normalise_link($profile_url))
108 $r = q("select * from glink where `cid` = %d and `uid` = %d and `gcid` = %d limit 1",
114 q("insert into glink ( `cid`,`uid`,`gcid`,`updated`) values (%d,%d,%d,'%s') ",
118 dbesc(datetime_convert())
122 q("update glink set updated = '%s' where `cid` = %d and `uid` = %d and `gcid` = %d limit 1",
123 dbesc(datetime_convert()),
132 q("delete from glink where `cid` = %d and `uid` = %d and `updated` < UTC_TIMESTAMP - INTERVAL 2 DAY",
140 function count_common_friends($uid,$cid) {
142 $r = q("SELECT count(*) as `total`
143 FROM `glink` left join `gcontact` on `glink`.`gcid` = `gcontact`.`id`
144 where `glink`.`cid` = %d and `glink`.`uid` = %d
145 and `gcontact`.`nurl` in (select nurl from contact where uid = %d and self = 0 and id != %d ) ",
153 return $r[0]['total'];
159 function common_friends($uid,$cid) {
161 $r = q("SELECT `gcontact`.*
162 FROM `glink` left join `gcontact` on `glink`.`gcid` = `gcontact`.`id`
163 where `glink`.`cid` = %d and `glink`.`uid` = %d
164 and `gcontact`.`nurl` in (select nurl from contact where uid = %d and self = 0 and id != %d )
165 order by `gcontact`.`name` asc ",
176 function count_all_friends($uid,$cid) {
178 $r = q("SELECT count(*) as `total`
179 FROM `glink` left join `gcontact` on `glink`.`gcid` = `gcontact`.`id`
180 where `glink`.`cid` = %d and `glink`.`uid` = %d ",
186 return $r[0]['total'];
192 function all_friends($uid,$cid,$start = 0, $limit = 80) {
194 $r = q("SELECT `gcontact`.*
195 FROM `glink` left join `gcontact` on `glink`.`gcid` = `gcontact`.`id`
196 where `glink`.`cid` = %d and `glink`.`uid` = %d
197 order by `gcontact`.`name` asc LIMIT %d, %d ",
209 function suggestion_query($uid, $start = 0, $limit = 40) {
214 $r = q("SELECT count(glink.gcid) as `total`, gcontact.* from gcontact
215 left join glink on glink.gcid = gcontact.id
216 where uid = %d and not gcontact.nurl in ( select nurl from contact where uid = %d)
217 and not gcontact.id in ( select gcid from gcign where uid = %d )
218 group by glink.gcid order by total desc limit %d, %d ",
229 $r = q("SELECT gcontact.* from gcontact
230 left join glink on glink.gcid = gcontact.id
231 where uid = 0 and cid = 0 and not gcontact.nurl in ( select nurl from contact where uid = %d)
232 and not gcontact.id in ( select gcid from gcign where uid = %d )
233 order by rand limit %d, %d ",
243 function update_suggestions() {
249 poco_load(0,0,$a->get_baseurl() . '/poco');
251 $done[] = $a->get_baseurl() . '/poco';
253 if(strlen(get_config('system','directory_submit_url'))) {
254 $x = fetch_url('http://dir.friendica.com/pubsites');
256 $j = json_decode($x);
258 foreach($j->entries as $entry) {
259 $url = $entry->url . '/poco';
260 if(! in_array($url,$done))
261 poco_load(0,0,$entry->url . '/poco');
267 $r = q("select distinct(poco) as poco from contact where network = '%s'",
273 $base = substr($rr['poco'],0,strrpos($rr['poco'],'/'));
274 if(! in_array($base,$done))
275 poco_load(0,0,$base);