made a copy
[atutor.git] / go.php
1 <?php
2 /****************************************************************/
3 /* ATutor                                                                                                               */
4 /****************************************************************/
5 /* Copyright (c) 2002-2008 by Greg Gay & Joel Kronenberg        */
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 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         foreach($var_query as $k=>$v){
79                 if ($k=='page_to_load'){
80                         continue;
81                 }
82
83                 //If mod_rewrite is on, the page# will be shown as <page#>.html.
84                 //in this case, parse the page number out.
85                 if ($k=='page'){
86                         if (preg_match('/(.*)\.html$/', $v, $matches)==1){
87                                 $v = $matches[1];
88                         }
89                 }
90                 $_GET[$k] = $v;
91                 $_REQUEST[$k] = $v;
92         }
93 }
94
95 /**
96  * Get path info
97  * @return the path info string
98  */
99 function getPathInfo(){
100         //Handles path_info in diff versions of PHP 
101         if(isset($_SERVER['ORIG_PATH_INFO']) && $_SERVER['ORIG_PATH_INFO']!=''){
102                 //if both PATH_INFO and ORIG_PATH_INFO are set, then decide which to use by str ops.
103                 if (isset($_SERVER['PATH_INFO'])){
104                         $pathpos = strpos($_SERVER['ORIG_PATH_INFO'], $_SERVER['SCRIPT_NAME']);
105                         $pathlen = strlen($_SERVER['SCRIPT_NAME']);
106                         if (substr($_SERVER['ORIG_PATH_INFO'], $pathpos + $pathlen) == $_SERVER['PATH_INFO']){
107                                 return $_SERVER['PATH_INFO'];
108                         } 
109                 }
110                 return $_SERVER['ORIG_PATH_INFO'];      
111         } else {
112                 return $_SERVER['PATH_INFO'];   
113         }
114 }
115 ?>