2 /* vim: set expandtab tabstop=4 shiftwidth=4: */
3 // +----------------------------------------------------------------------+
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 // +----------------------------------------------------------------------+
19 // $Id: Page.php,v 1.5 2003/02/22 01:52:50 alan Exp $
21 // A Base Page Class for use with HTML_Template_Flexy
22 // You could write one of these which used another template engine.
26 require_once 'HTML/Template/Flexy.php' ;
27 require_once 'HTML/Template/Flexy/Factory.php' ;
30 * The Base Page class - extend and override the methods to implement your own pages.
34 if (PHP_MAJOR_VERSION >= 7) {
35 set_error_handler(function ($errno, $errstr) {
36 return strpos($errstr, 'Declaration of') === 0;
41 class HTML_FlexyFramework_Page {
45 * the main Template name (which can include a body template)
47 * @var string template name
50 var $masterTemplate = "master.html";
52 * the body Template name
54 * @var string template name
57 var $template = "error.html";
64 * can be 'output', or 'data'
65 * used to set a default caching method
71 var $cacheMethod = '';
77 * cache store (PEAR's Cache Object Instance
78 * initialized at start
79 * set at output stage.
88 /* ---- Variables set by Page loader -------- */
93 * baseURL, that can be prefixed to URL's to ensure that they correctly relate to application
94 * (set by page loader)
100 * rootURL, the base installation directory - can be used to get images directories.
101 * (set by page loader)
107 * rootDir, the base installation directory - can be used to find relative files.
108 * (set by page loader)
114 * the full request string used by the getCacheID().
115 * (set by page loader)
119 var $request; // clean page request for page
121 * overrides for elements.
126 var $elements = array(); // key=>HTML_Template_Flexy_Element
129 * errors for elements
134 var $errors = array(); // key(element name)=>error message
139 * Arguments from cli if static $cli_opts is used.
144 var $cli_args = array(); // key(element name)=>error message
147 * Reference to the page loader
148 * @var type HTML_FlexyFramework -
152 var $bootLoader = false;
157 * The default page handler
158 * by default relays to get(), or post() methods depending on the request.
160 * Override this if you do not handle get or post differently.
163 * @param string request, the remainder of the request not handled by the object.
165 * @return none|string none = handled, string = redirect to another page = eg. data/list
169 function start($request,$isRedirect=false,$args=array())
171 $cli= HTML_Flexyframework::get()->cli;
172 if (!$cli && !$isRedirect && !empty($_SERVER["REQUEST_METHOD"]) && $_SERVER["REQUEST_METHOD"] == "POST") {
173 return $this->post($request,$args);
175 return $this->get($request,$args,$isRedirect);
179 * The get page handler
181 * Override this if you want to handle get requests
184 * @param string request, the remainder of the request not handled by the object.
186 * @return none|string none = handled, string = redirect to another page = eg. data/list
189 function get($request)
193 * The post page handler
195 * Override this if you want to handle get requests
198 * @param string request, the remainder of the request not handled by the object.
200 * @return none|string none = handled, string = redirect to another page = eg. data/list
203 function post($request)
207 * Authentication Check method
208 * Override this with a straight return for pages that do not require authentication
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
219 * @return none or string redirection to another page.
234 * The master Output layer.
236 * compiles the template
237 * if no caching - just runs the template.
238 * otherwise stores it in the cache.
240 * you dont normally need to override this.
242 * called by the page loader.
251 if (!empty($this->cli)) {
255 /* output the body if no masterTemplate is set */
256 $options = HTML_FlexyFramework::get();
258 $type = isset($this->contentType) ? $this->contentType : 'text/html';
259 header('Content-Type: '.$type.';charset='.( empty($options->charset) ? 'UTF-8' : $options->charset ));
262 if (!$this->masterTemplate) {
263 return $this->outputBody();
265 /* master template */
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);
275 $id = $this->_cache->generateID($this->getID());
276 $this->_cache->save($id, $template_engine->bufferedOutputObject($this,$this->elements));
277 echo $this->_cache->get($id);
281 * The body Output layer.
283 * compiles the template
284 * At present there is no caching in here.. - this may change latter..
286 * used by putting {outputBody} in the main template.
289 function outputBody() {
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);
296 $template_engine->elements = $this->elements;
297 $template_engine->outputObject($this,$this->elements);
303 * Do any Page Caching if $this->cacheMethod is set.
304 * You should also look at output caching by overriding the outputBody Method.
306 * Note that Caching is disabled in a number of situations.
307 * a) cacheMethod is empty
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.
312 * utilizes $this->getCacheID() to
314 * @return none|boolean|string|int|object Description
315 * @access public|private
316 * @see see also methods.....
320 function getCache() {
321 if (!$this->cacheMethod) {
324 if ($_SERVER["REQUEST_METHOD"] == "POST") {
328 /* lets assume we can cache ourselves.. */
329 $coptions = PEAR::getStaticProperty('Cache','options');
333 if (empty($coptions['useCache'])) {
336 require_once 'Cache.php';
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);
344 if ($data = $this->_cache->get($id)) {
352 * Get a distinct Page Cache ID.
354 * By default this is the full request string
355 * override this to define a more precise string
357 * @return string distinct page id (eg. the request url)
361 function getCacheID() {
362 return $this->request;
368 * Utility method : get the Class name (used on templates)
370 * @return string class name
375 function getClass() {
376 return get_class($this);
379 * Utility method : get the Time taken to generate the page.
381 * @return string time in seconds..
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());
395 * turn on off session - wrap long database queries or
396 * data processing with this to prevent locking
398 * @param int $state new session state - 0 = off, 1 = on
401 function sessionState($state)
403 static $ses_status = false;
405 // session status is only php5.4 and up..
406 if (!defined('PHP_SESSION_ACTIVE')) {
407 define('PHP_SESSION_ACTIVE' , 1);
409 if(!function_exists('session_status')){
412 $ses_status = ($ses_status === false) ? session_status() : $ses_status;
414 if (PHP_SESSION_ACTIVE != $ses_status) {
420 session_write_close();
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);
430 // sometimes raises a notice - ps_files_cleanup_dir.