PageRenderTime 45ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/website/process_registration.php

http://github.com/aichallenge/aichallenge
PHP | 312 lines | 241 code | 27 blank | 44 comment | 70 complexity | 56065c5f5155956f9e38c7a96424084e MD5 | raw file
Possible License(s): Apache-2.0, BSD-3-Clause
  1. <?php
  2. $title="Processing Registration";
  3. require_once 'header.php';
  4. require_once 'server_info.php';
  5. if($server_info["submissions_open"]) {
  6. require_once 'mysql_login.php';
  7. require_once 'bad_words.php';
  8. require_once 'web_util.php';
  9. require_once('memcache.php');
  10. function check_valid_user_status_code($code) {
  11. $query = "SELECT * FROM user_status_code WHERE status_id = ".(int)$code;
  12. $result = mysql_query($query);
  13. return (boolean)mysql_num_rows($result);
  14. }
  15. function check_valid_organization($code) {
  16. if ($code == 999) {
  17. return False;
  18. }
  19. $query = "SELECT * FROM organization WHERE org_id=".(int)$code;
  20. $result = mysql_query($query);
  21. return (boolean)mysql_num_rows($result);
  22. }
  23. function check_valid_country($id) {
  24. if ($id == 999 || !filter_var($id, FILTER_VALIDATE_INT)) {
  25. return False;
  26. }
  27. $query = "SELECT count(*) from country where country_id=". $id;
  28. $result = mysql_query($query);
  29. $row = mysql_fetch_assoc($result);
  30. if ($row['count(*)'] > 0) {
  31. return True;
  32. }
  33. return False;
  34. }
  35. function valid_username($s) {
  36. return strspn($s, "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_.-") == strlen($s);
  37. }
  38. function create_new_organization( $org_name ) {
  39. global $memcache;
  40. if ($memcache) {
  41. $memcache->delete('lookup:org_id');
  42. $memcache->delete('lookup:org_name');
  43. }
  44. $query = "SELECT org_id FROM organization WHERE name='".$org_name."'";
  45. $result = mysql_query($query);
  46. if ( mysql_num_rows($result) > 0 ) {
  47. return mysql_result($result, 0, 0);
  48. } else {
  49. $query = "INSERT INTO organization (`name`) VALUES('".$org_name."')";
  50. $result = mysql_query($query);
  51. return mysql_insert_id();
  52. }
  53. }
  54. // By default, send account confirmation emails.
  55. $send_email = 1;
  56. $errors = array();
  57. // Check that required information was sent
  58. if (!isset($_POST['username'], $_POST['password1'], $_POST['password2'],
  59. $_POST['user_email'], $_POST['user_status'], $_POST['user_country'],
  60. $_POST['user_organization'], $_POST['bio'])) {
  61. die("Missing required information to create profile");
  62. }
  63. // Gather the information entered by the user on the signup page.
  64. $username = mysql_real_escape_string(stripslashes($_POST['username']));
  65. $password1 = mysql_real_escape_string(stripslashes($_POST['password1']));
  66. $password2 = mysql_real_escape_string(stripslashes($_POST['password2']));
  67. $user_email = mysql_real_escape_string(stripslashes($_POST['user_email']));
  68. $user_status = mysql_real_escape_string(stripslashes($_POST['user_status']));
  69. $user_org = mysql_real_escape_string(stripslashes($_POST['user_organization']));
  70. $bio = mysql_real_escape_string(stripslashes($_POST['bio']));
  71. $country_id = mysql_real_escape_string(stripslashes($_POST['user_country']));
  72. // Uncomment the following line to disable account creation
  73. //$errors[] = "Accounts can not be created at this time. Come back later, " .
  74. // "once the contest opens.";
  75. // Check for bad words
  76. if (contains_bad_word($username)) {
  77. $errors[] = "Your username contains a bad word. Keep it professional.";
  78. }
  79. if (contains_bad_word($bio)) {
  80. $errors[] = "Your bio contains a bad word. Keep it professional.";
  81. }
  82. // Check if mailer address is "donotsend". If so, don't send any confirmation
  83. // mails. Display the confirmation code once the account creation finishes,
  84. // and let them access the account activation page themselves. This should
  85. // only be used when setting up test servers as contestants email addresses
  86. // will not be verified.
  87. if (strcmp($server_info["mailer_address"], "donotsend") == 0) {
  88. $send_email = 0;
  89. }
  90. else
  91. {
  92. require_once "email.php";
  93. }
  94. // Check if the username already exists.
  95. $sql="SELECT * FROM user WHERE username='$username'";
  96. $result = mysql_query($sql);
  97. if (mysql_num_rows($result) > 0) {
  98. $errors[] = "The username $username is already in use. Please choose a different username.";
  99. }
  100. // Check that the email address is not blank.
  101. if (strlen($user_email) <= 0) {
  102. $errors[] = "You must provide an email address. The email address that you specify will be used to activate your account.";
  103. }
  104. // Check if the email is already in use (except by an admin account or a donotsend account).
  105. if (strcmp($user_email, "donotsend") != 0) {
  106. $sql="select email from user where email = '$user_email' and admin = 0";
  107. $result = mysql_query($sql);
  108. if ($result && mysql_num_rows($result) > 0) {
  109. $errors[] = "The email $user_email is already in use. You are only allowed to have one account! It is easy for us to tell if you have two accounts, and you will be disqualified if you have two accounts! If there is some problem with your existing account, get in touch with the contest organizers on irc.freenode.com channel #aichallenge and we will help you get up-and-running again!";
  110. }
  111. $edomain = substr(strrchr($user_email, '@'), 1);
  112. $mx_records = array();
  113. if (!getmxrr($edomain, $mx_records) && (strcmp(gethostbyname($edomain), $edomain) == 0)) {
  114. $errors[] = "Could not find the email address entered. Please enter a valid email address.";
  115. }
  116. }
  117. // Check if the username is made up of the right kinds of characters
  118. if (!valid_username($username)) {
  119. $errors[] = "Invalid username. Your username must be longer than 4 characters and composed only of the characters a-z, A-Z, 0-9, '-', '_', and '.'";
  120. }
  121. // Check that the username is between 6 and 16 characters long
  122. if (strlen($username) < 5 || strlen($username) > 16) {
  123. $errors[] = "Your username must be between 5 and 16 characters long.";
  124. }
  125. // Check that the two passwords given match.
  126. if ($password1 != $password2) {
  127. $errors[] = "You made a mistake while entering your password. "
  128. . "The two passwords that you give should match.";
  129. }
  130. // Check that the desired password is long enough.
  131. if (strlen($password1) < 5) {
  132. $errors[] = "Your password must be at least 5 characters long.";
  133. }
  134. // Check that the user status code is valid.
  135. if (!check_valid_user_status_code($user_status)) {
  136. $errors[] = "The status you selected is invalid. Please contact the contest staff.";
  137. }
  138. // Check that the country code is not empty.
  139. if (!check_valid_country($country_id)) {
  140. $errors[] = "You did not select a valid country from the dropdown box.";
  141. }
  142. // Check that the user organziation code is valid.
  143. if( $user_org == '-1') {
  144. $_POST['user_organization_other'] = trim($_POST['user_organization_other']);
  145. if( $_POST['user_organization_other'] === '' ) {
  146. //don't create empty organizations
  147. $user_org = '0';
  148. } else {
  149. $user_org_other = mysql_real_escape_string(stripslashes($_POST['user_organization_other']));
  150. $user_org = create_new_organization( $user_org_other );
  151. }
  152. } elseif (!check_valid_organization($user_org)) {
  153. $errors[] = "The organization you selected is invalid. Please contact the contest staff.";
  154. }
  155. if (count($errors) <= 0) {
  156. // Add the user to the database, with no permissions.
  157. $confirmation_code = md5(salt(64));
  158. $query = "
  159. SELECT org.name AS name, COUNT(u.user_id) AS peers
  160. FROM organization org
  161. LEFT OUTER JOIN user u ON u.org_id = org.org_id
  162. WHERE org.org_id = " . $user_org;
  163. $result = mysql_query($query);
  164. $peer_message = "";
  165. $org_name = "";
  166. $num_peers = "";
  167. if ($result) {
  168. if ($row = mysql_fetch_assoc($result)) {
  169. $org_name = $row['name'];
  170. $num_peers = $row['peers'];
  171. if ($num_peers == 0) {
  172. $peer_message = "You are the first person from your organization to sign up " .
  173. "for the AI Challenge. We would really appreciate it if you would " .
  174. "encourage your friends to sign up for the Challenge as well. The more, " .
  175. "the merrier!\n\n";
  176. } else if (strcmp($org_name, "Other") == 0) {
  177. $peer_message = "You didn't associate yourself with an organization ".
  178. "when you signed up. You might want to change this in your ".
  179. "profile so you can compare how you're doing with others in ".
  180. "your school or company.\n\n";
  181. } else {
  182. $peer_message = "" . $num_peers . " other people from " . $org_name .
  183. " have already signed up for the AI Challenge. When you look " .
  184. "at the rankings, you can see the global rankings, or " .
  185. "you can filter the list to only show other contestants from your organization!\n\n";
  186. }
  187. }
  188. }
  189. $query = "
  190. INSERT INTO user (username,`password`,email,status_id,activation_code,org_id,bio,country_id,created,activated,admin)
  191. VALUES ('$username','" . mysql_real_escape_string(crypt($password1, '$6$rounds=54321$' . salt() . '$')) . "','$user_email',$user_status,'$confirmation_code',$user_org,'$bio',$country_id,CURRENT_TIMESTAMP,0,0)";
  192. if (mysql_query($query)) {
  193. if ($memcache) {
  194. $memcache->delete('lookup:user_id');
  195. $memcache->delete('lookup:username');
  196. }
  197. // Send confirmation mail to user.
  198. $mail_subject = "AI Challenge!";
  199. $activation_url = current_url();
  200. $activation_url = str_replace("process_registration.php",
  201. "account_confirmation.php",
  202. $activation_url);
  203. if (strlen($activation_url) < 5) {
  204. $activation_url = "http://aichallenge.org/account_confirmation.php";
  205. }
  206. $mail_content = "Welcome to the contest! Click the link below in order " .
  207. "to activate your account.\n\n" .
  208. $activation_url .
  209. "?confirmation_code=" . $confirmation_code . "\n\n" .
  210. "After you activate your account by clicking the link above, you will " .
  211. "be able to sign in and start competing. Good luck!\n\n" .
  212. $peer_message . "Thanks for participating and have fun,\nContest Staff\n";
  213. if ($send_email == 1 && strcmp($user_email, "donotsend") != 0) {
  214. $mail_accepted = send_email($user_email, $mail_subject, $mail_content);
  215. } else {
  216. $mail_accepted = true;
  217. }
  218. if (intval($mail_accepted) == 0) {
  219. $errors[] = "Failed to send confirmation email. Try again in a few " .
  220. "minutes.";
  221. $query = "DELETE FROM user WHERE username='$username' and " .
  222. "activation_code='" . $confirmation_code . "'";
  223. mysql_query($query);
  224. } else {
  225. // Send notification mail to contest admin.
  226. //$mail_subject = "New Contest User";
  227. //$mail_content = "username = " . $username . "\nOrganizationID = " .
  228. // $user_org . "\nUser number " . ($num_peers + 1) . " from " .
  229. // $org_name;
  230. //if ($send_email == 1) {
  231. // $mail_accepted = send_gmail($admin_address,
  232. // $mail_subject,
  233. // $mail_content);
  234. //} else {
  235. // $mail_accepted = true;
  236. //}
  237. //if (intval($mail_accepted) == 0) {
  238. // $errors[] = "Failed to send confirmation email. Try again in " .
  239. // "a few minutes.";
  240. // $query = "DELETE FROM users WHERE username='$username' and " .
  241. // "password='" . md5($password1) . "'";
  242. // mysql_query($query);
  243. //}
  244. }
  245. } else {
  246. $errors[] = "Failed to communicate with the registration database. Try " .
  247. "again in a few minutes. ($query : " . mysql_error() . ")";
  248. }
  249. }
  250. if (count($errors) > 0) {
  251. require 'register.php';
  252. } else {
  253. ?>
  254. <h1>Registration Successful!</h1>
  255. <p>Thank you for registering for the contest! A confirmation
  256. message will be sent to the email address that you provided.
  257. You must click the link in that message in order to activate
  258. your account.</p>
  259. <h2>Check Your Junk Mail Folder</h2>
  260. <p>If you don't see it in five minutes, remember to check your
  261. junk mail folder. Some free email providers are known to
  262. mistake confirmation emails for junk mail. Before you even think
  263. of sending us mail asking for help, <strong>check your junk mail
  264. folder!</strong></p>
  265. <p><a href="index.php">Back to the home page.</a></p>
  266. <?php
  267. if ($send_email == 0) {
  268. echo "<p>Confirmation emails are not being sent!</p>";
  269. echo "<p>This should only be used when setting up a test server.</p>";
  270. echo '<p><a href="account_confirmation.php?confirmation_code=' .
  271. $confirmation_code . '">Click Here</a> to activate the account.</p>';
  272. }
  273. } // end if
  274. ?>
  275. <?php } else { ?>
  276. <p>Sorry, account creation is now closed.</p>
  277. <?php } ?>
  278. <?php require_once 'footer.php'; ?>