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
{
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 $this->smarty
->compile_check
= true;
40 // $this->smarty->debugging = C_SYSTEM_DEBUG;
41 $this->smarty
->compile_dir
= S_C_TEMPL_DIR
;
42 // set_error_handler(array($this, 'triggerError'));
43 // commented in the light of the xdebug profiling -
44 // dropped loading times by .01 seconds
45 if (!empty($prevOutput)) {
46 $this->errors
= "\n".'<!-- output previous init at '.date('G:i:s').' -->'."\n".$prevOutput."\n";
58 // function __destruct(){
59 // $this->db->close();
62 protected 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
);
73 public function setTheme (){
76 $this->themePath
= THEME_PATH
. $this->theme
;
77 if (!is_dir($this->themePath
)) {
78 $this->theme
= DEFAULT_THEME
;
79 $this->themePath
= THEME_PATH
. $this->theme
;
82 $this->setThemeCookie();
85 private function setThemeCookie () {
86 if (empty ($_COOKIE['theme']) ||
$_COOKIE['theme'] != $this->theme
) {
87 $test = setcookie ('theme', $this->theme
, time()+
1296000, '/');
91 private function reportErrors (){
92 if (!stristr (C_SYSTEM_DEBUG_IPS
, $_SERVER['REMOTE_ADDR'])) {
95 if (!empty ($this->errors
)) {
96 $errors = $this->errors
;
97 $this->errors
= '<!-- ERRORS: '.get_class($this).' at '.date('G:i:s').'-->'."\n".$errors;
100 if (defined ('ERROR_LEVEL') && ERROR_LEVEL
== 9 )
101 $this->errors
.= "\n".var_export (debug_backtrace(), true);
102 switch (C_SYSTEM_DEBUG_METHOD
){
104 $this->smarty
->assign('errors', $this->errors
);
108 $mailMsg = $this->errors
;
109 // mail (DEBUG_MAIL,'DEBUG:',$mailMsg);
116 public function triggerError($errNo=0, $errStr='', $errFile='', $errLine=''){
117 if (empty($this->errors
)){
118 $this->errors
= "\n";
119 // if (is_a($this->smarty,'Smarty'))
120 // $this->smarty->assign('errors', var_export($this->errors));
124 $errType = 'WARNING';
129 case (256): // errNo = 4
130 $errType = 'USER_ERROR';
132 case (512): // errNo = 8
133 $errType = 'USER_WARNING';
135 case (1024): // errNo = 16
136 $errType = 'USER_NOTICE';
138 case (2048): // errNo = 16
139 $errType = 'USER_ERROR';
141 // case (4096): // errNo = 16
148 $baseColor = array('R'=>100,'G'=>16,'B'=>16);
149 $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)));
151 $baseColor = array('R'=>16,'G'=>16,'B'=>100);
152 $errNo = $errNo / 128;
153 $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)));
156 $this->errors
.= '<span style="color:#'.strtoupper($color).'">';
157 $this->errors
.= '<b>'.$errType.'</b> '.$errStr;
158 if (false || tsController
::getRequest('bt') == 'full') {
159 $this->errors
.= ' at line '.$errLine.' in file '.$errFile;
160 $t = debug_backtrace();
161 // $this->errors .= '<pre>'.var_export($t,true).'</pre>';
164 $this->errors
.= ' </span><br/>'."\n";
168 public function dispatch (){
172 $this->smarty
->assign('header', $this->themePath
. DIRECTORY_SEPARATOR
. S_TEMPL_DIR
. 'header.tpl');
173 $this->smarty
->assign('footer', $this->themePath
. DIRECTORY_SEPARATOR
. S_TEMPL_DIR
. 'footer.tpl');
175 $this->smarty
->assign('url', URL
);
176 $this->smarty
->assign('style', '');
177 $this->smarty
->assign('contact', tsController
::setRequest('contact'));
179 $className = get_class ($this);
180 $methArr = get_class_methods ($this);
181 // echo PAGE_PATH . $className . DIRECTORY_SEPARATOR . $className . '.tpl';
182 if (is_file (PAGE_PATH
. $className . DIRECTORY_SEPARATOR
. $className . '.tpl')) {
183 $this->smarty
->assign ('contentFile', PAGE_PATH
. $className . DIRECTORY_SEPARATOR
. $className . '.tpl');
185 $this->smarty
->assign ('contentFile', $this->themePath
. DIRECTORY_SEPARATOR
. S_TEMPL_DIR
. 'tpl404.tpl');
188 /* Loading of any varArray template variables we have in the derived classes
189 --------------------------------------------------------------------- */
190 if (is_array($this->varArray
)) {
191 $this->smarty
->assign ($this->varArray
);
194 // if(!empty($GLOBALS['errors'])) {
195 // $GLOBALS['errors'] = '.';
198 if (!C_SYSTEM_DEBUG
) {
202 $this->errors
.= ob_get_clean();
203 ob_start(); // clean output - heheh, yeah right :P
204 $this->reportErrors ();
207 // $this->smarty->assign ('time', true);
208 $this->content
= $this->smarty
->fetch ($this->themePath
. DIRECTORY_SEPARATOR
. S_TEMPL_DIR
. 'main.tpl');
210 $memcache = tsController
::memcacheEnabled ();
211 if ($memcache instanceof Memcache
) {
212 $memcache->connect('localhost', 11211);
213 $memcache->add (tsController
::getHash () , $this, MEMCACHE_COMPRESSED
);
216 $this->postDispatch ($this->content
);
220 private function postDispatch ($incString){
221 // later on we will do some string manipulation to have a bit _more_ control over the smarty output
222 // (for example smarty->fetch gives a lot of notices - bad templates) - maybe we want to catch them
223 // TODO LOSE SMARTY !! - php is enough for templating ... or so I heard ;)
224 // check to see if we're a 404 error
225 if (get_class ($this) == 'tsControllerHtml') {// yes 404
226 header ("HTTP/1.0 404 Not Found");
229 if ( stristr($_SERVER['HTTP_ACCEPT'],'application/xhtml+xml') && !C_SYSTEM_DEBUG
) {
230 header('Content-type: application/xhtml+xml');
232 header('Content-type: text/html');
236 array ('%TIME%','%QUERIES%', '%MEMUSED%'),
238 number_format(microtime (true) - $GLOBALS['st'] , 5, ',', '.'),
240 number_format(memory_get_usage()/1024, 3, ',', '.')