* refactored the tsPage::[get|set]Request to tsUrl::[get|set]Request
[vsc.git] / _res / _libs / tspage.class.php
blobb015cfe5cf310a982c595ac4221aa36cffe15e95
1 <?php
2 /**
3 * @name tsPage
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
7 *
8 * @public the smarty template class instance and the theme path
9 * @private the page identification
12 class tsPage extends Output {
13 public $smarty,
14 $varArray = array(),
15 $themePath,
16 $db;
18 protected $content;
20 private $to,
21 $output,
22 $errors;
24 /**
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";
44 } else {
45 $this->errors = '';
47 ob_start();
49 $this->setTheme();
51 session_start();
53 $this->connectDb();
56 // function __destruct(){
57 //// $this->db->close();
58 // }
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);
65 return true;
68 return false;
71 /**
72 * function to redirect the client
74 * @param string $url
75 * @param bool $die
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);
81 } else {
82 ob_end_clean();
83 header ('Location: '.str_replace('&amp;', '&', $url));
86 if ($die)
87 die();
90 // FIXME: I should stop using REQUEST for getting get and post variables !!!
91 static public function getRequest($varName = null) {
92 if (is_null($varName))
93 return $_REQUEST;
94 if (isset($_REQUEST[$varName]))
95 return $_REQUEST[$varName];
96 else
97 return false;
101 * function to generate URLs compatible with tsPage
103 * @param string $whereTo the page name
104 * @param string $varVal additional params
105 * @param string $method [get|TODO post]
106 * @return string
108 static public function setRequest ($whereTo = null, $varVal = null){
109 $url = new tsUrl ();
111 return $url->setRequest ($whereTo = null, $varVal = null);
114 public function setTheme (){
115 $this->getTheme();
117 $this->themePath = THEME_PATH . $this->theme;
118 if (!is_dir($this->themePath)) {
119 $this->theme = DEFAULT_THEME;
120 $this->themePath = THEME_PATH . $this->theme;
123 $this->setThemeCookie();
126 private function setThemeCookie () {
127 if (empty ($_COOKIE['theme']) || $_COOKIE['theme'] != $this->theme) {
128 $test = setcookie ('theme', $this->theme, time()+1296000, '/');
132 private function reportErrors (){
133 if (!stristr (C_SYSTEM_DEBUG_IPS, $_SERVER['REMOTE_ADDR'])) {
134 return false;
136 if (!empty ($this->errors)) {
137 $errors = $this->errors;
138 $this->errors = '<!-- ERRORS: '.get_class($this).' at '.date('G:i:s').'-->'."\n".$errors;
141 if (defined ('ERROR_LEVEL') && ERROR_LEVEL == 9 )
142 $this->errors .= "\n".var_export (debug_backtrace(), true);
143 switch (C_SYSTEM_DEBUG_METHOD){
144 case 0: // output
145 $this->smarty->assign('errors', $this->errors);
146 return true;
147 break;
148 case 1: // email
149 $mailMsg = $this->errors;
150 // mail (DEBUG_MAIL,'DEBUG:',$mailMsg);
151 return true;
152 // break;
154 return false;
157 public function triggerError($errNo=0, $errStr='', $errFile='', $errLine=''){
158 if (empty($this->errors)){
159 $this->errors = "\n";
160 // if (is_a($this->smarty,'Smarty'))
161 // $this->smarty->assign('errors', var_export($this->errors));
163 switch ($errNo){
164 case (2):
165 $errType = 'WARNING';
166 break;
167 case (8):
168 $errType = 'NOTICE';
169 break;
170 case (256): // errNo = 4
171 $errType = 'USER_ERROR';
172 break;
173 case (512): // errNo = 8
174 $errType = 'USER_WARNING';
175 break;
176 case (1024): // errNo = 16
177 $errType = 'USER_NOTICE';
178 break;
179 case (2048): // errNo = 16
180 $errType = 'USER_ERROR';
181 break;
182 // case (4096): // errNo = 16
183 // $errType = '?';
184 // break;
185 default:
186 var_dump($errNo);
188 if ($errNo < 255) {
189 $baseColor = array('R'=>100,'G'=>16,'B'=>16);
190 $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)));
191 } else {
192 $baseColor = array('R'=>16,'G'=>16,'B'=>100);
193 $errNo = $errNo / 128;
194 $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)));
197 $this->errors .= '<span style="color:#'.strtoupper($color).'">';
198 $this->errors .= '<b>'.$errType.'</b> '.$errStr;
199 if (false || tsUrl::getRequest('bt') == 'full') {
200 $this->errors .= ' at line '.$errLine.' in file '.$errFile;
201 // $t = debug_backtrace();
202 // $this->errors .= '<pre>'.var_export($t,true).'</pre>';
205 $this->errors .= ' </span><br/>'."\n";
207 return;
210 private function setOutput (){
211 $userAgent = $_SERVER['HTTP_USER_AGENT'];
212 //return 'wml';
215 public function dispatch (){
216 $this->db->close();
217 $this->setTheme();
219 $this->smarty = &new Smarty();
221 $this->smarty = &new Smarty();
223 $this->smarty->compile_check = true;
224 // $this->smarty->debugging = C_SYSTEM_DEBUG;
225 $this->smarty->compile_dir = RES_PATH . S_C_TEMPL_DIR;
227 $this->smarty->assign('header', $this->themePath . DIRECTORY_SEPARATOR. S_TEMPL_DIR . 'header.tpl');
228 $this->smarty->assign('footer', $this->themePath . DIRECTORY_SEPARATOR. S_TEMPL_DIR . 'footer.tpl');
230 $this->smarty->assign('url', URL);
231 $this->smarty->assign('style', '');
233 $className = get_class ($this);
234 $methArr = get_class_methods ($this);
236 if (is_file (PAGE_PATH . $className . DIRECTORY_SEPARATOR . $className . '.tpl')) {
237 $this->smarty->assign('contentFile', PAGE_PATH . $className . DIRECTORY_SEPARATOR . $className . '.tpl');
238 } else {
239 $this->smarty->assign('contentFile', $this->themePath . DIRECTORY_SEPARATOR . S_TEMPL_DIR . 'tpl404.tpl');
242 /* Execution of any _exec method we have in the derived classes
243 --------------------------------------------------------------------- */
244 foreach ($methArr as $method){
245 if (stristr($method, '_exec')){
246 $this->$method();
249 /* Loading of any varArray template variables we have in the derived classes
250 --------------------------------------------------------------------- */
251 if (is_array($this->varArray)) {
252 $this->smarty->assign ($this->varArray);
255 // if(!empty($GLOBALS['errors'])) {
256 // $GLOBALS['errors'] = '.';
257 // }
259 if (!C_SYSTEM_DEBUG) {
260 ob_end_clean();
261 ob_start();
262 } else {
263 $this->errors .= ob_get_clean();
264 ob_start(); // clean output - heheh, yeah right :P
265 $this->reportErrors ();
268 // $this->smarty->assign ('time', true);
269 $this->content = $this->smarty->fetch ($this->themePath . DIRECTORY_SEPARATOR . S_TEMPL_DIR . 'main.tpl');
271 $memcache = Output::memcacheEnabled ();
272 if ($memcache instanceof Memcache) {
273 $memcache->connect('localhost', 11211);
274 $memcache->add (Output::getHash () , $this, MEMCACHE_COMPRESSED);
277 $this->postDispatch ($this->content);
281 private function postDispatch ($incString){
282 // later on we will do some string manipulation to have a bit _more_ control over the smarty output
283 // (for example smarty->fetch gives a lot of notices - bad templates) - maybe we want to catch them
284 // TODO LOSE SMARTY !! - php is enough for templating ... or so I heard ;)
285 if ( stristr($_SERVER['HTTP_ACCEPT'],'application/xhtml+xml') && !C_SYSTEM_DEBUG) {
286 header('Content-type: application/xhtml+xml');
287 } else {
288 header('Content-type: text/html');
292 echo str_replace(
293 array ('%TIME%','%QUERIES%', '%MEMUSED%'),
294 array (
295 number_format(microtime (true) - $GLOBALS['st'] , 5, ',', '.'),
296 $GLOBALS['qCnt'],
297 number_format(memory_get_usage()/1024, 3, ',', '.')
299 $incString