]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/WikiPlugin.php
Refactor the HTTP validator generation/checking code.
[SourceForge/phpwiki.git] / lib / WikiPlugin.php
1 <?php //-*-php-*-
2 rcs_id('$Id: WikiPlugin.php,v 1.30 2003-02-16 20:04:46 dairiki Exp $');
3
4 class WikiPlugin
5 {
6     function getDefaultArguments() {
7         return array('description' => $this->getDescription());
8     }
9
10     /** Does the plugin manage its own HTTP validators?
11      *
12      * This should be overwritten by (some) individual plugins.
13      *
14      * If the output of the plugin is static, depending only
15      * on the plugin arguments, query arguments and contents
16      * of the current page, this can (and should) return true.
17      *
18      * If the plugin can deduce a modification time, or equivalent
19      * sort of tag for it's content, then the plugin should
20      * call $request->appendValidators() with appropriate arguments,
21      * and should override this method to return true.
22      *
23      * When in doubt, the safe answer here is false.
24      * Unfortunately, returning false here will most likely make
25      * any page which invokes the plugin uncacheable (by HTTP proxies
26      * or browsers).
27      */
28     function managesValidators() {
29         return false;
30     }
31     
32     // FIXME: args?
33     function run ($dbi, $argstr, &$request) {
34         trigger_error("WikiPlugin::run: pure virtual function",
35                       E_USER_ERROR);
36     }
37
38     /**
39      * Get name of plugin.
40      *
41      * This is used (by default) by getDefaultLinkArguments and
42      * getDefaultFormArguments to compute the default link/form
43      * targets.
44      *
45      * If you want to gettextify the name (probably a good idea),
46      * override this method in your plugin class, like:
47      * <pre>
48      *   function getName() { return _("MyPlugin"); }
49      * </pre>
50      *
51      * @return string plugin name/target.
52      */
53     function getName() {
54         return preg_replace('/^.*_/', '',  get_class($this));
55     }
56
57     function getDescription() {
58         return $this->getName();
59     }
60
61
62     function getArgs($argstr, $request, $defaults = false) {
63         if ($defaults === false)
64             $defaults = $this->getDefaultArguments();
65
66         list ($argstr_args, $argstr_defaults) = $this->parseArgStr($argstr);
67         $args = array();
68         foreach ($defaults as $arg => $default_val) {
69             if (isset($argstr_args[$arg]))
70                 $args[$arg] = $argstr_args[$arg];
71             elseif ( ($argval = $request->getArg($arg)) !== false )
72                 $args[$arg] = $argval;
73             elseif (isset($argstr_defaults[$arg]))
74                 $args[$arg] = (string) $argstr_defaults[$arg];
75             else
76                 $args[$arg] = $default_val;
77
78             $args[$arg] = $this->expandArg($args[$arg], $request);
79
80             unset($argstr_args[$arg]);
81             unset($argstr_defaults[$arg]);
82         }
83
84         foreach (array_merge($argstr_args, $argstr_defaults) as $arg => $val) {
85             trigger_error(sprintf(_("argument '%s' not declared by plugin"),
86                                   $arg), E_USER_NOTICE);
87         }
88
89         return $args;
90     }
91
92     function expandArg($argval, $request) {
93         return preg_replace('/\[(\w[\w\d]*)\]/e', '$request->getArg("$1")',
94                             $argval);
95     }
96
97
98     function parseArgStr($argstr) {
99         $arg_p = '\w+';
100         $op_p = '(?:\|\|)?=';
101         $word_p = '\S+';
102         $opt_ws = '\s*';
103         $qq_p = '" ( (?:[^"\\\\]|\\\\.)* ) "';
104         //"<--kludge for brain-dead syntax coloring
105         $q_p  = "' ( (?:[^'\\\\]|\\\\.)* ) '";
106         $gt_p = "_\\( $opt_ws $qq_p $opt_ws \\)";
107         $argspec_p = "($arg_p) $opt_ws ($op_p) $opt_ws (?: $qq_p|$q_p|$gt_p|($word_p))";
108
109         $args = array();
110         $defaults = array();
111
112         while (preg_match("/^$opt_ws $argspec_p $opt_ws/x", $argstr, $m)) {
113             @ list(,$arg,$op,$qq_val,$q_val,$gt_val,$word_val) = $m;
114             $argstr = substr($argstr, strlen($m[0]));
115
116             // Remove quotes from string values.
117             if ($qq_val)
118                 $val = stripslashes($qq_val);
119             elseif ($q_val)
120                 $val = stripslashes($q_val);
121             elseif ($gt_val)
122                 $val = _(stripslashes($gt_val));
123             else
124                 $val = $word_val;
125
126             if ($op == '=') {
127                 $args[$arg] = $val;
128             }
129             else {
130                 // NOTE: This does work for multiple args. Use the
131                 // separator character defined in your webserver
132                 // configuration, usually & or &amp; (See
133                 // http://www.htmlhelp.com/faq/cgifaq.4.html)
134                 // e.g. <plugin RecentChanges days||=1 show_all||=0 show_minor||=0>
135                 // url: RecentChanges?days=1&show_all=1&show_minor=0
136                 assert($op == '||=');
137                 $defaults[$arg] = $val;
138             }
139         }
140
141         if ($argstr) {
142            $this->handle_plugin_args_cruft($argstr, $args);
143         }
144
145         return array($args, $defaults);
146     }
147
148     /* A plugin can override this function to define how any remaining text is handled */
149     function handle_plugin_args_cruft($argstr, $args) {
150         trigger_error(sprintf(_("trailing cruft in plugin args: '%s'"),
151                               $argstr), E_USER_NOTICE);
152     }
153
154     function getDefaultLinkArguments() {
155         return array('targetpage' => $this->getName(),
156                      'linktext' => $this->getName(),
157                      'description' => $this->getDescription(),
158                      'class' => 'wikiaction');
159     }
160
161     function makeLink($argstr, $request) {
162         $defaults = $this->getDefaultArguments();
163         $link_defaults = $this->getDefaultLinkArguments();
164         $defaults = array_merge($defaults, $link_defaults);
165     
166         $args = $this->getArgs($argstr, $request, $defaults);
167         $plugin = $this->getName();
168     
169         $query_args = array();
170         foreach ($args as $arg => $val) {
171             if (isset($link_defaults[$arg]))
172                 continue;
173             if ($val != $defaults[$arg])
174                 $query_args[$arg] = $val;
175         }
176     
177         $link = Button($query_args, $args['linktext'], $args['targetpage']);
178         if (!empty($args['description']))
179             $link->addTooltip($args['description']);
180     
181         return $link;
182     }
183     
184     function getDefaultFormArguments() {
185         return array('targetpage' => $this->getName(),
186                      'buttontext' => $this->getName(),
187                      'class' => 'wikiaction',
188                      'method' => 'get',
189                      'textinput' => 's',
190                      'description' => $this->getDescription(),
191                      'formsize' => 30);
192     }
193     
194     function makeForm($argstr, $request) {
195         $form_defaults = $this->getDefaultFormArguments();
196         $defaults = array_merge($this->getDefaultArguments(),
197                                 $form_defaults);
198     
199         $args = $this->getArgs($argstr, $request, $defaults);
200         $plugin = $this->getName();
201         $textinput = $args['textinput'];
202         assert(!empty($textinput) && isset($args['textinput']));
203     
204         $form = HTML::form(array('action' => WikiURL($args['targetpage']),
205                                  'method' => $args['method'],
206                                  'class' => $args['class'],
207                                  'accept-charset' => CHARSET));
208         if (! USE_PATH_INFO ) {
209             $pagename = $request->get('pagename');
210             $form->pushContent(HTML::input(array('type' => 'hidden', 'name' => 'pagename', 
211                                                  'value' => $args['targetpage'])));
212         }
213         $contents = HTML::div();
214         $contents->setAttr('class', $args['class']);
215     
216         foreach ($args as $arg => $val) {
217             if (isset($form_defaults[$arg]))
218                 continue;
219             if ($arg != $textinput && $val == $defaults[$arg])
220                 continue;
221     
222             $i = HTML::input(array('name' => $arg, 'value' => $val));
223     
224             if ($arg == $textinput) {
225                 //if ($inputs[$arg] == 'file')
226                 //    $attr['type'] = 'file';
227                 //else
228                 $i->setAttr('type', 'text');
229                 $i->setAttr('size', $args['formsize']);
230                 if ($args['description'])
231                     $i->addTooltip($args['description']);
232             }
233             else {
234                 $i->setAttr('type', 'hidden');
235             }
236             $contents->pushContent($i);
237     
238             // FIXME: hackage
239             if ($i->getAttr('type') == 'file') {
240                 $form->setAttr('enctype', 'multipart/form-data');
241                 $form->setAttr('method', 'post');
242                 $contents->pushContent(HTML::input(array('name' => 'MAX_FILE_SIZE',
243                                                          'value' => MAX_UPLOAD_SIZE,
244                                                          'type' => 'hidden')));
245             }
246         }
247     
248         if (!empty($args['buttontext']))
249             $contents->pushContent(HTML::input(array('type' => 'submit',
250                                                      'class' => 'button',
251                                                      'value' => $args['buttontext'])));
252     
253         $form->pushContent($contents);
254         return $form;
255     }
256     
257     function error ($message) {
258         return HTML::div(array('class' => 'errors'),
259                         HTML::strong(fmt("Plugin %s failed.", $this->getName())), ' ',
260                         $message);
261     }
262
263     function disabled ($message='') {
264         $html[] = HTML::div(array('class' => 'title'),
265                             fmt("Plugin %s disabled.", $this->getName()),
266                             ' ', $message);
267         $html[] = HTML::pre($this->_pi);
268         return HTML::div(array('class' => 'disabled-plugin'), $html);
269     }
270 }
271
272 class WikiPluginLoader {
273     var $_errors;
274
275     function expandPI($pi, &$request) {
276         if (!preg_match('/^\s*<\?(plugin(?:-form|-link)?)\s+(\w+)\s*(.*?)\s*\?>\s*$/s', $pi, $m))
277             return $this->_error(sprintf("Bad %s", 'PI'));
278
279         list(, $pi_name, $plugin_name, $plugin_args) = $m;
280         $plugin = $this->getPlugin($plugin_name, $pi);
281         if (!is_object($plugin)) {
282             return new HtmlElement($pi_name == 'plugin-link' ? 'span' : 'p',
283                                    array('class' => 'plugin-error'),
284                                    $this->getErrorDetail());
285         }
286         switch ($pi_name) {
287             case 'plugin':
288                 // FIXME: change API for run() (no $dbi needed).
289                 $dbi = $request->getDbh();
290                 // FIXME: could do better here...
291                 if (! $plugin->managesValidators()) {
292                     // Output of plugin (potentially) depends on
293                     // the state of the WikiDB (other than the current
294                     // page.)
295                     
296                     // Lacking other information, we'll assume things
297                     // changed last time the wikidb was touched.
298                     
299                     // As an additional hack, mark the ETag weak, since,
300                     // for all we know, the page might depend
301                     // on things other than the WikiDB (e.g. PhpWeather,
302                     // Calendar...)
303                     
304                     $timestamp = $dbi->getTimestamp();
305                     $request->appendValidators(array('dbi_timestamp' => $timestamp,
306                                                      '%mtime' => (int)$timestamp,
307                                                      '%weak' => true));
308                 }
309                 return $plugin->run($dbi, $plugin_args, $request);
310             case 'plugin-link':
311                 return $plugin->makeLink($plugin_args, $request);
312             case 'plugin-form':
313                 return $plugin->makeForm($plugin_args, $request);
314         }
315     }
316
317     // Special treatment. Only called by Template.php:GeneratePage
318     function expandPI_head($pi, $request) {
319         if (!preg_match('/^\s*<\?plugin-head\s+(\w+)\s*(.*?)\s*\?>\s*$/s', $pi, $m))
320             return $this->_error(sprintf("Bad %s", 'PI'));
321
322         list(, $plugin_name, $plugin_args) = $m;
323         $plugin = $this->getPlugin($plugin_name);
324         if (!is_object($plugin)) {
325             return new HtmlElement('p', array('class' => 'plugin-error'),
326                                    $this->getErrorDetail());
327         }
328         $dbi = $request->getDbh();
329         return $plugin->run($dbi, $plugin_args, $request);
330     }
331
332     function getPlugin($plugin_name, $pi) {
333         global $ErrorManager;
334
335         // Note that there seems to be no way to trap parse errors
336         // from this include.  (At least not via set_error_handler().)
337         $plugin_source = "lib/plugin/$plugin_name.php";
338
339         $ErrorManager->pushErrorHandler(new WikiMethodCb($this, '_plugin_error_filter'));
340         $include_failed = !include_once("lib/plugin/$plugin_name.php");
341         $ErrorManager->popErrorHandler();
342
343         $plugin_class = "WikiPlugin_$plugin_name";
344         if (!class_exists($plugin_class)) {
345             if ($include_failed)
346                 return $this->_error(sprintf(_("Include of '%s' failed"),
347                                              $plugin_source));
348             return $this->_error(sprintf("%s: no such class", $plugin_class));
349         }
350
351         $plugin = new $plugin_class;
352         if (!is_subclass_of($plugin, "WikiPlugin"))
353             return $this->_error(sprintf("%s: not a subclass of WikiPlugin",
354                                          $plugin_class));
355
356         $plugin->_pi = $pi;
357         return $plugin;
358     }
359
360     function _plugin_error_filter ($err) {
361         if (preg_match("/Failed opening '.*' for inclusion/", $err->errstr))
362             return true;        // Ignore this error --- it's expected.
363         return false;
364     }
365
366     function getErrorDetail() {
367         return $this->_errors;
368     }
369
370     function _error($message) {
371         $this->_errors = $message;
372         return false;
373     }
374 };
375
376 // (c-file-style: "gnu")
377 // Local Variables:
378 // mode: php
379 // tab-width: 8
380 // c-basic-offset: 4
381 // c-hanging-comment-ender-p: nil
382 // indent-tabs-mode: nil
383 // End:
384 ?>