]> git.mxchange.org Git - friendica.git/blob - src/Model/FContact.php
Merge pull request #10969 from MrPetovan/task/remove-private-contacts
[friendica.git] / src / Model / FContact.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2010-2021, the Friendica project
4  *
5  * @license GNU AGPL version 3 or any later version
6  *
7  * This program is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU Affero General Public License as
9  * published by the Free Software Foundation, either version 3 of the
10  * License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU Affero General Public License for more details.
16  *
17  * You should have received a copy of the GNU Affero General Public License
18  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
19  *
20  */
21
22 namespace Friendica\Model;
23
24 use Friendica\Core\Logger;
25 use Friendica\Core\Protocol;
26 use Friendica\Database\DBA;
27 use Friendica\Network\Probe;
28 use Friendica\Util\DateTimeFormat;
29 use Friendica\Util\Strings;
30
31 class FContact
32 {
33         /**
34          * Fetches data for a given handle
35          *
36          * @param string $handle The handle
37          * @param boolean $update true = always update, false = never update, null = update when not found or outdated
38          *
39          * @return array the queried data
40          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
41          * @throws \ImagickException
42          */
43         public static function getByURL($handle, $update = null, $network = Protocol::DIASPORA)
44         {
45                 $person = DBA::selectFirst('fcontact', [], ['network' => $network, 'addr' => $handle]);
46                 if (!DBA::isResult($person)) {
47                         $urls = [$handle, str_replace('http://', 'https://', $handle), Strings::normaliseLink($handle)];
48                         $person = DBA::selectFirst('fcontact', [], ['network' => $network, 'url' => $urls]);
49                 }
50
51                 if (DBA::isResult($person)) {
52                         Logger::debug('In cache', ['person' => $person]);
53
54                         if (is_null($update)) {
55                                 // update record occasionally so it doesn't get stale
56                                 $d = strtotime($person["updated"]." +00:00");
57                                 if ($d < strtotime("now - 14 days")) {
58                                         $update = true;
59                                 }
60
61                                 if (empty($person['guid']) || empty($person['uri-id'])) {
62                                         $update = true;
63                                 }
64                         }
65                 } elseif (is_null($update)) {
66                         $update = !DBA::isResult($person);
67                 } else {
68                         $person = [];
69                 }
70
71                 if ($update) {
72                         Logger::info('create or refresh', ['handle' => $handle]);
73                         $r = Probe::uri($handle, $network);
74
75                         // Note that Friendica contacts will return a "Diaspora person"
76                         // if Diaspora connectivity is enabled on their server
77                         if ($r && ($r["network"] === $network)) {
78                                 self::updateFContact($r);
79
80                                 $person = self::getByURL($handle, false, $network);
81                         }
82                 }
83
84                 return $person;
85         }
86
87         /**
88          * Updates the fcontact table
89          *
90          * @param array $arr The fcontact data
91          * @throws \Exception
92          */
93         private static function updateFContact($arr)
94         {
95                 $fields = ['name' => $arr["name"], 'photo' => $arr["photo"],
96                         'request' => $arr["request"], 'nick' => $arr["nick"],
97                         'addr' => strtolower($arr["addr"]), 'guid' => $arr["guid"],
98                         'batch' => $arr["batch"], 'notify' => $arr["notify"],
99                         'poll' => $arr["poll"], 'confirm' => $arr["confirm"],
100                         'alias' => $arr["alias"], 'pubkey' => $arr["pubkey"],
101                         'uri-id' => ItemURI::insert(['uri' => $arr['url'], 'guid' => $arr['guid']]),
102                         'updated' => DateTimeFormat::utcNow()];
103
104                 $condition = ['url' => $arr["url"], 'network' => $arr["network"]];
105
106                 DBA::update('fcontact', $fields, $condition, true);
107         }
108
109         /**
110          * get a url (scheme://domain.tld/u/user) from a given Diaspora*
111          * fcontact guid
112          *
113          * @param mixed $fcontact_guid Hexadecimal string guid
114          *
115          * @return string the contact url or null
116          * @throws \Exception
117          */
118         public static function getUrlByGuid($fcontact_guid)
119         {
120                 Logger::info('fcontact', ['guid' => $fcontact_guid]);
121
122                 $fcontact = DBA::selectFirst('fcontact', ['url'], ["`url` != ? AND `network` = ? AND `guid` = ?", '', Protocol::DIASPORA, $fcontact_guid]);
123                 if (DBA::isResult($fcontact)) {
124                         return $fcontact['url'];
125                 }
126
127                 return null;
128         }
129 }