HTML/FlexyFramework/Page.php
[pear] / HTML / FlexyFramework / Page.php
1 <?php
2 /* vim: set expandtab tabstop=4 shiftwidth=4: */
3 // +----------------------------------------------------------------------+
4 // | PHP Version 4                                                        |
5 // +----------------------------------------------------------------------+
6 // | Copyright (c) 1997-2002 The PHP Group                                |
7 // +----------------------------------------------------------------------+
8 // | This source file is subject to version 2.02 of the PHP license,      |
9 // | that is bundled with this package in the file LICENSE, and is        |
10 // | available at through the world-wide-web at                           |
11 // | http://www.php.net/license/2_02.txt.                                 |
12 // | If you did not receive a copy of the PHP license and are unable to   |
13 // | obtain it through the world-wide-web, please send a note to          |
14 // | license@php.net so we can mail you a copy immediately.               |
15 // +----------------------------------------------------------------------+
16 // | Authors:  Alan Knowles <alan@akbkhome.com>                           |
17 // +----------------------------------------------------------------------+
18 //
19 // $Id: Page.php,v 1.5 2003/02/22 01:52:50 alan Exp $
20 //
21 // A Base Page Class for use with HTML_Template_Flexy
22 // You could write one of these which used another template engine.
23 //
24
25
26 require_once 'HTML/Template/Flexy.php' ;
27 require_once 'HTML/Template/Flexy/Factory.php' ;
28
29 /**
30 * The Base Page class - extend and override the methods to implement your own pages.
31 *
32 *
33 */
34
35
36 class HTML_FlexyFramework_Page  {
37
38
39     /**
40     * the main Template name (which can include a body template)
41     *
42     * @var string template name
43     * @access public
44     */
45     var $masterTemplate = "master.html";
46     /**
47     * the body Template name
48     *
49     * @var string template name
50     * @access public
51     */
52     var $template = "error.html";
53
54
55
56
57     /**
58     * cache method -
59     *   can be 'output', or 'data'
60     * used to set a default caching method
61     *
62     * @var string
63     * @access public
64     * @see getCache()
65     */
66     var $cacheMethod = '';
67
68
69
70
71    /**
72     * cache store (PEAR's Cache Object Instance
73     *   initialized at start
74     *   set at output stage.
75     *
76     * @var object
77     * @access private
78     * @see getCache()
79     */
80
81     var $_cache = NULL;
82
83     /* ---- Variables set by Page loader -------- */
84
85
86
87     /**
88     * baseURL, that can be prefixed to URL's to ensure that they correctly relate to application
89     * (set by page loader)
90     * @var string
91     * @access public
92     */
93     var $baseURL;
94     /**
95     * rootURL, the base installation directory - can be used to get images directories.
96     * (set by page loader)
97     * @var string
98     * @access public
99     */
100     var $rootURL;
101     /**
102     * rootDir, the base installation directory - can be used to find relative files.
103     * (set by page loader)
104     * @var string
105     * @access public
106     */
107     var $rootDir;
108     /**
109     * the full request string used by the getCacheID().
110     * (set by page loader)
111     * @var string
112     * @access public
113     */
114     var $request; // clean page request for page
115     /**
116     * overrides for elements.
117     *
118     * @var array
119     * @access public
120     */
121     var $elements = array(); // key=>HTML_Template_Flexy_Element
122
123      /**
124     * errors for elements
125     *
126     * @var array
127     * @access public
128     */
129     var $errors = array(); // key(element name)=>error message
130
131
132
133     /**
134     * Arguments from cli if static $cli_opts is used.
135     *
136     * @var array
137     * @access public
138     */
139     var $cli_args = array(); // key(element name)=>error message
140
141     /**
142      * Reference to the page loader
143      * @var type HTML_FlexyFramework -
144      *
145      */
146
147     var $bootLoader = false;
148
149
150
151     /**
152     * The default page handler
153     * by default relays to get(), or post() methods depending on the request.
154     *
155     * Override this if you do not handle get or post differently.
156     *
157     *
158     * @param   string  request, the remainder of the request not handled by the object.
159     *
160     * @return   none|string none = handled, string = redirect to another page = eg. data/list
161     * @access   public
162     */
163
164     function start($request,$isRedirect=false,$args=array())
165     {
166         $cli= HTML_Flexyframework::get()->cli;
167         if (!$cli && !$isRedirect && !empty($_SERVER["REQUEST_METHOD"]) && $_SERVER["REQUEST_METHOD"] == "POST") {
168             return $this->post($request,$args);
169         } else {
170             return $this->get($request,$args,$isRedirect);
171         }
172     }
173     /**
174     * The get page handler
175     *
176     * Override this if you want to handle get requests
177     *
178     *
179     * @param   string  request, the remainder of the request not handled by the object.
180     *
181     * @return   none|string none = handled, string = redirect to another page = eg. data/list
182     * @access   public
183     */
184     function get($request)
185     {
186     }
187      /**
188     * The post page handler
189     *
190     * Override this if you want to handle get requests
191     *
192     *
193     * @param   string  request, the remainder of the request not handled by the object.
194     *
195     * @return   none|string none = handled, string = redirect to another page = eg. data/list
196     * @access   public
197     */
198    function post($request)
199    {
200    }
201     /**
202     * Authentication Check method
203     * Override this with a straight return for pages that do not require authentication
204     *
205     * By default
206     *   a) redirects to login if authenticaiton fails
207     *   b) checks to see if a isAdmin method exists on the auth object
208     *       if it does see if the user is admin and let them in.
209     *       otherwise access denied error is raised
210     *   c) lets them in.
211     *
212     *
213     *
214     * @return   none or string redirection to another page.
215     * @access   public
216     */
217
218     function getAuth() {
219
220
221         return false;
222
223     }
224
225
226
227
228     /**
229     * The master Output layer.
230     *
231     * compiles the template
232     * if no caching - just runs the template.
233     * otherwise stores it in the cache.
234     *
235     * you dont normally need to override this.
236     *
237     * called by the page loader.
238     * @access   public
239     */
240
241
242
243     function output()
244     {
245
246         if (!empty($this->cli)) {
247             return;
248         }
249
250         /* output the body if no masterTemplate is set */
251         $options = HTML_FlexyFramework::get();
252
253         $type = isset($this->contentType) ? $this->contentType : 'text/html';
254         header('Content-Type: '.$type.';charset='.( empty($options->charset) ? 'UTF-8' : $options->charset ));
255
256         if (!$this->masterTemplate) {
257             return $this->outputBody();
258         }
259         
260         /* master template */
261         $template_engine = new HTML_Template_Flexy();
262         $template_engine->compile($this->masterTemplate);
263         if (!$this->_cache || !$this->cacheMethod) {
264             $template_engine->outputObject($this,$this->elements);
265             return;
266         }
267
268         $id = $this->_cache->generateID($this->getID());
269         $this->_cache->save($id, $template_engine->bufferedOutputObject($this,$this->elements));
270         echo $this->_cache->get($id);
271
272     }
273     /**
274     * The body Output layer.
275     *
276     * compiles the template
277     * At present there is no caching in here..  - this may change latter..
278     *
279     * used by putting {outputBody} in the main template.
280     * @access   public
281     */
282     function outputBody() {
283
284         $template_engine = new HTML_Template_Flexy();
285         $template_engine->compile($this->template);
286         if ($this->elements) { /* BC crap! */
287             $this->elements = HTML_Template_Flexy_Factory::setErrors($this->elements,$this->errors);
288         }
289         $template_engine->elements = $this->elements;
290         $template_engine->outputObject($this,$this->elements);
291
292     }
293
294
295      /**
296     * Do any Page Caching if $this->cacheMethod is set.
297     * You should also look at output caching by overriding the outputBody Method.
298     *
299     * Note that Caching is disabled in a number of situations.
300     *   a) cacheMethod is empty
301     *   b) POST requests
302     *   c) if sess is set (Eg. if you are using sessions)
303     *   d) useCache is not set in the [Cache] section of the config.
304     *
305     * utilizes $this->getCacheID() to
306     *
307     * @return   none|boolean|string|int|object    Description
308     * @access   public|private
309     * @see      see also methods.....
310     */
311
312
313     function getCache() {
314         if (!$this->cacheMethod) {
315             return;
316         }
317         if ($_SERVER["REQUEST_METHOD"] == "POST") {
318             return;
319         }
320
321         /* lets assume we can cache ourselves.. */
322         $coptions = PEAR::getStaticProperty('Cache','options');
323         if (!$coptions) {
324             return;
325         }
326         if (empty($coptions['useCache'])) {
327             return;
328         }
329         require_once 'Cache.php';
330
331         $this->_cache = new Cache($coptions['container'], $coptions);
332         $id = $this->_cache->generateID($this->getCacheID());
333         if ($_SERVER["REQUEST_METHOD"] == "POST") {
334             $this->_cache->flush($id);
335             return;
336         }
337         if ($data = $this->_cache->get($id)) {
338             echo $data;
339             return TRUE;
340         }
341
342     }
343
344     /**
345     * Get a distinct Page Cache ID.
346     *
347     * By default this is the full request string
348     * override this to define a more precise string
349     *
350     * @return   string   distinct page id (eg. the request url)
351     * @access   private
352     */
353
354     function getCacheID() {
355         return  $this->request;
356
357     }
358
359
360     /**
361     * Utility method : get the Class name (used on templates)
362     *
363     * @return   string   class name
364     * @access   public
365     */
366
367
368     function getClass() {
369         return get_class($this);
370     }
371     /**
372     * Utility method : get the Time taken to generate the page.
373     *
374     * @return   string   time in seconds..
375     * @access   public
376     */
377
378     function getTime() {
379
380         $m = explode(' ',microtime());
381         $ff =  HTML_FlexyFramework::get();
382         return sprintf('%0.2fs',($m[0] + $m[1]) -  $ff->start)
383                 . '/ Files ' . count(get_included_files());
384
385
386     }
387     /**
388      * turn on off session - wrap long database queries or
389      * data processing with this to prevent locking
390      * @see
391      * @param int $state new session state - 0 = off, 1 = on
392      */
393
394     function sessionState($state)
395     {
396         static $ses_status = false;
397         static $ini = false;
398         // session status is only php5.4 and up..
399         if (!defined('PHP_SESSION_ACTIVE')) {
400             define('PHP_SESSION_ACTIVE' , 1);
401         }
402         if(!function_exists('session_status')){
403              $ses_status = 1;
404         } else {
405             $ses_status = ($ses_status === false) ? session_status() : $ses_status;
406         }
407         if (PHP_SESSION_ACTIVE != $ses_status) {
408             return;
409         }
410
411         switch ($state) {
412             case 0:
413                 session_write_close();
414                 return;
415             case 1:
416                 if ($ini) {
417                     ini_set('session.use_only_cookies', false);
418                     ini_set('session.use_cookies', false);
419                     ini_set('session.use_trans_sid', false);
420                     ini_set('session.cache_limiter', null);
421                 }
422                 $ini = true;
423                 // sometimes raises a notice - ps_files_cleanup_dir.
424                 @session_start();
425                 return;
426         }
427     }
428
429 }