]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/plugin/WikiAdminSelect.php
Test 'limit' argument is numeric to avoid SQL injection
[SourceForge/phpwiki.git] / lib / plugin / WikiAdminSelect.php
1 <?php // -*-php-*-
2 rcs_id('$Id$');
3 /*
4  Copyright 2002 $ThePhpWikiProgrammingTeam
5  Copyright 2008-2009 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  * Allows selection of multiple pages which get passed to other
26  * WikiAdmin plugins then. Then do Rename, Remove, Chmod, Chown, ...
27  *
28  * Usage:   <?plugin WikiAdminSelect?>
29  * Author:  Reini Urban <rurban@x-ray.at>
30  *
31  * This is the base class for most WikiAdmin* classes, using
32  * collectPages() and preSelectS().
33  * "list" PagePermissions supported implicitly by PageList.
34  */
35 require_once('lib/PageList.php');
36
37 class WikiPlugin_WikiAdminSelect
38 extends WikiPlugin
39 {
40     function getName() {
41         return _("WikiAdminSelect");
42     }
43
44     function getDescription() {
45         return _("Allows selection of multiple pages which get passed to other WikiAdmin plugins.");
46     }
47
48     function getVersion() {
49         return preg_replace("/[Revision: $]/", '',
50                             "\$Revision$");
51     }
52
53     function getDefaultArguments() {
54         return array('s'       => '', // preselect pages
55                      /* select pages by meta-data: */
56                      'author'   => false,
57                      'owner'    => false,
58                      'creator'  => false,
59                      'only'    => '',
60                      'exclude' => '',
61                      'info'    => 'most',
62                      'sortby'  => 'pagename',
63                      'limit'   => 0,
64                      'paging'  => 'none'
65                     );
66     }
67
68     /**
69      * Default collector for all WikiAdmin* plugins.
70      * preSelectS() is similar, but fills $this->_list
71      */
72     function collectPages(&$list, &$dbi, $sortby, $limit=0, $exclude='') {
73         $allPages = $dbi->getAllPages(0, $sortby, $limit, $exclude);
74         while ($pagehandle = $allPages->next()) {
75             $pagename = $pagehandle->getName();
76             if (empty($list[$pagename]))
77                 $list[$pagename] = 0;
78         }
79         return $list;
80     }
81
82     /**
83      * Preselect a list of pagenames by the supporting the following args:
84      * 's': comma-seperated list of pagename wildcards
85      * 'author', 'owner', 'creator': from WikiDB_Page
86      * 'only: forgot what the difference to 's' was.
87      * Sets $this->_list, which is picked up by collectPages() and is a default for p[]
88      */
89     function preSelectS (&$args, &$request) {
90         // override plugin argument by GET: probably not needed if s||="" is used
91         // anyway, we force it for unique interface.
92         if (!empty($request->getArg['s']))
93             $args['s'] = $request->getArg['s'];
94         if ( !empty($args['owner']) )
95             $sl = PageList::allPagesByOwner($args['owner'],false,$args['sortby'],$args['limit'],$args['exclude']);
96         elseif ( !empty($args['author']) )
97             $sl = PageList::allPagesByAuthor($args['author'],false,$args['sortby'],$args['limit'],$args['exclude']);
98         elseif ( !empty($args['creator']) )
99             $sl = PageList::allPagesByCreator($args['creator'],false,$args['sortby'],$args['limit'],$args['exclude']);
100         elseif ( !empty($args['s']) or !empty($args['only']) ) {
101             // all pages by name
102             $sl = explodePageList(empty($args['only']) ? $args['s'] : $args['only']);
103         }
104         $this->_list = array();
105         if (!empty($sl)) {
106             $request->setArg('verify', 1);
107             foreach ($sl as $name) {
108                 if (!empty($args['exclude'])) {
109                     if (!in_array($name, $args['exclude']))
110                         $this->_list[$name] = 1;
111                 } else {
112                     $this->_list[$name] = 1;
113                 }
114             }
115         }
116         return $this->_list;
117     }
118
119     function run($dbi, $argstr, &$request, $basepage) {
120         //if ($request->getArg('action') != 'browse')
121         //    return $this->disabled("(action != 'browse')");
122         $args = $this->getArgs($argstr, $request);
123
124         if (!empty($args['limit']) && !is_numeric($args['limit'])) {
125             return $this->error(_("Illegal 'limit' argument: must be numeric"));
126         }
127
128         $this->_args = $args;
129         extract($args);
130         $this->preSelectS($args, $request);
131
132         $info = $args['info'];
133
134         // array_multisort($this->_list, SORT_NUMERIC, SORT_DESC);
135         $pagename = $request->getArg('pagename');
136         // GetUrlToSelf() with all given params
137         //$uri = $GLOBALS['HTTP_SERVER_VARS']['REQUEST_URI']; // without s would be better.
138         //$uri = $request->getURLtoSelf();//false, array('verify'));
139         $form = HTML::form(array('action' => $request->getPostURL(), 'method' => 'post'));
140         if ($request->getArg('WikiAdminSelect') == _("Go"))
141             $p = false;
142         else
143             $p = $request->getArg('p');
144         //$p = @$GLOBALS['HTTP_POST_VARS']['p'];
145         $form->pushContent(HTML::p(array('class' => 'wikitext'), _("Select: "),
146                                    HTML::input(array('type' => 'text',
147                                                      'name' => 's',
148                                                      'value' => $args['s'])),
149                                    HTML::input(array('type' => 'submit',
150                                                      'name' => 'WikiAdminSelect',
151                                                      'value' => _("Go")))));
152         if (! $request->getArg('verify')) {
153             $form->pushContent(HTML::input(array('type' => 'hidden',
154                                                  'name' => 'action',
155                                                  'value' => 'verify')));
156             $form->pushContent(Button('submit:verify', _("Select pages"),
157                                       'wikiadmin'),
158                                Button('submit:cancel', _("Cancel"), 'button'));
159         } else {
160             global $WikiTheme;
161             $form->pushContent(HTML::input(array('type' => 'hidden',
162                                                  'name' => 'action',
163                                                  'value' => 'WikiAdminSelect'))
164                                );
165             // Add the Buttons for all registered WikiAdmin plugins
166             $plugin_dir = 'lib/plugin';
167             if (defined('PHPWIKI_DIR'))
168                 $plugin_dir = PHPWIKI_DIR . "/$plugin_dir";
169             $fs = new fileSet($plugin_dir, 'WikiAdmin*.php');
170             $actions = $fs->getFiles();
171             sort($actions);
172             foreach ($actions as $f) {
173                 $f = preg_replace('/.php$/','', $f);
174                 $s = preg_replace('/^WikiAdmin/','', $f);
175                 if (!in_array($s,array("Select","Utils"))) { // disable Select and Utils
176                     $form->pushContent(Button("submit:wikiadmin[$f]", _($s), "wikiadmin"));
177                     $form->pushContent($WikiTheme->getButtonSeparator());
178                 }
179             }
180             // $form->pushContent(Button('submit:cancel', _("Cancel"), 'button'));
181         }
182
183         if ($request->isPost() 
184             && ! $request->getArg('wikiadmin')
185             && !empty($p)) {
186             $this->_list = array();
187             // List all selected pages again.
188             foreach ($p as $page => $name) {
189                 $this->_list[$name] = 1;
190             }
191         }
192         elseif ($request->isPost()
193                 and $request->_user->isAdmin()
194                 and !empty($p)
195                 //and $request->getArg('verify')
196                 and ($request->getArg('action') == 'WikiAdminSelect')
197                 and $request->getArg('wikiadmin')
198                )
199         {
200             // handle external plugin
201             $loader = new WikiPluginLoader();
202             $a = array_keys($request->getArg('wikiadmin'));
203             $plugin_action = $a[0];
204             $single_arg_plugins = array("Remove");
205             if (in_array($plugin_action, $single_arg_plugins)) {
206                 $plugin = $loader->getPlugin($plugin_action);
207                 $ul = HTML::ul();
208                 foreach ($p as $page => $name) {
209                     $plugin_args = "run_page=$name";
210                     $request->setArg($plugin_action, 1);
211                     $request->setArg('p', array($page => $name));
212                     // if the plugin requires more args than the pagename,
213                     // then this plugin will not return. (Rename, SearchReplace, ...)
214                     $action_result = $plugin->run($dbi, $plugin_args, $request, $basepage);
215                     $ul->pushContent(HTML::li(fmt("Selected page '%s' passed to '%s'.",
216                                                   $name, $select)));
217                     $ul->pushContent(HTML::ul(HTML::li($action_result)));
218                 }
219             } else {
220                 // redirect to the plugin page.
221                 // in which page is this plugin?
222                 $plugin_action = preg_replace("/^WikiAdmin/","",$plugin_action);
223                 $args = array();
224                 foreach ($p as $page => $x) {
225                   $args["p[$page]"] = 1;
226                 }
227                 header("Location: ".
228                   WikiURL(_("PhpWikiAdministration")."/"._($plugin_action),$args,1));
229                 exit();
230             }
231         } elseif (empty($args['s'])) {
232             // List all pages to select from.
233             $this->_list = $this->collectPages($this->_list, $dbi, $args['sortby'], $args['limit']);
234         }
235         $pagelist = new PageList_Selectable($info, $args['exclude'], $args);
236         $pagelist->addPageList($this->_list);
237         $form->pushContent($pagelist->getContent());
238         foreach ($args as $k => $v) {
239             if (!in_array($k,array('s','WikiAdminSelect','action','verify')))
240                 $form->pushContent(HiddenInputs(array($k => $v))); // plugin params
241         }
242         if (! $request->getArg('select')) {
243             return $form;
244         } else {
245             ; //return $action_result;
246         }
247     }
248
249     function _tablePush(&$table, $first, $second) {
250         $table->pushContent(
251                             HTML::tr(
252                                      HTML::td($first),
253                                      HTML::td($second)));
254     }
255
256 }
257
258 // Local Variables:
259 // mode: php
260 // tab-width: 8
261 // c-basic-offset: 4
262 // c-hanging-comment-ender-p: nil
263 // indent-tabs-mode: nil
264 // End:
265 ?>