Calculate the ATutor installation path and save into database at installation (or...
authorcindy li <cli@ocad.ca>
Tue, 23 Aug 2011 15:59:41 +0000 (15:59 -0000)
committercindy li <cli@ocad.ca>
Tue, 23 Aug 2011 15:59:41 +0000 (15:59 -0000)
docs/include/lib/constants.inc.php
docs/include/vitals.inc.php
docs/install/include/common.inc.php
docs/install/include/step3.php
docs/install/include/ustep3.php
docs/install/install.php
docs/install/upgrade.php

index 7f99877..930ca91 100644 (file)
@@ -195,14 +195,7 @@ if (($temp = strpos($_base_href, AT_PRETTY_URL_HANDLER)) > 0){
 
 }
 $_base_href    = substr($_base_href, 0, $endpos);
-$_base_path = $_session_path = substr($_base_href, strlen($server_protocol . $_SERVER['HTTP_HOST']));
-
-// $_session_path is used to attach the session ID on @ see include/vitals.inc.php - session_set_cookie_params()
-// $_session_path is ATutor installation path, which is not captured by $_base_path in the case that the sent-in
-// URL ends with '/'.
-if (substr($_SERVER['PHP_SELF'], -1) == '/') {
-       $_session_path = substr($_base_path, 0, strrpos(substr($_base_path, 0, -1), '/') + 1);
-}
+$_base_path = substr($_base_href, strlen($server_protocol . $_SERVER['HTTP_HOST']));
 
 define('AT_BASE_HREF', $_base_href);
 
index b5a459f..016ef46 100644 (file)
@@ -102,13 +102,12 @@ function check_session()
  *
  * 0. load config.inc.php
  * 1. load constants
- * 2. initilize session
- * 3. load language constants
+ * 2. initialize db connection and populate $_config
+ * 3. initialize session
  * 4. enable output compression
- * 5. initilize db connection
- * 6. load cache library
- * 7. initilize session localization
- * 8. load ContentManagement/output/Savant/Message libraries
+ * 5. validate login user
+ * 6. load language
+ * 7. load cache/ContentManagement/output/Savant/Message libraries
  ***/
 
 /**** 0. start system configuration options block ****/
@@ -129,90 +128,89 @@ function check_session()
                header('Location: ' . $relative_path . 'install/not_installed.php');
                exit;
        }
-/*** end system config block ****/
+/*** end system config block ***/
 
 /*** 1. constants ***/
-       if (!defined('AT_REDIRECT_LOADED')){
-               require_once(AT_INCLUDE_PATH.'lib/constants.inc.php');
-       }
+if (!defined('AT_REDIRECT_LOADED')){
+       require_once(AT_INCLUDE_PATH.'lib/constants.inc.php');
+}
 
-/***** 2. start session initilization block ****/
-       if (headers_sent()) {
-               require_once(AT_INCLUDE_PATH . 'classes/ErrorHandler/ErrorHandler.class.php');
-               $err = new ErrorHandler();
-               trigger_error('VITAL#<br /><br /><code><strong>An error occurred. Output sent before it should have. Please correct the above error(s).' . '</strong></code><br /><hr /><br />', E_USER_ERROR);
-       }
+/*** 2. initialize db connection and populate $_config ***/
 
-       @set_time_limit(0);
-       @ini_set('session.gc_maxlifetime', '36000'); /* 10 hours */
-       @session_cache_limiter('private, must-revalidate');
-       session_name('ATutorID');
-       error_reporting(AT_ERROR_REPORTING);
+if (!defined('AT_REDIRECT_LOADED')){
+       require_once(AT_INCLUDE_PATH.'lib/mysql_connect.inc.php');
+}
 
