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