]> git.mxchange.org Git - friendica.git/blob - mod/nodeinfo.php
Moved the rss/atom part to the protocols.
[friendica.git] / mod / nodeinfo.php
1 <?php
2 /*
3 Documentation: http://nodeinfo.diaspora.software/schema.html
4 */
5
6 require_once("include/plugin.php");
7
8 function nodeinfo_wellknown(&$a) {
9         if (!get_config("system", "nodeinfo")) {
10                 http_status_exit(404);
11                 killme();
12         }
13         $nodeinfo = array("links" => array("rel" => "http://nodeinfo.diaspora.software/ns/schema/1.0",
14                                         "href" => $a->get_baseurl()."/nodeinfo/1.0"));
15
16         header('Content-type: application/json; charset=utf-8');
17         echo json_encode($nodeinfo, JSON_PRETTY_PRINT|JSON_UNESCAPED_SLASHES);
18         exit;
19 }
20
21 function nodeinfo_init(&$a){
22         if (!get_config("system", "nodeinfo")) {
23                 http_status_exit(404);
24                 killme();
25         }
26
27         if (($a->argc != 2) OR ($a->argv[1] != "1.0")) {
28                 http_status_exit(404);
29                 killme();
30         }
31
32         $smtp = (function_exists("imap_open") AND !get_config("system","imap_disabled") AND !get_config("system","dfrn_only"));
33
34         $nodeinfo = array();
35         $nodeinfo["version"] = "1.0";
36         $nodeinfo["software"] = array("name" => "friendica", "version" => FRIENDICA_VERSION."-".DB_UPDATE_VERSION);
37
38         $nodeinfo["protocols"] = array();
39         $nodeinfo["protocols"]["inbound"] = array();
40         $nodeinfo["protocols"]["outbound"] = array();
41
42         if (get_config("system","diaspora_enabled")) {
43                 $nodeinfo["protocols"]["inbound"][] = "diaspora";
44                 $nodeinfo["protocols"]["outbound"][] = "diaspora";
45         }
46
47         $nodeinfo["protocols"]["inbound"][] = "friendica";
48         $nodeinfo["protocols"]["outbound"][] = "friendica";
49
50         if (!get_config("system","ostatus_disabled")) {
51                 $nodeinfo["protocols"]["inbound"][] = "gnusocial";
52                 $nodeinfo["protocols"]["outbound"][] = "gnusocial";
53         }
54
55         $nodeinfo["services"] = array();
56         $nodeinfo["services"]["inbound"] = array();
57         $nodeinfo["services"]["outbound"] = array();
58
59         $nodeinfo["openRegistrations"] = ($a->config['register_policy'] != 0);
60
61         $nodeinfo["usage"] = array();
62         $nodeinfo["usage"]["users"] = array("total" => (int)get_config("nodeinfo","total_users"),
63                                 "activeHalfyear" => (int)get_config("nodeinfo","active_users_halfyear"),
64                                 "activeMonth" => (int)get_config("nodeinfo","active_users_monthly"));
65         $nodeinfo["usage"]["localPosts"] = (int)get_config("nodeinfo","local_posts");
66         $nodeinfo["usage"]["localComments"] = (int)get_config("nodeinfo","local_comments");
67
68         $nodeinfo["metadata"] = array("nodeName" => $a->config["sitename"]);
69
70         if (nodeinfo_plugin_enabled("appnet") OR nodeinfo_plugin_enabled("buffer")) {
71                 $nodeinfo["services"]["inbound"][] = "appnet";
72                 $nodeinfo["services"]["outbound"][] = "appnet";
73         }
74
75         if (nodeinfo_plugin_enabled("blogger"))
76                 $nodeinfo["services"]["outbound"][] = "blogger";
77
78         if (nodeinfo_plugin_enabled("dwpost"))
79                 $nodeinfo["services"]["outbound"][] = "dreamwidth";
80
81         if (nodeinfo_plugin_enabled("fbpost") OR nodeinfo_plugin_enabled("buffer"))
82                 $nodeinfo["services"]["outbound"][] = "facebook";
83
84         if (nodeinfo_plugin_enabled("statusnet")) {
85                 $nodeinfo["services"]["inbound"][] = "gnusocial";
86                 $nodeinfo["services"]["outbound"][] = "gnusocial";
87         }
88
89         if (nodeinfo_plugin_enabled("gpluspost") OR nodeinfo_plugin_enabled("buffer"))
90                 $nodeinfo["services"]["outbound"][] = "google";
91
92         if (nodeinfo_plugin_enabled("ijpost"))
93                 $nodeinfo["services"]["outbound"][] = "insanejournal";
94
95         if (nodeinfo_plugin_enabled("libertree"))
96                 $nodeinfo["services"]["outbound"][] = "libertree";
97
98         if (nodeinfo_plugin_enabled("buffer"))
99                 $nodeinfo["services"]["outbound"][] = "linkedin";
100
101         if (nodeinfo_plugin_enabled("ljpost"))
102                 $nodeinfo["services"]["outbound"][] = "livejournal";
103
104         if (nodeinfo_plugin_enabled("buffer"))
105                 $nodeinfo["services"]["outbound"][] = "pinterest";
106
107         if (nodeinfo_plugin_enabled("posterous"))
108                 $nodeinfo["services"]["outbound"][] = "posterous";
109
110         if (nodeinfo_plugin_enabled("pumpio")) {
111                 $nodeinfo["services"]["inbound"][] = "pumpio";
112                 $nodeinfo["services"]["outbound"][] = "pumpio";
113         }
114
115         // redmatrix
116
117         if ($smtp)
118                 $nodeinfo["services"]["outbound"][] = "smtp";
119
120         if (nodeinfo_plugin_enabled("tumblr"))
121                 $nodeinfo["services"]["outbound"][] = "tumblr";
122
123         if (nodeinfo_plugin_enabled("twitter"))
124                 $nodeinfo["services"]["outbound"][] = "twitter";
125
126         if (nodeinfo_plugin_enabled("wppost"))
127                 $nodeinfo["services"]["outbound"][] = "wordpress";
128
129         $nodeinfo["metadata"]["protocols"] = $nodeinfo["protocols"];
130         $nodeinfo["metadata"]["protocols"]["outbound"][] = "atom1.0";
131         $nodeinfo["metadata"]["protocols"]["inbound"][] = "atom1.0";
132         $nodeinfo["metadata"]["protocols"]["inbound"][] = "rss2.0";
133
134         $nodeinfo["metadata"]["services"] = $nodeinfo["services"];
135
136         if (nodeinfo_plugin_enabled("twitter"))
137                 $nodeinfo["metadata"]["services"]["inbound"][] = "twitter";
138
139         header('Content-type: application/json; charset=utf-8');
140         echo json_encode($nodeinfo, JSON_PRETTY_PRINT|JSON_UNESCAPED_SLASHES);
141         exit;
142 }
143
144 function nodeinfo_plugin_enabled($plugin) {
145         $r = q("SELECT * FROM `addon` WHERE `installed` = 1 AND `name` = '%s'", $plugin);
146         return((bool)(count($r) > 0));
147 }
148
149 function nodeinfo_cron() {
150
151         $a = get_app();
152
153         // If the plugin "statistics_json" is enabled then disable it and actrivate nodeinfo.
154         if (nodeinfo_plugin_enabled("statistics_json")) {
155                 set_config("system", "nodeinfo", true);
156
157                 $plugin = "statistics_json";
158                 $plugins = get_config("system","addon");
159                 $plugins_arr = array();
160
161                 if($plugins) {
162                         $plugins_arr = explode(",",str_replace(" ", "",$plugins));
163
164                         $idx = array_search($plugin, $plugins_arr);
165                         if ($idx !== false){
166                                 unset($plugins_arr[$idx]);
167                                 uninstall_plugin($plugin);
168                                 set_config("system","addon", implode(", ",$plugins_arr));
169                         }
170                 }
171         }
172
173         if (!get_config("system", "nodeinfo"))
174                 return;
175
176         $last = get_config('nodeinfo','last_calucation');
177
178         if($last) {
179                 // Calculate every 24 hours
180                 $next = $last + (24 * 60 * 60);
181                 if($next > time()) {
182                         logger("calculation intervall not reached");
183                         return;
184                 }
185         }
186         logger("cron_start");
187
188         $users = q("SELECT profile.*, `user`.`login_date`, `lastitem`.`lastitem_date`
189                         FROM (SELECT MAX(`item`.`changed`) as `lastitem_date`, `item`.`uid`
190                                 FROM `item`
191                                         WHERE `item`.`type` = 'wall'
192                                                 GROUP BY `item`.`uid`) AS `lastitem`
193                                                 RIGHT OUTER JOIN `user` ON `user`.`uid` = `lastitem`.`uid`, `contact`, `profile`
194                                 WHERE
195                                         `user`.`uid` = `contact`.`uid` AND `profile`.`uid` = `user`.`uid`
196                                         AND `profile`.`is-default` AND (`profile`.`publish` OR `profile`.`net-publish`)
197                                         AND `user`.`verified` AND `contact`.`self`
198                                         AND NOT `user`.`blocked`
199                                         AND NOT `user`.`account_removed`
200                                         AND NOT `user`.`account_expired`");
201
202         if (is_array($users)) {
203                         $total_users = count($users);
204                         $active_users_halfyear = 0;
205                         $active_users_monthly = 0;
206
207                         $halfyear = time() - (180 * 24 * 60 * 60);
208                         $month = time() - (30 * 24 * 60 * 60);
209
210                         foreach ($users AS $user) {
211                                 if ((strtotime($user['login_date']) > $halfyear) OR
212                                         (strtotime($user['lastitem_date']) > $halfyear))
213                                         ++$active_users_halfyear;
214
215                                 if ((strtotime($user['login_date']) > $month) OR
216                                         (strtotime($user['lastitem_date']) > $month))
217                                         ++$active_users_monthly;
218
219                         }
220                         set_config('nodeinfo','total_users', $total_users);
221                         logger("total_users: ".$total_users, LOGGER_DEBUG);
222
223                         set_config('nodeinfo','active_users_halfyear', $active_users_halfyear);
224                         set_config('nodeinfo','active_users_monthly', $active_users_monthly);
225         }
226
227         //$posts = q("SELECT COUNT(*) AS local_posts FROM `item` WHERE `wall` AND `uid` != 0 AND `id` = `parent` AND left(body, 6) != '[share'");
228         $posts = q("SELECT COUNT(*) AS `local_posts` FROM `item`
229                         INNER JOIN `contact` ON `contact`.`id` = `item`.`contact-id`
230                         WHERE `contact`.`self` and `item`.`id` = `item`.`parent` AND left(body, 6) != '[share' AND `item`.`network` IN ('%s', '%s', '%s')",
231                         dbesc(NETWORK_OSTATUS), dbesc(NETWORK_DIASPORA), dbesc(NETWORK_DFRN));
232
233         if (!is_array($posts))
234                 $local_posts = -1;
235         else
236                 $local_posts = $posts[0]["local_posts"];
237
238         set_config('nodeinfo','local_posts', $local_posts);
239
240         logger("local_posts: ".$local_posts, LOGGER_DEBUG);
241
242         $posts = q("SELECT COUNT(*) AS `local_comments` FROM `item`
243                         INNER JOIN `contact` ON `contact`.`id` = `item`.`contact-id`
244                         WHERE `contact`.`self` and `item`.`id` != `item`.`parent` AND `item`.`network` IN ('%s', '%s', '%s')",
245                         dbesc(NETWORK_OSTATUS), dbesc(NETWORK_DIASPORA), dbesc(NETWORK_DFRN));
246
247         if (!is_array($posts))
248                 $local_comments = -1;
249         else
250                 $local_comments = $posts[0]["local_comments"];
251
252         set_config('nodeinfo','local_comments', $local_comments);
253
254         // Now trying to register
255         $url = "http://the-federation.info/register/".$a->get_hostname();
256         logger('registering url: '.$url, LOGGER_DEBUG);
257         $ret = fetch_url($url);
258         logger('registering answer: '.$ret, LOGGER_DEBUG);
259
260         logger("cron_end");
261         set_config('nodeinfo','last_calucation', time());
262 }
263
264 ?>