-       if (headers_sent()) {
-               require_once(AT_INCLUDE_PATH . 'classes/ErrorHandler/ErrorHandler.class.php');
-               $err = new ErrorHandler();
-               trigger_error('VITAL#<br /><code><strong>Headers already sent. ' .
-                                               'Cannot initialise session.</strong></code><br /><hr /><br />', E_USER_ERROR);
-               exit;
-       }
+/* get config variables. if they're not in the db then it uses the installation default value in constants.inc.php */
+$sql    = "SELECT * FROM ".TABLE_PREFIX."config";
+$result = mysql_query($sql, $db);
+while ($row = mysql_fetch_assoc($result)) { 
+       $_config[$row['name']] = $row['value'];
+}
+
+/***** 3. start session initilization block *****/
+if (headers_sent()) {
+       require_once(AT_INCLUDE_PATH . 'classes/ErrorHandler/ErrorHandler.class.php');
+       $err = new ErrorHandler();
+       trigger_error('VITAL#<br /><br /><code><strong>An error occurred. Output sent before it should have. Please correct the above error(s).' . '</strong></code><br /><hr /><br />', E_USER_ERROR);
+}
+
+@set_time_limit(0);
+@ini_set('session.gc_maxlifetime', '36000'); /* 10 hours */
+@session_cache_limiter('private, must-revalidate');
+session_name('ATutorID');
+error_reporting(AT_ERROR_REPORTING);
+
+if (headers_sent()) {
+       require_once(AT_INCLUDE_PATH . 'classes/ErrorHandler/ErrorHandler.class.php');
+       $err = new ErrorHandler();
+       trigger_error('VITAL#<br /><code><strong>Headers already sent. ' .
+                                       'Cannot initialise session.</strong></code><br /><hr /><br />', E_USER_ERROR);
+       exit;
+}
 
     $isHttps = (!isset($_SERVER['HTTPS']) || $_SERVER['HTTPS'] != "on")
               ? false
               : true;
     ob_start();
-    session_set_cookie_params(0, $_session_path, "", $isHttps);
-       session_start();
-       
-       // Regenerate session id at every page refresh to prevent CSRF
-       $valid_session = true;
-       if (count($_SESSION) == 0) {
-               regenerate_session();
-       } else {
-               $valid_session = check_session();
-       }
-       
-       $str = ob_get_contents();
-       ob_end_clean();
-       unregister_GLOBALS();
-       
-       // Re-direct to login page at a potential session hijack
-       if (!$valid_session) {
-               $_SESSION = array();
-               header('Location: '.AT_BASE_HREF.'login.php');
-               exit;
-       }
-       
-       if ($str) {
-               require_once(AT_INCLUDE_PATH . 'classes/ErrorHandler/ErrorHandler.class.php');
-               $err = new ErrorHandler();
-               trigger_error('VITAL#<br /><code><strong>Error initializing session. ' .
-                                               'Please varify that session.save_path is correctly set in your php.ini file ' .
-                                               'and the directory exists.</strong></code><br /><hr /><br />', E_USER_ERROR);
-               exit;
-       }
-       
+    session_set_cookie_params(0, $_config["session_path"], "", $isHttps);
+session_start();
 
-/***** end session initilization block ****/
+// Regenerate session id at every page refresh to prevent CSRF
+$valid_session = true;
+if (count($_SESSION) == 0) {
+       regenerate_session();
+} else {
+       $valid_session = check_session();
+}
 
