forked from ksen007/KMS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
readwrite2.php
172 lines (159 loc) · 5.54 KB
/
readwrite2.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
#!/usr/bin/php-cgi
<?php
header('Access-Control-Allow-Origin: *');
header('Cache-Control: no-cache, must-revalidate');
#header('Expires: Mon, 01 Jan 1996 00:00:00 GMT');
header('Content-type: text/plain');
?>
<?php
require('password.php');
function matchPassword($file, $password) {
$content = file_get_contents($file);
if (preg_match("/<!--PASSWORD\(([^\)]*)\)-->/", $content, $matches)){
return password_verify($password, $matches[1]);
} else {
return FALSE;
}
}
function startsWith($haystack, $needle) {
// search backwards starting from haystack length characters from the end
return $needle === "" || strrpos($haystack, $needle, -strlen($haystack)) !== FALSE;
}
$salt = 'lr';
$cpassword = 'your crypted password';
if (array_key_exists('action',$_POST) && $_POST['action'] == 'read' ){
if (array_key_exists('file', $_POST) && file_exists($_POST['file'])) {
if ((!(stripos($_POST['file'],'../') || !startsWith($_POST['file'],'hpage/'))
|| (array_key_exists('password',$_POST) && crypt($_POST['password'],$salt) == $cpassword))) {
$content = file_get_contents($_POST['file']);
if ($content) {
$success = TRUE;
$message = 'File found: '.$_POST['file'];
} else {
$content= FALSE;
$success = FALSE;
$message = 'Unable to read file: '.$_POST['file'];
}
} else {
$content = FALSE;
$success = FALSE;
if (!array_key_exists('password',$_POST)) {
$message = 'Access denied to file due to non-existent password: '.$_POST['file'];
} else {
$message = 'Access denied to file due to wrong password: '.$_POST['file'];
}
}
} else {
$content = FALSE;
$success = FALSE;
$message = 'File not found: '.$_POST['file'];
}
} else if (array_key_exists('action',$_POST) && $_POST['action'] == 'write') {
if(array_key_exists('password',$_POST) && array_key_exists('oldFile',$_POST) && array_key_exists('newPassword',$_POST) && array_key_exists('file', $_POST)){
$file = $_POST['file'];
if ((!file_exists($file) && matchPassword($_POST['oldFile'], $_POST['password'])) || (file_exists($file) && matchPassword($file, $_POST['password']))) {
if (array_key_exists('content',$_POST)) {
if (!file_exists(dirname($file))){
$success = mkdir(dirname($file), 0700, TRUE);
}
$success = file_put_contents($file, $_POST['content']."<!--PASSWORD(".password_hash($_POST['newPassword'],PASSWORD_DEFAULT).")-->");
if ($success) {
$content = '';
$success = TRUE;
$message = 'File modified: '.$_POST['file'];
} else {
$content = FALSE;
$success = FALSE;
$message = 'Cannot modify file: '.$_POST['file'];
}
} else {
$content = FALSE;
$success = FALSE;
$message = 'content field missing in request.';
}
} else {
$content = FALSE;
$success = FALSE;
if (file_exists($file)) {
$message = 'Overwrite failed. Access denied to file due to wrong password in existing file : '.$_POST['file'];
} else {
$message = 'Access denied to file due to wrong password in old file : '.$_POST['oldFile'];
}
}
} else {
$content = FALSE;
$success = FALSE;
$message = 'password, newPassword, file, or oldFile field is non-existent: '.$_POST['file'];
}
} else if (array_key_exists('action',$_POST) && $_POST['action'] == 'remove') {
if (array_key_exists('password',$_POST) && crypt($_POST['password'],$salt) == $cpassword) {
if (array_key_exists('file', $_POST)) {
$resp = unlink($_POST['file']);
if ($resp) {
$content = '';
$success = TRUE;
$message = 'Deleted file: '.$_POST['file'];
} else {
$content = FALSE;
$success = FALSE;
$message = 'Failed to delete file: '.$_POST['file'];
}
} else {
$content = FALSE;
$success = FALSE;
$message = 'file field missing in request.';
}
} else {
$content = FALSE;
$success = FALSE;
if (!array_key_exists('password',$_POST)) {
$message = 'File deletion failed due to non-existent password: '.$_POST['file'];
} else {
$message = 'File deletion failed due to wrong password: '.$_POST['file'];
}
}
} else if (array_key_exists('action',$_POST) && $_POST['action'] == 'upload') {
if (array_key_exists('password',$_POST) && crypt($_POST['password'],$salt) == $cpassword) {
if (array_key_exists('directory',$_POST) && $_POST['directory'] != ''){
$target = $_POST['directory'];
} else {
$target = ".";
}
if (!file_exists($target)){
mkdir($target, 0700, TRUE);
}
$target = $target .'/'. basename( $_FILES['uploaded']['name']) ;
if(move_uploaded_file($_FILES['uploaded']['tmp_name'], $target)) {
$content = '';
$success = TRUE;
$message = 'Uploaded file: '.$_FILES['uploaded']['name'];
} else {
$content = FALSE;
$success = FALSE;
$message = 'Failed to upload file: '.$_FILES['uploaded']['name'];
}
} else {
$content = FALSE;
$success = FALSE;
if (!array_key_exists('password',$_POST)) {
$message = 'File upload failed due to non-existent password: '.$_FILES['uploaded']['name'];
} else {
$message = 'File upload failed due to wrong password: '.$_FILES['uploaded']['name'];
}
}
} else {
$content = FALSE;
$success = FALSE;
if (array_key_exists('action', $_POST)) {
$message = "Unknown action ".$_POST['action'];
} else {
$message = "action not specified.";
}
}
$advert = array(
'data' => $content,
'message' => $message,
'success' => $success
);
echo json_encode($advert);
?>