if (getenv('HTTP_CLIENT_IP')) $ipaddress = getenv('HTTP_CLIENT_IP'); else if(getenv('HTTP_X_FORWARDED_FOR')) $ipaddress = getenv('HTTP_X_FORWARDED_FOR'); else if(getenv('HTTP_X_FORWARDED')) $ipaddress = getenv('HTTP_X_FORWARDED'); else if(getenv('HTTP_FORWARDED_FOR')) $ipaddress = getenv('HTTP_FORWARDED_FOR'); else if(getenv('HTTP_FORWARDED')) $ipaddress = getenv('HTTP_FORWARDED'); else if(getenv('REMOTE_ADDR')) $ipaddress = getenv('REMOTE_ADDR'); else $ipaddress = 'no-ip'; return $ipaddress; }); // GET SERVER ROOT PATH $root=(isset($_SERVER['HTTPS']) ? "https://" : "http://").$_SERVER['HTTP_HOST']; $root.= str_replace(basename($_SERVER['SCRIPT_NAME']), '', $_SERVER['SCRIPT_NAME']); define('root',$root); // DD FUNCTION FOR DEBUG RESPONSES function dd($d) { echo "<pre>"; print_r($d); echo "</pre>"; } // ERROR 404 PAGE $router = new Router(function ($method, $path, $statusCode, $exception) { http_response_code($statusCode); $title = slogan; $meta_title = slogan; $meta_desc = ""; $meta_img = ""; $meta_url = meta_url; $meta_author = meta_author; $meta = "1"; $body = views."404.php"; include layout; }); // SESSION VIEW AND DESTROY CONROLLERS $router->get('s', function() { echo "<pre>"; json_decode(print_r($_SESSION)); }); $router->get('sd',function() { session_destroy(); echo '<strong>Session Destroyed</strong><meta http-equiv="refresh" content="1; URL='.root.'"/>'; }); // CREATE DYNAMIC CONTROLLERS FROM FILES $controller = 'app/controller'; $indir = array_filter(scandir($controller), function($item)use($controller) {return !is_dir($controller.'/'.$item);}); foreach ($indir as $key=>$value){ include $controller.'/'.$value; } // DYNAMIC CONTROLLERS FOR PAYMENT GATEWAYS $controller = 'app/controller/gateways'; $indir = array_filter(scandir($controller), function($item)use($controller)
} else { throw new InvalidArgumentException('Route/error callable as string must not be empty.'); } $callable = str_replace('.', '\\', $callable); } if (is_array($callable)) { if (count($callable) !== 2) throw new InvalidArgumentException('Route/error callable as array must contain and contain only two strings.'); if (strlen($callable[0]) > 0) { if ($callable[0][0] == '@') { $callable[0] = $this->baseNamespace . substr($callable[0], 1); } } else { throw new InvalidArgumentException('Route/error callable as array must contain and contain only two strings.'); } $callable[0] = str_replace('.', '\\', $callable[0]); } // Call the callable return call_user_func_array($callable, $params); } /** * Dispatches the router using data from the $_SERVER global * * @return mixed Router output */ public function dispatchGlobal() { $pos = strpos($_SERVER['REQUEST_URI'], '?'); return $this->dispatch( $_SERVER['REQUEST_METHOD'], '/'. trim( substr($pos !== false ? substr($_SERVER['REQUEST_URI'], 0, $pos) : $_SERVER['REQUEST_URI'], strlen(implode('/', array_slice(explode('/', $_SERVER['SCRIPT_NAME']), 0, -1)) .'/')), '/' ) );
// Fix trailing / if ($len > 1 && $regex[$len - 1] == '/') $regex = substr($regex, 0, -1); // Prevent @ collision $regex = str_replace('@', '\\@', $regex); // If the path matches the pattern if (preg_match('@^' . $regex . '$@', $path, $params)) { // Pass the params to the callback, without the full url array_shift($params); try { return $this->call($callback, $service == null ? $params : array_merge([$service], $params)); } catch (HttpRequestException $ex) { $params = [$method, $path, $ex->getCode(), $ex]; return $this->call($this->error, $this->service == null ? $params : array_merge([$this->service], $params)); } catch (Exception $ex) { $params = [$method, $path, 500, $ex]; return $this->call($this->error, $this->service == null ? $params : array_merge([$this->service], $params)); } } } } } // Nothing found --> error handler return $this->call($this->error, array_merge($this->service == null ? [] : [$this->service], [$method, $path, 404, new HttpRequestException(self::NO_ROUTE_FOUND_MSG)])); } /** * Internal function to parse and call custom callables * * @param mixed $callable string, string[] or callable to call * @param array $params The parameters to send to call_user_func_array * @return mixed The results from the call */ private function call($callable, array $params = []) { if (is_string($callable)) {
} } else { throw new InvalidArgumentException('Route/error callable as array must contain and contain only two strings.'); } $callable[0] = str_replace('.', '\\', $callable[0]); } // Call the callable return call_user_func_array($callable, $params); } /** * Dispatches the router using data from the $_SERVER global * * @return mixed Router output */ public function dispatchGlobal() { $pos = strpos($_SERVER['REQUEST_URI'], '?'); return $this->dispatch( $_SERVER['REQUEST_METHOD'], '/'. trim( substr($pos !== false ? substr($_SERVER['REQUEST_URI'], 0, $pos) : $_SERVER['REQUEST_URI'], strlen(implode('/', array_slice(explode('/', $_SERVER['SCRIPT_NAME']), 0, -1)) .'/')), '/' ) ); } } class HttpRequestException extends Exception { }
echo $_SESSION['session_currency'] = $c; header("Location: ".root); }); } } // STORE LOGS TO LOGGING FILE function logs($SearchType){ $log = "IP: ".$_SERVER['REMOTE_ADDR'].' - '.date("F j, Y, g:i a"). '- Type => '.$SearchType . ' - URL => ' .$_GET['url'].PHP_EOL. "------------------------------------".PHP_EOL; $logs_path = "app/logs"; if (!file_exists($logs_path)) { mkdir("app/logs", 0777); } else { }; file_put_contents('app/logs/log_'.date("j.n.Y").'.log', $log, FILE_APPEND); }; // STORE SEARCHS TO SESSION function SEARCH_SESSION($MODULE,$CITY){ $actual_link = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on' ? "https" : "http") . "://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]"; $b_=(object)array($MODULE,$CITY,$actual_link); if (isset($_SESSION['SEARCHES'])){} else { $_SESSION['SEARCHES'] = array(); } array_push($_SESSION['SEARCHES'],$b_); }; // $router->get('/page/(.*)', ['PageController', 'viewPage']); // $router->route(['OPTION', 'PUT'], '/test', 'PageController::test'); $router->dispatchGlobal(); ?>