-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathuser_valid.php
582 lines (454 loc) · 14.5 KB
/
user_valid.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
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
<?php
require_once('db_fns.php');
function register($username, $password,$ip,$email,$name) {
$conn = db_connect();
$conn->autocommit(FALSE);
$result = $conn->query("select * from t_user where f_username='".$username."'");
if (!$result) {
return 1;
}
if ($result->num_rows>0) {
return 2;
}
$rs1 = $conn->query("insert into t_user (f_username, f_password,f_loginip, f_email,f_name) values
('".$username."', md5('".$password."'), '".$ip."', '".$email."', '".$name."')");
$rs2 = $conn->query("insert into t_postinfo (f_uname) values
('".$username."')");
if (!$rs1) {
return 3;
}
if($rs1 && $rs2){
$conn->commit();
$ret=0;
}else{
$conn->rollback();
$ret= 3;
}
$conn->close();
return $ret;
}
function login($username, $password,$type) {
$conn = db_connect();
$result = $conn->query("select * from t_user
where f_username='".$username."'
and f_password = md5('".$password."') ");
if (!$result) {
throw new Exception('Could not log you in.');
}
if ($result->num_rows>0) {
$rs=$result->fetch_object();
if($rs->f_tag==$type){
return true;
}else{
throw new Exception('请确认你的登入类型.');
}
} else {
throw new Exception('Could not log you in.');
}
}
function check_valid_user() {
// see if somebody is logged in and notify them if not
if (isset($_SESSION['valid_user'])) {
echo "Logged in as ".$_SESSION['valid_user'].".<br />";
} else {
// they are not logged in
do_html_heading('Problem:');
echo 'You are not logged in.<br />';
do_html_url('login.php', 'Login');
do_html_footer();
exit;
}
}
function change_password($username, $old_password, $new_password) {
// change password for username/old_password to new_password
// return true or false
// if the old password is right
// change their password to new_password and return true
// else throw an exception
login($username, $old_password);
$conn = db_connect();
$result = $conn->query("update user
set passwd = sha1('".$new_password."')
where username = '".$username."'");
if (!$result) {
throw new Exception('Password could not be changed.');
} else {
return true; // changed successfully
}
}
function get_random_word($min_length, $max_length) {
// grab a random word from dictionary between the two lengths
// and return it
// generate a random word
$word = '';
// remember to change this path to suit your system
$dictionary = '/usr/dict/words'; // the ispell dictionary
$fp = @fopen($dictionary, 'r');
if(!$fp) {
return false;
}
$size = filesize($dictionary);
// go to a random location in dictionary
$rand_location = rand(0, $size);
fseek($fp, $rand_location);
// get the next whole word of the right length in the file
while ((strlen($word) < $min_length) || (strlen($word)>$max_length) || (strstr($word, "'"))) {
if (feof($fp)) {
fseek($fp, 0); // if at end, go to start
}
$word = fgets($fp, 80); // skip first word as it could be partial
$word = fgets($fp, 80); // the potential password
}
$word = trim($word); // trim the trailing \n from fgets
return $word;
}
function reset_password($username) {
// set password for username to a random value
// return the new password or false on failure
// get a random dictionary word b/w 6 and 13 chars in length
$new_password = get_random_word(6, 13);
if($new_password == false) {
throw new Exception('Could not generate new password.');
}
// add a number between 0 and 999 to it
// to make it a slightly better password
$rand_number = rand(0, 999);
$new_password .= $rand_number;
// set user's password to this in database or return false
$conn = db_connect();
$result = $conn->query("update user
set passwd = sha1('".$new_password."')
where username = '".$username."'");
if (!$result) {
throw new Exception('Could not change password.'); // not changed
} else {
return $new_password; // changed successfully
}
}
function notify_password($username, $password) {
// notify the user that their password has been changed
$conn = db_connect();
$result = $conn->query("select email from user
where username='".$username."'");
if (!$result) {
throw new Exception('Could not find email address.');
} else if ($result->num_rows == 0) {
throw new Exception('Could not find email address.');
// username not in db
} else {
$row = $result->fetch_object();
$email = $row->email;
$from = "From: support@phpbookmark \r\n";
$mesg = "Your PHPBookmark password has been changed to ".$password."\r\n"
."Please change it next time you log in.\r\n";
if (mail($email, 'PHPBookmark login information', $mesg, $from)) {
return true;
} else {
throw new Exception('Could not send email.');
}
}
}
function getBoard(){
$m_boards = array();
$conn = db_connect();
$sql = "SELECT * FROM t_board";
$result = $conn->query($sql);
//获取行数
$num_results=$result->num_rows;
for ($i=0;$i<$num_results;$i++){
//获取每一行的记录
$m_boards[$i]=$result->fetch_assoc();
}
return $m_boards;
}
function getUser(){
$m_boards = array();
$conn = db_connect();
$sql = "SELECT * FROM t_user left outer join t_postinfo on t_user.f_username=t_postinfo.f_uname ";
$result = $conn->query($sql);
//获取行数
$num_results=$result->num_rows;
for ($i=0;$i<$num_results;$i++){
//获取每一行的记录
$m_boards[$i]=$result->fetch_object();
}
return $m_boards;
}
//获取用户发帖排行
function getPostRank(){
$m_boards = array();
$conn = db_connect();
$sql = "SELECT * FROM t_user right outer join t_postinfo on t_user.f_username=t_postinfo.f_uname order by f_post_times desc, f_reply_times desc limit 0, 20";
$result = $conn->query($sql);
//获取行数
$num_results=$result->num_rows;
for ($i=0;$i<$num_results;$i++){
//获取每一行的记录
$m_boards[$i]=$result->fetch_object();
}
return $m_boards;
}
//获取每一个版面的文章信息
function getArtical($f_id){
$Artical = array();
$conn=db_connect();
$sql="SELECT * from t_article where f_board_id= '".$f_id."' ";
$result=$conn->query($sql);
//获取行数
$num_results=$result->num_rows;
for ($i=0;$i<$num_results;$i++){
//获取每一行的记录
$Artical[$i]=$result->fetch_assoc();
}
return $Artical;
}
//获取每一篇文章的内容
function getAcontent($f_id){
$artical = array();
$review2=array();
$conn=db_connect();
$sql="SELECT * from t_article_content join t_article on t_article.f_id=t_article_content.f_id where t_article_content.f_id= '".$f_id."' ";
$result=$conn->query($sql);
//获取对象
$articalObj=$result->fetch_object();
if($articalObj->f_has_child){
$review1=getArticalInfo($articalObj->f_id);
$num=count($review1);
for($i=0;$i<$num;$i++){
//获取评论的子评论
if($review1[$i]->f_has_child){
$review2[$i]=getArticalInfo($review1[$i]->f_id);
}else{
$review2[$i]=array();
}
}
}
$artical[0]=$articalObj;
$artical[1]=isset($review1)?$review1:array();
$artical[2]=$review2;
return $artical;
}
//获取每一篇文章的内容
function getEnable($f_username){
$Artical = array();
$conn=db_connect();
$sql="SELECT * from t_postinfo where f_uname= '".$f_username."' ";
$result=$conn->query($sql);
//获取行数
$num_results=$result->num_rows;
for ($i=0;$i<$num_results;$i++){
//获取每一行的记录
$Artical[$i]=$result->fetch_assoc();
}
return $Artical;
}
//获取每一篇文章的内容
function getPost($f_parent_id, $f_title, $f_username, $f_board_id,$f_post_time, $f_ip,$content){
$Artical = array();
$conn=db_connect();
$conn->autocommit(FALSE);
$sql="INSERT into t_article (f_parent_id, f_title, f_username, f_board_id,f_post_time, f_ip) values";
$sql.="('".$f_parent_id."','".$f_title."','".$f_username."','".$f_board_id."','".$f_post_time."','".$f_ip."')";
$rs1=$conn->query($sql);
$ttt=mysqli_insert_id($conn);
$sq4="SELECT * from t_postinfo where f_uname='".$f_username."'";
$rs4=$conn->query($sq4);
if($rs4 && $rs4->num_rows){
$t=$rs4->fetch_object();
$post_reply=$f_parent_id ? $t->f_reply_times : $t->f_post_times;
}
$sq2="INSERT into t_article_content (f_id, f_content ) values (LAST_INSERT_ID(),'".$content."')";
$rs2=$conn->query($sq2);
if($f_parent_id){
$sq3="UPDATE t_article set f_has_child=1 where f_id='".$f_parent_id."'";
$rs3=$conn->query($sq3);
$sq4="UPDATE t_postinfo set f_reply_times=$post_reply+1 where f_uname='".$f_username."'";
$rs4=$conn->query($sq4);
}else{
$sq4="UPDATE t_postinfo set f_post_times=$post_reply+1 where f_uname='".$f_username."'";
$rs4=$conn->query($sq4);
}
if( ($rs1 && $rs2 && $f_parent_id && $rs3 && $rs4) || ($rs1 && $rs2 && !$f_parent_id && $rs4)){
$conn->commit();
$ret=$ttt;
}else{
$conn->rollback();
$ret=true;
}
$conn->close();
return $ret;
}
/*
function last_insert_id(){
$conn=db_connect();
$sql="SELECT * from t_article order by f_id desc limit 0,1";
$rs1=$conn->query($sql);
$num_rows=$rs1->fetch_object();
return $num_rows->f_id;
}
*/
//获取每一篇文章的详细信息,不包括内容
function getArticalInfo($f_id){
$Artical = array();
$conn=db_connect();
$sql="SELECT * from t_article_content join t_article on t_article.f_id=t_article_content.f_id where t_article.f_parent_id= '".$f_id."' ";
$result=$conn->query($sql);
$num_results=$result->num_rows;
for ($i=0;$i<$num_results;$i++){
//获取每一行的记录
$Artical[$i]=$result->fetch_object();
}
return $Artical;
}
//获取每一篇文章的详细信息,不包括内容
function getArticalInfo1($f_id){
$Artical = array();
$conn=db_connect();
$sql="SELECT * from t_article_content join t_article on t_article.f_id=t_article_content.f_id where t_article.f_parent_id= '".$f_id."' ";
$result=$conn->query($sql);
//获取行数
$num_results=$result->num_rows;
for ($i=0;$i<$num_results;$i++){
//获取每一行的记录
$Artical[$i]=$result->fetch_object();
}
return $Artical;
}
//增加一个版面
function addBoard($f_name,$f_desc ){
$f_post_time= date('y-m-d h:i:s',time());
$conn=db_connect();
$sql="INSERT into t_board (f_name,f_desc,f_created_time) values ( '".$f_name."', '".$f_desc."', '".$f_post_time."')";
$result=$conn->query($sql);
//获取行数
if(!$result){
return flase;
}
return true;
}
//屏蔽一个版面
function pingBoard($ping ){
$sql=array();$result=array();
$conn=db_connect();
$conn->autocommit(FALSE);
for($i=0;$i<count($ping);$i++){
$sql[$i]="UPDATE t_board set f_enabled=0 where f_id = '".$ping[$i]."'";
$result[$i]=$conn->query($sql[$i]);
}
for($i=0;$i<count($ping);$i++){
if(!$result[$i]>0){
$conn->rollback();
break;
return true;
}
}
$conn->commit();
return true;
}
//取消屏蔽一个版面
function cancleBoard($ping ){
$sql=array();$result=array();
$conn=db_connect();
$conn->autocommit(FALSE);
for($i=0;$i<count($ping);$i++){
$sql[$i]="UPDATE t_board set f_enabled=1 where f_id = '".$ping[$i]."'";
$result[$i]=$conn->query($sql[$i]);
}
for($i=0;$i<count($ping);$i++){
if(!$result[$i]>0){
$conn->rollback();
break;
return true;
}
}
$conn->commit();
return true;
}
//屏蔽一个版面
function pingArtical($ping ){
$sql=array();$result=array();
$conn=db_connect();
$conn->autocommit(FALSE);
for($i=0;$i<count($ping);$i++){
$sql[$i]="UPDATE t_article set f_enabled=0 where f_id = '".$ping[$i]."'";
$result[$i]=$conn->query($sql[$i]);
if(!$result[$i]){
$conn->rollback();
break;
return "1";
}
}
$conn->commit();
$conn->close();
return "123";
}
//取消屏蔽一个版面
function cancleReview($ping ){
$conn=db_connect();
$sql="UPDATE t_article set f_enabled=1 where f_id = '".$ping[0]."'";
$result=$conn->query($sql);
$sq2="SELECT * from t_article_content where f_id = '".$ping[0]."'";
$result1=$conn->query($sq2);
if($result && $result1 ){
return $result1->fetch_object();
}
return flase;
}
//取消屏蔽一个版面
function cancleArtical($ping ){
$sql=array();$result=array();
$conn=db_connect();
$conn->autocommit(FALSE);
for($i=0;$i<count($ping);$i++){
$sql[$i]="UPDATE t_article set f_enabled=1 where f_id = '".$ping[$i]."'";
$result[$i]=$conn->query($sql[$i]);
}
for($i=0;$i<count($ping);$i++){
if(!$result[$i]>0){
$conn->rollback();
break;
return true;
}
}
$conn->commit();
return true;
}
//屏蔽一个用户
function pingUser($ping ){
$sql=array();$result=array();
$conn=db_connect();
$conn->autocommit(FALSE);
for($i=0;$i<count($ping);$i++){
$sql[$i]="UPDATE t_postinfo set f_enabled=0 where f_uname = '".$ping[$i]."'";
$result[$i]=$conn->query($sql[$i]);
}
for($i=0;$i<count($ping);$i++){
if(!$result[$i]>0){
$conn->rollback();
break;
return true;
}
}
$conn->commit();
return true;
}
//取消用户屏蔽
function cancleUser($ping){
$sql=array();$result=array();
$conn=db_connect();
$conn->autocommit(FALSE);
for($i=0;$i<count($ping);$i++){
$sql[$i]="UPDATE t_postinfo set f_enabled=1 where f_uname = '".$ping[$i]."'";
$result[$i]=$conn->query($sql[$i]);
}
for($i=0;$i<count($ping);$i++){
if(!$result[$i]>0){
$conn->rollback();
break;
return true;
}
}
$conn->commit();
return true;
}
?>