]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/Template.php
If using Db auth and DBAUTH_AUTH_DSN is empty set DBAUTH_AUTH_DSN to $DBParams['dsn']
[SourceForge/phpwiki.git] / lib / Template.php
1 <?php //-*-php-*-
2 rcs_id('$Id: Template.php,v 1.56 2004-04-12 13:04:50 rurban Exp $');
3
4 require_once("lib/ErrorManager.php");
5
6
7 /** An HTML template.
8  */
9 class Template
10 {
11     /**
12      *
13      */
14     function Template ($name, &$request, $args = false) {
15         global $Theme;
16
17         $this->_request = &$request;
18         $this->_name = $name;
19         $this->_basepage = $request->getArg('pagename');
20         
21         $file = $Theme->findTemplate($name);
22         if (!$file) {
23             trigger_error("no template for $name found", E_USER_WARNING);
24             return;
25         }
26         $fp = fopen($file, "rb");
27         if (!$fp) {
28             trigger_error("$file not found", E_USER_WARNING);
29             return;
30         }
31         $this->_tmpl = fread($fp, filesize($file));
32         fclose($fp);
33         //$userid = $request->_user->_userid;
34         if (is_array($args))
35             $this->_locals = $args;
36         elseif ($args)
37             $this->_locals = array('CONTENT' => $args);
38         else
39             $this->_locals = array();
40     }
41
42     function _munge_input($template) {
43
44         // Convert < ?plugin expr ? > to < ?php $this->_printPluginPI("expr"); ? >
45         $orig[] = '/<\?plugin.*?\?>/se';
46         $repl[] = "\$this->_mungePlugin('\\0')";
47         
48         // Convert < ?= expr ? > to < ?php $this->_print(expr); ? >
49         $orig[] = '/<\?=(.*?)\?>/s';
50         $repl[] = '<?php $this->_print(\1);?>';
51         
52         return preg_replace($orig, $repl, $template);
53     }
54
55     function _mungePlugin($pi) {
56         // HACK ALERT: PHP's preg_replace, with the /e option seems to
57         // escape both single and double quotes with backslashes.
58         // So we need to unescape the double quotes here...
59
60         $pi = preg_replace('/(?!<\\\\)\\\\"/x', '"', $pi);
61         return sprintf('<?php $this->_printPlugin(%s); ?>',
62                        "'" . str_replace("'", "\'", $pi) . "'");
63     }
64     
65     function _printPlugin ($pi) {
66         include_once("lib/WikiPlugin.php");
67         static $loader;
68
69         if (empty($loader))
70             $loader = new WikiPluginLoader;
71         
72         $this->_print($loader->expandPI($pi, $this->_request, $this, $this->_basepage));
73     }
74     
75     function _print ($val) {
76         if (isa($val, 'Template'))
77             $this->_expandSubtemplate($val);
78         else
79             PrintXML($val);
80     }
81
82     function _expandSubtemplate (&$template) {
83         // FIXME: big hack!        
84         if (!$template->_request)
85             $template->_request = &$this->_request;
86         if (defined('DEBUG') and DEBUG) {
87             echo "<!-- Begin $template->_name -->\n";
88         }
89         // Expand sub-template with defaults from this template.
90         $template->printExpansion($this->_vars);
91         if (defined('DEBUG') and DEBUG) {
92             echo "<!-- End $template->_name -->\n";
93         }
94     }
95         
96     /**
97      * Substitute HTML replacement text for tokens in template. 
98      *
99      * Constructs a new WikiTemplate based upon the named template.
100      *
101      * @access public
102      *
103      * @param $token string Name of token to substitute for.
104      *
105      * @param $replacement string Replacement HTML text.
106      */
107     function replace($varname, $value) {
108         $this->_locals[$varname] = $value;
109     }
110
111     
112     function printExpansion ($defaults = false) {
113         if (!is_array($defaults))
114             $defaults = array('CONTENT' => $defaults);
115         $this->_vars = array_merge($defaults, $this->_locals);
116         extract($this->_vars);
117
118         $request = &$this->_request;
119         if (!isset($user))
120             $user = &$request->getUser();
121         if (!isset($page))
122             $page = &$request->getPage();
123         
124         global $Theme, $RCS_IDS;
125
126         
127         
128         //$this->_dump_template();
129
130         global $ErrorManager;
131         $ErrorManager->pushErrorHandler(new WikiMethodCb($this, '_errorHandler'));
132
133         eval('?>' . $this->_munge_input($this->_tmpl));
134
135         $ErrorManager->popErrorHandler();
136     }
137
138     function getExpansion ($defaults = false) {
139         ob_start();
140         $this->printExpansion($defaults);
141         $xml = ob_get_contents();
142         ob_end_clean();
143         return $xml;
144     }
145
146     function printXML () {
147         $this->printExpansion();
148     }
149
150     function asXML () {
151         return $this->getExpansion();
152     }
153     
154             
155     // Debugging:
156     function _dump_template () {
157         $lines = explode("\n", $this->_munge_input($this->_tmpl));
158         $pre = HTML::pre();
159         $n = 1;
160         foreach ($lines as $line)
161             $pre->pushContent(fmt("%4d  %s\n", $n++, $line));
162         $pre->printXML();
163     }
164
165     function _errorHandler($error) {
166         //if (!preg_match('/: eval\(\)\'d code$/', $error->errfile))
167         //    return false;
168
169         if (preg_match('/: eval\(\)\'d code$/', $error->errfile)) {
170             $error->errfile = "In template '$this->_name'";
171             // Hack alert: Ignore 'undefined variable' messages for variables
172             //  whose names are ALL_CAPS.
173             if (preg_match('/Undefined variable:\s*[_A-Z]+\s*$/', $error->errstr))
174                 return true;
175         }
176         else
177             $error->errfile .= " (In template '$this->_name')";
178         
179         $lines = explode("\n", $this->_tmpl);
180         if (isset($lines[$error->errline - 1]))
181             $error->errstr .= ":\n\t" . $lines[$error->errline - 1];
182         return $error;
183     }
184 };
185
186 /**
187  * Get a templates
188  *
189  * This is a convenience function and is equivalent to:
190  * <pre>
191  *   new Template(...)
192  * </pre>
193  */
194 function Template($name, $args = false) {
195     global $request;
196     return new Template($name, $request, $args);
197 }
198
199 /**
200  * Make and expand the top-level template. 
201  *
202  *
203  * @param $content mixed html content to put into the page
204  * @param $title string page title
205  * @param $page_revision object A WikiDB_PageRevision object
206  * @param $args hash Extract args for top-level template
207  *
208  * @return string HTML expansion of template.
209  */
210 function GeneratePage($content, $title, $page_revision = false, $args = false) {
211     global $request;
212     
213     if (!is_array($args))
214         $args = array();
215
216     $args['CONTENT'] = $content;
217     $args['TITLE'] = $title;
218     $args['revision'] = $page_revision;
219     
220     if (!isset($args['HEADER']))
221         $args['HEADER'] = $title;
222     
223     printXML(new Template('html', $request, $args));
224 }
225
226
227 /**
228  * For dumping pages as html to a file.
229  */
230 function GeneratePageasXML($content, $title, $page_revision = false, $args = false) {
231     global $request;
232     
233     if (!is_array($args))
234         $args = array();
235
236     $content->_basepage = $title;
237     $args['CONTENT'] = $content;
238     $args['TITLE'] = split_pagename($title);
239     $args['revision'] = $page_revision;
240     
241     if (!isset($args['HEADER']))
242         $args['HEADER'] = split_pagename($title);
243     
244     global $HIDE_TOOLBARS, $NO_BASEHREF, $HTML_DUMP;
245     $HIDE_TOOLBARS = true;
246     $HTML_DUMP = true;
247
248     $html = asXML(new Template('htmldump', $request, $args));
249
250     $HIDE_TOOLBARS = false;
251     $HTML_DUMP = false;
252     return $html;
253 }
254
255 // $Log: not supported by cvs2svn $
256 // Revision 1.55  2004/04/02 15:06:55  rurban
257 // fixed a nasty ADODB_mysql session update bug
258 // improved UserPreferences layout (tabled hints)
259 // fixed UserPreferences auth handling
260 // improved auth stability
261 // improved old cookie handling: fixed deletion of old cookies with paths
262 //
263 // Revision 1.54  2004/03/02 18:11:39  rurban
264 // CreateToc support: Pass the preparsed markup to each plugin as $dbi->_markup
265 // to be able to know about its context, and even let the plugin change it.
266 // (see CreateToc)
267 //
268 // Revision 1.53  2004/02/22 23:20:31  rurban
269 // fixed DumpHtmlToDir,
270 // enhanced sortby handling in PageList
271 //   new button_heading th style (enabled),
272 // added sortby and limit support to the db backends and plugins
273 //   for paging support (<<prev, next>> links on long lists)
274 //
275 // Revision 1.52  2003/12/20 23:59:19  carstenklapp
276 // Internal change: Added rcs Log tag & emacs php mode tag (sorry, forgot
277 // this in the last commit).
278 //
279
280 // Local Variables:
281 // mode: php
282 // tab-width: 8
283 // c-basic-offset: 4
284 // c-hanging-comment-ender-p: nil
285 // indent-tabs-mode: nil
286 // End:   
287 ?>