]> git.mxchange.org Git - friendica.git/blob - src/Model/FContact.php
Merge pull request #9823 from MrPetovan/task/9677-2fa-remember-device
[friendica.git] / src / Model / FContact.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2020, Friendica
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\DI;
28 use Friendica\Network\Probe;
29 use Friendica\Protocol\Activity;
30 use Friendica\Util\DateTimeFormat;
31 use Friendica\Util\Strings;
32
33 class FContact
34 {
35         /**
36          * Fetches data for a given handle
37          *
38          * @param string $handle The handle
39          * @param boolean $update true = always update, false = never update, null = update when not found or outdated
40          *
41          * @return array the queried data
42          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
43          * @throws \ImagickException
44          */
45         public static function getByURL($handle, $update = null, $network = Protocol::DIASPORA)
46         {
47                 $person = DBA::selectFirst('fcontact', [], ['network' => $network, 'addr' => $handle]);
48                 if (!DBA::isResult($person)) {
49                         $urls = [$handle, str_replace('http://', 'https://', $handle), Strings::normaliseLink($handle)];
50                         $person = DBA::selectFirst('fcontact', [], ['network' => $network, 'url' => $urls]);
51                 }
52
53                 if (DBA::isResult($person)) {
54                         Logger::debug('In cache', ['person' => $person]);
55
56                         if (is_null($update)) {
57                                 // update record occasionally so it doesn't get stale
58                                 $d = strtotime($person["updated"]." +00:00");
59                                 if ($d < strtotime("now - 14 days")) {
60                                         $update = true;
61                                 }
62
63                                 if ($person["guid"] == "") {
64                                         $update = true;
65                                 }
66                         }
67                 } elseif (is_null($update)) {
68                         $update = !DBA::isResult($person);
69                 } else {
70                         $person = [];
71                 }
72
73                 if ($update) {
74                         Logger::info('create or refresh', ['handle' => $handle]);
75                         $r = Probe::uri($handle, $network);
76
77                         // Note that Friendica contacts will return a "Diaspora person"
78                         // if Diaspora connectivity is enabled on their server
79                         if ($r && ($r["network"] === $network)) {
80                                 self::updateFContact($r);
81
82                                 $person = self::getByURL($handle, false, $network);
83                         }
84                 }
85
86                 return $person;
87         }
88
89         /**
90          * Updates the fcontact table
91          *
92          * @param array $arr The fcontact data
93          * @throws \Exception
94          */
95         private static function updateFContact($arr)
96         {
97                 $fields = ['name' => $arr["name"], 'photo' => $arr["photo"],
98                         'request' => $arr["request"], 'nick' => $arr["nick"],
99                         'addr' => strtolower($arr["addr"]), 'guid' => $arr["guid"],
100                         'batch' => $arr["batch"], 'notify' => $arr["notify"],
101                         'poll' => $arr["poll"], 'confirm' => $arr["confirm"],
102                         'alias' => $arr["alias"], 'pubkey' => $arr["pubkey"],
103                         'updated' => DateTimeFormat::utcNow()];
104
105                 $condition = ['url' => $arr["url"], 'network' => $arr["network"]];
106
107                 DBA::update('fcontact', $fields, $condition, true);
108         }
109
110         /**
111          * get a url (scheme://domain.tld/u/user) from a given Diaspora*
112          * fcontact guid
113          *
114          * @param mixed $fcontact_guid Hexadecimal string guid
115          *
116          * @return string the contact url or null
117          * @throws \Exception
118          */
119         public static function getUrlByGuid($fcontact_guid)
120         {
121                 Logger::info('fcontact', ['guid' => $fcontact_guid]);
122
123                 $r = q(
124                         "SELECT `url` FROM `fcontact` WHERE `url` != '' AND `network` = '%s' AND `guid` = '%s'",
125                         DBA::escape(Protocol::DIASPORA),
126                         DBA::escape($fcontact_guid)
127                 );
128
129                 if (DBA::isResult($r)) {
130                         return $r[0]['url'];
131                 }
132
133                 return null;
134         }
135
136         /**
137          * Suggest a given contact to a given user from a given contact
138          *
139          * @param integer $uid
140          * @param integer $cid
141          * @param integer $from_cid
142          * @return bool   Was the adding successful?
143          */
144         public static function addSuggestion(int $uid, int $cid, int $from_cid, string $note = '')
145         {
146                 $owner = User::getOwnerDataById($uid);
147                 $contact = Contact::getById($cid);
148                 $from_contact = Contact::getById($from_cid);
149
150                 if (DBA::exists('contact', ['nurl' => Strings::normaliseLink($contact['url']), 'uid' => $uid])) {
151                         return false;
152                 }
153
154                 $fcontact = self::getByURL($contact['url'], null, $contact['network']);
155                 if (empty($fcontact)) {
156                         Logger::warning('FContact had not been found', ['fcontact' => $contact['url']]);
157                         return false;
158                 }
159
160                 $fid = $fcontact['id'];
161
162                 // Quit if we already have an introduction for this person
163                 if (DBA::exists('intro', ['uid' => $uid, 'fid' => $fid])) {
164                         return false;
165                 }
166
167                 $suggest = [];
168                 $suggest['uid'] = $uid;
169                 $suggest['cid'] = $from_cid;
170                 $suggest['url'] = $contact['url'];
171                 $suggest['name'] = $contact['name'];
172                 $suggest['photo'] = $contact['photo'];
173                 $suggest['request'] = $contact['request'];
174                 $suggest['title'] = '';
175                 $suggest['body'] = $note;
176
177                 $hash = Strings::getRandomHex();
178                 $fields = ['uid' => $suggest['uid'], 'fid' => $fid, 'contact-id' => $suggest['cid'], 
179                         'note' => $suggest['body'], 'hash' => $hash, 'datetime' => DateTimeFormat::utcNow(), 'blocked' => false];
180                 DBA::insert('intro', $fields);
181
182                 notification([
183                         'type'  => Notification\Type::SUGGEST,
184                         'otype' => Notification\ObjectType::INTRO,
185                         'verb'  => Activity::REQ_FRIEND,
186                         'uid'   => $owner['uid'],
187                         'cid'   => $from_contact['uid'],
188                         'item'  => $suggest,
189                         'link'  => DI::baseUrl().'/notifications/intros',
190                 ]);
191
192                 return true;
193         }
194 }