| 
									
										
										
										
											2012-07-10 12:25:19 -04:00
										 |  |  | <?php | 
					
						
							| 
									
										
										
										
											2019-09-11 11:25:39 +03: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/>.
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-07-10 12:25:19 -04:00
										 |  |  | /** | 
					
						
							|  |  |  |  * @category  Moderation | 
					
						
							| 
									
										
										
										
											2019-09-11 11:25:39 +03:00
										 |  |  |  * @package   GNUsocial | 
					
						
							| 
									
										
										
										
											2012-07-10 12:25:19 -04:00
										 |  |  |  * @author    Evan Prodromou <evan@status.net> | 
					
						
							|  |  |  |  * @copyright 2012 StatusNet, Inc. | 
					
						
							| 
									
										
										
										
											2019-09-11 11:25:39 +03:00
										 |  |  |  * @license   https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later | 
					
						
							| 
									
										
										
										
											2012-07-10 12:25:19 -04:00
										 |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-09-11 11:25:39 +03:00
										 |  |  | defined('GNUSOCIAL') || die(); | 
					
						
							| 
									
										
										
										
											2012-07-10 12:25:19 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | /** | 
					
						
							|  |  |  |  * Moderation logging | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * Shows a history of moderation for this user in the sidebar | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * @copyright 2012 StatusNet, Inc. | 
					
						
							| 
									
										
										
										
											2019-09-11 11:25:39 +03:00
										 |  |  |  * @license   https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later | 
					
						
							| 
									
										
										
										
											2012-07-10 12:25:19 -04:00
										 |  |  |  */ | 
					
						
							|  |  |  | class ModLogPlugin extends Plugin | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2019-06-03 01:56:52 +01:00
										 |  |  |     const PLUGIN_VERSION = '2.0.0'; | 
					
						
							| 
									
										
										
										
											2012-07-10 12:25:19 -04:00
										 |  |  |     const VIEWMODLOG = 'ModLogPlugin::VIEWMODLOG'; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * Database schema setup | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * We keep a moderation log table | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @see Schema | 
					
						
							|  |  |  |      * @see ColumnDef | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @return boolean hook value; true means continue processing, false means stop. | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-09-11 11:25:39 +03:00
										 |  |  |     public function onCheckSchema() | 
					
						
							| 
									
										
										
										
											2012-07-10 12:25:19 -04:00
										 |  |  |     { | 
					
						
							|  |  |  |         $schema = Schema::get(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $schema->ensureTable('mod_log', ModLog::schemaDef()); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return true; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-09-11 11:25:39 +03:00
										 |  |  |     public function onEndGrantRole($profile, $role) | 
					
						
							| 
									
										
										
										
											2012-07-10 12:25:19 -04:00
										 |  |  |     { | 
					
						
							|  |  |  |         $modlog = new ModLog(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $modlog->id         = UUID::gen(); | 
					
						
							|  |  |  |         $modlog->profile_id = $profile->id; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $cur = common_current_user(); | 
					
						
							|  |  |  |          | 
					
						
							|  |  |  |         if (!empty($cur)) { | 
					
						
							|  |  |  |             $modlog->moderator_id = $cur->id; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $modlog->role     = $role; | 
					
						
							| 
									
										
										
										
											2019-09-11 11:25:39 +03:00
										 |  |  |         $modlog->is_grant = true; | 
					
						
							| 
									
										
										
										
											2012-07-10 12:25:19 -04:00
										 |  |  |         $modlog->created  = common_sql_now(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $modlog->insert(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return true; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-09-11 11:25:39 +03:00
										 |  |  |     public function onEndRevokeRole($profile, $role) | 
					
						
							| 
									
										
										
										
											2012-07-10 12:25:19 -04:00
										 |  |  |     { | 
					
						
							|  |  |  |         $modlog = new ModLog(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $modlog->id = UUID::gen(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $modlog->profile_id = $profile->id; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-10 13:44:47 +02:00
										 |  |  |         $scoped = Profile::current(); | 
					
						
							| 
									
										
										
										
											2012-07-10 12:25:19 -04:00
										 |  |  |          | 
					
						
							| 
									
										
										
										
											2015-07-10 13:44:47 +02:00
										 |  |  |         if ($scoped instanceof Profile) { | 
					
						
							|  |  |  |             $modlog->moderator_id = $scoped->getID(); | 
					
						
							| 
									
										
										
										
											2012-07-10 12:25:19 -04:00
										 |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $modlog->role     = $role; | 
					
						
							| 
									
										
										
										
											2019-09-11 11:25:39 +03:00
										 |  |  |         $modlog->is_grant = false; | 
					
						
							| 
									
										
										
										
											2012-07-10 12:25:19 -04:00
										 |  |  |         $modlog->created  = common_sql_now(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $modlog->insert(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return true; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-09-11 11:25:39 +03:00
										 |  |  |     public function onEndShowSections(Action $action) | 
					
						
							| 
									
										
										
										
											2012-07-10 12:25:19 -04:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2015-07-10 13:44:47 +02:00
										 |  |  |         if (!$action instanceof ShowstreamAction) { | 
					
						
							|  |  |  |             // early return for actions we're not interested in
 | 
					
						
							| 
									
										
										
										
											2012-07-10 12:25:19 -04:00
										 |  |  |             return true; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-10 13:44:47 +02:00
										 |  |  |         $scoped = $action->getScoped(); | 
					
						
							|  |  |  |         if (!$scoped instanceof Profile || !$scoped->hasRight(self::VIEWMODLOG)) { | 
					
						
							|  |  |  |             // only continue if we are allowed to VIEWMODLOG
 | 
					
						
							| 
									
										
										
										
											2012-07-10 12:25:19 -04:00
										 |  |  |             return true; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-10 13:44:47 +02:00
										 |  |  |         $profile = $action->getTarget(); | 
					
						
							| 
									
										
										
										
											2012-07-10 12:25:19 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  |         $ml = new ModLog(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-10 13:44:47 +02:00
										 |  |  |         $ml->profile_id = $profile->getID(); | 
					
						
							| 
									
										
										
										
											2012-07-10 12:25:19 -04:00
										 |  |  |         $ml->orderBy("created"); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $cnt = $ml->find(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if ($cnt > 0) { | 
					
						
							|  |  |  |             $action->elementStart('div', array('id' => 'entity_mod_log', | 
					
						
							|  |  |  |                                                'class' => 'section')); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             $action->element('h2', null, _('Moderation')); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             $action->elementStart('table'); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             while ($ml->fetch()) { | 
					
						
							|  |  |  |                 $action->elementStart('tr'); | 
					
						
							|  |  |  |                 $action->element('td', null, strftime('%y-%m-%d', strtotime($ml->created))); | 
					
						
							|  |  |  |                 $action->element('td', null, sprintf(($ml->is_grant) ? _('+%s') : _('-%s'), $ml->role)); | 
					
						
							|  |  |  |                 $action->elementStart('td'); | 
					
						
							|  |  |  |                 if ($ml->moderator_id) { | 
					
						
							| 
									
										
										
										
											2015-07-10 13:44:47 +02:00
										 |  |  |                     $mod = Profile::getByID($ml->moderator_id); | 
					
						
							| 
									
										
										
										
											2012-07-10 12:25:19 -04:00
										 |  |  |                     if (empty($mod)) { | 
					
						
							|  |  |  |                         $action->text(_('[unknown]')); | 
					
						
							|  |  |  |                     } else { | 
					
						
							| 
									
										
										
										
											2019-09-11 11:25:39 +03:00
										 |  |  |                         $action->element( | 
					
						
							|  |  |  |                             'a', | 
					
						
							|  |  |  |                             [ | 
					
						
							|  |  |  |                                 'href' => $mod->getUrl(), | 
					
						
							|  |  |  |                                 'title' => $mod->getFullname(), | 
					
						
							|  |  |  |                             ], | 
					
						
							|  |  |  |                             $mod->getNickname() | 
					
						
							|  |  |  |                         ); | 
					
						
							| 
									
										
										
										
											2012-07-10 12:25:19 -04:00
										 |  |  |                     } | 
					
						
							|  |  |  |                 } else { | 
					
						
							|  |  |  |                     $action->text(_('[unknown]')); | 
					
						
							|  |  |  |                 } | 
					
						
							|  |  |  |                 $action->elementEnd('td'); | 
					
						
							|  |  |  |                 $action->elementEnd('tr'); | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             $action->elementEnd('table'); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             $action->elementEnd('div'); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-09-11 11:25:39 +03:00
										 |  |  |     public function onUserRightsCheck($profile, $right, &$result) | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2012-07-10 12:25:19 -04:00
										 |  |  |         switch ($right) { | 
					
						
							|  |  |  |         case self::VIEWMODLOG: | 
					
						
							|  |  |  |             $result = ($profile->hasRole(Profile_role::MODERATOR) || $profile->hasRole('modhelper')); | 
					
						
							|  |  |  |             return false; | 
					
						
							|  |  |  |         default: | 
					
						
							|  |  |  |             return true; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-12 15:03:30 +01:00
										 |  |  |     public function onPluginVersion(array &$versions): bool | 
					
						
							| 
									
										
										
										
											2012-07-10 12:25:19 -04:00
										 |  |  |     { | 
					
						
							|  |  |  |         $versions[] = array('name' => 'ModLog', | 
					
						
							| 
									
										
										
										
											2019-06-03 01:56:52 +01:00
										 |  |  |                             'version' => self::PLUGIN_VERSION, | 
					
						
							| 
									
										
										
										
											2012-07-10 12:25:19 -04:00
										 |  |  |                             'author' => 'Evan Prodromou', | 
					
						
							| 
									
										
										
										
											2019-11-21 00:21:22 +00:00
										 |  |  |                             'homepage' => GNUSOCIAL_ENGINE_REPO_URL . 'tree/master/plugins/ModLog', | 
					
						
							| 
									
										
										
										
											2012-07-10 12:25:19 -04:00
										 |  |  |                             'description' => | 
					
						
							|  |  |  |                             _m('Show the moderation history for a profile in the sidebar')); | 
					
						
							|  |  |  |         return true; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | } |