| 
									
										
										
										
											2010-09-30 16:25:15 -07:00
										 |  |  | <?php | 
					
						
							| 
									
										
										
										
											2019-08-04 14:39:18 +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/>.
 | 
					
						
							| 
									
										
										
										
											2010-09-30 16:25:15 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | /** | 
					
						
							| 
									
										
										
										
											2019-08-04 14:39:18 +01:00
										 |  |  |  * The ForceGroup plugin allows forced group memberships and allows forcing all notices | 
					
						
							|  |  |  |  * to appear in some groups. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * @package   ForceGroupPlugin | 
					
						
							| 
									
										
										
										
											2010-09-30 16:25:15 -07:00
										 |  |  |  * @maintainer Brion Vibber <brion@status.net> | 
					
						
							| 
									
										
										
										
											2019-08-04 14:39:18 +01:00
										 |  |  |  * @copyright 2019 Free Software Foundation, Inc http://www.fsf.org | 
					
						
							|  |  |  |  * @license   https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later | 
					
						
							| 
									
										
										
										
											2010-09-30 16:25:15 -07:00
										 |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-04 14:39:18 +01:00
										 |  |  | defined('GNUSOCIAL') || die; | 
					
						
							| 
									
										
										
										
											2010-09-30 16:25:15 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | class ForceGroupPlugin extends Plugin | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2019-06-03 01:56:52 +01:00
										 |  |  |     const PLUGIN_VERSION = '2.0.0'; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-04 14:39:18 +01:00
										 |  |  |     // settings which can be set in config.php with addModule('ForceGroup', ['param'=>'value', ...]);
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-09-30 16:25:15 -07:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * Members of these groups will have all their posts mirrored into | 
					
						
							|  |  |  |      * the group even if they don't explicitly mention it. | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * List by local nickname. | 
					
						
							|  |  |  |      */ | 
					
						
							| 
									
										
										
										
											2019-08-04 14:39:18 +01:00
										 |  |  |     public $post = []; | 
					
						
							| 
									
										
										
										
											2011-04-08 18:46:41 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-09-30 16:25:15 -07:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * New user registrations will automatically join these groups on | 
					
						
							|  |  |  |      * registration. They're not prevented from leaving, however. | 
					
						
							| 
									
										
										
										
											2011-04-08 18:46:41 +02:00
										 |  |  |      * | 
					
						
							| 
									
										
										
										
											2010-09-30 16:25:15 -07:00
										 |  |  |      * List by local nickname. | 
					
						
							|  |  |  |      */ | 
					
						
							| 
									
										
										
										
											2019-08-04 14:39:18 +01:00
										 |  |  |     public $join = []; | 
					
						
							| 
									
										
										
										
											2010-09-30 16:25:15 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							| 
									
										
										
										
											2019-08-04 14:39:18 +01:00
										 |  |  |      * If poster is in one of the $post groups, make sure their notice | 
					
						
							| 
									
										
										
										
											2010-09-30 16:25:15 -07:00
										 |  |  |      * gets saved into that group even if not explicitly mentioned. | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @param Notice $notice | 
					
						
							| 
									
										
										
										
											2019-08-04 14:39:18 +01:00
										 |  |  |      * @return bool event hook return | 
					
						
							|  |  |  |      * @throws ServerException | 
					
						
							| 
									
										
										
										
											2010-09-30 16:25:15 -07:00
										 |  |  |      */ | 
					
						
							| 
									
										
										
										
											2019-08-04 14:39:18 +01:00
										 |  |  |     public function onStartNoticeDistribute($notice): bool | 
					
						
							| 
									
										
										
										
											2010-09-30 16:25:15 -07:00
										 |  |  |     { | 
					
						
							|  |  |  |         $profile = $notice->getProfile(); | 
					
						
							| 
									
										
										
										
											2012-06-18 23:39:47 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-10-25 13:17:29 +02:00
										 |  |  |         $isRemote = !(User::getKV('id', $profile->id)); | 
					
						
							| 
									
										
										
										
											2012-06-18 23:39:47 -04:00
										 |  |  |         if ($isRemote) { | 
					
						
							|  |  |  |             /* | 
					
						
							|  |  |  |              * Notices from remote users on other sites | 
					
						
							|  |  |  |              * will normally not end up here unless they're | 
					
						
							|  |  |  |              * specifically directed here, e.g.: via explicit | 
					
						
							|  |  |  |              * post to a remote (to them) group. But remote | 
					
						
							|  |  |  |              * notices can also be `pulled in' as a result of | 
					
						
							|  |  |  |              * local users subscribing to the remote user; | 
					
						
							|  |  |  |              * from the remote user's perspective, this results | 
					
						
							|  |  |  |              * in group-forcing appearing effectively random. | 
					
						
							|  |  |  |              * So let's be consistent, and just never force | 
					
						
							|  |  |  |              * incoming remote notices into a ForceGroup: | 
					
						
							|  |  |  |              */ | 
					
						
							|  |  |  |             return true; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-09-30 16:25:15 -07:00
										 |  |  |         foreach ($this->post as $nickname) { | 
					
						
							|  |  |  |             $group = User_group::getForNickname($nickname); | 
					
						
							|  |  |  |             if ($group && $profile->isMember($group)) { | 
					
						
							|  |  |  |                 $notice->addToGroupInbox($group); | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         return true; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-04 14:39:18 +01:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * Make sure that this new user is added to all of the $join groups | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @param Profile $profile | 
					
						
							|  |  |  |      * @return bool event hook return | 
					
						
							|  |  |  |      * @throws ServerException | 
					
						
							|  |  |  |      */ | 
					
						
							| 
									
										
										
										
											2013-09-14 18:36:35 +02:00
										 |  |  |     public function onEndUserRegister(Profile $profile) | 
					
						
							| 
									
										
										
										
											2010-09-30 16:25:15 -07:00
										 |  |  |     { | 
					
						
							|  |  |  |         foreach ($this->join as $nickname) { | 
					
						
							|  |  |  |             $group = User_group::getForNickname($nickname); | 
					
						
							|  |  |  |             if ($group && !$profile->isMember($group)) { | 
					
						
							|  |  |  |                 try { | 
					
						
							| 
									
										
										
										
											2011-03-21 14:35:29 -07:00
										 |  |  |                     $profile->joinGroup($group); | 
					
						
							| 
									
										
										
										
											2010-09-30 16:25:15 -07:00
										 |  |  |                 } catch (Exception $e) { | 
					
						
							| 
									
										
										
										
											2010-10-01 21:53:20 +02:00
										 |  |  |                     // TRANS: Server exception.
 | 
					
						
							|  |  |  |                     // TRANS: %1$s is a user nickname, %2$s is a group nickname.
 | 
					
						
							| 
									
										
										
										
											2019-08-04 14:39:18 +01:00
										 |  |  |                     throw new ServerException(sprintf( | 
					
						
							|  |  |  |                         _m('Could not join user %1$s to group %2$s.'), | 
					
						
							|  |  |  |                         $profile->nickname, | 
					
						
							|  |  |  |                         $group->nickname | 
					
						
							|  |  |  |                     )); | 
					
						
							| 
									
										
										
										
											2010-09-30 16:25:15 -07:00
										 |  |  |                 } | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2019-08-04 14:39:18 +01:00
										 |  |  |         return true; | 
					
						
							| 
									
										
										
										
											2010-09-30 16:25:15 -07:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2010-10-01 21:53:20 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * Provide plugin version information. | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * This data is used when showing the version page. | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @param array &$versions array of version data arrays; see EVENTS.txt | 
					
						
							|  |  |  |      * | 
					
						
							| 
									
										
										
										
											2019-08-04 14:39:18 +01:00
										 |  |  |      * @return bool hook value | 
					
						
							|  |  |  |      * @throws Exception | 
					
						
							| 
									
										
										
										
											2010-10-01 21:53:20 +02:00
										 |  |  |      */ | 
					
						
							| 
									
										
										
										
											2019-08-12 15:03:30 +01:00
										 |  |  |     public function onPluginVersion(array &$versions): bool | 
					
						
							| 
									
										
										
										
											2010-10-01 21:53:20 +02:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2019-11-21 00:21:22 +00:00
										 |  |  |         $url = GNUSOCIAL_ENGINE_REPO_URL . 'tree/master/plugins/ForceGroup'; | 
					
						
							| 
									
										
										
										
											2010-10-01 21:53:20 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |         $versions[] = array('name' => 'ForceGroup', | 
					
						
							| 
									
										
										
										
											2019-06-03 01:56:52 +01:00
										 |  |  |             'version' => self::PLUGIN_VERSION, | 
					
						
							| 
									
										
										
										
											2010-10-01 21:53:20 +02:00
										 |  |  |             'author' => 'Brion Vibber', | 
					
						
							|  |  |  |             'homepage' => $url, | 
					
						
							|  |  |  |             'rawdescription' => | 
					
						
							| 
									
										
										
										
											2019-08-04 14:39:18 +01:00
										 |  |  |             // TRANS: Module description.
 | 
					
						
							|  |  |  |                 _m('Allows forced group memberships and forces all notices to appear in groups that users were forced in.')); | 
					
						
							| 
									
										
										
										
											2010-10-01 21:53:20 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |         return true; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2010-09-30 16:25:15 -07:00
										 |  |  | } |