Debian Codegraph @

qchem/auth.php

raw
1 <?php
2 #
3 # Start unique session
4 #
5 if (session_id() == "")
6 session_start();
7
8 #
9 # Set constants
10 #
11 $authfile = "account";
12 $sitename = "CH5M3D Computational Chemistry Interface";
13 $errmsg = "<html><head><title>Unauthorized</title></head>
14 <body><h1>Access Denied</h1>
15 <p>An account is required to use this site.</p>
16 </body></html>";
17
18
19 #
20 # Function for failed authorization attempts
21 #
22 function auth_reject() {
23 global $sitename, $errmsg;
24
25 Header("HTTP/1.0 401 Unauthorized");
26 Header("WWW-Authenticate: Basic realm=\"$sitename\"");
27 session_destroy();
28 echo $errmsg;
29 exit();
30 }
31
32
33 #
34 # If no username/password sent, reject
35 #
36 if ( !isset($_SERVER['PHP_AUTH_USER']) || !isset($_SERVER['PHP_AUTH_PW'])) {
37 auth_reject();
38 }
39 $user = $_SERVER['PHP_AUTH_USER'];
40 $pass = sha1($_SERVER['PHP_AUTH_PW']);
41
42 #
43 # Verify username/password against data in account file.
44 #
45 $_SESSION['authorized'] = 0;
46 $fh = fopen($authfile,'r') or die("Unable to open authorization file.");
47 while ( !feof($fh)) {
48 $line = fgets($fh);
49 $info = split(",",$line);
50 if ( ($user == trim($info[0])) && ($pass == trim($info[1])) ) {
51 $_SESSION['authorized'] = 1;
52 }
53 }
54 fclose($fh);
55 if ( $_SESSION['authorized'] != 1) {
56 auth_reject();
57 }
58
59 ?>