4 * @package html Output class
5 * @desc the God class. It does everything, from database initialization
6 * to error reporting and page buffering. <- yeah it might be stupid
8 * @public the smarty template class instance and the theme path
9 * @private the page identification
12 class tsPage
extends Output
{
25 * the constructor that does it all.
26 * starts output buffering, and initializes different things
29 public function __construct(){
30 $prevOutput = ob_get_clean(); // errors previous to the tsPage init
32 // $this->output = $this->setOutput();
33 // Override standard string functions
34 if (extension_loaded('mbstring')) {
35 ini_set('mbstring.func_overload', 7);
38 // set_error_handler(array(&$this, 'triggerError'));
39 // commented in the light of the xdebug profiling -
40 // dropped loading times by almost one order of magnitude
41 // maybe it was just my imagination
42 if (!empty($prevOutput)) {
43 $this->errors
= "\n".'<!-- output previous init at '.date('G:i:s').' -->'."\n".$prevOutput."\n";
56 // function __destruct(){
57 //// $this->db->close();
60 private function connectDb () {
61 $this->db
= sqlFactory
::connect(DB_TYPE
);
63 if (defined ('DB_NAME') && DB_NAME
&& !empty($this->db
->link
)) {
64 $this->db
->selectDatabase(DB_NAME
);
72 * function to redirect the client
76 * @param bool $onlyJScript
78 static public function redirect($url, $die = true, $onlyJScript = false) {
79 if ($onlyJScript == true ||
headers_sent()) {
80 printf ('<?xml version="1.0" encoding="utf-8"?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"><html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en"><head><title>Redirect</title><meta http-equiv="Refresh" content="0;url=%s" /></head><body onload="try {self.location.href=\'%s\' } catch(e) {}"><p><a href="%s">REDIRECTING - PLEASE CLICK HERE !</a></p></body></html>', $url, $url, $url);
83 header ('Location: '.str_replace ('&', '&', $url));
90 // FIXME: I should stop using REQUEST for getting get and post variables !!!
91 public function getRequest($varName = null) {
92 return tsUrl
::getRequest($varName);
96 * function to generate URLs compatible with tsPage
98 * @param string $whereTo the page name
99 * @param string $varVal additional params
100 * @param string $method [get|TODO post]
103 public function setRequest ($whereTo = null, $varVal = null){
104 return tsUrl
::setRequest ($whereTo, $varVal);
107 public function setTheme (){
110 $this->themePath
= THEME_PATH
. $this->theme
;
111 if (!is_dir($this->themePath
)) {
112 $this->theme
= DEFAULT_THEME
;
113 $this->themePath
= THEME_PATH
. $this->theme
;
116 $this->setThemeCookie();
119 private function setThemeCookie () {
120 if (empty ($_COOKIE['theme']) ||
$_COOKIE['theme'] != $this->theme
) {
121 $test = setcookie ('theme', $this->theme
, time()+
1296000, '/');
125 private function reportErrors (){
126 if (!stristr (C_SYSTEM_DEBUG_IPS
, $_SERVER['REMOTE_ADDR'])) {
129 if (!empty ($this->errors
)) {
130 $errors = $this->errors
;
131 $this->errors
= '<!-- ERRORS: '.get_class($this).' at '.date('G:i:s').'-->'."\n".$errors;
134 if (defined ('ERROR_LEVEL') && ERROR_LEVEL
== 9 )
135 $this->errors
.= "\n".var_export (debug_backtrace(), true);
136 switch (C_SYSTEM_DEBUG_METHOD
){
138 $this->smarty
->assign('errors', $this->errors
);
142 $mailMsg = $this->errors
;
143 // mail (DEBUG_MAIL,'DEBUG:',$mailMsg);
150 public function triggerError($errNo=0, $errStr='', $errFile='', $errLine=''){
151 if (empty($this->errors
)){
152 $this->errors
= "\n";
153 // if (is_a($this->smarty,'Smarty'))
154 // $this->smarty->assign('errors', var_export($this->errors));
158 $errType = 'WARNING';
163 case (256): // errNo = 4
164 $errType = 'USER_ERROR';
166 case (512): // errNo = 8
167 $errType = 'USER_WARNING';
169 case (1024): // errNo = 16
170 $errType = 'USER_NOTICE';
172 case (2048): // errNo = 16
173 $errType = 'USER_ERROR';
175 // case (4096): // errNo = 16
182 $baseColor = array('R'=>100,'G'=>16,'B'=>16);
183 $color = (string)dechex($baseColor['R']+
40 * (log($errNo, 2))).(string)dechex($baseColor['G']+
(20 - 4*log($errNo, 2))*(log($errNo, 2))).(string)dechex($baseColor['B']+
(20 - 4*log($errNo, 2))*(log($errNo, 2)));
185 $baseColor = array('R'=>16,'G'=>16,'B'=>100);
186 $errNo = $errNo / 128;
187 $color = (string)dechex($baseColor['R']+
(20 - 4*log($errNo, 2)) * (log($errNo, 2))).(string)dechex($baseColor['G']+
(20 - 4*log($errNo, 2))*(log($errNo, 2))).(string)dechex($baseColor['B']+
40*(log($errNo, 2)));
190 $this->errors
.= '<span style="color:#'.strtoupper($color).'">';
191 $this->errors
.= '<b>'.$errType.'</b> '.$errStr;
192 if (false || tsPage
::getRequest('bt') == 'full') {
193 $this->errors
.= ' at line '.$errLine.' in file '.$errFile;
194 // $t = debug_backtrace();
195 // $this->errors .= '<pre>'.var_export($t,true).'</pre>';
198 $this->errors
.= ' </span><br/>'."\n";
203 // private function setOutput (){
204 // $userAgent = $_SERVER['HTTP_USER_AGENT'];
208 public function dispatch (){
212 $this->smarty
= &new Smarty();
214 $this->smarty
= &new Smarty();
216 $this->smarty
->compile_check
= true;
217 // $this->smarty->debugging = C_SYSTEM_DEBUG;
218 $this->smarty
->compile_dir
= RES_PATH
. S_C_TEMPL_DIR
;
220 $this->smarty
->assign('header', $this->themePath
. DIRECTORY_SEPARATOR
. S_TEMPL_DIR
. 'header.tpl');
221 $this->smarty
->assign('footer', $this->themePath
. DIRECTORY_SEPARATOR
. S_TEMPL_DIR
. 'footer.tpl');
223 $this->smarty
->assign('url', URL
);
224 $this->smarty
->assign('style', '');
226 $className = get_class ($this);
227 $methArr = get_class_methods ($this);
229 if (is_file (PAGE_PATH
. $className . DIRECTORY_SEPARATOR
. $className . '.tpl')) {
230 $this->smarty
->assign('contentFile', PAGE_PATH
. $className . DIRECTORY_SEPARATOR
. $className . '.tpl');
232 $this->smarty
->assign('contentFile', $this->themePath
. DIRECTORY_SEPARATOR
. S_TEMPL_DIR
. 'tpl404.tpl');
235 /* Execution of any _exec method we have in the derived classes
236 --------------------------------------------------------------------- */
237 foreach ($methArr as $method){
238 if (stristr($method, '_exec')){
242 /* Loading of any varArray template variables we have in the derived classes
243 --------------------------------------------------------------------- */
244 if (is_array($this->varArray
)) {
245 $this->smarty
->assign ($this->varArray
);
248 // if(!empty($GLOBALS['errors'])) {
249 // $GLOBALS['errors'] = '.';
252 if (!C_SYSTEM_DEBUG
) {
256 $this->errors
.= ob_get_clean();
257 ob_start(); // clean output - heheh, yeah right :P
258 $this->reportErrors ();
261 // $this->smarty->assign ('time', true);
262 $this->content
= $this->smarty
->fetch ($this->themePath
. DIRECTORY_SEPARATOR
. S_TEMPL_DIR
. 'main.tpl');
264 $memcache = Output
::memcacheEnabled ();
265 if ($memcache instanceof Memcache
) {
266 $memcache->connect('localhost', 11211);
267 $memcache->add (Output
::getHash () , $this, MEMCACHE_COMPRESSED
);
270 $this->postDispatch ($this->content
);
274 private function postDispatch ($incString){
275 // later on we will do some string manipulation to have a bit _more_ control over the smarty output
276 // (for example smarty->fetch gives a lot of notices - bad templates) - maybe we want to catch them
277 // TODO LOSE SMARTY !! - php is enough for templating ... or so I heard ;)
278 if ( stristr($_SERVER['HTTP_ACCEPT'],'application/xhtml+xml') && !C_SYSTEM_DEBUG
) {
279 header('Content-type: application/xhtml+xml');
281 header('Content-type: text/html');
286 array ('%TIME%','%QUERIES%', '%MEMUSED%'),
288 number_format(microtime (true) - $GLOBALS['st'] , 5, ',', '.'),
290 number_format(memory_get_usage()/1024, 3, ',', '.')