]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/plugin/IncludePage.php
Critical security fix: it was possible to see the content of pages protected by ACLs...
[SourceForge/phpwiki.git] / lib / plugin / IncludePage.php
1 <?php // -*-php-*-
2 rcs_id('$Id$');
3 /*
4  Copyright 1999, 2000, 2001, 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  * IncludePage:  include text from another wiki page in this one
26  * usage:   <?plugin IncludePage page=OtherPage rev=6 quiet=1 words=50 lines=6?>
27  * author:  Joe Edelman <joe@orbis-tertius.net>
28  */
29
30 class WikiPlugin_IncludePage
31 extends WikiPlugin
32 {
33     function getName() {
34         return _("IncludePage");
35     }
36
37     function getDescription() {
38         return _("Include text from another wiki page.");
39     }
40
41     function getVersion() {
42         return preg_replace("/[Revision: $]/", '',
43                             "\$Revision$");
44     }
45
46     function getDefaultArguments() {
47         return array( 'page'    => false, // the page to include
48                       'rev'     => false, // the revision (defaults to most recent)
49                       'quiet'   => false, // if set, inclusion appears as normal content
50                       'bytes'   => false, // maximum number of bytes to include
51                       'words'   => false, // maximum number of words to include
52                       'lines'   => false, // maximum number of lines to include
53                       'sections' => false, // maximum number of sections to include
54                       'section' => false, // include a named section
55                       'sectionhead' => false // when including a named section show the heading
56                       );
57     }
58
59     function getWikiPageLinks($argstr, $basepage) {
60         extract($this->getArgs($argstr));
61
62         if ($page) {
63             // Expand relative page names.
64             $page = new WikiPageName($page, $basepage);
65         }
66         if (!$page or !$page->name)
67             return false;
68         return array(array('linkto' => $page->name, 'relation' => 0));
69     }
70                 
71     function run($dbi, $argstr, &$request, $basepage) {
72         $args = $this->getArgs($argstr, $request);
73         extract($args);
74         if ($page) {
75             // Expand relative page names.
76             $page = new WikiPageName($page, $basepage);
77             $page = $page->name;
78         }
79         if (!$page) {
80             return $this->error(_("no page specified"));
81         }
82
83         // A page can include itself once (this is needed, e.g.,  when editing
84         // TextFormattingRules).
85         static $included_pages = array();
86         if (in_array($page, $included_pages)) {
87             return $this->error(sprintf(_("recursive inclusion of page %s ignored"),
88                                         $page));
89         }
90
91         // Check if user is allowed to get the Page.
92         if (!mayAccessPage ('view', $page)) {
93                 return $this->error(sprintf(_("Illegal inclusion of page %s: no read access"),
94                                         $page));
95         }
96         
97         $p = $dbi->getPage($page);
98         if ($rev) {
99             $r = $p->getRevision($rev);
100             if (!$r) {
101                 return $this->error(sprintf(_("%s(%d): no such revision"),
102                                             $page, $rev));
103             }
104         } else {
105             $r = $p->getCurrentRevision();
106         }
107         $c = $r->getContent();
108         
109         // follow redirects
110         if (preg_match('/<'.'\?plugin\s+RedirectTo\s+page=(\w+)\s+\?'.'>/', 
111                        implode("\n", $c), $m))
112         {
113             // trap recursive redirects
114             if (in_array($m[1], $included_pages)) {
115                 return $this->error(sprintf(_("recursive inclusion of page %s ignored"),
116                                                 $page.' => '.$m[1]));
117             }
118             $page = $m[1];
119             $p = $dbi->getPage($page);
120             $r = $p->getCurrentRevision();
121             $c = $r->getContent();   // array of lines
122         }
123         
124         $ct = $this->extractParts ($c, $page, $args);
125
126         // exclude from expansion
127         if (preg_match('/<noinclude>.+<\/noinclude>/s', $ct)) {
128             $ct = preg_replace("/<noinclude>.+?<\/noinclude>/s", "", $ct);
129         }
130         // only in expansion
131         $ct = preg_replace("/<includeonly>(.+)<\/includeonly>/s", "\\1", $ct);
132
133         array_push($included_pages, $page);
134
135         include_once('lib/BlockParser.php');
136         $content = TransformText($ct, $r->get('markup'), $page);
137
138         array_pop($included_pages);
139
140         if ($quiet)
141             return $content;
142
143         return HTML(HTML::p(array('class' => 'transclusion-title'),
144                             fmt("Included from %s", WikiLink($page))),
145
146                     HTML::div(array('class' => 'transclusion'),
147                               false, $content));
148     }
149     
150     /** 
151      * handles the arguments: section, sectionhead, lines, words, bytes,
152      * for UnfoldSubpages, IncludePage, ...
153      */
154     function extractParts ($c, $pagename, $args) {
155         extract($args);
156
157         if ($section) {
158             if ($sections) { 
159                 $c = extractSection($section, $c, $pagename, $quiet, 1);
160             } else {
161                 $c = extractSection($section, $c, $pagename, $quiet, $sectionhead);
162             }
163         }
164         if ($sections) {
165             $c = extractSections($sections, $c, $pagename, $quiet, 1);
166         }
167         if ($lines) {
168             $c = array_slice($c, 0, $lines);
169             $c[] = sprintf(_(" ... first %d lines"), $lines);
170         }
171         if ($words) {
172             $c = firstNWordsOfContent($words, $c);
173         }
174         if ($bytes) {
175             $ct = implode("\n", $c); // one string
176             if (strlen($ct) > $bytes) {
177                 $ct = substr($c, 0, $bytes);
178                 $c = array($ct, sprintf(_(" ... first %d bytes"), $bytes));
179             }
180         }
181         $ct = implode("\n", $c); // one string
182         return $ct;
183     }
184 };
185
186 // This is an excerpt from the css file I use:
187 //
188 // .transclusion-title {
189 //   font-style: oblique;
190 //   font-size: 0.75em;
191 //   text-decoration: underline;
192 //   text-align: right;
193 // }
194 //
195 // DIV.transclusion {
196 //   background: lightgreen;
197 //   border: thin;
198 //   border-style: solid;
199 //   padding-left: 0.8em;
200 //   padding-right: 0.8em;
201 //   padding-top: 0px;
202 //   padding-bottom: 0px;
203 //   margin: 0.5ex 0px;
204 // }
205
206 // For emacs users
207 // Local Variables:
208 // mode: php
209 // tab-width: 8
210 // c-basic-offset: 4
211 // c-hanging-comment-ender-p: nil
212 // indent-tabs-mode: nil
213 // End:
214 ?>