]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/WikiGroup.php
Replaced tabs with four spaces
[SourceForge/phpwiki.git] / lib / WikiGroup.php
1 <?php
2 rcs_id('$Id: WikiGroup.php,v 1.2 2003-01-17 21:11:31 zorloc Exp $')
3 /*
4  Copyright 2002 $ThePhpWikiProgrammingTeam
5
6  This file is part of PhpWiki.
7
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.
12
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.
17
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
21  */
22
23 /**
24  * WikiGroup is an abstract class to provide the base functions for determining
25  * group membership.
26  * 
27  * WikiGroup is an abstract class with three functions:
28  * <ol><li />Provide the static method getGroup with will return the proper
29  *         subclass.
30  *     <li />Provide an interface for subclasses to implement.
31  *     <li />Provide fallover methods (with error msgs) if not impemented in subclass.
32  * </ol>
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>
36  */ 
37 class WikiGroup{
38     /** User name */
39     var $username;
40     /** The global WikiRequest object */
41     var $request;
42     /** Array of groups $username is confirmed to belong to */
43     var $membership;
44     
45     /**
46      * Initializes a WikiGroup object which should never happen.  Use:
47      * $group = &WikiGroup::getGroup($request);
48      * @param object $request The global WikiRequest object -- ignored.
49      */ 
50     function WikiGroup($request){    
51         return;
52     }
53
54     /**
55      * Gets the current username and erases $this->membership if is different than
56      * the stored $this->username
57      * @return string Current username.
58      */ 
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;
66         }
67         return $username;
68     }
69     
70     /**
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.
75      */ 
76     function getGroup($request){
77         switch(GROUP_METHOD){
78             case GROUP_NONE: 
79                 return new GroupNone(&$request);
80                 break;
81             case GROUP_WIKIPAGE: 
82                 return new GroupWikiPage(&$request);
83                 break;
84 #            case GROUP_DB: 
85 #                return new GroupDB(&$user, &$request);
86 #                break;
87 #            case GROUP_LDAP: 
88 #                return new GroupLDAP(&$user, &$request);
89 #                break;
90             default:
91                 trigger_error("No GROUP_METHOD defined", E_USER_WARNING);
92                 return new WikiGroup(&$request);
93         }
94     }
95
96     /**
97      * Determines if the current user is a member of a group.
98      * 
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).
103      */ 
104     function isMember($group){
105         trigger_error("Method 'isMember' not implemented in this GROUP_METHOD", 
106                       E_USER_WARNING);
107         return false;
108     }
109     
110     /**
111      * Determines all of the groups of which the current user is a member.
112      * 
113      * This method is an abstraction.  An error is sent and an empty 
114      * array is returned.
115      * @return array Array of groups to which the user belongs (always empty).
116      */ 
117     function getAllGroupsIn(){
118         trigger_error("Method 'getAllGroupsIn' not implemented in this GROUP_METHOD",
119                       E_USER_WARNING);
120         return array();
121     }
122
123     /**
124      * Determines all of the members of a particular group.
125      * 
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).
130      */ 
131     function getMembersOf($group){
132         trigger_error("Method 'getMembersof' not implemented in this GROUP_METHOD", 
133                       E_USER_WARNING);
134         return array();
135     }
136
137     /**
138      * Add the current or specified user to a group.
139      * 
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.
145      */ 
146     function setMemberOf($group, $user = false){
147         trigger_error("Method 'setMemberOf' not implemented in this GROUP_METHOD", 
148                       E_USER_WARNING);
149         return false;
150     }
151     
152     /**
153      * Remove the current or specified user to a group.
154      * 
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.
160      */ 
161     function removeMemberOf($group, $user = false){
162         trigger_error("Method 'removeMemberOf' not implemented in this GROUP_METHOD", 
163                       E_USER_WARNING);
164         return false;
165     }
166 }
167
168 /**
169  * GroupNone disables all Group funtionality
170  * 
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>
174  */ 
175 class GroupNone extends WikiGroup{
176
177     /**
178      * Constructor
179      * 
180      * Ignores the parameter provided.
181      * @param object $request The global WikiRequest object - ignored.
182      */ 
183     function GroupNone($request){
184         return;
185     }    
186
187     /**
188      * Determines if the current user is a member of a group.
189      * 
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).
193      */ 
194     function isMember($group){
195         return false;
196     }
197     
198     /**
199      * Determines all of the groups of which the current user is a member.
200      * 
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).
204      */ 
205     function getAllGroupsIn(){
206         return array();
207     }
208
209     /**
210      * Determines all of the members of a particular group.
211      * 
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).
215      */ 
216     function getMembersOf($group){
217         return array();
218     }
219
220 }
221
222 /**
223  * GroupWikiPage provides group functionality via pages within the Wiki.
224  * 
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>
229  */ 
230 class GroupWikiPage extends WikiGroup{
231     
232     /**
233      * Constructor
234      * 
235      * Initiallizes the three superclass instance variables
236      * @param object $request The global WikiRequest object.
237      */ 
238     function GroupWikiPage($request){
239         $this->request = &$request;
240         $this->username = null;
241         $this->membership = array();
242     }
243
244     /**
245      * Determines if the current user is a member of a group.
246      * 
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.
253      */ 
254     function isMember($group){
255         $request = $this->request;
256         $username = $this->_getUserName();
257         if ($this->membership[$group]) {
258             return true;
259         }
260         $group_page = $request->getPage($group);
261         if ($this->_inGroupPage($group_page)) {
262             $this->membership[$group] = true;
263             return true;
264         }
265         return false;
266     }
267     
268     /**
269     * Private method to take a WikiDB_Page and parse to determine if the
270     * current_user is a member of the group.
271     * @param object $group_page WikiDB_Page object for the group's page
272     * @return boolean True if user is a member, else false.
273     * @access private
274     */
275     function _inGroupPage($group_page){
276         $group_revision = $group_page->getCurrentRevision();
277         if ($group_revision->hasDefaultContents()) {
278             $group = $group_page->getName();
279             trigger_error("Group $group does not exist", E_USER_WARNING);
280             return false;
281         }
282         $contents = $group_revision->getContent();
283         $match = '/^\s*[\*\#]+\s*' . $username . '\s*$/';
284         foreach($contents as $line){
285             if (preg_match($match, $line)) {
286                 return true;
287             }
288         }
289         return false;
290     }
291     
292     /**
293      * Determines all of the groups of which the current user is a member.
294      * 
295      * Checks the root Group page ('CategoryGroups') for the list of all groups, 
296      * then checks each group to see if the current user is a member.
297      * @param string $group Name of the group to check for membership.
298      * @return array Array of groups to which the user belongs.
299      */ 
300     function getAllGroupsIn(){
301         $request = &$this->request;
302         $username = $this->_getUserName();
303         $membership = array();
304         $dbh = &$request->getDbh();
305         $master_page = $request->getPage('CategeoryGroups');
306         $master_list = $master_page->getLinks(true);
307         while($group_page = $master_list->next()){
308             if ($this->_inGroupPage($group_page)) {
309                 $group = $group_page->getName();
310                 $membership[$group] = true;
311             }
312         }
313         $this->membership = $membership;
314         return $membership;
315     }
316
317     /**
318      * Determines all of the members of a particular group.
319      * 
320      * Checks a group's page to return all the current members.  Currently this
321      * method is disabled and triggers an error and returns an empty array.
322      * @param string $group Name of the group to get the full membership list of.
323      * @return array Array of usernames that have joined the group (always empty).
324      */ 
325     function getMembersOf($group){
326         trigger_error("GroupWikiPage::getMembersof is not yet implimented",
327                       E_USER_WARNING);
328         return array();
329         /*
330         * Waiting for a reliable way to check if a string is a username.
331         $request = $this->request;
332         $user = $this->user;
333         $group_page = $request->getPage($group);
334         $group_revision = $group_page->getCurrentRevision();
335         if ($group_revision->hasDefaultContents()) {
336             trigger_error("Group $group does not exist", E_USER_WARNING);
337             return false;
338         }
339         $contents = $group_revision->getContent();
340         $match = '/^(\s*[\*\#]+\s*)(\w+)(\s*)$/';
341         $members = array();
342         foreach($contents as $line){
343             $matches = array();
344             if(preg_match($match, $line, $matches)){
345                 $members[] = $matches[2];
346             }
347         }
348         return $members;
349         */
350     }
351 }
352
353 ?>