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