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.
35 class HTML_FlexyFramework_Page {
39 * the main Template name (which can include a body template)
41 * @var string template name
44 var $masterTemplate = "master.html";
46 * the body Template name
48 * @var string template name
51 var $template = "error.html";
58 * can be 'output', or 'data'
59 * used to set a default caching method
65 var $cacheMethod = '';
71 * cache store (PEAR's Cache Object Instance
72 * initialized at start
73 * set at output stage.
82 /* ---- Variables set by Page loader -------- */
87 * baseURL, that can be prefixed to URL's to ensure that they correctly relate to application
88 * (set by page loader)
94 * rootURL, the base installation directory - can be used to get images directories.
95 * (set by page loader)
101 * rootDir, the base installation directory - can be used to find relative files.
102 * (set by page loader)
108 * the full request string used by the getCacheID().
109 * (set by page loader)
113 var $request; // clean page request for page
115 * overrides for elements.
120 var $elements = array(); // key=>HTML_Template_Flexy_Element
123 * errors for elements
128 var $errors = array(); // key(element name)=>error message
133 * Arguments from cli if static $cli_opts is used.
138 var $cli_args = array(); // key(element name)=>error message
141 * Reference to the page loader
142 * @var type HTML_FlexyFramework -
146 var $bootLoader = false;
151 * The default page handler
152 * by default relays to get(), or post() methods depending on the request.
154 * Override this if you do not handle get or post differently.
157 * @param string request, the remainder of the request not handled by the object.
159 * @return none|string none = handled, string = redirect to another page = eg. data/list
163 function start($request,$isRedirect=false,$args=array())
165 $cli= HTML_Flexyframework::get()->cli;
166 if (!$cli && !$isRedirect && !empty($_SERVER["REQUEST_METHOD"]) && $_SERVER["REQUEST_METHOD"] == "POST") {
167 return $this->post($request,$args);
169 return $this->get($request,$args,$isRedirect);
173 * The get page handler
175 * Override this if you want to handle get requests
178 * @param string request, the remainder of the request not handled by the object.
180 * @return none|string none = handled, string = redirect to another page = eg. data/list
183 function get($request)
187 * The post page handler
189 * Override this if you want to handle get requests
192 * @param string request, the remainder of the request not handled by the object.
194 * @return none|string none = handled, string = redirect to another page = eg. data/list
197 function post($request)
201 * Authentication Check method
202 * Override this with a straight return for pages that do not require authentication
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
213 * @return none or string redirection to another page.
228 * The master Output layer.
230 * compiles the template
231 * if no caching - just runs the template.
232 * otherwise stores it in the cache.
234 * you dont normally need to override this.
236 * called by the page loader.
245 if (!empty($this->cli)) {
249 /* output the body if no masterTemplate is set */
250 $options = HTML_FlexyFramework::get();
252 $type = isset($this->contentType) ? $this->contentType : 'text/html';
253 header('Content-Type: '.$type.';charset='.( empty($options->charset) ? 'UTF-8' : $options->charset ));
256 if (!$this->masterTemplate) {
257 return $this->outputBody();
259 /* master template */
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);
269 $id = $this->_cache->generateID($this->getID());
270 $this->_cache->save($id, $template_engine->bufferedOutputObject($this,$this->elements));
271 echo $this->_cache->get($id);
275 * The body Output layer.
277 * compiles the template
278 * At present there is no caching in here.. - this may change latter..
280 * used by putting {outputBody} in the main template.
283 function outputBody() {
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);
291 $template_engine->elements = $this->elements;
292 $template_engine->outputObject($this,$this->elements);
298 * Do any Page Caching if $this->cacheMethod is set.
299 * You should also look at output caching by overriding the outputBody Method.
301 * Note that Caching is disabled in a number of situations.
302 * a) cacheMethod is empty
304 * c) if sess is set (Eg. if you are using sessions)
305 * d) useCache is not set in the [Cache] section of the config.
307 * utilizes $this->getCacheID() to
309 * @return none|boolean|string|int|object Description
310 * @access public|private
311 * @see see also methods.....
315 function getCache() {
316 if (!$this->cacheMethod) {
319 if ($_SERVER["REQUEST_METHOD"] == "POST") {
323 /* lets assume we can cache ourselves.. */
324 $coptions = PEAR::getStaticProperty('Cache','options');
328 if (empty($coptions['useCache'])) {
331 require_once 'Cache.php';
333 $this->_cache = new Cache($coptions['container'], $coptions);
334 $id = $this->_cache->generateID($this->getCacheID());
335 if ($_SERVER["REQUEST_METHOD"] == "POST") {
336 $this->_cache->flush($id);
339 if ($data = $this->_cache->get($id)) {
347 * Get a distinct Page Cache ID.
349 * By default this is the full request string
350 * override this to define a more precise string
352 * @return string distinct page id (eg. the request url)
356 function getCacheID() {
357 return $this->request;
363 * Utility method : get the Class name (used on templates)
365 * @return string class name
370 function getClass() {
371 return get_class($this);
374 * Utility method : get the Time taken to generate the page.
376 * @return string time in seconds..
382 $m = explode(' ',microtime());
383 $ff = HTML_FlexyFramework::get();
384 return sprintf('%0.2fs',($m[0] + $m[1]) - $ff->start)
385 . '/ Files ' . count(get_included_files());
390 * turn on off session - wrap long database queries or
391 * data processing with this to prevent locking
393 * @param int $state new session state - 0 = off, 1 = on
396 function sessionState($state)
398 static $ses_status = false;
400 // session status is only php5.4 and up..
401 if (!defined('PHP_SESSION_ACTIVE')) {
402 define('PHP_SESSION_ACTIVE' , 1);
404 if(!function_exists('session_status')){
407 $ses_status = ($ses_status === false) ? session_status() : $ses_status;
409 if (PHP_SESSION_ACTIVE != $ses_status) {
415 session_write_close();
419 ini_set('session.use_only_cookies', false);
420 ini_set('session.use_cookies', false);
421 ini_set('session.use_trans_sid', false);
422 ini_set('session.cache_limiter', null);
425 // sometimes raises a notice - ps_files_cleanup_dir.