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