approval.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. <?php
  2. session_start();
  3. if($_SESSION["slogged"] == false) {
  4. header('Location: login.php?referer=approval.php?accountid='.$_GET['accountid']);
  5. exit();
  6. }
  7. include('logo.html');
  8. include('menulist.html');
  9. include('configuration');
  10. $accountid = $_GET['accountid'];
  11. $username = $_SESSION['susername'];
  12. if(!isset($_POST['submit']) && !isset($_POST['cancel']))
  13. {
  14. //checking for admin rites
  15. $handle = sqlite3_open($db) or die("Could not open database");
  16. $query = "SELECT * FROM users where username=\"$username\"";
  17. $result = sqlite3_query($handle, $query) or die("Error in query: ".sqlite3_error($handle));
  18. $row = sqlite3_fetch_array($result);
  19. if ($row["admin"] == "false") {
  20. echo "<div align=\"center\">You donot have a rite to approve users</div>";
  21. exit();
  22. }
  23. //extracting user information
  24. $query = "SELECT * FROM users where username=\"$accountid\"";
  25. $result = sqlite3_query($handle, $query) or die("Error in query: ".sqlite3_error($handle));
  26. if (($row = sqlite3_fetch_array($result)) == '') {
  27. echo "<div align=\"center\">Not a valid account id</div>";
  28. exit();
  29. }
  30. $name = $row["name"];
  31. $email = $row["email"];
  32. $website = $row["website"];
  33. $desc = $row["description"];
  34. if($row["approved"] == "true") {
  35. $query = "SELECT * FROM approval where approved=\"$accountid\"";
  36. $result = sqlite3_query($handle, $query) or die("Error in query: ".sqlite3_error($handle));
  37. $row = sqlite3_fetch_array($result);
  38. echo "<div align=\"center\"> Already <b>$accountid</b> is approved by <b>".$row["approvedby"]."</b>...</div>";
  39. exit();
  40. }
  41. ?>
  42. <h3 class="firstheader" align="center">Approval</h3>
  43. <form method="post" action="approval.php?accountid=<?=$accountid?>" >
  44. <table align="center" border="0" cellpadding="10" bgcolor="gray">
  45. <tr align="left" bgcolor="lightgray">
  46. <td> <p>Full name: </td><td><?=$name;?></p></td>
  47. </tr>
  48. <tr align="left" bgcolor="silver">
  49. <td> <p>Account id: </td><td><?=$accountid;?></p></td>
  50. </tr>
  51. <tr align="left" bgcolor="lightgray">
  52. <td> <p>Email: </td><td><?=$email;?></p>
  53. </td>
  54. </tr>
  55. <tr align="left" bgcolor="silver">
  56. <td> <p>Description: </td><td><?=$desc;?></p> </td>
  57. </tr>
  58. <tr align="left" bgcolor="lightgray">
  59. <td> <p>Web Site: </td><td><?=$website;?></p> </td>
  60. </tr>
  61. <tr align="left" bgcolor="lightgray">
  62. <td>Admin rites</td><td><input type="checkbox" name="isadmin"> (check this if you want this user to be admin) </td>
  63. </tr>
  64. <tr align="center">
  65. <td> <input type="submit" name="submit" value="Approve"/></td>
  66. <td><input type="submit" name="cancel" value="Cancel Approval"/></td>
  67. </tr>
  68. </table>
  69. </form><hr>
  70. </body>
  71. </html>
  72. <?php
  73. }
  74. //if approved
  75. else if (isset($_POST['submit'])) {
  76. //set approval=true
  77. $handle = sqlite3_open($db) or die("Could not open database");
  78. $query = "update users set approved=\"true\" where username=\"$accountid\"";
  79. $result = sqlite3_exec($handle, $query) or die("Error in query: ".sqlite3_error($handle));
  80. //where whether user is given admin permission
  81. if($_POST['isadmin']) {
  82. $query = "update users set admin=\"true\" where username=\"$accountid\"";
  83. $result = sqlite3_exec($handle, $query) or die("Error in query: ".sqlite3_error($handle));
  84. }
  85. //inserting to db
  86. $query = "insert into approval values(\"$accountid\",\"$username\")";
  87. $result = sqlite3_exec($handle, $query) or die("Error in query: ".sqlite3_error($handle));
  88. //get email id
  89. $query = "SELECT * FROM users where username=\"$accountid\"";
  90. $result = sqlite3_query($handle, $query) or die("Error in query: ".sqlite3_error($handle));
  91. $row = sqlite3_fetch_array($result);
  92. $email = $row["email"];
  93. //generate password and send mail
  94. $password = generate_passwd(8);
  95. $subject = "Approval of ccan account";
  96. $message = "Your request for ccan account id is being approved.\n\n Please use the following password to login\n Password: ".$password;
  97. $password = md5($password);
  98. //insert password
  99. $query = "insert into login (username,password) values(\"$accountid\",\"$password\")";
  100. $result = sqlite3_exec($handle, $query) or die("Error in query: ".sqlite3_error($handle));
  101. //sendmail
  102. mail($email, $subject, $message, "From: $frommail");
  103. echo "<div align=center> Successfully approved <b>$accountid</b>...</div>";
  104. }
  105. //if approval is canceled
  106. else if (isset($_POST['cancel'])) {
  107. //delete user
  108. $handle = sqlite3_open($db) or die("Could not open database");
  109. $query = "delete from users where username=\"$accountid\"";
  110. $result = sqlite3_exec($handle, $query) or die("Error in query: ".sqlite3_error($handle));
  111. echo "<div align=center> Successfully cancelled <b>$accountid</b>...</div>";
  112. }
  113. function generate_passwd($length = 16) {
  114. static $chars = '!@#$%^&*abcdefghjkmnpqrstuvwxyzABCDEFGHJKLMNOPQRSTUVWXYZ23456789';
  115. $chars_len = strlen($chars);
  116. for ($i = 0; $i < $length; $i++)
  117. $password .= $chars[mt_rand(0, $chars_len - 1)];
  118. return $password;
  119. }
  120. ?>