* modifications related to enabling memcache and adding some expire headers
[vsc.git] / _res / _libs / tscontrollerhtml.class.php
blobf51be8402e7df61d4072371790c133e0196b36df
1 <?php
2 /**
3 * @name tsController
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 tsControllerHtml extends tsController {
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 tsController init
32 // $this->output = $this->setOutput();
33 // Override standard string functions
34 if (extension_loaded('mbstring')) {
35 ini_set('mbstring.func_overload', 7);
37 $this->smarty = new Smarty();
39 // set_error_handler(array($this, 'triggerError'));
40 // commented in the light of the xdebug profiling -
41 // dropped loading times by .01 seconds
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();
50 // get the persistent url params (NAV_VAR and ACT_VAR)
51 // $to = tsController::getRequest(NAV_VAR);
52 // if (!empty($to))
53 // $persistent[NAV_VAR] = $to;
55 // $do = tsController::getRequest(ACT_VAR);
56 // if (!empty($do))
57 // $persistent[ACT_VAR] = $do;
59 // $this->setParams($persistent);
61 session_start();
62 $this->connectDb();
65 // function __destruct(){
66 // $this->db->close();
67 // }
69 protected function connectDb () {
70 $this->db = sqlFactory::connect (DB_TYPE);
72 if (defined ('DB_NAME') && DB_NAME && !empty($this->db->link)) {
73 $this->db->selectDatabase(DB_NAME);
74 return true;
77 return false;
80 public function setTheme (){
81 $this->getTheme();
83 $this->themePath = THEME_PATH . $this->theme;
84 if (!is_dir($this->themePath)) {
85 $this->theme = DEFAULT_THEME;
86 $this->themePath = THEME_PATH . $this->theme;
89 $this->setThemeCookie();
92 private function setThemeCookie () {
93 if (empty ($_COOKIE['theme']) || $_COOKIE['theme'] != $this->theme) {
94 $test = setcookie ('theme', $this->theme, time()+1296000, '/');
98 private function reportErrors (){
99 if (!stristr (C_SYSTEM_DEBUG_IPS, $_SERVER['REMOTE_ADDR'])) {
100 return false;
102 if (!empty ($this->errors)) {
103 $errors = $this->errors;
104 $this->errors = '<!-- ERRORS: '.get_class($this).' at '.date('G:i:s').'-->'."\n".$errors;
107 if (defined ('ERROR_LEVEL') && ERROR_LEVEL == 9 )
108 $this->errors .= "\n".var_export (debug_backtrace(), true);
109 switch (C_SYSTEM_DEBUG_METHOD){
110 case 0: // output
111 $this->smarty->assign('errors', $this->errors);
112 return true;
113 break;
114 case 1: // email
115 $mailMsg = $this->errors;
116 // mail (DEBUG_MAIL,'DEBUG:',$mailMsg);
117 return true;
118 // break;
120 return false;
123 public function triggerError($errNo=0, $errStr='', $errFile='', $errLine=''){
124 if (empty($this->errors)){
125 $this->errors = "\n";
126 // if (is_a($this->smarty,'Smarty'))
127 // $this->smarty->assign('errors', var_export($this->errors));
129 switch ($errNo){
130 case (2):
131 $errType = 'WARNING';
132 break;
133 case (8):
134 $errType = 'NOTICE';
135 break;
136 case (256): // errNo = 4
137 $errType = 'USER_ERROR';
138 break;
139 case (512): // errNo = 8
140 $errType = 'USER_WARNING';
141 break;
142 case (1024): // errNo = 16
143 $errType = 'USER_NOTICE';
144 break;
145 case (2048): // errNo = 16
146 $errType = 'USER_ERROR';
147 break;
148 // case (4096): // errNo = 16
149 // $errType = '?';
150 // break;
151 default:
152 var_dump($errNo);
154 if ($errNo < 255) {
155 $baseColor = array('R'=>100,'G'=>16,'B'=>16);
156 $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)));
157 } else {
158 $baseColor = array('R'=>16,'G'=>16,'B'=>100);
159 $errNo = $errNo / 128;
160 $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)));
163 $this->errors .= '<span style="color:#'.strtoupper($color).'">';
164 $this->errors .= '<b>'.$errType.'</b> '.$errStr;
165 if (false || tsController::getRequest('bt') == 'full') {
166 $this->errors .= ' at line '.$errLine.' in file '.$errFile;
167 $t = debug_backtrace();
168 // $this->errors .= '<pre>'.var_export($t,true).'</pre>';
171 $this->errors .= ' </span><br/>'."\n";
173 return;
175 public function dispatch (){
176 $this->db->close();
177 $this->setTheme();
179 parent::dispatch ();
180 if (!($this->smarty instanceof Smarty))
181 $this->smarty = new Smarty();
183 $this->smarty->compile_check = true;
184 // $this->smarty->debugging = C_SYSTEM_DEBUG;
185 $this->smarty->compile_dir = S_C_TEMPL_DIR;
187 $this->smarty->assign('header', $this->themePath . DIRECTORY_SEPARATOR. S_TEMPL_DIR . 'header.tpl');
188 $this->smarty->assign('footer', $this->themePath . DIRECTORY_SEPARATOR. S_TEMPL_DIR . 'footer.tpl');
190 $this->smarty->assign('url', URL);
191 $this->smarty->assign('style', '');
192 $this->smarty->assign('contact', tsController::setRequest('contact'));
194 $className = get_class ($this);
195 $methArr = get_class_methods ($this);
196 // echo PAGE_PATH . $className . DIRECTORY_SEPARATOR . $className . '.tpl';
197 if (is_file (PAGE_PATH . $className . DIRECTORY_SEPARATOR . $className . '.tpl')) {
198 $this->smarty->assign ('contentFile', PAGE_PATH . $className . DIRECTORY_SEPARATOR . $className . '.tpl');
199 } else {
200 $this->smarty->assign ('contentFile', $this->themePath . DIRECTORY_SEPARATOR . S_TEMPL_DIR . 'tpl404.tpl');
203 /* Loading of any varArray template variables we have in the derived classes
204 --------------------------------------------------------------------- */
205 if (is_array($this->varArray)) {
206 $this->smarty->assign ($this->varArray);
209 // if(!empty($GLOBALS['errors'])) {
210 // $GLOBALS['errors'] = '.';
211 // }
213 if (!C_SYSTEM_DEBUG) {
214 ob_end_clean();
215 ob_start();
216 } else {
217 $this->errors .= ob_get_clean();
218 ob_start(); // clean output - heheh, yeah right :P
219 $this->reportErrors ();
222 // $this->smarty->assign ('time', true);
223 $this->content = $this->smarty->fetch ($this->themePath . DIRECTORY_SEPARATOR . S_TEMPL_DIR . 'main.tpl');
225 $this->postDispatch ($this->content);
229 private function postDispatch ($incString){
230 // check to see if we're a 404 error
231 if (get_class ($this) == 'tsControllerHtml') {// yes 404
232 header ("HTTP/1.0 404 Not Found");
235 if ( stristr($_SERVER['HTTP_ACCEPT'],'application/xhtml+xml') && !C_SYSTEM_DEBUG ) {
236 header('Content-type: application/xhtml+xml');
237 } else {
238 header('Content-type: text/html');
241 echo str_replace (
242 array ('%TIME%','%QUERIES%', '%MEMUSED%'),
243 array (
244 number_format(microtime (true) - $GLOBALS['st'] , 5, ',', '.'),
245 $GLOBALS['qCnt'],
246 number_format(memory_get_usage()/1024, 3, ',', '.')
248 $incString