/osis/functions/global_functions.php

https://github.com/jcohenho/OLLCF-SRS · PHP · 184 lines · 146 code · 18 blank · 20 comment · 30 complexity · cbe96d06281c3b5c0620f775b853aea9 MD5 · raw file

  1. <?php
  2. function dbConnect(){
  3. $con = mysql_connect("localhost","root","");
  4. if (!$con){die('Could not connect: ' . mysql_error());}
  5. mysql_select_db("osis", $con);
  6. }
  7. function getCourses(){
  8. dbConnect();
  9. $rsCourse = mysql_query("select * from course");
  10. if (mysql_num_rows($rsCourse)>0) return false; else return true;
  11. //mysql_free_result($rsCourse);
  12. //mysql_close ($con);
  13. }
  14. function register($username, $email, $password)
  15. // register new person with db
  16. // return true or error message
  17. {
  18. // connect to db
  19. $conn = dbConnect(); if (!$conn) return 'Could not connect to database server - please try later.';
  20. // check if username is unique
  21. $result = mysql_query("select * from user where username='$username'");
  22. if (!$result)
  23. return 'Could not execute query';
  24. if (mysql_num_rows($result)>0)
  25. return 'That username is taken - go back and choose another one.';
  26. // if ok, put in db
  27. $result = mysql_query("insert into user values
  28. ('$username', password('$password'), '$email')");
  29. if (!$result)
  30. return 'Could not register you in database - please try again later.';
  31. return true;
  32. }
  33. function loginAlumni($username, $password){
  34. dbConnect();
  35. $rsAlumni = mysql_query("select alumni_id,email,password from alumni_account where email='$username' and password = '$password'");
  36. if (!$rsAlumni) return false;
  37. if (mysql_num_rows($rsAlumni)>0) return true; else return false;
  38. mysql_free_result($rsAlumni);
  39. mysql_close ($con);
  40. }
  41. function check_valid_member()
  42. // see if somebody is logged in and notify them if not
  43. {
  44. }
  45. function getAlumniFullName($alumniId){
  46. dbConnect();
  47. $rsAlumni = mysql_query("select full_name from alumni_account where alumni_id='$alumniId'");
  48. $row_rsAlumni = mysql_fetch_assoc($rsAlumni);
  49. if (!$rsAlumni) return false;
  50. if (mysql_num_rows($rsAlumni)>0) return $row_rsAlumni['full_name']; else return false;
  51. mysql_free_result($rsAlumni);
  52. mysql_close ($con);
  53. }
  54. function getTotalInbox($alumniId){
  55. dbConnect();
  56. $rsAlumni = mysql_query("select full_name from alumni_account where alumni_id='$alumniId'");
  57. $row_rsAlumni = mysql_fetch_assoc($rsAlumni);
  58. if (!$rsAlumni) return false;
  59. if (mysql_num_rows($rsAlumni)>0) return $row_rsAlumni['full_name']; else return false;
  60. mysql_free_result($rsAlumni);
  61. mysql_close ($con);
  62. }
  63. function change_password($username, $old_password, $new_password)
  64. // change password for username/old_password to new_password
  65. // return true or false
  66. {
  67. // if the old password is right
  68. // change their password to new_password and return true
  69. // else return false
  70. if (login($username, $old_password))
  71. {
  72. if (!($conn = db_connect()))
  73. return false;
  74. $result = mysql_query( "update user
  75. set passwd = password('$new_password')
  76. where username = '$username'");
  77. if (!$result)
  78. return false; // not changed
  79. else
  80. return true; // changed successfully
  81. }
  82. else
  83. return false; // old password was wrong
  84. }
  85. function get_random_word($min_length, $max_length)
  86. // grab a random word from dictionary between the two lengths
  87. // and return it
  88. {
  89. // generate a random word
  90. $word = '';
  91. //remember to change this path to suit your system
  92. $dictionary = '/usr/dict/words'; // the ispell dictionary
  93. $fp = fopen($dictionary, 'r');
  94. if(!$fp)
  95. return false;
  96. $size = filesize($dictionary);
  97. // go to a random location in dictionary
  98. srand ((double) microtime() * 1000000);
  99. $rand_location = rand(0, $size);
  100. fseek($fp, $rand_location);
  101. // get the next whole word of the right length in the file
  102. while (strlen($word)< $min_length || strlen($word)>$max_length || strstr($word, "'"))
  103. {
  104. if (feof($fp))
  105. fseek($fp, 0); // if at end, go to start
  106. $word = fgets($fp, 80); // skip first word as it could be partial
  107. $word = fgets($fp, 80); // the potential password
  108. };
  109. $word=trim($word); // trim the trailing \n from fgets
  110. return $word;
  111. }
  112. function reset_password($username)
  113. // set password for username to a random value
  114. // return the new password or false on failure
  115. {
  116. // get a random dictionary word b/w 6 and 13 chars in length
  117. $new_password = get_random_word(6, 13);
  118. if($new_password==false)
  119. return false;
  120. // add a number between 0 and 999 to it
  121. // to make it a slightly better password
  122. srand ((double) microtime() * 1000000);
  123. $rand_number = rand(0, 999);
  124. $new_password .= $rand_number;
  125. // set user's password to this in database or return false
  126. if (!($conn = db_connect()))
  127. return false;
  128. $result = mysql_query( "update user
  129. set passwd = password('$new_password')
  130. where username = '$username'");
  131. if (!$result)
  132. return false; // not changed
  133. else
  134. return $new_password; // changed successfully
  135. }
  136. function notify_password($username, $password)
  137. // notify the user that their password has been changed
  138. {
  139. if (!($conn = db_connect()))
  140. return false;
  141. $result = mysql_query("select email from user
  142. where username='$username'");
  143. if (!$result)
  144. {
  145. return false; // not changed
  146. }
  147. else if (mysql_num_rows($result)==0)
  148. {
  149. return false; // username not in db
  150. }
  151. else
  152. {
  153. $email = mysql_result($result, 0, 'email');
  154. $from = "From: support@phpbookmark \r\n";
  155. $mesg = "Your PHPBookmark password has been changed to $password \r\n"
  156. ."Please change it next time you log in. \r\n";
  157. if (mail($email, 'PHPBookmark login information', $mesg, $from))
  158. return true;
  159. else
  160. return false;
  161. }
  162. }
  163. ?>