cvs: php-master-web /manage users.php
| From: | Jim Winstead | Date: | Thu, 18 Oct 2001 01:44:22 +0000 |
| Subject: | cvs: php-master-web /manage users.php | ||
| Groups: | php.mirrors | ||
| Request: | Send a blank email to php-mirrors+get-7062@lists.php.net to get a copy of this message | ||
jimw Wed Oct 17 21:44:22 2001 EDT
Modified files:
/php-master-web/manage users.php
Log:
clean up to handle new table structure, for the most part
Index: php-master-web/manage/users.php diff -u php-master-web/manage/users.php:1.6 php-master-web/manage/users.php:1.7 --- php-master-web/manage/users.php:1.6 Wed Oct 17 19:03:23 2001 +++ php-master-web/manage/users.php Wed Oct 17 21:44:22 2001 @@ -1,101 +1,79 @@ <?php -require 'cvs-auth.inc'; #TODO: +# /TODO # acls # trigger passwd file update on cvs.php.net # trigger mail alias update on php.net mx # some sort of search # handle flipping of the sort views -if (isset($save) && isset($user) && isset($pw)) { - setcookie("MAGIC_COOKIE",base64_encode("$user:$pw"),time()+3600*24*12,'/','.php.net'); -} -if (isset($MAGIC_COOKIE) && !isset($user) && !isset($pw)) { - list($user,$pw) = explode(":", base64_decode($MAGIC_COOKIE)); -} - -echo '<html><head><title>user administration</title></head><body>'; +require_once 'login.inc'; +require_once 'functions.inc'; -if (!$user || !$pw || !verify_password($user,$pw)) {?> -<form method="POST" action="<?php echo $PHP_SELF?>"> -<input type="hidden" name="save" value="1" /> -<table> - <tr> - <th align="right">Username:</th> - <td><input type="text" name="user" value="<?php echo htmlspecialchars(stripslashes($user));?>" /> - </tr> - <tr> - <th align="right">Password:</th> - <td><input type="password" name="pw" value="<?php echo htmlspecialchars(stripslashes($pw));?>" /> - </tr> - <tr> - <td align="center" colspan="2"><input type="submit" value="Login" /></td> - </tr> -</table> -</form> -<?php - echo '</body></html>'; - exit; -} +head("user administration"); @mysql_connect("localhost","nobody","") or die("unable to connect to database"); @mysql_select_db("php3"); +# ?username=whatever will look up 'whatever' by email or cvs username if (isset($username) && !isset($id)) { - $res = @mysql_query("SELECT userid FROM users WHERE cvsuser='$username' OR email='$username'"); - if ($res) $id = mysql_result($res,0); + $query = "SELECT users.userid FROM users LEFT JOIN users_cvs USING (userid)" + . " WHERE cvsuser='$username' OR email='$username'"; + $res = query($query); + if (!($id = @mysql_result($res,0))) { + warn("wasn't able to find user matching '".clean($username)."'"); + } } if (isset($id) && isset($in)) { - $approved = $in[approved] ? 1 : 0; - - if ($error = invalid_input($in)) { - echo "<p class=\"error\">$error</p>"; + if (!can_modify($user,$id)) { + warn("you're not allowed to modify this user."); } else { - if ($id) { - $res = mysql_query("SELECT * FROM users WHERE userid=$id") - or die("query failed"); - $row = mysql_fetch_array($res); - # need to allow all of group@php.net to edit everyone. just me for now. - if ($user != $row[cvsuser] && $user != 'jimw') { - die("can't edit someone else"); - } - - $pass = crypt($in[passwd],substr(md5(time()),0,2)); - $query = "UPDATE users SET" - . ($in[name] ? " name='$in[name]'" : "") - . ($in[email] ? ",email='$in[email]'," : "") - . ($in[cvsuser] ? "cvsuser='$in[cvsuser]'" : "") - . ($in[passwd] ? ",passwd='$pass]'" : "") - . ",approved=$approved WHERE userid=$id"; + if ($error = invalid_input($in)) { + warn($error); } else { - if ($in[passwd]) $in[passwd] = crypt($in[passwd],substr(md5(time()),0,2)); - $query = "INSERT INTO users (name,email,cvsuser,passwd,approved) VALUES ('$in[name]','$in[email]','$in[cvsuser]','$in[passwd]',$approved)"; - } + if ($in[rawpasswd]) { + $in[passwd] = crypt($in[rawpasswd],substr(md5(time()),0,2)); + } - if (!mysql_query($query)) { - echo "<h2 class=\"error\">Query '$query' failed: ", mysql_error(), "</h2>"; - } - else { - echo "<h2>$in[name] ($in[cvsuser]) ", $id ? "updated" : "added", ".</h2>"; - unset($id); + if ($id) { + # update main table data + if (isset($in[email]) && isset($in[name])) { + $query = "UPDATE users SET name='$in[name]',email='$in[email]'" + . ($in[passwd] ? ",passwd='$in[passwd]'" : "") + . " WHERE userid=$id"; + query($query); + } + # update cvsusers stuff + # TODO: create users_cvs record for user that didn't already have one + # (can't just use REPLACE because of the unique index on cvsuser) + $approved = $in[approved] ? 1 : 0; + $query = "UPDATE users_cvs" + . " SET cvsuser='$in[cvsuser]',approved=$approved" + . " WHERE userid=$id"; + query($query); + + warn("record $id updated"); + unset($id); + } + else { + # TODO: handle new users + warn("sorry, i don't know how to create new users yet."); + } } } } -if (isset($id)) { - if ($id) { - $res = mysql_query("SELECT * FROM users WHERE userid=$id"); - $row = mysql_fetch_array($res); - # need to allow all of group@php.net to edit everyone. just me for now. - if ($user != $row[cvsuser] && $user != 'jimw') { - die("can't edit someone else"); - } - } +if ($id) { + $query = "SELECT * FROM users LEFT JOIN users_cvs USING (userid)" + . " WHERE users.userid=$id"; + $res = query($query); + $row = mysql_fetch_array($res); + ?> <table> <form method="POST" action="<?php echo $PHP_SELF;?>"> @@ -109,22 +87,26 @@ <td><input type="text" name="in[email]" value="<?php echo htmlspecialchars($row[email]);?>" size="40" maxlength="255" /></td> </tr> <tr> - <th align="right">CVS name:</th> - <td><input type="text" name="in[cvsuser]" value="<?php echo htmlspecialchars($row[cvsuser]);?>" size="16" maxlength="16" /></td> -</tr> -<tr> <td colspan="2">Leave password fields blank to leave password unchanged.</td> </tr> <tr> <th align="right">Password:</th> - <td><input type="password" name="in[passwd]" value="" size="20" maxlength="20" /></td> + <td><input type="password" name="in[rawpasswd]" value="" size="20" maxlength="20" /></td> </tr> <tr> <th align="right">Password (again):</th> - <td><input type="password" name="in[passwd2]" value="" size="20" maxlength="20" /></td> + <td><input type="password" name="in[rawpasswd2]" value="" size="20" maxlength="20" /></td> +</tr> +<tr> + <th align="right">Password (crypted):</th> + <td><input type="text" name="in[passwd]" value="<?php echo htmlspecialchars($row[passwd]);?>" size="20" maxlength="20" /></td> </tr> <tr> - <th align="right">Approved?</th> + <th align="right">CVS username:</th> + <td><input type="text" name="in[cvsuser]" value="<?php echo htmlspecialchars($row[cvsuser]);?>" size="16" maxlength="16" /></td> +</tr> +<tr> + <th align="right">CVS access?</th> <td><input type="checkbox" name="in[approved]"<?php echo $row[approved] ? " checked" : "";?> /></td> </tr> <tr> @@ -132,7 +114,7 @@ </tr> </table> <? - echo '</body></html>'; + foot(); exit; } @@ -147,7 +129,7 @@ or die("query '$query' failed: ".mysql_error()); $total = mysql_result($res,0); -$query = "SELECT userid,approved,cvsuser,name,email FROM users $orderby $limit"; +$query = "SELECT users.userid,approved,cvsuser,name,email FROM users LEFT JOIN users_cvs USING (userid) $orderby $limit"; $res = mysql_query($query) or die("query '$query' failed: ".mysql_error()); @@ -178,46 +160,30 @@ <?php show_prev_next($begin,mysql_num_rows($res),$max,$total); ?> <p><a href="<?php echo $PHP_SELF;?>?id=0">add a new user</a></p> <?php -echo '</body></html>'; - -function show_prev_next($begin,$rows,$skip,$total) { - global $order, $PHP_SELF;?> -<table border="0" cellspacing="1" width="100%"> - <tr bgcolor="#eeeeee"> - <td align="left" width="33%"> - <?php - if ($begin > 0) { - printf("<a href=\"%s\">« Previous %d", - "$PHP_SELF?order=$order&begin=".max(0,$begin-$skip), - min($skip,$begin)); - } - ?> - - </td> - <td align="center" width="33%"> - <?php echo "Displaying ",$begin+1,"-",$begin+$rows," of $total";?> - </td> - <td align="right" width="33%"> - - <?php - if ($begin+$rows < $total) { - printf("<a href=\"%s\">Next %d »", - "$PHP_SELF?order=$order&begin=".($begin+$skip), - min($skip,$total-($begin+$skip))); - } - ?> - </td> - </tr> -</table> -<?php -} +foot(); function invalid_input($in) { if ($in[cvsuser] && !preg_match("/^[-\w]+\$/",$in[cvsuser])) { return "'$in[cvsuser]' is not a valid username"; } - if ($in[passwd] && $in[passwd] != $in[passwd2]) { + if ($in[rawpasswd] && $in[rawpasswd] != $in[rawpasswd2]) { return "the passwords you specified did not match!"; } return false; +} + +# returns false if $user is not allowed to modify $userid +function can_modify($user,$userid) { + #TODO: use acls, once implemented. + if (in_array($user,array("jimw","rasmus","andrei","zeev","andi","sas","thies","rubys","ssb"))) return true; + + $userid = (int)$userid; + + $quser = addslashes($user); + $query = "SELECT users.userid FROM users LEFT JOIN users_cvs USING (userid)" + . " WHERE users.userid=$userid" + . " AND (email='$quser' OR cvsuser='$quser')"; + + $res = mysql_query($query); + return $res ? mysql_num_rows($res) : false; }
Index: php-master-web/manage/users.php diff -u php-master-web/manage/users.php:1.6 php-master-web/manage/users.php:1.7 --- php-master-web/manage/users.php:1.6 Wed Oct 17 19:03:23 2001 +++ php-master-web/manage/users.php Wed Oct 17 21:44:22 2001 @@ -1,101 +1,79 @@ <?php -require 'cvs-auth.inc'; #TODO: +# /TODO # acls # trigger passwd file update on cvs.php.net # trigger mail alias update on php.net mx # some sort of search # handle flipping of the sort views -if (isset($save) && isset($user) && isset($pw)) { - setcookie("MAGIC_COOKIE",base64_encode("$user:$pw"),time()+3600*24*12,'/','.php.net'); -} -if (isset($MAGIC_COOKIE) && !isset($user) && !isset($pw)) { - list($user,$pw) = explode(":", base64_decode($MAGIC_COOKIE)); -} - -echo '<html><head><title>user administration</title></head><body>'; +require_once 'login.inc'; +require_once 'functions.inc'; -if (!$user || !$pw || !verify_password($user,$pw)) {?> -<form method="POST" action="<?php echo $PHP_SELF?>"> -<input type="hidden" name="save" value="1" /> -<table> - <tr> - <th align="right">Username:</th> - <td><input type="text" name="user" value="<?php echo htmlspecialchars(stripslashes($user));?>" /> - </tr> - <tr> - <th align="right">Password:</th> - <td><input type="password" name="pw" value="<?php echo htmlspecialchars(stripslashes($pw));?>" /> - </tr> - <tr> - <td align="center" colspan="2"><input type="submit" value="Login" /></td> - </tr> -</table> -</form> -<?php - echo '</body></html>'; - exit; -} +head("user administration"); @mysql_connect("localhost","nobody","") or die("unable to connect to database"); @mysql_select_db("php3"); +# ?username=whatever will look up 'whatever' by email or cvs username if (isset($username) && !isset($id)) { - $res = @mysql_query("SELECT userid FROM users WHERE cvsuser='$username' OR email='$username'"); - if ($res) $id = mysql_result($res,0); + $query = "SELECT users.userid FROM users LEFT JOIN users_cvs USING (userid)" + . " WHERE cvsuser='$username' OR email='$username'"; + $res = query($query); + if (!($id = @mysql_result($res,0))) { + warn("wasn't able to find user matching '".clean($username)."'"); + } } if (isset($id) && isset($in)) { - $approved = $in[approved] ? 1 : 0; - - if ($error = invalid_input($in)) { - echo "<p class=\"error\">$error</p>"; + if (!can_modify($user,$id)) { + warn("you're not allowed to modify this user."); } else { - if ($id) { - $res = mysql_query("SELECT * FROM users WHERE userid=$id") - or die("query failed"); - $row = mysql_fetch_array($res); - # need to allow all of group@php.net to edit everyone. just me for now. - if ($user != $row[cvsuser] && $user != 'jimw') { - die("can't edit someone else"); - } - - $pass = crypt($in[passwd],substr(md5(time()),0,2)); - $query = "UPDATE users SET" - . ($in[name] ? " name='$in[name]'" : "") - . ($in[email] ? ",email='$in[email]'," : "") - . ($in[cvsuser] ? "cvsuser='$in[cvsuser]'" : "") - . ($in[passwd] ? ",passwd='$pass]'" : "") - . ",approved=$approved WHERE userid=$id"; + if ($error = invalid_input($in)) { + warn($error); } else { - if ($in[passwd]) $in[passwd] = crypt($in[passwd],substr(md5(time()),0,2)); - $query = "INSERT INTO users (name,email,cvsuser,passwd,approved) VALUES ('$in[name]','$in[email]','$in[cvsuser]','$in[passwd]',$approved)"; - } + if ($in[rawpasswd]) { + $in[passwd] = crypt($in[rawpasswd],substr(md5(time()),0,2)); + } - if (!mysql_query($query)) { - echo "<h2 class=\"error\">Query '$query' failed: ", mysql_error(), "</h2>"; - } - else { - echo "<h2>$in[name] ($in[cvsuser]) ", $id ? "updated" : "added", ".</h2>"; - unset($id); + if ($id) { + # update main table data + if (isset($in[email]) && isset($in[name])) { + $query = "UPDATE users SET name='$in[name]',email='$in[email]'" + . ($in[passwd] ? ",passwd='$in[passwd]'" : "") + . " WHERE userid=$id"; + query($query); + } + # update cvsusers stuff + # TODO: create users_cvs record for user that didn't already have one + # (can't just use REPLACE because of the unique index on cvsuser) + $approved = $in[approved] ? 1 : 0; + $query = "UPDATE users_cvs" + . " SET cvsuser='$in[cvsuser]',approved=$approved" + . " WHERE userid=$id"; + query($query); + + warn("record $id updated"); + unset($id); + } + else { + # TODO: handle new users + warn("sorry, i don't know how to create new users yet."); + } } } } -if (isset($id)) { - if ($id) { - $res = mysql_query("SELECT * FROM users WHERE userid=$id"); - $row = mysql_fetch_array($res); - # need to allow all of group@php.net to edit everyone. just me for now. - if ($user != $row[cvsuser] && $user != 'jimw') { - die("can't edit someone else"); - } - } +if ($id) { + $query = "SELECT * FROM users LEFT JOIN users_cvs USING (userid)" + . " WHERE users.userid=$id"; + $res = query($query); + $row = mysql_fetch_array($res); + ?> <table> <form method="POST" action="<?php echo $PHP_SELF;?>"> @@ -109,22 +87,26 @@ <td><input type="text" name="in[email]" value="<?php echo htmlspecialchars($row[email]);?>" size="40" maxlength="255" /></td> </tr> <tr> - <th align="right">CVS name:</th> - <td><input type="text" name="in[cvsuser]" value="<?php echo htmlspecialchars($row[cvsuser]);?>" size="16" maxlength="16" /></td> -</tr> -<tr> <td colspan="2">Leave password fields blank to leave password unchanged.</td> </tr> <tr> <th align="right">Password:</th> - <td><input type="password" name="in[passwd]" value="" size="20" maxlength="20" /></td> + <td><input type="password" name="in[rawpasswd]" value="" size="20" maxlength="20" /></td> </tr> <tr> <th align="right">Password (again):</th> - <td><input type="password" name="in[passwd2]" value="" size="20" maxlength="20" /></td> + <td><input type="password" name="in[rawpasswd2]" value="" size="20" maxlength="20" /></td> +</tr> +<tr> + <th align="right">Password (crypted):</th> + <td><input type="text" name="in[passwd]" value="<?php echo htmlspecialchars($row[passwd]);?>" size="20" maxlength="20" /></td> </tr> <tr> - <th align="right">Approved?</th> + <th align="right">CVS username:</th> + <td><input type="text" name="in[cvsuser]" value="<?php echo htmlspecialchars($row[cvsuser]);?>" size="16" maxlength="16" /></td> +</tr> +<tr> + <th align="right">CVS access?</th> <td><input type="checkbox" name="in[approved]"<?php echo $row[approved] ? " checked" : "";?> /></td> </tr> <tr> @@ -132,7 +114,7 @@ </tr> </table> <? - echo '</body></html>'; + foot(); exit; } @@ -147,7 +129,7 @@ or die("query '$query' failed: ".mysql_error()); $total = mysql_result($res,0); -$query = "SELECT userid,approved,cvsuser,name,email FROM users $orderby $limit"; +$query = "SELECT users.userid,approved,cvsuser,name,email FROM users LEFT JOIN users_cvs USING (userid) $orderby $limit"; $res = mysql_query($query) or die("query '$query' failed: ".mysql_error()); @@ -178,46 +160,30 @@ <?php show_prev_next($begin,mysql_num_rows($res),$max,$total); ?> <p><a href="<?php echo $PHP_SELF;?>?id=0">add a new user</a></p> <?php -echo '</body></html>'; - -function show_prev_next($begin,$rows,$skip,$total) { - global $order, $PHP_SELF;?> -<table border="0" cellspacing="1" width="100%"> - <tr bgcolor="#eeeeee"> - <td align="left" width="33%"> - <?php - if ($begin > 0) { - printf("<a href=\"%s\">« Previous %d", - "$PHP_SELF?order=$order&begin=".max(0,$begin-$skip), - min($skip,$begin)); - } - ?> - - </td> - <td align="center" width="33%"> - <?php echo "Displaying ",$begin+1,"-",$begin+$rows," of $total";?> - </td> - <td align="right" width="33%"> - - <?php - if ($begin+$rows < $total) { - printf("<a href=\"%s\">Next %d »", - "$PHP_SELF?order=$order&begin=".($begin+$skip), - min($skip,$total-($begin+$skip))); - } - ?> - </td> - </tr> -</table> -<?php -} +foot(); function invalid_input($in) { if ($in[cvsuser] && !preg_match("/^[-\w]+\$/",$in[cvsuser])) { return "'$in[cvsuser]' is not a valid username"; } - if ($in[passwd] && $in[passwd] != $in[passwd2]) { + if ($in[rawpasswd] && $in[rawpasswd] != $in[rawpasswd2]) { return "the passwords you specified did not match!"; } return false; +} + +# returns false if $user is not allowed to modify $userid +function can_modify($user,$userid) { + #TODO: use acls, once implemented. + if (in_array($user,array("jimw","rasmus","andrei","zeev","andi","sas","thies","rubys","ssb"))) return true; + + $userid = (int)$userid; + + $quser = addslashes($user); + $query = "SELECT users.userid FROM users LEFT JOIN users_cvs USING (userid)" + . " WHERE users.userid=$userid" + . " AND (email='$quser' OR cvsuser='$quser')"; + + $res = mysql_query($query); + return $res ? mysql_num_rows($res) : false; }