2 rcs_id('$Id: WikiGroup.php,v 1.47 2004-11-19 13:23:47 rurban Exp $');
4 Copyright (C) 2003, 2004 $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
23 if (!defined('GROUP_METHOD') or
24 !in_array(GROUP_METHOD,
25 array('NONE','WIKIPAGE','DB','FILE','LDAP')))
26 trigger_error(_("No or unsupported GROUP_METHOD defined"), E_USER_WARNING);
28 /* Special group names for ACL */
29 define('GROUP_EVERY', _("Every"));
30 define('GROUP_ANONYMOUS', _("Anonymous Users"));
31 define('GROUP_BOGOUSER', _("Bogo Users"));
32 define('GROUP_HASHOMEPAGE', _("HasHomePage"));
33 define('GROUP_SIGNED', _("Signed Users"));
34 define('GROUP_AUTHENTICATED', _("Authenticated Users"));
35 define('GROUP_ADMIN', _("Administrators"));
36 define('GROUP_OWNER', _("Owner"));
37 define('GROUP_CREATOR', _("Creator"));
40 * WikiGroup is an abstract class to provide the base functions for determining
41 * group membership for a specific user. Some functions are user independent.
43 * Limitation: For the current user only. This must be fixed to be able to query
44 * for membership of any user.
46 * WikiGroup is an abstract class with three functions:
47 * <ol><li />Provide the static method getGroup with will return the proper
49 * <li />Provide an interface for subclasses to implement.
50 * <li />Provide fallover methods (with error msgs) if not impemented in subclass.
52 * Do not ever instantiate this class. Use: $group = &WikiGroup::getGroup();
53 * This will instantiate the proper subclass.
55 * @author Joby Walker <zorloc@imperium.org>
61 /** User object if different from current user */
63 /** The global WikiRequest object */
65 /** Array of groups $username is confirmed to belong to */
67 /** boolean if not the current user */
68 var $not_current = false;
71 * Initializes a WikiGroup object which should never happen. Use:
72 * $group = &WikiGroup::getGroup();
73 * @param object $request The global WikiRequest object -- ignored.
75 function WikiGroup($not_current = false) {
76 $this->not_current = $not_current;
77 //$this->request =& $GLOBALS['request'];
81 * Gets the current username from the internal user object
82 * and erases $this->membership if is different than
83 * the stored $this->username
84 * @return string Current username.
86 function _getUserName(){
88 $user = (!empty($this->user)) ? $this->user : $request->getUser();
89 $username = $user->getID();
90 if ($username != $this->username) {
91 $this->membership = array();
92 $this->username = $username;
94 if (!$this->not_current)
100 * Static method to return the WikiGroup subclass used in this wiki. Controlled
101 * by the constant GROUP_METHOD.
102 * @param object $request The global WikiRequest object.
103 * @return object Subclass of WikiGroup selected via GROUP_METHOD.
105 function getGroup($not_current = false){
106 switch (GROUP_METHOD){
108 return new GroupNone($not_current);
111 return new GroupWikiPage($not_current);
114 if ($GLOBALS['DBParams']['dbtype'] == 'ADODB') {
115 return new GroupDB_ADODB($not_current);
116 } elseif ($GLOBALS['DBParams']['dbtype'] == 'SQL') {
117 return new GroupDb_PearDB($not_current);
119 trigger_error("GROUP_METHOD = DB: Unsupported dbtype "
120 . $GLOBALS['DBParams']['dbtype'],
125 return new GroupFile($not_current);
128 return new GroupLDAP($not_current);
131 trigger_error(_("No or unsupported GROUP_METHOD defined"), E_USER_WARNING);
132 return new WikiGroup($not_current);
136 /** ACL PagePermissions will need those special groups based on the User status only.
139 function specialGroup($group){
140 return in_array($group,$this->specialGroups());
143 function _specialGroup($group){
144 return in_array($group,$this->_specialGroups());
147 function specialGroups(){
157 function _specialGroups(){
168 * Determines if the current user is a member of a group.
170 * This method is an abstraction. The group is ignored, an error is sent, and
171 * false (not a member of the group) is returned.
172 * @param string $group Name of the group to check for membership (ignored).
173 * @return boolean True if user is a member, else false (always false).
175 function isMember($group){
176 if (isset($this->membership[$group]))
177 return $this->membership[$group];
178 if ($this->specialGroup($group)) {
179 return $this->isSpecialMember($group);
181 trigger_error(__sprintf("Method '%s' not implemented in this GROUP_METHOD %s",
182 'isMember', GROUP_METHOD),
188 function isSpecialMember($group){
191 if (isset($this->membership[$group]))
192 return $this->membership[$group];
193 $user = (!empty($this->user)) ? $this->user : $request->getUser();
196 return $this->membership[$group] = true;
197 case GROUP_ANONYMOUS:
198 return $this->membership[$group] = ! $user->isSignedIn();
200 return $this->membership[$group] = (isa($user,'_BogoUser')
201 and $user->_level >= WIKIAUTH_BOGO);
203 return $this->membership[$group] = $user->isSignedIn();
204 case GROUP_AUTHENTICATED:
205 return $this->membership[$group] = $user->isAuthenticated();
207 return $this->membership[$group] = (isset($user->_level)
208 and $user->_level == WIKIAUTH_ADMIN);
210 trigger_error(__sprintf("Undefined method %s for special group %s",
218 * Determines all of the groups of which the current user is a member.
220 * This method is an abstraction. An error is sent and an empty
222 * @return array Array of groups to which the user belongs (always empty).
224 function getAllGroupsIn(){
225 trigger_error(__sprintf("Method '%s' not implemented in this GROUP_METHOD %s",
226 'getAllGroupsIn', GROUP_METHOD),
231 function _allUsers() {
232 static $result = array();
237 /* WikiPage users: */
238 $dbh =& $request->_dbi;
239 $page_iter = $dbh->getAllPages();
241 while ($page = $page_iter->next()) {
242 if ($page->isUserPage())
243 $users[] = $page->_pagename;
246 /* WikiDB users from prefs (not from users): */
248 $dbi = _PassUser::getAuthDbh();
252 if ($dbi and $dbh->getAuthParam('pref_select')) {
254 $sql = preg_replace('/SELECT .+ FROM/i','SELECT userid FROM',
255 $dbh->getAuthParam('pref_select'));
256 //don't strip WHERE, only the userid stuff.
257 $sql = preg_replace('/(WHERE.*?)\s+\w+\s*=\s*["\']\$userid[\'"]/i','\\1 AND 1', $sql);
258 $sql = str_replace('WHERE AND 1','',$sql);
259 if (isa($dbi, 'ADOConnection')) {
260 $db_result = $dbi->Execute($sql);
261 foreach ($db_result->GetArray() as $u) {
262 $users = array_merge($users,array_values($u));
264 } elseif (isa($dbi, 'DB_common')) { // PearDB
265 $users = array_merge($users, $dbi->getCol($sql));
269 /* WikiDB users from users: */
270 // Fixme: don't strip WHERE, only the userid stuff.
271 if ($dbi and $dbh->getAuthParam('auth_user_exists')) {
272 //don't strip WHERE, only the userid stuff.
273 $sql = preg_replace('/(WHERE.*?)\s+\w+\s*=\s*["\']\$userid[\'"]/i','\\1 AND 1',
274 $dbh->getAuthParam('auth_user_exists'));
275 $sql = str_replace('WHERE AND 1','', $sql);
276 if (isa($dbi, 'ADOConnection')) {
277 $db_result = $dbi->Execute($sql);
278 foreach ($db_result->GetArray() as $u) {
279 $users = array_merge($users, array_values($u));
281 } elseif (isa($dbi, 'DB_common')) {
282 $users = array_merge($users, $dbi->getCol($sql));
286 // remove empty and duplicate users
288 foreach ($users as $u) {
289 if (empty($u) or in_array($u,$result)) continue;
296 * Determines all of the members of a particular group.
298 * This method is an abstraction. The group is ignored, an error is sent,
299 * and an empty array is returned
300 * @param string $group Name of the group to get the full membership list of.
301 * @return array Array of usernames that have joined the group (always empty).
303 function getMembersOf($group){
304 if ($this->specialGroup($group)) {
305 return getSpecialMembersOf($group);
307 trigger_error(__sprintf("Method '%s' not implemented in this GROUP_METHOD %s",
308 'getMembersOf', GROUP_METHOD),
313 function getSpecialMembersOf($group) {
314 //$request = &$this->request;
315 $all = $this->_allUsers();
320 case GROUP_ANONYMOUS:
323 foreach ($all as $u) {
324 if (isWikiWord($user)) $users[] = $u;
328 foreach ($all as $u) {
329 $user = WikiUser($u);
330 if ($user->isSignedIn()) $users[] = $u;
333 case GROUP_AUTHENTICATED:
334 foreach ($all as $u) {
335 $user = WikiUser($u);
336 if ($user->isAuthenticated()) $users[] = $u;
340 foreach ($all as $u) {
341 $user = WikiUser($u);
342 if (isset($user->_level) and $user->_level == WIKIAUTH_ADMIN)
347 trigger_error(__sprintf("Unknown special group '%s'", $group),
353 * Add the current or specified user to a group.
355 * This method is an abstraction. The group and user are ignored, an error
356 * is sent, and false (not added) is always returned.
357 * @param string $group User added to this group.
358 * @param string $user Username to add to the group (default = current user).
359 * @return bool On true user was added, false if not.
361 function setMemberOf($group, $user = false){
362 trigger_error(__sprintf("Method '%s' not implemented in this GROUP_METHOD %s",
363 'setMemberOf', GROUP_METHOD),
369 * Remove the current or specified user to a group.
371 * This method is an abstraction. The group and user are ignored, and error
372 * is sent, and false (not removed) is always returned.
373 * @param string $group User removed from this group.
374 * @param string $user Username to remove from the group (default = current user).
375 * @return bool On true user was removed, false if not.
377 function removeMemberOf($group, $user = false){
378 trigger_error(__sprintf("Method '%s' not implemented in this GROUP_METHOD %s",
379 'removeMemberOf', GROUP_METHOD),
386 * GroupNone disables all Group funtionality
388 * All of the GroupNone functions return false or empty values to indicate failure or
389 * no results. Use GroupNone if group controls are not desired.
390 * @author Joby Walker <zorloc@imperium.org>
392 class GroupNone extends WikiGroup{
397 * Ignores the parameter provided.
398 * @param object $request The global WikiRequest object - ignored.
400 function GroupNone() {
401 //$this->request = &$GLOBALS['request'];
406 * Determines if the current user is a member of a group.
408 * The group is ignored and false (not a member of the group) is returned.
409 * @param string $group Name of the group to check for membership (ignored).
410 * @return boolean True if user is a member, else false (always false).
412 function isMember($group){
413 if ($this->specialGroup($group)) {
414 return $this->isSpecialMember($group);
421 * Determines all of the groups of which the current user is a member.
423 * The group is ignored and an empty array (a member of no groups) is returned.
424 * @param string $group Name of the group to check for membership (ignored).
425 * @return array Array of groups to which the user belongs (always empty).
427 function getAllGroupsIn(){
432 * Determines all of the members of a particular group.
434 * The group is ignored and an empty array (a member of no groups) is returned.
435 * @param string $group Name of the group to check for membership (ignored).
436 * @return array Array of groups user belongs to (always empty).
438 function getMembersOf($group){
445 * GroupWikiPage provides group functionality via pages within the Wiki.
447 * GroupWikiPage is the Wiki way of managing a group. Every group will have
448 * a page. To modify the membership of the group, one only needs to edit the
449 * membership list on the page.
450 * @author Joby Walker <zorloc@imperium.org>
452 class GroupWikiPage extends WikiGroup{
457 * Initializes the three superclass instance variables
458 * @param object $request The global WikiRequest object.
460 function GroupWikiPage() {
461 //$this->request = &$GLOBALS['request'];
462 $this->username = $this->_getUserName();
463 //$this->username = null;
464 $this->membership = array();
468 * Determines if the current user is a member of a group.
470 * To determine membership in a particular group, this method checks the
471 * superclass instance variable $membership to see if membership has
472 * already been determined. If not, then the group page is parsed to
473 * determine membership.
474 * @param string $group Name of the group to check for membership.
475 * @return boolean True if user is a member, else false.
477 function isMember($group){
478 if (isset($this->membership[$group])) {
479 return $this->membership[$group];
482 $group_page = $request->getPage($group);
483 if ($this->_inGroupPage($group_page)) {
484 $this->membership[$group] = true;
487 $this->membership[$group] = false;
488 // Let grouppages override certain defaults, such as members of admin
489 if ($this->specialGroup($group)) {
490 return $this->isSpecialMember($group);
496 * Private method to take a WikiDB_Page and parse to determine if the
497 * current_user is a member of the group.
498 * @param object $group_page WikiDB_Page object for the group's page
499 * @return boolean True if user is a member, else false.
502 function _inGroupPage($group_page, $strict=false){
503 $group_revision = $group_page->getCurrentRevision();
504 if ($group_revision->hasDefaultContents()) {
505 $group = $group_page->getName();
506 if ($strict) trigger_error(sprintf(_("Group page '%s' does not exist"), $group),
510 $contents = $group_revision->getContent();
511 $match = '/^\s*[\*\#]+\s*' . $this->username . '\s*$/';
512 foreach ($contents as $line){
513 if (preg_match($match, $line)) {
521 * Determines all of the groups of which the current user is a member.
523 * Checks the root Group page ('CategoryGroup') for the list of all groups,
524 * then checks each group to see if the current user is a member.
525 * @param string $group Name of the group to check for membership.
526 * @return array Array of groups to which the user belongs.
528 function getAllGroupsIn(){
529 $membership = array();
531 $specialgroups = $this->specialGroups();
532 foreach ($specialgroups as $group) {
533 $this->membership[$group] = $this->isSpecialMember($group);
537 $dbh = &$request->_dbi;
538 $master_page = $request->getPage(CATEGORY_GROUP_PAGE);
539 $master_list = $master_page->getLinks(true);
540 while ($group_page = $master_list->next()){
541 $group = $group_page->getName();
542 $this->membership[$group] = $this->_inGroupPage($group_page);
544 foreach ($this->membership as $g => $bool) {
545 if ($bool) $membership[] = $g;
551 * Determines all of the members of a particular group.
553 * Checks a group's page to return all the current members. Currently this
554 * method is disabled and triggers an error and returns an empty array.
555 * @param string $group Name of the group to get the full membership list of.
556 * @return array Array of usernames that have joined the group (always empty).
558 function getMembersOf($group){
559 if ($this->specialGroup($group))
560 return $this->getSpecialMembersOf($group);
562 trigger_error("GroupWikiPage::getMembersOf is not yet implimented",
566 * Waiting for a reliable way to check if a string is a username.
567 $request = $this->request;
569 $group_page = $request->getPage($group);
570 $group_revision = $group_page->getCurrentRevision();
571 if ($group_revision->hasDefaultContents()) {
572 trigger_error("Group $group does not exist", E_USER_WARNING);
575 $contents = $group_revision->getContent();
576 $match = '/^(\s*[\*\#]+\s*)(\w+)(\s*)$/';
578 foreach($contents as $line){
580 if(preg_match($match, $line, $matches)){
581 $members[] = $matches[2];
590 * GroupDb is configured by $DbAuthParams[] statements
595 class GroupDb extends WikiGroup {
597 var $_is_member, $_group_members, $_user_groups;
602 * @param object $request The global WikiRequest object. ignored
605 global $DBAuthParams, $DBParams;
606 //$this->request = &$GLOBALS['request'];
607 $this->username = $this->_getUserName();
608 $this->membership = array();
610 if (empty($DBAuthParams['group_members']) or
611 empty($DBAuthParams['user_groups']) or
612 empty($DBAuthParams['is_member'])) {
613 trigger_error(_("No or not enough GROUP_DB SQL statements defined"),
615 return new GroupNone();
617 // FIXME: This only works with ENABLE_USER_NEW
618 if (empty($this->user)) {
619 // use _PassUser::prepare instead
620 if (isa($request->getUser(),'_PassUser'))
621 $user = $request->getUser();
623 $user = new _PassUser($this->username);
624 } elseif (!isa($this->user, '_PassUser')) {
625 $user = new _PassUser($this->username);
627 $user =& $this->user;
629 if (isa($this->user, '_PassUser')) { // TODO: safety by Charles Corrigan
630 $this->_is_member = $user->prepare($DBAuthParams['is_member'],
631 array('userid','groupname'));
632 $this->_group_members = $user->prepare($DBAuthParams['group_members'],'groupname');
633 $this->_user_groups = $user->prepare($DBAuthParams['user_groups'],'userid');
634 $this->dbh = $user->_auth_dbi;
644 class GroupDb_PearDB extends GroupDb {
647 * Determines if the current user is a member of a database group.
649 * @param string $group Name of the group to check for membership.
650 * @return boolean True if user is a member, else false.
652 function isMember($group) {
653 if (isset($this->membership[$group])) {
654 return $this->membership[$group];
657 $db_result = $dbh->query(sprintf($this->_is_member,$dbh->quote($this->username),
658 $dbh->quote($group)));
659 if ($db_result->numRows() > 0) {
660 $this->membership[$group] = true;
663 $this->membership[$group] = false;
664 // Let override certain defaults, such as members of admin
665 if ($this->specialGroup($group))
666 return $this->isSpecialMember($group);
671 * Determines all of the groups of which the current user is a member.
673 * then checks each group to see if the current user is a member.
674 * @param string $group Name of the group to check for membership.
675 * @return array Array of groups to which the user belongs.
677 function getAllGroupsIn(){
678 $membership = array();
681 $db_result = $dbh->query(sprintf($this->_user_groups,$dbh->quote($this->username)));
682 if ($db_result->numRows() > 0) {
683 while (list($group) = $db_result->fetchRow(DB_FETCHMODE_ORDERED)) {
684 $membership[] = $group;
685 $this->membership[$group] = true;
689 $specialgroups = $this->specialGroups();
690 foreach ($specialgroups as $group) {
691 if ($this->isMember($group)) {
692 $membership[] = $group;
699 * Determines all of the members of a particular group.
701 * Checks a group's page to return all the current members. Currently this
702 * method is disabled and triggers an error and returns an empty array.
703 * @param string $group Name of the group to get the full membership list of.
704 * @return array Array of usernames that have joined the group.
706 function getMembersOf($group){
710 $db_result = $dbh->query(sprintf($this->_group_members,$dbh->quote($group)));
711 if ($db_result->numRows() > 0) {
712 while (list($userid) = $db_result->fetchRow(DB_FETCHMODE_ORDERED)) {
713 $members[] = $userid;
716 // add certain defaults, such as members of admin
717 if ($this->specialGroup($group))
718 $members = array_merge($members, $this->getSpecialMembersOf($group));
728 class GroupDb_ADODB extends GroupDb {
731 * Determines if the current user is a member of a database group.
733 * @param string $group Name of the group to check for membership.
734 * @return boolean True if user is a member, else false.
736 function isMember($group) {
737 if (isset($this->membership[$group])) {
738 return $this->membership[$group];
741 $rs = $dbh->Execute(sprintf($this->_is_member,$dbh->qstr($this->username),
742 $dbh->qstr($group)));
746 if ($rs->numRows() > 0) {
747 $this->membership[$group] = true;
752 $this->membership[$group] = false;
753 if ($this->specialGroup($group))
754 return $this->isSpecialMember($group);
760 * Determines all of the groups of which the current user is a member.
761 * then checks each group to see if the current user is a member.
763 * @param string $group Name of the group to check for membership.
764 * @return array Array of groups to which the user belongs.
766 function getAllGroupsIn(){
767 $membership = array();
770 $rs = $dbh->Execute(sprintf($this->_user_groups, $dbh->qstr($this->username)));
771 if (!$rs->EOF and $rs->numRows() > 0) {
773 $group = reset($rs->fields);
774 $membership[] = $group;
775 $this->membership[$group] = true;
781 $specialgroups = $this->specialGroups();
782 foreach ($specialgroups as $group) {
783 if ($this->isMember($group)) {
784 $membership[] = $group;
791 * Determines all of the members of a particular group.
793 * @param string $group Name of the group to get the full membership list of.
794 * @return array Array of usernames that have joined the group.
796 function getMembersOf($group){
799 $rs = $dbh->Execute(sprintf($this->_group_members,$dbh->qstr($group)));
800 if (!$rs->EOF and $rs->numRows() > 0) {
802 $members[] = reset($rs->fields);
807 // add certain defaults, such as members of admin
808 if ($this->specialGroup($group))
809 $members = array_merge($members, $this->getSpecialMembersOf($group));
815 * GroupFile is configured by AUTH_GROUP_FILE
816 * groupname: user1 user2 ...
820 class GroupFile extends WikiGroup {
825 * @param object $request The global WikiRequest object.
827 function GroupFile(){
828 //$this->request = &$GLOBALS['request'];
829 $this->username = $this->_getUserName();
830 //$this->username = null;
831 $this->membership = array();
833 if (!defined('AUTH_GROUP_FILE')) {
834 trigger_error(_("AUTH_GROUP_FILE not defined"), E_USER_WARNING);
837 if (!file_exists(AUTH_GROUP_FILE)) {
838 trigger_error(sprintf(_("Cannot open AUTH_GROUP_FILE %s"), AUTH_GROUP_FILE),
842 require_once('lib/pear/File_Passwd.php');
843 $this->_file = new File_Passwd(AUTH_GROUP_FILE,false,AUTH_GROUP_FILE.".lock");
847 * Determines if the current user is a member of a group.
849 * To determine membership in a particular group, this method checks the
850 * superclass instance variable $membership to see if membership has
851 * already been determined. If not, then the group file is parsed to
852 * determine membership.
853 * @param string $group Name of the group to check for membership.
854 * @return boolean True if user is a member, else false.
856 function isMember($group) {
857 //$request = $this->request;
858 //$username = $this->username;
859 if (isset($this->membership[$group])) {
860 return $this->membership[$group];
863 if (is_array($this->_file->users)) {
864 foreach ($this->_file->users as $g => $u) {
865 $users = explode(' ',$u);
866 if (in_array($this->username,$users)) {
867 $this->membership[$group] = true;
872 $this->membership[$group] = false;
873 if ($this->specialGroup($group))
874 return $this->isSpecialMember($group);
879 * Determines all of the groups of which the current user is a member.
881 * then checks each group to see if the current user is a member.
882 * @param string $group Name of the group to check for membership.
883 * @return array Array of groups to which the user belongs.
885 function getAllGroupsIn(){
886 //$username = $this->_getUserName();
887 $membership = array();
889 if (is_array($this->_file->users)) {
890 foreach ($this->_file->users as $group => $u) {
891 $users = explode(' ',$u);
892 if (in_array($this->username,$users)) {
893 $this->membership[$group] = true;
894 $membership[] = $group;
899 $specialgroups = $this->specialGroups();
900 foreach ($specialgroups as $group) {
901 if ($this->isMember($group)) {
902 $this->membership[$group] = true;
903 $membership[] = $group;
910 * Determines all of the members of a particular group.
912 * Return all the current members.
913 * @param string $group Name of the group to get the full membership list of.
914 * @return array Array of usernames that have joined the group.
916 function getMembersOf($group){
918 if (!empty($this->_file->users[$group])) {
919 $members = explode(' ',$this->_file->users[$group]);
921 if ($this->specialGroup($group)) {
922 $members = array_merge($members, $this->getSpecialMembersOf($group));
929 * Ldap is configured in index.php
933 class GroupLdap extends WikiGroup {
938 * @param object $request The global WikiRequest object.
940 function GroupLdap(){
941 //$this->request = &$GLOBALS['request'];
942 $this->username = $this->_getUserName();
943 $this->membership = array();
945 if (!defined("LDAP_AUTH_HOST")) {
946 trigger_error(_("LDAP_AUTH_HOST not defined"), E_USER_WARNING);
949 // We should ignore multithreaded environments, not generally windows.
951 if (! function_exists('ldap_connect') and (!isWindows() or isCGI())) {
952 // on MacOSX >= 4.3 you'll need PHP_SHLIB_SUFFIX instead.
953 dl("ldap".defined('PHP_SHLIB_SUFFIX') ? PHP_SHLIB_SUFFIX : DLL_EXT);
954 if (! function_exists('ldap_connect')) {
955 trigger_error(_("No LDAP in this PHP version"), E_USER_WARNING);
959 if (!defined("LDAP_BASE_DN"))
960 define("LDAP_BASE_DN",'');
961 $this->base_dn = LDAP_BASE_DN;
962 // if no users ou (organizational unit) is defined,
963 // then take out the ou= from the base_dn (if exists) and append a default
964 // from users and group
966 if (strstr(LDAP_BASE_DN, "ou="))
967 $this->base_dn = preg_replace("/(ou=\w+,)?()/","\$2", LDAP_BASE_DN);
969 if (!isset($this->user) or !isa($this->user, '_LDAPPassUser'))
970 $this->_user = new _LDAPPassUser('LdapGroupTest'); // to have a valid username
972 $this->_user =& $this->user;
976 * Determines if the current user is a member of a group.
979 * @param string $group Name of the group to check for membership.
980 * @return boolean True if user is a member, else false.
982 function isMember($group) {
983 if (isset($this->membership[$group])) {
984 return $this->membership[$group];
986 //$request = $this->request;
987 //$username = $this->_getUserName();
988 $this->membership[$group] = in_array($this->username,$this->getMembersOf($group));
989 if ($this->membership[$group])
991 if ($this->specialGroup($group))
992 return $this->isSpecialMember($group);
996 * Determines all of the groups of which the current user is a member.
998 * @param string $group Name of the group to check for membership.
999 * @return array Array of groups to which the user belongs.
1001 function getAllGroupsIn(){
1002 //$request = &$this->request;
1003 //$username = $this->_getUserName();
1004 $membership = array();
1006 $specialgroups = $this->specialGroups();
1007 foreach ($specialgroups as $group) {
1008 if ($this->isMember($group)) {
1009 $this->membership[$group] = true;
1010 $membership[] = $group;
1014 // must be a valid LDAP server, and username must not contain a wildcard
1015 if ($ldap = $this->_user->_init()) {
1016 $st_search = LDAP_SEARCH_FIELD ? LDAP_SEARCH_FIELD."=".$this->username
1017 : "uid=".$this->username;
1018 $sr = ldap_search($ldap, (LDAP_OU_USERS ? LDAP_OU_USERS : "ou=Users")
1019 .($this->base_dn ? ",".$this->base_dn : ''),
1022 $this->_user->_free();
1023 return $this->membership;
1025 $info = ldap_get_entries($ldap, $sr);
1026 if (empty($info["count"])) {
1027 $this->_user->_free();
1028 return $this->membership;
1030 for ($i = 0; $i < $info["count"]; $i++) {
1031 if ($info[$i]["gidNumber"]["count"]) {
1032 $gid = $info[$i]["gidnumber"][0];
1033 $sr2 = ldap_search($ldap, (LDAP_OU_GROUP ? LDAP_OU_GROUP : "ou=Groups")
1034 .($this->base_dn ? ",".$this->base_dn : ''),
1037 $info2 = ldap_get_entries($ldap, $sr2);
1038 if (!empty($info2["count"]))
1039 $membership[] = $info2[0]["cn"][0];
1044 trigger_error(fmt("Unable to connect to LDAP server %s", LDAP_AUTH_HOST),
1047 $this->_user->_free();
1048 //ldap_close($ldap);
1049 $this->membership = $membership;
1054 * Determines all of the members of a particular group.
1056 * Return all the members of the given group. LDAP just returns the gid of each user
1057 * @param string $group Name of the group to get the full membership list of.
1058 * @return array Array of usernames that have joined the group.
1060 function getMembersOf($group){
1062 if ($ldap = $this->_user->_init()) {
1063 $base_dn = (LDAP_OU_GROUP ? LDAP_OU_GROUP : "ou=Groups")
1064 .($this->base_dn ? ",".$this->base_dn : '');
1065 $sr2 = ldap_search($ldap, $base_dn, "cn=$group");
1067 $info = ldap_get_entries($ldap, $sr);
1069 $info = array('count' => 0);
1070 trigger_error("LDAP_SEARCH: base=\"$base_dn\" \"(cn=$group)\" failed", E_USER_NOTICE);
1072 $base_dn = (LDAP_OU_USERS ? LDAP_OU_USERS : "ou=Users")
1073 .($this->base_dn ? ",".$this->base_dn : '');
1074 for ($i = 0; $i < $info["count"]; $i++) {
1075 $gid = $info[$i]["gidNumber"][0];
1076 //uid=* would be better probably
1077 $sr2 = ldap_search($ldap, $base_dn, "gidNumber=$gid");
1079 $info2 = ldap_get_entries($ldap, $sr2);
1080 for ($j = 0; $j < $info2["count"]; $j++) {
1081 $members[] = $info2[$j]["cn"][0];
1084 trigger_error("LDAP_SEARCH: base=\"$base_dn\" \"(gidNumber=$gid)\" failed", E_USER_NOTICE);
1088 $this->_user->_free();
1089 //ldap_close($ldap);
1091 if ($this->specialGroup($group)) {
1092 $members = array_merge($members, $this->getSpecialMembersOf($group));
1098 // $Log: not supported by cvs2svn $
1099 // Revision 1.46 2004/11/18 09:52:23 rurban
1100 // more safety, requested by Charles Corrigan
1102 // Revision 1.45 2004/11/17 20:06:30 rurban
1103 // possible group fix
1105 // Revision 1.44 2004/11/11 10:31:26 rurban
1106 // Disable default options in config-dist.ini
1107 // Add new CATEGORY_GROUP_PAGE root page: Default: Translation of "CategoryGroup"
1108 // Clarify more options.
1110 // Revision 1.43 2004/11/10 15:29:21 rurban
1111 // * requires newer Pear_DB (as the internal one): quote() uses now escapeSimple for strings
1112 // * ACCESS_LOG_SQL: fix cause request not yet initialized
1113 // * WikiDB: moved SQL specific methods upwards
1114 // * new Pear_DB quoting: same as ADODB and as newer Pear_DB.
1115 // fixes all around: WikiGroup, WikiUserNew SQL methods, SQL logging
1117 // Revision 1.42 2004/11/01 10:43:56 rurban
1118 // seperate PassUser methods into seperate dir (memory usage)
1119 // fix WikiUser (old) overlarge data session
1120 // remove wikidb arg from various page class methods, use global ->_dbi instead
1123 // Revision 1.41 2004/09/17 14:21:28 rurban
1124 // fix LDAP ou= issue, wrong strstr arg order
1126 // Revision 1.40 2004/06/29 06:48:02 rurban
1127 // Improve LDAP auth and GROUP_LDAP membership:
1128 // no error message on false password,
1129 // added two new config vars: LDAP_OU_USERS and LDAP_OU_GROUP with GROUP_METHOD=LDAP
1130 // fixed two group queries (this -> user)
1131 // stdlib: ConvertOldMarkup still flawed
1133 // Revision 1.39 2004/06/28 15:39:28 rurban
1134 // fixed endless recursion in WikiGroup: isAdmin()
1136 // Revision 1.38 2004/06/27 10:24:19 rurban
1137 // suggestion by Paul Henry
1139 // Revision 1.37 2004/06/25 14:29:18 rurban
1140 // WikiGroup refactoring:
1141 // global group attached to user, code for not_current user.
1142 // improved helpers for special groups (avoid double invocations)
1143 // new experimental config option ENABLE_XHTML_XML (fails with IE, and document.write())
1144 // fixed a XHTML validation error on userprefs.tmpl
1146 // Revision 1.36 2004/06/16 13:21:05 rurban
1147 // stabilize on failing ldap queries or bind
1149 // Revision 1.35 2004/06/16 12:08:25 rurban
1152 // Revision 1.34 2004/06/16 11:51:35 rurban
1153 // catch dl error on Windows
1155 // Revision 1.33 2004/06/15 10:40:35 rurban
1156 // minor WikiGroup cleanup: no request param, start of current user independency
1158 // Revision 1.32 2004/06/15 09:15:52 rurban
1159 // IMPORTANT: fixed passwd handling for passwords stored in prefs:
1160 // fix encrypted usage, actually store and retrieve them from db
1161 // fix bogologin with passwd set.
1162 // fix php crashes with call-time pass-by-reference (references wrongly used
1163 // in declaration AND call). This affected mainly Apache2 and IIS.
1164 // (Thanks to John Cole to detect this!)
1166 // Revision 1.31 2004/06/03 18:06:29 rurban
1167 // fix file locking issues (only needed on write)
1168 // fixed immediate LANG and THEME in-session updates if not stored in prefs
1169 // advanced editpage toolbars (search & replace broken)
1171 // Revision 1.30 2004/06/03 09:39:51 rurban
1172 // fix LDAP injection (wildcard in username) detected by Steve Christey, MITRE
1174 // Revision 1.29 2004/05/16 22:07:35 rurban
1175 // check more config-default and predefined constants
1176 // various PagePerm fixes:
1177 // fix default PagePerms, esp. edit and view for Bogo and Password users
1178 // implemented Creator and Owner
1179 // BOGOUSERS renamed to BOGOUSER
1180 // fixed syntax errors in signin.tmpl
1182 // Revision 1.28 2004/05/15 22:54:49 rurban
1183 // fixed important WikiDB bug with DEBUG > 0: wrong assertion
1184 // improved SetAcl (works) and PagePerms, some WikiGroup helpers.
1186 // Revision 1.27 2004/05/06 13:56:40 rurban
1187 // Enable the Administrators group, and add the WIKIPAGE group default root page.
1189 // Revision 1.26 2004/04/07 23:13:18 rurban
1190 // fixed pear/File_Passwd for Windows
1191 // fixed FilePassUser sessions (filehandle revive) and password update
1193 // Revision 1.25 2004/03/29 10:40:36 rurban
1194 // GroupDb_PearDB fetchmode fix
1196 // Revision 1.24 2004/03/14 16:26:22 rurban
1199 // Revision 1.23 2004/03/12 23:20:58 rurban
1200 // pref fixes (base64)
1202 // Revision 1.22 2004/03/12 15:48:07 rurban
1203 // fixed explodePageList: wrong sortby argument order in UnfoldSubpages
1204 // simplified lib/stdlib.php:explodePageList
1206 // Revision 1.21 2004/03/12 11:18:24 rurban
1207 // fixed ->membership chache
1209 // Revision 1.20 2004/03/12 10:47:30 rurban
1210 // fixed GroupDB for ADODB
1212 // Revision 1.19 2004/03/11 16:27:30 rurban
1213 // fixed GroupFile::getMembersOf for special groups
1214 // added authenticated bind for GroupLdap (Windows AD) as in WikiUserNew
1216 // Revision 1.18 2004/03/11 13:30:47 rurban
1217 // fixed File Auth for user and group
1218 // missing only getMembersOf(Authenticated Users),getMembersOf(Every),getMembersOf(Signed Users)
1220 // Revision 1.17 2004/03/10 15:38:48 rurban
1221 // store current user->page and ->action in session for WhoIsOnline
1222 // better WhoIsOnline icon
1223 // fixed WhoIsOnline warnings
1225 // Revision 1.15 2004/03/09 12:11:57 rurban
1226 // prevent from undefined DBAuthParams warning
1228 // Revision 1.14 2004/03/08 19:30:01 rurban
1229 // fixed Theme->getButtonURL
1230 // AllUsers uses now WikiGroup (also DB User and DB Pref users)
1231 // PageList fix for empty pagenames
1233 // Revision 1.13 2004/03/08 18:17:09 rurban
1234 // added more WikiGroup::getMembersOf methods, esp. for special groups
1235 // fixed $LDAP_SET_OPTIONS
1236 // fixed _AuthInfo group methods
1238 // Revision 1.12 2004/02/23 21:30:25 rurban
1239 // more PagePerm stuff: (working against 1.4.0)
1240 // ACL editing and simplification of ACL's to simple rwx------ string
1243 // Revision 1.11 2004/02/07 10:41:25 rurban
1244 // fixed auth from session (still double code but works)
1246 // fixed DbPassUser upgrade and policy=old
1249 // Revision 1.10 2004/02/03 09:45:39 rurban
1250 // LDAP cleanup, start of new Pref classes
1252 // Revision 1.9 2004/02/01 09:14:11 rurban
1253 // Started with Group_Ldap (not yet ready)
1254 // added new _AuthInfo plugin to help in auth problems (warning: may display passwords)
1255 // fixed some configurator vars
1256 // renamed LDAP_AUTH_SEARCH to LDAP_BASE_DN
1257 // changed PHPWIKI_VERSION from 1.3.8a to 1.3.8pre
1258 // USE_DB_SESSION defaults to true on SQL
1259 // changed GROUP_METHOD definition to string, not constants
1260 // changed sample user DBAuthParams from UPDATE to REPLACE to be able to
1261 // create users. (Not to be used with external databases generally, but
1262 // with the default internal user table)
1264 // fixed the IndexAsConfigProblem logic. this was flawed:
1265 // scripts which are the same virtual path defined their own lib/main call
1266 // (hmm, have to test this better, phpwiki.sf.net/demo works again)
1268 // Revision 1.8 2004/01/27 23:23:39 rurban
1269 // renamed ->Username => _userid for consistency
1270 // renamed mayCheckPassword => mayCheckPass
1271 // fixed recursion problem in WikiUserNew
1272 // fixed bogo login (but not quite 100% ready yet, password storage)
1274 // Revision 1.7 2004/01/26 16:52:40 rurban
1275 // added GroupDB and GroupFile classes
1277 // Revision 1.6 2003/12/07 19:29:11 carstenklapp
1278 // Code Housecleaning: fixed syntax errors. (php -l *.php)
1280 // Revision 1.5 2003/02/22 20:49:55 dairiki
1281 // Fixes for "Call-time pass by reference has been deprecated" errors.
1283 // Revision 1.4 2003/01/21 04:02:39 zorloc
1284 // Added Log entry and page footer.
1290 // c-basic-offset: 4
1291 // c-hanging-comment-ender-p: nil
1292 // indent-tabs-mode: nil