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