Last active
August 29, 2015 13:56
-
-
Save RaffaeleSgarro/8902552 to your computer and use it in GitHub Desktop.
Simple breadcrumb for PHP scripts apps deployed by mapping request paths to the server's filesystem
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
RewriteEngine On | |
RewriteRule index.php index.php [END] | |
RewriteRule (.*) index.php?request=$1 [END] |
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 | |
// This file is UTF-8 encoded | |
define('BASE_URL', '/breadcrumbs'); | |
function parse($script, $directorySeparator, $skip) { | |
mb_regex_encoding('UTF-8'); | |
mb_internal_encoding('UTF-8'); | |
$offset = mb_strlen($skip, 'UTF-8'); | |
$length = mb_strlen($script, 'UTF-8') - $offset; | |
$script = mb_substr($script, $offset, $length, 'UTF-8'); | |
$directorySeparator = preg_quote($directorySeparator); | |
return mb_split($directorySeparator, $script); | |
} | |
function makeHref($crumbs, $idx, $urlBase) { | |
$dst = array(); | |
foreach (array_slice($crumbs, 0, $idx + 1) as $crumb) { | |
$dst []= rawurlencode($crumb); | |
} | |
return $urlBase . '/' . implode('/', $dst); | |
} | |
function makeHtmlWidget($crumbs, $urlBase = '', $separator = '::') { | |
$out = ''; | |
// WARNING: use an appropriate function here, even if XML special characters | |
// are invalid in file names of most filesystems out there | |
$sanitizeHtml = function($in) { return $in; }; | |
$caption = function($in) { return $in; }; | |
$current_separator = ''; | |
foreach ($crumbs as $idx => $crumb) { | |
$href = makeHref($crumbs, $idx, $urlBase); | |
$displayLink = $caption($crumb); | |
$out .= $current_separator . "<a href='$href'>$displayLink</a>"; | |
$current_separator = $separator; | |
} | |
return $out; | |
} | |
header("Content-Type: text/html; charset=utf-8"); | |
function test($in, $separator, $skip, $base) { | |
$crumbs = parse($in, $separator, $skip); | |
echo '<li>' . makeHtmlWidget($crumbs, $base) . '</li>'; | |
} | |
echo "<ul>"; | |
test('/var/www/app/TR/Pròdùçts/Tàrgét.php', '/', '/var/www/app/', BASE_URL); | |
test('C:\\xampp\\htdocs\\app\\TR\\Pròduçts\\Tàrgét.php', '\\', 'C:\\xampp\\htdocs\\app\\', BASE_URL); | |
echo "</ul>"; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment