]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/Nodeinfo/actions/nodeinfo_2_0.php
[Nodeinfo] Fix twitterimport enabled check
[quix0rs-gnu-social.git] / plugins / Nodeinfo / actions / nodeinfo_2_0.php
1 <?php
2
3 if (!defined('GNUSOCIAL')) {
4     exit(1);
5 }
6
7 class Nodeinfo_2_0Action extends ApiAction
8 {
9     private $plugins;
10
11     protected function handle()
12     {
13         parent::handle();
14
15         $this->plugins = $this->getActivePluginList();
16
17         $this->showNodeInfo();
18     }
19
20     public function getActivePluginList()
21     {
22         $pluginversions = array();
23         $plugins = array();
24
25         Event::handle('PluginVersion', array(&$pluginversions));
26
27         foreach ($pluginversions as $plugin) {
28             $plugins[strtolower($plugin['name'])] = 1;
29         }
30
31         return $plugins;
32     }
33
34     /*
35      * Technically, the NodeInfo spec defines 'active' as 'signed in at least once',
36      * but GNU social doesn't keep track of when users last logged in, so let's return
37      * the number of users that 'posted at least once', I guess.
38      */
39
40     public function showNodeInfo()
41     {
42         $openRegistrations = $this->getRegistrationsStatus();
43         $userCount = $this->getUserCount();
44         $postCount = $this->getPostCount();
45         $commentCount = $this->getCommentCount();
46
47         $usersActiveHalfyear = $this->getActiveUsers(180);
48         $usersActiveMonth = $this->getActiveUsers(30);
49
50         $protocols = $this->getProtocols();
51         $inboundServices = $this->getInboundServices();
52         $outboundServices = $this->getOutboundServices();
53
54         $json = json_encode([
55             'version' => '2.0',
56
57             'software' => [
58                 'name' => 'gnusocial',
59                 'version' => GNUSOCIAL_VERSION
60             ],
61
62             'protocols' => $protocols,
63
64             // TODO: Have plugins register services
65             'services' => [
66                 'inbound' => $inboundServices,
67                 'outbound' => $outboundServices
68             ],
69
70             'openRegistrations' => $openRegistrations,
71
72             'usage' => [
73                 'users' => [
74                     'total' => $userCount,
75                     'activeHalfyear' => $usersActiveHalfyear,
76                     'activeMonth' => $usersActiveMonth
77                 ],
78                 'localPosts' => $postCount,
79                 'localComments' => $commentCount
80             ],
81
82             'metadata' => new stdClass()
83         ]);
84
85         $this->initDocument('json');
86         print $json;
87         $this->endDocument('json');
88     }
89
90     public function getRegistrationsStatus()
91     {
92         $areRegistrationsClosed = (common_config('site', 'closed')) ? true : false;
93         $isSiteInviteOnly = (common_config('site', 'inviteonly')) ? true : false;
94
95         return !($areRegistrationsClosed || $isSiteInviteOnly);
96     }
97
98     public function getUserCount()
99     {
100         $users = new Usage_stats();
101         $userCount = $users->getUserCount();
102
103         return $userCount;
104     }
105
106     public function getPostCount()
107     {
108         $posts = new Usage_stats();
109         $postCount = $posts->getPostCount();
110
111         return $postCount;
112     }
113
114     public function getCommentCount()
115     {
116         $comments = new Usage_stats();
117         $commentCount = $comments->getCommentCount();
118
119         return $commentCount;
120     }
121
122     public function getActiveUsers($days)
123     {
124         $notices = new Notice();
125         $notices->joinAdd(array('profile_id', 'user:id'));
126         $notices->whereAdd('notice.created >= NOW() - INTERVAL ' . $days . ' DAY');
127
128         $activeUsersCount = $notices->count('distinct profile_id');
129
130         return $activeUsersCount;
131     }
132
133     public function getProtocols()
134     {
135         $protocols = [];
136
137         Event::handle('NodeInfoProtocols', array(&$protocols));
138
139         return $protocols;
140     }
141
142     public function getInboundServices()
143     {
144         // FIXME: Are those always on?
145         $inboundServices = array('atom1.0', 'rss2.0');
146
147         if (array_key_exists('twitterbridge', $this->plugins) && common_config('twitterimport', 'enabled')) {
148             $inboundServices[] = 'twitter';
149         }
150
151         if (array_key_exists('ostatus', $this->plugins)) {
152             $inboundServices[] = 'gnusocial';
153         }
154
155         return $inboundServices;
156     }
157
158     public function getOutboundServices()
159     {
160         $xmppEnabled = (array_key_exists('xmpp', $this->plugins) && common_config('xmpp', 'enabled')) ? true : false;
161
162         // FIXME: Are those always on?
163         $outboundServices = array('atom1.0', 'rss2.0');
164
165         if (array_key_exists('twitterbridge', $this->plugins)) {
166             $outboundServices[] = 'twitter';
167         }
168
169         if (array_key_exists('ostatus', $this->plugins)) {
170             $outboundServices[] = 'gnusocial';
171         }
172
173         if ($xmppEnabled) {
174             $outboundServices[] = 'xmpp';
175         }
176
177         return $outboundServices;
178     }
179 }