]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/plugin/WikiAdminChmod.php
Upper case
[SourceForge/phpwiki.git] / lib / plugin / WikiAdminChmod.php
1 <?php
2
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 along
20  * with PhpWiki; if not, write to the Free Software Foundation, Inc.,
21  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22  */
23
24 /**
25  * Set individual PagePermissions, simplifying effective ACLs to
26  * unix-like rwxr--r--+ permissions. (as in cygwin)
27  *
28  * Usage:   <<WikiAdminChmod >> or called via WikiAdminSelect
29  * Author:  Reini Urban <rurban@x-ray.at>
30  *
31  */
32 require_once 'lib/PageList.php';
33 require_once 'lib/plugin/WikiAdminSelect.php';
34
35 class WikiPlugin_WikiAdminChmod
36     extends WikiPlugin_WikiAdminSelect
37 {
38     function getDescription()
39     {
40         return _("Set individual page permissions.");
41     }
42
43     function getDefaultArguments()
44     {
45         return array_merge
46         (
47             PageList::supportedArgs(),
48             array(
49                 's' => false,
50                 'perm' => false,
51                 /* Columns to include in listing */
52                 'info' => 'pagename,perm,mtime,author',
53             ));
54     }
55
56     // todo: change permstring to some kind of default ACL hash.
57     // See PagePermission class
58     function chmodHelper($permstring)
59     {
60         $perm = array();
61         return $perm;
62     }
63
64     function chmodPages(&$dbi, &$request, $pages, $permstring)
65     {
66         $ul = HTML::ul();
67         $count = 0;
68         $acl = chmodHelper($permstring);
69         if ($perm = new PagePermission($acl)) {
70             foreach ($pages as $name) {
71                 if ($perm->store($dbi->getPage($name))) {
72                     $ul->pushContent(HTML::li(fmt("chmod page “%s” to “%s”.", $name, $permstring)));
73                     $count++;
74                 } else {
75                     $ul->pushContent(HTML::li(fmt("Couldn't chmod page “%s” to “%s”.", $name, $permstring)));
76                 }
77             }
78         } else {
79             $ul->pushContent(HTML::li(_("Invalid chmod string")));
80         }
81         if ($count) {
82             $dbi->touch();
83             return HTML($ul,
84                 HTML::p(fmt("%d pages have been changed.", $count)));
85         } else {
86             return HTML($ul,
87                 HTML::p(_("No pages changed.")));
88         }
89     }
90
91     function run($dbi, $argstr, &$request, $basepage)
92     {
93         if (!DEBUG) {
94             return $this->disabled("WikiAdminChmod not yet enabled. Set DEBUG to try it.");
95         }
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         ) {
111             // without individual PagePermissions:
112             if (!ENABLE_PAGEPERM and !$request->_user->isAdmin()) {
113                 $request->_notAuthorized(WIKIAUTH_ADMIN);
114                 $this->disabled("! user->isAdmin");
115             }
116
117             if ($post_args['action'] == 'verify') {
118                 // Real action
119                 return $this->chmodPages($dbi, $request, array_keys($p),
120                     $post_args['perm']);
121             }
122             if ($post_args['action'] == 'select') {
123                 if (!empty($post_args['perm']))
124                     $next_action = 'verify';
125                 foreach ($p as $name => $c) {
126                     $pages[$name] = 1;
127                 }
128             }
129         }
130         if ($next_action == 'select' and empty($pages)) {
131             // List all pages to select from.
132             $pages = $this->collectPages($pages, $dbi, $args['sortby'], $args['limit'], $args['exclude']);
133         }
134         if ($next_action == 'verify') {
135             $args['info'] = "checkbox,pagename,perm,author,mtime";
136         }
137         $args['types'] = array('perm' => new _PageList_Column_chmod_perm('perm', _("Permission")));
138         $pagelist = new PageList_Selectable($args['info'], $args['exclude'], $args);
139         $pagelist->addPageList($pages);
140
141         $header = HTML::p();
142         if ($next_action == 'verify') {
143             $button_label = _("Yes");
144             $header = $this->chmodForm($header, $post_args);
145             $header->pushContent(
146                 HTML::p(HTML::strong(
147                     _("Are you sure you want to permanently change the selected files?"))));
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     {
173         $header->pushContent(
174             HTML::p(HTML::em(
175                 _("This plugin is currently under development and does not work!"))));
176         $header->pushContent(_("Chmod to permission:"));
177         $header->pushContent(HTML::input(array('name' => 'admin_chmod[perm]',
178             'value' => $post_args['perm'])));
179         $header->pushContent(' ' . _("(ugo : rwx)"));
180         $header->pushContent(HTML::p());
181         $checkbox = HTML::input(array('type' => 'checkbox',
182             'name' => 'admin_chmod[updatechildren]',
183             'value' => 1));
184         if (!empty($post_args['updatechildren'])) $checkbox->setAttr('checked', 'checked');
185         $header->pushContent($checkbox, HTML::raw("&nbsp;"),
186             _("Propagate new permissions to all subpages?"),
187             HTML::raw("&nbsp;&nbsp;"),
188             HTML::em(_("(disable individual page permissions, enable inheritance)?")));
189         $header->pushContent(HTML::hr(), HTML::p());
190         return $header;
191     }
192 }
193
194 // Local Variables:
195 // mode: php
196 // tab-width: 8
197 // c-basic-offset: 4
198 // c-hanging-comment-ender-p: nil
199 // indent-tabs-mode: nil
200 // End: