Skip to content

Instantly share code, notes, and snippets.

@aufa
Last active April 14, 2019 19:00
Show Gist options
  • Save aufa/eee89dc211a1979b0a76802ecd480f4f to your computer and use it in GitHub Desktop.
Save aufa/eee89dc211a1979b0a76802ecd480f4f to your computer and use it in GitHub Desktop.
Prevent Visitor Accessed Direct Controller & Fix Main Url on Code Igniter config base url not set
<?php
/**
* Hackie of CI 3 Core URL Fix Sanitized
* Automation detect base url
* @pentagonal
*/
if (!class_exists('CI_Config')) {
return;
}
class MY_Config extends CI_Config
{
/**
* Constructor
*
* Override Config Core to Auto Detection URL if Config URL does not set
*/
public function __construct()
{
// CI use refference to allow arguments take override nested
$this->config =& get_config();
// Set the base_url automatically if none was provided
if (empty($this->config['base_url']) || trim($this->config['base_url']) == '') {
/**
* Auto Detection URL on Code Igniter
*/
if (isset($_SERVER['HTTP_HOST'])) {
$base_url = $this->portUrlMessDetector($_SERVER['HTTP_HOST']);
} elseif (isset($_SERVER['SERVER_NAME'])) {
$base_url = $this->portUrlMessDetector($_SERVER['HTTP_HOST']);
} elseif (isset($_SERVER['SERVER_ADDR'])) {
// this is IPV 6 ipv 6 accessed by http(s)://[11:22:33:44]/
if (strpos($_SERVER['SERVER_ADDR'], ':') !== false) {
$base_url = $this->portUrlMessDetector('['.$_SERVER['SERVER_ADDR'].']');
} else {
$base_url = $this->portUrlMessDetector($_SERVER['SERVER_ADDR']);
}
} else {
// default host , but it will be almost impossible, if server config has not wrong!
$base_url = 'http://localhost/';
}
$this->set_item('base_url', $base_url);
}
// do it like core , send log into system
log_message('info', 'Config Class Initialized');
}
/**
* Fixing the server address & URL
*
* @param string $server_addr url
* @return string base URL
*/
protected function portUrlMessDetector($server_addr)
{
/**
* Base on Different port not 80 / 443
*/
if (isset($_SERVER['SERVER_PORT'])) {
$server_addr .= $_SERVER['SERVER_PORT'] != 80 && $_SERVER['SERVER_PORT'] != 443
? ':'.$_SERVER['SERVER_PORT']
: '';
}
$base_url = (is_https() ? 'https' : 'http').'://'. $server_addr
. substr($_SERVER['SCRIPT_NAME'], 0, strpos($_SERVER['SCRIPT_NAME'], basename($_SERVER['SCRIPT_FILENAME'])));
return $base_url;
}
}
<?php
/**
* Hackie of CI 3 Core URL Fixed Sanitizer
* Prevent direct access to Uri Controller
* @pentagonal
*/
if (!class_exists('CI_Router')) {
return;
}
class MY_Router extends CI_Router
{
/**
* Parse Routes
*
* Matches any routes that may exist in the config/routes.php file
* against the URI to determine if the class/method need to be remapped.
*
* @return void
*/
protected function _parse_routes()
{
// Turn the segment array into a URI string
$uri = implode('/', $this->uri->segments);
// Get HTTP verb
$http_verb = isset($_SERVER['REQUEST_METHOD']) ? strtolower($_SERVER['REQUEST_METHOD']) : 'cli';
// Loop through the route array looking for wildcards
foreach ($this->routes as $key => $val) {
// Check if route format is using HTTP verbs
if (is_array($val)) {
$val = array_change_key_case($val, CASE_LOWER);
if (isset($val[$http_verb])) {
$val = $val[$http_verb];
} else {
continue;
}
}
// Convert wildcards to RegEx
$key = str_replace(array(':any', ':num'), array('[^/]+', '[0-9]+'), $key);
// Does the RegEx match?
if (preg_match('#^'.$key.'$#', $uri, $matches)) {
// Are we using callbacks to process back-references?
if ( ! is_string($val) && is_callable($val)) {
// Remove the original string from the matches array.
array_shift($matches);
// Execute the callback using the values in matches as its parameters.
$val = call_user_func_array($val, $matches);
} elseif (strpos($val, '$') !== false && strpos($key, '(') !== false) {
// Are we using the default routing method for back-references?
$val = preg_replace('#^'.$key.'$#', $val, $uri);
}
$this->_set_request(explode('/', $val));
return;
}
}
/**
* We Must Comment this ,
* because it will be allow visitor access by direct Controller CLASS!
* if you have Controller Wellcome
* and user access
* http://example.com/index.php/Wellcome
* it will be same as http://example.com/index.php/(<url to controller route>)
*/
/*
// If we got this far it means we didn't encounter a
// matching route so we'll set the site default route
$this->_set_request(array_values($this->uri->segments));
*/
}
}
@inandi
Copy link

inandi commented Apr 14, 2019

where should I put these files? will it prevent direct access all controller's method except ajax call? Little help required.
Thanks in advance

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment