PageRenderTime 59ms CodeModel.GetById 22ms RepoModel.GetById 1ms app.codeStats 0ms

/authentication.inc.php

https://github.com/adamfranco/segue-1.x
PHP | 238 lines | 167 code | 33 blank | 38 comment | 44 complexity | 11891a6afe54b3e014ac17ce183e8d49 MD5 | raw file
  1. <? /* $Id$ */
  2. // handles the authentication of scripts executed and decides if the user needs to be
  3. // authenticated in the first place.
  4. // - this script essentially has the same structure as checklogin.inc.php
  5. // - but adds functionality specific to segue. i may eventually combine these two into one
  6. // include the authentication modules
  7. foreach ($_auth_mods as $i) {
  8. if (!preg_match('/^[a-z_0-9]+$/i', $i))
  9. die ('Error: invalid auth module, '.$i.'.');
  10. include("auth_mods/".urlencode($i).".inc.php");
  11. }
  12. $loginerror=0;
  13. $_loggedin=0;
  14. //$name = strtolower($name);
  15. require_once("auth_not_req_actions.inc.php");
  16. // first off, if el user is already logged in, lets make sure their info is good
  17. if ($_SESSION[luser]) {
  18. if (!loginvalid($_SESSION['luser'],$_SESSION['lpass'],1)) $loginerror=1;
  19. else $_loggedin=1;
  20. }
  21. // if we're not yet logged in
  22. if (!$_loggedin) {
  23. if ($_REQUEST[loginform]) { // they just entered their name & pass
  24. if (!isset($_SERVER['HTTP_COOKIE']) || !$_SERVER['HTTP_COOKIE']) {
  25. error("You must have cookies enabled in your browser.
  26. <ul >
  27. <li style='margin-bottom: 15px;'><strong>FireFox/Mozilla:</strong>
  28. <ol>
  29. <li>Open the FireFox Preferences
  30. <ul>
  31. <li>Windows: <strong>Tools</strong> -&gt; <strong>Options...</strong></li>
  32. <li>Mac: <strong>FireFox</strong> -&gt; <strong>Preferences...</strong></li>
  33. </ul>
  34. </li>
  35. <li>In the preferences window, go to <strong>Privacy</strong> -&gt; <strong>Cookies</strong></li>
  36. <li>Check the <strong>Allow sites to set Cookies/Enable Cookies</strong> box.</li>
  37. <li>Refresh this page at least once, this error should go away.</li>
  38. </ol>
  39. </li>
  40. <li style='margin-bottom: 15px;'><strong>Internet Explorer (IE):</strong>
  41. <ol>
  42. <li>Open the Internet Options: <strong>Tools</strong> -&gt; <strong>Internet Options</strong></li>
  43. <li>Click on the <strong>Privacy</strong> tab.</li>
  44. <li>Change the privacy setting to <strong>Medium High</strong> or less</li>
  45. <li>Refresh this page at least once, this error should go away.</li>
  46. </ol>
  47. </li>
  48. <li style='margin-bottom: 15px;'><strong>Safari:</strong>
  49. <ol>
  50. <li>Open the Safari Preferences: <strong>Safari</strong> -&gt; <strong>Preferences...</strong></li>
  51. <li>In the preferences window, go to the <strong>Security</strong> tab.</li>
  52. <li>Select <strong>Only from sites you navigate to</strong> option under <strong>Accept Cookies</strong>.</li>
  53. <li>Refresh this page at least once, this error should go away.</li>
  54. </ol>
  55. </li>
  56. </ul>
  57. <br />");
  58. } else
  59. // now, assuming they were successful
  60. if (loginvalid($_REQUEST[name],$_REQUEST[password])) {
  61. $newquerystring = ereg_replace("PHPSESSID","OLDID",urldecode($_REQUEST[getquery]));
  62. $newurl = ereg_replace("PHPSESSID","OLDID",urldecode($_REQUEST[gotourl]));
  63. $_loggedin=1;
  64. // header("Location: index.php?$sid&$newquerystring");
  65. if (!ereg('\?',$newurl)) $g = '?';
  66. //print $newurl.$g."&".$sid;
  67. header("Location: ".$newurl.$g."&".$sid);
  68. exit;
  69. } else {
  70. // username or passwd incorrect
  71. $loginerror=1;
  72. }
  73. }
  74. if (!$_loggedin) { // if we still have no login
  75. if ($loginerror) error("The username and password pair you entered is not valid. Please try again.<br />");
  76. if ($_REQUEST[action]) $try = $_REQUEST[action];
  77. if ($action) $try = $action;
  78. else $try = trim($_SERVER['SCRIPT_NAME'],"/");
  79. // :: hack for fullstory w/out auth
  80. if (trim($_SERVER['SCRIPT_NAME'],"/") == "fullstory.php") $try = "fullstory.php";
  81. if (!in_array($try,$actions_noauth)) {
  82. $loginerror=1;
  83. error("You must be authenticated to view this page. Please log in above.");
  84. }
  85. }
  86. }
  87. function loginvalid($user,$pass,$alreadyloggedin=0) {
  88. global $_auth_mods;
  89. // we have two choices in this function. either the user has already logged in
  90. // or we have to check for them
  91. if ($alreadyloggedin) {
  92. // print "lmethod: $_SESSION['lmethod'] - $_SESSION['luser']<br />";
  93. if (!$_SESSION[lmethod]) {
  94. error("An unknown error happened during authentication. Please <a href='index.php?login'>logout</a> and try again. Ignore the error(s) below.");
  95. return 0;
  96. }
  97. $func = "_valid_".$_SESSION[lmethod];
  98. /* if ($func($user,$pass)) */
  99. /* return 1; // ok, they passed the test */
  100. /* else */
  101. /* return 0; */
  102. return 1;
  103. } else {
  104. $valid=0;
  105. // $valid = $x = _valid_pam($user,$pass);
  106. // print_r($_auth_mods);
  107. foreach ($_auth_mods as $_auth) {
  108. $func = "_valid_".$_auth;
  109. // print "<br />AUTH: trying ".$_auth ."..."; //debug
  110. if ($x = $func($user,$pass)) {
  111. $valid = 1;
  112. break;
  113. }
  114. }
  115. // print "<br />$valid<br />";
  116. // print_r($x);
  117. if ($valid) { // register all of the needed variables
  118. // and send them to the correct page
  119. // set the acting user variables.. default to same as login -- may change later
  120. $_SESSION[aid] = $_SESSION[lid] = $x[id];
  121. $_SESSION[auser] = $_SESSION[luser] = $user;
  122. // $_SESSION[lpass] = $pass;
  123. $_SESSION[afname] = $_SESSION[lfname] = $x[fullname];
  124. $_SESSION[aemail] = $_SESSION[lemail] = $x[email];
  125. $_SESSION[atype] = $_SESSION[ltype] = $x[type];
  126. $_SESSION[amethod] = $_SESSION[lmethod] = $x[method];
  127. log_entry("login","$_SESSION[luser] authenticated");
  128. return 1;
  129. } else return 0;
  130. }
  131. return 0;
  132. }
  133. function _auth_check_db($x,$add_to_db=0) {
  134. // check to see if the user is already in the db... if not, add their info (if add_to_db is set)
  135. // $x is an array that contains user info
  136. // $x[user] and $x[method] must be set
  137. global $dbuser,$dbhost,$dbpass,$dbdb;
  138. db_connect($dbhost, $dbuser, $dbpass, $dbdb);
  139. $query = "
  140. SELECT
  141. *
  142. FROM
  143. user
  144. WHERE
  145. user_uname='".addslashes($x[user])."'
  146. ";
  147. $r = db_query($query);
  148. if (db_num_rows($r)) { // they have an entry already -- pull down their info
  149. $a = db_fetch_assoc($r);
  150. // if their authentication method is not db, then sync the db to the other method
  151. if (strtolower($a[user_authtype]) != "db"
  152. && (
  153. $x[fullname] != $a[user_fname]
  154. || $x[email] != $a[user_email]
  155. || ($x[type] != $a[user_type]
  156. && $a[user_type] != "admin")
  157. )
  158. ) {
  159. //$x[fullname] = addslashes($x[fullname]);
  160. $query = "
  161. UPDATE
  162. user
  163. SET
  164. user_email='".addslashes($x[email])."',
  165. user_fname='".addslashes($x[fullname])."'
  166. ";
  167. if ($a[user_type] != "admin") {
  168. $query .= ", user_type='".addslashes($x[type])."'";
  169. }
  170. $query .="
  171. WHERE
  172. user_uname='".addslashes($x[user])."'
  173. ";
  174. $r = db_query($query);
  175. }
  176. if ($a[user_type] == 'admin') {
  177. $x[type] = $a[user_type];
  178. }
  179. $x[id] = $a[user_id];
  180. // return the new array with info
  181. return $x;
  182. } else { // they have no database entry
  183. if ($add_to_db) { // add them to the database and return new id
  184. //$x[fullname] = addslashes($x[fullname]);
  185. $query = "
  186. INSERT INTO
  187. user
  188. SET
  189. user_uname='".addslashes($x[user])."',
  190. user_email='".addslashes($x[email])."',
  191. user_fname='".addslashes($x[fullname])."',
  192. user_type='".addslashes($x[type])."',
  193. user_pass='".addslashes(strtoupper($x[method]))." PASS',
  194. user_authtype='".addslashes($x[method])."'
  195. ";
  196. $r = db_query($query);
  197. // the query could fail if a user with that username is already in the database, but:
  198. if (!$r) return 0;
  199. //echo $query."<br />";
  200. // if (!$r) error occured;
  201. $x[id] = lastid();
  202. return $x;
  203. } else { return 0; } // no database entry, don't add to db, so return 0
  204. }
  205. }