Initial version of the create-assurers script
[cacert-mgr.git] / dbadm / create-assurers.php
1 #!/usr/bin/php
2 <?php
3 /**
4 * Script to create more dummy assurer accounts (for use on the test system)
5 * which can be used by the test manager to create automated assurances
6 *
7 * @author Michael Tänzer
8 */
9
10 class CreateAssurers {
11 protected $db;
12
13 // define constants for the positions in $argv
14 const ARGV_SCRIPT = 0;
15 const ARGV_DB_HOST = 1;
16 const ARGV_DB_USER = 2;
17 const ARGV_DB_PASSWD = 3;
18 const ARGV_DB_NAME = 4;
19 const ARGV_QUANTITY = 5;
20
21 // required length of $argv
22 const ARGV_LENGTH = 6;
23
24
25 function __construct(mysqli $db){
26 $this->db = $db;
27 }
28
29
30 public static function echoUsage($script_name){
31 print 'Usage: ' . $script_name; ?> <db_host> <db_user> <db_passwd>
32 <db_name> <quantity>
33
34 Script to create more dummy assurer accounts (for use on the test system)
35 which can be used by the test manager to create automated assurances
36
37 <quantity> specifies how many new dummy accounts should be created
38
39 <?php
40 }
41
42
43 public static function main(){
44 $argc = $_SERVER['argc'];
45 $argv = $_SERVER['argv'];
46
47 if ($argc != self::ARGV_LENGTH){
48 self::echoUsage($argv[self::ARGV_SCRIPT]);
49 fwrite(STDERR, "Error: Invalid number of parameters\n");
50 exit(1);
51 }
52
53 $quantity = (int)($argv[self::ARGV_QUANTITY]);
54 if (!is_numeric($argv[self::ARGV_QUANTITY]) || $quantity < 0){
55 self::echoUsage($argv[self::ARGV_SCRIPT]);
56 fwrite(STDERR, "Error: Last parameter has to be a positive integer\n");
57 exit(2);
58 }
59
60 // Try to connect to the database
61 $db = new mysqli($argv[self::ARGV_DB_HOST], $argv[self::ARGV_DB_USER],
62 $argv[self::ARGV_DB_PASSWD], $argv[self::ARGV_DB_NAME]);
63 if (mysqli_connect_error()){
64 fwrite(STDERR, "Error: Could not connect to the data base\n".
65 "(".mysqli_connect_errno()."): ".mysqli_connect_error());
66 exit(3);
67 }
68
69
70 $me = new self($db);
71 $status = $me->createAssurers($quantity);
72
73 $db->close();
74 exit($status);
75 }
76
77
78 public function createAssurers($quantity){
79 // get last assurer
80 $result = $this->db->query('select `mname` from `users` where `id`=
81 (select max(`id`) from `users` where `email` like
82 \'john.doe-___@example.com\')');
83 $row = $result->fetch_assoc();
84 if ($row === NULL){
85 $last_assurer = 0;
86 printf("1\n");
87 } else {
88 $last_assurer = (int)($row['mname']);
89 printf("2: \$last_assurer: %d\n", $last_assurer);
90 }
91
92
93 // prepare the statements
94 $insert_user = $this->db->prepare('insert into `users` set
95 `email` = ? ,
96 `password` = \'invalid\' ,
97 `fname` = ? ,
98 `mname` = ? ,
99 `lname` = \'Doe\' ,
100 `suffix` = \'\' ,
101 `dob` = ? ,
102 `Q1` = SHA1(rand()) ,
103 `Q2` = SHA1(rand()) ,
104 `Q3` = SHA1(rand()) ,
105 `Q4` = SHA1(rand()) ,
106 `Q5` = SHA1(rand()) ,
107 `A1` = SHA1(rand()) ,
108 `A2` = SHA1(rand()) ,
109 `A3` = SHA1(rand()) ,
110 `A4` = SHA1(rand()) ,
111 `A5` = SHA1(rand()) ,
112 `created` = now() ,
113 `uniqueID` = SHA1(rand()) ,
114 `verified` = 1 ,
115 `assurer` = 1 ');
116 $insert_user->bind_param('ssss', $email, $fname, $mname, $dob);
117
118 $insert_email = $this->db->prepare('insert into `email` set
119 `email` = ? ,
120 `hash` = \'\' ,
121 `created` = now() ,
122 `modified` = now() ,
123 `memid` = ? ');
124 $insert_email->bind_param('si', $email, $memid);
125
126 $insert_alerts = $this->db->prepare('insert into `alerts` set
127 `memid` = ? ,
128 `general` = 0 ,
129 `country` = 0 ,
130 `regional` = 0 ,
131 `radius` = 0 ');
132 $insert_alerts->bind_param('i', $memid);
133
134 $insert_points = $this->db->prepare('insert into `notary` set
135 `from` = ? ,
136 `to` = ? ,
137 `points` = 150 ,
138 `awarded` = 150 ,
139 `location` = \'Init Points\' ,
140 `date` = curdate() ,
141 `method` = \'Administrative Increase\' ,
142 `when` = now() ');
143 $insert_points->bind_param('ii', $memid, $memid);
144
145 $insert_cats = $this->db->prepare('insert into `cats_passed` set
146 `user_id` = ? ,
147 `pass_date` = now() ,
148 `variant_id` =
149 (select `id` from `cats_variant` where `type_id` = 1) ');
150 $insert_cats->bind_param('i', $memid);
151
152
153 // do the actual work
154 for ($i = $last_assurer + 1; $i <= $last_assurer + $quantity; $i++){
155 $email = sprintf('john.doe-%03u@example.com', $i);
156 $fname = sprintf('John %u', $i);
157 $mname = sprintf('%u', $i);
158 $dob = sprintf('19%02u-01-%02u', $i % 90, (int)(($i/90) + 1) );
159
160 $insert_user->execute();
161 $memid = $insert_user->insert_id;
162 if ($memid == 0){
163 fwrite(STDERR, "Error: didn't get a valid ID for the user\n");
164 return 10;
165 }
166
167 $insert_email->execute();
168 $insert_alerts->execute();
169 $insert_points->execute();
170 $insert_cats->execute();
171
172 printf('Assurer number %u %s Doe <%s>'."\n".
173 'born on %s with the ID %d has been added'."\n", $i,
174 $fname, $email, $dob, $memid);
175 }
176
177 return 0;
178 }
179 }
180
181
182 CreateAssurers::main();