-// 4. enable output compression, if it isn't already enabled:
-if ((@ini_get('output_handler') == '') && (@ini_get('zlib.output_handler') == '')) {
-       @ini_set('zlib.output_compression', 1);
+$str = ob_get_contents();
+ob_end_clean();
+unregister_GLOBALS();
+
+// Re-direct to login page at a potential session hijack
+if (!$valid_session) {
+       $_SESSION = array();
+       header('Location: '.AT_BASE_HREF.'login.php');
+       exit;
 }
 
-/* 5. database connection */
-if (!defined('AT_REDIRECT_LOADED')){
-       require_once(AT_INCLUDE_PATH.'lib/mysql_connect.inc.php');
+if ($str) {
+       require_once(AT_INCLUDE_PATH . 'classes/ErrorHandler/ErrorHandler.class.php');
+       $err = new ErrorHandler();
+       trigger_error('VITAL#<br /><code><strong>Error initializing session. ' .
+                                       'Please varify that session.save_path is correctly set in your php.ini file ' .
+                                       'and the directory exists.</strong></code><br /><hr /><br />', E_USER_ERROR);
+       exit;
 }
+/***** end session initilization block ****/
 
-/* get config variables. if they're not in the db then it uses the installation default value in constants.inc.php */
-$sql    = "SELECT * FROM ".TABLE_PREFIX."config";
-$result = mysql_query($sql, $db);
-while ($row = mysql_fetch_assoc($result)) { 
-       $_config[$row['name']] = $row['value'];
+/**** 4. enable output compression, if it isn't already enabled: ****/
+if ((@ini_get('output_handler') == '') && (@ini_get('zlib.output_handler') == '')) {
+       @ini_set('zlib.output_compression', 1);
 }
 
-//Check if users=valid
+/**** 5. validate login user ****/
 if (!isset($_SESSION['course_id']) && !isset($_SESSION['valid_user']) && (!isset($_user_location) || $_user_location != 'public') && !isset($_pretty_url_course_id)) {
        if (isset($in_get) && $in_get && (($pos = strpos($_SERVER['PHP_SELF'], 'get.php/')) !== FALSE)) {
                $redirect = substr($_SERVER['PHP_SELF'], 0, $pos) . 'login.php';
@@ -262,105 +260,105 @@ if ($_config['time_zone']) {
                @putenv("TZ={$_config['time_zone']}");
        }
 }
-/***** 7. start language block *****/
-       // set current language
-       require(AT_INCLUDE_PATH . '../mods/_core/languages/classes/LanguageManager.class.php');
-       $languageManager = new LanguageManager();
+/***** 6. load language *****/
+// set current language
+require(AT_INCLUDE_PATH . '../mods/_core/languages/classes/LanguageManager.class.php');
+$languageManager = new LanguageManager();
 
-       $myLang =& $languageManager->getMyLanguage();
+$myLang =& $languageManager->getMyLanguage();
 
-       if ($myLang === FALSE) {
-               echo 'There are no languages installed!';
-               exit;
-       }
-       $myLang->saveToSession();
-       if (isset($_GET['lang']) && $_SESSION['valid_user']) {
-               if ($_SESSION['course_id'] == -1) {
-                       $myLang->saveToPreferences($_SESSION['login'], 1);      //1 for admin                   
-               } else {
-                       $myLang->saveToPreferences($_SESSION['member_id'], 0);  //0 for non-admin
-               }
+if ($myLang === FALSE) {
+       echo 'There are no languages installed!';
+       exit;
+}
+$myLang->saveToSession();
+if (isset($_GET['lang']) && $_SESSION['valid_user']) {
+       if ($_SESSION['course_id'] == -1) {
+               $myLang->saveToPreferences($_SESSION['login'], 1);      //1 for admin                   
+       } else {
+               $myLang->saveToPreferences($_SESSION['member_id'], 0);  //0 for non-admin
        }
-       $myLang->sendContentTypeHeader();
+}
+$myLang->sendContentTypeHeader();
 
-       /* set right-to-left language */
-       $rtl = '';
-       if ($myLang->isRTL()) {
-               $rtl = 'rtl_'; /* basically the prefix to a rtl variant directory/filename. eg. rtl_tree */
-       }
+/* set right-to-left language */
+$rtl = '';
+if ($myLang->isRTL()) {
+       $rtl = 'rtl_'; /* basically the prefix to a rtl variant directory/filename. eg. rtl_tree */
+}
 /***** end language block ****/
 
-/* 8. load common libraries */
-       require(AT_INCLUDE_PATH.'classes/ContentManager.class.php');  /* content management class */
-       require_once(AT_INCLUDE_PATH.'lib/output.inc.php');           /* output functions */
-       if (!(defined('AT_REDIRECT_LOADED'))){
-               require_once(AT_INCLUDE_PATH . 'classes/UrlRewrite/UrlParser.class.php');       /* pretty url tool */
-       }
-       require(AT_INCLUDE_PATH.'classes/Savant2/Savant2.php');       /* for the theme and template management */
-
-       // set default template paths:
-       $savant = new Savant2();
-       $savant->addPath('template', AT_INCLUDE_PATH . '../themes/default/');
+/* 7. load common libraries */
+require(AT_INCLUDE_PATH.'classes/ContentManager.class.php');  /* content management class */
+require_once(AT_INCLUDE_PATH.'lib/output.inc.php');           /* output functions */
+if (!(defined('AT_REDIRECT_LOADED'))){
+       require_once(AT_INCLUDE_PATH . 'classes/UrlRewrite/UrlParser.class.php');       /* pretty url tool */
+}
+require(AT_INCLUDE_PATH.'classes/Savant2/Savant2.php');       /* for the theme and template management */
+
+// set default template paths:
+$savant = new Savant2();
+$savant->addPath('template', AT_INCLUDE_PATH . '../themes/default/');
+
+//if user has requested theme change, make the change here
+if (($_POST['theme'] || $_POST['mobile_theme']) && $_POST['submit']) {
+    $_SESSION['prefs']['PREF_THEME'] = $addslashes($_POST['theme']);
+    $_SESSION['prefs']['PREF_MOBILE_THEME'] = $addslashes($_POST['mobile_theme']);
+} else if ($_POST['set_default']) {
+    $_SESSION['prefs']['PREF_THEME'] = 'default';
+    $_SESSION['prefs']['PREF_MOBILE_THEME'] = 'mobile';
+}
 
-       //if user has requested theme change, make the change here
-       if (($_POST['theme'] || $_POST['mobile_theme']) && $_POST['submit']) {
-           $_SESSION['prefs']['PREF_THEME'] = $addslashes($_POST['theme']);
-           $_SESSION['prefs']['PREF_MOBILE_THEME'] = $addslashes($_POST['mobile_theme']);
-       } else if ($_POST['set_default']) {
-           $_SESSION['prefs']['PREF_THEME'] = 'default';
-           $_SESSION['prefs']['PREF_MOBILE_THEME'] = 'mobile';
-       }
-       
-       // Reset PREF_THEME when:
-       // 1. If PREF_THEME is not set 
-       // 2. The request is from the mobile device but PREF_THEME is not a mobile theme 
-       if (!isset($_SESSION['prefs']['PREF_THEME']) ||
-           $_SESSION['prefs']['PREF_THEME'] == "" ||
-           (is_mobile_device() && !is_mobile_theme($_SESSION['prefs']['PREF_THEME']))) {
-               // get default
-               $default_theme = get_default_theme();
-               
-               $_SESSION['prefs']['PREF_THEME'] = $default_theme['dir_name'];
-       }
-       
-       if (!is_dir(AT_INCLUDE_PATH . '../themes/' . $_SESSION['prefs']['PREF_THEME']) || $_SESSION['prefs']['PREF_THEME'] == '') {
-               $_SESSION['prefs']['PREF_THEME'] = get_system_default_theme();
-       }
+// Reset PREF_THEME when:
+// 1. If PREF_THEME is not set 
+// 2. The request is from the mobile device but PREF_THEME is not a mobile theme 
+if (!isset($_SESSION['prefs']['PREF_THEME']) ||
+    $_SESSION['prefs']['PREF_THEME'] == "" ||
+    (is_mobile_device() && !is_mobile_theme($_SESSION['prefs']['PREF_THEME']))) {
+       // get default
+       $default_theme = get_default_theme();
        
-       // use "mobile" theme for mobile devices. For now, there's only one mobile theme and it's hardcoded.
-       // When more mobile themes come in, this should be changed.
-       if (isset($_SESSION['prefs']['PREF_THEME']) && file_exists(AT_INCLUDE_PATH . '../themes/' . $_SESSION['prefs']['PREF_THEME']) && isset($_SESSION['valid_user']) && $_SESSION['valid_user']) {
-               if ($_SESSION['course_id'] == -1) {
-                       if ($_SESSION['prefs']['PREF_THEME'] == '' || !is_dir(AT_INCLUDE_PATH . '../themes/' . $_SESSION['prefs']['PREF_THEME'])) {
-                               $_SESSION['prefs']['PREF_THEME'] = get_system_default_theme();
-                       }
+       $_SESSION['prefs']['PREF_THEME'] = $default_theme['dir_name'];
+}
+
+if (!is_dir(AT_INCLUDE_PATH . '../themes/' . $_SESSION['prefs']['PREF_THEME']) || $_SESSION['prefs']['PREF_THEME'] == '') {
+       $_SESSION['prefs']['PREF_THEME'] = get_system_default_theme();
+}
+
+// use "mobile" theme for mobile devices. For now, there's only one mobile theme and it's hardcoded.
+// When more mobile themes come in, this should be changed.
+if (isset($_SESSION['prefs']['PREF_THEME']) && file_exists(AT_INCLUDE_PATH . '../themes/' . $_SESSION['prefs']['PREF_THEME']) && isset($_SESSION['valid_user']) && $_SESSION['valid_user']) {
+       if ($_SESSION['course_id'] == -1) {
+               if ($_SESSION['prefs']['PREF_THEME'] == '' || !is_dir(AT_INCLUDE_PATH . '../themes/' . $_SESSION['prefs']['PREF_THEME'])) {
+                       $_SESSION['prefs']['PREF_THEME'] = get_system_default_theme();
+               }
+       } else {
+               //check if enabled
+               $sql    = "SELECT status FROM ".TABLE_PREFIX."themes WHERE dir_name = '".$_SESSION['prefs']['PREF_THEME']."'";
+               $result = mysql_query($sql, $db);
+               $row = mysql_fetch_assoc($result);
+               if ($row['status'] > 0) {
                } else {
-                       //check if enabled
-                       $sql    = "SELECT status FROM ".TABLE_PREFIX."themes WHERE dir_name = '".$_SESSION['prefs']['PREF_THEME']."'";
-                       $result = mysql_query($sql, $db);
-                       $row = mysql_fetch_assoc($result);
-                       if ($row['status'] > 0) {
-                       } else {
-                               // get default
-                               $default_theme = get_default_theme();
-                               if (!is_dir(AT_INCLUDE_PATH . '../themes/' . $default_theme['dir_name'])) {
-                                       $default_theme = array('dir_name' => get_system_default_theme());
-                               }
-                               $_SESSION['prefs']['PREF_THEME'] = $default_theme['dir_name'];
+                       // get default
+                       $default_theme = get_default_theme();
+                       if (!is_dir(AT_INCLUDE_PATH . '../themes/' . $default_theme['dir_name'])) {
+                               $default_theme = array('dir_name' => get_system_default_theme());
                        }
+                       $_SESSION['prefs']['PREF_THEME'] = $default_theme['dir_name'];
                }
        }
-       
-       $savant->addPath('template', AT_INCLUDE_PATH . '../themes/' . $_SESSION['prefs']['PREF_THEME'] . '/');
-       require(AT_INCLUDE_PATH . '../themes/' . $_SESSION['prefs']['PREF_THEME'] . '/theme.cfg.php');
+}
 
-       require(AT_INCLUDE_PATH.'classes/Message/Message.class.php');
-       $msg = new Message($savant);
+$savant->addPath('template', AT_INCLUDE_PATH . '../themes/' . $_SESSION['prefs']['PREF_THEME'] . '/');
+require(AT_INCLUDE_PATH . '../themes/' . $_SESSION['prefs']['PREF_THEME'] . '/theme.cfg.php');
 
-       $contentManager = new ContentManager($db, isset($_SESSION['course_id']) ? $_SESSION['course_id'] : $_GET['p_course']);
-       $contentManager->initContent();
-/**************************************************/
+require(AT_INCLUDE_PATH.'classes/Message/Message.class.php');
+$msg = new Message($savant);
 
+$contentManager = new ContentManager($db, isset($_SESSION['course_id']) ? $_SESSION['course_id'] : $_GET['p_course']);
+$contentManager->initContent();
+
+/**************************************************/
 require(AT_INCLUDE_PATH.'phpCache/phpCache.inc.php'); // cache library
 require(AT_INCLUDE_PATH.'lib/utf8.php');                       //UTF-8 multibyte library
 
index 53e44b3..b323749 100644 (file)
@@ -237,6 +237,30 @@ function print_meta_redirect(){
        return $html;
 }
 
+/**
+ * This function calculate the ATutor installation path
+ * @access  public
+ * @param   none
+ * @return  string: atutor installation path, for example: /ATutor/
+ */
+function get_atutor_installation_path() {
+       /* get the base url     */
+       if (isset($_SERVER['HTTPS']) && (strtolower($_SERVER['HTTPS']) == 'on')) {
+               $server_protocol = 'https://';
+       } else {
+               $server_protocol = 'http://';
+       }
+
+       $dir_deep        = substr_count(AT_INCLUDE_PATH, '..');
+       $url_parts       = explode('/', $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF']);
+       $base_href       = array_slice($url_parts, 0, count($url_parts) - $dir_deep-2);
+       $base_href       = $server_protocol . implode('/', $base_href).'/';
+       
+       $session_path = substr($base_href, strlen($server_protocol . $_SERVER['HTTP_HOST']));
+       
+       return $session_path;
+}
+
 /**
  * This function is used for printing variables for debugging.
  * @access  public
index 87f34f9..11ee04b 100644 (file)
@@ -120,6 +120,11 @@ if(isset($_POST['submit']) && ($_POST['action'] == 'process')) {
                $sql = "INSERT INTO ".$_POST['step2']['tb_prefix']."config VALUES ('set_utf8', '1')";
                mysql_query($sql ,$db);
 
+               // Calculate the ATutor installation path and save into database for the usage of
+               // session associated path @ include/vitals.inc.php
+               $sql = "INSERT INTO ".$_POST['step2']['tb_prefix']."config VALUES ('session_path', '".get_atutor_installation_path()."')";
+               mysql_query($sql ,$db);
+               
                unset($_POST['admin_username']);
                unset($_POST['form_admin_password_hidden']);
                unset($_POST['admin_email']);
index 1f69ac2..036db89 100644 (file)
@@ -217,7 +217,7 @@ if(isset($_POST['submit']) && ($_POST['action'] == 'process')) {
        /* or delete it from the modules table. or maybe disable it instead? */
        if (version_compare($_POST['step1']['old_version'], '1.5.1', '>')) {
                define('TABLE_PREFIX', $_POST['step1']['tb_prefix']);
-               require(AT_INCLUDE_PATH . '../mods/_core/modules/classes/Module.class.php');
+               require(AT_INCLUDE_PATH . '../../mods/_core/modules/classes/Module.class.php');
                $moduleFactory = new ModuleFactory(FALSE);
                $module_list =& $moduleFactory->getModules(AT_MODULE_STATUS_DISABLED | AT_MODULE_STATUS_ENABLED);
                $keys = array_keys($module_list);
@@ -244,6 +244,14 @@ if(isset($_POST['submit']) && ($_POST['action'] == 'process')) {
                mysql_query($sql, $db);
        }
 
+       /* Saved the atutor installation path into "config" table after 2.0.2 */
+       if (version_compare($_POST['step1']['new_version'], '2.0.2', '>')) {
+               // Calculate the ATutor installation path and save into database for the usage of
+               // session associated path @ include/vitals.inc.php
+               $sql = "INSERT INTO ".$_POST['step1']['tb_prefix']."config VALUES ('session_path', '".get_atutor_installation_path()."')";
+               mysql_query($sql ,$db);
+       }
+
        if (!isset($errors)) {
                unset($errors);
                unset($_POST['submit']);
index 3d59cb3..eaed621 100644 (file)
@@ -10,6 +10,9 @@
 /************************************************************************/
 // $Id$
 
+// Be careful at modifying AT_INCLUDE_PATH constant which is used by 
+// get_atutor_installation_path() @ common.inc.php. Must make sure the 
+// function doesn't get broken at its modification.
 define('AT_INCLUDE_PATH', 'include/');
 require(AT_INCLUDE_PATH.'common.inc.php');
 
index 97f801d..bc434a1 100644 (file)
 /************************************************************************/
 // $Id$
 
-define('AT_INCLUDE_PATH', '../include/');
+// Be careful at modifying AT_INCLUDE_PATH constant which is used by 
+// get_atutor_installation_path() @ common.inc.php. Must make sure the 
+// function doesn't get broken at its modification.
+define('AT_INCLUDE_PATH', 'include/');
 
 if (isset($_POST['new_version'])) {
        $new_version = $_POST['new_version'];
@@ -24,7 +27,7 @@ if (!isset($step) || ($step == 0)) {
        $step = 1;
 }
 
-require('include/common.inc.php');
+require(AT_INCLUDE_PATH.'common.inc.php');
 
 if (($step == 2) && isset($_POST['override']) && ($_POST['override'] == 0)) {
        header('Location: index.php');