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