]> git.mxchange.org Git - friendica.git/blob - mod/ostatus_subscribe.php
Avoid warning "Undefined namespace prefix"
[friendica.git] / mod / ostatus_subscribe.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 use Friendica\App;
23 use Friendica\Core\Protocol;
24 use Friendica\DI;
25 use Friendica\Model\APContact;
26 use Friendica\Model\Contact;
27 use Friendica\Network\HTTPClient\Client\HttpClientAccept;
28 use Friendica\Protocol\ActivityPub;
29
30 function ostatus_subscribe_content(App $a)
31 {
32         if (!local_user()) {
33                 notice(DI::l10n()->t('Permission denied.'));
34                 DI::baseUrl()->redirect('ostatus_subscribe');
35                 // NOTREACHED
36         }
37
38         $o = '<h2>' . DI::l10n()->t('Subscribing to contacts') . '</h2>';
39
40         $uid = local_user();
41
42         $counter = intval($_REQUEST['counter'] ?? 0);
43
44         if (DI::pConfig()->get($uid, 'ostatus', 'legacy_friends') == '') {
45
46                 if ($_REQUEST['url'] == '') {
47                         DI::pConfig()->delete($uid, 'ostatus', 'legacy_contact');
48                         return $o . DI::l10n()->t('No contact provided.');
49                 }
50
51                 $contact = Contact::getByURL($_REQUEST['url']);
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                 if ($contact['network'] == Protocol::OSTATUS) {
58                         $api = $contact['baseurl'] . '/api/';
59
60                         // Fetching friends
61                         $curlResult = DI::httpClient()->get($api . 'statuses/friends.json?screen_name=' . $contact['nick'], HttpClientAccept::JSON);
62
63                         if (!$curlResult->isSuccess()) {
64                                 DI::pConfig()->delete($uid, 'ostatus', 'legacy_contact');
65                                 return $o . DI::l10n()->t('Couldn\'t fetch friends for contact.');
66                         }
67
68                         $friends = $curlResult->getBody();
69                         if (empty($friends)) {
70                                 DI::pConfig()->delete($uid, 'ostatus', 'legacy_contact');
71                                 return $o . DI::l10n()->t('Couldn\'t fetch following contacts.');
72                         }
73                         DI::pConfig()->set($uid, 'ostatus', 'legacy_friends', $friends);
74                 } elseif ($apcontact = APContact::getByURL($contact['url'])) {
75                         if (empty($apcontact['following'])) {
76                                 DI::pConfig()->delete($uid, 'ostatus', 'legacy_contact');
77                                 return $o . DI::l10n()->t('Couldn\'t fetch remote profile.');
78                         }
79                         $followings = ActivityPub::fetchItems($apcontact['following']);
80                         if (empty($followings)) {
81                                 DI::pConfig()->delete($uid, 'ostatus', 'legacy_contact');
82                                 return $o . DI::l10n()->t('Couldn\'t fetch following contacts.');
83                         }
84                         DI::pConfig()->set($uid, 'ostatus', 'legacy_friends', json_encode($followings));
85                 } else {
86                         DI::pConfig()->delete($uid, 'ostatus', 'legacy_contact');
87                         return $o . DI::l10n()->t('Unsupported network');
88                 }
89         }
90
91         $friends = json_decode(DI::pConfig()->get($uid, 'ostatus', 'legacy_friends'));
92
93         if (empty($friends)) {
94                 $friends = [];
95         }
96
97         $total = sizeof($friends);
98
99         if ($counter >= $total) {
100                 DI::page()['htmlhead'] = '<meta http-equiv="refresh" content="0; URL=' . DI::baseUrl() . '/settings/connectors">';
101                 DI::pConfig()->delete($uid, 'ostatus', 'legacy_friends');
102                 DI::pConfig()->delete($uid, 'ostatus', 'legacy_contact');
103                 $o .= DI::l10n()->t('Done');
104                 return $o;
105         }
106
107         $friend = $friends[$counter++];
108
109         $url = $friend->statusnet_profile_url ?? $friend;
110
111         $o .= '<p>' . $counter . '/' . $total . ': ' . $url;
112
113         $probed = Contact::getByURL($url);
114         if (in_array($probed['network'], Protocol::FEDERATED)) {
115                 $result = Contact::createFromProbeForUser($a->getLoggedInUserId(), $probed['url']);
116                 if ($result['success']) {
117                         $o .= ' - ' . DI::l10n()->t('success');
118                 } else {
119                         $o .= ' - ' . DI::l10n()->t('failed');
120                 }
121         } else {
122                 $o .= ' - ' . DI::l10n()->t('ignored');
123         }
124
125         $o .= '</p>';
126
127         $o .= '<p>' . DI::l10n()->t('Keep this window open until done.') . '</p>';
128
129         DI::page()['htmlhead'] = '<meta http-equiv="refresh" content="0; URL=' . DI::baseUrl() . '/ostatus_subscribe?counter=' . $counter . '">';
130
131         return $o;
132 }