3 * @copyright Copyright (C) 2020, Friendica
5 * @license GNU AGPL version 3 or any later version
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.
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.
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/>.
22 namespace Friendica\Model;
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;
34 * Fetches data for a given handle
36 * @param string $handle The handle
37 * @param boolean $update true = always update, false = never update, null = update when not found or outdated
39 * @return array the queried data
40 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
41 * @throws \ImagickException
43 public static function getByURL($handle, $update = null)
45 $person = DBA::selectFirst('fcontact', [], ['network' => Protocol::DIASPORA, 'addr' => $handle]);
46 if (!DBA::isResult($person)) {
47 $urls = [$handle, str_replace('http://', 'https://', $handle), Strings::normaliseLink($handle)];
48 $person = DBA::selectFirst('fcontact', [], ['network' => Protocol::DIASPORA, 'url' => $urls]);
51 if (DBA::isResult($person)) {
52 Logger::debug('In cache', ['person' => $person]);
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")) {
61 if ($person["guid"] == "") {
65 } elseif (is_null($update)) {
66 $update = !DBA::isResult($person);
72 Logger::info('create or refresh', ['handle' => $handle]);
73 $r = Probe::uri($handle, Protocol::DIASPORA);
75 // Note that Friendica contacts will return a "Diaspora person"
76 // if Diaspora connectivity is enabled on their server
77 if ($r && ($r["network"] === Protocol::DIASPORA)) {
78 self::updateFContact($r);
80 $person = self::getByURL($handle, false);
88 * Updates the fcontact table
90 * @param array $arr The fcontact data
93 private static function updateFContact($arr)
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 'updated' => DateTimeFormat::utcNow()];
103 $condition = ['url' => $arr["url"], 'network' => $arr["network"]];
105 DBA::update('fcontact', $fields, $condition, true);
109 * get a url (scheme://domain.tld/u/user) from a given Diaspora*
112 * @param mixed $fcontact_guid Hexadecimal string guid
114 * @return string the contact url or null
117 public static function getUrlByGuid($fcontact_guid)
119 Logger::info('fcontact', ['guid' => $fcontact_guid]);
122 "SELECT `url` FROM `fcontact` WHERE `url` != '' AND `network` = '%s' AND `guid` = '%s'",
123 DBA::escape(Protocol::DIASPORA),
124 DBA::escape($fcontact_guid)
127 if (DBA::isResult($r)) {