]> git.mxchange.org Git - friendica.git/blob - include/socgraph.php
b6bfa6831fe76534aa0e544128b458134e4158a6
[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,updated,network' : '?fields=displayName,urls,photos,updated,network') ;
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                 $network = '';
71                 $updated = '0000-00-00 00:00:00';
72
73                 $name = $entry->displayName;
74
75                 if(isset($entry->urls)) {
76                         foreach($entry->urls as $url) {
77                                 if($url->type == 'profile') {
78                                         $profile_url = $url->value;
79                                         continue;
80                                 }
81                                 if($url->type == 'webfinger') {
82                                         $connect_url = str_replace('acct:' , '', $url->value);
83                                         continue;
84                                 }
85                         }
86                 }
87                 if(isset($entry->photos)) {
88                         foreach($entry->photos as $photo) {
89                                 if($photo->type == 'profile') {
90                                         $profile_photo = $photo->value;
91                                         continue;
92                                 }
93                         }
94                 }
95
96                 if(isset($entry->updated))
97                         $updated = date("Y-m-d H:i:s", strtotime($entry->updated));
98
99                 if(isset($entry->network))
100                         $network = $entry->network;
101
102                 poco_check($profile_url, $name, $network, $profile_photo, $connect_url, $updated, $cid, $uid, $zcid);
103
104         }
105         logger("poco_load: loaded $total entries",LOGGER_DEBUG);
106
107         q("DELETE FROM `glink` WHERE `cid` = %d AND `uid` = %d AND `zcid` = %d AND `updated` < UTC_TIMESTAMP - INTERVAL 2 DAY",
108                 intval($cid),
109                 intval($uid),
110                 intval($zcid)
111         );
112
113 }
114
115 function poco_check($profile_url, $name, $network, $profile_photo, $connect_url, $updated, $cid = 0, $uid = 0, $zcid = 0) {
116         $gcid = "";
117
118         if ($profile_url == "")
119                 return $gcid;
120
121         if (($network == "") OR ($name == "") OR ($profile_photo == "")) {
122                 require_once("include/Scrape.php");
123
124                 $data = probe_url($profile_url, PROBE_DIASPORA);
125                 $network = $data["network"];
126                 $name = $data["name"];
127                 $profile_photo = $data["photo"];
128         }
129
130         if (!in_array($network, array(NETWORK_DFRN, NETWORK_OSTATUS, NETWORK_DIASPORA, NETWORK_STATUSNET)))
131                 return $gcid;
132
133         if (($name == "") OR ($profile_photo == ""))
134                 return $gcid;
135
136         logger("profile-check URL: ".$profile_url." name: ".$name." avatar: ".$profile_photo, LOGGER_DEBUG);
137
138         $x = q("SELECT * FROM `gcontact` WHERE `nurl` = '%s' LIMIT 1",
139                 dbesc(normalise_link($profile_url))
140         );
141
142         if(count($x)) {
143                 $gcid = $x[0]['id'];
144
145                 if($x[0]['name'] != $name || $x[0]['photo'] != $profile_photo || $x[0]['updated'] < $updated) {
146                         q("update gcontact set `name` = '%s', `network` = '%s', `photo` = '%s', `connect` = '%s', `url` = '%s', `updated` = '%s'
147                                 where `nurl` = '%s'",
148                                 dbesc($name),
149                                 dbesc($network),
150                                 dbesc($profile_photo),
151                                 dbesc($connect_url),
152                                 dbesc($profile_url),
153                                 dbesc($updated),
154                                 dbesc(normalise_link($profile_url))
155                         );
156                 }
157         } else {
158                 q("insert into `gcontact` (`name`,`network`, `url`,`nurl`,`photo`,`connect`, `updated`)
159                         values ('%s', '%s', '%s', '%s', '%s','%s', '%s')",
160                         dbesc($name),
161                         dbesc($network),
162                         dbesc($profile_url),
163                         dbesc(normalise_link($profile_url)),
164                         dbesc($profile_photo),
165                         dbesc($connect_url),
166                         dbesc($updated)
167                 );
168                 $x = q("SELECT * FROM `gcontact` WHERE `nurl` = '%s' LIMIT 1",
169                         dbesc(normalise_link($profile_url))
170                 );
171                 if(count($x))
172                         $gcid = $x[0]['id'];
173         }
174
175         if(! $gcid)
176                 return $gcid;
177
178         $r = q("SELECT * FROM `glink` WHERE `cid` = %d AND `uid` = %d AND `gcid` = %d AND `zcid` = %d LIMIT 1",
179                 intval($cid),
180                 intval($uid),
181                 intval($gcid),
182                 intval($zcid)
183         );
184         if(! count($r)) {
185                 q("INSERT INTO `glink` (`cid`,`uid`,`gcid`,`zcid`, `updated`) VALUES (%d,%d,%d,%d, '%s') ",
186                         intval($cid),
187                         intval($uid),
188                         intval($gcid),
189                         intval($zcid),
190                         dbesc(datetime_convert())
191                 );
192         } else {
193                 q("UPDATE `glink` SET `updated` = '%s' WHERE `cid` = %d AND `uid` = %d AND `gcid` = %d AND `zcid` = %d",
194                         dbesc(datetime_convert()),
195                         intval($cid),
196                         intval($uid),
197                         intval($gcid),
198                         intval($zcid)
199                 );
200         }
201
202         // For unknown reasons there are sometimes duplicates
203         q("DELETE FROM `gcontact` WHERE `nurl` = '%s' AND `id` != %d AND
204                 NOT EXISTS (SELECT `gcid` FROM `glink` WHERE `gcid` = `gcontact`.`id`)",
205                 dbesc(normalise_link($profile_url)),
206                 intval($gcid)
207         );
208
209         return $gcid;
210 }
211
212 function poco_contact_from_body($body, $created, $cid, $uid) {
213         preg_replace_callback("/\[share(.*?)\].*?\[\/share\]/ism",
214                 function ($match) use ($created, $cid, $uid){
215                         return(sub_poco_from_share($match, $created, $cid, $uid));
216                 }, $body);
217 }
218
219 function sub_poco_from_share($share, $created, $cid, $uid) {
220         $profile = "";
221         preg_match("/profile='(.*?)'/ism", $share[1], $matches);
222         if ($matches[1] != "")
223                 $profile = $matches[1];
224
225         preg_match('/profile="(.*?)"/ism', $share[1], $matches);
226         if ($matches[1] != "")
227                 $profile = $matches[1];
228
229         if ($profile == "")
230                 return;
231
232         logger("prepare poco_check for profile ".$profile, LOGGER_DEBUG);
233         poco_check($profile, "", "", "", "", $created, $cid, $uid);
234 }
235
236 function count_common_friends($uid,$cid) {
237
238         $r = q("SELECT count(*) as `total`
239                 FROM `glink` INNER JOIN `gcontact` on `glink`.`gcid` = `gcontact`.`id`
240                 where `glink`.`cid` = %d and `glink`.`uid` = %d
241                 and `gcontact`.`nurl` in (select nurl from contact where uid = %d and self = 0 and blocked = 0 and hidden = 0 and id != %d ) ",
242                 intval($cid),
243                 intval($uid),
244                 intval($uid),
245                 intval($cid)
246         );
247
248 //      logger("count_common_friends: $uid $cid {$r[0]['total']}"); 
249         if(count($r))
250                 return $r[0]['total'];
251         return 0;
252
253 }
254
255
256 function common_friends($uid,$cid,$start = 0,$limit=9999,$shuffle = false) {
257
258         if($shuffle)
259                 $sql_extra = " order by rand() ";
260         else
261                 $sql_extra = " order by `gcontact`.`name` asc ";
262
263         $r = q("SELECT `gcontact`.*
264                 FROM `glink` INNER JOIN `gcontact` on `glink`.`gcid` = `gcontact`.`id`
265                 where `glink`.`cid` = %d and `glink`.`uid` = %d
266                 and `gcontact`.`nurl` in (select nurl from contact where uid = %d and self = 0 and blocked = 0 and hidden = 0 and id != %d ) 
267                 $sql_extra limit %d, %d",
268                 intval($cid),
269                 intval($uid),
270                 intval($uid),
271                 intval($cid),
272                 intval($start),
273                 intval($limit)
274         );
275
276         return $r;
277
278 }
279
280
281 function count_common_friends_zcid($uid,$zcid) {
282
283         $r = q("SELECT count(*) as `total`
284                 FROM `glink` INNER JOIN `gcontact` on `glink`.`gcid` = `gcontact`.`id`
285                 where `glink`.`zcid` = %d
286                 and `gcontact`.`nurl` in (select nurl from contact where uid = %d and self = 0 and blocked = 0 and hidden = 0 ) ",
287                 intval($zcid),
288                 intval($uid)
289         );
290
291         if(count($r))
292                 return $r[0]['total'];
293         return 0;
294
295 }
296
297 function common_friends_zcid($uid,$zcid,$start = 0, $limit = 9999,$shuffle = false) {
298
299         if($shuffle)
300                 $sql_extra = " order by rand() ";
301         else
302                 $sql_extra = " order by `gcontact`.`name` asc ";
303
304         $r = q("SELECT `gcontact`.*
305                 FROM `glink` INNER JOIN `gcontact` on `glink`.`gcid` = `gcontact`.`id`
306                 where `glink`.`zcid` = %d
307                 and `gcontact`.`nurl` in (select nurl from contact where uid = %d and self = 0 and blocked = 0 and hidden = 0 ) 
308                 $sql_extra limit %d, %d",
309                 intval($zcid),
310                 intval($uid),
311                 intval($start),
312                 intval($limit)
313         );
314
315         return $r;
316
317 }
318
319
320 function count_all_friends($uid,$cid) {
321
322         $r = q("SELECT count(*) as `total`
323                 FROM `glink` INNER JOIN `gcontact` on `glink`.`gcid` = `gcontact`.`id`
324                 where `glink`.`cid` = %d and `glink`.`uid` = %d ",
325                 intval($cid),
326                 intval($uid)
327         );
328
329         if(count($r))
330                 return $r[0]['total'];
331         return 0;
332
333 }
334
335
336 function all_friends($uid,$cid,$start = 0, $limit = 80) {
337
338         $r = q("SELECT `gcontact`.*
339                 FROM `glink` INNER JOIN `gcontact` on `glink`.`gcid` = `gcontact`.`id`
340                 where `glink`.`cid` = %d and `glink`.`uid` = %d
341                 order by `gcontact`.`name` asc LIMIT %d, %d ",
342                 intval($cid),
343                 intval($uid),
344                 intval($start),
345                 intval($limit)
346         );
347
348         return $r;
349 }
350
351
352
353 function suggestion_query($uid, $start = 0, $limit = 80) {
354
355         if(! $uid)
356                 return array();
357
358         $network = array(NETWORK_DFRN);
359
360         if (get_config('system','diaspora_enabled'))
361                 $network[] = NETWORK_DIASPORA;
362
363         if (!get_config('system','ostatus_disabled'))
364                 $network[] = NETWORK_OSTATUS;
365
366         $sql_network = implode("', '", $network);
367         //$sql_network = "'".$sql_network."', ''";
368         $sql_network = "'".$sql_network."'";
369
370         $r = q("SELECT count(glink.gcid) as `total`, gcontact.* from gcontact
371                 INNER JOIN glink on glink.gcid = gcontact.id
372                 where uid = %d and not gcontact.nurl in ( select nurl from contact where uid = %d )
373                 and not gcontact.name in ( select name from contact where uid = %d )
374                 and not gcontact.id in ( select gcid from gcign where uid = %d )
375                 AND `gcontact`.`updated` != '0000-00-00 00:00:00'
376                 AND `gcontact`.`network` IN (%s)
377                 group by glink.gcid order by gcontact.updated desc,total desc limit %d, %d ",
378                 intval($uid),
379                 intval($uid),
380                 intval($uid),
381                 intval($uid),
382                 $sql_network,
383                 intval($start),
384                 intval($limit)
385         );
386
387         if(count($r) && count($r) >= ($limit -1))
388                 return $r;
389
390         $r2 = q("SELECT gcontact.* from gcontact
391                 INNER JOIN glink on glink.gcid = gcontact.id
392                 where glink.uid = 0 and glink.cid = 0 and glink.zcid = 0 and not gcontact.nurl in ( select nurl from contact where uid = %d )
393                 and not gcontact.name in ( select name from contact where uid = %d )
394                 and not gcontact.id in ( select gcid from gcign where uid = %d )
395                 AND `gcontact`.`updated` != '0000-00-00 00:00:00'
396                 AND `gcontact`.`network` IN (%s)
397                 order by rand() limit %d, %d ",
398                 intval($uid),
399                 intval($uid),
400                 intval($uid),
401                 $sql_network,
402                 intval($start),
403                 intval($limit)
404         );
405
406         $list = array();
407         foreach ($r2 AS $suggestion)
408                 $list[$suggestion["nurl"]] = $suggestion;
409
410         foreach ($r AS $suggestion)
411                 $list[$suggestion["nurl"]] = $suggestion;
412
413         return $list;
414 }
415
416 function update_suggestions() {
417
418         $a = get_app();
419
420         $done = array();
421
422         poco_load(0,0,0,$a->get_baseurl() . '/poco');
423
424         $done[] = $a->get_baseurl() . '/poco';
425
426         if(strlen(get_config('system','directory_submit_url'))) {
427                 $x = fetch_url('http://dir.friendica.com/pubsites');
428                 if($x) {
429                         $j = json_decode($x);
430                         if($j->entries) {
431                                 foreach($j->entries as $entry) {
432                                         $url = $entry->url . '/poco';
433                                         if(! in_array($url,$done))
434                                                 poco_load(0,0,0,$entry->url . '/poco');
435                                 }
436                         }
437                 }
438         }
439
440         $r = q("select distinct(poco) as poco from contact where network = '%s'",
441                 dbesc(NETWORK_DFRN)
442         );
443
444         if(count($r)) {
445                 foreach($r as $rr) {
446                         $base = substr($rr['poco'],0,strrpos($rr['poco'],'/'));
447                         if(! in_array($base,$done))
448                                 poco_load(0,0,0,$base);
449                 }
450         }
451 }