-
Notifications
You must be signed in to change notification settings - Fork 0
/
user_mgmt.php
395 lines (358 loc) · 13.3 KB
/
user_mgmt.php
1
2
3
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
79
80
81
82
83
84
85
86
87
88
89
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
<doctype! html>
<html>
<head></head>
<body>
<?php
class Woot{ public $message, $timestamp; }
class User{
public $username, $password, $id, $full_name, $age, $email;
public $followers, $followees, $free_bit, $num_woots;
}
$PORT_NUM=13090;
$SEND_SIZE=560;
$TIME_OUT=50;
// the following max values are hard coded into registration.html to not have to change it to .php
$MIN_USER_LEN=8; $MAX_USER_LEN=24+1;
$MIN_PWD_LEN=10; $MAX_PWD_LEN=32+1;
$MAX_ID_LEN=10+1; // at most 10 digits for the # of users
$MAX_FNM_LEN=25; $MAX_LNM_LEN=25+1; $MAX_EML_LEN=100+1;
$MAX_NUM_WOOTS=10; // at most 10 digits for the # of woots per user
// age is hardcoded to 4, 3 digits + 1 pad
$MAX_FLR_LEN=10+1; $MAX_FLE_LEN=10+1; // at most 10 digits for the # of followees
$MAX_ULINE_LEN=$MAX_USER_LEN+$MAX_PWD_LEN+$MAX_ID_LEN+$MAX_FNM_LEN+1+$MAX_LNM_LEN+4+$MAX_EML_LEN+$MAX_FLR_LEN+$MAX_FLE_LEN+1+$MAX_NUM_WOOTS+1;
$USERS_PER_FILE=10; $FID_HDR_LEN=5; $FLRS_PER_LINE=10;
$MAX_FLR_LINE_LEN=($MAX_ID_LEN*$FLRS_PER_LINE)+1;
$MY_DELIMITER=".";
$WOOTS_PER_LINE=10; $MAX_WOOT_LEN=100; $MAX_WOOT_TMSTP=22;
$MAX_WOOT_LINE_LEN=$WOOTS_PER_LINE*($MAX_WOOT_LEN+1+$MAX_WOOT_TMSTP+1);
// interfaces to client functions
function create_new_user($user_obj){
/*
create_new_user() takes in a user object assembled from the registration page and fully
integrates that user into the system. This is done by finding it a free user ID and by
writing the user's attributes to the first available spot in the user file base.
* The user object is then returned for convenience.
*/
global $PORT_NUM;
return real_create_new_user($user_obj, $PORT_NUM);
}
function flush_user($user_obj){
/*
flush_user() writes the attributes of $user_obj one by one into the file $file_handle
at the offset $offset. Each attribute is padded to meet its maximum field size
so that each field, line, and user file are of a fixed, constant size.
*/
global $PORT_NUM;
return real_flush_user($user_obj, $PORT_NUM);
}
function read_user($username){
/*
read_user() takes in a username string, then finds it in the user file base and returns its user object.
If the user does not exist in the user file base, a void return takes place.
*/
global $PORT_NUM;
return real_read_user($username, $PORT_NUM);
}
function read_id($id_num){
/*
read_id() takes in a username id #, then finds it in the user file base and returns its user object.
If the user does not exist in the user file base, a void return takes place.
This function allows for O(1) look up for followers/followees instead of an O(n) scan.
*/
global $PORT_NUM;
return real_read_id($id_num, $PORT_NUM);
}
function follow($home_user, $other_user){
/*
follow() creates a "is following" relationship between the logged in $home_user and the target $other_user.
A reciprocating "is being followed by" is created between the target user and logged in user.
*/
global $PORT_NUM;
return real_follow($home_user, $other_user, $PORT_NUM);
}
function unfollow($home_user, $other_user){
/*
unfollow() takes in a logged in $home_user and a target $other_user and destroys any "is following"
relationship from the first to the second. It also destroys any reciprocating "is being followed by"
relationship from the second to the first.
An "lost follower" leaves an empty ID field for that portion of the file, which can then be recycled
by future follow() attempts using first_free to find said empty field.
* This is to prevent subsequent follow/unfollows from arbitrarily growing the # of files tracking this information.
*/
global $PORT_NUM;
return real_unfollow($home_user, $other_user, $PORT_NUM);
}
function load_following($home_user, $string, $num){
/*
load_following() takes in the logged in user object, the kind of following (followers
or followees), the # to read in, and then returns an array of those user IDs.
*/
global $PORT_NUM;
return real_load_following($home_user, $string, $num, $PORT_NUM);
}
function write_woot($home_user, $woot, $timestamp){
/*
write_woot() takes in the logged in user object, the woot to write, and the timestamp
associated with that woot and writes this to the woot file base.
*/
global $PORT_NUM;
return real_write_woot($home_user, $woot, $timestamp, $PORT_NUM);
}
function load_woots($home_user, $num_woots){
/*
load_woots() takes in the logged in user object, the number of woots to load, and returns
an array of that many woots starting from the most recent one.
*/
global $PORT_NUM;
return real_load_woots($home_user, $num_woots, $PORT_NUM, "n");
}
function load_woots_seq($home_user, $num_woots){
/*
load_woots_seq() takes in the logged in user object, the number of woots to load, and returns
an array of that many woots starting from the least recent one.
*/
global $PORT_NUM;
return real_load_woots($home_user, $num_woots, $PORT_NUM, "y");
}
function already_following($home_user, $other_id){
global $PORT_NUM;
return real_already_following($home_user, $other_id, $PORT_NUM);
}
// internal functions that handle communicating with network server
function format($some_str, $length){
while(strlen($some_str)< $length){ $some_str=$some_str.' '; }
return $some_str;
}
function user_explode($user_obj){
global $MAX_USER_LEN, $MAX_PWD_LEN, $MAX_ID_LEN, $MAX_FNM_LEN, $MAX_LNM_LEN, $MAX_EML_LEN, $MAX_FLR_LEN, $MAX_FLE_LEN, $MAX_NUM_WOOTS, $MAX_USER_LEN;
$exploded="";
if(isset($user_obj)) { // if an empty user, just send over an empty string
$exploded=$exploded.str_pad($user_obj->username, $MAX_USER_LEN, " ");
$exploded=$exploded.str_pad($user_obj->password, $MAX_PWD_LEN, " ");
$exploded=$exploded.str_pad($user_obj->id, $MAX_ID_LEN, " ");
$exploded=$exploded.str_pad($user_obj->full_name, $MAX_FNM_LEN+$MAX_LNM_LEN+1, " ");
$exploded=$exploded.str_pad($user_obj->age, 4, " ");
$exploded=$exploded.str_pad($user_obj->email, $MAX_EML_LEN, " ");
$exploded=$exploded.str_pad($user_obj->followers, $MAX_FLR_LEN, " ");
$exploded=$exploded.str_pad($user_obj->followees, $MAX_FLE_LEN, " ");
$exploded=$exploded.$user_obj->free_bit;
$exploded=$exploded.str_pad($user_obj->num_woots, $MAX_NUM_WOOTS, " ");
}
else { $exploded=str_pad(" ", $MAX_USER_LEN); }
return $exploded;
}
function user_unexplode($user_obj, $user_str){
global $MAX_USER_LEN, $MAX_PWD_LEN, $MAX_ID_LEN, $MAX_FNM_LEN, $MAX_LNM_LEN, $MAX_EML_LEN, $MAX_FLR_LEN, $MAX_FLE_LEN, $MAX_NUM_WOOTS;
$offset=0;
$user_obj->username=trim(substr($user_str, $offset, $MAX_USER_LEN));
if(empty($user_obj->username)) { // if no user sent over, stop reading/unserializing here
unset($user_obj);
return;
}
$offset+=$MAX_USER_LEN;
$user_obj->password=trim(substr($user_str, $offset, $MAX_PWD_LEN));
$offset+=$MAX_PWD_LEN;
$user_obj->id=trim(substr($user_str, $offset, $MAX_ID_LEN) );
$offset+=$MAX_ID_LEN;
$user_obj->full_name=trim(substr($user_str, $offset, $MAX_FNM_LEN + $MAX_LNM_LEN +1));
$offset+=$MAX_FNM_LEN+$MAX_LNM_LEN+1;
$user_obj->age=trim(substr($user_str, $offset, 4));
$offset+=4;
$user_obj->email=trim(substr($user_str, $offset, $MAX_EML_LEN));
$offset+=$MAX_EML_LEN;
$user_obj->followers=trim(substr($user_str, $offset, $MAX_FLR_LEN) );
$offset+=$MAX_FLR_LEN;
$user_obj->followees=trim(substr($user_str, $offset, $MAX_FLE_LEN) );
$offset+=$MAX_FLE_LEN;
$user_obj->free_bit=trim(substr($user_str, $offset, 1) );
$offset++;
$user_obj->num_woots=trim(substr($user_str, $offset, $MAX_NUM_WOOTS) );
return $user_obj;
}
function real_create_new_user($user_obj, $port){
global $SEND_SIZE;
$fp = stream_socket_client('localhost:'.$port, $errno, $errstr, $TIME_OUT);
if (!$fp) {
echo $errstr;
exit(1);
}
$request=format(format("create_new_user ",40).user_explode($user_obj), $SEND_SIZE );
fwrite($fp, $request);
$line=fgets($fp);
$user_obj=user_unexplode($user_obj, $line);
fclose($fp);
}
function real_read_user($user_name, $port){
global $SEND_SIZE;
$fp = stream_socket_client('localhost:'.$port, $errno, $errstr, $TIME_OUT);
if (!$fp) {
echo $errstr;
exit(1);
}
$request=format(format("read_user",40).$user_name, $SEND_SIZE );
fwrite($fp, $request);
$line=fgets($fp);
$user_obj=new User;
$user_obj=user_unexplode($user_obj, $line);
fclose($fp);
return $user_obj;
}
function real_flush_user($user_obj, $port) {
global $SEND_SIZE;
$fp = stream_socket_client('localhost:'.$port, $errno, $errstr, $TIME_OUT);
if (!$fp) {
echo $errstr;
exit(1);
}
$request=format(format("flush_user",40).user_explode($user_obj), $SEND_SIZE );
fwrite($fp, $request);
$line=fgets($fp);
fclose($fp);
$user_obj=new User;
$user_obj=user_unexplode($user_obj, $line);
return $user_obj; // potentially unnecessary
}
function real_read_id($id, $port){
global $SEND_SIZE;
$fp = stream_socket_client('localhost:'.$port, $errno, $errstr, $TIME_OUT);
if (!$fp) {
echo $errstr;
exit(1);
}
$request=format(format("read_id",40).$id, $SEND_SIZE );
fwrite($fp, $request);
$line=fgets($fp);
fclose($fp);
$user_obj=new User;
$user_obj=user_unexplode($user_obj, $line);
return $user_obj;
}
function real_follow($home_user, $other_user, $port){
global $SEND_SIZE;
$fp = stream_socket_client('localhost:'.$port, $errno, $errstr, $TIME_OUT);
if (!$fp) {
echo $errstr;
exit(1);
}
$request=format(format("follow",40).user_explode($home_user)." ".user_explode($other_user), $SEND_SIZE );
fwrite($fp, $request);
$line=fgets($fp);
$home_user=user_unexplode($home_user, $line);
$line=fgets($fp);
$other_user=user_unexplode($other_user, $line);
fclose($fp);
}
function real_unfollow($home_user, $other_user, $port){
global $SEND_SIZE;
$fp = stream_socket_client('localhost:'.$port, $errno, $errstr, $TIME_OUT);
if (!$fp) {
echo $errstr;
exit(1);
}
$request=format(format("unfollow",40).user_explode($home_user)." ".user_explode($other_user), $SEND_SIZE );
fwrite($fp, $request);
$line=fgets($fp);
$home_user=user_unexplode($home_user, $line);
$line=fgets($fp);
$other_user=user_unexplode($other_user, $line);
fclose($fp);
}
function real_load_following($home_user, $group_type, $num_ppl, $port){
global $SEND_SIZE, $MAX_ID_LEN;
$fp = stream_socket_client('localhost:'.$port, $errno, $errstr, $TIME_OUT);
if (!$fp) {
echo $errstr;
exit(1);
}
$ids_per_msg=(int)($SEND_SIZE/($MAX_ID_LEN+1));
$request=format(format("load_following",40).user_explode($home_user)." ".$group_type." ".$num_ppl, $SEND_SIZE );
fwrite($fp, $request);
$cult=array();
$ids_received=0;
while($line=fgets($fp)){
$offset=0;
for($current_id=0; $current_id<$ids_per_msg && $ids_received<$num_ppl; $current_id++, $ids_received++){
$read_id=trim(substr($line, $offset, $MAX_ID_LEN));
if($read_id=="") { break; }
$offset+=$MAX_ID_LEN+1; // a space between it and the next one
array_push($cult, $read_id);
}
}
fclose($fp);
return $cult;
}
function cult_unexplode($line, $num_ppl){
global $MAX_ID_LEN;
$cult=array();
$offset=0; // does not do any safety checking on $num_ppl
for($index=0; $index<$num_ppl;$index++){
$current_id=trim(substr($line, $offset, $MAX_ID_LEN));
if(!empty(trim(substr($line, $offset, $MAX_ID_LEN)))){ array_push($cult, $current_id); }
$offset+=$MAX_ID_LEN;
}
return $cult;
}
function real_write_woot($other_user, $woot, $timestamp, $port){
global $SEND_SIZE, $MAX_WOOT_LEN, $MAX_WOOT_TMSTP;
$fp = stream_socket_client('localhost:'.$port, $errno, $errstr, $TIME_OUT);
if (!$fp) {
echo $errstr;
exit(1);
}
$ready_woot=trim(preg_replace('/\s+/', ' ', $woot));
if(!empty($ready_woot)) {
$ready_woot=str_pad($ready_woot, $MAX_WOOT_LEN, " ");
$request=format(format("write_woot",40).user_explode($other_user)." ".$ready_woot." ".str_pad($timestamp, $MAX_WOOT_TMSTP, " "), $SEND_SIZE );
fwrite($fp, $request);
$line=fgets($fp);
fclose($fp);
$other_user=user_unexplode($other_user, $line);
}
return $other_user; // to update num_woots
}
function real_load_woots($other_user, $num_woots, $port, $seq){
global $SEND_SIZE, $MAX_WOOT_LEN, $MAX_WOOT_TMSTP;
$fp = stream_socket_client('localhost:'.$port, $errno, $errstr, $TIME_OUT);
if (!$fp) {
echo $errstr;
exit(1);
}
$woots_per_msg=(int)($SEND_SIZE/($MAX_WOOT_LEN+$MAX_WOOT_TMPSTP+1));
if($seq=="y"){ $request=format(format("load_woots_seq",40).user_explode($other_user)." ".$num_woots, $SEND_SIZE ); }
else { $request=format(format("load_woots",40).user_explode($other_user)." ".$num_woots, $SEND_SIZE );}
fwrite($fp, $request); // assumes it works, that it must work
$woots=array();
$woots_received=0;
while($line = fgets($fp)) {
$offset=0;
for($current_woot=0; $current_woot<$woots_per_msg && $woots_received<$num_woots; $current_woot++, $woots_received++){ // 560 char, 124 char woots, 4 woots per line
$read_woot=new Woot;
$read_woot->message=trim(substr($line, $offset, $MAX_WOOT_LEN));
if(empty($read_woot->message)) { break; }
$offset+=$MAX_WOOT_LEN+1; // a space between it and the timestamp
$read_woot->timestamp=trim(substr($line, $offset, $MAX_WOOT_TMSTP+1));
$offset+=$MAX_WOOT_TMSTP+1; // a space between it and the next one
array_push($woots, $read_woot);
}
}
fclose($fp);
return $woots;
}
function real_already_following($home_user, $other_id, $port){
global $SEND_SIZE;
$fp = stream_socket_client('localhost:'.$port, $errno, $errstr, $TIME_OUT);
if (!$fp) {
echo $errstr;
exit(1);
}
$request=format(format("already_following",40).user_explode($home_user)." ".$other_id, $SEND_SIZE );
fwrite($fp, $request);
$line=fgets($fp);
$answer=trim(substr($line, 0, 3));
if( $answer != "YES"){ unset($answer); } // hardcoded YES or NO
fclose($fp);
return $answer;
}
?>
</body>
</html>