Esempio n. 1
0
/**
 * If pretty URL within admin config is switched on.  We will apply pretty URL 
 * to all the links in ATutor.  This function will authenticate itself towards the current pages.
 * In our definition, admins, login, registration pages shouldn't have pretty url applied.  However,
 * if one want to use url_rewrite on these pages, please force it by using the third parameter.  
 * Note: If system config has turned off this feature, $force will have no effect.
 * @param	string	the Url should be a relative link, have to improve this later on, to check if 
 *					it's a relative link, if not, truncate it.
 * @param	boolean	Available values are AT_PRETTY_URL_IS_HEADER, AT_PRETTY_URL_NOT_HEADER(default)
 *			use AT_PRETTY_URL_IS_HEADER if url_rewrite is used in php header('Location:..'), absolute path is needed for this.
 * @param	boolean	true to force the url_rewrite, false otheriwse.  False is the default.
 * @author	Harris Wong
 */
function url_rewrite($url, $is_rewriting_header = AT_PRETTY_URL_NOT_HEADER, $force = false)
{
    global $_config, $db;
    $url_parser = new UrlParser();
    $pathinfo = $url_parser->getPathArray();
    /* If this is any kind of admins, don't prettify the url
     * $_SESSION['is_guest'] is used to check against login/register/browse page, the links on this page will 
     * only be prettified when a user has logged in.
     * Had used $_SESSION[valid_user] before but it created this problem: 
     * http://www.atutor.ca/atutor/mantis/view.php?id=3426
     */
    if ($force || isset($_SESSION['course_id']) && $_SESSION['course_id'] > 0) {
        //if course id is defined, apply pretty url.
    } else {
        if (admin_authenticate(AT_ADMIN_PRIV_ADMIN, AT_PRIV_RETURN) || isset($_SESSION['privileges']) && admin_authenticate($_SESSION['privileges'], AT_PRIV_RETURN) || isset($_SESSION['is_guest']) && $_SESSION['is_guest'] == 1) {
            return $url;
        }
    }
    //if we allow pretty url in the system
    if ($_config['pretty_url'] > 0) {
        $course_id = 0;
        //If we allow course dir name from sys perf
        if ($_config['course_dir_name'] > 0) {
            if (preg_match('/bounce.php\\?course=([\\d]+)$/', $url, $matches) == 1) {
                // bounce has the highest priority, even if session is set, work on
                // bounce first.
                $course_id = $url_parser->getCourseDirName($matches[1]);
            } elseif (isset($_REQUEST['course'])) {
                //jump menu
                $course_id = $url_parser->getCourseDirName($_REQUEST['course']);
            } elseif (isset($_REQUEST['p_course'])) {
                // is set when guests access public course. @see bounce.php
                $course_id = $url_parser->getCourseDirName($_REQUEST['p_course']);
            } elseif (isset($_SESSION['course_id']) && $_SESSION['course_id'] > 0) {
                $course_id = $url_parser->getCourseDirName($_SESSION['course_id']);
            }
        } else {
            if (isset($_SESSION['course_id'])) {
                $course_id = $_SESSION['course_id'];
            }
        }
        $url = $pathinfo[1]->convertToPrettyUrl($course_id, $url);
    } elseif ($_config['course_dir_name'] > 0) {
        //enabled course directory name, disabled pretty url
        if (preg_match('/bounce.php\\?course=([\\d]+)$/', $url, $matches) == 1) {
            // bounce has the highest priority, even if session is set, work on
            // bounce first.
            $course_id = $url_parser->getCourseDirName($matches[1]);
        } elseif (isset($_REQUEST['course'])) {
            $course_id = $url_parser->getCourseDirName($_REQUEST['course']);
        } elseif (isset($_REQUEST['p_course'])) {
            // is set when guests access public course. @see bounce.php
            $course_id = $url_parser->getCourseDirName($_REQUEST['p_course']);
        } elseif (isset($_SESSION['course_id']) && $_SESSION['course_id'] > 0) {
            $course_id = $url_parser->getCourseDirName($_SESSION['course_id']);
        }
        $url = $pathinfo[1]->convertToPrettyUrl($course_id, $url);
    }
    //instead of putting AT_BASE_HREF in all the headers location, we will put it here.
    //Abs paths are required for pretty url because otherwise the url location will be appeneded.
    //ie.	ATutor_161/blogs/CoURSe_rOAd/blogs/view.php/ot/1/oid/1/ instead of
    //		ATutor_161/CoURSe_rOAd/blogs/view.php/ot/1/oid/1/
    if ($is_rewriting_header == true) {
        return AT_BASE_HREF . $url;
    }
    return $url;
}