Remove dependency to App in Contact::createFromProbe
[friendica.git/.git] / mod / ostatus_subscribe.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 use Friendica\App;
23 use Friendica\Core\Protocol;
24 use Friendica\DI;
25 use Friendica\Model\Contact;
26 use Friendica\Network\Probe;
27 use Friendica\Util\Network;
28
29 function ostatus_subscribe_content(App $a)
30 {
31         if (!local_user()) {
32                 notice(DI::l10n()->t('Permission denied.') . EOL);
33                 DI::baseUrl()->redirect('ostatus_subscribe');
34                 // NOTREACHED
35         }
36
37         $o = '<h2>' . DI::l10n()->t('Subscribing to OStatus contacts') . '</h2>';
38
39         $uid = local_user();
40
41         $counter = intval($_REQUEST['counter']);
42
43         if (DI::pConfig()->get($uid, 'ostatus', 'legacy_friends') == '') {
44
45                 if ($_REQUEST['url'] == '') {
46                         DI::pConfig()->delete($uid, 'ostatus', 'legacy_contact');
47                         return $o . DI::l10n()->t('No contact provided.');
48                 }
49
50                 $contact = Probe::uri($_REQUEST['url']);
51
52                 if (!$contact) {
53                         DI::pConfig()->delete($uid, 'ostatus', 'legacy_contact');
54                         return $o . DI::l10n()->t('Couldn\'t fetch information for contact.');
55                 }
56
57                 $api = $contact['baseurl'] . '/api/';
58
59                 // Fetching friends
60                 $curlResult = Network::curl($api . 'statuses/friends.json?screen_name=' . $contact['nick']);
61
62                 if (!$curlResult->isSuccess()) {
63                         DI::pConfig()->delete($uid, 'ostatus', 'legacy_contact');
64                         return $o . DI::l10n()->t('Couldn\'t fetch friends for contact.');
65                 }
66
67                 DI::pConfig()->set($uid, 'ostatus', 'legacy_friends', $curlResult->getBody());
68         }
69
70         $friends = json_decode(DI::pConfig()->get($uid, 'ostatus', 'legacy_friends'));
71
72         if (empty($friends)) {
73                 $friends = [];
74         }
75
76         $total = sizeof($friends);
77
78         if ($counter >= $total) {
79                 DI::page()['htmlhead'] = '<meta http-equiv="refresh" content="0; URL=' . DI::baseUrl() . '/settings/connectors">';
80                 DI::pConfig()->delete($uid, 'ostatus', 'legacy_friends');
81                 DI::pConfig()->delete($uid, 'ostatus', 'legacy_contact');
82                 $o .= DI::l10n()->t('Done');
83                 return $o;
84         }
85
86         $friend = $friends[$counter++];
87
88         $url = $friend->statusnet_profile_url;
89
90         $o .= '<p>' . $counter . '/' . $total . ': ' . $url;
91
92         $probed = Probe::uri($url);
93         if ($probed['network'] == Protocol::OSTATUS) {
94                 $result = Contact::createFromProbe($a->user, $url, true, Protocol::OSTATUS);
95                 if ($result['success']) {
96                         $o .= ' - ' . DI::l10n()->t('success');
97                 } else {
98                         $o .= ' - ' . DI::l10n()->t('failed');
99                 }
100         } else {
101                 $o .= ' - ' . DI::l10n()->t('ignored');
102         }
103
104         $o .= '</p>';
105
106         $o .= '<p>' . DI::l10n()->t('Keep this window open until done.') . '</p>';
107
108         DI::page()['htmlhead'] = '<meta http-equiv="refresh" content="0; URL=' . DI::baseUrl() . '/ostatus_subscribe?counter=' . $counter . '">';
109
110         return $o;
111 }