3 * @copyright Copyright (C) 2010-2022, the Friendica project
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\Core;
25 use Friendica\Database;
26 use Friendica\Util\Strings;
27 use Friendica\Worker\Delivery;
36 * @var Database\Database
40 * @var Config\Capability\IManageConfigValues
44 public function __construct(App\BaseURL $baseUrl, Database\Database $database, Config\Capability\IManageConfigValues $config)
46 $this->baseUrl = $baseUrl;
47 $this->database = $database;
48 $this->config = $config;
54 * @param string $new_url The new node URL, including the scheme
55 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
57 public function run(string $new_url)
59 $new_url = rtrim($new_url, '/');
61 $parsed = @parse_url($new_url);
62 if (!is_array($parsed) || empty($parsed['host']) || empty($parsed['scheme'])) {
63 throw new \InvalidArgumentException('Can not parse base URL. Must have at least <scheme>://<domain>');
67 * replace all "baseurl" to "new_url" in config, profile, term, items and contacts
68 * send relocate for every local user
70 $old_url = $this->baseUrl->get(true);
72 // Generate host names for relocation the addresses in the format user@address.tld
73 $new_host = str_replace('http://', '@', Strings::normaliseLink($new_url));
74 $old_host = str_replace('http://', '@', Strings::normaliseLink($old_url));
77 // update profile links in the format "http://server.tld"
78 $this->database->replaceInTableFields('profile', ['photo', 'thumb'], $old_url, $new_url);
79 $this->database->replaceInTableFields('contact', ['photo', 'thumb', 'micro', 'url', 'nurl', 'alias', 'request', 'notify', 'poll', 'confirm', 'poco', 'avatar'], $old_url, $new_url);
80 $this->database->replaceInTableFields('post-content', ['body'], $old_url, $new_url);
82 // update profile addresses in the format "user@server.tld"
83 $this->database->replaceInTableFields('contact', ['addr'], $old_host, $new_host);
86 $this->config->set('system', 'url', $new_url);
87 $this->baseUrl->saveByURL($new_url);
90 $users = $this->database->selectToArray('user', ['uid'], ['account_removed' => false, 'account_expired' => false]);
91 foreach ($users as $user) {
92 Worker::add(PRIORITY_HIGH, 'Notifier', Delivery::RELOCATION, $user['uid']);