]> git.mxchange.org Git - friendica.git/blob - include/socgraph.php
Poco: New fields for last contact and last failure of a contact.
[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
199         if (count($x)) {
200                 if (($network == "") AND ($x[0]["network"] != NETWORK_STATUSNET))
201                         $network = $x[0]["network"];
202
203                 if ($updated = "0000-00-00 00:00:00")
204                         $updated = $x[0]["updated"];
205
206                 $last_contact = $x[0]["last_contact"];
207                 $last_failure = $x[0]["last_failure"];
208         } else {
209                 $last_contact = "0000-00-00 00:00:00";
210                 $last_failure = "0000-00-00 00:00:00";
211         }
212
213         if (($network == "") OR ($name == "") OR ($profile_photo == "")) {
214                 require_once("include/Scrape.php");
215
216                 $data = probe_url($profile_url);
217                 $network = $data["network"];
218                 $name = $data["name"];
219                 $profile_url = $data["url"];
220                 $profile_photo = $data["photo"];
221         }
222
223         if (count($x) AND ($x[0]["network"] == "") AND ($network != "")) {
224                 q("UPDATE `gcontact` SET `network` = '%s' WHERE `nurl` = '%s'",
225                         dbesc($network),
226                         dbesc(normalise_link($profile_url))
227                 );
228         }
229
230         if (($name == "") OR ($profile_photo == ""))
231                 return $gcid;
232
233         if (!in_array($network, array(NETWORK_DFRN, NETWORK_OSTATUS, NETWORK_DIASPORA)))
234                 return $gcid;
235
236         logger("profile-check generation: ".$generation." Network: ".$network." URL: ".$profile_url." name: ".$name." avatar: ".$profile_photo, LOGGER_DEBUG);
237
238         if (poco_do_update($updated, $last_contact, $last_failure)) {
239                 $last_updated = poco_last_updated($profile_url);
240                 if ($last_updated) {
241                         $updated = $last_updated;
242                         $last_contact = datetime_convert();
243                         logger("Last updated for profile ".$profile_url.": ".$updated, LOGGER_DEBUG);
244
245                         if (count($x))
246                                 q("UPDATE `gcontact` SET `last_contact` = '%s' WHERE `nurl` = '%s'", dbesc($last_contact), dbesc(normalise_link($profile_url)));
247                 } else {
248                         $last_failure = datetime_convert();
249
250                         if (count($x))
251                                 q("UPDATE `gcontact` SET `last_failure` = '%s' WHERE `nurl` = '%s'", dbesc($last_failure), dbesc(normalise_link($profile_url)));
252                 }
253         }
254
255         if(count($x)) {
256                 $gcid = $x[0]['id'];
257
258                 if (($location == "") AND ($x[0]['location'] != ""))
259                         $location = $x[0]['location'];
260
261                 if (($about == "") AND ($x[0]['about'] != ""))
262                         $about = $x[0]['about'];
263
264                 if (($gender == "") AND ($x[0]['gender'] != ""))
265                         $gender = $x[0]['gender'];
266
267                 if (($keywords == "") AND ($x[0]['keywords'] != ""))
268                         $keywords = $x[0]['keywords'];
269
270                 if (($generation == 0) AND ($x[0]['generation'] > 0))
271                         $generation = $x[0]['generation'];
272
273                 if ($last_contact < $x[0]['last_contact'])
274                         $last_contact = $x[0]['last_contact'];
275
276                 if($x[0]['name'] != $name || $x[0]['photo'] != $profile_photo || $x[0]['updated'] < $updated) {
277                         q("UPDATE `gcontact` SET `name` = '%s', `network` = '%s', `photo` = '%s', `connect` = '%s', `url` = '%s',
278                                 `updated` = '%s', `location` = '%s', `about` = '%s', `keywords` = '%s', `gender` = '%s', `generation` = %d
279                                 WHERE (`generation` >= %d OR `generation` = 0) AND `nurl` = '%s'",
280                                 dbesc($name),
281                                 dbesc($network),
282                                 dbesc($profile_photo),
283                                 dbesc($connect_url),
284                                 dbesc($profile_url),
285                                 dbesc($updated),
286                                 dbesc($location),
287                                 dbesc($about),
288                                 dbesc($keywords),
289                                 dbesc($gender),
290                                 intval($generation),
291                                 intval($generation),
292                                 dbesc(normalise_link($profile_url))
293                         );
294                 }
295         } else {
296                 q("INSERT INTO `gcontact` (`name`,`network`, `url`,`nurl`,`photo`,`connect`, `updated`, `last_contact`, `last_failure`, `location`, `about`, `keywords`, `gender`, `generation`)
297                         VALUES ('%s', '%s', '%s', '%s', '%s','%s', '%s', '%s', '%s', '%s', '%s', %d)",
298                         dbesc($name),
299                         dbesc($network),
300                         dbesc($profile_url),
301                         dbesc(normalise_link($profile_url)),
302                         dbesc($profile_photo),
303                         dbesc($connect_url),
304                         dbesc($updated),
305                         dbesc($last_contact),
306                         dbesc($last_failure),
307                         dbesc($location),
308                         dbesc($about),
309                         dbesc($keywords),
310                         dbesc($gender),
311                         intval($generation)
312                 );
313                 $x = q("SELECT * FROM `gcontact` WHERE `nurl` = '%s' LIMIT 1",
314                         dbesc(normalise_link($profile_url))
315                 );
316                 if(count($x))
317                         $gcid = $x[0]['id'];
318         }
319
320         if(! $gcid)
321                 return $gcid;
322
323         $r = q("SELECT * FROM `glink` WHERE `cid` = %d AND `uid` = %d AND `gcid` = %d AND `zcid` = %d LIMIT 1",
324                 intval($cid),
325                 intval($uid),
326                 intval($gcid),
327                 intval($zcid)
328         );
329         if(! count($r)) {
330                 q("INSERT INTO `glink` (`cid`,`uid`,`gcid`,`zcid`, `updated`) VALUES (%d,%d,%d,%d, '%s') ",
331                         intval($cid),
332                         intval($uid),
333                         intval($gcid),
334                         intval($zcid),
335                         dbesc(datetime_convert())
336                 );
337         } else {
338                 q("UPDATE `glink` SET `updated` = '%s' WHERE `cid` = %d AND `uid` = %d AND `gcid` = %d AND `zcid` = %d",
339                         dbesc(datetime_convert()),
340                         intval($cid),
341                         intval($uid),
342                         intval($gcid),
343                         intval($zcid)
344                 );
345         }
346
347         // For unknown reasons there are sometimes duplicates
348         q("DELETE FROM `gcontact` WHERE `nurl` = '%s' AND `id` != %d AND
349                 NOT EXISTS (SELECT `gcid` FROM `glink` WHERE `gcid` = `gcontact`.`id`)",
350                 dbesc(normalise_link($profile_url)),
351                 intval($gcid)
352         );
353
354         return $gcid;
355 }
356
357 function poco_last_updated($profile) {
358         $data = probe_url($profile);
359
360         if (($data["poll"] == "") OR ($data["network"] == NETWORK_FEED))
361                 return false;
362
363         $feedret = z_fetch_url($data["poll"]);
364
365         if (!$feedret["success"])
366                 return false;
367
368         $doc = new DOMDocument();
369         @$doc->loadXML($feedret["body"]);
370
371         $xpath = new DomXPath($doc);
372         $xpath->registerNamespace('atom', "http://www.w3.org/2005/Atom");
373
374         $entries = $xpath->query('/atom:feed/atom:entry');
375
376         $last_updated = "";
377
378         foreach ($entries AS $entry) {
379                 $published = $xpath->query('atom:published/text()', $entry)->item(0)->nodeValue;
380                 $updated = $xpath->query('atom:updated/text()', $entry)->item(0)->nodeValue;
381
382                 if ($last_updated < $published)
383                         $last_updated = $published;
384
385                 if ($last_updated < $updated)
386                         $last_updated = $updated;
387         }
388
389         // Maybe there aren't any entries. Then check if it is a valid feed
390         if ($last_updated == "")
391                 if ($xpath->query('/atom:feed')->length > 0)
392                         $last_updated = "0000-00-00 00:00:00";
393
394         return($last_updated);
395 }
396
397 function poco_do_update($updated, $last_contact, $last_failure) {
398         $now = strtotime(datetime_convert());
399
400         if ($updated > $last_contact)
401                 $contact_time = strtotime($updated);
402         else
403                 $contact_time = strtotime($last_contact);
404
405         $failure_time = strtotime($last_failure);
406
407         // If the last contact was less than 24 hours then don't update
408         if (($now - $contact_time) < (60 * 60 * 24))
409                 return false;
410
411         // If the last failure was less than 24 hours then don't update
412         if (($now - $failure_time) < (60 * 60 * 24))
413                 return false;
414
415         // If the last contact was less than a week ago and the last failure is older than a week then don't update
416         if ((($now - $contact_time) < (60 * 60 * 24 * 7)) AND ($contact_time > $failure_time))
417                 return false;
418
419         // If the last contact time was more than a week ago, then only try once a week
420         if (($now - $contact_time) > (60 * 60 * 24 * 7) AND ($now - $failure_time) < (60 * 60 * 24 * 7))
421                 return false;
422
423         // If the last contact time was more than a month ago, then only try once a month
424         if (($now - $contact_time) > (60 * 60 * 24 * 30) AND ($now - $failure_time) < (60 * 60 * 24 * 30))
425                 return false;
426
427         return true;
428 }
429
430 function poco_contact_from_body($body, $created, $cid, $uid) {
431         preg_replace_callback("/\[share(.*?)\].*?\[\/share\]/ism",
432                 function ($match) use ($created, $cid, $uid){
433                         return(sub_poco_from_share($match, $created, $cid, $uid));
434                 }, $body);
435 }
436
437 function sub_poco_from_share($share, $created, $cid, $uid) {
438         $profile = "";
439         preg_match("/profile='(.*?)'/ism", $share[1], $matches);
440         if ($matches[1] != "")
441                 $profile = $matches[1];
442
443         preg_match('/profile="(.*?)"/ism', $share[1], $matches);
444         if ($matches[1] != "")
445                 $profile = $matches[1];
446
447         if ($profile == "")
448                 return;
449
450         logger("prepare poco_check for profile ".$profile, LOGGER_DEBUG);
451         poco_check($profile, "", "", "", "", "", "", "", "", $created, 3, $cid, $uid);
452 }
453
454 function poco_store($item) {
455
456         // Isn't it public?
457         if ($item['private'])
458                 return;
459
460         // Or is it from a network where we don't store the global contacts?
461         if (!in_array($item["network"], array(NETWORK_DFRN, NETWORK_DIASPORA, NETWORK_OSTATUS, NETWORK_STATUSNET, "")))
462                 return;
463
464         // Is it a global copy?
465         $store_gcontact = ($item["uid"] == 0);
466
467         // Is it a comment on a global copy?
468         if (!$store_gcontact AND ($item["uri"] != $item["parent-uri"])) {
469                 $q = q("SELECT `id` FROM `item` WHERE `uri`='%s' AND `uid` = 0", $item["parent-uri"]);
470                 $store_gcontact = count($q);
471         }
472
473         if (!$store_gcontact)
474                 return;
475
476         // "3" means: We don't know this contact directly (Maybe a reshared item)
477         $generation = 3;
478         $network = "";
479         $profile_url = $item["author-link"];
480
481         // Is it a user from our server?
482         $q = q("SELECT `id` FROM `contact` WHERE `self` AND `nurl` = '%s' LIMIT 1",
483                 dbesc(normalise_link($item["author-link"])));
484         if (count($q)) {
485                 logger("Our user (generation 1): ".$item["author-link"], LOGGER_DEBUG);
486                 $generation = 1;
487                 $network = NETWORK_DFRN;
488         } else { // Is it a contact from a user on our server?
489                 $q = q("SELECT `network`, `url` FROM `contact` WHERE `uid` != 0 AND `network` != ''
490                         AND (`nurl` = '%s' OR `alias` IN ('%s', '%s')) AND `network` != '%s' LIMIT 1",
491                         dbesc(normalise_link($item["author-link"])),
492                         dbesc(normalise_link($item["author-link"])),
493                         dbesc($item["author-link"]),
494                         dbesc(NETWORK_STATUSNET));
495                 if (count($q)) {
496                         $generation = 2;
497                         $network = $q[0]["network"];
498                         $profile_url = $q[0]["url"];
499                         logger("Known contact (generation 2): ".$profile_url, LOGGER_DEBUG);
500                 }
501         }
502
503         if ($generation == 3)
504                 logger("Unknown contact (generation 3): ".$item["author-link"], LOGGER_DEBUG);
505
506         poco_check($profile_url, $item["author-name"], $network, $item["author-avatar"], "", "", "", "", "", $item["received"], $generation, $item["contact-id"], $item["uid"]);
507
508         // Maybe its a body with a shared item? Then extract a global contact from it.
509         poco_contact_from_body($item["body"], $item["received"], $item["contact-id"], $item["uid"]);
510 }
511
512 function count_common_friends($uid,$cid) {
513
514         $r = q("SELECT count(*) as `total`
515                 FROM `glink` INNER JOIN `gcontact` on `glink`.`gcid` = `gcontact`.`id`
516                 where `glink`.`cid` = %d and `glink`.`uid` = %d
517                 and `gcontact`.`nurl` in (select nurl from contact where uid = %d and self = 0 and blocked = 0 and hidden = 0 and id != %d ) ",
518                 intval($cid),
519                 intval($uid),
520                 intval($uid),
521                 intval($cid)
522         );
523
524 //      logger("count_common_friends: $uid $cid {$r[0]['total']}"); 
525         if(count($r))
526                 return $r[0]['total'];
527         return 0;
528
529 }
530
531
532 function common_friends($uid,$cid,$start = 0,$limit=9999,$shuffle = false) {
533
534         if($shuffle)
535                 $sql_extra = " order by rand() ";
536         else
537                 $sql_extra = " order by `gcontact`.`name` asc ";
538
539         $r = q("SELECT `gcontact`.*
540                 FROM `glink` INNER JOIN `gcontact` on `glink`.`gcid` = `gcontact`.`id`
541                 where `glink`.`cid` = %d and `glink`.`uid` = %d
542                 and `gcontact`.`nurl` in (select nurl from contact where uid = %d and self = 0 and blocked = 0 and hidden = 0 and id != %d ) 
543                 $sql_extra limit %d, %d",
544                 intval($cid),
545                 intval($uid),
546                 intval($uid),
547                 intval($cid),
548                 intval($start),
549                 intval($limit)
550         );
551
552         return $r;
553
554 }
555
556
557 function count_common_friends_zcid($uid,$zcid) {
558
559         $r = q("SELECT count(*) as `total`
560                 FROM `glink` INNER JOIN `gcontact` on `glink`.`gcid` = `gcontact`.`id`
561                 where `glink`.`zcid` = %d
562                 and `gcontact`.`nurl` in (select nurl from contact where uid = %d and self = 0 and blocked = 0 and hidden = 0 ) ",
563                 intval($zcid),
564                 intval($uid)
565         );
566
567         if(count($r))
568                 return $r[0]['total'];
569         return 0;
570
571 }
572
573 function common_friends_zcid($uid,$zcid,$start = 0, $limit = 9999,$shuffle = false) {
574
575         if($shuffle)
576                 $sql_extra = " order by rand() ";
577         else
578                 $sql_extra = " order by `gcontact`.`name` asc ";
579
580         $r = q("SELECT `gcontact`.*
581                 FROM `glink` INNER JOIN `gcontact` on `glink`.`gcid` = `gcontact`.`id`
582                 where `glink`.`zcid` = %d
583                 and `gcontact`.`nurl` in (select nurl from contact where uid = %d and self = 0 and blocked = 0 and hidden = 0 ) 
584                 $sql_extra limit %d, %d",
585                 intval($zcid),
586                 intval($uid),
587                 intval($start),
588                 intval($limit)
589         );
590
591         return $r;
592
593 }
594
595
596 function count_all_friends($uid,$cid) {
597
598         $r = q("SELECT count(*) as `total`
599                 FROM `glink` INNER JOIN `gcontact` on `glink`.`gcid` = `gcontact`.`id`
600                 where `glink`.`cid` = %d and `glink`.`uid` = %d ",
601                 intval($cid),
602                 intval($uid)
603         );
604
605         if(count($r))
606                 return $r[0]['total'];
607         return 0;
608
609 }
610
611
612 function all_friends($uid,$cid,$start = 0, $limit = 80) {
613
614         $r = q("SELECT `gcontact`.*
615                 FROM `glink` INNER JOIN `gcontact` on `glink`.`gcid` = `gcontact`.`id`
616                 where `glink`.`cid` = %d and `glink`.`uid` = %d
617                 order by `gcontact`.`name` asc LIMIT %d, %d ",
618                 intval($cid),
619                 intval($uid),
620                 intval($start),
621                 intval($limit)
622         );
623
624         return $r;
625 }
626
627
628
629 function suggestion_query($uid, $start = 0, $limit = 80) {
630
631         if(! $uid)
632                 return array();
633
634         $network = array(NETWORK_DFRN);
635
636         if (get_config('system','diaspora_enabled'))
637                 $network[] = NETWORK_DIASPORA;
638
639         if (!get_config('system','ostatus_disabled'))
640                 $network[] = NETWORK_OSTATUS;
641
642         $sql_network = implode("', '", $network);
643         //$sql_network = "'".$sql_network."', ''";
644         $sql_network = "'".$sql_network."'";
645
646         $r = q("SELECT count(glink.gcid) as `total`, gcontact.* from gcontact
647                 INNER JOIN glink on glink.gcid = gcontact.id
648                 where uid = %d and not gcontact.nurl in ( select nurl from contact where uid = %d )
649                 and not gcontact.name in ( select name from contact where uid = %d )
650                 and not gcontact.id in ( select gcid from gcign where uid = %d )
651                 AND `gcontact`.`updated` != '0000-00-00 00:00:00'
652                 AND `gcontact`.`network` IN (%s)
653                 group by glink.gcid order by gcontact.updated desc,total desc limit %d, %d ",
654                 intval($uid),
655                 intval($uid),
656                 intval($uid),
657                 intval($uid),
658                 $sql_network,
659                 intval($start),
660                 intval($limit)
661         );
662
663         if(count($r) && count($r) >= ($limit -1))
664                 return $r;
665
666         $r2 = q("SELECT gcontact.* from gcontact
667                 INNER JOIN glink on glink.gcid = gcontact.id
668                 where glink.uid = 0 and glink.cid = 0 and glink.zcid = 0 and not gcontact.nurl in ( select nurl from contact where uid = %d )
669                 and not gcontact.name in ( select name from contact where uid = %d )
670                 and not gcontact.id in ( select gcid from gcign where uid = %d )
671                 AND `gcontact`.`updated` != '0000-00-00 00:00:00'
672                 AND `gcontact`.`network` IN (%s)
673                 order by rand() limit %d, %d ",
674                 intval($uid),
675                 intval($uid),
676                 intval($uid),
677                 $sql_network,
678                 intval($start),
679                 intval($limit)
680         );
681
682         $list = array();
683         foreach ($r2 AS $suggestion)
684                 $list[$suggestion["nurl"]] = $suggestion;
685
686         foreach ($r AS $suggestion)
687                 $list[$suggestion["nurl"]] = $suggestion;
688
689         return $list;
690 }
691
692 function update_suggestions() {
693
694         $a = get_app();
695
696         $done = array();
697
698         poco_load(0,0,0,$a->get_baseurl() . '/poco');
699
700         $done[] = $a->get_baseurl() . '/poco';
701
702         if(strlen(get_config('system','directory_submit_url'))) {
703                 $x = fetch_url('http://dir.friendica.com/pubsites');
704                 if($x) {
705                         $j = json_decode($x);
706                         if($j->entries) {
707                                 foreach($j->entries as $entry) {
708                                         $url = $entry->url . '/poco';
709                                         if(! in_array($url,$done))
710                                                 poco_load(0,0,0,$entry->url . '/poco');
711                                 }
712                         }
713                 }
714         }
715
716         $r = q("select distinct(poco) as poco from contact where network = '%s'",
717                 dbesc(NETWORK_DFRN)
718         );
719
720         if(count($r)) {
721                 foreach($r as $rr) {
722                         $base = substr($rr['poco'],0,strrpos($rr['poco'],'/'));
723                         if(! in_array($base,$done))
724                                 poco_load(0,0,0,$base);
725                 }
726         }
727 }