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