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.
36 class HTML_FlexyFramework_Page {
40 * the main Template name (which can include a body template)
42 * @var string template name
45 var $masterTemplate = "master.html";
47 * the body Template name
49 * @var string template name
52 var $template = "error.html";
59 * can be 'output', or 'data'
60 * used to set a default caching method
66 var $cacheMethod = '';
72 * cache store (PEAR's Cache Object Instance
73 * initialized at start
74 * set at output stage.
83 /* ---- Variables set by Page loader -------- */
88 * baseURL, that can be prefixed to URL's to ensure that they correctly relate to application
89 * (set by page loader)
95 * rootURL, the base installation directory - can be used to get images directories.
96 * (set by page loader)
102 * rootDir, the base installation directory - can be used to find relative files.
103 * (set by page loader)
109 * the full request string used by the getCacheID().
110 * (set by page loader)
114 var $request; // clean page request for page
116 * overrides for elements.
121 var $elements = array(); // key=>HTML_Template_Flexy_Element
124 * errors for elements
129 var $errors = array(); // key(element name)=>error message
134 * Arguments from cli if static $cli_opts is used.
139 var $cli_args = array(); // key(element name)=>error message
142 * Reference to the page loader
143 * @var type HTML_FlexyFramework -
147 var $bootLoader = false;
152 * The default page handler
153 * by default relays to get(), or post() methods depending on the request.
155 * Override this if you do not handle get or post differently.
158 * @param string request, the remainder of the request not handled by the object.
160 * @return none|string none = handled, string = redirect to another page = eg. data/list
164 function start($request,$isRedirect=false,$args=array())
166 $cli= HTML_Flexyframework::get()->cli;
167 if (!$cli && !$isRedirect && !empty($_SERVER["REQUEST_METHOD"]) && $_SERVER["REQUEST_METHOD"] == "POST") {
168 return $this->post($request,$args);
170 return $this->get($request,$args,$isRedirect);
174 * The get page handler
176 * Override this if you want to handle get requests
179 * @param string request, the remainder of the request not handled by the object.
181 * @return none|string none = handled, string = redirect to another page = eg. data/list
184 function get($request)
188 * The post page handler
190 * Override this if you want to handle get requests
193 * @param string request, the remainder of the request not handled by the object.
195 * @return none|string none = handled, string = redirect to another page = eg. data/list
198 function post($request)
202 * Authentication Check method
203 * Override this with a straight return for pages that do not require authentication
206 * a) redirects to login if authenticaiton fails
207 * b) checks to see if a isAdmin method exists on the auth object
208 * if it does see if the user is admin and let them in.
209 * otherwise access denied error is raised
214 * @return none or string redirection to another page.
229 * The master Output layer.
231 * compiles the template
232 * if no caching - just runs the template.
233 * otherwise stores it in the cache.
235 * you dont normally need to override this.
237 * called by the page loader.
245 print_r('bresk');exit;
248 if (!empty($this->cli)) {
252 /* output the body if no masterTemplate is set */
253 $options = HTML_FlexyFramework::get();
255 $type = isset($this->contentType) ? $this->contentType : 'text/html';
256 header('Content-Type: '.$type.';charset='.( empty($options->charset) ? 'UTF-8' : $options->charset ));
259 if (!$this->masterTemplate) {
260 return $this->outputBody();
262 /* master template */
265 $template_engine = new HTML_Template_Flexy();
266 $template_engine->compile($this->masterTemplate);
267 if (!$this->_cache || !$this->cacheMethod) {
268 $template_engine->outputObject($this,$this->elements);
272 $id = $this->_cache->generateID($this->getID());
273 $this->_cache->save($id, $template_engine->bufferedOutputObject($this,$this->elements));
274 echo $this->_cache->get($id);
278 * The body Output layer.
280 * compiles the template
281 * At present there is no caching in here.. - this may change latter..
283 * used by putting {outputBody} in the main template.
286 function outputBody() {
288 $template_engine = new HTML_Template_Flexy();
289 $template_engine->compile($this->template);
290 if ($this->elements) { /* BC crap! */
291 $this->elements = HTML_Template_Flexy_Factory::setErrors($this->elements,$this->errors);
293 $template_engine->elements = $this->elements;
294 $template_engine->outputObject($this,$this->elements);
300 * Do any Page Caching if $this->cacheMethod is set.
301 * You should also look at output caching by overriding the outputBody Method.
303 * Note that Caching is disabled in a number of situations.
304 * a) cacheMethod is empty
306 * c) if sess is set (Eg. if you are using sessions)
307 * d) useCache is not set in the [Cache] section of the config.
309 * utilizes $this->getCacheID() to
311 * @return none|boolean|string|int|object Description
312 * @access public|private
313 * @see see also methods.....
317 function getCache() {
318 if (!$this->cacheMethod) {
321 if ($_SERVER["REQUEST_METHOD"] == "POST") {
325 /* lets assume we can cache ourselves.. */
326 $coptions = PEAR::getStaticProperty('Cache','options');
330 if (empty($coptions['useCache'])) {
333 require_once 'Cache.php';
335 $this->_cache = new Cache($coptions['container'], $coptions);
336 $id = $this->_cache->generateID($this->getCacheID());
337 if ($_SERVER["REQUEST_METHOD"] == "POST") {
338 $this->_cache->flush($id);
341 if ($data = $this->_cache->get($id)) {
349 * Get a distinct Page Cache ID.
351 * By default this is the full request string
352 * override this to define a more precise string
354 * @return string distinct page id (eg. the request url)
358 function getCacheID() {
359 return $this->request;
365 * Utility method : get the Class name (used on templates)
367 * @return string class name
372 function getClass() {
373 return get_class($this);
376 * Utility method : get the Time taken to generate the page.
378 * @return string time in seconds..
384 $m = explode(' ',microtime());
385 $ff = HTML_FlexyFramework::get();
386 return sprintf('%0.2fs',($m[0] + $m[1]) - $ff->start)
387 . '/ Files ' . count(get_included_files());
392 * turn on off session - wrap long database queries or
393 * data processing with this to prevent locking
395 * @param int $state new session state - 0 = off, 1 = on
398 function sessionState($state)
400 static $ses_status = false;
402 // session status is only php5.4 and up..
403 if (!defined('PHP_SESSION_ACTIVE')) {
404 define('PHP_SESSION_ACTIVE' , 1);
406 if(!function_exists('session_status')){
409 $ses_status = ($ses_status === false) ? session_status() : $ses_status;
411 if (PHP_SESSION_ACTIVE != $ses_status) {
417 session_write_close();
421 ini_set('session.use_only_cookies', false);
422 ini_set('session.use_cookies', false);
423 ini_set('session.use_trans_sid', false);
424 ini_set('session.cache_limiter', null);
427 // sometimes raises a notice - ps_files_cleanup_dir.