]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/plugin/RandomPage.php
Test 'limit' argument is numeric to avoid SQL injection
[SourceForge/phpwiki.git] / lib / plugin / RandomPage.php
1 <?php // -*-php-*-
2 rcs_id('$Id$');
3 /**
4  Copyright 1999,2000,2001,2002,2005 $ThePhpWikiProgrammingTeam
5
6  This file is part of PhpWiki.
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 require_once('lib/PageList.php');
24
25 /**
26  * With 1.3.11 the "pages" argument was renamed to "numpages".
27  * action=upgrade should deal with pages containing RandomPage modified earlier than 2005-01-24
28  */
29 class WikiPlugin_RandomPage
30 extends WikiPlugin
31 {
32     function getName () {
33         return _("RandomPage");
34     }
35
36     function getDescription () {
37         return _("Displays a list of randomly chosen pages or redirects to a random page.");
38     }
39
40     function getVersion() {
41         return preg_replace("/[Revision: $]/", '',
42                             "\$Revision$");
43     }
44
45     function getDefaultArguments() {
46         return array_merge
47             (
48              PageList::supportedArgs(),
49              array('numpages'     => 20,     // was pages
50                    'pages'        => false, // deprecated
51                    'redirect'     => false,
52                    'hidename'     => false, // only for numpages=1
53                    'exclude'      => $this->default_exclude(),
54                    'info'         => ''));
55     }
56
57     function run($dbi, $argstr, &$request, $basepage) {
58         $args = $this->getArgs($argstr, $request);
59
60         if (!empty($args['limit']) && !is_numeric($args['limit'])) {
61             return $this->error(_("Illegal 'limit' argument: must be numeric"));
62         }
63
64         extract($args);
65
66         // fix deprecated arg
67         if (is_integer($pages)) {
68             $numpages = $pages;
69             $pages = false;
70         // fix new pages handling in arg preprozessor.    
71         } elseif (is_array($pages)) {
72             $numpages = (int)$pages[0];
73             if ($numpages > 0 and !$dbi->isWikiPage($numpages)) $pages = false;
74             else $numpages = 1;
75         }
76
77         $allpages = $dbi->getAllPages(false, $sortby, $limit, $exclude);
78         $pagearray = $allpages->asArray();
79         better_srand(); // Start with a good seed.
80
81         if (($numpages == 1) && $pagearray) {
82             $page = $pagearray[array_rand($pagearray)];
83             $pagename = $page->getName();
84             if ($redirect)
85                 $request->redirect(WikiURL($pagename, false, 'absurl')); // noreturn
86             if ($hidename)
87                 return WikiLink($pagename, false, _("RandomPage"));
88             else
89                 return WikiLink($pagename);
90         }
91
92         $numpages = min( max(1, (int) $numpages), 20, count($pagearray));
93         $pagelist = new PageList($info, $exclude, $args);
94         $shuffle = array_rand($pagearray, $numpages);
95         if (is_array($shuffle)) {
96             foreach ($shuffle as $i)
97                 if (isset($pagearray[$i])) $pagelist->addPage($pagearray[$i]);
98         } else { // if $numpages = 1
99              if (isset($pagearray[$shuffle]))
100                  $pagelist->addPage($pagearray[$shuffle]);
101         }
102         return $pagelist;
103     }
104
105     function default_exclude() {
106         // Some useful default pages to exclude.
107         $default_exclude = 'RandomPage,HomePage,AllPages,RecentChanges,RecentEdits,FullRecentChanges';
108         foreach (explode(",", $default_exclude) as $e) {
109             $exclude[] = gettext($e);
110         }
111         return implode(",", $exclude);
112     }
113 };
114
115 // Local Variables:
116 // mode: php
117 // tab-width: 8
118 // c-basic-offset: 4
119 // c-hanging-comment-ender-p: nil
120 // indent-tabs-mode: nil
121 // End:
122 ?>