PageRenderTime 25ms CodeModel.GetById 15ms RepoModel.GetById 1ms app.codeStats 0ms

/new_account.php

https://github.com/atutor/mahara
PHP | 244 lines | 157 code | 41 blank | 46 comment | 23 complexity | b2469bf4ba68e009f55aefe0fc1f4cad MD5 | raw file
  1. <?php
  2. /*
  3. This belongs to the ATutor+Mahara module. It is called from index.php when
  4. the user does not have a Mahara account associated with ATutor. This script
  5. automatically creates a new Mahara account for the user and saves the login
  6. information with ATutor. If the user already has a Mahara account, the script
  7. simply adds the login information to ATutor and reassigns an automatically
  8. generated password for Mahara.
  9. Most of the necessary code is copied and modified from init.php and
  10. register.php of Mahara.
  11. by: Boon-Hau Teh
  12. */
  13. $_user_location = 'public';
  14. if (!defined('new_account')) { exit; }
  15. if (!defined('AT_INCLUDE_PATH')) { exit; }
  16. $sql = 'SELECT * FROM '.TABLE_PREFIX.'members WHERE member_id="'.$_SESSION['member_id'].'"';
  17. $result = mysql_query($sql, $db);
  18. $row = mysql_fetch_assoc($result);
  19. $registration->username = $row['login'];
  20. $registration->firstname = $row['first_name'];
  21. $registration->lastname = $row['last_name'];
  22. $registration->password = $row['password'];
  23. $registration->email = $row['email'];
  24. define (MAHARA_PATH, $_config['mahara']);
  25. /******************from init.php*************************/
  26. define('INTERNAL', 1);
  27. define('PUBLIC', 1);
  28. define('SECTION_PLUGINTYPE', 'core');
  29. define('SECTION_PLUGINNAME', 'site');
  30. define('SECTION_PAGE', 'register');
  31. $CFG = new StdClass;
  32. $CFG->docroot = MAHARA_PATH;
  33. // Figure out our include path
  34. if (!empty($_SERVER['MAHARA_LIBDIR'])) {
  35. $CFG->libroot = $_SERVER['MAHARA_LIBDIR'];
  36. } else {
  37. $CFG->libroot = MAHARA_PATH. 'lib/';
  38. }
  39. set_include_path($CFG->libroot . PATH_SEPARATOR . $CFG->libroot . 'pear/' . PATH_SEPARATOR . get_include_path());
  40. // Set up error handling
  41. require(MAHARA_PATH.'lib/errors.php');
  42. if (!is_readable($CFG->docroot . 'config.php')) {
  43. // @todo Later, this will redirect to the installer script. For now, we
  44. // just log and exit.
  45. log_environ(_AT('MAHARA_ERROR_INSTALL'));
  46. header('Location: '.AT_BASE_HREF);
  47. }
  48. require(MAHARA_PATH.'config.php');
  49. $CFG = (object)array_merge((array)$cfg, (array)$CFG);
  50. // Fix up paths in $CFG
  51. foreach (array('docroot', 'dataroot') as $path) {
  52. $CFG->{$path} = (substr($CFG->{$path}, -1) != DIRECTORY_SEPARATOR) ? $CFG->{$path} . DIRECTORY_SEPARATOR : $CFG->{$path};
  53. }
  54. // xmldb stuff
  55. $CFG->xmldbdisablenextprevchecking = true;
  56. $CFG->xmldbdisablecommentchecking = true;
  57. // ensure directorypermissions is set
  58. if (empty($CFG->directorypermissions)) {
  59. $CFG->directorypermissions = 0700;
  60. }
  61. // core libraries
  62. require(MAHARA_PATH.'lib/mahara.php');
  63. ensure_sanity();
  64. require(MAHARA_PATH.'auth/internal/lib.php');
  65. require(MAHARA_PATH.'lib/dml.php');
  66. require(MAHARA_PATH.'lib/ddl.php');
  67. require(MAHARA_PATH.'lib/activity.php');
  68. require(MAHARA_PATH.'lib/user.php');
  69. require(MAHARA_PATH.'lib/web.php');
  70. // Database access functions
  71. require(MAHARA_PATH.'lib/adodb/adodb-exceptions.inc.php');
  72. require(MAHARA_PATH.'lib/adodb/adodb.inc.php');
  73. try {
  74. // ADODB does not provide the raw driver error message if the connection
  75. // fails for some reason, so we use output buffering to catch whatever
  76. // the error is instead.
  77. ob_start();
  78. $db = &ADONewConnection($CFG->dbtype);
  79. $dbgenerator = null;
  80. if (empty($CFG->dbhost)) {
  81. $CFG->dbhost = '';
  82. }
  83. else if (!empty($CFG->dbport)) {
  84. $CFG->dbhost .= ':'.$CFG->dbport;
  85. }
  86. if (!empty($CFG->dbpersist)) { // Use persistent connection (default)
  87. $dbconnected = $db->PConnect($CFG->dbhost,$CFG->dbuser,$CFG->dbpass,$CFG->dbname);
  88. }
  89. else { // Use single connection
  90. $dbconnected = $db->Connect($CFG->dbhost,$CFG->dbuser,$CFG->dbpass,$CFG->dbname);
  91. }
  92. $db->SetFetchMode(ADODB_FETCH_ASSOC);
  93. configure_dbconnection();
  94. ensure_internal_plugins_exist();
  95. ob_end_clean();
  96. }
  97. catch (Exception $e) {
  98. $errormessage = ob_get_contents();
  99. if (!$errormessage) {
  100. $errormessage = $e->getMessage();
  101. }
  102. ob_end_clean();
  103. $errormessage = get_string('dbconnfailed', 'error') . $errormessage;
  104. throw new ConfigSanityException($errormessage);
  105. }
  106. try {
  107. db_ignore_sql_exceptions(true);
  108. load_config();
  109. db_ignore_sql_exceptions(false);
  110. }
  111. catch (SQLException $e) {
  112. db_ignore_sql_exceptions(false);
  113. }
  114. // Only do authentication once we know the page theme, so that the login form
  115. // can have the correct theming.
  116. require_once(MAHARA_PATH.'auth/lib.php');
  117. $USER = new LiveUser();
  118. /***************end from init.php*************************/
  119. /*~~~~~~~~~modified from register.php~~~~~~~~~~*/
  120. $random_password = substr(md5($registration->password.rand(100000, 999999)), 2, 8);
  121. /*-- from register_submit function --*/
  122. $registration->salt = substr(md5(rand(1000000, 9999999)), 2, 8);
  123. $registration->password = AuthInternal::encrypt_password($random_password, $registration->salt);
  124. $registration->expiry = NULL;
  125. /*-----------------------------------*/
  126. // Check if user already exists in Mahara
  127. if ($data_record = get_record('usr', 'username', $registration->username)) {
  128. $registration -> id = $data_record -> id;
  129. update_record('usr', $registration, 'username');
  130. } else {
  131. create_registered_user(); // Send register info to create a new account
  132. }
  133. // Reconnect to ATutor Database
  134. $db_atutor = @mysql_connect(DB_HOST . ':' . DB_PORT, DB_USER, DB_PASSWORD);
  135. if (!$db_atutor) {
  136. /* AT_ERROR_NO_DB_CONNECT */
  137. require_once(AT_INCLUDE_PATH . 'classes/ErrorHandler/ErrorHandler.class.php');
  138. $err =& new ErrorHandler();
  139. trigger_error('VITAL#Unable to connect to db.', E_USER_ERROR);
  140. exit;
  141. }
  142. if (!@mysql_select_db(DB_NAME, $db_atutor)) {
  143. require_once(AT_INCLUDE_PATH . 'classes/ErrorHandler/ErrorHandler.class.php');
  144. $err =& new ErrorHandler();
  145. trigger_error('VITAL#DB connection established, but database "'.DB_NAME.'" cannot be selected.',
  146. E_USER_ERROR);
  147. exit;
  148. }
  149. // Store data into ATutor Databse
  150. $sql = "INSERT INTO ".TABLE_PREFIX."mahara SET at_login='".$_SESSION['login']."', username='".$registration->username."', password='".$random_password."'";
  151. if (!mysql_query($sql, $db_atutor))
  152. exit; // in case there's some external error; prevent being caught in an infinite loop
  153. /**
  154. * This function is copied and modified from register.php of Mahara
  155. *
  156. * @param array profilefields Array of values from registration form. In this module, we're not using a form so we don't pass anything
  157. * @return boolean Returns true if function exits without any problems
  158. */
  159. function create_registered_user($profilefields=array()) {
  160. global $registration, $USER;
  161. db_begin();
  162. // Move the user record to the usr table from the registration table
  163. $registrationid = $registration->id;
  164. unset($registration->id);
  165. unset($registration->expiry);
  166. if ($expirytime = get_config('defaultaccountlifetime')) {
  167. $registration->expiry = db_format_timestamp(time() + $expirytime);
  168. }
  169. $registration->lastlogin = db_format_timestamp(time());
  170. $user = new User();
  171. $user->username = $registration->username;
  172. $user->password = $registration->password;
  173. $user->salt = $registration->salt;
  174. $user->passwordchange = 0;
  175. $user->active = 1;
  176. $user->authinstance = $authinstance->id;
  177. $user->firstname = $registration->firstname;
  178. $user->lastname = $registration->lastname;
  179. $user->email = $registration->email;
  180. $user->commit();
  181. $registration->id = $user->id;
  182. // Insert standard stuff as artefacts
  183. set_profile_field($user->id, 'email', $registration->email);
  184. set_profile_field($user->id, 'firstname', $registration->firstname);
  185. set_profile_field($user->id, 'lastname', $registration->lastname);
  186. if (!empty($registration->lang) && $registration->lang != 'default') {
  187. set_account_preference($user->id, 'lang', $registration->lang);
  188. }
  189. // Set mandatory profile fields
  190. foreach(ArtefactTypeProfile::get_mandatory_fields() as $field => $type) {
  191. // @todo here and above, use the method for getting "always mandatory" fields
  192. if (in_array($field, array('firstname', 'lastname', 'email'))) {
  193. continue;
  194. }
  195. set_profile_field($user->id, $field, $profilefields[$field]);
  196. }
  197. db_commit();
  198. handle_event('createuser', $registration);
  199. return true;
  200. }
  201. ?>