remove old readme
[atutor.git] / docs / include / classes / UrlRewrite / UrlParser.class.php
1 <?php
2 /************************************************************************/
3 /* ATutor                                                                                                                               */
4 /************************************************************************/
5 /* Copyright (c) 2002-2010                                              */
6 /* Inclusive Design Institute                                           */
7 /* http://atutor.ca                                                     */
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 /************************************************************************/
12 // $Id$
13
14 require_once('UrlRewrite.class.php');
15
16 /**
17 * UrlParser
18 * Class for rewriting pretty urls.
19 * @access       public
20 * @author       Harris Wong
21 * @package      UrlParser
22 */
23 class UrlParser {
24         //Variables
25         var $path_array;        //an array [0]->course_id; [1]->class obj
26
27         // Constructor
28         function UrlParser($pathinfo=''){
29                 if ($pathinfo==''){
30                         $pathinfo = isset($_SERVER['PATH_INFO']) ? $_SERVER['PATH_INFO'] : '';
31                 }
32                 $this->parsePathInfo($pathinfo);
33         }
34
35         /**
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
43          * @access      private
44          */
45         function parsePathinfo($pathinfo){
46                 global $db;
47                 $pathinfo = strtolower($pathinfo);
48
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);
52                 }
53
54                 /* 
55                  * matches[1] = course slug/id
56                  * matches[2] = path
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
61                  */
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);                   
65                 } else {
66                         preg_match('/^\/[\w\-]+\/?$|(\/[\w]+)(([\/\w]*))\/([\w\_\.]+\.php)([\/\w\W]*)/', $pathinfo, $matches);
67                 }
68
69                 if (empty($matches)){
70                         //no matches.
71                         $matches[1] = 0;
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];
75                 } 
76
77                 //take out the front slash
78                 $matches[1] = preg_replace('/\//', '', $matches[1]);
79                 $course_id = $matches[1];
80
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'];
89                         } else {
90                                 $course_id = 0;
91                         }
92                 }
93
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'];
97                 }
98
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]);
103
104                 $this->path_array = array($course_id, $url_obj);
105         }
106
107         
108         /**
109          * return the path array
110          */
111         function getPathArray(){
112                 return $this->path_array;
113         }
114
115
116         /**
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
120          *
121          * @param       int             course id
122          * @return      mixed   course id if config[course_dir_name] is 0, course_dir_name otherwise
123          */
124         function getCourseDirName($course_id){
125                 global $db; 
126                 $course_id = intval($course_id);
127
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'];
133                 } 
134
135                 return $course_id;
136         }
137 }
138 ?>