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