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 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    
142    
143    
144    
145    
146     /**
147     * The default page handler
148     * by default relays to get(), or post() methods depending on the request.
149     *
150     * Override this if you do not handle get or post differently.
151     * 
152     * 
153     * @param   string  request, the remainder of the request not handled by the object.
154     *
155     * @return   none|string none = handled, string = redirect to another page = eg. data/list
156     * @access   public
157     */
158   
159     function start($request,$isRedirect=false,$args=array()) 
160     { 
161         $cli= HTML_Flexyframework::get()->cli;
162         if (!$cli && !$isRedirect && !empty($_SERVER["REQUEST_METHOD"]) && $_SERVER["REQUEST_METHOD"] == "POST") {
163             return $this->post($request,$args);
164         } else {
165             return $this->get($request,$args,$isRedirect);
166         }
167     }
168     /**
169     * The get page handler
170     *
171     * Override this if you want to handle get requests 
172     * 
173     * 
174     * @param   string  request, the remainder of the request not handled by the object.
175     *
176     * @return   none|string none = handled, string = redirect to another page = eg. data/list
177     * @access   public
178     */
179     function get($request) 
180     {
181     }
182      /**
183     * The post page handler
184     *
185     * Override this if you want to handle get requests 
186     * 
187     * 
188     * @param   string  request, the remainder of the request not handled by the object.
189     *
190     * @return   none|string none = handled, string = redirect to another page = eg. data/list
191     * @access   public
192     */
193    function post($request) 
194    {
195    }
196     /**
197     * Authentication Check method
198     * Override this with a straight return for pages that do not require authentication
199     *
200     * By default 
201     *   a) redirects to login if authenticaiton fails
202     *   b) checks to see if a isAdmin method exists on the auth object
203     *       if it does see if the user is admin and let them in.
204     *       otherwise access denied error is raised
205     *   c) lets them in.
206     * 
207     * 
208     *
209     * @return   none or string redirection to another page.
210     * @access   public
211     */
212  
213     function getAuth() {
214          
215         
216         return false;
217         
218     }
219     
220   
221     
222      
223     /**
224     * The master Output layer.
225     * 
226     * compiles the template
227     * if no caching - just runs the template.
228     * otherwise stores it in the cache.
229     * 
230     * you dont normally need to override this.
231     * 
232     * called by the page loader.
233     * @access   public
234     */
235     
236     
237     
238     function output() 
239     {
240         
241         if (!empty($this->cli)) {
242             return;
243         }
244         
245         /* output the body if no masterTemplate is set */
246         $options = HTML_FlexyFramework::get();
247         
248         $type = isset($this->contentType) ? $this->contentType : 'text/html'; 
249         header('Content-Type: '.$type.';charset='.( empty($options->charset) ? 'UTF-8' : $options->charset ));
250         
251          
252         if (!$this->masterTemplate) {
253             return $this->outputBody();
254         }
255         /* master template */
256         
257        
258         $template_engine = new HTML_Template_Flexy();
259         $template_engine->compile($this->masterTemplate);
260         if (!$this->_cache || !$this->cacheMethod) {
261             $template_engine->outputObject($this,$this->elements);
262             return;
263         }
264         
265         $id = $this->_cache->generateID($this->getID());
266         $this->_cache->save($id, $template_engine->bufferedOutputObject($this,$this->elements));
267         echo $this->_cache->get($id);
268         
269     }
270     /**
271     * The body Output layer.
272     * 
273     * compiles the template
274     * At present there is no caching in here..  - this may change latter..
275     * 
276     * used by putting {outputBody} in the main template.
277     * @access   public
278     */    
279     function outputBody() {
280
281         $template_engine = new HTML_Template_Flexy();
282         $template_engine->compile($this->template);
283         if ($this->elements) { /* BC crap! */
284             $this->elements = HTML_Template_Flexy_Factory::setErrors($this->elements,$this->errors);
285         }
286         $template_engine->elements = $this->elements;
287         $template_engine->outputObject($this,$this->elements);
288         
289     }
290     
291    
292      /**
293     * Do any Page Caching if $this->cacheMethod is set.
294     * You should also look at output caching by overriding the outputBody Method.
295     *
296     * Note that Caching is disabled in a number of situations.
297     *   a) cacheMethod is empty
298     *   b) POST requests
299     *   c) if sess is set (Eg. if you are using sessions)
300     *   d) useCache is not set in the [Cache] section of the config.
301     * 
302     * utilizes $this->getCacheID() to  
303     *
304     * @return   none|boolean|string|int|object    Description
305     * @access   public|private
306     * @see      see also methods.....
307     */
308   
309     
310     function getCache() {
311         if (!$this->cacheMethod) {
312             return;
313         }
314         if ($_SERVER["REQUEST_METHOD"] == "POST") {
315             return;
316         } 
317          
318         /* lets assume we can cache ourselves.. */
319         $coptions = PEAR::getStaticProperty('Cache','options');
320         if (!$coptions) {
321             return;
322         }
323         if (empty($coptions['useCache'])) {
324             return;
325         }
326         require_once 'Cache.php';
327         
328         $this->_cache = new Cache($coptions['container'], $coptions);
329         $id = $this->_cache->generateID($this->getCacheID());
330         if ($_SERVER["REQUEST_METHOD"] == "POST") {
331             $this->_cache->flush($id);
332             return;
333         }
334         if ($data = $this->_cache->get($id)) {
335             echo $data;
336             return TRUE;
337         }
338     
339     }
340     
341     /**
342     * Get a distinct Page Cache ID. 
343     *
344     * By default this is the full request string
345     * override this to define a more precise string 
346     *
347     * @return   string   distinct page id (eg. the request url)
348     * @access   private
349     */
350   
351     function getCacheID() {
352         return  $this->request;
353         
354     }
355     
356     
357     /**
358     * Utility method : get the Class name (used on templates)
359     *
360     * @return   string   class name
361     * @access   public
362     */
363     
364     
365     function getClass() {
366         return get_class($this);
367     }
368     /**
369     * Utility method : get the Time taken to generate the page.
370     *
371     * @return   string   time in seconds..
372     * @access   public
373     */
374     
375     function getTime() {
376          
377         $m = explode(' ',microtime());
378         $ff =  HTML_FlexyFramework::get();
379         return sprintf('%0.2fs',($m[0] + $m[1]) -  $ff->start)
380                 . '/ Files ' . count(get_included_files());
381     
382     
383     }
384     /**
385      * turn on off session - wrap long database queries or
386      * data processing with this to prevent locking
387      * @see
388      * @param int $state new session state - 0 = off, 1 = on
389      */ 
390     
391     function sessionState($state)
392     { 
393         static $ses_status = false;
394         static $ini = false;
395         // session status is only php5.4 and up..
396         if (!defined('PHP_SESSION_ACTIVE')) {
397             define('PHP_SESSION_ACTIVE' , 1);
398         }
399         if(!function_exists('session_status')){
400              $ses_status = 1;
401         } else {
402             $ses_status = ($ses_status === false) ? session_status() : $ses_status;        
403         }
404         if (PHP_SESSION_ACTIVE != $ses_status) {
405             return;
406         }
407         
408         switch ($state) {
409             case 0:
410                 session_write_close();
411                 return;
412             case 1:
413                 if ($ini) {  
414                     ini_set('session.use_only_cookies', false);
415                     ini_set('session.use_cookies', false);
416                     ini_set('session.use_trans_sid', false);
417                     ini_set('session.cache_limiter', null);
418                 }
419                 $ini = true;
420                 @session_start();
421                 return;
422         }
423     }
424     
425 }
426
427