* modifications related to enabling memcache and adding some expire headers
[vsc.git] / _res / _libs / tsurl.class.php
blob22b6743823c41da23cc8cb9dc986bf0dfdfb901e
1 <?php
2 /**
3 * TODO: if (stristr ($topic, '~')) {
4 * $topic = ??; // TBD
5 * $major = 'user';
6 * $title = substr($topic, 1);
7 * }
8 * FIXME: 1. the whole static idea sucks - no way around it so far
9 * 2. I need to think of a method like the Zend router objects for getting/setting urls
12 // will generate : http://base_url/topic/major/title/minor1/1/minor2/2
13 define ('FRIENDLY' , 1);
14 // will generate : http://base_url/?NAV_VAR=topic&major=title&minor1=1&minor2=2
15 define ('REGULAR' , 2);
16 define ('METHOD' , FRIENDLY);
18 /**
19 * Object to generate URLs
22 class tsUrl {
23 static public $instance;
25 public $options = array ();
27 /**
28 * The URL components
30 protected $to, // REGULAR : NAV_VAR=$topic | FRIENDLY : /$topic/
31 $action, // REGULAR : do=$action | FRIENDLY : /$topic/$action/
32 $major, // REGULAR : &amp;$major=$title
33 $title, // FRIENDLY : ../$major/$title
34 $minors; // TBD
36 public function __construct ($opt = null) {
37 if (is_null ($opt)) {
38 if (defined('BASE_URL'))
39 $options['base'] = BASE_URL;
40 else
41 $options['base'] = '';
46 public function __destruct () {
50 /**
51 * method to return the url as a string
54 public function __toString () {
58 /**
59 * method to generate an friendly URL slug
61 static public function buildSlug () {
62 // TODO
65 static function getValue ($string, $varName = '') {
66 if (stristr ($string, ':')) {
67 if (!stristr ($string, $varName)) {
68 return false;
70 if (stristr ($string,'/'))
71 substr ($string, 0, -1);
72 // returns what's right of $glue=: in $string
73 return str_replace ($varName . ':', '', $string);
75 // we have no var name on the left of glue
76 return $string;
79 static function getVarName ($string) {
80 if (stristr ($string, ':')) {
81 // returns what's left of $glue=: in $string (ie, the var name)
82 return str_replace (stristr ($string,':'), '', $string);
84 return null;
87 static public function getController () {
88 return tsUrl::getRequest (NAV_VAR);
91 static private function getFriendlyParams () {
92 $retArr = array ();
93 // var_dump(substr(stristr($_SERVER['REDIRECT_URI'], 'index.php/'), 10));die;
94 // this only works for lighttpd, for Apache we need to use $_SERVER['REDIRECT_URL']
95 if (stristr($_SERVER['SERVER_SOFTWARE'], 'Apache'))
96 $redirectUri = 'REDIRECT_URL';
97 elseif (stristr($_SERVER['SERVER_SOFTWARE'], 'lighttpd'))
98 $redirectUri = 'REDIRECT_URI';
99 else
100 $redirectUri = 'REDIRECT_URI';
102 $parArr = explode ('/', substr(stristr($_SERVER[$redirectUri], 'index.php/'), 10));
103 // array_shift($parArr);
104 $size = sizeof ($parArr);
105 // var_dump(explode ('/', substr(stristr($_SERVER['REDIRECT_URI'], 'index.php/'), 10)));
106 while ($size > 0) {
107 foreach ($parArr as $k => $cont) {
108 if (!empty ($k) || !empty ($cont)) {
109 $key = tsUrl::getVarName ($cont);
110 $retArr[$key ? $key : $k] = tsUrl::getValue ($cont, $key);
112 $size--;
116 // navigation variable - logically first
117 if (!empty ($retArr[0])) {
118 $retArr[NAV_VAR] = $retArr[0];
119 unset ($retArr[0]);
121 // action variable - right after
122 if (!empty ($retArr[1])) {
123 $retArr[ACT_VAR] = $retArr[1];
124 unset ($retArr[1]);
126 return $retArr;
129 static public function composeUrl ($incArr) {
130 $retStr = '';
131 if (!is_array ($incArr)) {
132 $incArr[] = '';
134 // FIXME: find a way around hard-coding the $glue and $separator
135 if (defined('METHOD') && METHOD == FRIENDLY) {
136 $glue = ':';
137 $separator = '/';
138 } else {
139 $glue = '=';
140 $separator = '&amp;';
143 foreach ($incArr as $key => $val) {
144 if (!empty($key) && !empty($val)) {
145 $retStr .= (($separator == '&amp;') || (($key != 'to') && ($key != 'do')) ? $key . $glue : '') . $val . $separator;
149 if ($separator == '&amp;') {
150 // to remove the final '&amp;'
151 $retStr = '/?'.substr ($retStr, 0, -5);
152 } else {
153 $retStr = '/' . $retStr;
156 return $retStr;
159 static private function getRegularParams () {
160 // $this->to = $_GET[NAV_VAR];
161 // dumb - this contradicts my previous idea of letting the $to object
162 // decide what to do with the URL params = less flexibility
163 // $this->action = $_GET[ACT_VAR];
164 return $_GET;
167 static public function getRequest ($varName = null) {
168 if (defined('METHOD') && METHOD == FRIENDLY) {
169 $params = tsUrl::getFriendlyParams ();
170 } else {
171 $params = tsUrl::getRegularParams ();
174 if (!$varName) {
175 return $params;
176 } elseif (!empty ($_REQUEST[$varName])) {
177 return $_REQUEST[$varName];
178 } elseif (!empty ($params[$varName])) {
179 return $params[$varName];
184 * method to return the URL based on $method
186 * TODO: find a method to overlook folders
187 * - done by hardcoding folder names in the rewrite rule
189 static public function setRequest ($whereTo = null, $varVal = array()) {
190 // var_dump($varVal);
191 // generating friendly URLs
192 // if (is_null ($whereTo))
193 // $to = tsUrl::getController ();
195 if (!is_array ($varVal)) {
196 $t[] = (int)$varVal;
197 $varVal = $t;
199 // var_dump($varVal);
200 $paramArr = array_merge (array ('to' => $whereTo) , $varVal);
201 return URL . tsUrl::composeUrl ($paramArr);