]> git.mxchange.org Git - friendica.git/blob - mod/ostatus_subscribe.php
Correct content type
[friendica.git] / mod / ostatus_subscribe.php
1 <?php
2 /**
3  * @file mod/ostatus_subscribe.php
4  */
5 use Friendica\App;
6 use Friendica\Core\L10n;
7 use Friendica\Core\PConfig;
8 use Friendica\Core\Protocol;
9 use Friendica\Core\System;
10 use Friendica\Model\Contact;
11 use Friendica\Network\Probe;
12 use Friendica\Util\Network;
13
14 function ostatus_subscribe_content(App $a) {
15
16         if (! local_user()) {
17                 notice(L10n::t('Permission denied.') . EOL);
18                 goaway($_SESSION['return_url']);
19                 // NOTREACHED
20         }
21
22         $o = "<h2>".L10n::t("Subscribing to OStatus contacts")."</h2>";
23
24         $uid = local_user();
25
26         $a = get_app();
27
28         $counter = intval($_REQUEST['counter']);
29
30         if (PConfig::get($uid, "ostatus", "legacy_friends") == "") {
31
32                 if ($_REQUEST["url"] == "") {
33                         PConfig::delete($uid, "ostatus", "legacy_contact");
34                         return $o.L10n::t("No contact provided.");
35                 }
36
37                 $contact = Probe::uri($_REQUEST["url"]);
38
39                 if (!$contact) {
40                         PConfig::delete($uid, "ostatus", "legacy_contact");
41                         return $o.L10n::t("Couldn't fetch information for contact.");
42                 }
43
44                 $api = $contact["baseurl"]."/api/";
45
46                 // Fetching friends
47                 $data = Network::curl($api."statuses/friends.json?screen_name=".$contact["nick"]);
48
49                 if (!$data["success"]) {
50                         PConfig::delete($uid, "ostatus", "legacy_contact");
51                         return $o.L10n::t("Couldn't fetch friends for contact.");
52                 }
53
54                 PConfig::set($uid, "ostatus", "legacy_friends", $data["body"]);
55         }
56
57         $friends = json_decode(PConfig::get($uid, "ostatus", "legacy_friends"));
58
59         $total = sizeof($friends);
60
61         if ($counter >= $total) {
62                 $a->page['htmlhead'] = '<meta http-equiv="refresh" content="0; URL='.System::baseUrl().'/settings/connectors">';
63                 PConfig::delete($uid, "ostatus", "legacy_friends");
64                 PConfig::delete($uid, "ostatus", "legacy_contact");
65                 $o .= L10n::t("Done");
66                 return $o;
67         }
68
69         $friend = $friends[$counter++];
70
71         $url = $friend->statusnet_profile_url;
72
73         $o .= "<p>".$counter."/".$total.": ".$url;
74
75         $data = Probe::uri($url);
76         if ($data["network"] == Protocol::OSTATUS) {
77                 $result = Contact::createFromProbe($uid, $url, true, Protocol::OSTATUS);
78                 if ($result["success"]) {
79                         $o .= " - ".L10n::t("success");
80                 } else {
81                         $o .= " - ".L10n::t("failed");
82                 }
83         } else {
84                 $o .= " - ".L10n::t("ignored");
85         }
86
87         $o .= "</p>";
88
89         $o .= "<p>".L10n::t("Keep this window open until done.")."</p>";
90
91         $a->page['htmlhead'] = '<meta http-equiv="refresh" content="0; URL='.System::baseUrl().'/ostatus_subscribe?counter='.$counter.'">';
92
93         return $o;
94 }