-
Notifications
You must be signed in to change notification settings - Fork 0
/
handle_admin3.php
84 lines (78 loc) · 2.04 KB
/
handle_admin3.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
<?php
session_start();
require "vendor/autoload.php";
$lifetime = 10;
session_set_cookie_params($lifetime);
function handle_admin()
{
if(empty($_POST['admin_account'])==false && empty($_POST['admin_password'])==false)
{
$account = $_POST['admin_account'];
$password = md5($_POST['admin_password']);
//password: ccc95
$file_path = "../sqlite/books_web.s3db";
if(file_exists($file_path))
{
$link = new SQLite3($file_path);
$sql_cmd = "SELECT password FROM root_account WHERE password='$password'";
$result = $link -> query($sql_cmd);
$i = 0;
$row = array();
while($res = $result -> fetchArray(SQLITE3_ASSOC))
{
$row[$i]['password'] = $res['password'];
$i++;
}
if(count($row)!=0)
{
$_SESSION['root'] = "root";
return "admin login success.";
}
else
{
return "admin login failed.";
}
$link -> close();
}
else
{
return "cannot link database.";
}
}
else
{
return "post error";
}
}
class handleAdminTest3 extends PHPUnit_Framework_TestCase
{
/** @test */
public function adminTest()
{
//[1,2]
$res = handle_admin();
$this -> assertEquals("post error", $res);
//[1,3,4]
//sqlite database file was not existed.
$_POST['admin_account'] = "text-account";
$_POST['admin_password'] = "text-pass";
copy("../sqlite/books_web.s3db", "./books_web.s3db");
@unlink("../sqlite/books_web.s3db");
$res = handle_admin();
$this -> assertEquals("cannot link database.", $res);
//[1,3,5,7]
$_POST['admin_account'] = "text-account";
$_POST['admin_password'] = "text-pass";
copy("./books_web.s3db", "../sqlite/books_web.s3db");
//sqlite database file already has existed.
$res = handle_admin();
$this -> assertEquals("admin login failed.", $res);
//[1,3,5,6]
copy("./books_web.s3db", "../sqlite/books_web.s3db");
$_POST['admin_account'] = "text-account";
$_POST['admin_password'] = "ccc95";
$res = handle_admin();
$this -> assertEquals("admin login success.", $res);
}
}
?>