]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/plugin/WikiAdminChmod.php
Normalize header
[SourceForge/phpwiki.git] / lib / plugin / WikiAdminChmod.php
1 <?php // -*-php-*-
2 rcs_id('$Id$');
3 /*
4  * Copyright 2004 $ThePhpWikiProgrammingTeam
5  * Copyright 2008 Marc-Etienne Vargenau, Alcatel-Lucent
6  *
7  * This file is part of PhpWiki.
8  *
9  * PhpWiki is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * PhpWiki is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with PhpWiki; if not, write to the Free Software
21  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
22  */
23
24 /**
25  * Set individual PagePermissions, simplifying effective ACLs to 
26  * unix-like rwxr--r--+ permissions. (as in cygwin)
27  *
28  * Usage:   <?plugin WikiAdminChmod ?> or called via WikiAdminSelect
29  * Author:  Reini Urban <rurban@x-ray.at>
30  *
31  * KNOWN ISSUES:
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_WikiAdminChmod
38 extends WikiPlugin_WikiAdminSelect
39 {
40     function getName() {
41         return _("WikiAdminChmod");
42     }
43
44     function getDescription() {
45         return _("Set individual page permissions.");
46     }
47
48     function getVersion() {
49         return preg_replace("/[Revision: $]/", '',
50                             "\$Revision$");
51     }
52
53     function getDefaultArguments() {
54         return array_merge
55             (
56              PageList::supportedArgs(),
57              array(
58                    's'          => false,
59                    'perm'       => false,
60                    /* Columns to include in listing */
61                    'info'     => 'pagename,perm,mtime,author',
62                    ));
63     }
64
65     // todo: change permstring to some kind of default ACL hash. 
66     // See PagePermission class
67     function chmodHelper($permstring) {
68         $perm = array();
69         return $perm;
70     }
71
72     function chmodPages(&$dbi, &$request, $pages, $permstring) {
73         $ul = HTML::ul();
74         $count = 0;
75         $acl = chmodHelper($permstring);
76         if ($perm = new PagePermission($acl)) {
77             foreach ($pages as $name) {
78                 if ( $perm->store($dbi->getPage($name)) ) {
79                     $ul->pushContent(HTML::li(fmt("chmod page '%s' to '%s'.",$name, $permstring)));
80                     $count++;
81                 } else {
82                     $ul->pushContent(HTML::li(fmt("Couldn't chmod page '%s' to '%s'.", $name, $permstring)));
83                 }
84             }
85         } else {
86             $ul->pushContent(HTML::li(fmt("Invalid chmod string")));
87         }
88         if ($count) {
89             $dbi->touch();
90             return HTML($ul,
91                         HTML::p(fmt("%s pages have been changed.",$count)));
92         } else {
93             return HTML($ul,
94                         HTML::p(fmt("No pages changed.")));
95         }
96     }
97     
98     function run($dbi, $argstr, &$request, $basepage) {
99         if (!DEBUG)
100             return $this->disabled("WikiAdminChmod not yet enabled. Set DEBUG to try it.");
101         
102         $args = $this->getArgs($argstr, $request);
103         $this->_args = $args;
104         $this->preSelectS($args, $request);
105
106         $p = $request->getArg('p');
107         if (!$p) $p = $this->_list;
108         $post_args = $request->getArg('admin_chmod');
109         $next_action = 'select';
110         $pages = array();
111         if ($p && !$request->isPost())
112             $pages = $p;
113         if ($p && $request->isPost() &&
114             !empty($post_args['chmod']) && empty($post_args['cancel'])) {
115             // without individual PagePermissions:
116             if (!ENABLE_PAGEPERM and !$request->_user->isAdmin()) {
117                 $request->_notAuthorized(WIKIAUTH_ADMIN);
118                 $this->disabled("! user->isAdmin");
119             }
120
121             if ($post_args['action'] == 'verify') {
122                 // Real action
123                 return $this->chmodPages($dbi, $request, array_keys($p), 
124                                           $post_args['perm']);
125             }
126             if ($post_args['action'] == 'select') {
127                 if (!empty($post_args['perm']))
128                     $next_action = 'verify';
129                 foreach ($p as $name => $c) {
130                     $pages[$name] = 1;
131                 }
132             }
133         }
134         if ($next_action == 'select' and empty($pages)) {
135             // List all pages to select from.
136             $pages = $this->collectPages($pages, $dbi, $args['sortby'], $args['limit'], $args['exclude']);
137         }
138         if ($next_action == 'verify') {
139             $args['info'] = "checkbox,pagename,perm,author,mtime";
140         }
141         $args['types'] = array('perm' => new _PageList_Column_chmod_perm('perm', _("Permission")));
142         $pagelist = new PageList_Selectable($args['info'], $args['exclude'], $args);
143         $pagelist->addPageList($pages);
144
145         $header = HTML::p();
146         if ($next_action == 'verify') {
147             $button_label = _("Yes");
148             $header = $this->chmodForm($header, $post_args);
149             $header->pushContent(
150               HTML::p(HTML::strong(
151                 _("Are you sure you want to permanently change the selected files?"))));
152         }
153         else {
154             $button_label = _("Chmod");
155             $header = $this->chmodForm($header, $post_args);
156             $header->pushContent(HTML::p(_("Select the pages to change:")));
157         }
158
159         $buttons = HTML::p(Button('submit:admin_chmod[chmod]', $button_label, 'wikiadmin'),
160                            Button('submit:admin_chmod[cancel]', _("Cancel"), 'button'));
161
162         return HTML::form(array('action' => $request->getPostURL(),
163                                 'method' => 'post'),
164                           $header,
165                           $buttons,
166                           $pagelist->getContent(),
167                           HiddenInputs($request->getArgs(),
168                                         false,
169                                         array('admin_chmod')),
170                           HiddenInputs(array('admin_chmod[action]' => $next_action)),
171                           ENABLE_PAGEPERM
172                           ? ''
173                           : HiddenInputs(array('require_authority_for_post' => WIKIAUTH_ADMIN)));
174     }
175
176     function chmodForm(&$header, $post_args) {
177         $header->pushContent(
178             HTML::p(HTML::em(
179                _("This plugin is currently under development and does not work!"))));
180         $header->pushContent(_("Chmod to permission:"));
181         $header->pushContent(HTML::input(array('name' => 'admin_chmod[perm]',
182                                                'value' => $post_args['perm'])));
183         $header->pushContent(' '._("(ugo : rwx)"));
184         $header->pushContent(HTML::p());
185         $checkbox = HTML::input(array('type' => 'checkbox',
186                                       'name' => 'admin_chmod[updatechildren]',
187                                       'value' => 1));
188         if (!empty($post_args['updatechildren']))  $checkbox->setAttr('checked','checked');
189         $header->pushContent($checkbox, HTML::raw("&nbsp;"),
190                 _("Propagate new permissions to all subpages?"),
191                 HTML::raw("&nbsp;&nbsp;"),
192                 HTML::em(_("(disable individual page permissions, enable inheritance)?")));
193         $header->pushContent(HTML::hr(),HTML::p());
194         return $header;
195     }
196 }
197
198 // conflicts with WikiAdminSetAcl
199 class _PageList_Column_chmod_perm extends _PageList_Column {
200     function _getValue ($page_handle, &$revision_handle) {
201         $perm_array = pagePermissions($page_handle->_pagename);
202         return pagePermissionsSimpleFormat($perm_array,
203                                            $page_handle->get('author'),
204                                            $page_handle->get('group'));
205     }
206 };
207
208 // Local Variables:
209 // mode: php
210 // tab-width: 8
211 // c-basic-offset: 4
212 // c-hanging-comment-ender-p: nil
213 // indent-tabs-mode: nil
214 // End:
215 ?>