PageRenderTime 157ms CodeModel.GetById 40ms RepoModel.GetById 0ms app.codeStats 0ms

/mahara_login.php

https://github.com/atutor/mahara
PHP | 199 lines | 123 code | 37 blank | 39 comment | 25 complexity | fdf921ec607882fda894bfddbd7aefbd MD5 | raw file
  1. <?php
  2. /*
  3. This belongs to the ATutor Mahara module page. It is called within an iframe or
  4. a new window from index.php and allows a user to access
  5. his/her ePortfolio account on Mahara through their account on ATutor.
  6. Login information for Mahara is passed using cookies (password encrypted in SHA1).
  7. This is to avoid conflicting sessions between ATutor and Mahara from within
  8. the same script.
  9. by: Boon-Hau Teh
  10. */
  11. $_user_location = 'public';
  12. define('AT_INCLUDE_PATH', '../../include/');
  13. /*~~~~~~~~~~~~~few essentials copied from ATutor's vitals.inc.php~~~~~~~~~~~~*/
  14. /**** 0. start system configuration options block ****/
  15. error_reporting(0);
  16. if (!defined(AT_REDIRECT_LOADED)){
  17. include_once(AT_INCLUDE_PATH.'config.inc.php');
  18. }
  19. error_reporting(AT_ERROR_REPORTING);
  20. if (!defined('AT_INSTALL') || !AT_INSTALL) {
  21. header('Cache-Control: no-store, no-cache, must-revalidate');
  22. header('Pragma: no-cache');
  23. $relative_path = substr(AT_INCLUDE_PATH, 0, -strlen('include/'));
  24. header('Location: ' . $relative_path . 'install/not_installed.php');
  25. exit;
  26. }
  27. /*** 1. constants ***/
  28. if (!defined(AT_REDIRECT_LOADED)){
  29. require_once(AT_INCLUDE_PATH.'lib/constants.inc.php');
  30. }
  31. $db = @mysql_connect(DB_HOST . ':' . DB_PORT, DB_USER, DB_PASSWORD);
  32. if (!$db) {
  33. /* AT_ERROR_NO_DB_CONNECT */
  34. require_once(AT_INCLUDE_PATH . 'classes/ErrorHandler/ErrorHandler.class.php');
  35. $err =& new ErrorHandler();
  36. trigger_error('VITAL#Unable to connect to db.', E_USER_ERROR);
  37. exit;
  38. }
  39. if (!@mysql_select_db(DB_NAME, $db)) {
  40. require_once(AT_INCLUDE_PATH . 'classes/ErrorHandler/ErrorHandler.class.php');
  41. $err =& new ErrorHandler();
  42. trigger_error('VITAL#DB connection established, but database "'.DB_NAME.'" cannot be selected.',
  43. E_USER_ERROR);
  44. exit;
  45. }
  46. /* get config variables. if they're not in the db then it uses the installation default value in constants.inc.php */
  47. $sql = "SELECT * FROM ".TABLE_PREFIX."config";
  48. $result = mysql_query($sql, $db);
  49. while ($row = mysql_fetch_assoc($result)) {
  50. $_config[$row['name']] = $row['value'];
  51. }
  52. /***** 7. start language block *****/
  53. // set current language
  54. require(AT_INCLUDE_PATH . 'classes/Language/LanguageManager.class.php');
  55. $languageManager =& new LanguageManager();
  56. $myLang =& $languageManager->getMyLanguage();
  57. if ($myLang === FALSE) {
  58. echo 'There are no languages installed!';
  59. exit;
  60. }
  61. $myLang->saveToSession();
  62. if (isset($_GET['lang']) && $_SESSION['valid_user']) {
  63. if ($_SESSION['course_id'] == -1) {
  64. $myLang->saveToPreferences($_SESSION['login'], 1); //1 for admin
  65. } else {
  66. $myLang->saveToPreferences($_SESSION['member_id'], 0); //0 for non-admin
  67. }
  68. }
  69. $myLang->sendContentTypeHeader();
  70. /* set right-to-left language */
  71. $rtl = '';
  72. if ($myLang->isRTL()) {
  73. $rtl = 'rtl_'; /* basically the prefix to a rtl variant directory/filename. eg. rtl_tree */
  74. }
  75. /***** end language block ****/
  76. /*~~~~~~~~~~~~~~~~~~~~~~~end of vitals.inc.php~~~~~~~~~~~~~~~~~~~~~~*/
  77. // Read Mahara login information from cookies passed by ATutor
  78. $usr = array();
  79. if (isset($_COOKIE['ATutor_Mahara'])) {
  80. foreach ($_COOKIE['ATutor_Mahara'] as $name => $value) {
  81. $usr[$name] = $value;
  82. // expire the cookie
  83. ATutor.setcookie ("ATutor_Mahara[".$name."]", "", time() - 3600);
  84. }
  85. //expire the cookie array
  86. ATutor.setcookie ("ATutor_Mahara", "", time() - 3600);
  87. } else {
  88. echo 'Unable to detect cookies or the session has timed out. Please check that cookies are enabled on your browser and try again.';
  89. exit;
  90. }
  91. // Get password from ATutor's database
  92. $sql = "SELECT password FROM ".TABLE_PREFIX."mahara WHERE at_login='".$usr["at_login"]."' AND username='".$usr["username"]."' AND SHA1(password)='".$usr["password"]."'";
  93. $result = mysql_query($sql, $db);
  94. if (!($row = @mysql_fetch_array($result))) {
  95. echo 'Incorrect login information. Please check with course instructor or administrator.';
  96. exit;
  97. } else {
  98. $pwd = $row[0];
  99. if (isset($_config['mahara'])) {
  100. /****** Taken from index.php of /mahara *****/
  101. define('INTERNAL', 1);
  102. define('PUBLIC', 1);
  103. define('MENUITEM', '');
  104. define (MAHARA_PATH, $_config['mahara']);
  105. require (MAHARA_PATH.'init.php');
  106. define('TITLE', get_string('home'));
  107. // Check if user exists in Mahara
  108. if (!(record_exists('usr', 'username', $usr["username"]))) {
  109. // Reconnect to ATutor Database and remove the record from the mahara table
  110. $db_atutor = @mysql_connect(DB_HOST . ':' . DB_PORT, DB_USER, DB_PASSWORD);
  111. if (!$db_atutor) {
  112. /* AT_ERROR_NO_DB_CONNECT */
  113. require_once(AT_INCLUDE_PATH . 'classes/ErrorHandler/ErrorHandler.class.php');
  114. $err =& new ErrorHandler();
  115. trigger_error('VITAL#Unable to connect to db.', E_USER_ERROR);
  116. exit;
  117. }
  118. if (!@mysql_select_db(DB_NAME, $db_atutor)) {
  119. require_once(AT_INCLUDE_PATH . 'classes/ErrorHandler/ErrorHandler.class.php');
  120. $err =& new ErrorHandler();
  121. trigger_error('VITAL#DB connection established, but database "'.DB_NAME.'" cannot be selected.',
  122. E_USER_ERROR);
  123. exit;
  124. }
  125. // Delete record from ATutor database since it should not be there
  126. $sql = "DELETE FROM ".TABLE_PREFIX."mahara WHERE at_login='".$usr["at_login"]."'";
  127. $result = mysql_query($sql, $db_atutor);
  128. echo "Successfully synchronized user login with Mahara database. Please refresh the page from ATutor.";
  129. exit;
  130. }
  131. session_start();
  132. /*~~~~~~~~~~~copied from index.php of Mahara~~~~~~~~~~~~~~~*/
  133. // Check for whether the user is logged in, before processing the page. After
  134. // this, we can guarantee whether the user is logged in or not for this page.
  135. if (!$USER->is_logged_in()) {
  136. $lang = param_alphanumext('lang', null);
  137. if (!empty($lang)) {
  138. $SESSION->set('lang', $lang);
  139. }
  140. // Read login information
  141. $values['login_username'] = $usr["username"];
  142. $values['login_password'] = $pwd;
  143. $values['submit'] = "Login";
  144. $values['sesskey'] = "";
  145. $values['pieform_login'] = "";
  146. // login
  147. login_submit(null, $values);
  148. $adminpage = ($USER->get('admin')) ? 'admin/' : '';
  149. }
  150. /* Logged in session should be created. Now redirect to the Mahara page
  151. and it should read from this session
  152. */
  153. header('Location: '.get_config('wwwroot').$adminpage);
  154. /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
  155. } else {
  156. echo 'You have incorrect config settings for the Mahara module.';
  157. exit;
  158. }
  159. }
  160. ?>