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