Repository navigation
Expand file tree
/
Copy pathrosdash.php
More file actions
326 lines (325 loc) · 7.34 KB
/
Copy pathrosdash.php
File metadata and controls
326 lines (325 loc) · 7.34 KB
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
<?php
require __DIR__.'/lib/predis/autoload.php';
$HOST = "localhost";
$PORT = "6379";
function saveFile ()
{
//@todo must test if the directory exists
$file = $_POST['file_name'];
$json = json_encode($_POST['data']);
echo file_put_contents($file, $json);
}
function getOwnerList ()
{
$dirs = glob('./file/*', GLOB_ONLYDIR);
foreach ($dirs as $i)
{
if (strlen($i) > 7)
{
echo substr($i, 7).",";
}
}
}
function getPanelList ()
{
// Define the full path to your folder from root
$path = './file/'.$_POST["user"];
// Open the folder
$dir_handle = @opendir($path) or die("Unable to open $path");
// Loop through the files
while ($file = readdir($dir_handle))
{
if($file == "." || $file == "..")
{
continue;
}
echo $file." ";
/*$pos = strpos($file, "-");
if ($pos && substr($file, $pos) == "-panel.json")
{
echo substr($file, 0, $pos)." ";
}*/
}
// Close
closedir($dir_handle);
}
function newUser ()
{
$user = $_POST['username'];
if ("" == $user || " " == $user)
{
echo "user name error: ".$user;
return false;
}
if (file_exists("file/".$user."/"))
{
echo "user dir exists " . $user;
return false;
}
// create user dir
$old_mask = umask(0);
if (! mkdir("file/".$user, 0777))
{
echo "unable to make dir: ".$user;
return false;
}
umask($old_mask);
// create user conf
$conf = file_get_contents("file/index/conf.json");
if (! $conf)
{
echo "unable to get default conf";
return false;
}
$json = json_decode($conf, true);
$json["user"] = $user;
$json["discrip"] = "";
// save user conf to database
$con = mysqli_connect("localhost", "root", "123456", "rosdash");
if (mysqli_connect_errno($con))
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
} else
{
mysqli_query($con,'INSERT INTO users (Name, Openid) VALUES ("' . $user . '", "'. $conf .'")');
}
mysqli_close($con);
// save user conf to file
if (! file_put_contents('file/'.$user.'/conf.json', json_encode($json)))
{
echo "unable to create user conf file";
return false;
}
if (! chmod('file/'.$user.'/conf.json', 0777))
{
echo "unable to chmod user conf file";
return false;
}
// create index panel
$conf = file_get_contents("file/index/new-panel.json");
if (! $conf)
{
echo "unable to get default index panel page";
return false;
}
$json = json_decode($conf, true);
$json["user"] = $user;
$json["discrip"] = "";
$json["panel_name"] = "index";
if (! file_put_contents('file/'.$user.'/index-panel.json', json_encode($json)))
{
echo "unable to create index panel file";
return false;
}
if (! chmod('file/'.$user.'/index-panel.json', 0777))
{
echo "unable to chmod index panel file";
return false;
}
// create index diagram
$conf = file_get_contents("file/index/new-diagram.json");
if (! $conf)
{
echo "unable to get default index diagram page";
return false;
}
$json = json_decode($conf, true);
$json["user"] = $user;
$json["discrip"] = "";
$json["panel_name"] = "index";
if (! file_put_contents('file/'.$user.'/index-diagram.json', json_encode($json)))
{
echo "unable to create index diagram file";
return false;
}
if (! chmod('file/'.$user.'/index-diagram.json', 0777))
{
echo "unable to chmod index diagram file";
return false;
}
return true;
}
function newPanel ()
{
$user = $_POST['username'];
if ("" == $user || " " == $user)
{
echo "user name error: ".$user;
return false;
}
$panel = $_POST['panel'];
if ("" == $panel || " " == $panel)
{
echo "panel name error: ".$panel;
return false;
}
// create panel
$conf = file_get_contents("file/index/empty-panel.json");
if (! $conf)
{
echo "unable to get empty panel page";
return false;
}
$json = json_decode($conf, true);
$json["user"] = $user;
$json["discrip"] = "";
$json["panel_name"] = $panel;
if (! file_put_contents('file/'.$user.'/'.$panel.'-panel.json', json_encode($json)))
{
echo "unable to create panel file";
return false;
}
if (! chmod('file/'.$user.'/'.$panel.'-panel.json', 0777))
{
echo "unable to chmod panel file";
return false;
}
// create index diagram
$conf = file_get_contents("file/index/empty-diagram.json");
if (! $conf)
{
echo "unable to get empty diagram page";
return false;
}
$json = json_decode($conf, true);
$json["user"] = $user;
$json["discrip"] = "";
$json["panel_name"] = $panel;
if (! file_put_contents('file/'.$user.'/'.$panel.'-diagram.json', json_encode($json)))
{
echo "unable to create diagram file";
return false;
}
if (! chmod('file/'.$user.'/'.$panel.'-diagram.json', 0777))
{
echo "unable to chmod diagram file";
return false;
}
return true;
}
function redisConnect ($host, $port)
{
$server = array(
'host' => is_null($_GET["host"]) ? $host : $_GET["host"],
'port' => is_null($_GET["port"]) ? $port : $_GET["port"]
);
$client = new Predis\Client($server);
try {
$client->connect();
} catch (Exception $e) {
exit($server . " error: " . $e);
}
if (is_null($client) || is_null($client->ping()))
{
echo "cannot reach server" . $host . ":" . $port;
return;
}
return $client;
}
function redisStatus ()
{
global $HOST, $PORT;
$client = redisConnect($HOST, $PORT);
if (is_null($client))
{
return;
}
//echo "dbsize ".$client->dbsize().", lastsave ".$client->lastsave().", ";
$info = $client->info();
echo json_encode($info);
}
function redisSet ()
{
global $HOST, $PORT;
$client = redisConnect($HOST, $PORT);
if (is_null($client))
{
return;
}
$client->set($_POST["key"], $_POST["value"]);
}
function redisGet ()
{
global $HOST, $PORT;
$client = redisConnect($HOST, $PORT);
if (is_null($client))
{
return;
}
echo $client->get($_POST["key"]);
}
function redisBackup ()
{
global $HOST, $PORT;
$client = redisConnect($HOST, $PORT);
if (is_null($client))
{
return;
}
$key = $_POST["key"];
$value = $_POST["value"];
$time = round(microtime(true) * 1000);
$client->zadd($key, $time, $data);
}
function redisGetBackup ()
{
global $HOST, $PORT;
$client = redisConnect($HOST, $PORT);
if (is_null($client))
{
return;
}
$key = $_POST["key"];
$value = $_POST["value"];
$from = strtotime($_POST["from"]) * 1000;
// if duration is not valid, use "to"
if ($_POST["duration"] == "" || $_POST["duration"] == " " || $_POST["duration"] == "0")
{
$to = strtotime($_POST["to"]) * 1000;
} else
{
$to = $from + intval($_POST["duration"]) * 1000;
}
// get data ranging from "from" to "to"
echo json_encode($client->zrangebyscore($key, $from, $to));
}
// set old data to the place where current data exist
function redisTimeTravel()
{
global $HOST, $PORT;
$client = redisConnect($HOST, $PORT);
if (is_null($client))
{
return;
}
if ($_POST["timestamp"] == "" || $_POST["timestamp"] == " " || $_POST["timestamp"] == "undefined")
{
return;
}
$timestamp = strtotime($_POST["timestamp"]) * 1000;
$interval = floatval($_POST["interval"]) * 1000;
$key = $_POST["key"];
/*$tmp = $client->zrangebyscore($key, $nexttime, $nexttime);
$tmp = $tmp[0];
$index = $client->zrank($key, $tmp);
$tmp = $client->zrange($key, $index + 1, $index + 1);
$tmp = $client->zscore($key, $tmp[0]);
if ($tmp >= $timestamp && $tmp < $next)
{
$next = $tmp;
}
echo ($next / 1000.0)." ".($next - $nexttime);*/
}
// call corresponding method according to $method
function callMethod ($func)
{
if(is_null($func) || "" == $func || ! function_exists($func))
{
echo "invalid func: ".$func;
return;
}
$func();
}
//call method specified by javascript
callMethod($_POST["func"]);
?>