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