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