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