* modifications related to enabling memcache and adding some expire headers
[vsc.git] / _res / _libs / tscontroller.class.php
blobee4cbb2631214c2273eb8c69f973d2724d7d661b
1 <?php
2 abstract class tsController {
3 public $theme = null,
4 $params = array(),
5 $cookieSet = false,
6 $time = array('start' => 0,'end' => 0);
8 static private $instance = null;
10 static protected function memcacheEnabled () {
11 // this sucks on so many ways
12 if (extension_loaded ('memcache') && C_USE_MEMCACHE == true) {
13 $memcache = new Memcache;
14 if (@$memcache->connect('localhost', 11211)) {
15 memcache_debug (true);
16 return $memcache;
19 return false;
22 /**
23 * function to redirect the client
25 * @param string $url
26 * @param bool $die
27 * @param bool $onlyJScript
29 static public function redirect ($url, $die = true, $onlyJScript = false) {
30 if ($onlyJScript == true || headers_sent()) {
31 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);
32 } else {
33 ob_end_clean();
34 header ('Location: '.str_replace ('&amp;', '&', $url));
37 if ($die)
38 die();
41 // FIXME: I should stop using REQUEST for getting get and post variables !!!
42 static public function getRequest($varName = null) {
43 return tsUrl::getRequest($varName);
46 /**
47 * function to generate URLs compatible with tsController
49 * @param string $whereTo the page name
50 * @param string $varVal additional params
51 * @param string $method [get|TODO post]
52 * @return string
55 public function setRequest ($whereTo = null, $varVal = array()){
56 if (!empty ($this->params)) {
57 if (!empty ($whereTo))
58 array_shift ($this->params);
59 array_merge ($this->params, $varVal);
62 return tsUrl::setRequest ($whereTo, $varVal);
65 /**
66 * function to set the persistent URL parameters
67 * @param array $incArray
69 public function setParams ($incArray = array ()) {
70 if (!empty ($incArray))
71 $this->params = array_merge ($this->params, $incArray);
73 return false;
76 public function getParams () {
77 return $this->params;
80 public function store () {
84 /**
85 * static method to return the tsController singleton instance
87 * @param string $to
88 * @return tsController
90 static function getInstance ($to) {
91 // we have already initialized the page singleton
92 if (tsController::$instance instanceof tsController) {
93 return tsController::$instance;
94 } else {
95 // the memcache object should also be a singleton
96 // with a static instance I can call wherever.
97 if (extension_loaded ('Memcache')) {
98 $memcache = tsController::memcacheEnabled ();
99 if ( $memcache) {
100 tsController::$instance = $memcache->get ( tsController::getHash ());
103 if (!(tsController::$instance instanceof tsController)) {
104 tsController::$instance = new $to ();
107 return tsController::$instance;
110 static public function getHash () {
111 if (empty ($_SERVER['QUERY_STRING'])) {
112 if (stristr($_SERVER['SERVER_SOFTWARE'], 'Apache'))
113 $redirectUri = 'REDIRECT_URL';
114 elseif (stristr($_SERVER['SERVER_SOFTWARE'], 'lighttpd'))
115 $redirectUri = 'REDIRECT_URI';
116 else
117 $redirectUri = 'REDIRECT_URI';
118 $qstring = $_SERVER[$redirectUri];
119 } else {
120 $qstring = $_SERVER['QUERY_STRING'];
122 return md5($_SERVER['HTTP_HOST'] . $qstring);
125 private function setHeaders () {
126 // set expire header.
127 header ('Expires: '.strftime ('%a, %d %m %Y %T GMT', time()+2419200)); // one month
130 private function addToCache () {
131 if (!extension_loaded ('Memcache'))
132 return false;
133 $memcache = tsController::memcacheEnabled ();
134 $hash = tsController::getHash ();
135 if ($memcache instanceof Memcache && !$memcache->get ($hash)) {
136 $memcache->connect('localhost', 11211);
137 $memcache->add ($hash , $this, MEMCACHE_COMPRESSED);
141 public function getTheme(){
142 // this should most probably be changed to tsController::getRequest ('theme')
143 if (!empty($_GET['theme']) && isDebug()) {
144 $this->theme = $_GET['theme'];
145 } elseif (!empty($_POST['theme'])) {
146 $this->theme = $_POST['theme'];
147 } elseif (!empty($_COOKIE['theme'])) {
148 $this->theme = $_COOKIE['theme'];
150 if (empty($this->theme) ||
151 !is_dir(THEME_PATH.$this->theme) ||
152 !is_dir(THEME_PATH.$this->theme . DIRECTORY_SEPARATOR . "_css") ||
153 !is_dir(THEME_PATH.$this->theme . DIRECTORY_SEPARATOR . "_templates")
155 $this->theme = DEFAULT_THEME;
158 return $this->theme;
161 public function __construct() {}
163 public function __destruct () {}
165 public function dispatch () {
166 $this->setHeaders();
167 $this->addToCache();