3 * @copyright Copyright (C) 2010-2021, 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/>.
23 use Friendica\Core\Protocol;
25 use Friendica\Model\APContact;
26 use Friendica\Model\Contact;
27 use Friendica\Protocol\ActivityPub;
29 function ostatus_subscribe_content(App $a)
32 notice(DI::l10n()->t('Permission denied.'));
33 DI::baseUrl()->redirect('ostatus_subscribe');
37 $o = '<h2>' . DI::l10n()->t('Subscribing to contacts') . '</h2>';
41 $counter = intval($_REQUEST['counter'] ?? 0);
43 if (DI::pConfig()->get($uid, 'ostatus', 'legacy_friends') == '') {
45 if ($_REQUEST['url'] == '') {
46 DI::pConfig()->delete($uid, 'ostatus', 'legacy_contact');
47 return $o . DI::l10n()->t('No contact provided.');
50 $contact = Contact::getByURL($_REQUEST['url']);
52 DI::pConfig()->delete($uid, 'ostatus', 'legacy_contact');
53 return $o . DI::l10n()->t('Couldn\'t fetch information for contact.');
56 if ($contact['network'] == Protocol::OSTATUS) {
57 $api = $contact['baseurl'] . '/api/';
60 $curlResult = DI::httpRequest()->get($api . 'statuses/friends.json?screen_name=' . $contact['nick']);
62 if (!$curlResult->isSuccess()) {
63 DI::pConfig()->delete($uid, 'ostatus', 'legacy_contact');
64 return $o . DI::l10n()->t('Couldn\'t fetch friends for contact.');
67 $friends = $curlResult->getBody();
68 if (empty($friends)) {
69 DI::pConfig()->delete($uid, 'ostatus', 'legacy_contact');
70 return $o . DI::l10n()->t('Couldn\'t fetch following contacts.');
72 DI::pConfig()->set($uid, 'ostatus', 'legacy_friends', $friends);
73 } elseif ($apcontact = APContact::getByURL($contact['url'])) {
74 if (empty($apcontact['following'])) {
75 DI::pConfig()->delete($uid, 'ostatus', 'legacy_contact');
76 return $o . DI::l10n()->t('Couldn\'t fetch remote profile.');
78 $followings = ActivityPub::fetchItems($apcontact['following']);
79 if (empty($followings)) {
80 DI::pConfig()->delete($uid, 'ostatus', 'legacy_contact');
81 return $o . DI::l10n()->t('Couldn\'t fetch following contacts.');
83 DI::pConfig()->set($uid, 'ostatus', 'legacy_friends', json_encode($followings));
85 DI::pConfig()->delete($uid, 'ostatus', 'legacy_contact');
86 return $o . DI::l10n()->t('Unsupported network');
90 $friends = json_decode(DI::pConfig()->get($uid, 'ostatus', 'legacy_friends'));
92 if (empty($friends)) {
96 $total = sizeof($friends);
98 if ($counter >= $total) {
99 DI::page()['htmlhead'] = '<meta http-equiv="refresh" content="0; URL=' . DI::baseUrl() . '/settings/connectors">';
100 DI::pConfig()->delete($uid, 'ostatus', 'legacy_friends');
101 DI::pConfig()->delete($uid, 'ostatus', 'legacy_contact');
102 $o .= DI::l10n()->t('Done');
106 $friend = $friends[$counter++];
108 $url = $friend->statusnet_profile_url ?? $friend;
110 $o .= '<p>' . $counter . '/' . $total . ': ' . $url;
112 $probed = Contact::getByURL($url);
113 if (in_array($probed['network'], Protocol::FEDERATED)) {
114 $result = Contact::createFromProbe($a->user, $probed['url']);
115 if ($result['success']) {
116 $o .= ' - ' . DI::l10n()->t('success');
118 $o .= ' - ' . DI::l10n()->t('failed');
121 $o .= ' - ' . DI::l10n()->t('ignored');
126 $o .= '<p>' . DI::l10n()->t('Keep this window open until done.') . '</p>';
128 DI::page()['htmlhead'] = '<meta http-equiv="refresh" content="0; URL=' . DI::baseUrl() . '/ostatus_subscribe?counter=' . $counter . '">';