]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/plugin/WikiAdminSetAcl.php
renamed global $Theme to $WikiTheme (gforge nameclash)
[SourceForge/phpwiki.git] / lib / plugin / WikiAdminSetAcl.php
1 <?php // -*-php-*-
2 rcs_id('$Id: WikiAdminSetAcl.php,v 1.18 2004-06-14 11:31:39 rurban Exp $');
3 /*
4  Copyright 2004 $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  * Set individual PagePermissions
25  *
26  * Usage:   <?plugin WikiAdminSetAcl ?> or called via WikiAdminSelect
27  * Author:  Reini Urban <rurban@x-ray.at>
28  *
29  * KNOWN ISSUES:
30  * Requires PHP 4.2 so far.
31  */
32 require_once('lib/PageList.php');
33 require_once('lib/plugin/WikiAdminSelect.php');
34
35 class WikiPlugin_WikiAdminSetAcl
36 extends WikiPlugin_WikiAdminSelect
37 {
38     function getName() {
39         return _("WikiAdminSetAcl");
40     }
41
42     function getDescription() {
43         return _("Set individual page permissions.");
44     }
45
46     function getVersion() {
47         return preg_replace("/[Revision: $]/", '',
48                             "\$Revision: 1.18 $");
49     }
50
51     function getDefaultArguments() {
52         return array_merge
53             (
54              PageList::supportedArgs(),
55              array(
56                      'p'        => "[]",  // list of pages
57                      's'        => false, /* select by pagename */
58                      /* Columns to include in listing */
59                      'info'     => 'pagename,perm,mtime,owner,author',
60                      ));
61     }
62
63     function setaclPages(&$request, $pages, $acl) {
64         $ul = HTML::ul();
65         $count = 0;
66         $dbi =& $request->_dbi; 
67         // check new_group and new_perm
68         if (isset($acl['_add_group'])) {
69             //add groups with perm
70             foreach ($acl['_add_group'] as $access => $dummy) {
71                 $group = $acl['_new_group'][$access];
72                 $acl[$access][$group] = isset($acl['_new_perm'][$access]) ? 1 : 0;
73             }
74             unset($acl['_add_group']); 
75         }
76         unset($acl['_new_group']); unset($acl['_new_perm']);
77         if (isset($acl['_del_group'])) {
78             //del groups with perm
79             foreach ($acl['_del_group'] as $access => $del) {
80                 while (list($group,$dummy) = each($del)) 
81                     unset($acl[$access][$group]);
82             }
83             unset($acl['_del_group']);
84         }
85         if ($perm = new PagePermission($acl)) {
86             $perm->sanify();
87             foreach ($pages as $pagename) {
88                 // check if unchanged? we need a deep array_equal
89                 $page = $dbi->getPage($pagename);
90                 $oldperm = getPagePermissions($page);
91                 if ($oldperm)
92                     $oldperm->sanify();
93                 if ($oldperm and $perm->equal($oldperm->perm)) // (serialize($oldperm->perm) == serialize($perm->perm))
94                     $ul->pushContent(HTML::li(fmt("ACL not changed for page '%s'.",$pagename)));
95                 elseif (mayAccessPage('change', $pagename)) {
96                     setPagePermissions ($page, $perm);
97                     $ul->pushContent(HTML::li(fmt("ACL changed for page '%s'.",$pagename)));
98                     $count++;
99                 } else {
100                     $ul->pushContent(HTML::li(fmt("Access denied to change page '%s'.",$pagename)));
101                 }
102             }
103         } else {
104             $ul->pushContent(HTML::li(fmt("Invalid ACL")));
105         }
106         if ($count) {
107             $dbi->touch();
108             return HTML($ul,
109                         HTML::p(fmt("%s pages have been changed.",$count)));
110         } else {
111             return HTML($ul,
112                         HTML::p(fmt("No pages changed.")));
113         }
114     }
115     
116     function run($dbi, $argstr, &$request, $basepage) {
117         //if (!DEBUG)
118         //    return $this->disabled("WikiAdminSetAcl not yet enabled. Set DEBUG to try it.");
119         if ($request->getArg('action') != 'browse')
120             if ($request->getArg('action') != _("PhpWikiAdministration/SetAcl"))
121                 return $this->disabled("(action != 'browse')");
122         
123         $args = $this->getArgs($argstr, $request);
124         $this->_args = $args;
125         if (!empty($args['exclude']))
126             $exclude = explodePageList($args['exclude']);
127         else
128             $exclude = false;
129         $this->preSelectS(&$args, &$request);
130
131         $p = $request->getArg('p');
132         $post_args = $request->getArg('admin_setacl');
133         $next_action = 'select';
134         $pages = array();
135         if ($p && !$request->isPost())
136             $pages = $p;
137         elseif ($this->_list)
138             $pages = $this->_list;
139         $header = HTML::p();
140         if ($p && $request->isPost() &&
141             !empty($post_args['acl']) && empty($post_args['cancel'])) {
142             // without individual PagePermissions:
143             if (!ENABLE_PAGEPERM and !$request->_user->isAdmin()) {
144                 $request->_notAuthorized(WIKIAUTH_ADMIN);
145                 $this->disabled("! user->isAdmin");
146             }
147             if ($post_args['action'] == 'verify') {
148                 // Real action
149                 $header->pushContent(
150                     $this->setaclPages($request, array_keys($p),
151                                        $request->getArg('acl')));
152             }
153             if ($post_args['action'] == 'select') {
154                 if (!empty($post_args['acl']))
155                     $next_action = 'verify';
156                 foreach ($p as $name => $c) {
157                     $pages[$name] = 1;
158                 }
159             }
160         }
161         if ($next_action == 'select' and empty($pages)) {
162             // List all pages to select from.
163             $pages = $this->collectPages($pages, $dbi, $args['sortby'], $args['limit']);
164         }
165         if ($next_action == 'verify') {
166             $args['info'] = "checkbox,pagename,perm,mtime,owner,author";
167         }
168         $pagelist = new PageList_Selectable($args['info'], 
169                                             $exclude,
170                                             array('types' => array(
171                                                   'perm'
172                                                   => new _PageList_Column_perm('perm', _("Permission")),
173                                                   'acl'
174                                                   => new _PageList_Column_acl('acl', _("ACL")))));
175
176         $pagelist->addPageList($pages);
177         if ($next_action == 'verify') {
178             $button_label = _("Yes");
179             $header = $this->setaclForm($header, $post_args, $pages);
180             $header->pushContent(
181               HTML::p(HTML::strong(
182                   _("Are you sure you want to permanently change access to the selected files?"))));
183         }
184         else {
185             $button_label = _("SetAcl");
186             $header = $this->setaclForm($header, $post_args, $pages);
187             $header->pushContent(HTML::p(_("Select the pages to change:")));
188         }
189
190         $buttons = HTML::p(Button('submit:admin_setacl[acl]', $button_label, 'wikiadmin'),
191                            Button('submit:admin_setacl[cancel]', _("Cancel"), 'button'));
192
193         return HTML::form(array('action' => $request->getPostURL(),
194                                 'method' => 'post'),
195                           $header,
196                           $pagelist->getContent(),
197                           HiddenInputs($request->getArgs(),
198                                         false,
199                                         array('admin_setacl')),
200                           HiddenInputs(array('admin_setacl[action]' => $next_action)),
201                           ENABLE_PAGEPERM
202                           ? ''
203                           : HiddenInputs(array('require_authority_for_post' => WIKIAUTH_ADMIN)),
204                           $buttons);
205     }
206
207     function setaclForm(&$header, $post_args, $pagehash) {
208         $acl = $post_args['acl'];
209
210         //FIXME: find intersection of all pages perms, not just from the last pagename
211         $pages = array();
212         foreach ($pagehash as $name => $checked) {
213            if ($checked) $pages[] = $name;
214         }
215         $perm_tree = pagePermissions($name);
216         $table = pagePermissionsAclFormat($perm_tree, !empty($pages));
217         $header->pushContent(HTML::strong(_("Selected Pages: ")), HTML::tt(join(', ',$pages)), HTML::br());
218         $first_page = $GLOBALS['request']->_dbi->getPage($name);
219         $owner = $first_page->getOwner();
220         list($type, $perm) = pagePermissionsAcl($perm_tree[0], $perm_tree);
221         //if (DEBUG) $header->pushContent(HTML::pre("Permission tree for $name:\n",print_r($perm_tree,true)));
222         if ($type == 'inherited')
223             $type = sprintf(_("page permission inherited from %s"), $perm_tree[1][0]);
224         elseif ($type == 'page')
225             $type = _("invidual page permission");
226         elseif ($type == 'default')
227             $type = _("default page permission");
228         $header->pushContent(HTML::strong(_("Type: ")), HTML::tt($type),HTML::br());
229         $header->pushContent(HTML::strong(_("getfacl: ")), pagePermissionsSimpleFormat($perm_tree, $owner),HTML::br());
230         $header->pushContent(HTML::strong(_("ACL: ")), HTML::tt($perm->asAclLines()),HTML::br());
231         
232         $header->pushContent(HTML::p(HTML::strong(_("Description: ")),
233                                      _("Selected Grant checkboxes allow access, unselected checkboxes deny access."),
234                                      _("To ignore delete the line."),
235                                      _("To add check 'Add' near the dropdown list.")
236                                      ));
237         $header->pushContent(HTML::blockquote($table));
238         //
239         // display array of checkboxes for existing perms
240         // and a dropdown for user/group to add perms.
241         // disabled if inherited, 
242         // checkbox to disable inheritance, 
243         // another checkbox to progate new permissions to all childs (if there exist some)
244         //Todo:
245         // warn if more pages are selected and they have different perms
246         //$header->pushContent(HTML::input(array('name' => 'admin_setacl[acl]',
247         //                                       'value' => $post_args['acl'])));
248         $header->pushContent(HTML::br());
249         if (!empty($pages) and DEBUG) {
250           $checkbox = HTML::input(array('type' => 'checkbox',
251                                         'name' => 'admin_setacl[updatechildren]',
252                                         'value' => 1));
253           if (!empty($post_args['updatechildren']))  $checkbox->setAttr('checked','checked');
254           $header->pushContent($checkbox,
255                   _("Propagate new permissions to all subpages?"),
256                   HTML::raw("&nbsp;&nbsp;"),
257                   HTML::em(_("(disable individual page permissions, enable inheritance)?")),
258                   HTML::br(),HTML::em(_("(Currently not working)"))
259                                );
260         }
261         $header->pushContent(HTML::hr(),HTML::p());
262         return $header;
263     }
264 }
265
266 class _PageList_Column_acl extends _PageList_Column {
267     function _getValue ($page_handle, &$revision_handle) {
268         $perm_tree = pagePermissions($page_handle->_pagename);
269         return pagePermissionsAclFormat($perm_tree);
270         if (0) {
271             ob_start();
272             var_dump($perm_array);
273             $xml = ob_get_contents();
274             ob_end_clean();
275             return $xml;
276         }
277     }
278 };
279
280 class _PageList_Column_perm extends _PageList_Column {
281     function _getValue ($page_handle, &$revision_handle) {
282         $perm_array = pagePermissions($page_handle->_pagename);
283         return pagePermissionsSimpleFormat($perm_array,
284                                            $page_handle->get('author'),
285                                            $page_handle->get('group'));
286         if (0) {
287             ob_start();
288             var_dump($perm_array);
289             $xml = ob_get_contents();
290             ob_end_clean();
291             return $xml;
292         }
293     }
294 };
295
296 // $Log: not supported by cvs2svn $
297 // Revision 1.17  2004/06/13 15:33:20  rurban
298 // new support for arguments owner, author, creator in most relevant
299 // PageList plugins. in WikiAdmin* via preSelectS()
300 //
301 // Revision 1.16  2004/06/08 13:50:43  rurban
302 // show getfacl and acl line
303 //
304 // Revision 1.15  2004/06/08 10:05:12  rurban
305 // simplified admin action shortcuts
306 //
307 // Revision 1.14  2004/06/07 22:28:06  rurban
308 // add acl field to mimified dump
309 //
310 // Revision 1.13  2004/06/04 20:32:54  rurban
311 // Several locale related improvements suggested by Pierrick Meignen
312 // LDAP fix by John Cole
313 // reanable admin check without ENABLE_PAGEPERM in the admin plugins
314 //
315 // Revision 1.12  2004/06/03 22:24:48  rurban
316 // reenable admin check on !ENABLE_PAGEPERM, honor s=Wildcard arg, fix warning after Remove
317 //
318 // Revision 1.11  2004/06/01 15:28:02  rurban
319 // AdminUser only ADMIN_USER not member of Administrators
320 // some RateIt improvements by dfrankow
321 // edit_toolbar buttons
322 //
323 // Revision 1.10  2004/05/27 17:49:06  rurban
324 // renamed DB_Session to DbSession (in CVS also)
325 // added WikiDB->getParam and WikiDB->getAuthParam method to get rid of globals
326 // remove leading slash in error message
327 // added force_unlock parameter to File_Passwd (no return on stale locks)
328 // fixed adodb session AffectedRows
329 // added FileFinder helpers to unify local filenames and DATA_PATH names
330 // editpage.php: new edit toolbar javascript on ENABLE_EDIT_TOOLBAR
331 //
332 // Revision 1.9  2004/05/24 17:34:53  rurban
333 // use ACLs
334 //
335 // Revision 1.8  2004/05/16 22:32:54  rurban
336 // setacl icons
337 //
338 // Revision 1.7  2004/05/16 22:07:35  rurban
339 // check more config-default and predefined constants
340 // various PagePerm fixes:
341 //   fix default PagePerms, esp. edit and view for Bogo and Password users
342 //   implemented Creator and Owner
343 //   BOGOUSERS renamed to BOGOUSER
344 // fixed syntax errors in signin.tmpl
345 //
346 // Revision 1.5  2004/04/07 23:13:19  rurban
347 // fixed pear/File_Passwd for Windows
348 // fixed FilePassUser sessions (filehandle revive) and password update
349 //
350 // Revision 1.4  2004/03/17 20:23:44  rurban
351 // fixed p[] pagehash passing from WikiAdminSelect, fixed problem removing pages with [] in the pagename
352 //
353 // Revision 1.3  2004/03/12 13:31:43  rurban
354 // enforce PagePermissions, errormsg if not Admin
355 //
356 // Revision 1.2  2004/02/24 04:02:07  rurban
357 // Better warning messages
358 //
359 // Revision 1.1  2004/02/23 21:30:25  rurban
360 // more PagePerm stuff: (working against 1.4.0)
361 //   ACL editing and simplification of ACL's to simple rwx------ string
362 //   not yet working.
363 //
364 //
365
366 // Local Variables:
367 // mode: php
368 // tab-width: 8
369 // c-basic-offset: 4
370 // c-hanging-comment-ender-p: nil
371 // indent-tabs-mode: nil
372 // End:
373 ?>