| 
									
										
										
										
											2019-04-22 06:48:51 +01:00
										 |  |  | <?php | 
					
						
							| 
									
										
										
										
											2019-08-12 04:45:25 +01:00
										 |  |  | // This file is part of GNU social - https://www.gnu.org/software/social
 | 
					
						
							|  |  |  | //
 | 
					
						
							|  |  |  | // GNU social is free software: you can redistribute it and/or modify
 | 
					
						
							|  |  |  | // it under the terms of the GNU Affero General Public License as published by
 | 
					
						
							|  |  |  | // the Free Software Foundation, either version 3 of the License, or
 | 
					
						
							|  |  |  | // (at your option) any later version.
 | 
					
						
							|  |  |  | //
 | 
					
						
							|  |  |  | // GNU social is distributed in the hope that it will be useful,
 | 
					
						
							|  |  |  | // but WITHOUT ANY WARRANTY; without even the implied warranty of
 | 
					
						
							|  |  |  | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 | 
					
						
							|  |  |  | // GNU Affero General Public License for more details.
 | 
					
						
							|  |  |  | //
 | 
					
						
							|  |  |  | // You should have received a copy of the GNU Affero General Public License
 | 
					
						
							|  |  |  | // along with GNU social.  If not, see <http://www.gnu.org/licenses/>.
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /** | 
					
						
							|  |  |  |  * Plugin that presents basic instance information using the [NodeInfo standard](http://nodeinfo.diaspora.software/). | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * @package   NodeInfo | 
					
						
							|  |  |  |  * @author    Stéphane Bérubé <chimo@chromic.org> | 
					
						
							|  |  |  |  * @author    Diogo Cordeiro <diogo@fc.up.pt> | 
					
						
							|  |  |  |  * @copyright 2018-2019 Free Software Foundation, Inc http://www.fsf.org | 
					
						
							|  |  |  |  * @license   https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | defined('GNUSOCIAL') || die(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /** | 
					
						
							|  |  |  |  * Controls cache and routes | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * @copyright 2018-2019 Free Software Foundation, Inc http://www.fsf.org | 
					
						
							|  |  |  |  * @license   https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later | 
					
						
							|  |  |  |  */ | 
					
						
							| 
									
										
										
										
											2019-04-22 06:48:51 +01:00
										 |  |  | class NodeinfoPlugin extends Plugin | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2019-08-12 04:45:25 +01:00
										 |  |  |     const PLUGIN_VERSION = '2.0.0'; | 
					
						
							| 
									
										
										
										
											2019-04-22 06:48:51 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-12 04:45:25 +01:00
										 |  |  |     public function onRouterInitialized($m): bool | 
					
						
							| 
									
										
										
										
											2019-04-22 06:48:51 +01:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2019-08-12 04:45:25 +01:00
										 |  |  |         $m->connect( | 
					
						
							|  |  |  |             '.well-known/nodeinfo', | 
					
						
							|  |  |  |             ['action' => 'nodeinfojrd'] | 
					
						
							|  |  |  |         ); | 
					
						
							| 
									
										
										
										
											2019-04-22 06:48:51 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-12 04:45:25 +01:00
										 |  |  |         $m->connect( | 
					
						
							|  |  |  |             'api/nodeinfo/2.0.json', | 
					
						
							|  |  |  |             ['action' => 'nodeinfo_2_0'] | 
					
						
							|  |  |  |         ); | 
					
						
							| 
									
										
										
										
											2019-04-22 06:48:51 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |         return true; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-04-22 17:22:17 +01:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * Make sure necessary tables are filled out. | 
					
						
							|  |  |  |      * | 
					
						
							| 
									
										
										
										
											2019-08-12 04:45:25 +01:00
										 |  |  |      * @return bool hook true | 
					
						
							|  |  |  |      * @author Diogo Cordeiro <diogo@fc.up.pt> | 
					
						
							| 
									
										
										
										
											2019-04-22 17:22:17 +01:00
										 |  |  |      */ | 
					
						
							| 
									
										
										
										
											2019-08-12 04:45:25 +01:00
										 |  |  |     public function onCheckSchema(): bool | 
					
						
							| 
									
										
										
										
											2019-04-22 17:22:17 +01:00
										 |  |  |     { | 
					
						
							|  |  |  |         // Ensure schema
 | 
					
						
							|  |  |  |         $schema = Schema::get(); | 
					
						
							|  |  |  |         $schema->ensureTable('usage_stats', Usage_stats::schemaDef()); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         // Ensure default rows
 | 
					
						
							|  |  |  |         if (Usage_stats::getKV('type', 'users') == null) { | 
					
						
							|  |  |  |             $us = new Usage_stats(); | 
					
						
							|  |  |  |             $us->type = 'users'; | 
					
						
							|  |  |  |             $us->insert(); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if (Usage_stats::getKV('type', 'posts') == null) { | 
					
						
							|  |  |  |             $us = new Usage_stats(); | 
					
						
							|  |  |  |             $us->type = 'posts'; | 
					
						
							|  |  |  |             $us->insert(); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if (Usage_stats::getKV('type', 'comments') == null) { | 
					
						
							|  |  |  |             $us = new Usage_stats(); | 
					
						
							|  |  |  |             $us->type = 'comments'; | 
					
						
							|  |  |  |             $us->insert(); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return true; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * Increment notices/replies counter | 
					
						
							|  |  |  |      * | 
					
						
							| 
									
										
										
										
											2019-08-12 04:45:25 +01:00
										 |  |  |      * @param  Notice $notice | 
					
						
							|  |  |  |      * @return bool hook flag | 
					
						
							| 
									
										
										
										
											2019-04-22 17:22:17 +01:00
										 |  |  |      * @author Diogo Cordeiro <diogo@fc.up.pt> | 
					
						
							|  |  |  |      */ | 
					
						
							| 
									
										
										
										
											2019-08-12 04:45:25 +01:00
										 |  |  |     public function onStartNoticeDistribute(Notice $notice): bool | 
					
						
							| 
									
										
										
										
											2019-04-22 17:22:17 +01:00
										 |  |  |     { | 
					
						
							|  |  |  |         assert($notice->id > 0);        // Ignore if not a valid notice
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $profile = $notice->getProfile(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if (!$profile->isLocal()) { | 
					
						
							|  |  |  |             return true; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-12 04:45:25 +01:00
										 |  |  |         // Ignore for activity/non-(post/share)-verb notices
 | 
					
						
							| 
									
										
										
										
											2019-04-22 17:22:17 +01:00
										 |  |  |         if (method_exists('ActivityUtils', 'compareVerbs')) { | 
					
						
							| 
									
										
										
										
											2019-08-12 04:45:25 +01:00
										 |  |  |             $is_valid_verb = ActivityUtils::compareVerbs( | 
					
						
							| 
									
										
										
										
											2019-04-22 17:22:17 +01:00
										 |  |  |                 $notice->verb, | 
					
						
							| 
									
										
										
										
											2019-08-12 04:45:25 +01:00
										 |  |  |                 [ActivityVerb::POST, | 
					
						
							|  |  |  |                  ActivityVerb::SHARE] | 
					
						
							| 
									
										
										
										
											2019-04-22 17:22:17 +01:00
										 |  |  |             ); | 
					
						
							|  |  |  |         } else { | 
					
						
							| 
									
										
										
										
											2019-08-12 04:45:25 +01:00
										 |  |  |             $is_valid_verb = ($notice->verb == ActivityVerb::POST || | 
					
						
							|  |  |  |                               $notice->verb == ActivityVerb::SHARE); | 
					
						
							| 
									
										
										
										
											2019-04-22 17:22:17 +01:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2019-08-12 04:45:25 +01:00
										 |  |  |         if ($notice->source == 'activity' || !$is_valid_verb) { | 
					
						
							| 
									
										
										
										
											2019-04-22 17:22:17 +01:00
										 |  |  |             return true; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         // Is a reply?
 | 
					
						
							|  |  |  |         if ($notice->reply_to) { | 
					
						
							|  |  |  |             $us = Usage_stats::getKV('type', 'comments'); | 
					
						
							|  |  |  |             $us->count += 1; | 
					
						
							|  |  |  |             $us->update(); | 
					
						
							|  |  |  |             return true; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         // Is an Announce?
 | 
					
						
							|  |  |  |         if ($notice->isRepeat()) { | 
					
						
							|  |  |  |             return true; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $us = Usage_stats::getKV('type', 'posts'); | 
					
						
							|  |  |  |         $us->count += 1; | 
					
						
							|  |  |  |         $us->update(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         // That was it
 | 
					
						
							|  |  |  |         return true; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * Decrement notices/replies counter | 
					
						
							|  |  |  |      * | 
					
						
							| 
									
										
										
										
											2019-08-12 04:45:25 +01:00
										 |  |  |      * @param  User $user | 
					
						
							|  |  |  |      * @param  Notice $notice | 
					
						
							|  |  |  |      * @return bool hook flag | 
					
						
							|  |  |  |      * @throws UserNoProfileException | 
					
						
							| 
									
										
										
										
											2019-04-22 17:22:17 +01:00
										 |  |  |      * @author Diogo Cordeiro <diogo@fc.up.pt> | 
					
						
							|  |  |  |      */ | 
					
						
							| 
									
										
										
										
											2019-08-12 04:45:25 +01:00
										 |  |  |     public function onStartDeleteOwnNotice(User $user, Notice $notice): bool | 
					
						
							| 
									
										
										
										
											2019-04-22 17:22:17 +01:00
										 |  |  |     { | 
					
						
							|  |  |  |         $profile = $user->getProfile(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         // Only count local notices
 | 
					
						
							|  |  |  |         if (!$profile->isLocal()) { | 
					
						
							|  |  |  |             return true; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if ($notice->reply_to) { | 
					
						
							|  |  |  |             $us = Usage_stats::getKV('type', 'comments'); | 
					
						
							|  |  |  |             $us->count -= 1; | 
					
						
							|  |  |  |             $us->update(); | 
					
						
							|  |  |  |             return true; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $us = Usage_stats::getKV('type', 'posts'); | 
					
						
							|  |  |  |         $us->count -= 1; | 
					
						
							|  |  |  |         $us->update(); | 
					
						
							|  |  |  |         return true; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * Increment users counter | 
					
						
							|  |  |  |      * | 
					
						
							| 
									
										
										
										
											2019-08-12 04:45:25 +01:00
										 |  |  |      * @return bool hook flag | 
					
						
							| 
									
										
										
										
											2019-04-22 17:22:17 +01:00
										 |  |  |      * @author Diogo Cordeiro <diogo@fc.up.pt> | 
					
						
							|  |  |  |      */ | 
					
						
							| 
									
										
										
										
											2019-08-12 04:45:25 +01:00
										 |  |  |     public function onEndRegistrationTry(): bool | 
					
						
							| 
									
										
										
										
											2019-04-22 17:22:17 +01:00
										 |  |  |     { | 
					
						
							|  |  |  |         $us = Usage_stats::getKV('type', 'users'); | 
					
						
							|  |  |  |         $us->count += 1; | 
					
						
							|  |  |  |         $us->update(); | 
					
						
							|  |  |  |         return true; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * Decrement users counter | 
					
						
							|  |  |  |      * | 
					
						
							| 
									
										
										
										
											2019-08-12 04:45:25 +01:00
										 |  |  |      * @return bool hook flag | 
					
						
							| 
									
										
										
										
											2019-04-22 17:22:17 +01:00
										 |  |  |      * @author Diogo Cordeiro <diogo@fc.up.pt> | 
					
						
							|  |  |  |      */ | 
					
						
							| 
									
										
										
										
											2019-08-12 04:45:25 +01:00
										 |  |  |     public function onEndDeleteUser(): bool | 
					
						
							| 
									
										
										
										
											2019-04-22 17:22:17 +01:00
										 |  |  |     { | 
					
						
							|  |  |  |         $us = Usage_stats::getKV('type', 'users'); | 
					
						
							|  |  |  |         $us->count -= 1; | 
					
						
							|  |  |  |         $us->update(); | 
					
						
							|  |  |  |         return true; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-12 04:45:25 +01:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * Plugin version information | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @param  array $versions | 
					
						
							|  |  |  |      * @return bool hook true | 
					
						
							|  |  |  |      * @throws Exception | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function onPluginVersion(array &$versions): bool | 
					
						
							| 
									
										
										
										
											2019-04-22 06:48:51 +01:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2019-08-12 04:45:25 +01:00
										 |  |  |         $versions[] = [ | 
					
						
							|  |  |  |             'name' => 'Nodeinfo', | 
					
						
							| 
									
										
										
										
											2019-06-05 10:04:59 +01:00
										 |  |  |             'version' => self::PLUGIN_VERSION, | 
					
						
							| 
									
										
										
										
											2019-08-12 04:45:25 +01:00
										 |  |  |             'author' => 'Stéphane Bérubé, Diogo Cordeiro', | 
					
						
							|  |  |  |             'homepage' => 'https://code.chromic.org/chimo/gs-nodeinfo', | 
					
						
							|  |  |  |             'description' => _m('Plugin that presents basic instance information using the NodeInfo standard.') | 
					
						
							|  |  |  |         ]; | 
					
						
							| 
									
										
										
										
											2019-04-22 06:48:51 +01:00
										 |  |  |         return true; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2019-06-03 02:39:39 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-12 04:45:25 +01:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * Cache was added in a newer version of the plugin, this ensures we fix cached values on upgrade | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @return bool hook flag | 
					
						
							|  |  |  |      * @author Diogo Cordeiro <diogo@fc.up.pt> | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function onEndUpgrade(): bool | 
					
						
							| 
									
										
										
										
											2019-06-03 02:39:39 +01:00
										 |  |  |     { | 
					
						
							|  |  |  |         $users = new Usage_stats(); | 
					
						
							|  |  |  |         if ($users->getUserCount() == 0) { | 
					
						
							|  |  |  |             define('NODEINFO_UPGRADE', true); | 
					
						
							|  |  |  |             require_once __DIR__ . DIRECTORY_SEPARATOR . 'scripts' . DIRECTORY_SEPARATOR . 'fix_stats.php'; | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2019-08-12 04:45:25 +01:00
										 |  |  |         return true; | 
					
						
							| 
									
										
										
										
											2019-06-03 02:39:39 +01:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2019-04-22 06:48:51 +01:00
										 |  |  | } |