From 4b50e948cd04cc7110c4be94f4718a1d1e0a7729 Mon Sep 17 00:00:00 2001 From: habarnam Date: Sat, 23 Aug 2008 13:12:53 +0300 Subject: [PATCH] * modifications related to enabling memcache and adding some expire headers --- _res/_libs/functions.inc.php | 66 +++++++++++++++------- _res/_libs/tdoabstract.class.php | 100 +++++++++++++++++---------------- _res/_libs/tscontroller.class.php | 51 +++++++++++++---- _res/_libs/tscontrollercss.class.php | 5 +- _res/_libs/tscontrollerhtml.class.php | 40 ++++++------- _res/_libs/tscontrollerimage.class.php | 63 +++++++++++---------- _res/_libs/tscontrollerrss.class.php | 6 +- _res/_libs/tspaginator.class.php | 3 +- _res/_libs/tsrouter.class.php | 37 ++++++------ _res/_libs/tsurl.class.php | 73 ++++++++++++------------ _res/schema.sql | 1 + 11 files changed, 257 insertions(+), 188 deletions(-) diff --git a/_res/_libs/functions.inc.php b/_res/_libs/functions.inc.php index 8636f0e..206a281 100644 --- a/_res/_libs/functions.inc.php +++ b/_res/_libs/functions.inc.php @@ -2,12 +2,40 @@ /** * the __autoload automagic function for class initialization, * see usingClass -* +* * @param string $className */ function __autoload ($className) { - usingClass($className); +// usingClass($className); + if (class_exists($className, false)) { + return true; + } + + $classNameLow = strtolower($className); + + $classPaths = array ( + $classNameLow . '.class.php', // regular homegrown class + $classNameLow . DIRECTORY_SEPARATOR .$classNameLow . '.class.php', // regular page + $classNameLow . DIRECTORY_SEPARATOR . $classNameLow.'.class.php', // imported + ); + + foreach ($classPaths as $classPath) { + $fileIncluded = @include($classPath); + + if ($fileIncluded) { + if (!class_exists($className, false)) { + trigger_error ('Unable to load class '.$className.' in path '.get_include_path().'.class.php', E_USER_ERROR); + return false; + } + return true; + } + } + + if (!$fileIncluded) { + trigger_error ('Not found file containing '. $className .'.', E_USER_ERROR); + return false; + } } /** @@ -18,36 +46,36 @@ function __autoload ($className) { function isDebug (){ if (stristr(C_SYSTEM_DEBUG_IPS, $_SERVER['REMOTE_ADDR']) && C_SYSTEM_DEBUG) return true; - - return false; + + return false; } if (!function_exists('usingClass')) { /** - * The php4 function for including the class file + * The php4 function for including the class file * * @param string $className - * @return bool + * @return bool */ function usingClass($className) { if (class_exists($className)) { return true; } - + $classNameLow = strtolower($className); - + $classPaths = array ( - $classNameLow . '.class.php', // regular homegrown class + $classNameLow . '.class.php', // regular homegrown class $classNameLow . DIRECTORY_SEPARATOR .$classNameLow . '.class.php', // regular page $classNameLow . DIRECTORY_SEPARATOR . $classNameLow.'.class.php', // imported ); foreach ($classPaths as $classPath) { $fileIncluded = @include($classPath); - + if ($fileIncluded) { - return true; + return true; } } @@ -55,7 +83,7 @@ if (!function_exists('usingClass')) { trigger_error ('Not found file containing '. $className .'.', E_USER_ERROR); return false; } - + if (!class_exists($className)) { trigger_error ('Unable to load class '.$className.' in path '.get_include_path().'.class.php', E_USER_ERROR); return false; @@ -73,7 +101,7 @@ function emailIsValid ($address) { } function getDirFiles ( $dir, $showHidden = false){ - + $files = array(); if (!is_dir($dir)){ trigger_error('Can not find : '.$dir); @@ -82,14 +110,14 @@ function getDirFiles ( $dir, $showHidden = false){ if ( $root = @opendir($dir) ){ while ($file = readdir ($root)){ if ( ($file == '.' || $file == '..') || ($showHidden == false && stripos($file, '.') === 0)){continue;} - + if( is_dir($dir.'/'.$file) ){ $files = array_merge($files, getDirFiles($dir.'/'.$file)); } else { /*if ( stristr($file, 'tpl') )*/ $files[] = $dir.'/'.$file; } } - } + } return $files; } @@ -100,13 +128,13 @@ function isDBLink($incData) { return false; } -if (!function_exists('mime_content_type')) { +if (!function_exists('mime_content_type')) { function mime_content_type ($filename) { $t = getimagesize($filename); - + if (is_array($t)) return $t['mime']; - else + else return 'application/unknown'; } -} +} diff --git a/_res/_libs/tdoabstract.class.php b/_res/_libs/tdoabstract.class.php index c3ddbf7..a7d1b60 100644 --- a/_res/_libs/tdoabstract.class.php +++ b/_res/_libs/tdoabstract.class.php @@ -11,16 +11,16 @@ class tdoAbstract { * @var mySqlIm $db */ public $db, - $wheres = array(), - $groups, -// $orders, - $limit, - $refers = array(); + $wheres = array(), + $groups, + // $orders, + $limit, + $refers = array(); protected $id, // public to allow debugging outside the class - $name, - $alias, - $fields; + $name, + $alias, + $fields; /** * @desc function to implement get_ | set_ virtual methods @@ -95,7 +95,7 @@ class tdoAbstract { $this->instantiateMembers(); } - + public function __destruct() {} /** @@ -115,9 +115,9 @@ class tdoAbstract { */ public function isValidMember ($incMember) { if ( - ! ($incMember instanceof tdoAbstractField ) || - // this prevents an error in php > 5.2 object comparison - ! in_array ($incMember, $this->get_members() /**/, true/**/) + ! ($incMember instanceof tdoAbstractField ) || + // this prevents an error in php > 5.2 object comparison + ! in_array ($incMember, $this->get_members() /**/, true/**/) ) { return false; } else @@ -226,7 +226,7 @@ class tdoAbstract { public function get ($id) { $id = $this->db->escape($id); - + $this->instantiateMembers(); $this->id->set_value ($id); @@ -278,6 +278,8 @@ class tdoAbstract { $valueStr = ''; $f = $this->get_members(); +// $sql .= ' '.$this->outputRefers().' SET'; + foreach ($f as $fieldName => $field) { if ($this->isValidMember ($field) && ($field != $this->id) && !is_null ($field->value)) { $value = $this->escape ($field->value); @@ -309,7 +311,7 @@ class tdoAbstract { if (is_array ($this->refers) && !empty ($this->refers)){ $this->refers = array_reverse ($this->refers); - + foreach ($this->refers as $ref) $sql .= $ref; } @@ -322,7 +324,7 @@ class tdoAbstract { if (($field instanceof tdoAbstractField) && $field != $this->id) { // TODO: make a more real check for field is an id $value = $field->value; } - + if ((isset($value) && !is_null($value))) { $sql.= $field->table.'.'.$fieldName.' = '.$this->escape ($value).', '; } @@ -353,7 +355,7 @@ class tdoAbstract { // TODO: make this the same way the find first method works public function delete ($id = null) { $id = (!is_null ($id) ? $id :$this->id->value); - + if (!is_null ($id)) { // no need for other wheres $this->wheres = array(); @@ -367,7 +369,7 @@ class tdoAbstract { $temp = $this->alias; $this->set_alias(null); - + $sql = 'DELETE FROM '.$this->name.' WHERE '; //$this->id->name.' = '.$this->escape($value).' LIMIT 1'; $sql.= $this->outputWheres(); // echo $sql;die; @@ -401,11 +403,11 @@ class tdoAbstract { $this->id->set_modifier ('COUNT(%s)'); $t = sprintf ($this->id->modifier, $this->id->name); - + $sql = $this->db->_SELECT ($t). $this->db->_FROM ($this->name). $this->db->_WHERE ($this->id->name.' = '.$this->escape($id)); - + $this->db->query($sql); if ($this->db->getScalar()) { @@ -433,7 +435,7 @@ class tdoAbstract { } else { trigger_error ($fieldName . ' is not a valid member of ' . get_class($this)); -// throw new Exception ($fieldName . ' is not a valid member of ' . get_class($this)); +// throw new Exception ($fieldName . ' is not a valid member of ' . get_class($this)); return false; } } @@ -451,7 +453,7 @@ class tdoAbstract { $f = $this->get_members (); foreach ($f as $field) { if (!is_null ($field->group) ) { - $groups .= (!empty($groups) ? ', ' : '') . + $groups .= (!empty($groups) ? ', ' : '') . $field->table . '.' . $field->name; } } @@ -464,8 +466,8 @@ class tdoAbstract { $f = $this->get_members(); foreach ($f as $field) if (!is_null($field->order)) { - $orders .= (!empty($orders) ? ', ': '') . - $field->table . '.' . $field->name . + $orders .= (!empty($orders) ? ', ': '') . + $field->table . '.' . $field->name . ($field->order == true ? ' ASC' : ' DESC'); } return $orders; @@ -503,7 +505,7 @@ class tdoAbstract { $this->wheres[] = new tdoAbstractClause ($t); } } - + /** * function to add an abstract clause to the current object if it doesn't exist * @@ -541,14 +543,14 @@ class tdoAbstract { /** * @param int $start - * @param int $count - */ - + * @param int $count + */ + public function addLimit ($start = 0, $count=null) { if (empty($this->limit)) $this->limit = $this->db->_LIMIT ($start, $count); } - + /** * building a normal SELECT query * @@ -611,11 +613,11 @@ class tdoAbstract { $this->db->query($this->buildSql()); return $this->db->getScalar(); } - + public function getCount() { // this is bad: // it takes into account the counting rows in many2many table relationshit - // but it does not for more than one group by + // but it does not for more than one group by foreach ($this->get_members() as $fieldName => $field) { if ($field->get_group() == true) { $what = 'DISTINCT(' . $field->table .'.' . $fieldName . ')'; @@ -626,7 +628,7 @@ class tdoAbstract { $what = '*'; // made it a bit more clean and less sql portable by adding hard coded - // MY SQL stuff + // MY SQL stuff $sql = $this->db->_SELECT (' COUNT(' . $what . ') '); $sql .= $this->db->_FROM( $this->name. $this->db->_AS($this->alias) ); @@ -636,14 +638,14 @@ class tdoAbstract { $sql .= $this->outputRefers(); $sql .= $this->db->_WHERE ($this->outputWheres()); - - // this would need to be replaced with a count(distinct(group by column)) - // because we're using many2many relations + + // this would need to be replaced with a count(distinct(group by column)) + // because we're using many2many relations // also I do not know how this behaves for: // 1. multiple group by's // 2. !many2many relations. // $sql .= $this->db->_GROUP ($this->outputGroups()); - + $this->db->query($sql); return $this->db->getScalar(); } @@ -687,7 +689,7 @@ class tdoAbstract { foreach ($incOb->refers as $alias => $ref) { $tAl = $refs++; - + $this->refers[$tAl] = $ref;//str_replace(array($alias, $aliases[$alias][1]), array($tAl, $aliases[$alias][2]), $ref); $this->refers[$tAl]->set_state ($tAl); } @@ -716,7 +718,7 @@ class tdoAbstract { return; } else { $incOb->set_alias($tAl); - + if($thisJField == null || !($thisJField instanceof tdoAbstractField)) $thisJField = $this->id; @@ -776,11 +778,11 @@ class tdoAbstractClause { if (($subject instanceof tdoAbstractField ) || ($subject instanceof tdoAbstractClause )) { $this->subject = &$subject; - + $subject->set_where = true; - + $this->predicative = &$predicative; - + if ($this->validPredicative ($predicate)) $this->predicate = $predicate; @@ -788,7 +790,7 @@ class tdoAbstractClause { $this->subject = ''; $this->predicate = ''; $this->predicative = ''; - + return; } } @@ -822,7 +824,7 @@ class tdoAbstractClause { if ($this->predicate == 'LIKE') { $preStr = '%'.$this->predicate.'%'; } - + $preStr = (stripos($preStr, '"') !== 0 ? '"'.$preStr.'"' : $preStr);//'"'.$preStr.'"'; } elseif (is_array($this->predicative)) { $preStr = '("'.implode('", "',$this->predicative).'")'; @@ -915,7 +917,7 @@ class tdoAbstractJoin { ); protected $state, $type, - + $leftTable, $rightTable, @@ -952,11 +954,11 @@ class tdoAbstractJoin { public function __toString() { $lAlias = $this->leftTable->get_alias(); - + return (string)$this->type.' JOIN '.$this->rightTable->get_name(). ' AS t'.$this->state.' ON '.(isset($lAlias) ? $lAlias : $this->leftTable->get_name()). '.'.$this->leftField->name. - ' = t'.$this->state.'.'.$this->rightField->name.' '; + ' = t'.$this->state.'.'.$this->rightField->name.' '; } /** @@ -1009,8 +1011,8 @@ define ('FULLTEXT', 8); class tdoAbstractField { static public $validTypes = array ( 'VARCHAR', - 'INT', - 'DATE', + 'INT', + 'DATE', 'TEXT', 'FLOAT', 'TIMESTAMP', @@ -1029,10 +1031,10 @@ class tdoAbstractField { public function __set ( $key, $value ) { if ( array_key_exists ($key, get_object_vars($this)) ) { $this->$key = $value; - + // if ($key == 'where') // var_dump($this); - + if ( is_null ($this->type) ) $this->setType(); diff --git a/_res/_libs/tscontroller.class.php b/_res/_libs/tscontroller.class.php index bb0eb99..ee4cbb2 100644 --- a/_res/_libs/tscontroller.class.php +++ b/_res/_libs/tscontroller.class.php @@ -8,7 +8,7 @@ abstract class tsController { static private $instance = null; static protected function memcacheEnabled () { - /// this sucks on so many ways + // this sucks on so many ways if (extension_loaded ('memcache') && C_USE_MEMCACHE == true) { $memcache = new Memcache; if (@$memcache->connect('localhost', 11211)) { @@ -52,7 +52,7 @@ abstract class tsController { * @return string */ - public function setRequest ($whereTo = null, $varVal = null){ + public function setRequest ($whereTo = null, $varVal = array()){ if (!empty ($this->params)) { if (!empty ($whereTo)) array_shift ($this->params); @@ -94,11 +94,12 @@ abstract class tsController { } else { // the memcache object should also be a singleton // with a static instance I can call wherever. - $memcache = tsController::memcacheEnabled (); - if ( $memcache) { - tsController::$instance = $memcache->get ( tsController::getHash ()); + if (extension_loaded ('Memcache')) { + $memcache = tsController::memcacheEnabled (); + if ( $memcache) { + tsController::$instance = $memcache->get ( tsController::getHash ()); + } } - if (!(tsController::$instance instanceof tsController)) { tsController::$instance = new $to (); } @@ -107,7 +108,34 @@ abstract class tsController { } static public function getHash () { - return md5($_SERVER['HTTP_HOST'].$_SERVER['QUERY_STRING']); + if (empty ($_SERVER['QUERY_STRING'])) { + if (stristr($_SERVER['SERVER_SOFTWARE'], 'Apache')) + $redirectUri = 'REDIRECT_URL'; + elseif (stristr($_SERVER['SERVER_SOFTWARE'], 'lighttpd')) + $redirectUri = 'REDIRECT_URI'; + else + $redirectUri = 'REDIRECT_URI'; + $qstring = $_SERVER[$redirectUri]; + } else { + $qstring = $_SERVER['QUERY_STRING']; + } + return md5($_SERVER['HTTP_HOST'] . $qstring); + } + + private function setHeaders () { + // set expire header. + header ('Expires: '.strftime ('%a, %d %m %Y %T GMT', time()+2419200)); // one month + } + + private function addToCache () { + if (!extension_loaded ('Memcache')) + return false; + $memcache = tsController::memcacheEnabled (); + $hash = tsController::getHash (); + if ($memcache instanceof Memcache && !$memcache->get ($hash)) { + $memcache->connect('localhost', 11211); + $memcache->add ($hash , $this, MEMCACHE_COMPRESSED); + } } public function getTheme(){ @@ -121,8 +149,8 @@ abstract class tsController { } if (empty($this->theme) || !is_dir(THEME_PATH.$this->theme) || - !is_dir(THEME_PATH.$this->theme.DIRECTORY_SEPARATOR."_css") || - !is_dir(THEME_PATH.$this->theme.DIRECTORY_SEPARATOR."_templates") + !is_dir(THEME_PATH.$this->theme . DIRECTORY_SEPARATOR . "_css") || + !is_dir(THEME_PATH.$this->theme . DIRECTORY_SEPARATOR . "_templates") ) { $this->theme = DEFAULT_THEME; } @@ -134,5 +162,8 @@ abstract class tsController { public function __destruct () {} - abstract public function dispatch (); + public function dispatch () { + $this->setHeaders(); + $this->addToCache(); + } } diff --git a/_res/_libs/tscontrollercss.class.php b/_res/_libs/tscontrollercss.class.php index f8a3bc4..310f289 100644 --- a/_res/_libs/tscontrollercss.class.php +++ b/_res/_libs/tscontrollercss.class.php @@ -17,7 +17,7 @@ class tsControllerCss extends tsController { public function __construct(){ ob_start(); - $this->theme = (!empty($_COOKIE['theme']) ? $_COOKIE['theme'] : DEFAULT_THEME); + $this->theme = (!empty($_COOKIE['theme']) ? $_COOKIE['theme'] : DEFAULT_THEME); $this->media = tsController::getRequest('do'); @@ -32,6 +32,7 @@ class tsControllerCss extends tsController { } public function dispatch (){ + parent::dispatch (); header('Content-type: text/css'); $out = file_get_contents ( THEME_PATH. $this->theme . DIRECTORY_SEPARATOR .'_css'. DIRECTORY_SEPARATOR .$this->media.'.css'); $this->postDispatch ( $out ); @@ -39,6 +40,8 @@ class tsControllerCss extends tsController { private function postDispatch ($incString){ + // set expire header. + header ('Expires: '.strftime ('%a, %d %m %Y %T GMT', time()+2419200)); // one month if (stristr ($_SERVER['HTTP_USER_AGENT'], 'MSIE')) { $incString = str_replace(array('min-width:', 'max-width:'), 'width:', $incString); } diff --git a/_res/_libs/tscontrollerhtml.class.php b/_res/_libs/tscontrollerhtml.class.php index f2d312f..f51be84 100644 --- a/_res/_libs/tscontrollerhtml.class.php +++ b/_res/_libs/tscontrollerhtml.class.php @@ -36,9 +36,6 @@ class tsControllerHtml extends tsController { } $this->smarty = new Smarty(); - $this->smarty->compile_check = true; -// $this->smarty->debugging = C_SYSTEM_DEBUG; - $this->smarty->compile_dir = S_C_TEMPL_DIR; // set_error_handler(array($this, 'triggerError')); // commented in the light of the xdebug profiling - // dropped loading times by .01 seconds @@ -51,15 +48,15 @@ class tsControllerHtml extends tsController { $this->setTheme(); // get the persistent url params (NAV_VAR and ACT_VAR) - $to = tsController::getRequest(NAV_VAR); - if (!empty($to)) - $persistent[NAV_VAR] = $to; - - $do = tsController::getRequest(ACT_VAR); - if (!empty($do)) - $persistent[ACT_VAR] = $do; - - $this->setParams($persistent); +// $to = tsController::getRequest(NAV_VAR); +// if (!empty($to)) +// $persistent[NAV_VAR] = $to; +// +// $do = tsController::getRequest(ACT_VAR); +// if (!empty($do)) +// $persistent[ACT_VAR] = $do; +// +// $this->setParams($persistent); session_start(); $this->connectDb(); @@ -179,6 +176,14 @@ class tsControllerHtml extends tsController { $this->db->close(); $this->setTheme(); + parent::dispatch (); + if (!($this->smarty instanceof Smarty)) + $this->smarty = new Smarty(); + + $this->smarty->compile_check = true; +// $this->smarty->debugging = C_SYSTEM_DEBUG; + $this->smarty->compile_dir = S_C_TEMPL_DIR; + $this->smarty->assign('header', $this->themePath . DIRECTORY_SEPARATOR. S_TEMPL_DIR . 'header.tpl'); $this->smarty->assign('footer', $this->themePath . DIRECTORY_SEPARATOR. S_TEMPL_DIR . 'footer.tpl'); @@ -217,26 +222,17 @@ class tsControllerHtml extends tsController { // $this->smarty->assign ('time', true); $this->content = $this->smarty->fetch ($this->themePath . DIRECTORY_SEPARATOR . S_TEMPL_DIR . 'main.tpl'); - $memcache = tsController::memcacheEnabled (); - if ($memcache instanceof Memcache) { - $memcache->connect('localhost', 11211); - $memcache->add (tsController::getHash () , $this, MEMCACHE_COMPRESSED); - } - $this->postDispatch ($this->content); } private function postDispatch ($incString){ - // later on we will do some string manipulation to have a bit _more_ control over the smarty output - // (for example smarty->fetch gives a lot of notices - bad templates) - maybe we want to catch them - // TODO LOSE SMARTY !! - php is enough for templating ... or so I heard ;) // check to see if we're a 404 error if (get_class ($this) == 'tsControllerHtml') {// yes 404 header ("HTTP/1.0 404 Not Found"); } - if ( stristr($_SERVER['HTTP_ACCEPT'],'application/xhtml+xml') && !C_SYSTEM_DEBUG) { + if ( stristr($_SERVER['HTTP_ACCEPT'],'application/xhtml+xml') && !C_SYSTEM_DEBUG ) { header('Content-type: application/xhtml+xml'); } else { header('Content-type: text/html'); diff --git a/_res/_libs/tscontrollerimage.class.php b/_res/_libs/tscontrollerimage.class.php index 7445524..5c50bd1 100644 --- a/_res/_libs/tscontrollerimage.class.php +++ b/_res/_libs/tscontrollerimage.class.php @@ -11,20 +11,20 @@ class tsControllerImage extends tsController { private $content, $imgAttr, $hashedPath = null; - + public function __construct () { ob_start(); $this->time['start'] = microtime (true); $this->path = $this->getPath (); } - + protected function getPath () { if (empty($this->basePath)) { $this->basePath = PAGE_PATH; }/* else { - $this->basePath .= DIRECTORY_SEPARATOR . 'images' . DIRECTORY_SEPARATOR; + $this->basePath .= DIRECTORY_SEPARATOR . 'images' . DIRECTORY_SEPARATOR; }*/ - + // list ($to, $do, $file) = tsController::getRequest(); // var_dump( $this->basePath);die; @@ -38,18 +38,18 @@ class tsControllerImage extends tsController { // die; return false; } - + public function __destruct (){ if ( !empty($this->content) ) imagedestroy($this->content); } - + public function testPath ($incPath = null) { if ($incPath == null) $incPath = $this->path; - + $ext = end(explode('.', $incPath)); - + if (in_array ($ext, tsControllerImage::$allowedExtensions)){ $realPath = realpath ($incPath); if (file_exists($realPath)){ @@ -59,7 +59,7 @@ class tsControllerImage extends tsController { } return false; } - + public function getType(){ $this->imgAttr = getimagesize ($this->path); switch ($this->imgAttr[2]) { @@ -78,23 +78,23 @@ class tsControllerImage extends tsController { break; } } - + public function outputHash(){ $retVal = false; - + if (tsControllerImage::$allowedExtensions[$this->imgAttr[2]] == 'jpg') $type = 'jpeg'; - else + else $type = tsControllerImage::$allowedExtensions[$this->imgAttr[2]]; - + $functionName = 'image'.$type; - + if (!is_file($this->hashedPath) && !empty($this->content)) { header ('Content-type: image/'.$type); $retVal = $functionName($this->content, $this->hashedPath); } - - + + return $retVal; } public function resize($w, $h = null, $method = 'scale'){ @@ -104,8 +104,8 @@ class tsControllerImage extends tsController { // check cache if (!empty($w)) $this->hashedPath = S_C_TEMPL_DIR.md5($this->path.$w.$h); - -// die(print_r($this->hashedPath)); + +// die(print_r($this->hashedPath)); if (is_file($this->hashedPath)) { $imgAttr = getimagesize($this->hashedPath); // if ($w == $imgAttr[0] && $h == $imgAttr[1] && !empty($allowedExtensions[$imgAttr[2]])) { @@ -116,13 +116,13 @@ class tsControllerImage extends tsController { // } } else { $temp = $this->content; - + if (empty($h)) // only width... we will make it proportional $h = round($this->imgAttr[1]/($this->imgAttr[0]/$w)); - + $this->content = imagecreatetruecolor ($w, $h); $retVal = imagecopyresampled ($this->content, $temp, 0, 0, 0, 0, $w, $h, $this->imgAttr[0], $this->imgAttr[1]); - + $this->outputHash(); } if (!empty($retVal)) { @@ -131,33 +131,34 @@ class tsControllerImage extends tsController { $this->content = $temp; return false; } - } - + } + public function dispatch (){ + parent::dispatch (); ob_end_clean(); - + $this->getType(); - + if (!is_array($this->imgAttr)) trigger_error ($this->path.' is not an image', E_USER_ERROR); if (tsControllerImage::$allowedExtensions[$this->imgAttr[2]] == 'jpg') $type = 'jpeg'; - else + else $type = tsControllerImage::$allowedExtensions[$this->imgAttr[2]]; - + $functionName = 'image'.$type; - header ('Content-type: '.$this->imgAttr['mime']); + header ('Content-type: '.$this->imgAttr['mime']); if (!empty($w)) $this->hashedPath = S_C_TEMPL_DIR . md5($this->path.$w.$h); - + if (!is_file($this->hashedPath)) { ob_start(); $retVal = $functionName($this->content); $imageData = ob_get_contents(); echo $imageDataLength = ob_get_length(); ob_end_clean(); - + header ('Content-Length: ' . $imageDataLength); echo $imageData.'Not Hashed'; } else { @@ -168,7 +169,7 @@ class tsControllerImage extends tsController { $this->time['end'] = microtime (true); // return $retVal; } - + public function debug(){ ob_end_clean(); $this->time['end'] = microtime (true); diff --git a/_res/_libs/tscontrollerrss.class.php b/_res/_libs/tscontrollerrss.class.php index b121268..2678baf 100644 --- a/_res/_libs/tscontrollerrss.class.php +++ b/_res/_libs/tscontrollerrss.class.php @@ -29,7 +29,9 @@ class tsControllerRss extends tsControllerHtml { $this->db->close(); $this->setTheme(); - $this->smarty = new Smarty(); + parent::dispatch (); +// if (!($this->smarty instanceof Smarty)) +// $this->smarty = new Smarty(); $this->smarty->compile_check = false; // $this->smarty->debugging = C_SYSTEM_DEBUG; @@ -51,6 +53,8 @@ class tsControllerRss extends tsControllerHtml { protected function postDispatch ($incString) { header ('Content-Type: application/rss+xml'); + // set expire header. + header ('Expires: '.strftime ('%a, %d %m %Y %T GMT', time()+2419200)); // one month echo str_replace( array ('%TIME%','%QUERIES%', '%MEMUSED%'), array ( diff --git a/_res/_libs/tspaginator.class.php b/_res/_libs/tspaginator.class.php index e49e229..fb979a0 100644 --- a/_res/_libs/tspaginator.class.php +++ b/_res/_libs/tspaginator.class.php @@ -43,7 +43,8 @@ class tsPaginator { else $retVal .= $i.' '; } - + + // this will be replaced by the helper functionality of using templates :D if ($this->page > 1) $left = '« '; else diff --git a/_res/_libs/tsrouter.class.php b/_res/_libs/tsrouter.class.php index d8df2b8..4f52c9b 100644 --- a/_res/_libs/tsrouter.class.php +++ b/_res/_libs/tsrouter.class.php @@ -3,56 +3,57 @@ class tsRouter { protected $routes = array(), $defaultRoute; - - public function __construct (){ - // adding default routes for css and rss -// $this->addRoute ('rss'); -// $this->addRoute ('style'); + + public function __construct () { } - + public function addRoute ($ctrlName, $incRoute = null, $default = null) { if (empty ($ctrlName)) { return false; } - + if (empty ($incRoute)) $incRoute = '/'.$ctrlName.'/'; - + if ($default) $this->defaultRoute = $ctrlName; - + $this->routes[$ctrlName] = $incRoute; + return true; } - + public function getController () { $to = urldecode(tsUrl::getRequest(NAV_VAR)); // var_dump($_GET, $to); if ($this->validController ($to)) { - return new $to; + return tsController::getInstance ($to); } // die; foreach ($this->routes as $ctrlName => $regex) { // var_dump($to, $ctrlName, $regex,preg_match('/'.$ctrlName.'/i', $to), preg_match($regex, $to,$a)); - if ((preg_match('/'.$ctrlName.'/i', $to) || preg_match($regex, $to)) && $this->validController ($ctrlName)) { - return new $ctrlName; - } + if ((preg_match('/'.$ctrlName.'/i', $to) || preg_match($regex, $to)) && $this->validController ($ctrlName)) { + return tsController::getInstance ( $ctrlName); + } } // die; if ($this->validController ($this->defaultRoute)) return new $this->defaultRoute; + // try index if it exists + if ($this->validController ('index')) + return tsController::getInstance ('index'); // die; } - + public function validController ($ctrlName) { $ctrlName = strtolower($ctrlName); // var_dump($ctrlName); - + if (!empty($ctrlName) && is_dir(PAGE_PATH . $ctrlName) && is_file (PAGE_PATH . $ctrlName . DIRECTORY_SEPARATOR . $ctrlName . '.class.php')) { return true; - } - + } + return false; } } diff --git a/_res/_libs/tsurl.class.php b/_res/_libs/tsurl.class.php index ef8b7c0..22b6743 100644 --- a/_res/_libs/tsurl.class.php +++ b/_res/_libs/tsurl.class.php @@ -1,15 +1,15 @@ $val) { if (!empty($key) && !empty($val)) { $retStr .= (($separator == '&') || (($key != 'to') && ($key != 'do')) ? $key . $glue : '') . $val . $separator; } } - + if ($separator == '&') { // to remove the final '&' $retStr = '/?'.substr ($retStr, 0, -5); } else { $retStr = '/' . $retStr; } - + return $retStr; } @@ -161,9 +161,9 @@ class tsUrl { // dumb - this contradicts my previous idea of letting the $to object // decide what to do with the URL params = less flexibility // $this->action = $_GET[ACT_VAR]; - return $_GET; + return $_GET; } - + static public function getRequest ($varName = null) { if (defined('METHOD') && METHOD == FRIENDLY) { $params = tsUrl::getFriendlyParams (); @@ -172,28 +172,29 @@ class tsUrl { } if (!$varName) { - return $params; + return $params; } elseif (!empty ($_REQUEST[$varName])) { return $_REQUEST[$varName]; - } elseif (!empty ($params[$varName])) { + } elseif (!empty ($params[$varName])) { return $params[$varName]; } } - + /** * method to return the URL based on $method - * - * TODO: find a method to overlook folders - * - done by hardcoding folder names in the rewrite rule + * + * TODO: find a method to overlook folders + * - done by hardcoding folder names in the rewrite rule */ - static public function setRequest ($whereTo = null, $varVal = null) { + static public function setRequest ($whereTo = null, $varVal = array()) { // var_dump($varVal); // generating friendly URLs // if (is_null ($whereTo)) // $to = tsUrl::getController (); - - if (is_null($varVal) && !is_array ($varVal)) { - $varVal[] = (int)$varVal; + + if (!is_array ($varVal)) { + $t[] = (int)$varVal; + $varVal = $t; } // var_dump($varVal); $paramArr = array_merge (array ('to' => $whereTo) , $varVal); diff --git a/_res/schema.sql b/_res/schema.sql index 7a811dd..24700df 100644 --- a/_res/schema.sql +++ b/_res/schema.sql @@ -122,6 +122,7 @@ INSERT INTO `options` (`option_id`, `option_name`, `option_value`, `option_modif CREATE TABLE IF NOT EXISTS `posts` ( `post_id` int(10) NOT NULL auto_increment, + `post_parent` INT(10) NULL DEFAULT NULL, `post_time` timestamp NOT NULL default CURRENT_TIMESTAMP, `post_title` varchar(250) collate utf8_unicode_ci NOT NULL, `post_author` int(11) NOT NULL, -- 2.11.4.GIT