2 /************************************************************************/
4 /************************************************************************/
5 /* Copyright (c) 2002-2010 */
6 /* Inclusive Design Institute */
8 /* This program is free software. You can redistribute it and/or */
9 /* modify it under the terms of the GNU General Public License */
10 /* as published by the Free Software Foundation. */
11 /************************************************************************/
14 require_once('UrlRewrite.class.php');
18 * Class for rewriting pretty urls.
25 var $path_array; //an array [0]->course_id; [1]->class obj
28 function UrlParser($pathinfo=''){
30 $pathinfo = isset($_SERVER['PATH_INFO']) ? $_SERVER['PATH_INFO'] : '';
32 $this->parsePathInfo($pathinfo);
36 * This function will take the pathinfo and return an array of elements
37 * retrieved from the path info.
38 * An ATutor pathinfo will always be in the format of /<course_slug>/<type>/<parts>
39 * course_slug is the course_slug defined in course preference (or course id if it's empty)
40 * type is the folder, particularlly forums, content, tests, blogs, mods, etc.
41 * parts is the extra info about this url request.
42 * @param string the pathinfo from the URL
45 function parsePathinfo($pathinfo){
47 $pathinfo = strtolower($pathinfo);
49 //remove AT_PRETTY_URL_HANDLER from the path info.
50 if (($pos=strpos($pathinfo, AT_PRETTY_URL_HANDLER))!==FALSE){
51 $pathinfo = substr($pathinfo, $pos);
55 * matches[1] = course slug/id
57 * matches[3] = useless, just a place holder
58 * matches[4] = filename
59 * matches[5] = query string in pretty format
60 * @http://ca3.php.net/preg_match
62 if (strpos($pathinfo, 'mods')!==FALSE){
63 //If this is a mod, its file name will be longer with mods/ infront
64 preg_match('/^\/[\w\-]+\/?$|(\/[\w]+)(\/mods(\/[\w]+)+)\/([\w\_\.]+\.php)([\/\w\W]*)/', $pathinfo, $matches);
66 preg_match('/^\/[\w\-]+\/?$|(\/[\w]+)(([\/\w]*))\/([\w\_\.]+\.php)([\/\w\W]*)/', $pathinfo, $matches);
72 } elseif (sizeof($matches)==1){
73 //if the url consist of just the course slug, the size would be just 2
74 $matches[1] = $matches[0];
77 //take out the front slash
78 $matches[1] = preg_replace('/\//', '', $matches[1]);
79 $course_id = $matches[1];
81 //check if this is a course slug or course id.
82 if (preg_match('/^[\d]+$/', $matches[1])==0){
83 //it's a course slug, log into the course.
84 $sql = "SELECT course_id FROM ".TABLE_PREFIX."courses WHERE course_dir_name='$matches[1]'";
85 $result = mysql_query($sql, $db);
86 $row = mysql_fetch_assoc($result);
87 if ($row['course_id']!=''){
88 $course_id = $row['course_id'];
94 //Check if there are any matches for prettied query string, if not, use the actual query.
95 if (!isset($matches[5]) || $matches[5] == ''){
96 $matches[5] = $_SERVER['QUERY_STRING'];
99 //Create object based on this path.
100 $matches[2] = isset($matches[2]) ? $matches[2] : '';
101 $matches[4] = isset($matches[4]) ? $matches[4] : '';
102 $url_obj = new UrlRewrite($matches[2], $matches[4], $matches[5]);
104 $this->path_array = array($course_id, $url_obj);
109 * return the path array
111 function getPathArray(){
112 return $this->path_array;
117 * Returns course_id if config_[course_dir_name] is switched off,
118 * otherwise, return the course dir name.
119 * Called by vitals.inc.php
121 * @param int course id
122 * @return mixed course id if config[course_dir_name] is 0, course_dir_name otherwise
124 function getCourseDirName($course_id){
126 $course_id = intval($course_id);
128 $sql = "SELECT course_dir_name FROM ".TABLE_PREFIX."courses WHERE course_id=$course_id";
129 $result = mysql_query($sql, $db);
130 $row = mysql_fetch_assoc($result);
131 if ($row['course_dir_name']!=''){
132 $course_id = $row['course_dir_name'];