2 rcs_id('$Id: WikiGroup.php,v 1.5 2003-02-22 20:49:55 dairiki Exp $')
4 Copyright 2002 $ThePhpWikiProgrammingTeam
6 This file is part of PhpWiki.
8 PhpWiki is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 PhpWiki is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with PhpWiki; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 * WikiGroup is an abstract class to provide the base functions for determining
27 * WikiGroup is an abstract class with three functions:
28 * <ol><li />Provide the static method getGroup with will return the proper
30 * <li />Provide an interface for subclasses to implement.
31 * <li />Provide fallover methods (with error msgs) if not impemented in subclass.
33 * Do not ever instantiate this class use: $group = &WikiGroup::getGroup($request);
34 * This will instantiate the proper subclass.
35 * @author Joby Walker <zorloc@imperium.org>
40 /** The global WikiRequest object */
42 /** Array of groups $username is confirmed to belong to */
46 * Initializes a WikiGroup object which should never happen. Use:
47 * $group = &WikiGroup::getGroup($request);
48 * @param object $request The global WikiRequest object -- ignored.
50 function WikiGroup(&$request){
55 * Gets the current username and erases $this->membership if is different than
56 * the stored $this->username
57 * @return string Current username.
59 function _getUserName(){
60 $request = &$this->request;
61 $user = $request->getUser();
62 $username = $user->getID();
63 if ($username != $this->username) {
64 $this->membership = array();
65 $this->username = $username;
71 * Static method to return the WikiGroup subclass used in this wiki. Controlled
72 * by the constant GROUP_METHOD.
73 * @param object $request The global WikiRequest object.
74 * @return object Subclass of WikiGroup selected via GROUP_METHOD.
76 function getGroup($request){
79 return new GroupNone($request);
82 return new GroupWikiPage($request);
85 # return new GroupDB($user, $request);
88 # return new GroupLDAP($user, $request);
91 trigger_error("No GROUP_METHOD defined", E_USER_WARNING);
92 return new WikiGroup($request);
97 * Determines if the current user is a member of a group.
99 * This method is an abstraction. The group is ignored, an error is sent, and
100 * false (not a member of the group) is returned.
101 * @param string $group Name of the group to check for membership (ignored).
102 * @return boolean True if user is a member, else false (always false).
104 function isMember($group){
105 trigger_error("Method 'isMember' not implemented in this GROUP_METHOD",
111 * Determines all of the groups of which the current user is a member.
113 * This method is an abstraction. An error is sent and an empty
115 * @return array Array of groups to which the user belongs (always empty).
117 function getAllGroupsIn(){
118 trigger_error("Method 'getAllGroupsIn' not implemented in this GROUP_METHOD",
124 * Determines all of the members of a particular group.
126 * This method is an abstraction. The group is ignored, an error is sent,
127 * and an empty array is returned
128 * @param string $group Name of the group to get the full membership list of.
129 * @return array Array of usernames that have joined the group (always empty).
131 function getMembersOf($group){
132 trigger_error("Method 'getMembersof' not implemented in this GROUP_METHOD",
138 * Add the current or specified user to a group.
140 * This method is an abstraction. The group and user are ignored, an error
141 * is sent, and false (not added) is always returned.
142 * @param string $group User added to this group.
143 * @param string $user Username to add to the group (default = current user).
144 * @return bool On true user was added, false if not.
146 function setMemberOf($group, $user = false){
147 trigger_error("Method 'setMemberOf' not implemented in this GROUP_METHOD",
153 * Remove the current or specified user to a group.
155 * This method is an abstraction. The group and user are ignored, and error
156 * is sent, and false (not removed) is always returned.
157 * @param string $group User removed from this group.
158 * @param string $user Username to remove from the group (default = current user).
159 * @return bool On true user was removed, false if not.
161 function removeMemberOf($group, $user = false){
162 trigger_error("Method 'removeMemberOf' not implemented in this GROUP_METHOD",
169 * GroupNone disables all Group funtionality
171 * All of the GroupNone functions return false or empty values to indicate failure or
172 * no results. Use GroupNone if group controls are not desired.
173 * @author Joby Walker <zorloc@imperium.org>
175 class GroupNone extends WikiGroup{
180 * Ignores the parameter provided.
181 * @param object $request The global WikiRequest object - ignored.
183 function GroupNone(&$request){
188 * Determines if the current user is a member of a group.
190 * The group is ignored and false (not a member of the group) is returned.
191 * @param string $group Name of the group to check for membership (ignored).
192 * @return boolean True if user is a member, else false (always false).
194 function isMember($group){
199 * Determines all of the groups of which the current user is a member.
201 * The group is ignored and an empty array (a member of no groups) is returned.
202 * @param string $group Name of the group to check for membership (ignored).
203 * @return array Array of groups to which the user belongs (always empty).
205 function getAllGroupsIn(){
210 * Determines all of the members of a particular group.
212 * The group is ignored and an empty array (a member of no groups) is returned.
213 * @param string $group Name of the group to check for membership (ignored).
214 * @return array Array of groups user belongs to (always empty).
216 function getMembersOf($group){
223 * GroupWikiPage provides group functionality via pages within the Wiki.
225 * GroupWikiPage is the Wiki way of managing a group. Every group will have
226 * a page. To modify the membership of the group, one only needs to edit the
227 * membership list on the page.
228 * @author Joby Walker <zorloc@imperium.org>
230 class GroupWikiPage extends WikiGroup{
235 * Initiallizes the three superclass instance variables
236 * @param object $request The global WikiRequest object.
238 function GroupWikiPage(&$request){
239 $this->request = &$request;
240 $this->username = null;
241 $this->membership = array();
245 * Determines if the current user is a member of a group.
247 * To determine membership in a particular group, this method checks the
248 * superclass instance variable $membership to see if membership has
249 * already been determined. If not, then the group page is parsed to
250 * determine membership.
251 * @param string $group Name of the group to check for membership.
252 * @return boolean True if user is a member, else false.
254 function isMember($group){
255 $request = $this->request;
256 $username = $this->_getUserName();
257 if (isset($this->membership[$group])) {
258 return $this->membership[$group];
260 $group_page = $request->getPage($group);
261 if ($this->_inGroupPage($group_page)) {
262 $this->membership[$group] = true;
265 $this->membership[$group] = false;
270 * Private method to take a WikiDB_Page and parse to determine if the
271 * current_user is a member of the group.
272 * @param object $group_page WikiDB_Page object for the group's page
273 * @return boolean True if user is a member, else false.
276 function _inGroupPage($group_page){
277 $group_revision = $group_page->getCurrentRevision();
278 if ($group_revision->hasDefaultContents()) {
279 $group = $group_page->getName();
280 trigger_error("Group $group does not exist", E_USER_WARNING);
283 $contents = $group_revision->getContent();
284 $match = '/^\s*[\*\#]+\s*' . $username . '\s*$/';
285 foreach($contents as $line){
286 if (preg_match($match, $line)) {
294 * Determines all of the groups of which the current user is a member.
296 * Checks the root Group page ('CategoryGroups') for the list of all groups,
297 * then checks each group to see if the current user is a member.
298 * @param string $group Name of the group to check for membership.
299 * @return array Array of groups to which the user belongs.
301 function getAllGroupsIn(){
302 $request = &$this->request;
303 $username = $this->_getUserName();
304 $membership = array();
305 $dbh = &$request->getDbh();
306 $master_page = $request->getPage('CategeoryGroups');
307 $master_list = $master_page->getLinks(true);
308 while($group_page = $master_list->next()){
309 if ($this->_inGroupPage($group_page)) {
310 $group = $group_page->getName();
311 $membership[$group] = true;
313 $membership[$group] = false;
316 $this->membership = $membership;
321 * Determines all of the members of a particular group.
323 * Checks a group's page to return all the current members. Currently this
324 * method is disabled and triggers an error and returns an empty array.
325 * @param string $group Name of the group to get the full membership list of.
326 * @return array Array of usernames that have joined the group (always empty).
328 function getMembersOf($group){
329 trigger_error("GroupWikiPage::getMembersof is not yet implimented",
333 * Waiting for a reliable way to check if a string is a username.
334 $request = $this->request;
336 $group_page = $request->getPage($group);
337 $group_revision = $group_page->getCurrentRevision();
338 if ($group_revision->hasDefaultContents()) {
339 trigger_error("Group $group does not exist", E_USER_WARNING);
342 $contents = $group_revision->getContent();
343 $match = '/^(\s*[\*\#]+\s*)(\w+)(\s*)$/';
345 foreach($contents as $line){
347 if(preg_match($match, $line, $matches)){
348 $members[] = $matches[2];
356 // $Log: not supported by cvs2svn $
357 // Revision 1.4 2003/01/21 04:02:39 zorloc
358 // Added Log entry and page footer.
365 // c-hanging-comment-ender-p: nil
366 // indent-tabs-mode: nil