]> git.mxchange.org Git - friendica.git/blob - mod/nodeinfo.php
forumlist: nodeinfo_plugin_enabled() is now plugin_enabled()
[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(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 (plugin_enabled("appnet"))
71                 $nodeinfo["services"]["inbound"][] = "appnet";
72
73         if (plugin_enabled("appnet") OR plugin_enabled("buffer"))
74                 $nodeinfo["services"]["outbound"][] = "appnet";
75
76         if (plugin_enabled("blogger"))
77                 $nodeinfo["services"]["outbound"][] = "blogger";
78
79         if (plugin_enabled("dwpost"))
80                 $nodeinfo["services"]["outbound"][] = "dreamwidth";
81
82         if (plugin_enabled("fbpost") OR plugin_enabled("buffer"))
83                 $nodeinfo["services"]["outbound"][] = "facebook";
84
85         if (plugin_enabled("statusnet")) {
86                 $nodeinfo["services"]["inbound"][] = "gnusocial";
87                 $nodeinfo["services"]["outbound"][] = "gnusocial";
88         }
89
90         if (plugin_enabled("gpluspost") OR plugin_enabled("buffer"))
91                 $nodeinfo["services"]["outbound"][] = "google";
92
93         if (plugin_enabled("ijpost"))
94                 $nodeinfo["services"]["outbound"][] = "insanejournal";
95
96         if (plugin_enabled("libertree"))
97                 $nodeinfo["services"]["outbound"][] = "libertree";
98
99         if (plugin_enabled("buffer"))
100                 $nodeinfo["services"]["outbound"][] = "linkedin";
101
102         if (plugin_enabled("ljpost"))
103                 $nodeinfo["services"]["outbound"][] = "livejournal";
104
105         if (plugin_enabled("buffer"))
106                 $nodeinfo["services"]["outbound"][] = "pinterest";
107
108         if (plugin_enabled("posterous"))
109                 $nodeinfo["services"]["outbound"][] = "posterous";
110
111         if (plugin_enabled("pumpio")) {
112                 $nodeinfo["services"]["inbound"][] = "pumpio";
113                 $nodeinfo["services"]["outbound"][] = "pumpio";
114         }
115
116         // redmatrix
117
118         if ($smtp)
119                 $nodeinfo["services"]["outbound"][] = "smtp";
120
121         if (plugin_enabled("tumblr"))
122                 $nodeinfo["services"]["outbound"][] = "tumblr";
123
124         if (plugin_enabled("twitter") OR plugin_enabled("buffer"))
125                 $nodeinfo["services"]["outbound"][] = "twitter";
126
127         if (plugin_enabled("wppost"))
128                 $nodeinfo["services"]["outbound"][] = "wordpress";
129
130         $nodeinfo["metadata"]["protocols"] = $nodeinfo["protocols"];
131         $nodeinfo["metadata"]["protocols"]["outbound"][] = "atom1.0";
132         $nodeinfo["metadata"]["protocols"]["inbound"][] = "atom1.0";
133         $nodeinfo["metadata"]["protocols"]["inbound"][] = "rss2.0";
134
135         $nodeinfo["metadata"]["services"] = $nodeinfo["services"];
136
137         if (plugin_enabled("twitter"))
138                 $nodeinfo["metadata"]["services"]["inbound"][] = "twitter";
139
140         header('Content-type: application/json; charset=utf-8');
141         echo json_encode($nodeinfo, JSON_PRETTY_PRINT|JSON_UNESCAPED_SLASHES);
142         exit;
143 }
144
145
146
147 function nodeinfo_cron() {
148
149         $a = get_app();
150
151         // If the plugin "statistics_json" is enabled then disable it and actrivate nodeinfo.
152         if (plugin_enabled("statistics_json")) {
153                 set_config("system", "nodeinfo", true);
154
155                 $plugin = "statistics_json";
156                 $plugins = get_config("system","addon");
157                 $plugins_arr = array();
158
159                 if($plugins) {
160                         $plugins_arr = explode(",",str_replace(" ", "",$plugins));
161
162                         $idx = array_search($plugin, $plugins_arr);
163                         if ($idx !== false){
164                                 unset($plugins_arr[$idx]);
165                                 uninstall_plugin($plugin);
166                                 set_config("system","addon", implode(", ",$plugins_arr));
167                         }
168                 }
169         }
170
171         if (!get_config("system", "nodeinfo"))
172                 return;
173
174         $last = get_config('nodeinfo','last_calucation');
175
176         if($last) {
177                 // Calculate every 24 hours
178                 $next = $last + (24 * 60 * 60);
179                 if($next > time()) {
180                         logger("calculation intervall not reached");
181                         return;
182                 }
183         }
184         logger("cron_start");
185
186         $users = q("SELECT profile.*, `user`.`login_date`, `lastitem`.`lastitem_date`
187                         FROM (SELECT MAX(`item`.`changed`) as `lastitem_date`, `item`.`uid`
188                                 FROM `item`
189                                         WHERE `item`.`type` = 'wall'
190                                                 GROUP BY `item`.`uid`) AS `lastitem`
191                                                 RIGHT OUTER JOIN `user` ON `user`.`uid` = `lastitem`.`uid`, `contact`, `profile`
192                                 WHERE
193                                         `user`.`uid` = `contact`.`uid` AND `profile`.`uid` = `user`.`uid`
194                                         AND `profile`.`is-default` AND (`profile`.`publish` OR `profile`.`net-publish`)
195                                         AND `user`.`verified` AND `contact`.`self`
196                                         AND NOT `user`.`blocked`
197                                         AND NOT `user`.`account_removed`
198                                         AND NOT `user`.`account_expired`");
199
200         if (is_array($users)) {
201                         $total_users = count($users);
202                         $active_users_halfyear = 0;
203                         $active_users_monthly = 0;
204
205                         $halfyear = time() - (180 * 24 * 60 * 60);
206                         $month = time() - (30 * 24 * 60 * 60);
207
208                         foreach ($users AS $user) {
209                                 if ((strtotime($user['login_date']) > $halfyear) OR
210                                         (strtotime($user['lastitem_date']) > $halfyear))
211                                         ++$active_users_halfyear;
212
213                                 if ((strtotime($user['login_date']) > $month) OR
214                                         (strtotime($user['lastitem_date']) > $month))
215                                         ++$active_users_monthly;
216
217                         }
218                         set_config('nodeinfo','total_users', $total_users);
219                         logger("total_users: ".$total_users, LOGGER_DEBUG);
220
221                         set_config('nodeinfo','active_users_halfyear', $active_users_halfyear);
222                         set_config('nodeinfo','active_users_monthly', $active_users_monthly);
223         }
224
225         //$posts = q("SELECT COUNT(*) AS local_posts FROM `item` WHERE `wall` AND `uid` != 0 AND `id` = `parent` AND left(body, 6) != '[share'");
226         $posts = q("SELECT COUNT(*) AS `local_posts` FROM `item`
227                         INNER JOIN `contact` ON `contact`.`id` = `item`.`contact-id`
228                         WHERE `contact`.`self` and `item`.`id` = `item`.`parent` AND left(body, 6) != '[share' AND `item`.`network` IN ('%s', '%s', '%s')",
229                         dbesc(NETWORK_OSTATUS), dbesc(NETWORK_DIASPORA), dbesc(NETWORK_DFRN));
230
231         if (!is_array($posts))
232                 $local_posts = -1;
233         else
234                 $local_posts = $posts[0]["local_posts"];
235
236         set_config('nodeinfo','local_posts', $local_posts);
237
238         logger("local_posts: ".$local_posts, LOGGER_DEBUG);
239
240         $posts = q("SELECT COUNT(*) AS `local_comments` FROM `item`
241                         INNER JOIN `contact` ON `contact`.`id` = `item`.`contact-id`
242                         WHERE `contact`.`self` and `item`.`id` != `item`.`parent` AND `item`.`network` IN ('%s', '%s', '%s')",
243                         dbesc(NETWORK_OSTATUS), dbesc(NETWORK_DIASPORA), dbesc(NETWORK_DFRN));
244
245         if (!is_array($posts))
246                 $local_comments = -1;
247         else
248                 $local_comments = $posts[0]["local_comments"];
249
250         set_config('nodeinfo','local_comments', $local_comments);
251
252         // Now trying to register
253         $url = "http://the-federation.info/register/".$a->get_hostname();
254         logger('registering url: '.$url, LOGGER_DEBUG);
255         $ret = fetch_url($url);
256         logger('registering answer: '.$ret, LOGGER_DEBUG);
257
258         logger("cron_end");
259         set_config('nodeinfo','last_calucation', time());
260 }
261
262 ?>