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