-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.php
188 lines (171 loc) · 5.04 KB
/
test.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
<?php
require_once 'file.class.php';
/*
$path = '/Applications/XAMPP/htdocs';
$file = scandir($path);
$files = array();
$dotPattern = '/^(\.){1,2}$/i';
//questionable
//$suffixPattern = '/((?<=\.)[A-Za-z0-9_]+)$/i';
//$ptn = '/^(?:[^\.]+)(?:.*)(?<=\.)([a-zA-z0-9_]+)$/i';
$ptn2 = '/(?:^[^\.].*(?<=\.)([a-zA-z0-9_]+)$)|(?:^\..+(?<=\.)([a-zA-Z0-9_]+)$)/i';
foreach ($file as $val) {
if(!preg_match($dotPattern, $val)) {
$fullName = $path.'/'.$val;
$f = new file();
$f->name = $val;
$f->fullName = $fullName;
$f->type = filetype($fullName);
$f->fileSize = filesize($fullName);
$f->accessTime = fileatime($fullName);
$f->modifyTime = filemtime($fullName);
$matches = array();
preg_match($ptn2, $val, $matches);
$f->suffix = @$matches[count($matches) - 1];
unset($matches);
$files[] = $f;
}
}
$file = testFiltrateSuffix($files, 'PHP');
echo '<pre>';
print_r($file);
echo '</pre>';
$ptn = '/^\S*htdocs(\S*)$/i';
$str = '/Applications/XAMPP/htdocs/MySite';
$str1 = 'hah.x';
$matches = array();
$res = preg_match($ptn, $str,$matches);
echo '<pre>';
print_r($matches);
echo '</pre>';
*/
/*
testFileDownload('test.php', '/Applications/XAMPP/htdocs/FileViewer');
function testFileDownload($fileName,$filePath) {
$fullPath = $filePath.'/'.$fileName;
if(file_exists($fullPath)) {
$file = fopen($fullPath, 'r') or die('File does not exit');
$size = filesize($fullPath);
$buff = '';
$readLength = 0;
header("Content-type: application/octet-stream");
header("Accept-ranges: bytes");
header("Accept-Length: $size");
header("Content-Disposition: attachment; filename=".$fileName);
while(!feof($file) && $readLength <= $size) {
echo $buff = fread($file, 1024);
$readLength+=1024;
}
} else {
return;
}
}
*/
/*
function fileUpload($uploadedPath) {
$fileElementName = 'dialog_upload_file';
if(!empty($_FILES[$fileElementName]['error'])) {
//Uploaded error!
} else if(empty($_FILES[$fileElementName]['tmp_name'])) {
//No file has been uploaded!
} else {
move_uploaded_file($_FILES[$fileElementName]['tmp_name'], $uploadedPath);
}
}
*/
/*
function fileUpload($uploadedPath) {
$error = "";
$msg = "";
$fileElementName = 'dialog_upload_file';
if(!empty($_FILES[$fileElementName]['error'])) {
switch($_FILES[$fileElementName]['error']) {
case '1':
$error = 'The uploaded file exceeds the upload_max_filesize directive in php.ini';
break;
case '2':
$error = 'The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form';
break;
case '3':
$error = 'The uploaded file was only partially uploaded';
break;
case '4':
$error = 'No file was uploaded.';
break;
case '6':
$error = 'Missing a temporary folder';
break;
case '7':
$error = 'Failed to write file to disk';
break;
case '8':
$error = 'File upload stopped by extension';
break;
case '999':
default:
$error = 'No error code avaliable';
}
} else if(empty($_FILES[$fileElementName]['tmp_name']) || $_FILES[$fileElementName]['tmp_name'] == 'none') {
$error = 'No file was uploaded..';
} else {
$msg .= " File Name: " . $_FILES[$fileElementName]['name'] . ", ";
$msg .= " File Size: " . @filesize($_FILES[$fileElementName]['tmp_name']);
move_uploaded_file($_FILES[$fileElementName]['tmp_name'], $uploadedPath.'/'.$_FILES[$fileElementName]['name']);
//for security reason, we force to remove all uploaded file
@unlink($_FILES[$fileElementName]);
}
echo "{";
echo "error: '" . $error . "',\n";
echo "msg: '" . $msg . "'\n";
echo "}";
}
*/
$arr = array();
$arr['username'] = 'Paul';
$arr['password'] = '123456';
$path = '/Applications/XAMPP/xamppfiles/htdocs/testUpload';
$fileName = 'testIni.txt';
function saveToIni($arr,$fileName,$path) {
$content = '';
foreach($arr as $key=>$val) {
$content .= $key.'='.$val."\r\n";
}
$fullName = $path.'/'.$fileName;
file_put_contents($fullName, $content);
}
function changePassword($password) {
$filePath = $_SERVER['DOCUMENT_ROOT'].'/'.'FileViewer/config.ini';
$content = parse_ini_file($filePath);
if(isset($password) && '' != $password) {
$content['password'] = md5($password);
saveToIni($content, 'config.ini', $_SERVER['DOCUMENT_ROOT'].'/'.'FileViewer');
} else {
echo 'Password incorrect';
}
}
// $file = parse_ini_file('/Applications/XAMPP/xamppfiles/htdocs/testUpload/testIni.txt');
// echo '<pre>';
// print_r($file);
// echo '</pre>';
// changePassword('123456');
// verify('123456');
function verify($password = '') {
$filePath = $_SERVER['DOCUMENT_ROOT'].'/'.'FileViewer/config.ini';
$content = parse_ini_file($filePath);
if(!isset($password) || '' == $password) {
echo 'No password entered';
} else if(md5($password) == $content['password']) {
//Todo
echo 'Password correct';
} else {
echo 'Password incorrect';
}
}
function setTokenInClient() {
setcookie('token',generateToken());
}
function generateToken() {
return md5(time());
}
echo generateToken();
?>