]> git.mxchange.org Git - friendica.git/blob - include/socgraph.php
Poco now returns the last update date as well. This date will be stored in the gconta...
[friendica.git] / include / socgraph.php
1 <?php
2
3 require_once('include/datetime.php');
4
5 /*
6  * poco_load
7  *
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
12  *
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. 
16  *
17  */
18  
19
20
21
22 function poco_load($cid,$uid = 0,$zcid = 0,$url = null) {
23         $a = get_app();
24
25         if($cid) {
26                 if((! $url) || (! $uid)) {
27                         $r = q("select `poco`, `uid` from `contact` where `id` = %d limit 1",
28                                 intval($cid)
29                         );
30                         if(count($r)) {
31                                 $url = $r[0]['poco'];
32                                 $uid = $r[0]['uid'];
33                         }
34                 }
35                 if(! $uid)
36                         return;
37         }
38
39         if(! $url)
40                 return;
41
42         $url = $url . (($uid) ? '/@me/@all?fields=displayName,urls,photos' : '?fields=displayName,urls,photos,updated') ;
43
44         logger('poco_load: ' . $url, LOGGER_DEBUG);
45
46         $s = fetch_url($url);
47
48         logger('poco_load: returns ' . $s, LOGGER_DATA);
49
50         logger('poco_load: return code: ' . $a->get_curl_code(), LOGGER_DEBUG);
51
52         if(($a->get_curl_code() > 299) || (! $s))
53                 return;
54
55         $j = json_decode($s);
56
57         logger('poco_load: json: ' . print_r($j,true),LOGGER_DATA);
58
59         if(! isset($j->entry))
60                 return;
61
62         $total = 0;
63         foreach($j->entry as $entry) {
64
65                 $total ++;
66                 $profile_url = '';
67                 $profile_photo = '';
68                 $connect_url = '';
69                 $name = '';
70                 $updated = '0000-00-00 00:00:00';
71
72                 $name = $entry->displayName;
73
74                 if(isset($entry->urls)) {
75                         foreach($entry->urls as $url) {
76                                 if($url->type == 'profile') {
77                                         $profile_url = $url->value;
78                                         continue;
79                                 }
80                                 if($url->type == 'webfinger') {
81                                         $connect_url = str_replace('acct:' , '', $url->value);
82                                         continue;
83                                 }
84                         }
85                 }
86                 if(isset($entry->photos)) {
87                         foreach($entry->photos as $photo) {
88                                 if($photo->type == 'profile') {
89                                         $profile_photo = $photo->value;
90                                         continue;
91                                 }
92                         }
93                 }
94
95                 if(isset($entry->updated))
96                         $updated = date("Y-m-d H:i:s", strtotime($entry->updated));
97
98                 if((! $name) || (! $profile_url) || (! $profile_photo))
99                         continue;
100
101                 $x = q("select * from `gcontact` where `nurl` = '%s' limit 1",
102                         dbesc(normalise_link($profile_url))
103                 );
104
105                 if(count($x)) {
106                         $gcid = $x[0]['id'];
107
108                         if($x[0]['name'] != $name || $x[0]['photo'] != $profile_photo || $x[0]['updated'] < $updated) {
109                                 q("update gcontact set `name` = '%s', `photo` = '%s', `connect` = '%s', `url` = '%s', `updated` = '%s'
110                                         where `nurl` = '%s'",
111                                         dbesc($name),
112                                         dbesc($profile_photo),
113                                         dbesc($connect_url),
114                                         dbesc($profile_url),
115                                         dbesc($updated),
116                                         dbesc(normalise_link($profile_url))
117                                 );
118                         }
119                 }
120                 else {
121                         q("insert into `gcontact` (`name`,`url`,`nurl`,`photo`,`connect`, `updated`)
122                                 values ( '%s', '%s', '%s', '%s','%s') ",
123                                 dbesc($name),
124                                 dbesc($profile_url),
125                                 dbesc(normalise_link($profile_url)),
126                                 dbesc($profile_photo),
127                                 dbesc($connect_url),
128                                 dbesc($updated)
129                         );
130                         $x = q("select * from `gcontact` where `nurl` = '%s' limit 1",
131                                 dbesc(normalise_link($profile_url))
132                         );
133                         if(count($x))
134                                 $gcid = $x[0]['id'];
135                 }
136                 if(! $gcid)
137                         return;
138
139                 $r = q("select * from glink where `cid` = %d and `uid` = %d and `gcid` = %d and `zcid` = %d limit 1",
140                         intval($cid),
141                         intval($uid),
142                         intval($gcid),
143                         intval($zcid)
144                 );
145                 if(! count($r)) {
146                         q("insert into glink ( `cid`,`uid`,`gcid`,`zcid`, `updated`) values (%d,%d,%d,%d, '%s') ",
147                                 intval($cid),
148                                 intval($uid),
149                                 intval($gcid),
150                                 intval($zcid),
151                                 dbesc(datetime_convert())
152                         );
153                 }
154                 else {
155                         q("update glink set updated = '%s' where `cid` = %d and `uid` = %d and `gcid` = %d and zcid = %d",
156                                 dbesc(datetime_convert()),
157                                 intval($cid),
158                                 intval($uid),
159                                 intval($gcid),
160                                 intval($zcid)
161                         );
162                 }
163
164         }
165         logger("poco_load: loaded $total entries",LOGGER_DEBUG);
166
167         q("delete from glink where `cid` = %d and `uid` = %d and `zcid` = %d and `updated` < UTC_TIMESTAMP - INTERVAL 2 DAY",
168                 intval($cid),
169                 intval($uid),
170                 intval($zcid)
171         );
172
173 }
174
175
176 function count_common_friends($uid,$cid) {
177
178         $r = q("SELECT count(*) as `total`
179                 FROM `glink` INNER JOIN `gcontact` on `glink`.`gcid` = `gcontact`.`id`
180                 where `glink`.`cid` = %d and `glink`.`uid` = %d
181                 and `gcontact`.`nurl` in (select nurl from contact where uid = %d and self = 0 and blocked = 0 and hidden = 0 and id != %d ) ",
182                 intval($cid),
183                 intval($uid),
184                 intval($uid),
185                 intval($cid)
186         );
187
188 //      logger("count_common_friends: $uid $cid {$r[0]['total']}"); 
189         if(count($r))
190                 return $r[0]['total'];
191         return 0;
192
193 }
194
195
196 function common_friends($uid,$cid,$start = 0,$limit=9999,$shuffle = false) {
197
198         if($shuffle)
199                 $sql_extra = " order by rand() ";
200         else
201                 $sql_extra = " order by `gcontact`.`name` asc "; 
202
203         $r = q("SELECT `gcontact`.* 
204                 FROM `glink` INNER JOIN `gcontact` on `glink`.`gcid` = `gcontact`.`id`
205                 where `glink`.`cid` = %d and `glink`.`uid` = %d
206                 and `gcontact`.`nurl` in (select nurl from contact where uid = %d and self = 0 and blocked = 0 and hidden = 0 and id != %d ) 
207                 $sql_extra limit %d, %d",
208                 intval($cid),
209                 intval($uid),
210                 intval($uid),
211                 intval($cid),
212                 intval($start),
213                 intval($limit)
214         );
215
216         return $r;
217
218 }
219
220
221 function count_common_friends_zcid($uid,$zcid) {
222
223         $r = q("SELECT count(*) as `total` 
224                 FROM `glink` INNER JOIN `gcontact` on `glink`.`gcid` = `gcontact`.`id`
225                 where `glink`.`zcid` = %d
226                 and `gcontact`.`nurl` in (select nurl from contact where uid = %d and self = 0 and blocked = 0 and hidden = 0 ) ",
227                 intval($zcid),
228                 intval($uid)
229         );
230
231         if(count($r))
232                 return $r[0]['total'];
233         return 0;
234
235 }
236
237 function common_friends_zcid($uid,$zcid,$start = 0, $limit = 9999,$shuffle = false) {
238
239         if($shuffle)
240                 $sql_extra = " order by rand() ";
241         else
242                 $sql_extra = " order by `gcontact`.`name` asc "; 
243
244         $r = q("SELECT `gcontact`.* 
245                 FROM `glink` INNER JOIN `gcontact` on `glink`.`gcid` = `gcontact`.`id`
246                 where `glink`.`zcid` = %d
247                 and `gcontact`.`nurl` in (select nurl from contact where uid = %d and self = 0 and blocked = 0 and hidden = 0 ) 
248                 $sql_extra limit %d, %d",
249                 intval($zcid),
250                 intval($uid),
251                 intval($start),
252                 intval($limit)
253         );
254
255         return $r;
256
257 }
258
259
260 function count_all_friends($uid,$cid) {
261
262         $r = q("SELECT count(*) as `total`
263                 FROM `glink` INNER JOIN `gcontact` on `glink`.`gcid` = `gcontact`.`id`
264                 where `glink`.`cid` = %d and `glink`.`uid` = %d ",
265                 intval($cid),
266                 intval($uid)
267         );
268
269         if(count($r))
270                 return $r[0]['total'];
271         return 0;
272
273 }
274
275
276 function all_friends($uid,$cid,$start = 0, $limit = 80) {
277
278         $r = q("SELECT `gcontact`.* 
279                 FROM `glink` INNER JOIN `gcontact` on `glink`.`gcid` = `gcontact`.`id`
280                 where `glink`.`cid` = %d and `glink`.`uid` = %d 
281                 order by `gcontact`.`name` asc LIMIT %d, %d ",
282                 intval($cid),
283                 intval($uid),
284                 intval($start),
285                 intval($limit)
286         );
287
288         return $r;
289 }
290
291
292
293 function suggestion_query($uid, $start = 0, $limit = 80) {
294
295         if(! $uid)
296                 return array();
297
298         $r = q("SELECT count(glink.gcid) as `total`, gcontact.* from gcontact 
299                 INNER JOIN glink on glink.gcid = gcontact.id 
300                 where uid = %d and not gcontact.nurl in ( select nurl from contact where uid = %d )
301                 and not gcontact.name in ( select name from contact where uid = %d )
302                 and not gcontact.id in ( select gcid from gcign where uid = %d )
303                 group by glink.gcid order by total desc limit %d, %d ",
304                 intval($uid),
305                 intval($uid),
306                 intval($uid),
307                 intval($uid),
308                 intval($start),
309                 intval($limit)
310         );
311
312         if(count($r) && count($r) >= ($limit -1))
313                 return $r;
314
315         $r2 = q("SELECT gcontact.* from gcontact 
316                 INNER JOIN glink on glink.gcid = gcontact.id 
317                 where glink.uid = 0 and glink.cid = 0 and glink.zcid = 0 and not gcontact.nurl in ( select nurl from contact where uid = %d )
318                 and not gcontact.name in ( select name from contact where uid = %d )
319                 and not gcontact.id in ( select gcid from gcign where uid = %d )
320                 order by rand() limit %d, %d ",
321                 intval($uid),
322                 intval($uid),
323                 intval($uid),
324                 intval($start),
325                 intval($limit)
326         );
327
328
329         return array_merge($r,$r2);
330
331 }
332
333 function update_suggestions() {
334
335         $a = get_app();
336
337         $done = array();
338
339         poco_load(0,0,0,$a->get_baseurl() . '/poco');
340
341         $done[] = $a->get_baseurl() . '/poco';
342
343         if(strlen(get_config('system','directory_submit_url'))) {
344                 $x = fetch_url('http://dir.friendica.com/pubsites');
345                 if($x) {
346                         $j = json_decode($x);
347                         if($j->entries) {
348                                 foreach($j->entries as $entry) {
349                                         $url = $entry->url . '/poco';
350                                         if(! in_array($url,$done))
351                                                 poco_load(0,0,0,$entry->url . '/poco');
352                                 }
353                         }
354                 }
355         }
356
357         $r = q("select distinct(poco) as poco from contact where network = '%s'",
358                 dbesc(NETWORK_DFRN)
359         );
360
361         if(count($r)) {
362                 foreach($r as $rr) {
363                         $base = substr($rr['poco'],0,strrpos($rr['poco'],'/'));
364                         if(! in_array($base,$done))
365                                 poco_load(0,0,0,$base);
366                 }
367         }
368 }