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