Skip to content

Commit

Permalink
Clean URLs for foreign char
Browse files Browse the repository at this point in the history
Previously any foreign char always removed. Fix multi tags and is_front
function.
  • Loading branch information
danpros committed Jun 14, 2014
1 parent 282ac67 commit 90a07f1
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 36 deletions.
45 changes: 14 additions & 31 deletions system/admin/admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,14 @@ function session($user, $pass, $str = null) {
}
}

// Clean URLs
function remove_accent($str)
{
$a = array('À', 'Á', 'Â', 'Ã', 'Ä', 'Å', 'Æ', 'Ç', 'È', 'É', 'Ê', 'Ë', 'Ì', 'Í', 'Î', 'Ï', 'Ð', 'Ñ', 'Ò', 'Ó', 'Ô', 'Õ', 'Ö', 'Ø', 'Ù', 'Ú', 'Û', 'Ü', 'Ý', 'ß', 'à', 'á', 'â', 'ã', 'ä', 'å', 'æ', 'ç', 'è', 'é', 'ê', 'ë', 'ì', 'í', 'î', 'ï', 'ñ', 'ò', 'ó', 'ô', 'õ', 'ö', 'ø', 'ù', 'ú', 'û', 'ü', 'ý', 'ÿ', 'Ā', 'ā', 'Ă', 'ă', 'Ą', 'ą', 'Ć', 'ć', 'Ĉ', 'ĉ', 'Ċ', 'ċ', 'Č', 'č', 'Ď', 'ď', 'Đ', 'đ', 'Ē', 'ē', 'Ĕ', 'ĕ', 'Ė', 'ė', 'Ę', 'ę', 'Ě', 'ě', 'Ĝ', 'ĝ', 'Ğ', 'ğ', 'Ġ', 'ġ', 'Ģ', 'ģ', 'Ĥ', 'ĥ', 'Ħ', 'ħ', 'Ĩ', 'ĩ', 'Ī', 'ī', 'Ĭ', 'ĭ', 'Į', 'į', 'İ', 'ı', 'IJ', 'ij', 'Ĵ', 'ĵ', 'Ķ', 'ķ', 'Ĺ', 'ĺ', 'Ļ', 'ļ', 'Ľ', 'ľ', 'Ŀ', 'ŀ', 'Ł', 'ł', 'Ń', 'ń', 'Ņ', 'ņ', 'Ň', 'ň', 'ʼn', 'Ō', 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', 'n', 'N', 'n', 'N', 'n', 'n', 'O', 'o', 'O', 'o', 'O', 'o', 'OE', 'oe', 'R', 'r', 'R', 'r', 'R', 'r', 'S', 's', 'S', 's', 'S', 's', 'S', 's', 'T', 't', 'T', 't', 'T', 't', 'U', 'u', 'U', 'u', 'U', 'u', 'U', 'u', 'U', 'u', 'U', 'u', 'W', 'w', 'Y', 'y', 'Y', 'Z', 'z', 'Z', 'z', 'Z', 'z', 's', 'f', 'O', 'o', 'U', 'u', 'A', 'a', 'I', 'i', 'O', 'o', 'U', 'u', 'U', 'u', 'U', 'u', 'U', 'u', 'U', 'u', 'A', 'a', 'AE', 'ae', 'O', 'o');
return str_replace($a, $b, $str);
}

// Edit blog posts
function edit_post($title, $tag, $url, $content, $oldfile, $destination = null) {

Expand All @@ -40,11 +48,7 @@ function edit_post($title, $tag, $url, $content, $oldfile, $destination = null)
$post_tag = preg_replace('/[^A-Za-z0-9,.-]/u', '', $tag);
$post_tag = str_replace(' ', '-',$post_tag);
$post_tag = rtrim(ltrim($post_tag, ',\.\-'), ',\.\-');
$post_url = preg_replace('/[^A-Za-z0-9 ,.-]/u', '', strtolower($url));
$post_url = str_replace(' ', '-',$post_url);
$post_url = str_replace('--', '-',$post_url);
$post_url = str_replace('--', '-',$post_url);
$post_url = rtrim(ltrim($post_url, ',\.\-'), ',\.\-');
$post_url = strtolower(preg_replace(array('/[^a-zA-Z0-9 -]/', '/[ -]+/', '/^-|-$/'), array('', '-', ''), remove_accent($url)));
$post_content = '<!--t ' . $post_title . ' t-->' . "\n\n" . $content;

if(!empty($post_title) && !empty($post_tag) && !empty($post_url) && !empty($post_content)) {
Expand Down Expand Up @@ -90,11 +94,7 @@ function edit_page($title, $url, $content, $oldfile, $destination = null) {
$dir = substr($oldfile, 0, strrpos($oldfile, '/'));

$post_title = $title;
$post_url = preg_replace('/[^A-Za-z0-9 ,.-]/u', '', strtolower($url));
$post_url = str_replace(' ', '-',$post_url);
$post_url = str_replace('--', '-',$post_url);
$post_url = str_replace('--', '-',$post_url);
$post_url = rtrim(ltrim($post_url, ',\.\-'), ',\.\-');
$post_url = strtolower(preg_replace(array('/[^a-zA-Z0-9 -]/', '/[ -]+/', '/^-|-$/'), array('', '-', ''), remove_accent($url)));
$post_content = '<!--t ' . $post_title . ' t-->' . "\n\n" . $content;

if(!empty($post_title) && !empty($post_url) && !empty($post_content)) {
Expand Down Expand Up @@ -131,11 +131,7 @@ function add_post($title, $tag, $url, $content, $user) {
$post_title = $title;
$post_tag = preg_replace('/[^A-Za-z0-9,.-]/u', '', $tag);
$post_tag = rtrim(ltrim($post_tag, ',\.\-'), ',\.\-');
$post_url = preg_replace('/[^A-Za-z0-9 ,.-]/u', '', strtolower($url));
$post_url = str_replace(' ', '-',$post_url);
$post_url = str_replace('--', '-',$post_url);
$post_url = str_replace('--', '-',$post_url);
$post_url = rtrim(ltrim($post_url, ' \,\.\-'), ' \,\.\-');
$post_url = strtolower(preg_replace(array('/[^a-zA-Z0-9 -]/', '/[ -]+/', '/^-|-$/'), array('', '-', ''), remove_accent($url)));
$post_content = '<!--t ' . $post_title . ' t-->' . "\n\n" . $content;

if(!empty($post_title) && !empty($post_tag) && !empty($post_url) && !empty($post_content)) {
Expand All @@ -161,11 +157,7 @@ function add_post($title, $tag, $url, $content, $user) {
function add_page($title, $url, $content) {

$post_title = $title;
$post_url = preg_replace('/[^A-Za-z0-9 ,.-]/u', '', strtolower($url));
$post_url = str_replace(' ', '-',$post_url);
$post_url = str_replace('--', '-',$post_url);
$post_url = str_replace('--', '-',$post_url);
$post_url = rtrim(ltrim($post_url, ',\.\-'), ',\.\-');
$post_url = strtolower(preg_replace(array('/[^a-zA-Z0-9 -]/', '/[ -]+/', '/^-|-$/'), array('', '-', ''), remove_accent($url)));
$post_content = '<!--t ' . $post_title . ' t-->' . "\n\n" . $content;

if(!empty($post_title) && !empty($post_url) && !empty($post_content)) {
Expand Down Expand Up @@ -251,11 +243,7 @@ function migrate($title, $time, $tags, $content, $url, $user, $source) {
$post_title = $title;
$post_tag = preg_replace('/[^A-Za-z0-9,.-]/u', '', $tags);
$post_tag = rtrim(ltrim($post_tag, ',\.\-'), ',\.\-');
$post_url = preg_replace('/[^A-Za-z0-9 ,.-]/u', '', strtolower($url));
$post_url = str_replace(' ', '-',$post_url);
$post_url = str_replace('--', '-',$post_url);
$post_url = str_replace('--', '-',$post_url);
$post_url = rtrim(ltrim($post_url, ',\.\-'), ',\.\-');
$post_url = strtolower(preg_replace(array('/[^a-zA-Z0-9 -]/', '/[ -]+/', '/^-|-$/'), array('', '-', ''), remove_accent($url)));
if(!empty($source)) {
$post_content = '<!--t ' . $post_title . ' t-->' . "\n\n" . $content . "\n\n" . 'Source: <a target="_blank" href="' . $source . '">' . $title . '</a>';
}
Expand Down Expand Up @@ -306,12 +294,7 @@ function get_feed($feed_url, $credit, $message=null) {
$title = rtrim($entry->title, ' \,\.\-');
$title = ltrim($title, ' \,\.\-');
$user = $_SESSION['user'];
$url = preg_replace('/[^A-Za-z0-9 .-]/u', '', strtolower($title));
$url = str_replace(' ', '-',$url);
$url = str_replace('--', '-',$url);
$url = str_replace('--', '-',$url);
$url = rtrim($url, ',\.\-');
$url = ltrim($url, ',\.\-');
$url = strtolower(preg_replace(array('/[^a-zA-Z0-9 -]/', '/[ -]+/', '/^-|-$/'), array('', '-', ''), remove_accent($title)));
if ($credit == 'yes') {
$source = $entry->link;
}
Expand Down
12 changes: 7 additions & 5 deletions system/includes/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,7 @@ function get_tag($tag, $page, $perpage, $random){
$etag = explode(',', $tag);
foreach ($mtag as $t) {
foreach ($etag as $e) {
$e = trim($e);
if($t === $e){
$tmp[] = $v;
}
Expand All @@ -272,6 +273,8 @@ function get_tag($tag, $page, $perpage, $random){
not_found();
}

$tmp = array_unique($tmp, SORT_REGULAR);

return $tmp = get_posts($tmp, $page, $perpage);

}
Expand Down Expand Up @@ -476,7 +479,7 @@ function get_related($tag) {
$posts = get_tag(strip_tags($tag), 1, $perpage+1, true);
$tmp = array();
$req = $_SERVER['REQUEST_URI'];

foreach ($posts as $post) {
$url = $post->url;
if( strpos($url, $req) === false){
Expand Down Expand Up @@ -1469,7 +1472,7 @@ function Zip($source, $destination, $include_dir = false) {
// TRUE if the current page is the front page.
function is_front() {
$req = $_SERVER['REQUEST_URI'];
if($req == site_path() . '/') {
if($req == site_path() . '/' || strpos($req, site_path() . '/?page') !== false) {
return true;
}
else {
Expand All @@ -1480,7 +1483,7 @@ function is_front() {
// TRUE if the current page is an index page like frontpage, tag index, archive index and search index.
function is_index() {
$req = $_SERVER['REQUEST_URI'];
if(strpos($req, '/archive/') !== false || strpos($req, '/tag/') !== false || strpos($req, '/search/') !== false || $req == site_path() . '/'){
if(strpos($req, '/archive/') !== false || strpos($req, '/tag/') !== false || strpos($req, '/search/') !== false || $req == site_path() . '/' || strpos($req, site_path() . '/?page') !== false){
return true;
}
else {
Expand Down Expand Up @@ -1516,11 +1519,10 @@ function authorinfo($title=null, $body=null) {
}

function head_contents($title, $description, $canonical) {

$styleImage = config('lightbox');
$jq = config('jquery');
$output = '';

$title = '<title>' . $title . '</title>';
$favicon = '<link href="' . site_url() . 'favicon.ico" rel="icon" type="image/x-icon"/>';
$charset = '<meta charset="utf-8" />';
Expand Down

0 comments on commit 90a07f1

Please sign in to comment.