]> git.mxchange.org Git - friendica.git/blob - src/Model/Nodeinfo.php
84a14a294d5c3ab0f4c2d7c5d90f68bbebddcca7
[friendica.git] / src / Model / Nodeinfo.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2010-2022, the Friendica project
4  *
5  * @license GNU AGPL version 3 or any later version
6  *
7  * This program is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU Affero General Public License as
9  * published by the Free Software Foundation, either version 3 of the
10  * License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU Affero General Public License for more details.
16  *
17  * You should have received a copy of the GNU Affero General Public License
18  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
19  *
20  */
21
22 namespace Friendica\Model;
23
24 use Friendica\Core\Addon;
25 use Friendica\Core\Config\Capability\IManageConfigValues;
26 use Friendica\Database\DBA;
27 use Friendica\DI;
28 use stdClass;
29
30 /**
31  * Model interaction for the nodeinfo
32  */
33 class Nodeinfo
34 {
35         /**
36          * Updates the info about the current node
37          *
38          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
39          */
40         public static function update()
41         {
42                 $config = DI::config();
43                 $logger = DI::logger();
44
45                 // If the addon 'statistics_json' is enabled then disable it and activate nodeinfo.
46                 if (Addon::isEnabled('statistics_json')) {
47                         $config->set('system', 'nodeinfo', true);
48                         Addon::uninstall('statistics_json');
49                 }
50
51                 if (empty($config->get('system', 'nodeinfo'))) {
52                         return;
53                 }
54
55                 $userStats = User::getStatistics();
56
57                 $config->set('nodeinfo', 'total_users', $userStats['total_users']);
58                 $config->set('nodeinfo', 'active_users_halfyear', $userStats['active_users_halfyear']);
59                 $config->set('nodeinfo', 'active_users_monthly', $userStats['active_users_monthly']);
60                 $config->set('nodeinfo', 'active_users_weekly', $userStats['active_users_weekly']);
61
62                 $logger->info('user statistics', $userStats);
63
64                 $posts = DBA::count('post-thread', ["EXISTS(SELECT `uri-id` FROM `post-user` WHERE NOT `deleted` AND `origin` AND `uri-id` = `post-thread`.`uri-id`)"]);
65                 $comments = DBA::count('post', ["NOT `deleted` AND `gravity` = ? AND EXISTS(SELECT `uri-id` FROM `post-user` WHERE `origin` AND `uri-id` = `post`.`uri-id`)", GRAVITY_COMMENT]);
66                 $config->set('nodeinfo', 'local_posts', $posts);
67                 $config->set('nodeinfo', 'local_comments', $comments);
68
69                 $logger->info('User actitivy', ['posts' => $posts, 'comments' => $comments]);
70         }
71
72         /**
73          * Return the supported services
74          *
75          * @return Object with supported services
76         */
77         public static function getUsage(bool $version2 = false)
78         {
79                 $config = DI::config();
80
81                 $usage = new stdClass();
82                 $usage->users = [];
83
84                 if (!empty($config->get('system', 'nodeinfo'))) {
85                         $usage->users = [
86                                 'total'          => intval($config->get('nodeinfo', 'total_users')),
87                                 'activeHalfyear' => intval($config->get('nodeinfo', 'active_users_halfyear')),
88                                 'activeMonth'    => intval($config->get('nodeinfo', 'active_users_monthly'))
89                         ];
90                         $usage->localPosts = intval($config->get('nodeinfo', 'local_posts'));
91                         $usage->localComments = intval($config->get('nodeinfo', 'local_comments'));
92
93                         if ($version2) {
94                                 $usage->users['activeWeek'] = intval($config->get('nodeinfo', 'active_users_weekly'));
95                         }
96                 }
97
98                 return $usage;
99         }
100
101         /**
102          * Return the supported services
103          *
104          * @return array with supported services
105         */
106         public static function getServices(): array
107         {
108                 $services = [
109                         'inbound'  => [],
110                         'outbound' => [],
111                 ];
112
113                 if (Addon::isEnabled('blogger')) {
114                         $services['outbound'][] = 'blogger';
115                 }
116                 if (Addon::isEnabled('dwpost')) {
117                         $services['outbound'][] = 'dreamwidth';
118                 }
119                 if (Addon::isEnabled('statusnet')) {
120                         $services['inbound'][] = 'gnusocial';
121                         $services['outbound'][] = 'gnusocial';
122                 }
123                 if (Addon::isEnabled('ijpost')) {
124                         $services['outbound'][] = 'insanejournal';
125                 }
126                 if (Addon::isEnabled('libertree')) {
127                         $services['outbound'][] = 'libertree';
128                 }
129                 if (Addon::isEnabled('buffer')) {
130                         $services['outbound'][] = 'linkedin';
131                 }
132                 if (Addon::isEnabled('ljpost')) {
133                         $services['outbound'][] = 'livejournal';
134                 }
135                 if (Addon::isEnabled('buffer')) {
136                         $services['outbound'][] = 'pinterest';
137                 }
138                 if (Addon::isEnabled('posterous')) {
139                         $services['outbound'][] = 'posterous';
140                 }
141                 if (Addon::isEnabled('pumpio')) {
142                         $services['inbound'][] = 'pumpio';
143                         $services['outbound'][] = 'pumpio';
144                 }
145
146                 $services['outbound'][] = 'smtp';
147
148                 if (Addon::isEnabled('tumblr')) {
149                         $services['outbound'][] = 'tumblr';
150                 }
151                 if (Addon::isEnabled('twitter') || Addon::isEnabled('buffer')) {
152                         $services['outbound'][] = 'twitter';
153                 }
154                 if (Addon::isEnabled('wppost')) {
155                         $services['outbound'][] = 'wordpress';
156                 }
157
158                 return $services;
159         }
160
161         /**
162          * Gathers organization information and returns it as an array
163          *
164          * @param IManageConfigValues $config Configuration instance
165          * @return array Organization information
166          */
167         public static function getOrganization(IManageConfigValues $config): array
168         {
169                 $organization = [
170                         'name' => null,
171                         'contact' => null,
172                         'account' => null
173                 ];
174
175                 if (!empty($config->get('config', 'admin_email'))) {
176                         $adminList = explode(',', str_replace(' ', '', $config->get('config', 'admin_email')));
177                         $organization['contact'] = $adminList[0];
178                         $administrator = User::getByEmail($adminList[0], ['username', 'nickname']);
179                         if (!empty($administrator)) {
180                                 $organization['name'] = $administrator['username'];
181                                 $organization['account'] = DI::baseUrl()->get() . '/profile/' . $administrator['nickname'];
182                         }
183                 }
184
185                 return $organization;
186         }
187 }