* the basic functionality of memcache, but I am _not_ satisfied with this at all
[vsc.git] / _res / _libs / tspage.class.php
blobcc613411b53e55690940060636dce335d47c73e1
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){
92 if (isset($_REQUEST[$varName]))
93 return $_REQUEST[$varName];
94 else
95 return false;
98 /**
99 * function to generate URLs compatible with tsPage
101 * @param string $whereTo the page name
102 * @param string $varVal additional params
103 * @param string $method [get|TODO post]
104 * @return string
106 static public function setRequest ($whereTo = null, $varVal = null, $method = 'get'){
107 if (empty($whereTo))
108 return URL;
110 if (is_array($varVal)) {
111 foreach ($varVal as $key => $val){
112 if (is_numeric($key) && stristr ($val, '#')) {
113 // probably an href with id in it
114 $end = $val;
115 } else {
116 $tArr[] = $key.(!empty($val) ? '='.$val : '');
119 $retUrl = implode ('&amp;', $tArr).$end;
120 } elseif (is_string($varVal)){
121 $retUrl = $varVal;
124 $outStr = URL;
126 if ($method == 'get' && !empty($whereTo)){
127 $outStr .= '/?' . NAV_VAR . '=' . $whereTo.(!empty($retUrl) ? '&amp;'.$retUrl : '');
130 return $outStr;
133 public function setTheme (){
134 $this->getTheme();
136 $this->themePath = THEME_PATH . $this->theme;
137 if (!is_dir($this->themePath)) {
138 $this->theme = DEFAULT_THEME;
139 $this->themePath = THEME_PATH . $this->theme;
142 $this->setThemeCookie();
145 private function setThemeCookie () {
146 if (empty ($_COOKIE['theme']) || $_COOKIE['theme'] != $this->theme) {
147 $test = setcookie ('theme', $this->theme, time()+1296000, '/');
151 private function reportErrors (){
152 if (!stristr (C_SYSTEM_DEBUG_IPS, $_SERVER['REMOTE_ADDR'])) {
153 return false;
155 if (!empty ($this->errors)) {
156 $errors = $this->errors;
157 $this->errors = '<!-- ERRORS: '.get_class($this).' at '.date('G:i:s').'-->'."\n".$errors;
160 if (defined ('ERROR_LEVEL') && ERROR_LEVEL == 9 )
161 $this->errors .= "\n".var_export (debug_backtrace(), true);
162 switch (C_SYSTEM_DEBUG_METHOD){
163 case 0: // output
164 $this->smarty->assign('errors', $this->errors);
165 return true;
166 break;
167 case 1: // email
168 $mailMsg = $this->errors;
169 // mail (DEBUG_MAIL,'DEBUG:',$mailMsg);
170 return true;
171 // break;
173 return false;
176 public function triggerError($errNo=0, $errStr='', $errFile='', $errLine=''){
177 if (empty($this->errors)){
178 $this->errors = "\n";
179 // if (is_a($this->smarty,'Smarty'))
180 // $this->smarty->assign('errors', var_export($this->errors));
182 switch ($errNo){
183 case (2):
184 $errType = 'WARNING';
185 break;
186 case (8):
187 $errType = 'NOTICE';
188 break;
189 case (256): // errNo = 4
190 $errType = 'USER_ERROR';
191 break;
192 case (512): // errNo = 8
193 $errType = 'USER_WARNING';
194 break;
195 case (1024): // errNo = 16
196 $errType = 'USER_NOTICE';
197 break;
198 case (2048): // errNo = 16
199 $errType = 'USER_ERROR';
200 break;
201 // case (4096): // errNo = 16
202 // $errType = '?';
203 // break;
204 default:
205 var_dump($errNo);
207 if ($errNo < 255) {
208 $baseColor = array('R'=>100,'G'=>16,'B'=>16);
209 $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)));
210 } else {
211 $baseColor = array('R'=>16,'G'=>16,'B'=>100);
212 $errNo = $errNo / 128;
213 $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)));
216 $this->errors .= '<span style="color:#'.strtoupper($color).'">';
217 $this->errors .= '<b>'.$errType.'</b> '.$errStr;
218 if (false || tsPage::getRequest('bt') == 'full') {
219 $this->errors .= ' at line '.$errLine.' in file '.$errFile;
220 // $t = debug_backtrace();
221 // $this->errors .= '<pre>'.var_export($t,true).'</pre>';
224 $this->errors .= ' </span><br/>'."\n";
226 return;
229 private function setOutput (){
230 $userAgent = $_SERVER['HTTP_USER_AGENT'];
231 //return 'wml';
234 public function dispatch (){
235 $this->db->close();
236 $this->setTheme();
238 $this->smarty = &new Smarty();
240 $this->smarty->compile_check = true;
241 // $this->smarty->debugging = C_SYSTEM_DEBUG;
242 $this->smarty->compile_dir = RES_PATH . S_C_TEMPL_DIR;
244 $this->smarty->assign('header', $this->themePath . DIRECTORY_SEPARATOR. S_TEMPL_DIR . 'header.tpl');
245 $this->smarty->assign('footer', $this->themePath . DIRECTORY_SEPARATOR. S_TEMPL_DIR . 'footer.tpl');
247 $this->smarty->assign('url', URL);
248 $this->smarty->assign('style', '');
250 $className = get_class ($this);
251 $methArr = get_class_methods ($this);
253 if (is_file (PAGE_PATH . $className . DIRECTORY_SEPARATOR . $className . '.tpl')) {
254 $this->smarty->assign('contentFile', PAGE_PATH . $className . DIRECTORY_SEPARATOR . $className . '.tpl');
255 } else {
256 $this->smarty->assign('contentFile', $this->themePath . DIRECTORY_SEPARATOR . S_TEMPL_DIR . 'tpl404.tpl');
259 /* Execution of any _exec method we have in the derived classes
260 --------------------------------------------------------------------- */
261 foreach ($methArr as $method){
262 if (stristr($method, '_exec')){
263 $this->$method();
266 /* Loading of any varArray template variables we have in the derived classes
267 --------------------------------------------------------------------- */
268 if (is_array($this->varArray)) {
269 $this->smarty->assign ($this->varArray);
272 // if(!empty($GLOBALS['errors'])) {
273 // $GLOBALS['errors'] = '.';
274 // }
276 if (!C_SYSTEM_DEBUG) {
277 ob_end_clean();
278 ob_start();
279 } else {
280 $this->errors .= ob_get_clean();
281 ob_start(); // clean output - heheh, yeah right :P
282 $this->reportErrors ();
285 // $this->smarty->assign ('time', true);
287 $this->content = $this->smarty->fetch($this->themePath . DIRECTORY_SEPARATOR . S_TEMPL_DIR . 'main.tpl');
289 $memcache = Output::memcacheEnabled();
290 if ($memcache instanceof Memcache) {
291 $memcache->connect('localhost', 11211);
292 $memcache->add (get_class($this), $this);
295 $this->postDispatch ($this->content);
299 private function postDispatch ($incString){
301 // later on we will do some string manipulation to have a bit _more_ control over the smarty output
302 // (for example smarty->fetch gives a lot of notices - bad templates) - maybe we want to catch them
303 // TODO LOSE SMARTY !! - php is enough for templating ... or so I heard ;)
304 if ( stristr($_SERVER['HTTP_ACCEPT'],'application/xhtml+xml') && !C_SYSTEM_DEBUG) {
305 header('Content-type: application/xhtml+xml');
306 } else {
307 header('Content-type: text/html');
311 echo str_replace(
312 array ('%TIME%','%QUERIES%', '%MEMUSED%'),
313 array (
314 number_format(microtime (true) - $GLOBALS['st'] , 5, ',', '.'),
315 $GLOBALS['qCnt'],
316 number_format(memory_get_usage()/1024, 3, ',', '.')
318 $incString