]> git.mxchange.org Git - friendica.git/blob - src/Core/Relocate.php
Move relocation feature in its separate class
[friendica.git] / src / Core / Relocate.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2010-2022, 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\Core;
23
24 use Friendica\App;
25 use Friendica\Database;
26 use Friendica\Util\Strings;
27 use Friendica\Worker\Delivery;
28
29 class Relocate
30 {
31         /**
32          * @var App\BaseURL
33          */
34         private $baseUrl;
35         /**
36          * @var Database\Database
37          */
38         private $database;
39         /**
40          * @var Config\Capability\IManageConfigValues
41          */
42         private $config;
43
44         public function __construct(App\BaseURL $baseUrl, Database\Database $database, Config\Capability\IManageConfigValues $config)
45         {
46                 $this->baseUrl  = $baseUrl;
47                 $this->database = $database;
48                 $this->config   = $config;
49         }
50
51         /**
52          * Performs relocation
53          *
54          * @param string $new_url The new node URL, including the scheme
55          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
56          */
57         public function run(string $new_url)
58         {
59                 $new_url = rtrim($new_url, '/');
60
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>');
64                 }
65
66                 /* steps:
67                  * replace all "baseurl" to "new_url" in config, profile, term, items and contacts
68                  * send relocate for every local user
69                  * */
70                 $old_url = $this->baseUrl->get(true);
71
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));
75
76                 // update tables
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);
81
82                 // update profile addresses in the format "user@server.tld"
83                 $this->database->replaceInTableFields('contact', ['addr'], $old_host, $new_host);
84
85                 // update config
86                 $this->config->set('system', 'url', $new_url);
87                 $this->baseUrl->saveByURL($new_url);
88
89                 // send relocate
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']);
93                 }
94         }
95 }