]> git.mxchange.org Git - friendica.git/blob - include/socgraph.php
b9e890427ce1241d993e3ada0fc59567e08bd4b9
[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                 } else {
120                         q("insert into `gcontact` (`name`,`url`,`nurl`,`photo`,`connect`, `updated`)
121                                 values ( '%s', '%s', '%s', '%s','%s') ",
122                                 dbesc($name),
123                                 dbesc($profile_url),
124                                 dbesc(normalise_link($profile_url)),
125                                 dbesc($profile_photo),
126                                 dbesc($connect_url),
127                                 dbesc($updated)
128                         );
129                         $x = q("SELECT * FROM `gcontact` WHERE `nurl` = '%s' LIMIT 1",
130                                 dbesc(normalise_link($profile_url))
131                         );
132                         if(count($x))
133                                 $gcid = $x[0]['id'];
134                 }
135                 if(! $gcid)
136                         return;
137
138                 $r = q("SELECT * FROM `glink` WHERE `cid` = %d AND `uid` = %d AND `gcid` = %d AND `zcid` = %d LIMIT 1",
139                         intval($cid),
140                         intval($uid),
141                         intval($gcid),
142                         intval($zcid)
143                 );
144                 if(! count($r)) {
145                         q("INSERT INTO `glink` (`cid`,`uid`,`gcid`,`zcid`, `updated`) VALUES (%d,%d,%d,%d, '%s') ",
146                                 intval($cid),
147                                 intval($uid),
148                                 intval($gcid),
149                                 intval($zcid),
150                                 dbesc(datetime_convert())
151                         );
152                 }
153                 else {
154                         q("UPDATE `glink` SET `updated` = '%s' WHERE `cid` = %d AND `uid` = %d AND `gcid` = %d AND `zcid` = %d",
155                                 dbesc(datetime_convert()),
156                                 intval($cid),
157                                 intval($uid),
158                                 intval($gcid),
159                                 intval($zcid)
160                         );
161                 }
162
163                 // For unknown reasons there are sometimes duplicates
164                 q("DELETE FROM `gcontact` WHERE `nurl` = '%s' AND `id` != %d AND
165                         NOT EXISTS (SELECT `gcid` FROM `glink` WHERE `gcid` = `gcontact`.`id`)",
166                         dbesc(normalise_link($profile_url)),
167                         intval($gcid)
168                 );
169
170         }
171         logger("poco_load: loaded $total entries",LOGGER_DEBUG);
172
173         q("DELETE FROM `glink` WHERE `cid` = %d AND `uid` = %d AND `zcid` = %d AND `updated` < UTC_TIMESTAMP - INTERVAL 2 DAY",
174                 intval($cid),
175                 intval($uid),
176                 intval($zcid)
177         );
178
179 }
180
181
182 function count_common_friends($uid,$cid) {
183
184         $r = q("SELECT count(*) as `total`
185                 FROM `glink` INNER JOIN `gcontact` on `glink`.`gcid` = `gcontact`.`id`
186                 where `glink`.`cid` = %d and `glink`.`uid` = %d
187                 and `gcontact`.`nurl` in (select nurl from contact where uid = %d and self = 0 and blocked = 0 and hidden = 0 and id != %d ) ",
188                 intval($cid),
189                 intval($uid),
190                 intval($uid),
191                 intval($cid)
192         );
193
194 //      logger("count_common_friends: $uid $cid {$r[0]['total']}"); 
195         if(count($r))
196                 return $r[0]['total'];
197         return 0;
198
199 }
200
201
202 function common_friends($uid,$cid,$start = 0,$limit=9999,$shuffle = false) {
203
204         if($shuffle)
205                 $sql_extra = " order by rand() ";
206         else
207                 $sql_extra = " order by `gcontact`.`name` asc "; 
208
209         $r = q("SELECT `gcontact`.* 
210                 FROM `glink` INNER JOIN `gcontact` on `glink`.`gcid` = `gcontact`.`id`
211                 where `glink`.`cid` = %d and `glink`.`uid` = %d
212                 and `gcontact`.`nurl` in (select nurl from contact where uid = %d and self = 0 and blocked = 0 and hidden = 0 and id != %d ) 
213                 $sql_extra limit %d, %d",
214                 intval($cid),
215                 intval($uid),
216                 intval($uid),
217                 intval($cid),
218                 intval($start),
219                 intval($limit)
220         );
221
222         return $r;
223
224 }
225
226
227 function count_common_friends_zcid($uid,$zcid) {
228
229         $r = q("SELECT count(*) as `total` 
230                 FROM `glink` INNER JOIN `gcontact` on `glink`.`gcid` = `gcontact`.`id`
231                 where `glink`.`zcid` = %d
232                 and `gcontact`.`nurl` in (select nurl from contact where uid = %d and self = 0 and blocked = 0 and hidden = 0 ) ",
233                 intval($zcid),
234                 intval($uid)
235         );
236
237         if(count($r))
238                 return $r[0]['total'];
239         return 0;
240
241 }
242
243 function common_friends_zcid($uid,$zcid,$start = 0, $limit = 9999,$shuffle = false) {
244
245         if($shuffle)
246                 $sql_extra = " order by rand() ";
247         else
248                 $sql_extra = " order by `gcontact`.`name` asc "; 
249
250         $r = q("SELECT `gcontact`.* 
251                 FROM `glink` INNER JOIN `gcontact` on `glink`.`gcid` = `gcontact`.`id`
252                 where `glink`.`zcid` = %d
253                 and `gcontact`.`nurl` in (select nurl from contact where uid = %d and self = 0 and blocked = 0 and hidden = 0 ) 
254                 $sql_extra limit %d, %d",
255                 intval($zcid),
256                 intval($uid),
257                 intval($start),
258                 intval($limit)
259         );
260
261         return $r;
262
263 }
264
265
266 function count_all_friends($uid,$cid) {
267
268         $r = q("SELECT count(*) as `total`
269                 FROM `glink` INNER JOIN `gcontact` on `glink`.`gcid` = `gcontact`.`id`
270                 where `glink`.`cid` = %d and `glink`.`uid` = %d ",
271                 intval($cid),
272                 intval($uid)
273         );
274
275         if(count($r))
276                 return $r[0]['total'];
277         return 0;
278
279 }
280
281
282 function all_friends($uid,$cid,$start = 0, $limit = 80) {
283
284         $r = q("SELECT `gcontact`.* 
285                 FROM `glink` INNER JOIN `gcontact` on `glink`.`gcid` = `gcontact`.`id`
286                 where `glink`.`cid` = %d and `glink`.`uid` = %d 
287                 order by `gcontact`.`name` asc LIMIT %d, %d ",
288                 intval($cid),
289                 intval($uid),
290                 intval($start),
291                 intval($limit)
292         );
293
294         return $r;
295 }
296
297
298
299 function suggestion_query($uid, $start = 0, $limit = 80) {
300
301         if(! $uid)
302                 return array();
303
304         $r = q("SELECT count(glink.gcid) as `total`, gcontact.* from gcontact
305                 INNER JOIN glink on glink.gcid = gcontact.id
306                 where uid = %d and not gcontact.nurl in ( select nurl from contact where uid = %d )
307                 and not gcontact.name in ( select name from contact where uid = %d )
308                 and not gcontact.id in ( select gcid from gcign where uid = %d )
309                 AND `gcontact`.`updated` != '0000-00-00 00:00:00'
310                 group by glink.gcid order by gcontact.updated desc,total desc limit %d, %d ",
311                 intval($uid),
312                 intval($uid),
313                 intval($uid),
314                 intval($uid),
315                 intval($start),
316                 intval($limit)
317         );
318
319         if(count($r) && count($r) >= ($limit -1))
320                 return $r;
321
322         $r2 = q("SELECT gcontact.* from gcontact
323                 INNER JOIN glink on glink.gcid = gcontact.id
324                 where glink.uid = 0 and glink.cid = 0 and glink.zcid = 0 and not gcontact.nurl in ( select nurl from contact where uid = %d )
325                 and not gcontact.name in ( select name from contact where uid = %d )
326                 and not gcontact.id in ( select gcid from gcign where uid = %d )
327                 AND `gcontact`.`updated` != '0000-00-00 00:00:00'
328                 order by rand() limit %d, %d ",
329                 intval($uid),
330                 intval($uid),
331                 intval($uid),
332                 intval($start),
333                 intval($limit)
334         );
335
336         $list = array();
337         foreach ($r2 AS $suggestion)
338                 $list[$suggestion["nurl"]] = $suggestion;
339
340         foreach ($r AS $suggestion)
341                 $list[$suggestion["nurl"]] = $suggestion;
342
343         return $list;
344 }
345
346 function update_suggestions() {
347
348         $a = get_app();
349
350         $done = array();
351
352         poco_load(0,0,0,$a->get_baseurl() . '/poco');
353
354         $done[] = $a->get_baseurl() . '/poco';
355
356         if(strlen(get_config('system','directory_submit_url'))) {
357                 $x = fetch_url('http://dir.friendica.com/pubsites');
358                 if($x) {
359                         $j = json_decode($x);
360                         if($j->entries) {
361                                 foreach($j->entries as $entry) {
362                                         $url = $entry->url . '/poco';
363                                         if(! in_array($url,$done))
364                                                 poco_load(0,0,0,$entry->url . '/poco');
365                                 }
366                         }
367                 }
368         }
369
370         $r = q("select distinct(poco) as poco from contact where network = '%s'",
371                 dbesc(NETWORK_DFRN)
372         );
373
374         if(count($r)) {
375                 foreach($r as $rr) {
376                         $base = substr($rr['poco'],0,strrpos($rr['poco'],'/'));
377                         if(! in_array($base,$done))
378                                 poco_load(0,0,0,$base);
379                 }
380         }
381 }