]> git.mxchange.org Git - friendica.git/blob - include/socgraph.php
Merge branch 'release/3.4'
[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
24         require_once("include/html2bbcode.php");
25
26         $a = get_app();
27
28         if($cid) {
29                 if((! $url) || (! $uid)) {
30                         $r = q("select `poco`, `uid` from `contact` where `id` = %d limit 1",
31                                 intval($cid)
32                         );
33                         if(count($r)) {
34                                 $url = $r[0]['poco'];
35                                 $uid = $r[0]['uid'];
36                         }
37                 }
38                 if(! $uid)
39                         return;
40         }
41
42         if(! $url)
43                 return;
44
45         $url = $url . (($uid) ? '/@me/@all?fields=displayName,urls,photos,updated,network,aboutMe,currentLocation,tags,gender,generation' : '?fields=displayName,urls,photos,updated,network,aboutMe,currentLocation,tags,gender,generation') ;
46
47         logger('poco_load: ' . $url, LOGGER_DEBUG);
48
49         $s = fetch_url($url);
50
51         logger('poco_load: returns ' . $s, LOGGER_DATA);
52
53         logger('poco_load: return code: ' . $a->get_curl_code(), LOGGER_DEBUG);
54
55         if(($a->get_curl_code() > 299) || (! $s))
56                 return;
57
58         $j = json_decode($s);
59
60         logger('poco_load: json: ' . print_r($j,true),LOGGER_DATA);
61
62         if(! isset($j->entry))
63                 return;
64
65         $total = 0;
66         foreach($j->entry as $entry) {
67
68                 $total ++;
69                 $profile_url = '';
70                 $profile_photo = '';
71                 $connect_url = '';
72                 $name = '';
73                 $network = '';
74                 $updated = '0000-00-00 00:00:00';
75                 $location = '';
76                 $about = '';
77                 $keywords = '';
78                 $gender = '';
79                 $generation = 0;
80
81                 $name = $entry->displayName;
82
83                 if(isset($entry->urls)) {
84                         foreach($entry->urls as $url) {
85                                 if($url->type == 'profile') {
86                                         $profile_url = $url->value;
87                                         continue;
88                                 }
89                                 if($url->type == 'webfinger') {
90                                         $connect_url = str_replace('acct:' , '', $url->value);
91                                         continue;
92                                 }
93                         }
94                 }
95                 if(isset($entry->photos)) {
96                         foreach($entry->photos as $photo) {
97                                 if($photo->type == 'profile') {
98                                         $profile_photo = $photo->value;
99                                         continue;
100                                 }
101                         }
102                 }
103
104                 if(isset($entry->updated))
105                         $updated = date("Y-m-d H:i:s", strtotime($entry->updated));
106
107                 if(isset($entry->network))
108                         $network = $entry->network;
109
110                 if(isset($entry->currentLocation))
111                         $location = $entry->currentLocation;
112
113                 if(isset($entry->aboutMe))
114                         $about = html2bbcode($entry->aboutMe);
115
116                 if(isset($entry->gender))
117                         $gender = $entry->gender;
118
119                 if(isset($entry->generation) AND ($entry->generation > 0))
120                         $generation = ++$entry->generation;
121
122                 if(isset($entry->tags))
123                         foreach($entry->tags as $tag)
124                                 $keywords = implode(", ", $tag);
125
126                 // If you query a Friendica server for its profiles, the network has to be Friendica
127                 if ($uid == 0)
128                         $network = NETWORK_DFRN;
129
130                 poco_check($profile_url, $name, $network, $profile_photo, $about, $location, $gender, $keywords, $connect_url, $updated, $generation, $cid, $uid, $zcid);
131
132                 // Update the Friendica contacts. Diaspora is doing it via a message. (See include/diaspora.php)
133                 if (($location != "") OR ($about != "") OR ($keywords != "") OR ($gender != ""))
134                         q("UPDATE `contact` SET `location` = '%s', `about` = '%s', `keywords` = '%s', `gender` = '%s'
135                                 WHERE `nurl` = '%s' AND NOT `self` AND `network` = '%s'",
136                                 dbesc($location),
137                                 dbesc($about),
138                                 dbesc($keywords),
139                                 dbesc($gender),
140                                 dbesc(normalise_link($profile_url)),
141                                 dbesc(NETWORK_DFRN));
142         }
143         logger("poco_load: loaded $total entries",LOGGER_DEBUG);
144
145         q("DELETE FROM `glink` WHERE `cid` = %d AND `uid` = %d AND `zcid` = %d AND `updated` < UTC_TIMESTAMP - INTERVAL 2 DAY",
146                 intval($cid),
147                 intval($uid),
148                 intval($zcid)
149         );
150
151 }
152
153 function poco_check($profile_url, $name, $network, $profile_photo, $about, $location, $gender, $keywords, $connect_url, $updated, $generation, $cid = 0, $uid = 0, $zcid = 0) {
154
155         $a = get_app();
156
157         // Generation:
158         //  0: No definition
159         //  1: Profiles on this server
160         //  2: Contacts of profiles on this server
161         //  3: Contacts of contacts of profiles on this server
162         //  4: ...
163
164         $gcid = "";
165
166         if ($profile_url == "")
167                 return $gcid;
168
169         // Don't store the statusnet connector as network
170         // We can't simply set this to NETWORK_OSTATUS since the connector could have fetched posts from friendica as well
171         if ($network == NETWORK_STATUSNET)
172                 $network = "";
173
174         // The global contacts should contain the original picture, not the cached one
175         if (($generation != 1) AND stristr(normalise_link($profile_photo), normalise_link($a->get_baseurl()."/photo/")))
176                 $profile_photo = "";
177
178         $r = q("SELECT `network` FROM `contact` WHERE `nurl` = '%s' AND `network` != '' AND `network` != '%s' LIMIT 1",
179                 dbesc(normalise_link($profile_url)), dbesc(NETWORK_STATUSNET)
180         );
181         if(count($r))
182                 $network = $r[0]["network"];
183
184         if (($network == "") OR ($network == NETWORK_OSTATUS)) {
185                 $r = q("SELECT `network`, `url` FROM `contact` WHERE `alias` IN ('%s', '%s') AND `network` != '' AND `network` != '%s' LIMIT 1",
186                         dbesc($profile_url), dbesc(normalise_link($profile_url)), dbesc(NETWORK_STATUSNET)
187                 );
188                 if(count($r)) {
189                         $network = $r[0]["network"];
190                         $profile_url = $r[0]["url"];
191                 }
192         }
193
194         $x = q("SELECT * FROM `gcontact` WHERE `nurl` = '%s' LIMIT 1",
195                 dbesc(normalise_link($profile_url))
196         );
197         if(count($x) AND ($network == "") AND ($x[0]["network"] != NETWORK_STATUSNET))
198                 $network = $x[0]["network"];
199
200         if (($network == "") OR ($name == "") OR ($profile_photo == "")) {
201                 require_once("include/Scrape.php");
202
203                 $data = probe_url($profile_url);
204                 $network = $data["network"];
205                 $name = $data["name"];
206                 $profile_url = $data["url"];
207                 $profile_photo = $data["photo"];
208         }
209
210         if (count($x) AND ($x[0]["network"] == "") AND ($network != "")) {
211                 q("UPDATE `gcontact` SET `network` = '%s' WHERE `nurl` = '%s'",
212                         dbesc($network),
213                         dbesc(normalise_link($profile_url))
214                 );
215         }
216
217         if (($name == "") OR ($profile_photo == ""))
218                 return $gcid;
219
220         if (!in_array($network, array(NETWORK_DFRN, NETWORK_OSTATUS, NETWORK_DIASPORA)))
221                 return $gcid;
222
223         logger("profile-check generation: ".$generation." Network: ".$network." URL: ".$profile_url." name: ".$name." avatar: ".$profile_photo, LOGGER_DEBUG);
224
225         if(count($x)) {
226                 $gcid = $x[0]['id'];
227
228                 if (($location == "") AND ($x[0]['location'] != ""))
229                         $location = $x[0]['location'];
230
231                 if (($about == "") AND ($x[0]['about'] != ""))
232                         $about = $x[0]['about'];
233
234                 if (($gender == "") AND ($x[0]['gender'] != ""))
235                         $gender = $x[0]['gender'];
236
237                 if (($keywords == "") AND ($x[0]['keywords'] != ""))
238                         $keywords = $x[0]['keywords'];
239
240                 if (($generation == 0) AND ($x[0]['generation'] > 0))
241                         $generation = $x[0]['generation'];
242
243                 if($x[0]['name'] != $name || $x[0]['photo'] != $profile_photo || $x[0]['updated'] < $updated) {
244                         q("UPDATE `gcontact` SET `name` = '%s', `network` = '%s', `photo` = '%s', `connect` = '%s', `url` = '%s',
245                                 `updated` = '%s', `location` = '%s', `about` = '%s', `keywords` = '%s', `gender` = '%s', `generation` = %d
246                                 WHERE (`generation` >= %d OR `generation` = 0) AND `nurl` = '%s'",
247                                 dbesc($name),
248                                 dbesc($network),
249                                 dbesc($profile_photo),
250                                 dbesc($connect_url),
251                                 dbesc($profile_url),
252                                 dbesc($updated),
253                                 dbesc($location),
254                                 dbesc($about),
255                                 dbesc($keywords),
256                                 dbesc($gender),
257                                 intval($generation),
258                                 intval($generation),
259                                 dbesc(normalise_link($profile_url))
260                         );
261                 }
262         } else {
263                 q("INSERT INTO `gcontact` (`name`,`network`, `url`,`nurl`,`photo`,`connect`, `updated`, `location`, `about`, `keywords`, `gender`, `generation`)
264                         VALUES ('%s', '%s', '%s', '%s', '%s','%s', '%s', '%s', '%s', '%s', '%s', %d)",
265                         dbesc($name),
266                         dbesc($network),
267                         dbesc($profile_url),
268                         dbesc(normalise_link($profile_url)),
269                         dbesc($profile_photo),
270                         dbesc($connect_url),
271                         dbesc($updated),
272                         dbesc($location),
273                         dbesc($about),
274                         dbesc($keywords),
275                         dbesc($gender),
276                         intval($generation)
277                 );
278                 $x = q("SELECT * FROM `gcontact` WHERE `nurl` = '%s' LIMIT 1",
279                         dbesc(normalise_link($profile_url))
280                 );
281                 if(count($x))
282                         $gcid = $x[0]['id'];
283         }
284
285         if(! $gcid)
286                 return $gcid;
287
288         $r = q("SELECT * FROM `glink` WHERE `cid` = %d AND `uid` = %d AND `gcid` = %d AND `zcid` = %d LIMIT 1",
289                 intval($cid),
290                 intval($uid),
291                 intval($gcid),
292                 intval($zcid)
293         );
294         if(! count($r)) {
295                 q("INSERT INTO `glink` (`cid`,`uid`,`gcid`,`zcid`, `updated`) VALUES (%d,%d,%d,%d, '%s') ",
296                         intval($cid),
297                         intval($uid),
298                         intval($gcid),
299                         intval($zcid),
300                         dbesc(datetime_convert())
301                 );
302         } else {
303                 q("UPDATE `glink` SET `updated` = '%s' WHERE `cid` = %d AND `uid` = %d AND `gcid` = %d AND `zcid` = %d",
304                         dbesc(datetime_convert()),
305                         intval($cid),
306                         intval($uid),
307                         intval($gcid),
308                         intval($zcid)
309                 );
310         }
311
312         // For unknown reasons there are sometimes duplicates
313         q("DELETE FROM `gcontact` WHERE `nurl` = '%s' AND `id` != %d AND
314                 NOT EXISTS (SELECT `gcid` FROM `glink` WHERE `gcid` = `gcontact`.`id`)",
315                 dbesc(normalise_link($profile_url)),
316                 intval($gcid)
317         );
318
319         return $gcid;
320 }
321
322 function poco_contact_from_body($body, $created, $cid, $uid) {
323         preg_replace_callback("/\[share(.*?)\].*?\[\/share\]/ism",
324                 function ($match) use ($created, $cid, $uid){
325                         return(sub_poco_from_share($match, $created, $cid, $uid));
326                 }, $body);
327 }
328
329 function sub_poco_from_share($share, $created, $cid, $uid) {
330         $profile = "";
331         preg_match("/profile='(.*?)'/ism", $share[1], $matches);
332         if ($matches[1] != "")
333                 $profile = $matches[1];
334
335         preg_match('/profile="(.*?)"/ism', $share[1], $matches);
336         if ($matches[1] != "")
337                 $profile = $matches[1];
338
339         if ($profile == "")
340                 return;
341
342         logger("prepare poco_check for profile ".$profile, LOGGER_DEBUG);
343         poco_check($profile, "", "", "", "", "", "", "", "", $created, 3, $cid, $uid);
344 }
345
346 function poco_store($item) {
347
348         // Isn't it public?
349         if ($item['private'])
350                 return;
351
352         // Or is it from a network where we don't store the global contacts?
353         if (!in_array($item["network"], array(NETWORK_DFRN, NETWORK_DIASPORA, NETWORK_OSTATUS, NETWORK_STATUSNET, "")))
354                 return;
355
356         // Is it a global copy?
357         $store_gcontact = ($item["uid"] == 0);
358
359         // Is it a comment on a global copy?
360         if (!$store_gcontact AND ($item["uri"] != $item["parent-uri"])) {
361                 $q = q("SELECT `id` FROM `item` WHERE `uri`='%s' AND `uid` = 0", $item["parent-uri"]);
362                 $store_gcontact = count($q);
363         }
364
365         if (!$store_gcontact)
366                 return;
367
368         // "3" means: We don't know this contact directly (Maybe a reshared item)
369         $generation = 3;
370         $network = "";
371         $profile_url = $item["author-link"];
372
373         // Is it a user from our server?
374         $q = q("SELECT `id` FROM `contact` WHERE `self` AND `nurl` = '%s' LIMIT 1",
375                 dbesc(normalise_link($item["author-link"])));
376         if (count($q)) {
377                 logger("Our user (generation 1): ".$item["author-link"], LOGGER_DEBUG);
378                 $generation = 1;
379                 $network = NETWORK_DFRN;
380         } else { // Is it a contact from a user on our server?
381                 $q = q("SELECT `network`, `url` FROM `contact` WHERE `uid` != 0 AND `network` != ''
382                         AND (`nurl` = '%s' OR `alias` IN ('%s', '%s')) AND `network` != '%s' LIMIT 1",
383                         dbesc(normalise_link($item["author-link"])),
384                         dbesc(normalise_link($item["author-link"])),
385                         dbesc($item["author-link"]),
386                         dbesc(NETWORK_STATUSNET));
387                 if (count($q)) {
388                         $generation = 2;
389                         $network = $q[0]["network"];
390                         $profile_url = $q[0]["url"];
391                         logger("Known contact (generation 2): ".$profile_url, LOGGER_DEBUG);
392                 }
393         }
394
395         if ($generation == 3)
396                 logger("Unknown contact (generation 3): ".$item["author-link"], LOGGER_DEBUG);
397
398         poco_check($profile_url, $item["author-name"], $network, $item["author-avatar"], "", "", "", "", "", $item["received"], $generation, $item["contact-id"], $item["uid"]);
399
400         // Maybe its a body with a shared item? Then extract a global contact from it.
401         poco_contact_from_body($item["body"], $item["received"], $item["contact-id"], $item["uid"]);
402 }
403
404 function count_common_friends($uid,$cid) {
405
406         $r = q("SELECT count(*) as `total`
407                 FROM `glink` INNER JOIN `gcontact` on `glink`.`gcid` = `gcontact`.`id`
408                 where `glink`.`cid` = %d and `glink`.`uid` = %d
409                 and `gcontact`.`nurl` in (select nurl from contact where uid = %d and self = 0 and blocked = 0 and hidden = 0 and id != %d ) ",
410                 intval($cid),
411                 intval($uid),
412                 intval($uid),
413                 intval($cid)
414         );
415
416 //      logger("count_common_friends: $uid $cid {$r[0]['total']}"); 
417         if(count($r))
418                 return $r[0]['total'];
419         return 0;
420
421 }
422
423
424 function common_friends($uid,$cid,$start = 0,$limit=9999,$shuffle = false) {
425
426         if($shuffle)
427                 $sql_extra = " order by rand() ";
428         else
429                 $sql_extra = " order by `gcontact`.`name` asc ";
430
431         $r = q("SELECT `gcontact`.*
432                 FROM `glink` INNER JOIN `gcontact` on `glink`.`gcid` = `gcontact`.`id`
433                 where `glink`.`cid` = %d and `glink`.`uid` = %d
434                 and `gcontact`.`nurl` in (select nurl from contact where uid = %d and self = 0 and blocked = 0 and hidden = 0 and id != %d ) 
435                 $sql_extra limit %d, %d",
436                 intval($cid),
437                 intval($uid),
438                 intval($uid),
439                 intval($cid),
440                 intval($start),
441                 intval($limit)
442         );
443
444         return $r;
445
446 }
447
448
449 function count_common_friends_zcid($uid,$zcid) {
450
451         $r = q("SELECT count(*) as `total`
452                 FROM `glink` INNER JOIN `gcontact` on `glink`.`gcid` = `gcontact`.`id`
453                 where `glink`.`zcid` = %d
454                 and `gcontact`.`nurl` in (select nurl from contact where uid = %d and self = 0 and blocked = 0 and hidden = 0 ) ",
455                 intval($zcid),
456                 intval($uid)
457         );
458
459         if(count($r))
460                 return $r[0]['total'];
461         return 0;
462
463 }
464
465 function common_friends_zcid($uid,$zcid,$start = 0, $limit = 9999,$shuffle = false) {
466
467         if($shuffle)
468                 $sql_extra = " order by rand() ";
469         else
470                 $sql_extra = " order by `gcontact`.`name` asc ";
471
472         $r = q("SELECT `gcontact`.*
473                 FROM `glink` INNER JOIN `gcontact` on `glink`.`gcid` = `gcontact`.`id`
474                 where `glink`.`zcid` = %d
475                 and `gcontact`.`nurl` in (select nurl from contact where uid = %d and self = 0 and blocked = 0 and hidden = 0 ) 
476                 $sql_extra limit %d, %d",
477                 intval($zcid),
478                 intval($uid),
479                 intval($start),
480                 intval($limit)
481         );
482
483         return $r;
484
485 }
486
487
488 function count_all_friends($uid,$cid) {
489
490         $r = q("SELECT count(*) as `total`
491                 FROM `glink` INNER JOIN `gcontact` on `glink`.`gcid` = `gcontact`.`id`
492                 where `glink`.`cid` = %d and `glink`.`uid` = %d ",
493                 intval($cid),
494                 intval($uid)
495         );
496
497         if(count($r))
498                 return $r[0]['total'];
499         return 0;
500
501 }
502
503
504 function all_friends($uid,$cid,$start = 0, $limit = 80) {
505
506         $r = q("SELECT `gcontact`.*
507                 FROM `glink` INNER JOIN `gcontact` on `glink`.`gcid` = `gcontact`.`id`
508                 where `glink`.`cid` = %d and `glink`.`uid` = %d
509                 order by `gcontact`.`name` asc LIMIT %d, %d ",
510                 intval($cid),
511                 intval($uid),
512                 intval($start),
513                 intval($limit)
514         );
515
516         return $r;
517 }
518
519
520
521 function suggestion_query($uid, $start = 0, $limit = 80) {
522
523         if(! $uid)
524                 return array();
525
526         $network = array(NETWORK_DFRN);
527
528         if (get_config('system','diaspora_enabled'))
529                 $network[] = NETWORK_DIASPORA;
530
531         if (!get_config('system','ostatus_disabled'))
532                 $network[] = NETWORK_OSTATUS;
533
534         $sql_network = implode("', '", $network);
535         //$sql_network = "'".$sql_network."', ''";
536         $sql_network = "'".$sql_network."'";
537
538         $r = q("SELECT count(glink.gcid) as `total`, gcontact.* from gcontact
539                 INNER JOIN glink on glink.gcid = gcontact.id
540                 where uid = %d and not gcontact.nurl in ( select nurl from contact where uid = %d )
541                 and not gcontact.name in ( select name from contact where uid = %d )
542                 and not gcontact.id in ( select gcid from gcign where uid = %d )
543                 AND `gcontact`.`updated` != '0000-00-00 00:00:00'
544                 AND `gcontact`.`network` IN (%s)
545                 group by glink.gcid order by gcontact.updated desc,total desc limit %d, %d ",
546                 intval($uid),
547                 intval($uid),
548                 intval($uid),
549                 intval($uid),
550                 $sql_network,
551                 intval($start),
552                 intval($limit)
553         );
554
555         if(count($r) && count($r) >= ($limit -1))
556                 return $r;
557
558         $r2 = q("SELECT gcontact.* from gcontact
559                 INNER JOIN glink on glink.gcid = gcontact.id
560                 where glink.uid = 0 and glink.cid = 0 and glink.zcid = 0 and not gcontact.nurl in ( select nurl from contact where uid = %d )
561                 and not gcontact.name in ( select name from contact where uid = %d )
562                 and not gcontact.id in ( select gcid from gcign where uid = %d )
563                 AND `gcontact`.`updated` != '0000-00-00 00:00:00'
564                 AND `gcontact`.`network` IN (%s)
565                 order by rand() limit %d, %d ",
566                 intval($uid),
567                 intval($uid),
568                 intval($uid),
569                 $sql_network,
570                 intval($start),
571                 intval($limit)
572         );
573
574         $list = array();
575         foreach ($r2 AS $suggestion)
576                 $list[$suggestion["nurl"]] = $suggestion;
577
578         foreach ($r AS $suggestion)
579                 $list[$suggestion["nurl"]] = $suggestion;
580
581         return $list;
582 }
583
584 function update_suggestions() {
585
586         $a = get_app();
587
588         $done = array();
589
590         poco_load(0,0,0,$a->get_baseurl() . '/poco');
591
592         $done[] = $a->get_baseurl() . '/poco';
593
594         if(strlen(get_config('system','directory_submit_url'))) {
595                 $x = fetch_url('http://dir.friendica.com/pubsites');
596                 if($x) {
597                         $j = json_decode($x);
598                         if($j->entries) {
599                                 foreach($j->entries as $entry) {
600                                         $url = $entry->url . '/poco';
601                                         if(! in_array($url,$done))
602                                                 poco_load(0,0,0,$entry->url . '/poco');
603                                 }
604                         }
605                 }
606         }
607
608         $r = q("select distinct(poco) as poco from contact where network = '%s'",
609                 dbesc(NETWORK_DFRN)
610         );
611
612         if(count($r)) {
613                 foreach($r as $rr) {
614                         $base = substr($rr['poco'],0,strrpos($rr['poco'],'/'));
615                         if(! in_array($base,$done))
616                                 poco_load(0,0,0,$base);
617                 }
618         }
619 }