]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/plugin/WikiAdminSetAcl.php
fixed pear/File_Passwd for Windows
[SourceForge/phpwiki.git] / lib / plugin / WikiAdminSetAcl.php
1 <?php // -*-php-*-
2 rcs_id('$Id: WikiAdminSetAcl.php,v 1.5 2004-04-07 23:13:19 rurban Exp $');
3 /*
4  Copyright 2004 $ThePhpWikiProgrammingTeam
5
6  This file is not yet part of PhpWiki. It does not work yet.
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  * Currently we must be Admin. Later we use PagePermissions authorization.
31  *   (require_authority_for_post' => WIKIAUTH_ADMIN)
32  * Requires PHP 4.2 so far.
33  */
34 require_once('lib/PageList.php');
35 require_once('lib/plugin/WikiAdminSelect.php');
36
37 class WikiPlugin_WikiAdminSetAcl
38 extends WikiPlugin_WikiAdminSelect
39 {
40     function getName() {
41         return _("WikiAdminSetAcl");
42     }
43
44     function getDescription() {
45         return _("Set individual page permissions.");
46     }
47
48     function getVersion() {
49         return preg_replace("/[Revision: $]/", '',
50                             "\$Revision: 1.5 $");
51     }
52
53     function getDefaultArguments() {
54         return array(
55                      'p'        => "[]",
56                      'acl'      => false,
57                      /* Pages to exclude in listing */
58                      'exclude'  => '',
59                      /* Columns to include in listing */
60                      'info'     => 'pagename,perm,owner,group,mtime,author',
61                      /* How to sort */
62                      'sortby'   => 'pagename',
63                      'limit'    => 0,
64                      );
65     }
66
67     function setaclPages(&$dbi, &$request, $pages, $acl) {
68         $ul = HTML::ul();
69         $count = 0;
70         if ($perm = new PagePermission($acl)) {
71           foreach ($pages as $name) {
72             if ( $perm->store($dbi->getPage($name)) ) {
73                 /* not yet implemented for all backends */
74                 $ul->pushContent(HTML::li(fmt("set acl for page '%s'.",$name)));
75                 $count++;
76             } else {
77                 $ul->pushContent(HTML::li(fmt("Couldn't setacl page '%s'.", $name)));
78             }
79           }
80         } else {
81             $ul->pushContent(HTML::li(fmt("Invalid acl")));
82         }
83         if ($count) {
84             $dbi->touch();
85             return HTML($ul,
86                         HTML::p(fmt("%s pages have been changed.",$count)));
87         } else {
88             return HTML($ul,
89                         HTML::p(fmt("No pages changed.")));
90         }
91     }
92     
93     function run($dbi, $argstr, &$request, $basepage) {
94         if (!DEBUG)
95             return $this->disabled("WikiAdminSetAcl not yet enabled. Set DEBUG to try it.");
96         
97         $args = $this->getArgs($argstr, $request);
98         $this->_args = $args;
99         if (!empty($args['exclude']))
100             $exclude = explodePageList($args['exclude']);
101         else
102             $exclude = false;
103
104         $p = $request->getArg('p');
105         $post_args = $request->getArg('admin_setacl');
106         $next_action = 'select';
107         $pages = array();
108         if ($p && !$request->isPost())
109             $pages = $p;
110         if ($p && $request->isPost() &&
111             !empty($post_args['acl']) && empty($post_args['cancel'])) {
112
113             // FIXME: check individual PagePermissions
114             if (!$request->_user->isAdmin()) {
115                 $request->_notAuthorized(WIKIAUTH_ADMIN);
116                 $this->disabled("! user->isAdmin");
117             }
118
119             if ($post_args['action'] == 'verify') {
120                 // Real action
121                 return $this->setaclPages($dbi, $request, array_keys($p), 
122                                           $post_args['acl']);
123             }
124             if ($post_args['action'] == 'select') {
125                 if (!empty($post_args['acl']))
126                     $next_action = 'verify';
127                 foreach ($p as $name => $c) {
128                     $pages[$name] = 1;
129                 }
130             }
131         }
132         if ($next_action == 'select' and empty($pages)) {
133             // List all pages to select from.
134             $pages = $this->collectPages($pages, $dbi, $args['sortby'], $args['limit']);
135         }
136         if ($next_action == 'verify') {
137             $args['info'] = "checkbox,pagename,perm,owner,group,mtime,author";
138         }
139         $pagelist = new PageList_Selectable($args['info'], 
140                                             $exclude,
141                                             array('types' => array(
142                                                   'perm'
143                                                   => new _PageList_Column_perm('perm', _("Permission")),
144                                                   'acl'
145                                                   => new _PageList_Column_acl('acl', _("ACL")))));
146
147         $pagelist->addPageList($pages);
148
149         $header = HTML::p();
150         if ($next_action == 'verify') {
151             $button_label = _("Yes");
152             $header = $this->setaclForm($header, $post_args, $pages);
153             $header->pushContent(
154               HTML::p(HTML::strong(
155                                    _("Are you sure you want to permanently change the selected files?"))));
156         }
157         else {
158             $button_label = _("SetAcl");
159             $header = $this->setaclForm($header, $post_args, $pages);
160             $header->pushContent(HTML::p(_("Select the pages to change:")));
161         }
162
163         $buttons = HTML::p(Button('submit:admin_setacl[acl]', $button_label, 'wikiadmin'),
164                            Button('submit:admin_setacl[cancel]', _("Cancel"), 'button'));
165
166         return HTML::form(array('action' => $request->getPostURL(),
167                                 'method' => 'post'),
168                           $header,
169                           $pagelist->getContent(),
170                           HiddenInputs($request->getArgs(),
171                                         false,
172                                         array('admin_setacl')),
173                           HiddenInputs(array('admin_setacl[action]' => $next_action,
174                                              'require_authority_for_post' => WIKIAUTH_ADMIN)),
175                           $buttons);
176     }
177
178     function setaclForm(&$header, $post_args, $pagehash) {
179         $acl = $post_args['acl'];
180         $header->pushContent(HTML::p(HTML::em(_("This plugin is currently under development and does not work!"))));
181         //todo: find intersection of all page perms
182         $pages = array();
183         foreach ($pagehash as $name => $checked) {
184            if ($checked) $pages[] = $name;
185         }
186         $perm_tree = pagePermissions($name);
187         $table = pagePermissionsAclFormat($perm_tree,true);
188         $header->pushContent(HTML::p(fmt("Pages: %s",join(', ',$pages))));
189         if (DEBUG) {
190             $header->pushContent(HTML::pre("Permission tree for $name:\n",print_r($perm_tree,true)));
191         }
192         $type = $perm_tree[0];
193         if ($type == 'inherited')
194             $type = sprintf(_("page permission inherited from %s"),$perm_tree[1][0]);
195         elseif ($type == 'page')
196             $type = _("invidual page permission");
197         elseif ($type == 'default')
198             $type = _("default page permission");
199         $header->pushContent(HTML::p(_("Type: "),$type));
200         $header->pushContent(HTML::blockquote($table));
201         //todo:
202         // display array of checkboxes for existing perms
203         // and a dropdown for user/group to add perms.
204         // disabled if inherited, 
205         // checkbox to disable inheritance, 
206         // another checkbox to progate new permissions to all childs (if there exist some)
207         // warn if more pages are selected and they have different perms
208         //$header->pushContent(HTML::input(array('name' => 'admin_setacl[acl]',
209         //                                       'value' => $post_args['acl'])));
210         $header->pushContent(HTML::br());
211         $checkbox = HTML::input(array('type' => 'checkbox',
212                                       'name' => 'admin_setacl[updatechildren]',
213                                       'value' => 1));
214         if (!empty($post_args['updatechildren']))  $checkbox->setAttr('checked','checked');
215         $header->pushContent($checkbox,
216                 _("Propagate new permissions to all subpages?"),
217                 HTML::raw("&nbsp;&nbsp;"),
218                 HTML::em(_("(disable individual page permissions, enable inheritance)?")));
219         $header->pushContent(HTML::hr(),HTML::p());
220         return $header;
221     }
222 }
223
224 class _PageList_Column_acl extends _PageList_Column {
225     function _getValue ($page_handle, &$revision_handle) {
226         $perm_tree = pagePermissions($page_handle->_pagename);
227         return pagePermissionsAclFormat($perm_tree);
228         if (0) {
229             ob_start();
230             var_dump($perm_array);
231             $xml = ob_get_contents();
232             ob_end_clean();
233             return $xml;
234         }
235     }
236 };
237
238 class _PageList_Column_perm extends _PageList_Column {
239     function _getValue ($page_handle, &$revision_handle) {
240         $perm_array = pagePermissions($page_handle->_pagename);
241         return pagePermissionsSimpleFormat($perm_array,
242                                            $page_handle->get('author'),
243                                            $page_handle->get('group'));
244         if (0) {
245             ob_start();
246             var_dump($perm_array);
247             $xml = ob_get_contents();
248             ob_end_clean();
249             return $xml;
250         }
251     }
252 };
253
254 // $Log: not supported by cvs2svn $
255 // Revision 1.4  2004/03/17 20:23:44  rurban
256 // fixed p[] pagehash passing from WikiAdminSelect, fixed problem removing pages with [] in the pagename
257 //
258 // Revision 1.3  2004/03/12 13:31:43  rurban
259 // enforce PagePermissions, errormsg if not Admin
260 //
261 // Revision 1.2  2004/02/24 04:02:07  rurban
262 // Better warning messages
263 //
264 // Revision 1.1  2004/02/23 21:30:25  rurban
265 // more PagePerm stuff: (working against 1.4.0)
266 //   ACL editing and simplification of ACL's to simple rwx------ string
267 //   not yet working.
268 //
269 //
270
271 // Local Variables:
272 // mode: php
273 // tab-width: 8
274 // c-basic-offset: 4
275 // c-hanging-comment-ender-p: nil
276 // indent-tabs-mode: nil
277 // End:
278 ?>