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