]> git.mxchange.org Git - friendica.git/blob - include/socgraph.php
Query other servers periodically for their known contacts
[friendica.git] / include / socgraph.php
1 <?php
2
3 require_once('include/datetime.php');
4 require_once("include/Scrape.php");
5 require_once("include/html2bbcode.php");
6
7 /*
8  * poco_load
9  *
10  * Given a contact-id (minimum), load the PortableContacts friend list for that contact,
11  * and add the entries to the gcontact (Global Contact) table, or update existing entries
12  * if anything (name or photo) has changed.
13  * We use normalised urls for comparison which ignore http vs https and www.domain vs domain
14  *
15  * Once the global contact is stored add (if necessary) the contact linkage which associates
16  * the given uid, cid to the global contact entry. There can be many uid/cid combinations
17  * pointing to the same global contact id.
18  *
19  */
20
21
22
23
24 function poco_load($cid,$uid = 0,$zcid = 0,$url = null) {
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         $orig_updated = $updated;
171
172         // Don't store the statusnet connector as network
173         // We can't simply set this to NETWORK_OSTATUS since the connector could have fetched posts from friendica as well
174         if ($network == NETWORK_STATUSNET)
175                 $network = "";
176
177         // The global contacts should contain the original picture, not the cached one
178         if (($generation != 1) AND stristr(normalise_link($profile_photo), normalise_link($a->get_baseurl()."/photo/")))
179                 $profile_photo = "";
180
181         $r = q("SELECT `network` FROM `contact` WHERE `nurl` = '%s' AND `network` != '' AND `network` != '%s' LIMIT 1",
182                 dbesc(normalise_link($profile_url)), dbesc(NETWORK_STATUSNET)
183         );
184         if(count($r))
185                 $network = $r[0]["network"];
186
187         if (($network == "") OR ($network == NETWORK_OSTATUS)) {
188                 $r = q("SELECT `network`, `url` FROM `contact` WHERE `alias` IN ('%s', '%s') AND `network` != '' AND `network` != '%s' LIMIT 1",
189                         dbesc($profile_url), dbesc(normalise_link($profile_url)), dbesc(NETWORK_STATUSNET)
190                 );
191                 if(count($r)) {
192                         $network = $r[0]["network"];
193                         $profile_url = $r[0]["url"];
194                 }
195         }
196
197         $x = q("SELECT * FROM `gcontact` WHERE `nurl` = '%s' LIMIT 1",
198                 dbesc(normalise_link($profile_url))
199         );
200
201         if (count($x)) {
202                 if (($network == "") AND ($x[0]["network"] != NETWORK_STATUSNET))
203                         $network = $x[0]["network"];
204
205                 if ($updated == "0000-00-00 00:00:00")
206                         $updated = $x[0]["updated"];
207
208                 $last_contact = $x[0]["last_contact"];
209                 $last_failure = $x[0]["last_failure"];
210                 $server_url = $x[0]["server_url"];
211         } else {
212                 $last_contact = "0000-00-00 00:00:00";
213                 $last_failure = "0000-00-00 00:00:00";
214                 $server_url = "";
215         }
216
217         if (($network == "") OR ($name == "") OR ($profile_photo == "") OR ($server_url == "")) {
218                 $data = probe_url($profile_url);
219                 $network = $data["network"];
220                 $name = $data["name"];
221                 $profile_url = $data["url"];
222                 $profile_photo = $data["photo"];
223                 $server_url = $data["baseurl"];
224         }
225
226         if (count($x) AND ($x[0]["network"] == "") AND ($network != "")) {
227                 q("UPDATE `gcontact` SET `network` = '%s' WHERE `nurl` = '%s'",
228                         dbesc($network),
229                         dbesc(normalise_link($profile_url))
230                 );
231         }
232
233         if (($name == "") OR ($profile_photo == ""))
234                 return $gcid;
235
236         if (!in_array($network, array(NETWORK_DFRN, NETWORK_OSTATUS, NETWORK_DIASPORA)))
237                 return $gcid;
238
239         logger("profile-check generation: ".$generation." Network: ".$network." URL: ".$profile_url." name: ".$name." avatar: ".$profile_photo, LOGGER_DEBUG);
240
241         // Only fetch last update manually if it wasn't provided and enabled in the system
242         if (get_config('system','poco_completion') AND ($orig_updated == "0000-00-00 00:00:00") AND poco_do_update($updated, $last_contact, $last_failure)) {
243                 $last_updated = poco_last_updated($profile_url);
244                 if ($last_updated) {
245                         $updated = $last_updated;
246                         $last_contact = datetime_convert();
247                         logger("Last updated for profile ".$profile_url.": ".$updated, LOGGER_DEBUG);
248
249                         if (count($x))
250                                 q("UPDATE `gcontact` SET `last_contact` = '%s' WHERE `nurl` = '%s'", dbesc($last_contact), dbesc(normalise_link($profile_url)));
251                 } else {
252                         $last_failure = datetime_convert();
253
254                         if (count($x))
255                                 q("UPDATE `gcontact` SET `last_failure` = '%s' WHERE `nurl` = '%s'", dbesc($last_failure), dbesc(normalise_link($profile_url)));
256                 }
257         }
258
259         poco_check_server($server_url, $network);
260
261         // Test - remove before flight
262         //if ($last_contact > $last_failure)
263         //      q("UPDATE `gserver` SET `last_contact` = '%s' WHERE `nurl` = '%s'", dbesc($last_contact), dbesc(normalise_link($server_url)));
264         //else
265         //      q("UPDATE `gserver` SET `last_failure` = '%s' WHERE `nurl` = '%s'", dbesc($last_failure), dbesc(normalise_link($server_url)));
266
267         if(count($x)) {
268                 $gcid = $x[0]['id'];
269
270                 if (($location == "") AND ($x[0]['location'] != ""))
271                         $location = $x[0]['location'];
272
273                 if (($about == "") AND ($x[0]['about'] != ""))
274                         $about = $x[0]['about'];
275
276                 if (($gender == "") AND ($x[0]['gender'] != ""))
277                         $gender = $x[0]['gender'];
278
279                 if (($keywords == "") AND ($x[0]['keywords'] != ""))
280                         $keywords = $x[0]['keywords'];
281
282                 if (($generation == 0) AND ($x[0]['generation'] > 0))
283                         $generation = $x[0]['generation'];
284
285                 if($x[0]['name'] != $name || $x[0]['photo'] != $profile_photo || $x[0]['updated'] < $updated) {
286                         q("UPDATE `gcontact` SET `name` = '%s', `network` = '%s', `photo` = '%s', `connect` = '%s', `url` = '%s', `server_url` = '%s',
287                                 `updated` = '%s', `location` = '%s', `about` = '%s', `keywords` = '%s', `gender` = '%s', `generation` = %d
288                                 WHERE (`generation` >= %d OR `generation` = 0) AND `nurl` = '%s'",
289                                 dbesc($name),
290                                 dbesc($network),
291                                 dbesc($profile_photo),
292                                 dbesc($connect_url),
293                                 dbesc($profile_url),
294                                 dbesc($server_url),
295                                 dbesc($updated),
296                                 dbesc($location),
297                                 dbesc($about),
298                                 dbesc($keywords),
299                                 dbesc($gender),
300                                 intval($generation),
301                                 intval($generation),
302                                 dbesc(normalise_link($profile_url))
303                         );
304                 }
305         } else {
306                 q("INSERT INTO `gcontact` (`name`,`network`, `url`,`nurl`,`photo`,`connect`, `server_url`, `updated`, `last_contact`, `last_failure`, `location`, `about`, `keywords`, `gender`, `generation`)
307                         VALUES ('%s', '%s', '%s', '%s', '%s','%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', %d)",
308                         dbesc($name),
309                         dbesc($network),
310                         dbesc($profile_url),
311                         dbesc(normalise_link($profile_url)),
312                         dbesc($profile_photo),
313                         dbesc($connect_url),
314                         dbesc($server_url),
315                         dbesc($updated),
316                         dbesc($last_contact),
317                         dbesc($last_failure),
318                         dbesc($location),
319                         dbesc($about),
320                         dbesc($keywords),
321                         dbesc($gender),
322                         intval($generation)
323                 );
324                 $x = q("SELECT * FROM `gcontact` WHERE `nurl` = '%s' LIMIT 1",
325                         dbesc(normalise_link($profile_url))
326                 );
327                 if(count($x))
328                         $gcid = $x[0]['id'];
329         }
330
331         if(! $gcid)
332                 return $gcid;
333
334         $r = q("SELECT * FROM `glink` WHERE `cid` = %d AND `uid` = %d AND `gcid` = %d AND `zcid` = %d LIMIT 1",
335                 intval($cid),
336                 intval($uid),
337                 intval($gcid),
338                 intval($zcid)
339         );
340         if(! count($r)) {
341                 q("INSERT INTO `glink` (`cid`,`uid`,`gcid`,`zcid`, `updated`) VALUES (%d,%d,%d,%d, '%s') ",
342                         intval($cid),
343                         intval($uid),
344                         intval($gcid),
345                         intval($zcid),
346                         dbesc(datetime_convert())
347                 );
348         } else {
349                 q("UPDATE `glink` SET `updated` = '%s' WHERE `cid` = %d AND `uid` = %d AND `gcid` = %d AND `zcid` = %d",
350                         dbesc(datetime_convert()),
351                         intval($cid),
352                         intval($uid),
353                         intval($gcid),
354                         intval($zcid)
355                 );
356         }
357
358         // For unknown reasons there are sometimes duplicates
359         q("DELETE FROM `gcontact` WHERE `nurl` = '%s' AND `id` != %d AND
360                 NOT EXISTS (SELECT `gcid` FROM `glink` WHERE `gcid` = `gcontact`.`id`)",
361                 dbesc(normalise_link($profile_url)),
362                 intval($gcid)
363         );
364
365         return $gcid;
366 }
367
368 function poco_last_updated($profile) {
369         $data = probe_url($profile);
370
371         if (($data["poll"] == "") OR ($data["network"] == NETWORK_FEED))
372                 return false;
373
374         // To-Do: Use noscrape
375
376         $feedret = z_fetch_url($data["poll"]);
377
378         if (!$feedret["success"])
379                 return false;
380
381         $doc = new DOMDocument();
382         @$doc->loadXML($feedret["body"]);
383
384         $xpath = new DomXPath($doc);
385         $xpath->registerNamespace('atom', "http://www.w3.org/2005/Atom");
386
387         $entries = $xpath->query('/atom:feed/atom:entry');
388
389         $last_updated = "";
390
391         foreach ($entries AS $entry) {
392                 $published = $xpath->query('atom:published/text()', $entry)->item(0)->nodeValue;
393                 $updated = $xpath->query('atom:updated/text()', $entry)->item(0)->nodeValue;
394
395                 if ($last_updated < $published)
396                         $last_updated = $published;
397
398                 if ($last_updated < $updated)
399                         $last_updated = $updated;
400         }
401
402         // Maybe there aren't any entries. Then check if it is a valid feed
403         if ($last_updated == "")
404                 if ($xpath->query('/atom:feed')->length > 0)
405                         $last_updated = "0000-00-00 00:00:00";
406
407         return($last_updated);
408 }
409
410 function poco_do_update($updated, $last_contact, $last_failure) {
411         $now = strtotime(datetime_convert());
412
413         if ($updated > $last_contact)
414                 $contact_time = strtotime($updated);
415         else
416                 $contact_time = strtotime($last_contact);
417
418         $failure_time = strtotime($last_failure);
419
420         // If the last contact was less than 24 hours then don't update
421         if (($now - $contact_time) < (60 * 60 * 24))
422                 return false;
423
424         // If the last failure was less than 24 hours then don't update
425         if (($now - $failure_time) < (60 * 60 * 24))
426                 return false;
427
428         // If the last contact was less than a week ago and the last failure is older than a week then don't update
429         if ((($now - $contact_time) < (60 * 60 * 24 * 7)) AND ($contact_time > $failure_time))
430                 return false;
431
432         // If the last contact time was more than a week ago, then only try once a week
433         if (($now - $contact_time) > (60 * 60 * 24 * 7) AND ($now - $failure_time) < (60 * 60 * 24 * 7))
434                 return false;
435
436         // If the last contact time was more than a month ago, then only try once a month
437         if (($now - $contact_time) > (60 * 60 * 24 * 30) AND ($now - $failure_time) < (60 * 60 * 24 * 30))
438                 return false;
439
440         return true;
441 }
442
443 function poco_to_boolean($val) {
444         if (($val == "true") OR ($val == 1))
445                 return(true);
446         if (($val == "false") OR ($val == 0))
447                 return(false);
448
449         return ($val);
450 }
451
452 function poco_check_server($server_url, $network = "") {
453
454         if ($server_url == "")
455                 return;
456
457         $servers = q("SELECT * FROM `gserver` WHERE `nurl` = '%s'", dbesc(normalise_link($server_url)));
458         if ($servers) {
459                 $poco = $servers[0]["poco"];
460                 $noscrape = $servers[0]["noscrape"];
461
462                 if ($network == "")
463                         $network = $servers[0]["network"];
464
465                 $last_contact = $servers[0]["last_contact"];
466                 $last_failure = $servers[0]["last_failure"];
467                 $version = $servers[0]["version"];
468                 $platform = $servers[0]["platform"];
469                 $site_name = $servers[0]["site_name"];
470                 $info = $servers[0]["info"];
471                 $register_policy = $servers[0]["register_policy"];
472
473                 // Only check the server once a week
474                 if (strtotime(datetime_convert()) < (strtotime($last_contact) + (60 * 60 * 24 * 7)))
475                         return;
476
477                 if (strtotime(datetime_convert()) < (strtotime($last_failure) + (60 * 60 * 24 * 7)))
478                         return;
479         } else {
480                 $poco = "";
481                 $noscrape = "";
482                 $version = "";
483                 $platform = "";
484                 $site_name = "";
485                 $info = "";
486                 $register_policy = -1;
487
488                 $last_contact = "0000-00-00 00:00:00";
489                 $last_failure = "0000-00-00 00:00:00";
490         }
491
492         $failure = false;
493         $orig_last_failure = $last_failure;
494
495         // Check if the page is accessible via SSL.
496         $server_url = str_replace("http://", "https://", $server_url);
497         $serverret = z_fetch_url($server_url."/.well-known/host-meta");
498
499         // Maybe the page is unencrypted only?
500         $xmlobj = @simplexml_load_string($serverret["body"],'SimpleXMLElement',0, "http://docs.oasis-open.org/ns/xri/xrd-1.0");
501         if (!$serverret["success"] OR ($serverret["body"] == "") OR (@sizeof($xmlobj) == 0) OR !is_object($xmlobj)) {
502                 $server_url = str_replace("https://", "http://", $server_url);
503                 $serverret = z_fetch_url($server_url."/.well-known/host-meta");
504
505                 $xmlobj = @simplexml_load_string($serverret["body"],'SimpleXMLElement',0, "http://docs.oasis-open.org/ns/xri/xrd-1.0");
506         }
507
508         if (!$serverret["success"] OR ($serverret["body"] == "") OR (sizeof($xmlobj) == 0) OR !is_object($xmlobj)) {
509                 $last_failure = datetime_convert();
510                 $failure = true;
511         } elseif ($network == NETWORK_DIASPORA)
512                 $last_contact = datetime_convert();
513
514         if (!$failure) {
515                 // Test for Statusnet
516                 // Will also return data for Friendica and GNU Social - but it will be overwritten later
517                 // The "not implemented" is a special treatment for really, really old Friendica versions
518                 $serverret = z_fetch_url($server_url."/api/statusnet/version.json");
519                 if ($serverret["success"] AND ($serverret["body"] != '{"error":"not implemented"}') AND ($serverret["body"] != '') AND (strlen($serverret["body"]) < 250)) {
520                         $platform = "StatusNet";
521                         $version = trim($serverret["body"], '"');
522                         $network = NETWORK_OSTATUS;
523                 }
524
525                 // Test for GNU Social
526                 $serverret = z_fetch_url($server_url."/api/gnusocial/version.json");
527                 if ($serverret["success"] AND ($serverret["body"] != '{"error":"not implemented"}') AND ($serverret["body"] != '') AND (strlen($serverret["body"]) < 250)) {
528                         $platform = "GNU Social";
529                         $version = trim($serverret["body"], '"');
530                         $network = NETWORK_OSTATUS;
531                 }
532
533                 $serverret = z_fetch_url($server_url."/api/statusnet/config.json");
534                 if ($serverret["success"]) {
535                         $data = json_decode($serverret["body"]);
536
537                         if (isset($data->site->server)) {
538                                 $last_contact = datetime_convert();
539
540                                 if (isset($data->site->redmatrix)) {
541                                         if (isset($data->site->redmatrix->PLATFORM_NAME))
542                                                 $platform = $data->site->redmatrix->PLATFORM_NAME;
543                                         elseif (isset($data->site->redmatrix->RED_PLATFORM))
544                                                 $platform = $data->site->redmatrix->RED_PLATFORM;
545
546                                         $version = $data->site->redmatrix->RED_VERSION;
547                                         $network = NETWORK_DIASPORA;
548                                 }
549                                 if (isset($data->site->friendica)) {
550                                         $platform = $data->site->friendica->FRIENDICA_PLATFORM;
551                                         $version = $data->site->friendica->FRIENDICA_VERSION;
552                                         $network = NETWORK_DFRN;
553                                 }
554
555                                 $site_name = $data->site->name;
556
557                                 $data->site->closed = poco_to_boolean($data->site->closed);
558                                 $data->site->private = poco_to_boolean($data->site->private);
559                                 $data->site->inviteonly = poco_to_boolean($data->site->inviteonly);
560
561                                 if (!$data->site->closed AND !$data->site->private and $data->site->inviteonly)
562                                         $register_policy = REGISTER_APPROVE;
563                                 elseif (!$data->site->closed AND !$data->site->private)
564                                         $register_policy = REGISTER_OPEN;
565                                 else
566                                         $register_policy = REGISTER_CLOSED;
567                         }
568                 }
569         }
570
571         // Query statistics.json. Optional package for Diaspora, Friendica and Redmatrix
572         if (!$failure) {
573                 $serverret = z_fetch_url($server_url."/statistics.json");
574                 if ($serverret["success"]) {
575                         $data = json_decode($serverret["body"]);
576                         if ($version == "")
577                                 $version = $data->version;
578
579                         $site_name = $data->name;
580
581                         if (isset($data->network) AND ($platform == ""))
582                                 $platform = $data->network;
583
584                         if ($data->registrations_open)
585                                 $register_policy = REGISTER_OPEN;
586                         else
587                                 $register_policy = REGISTER_CLOSED;
588
589                         if (isset($data->version))
590                                 $last_contact = datetime_convert();
591                 }
592         }
593
594         // Check for noscrape
595         // Friendica servers could be detected as OStatus servers
596         if (!$failure AND in_array($network, array(NETWORK_DFRN, NETWORK_OSTATUS))) {
597                 $serverret = z_fetch_url($server_url."/friendica/json");
598
599                 if ($serverret["success"]) {
600                         $data = json_decode($serverret["body"]);
601
602                         if (isset($data->version)) {
603                                 $last_contact = datetime_convert();
604                                 $network = NETWORK_DFRN;
605
606                                 $noscrape = $data->no_scrape_url;
607                                 $version = $data->version;
608                                 $site_name = $data->site_name;
609                                 $info = $data->info;
610                                 $register_policy_str = $data->register_policy;
611                                 $platform = $data->platform;
612
613                                 switch ($register_policy_str) {
614                                         case "REGISTER_CLOSED":
615                                                 $register_policy = REGISTER_CLOSED;
616                                                 break;
617                                         case "REGISTER_APPROVE":
618                                                 $register_policy = REGISTER_APPROVE;
619                                                 break;
620                                         case "REGISTER_OPEN":
621                                                 $register_policy = REGISTER_OPEN;
622                                                 break;
623                                 }
624                         }
625                 }
626         }
627
628         // Look for poco
629         if (!$failure) {
630                 $serverret = z_fetch_url($server_url."/poco");
631                 if ($serverret["success"]) {
632                         $data = json_decode($serverret["body"]);
633                         if (isset($data->totalResults)) {
634                                 $poco = $server_url."/poco";
635                                 $last_contact = datetime_convert();
636                         }
637                 }
638         }
639
640         if ($servers)
641                  q("UPDATE `gserver` SET `url` = '%s', `version` = '%s', `site_name` = '%s', `info` = '%s', `register_policy` = %d, `poco` = '%s', `noscrape` = '%s',
642                         `network` = '%s', `platform` = '%s', `last_contact` = '%s', `last_failure` = '%s' WHERE `nurl` = '%s'",
643                         dbesc($server_url),
644                         dbesc($version),
645                         dbesc($site_name),
646                         dbesc($info),
647                         intval($register_policy),
648                         dbesc($poco),
649                         dbesc($noscrape),
650                         dbesc($network),
651                         dbesc($platform),
652                         dbesc($last_contact),
653                         dbesc($last_failure),
654                         dbesc(normalise_link($server_url))
655                 );
656         else
657                 q("INSERT INTO `gserver` (`url`, `nurl`, `version`, `site_name`, `info`, `register_policy`, `poco`, `noscrape`, `network`, `platform`, `last_contact`)
658                                         VALUES ('%s', '%s', '%s', '%s', '%s', %d, '%s', '%s', '%s', '%s', '%s')",
659                                 dbesc($server_url),
660                                 dbesc(normalise_link($server_url)),
661                                 dbesc($version),
662                                 dbesc($site_name),
663                                 dbesc($info),
664                                 intval($register_policy),
665                                 dbesc($poco),
666                                 dbesc($noscrape),
667                                 dbesc($network),
668                                 dbesc($platform),
669                                 dbesc(datetime_convert())
670                 );
671 }
672
673 function poco_contact_from_body($body, $created, $cid, $uid) {
674         preg_replace_callback("/\[share(.*?)\].*?\[\/share\]/ism",
675                 function ($match) use ($created, $cid, $uid){
676                         return(sub_poco_from_share($match, $created, $cid, $uid));
677                 }, $body);
678 }
679
680 function sub_poco_from_share($share, $created, $cid, $uid) {
681         $profile = "";
682         preg_match("/profile='(.*?)'/ism", $share[1], $matches);
683         if ($matches[1] != "")
684                 $profile = $matches[1];
685
686         preg_match('/profile="(.*?)"/ism', $share[1], $matches);
687         if ($matches[1] != "")
688                 $profile = $matches[1];
689
690         if ($profile == "")
691                 return;
692
693         logger("prepare poco_check for profile ".$profile, LOGGER_DEBUG);
694         poco_check($profile, "", "", "", "", "", "", "", "", $created, 3, $cid, $uid);
695 }
696
697 function poco_store($item) {
698
699         // Isn't it public?
700         if ($item['private'])
701                 return;
702
703         // Or is it from a network where we don't store the global contacts?
704         if (!in_array($item["network"], array(NETWORK_DFRN, NETWORK_DIASPORA, NETWORK_OSTATUS, NETWORK_STATUSNET, "")))
705                 return;
706
707         // Is it a global copy?
708         $store_gcontact = ($item["uid"] == 0);
709
710         // Is it a comment on a global copy?
711         if (!$store_gcontact AND ($item["uri"] != $item["parent-uri"])) {
712                 $q = q("SELECT `id` FROM `item` WHERE `uri`='%s' AND `uid` = 0", $item["parent-uri"]);
713                 $store_gcontact = count($q);
714         }
715
716         if (!$store_gcontact)
717                 return;
718
719         // "3" means: We don't know this contact directly (Maybe a reshared item)
720         $generation = 3;
721         $network = "";
722         $profile_url = $item["author-link"];
723
724         // Is it a user from our server?
725         $q = q("SELECT `id` FROM `contact` WHERE `self` AND `nurl` = '%s' LIMIT 1",
726                 dbesc(normalise_link($item["author-link"])));
727         if (count($q)) {
728                 logger("Our user (generation 1): ".$item["author-link"], LOGGER_DEBUG);
729                 $generation = 1;
730                 $network = NETWORK_DFRN;
731         } else { // Is it a contact from a user on our server?
732                 $q = q("SELECT `network`, `url` FROM `contact` WHERE `uid` != 0 AND `network` != ''
733                         AND (`nurl` = '%s' OR `alias` IN ('%s', '%s')) AND `network` != '%s' LIMIT 1",
734                         dbesc(normalise_link($item["author-link"])),
735                         dbesc(normalise_link($item["author-link"])),
736                         dbesc($item["author-link"]),
737                         dbesc(NETWORK_STATUSNET));
738                 if (count($q)) {
739                         $generation = 2;
740                         $network = $q[0]["network"];
741                         $profile_url = $q[0]["url"];
742                         logger("Known contact (generation 2): ".$profile_url, LOGGER_DEBUG);
743                 }
744         }
745
746         if ($generation == 3)
747                 logger("Unknown contact (generation 3): ".$item["author-link"], LOGGER_DEBUG);
748
749         poco_check($profile_url, $item["author-name"], $network, $item["author-avatar"], "", "", "", "", "", $item["received"], $generation, $item["contact-id"], $item["uid"]);
750
751         // Maybe its a body with a shared item? Then extract a global contact from it.
752         poco_contact_from_body($item["body"], $item["received"], $item["contact-id"], $item["uid"]);
753 }
754
755 function count_common_friends($uid,$cid) {
756
757         $r = q("SELECT count(*) as `total`
758                 FROM `glink` INNER JOIN `gcontact` on `glink`.`gcid` = `gcontact`.`id`
759                 where `glink`.`cid` = %d and `glink`.`uid` = %d
760                 and `gcontact`.`nurl` in (select nurl from contact where uid = %d and self = 0 and blocked = 0 and hidden = 0 and id != %d ) ",
761                 intval($cid),
762                 intval($uid),
763                 intval($uid),
764                 intval($cid)
765         );
766
767 //      logger("count_common_friends: $uid $cid {$r[0]['total']}"); 
768         if(count($r))
769                 return $r[0]['total'];
770         return 0;
771
772 }
773
774
775 function common_friends($uid,$cid,$start = 0,$limit=9999,$shuffle = false) {
776
777         if($shuffle)
778                 $sql_extra = " order by rand() ";
779         else
780                 $sql_extra = " order by `gcontact`.`name` asc ";
781
782         $r = q("SELECT `gcontact`.*
783                 FROM `glink` INNER JOIN `gcontact` on `glink`.`gcid` = `gcontact`.`id`
784                 where `glink`.`cid` = %d and `glink`.`uid` = %d
785                 and `gcontact`.`nurl` in (select nurl from contact where uid = %d and self = 0 and blocked = 0 and hidden = 0 and id != %d ) 
786                 $sql_extra limit %d, %d",
787                 intval($cid),
788                 intval($uid),
789                 intval($uid),
790                 intval($cid),
791                 intval($start),
792                 intval($limit)
793         );
794
795         return $r;
796
797 }
798
799
800 function count_common_friends_zcid($uid,$zcid) {
801
802         $r = q("SELECT count(*) as `total`
803                 FROM `glink` INNER JOIN `gcontact` on `glink`.`gcid` = `gcontact`.`id`
804                 where `glink`.`zcid` = %d
805                 and `gcontact`.`nurl` in (select nurl from contact where uid = %d and self = 0 and blocked = 0 and hidden = 0 ) ",
806                 intval($zcid),
807                 intval($uid)
808         );
809
810         if(count($r))
811                 return $r[0]['total'];
812         return 0;
813
814 }
815
816 function common_friends_zcid($uid,$zcid,$start = 0, $limit = 9999,$shuffle = false) {
817
818         if($shuffle)
819                 $sql_extra = " order by rand() ";
820         else
821                 $sql_extra = " order by `gcontact`.`name` asc ";
822
823         $r = q("SELECT `gcontact`.*
824                 FROM `glink` INNER JOIN `gcontact` on `glink`.`gcid` = `gcontact`.`id`
825                 where `glink`.`zcid` = %d
826                 and `gcontact`.`nurl` in (select nurl from contact where uid = %d and self = 0 and blocked = 0 and hidden = 0 ) 
827                 $sql_extra limit %d, %d",
828                 intval($zcid),
829                 intval($uid),
830                 intval($start),
831                 intval($limit)
832         );
833
834         return $r;
835
836 }
837
838
839 function count_all_friends($uid,$cid) {
840
841         $r = q("SELECT count(*) as `total`
842                 FROM `glink` INNER JOIN `gcontact` on `glink`.`gcid` = `gcontact`.`id`
843                 where `glink`.`cid` = %d and `glink`.`uid` = %d ",
844                 intval($cid),
845                 intval($uid)
846         );
847
848         if(count($r))
849                 return $r[0]['total'];
850         return 0;
851
852 }
853
854
855 function all_friends($uid,$cid,$start = 0, $limit = 80) {
856
857         $r = q("SELECT `gcontact`.*
858                 FROM `glink` INNER JOIN `gcontact` on `glink`.`gcid` = `gcontact`.`id`
859                 where `glink`.`cid` = %d and `glink`.`uid` = %d
860                 order by `gcontact`.`name` asc LIMIT %d, %d ",
861                 intval($cid),
862                 intval($uid),
863                 intval($start),
864                 intval($limit)
865         );
866
867         return $r;
868 }
869
870
871
872 function suggestion_query($uid, $start = 0, $limit = 80) {
873
874         if(! $uid)
875                 return array();
876
877         $network = array(NETWORK_DFRN);
878
879         if (get_config('system','diaspora_enabled'))
880                 $network[] = NETWORK_DIASPORA;
881
882         if (!get_config('system','ostatus_disabled'))
883                 $network[] = NETWORK_OSTATUS;
884
885         $sql_network = implode("', '", $network);
886         //$sql_network = "'".$sql_network."', ''";
887         $sql_network = "'".$sql_network."'";
888
889         $r = q("SELECT count(glink.gcid) as `total`, gcontact.* from gcontact
890                 INNER JOIN glink on glink.gcid = gcontact.id
891                 where uid = %d and not gcontact.nurl in ( select nurl from contact where uid = %d )
892                 and not gcontact.name in ( select name from contact where uid = %d )
893                 and not gcontact.id in ( select gcid from gcign where uid = %d )
894                 AND `gcontact`.`updated` != '0000-00-00 00:00:00'
895                 AND `gcontact`.`last_contact` >= `gcontact`.`last_failure`
896                 AND `gcontact`.`network` IN (%s)
897                 group by glink.gcid order by gcontact.updated desc,total desc limit %d, %d ",
898                 intval($uid),
899                 intval($uid),
900                 intval($uid),
901                 intval($uid),
902                 $sql_network,
903                 intval($start),
904                 intval($limit)
905         );
906
907         if(count($r) && count($r) >= ($limit -1))
908                 return $r;
909
910         $r2 = q("SELECT gcontact.* from gcontact
911                 INNER JOIN glink on glink.gcid = gcontact.id
912                 where glink.uid = 0 and glink.cid = 0 and glink.zcid = 0 and not gcontact.nurl in ( select nurl from contact where uid = %d )
913                 and not gcontact.name in ( select name from contact where uid = %d )
914                 and not gcontact.id in ( select gcid from gcign where uid = %d )
915                 AND `gcontact`.`updated` != '0000-00-00 00:00:00'
916                 AND `gcontact`.`network` IN (%s)
917                 order by rand() limit %d, %d ",
918                 intval($uid),
919                 intval($uid),
920                 intval($uid),
921                 $sql_network,
922                 intval($start),
923                 intval($limit)
924         );
925
926         $list = array();
927         foreach ($r2 AS $suggestion)
928                 $list[$suggestion["nurl"]] = $suggestion;
929
930         foreach ($r AS $suggestion)
931                 $list[$suggestion["nurl"]] = $suggestion;
932
933         return $list;
934 }
935
936 function update_suggestions() {
937
938         $a = get_app();
939
940         $done = array();
941
942         poco_load(0,0,0,$a->get_baseurl() . '/poco');
943
944         $done[] = $a->get_baseurl() . '/poco';
945
946         if(strlen(get_config('system','directory_submit_url'))) {
947                 $x = fetch_url('http://dir.friendica.com/pubsites');
948                 if($x) {
949                         $j = json_decode($x);
950                         if($j->entries) {
951                                 foreach($j->entries as $entry) {
952                                         $url = $entry->url . '/poco';
953                                         if(! in_array($url,$done))
954                                                 poco_load(0,0,0,$entry->url . '/poco');
955                                 }
956                         }
957                 }
958         }
959
960         $r = q("select distinct(poco) as poco from contact where network = '%s'",
961                 dbesc(NETWORK_DFRN)
962         );
963
964         if(count($r)) {
965                 foreach($r as $rr) {
966                         $base = substr($rr['poco'],0,strrpos($rr['poco'],'/'));
967                         if(! in_array($base,$done))
968                                 poco_load(0,0,0,$base);
969                 }
970         }
971 }
972
973 function poco_discover() {
974
975         $last_update = date("c", time() - (60 * 60 * 24));
976
977         $r = q("SELECT `poco`, `nurl` FROM `gserver` WHERE `last_contact` > `last_failure` AND `poco` != '' AND `last_poco_query` < '%s' ORDER BY RAND()", dbesc($last_update));
978         if ($r)
979                 foreach ($r AS $server) {
980                         $url = $server["poco"]."/@global?fields=displayName,urls,photos,updated,network,aboutMe,currentLocation,tags,gender,generation";
981
982                         $retdata = z_fetch_url($url);
983                         if ($retdata["success"]) {
984                                 poco_discover_server(json_decode($retdata["body"]));
985                                 q("UPDATE `gserver` SET `last_poco_query` = '%s' WHERE `nurl` = '%s'", dbesc(datetime_convert()), dbesc($server["nurl"]));
986                                 break;
987                         } else
988                                 q("UPDATE `gserver` SET `last_poco_query` = '%s' WHERE `nurl` = '%s'", dbesc(datetime_convert()), dbesc($server["nurl"]));
989
990                 }
991 }
992
993 function poco_discover_server($data) {
994
995         foreach ($data->entry AS $entry) {
996                 $profile_url = '';
997                 $profile_photo = '';
998                 $connect_url = '';
999                 $name = '';
1000                 $network = '';
1001                 $updated = '0000-00-00 00:00:00';
1002                 $location = '';
1003                 $about = '';
1004                 $keywords = '';
1005                 $gender = '';
1006                 $generation = 0;
1007
1008                 $name = $entry->displayName;
1009
1010                 if(isset($entry->urls)) {
1011                         foreach($entry->urls as $url) {
1012                                 if($url->type == 'profile') {
1013                                         $profile_url = $url->value;
1014                                         continue;
1015                                 }
1016                                 if($url->type == 'webfinger') {
1017                                         $connect_url = str_replace('acct:' , '', $url->value);
1018                                         continue;
1019                                 }
1020                         }
1021                 }
1022                 if(isset($entry->photos)) {
1023                         foreach($entry->photos as $photo) {
1024                                 if($photo->type == 'profile') {
1025                                         $profile_photo = $photo->value;
1026                                         continue;
1027                                 }
1028                         }
1029                 }
1030
1031                 if(isset($entry->updated))
1032                         $updated = date("Y-m-d H:i:s", strtotime($entry->updated));
1033
1034                 if(isset($entry->network))
1035                         $network = $entry->network;
1036
1037                 if(isset($entry->currentLocation))
1038                         $location = $entry->currentLocation;
1039
1040                 if(isset($entry->aboutMe))
1041                         $about = html2bbcode($entry->aboutMe);
1042
1043                 if(isset($entry->gender))
1044                         $gender = $entry->gender;
1045
1046                 if(isset($entry->generation) AND ($entry->generation > 0))
1047                         $generation = ++$entry->generation;
1048
1049                 if(isset($entry->tags))
1050                         foreach($entry->tags as $tag)
1051                                 $keywords = implode(", ", $tag);
1052
1053                 if ($generation > 0)
1054                         poco_check($profile_url, $name, $network, $profile_photo, $about, $location, $gender, $keywords, $connect_url, $updated, $generation);
1055         }
1056 }
1057 ?>