<?php

// Written by Bruce Webster
// Search /redirect 404

	require_once('config.php'); // global moodle config file.
	require_once('lib/moodlelib.php'); // global moodle config file.


//match up path...

$cleanr=clean_param(str_replace('%20',' ',str_replace('+',' ',substr($_SERVER['REQUEST_URI'],strrpos($_SERVER['PHP_SELF'],'/')+1))),PARAM_TEXT);

if(strpos($cleanr,'/')===false) { // if NOT a subdirectory, we will try a course shortname search.
    $c=get_recordset_sql("SELECT id FROM {$CFG->prefix}course WHERE shortname LIKE '$cleanr%' order by visible DESC",0,2);
    if(($c->_numOfRows)==1) {
        header("Location: $CFG->wwwroot/course/view.php?id={$c->fields['id']}"); exit;
    }
    // Not unique - go to course search page to display them all.
    header("Location: $CFG->wwwroot/course/search.php?search=".stripslashes($cleanr)); exit;
}

print_header('404:  Not Found',' ');

//Can't just call print_error because I don't want docs links to error code (unless this goes into core one day)
echo '<div class="box errorbox errorboxcontent"><p class="errormessage">'.get_string("invalidURL","error",$cleanr)."</p></div>";
echo "<div class=\"continuebutton\"><p><a href=\"{$CFG->wwwroot}/course/index.php\">".get_string("fulllistofcourses")."</a></p></div>";

print_continue('');
$dontdodocs=true;  // Hacky -- we do NOT want a doc link to a non-existant page! Req. patch to footer doc link printing.
print_footer();
?>
