ACC SHELL
<?php
class MY_URI extends CI_Uri {
public $keyval = array();
public $sef_string;
public $uri_string;
public $segments = array();
public $rsegments = array();
public $default_controller = "home";
public $root_rel = array(); //filled in construct
public function __construct() {
parent::__construct();
$domain = str_replace('www.',false,strtolower($_SERVER['SERVER_NAME']));;
$q = mysql_query("select * from relations where id =
(select id_relations from settings where name='alias' && value='{$domain}' limit 1)");
$this->root_rel = mysql_fetch_array($q);
}
public function redirect($url="",$permanent=false){
if($url=="404"){
header("HTTP/1.0 404 Not Found");
header("location: /");
die("exit redirect 404");
}
if($permanent) header("HTTP/1.1 301 Moved Permanently");
$url = ($url[0]!="/"?"/":"").$url;
header("location: ".$url);
die("exit redirect");
}
public function real_url($path){
$e = explode("/",trim($path,"/"));
$e = explode("?",$e[0]); // without query string
$q = mysql_query("select url from urls
left join relations on relations.id=id_relations
where sef='".$e[0]."' && (!relations.id || lft>='".$this->root_rel['lft']."' && rgt<='".$this->root_rel['rgt']."')
");
$a = mysql_fetch_array($q);
if(empty($a)){
return $path;
}else{
return "/" . $a['url'] . substr(trim($path,"/"),strlen($e[0]));
}
}
//returns sef
public function sef($path="/"){
if(empty($path)) $path = "/"; //empty -> "/"
$sql = "select sef from urls where url='".trim($path,"/")."' order by id desc limit 1";
$q = mysql_query($sql);
$a = mysql_fetch_array($q);
if(empty($a)){
return ($path[0]!="/"?"/":"").$path;
}else{
return (@$a['sef'][0]!="/"?"/":"").$a['sef'];
}
}
public function sef_site($idr){
return $this->sef("/$this->default_controller/site/".$idr);
}
public function sef_tag($tag){
$sm = new Sites_model();
return $this->sef_site($sm->tag($tag)->idr);
}
// save sef
public function save_sef($url,$sef,$id_relations=NULL){
$sef = Tools::str2url($sef);
$url = trim($url,"/");
// if find saved url, break
$q = mysql_query("select * from urls where url='".$url."'");
$break = false;
// find if last sef is the same
while($row = mysql_fetch_assoc($q)){
if(substr($row['sef'],0,strlen($sef)) == $sef && strlen($sef)>=strlen($row['sef'])-3){
$break = true;
}else $break = false;
}
if($break) return;
// if find saved sef, find other
$i = -1;
do{
$save_sef = $sef . (++$i?"-".$i:"");
$sql = "select id from urls
left join relations on relations.id=id_relations
where sef='".trim($save_sef,"/")."' &&
(!relations.id || lft>'".$this->root_rel['lft']."' && rgt<'".$this->root_rel['rgt']."')";
$q = mysql_query($sql);
$a = mysql_fetch_array($q);
}while(!empty($a));
mysql_query("insert into urls (url,sef,id_relations) values ('".$url."','".trim($save_sef,"/")."','".$id_relations."')");
}
// redirects old urls to sef or old sef in db
public function sef_redirect(){
$q = mysql_query("select sef from urls where url='".trim($this->sef_string,"/")."' order by id desc limit 1");
$a = ($q ? mysql_fetch_array($q) : false);
if(!empty($a)){
$this->redirect($a['sef'],true);
}
$sef = trim($this->sef_string,"/");
$e = explode("/",trim($this->sef_string,"/"));
if( $e[0] != $sef
&& mysql_result(mysql_query("select count(id) from urls where url=(select url from urls where sef='".$sef."' limit 1)"),0)){
$sql = "select sef from urls where url=(select url from urls where sef='".$sef."' limit 1) order by id desc limit 1";
$full = true;
}else{
$sql = "select sef from urls u1 where url=(select url u2 from urls where sef='".$e[0]."' limit 1) order by id desc limit 1";
$full = false;
}
$q = mysql_query($sql);
$a = ($q ? mysql_fetch_array($q) : false);
if(!empty($a['sef']) && $a['sef']!=$sef && $full){
$this->redirect($a['sef'],true);
}elseif(!empty($a['sef']) && $a['sef']!=$e[0] && !$full){
$this->redirect($a['sef'].substr(trim($this->sef_string,"/"),strlen($e[0])),true);
}
}
/**
* saves url of this site to intern history
*/
public function save_history(){
$url = $this->uri_string();
$segments = $this->rsegment_array();
$max = 12;
$url = trim($url," /\\");
$_SESSION['history'][] = array(
'controller' => $segments[1],
'action' => $segments[2],
'url' => $url,
);
if(count($_SESSION['history'])>$max){
foreach($_SESSION['history'] as $i=>$h){
unset($_SESSION['history'][$i]); break;
}
}
}
public function back($options = array()){
$url = self::back_url($options);
$this->redirect($url);
}
/**
*
* @param array $options there we can specify:
* controller
* action
*/
public function back_url($options = array()){
if(!isset($_SESSION['history'])){
return ("/");
}
$history = array_reverse($_SESSION['history']);
$i = 0;
foreach($history as $h){
if(!$i){
$i++;
continue; // vynecha prvni
}
if(isset($options['deep']) && $options['deep']>$i){
continue;
}
if(
(empty($options['action']) || $h['action']==$options['action'])
&& (empty($options['controller']) || $h['controller']==$options['controller'])
&& (empty($options['no_controller']) || $h['controller']!=$options['no_controller'])
&& (empty($options['no_action']) || $h['action']!=$options['no_action'])
){
// ends cycling
return ("/".$h['url']);
}
$i++;
}
// if there isn't useable url
array_pop ( $_SESSION['history'] ); // vyhodi pouzitou adresu z historie
if(isset($history[1]['url'])){
return ("/".$history[1]['url']);
}elseif(isset($history[0]['url'])){
return ("/".$history[0]['url']);
}else{
return ("/");
}
}
// to jenom kdyby mě sraly zakázané znaky v adrese... http://codeigniter.com/forums/viewthread/160377/
// this functin disable this: $this->config->item('permitted_uri_chars')
function _filter_uri($str) {
// Convert programatic characters to entities
$bad = array('$', '(', ')', '%28', '%29');
$good = array('$', '(', ')', '(', ')');
return str_replace($bad, $good, $str);
}
// --------------------------------------------------------------------
/**
* Get the URI String
*
* @access private
* @return string
*/
function _fetch_uri_string()
{
if (strtoupper($this->config->item('uri_protocol')) == 'AUTO')
{
// If the URL has a question mark then it's simplest to just
// build the URI string from the zero index of the $_GET array.
// This avoids having to deal with $_SERVER variables, which
// can be unreliable in some environments
// MY_BLOCKED
/*
if (is_array($_GET) && count($_GET) == 1 && trim(key($_GET), '/') != '')
{
$this->uri_string = key($_GET);
return;
}*/
// Is there a PATH_INFO variable?
// Note: some servers seem to have trouble with getenv() so we'll test it two ways
$path = (isset($_SERVER['PATH_INFO'])) ? $_SERVER['PATH_INFO'] : @getenv('PATH_INFO');
if (trim($path, '/') != '' && $path != "/".SELF)
{
//$this->uri_string = $path;
$this->sef_string = $path;
$this->sef_redirect();
$this->uri_string = $this->real_url($path);
return;
}
// No PATH_INFO?... What about QUERY_STRING?
$path = (isset($_SERVER['QUERY_STRING'])) ? $_SERVER['QUERY_STRING'] : @getenv('QUERY_STRING');
if (trim($path, '/') != '')
{
$this->uri_string = $path;
return;
}
// No QUERY_STRING?... Maybe the ORIG_PATH_INFO variable exists?
$path = str_replace($_SERVER['SCRIPT_NAME'], '', (isset($_SERVER['ORIG_PATH_INFO'])) ? $_SERVER['ORIG_PATH_INFO'] : @getenv('ORIG_PATH_INFO'));
if (trim($path, '/') != '' && $path != "/".SELF)
{
// remove path and script information so we have good URI data
$this->uri_string = $path;
return;
}
// We've exhausted all our options...
$this->uri_string = "home/site/" . $this->root_rel['id'];
}
else
{
$uri = strtoupper($this->config->item('uri_protocol'));
if ($uri == 'REQUEST_URI')
{
$this->uri_string = $this->_parse_request_uri();
return;
}
$this->uri_string = (isset($_SERVER[$uri])) ? $_SERVER[$uri] : @getenv($uri);
}
// If the URI contains only a slash we'll kill it
if ($this->uri_string == '/')
{
$this->uri_string = '';
}
}
}
ACC SHELL 2018