made a copy
[atutor.git] / include / classes / UrlRewrite / UrlParser.class.php
1 <?php
2 /************************************************************************/
3 /* ATutor                                                                                                                               */
4 /************************************************************************/
5 /* Copyright (c) 2002-2008 by Greg Gay, Joel Kronenberg & Heidi Hazelton*/
6 /* Adaptive Technology Resource Centre / University of Toronto                  */
7 /* http://atutor.ca                                                                                                             */
8 /*                                                                                                                                              */
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 /************************************************************************/
13 // $Id$
14
15 require_once('UrlRewrite.class.php');
16
17 /**
18 * UrlParser
19 * Class for rewriting pretty urls.
20 * @access       public
21 * @author       Harris Wong
22 * @package      UrlParser
23 */
24 class UrlParser {
25         //Variables
26         var $path_array;        //an array [0]->course_id; [1]->class obj
27
28         // Constructor
29         function UrlParser($pathinfo=''){
30                 if ($pathinfo==''){
31                         $pathinfo = isset($_SERVER['PATH_INFO']) ? $_SERVER['PATH_INFO'] : '';
32                 }
33                 $this->parsePathInfo($pathinfo);
34         }
35
36         /**
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
44          * @access      private
45          */
46         function parsePathinfo($pathinfo){
47                 global $db;
48                 $pathinfo = strtolower($pathinfo);
49
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);
53                 }
54
55                 /* 
56                  * matches[1] = course slug/id
57                  * matches[2] = path
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
62                  */
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);                   
66                 } else {
67                         preg_match('/^\/[\w\-]+\/?$|(\/[\w]+)(([\/\w]*))\/([\w\_\.]+\.php)([\/\w\W]*)/', $pathinfo, $matches);
68                 }
69
70                 if (empty($matches)){
71                         //no matches.
72                         $matches[1] = 0;
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];
76                 } 
77
78                 //take out the front slash
79                 $matches[1] = preg_replace('/\//', '', $matches[1]);
80                 $course_id = $matches[1];
81
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'];
90                         } else {
91                                 $course_id = 0;
92                         }
93                 }
94
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'];
98                 }
99
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]);
104
105                 $this->path_array = array($course_id, $url_obj);
106         }
107
108         
109         /**
110          * return the path array
111          */
112         function getPathArray(){
113                 return $this->path_array;
114         }
115
116
117         /**
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
121          *
122          * @param       int             course id
123          * @return      mixed   course id if config[course_dir_name] is 0, course_dir_name otherwise
124          */
125         function getCourseDirName($course_id){
126                 global $db; 
127                 $course_id = intval($course_id);
128
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'];
134                 } 
135
136                 return $course_id;
137         }
138 }
139 ?>