session = Session::singleton(); $this->server = $_SERVER['SERVER_NAME']; $this->uri = strtolower($_SERVER['REQUEST_URI']); $this->local = (strstr($_SERVER['SERVER_NAME'], '.dev') || strstr($_SERVER['SERVER_NAME'], '192.168')); $this->file = $this->getFile(); $this->type = $this->getType(); $this->validatePage(); } function getType($path = null){ $path = is_null($path) ? strtolower($this->file) : strtolower($path); $pieces = explode('.', array_pop(explode('/', (strpos($path, '/') === 0 ? substr($path, 1) : $path)))); return $pieces[0]; } /** * Method: getFile():String * Returns the file to use from the template.php file's querystring */ public function getFile(){ $page = isset($_GET['p']) ? $_GET['p'] : ''; $file = substr($this->uri, strrpos($this->uri, "/") + 1); if(is_file(DIR_ROOT . $file)){ include($file); exit; } if(is_array($page) && count($page) > 0){ $sections = []; foreach($page as $section){ $sections[] = $section; } if(count($sections) > 0){ $filestub = implode('.', $sections); $level = isset($this->session->user) && isset($this->session->user['level']) ? $this->session->user['level'] : 0; switch($level){ case 9: case 8: if($file = $this->findpage('admin', $filestub)){ break; } case 7: case 6: case 5: case 4: case 3: case 2: case 1: if($file = $this->findpage('private', $filestub)){ break; } default: if($file = $this->findpage('public', $filestub)){ break; } } }else{ $file = 'public.global.index.php'; } }else{ $file = 'public.global.index.php'; } if(!isset($file) || empty($file) || $file === false){ //$this->session->quickerror = 'You do not have access to that resource. Click here to return'; //header('Location: http://' . $_SERVER['SERVER_NAME'] . '/error.html'); $file = 'public.global.index.php'; header("HTTP/1.0 404 Not Found"); } if(strpos($file, 'ajax.') !== false){ include($file); exit; } return $file; } function findpage($level = 'public', $stub){ if(is_file(DIR_ROOT . $level . '.' . $stub . '.php')){ $file = $level . '.' . $stub . '.php'; }else if(is_file(DIR_ROOT . $level . '.global.' . $stub . '.php')){ $file = $level . '.global.' . $stub . '.php'; }else if(is_file(DIR_ROOT . $level . '.' . $stub . '.index.php')){ $file = $level . '.' . $stub . '.index.php'; } if(!isset($file)){ $thisstub = preg_replace('/\.\[\]/si', ':[]', $stub); if(strrpos($thisstub, '.') !== false){ $newstub = substr($thisstub, 0, strrpos($thisstub, '.')) . ':[]'; $newstub = preg_replace('/\:\[\]/si', '.[]', $newstub); $file = $this->findpage($level, $newstub); } } return isset($file) ? $file : false; } function checkTypes(){ if(!isset($this->type)){ $this->type = $this->getType(); } if(in_array($this->type, $this->types)){ return true; } return false; } function validatePage(){ if(!isset($this->type)){ $this->type = $this->getType(); } if($this->checkTypes() !== false){ if($this->type == 'public'){ return true; }else{ if(isset($this->session->user['level']) && $this->session->user['level'] >= 1){ if($this->type == 'admin' && $this->session->user['level'] >= 8){ return true; }else if($this->type == 'private'){ return true; }else{ $this->session->quickerror = 'You do not have access to that resource.'; header('Location: http://' . $_SERVER['SERVER_NAME'] . '/error.html'); } }else if(isset($this->session->user['level']) && $this->session->user['level'] <= 0){ $this->session->quickerror = 'Your account is not authorized yet. Please authorize your account to proceed.'; header('Location: http://' . $_SERVER['SERVER_NAME'] . '/'); }else{ $this->session->prelogintry = $_SERVER['REQUEST_URI']; $this->session->quickerror = 'You must be logged in to view that resource.'; header('Location: http://' . $_SERVER['SERVER_NAME'] . '/'); } } } } /** * Method: getArrayValue($key:String[, $array = $_POST, $default = '']):Mixed * Returns value of $array[$key] if exists, else returns defult text * Example: If exists $_POST['name'] display it, otherwise display 'Enter Your Name Here' * getArrayValue('name', $_POST, 'Enter Your Name Here') */ public function getArrayValue($key, $array = null, $default = ''){ $array = (!is_array($array) || is_null($array)) ? $_POST : $array; if(isset($array[$key])){ return $array[$key]; } return $default; } } ?>