/
index.php
113 lines (81 loc) · 1.93 KB
/
index.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
<?php
/**
* @author hehanlin
* @time 2015-7-24
*/
session_start();
define('DATA_FILE',__DIR__.'/data/data.txt');
if(!file_exists(DATA_FILE))exit('data.txt is not exists!');
class Controller{
public function index()
{
require_once __DIR__.'/static/home.html';
}
public function getList()
{
echo $this->readAllFile();
}
public function create()
{
$content = isset($_POST['data']) ? $_POST['data'] : '';
echo $this->appendToFile($content) ? '1' : '0';
}
public function compareCode()
{
$code = isset($_GET['code']) ? trim($_GET['code']) : '';
if(strtolower($code)==strtolower($_SESSION['code']))
{
echo "1";exit;
}else{
echo "0";exit;
}
}
public function getLineNum()
{
$handle = $this->openFile('r');
$count = 0;
while(fgets($handle))
{
$count++;
}
fclose($handle);
echo $count;exit;
}
private function openFile($openType)
{
if(!$handle=fopen(DATA_FILE,$openType))exit('open file failed');
return $handle;
}
private function readAllFile()
{
$handle = $this->openFile('r');
echo fread($handle,filesize(DATA_FILE));
fclose($handle);
}
private function appendToFile($content)
{
$handle = $this->openFile('a');
if(!fwrite($handle,$content))exit('write file failed');
fclose($handle);
return true;
}
}
$obj = new Controller();
$method = isset($_GET['m']) ? trim($_GET['m']) : '';
switch($method)
{
case 'getList':
$obj->getList();
break;
case '':
$obj->index();
break;
case 'create':
$obj->create();
break;
case 'getLineNum':
$obj->getLineNum();
case 'compareCode':
$obj->compareCode();
}
exit;