]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/plugin/WikiAdminSetAcl.php
add acl field to mimified dump
[SourceForge/phpwiki.git] / lib / plugin / WikiAdminSetAcl.php
1 <?php // -*-php-*-
2 rcs_id('$Id: WikiAdminSetAcl.php,v 1.14 2004-06-07 22:28:06 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  * Doesn't accept yet s=wildcard preselection
31  * Requires PHP 4.2 so far.
32  */
33 require_once('lib/PageList.php');
34 require_once('lib/plugin/WikiAdminSelect.php');
35
36 class WikiPlugin_WikiAdminSetAcl
37 extends WikiPlugin_WikiAdminSelect
38 {
39     function getName() {
40         return _("WikiAdminSetAcl");
41     }
42
43     function getDescription() {
44         return _("Set individual page permissions.");
45     }
46
47     function getVersion() {
48         return preg_replace("/[Revision: $]/", '',
49                             "\$Revision: 1.14 $");
50     }
51
52     function getDefaultArguments() {
53         return array(
54                      's'        => false,
55                      'p'        => "[]",
56                      /* Pages to exclude in listing */
57                      'exclude'  => '',
58                      /* Columns to include in listing */
59                      'info'     => 'pagename,perm,mtime,owner,author',
60                      /* How to sort */
61                      'sortby'   => 'pagename',
62                      'limit'    => 0,
63                      );
64     }
65
66     function setaclPages(&$request, $pages, $acl) {
67         $ul = HTML::ul();
68         $count = 0;
69         $dbi =& $request->_dbi; 
70         // check new_group and new_perm
71         if (isset($acl['_add_group'])) {
72             //add groups with perm
73             foreach ($acl['_add_group'] as $access => $dummy) {
74                 $group = $acl['_new_group'][$access];
75                 $acl[$access][$group] = isset($acl['_new_perm'][$access]) ? 1 : 0;
76             }
77             unset($acl['_add_group']); 
78         }
79         unset($acl['_new_group']); unset($acl['_new_perm']);
80         if (isset($acl['_del_group'])) {
81             //del groups with perm
82             foreach ($acl['_del_group'] as $access => $del) {
83                 while (list($group,$dummy) = each($del)) 
84                     unset($acl[$access][$group]);
85             }
86             unset($acl['_del_group']);
87         }
88         if ($perm = new PagePermission($acl)) {
89             $perm->sanify();
90             foreach ($pages as $pagename) {
91                 // check if unchanged? we need a deep array_equal
92                 $page = $dbi->getPage($pagename);
93                 $oldperm = getPagePermissions($page);
94                 if ($oldperm)
95                     $oldperm->sanify();
96                 if ($oldperm and $perm->equal($oldperm->perm)) // (serialize($oldperm->perm) == serialize($perm->perm))
97                     $ul->pushContent(HTML::li(fmt("ACL not changed for page '%s'.",$pagename)));
98                 elseif (mayAccessPage('change', $pagename)) {
99                     setPagePermissions ($page, $perm);
100                     $ul->pushContent(HTML::li(fmt("ACL changed for page '%s'.",$pagename)));
101                     $count++;
102                 } else {
103                     $ul->pushContent(HTML::li(fmt("Access denied to change page '%s'.",$pagename)));
104                 }
105             }
106         } else {
107             $ul->pushContent(HTML::li(fmt("Invalid ACL")));
108         }
109         if ($count) {
110             $dbi->touch();
111             return HTML($ul,
112                         HTML::p(fmt("%s pages have been changed.",$count)));
113         } else {
114             return HTML($ul,
115                         HTML::p(fmt("No pages changed.")));
116         }
117     }
118     
119     function run($dbi, $argstr, &$request, $basepage) {
120         //if (!DEBUG)
121         //    return $this->disabled("WikiAdminSetAcl not yet enabled. Set DEBUG to try it.");
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         //$header->pushContent(HTML::p(HTML::em(_("This plugin is currently under development and does not work!"))));
210         //todo: find intersection of all page perms
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::p(fmt("Selected Pages: %s",join(', ',$pages))));
218         if (DEBUG) {
219             ;//$header->pushContent(HTML::pre("Permission tree for $name:\n",print_r($perm_tree,true)));
220         }
221         $type = $perm_tree[0];
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::p(_("Type: "),$type));
229         $header->pushContent(HTML::p(
230                                      _("Description: Selected Grant checkboxes allow access, unselected checkboxes deny access."),
231                                      _("To ignore delete the line."),
232                                      _("To add check 'Add' near the dropdown list.")
233                                      ));
234         $header->pushContent(HTML::blockquote($table));
235         //
236         // display array of checkboxes for existing perms
237         // and a dropdown for user/group to add perms.
238         // disabled if inherited, 
239         // checkbox to disable inheritance, 
240         // another checkbox to progate new permissions to all childs (if there exist some)
241         //Todo:
242         // warn if more pages are selected and they have different perms
243         //$header->pushContent(HTML::input(array('name' => 'admin_setacl[acl]',
244         //                                       'value' => $post_args['acl'])));
245         $header->pushContent(HTML::br());
246         if (!empty($pages) and DEBUG) {
247           $checkbox = HTML::input(array('type' => 'checkbox',
248                                         'name' => 'admin_setacl[updatechildren]',
249                                         'value' => 1));
250           if (!empty($post_args['updatechildren']))  $checkbox->setAttr('checked','checked');
251           $header->pushContent($checkbox,
252                   _("Propagate new permissions to all subpages?"),
253                   HTML::raw("&nbsp;&nbsp;"),
254                   HTML::em(_("(disable individual page permissions, enable inheritance)?")),
255                   HTML::em(_("(Currently not working)"))
256                                );
257         }
258         $header->pushContent(HTML::hr(),HTML::p());
259         return $header;
260     }
261 }
262
263 class _PageList_Column_acl extends _PageList_Column {
264     function _getValue ($page_handle, &$revision_handle) {
265         $perm_tree = pagePermissions($page_handle->_pagename);
266         return pagePermissionsAclFormat($perm_tree);
267         if (0) {
268             ob_start();
269             var_dump($perm_array);
270             $xml = ob_get_contents();
271             ob_end_clean();
272             return $xml;
273         }
274     }
275 };
276
277 class _PageList_Column_perm extends _PageList_Column {
278     function _getValue ($page_handle, &$revision_handle) {
279         $perm_array = pagePermissions($page_handle->_pagename);
280         return pagePermissionsSimpleFormat($perm_array,
281                                            $page_handle->get('author'),
282                                            $page_handle->get('group'));
283         if (0) {
284             ob_start();
285             var_dump($perm_array);
286             $xml = ob_get_contents();
287             ob_end_clean();
288             return $xml;
289         }
290     }
291 };
292
293 // $Log: not supported by cvs2svn $
294 // Revision 1.13  2004/06/04 20:32:54  rurban
295 // Several locale related improvements suggested by Pierrick Meignen
296 // LDAP fix by John Cole
297 // reanable admin check without ENABLE_PAGEPERM in the admin plugins
298 //
299 // Revision 1.12  2004/06/03 22:24:48  rurban
300 // reenable admin check on !ENABLE_PAGEPERM, honor s=Wildcard arg, fix warning after Remove
301 //
302 // Revision 1.11  2004/06/01 15:28:02  rurban
303 // AdminUser only ADMIN_USER not member of Administrators
304 // some RateIt improvements by dfrankow
305 // edit_toolbar buttons
306 //
307 // Revision 1.10  2004/05/27 17:49:06  rurban
308 // renamed DB_Session to DbSession (in CVS also)
309 // added WikiDB->getParam and WikiDB->getAuthParam method to get rid of globals
310 // remove leading slash in error message
311 // added force_unlock parameter to File_Passwd (no return on stale locks)
312 // fixed adodb session AffectedRows
313 // added FileFinder helpers to unify local filenames and DATA_PATH names
314 // editpage.php: new edit toolbar javascript on ENABLE_EDIT_TOOLBAR
315 //
316 // Revision 1.9  2004/05/24 17:34:53  rurban
317 // use ACLs
318 //
319 // Revision 1.8  2004/05/16 22:32:54  rurban
320 // setacl icons
321 //
322 // Revision 1.7  2004/05/16 22:07:35  rurban
323 // check more config-default and predefined constants
324 // various PagePerm fixes:
325 //   fix default PagePerms, esp. edit and view for Bogo and Password users
326 //   implemented Creator and Owner
327 //   BOGOUSERS renamed to BOGOUSER
328 // fixed syntax errors in signin.tmpl
329 //
330 // Revision 1.5  2004/04/07 23:13:19  rurban
331 // fixed pear/File_Passwd for Windows
332 // fixed FilePassUser sessions (filehandle revive) and password update
333 //
334 // Revision 1.4  2004/03/17 20:23:44  rurban
335 // fixed p[] pagehash passing from WikiAdminSelect, fixed problem removing pages with [] in the pagename
336 //
337 // Revision 1.3  2004/03/12 13:31:43  rurban
338 // enforce PagePermissions, errormsg if not Admin
339 //
340 // Revision 1.2  2004/02/24 04:02:07  rurban
341 // Better warning messages
342 //
343 // Revision 1.1  2004/02/23 21:30:25  rurban
344 // more PagePerm stuff: (working against 1.4.0)
345 //   ACL editing and simplification of ACL's to simple rwx------ string
346 //   not yet working.
347 //
348 //
349
350 // Local Variables:
351 // mode: php
352 // tab-width: 8
353 // c-basic-offset: 4
354 // c-hanging-comment-ender-p: nil
355 // indent-tabs-mode: nil
356 // End:
357 ?>