Skip to content

Instantly share code, notes, and snippets.

@CNG
Last active April 20, 2017 08:30
Show Gist options
  • Save CNG/89dd30aaa38c9f68b0d7 to your computer and use it in GitHub Desktop.
Save CNG/89dd30aaa38c9f68b0d7 to your computer and use it in GitHub Desktop.
Listing chooser
<?php
/*
This script takes URL parameters language_path, category, year and month and
assigns to variables $p, $c, $y and $m. It is assumed language_path and category
will consist of alphanumeric or underscore/hyphen characters, and the year and
month will be integers.
The user is redirected to the appropriate predictable archive listing URL if it
exists, otherwise user is redirected to a "no results" page.
TO DO: If user specifies a month but no year, currently no "invalid" message is
displayed, but rather we just redirect to the "no results" page.
*/
$p = isset($_GET['language_path']) ? preg_replace('/[^a-z0-1-_]/i','',$_GET['language_path']) : '' ;
$c = isset($_GET['category']) ? preg_replace('/[^a-z0-1-_]/i','',$_GET['category']) : false ;
$y = isset($_GET['year']) ? strval(intval($_GET['year'])) : false ;
$m = isset($_GET['month']) ? intval($_GET['month']) : false ;
$blog_url = str_replace('/main/','/main/'.$p,'/<$mt:BlogURL$>');
$blog_path = str_replace('/main/','/main/'.$p,'/<$mt:BlogSitePath$>');
$params = '?category='.$c.'&year='.str_pad($y,4,'0',STR_PAD_LEFT).'&month='.str_pad($m,2,'0',STR_PAD_LEFT);
$options = '';
if( $c && $y && $m ){
$options = $c.'/'.str_pad($y,4,'0',STR_PAD_LEFT).'/'.str_pad($m,2,'0',STR_PAD_LEFT);
} else if( $c && $y ){
$options = $c.'/'.str_pad($y,4,'0',STR_PAD_LEFT);
} else if( $c && $m ){
$params .= '&nonefound';
} else if( $c ){
$options = $c;
} else if( $y && $m ){
$options = str_pad($y,4,'0',STR_PAD_LEFT).'/'.str_pad($m,2,'0',STR_PAD_LEFT);
} else if( $y ){
$options = str_pad($y,4,'0',STR_PAD_LEFT);
} else if( $m ){
$params .= '&nonefound';
}
if( strlen($options) > 0 ){
$target_file = $options.'/index.html';
} else {
$target_file = 'index.html';
}
if( !file_exists( $blog_path.$target_file ) ){
$target_file = 'index.html';
$params .= '&nonefound';
}
header( 'Location: '.$blog_url.$target_file.$params );
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment