Last active
August 19, 2019 12:22
-
-
Save fatihgvn/8cb5c3bd085c3a097fee0f2b4b005020 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* uri ve file path yollarını combinler | |
* author: https://github.com/fatihgvn | |
* source: https://gist.github.com/fatihgvn/8cb5c3bd085c3a097fee0f2b4b005020 | |
* date: 19.08.2019 | |
*/ | |
class Combine | |
{ | |
/** | |
* yerel dosya sistemine göre bir veya iki dizini kurallara uygun şekilde birleştirir. | |
* istenirse bu dizinin olup olmadığı sorgulanabilir | |
* | |
* @param string $base başlangıç dizini | |
* @param string $com eklenecek dizin | |
* @param boolean $isReal dizin kontrolü | |
* | |
* @return String if $isReal is false | |
* @return Var if $isReal is true | |
*/ | |
public static function path($base, $com = null, $isReal = false) | |
{ | |
if(substr($base, -1)!=DIRECTORY_SEPARATOR) $base.=DIRECTORY_SEPARATOR; | |
if($com) $base.=$com; | |
$base = preg_replace('/(\/+|\\\\+)/', DIRECTORY_SEPARATOR, $base); | |
while(preg_match('/(\/[\w\s_-]+\/\.\.)/', $base)){ | |
$base = preg_replace('/(\/[\w\s_-]+\/\.\.)/', "", $base); | |
if(preg_match('/\/\.\.\//', $base)) | |
throw new \Exception("Error directory don't have parent folder!", 1); | |
} | |
if($isReal){ | |
$base = realpath($base); | |
if(is_dir($base)) $base .= DIRECTORY_SEPARATOR; | |
} | |
return $base; | |
} | |
/** | |
* yerel dosya sistemine göre bir veya iki dizini kurallara uygun şekilde birleştirir. | |
* istenirse bu dizinin olup olmadığı sorgulanabilir | |
* | |
* @param string $base başlangıç dizini | |
* @param string $com eklenecek dizin | |
* | |
* @return Var Eğer $base yanlış yapıya sahipse false yanıtı verir. | |
* Eğer $base doğruysa yeni uri adresini verir. | |
*/ | |
public static function uri($base, $com) | |
{ | |
// check base | |
if(preg_match('/(https?|ftp|tcp|udp):\/\/([\w]+\.)?([\w]{4,})((\.\w{2,5}){1,2})/', $base)){ | |
$base = parse_url($base); | |
if(preg_match('/^\//', $com)){ | |
$base['path'] = $com; | |
unset($base['query']); | |
}elseif (preg_match('/^\.\//', $com)) { | |
$base['path'] = strrev(preg_replace('/^[\w\s-_\.]+\//',"",strrev($base['path']))); | |
$base['path'] .= substr($com, 1); | |
unset($base['query']); | |
}else if(parse_url($com , PHP_URL_QUERY) != null){ | |
if(isset($base['query'])){ | |
$base['query'] .= '&'.substr($com, 1); | |
}else{ | |
$base['query'] = substr($com, 1); | |
} | |
} | |
if(!isset($base['query'])) return $base["scheme"]."://".$base["host"].$base['path']; | |
else return $base["scheme"]."://".$base["host"].$base['path'].'?'.$base['query']; | |
}else { | |
return false; | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
require_once 'Combine.php'; | |
var_dump(Combine::uri("http://example.com/deneme.php?a=asdasd","/index.php")); | |
// string(35) "http://example.com/index.php" | |
var_dump(Combine::uri("http://example.com/deneme.php?a=asdasd","/index.php?deneme=test")); | |
// string(47) "http://example.com/index.php?deneme=test" | |
var_dump(Combine::uri("http://example.com/deneme.php?a=asdasd","?add=test")); | |
// string(54) "http://example.com/deneme.php?a=asdasd&add=test" | |
var_dump(Combine::uri("http://example.com/deneme/asdasd","?a=asdasd")); | |
// string(48) "http://example.com/deneme/asdasd?a=asdasd" | |
var_dump(Combine::uri("http://example.com/deneme/asdasd.php","./index.php")); | |
// string(42) "http://example.com/deneme/index.php" | |
var_dump(Combine::path("www///system", "Combine/../")); | |
// string(11) "www/system/" | |
var_dump(Combine::path("System", "Combine/../", true)); | |
// string(40) "/home/snow/Desktop/localhost/www/System/" | |
var_dump(Combine::path("System", "Combine", true)); | |
// string(48) "/home/snow/Desktop/localhost/www/System/Combine/" | |
var_dump(Combine::path("System", "Combine/notPath", true)); // if you try to create a path that does not exist | |
// bool(false) | |
var_dump(Combine::path("System", "Combine/class.Combine.php", true)); // you can also select a file | |
//string(65) "/home/snow/Desktop/localhost/www/System/Combine/class.Combine.php" | |
var_dump(Combine::path("/home/testuser\\badPath///////repair")); | |
// string(30) "/home/testuser/badPath/repair/" | |
var_dump(Combine::path("/System/Combine/test/asd", "../../../../../")); | |
// throw error output | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment