]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/WikiGroup.php
Fixes for "Call-time pass by reference has been deprecated" errors.
[SourceForge/phpwiki.git] / lib / WikiGroup.php
1 <?php
2 rcs_id('$Id: WikiGroup.php,v 1.5 2003-02-22 20:49:55 dairiki 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 (isset($this->membership[$group])) {
258             return $this->membership[$group];
259         }
260         $group_page = $request->getPage($group);
261         if ($this->_inGroupPage($group_page)) {
262             $this->membership[$group] = true;
263             return true;
264         }
265         $this->membership[$group] = false;
266         return false;
267     }
268     
269     /**
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.
274     * @access private
275     */
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);
281             return false;
282         }
283         $contents = $group_revision->getContent();
284         $match = '/^\s*[\*\#]+\s*' . $username . '\s*$/';
285         foreach($contents as $line){
286             if (preg_match($match, $line)) {
287                 return true;
288             }
289         }
290         return false;
291     }
292     
293     /**
294      * Determines all of the groups of which the current user is a member.
295      * 
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.
300      */ 
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;
312             } else {
313                 $membership[$group] = false;
314             }
315         }
316         $this->membership = $membership;
317         return $membership;
318     }
319
320     /**
321      * Determines all of the members of a particular group.
322      * 
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).
327      */ 
328     function getMembersOf($group){
329         trigger_error("GroupWikiPage::getMembersof is not yet implimented",
330                       E_USER_WARNING);
331         return array();
332         /*
333         * Waiting for a reliable way to check if a string is a username.
334         $request = $this->request;
335         $user = $this->user;
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);
340             return false;
341         }
342         $contents = $group_revision->getContent();
343         $match = '/^(\s*[\*\#]+\s*)(\w+)(\s*)$/';
344         $members = array();
345         foreach($contents as $line){
346             $matches = array();
347             if(preg_match($match, $line, $matches)){
348                 $members[] = $matches[2];
349             }
350         }
351         return $members;
352         */
353     }
354 }
355
356 // $Log: not supported by cvs2svn $
357 // Revision 1.4  2003/01/21 04:02:39  zorloc
358 // Added Log entry and page footer.
359 //
360
361 // Local Variables:
362 // mode: php
363 // tab-width: 8
364 // c-basic-offset: 4
365 // c-hanging-comment-ender-p: nil
366 // indent-tabs-mode: nil
367 // End:
368 ?>