00001 <?
00002 class MContext
00003 {
00004 const DISPATCH = 1;
00005 const MODULE = 3;
00006 const ACTION = 5;
00007 const DELIMITER = 6;
00008 const TYPE = 7;
00009
00010 public $manager;
00011 public $module;
00012 public $action;
00013 public $item;
00014 private $actionTokens;
00015 private $currentToken;
00016 private $path;
00017 private $queryString;
00018 private $vars;
00019 private $host;
00020 private $dispatch;
00021 public $style;
00022 public $isFile;
00023 public $isRoot;
00024 public $fileName;
00025 public $fileArea;
00026 public $fileType;
00027 public $scramble;
00028
00029
00030 public function __construct($url = '', $style = 0, $scramble = false)
00031 {
00032 global $MIOLO;
00033
00034 $this->dispatch = $MIOLO->getConf('options.dispatch');
00035
00036 if (empty($url))
00037 {
00038 $httpHost = $_SERVER['HTTP_HOST'];
00039 $scriptName = $_SERVER['SCRIPT_NAME'];
00040 $pathInfo = $_SERVER['PATH_INFO'];
00041 $queryString = $_SERVER['QUERY_STRING'];
00042 $url = "http://$httpHost$scriptName$pathInfo";
00043 if ($queryString != '')
00044 {
00045 $url .= "?{$queryString}";
00046 }
00047 }
00048 $this->style = (int)$style;
00049 $this->scramble = (bool)$scramble;
00050 if (strpos($url,'MIOLO_URI'))
00051 {
00052 $url = $this->parseScramble($url);
00053 }
00054 $this->parseUrl($url);
00055
00056 if(!$this->module)
00057 {
00058 $this->module = $MIOLO->getConf('options.startup');
00059 }
00060 if(!$this->action)
00061 {
00062 $this->action = 'main';
00063 $this->GetTokens();
00064 }
00065 }
00066
00067 private function parseScramble($url)
00068 {
00069 global $MIOLO;
00070 $urlParts = parse_url($url);
00071 parse_str($urlParts['query'], $this->vars);
00072 $url = preg_replace('/\?MIOLO_URI=.*/',$MIOLO->UnScramble($this->vars['MIOLO_URI']), $url);
00073 return $url;
00074 }
00075
00076 private function parseUrl($url)
00077 {
00078 $url = str_replace('&', '&', $url);
00079
00080 $urlParts = parse_url($url);
00081 $this->path = $urlParts['path'];
00082 $this->host = 'http:
00083 if (($this->queryString = $urlParts['query']) != '')
00084 {
00085 parse_str($this->queryString, $this->vars);
00086 }
00087 if (($this->path != "/{$this->dispatch}") || ($this->queryString != ''))
00088 {
00089 $this->style = (strpos($url, 'module') ? 0 : 1);
00090 $this->parseURI();
00091 }
00092 }
00093
00094 private function parseURI()
00095 { global $MIOLO;
00096 $uri = trim($this->path) . (($this->queryString != '') ? '?' . $this->queryString : '');
00097 $regexp = ($this->style == 0) ?
00098 "\/({$this->dispatch})(\?)module=([^&]*)(&?action=([^&.]*)(&|\.|$)(.*)|$)" :
00099 "\/({$this->dispatch})(/?)([^\/]*)(/([^&.]*)(&|\.|$)(.*)|$)";
00100 $this->isFile = false;
00101 if (ereg($regexp, $uri, $parts))
00102 {
00103 $this->dispatch = $parts[self::DISPATCH];
00104 $this->module = $parts[self::MODULE];
00105 $this->action = str_replace('/',':', $parts[self::ACTION]);
00106 $this->delimiter = $parts[self::DELIMITER];
00107 $this->fileType = $parts[self::TYPE];
00108 $this->GetTokens();
00109 }
00110 else
00111 {
00112 if ( $MIOLO->getConf('options.dispatch.ignore') == 'true' )
00113 {
00114
00115
00116 }
00117 else
00118 {
00119 die ('invalid URL - regexp: '.$uri);
00120 }
00121 }
00122 }
00123
00124 private function GetTokens()
00125 {
00126 $this->actionTokens = explode(':', $this->action);
00127 $this->currentToken = 0;
00128
00129 if ($this->delimiter == '.')
00130 {
00131 $this->fileArea = array_shift($this->actionTokens);
00132 $fileName = array_pop($this->actionTokens);
00133
00134 if ($fileName != NULL)
00135 {
00136 $this->isRoot = ($this->module == 'miolo');
00137 $this->isFile = true;
00138 $path = implode('/', $this->actionTokens);
00139 $this->fileName = ($path != '' ? '/' . $path . '/' : '/') . $fileName . '.' . $this->fileType;
00140 }
00141 else
00142 {
00143 die ('invalid URL - filetype');
00144 }
00145 }
00146 }
00147
00148 function SetStyle($value = 0)
00149 {
00150 $this->style = $value;
00151 }
00152
00153 function GetStyle()
00154 {
00155 return $this->style;
00156 }
00157
00158 function GetAction($index = 0)
00159 {
00160 $action = ($index >= 0) && ($index < count($this->actionTokens)) ? $this->actionTokens[$index] : NULL;
00161
00162 return $action;
00163 }
00164
00165 function GetVar($name)
00166 {
00167 return $this->vars[$name];
00168 }
00169
00170 function ShiftAction()
00171 {
00172 $action = $this->currentToken < count($this->actionTokens) ? $this->actionTokens[$this->currentToken++] : null;
00173 return $action;
00174 }
00175
00176 function PushAction($a)
00177 {
00178 if ($this->action)
00179 $this->action .= '/';
00180
00181 $this->action .= $a;
00182
00183 $this->actionTokens = explode('/', $this->action);
00184 $this->currentToken = 0;
00185 }
00186
00187 function ComposeURL($dispatch = '', $module = '', $action = '', $args = '', $scramble = false)
00188 {
00189 global $MIOLO;
00190
00191 $dispatch = ($dispatch == '') ? $this->dispatch : $dispatch;
00192 $module = ($module == '') ? $this->module : $module;
00193 $action = ($action == '') ? (($this->action == '') ? 'main' : $this->action) : $action;
00194
00195 $amp = '&';
00196 if ($this->style)
00197 {
00198 $action = str_replace(':', '/', $action);
00199 $url = "/$module/$action" . ($args ? '?' . $args : '');
00200 }
00201 else
00202 {
00203 $url = "?module=$module" . $amp . "action=$action" . ($args ? $amp . $args : '');
00204 }
00205
00206 if ($this->scramble || $scramble)
00207 {
00208 $url = "$dispatch?MIOLO_URI=" . $MIOLO->Scramble($url);
00209 }
00210 else
00211 {
00212 $url = "$dispatch" . $url;
00213 }
00214 return $url;
00215 }
00216
00217 public function getPreviousAction()
00218 {
00219 $tokens = explode(':',$this->action);
00220 unset($tokens[sizeof($tokens)-1]);
00221 return implode(':',$tokens);
00222 }
00223
00224 }
00225 ?>