Newer
Older
# WiSP multi-user add
# Copyright (C) 2007-2009, AllWorldIT
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
include_once("includes/header.php");
include_once("includes/footer.php");
include_once("includes/db.php");
$db = connect_db();
printHeader(array(
));
if (!isset($_POST['frmaction'])) {
?>
<p class="pageheader">Add WiSP Users</p>
<!-- Add user input fields -->
<form method="post" action="wisp-multiuser-add.php">
<div>
<input type="hidden" name="frmaction" value="insert" />
</div>
<table class="entry">
<tr>
<td class="textcenter" colspan="2">Add multiple users</td>
</tr>
<tr>
<td><div></div><td>
</tr>
<tr>
<td class="entrytitle">Number of users</td>
<td><input type="text" name="num_users" /></td>
</tr>
<tr>
<td class="entrytitle">Login Prefix</td>
<td><input type="text" name="login_prefix" /></td>
</tr>
<tr>
<td class="entrytitle">Uptime Limit</td>
<td><input type="text" name="session_timeout" /></td>
</tr>
<tr>
<td class="entrytitle">Data Limit</td>
<td><input type="text" name="data_limit" /></td>
</tr>
<tr>
<td class="entrytitle">Time Limit</td>
<td><input type="text" name="time_limit" /></td>
</tr>
<tr>
<td class="textcenter" colspan="2"><input type="submit" value="Submit" /></td>
</tr>
</table>
</form>
<?php
}
if (isset($_POST['frmaction']) && $_POST['frmaction'] == "insert") {
?>
<p class="pageheader">Add WiSP Users</p>
<?php
# Perform checks on input
if (!empty($_POST['num_users']) && !empty($_POST['session_timeout']) && !empty($_POST['data_limit']) && !empty($_POST['time_limit'])) {
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
$db->beginTransaction();
$numberOfUsers = (int)$_POST['num_users'];
$sessionTimeout = (int)$_POST['session_timeout'];
$dataLimit = (int)$_POST['data_limit'];
$timeLimit = (int)$_POST['time_limit'];
$loginNamePrefix = $_POST['login_prefix'];
for ($counter = 0; $counter <= $numberOfUsers; $counter += 1) {
# Check if user already exists
$checkUsernameDuplicates = 0;
do {
# Generate random username
$randomString = chr(rand(97,122)).
chr(rand(97,122)).
chr(rand(97,122)).
chr(rand(97,122)).
chr(rand(97,122)).
chr(rand(97,122)).
chr(rand(97,122)).
chr(rand(97,122));
# If there is no login name prefix
if (empty($loginNamePrefix)) {
$userName = $randomString;
$lookForUser = $db->query("SELECT ID FROM ${DB_TABLE_PREFIX}users WHERE Username LIKE '%$userName%'");
# If the user was found
$checkUsernameDuplicates = 1;
} else {
$checkUsernameDuplicates = 0;
}
# If there is a login name prefix
} else {
$userName = $loginNamePrefix."_".$randomString;
$lookForUser = $db->query("SELECT ID FROM ${DB_TABLE_PREFIX}users WHERE Username LIKE '%$userName%'");
# If the user was found
$checkUsernameDuplicates = 1;
} else {
$checkUsernameDuplicates = 0;
}
}
} while ($checkUsernameDuplicates > 0);
#Insert user into users table
$userInsert = $db->prepare("INSERT INTO
${DB_TABLE_PREFIX}users (Username)
VALUES
(?)
");
$userInsertExec = $userInsert->execute(array($userName));
$failed = 0;
# After a user add is successful, continue with inserting the other data
if ($userInsertExec) {
# Get user ID to insert into other tables
$getUserID = $db->query("SELECT ID FROM ${DB_TABLE_PREFIX}users WHERE Username = '$userName'");
$resultRow = $getUserID->fetchObject();
$userID = $resultRow->id;
$userDataStatement = $db->prepare(" INSERT INTO
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
VALUES
(?)
");
$userDataResult = $userDataStatement->execute(array($userID));
# Generate a password
$userPassword = chr(rand(97,122)).
chr(rand(97,122)).
chr(rand(97,122)).
chr(rand(97,122)).
chr(rand(97,122)).
chr(rand(97,122)).
chr(rand(97,122)).
chr(rand(97,122));
# Insert password into user_attributes table
$userPasswordStatement = $db->prepare(" INSERT INTO
${DB_TABLE_PREFIX}user_attributes (UserID,Name,Operator,Value)
VALUES
($userID,'User-Password','==',?)
");
$userPasswordResult = $userPasswordStatement->execute(array($userPassword));
# Insert data limit into user_attributes table
$userDataLimitStatement = $db->prepare("INSERT INTO
${DB_TABLE_PREFIX}user_attributes (UserID,Name,Operator,Value)
VALUES
($userID,'SMRadius-Capping-Traffic-Limit',':=',?)
");
$userDataLimitResult = $userDataLimitStatement->execute(array($dataLimit,));
# Insert time limit into user_attributes table
$userTimeStatement = $db->prepare(" INSERT INTO
${DB_TABLE_PREFIX}user_attributes (UserID,Name,Operator,Value)
VALUES
($userID,'SMRadius-Capping-UpTime-Limit',':=',?)
");
$userTimeResult = $userTimeStatement->execute(array($timeLimit,));
# Insert timeout into user_attributes table
$userTimeOutStatement = $db->prepare(" INSERT INTO
${DB_TABLE_PREFIX}user_attributes (UserID,Name,Operator,Value)
VALUES
($userID,'Session-Timeout','+=',?)
");
$userTimeOutResult = $userTimeOutStatement->execute(array($sessionTimeout,));
if ($userTimeOutResult && $userTimeResult && $userDataResult && $userPasswordResult && $userDataLimitResult) {
$failed = 0;
} else {
$failed = 1;
}
# If one was not successful, rollback
} else {
print_r($db->errorInfo());
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
$failed = 1;
break;
}
}
if ($failed == 0) {
$db->commit();
?>
<div class="notice">Users added</div>
<?php
}
} else {
?>
<div class="warning">One or more fields have been left empty</div>
<?php
}
}
printFooter();
# vim: ts=4
?>