reSIProcate/repro  9694
userhome.php
Go to the documentation of this file.
00001 <?php
00002 
00003 require('reprofunctions.php');
00004 dbgSquirt("============= userhome ===============");
00005 
00006 // TODO
00007 /* There is a bug here ... if a user has authenticated successfully (and hence
00008         the cookies for username and passwordMD5 are set) and then they use BACK
00009         to go back to the login page, enter some values for username and 
00010         password, and click login, then what they just typed will be ignored, 
00011         and they will remain logged in under their original credentials. */
00012 
00013 // this variable controls whether the user is forced back to the main page to
00014 // login.  For safety, the default value is to force you back.
00015 $forceLogin = TRUE;
00016 $error = "";
00017 $time = time();
00018 
00019 if (!checkCookies($forceLogin,$error,TRUE)) {
00020   // we got an error back that occurred while checkCookies was being run
00021   dbgSquirt('Error from checkCookies');
00022   header("Location: http://" . $_SERVER['HTTP_HOST'] . 
00023          dirname($_SERVER['PHP_SELF']) . "/index.php?error=$error");
00024   exit;
00025  }
00026 
00027 // if the cookie's didn't pass authentication, or if the cookie's passed BUT
00028 // we've received new values for POST that don't match on username (they did
00029 // a BACK to the login page w/o a logout and then did a new login), then
00030 // try to authenticate via the POSTED values been supplied.
00031 if (isset($_POST['username']) && ($_POST['username'] != $_COOKIE['user']))
00032   $forceLogin = TRUE;
00033 
00034 if ($forceLogin) {
00035   dbgSquirt('forceLogin is still true... checking post variables');
00036   if (isset($_POST['username']) || isset($_POST['password'])) {
00037     // we have one or more post variables
00038     dbgSquirt('Post variables are set');
00039     if (empty($_POST['username']) || empty($_POST['password'])) {
00040       // can't have empty values for username or password
00041       dbgSquirt('...but one is empty');
00042       $error = "Authentication error -- you must enter a username and password.";
00043     } else {
00044       // we have non-empty values for username and password from POST so
00045       // lets validate them
00046       dbgSquirt('...both are non-empty [good]');
00047       $username = $_POST['username'];
00048       $password = $_POST['password'];
00049       $encryptedPassword = createPassword($username,$password);
00050 
00051       $state = validateUser($username,$encryptedPassword);
00052       if ("N" == $state) {
00053         dbgSquirt('Not a valid user');
00054         $error = "Authentication error -- Invalid username/password combination.";
00055       } else if ("A" == $state) {
00056         // active account and username/password match
00057         dbgSquirt('Active account matched.');
00058 
00059         // if we haven't already looked up the salt, do so now
00060         $result = TRUE;
00061         if (empty($salt)) {
00062           dbgSquirt('Getting salt');
00063           $result = getSalt($salt); }
00064 
00065         if (FALSE == $result) {
00066           // uh-oh ... we got an error getting the salt
00067           dbgSquirt('Error in getSalt');
00068           $error = "Internal error -- failure while processing login. Please contact an administrator.";
00069         } else {
00070           dbgSquirt('Extending cookies');
00071           dbgSquirt("Time -- $time");
00072           dbgSquirt("Time + Duration -- ". ($time+$sessionDuration));
00073           $result = setcookie("user",$username,$time+$sessionDuration);
00074           $result1 = setcookie("authentication",sha1($username . $salt),
00075                                $time+$sessionDuration);
00076 
00077           if ((TRUE == $result) && (TRUE == $result1)) {
00078             // everything worked
00079             dbgSquirt('Everything worked.');
00080             $forceLogin = FALSE;
00081           } else {
00082             dbgSquirt('Error while creating cookies');
00083             $error = "Internal error -- problem while creating cookies.  Please contact an administrator.";
00084           }
00085         }
00086       } else if ("U" == $state) {
00087         // unverified account
00088         dbgSquirt('Unverified Account');
00089         $error="This account has not been verified. Please check for the verification email you were sent as part of the signup process.";
00090       } else if ("D" == $state) {
00091         // disabled account
00092         dbgSquirt('Disabled Account');
00093         $error = "This account has been disabled.";
00094       } else {
00095         // should not happen ... checked return value from validateUser
00096         dbgSquirt('Unknown return code from validateUser');
00097         $error = "Internal Error -- error validating username/password.  Please try again.  This this error reoccurs, please contact an administrator.";
00098           }
00099     }
00100   } else {
00101     // no post variables supplied
00102     dbgSquirt('No post variables');
00103     $error = "Authentication error -- you must enter a username and password.";
00104   }
00105  } else {
00106   // forceLogin was FALSE ... that means the cookie's were valid
00107   // so get username from the cookie
00108   $username = $_COOKIE['user'];
00109  }
00110 
00111 // after checking cookies and post variables, if a login is still needed, then
00112 // redirect
00113 dbgSquirt("After post check -- forceLogin = $forceLogin");
00114 if ($forceLogin) {
00115   header("Location: http://" . $_SERVER['HTTP_HOST'] . 
00116             dirname($_SERVER['PHP_SELF']) . 
00117             "/index.php?error=$error");
00118   exit;
00119  }
00120 ?>
00121 
00122 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
00123 
00124 <!--
00125 System:  Repro
00126 File:    userhome.php
00127 Purpose: User Home Page.  This displays the users personal information and
00128          allows changes to be made.
00129 Author:  S. Chanin
00130 -->
00131 
00132 <html>
00133 <head>
00134 <link rel="stylesheet" type="text/css" href="repro_style.css" />
00135 <title></title>
00136 </head>
00137 
00138 <body>
00139 <h1 class="title">Repro</h1>
00140 
00141 <h1>User Home Page</h1>
00142 <hr />
00143 
00144 <?php
00145 // if we've looped back due to an error, show the message
00146 if (isset($_GET["error"]) && !empty($_GET['error'])) {
00147     echo '<p class="error">' . $_GET["error"] . "</p>\n";
00148 }
00149 if (!lookupUserInformation($username,$id,$fullname,$domain,$email)) {
00150         echo "<h2>Error -- Error while accessing account information</h2>\n";
00151         echo "<p>Please contact your administrator for assistance.</p>\n";
00152 } else {
00153 ?>
00154 <!-- show basic user information with the user -->
00155 <table border="0" cellpadding="5">
00156 <tr>
00157 <td>Username</td>
00158 <td><h2><?php echo $username ?></h2></td>
00159 </tr>
00160 
00161 <tr>
00162 <td>Fullname</td>
00163 <td><?php echo $fullname ?></td>
00164 <td><a href="changefullname.php">Change Fullname</a></td>
00165 </tr>
00166 
00167 <tr>
00168 <td>Password</td>
00169 <td>********</td>
00170 <td><a href="changepassword.php">Change Password</a></td>
00171 </tr>
00172 
00173 <tr>
00174 <td>Email</td>
00175 <td><?php echo $email ?></td>
00176 <td><a href="changeemail.php">Change Email</a></td>
00177 </tr>
00178 
00179 <tr>
00180 <td>Domain</td>
00181 <td><?php echo $domain ?></td>
00182 </tr>
00183 </table>
00184 
00185 <!-- now show the resources associated with the user -->
00186 <br />
00187 <table border="1">
00188 <th class="header">Address</th><th class="header">Forward</th>
00189 <th class="header">Voicemail</th><th class="header">Edit</th><th class="header">Delete</th>
00190 
00191 <?php
00192 $result = getResourcesByUsername($username,$resources);
00193 // print "<br />Final Result --";
00194 // print_r($resources);
00195 foreach ($resources as $r) {
00196   // print "Row -- ";
00197   // print_r($r);
00198   // print "<br />";
00199         
00200   $id = $r[0];
00201   $aor = $r[1];
00202   $forwardType = $r[2];
00203   $forward = $r[3];
00204   $voicemail = $r[4];
00205 
00206   echo "<tr>";
00207   echo '<form method="post" action="modifyresource.php">'."\n";
00208   echo "<td>$aor</td>\n";
00209   if ("Y" == $forwardType)
00210     echo "<td>$forward</td>\n";
00211   else
00212     echo "<td>&nbsp</td>\n";
00213   echo "<td>$voicemail</td>\n";
00214 
00215   echo '<td><input type="submit" name="edit" id="edit" value="Edit"/></td>'."\n";
00216   echo '<td><input type="submit" name="delete" id="delete" value="Delete"/></td>'."\n";
00217   echo '<input type="hidden" name="resourceId" id="resourceId" value="' . $id .'" />'."\n";
00218   echo '<input type="hidden" name="aor" id="aor" value="' . $aor .'" />'."\n";
00219   echo '<input type="hidden" name="forwardType" id="forwardType" value="' . $forwardType .'" />'."\n";
00220   echo '<input type="hidden" name="forward" id="forward" value="' . $forward .'" />'."\n";
00221   echo '<input type="hidden" name="voicemail" id="voicemail" value="' . $voicemail .'" />'."\n";
00222   echo "</form>\n";
00223   echo "</tr>\n";
00224 }
00225 ?>
00226 </table>
00227 <form method="post" action="addresource.php">
00228 <input type="submit" name="addResource" id="addResource" value="Add Resource" />
00229 </form>
00230 
00231 <?php
00232 } ?>
00233 <br /><hr /><a href="logout.php">Logout</a>
00234 
00235 </body>
00236 </html>