Merge pull request #8 from radiocontrolled/0004872
[atutor.git] / go.php
1 <?php
2 /****************************************************************/
3 /* ATutor                                                                                                               */
4 /****************************************************************/
5 /* Copyright (c) 2002-2010                                      */
6 /* Inclusive Design Institute                                   */
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 define('AT_INCLUDE_PATH', 'include/');
16 define('AT_REDIRECT_LOADED', true);
17
18 $_user_location = 'public';  //like browse, and registration, doesn't need username/passwords to get into
19
20 require_once(AT_INCLUDE_PATH . 'classes/UrlRewrite/UrlParser.class.php');
21 include_once(AT_INCLUDE_PATH.'config.inc.php');
22 require_once(AT_INCLUDE_PATH.'lib/constants.inc.php');
23 require_once(AT_INCLUDE_PATH.'lib/mysql_connect.inc.php');
24 //require_once(AT_INCLUDE_PATH.'vitals.inc.php');
25
26 //mimic config variables, vitals.inc.php 135-140
27 /* get config variables. if they're not in the db then it uses the installation default value in constants.inc.php */
28 $sql    = "SELECT * FROM ".TABLE_PREFIX."config";
29 $result = mysql_query($sql, $db);
30 while ($row = mysql_fetch_assoc($result)) { 
31         $_config[$row['name']] = $row['value'];
32 }
33
34 //Get path info
35 $pathinfo = getPathInfo();
36
37 $url_parser = new UrlParser($pathinfo);
38 $path_array =  $url_parser->getPathArray();
39 $_pretty_url_course_id = $path_array[0];
40 $obj = $path_array[1];
41
42 if (!$obj->isEmpty()){
43         /* 
44          * Addresses the issue for relative uri 
45          * @refer to constants.inc.php $_rel_link
46          */
47         $_rel_url = $obj->redirect();
48         $var_query = $obj->parsePrettyQuery();
49         save2Get($var_query);   //remake all the _GET and _REQUEST variables so that the vitals can use it
50
51         $_user_location = '';   //reset user_location so that the vital file in each page would validate
52         $pretty_current_page = $obj->getPage();
53         //If page not found, forward back to index.php
54         //ignore error, cause it will give a warning: No such file error, and can't send header.
55         if (!@include($pretty_current_page)){
56                 header('Location: '.AT_BASE_HREF.'index.php');
57                 exit;
58         } 
59 } elseif ($_pretty_url_course_id==0) {
60         //TODO: $_SESSION[course_id] seems to be resetted to 0, causing vitals.inc.php line 273 to redirect incorrectly.
61         //              Need to find out where exactly the course_id is being resetted. 
62         return;
63 //      header('location: '.AT_BASE_HREF.'bounce.php?course=0');
64 //      exit;
65 } elseif ($_pretty_url_course_id != ''){
66         header('location: '.AT_BASE_HREF.'bounce.php?course='.$_pretty_url_course_id);
67         exit;
68 }
69
70
71 /**
72  * This function will reconstruct all the $_GET variables.
73  * @param       array   consist of all the pathinfo variables in querystring format
74  */
75 function save2Get($var_query){
76     if (empty($var_query) || !is_array($var_query))
77         return;
78
79     //recreate URL querystring so we can use the PHP function - parse_str() later on
80     foreach($var_query as $k=>$v){
81         if ($k=='page_to_load'){
82             continue;
83         }
84
85         //If mod_rewrite is on, the page# will be shown as <page#>.html.
86         //in this case, parse the page number out.
87         if ($k=='page'){
88             if (preg_match('/(.*)\.html$/', $v, $matches)==1){
89                 $v = $matches[1];
90             }
91         }
92         $temp[] = $k . '=' . $v;
93     }
94     $var_query = implode(SEP, $temp);
95     parse_str($var_query, $output); //convert querystring to php array
96
97     //saves it to both GET and REQUEST
98     //TODO: Would overwrite POST value that has the same GET name within REQUEST. Fix this.
99     //Avoid the use of REQUEST in the code.
100     foreach($output as $k=>$v){
101         $_GET[$k] = $v;
102         $_REQUEST[$k] = $v;
103     }
104 }
105
106 /**
107  * Get path info
108  * @return the path info string
109  */
110 function getPathInfo(){
111         //Handles path_info in diff versions of PHP 
112         if(isset($_SERVER['ORIG_PATH_INFO']) && $_SERVER['ORIG_PATH_INFO']!=''){
113                 //if both PATH_INFO and ORIG_PATH_INFO are set, then decide which to use by str ops.
114                 if (isset($_SERVER['PATH_INFO'])){
115                         $pathpos = strpos($_SERVER['ORIG_PATH_INFO'], $_SERVER['SCRIPT_NAME']);
116                         $pathlen = strlen($_SERVER['SCRIPT_NAME']);
117                         if (substr($_SERVER['ORIG_PATH_INFO'], $pathpos + $pathlen) == $_SERVER['PATH_INFO']){
118                                 return $_SERVER['PATH_INFO'];
119                         } 
120                 }
121                 return $_SERVER['ORIG_PATH_INFO'];      
122         } else {
123                 return $_SERVER['PATH_INFO'];   
124         }
125 }
126 ?>