/
Database.php
352 lines (321 loc) · 7.44 KB
/
Database.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
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
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
<?php
require ("rb.php");
require ("papercut.php");
R::setup("sqlite:db/database.db");
$ini = parse_ini_file('config.ini');
define('threadCount', $ini['threadCount']);
$global_filename = '';
$global_filepath = '';
class DB
{
public static function Zip($source, $destination)
{
if(!extension_loaded('zip') || !file_exists($source))
{
return false;
}
$zip = new ZipArchive();
if(!$zip->open($destination, ZIPARCHIVE::CREATE))
{
return false;
}
$source = str_replace('\\', '/', realpath($source));
if(is_dir($source) === true)
{
$files = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($source), RecursiveIteratorIterator::SELF_FIRST);
foreach($files as $file)
{
$file = str_replace('\\', '/', $file);
if(in_array(substr($file, strrpos($file, '/') + 1), array('.', '..')))
{
continue;
}
$file = realpath($file);
if(is_dir($file) === true)
{
$zip->addEmptyDir(str_replace($source . '/', '', $file . '/'));
}
elseif(is_file($file) === true)
{
$zip->addFromString(str_replace($source . '/', '', $file), file_get_contents($file));
}
}
}
elseif(is_file($source) === true)
{
$zip->addFromString(basename($source), file_get_contents($source));
}
return $zip->close();
}
public static function AddJob($filename)
{
$w = R::dispense('job');
$w->name = $filename;
$t = time();
$w->timestamp = $t;
$w->status = R::enum('status:Queued');
$w->progress = 0;
$w->is_zipped = 0;
$id = R::store( $w );
$bean = R::load('job', $id);
$bean->filename = $filename;
http_response_code(200);
print $bean;
}
public static function ListJobs($app)
{
$bool = DB::refreshJobs();
if ($bool[0])
{
$app->response()->status(200);
print json_encode($bool[1]);
}
else
{
$app->response()->status(400);
$error = "No jobs in your table";
print $error;
}
}
public static function StartJob($id, $app)
{
$job = R::load('job', $id);
$path = 'uploads/' . $job['timestamp'] . '/' . $job['name'];
$newPath = substr($path, 0, -3) . 'stop';
if ($job->id !== 0)
{
if (file_exists($newPath))
{
if (unlink($newPath))
{
$oldContent = file_get_contents($path);
$file = fopen($path, 'w');
if( strpos($oldContent, "&MISC RESTART=.TRUE. /") === false)
{
rewind($file);
if (fwrite($file, "&MISC RESTART=.TRUE. /\n"))
{
fwrite($file, $oldContent);
fclose($file);
$app->response()->status(200);
$success = "Job ready for restart.";
print $success;
}
else
{
$app->response()->status(400);
$error = "The .fds file could not be edited.";
print $error;
}
}
else
{
$app->response()->status(200);
$success = "Job ready for restart.";
print $success;
}
}
else
{
$app->response()->status(400);
$error = "The .stop file could not be deleted.";
print $error;
}
}
else
{
$app->response()->status(400);
$error = "The job was never stopped (paused).";
print $error;
}
}
else
{
$app->response()->status(400);
$error = "The job with ID " . $id . " does not exist.";
print $error;
}
}
public static function FindJob($id)
{
$job = R::load('job', $id);
print_r(json_decode($job));
}
public static function GetJob($id)
{
$job = R::load('job', $id);
return $job;
}
public static function DeleteJob($id, $app)
{
$job = R::load('job', $id);
$filename = $job['name'];
$timestamp = $job['timestamp'];
//$bool = DB::deleteDir("uploads\\" . $timestamp);
// if(!$bool)
// {
// $app->response()->status(400);
// $error = "File {$filename} could not be deleted or was open.";
// return json_encode($error);
// }
$result = R::trash('job', $id);
if ($result)
{
$app->response()->status(200);
$response = "Job {$id} has been deleted.";
return json_encode($response);
}
else
{
$app->response()->status(400);
$error = "Job {$id} could not be deleted or doesn't exist.";
return json_encode($error);
}
}
/*
public static function deleteDir($dirPath)
{
if (!is_dir($dirPath))
{
//throw new InvalidArgumentException("$dirPath must be a directory");
return false;
}
if (substr($dirPath, strlen($dirPath) - 1, 1) != '/')
{
$dirPath .= '/';
}
$files = glob($dirPath . '*', GLOB_MARK);
foreach ($files as $file)
{
if (is_dir($file))
{
self::deleteDir($file);
}
else
{
unlink($file);
}
}
rmdir($dirPath);
return true;
}*/
public static function UpdateStatus($id, $percentage)
{
$job = R::load('job', $id);
if ($percentage >= 100)
{
$percentage = 100;
$job->status = R::enum('status:Completed');
//DB::zip_file($id);
}
else if ($percentage < 100 && $percentage > 0)
{
if ($job->status != R::enum('status:Stopped')->id)
{
$job->status = R::enum('status:In Progress');
}
}
$job->progress = $percentage;
R::store($job);
}
public static function zip_file($id)
{
set_time_limit(0);
$job = DB::GetJob($id);
if($job->is_zipped === 0)
{
if(!is_writeable("uploads/" . $timestamp)){
die("Cannot write in the uploads directory.");
}
if(!file_exists('archives'))
{
mkdir('archives', 0777, true);
}
if(!file_exists('archives/' . $timestamp))
{
mkdir('archives/' . $timestamp, 0777, true);
}
DB::Zip('uploads/' . $timestamp, 'archives/' . $timestamp . '/' . $name . '.zip');
$job->is_zipped = 1;
}
}
//debug function
public static function MakeJob($filename)
{
$w = R::dispense('job');
$w->name = $filename;
$t = time();
$w->timestamp = $t;
$w->progress = $percentage;
$w->status = R::enum('status:Queued');
$id = R::store($w);
}
public static function refreshJobs()
{
$jobs = R::find('job');
if (!count($jobs))
{
return array(false);
}
$beans = R::exportAll($jobs);
foreach ($beans as $bean)
{
$percentage = pc::getTime($bean["timestamp"] . '/' . substr($bean["name"], 0, strrpos($bean["name"], '.')) . ".out", $bean["id"]);
if ($percentage != null)
{
DB::UpdateStatus($bean["id"], round($percentage, 2));
}
else
{
DB::UpdateStatus($bean["id"], 0);
}
}
return array(true, $beans);
}
public static function queryFiles()
{
$toRunJobs = array();
//refresh jobs
$result = DB::refreshJobs();
//check result for true return or false return
//find out how many are running currently
$inProgress = R::find('job', ' status_id = ? ', [R::enum('status:In Progress')->id]);
$count = count($inProgress);
if ($count < threadCount)
{
//query for next jobs
$jobs = R::find('job', ' status_id = ? ', [R::enum('status:Queued')->id]);
//forech for each job queued and add them to an array of jobs and add 1 to count
foreach ($jobs as $job)
{
if ($count < threadCount)
{
$path = $job["timestamp"] . '/' . $job["name"];
array_push($toRunJobs, $path);
$count++;
}
else
{
break;
}
}
return $toRunJobs;
}
else
{
return null;
}
}
public static function StopJob($id, $app)
{
$job = DB::GetJob($id);
$filename = $job['name'];
$timestamp = $job['timestamp'];
$path = "uploads/" .$timestamp . '/' . substr($filename, 0, strlen($filename)-4);
$myfile = fopen($path .".stop", "w");
$job->status = R::enum('status:Stopped');
R::store($job);
$app->response(200);
}
}
?>