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