]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/plugin/WikiAdminChmod.php
rcs_id no longer makes sense with Subversion global version number
[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 getDefaultArguments() {
49         return array_merge
50             (
51              PageList::supportedArgs(),
52              array(
53                    's'                 => false,
54                    'perm'         => false,
55                    /* Columns to include in listing */
56                    'info'     => 'pagename,perm,mtime,author',
57                    ));
58     }
59
60     // todo: change permstring to some kind of default ACL hash.
61     // See PagePermission class
62     function chmodHelper($permstring) {
63         $perm = array();
64         return $perm;
65     }
66
67     function chmodPages(&$dbi, &$request, $pages, $permstring) {
68         $ul = HTML::ul();
69         $count = 0;
70         $acl = chmodHelper($permstring);
71         if ($perm = new PagePermission($acl)) {
72             foreach ($pages as $name) {
73                 if ( $perm->store($dbi->getPage($name)) ) {
74                     $ul->pushContent(HTML::li(fmt("chmod page '%s' to '%s'.",$name, $permstring)));
75                     $count++;
76                 } else {
77                     $ul->pushContent(HTML::li(fmt("Couldn't chmod page '%s' to '%s'.", $name, $permstring)));
78                 }
79             }
80         } else {
81             $ul->pushContent(HTML::li(fmt("Invalid chmod string")));
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("WikiAdminChmod not yet enabled. Set DEBUG to try it.");
96
97         $args = $this->getArgs($argstr, $request);
98         $this->_args = $args;
99         $this->preSelectS($args, $request);
100
101         $p = $request->getArg('p');
102         if (!$p) $p = $this->_list;
103         $post_args = $request->getArg('admin_chmod');
104         $next_action = 'select';
105         $pages = array();
106         if ($p && !$request->isPost())
107             $pages = $p;
108         if ($p && $request->isPost() &&
109             !empty($post_args['chmod']) && empty($post_args['cancel'])) {
110             // without individual PagePermissions:
111             if (!ENABLE_PAGEPERM and !$request->_user->isAdmin()) {
112                 $request->_notAuthorized(WIKIAUTH_ADMIN);
113                 $this->disabled("! user->isAdmin");
114             }
115
116             if ($post_args['action'] == 'verify') {
117                 // Real action
118                 return $this->chmodPages($dbi, $request, array_keys($p),
119                                           $post_args['perm']);
120             }
121             if ($post_args['action'] == 'select') {
122                 if (!empty($post_args['perm']))
123                     $next_action = 'verify';
124                 foreach ($p as $name => $c) {
125                     $pages[$name] = 1;
126                 }
127             }
128         }
129         if ($next_action == 'select' and empty($pages)) {
130             // List all pages to select from.
131             $pages = $this->collectPages($pages, $dbi, $args['sortby'], $args['limit'], $args['exclude']);
132         }
133         if ($next_action == 'verify') {
134             $args['info'] = "checkbox,pagename,perm,author,mtime";
135         }
136         $args['types'] = array('perm' => new _PageList_Column_chmod_perm('perm', _("Permission")));
137         $pagelist = new PageList_Selectable($args['info'], $args['exclude'], $args);
138         $pagelist->addPageList($pages);
139
140         $header = HTML::p();
141         if ($next_action == 'verify') {
142             $button_label = _("Yes");
143             $header = $this->chmodForm($header, $post_args);
144             $header->pushContent(
145               HTML::p(HTML::strong(
146                 _("Are you sure you want to permanently change the selected files?"))));
147         }
148         else {
149             $button_label = _("Chmod");
150             $header = $this->chmodForm($header, $post_args);
151             $header->pushContent(HTML::p(_("Select the pages to change:")));
152         }
153
154         $buttons = HTML::p(Button('submit:admin_chmod[chmod]', $button_label, 'wikiadmin'),
155                            Button('submit:admin_chmod[cancel]', _("Cancel"), 'button'));
156
157         return HTML::form(array('action' => $request->getPostURL(),
158                                 'method' => 'post'),
159                           $header,
160                           $buttons,
161                           $pagelist->getContent(),
162                           HiddenInputs($request->getArgs(),
163                                         false,
164                                         array('admin_chmod')),
165                           HiddenInputs(array('admin_chmod[action]' => $next_action)),
166                           ENABLE_PAGEPERM
167                           ? ''
168                           : HiddenInputs(array('require_authority_for_post' => WIKIAUTH_ADMIN)));
169     }
170
171     function chmodForm(&$header, $post_args) {
172         $header->pushContent(
173             HTML::p(HTML::em(
174                _("This plugin is currently under development and does not work!"))));
175         $header->pushContent(_("Chmod to permission:"));
176         $header->pushContent(HTML::input(array('name' => 'admin_chmod[perm]',
177                                                'value' => $post_args['perm'])));
178         $header->pushContent(' '._("(ugo : rwx)"));
179         $header->pushContent(HTML::p());
180         $checkbox = HTML::input(array('type' => 'checkbox',
181                                       'name' => 'admin_chmod[updatechildren]',
182                                       'value' => 1));
183         if (!empty($post_args['updatechildren']))  $checkbox->setAttr('checked','checked');
184         $header->pushContent($checkbox, HTML::raw("&nbsp;"),
185                 _("Propagate new permissions to all subpages?"),
186                 HTML::raw("&nbsp;&nbsp;"),
187                 HTML::em(_("(disable individual page permissions, enable inheritance)?")));
188         $header->pushContent(HTML::hr(),HTML::p());
189         return $header;
190     }
191 }
192
193 // conflicts with WikiAdminSetAcl
194 class _PageList_Column_chmod_perm extends _PageList_Column {
195     function _getValue ($page_handle, &$revision_handle) {
196         $perm_array = pagePermissions($page_handle->_pagename);
197         return pagePermissionsSimpleFormat($perm_array,
198                                            $page_handle->get('author'),
199                                            $page_handle->get('group'));
200     }
201 };
202
203 // Local Variables:
204 // mode: php
205 // tab-width: 8
206 // c-basic-offset: 4
207 // c-hanging-comment-ender-p: nil
208 // indent-tabs-mode: nil
209 // End:
210 ?>