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