2 /************************************************************************/
4 /************************************************************************/
5 /* Copyright (c) 2002-2008 by Greg Gay, Joel Kronenberg & Heidi Hazelton*/
6 /* Adaptive Technology Resource Centre / University of Toronto */
9 /* This program is free software. You can redistribute it and/or */
10 /* modify it under the terms of the GNU General Public License */
11 /* as published by the Free Software Foundation. */
12 /************************************************************************/
15 require_once('UrlRewrite.class.php');
19 * Class for rewriting pretty urls.
26 var $path_array; //an array [0]->course_id; [1]->class obj
29 function UrlParser($pathinfo=''){
31 $pathinfo = isset($_SERVER['PATH_INFO']) ? $_SERVER['PATH_INFO'] : '';
33 $this->parsePathInfo($pathinfo);
37 * This function will take the pathinfo and return an array of elements
38 * retrieved from the path info.
39 * An ATutor pathinfo will always be in the format of /<course_slug>/<type>/<parts>
40 * course_slug is the course_slug defined in course preference (or course id if it's empty)
41 * type is the folder, particularlly forums, content, tests, blogs, mods, etc.
42 * parts is the extra info about this url request.
43 * @param string the pathinfo from the URL
46 function parsePathinfo($pathinfo){
48 $pathinfo = strtolower($pathinfo);
50 //remove AT_PRETTY_URL_HANDLER from the path info.
51 if (($pos=strpos($pathinfo, AT_PRETTY_URL_HANDLER))!==FALSE){
52 $pathinfo = substr($pathinfo, $pos);
56 * matches[1] = course slug/id
58 * matches[3] = useless, just a place holder
59 * matches[4] = filename
60 * matches[5] = query string in pretty format
61 * @http://ca3.php.net/preg_match
63 if (strpos($pathinfo, 'mods')!==FALSE){
64 //If this is a mod, its file name will be longer with mods/ infront
65 preg_match('/^\/[\w\-]+\/?$|(\/[\w]+)(\/mods(\/[\w]+)+)\/([\w\_\.]+\.php)([\/\w\W]*)/', $pathinfo, $matches);
67 preg_match('/^\/[\w\-]+\/?$|(\/[\w]+)(([\/\w]*))\/([\w\_\.]+\.php)([\/\w\W]*)/', $pathinfo, $matches);
73 } elseif (sizeof($matches)==1){
74 //if the url consist of just the course slug, the size would be just 2
75 $matches[1] = $matches[0];
78 //take out the front slash
79 $matches[1] = preg_replace('/\//', '', $matches[1]);
80 $course_id = $matches[1];
82 //check if this is a course slug or course id.
83 if (preg_match('/^[\d]+$/', $matches[1])==0){
84 //it's a course slug, log into the course.
85 $sql = "SELECT course_id FROM ".TABLE_PREFIX."courses WHERE course_dir_name='$matches[1]'";
86 $result = mysql_query($sql, $db);
87 $row = mysql_fetch_assoc($result);
88 if ($row['course_id']!=''){
89 $course_id = $row['course_id'];
95 //Check if there are any matches for prettied query string, if not, use the actual query.
96 if (!isset($matches[5]) || $matches[5] == ''){
97 $matches[5] = $_SERVER['QUERY_STRING'];
100 //Create object based on this path.
101 $matches[2] = isset($matches[2]) ? $matches[2] : '';
102 $matches[4] = isset($matches[4]) ? $matches[4] : '';
103 $url_obj = new UrlRewrite($matches[2], $matches[4], $matches[5]);
105 $this->path_array = array($course_id, $url_obj);
110 * return the path array
112 function getPathArray(){
113 return $this->path_array;
118 * Returns course_id if config_[course_dir_name] is switched off,
119 * otherwise, return the course dir name.
120 * Called by vitals.inc.php
122 * @param int course id
123 * @return mixed course id if config[course_dir_name] is 0, course_dir_name otherwise
125 function getCourseDirName($course_id){
127 $course_id = intval($course_id);
129 $sql = "SELECT course_dir_name FROM ".TABLE_PREFIX."courses WHERE course_id=$course_id";
130 $result = mysql_query($sql, $db);
131 $row = mysql_fetch_assoc($result);
132 if ($row['course_dir_name']!=''){
133 $course_id = $row['course_dir_name'];