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