Merge branch 'master' into users
[vsc.git] / _res / _libs / tspage.class.php
blob1bfc74bfc3facbe0b1435ba2edf10013d1c2a2a4
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;
17 private $to,
18 $output,
19 $errors,
20 $content;
22 /**
23 * the constructor that does it all.
24 * starts output buffering, and initializes different things
27 public function __construct(){
28 $prevOutput = ob_get_clean(); // errors previous to the tsPage init
30 // $this->output = $this->setOutput();
31 // Override standard string functions
32 if (extension_loaded('mbstring')) {
33 ini_set('mbstring.func_overload', 7);
36 $this->time['start'] = microtime (true);
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 $this->smarty = &new Smarty();
53 session_start();
55 $this->connectDb();
58 // function __destruct(){
59 //// $this->db->close();
60 // }
62 private function connectDb () {
63 $this->db = sqlFactory::connect(DB_TYPE);
65 if (defined ('DB_NAME') && DB_NAME && !empty($this->db->link)) {
66 $this->db->selectDatabase(DB_NAME);
67 return true;
70 return false;
73 /**
74 * function to redirect the client
76 * @param string $url
77 * @param bool $die
78 * @param bool $onlyJScript
80 static public function redirect($url, $die = true, $onlyJScript = false) {
81 if ($onlyJScript == true || headers_sent()) {
82 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 } else {
84 ob_end_clean();
85 header ('Location: '.str_replace('&amp;', '&', $url));
88 if ($die)
89 die();
92 // FIXME: I should stop using REQUEST for getting get and post variables !!!
93 static public function getRequest($varName){
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, $method = 'get'){
109 if (empty($whereTo))
110 return URL;
112 if (is_array($varVal)) {
113 foreach ($varVal as $key => $val){
114 $tArr[] = $key.(!empty($val) ? '='.$val : '');
115 $retUrl = implode ('&amp;',$tArr);
117 } elseif (is_string($varVal)){
118 $retUrl = $varVal;
121 $outStr = URL;
123 if ($method == 'get' && !empty($whereTo)){
124 $outStr .= '/?' . NAV_VAR . '=' . $whereTo.(!empty($retUrl) ? '&amp;'.$retUrl : '');
127 return $outStr;
130 public function setTheme (){
131 $this->getTheme();
133 $this->themePath = THEME_PATH . $this->theme;
134 if (!is_dir($this->themePath)) {
135 $this->theme = DEFAULT_THEME;
136 $this->themePath = THEME_PATH . $this->theme;
139 $this->setThemeCookie();
142 private function setThemeCookie () {
143 if (empty ($_COOKIE['theme']) || $_COOKIE['theme'] != $this->theme) {
144 $test = setcookie ('theme', $this->theme, time()+1296000, '/');
148 private function reportErrors (){
149 if (!stristr (C_SYSTEM_DEBUG_IPS, $_SERVER['REMOTE_ADDR'])) {
150 return false;
152 if (!empty ($this->errors)) {
153 $errors = $this->errors;
154 $this->errors = '<!-- ERRORS: '.get_class($this).' at '.date('G:i:s').'-->'."\n".$errors;
157 if (defined ('ERROR_LEVEL') && ERROR_LEVEL == 9 )
158 $this->errors .= "\n".var_export (debug_backtrace(), true);
159 switch (C_SYSTEM_DEBUG_METHOD){
160 case 0: // output
161 $this->smarty->assign('errors', $this->errors);
162 return true;
163 break;
164 case 1: // email
165 $mailMsg = $this->errors;
166 // mail (DEBUG_MAIL,'DEBUG:',$mailMsg);
167 return true;
168 // break;
170 return false;
173 public function triggerError($errNo=0, $errStr='', $errFile='', $errLine=''){
174 if (empty($this->errors)){
175 $this->errors = "\n";
176 // if (is_a($this->smarty,'Smarty'))
177 // $this->smarty->assign('errors', var_export($this->errors));
179 switch ($errNo){
180 case (2):
181 $errType = 'WARNING';
182 break;
183 case (8):
184 $errType = 'NOTICE';
185 break;
186 case (256): // errNo = 4
187 $errType = 'USER_ERROR';
188 break;
189 case (512): // errNo = 8
190 $errType = 'USER_WARNING';
191 break;
192 case (1024): // errNo = 16
193 $errType = 'USER_NOTICE';
194 break;
195 case (2048): // errNo = 16
196 $errType = 'USER_ERROR';
197 break;
198 // case (4096): // errNo = 16
199 // $errType = '?';
200 // break;
201 default:
202 var_dump($errNo);
204 if ($errNo < 255) {
205 $baseColor = array('R'=>100,'G'=>16,'B'=>16);
206 $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)));
207 } else {
208 $baseColor = array('R'=>16,'G'=>16,'B'=>100);
209 $errNo = $errNo / 128;
210 $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)));
213 $this->errors .= '<span style="color:#'.strtoupper($color).'">';
214 $this->errors .= '<b>'.$errType.'</b> '.$errStr;
215 if (false || tsPage::getRequest('bt') == 'full') {
216 $this->errors .= ' at line '.$errLine.' in file '.$errFile;
217 // $t = debug_backtrace();
218 // $this->errors .= '<pre>'.var_export($t,true).'</pre>';
221 $this->errors .= ' </span><br/>'."\n";
223 return;
226 private function setOutput (){
227 $userAgent = $_SERVER['HTTP_USER_AGENT'];
228 //return 'wml';
231 public function dispatch (){
232 $this->db->close();
233 $this->setTheme();
235 $this->smarty->compile_check = true;
236 // $this->smarty->debugging = C_SYSTEM_DEBUG;
237 $this->smarty->compile_dir = RES_PATH . S_C_TEMPL_DIR;
239 $this->smarty->assign('header', $this->themePath . DIRECTORY_SEPARATOR. S_TEMPL_DIR . 'header.tpl');
240 $this->smarty->assign('footer', $this->themePath . DIRECTORY_SEPARATOR. S_TEMPL_DIR . 'footer.tpl');
242 $this->smarty->assign('url', URL);
243 $this->smarty->assign('style', '');
245 $className = get_class($this);
246 $methArr = get_class_methods($this);
248 if (is_file(PAGE_PATH . $className . DIRECTORY_SEPARATOR . $className . '.tpl')) {
249 $this->smarty->assign('contentFile', PAGE_PATH . $className . DIRECTORY_SEPARATOR . $className . '.tpl');
250 } else {
251 $this->smarty->assign('contentFile', $this->themePath . DIRECTORY_SEPARATOR . S_TEMPL_DIR . 'tpl404.tpl');
254 /* Execution of any _exec method we have in the derived classes
255 --------------------------------------------------------------------- */
256 foreach ($methArr as $method){
257 if (stristr($method, '_exec')){
258 $this->$method();
261 /* Loading of any varArray template variables we have in the derived classes
262 --------------------------------------------------------------------- */
263 if (is_array($this->varArray)) {
264 $this->smarty->assign ($this->varArray);
267 // if(!empty($GLOBALS['errors'])) {
268 // $GLOBALS['errors'] = '.';
269 // }
271 if (!C_SYSTEM_DEBUG) { // no debug - FIXED
272 ob_end_clean();
273 ob_start();
274 } else {
275 $this->errors .= ob_get_clean();
276 ob_start(); // clean output - heheh, yeah right :P
277 $this->reportErrors();
280 $this->smarty->assign('time', true);//number_format(($this->time['end'] - $this->time['start']) * 1000 , 2, ',' , '.'));
281 // echo $this->themePath . DIRECTORY_SEPARATOR . S_TEMPL_DIR . 'main.tpl';
282 $this->content = $this->smarty->fetch($this->themePath . DIRECTORY_SEPARATOR . S_TEMPL_DIR . 'main.tpl');
283 // var_dump($this->theme);//die;
284 $this->postParse($this->content);
287 private function postParse($incString){
288 // later on we will do some string manipulation to have a bit _more_ control over the smarty output
289 // (for example smarty->fetch gives a lot of notices - bad templates) - maybe we want to catch them
290 // TODO LOSE SMARTY !! - php is enough for templating ... or so I heard ;)
291 if ( stristr($_SERVER['HTTP_ACCEPT'],'application/xhtml+xml') && !C_SYSTEM_DEBUG) {
292 header('Content-type: application/xhtml+xml');
293 } else {
294 header('Content-type: text/html');
296 $this->time['end'] = microtime (true);
297 echo str_replace(array('%TIME%','%QUERIES%'), array(number_format($this->time['end'] - $this->time['start'] , 5, ',', '.'), $GLOBALS['qCnt']), $incString);