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