]> git.mxchange.org Git - friendica.git/blob - src/Model/GContact.php
Add 'addon' folder as 'Friendica\Addon' namespace for autoload
[friendica.git] / src / Model / GContact.php
1 <?php
2
3 /**
4  * @file src/Model/GlobalContact.php
5  * @brief This file includes the GlobalContact class with directory related functions
6  */
7 namespace Friendica\Model;
8
9 use Exception;
10 use Friendica\Core\Config;
11 use Friendica\Core\Logger;
12 use Friendica\Core\Protocol;
13 use Friendica\Core\System;
14 use Friendica\Core\Worker;
15 use Friendica\Database\DBA;
16 use Friendica\Network\Probe;
17 use Friendica\Protocol\PortableContact;
18 use Friendica\Util\DateTimeFormat;
19 use Friendica\Util\Network;
20 use Friendica\Util\Strings;
21
22 /**
23  * @brief This class handles GlobalContact related functions
24  */
25 class GContact
26 {
27         /**
28          * @brief Search global contact table by nick or name
29          *
30          * @param string $search Name or nick
31          * @param string $mode   Search mode (e.g. "community")
32          *
33          * @return array with search results
34          */
35         public static function searchByName($search, $mode = '')
36         {
37                 if (empty($search)) {
38                         return [];
39                 }
40
41                 // check supported networks
42                 if (Config::get('system', 'diaspora_enabled')) {
43                         $diaspora = Protocol::DIASPORA;
44                 } else {
45                         $diaspora = Protocol::DFRN;
46                 }
47
48                 if (!Config::get('system', 'ostatus_disabled')) {
49                         $ostatus = Protocol::OSTATUS;
50                 } else {
51                         $ostatus = Protocol::DFRN;
52                 }
53
54                 // check if we search only communities or every contact
55                 if ($mode === "community") {
56                         $extra_sql = " AND `community`";
57                 } else {
58                         $extra_sql = "";
59                 }
60
61                 $search .= "%";
62
63                 $results = DBA::p("SELECT `nurl` FROM `gcontact`
64                         WHERE NOT `hide` AND `network` IN (?, ?, ?, ?) AND
65                                 ((`last_contact` >= `last_failure`) OR (`updated` >= `last_failure`)) AND
66                                 (`addr` LIKE ? OR `name` LIKE ? OR `nick` LIKE ?) $extra_sql
67                                 GROUP BY `nurl` ORDER BY `nurl` DESC LIMIT 1000",
68                         Protocol::DFRN, Protocol::ACTIVITYPUB, $ostatus, $diaspora, $search, $search, $search
69                 );
70
71                 $gcontacts = [];
72                 while ($result = DBA::fetch($results)) {
73                         $urlparts = parse_url($result["nurl"]);
74
75                         // Ignore results that look strange.
76                         // For historic reasons the gcontact table does contain some garbage.
77                         if (!empty($urlparts['query']) || !empty($urlparts['fragment'])) {
78                                 continue;
79                         }
80
81                         $gcontacts[] = Contact::getDetailsByURL($result["nurl"], local_user());
82                 }
83                 return $gcontacts;
84         }
85
86         /**
87          * @brief Link the gcontact entry with user, contact and global contact
88          *
89          * @param integer $gcid Global contact ID
90          * @param integer $uid  User ID
91          * @param integer $cid  Contact ID
92          * @param integer $zcid Global Contact ID
93          * @return void
94          */
95         public static function link($gcid, $uid = 0, $cid = 0, $zcid = 0)
96         {
97                 if ($gcid <= 0) {
98                         return;
99                 }
100
101                 $condition = ['cid' => $cid, 'uid' => $uid, 'gcid' => $gcid, 'zcid' => $zcid];
102                 DBA::update('glink', ['updated' => DateTimeFormat::utcNow()], $condition, true);
103         }
104
105         /**
106          * @brief Sanitize the given gcontact data
107          *
108          * @param array $gcontact array with gcontact data
109          * @throw Exception
110          *
111          * Generation:
112          *  0: No definition
113          *  1: Profiles on this server
114          *  2: Contacts of profiles on this server
115          *  3: Contacts of contacts of profiles on this server
116          *  4: ...
117          * @return array $gcontact
118          */
119         public static function sanitize($gcontact)
120         {
121                 if ($gcontact['url'] == "") {
122                         throw new Exception('URL is empty');
123                 }
124
125                 $urlparts = parse_url($gcontact['url']);
126                 if (!isset($urlparts["scheme"])) {
127                         throw new Exception("This (".$gcontact['url'].") doesn't seem to be an url.");
128                 }
129
130                 if (in_array($urlparts["host"], ["twitter.com", "identi.ca"])) {
131                         throw new Exception('Contact from a non federated network ignored. ('.$gcontact['url'].')');
132                 }
133
134                 // Don't store the statusnet connector as network
135                 // We can't simply set this to Protocol::OSTATUS since the connector could have fetched posts from friendica as well
136                 if ($gcontact['network'] == Protocol::STATUSNET) {
137                         $gcontact['network'] = "";
138                 }
139
140                 // Assure that there are no parameter fragments in the profile url
141                 if (in_array($gcontact['network'], [Protocol::ACTIVITYPUB, Protocol::DFRN, Protocol::DIASPORA, Protocol::OSTATUS, ""])) {
142                         $gcontact['url'] = self::cleanContactUrl($gcontact['url']);
143                 }
144
145                 $alternate = PortableContact::alternateOStatusUrl($gcontact['url']);
146
147                 // The global contacts should contain the original picture, not the cached one
148                 if (($gcontact['generation'] != 1) && stristr(Strings::normaliseLink($gcontact['photo']), Strings::normaliseLink(System::baseUrl()."/photo/"))) {
149                         $gcontact['photo'] = "";
150                 }
151
152                 if (!isset($gcontact['network'])) {
153                         $condition = ["`uid` = 0 AND `nurl` = ? AND `network` != '' AND `network` != ?",
154                                 Strings::normaliseLink($gcontact['url']), Protocol::STATUSNET];
155                         $contact = DBA::selectFirst('contact', ['network'], $condition);
156                         if (DBA::isResult($contact)) {
157                                 $gcontact['network'] = $contact["network"];
158                         }
159
160                         if (($gcontact['network'] == "") || ($gcontact['network'] == Protocol::OSTATUS)) {
161                                 $condition = ["`uid` = 0 AND `alias` IN (?, ?) AND `network` != '' AND `network` != ?",
162                                         $gcontact['url'], Strings::normaliseLink($gcontact['url']), Protocol::STATUSNET];
163                                 $contact = DBA::selectFirst('contact', ['network'], $condition);
164                                 if (DBA::isResult($contact)) {
165                                         $gcontact['network'] = $contact["network"];
166                                 }
167                         }
168                 }
169
170                 $gcontact['server_url'] = '';
171                 $gcontact['network'] = '';
172
173                 $fields = ['network', 'updated', 'server_url', 'url', 'addr'];
174                 $gcnt = DBA::selectFirst('gcontact', $fields, ['nurl' => Strings::normaliseLink($gcontact['url'])]);
175                 if (DBA::isResult($gcnt)) {
176                         if (!isset($gcontact['network']) && ($gcnt["network"] != Protocol::STATUSNET)) {
177                                 $gcontact['network'] = $gcnt["network"];
178                         }
179                         if ($gcontact['updated'] <= DBA::NULL_DATETIME) {
180                                 $gcontact['updated'] = $gcnt["updated"];
181                         }
182                         if (!isset($gcontact['server_url']) && (Strings::normaliseLink($gcnt["server_url"]) != Strings::normaliseLink($gcnt["url"]))) {
183                                 $gcontact['server_url'] = $gcnt["server_url"];
184                         }
185                         if (!isset($gcontact['addr'])) {
186                                 $gcontact['addr'] = $gcnt["addr"];
187                         }
188                 }
189
190                 if ((!isset($gcontact['network']) || !isset($gcontact['name']) || !isset($gcontact['addr']) || !isset($gcontact['photo']) || !isset($gcontact['server_url']) || $alternate)
191                         && PortableContact::reachable($gcontact['url'], $gcontact['server_url'], $gcontact['network'], false)
192                 ) {
193                         $data = Probe::uri($gcontact['url']);
194
195                         if ($data["network"] == Protocol::PHANTOM) {
196                                 throw new Exception('Probing for URL '.$gcontact['url'].' failed');
197                         }
198
199                         $orig_profile = $gcontact['url'];
200
201                         $gcontact["server_url"] = $data["baseurl"];
202
203                         $gcontact = array_merge($gcontact, $data);
204
205                         if ($alternate && ($gcontact['network'] == Protocol::OSTATUS)) {
206                                 // Delete the old entry - if it exists
207                                 if (DBA::exists('gcontact', ['nurl' => Strings::normaliseLink($orig_profile)])) {
208                                         DBA::delete('gcontact', ['nurl' => Strings::normaliseLink($orig_profile)]);
209                                 }
210                         }
211                 }
212
213                 if (!isset($gcontact['name']) || !isset($gcontact['photo'])) {
214                         throw new Exception('No name and photo for URL '.$gcontact['url']);
215                 }
216
217                 if (!in_array($gcontact['network'], [Protocol::ACTIVITYPUB, Protocol::DFRN, Protocol::OSTATUS, Protocol::DIASPORA])) {
218                         throw new Exception('No federated network ('.$gcontact['network'].') detected for URL '.$gcontact['url']);
219                 }
220
221                 if (!isset($gcontact['server_url'])) {
222                         // We check the server url to be sure that it is a real one
223                         $server_url = PortableContact::detectServer($gcontact['url']);
224
225                         // We are now sure that it is a correct URL. So we use it in the future
226                         if ($server_url != "") {
227                                 $gcontact['server_url'] = $server_url;
228                         }
229                 }
230
231                 // The server URL doesn't seem to be valid, so we don't store it.
232                 if (!PortableContact::checkServer($gcontact['server_url'], $gcontact['network'])) {
233                         $gcontact['server_url'] = "";
234                 }
235
236                 return $gcontact;
237         }
238
239         /**
240          * @param integer $uid id
241          * @param integer $cid id
242          * @return integer
243          */
244         public static function countCommonFriends($uid, $cid)
245         {
246                 $r = q(
247                         "SELECT count(*) as `total`
248                         FROM `glink` INNER JOIN `gcontact` on `glink`.`gcid` = `gcontact`.`id`
249                         WHERE `glink`.`cid` = %d AND `glink`.`uid` = %d AND
250                         ((`gcontact`.`last_contact` >= `gcontact`.`last_failure`) OR
251                         (`gcontact`.`updated` >= `gcontact`.`last_failure`))
252                         AND `gcontact`.`nurl` IN (select nurl from contact where uid = %d and self = 0 and blocked = 0 and hidden = 0 and id != %d ) ",
253                         intval($cid),
254                         intval($uid),
255                         intval($uid),
256                         intval($cid)
257                 );
258
259                 // Logger::log("countCommonFriends: $uid $cid {$r[0]['total']}");
260                 if (DBA::isResult($r)) {
261                         return $r[0]['total'];
262                 }
263                 return 0;
264         }
265
266         /**
267          * @param integer $uid  id
268          * @param integer $zcid zcid
269          * @return integer
270          */
271         public static function countCommonFriendsZcid($uid, $zcid)
272         {
273                 $r = q(
274                         "SELECT count(*) as `total`
275                         FROM `glink` INNER JOIN `gcontact` on `glink`.`gcid` = `gcontact`.`id`
276                         where `glink`.`zcid` = %d
277                         and `gcontact`.`nurl` in (select nurl from contact where uid = %d and self = 0 and blocked = 0 and hidden = 0 ) ",
278                         intval($zcid),
279                         intval($uid)
280                 );
281
282                 if (DBA::isResult($r)) {
283                         return $r[0]['total'];
284                 }
285
286                 return 0;
287         }
288
289         /**
290          * @param integer $uid     user
291          * @param integer $cid     cid
292          * @param integer $start   optional, default 0
293          * @param integer $limit   optional, default 9999
294          * @param boolean $shuffle optional, default false
295          * @return object
296          */
297         public static function commonFriends($uid, $cid, $start = 0, $limit = 9999, $shuffle = false)
298         {
299                 if ($shuffle) {
300                         $sql_extra = " order by rand() ";
301                 } else {
302                         $sql_extra = " order by `gcontact`.`name` asc ";
303                 }
304
305                 $r = q(
306                         "SELECT `gcontact`.*, `contact`.`id` AS `cid`
307                         FROM `glink`
308                         INNER JOIN `gcontact` ON `glink`.`gcid` = `gcontact`.`id`
309                         INNER JOIN `contact` ON `gcontact`.`nurl` = `contact`.`nurl`
310                         WHERE `glink`.`cid` = %d and `glink`.`uid` = %d
311                                 AND `contact`.`uid` = %d AND `contact`.`self` = 0 AND `contact`.`blocked` = 0
312                                 AND `contact`.`hidden` = 0 AND `contact`.`id` != %d
313                                 AND ((`gcontact`.`last_contact` >= `gcontact`.`last_failure`) OR (`gcontact`.`updated` >= `gcontact`.`last_failure`))
314                                 $sql_extra LIMIT %d, %d",
315                         intval($cid),
316                         intval($uid),
317                         intval($uid),
318                         intval($cid),
319                         intval($start),
320                         intval($limit)
321                 );
322
323                 /// @TODO Check all calling-findings of this function if they properly use DBA::isResult()
324                 return $r;
325         }
326
327         /**
328          * @param integer $uid     user
329          * @param integer $zcid    zcid
330          * @param integer $start   optional, default 0
331          * @param integer $limit   optional, default 9999
332          * @param boolean $shuffle optional, default false
333          * @return object
334          */
335         public static function commonFriendsZcid($uid, $zcid, $start = 0, $limit = 9999, $shuffle = false)
336         {
337                 if ($shuffle) {
338                         $sql_extra = " order by rand() ";
339                 } else {
340                         $sql_extra = " order by `gcontact`.`name` asc ";
341                 }
342
343                 $r = q(
344                         "SELECT `gcontact`.*
345                         FROM `glink` INNER JOIN `gcontact` on `glink`.`gcid` = `gcontact`.`id`
346                         where `glink`.`zcid` = %d
347                         and `gcontact`.`nurl` in (select nurl from contact where uid = %d and self = 0 and blocked = 0 and hidden = 0 )
348                         $sql_extra limit %d, %d",
349                         intval($zcid),
350                         intval($uid),
351                         intval($start),
352                         intval($limit)
353                 );
354
355                 /// @TODO Check all calling-findings of this function if they properly use DBA::isResult()
356                 return $r;
357         }
358
359         /**
360          * @param integer $uid user
361          * @param integer $cid cid
362          * @return integer
363          */
364         public static function countAllFriends($uid, $cid)
365         {
366                 $r = q(
367                         "SELECT count(*) as `total`
368                         FROM `glink` INNER JOIN `gcontact` on `glink`.`gcid` = `gcontact`.`id`
369                         where `glink`.`cid` = %d and `glink`.`uid` = %d AND
370                         ((`gcontact`.`last_contact` >= `gcontact`.`last_failure`) OR (`gcontact`.`updated` >= `gcontact`.`last_failure`))",
371                         intval($cid),
372                         intval($uid)
373                 );
374
375                 if (DBA::isResult($r)) {
376                         return $r[0]['total'];
377                 }
378
379                 return 0;
380         }
381
382         /**
383          * @param integer $uid   user
384          * @param integer $cid   cid
385          * @param integer $start optional, default 0
386          * @param integer $limit optional, default 80
387          * @return array
388          */
389         public static function allFriends($uid, $cid, $start = 0, $limit = 80)
390         {
391                 $r = q(
392                         "SELECT `gcontact`.*, `contact`.`id` AS `cid`
393                         FROM `glink`
394                         INNER JOIN `gcontact` on `glink`.`gcid` = `gcontact`.`id`
395                         LEFT JOIN `contact` ON `contact`.`nurl` = `gcontact`.`nurl` AND `contact`.`uid` = %d
396                         WHERE `glink`.`cid` = %d AND `glink`.`uid` = %d AND
397                         ((`gcontact`.`last_contact` >= `gcontact`.`last_failure`) OR (`gcontact`.`updated` >= `gcontact`.`last_failure`))
398                         ORDER BY `gcontact`.`name` ASC LIMIT %d, %d ",
399                         intval($uid),
400                         intval($cid),
401                         intval($uid),
402                         intval($start),
403                         intval($limit)
404                 );
405
406                 /// @TODO Check all calling-findings of this function if they properly use DBA::isResult()
407                 return $r;
408         }
409
410         /**
411          * @param object  $uid   user
412          * @param integer $start optional, default 0
413          * @param integer $limit optional, default 80
414          * @return array
415          */
416         public static function suggestionQuery($uid, $start = 0, $limit = 80)
417         {
418                 if (!$uid) {
419                         return [];
420                 }
421
422                 /*
423                 * Uncommented because the result of the queries are to big to store it in the cache.
424                 * We need to decide if we want to change the db column type or if we want to delete it.
425                 */
426                 //$list = Cache::get("suggestion_query:".$uid.":".$start.":".$limit);
427                 //if (!is_null($list)) {
428                 //      return $list;
429                 //}
430
431                 $network = [Protocol::DFRN, Protocol::ACTIVITYPUB];
432
433                 if (Config::get('system', 'diaspora_enabled')) {
434                         $network[] = Protocol::DIASPORA;
435                 }
436
437                 if (!Config::get('system', 'ostatus_disabled')) {
438                         $network[] = Protocol::OSTATUS;
439                 }
440
441                 $sql_network = implode("', '", $network);
442                 $sql_network = "'".$sql_network."'";
443
444                 /// @todo This query is really slow
445                 // By now we cache the data for five minutes
446                 $r = q(
447                         "SELECT count(glink.gcid) as `total`, gcontact.* from gcontact
448                         INNER JOIN `glink` ON `glink`.`gcid` = `gcontact`.`id`
449                         where uid = %d and not gcontact.nurl in ( select nurl from contact where uid = %d )
450                         AND NOT `gcontact`.`name` IN (SELECT `name` FROM `contact` WHERE `uid` = %d)
451                         AND NOT `gcontact`.`id` IN (SELECT `gcid` FROM `gcign` WHERE `uid` = %d)
452                         AND `gcontact`.`updated` >= '%s' AND NOT `gcontact`.`hide`
453                         AND `gcontact`.`last_contact` >= `gcontact`.`last_failure`
454                         AND `gcontact`.`network` IN (%s)
455                         GROUP BY `glink`.`gcid` ORDER BY `gcontact`.`updated` DESC,`total` DESC LIMIT %d, %d",
456                         intval($uid),
457                         intval($uid),
458                         intval($uid),
459                         intval($uid),
460                         DBA::NULL_DATETIME,
461                         $sql_network,
462                         intval($start),
463                         intval($limit)
464                 );
465
466                 if (DBA::isResult($r) && count($r) >= ($limit -1)) {
467                         /*
468                         * Uncommented because the result of the queries are to big to store it in the cache.
469                         * We need to decide if we want to change the db column type or if we want to delete it.
470                         */
471                         //Cache::set("suggestion_query:".$uid.":".$start.":".$limit, $r, Cache::FIVE_MINUTES);
472
473                         return $r;
474                 }
475
476                 $r2 = q(
477                         "SELECT gcontact.* FROM gcontact
478                         INNER JOIN `glink` ON `glink`.`gcid` = `gcontact`.`id`
479                         WHERE `glink`.`uid` = 0 AND `glink`.`cid` = 0 AND `glink`.`zcid` = 0 AND NOT `gcontact`.`nurl` IN (SELECT `nurl` FROM `contact` WHERE `uid` = %d)
480                         AND NOT `gcontact`.`name` IN (SELECT `name` FROM `contact` WHERE `uid` = %d)
481                         AND NOT `gcontact`.`id` IN (SELECT `gcid` FROM `gcign` WHERE `uid` = %d)
482                         AND `gcontact`.`updated` >= '%s'
483                         AND `gcontact`.`last_contact` >= `gcontact`.`last_failure`
484                         AND `gcontact`.`network` IN (%s)
485                         ORDER BY rand() LIMIT %d, %d",
486                         intval($uid),
487                         intval($uid),
488                         intval($uid),
489                         DBA::NULL_DATETIME,
490                         $sql_network,
491                         intval($start),
492                         intval($limit)
493                 );
494
495                 $list = [];
496                 foreach ($r2 as $suggestion) {
497                         $list[$suggestion["nurl"]] = $suggestion;
498                 }
499
500                 foreach ($r as $suggestion) {
501                         $list[$suggestion["nurl"]] = $suggestion;
502                 }
503
504                 while (sizeof($list) > ($limit)) {
505                         array_pop($list);
506                 }
507
508                 /*
509                 * Uncommented because the result of the queries are to big to store it in the cache.
510                 * We need to decide if we want to change the db column type or if we want to delete it.
511                 */
512                 //Cache::set("suggestion_query:".$uid.":".$start.":".$limit, $list, Cache::FIVE_MINUTES);
513                 return $list;
514         }
515
516         /**
517          * @return void
518          */
519         public static function updateSuggestions()
520         {
521                 $a = \get_app();
522
523                 $done = [];
524
525                 /// @TODO Check if it is really neccessary to poll the own server
526                 PortableContact::loadWorker(0, 0, 0, System::baseUrl() . '/poco');
527
528                 $done[] = System::baseUrl() . '/poco';
529
530                 if (strlen(Config::get('system', 'directory'))) {
531                         $x = Network::fetchUrl(get_server()."/pubsites");
532                         if (!empty($x)) {
533                                 $j = json_decode($x);
534                                 if (!empty($j->entries)) {
535                                         foreach ($j->entries as $entry) {
536                                                 PortableContact::checkServer($entry->url);
537
538                                                 $url = $entry->url . '/poco';
539                                                 if (!in_array($url, $done)) {
540                                                         PortableContact::loadWorker(0, 0, 0, $url);
541                                                         $done[] = $url;
542                                                 }
543                                         }
544                                 }
545                         }
546                 }
547
548                 // Query your contacts from Friendica and Redmatrix/Hubzilla for their contacts
549                 $r = q(
550                         "SELECT DISTINCT(`poco`) AS `poco` FROM `contact` WHERE `network` IN ('%s', '%s')",
551                         DBA::escape(Protocol::DFRN),
552                         DBA::escape(Protocol::DIASPORA)
553                 );
554
555                 if (DBA::isResult($r)) {
556                         foreach ($r as $rr) {
557                                 $base = substr($rr['poco'], 0, strrpos($rr['poco'], '/'));
558                                 if (! in_array($base, $done)) {
559                                         PortableContact::loadWorker(0, 0, 0, $base);
560                                 }
561                         }
562                 }
563         }
564
565         /**
566          * @brief Removes unwanted parts from a contact url
567          *
568          * @param string $url Contact url
569          *
570          * @return string Contact url with the wanted parts
571          */
572         public static function cleanContactUrl($url)
573         {
574                 $parts = parse_url($url);
575
576                 if (!isset($parts["scheme"]) || !isset($parts["host"])) {
577                         return $url;
578                 }
579
580                 $new_url = $parts["scheme"]."://".$parts["host"];
581
582                 if (isset($parts["port"])) {
583                         $new_url .= ":".$parts["port"];
584                 }
585
586                 if (isset($parts["path"])) {
587                         $new_url .= $parts["path"];
588                 }
589
590                 if ($new_url != $url) {
591                         Logger::log("Cleaned contact url ".$url." to ".$new_url." - Called by: ".System::callstack(), Logger::DEBUG);
592                 }
593
594                 return $new_url;
595         }
596
597         /**
598          * @brief Replace alternate OStatus user format with the primary one
599          *
600          * @param array $contact contact array (called by reference)
601          * @return void
602          */
603         public static function fixAlternateContactAddress(&$contact)
604         {
605                 if (($contact["network"] == Protocol::OSTATUS) && PortableContact::alternateOStatusUrl($contact["url"])) {
606                         $data = Probe::uri($contact["url"]);
607                         if ($contact["network"] == Protocol::OSTATUS) {
608                                 Logger::log("Fix primary url from ".$contact["url"]." to ".$data["url"]." - Called by: ".System::callstack(), Logger::DEBUG);
609                                 $contact["url"] = $data["url"];
610                                 $contact["addr"] = $data["addr"];
611                                 $contact["alias"] = $data["alias"];
612                                 $contact["server_url"] = $data["baseurl"];
613                         }
614                 }
615         }
616
617         /**
618          * @brief Fetch the gcontact id, add an entry if not existed
619          *
620          * @param array $contact contact array
621          *
622          * @return bool|int Returns false if not found, integer if contact was found
623          */
624         public static function getId($contact)
625         {
626                 $gcontact_id = 0;
627                 $doprobing = false;
628                 $last_failure_str = '';
629                 $last_contact_str = '';
630
631                 if (empty($contact["network"])) {
632                         Logger::log("Empty network for contact url ".$contact["url"]." - Called by: ".System::callstack(), Logger::DEBUG);
633                         return false;
634                 }
635
636                 if (in_array($contact["network"], [Protocol::PHANTOM])) {
637                         Logger::log("Invalid network for contact url ".$contact["url"]." - Called by: ".System::callstack(), Logger::DEBUG);
638                         return false;
639                 }
640
641                 if ($contact["network"] == Protocol::STATUSNET) {
642                         $contact["network"] = Protocol::OSTATUS;
643                 }
644
645                 // All new contacts are hidden by default
646                 if (!isset($contact["hide"])) {
647                         $contact["hide"] = true;
648                 }
649
650                 // Replace alternate OStatus user format with the primary one
651                 self::fixAlternateContactAddress($contact);
652
653                 // Remove unwanted parts from the contact url (e.g. "?zrl=...")
654                 if (in_array($contact["network"], [Protocol::ACTIVITYPUB, Protocol::DFRN, Protocol::DIASPORA, Protocol::OSTATUS])) {
655                         $contact["url"] = self::cleanContactUrl($contact["url"]);
656                 }
657
658                 DBA::lock('gcontact');
659                 $fields = ['id', 'last_contact', 'last_failure', 'network'];
660                 $gcnt = DBA::selectFirst('gcontact', $fields, ['nurl' => Strings::normaliseLink($contact["url"])]);
661                 if (DBA::isResult($gcnt)) {
662                         $gcontact_id = $gcnt["id"];
663
664                         // Update every 90 days
665                         if (in_array($gcnt["network"], [Protocol::DFRN, Protocol::DIASPORA, Protocol::OSTATUS, ""])) {
666                                 $last_failure_str = $gcnt["last_failure"];
667                                 $last_failure = strtotime($gcnt["last_failure"]);
668                                 $last_contact_str = $gcnt["last_contact"];
669                                 $last_contact = strtotime($gcnt["last_contact"]);
670                                 $doprobing = (((time() - $last_contact) > (90 * 86400)) && ((time() - $last_failure) > (90 * 86400)));
671                         }
672                 } else {
673                         $contact['location'] = defaults($contact, 'location', '');
674                         $contact['about'] = defaults($contact, 'about', '');
675                         $contact['generation'] = defaults($contact, 'generation', 0);
676
677                         q(
678                                 "INSERT INTO `gcontact` (`name`, `nick`, `addr` , `network`, `url`, `nurl`, `photo`, `created`, `updated`, `location`, `about`, `hide`, `generation`)
679                                 VALUES ('%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', %d, %d)",
680                                 DBA::escape($contact["name"]),
681                                 DBA::escape($contact["nick"]),
682                                 DBA::escape($contact["addr"]),
683                                 DBA::escape($contact["network"]),
684                                 DBA::escape($contact["url"]),
685                                 DBA::escape(Strings::normaliseLink($contact["url"])),
686                                 DBA::escape($contact["photo"]),
687                                 DBA::escape(DateTimeFormat::utcNow()),
688                                 DBA::escape(DateTimeFormat::utcNow()),
689                                 DBA::escape($contact["location"]),
690                                 DBA::escape($contact["about"]),
691                                 intval($contact["hide"]),
692                                 intval($contact["generation"])
693                         );
694
695                         $condition = ['nurl' => Strings::normaliseLink($contact["url"])];
696                         $cnt = DBA::selectFirst('gcontact', ['id', 'network'], $condition, ['order' => ['id']]);
697                         if (DBA::isResult($cnt)) {
698                                 $gcontact_id = $cnt["id"];
699                                 $doprobing = in_array($cnt["network"], [Protocol::DFRN, Protocol::DIASPORA, Protocol::OSTATUS, ""]);
700                         }
701                 }
702                 DBA::unlock();
703
704                 if ($doprobing) {
705                         Logger::log("Last Contact: ". $last_contact_str." - Last Failure: ".$last_failure_str." - Checking: ".$contact["url"], Logger::DEBUG);
706                         Worker::add(PRIORITY_LOW, 'GProbe', $contact["url"]);
707                 }
708
709                 return $gcontact_id;
710         }
711
712         /**
713          * @brief Updates the gcontact table from a given array
714          *
715          * @param array $contact contact array
716          *
717          * @return bool|int Returns false if not found, integer if contact was found
718          */
719         public static function update($contact)
720         {
721                 // Check for invalid "contact-type" value
722                 if (isset($contact['contact-type']) && (intval($contact['contact-type']) < 0)) {
723                         $contact['contact-type'] = 0;
724                 }
725
726                 /// @todo update contact table as well
727
728                 $gcontact_id = self::getId($contact);
729
730                 if (!$gcontact_id) {
731                         return false;
732                 }
733
734                 $public_contact = q(
735                         "SELECT `name`, `nick`, `photo`, `location`, `about`, `addr`, `generation`, `birthday`, `gender`, `keywords`,
736                                 `contact-type`, `hide`, `nsfw`, `network`, `alias`, `notify`, `server_url`, `connect`, `updated`, `url`
737                         FROM `gcontact` WHERE `id` = %d LIMIT 1",
738                         intval($gcontact_id)
739                 );
740
741                 // Get all field names
742                 $fields = [];
743                 foreach ($public_contact[0] as $field => $data) {
744                         $fields[$field] = $data;
745                 }
746
747                 unset($fields["url"]);
748                 unset($fields["updated"]);
749                 unset($fields["hide"]);
750
751                 // Bugfix: We had an error in the storing of keywords which lead to the "0"
752                 // This value is still transmitted via poco.
753                 if (!empty($contact["keywords"]) && ($contact["keywords"] == "0")) {
754                         unset($contact["keywords"]);
755                 }
756
757                 if (!empty($public_contact[0]["keywords"]) && ($public_contact[0]["keywords"] == "0")) {
758                         $public_contact[0]["keywords"] = "";
759                 }
760
761                 // assign all unassigned fields from the database entry
762                 foreach ($fields as $field => $data) {
763                         if (!isset($contact[$field]) || ($contact[$field] == "")) {
764                                 $contact[$field] = $public_contact[0][$field];
765                         }
766                 }
767
768                 if (!isset($contact["hide"])) {
769                         $contact["hide"] = $public_contact[0]["hide"];
770                 }
771
772                 $fields["hide"] = $public_contact[0]["hide"];
773
774                 if ($contact["network"] == Protocol::STATUSNET) {
775                         $contact["network"] = Protocol::OSTATUS;
776                 }
777
778                 // Replace alternate OStatus user format with the primary one
779                 self::fixAlternateContactAddress($contact);
780
781                 if (!isset($contact["updated"])) {
782                         $contact["updated"] = DateTimeFormat::utcNow();
783                 }
784
785                 if ($contact["network"] == Protocol::TWITTER) {
786                         $contact["server_url"] = 'http://twitter.com';
787                 }
788
789                 if ($contact["server_url"] == "") {
790                         $data = Probe::uri($contact["url"]);
791                         if ($data["network"] != Protocol::PHANTOM) {
792                                 $contact["server_url"] = $data['baseurl'];
793                         }
794                 } else {
795                         $contact["server_url"] = Strings::normaliseLink($contact["server_url"]);
796                 }
797
798                 if (($contact["addr"] == "") && ($contact["server_url"] != "") && ($contact["nick"] != "")) {
799                         $hostname = str_replace("http://", "", $contact["server_url"]);
800                         $contact["addr"] = $contact["nick"]."@".$hostname;
801                 }
802
803                 // Check if any field changed
804                 $update = false;
805                 unset($fields["generation"]);
806
807                 if ((($contact["generation"] > 0) && ($contact["generation"] <= $public_contact[0]["generation"])) || ($public_contact[0]["generation"] == 0)) {
808                         foreach ($fields as $field => $data) {
809                                 if ($contact[$field] != $public_contact[0][$field]) {
810                                         Logger::log("Difference for contact ".$contact["url"]." in field '".$field."'. New value: '".$contact[$field]."', old value '".$public_contact[0][$field]."'", Logger::DEBUG);
811                                         $update = true;
812                                 }
813                         }
814
815                         if ($contact["generation"] < $public_contact[0]["generation"]) {
816                                 Logger::log("Difference for contact ".$contact["url"]." in field 'generation'. new value: '".$contact["generation"]."', old value '".$public_contact[0]["generation"]."'", Logger::DEBUG);
817                                 $update = true;
818                         }
819                 }
820
821                 if ($update) {
822                         Logger::log("Update gcontact for ".$contact["url"], Logger::DEBUG);
823                         $condition = ['`nurl` = ? AND (`generation` = 0 OR `generation` >= ?)',
824                                         Strings::normaliseLink($contact["url"]), $contact["generation"]];
825                         $contact["updated"] = DateTimeFormat::utc($contact["updated"]);
826
827                         $updated = ['photo' => $contact['photo'], 'name' => $contact['name'],
828                                         'nick' => $contact['nick'], 'addr' => $contact['addr'],
829                                         'network' => $contact['network'], 'birthday' => $contact['birthday'],
830                                         'gender' => $contact['gender'], 'keywords' => $contact['keywords'],
831                                         'hide' => $contact['hide'], 'nsfw' => $contact['nsfw'],
832                                         'contact-type' => $contact['contact-type'], 'alias' => $contact['alias'],
833                                         'notify' => $contact['notify'], 'url' => $contact['url'],
834                                         'location' => $contact['location'], 'about' => $contact['about'],
835                                         'generation' => $contact['generation'], 'updated' => $contact['updated'],
836                                         'server_url' => $contact['server_url'], 'connect' => $contact['connect']];
837
838                         DBA::update('gcontact', $updated, $condition, $fields);
839
840                         // Now update the contact entry with the user id "0" as well.
841                         // This is used for the shadow copies of public items.
842                         /// @todo Check if we really should do this.
843                         // The quality of the gcontact table is mostly lower than the public contact
844                         $public_contact = DBA::selectFirst('contact', ['id'], ['nurl' => Strings::normaliseLink($contact["url"]), 'uid' => 0]);
845                         if (DBA::isResult($public_contact)) {
846                                 Logger::log("Update public contact ".$public_contact["id"], Logger::DEBUG);
847
848                                 Contact::updateAvatar($contact["photo"], 0, $public_contact["id"]);
849
850                                 $fields = ['name', 'nick', 'addr',
851                                                 'network', 'bd', 'gender',
852                                                 'keywords', 'alias', 'contact-type',
853                                                 'url', 'location', 'about'];
854                                 $old_contact = DBA::selectFirst('contact', $fields, ['id' => $public_contact["id"]]);
855
856                                 // Update it with the current values
857                                 $fields = ['name' => $contact['name'], 'nick' => $contact['nick'],
858                                                 'addr' => $contact['addr'], 'network' => $contact['network'],
859                                                 'bd' => $contact['birthday'], 'gender' => $contact['gender'],
860                                                 'keywords' => $contact['keywords'], 'alias' => $contact['alias'],
861                                                 'contact-type' => $contact['contact-type'], 'url' => $contact['url'],
862                                                 'location' => $contact['location'], 'about' => $contact['about']];
863
864                                 // Don't update the birthday field if not set or invalid
865                                 if (empty($contact['birthday']) || ($contact['birthday'] <= DBA::NULL_DATE)) {
866                                         unset($fields['bd']);
867                                 }
868
869
870                                 DBA::update('contact', $fields, ['id' => $public_contact["id"]], $old_contact);
871                         }
872                 }
873
874                 return $gcontact_id;
875         }
876
877         /**
878          * @brief Updates the gcontact entry from probe
879          *
880          * @param string $url profile link
881          * @return void
882          */
883         public static function updateFromProbe($url)
884         {
885                 $data = Probe::uri($url);
886
887                 if (in_array($data["network"], [Protocol::PHANTOM])) {
888                         Logger::log("Invalid network for contact url ".$data["url"]." - Called by: ".System::callstack(), Logger::DEBUG);
889                         return;
890                 }
891
892                 $data["server_url"] = $data["baseurl"];
893
894                 self::update($data);
895         }
896
897         /**
898          * @brief Update the gcontact entry for a given user id
899          *
900          * @param int $uid User ID
901          * @return void
902          */
903         public static function updateForUser($uid)
904         {
905                 $r = q(
906                         "SELECT `profile`.`locality`, `profile`.`region`, `profile`.`country-name`,
907                                 `profile`.`name`, `profile`.`about`, `profile`.`gender`,
908                                 `profile`.`pub_keywords`, `profile`.`dob`, `profile`.`photo`,
909                                 `profile`.`net-publish`, `user`.`nickname`, `user`.`hidewall`,
910                                 `contact`.`notify`, `contact`.`url`, `contact`.`addr`
911                         FROM `profile`
912                                 INNER JOIN `user` ON `user`.`uid` = `profile`.`uid`
913                                 INNER JOIN `contact` ON `contact`.`uid` = `profile`.`uid`
914                         WHERE `profile`.`uid` = %d AND `profile`.`is-default` AND `contact`.`self`",
915                         intval($uid)
916                 );
917
918                 if (!DBA::isResult($r)) {
919                         Logger::log('Cannot find user with uid=' . $uid, Logger::INFO);
920                         return false;
921                 }
922
923                 $location = Profile::formatLocation(
924                         ["locality" => $r[0]["locality"], "region" => $r[0]["region"], "country-name" => $r[0]["country-name"]]
925                 );
926
927                 // The "addr" field was added in 3.4.3 so it can be empty for older users
928                 if ($r[0]["addr"] != "") {
929                         $addr = $r[0]["nickname"].'@'.str_replace(["http://", "https://"], "", System::baseUrl());
930                 } else {
931                         $addr = $r[0]["addr"];
932                 }
933
934                 $gcontact = ["name" => $r[0]["name"], "location" => $location, "about" => $r[0]["about"],
935                                 "gender" => $r[0]["gender"], "keywords" => $r[0]["pub_keywords"],
936                                 "birthday" => $r[0]["dob"], "photo" => $r[0]["photo"],
937                                 "notify" => $r[0]["notify"], "url" => $r[0]["url"],
938                                 "hide" => ($r[0]["hidewall"] || !$r[0]["net-publish"]),
939                                 "nick" => $r[0]["nickname"], "addr" => $addr,
940                                 "connect" => $addr, "server_url" => System::baseUrl(),
941                                 "generation" => 1, "network" => Protocol::DFRN];
942
943                 self::update($gcontact);
944         }
945
946         /**
947          * @brief Fetches users of given GNU Social server
948          *
949          * If the "Statistics" addon is enabled (See http://gstools.org/ for details) we query user data with this.
950          *
951          * @param string $server Server address
952          * @return void
953          */
954         public static function fetchGsUsers($server)
955         {
956                 Logger::log("Fetching users from GNU Social server ".$server, Logger::DEBUG);
957
958                 $url = $server."/main/statistics";
959
960                 $curlResult = Network::curl($url);
961                 if (!$curlResult->isSuccess()) {
962                         return false;
963                 }
964
965                 $statistics = json_decode($curlResult->getBody());
966
967                 if (!empty($statistics->config->instance_address)) {
968                         if (!empty($statistics->config->instance_with_ssl)) {
969                                 $server = "https://";
970                         } else {
971                                 $server = "http://";
972                         }
973
974                         $server .= $statistics->config->instance_address;
975
976                         $hostname = $statistics->config->instance_address;
977                 } elseif (!empty($statistics->instance_address)) {
978                         if (!empty($statistics->instance_with_ssl)) {
979                                 $server = "https://";
980                         } else {
981                                 $server = "http://";
982                         }
983
984                         $server .= $statistics->instance_address;
985
986                         $hostname = $statistics->instance_address;
987                 }
988
989                 if (!empty($statistics->users)) {
990                         foreach ($statistics->users as $nick => $user) {
991                                 $profile_url = $server."/".$user->nickname;
992
993                                 $contact = ["url" => $profile_url,
994                                                 "name" => $user->fullname,
995                                                 "addr" => $user->nickname."@".$hostname,
996                                                 "nick" => $user->nickname,
997                                                 "network" => Protocol::OSTATUS,
998                                                 "photo" => System::baseUrl()."/images/person-300.jpg"];
999
1000                                 if (isset($user->bio)) {
1001                                         $contact["about"] = $user->bio;
1002                                 }
1003
1004                                 self::getId($contact);
1005                         }
1006                 }
1007         }
1008
1009         /**
1010          * @brief Asking GNU Social server on a regular base for their user data
1011          * @return void
1012          */
1013         public static function discoverGsUsers()
1014         {
1015                 $requery_days = intval(Config::get("system", "poco_requery_days"));
1016
1017                 $last_update = date("c", time() - (60 * 60 * 24 * $requery_days));
1018
1019                 $r = q(
1020                         "SELECT `nurl`, `url` FROM `gserver` WHERE `last_contact` >= `last_failure` AND `network` = '%s' AND `last_poco_query` < '%s' ORDER BY RAND() LIMIT 5",
1021                         DBA::escape(Protocol::OSTATUS),
1022                         DBA::escape($last_update)
1023                 );
1024
1025                 if (!DBA::isResult($r)) {
1026                         return;
1027                 }
1028
1029                 foreach ($r as $server) {
1030                         self::fetchGsUsers($server["url"]);
1031                         q("UPDATE `gserver` SET `last_poco_query` = '%s' WHERE `nurl` = '%s'", DBA::escape(DateTimeFormat::utcNow()), DBA::escape($server["nurl"]));
1032                 }
1033         }
1034
1035         /**
1036          * @return string
1037          */
1038         public static function getRandomUrl()
1039         {
1040                 $r = q(
1041                         "SELECT `url` FROM `gcontact` WHERE `network` = '%s'
1042                                         AND `last_contact` >= `last_failure`
1043                                         AND `updated` > UTC_TIMESTAMP - INTERVAL 1 MONTH
1044                                 ORDER BY rand() LIMIT 1",
1045                         DBA::escape(Protocol::DFRN)
1046                 );
1047
1048                 if (DBA::isResult($r)) {
1049                         return dirname($r[0]['url']);
1050                 }
1051
1052                 return '';
1053         }
1054 }