2
class DOWNLOADMANAGER {
10
function __construct() {
14
if($_GET["target"] == "dlmanager_add"){
15
$this->req = new DATARequest();
16
$this->props = $this->req->GetProps();
17
if($this->props['db_server'] != 'virtual') $this->props['srctree'] = ""; //<- This because if user selects virtual server and some channels and then
18
} //switches to single server the srctree= attribute doesnt clear
19
$this->cache = new CACHEDB(); //and it messes download details channel listing.
20
$this->downloads_path = $TMP_PATH."/downloads";
21
$this->images_path = $TMP_PATH."/downloads/images";
22
$this->CheckAndCreateDirectories();
25
function CheckAndCreateDirectories(){
26
if (!is_dir($this->downloads_path)) {
27
if (@mkdir($this->downloads_path, 0777, true)){
28
@chmod($this->downloads_path, 0777);
31
if (!is_dir($this->images_path)) {
32
if (@mkdir($this->images_path, 0777, true)){
33
@chmod($this->images_path, 0777);
38
public function DlmanagerUpdate($target, $progress = NULL, $download = NULL) {
41
$this->UpdateProgress($progress, $download);
42
if(mysql_fetch_array($this->cache->GetDownloads($download)) == "") $ret = false;
47
$download = $this->GetDownload();
48
$this->UpdateProgress(1, $download);
52
$filesremaining = $this->GetFilesRemaining($download);
53
if($filesremaining == 0 || $filesremaining == 1) {
54
if($filesremaining == 1) $this->UpdateFilesRemaining($download, 0);
55
$this->UpdateProgress(100, $download);
56
$this->ChangeStatus($download, "Finalizing");
59
$this->UpdateFilesRemaining($download, $filesremaining - 1);
60
$this->UpdateProgress(1, $download);
66
function GetDownload() {
67
$res = $this->cache->GetDownloads("","ASC");
68
while($row = mysql_fetch_array($res)) {
69
if($row['status'] == "Preparing") {
76
function AddDownload() {
78
if(!is_numeric($this->props['db_group'])) $db_group = " - " .ucfirst($this->props['db_group']);
80
$name = ucfirst("{$this->props['db_server']} - ").ucfirst("{$this->props['db_name']}") .$db_group;
81
$download = md5(uniqid (rand(), true));
82
$reader = $this->req->CreateReader();
83
//$this->Logit(var_export($reader,true));
84
$iv = $reader->CreateInterval();
85
$spec['from'] = $iv->GetWindowStart();
86
$spec['to'] = $iv->GetWindowEnd();
87
$detwin = $this->ParseTitleDate($spec);
90
array("dl_id" => $download,
92
"db_server" => $this->props['db_server'],
93
"db_name" => $this->props['db_name'],
94
"db_group" => $this->props['db_group'],
95
"db_mask" => $this->props['db_mask'],
96
"control_group" => $this->props['control_group'],
97
"resample" => $this->props['resample'],
98
"experiment" => $this->props['experiment'],
99
"window" => $this->props['window'],
101
"startdate" => time(),
103
"format" => $this->props['format'],
104
"virtual" => $this->props['virtual'],
105
"srctree" => $this->props['srctree'],
110
"filesremaining" => 1,
111
"readablewindow" => "",
113
"detwindow" => $detwin,
114
"axis_range" => $this->props['axis_range'],
115
"temperature_axis_range" => $this->props['temperature_axis_range'],
116
"voltage_axis_range" => $this->props['voltage_axis_range'],
117
"aggregation" => $this->props['aggregation'],
118
"interpolate" => $this->props['interpolate'],
119
"show_gaps" => $this->props['show_gaps'],
120
"mask_mode" => $this->props['mask_mode'],
121
"auto_delete" => "true"
123
$this->Logit(var_export($download_props,true));
124
$this->cache->AddDownload($download_props);
125
$this->GetCacheImg($download);
127
$this->RunBackground();
130
function RemoveDownload($download = NULL, $noresp = false) {
131
if(!$download) $download = $_GET['dl_id'];
132
$file = $this->downloads_path."/".$this->Getfilename($download);
133
$img = $this->downloads_path."/images/$download.png";
134
$this->cache->RemoveDownload($download);
137
if(!$noresp)$this->Response();
140
function GetDownloads() {
142
$sqlres = $this->cache->GetDownloads();
143
$user = $this->GetIp();
144
$download_list = array();
146
while($row = mysql_fetch_array($sqlres)) {
147
$download_props = array();
148
if(is_array($_SESSION['sortby'])) {
149
foreach($_SESSION['sortby'] as $sortby) {
150
if($row['user'] == $sortby && $user != $sortby) {
151
$download_props['sort'] = "true";
152
$download_props['owner'] = "false";
153
foreach ($row as $key => $value) {
154
if($key == 'startdate') $value = $this->ParseReadableWindow($value, 1);
155
if(!is_numeric($key)) $download_props[$key] = $value;
161
if($row['user'] == $user) {
162
$download_props['sort'] = "true";
163
$download_props['owner'] = "true";
164
foreach ($row as $key => $value) {
165
if($key == 'startdate') $value = $this->ParseReadableWindow($value, 1);
166
if(!is_numeric($key)) $download_props[$key] = $value;
170
$download_props['sort'] = "rest";
171
$download_props['owner'] = "false";
172
foreach ($row as $key => $value) {
173
if($key == 'startdate') $value = $this->ParseReadableWindow($value, 1);
174
if(!is_numeric($key)) $download_props[$key] = $value;
177
// if prepared file not found from server -> remove download.
178
$file = $this->downloads_path."/".$this->Getfilename($download_props['dl_id']);
179
if($download_props['status'] != "Queue" && !file_exists($file)) $this->RemoveDownload($download_props['dl_id'], 1);
180
else array_push($download_list, $download_props);
182
return $download_list;
185
function GetDOwnloadProps($download) {
186
$filename = $this->Getfilename($download);
187
$content = $this->GetContentType($download);
188
$file = $this->downloads_path."/$filename";
189
$props = array('filename' => $filename,
190
'content_type' => $content,
195
function UpdateFilesRemaining($download, $val) {
196
$this->cache->UpdateDownloadCol($download, "filesremaining", $val);
199
function GetFilesRemaining($download) {
200
$res = $this->cache->GetDownloads($download);
201
$row = mysql_fetch_assoc($res);
202
return $row['filesremaining'];
205
function DlManagerRun() {
206
$res = $this->cache->GetDownloads("","ASC");
209
while($row = mysql_fetch_array($res)) {
210
$status = $row['status'];
211
if($status == "Finalizing") {
212
$download = $row['dl_id'];
213
$name = $this->Getfilename($download);
214
$file = $this->downloads_path."/".$name;
215
$fsize = filesize($file);
216
$lastmodified = time() - filemtime($file);
217
if($fsize > 1 && $lastmodified > 1) $this->ChangeStatus($row['dl_id'], 'Ready');
221
else if($status == "Preparing" && $finalizing == 0) {
222
$download = $row['dl_id'];
223
$progress = $row['progress'];
224
$frem = $row['filesremaining'];
227
if($nextdownload == "" && $status == "Queue"){
228
$nextdownload = $row['dl_id'];
229
$frem = $row['filesremaining'];
232
if($nextdownload != "" && $busy != 1) {
233
if($this->RunBackground() == 0) $st = "Done";
235
if($progress != "") {
236
$extra = "dl_id=\"$download\" progress=\"$progress\" frem=\"$frem\" finalizing=\"$finalizing\"";
237
$st = "Showprogress";
240
if ($finalizing) $extra = "finalizing=\"$finalizing\"";
242
if ($finalizing) $extra = "finalizing=\"$finalizing\"";
245
$this->Response($st, $extra);
248
function RunBackground() {
250
global $ADEI_ROOTDIR;
251
$res = exec('ps xa | grep "downloads_check.php" | grep -v grep | wc -l');
252
if($res == 0) exec("$PHP_BINARY $ADEI_ROOTDIR"."system/downloads_check.php &");
253
// if($res == 0) exec("$PHP_BINARY $ADEI_ROOTDIR"."system/downloads_check.php >/tmp/xxx &");
257
function DataRequest($opts){
259
$ADEI->RequireClass("export");
260
$req = new DATARequest($opts);
261
$export = new EXPORT($req, $h, $format, $this);
266
if($download = $_GET["dl_id"]) {
267
$props = $this->GetDownloadProps($download);
268
$content_type = $props['content_type'];
269
$filename = $props['filename'];
270
$file = $props['file'];
271
$fsize = filesize($file);
272
header("Cache-Control: no-cache, must-revalidate");
273
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
274
header("Content-type: $content_type");
275
header("Content-length: $fsize");
276
header("Content-Disposition: attachment; filename=\"$filename\"");
279
else throw new ADEIException(translate("Error while starting file download"));
282
function GetDmOutput($multimode, $ext, $req) {
283
$props = $req->GetProps();
284
$download = $props["dl_id"];
285
$dl_name = "{$props['db_server']}_{$props['db_name']}_$download";
286
if($multimode)$ext = "zip";
287
$astring = $this->downloads_path."/$dl_name.$ext";
288
$output = new IO($astring);
289
$this->ChangeStatus($download, "Preparing");
293
function CreateDataRequestOpts($download) {
294
$res = $this->cache->GetDownloads($download);
295
$row = mysql_fetch_assoc($res);
296
$opts = array('dl_id' => '');
297
foreach ($row as $key => $value) {
298
$opts[$key] = $value;
303
function UpdateProgress($prog, $download) {
305
$dl_name = $this->Getfilename($download);
306
$fsize = $this->GetFilesize($dl_name);
307
$this->cache->UpdateDownloadCol($download, "status", "Ready", $fsize);
309
else $this->cache->UpdateDownloadCol($download, "progress", $prog);
312
function ChangeStatus($download = NULL, $status = NULL) {
313
if($status == "Ready") {
314
$fsize = $this->GetFilesize($this->Getfilename($download));
316
$this->cache->UpdateDownloadCol($download, "status", $status, $fsize);
319
function SetError($download, $error) {
320
$this->cache->UpdateDownloadCol($download, "error", $error);
323
function CheckStatus($download) {
324
if(mysql_fetch_array($this->cache->GetDownloads($download)) == "") $ret = 0;
329
function Response($st = NULL, $extra = NULL) {
330
if(!$st) $st = "Done";
331
echo "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<result job=\"".$st."\" $extra></result>";
334
function Getfilename($download) {
335
$res = $this->cache->GetDownloads($download);
336
while($row = mysql_fetch_array($res)) {
337
$dl_name = "{$row['db_server']}_{$row['db_name']}_$download";
338
$ext = $this->GetFileExtension($dl_name);
344
function GetFileExtension($dl_name) {
345
$dir = opendir($this->downloads_path);
346
while ($file = readdir($dir)) {
347
if (preg_match("/".$dl_name."./i", $file)) {
348
return pathinfo($file, PATHINFO_EXTENSION);
353
function GetFilesize($dl_name) {
354
$file = $this->downloads_path."/".$dl_name;
355
$fsize = round((filesize($file) / 1048576), 2);
356
if($fsize >= 1)$fsize = round($fsize, 1);
357
else if($fsize >= 2)$fsize = round($fsize, -1);
358
if($fsize < 1) $fsize = 0.1;
362
function Toggleautodelete($download) {
363
$res = $this->cache->GetDownloads($download);
364
$row = mysql_fetch_assoc($res);
365
if($row["auto_delete"] == "true") $status = "no";
366
else $status = "true";
367
$this->cache->UpdateDownloadCol($download,"auto_delete", $status);
373
if(isset($_SESSION['sortby']) && is_array($_SESSION['sortby'])) {
374
foreach($_SESSION['sortby'] as $key=>$value) {
375
if($value == $_GET['sortby']) {
376
unset($_SESSION['sortby'][$key]);
381
array_push($_SESSION['sortby'], $_GET['sortby']);
385
$arr = array($_GET['sortby']);
386
$_SESSION['sortby'] = $arr;
391
function GetDownloadDetails() {
393
$download = $_GET['dl_id'];
394
$res = $this->cache->GetDownloads($download);
395
$row = mysql_fetch_assoc($res);
396
$opts = array('dl_id' => '');
397
foreach ($row as $key => $value) {
398
$opts[$key] = $value;
401
if(!$size = $opts['filesize']) $size = "n/a";
402
else if($size < 1) $size = "<1 MB";
404
if($opts['ctype'] == "application/x-zip-compressed") $format = "zip/{$opts['format']}";
405
else $format = $opts['format'];
407
if($row['srctree'] != "") {
408
$res = SOURCETree::Parse($row['srctree']);
409
foreach($res as $key => $grp) {
410
$req = new DATARequest($grp['props']);
411
$reader = $req->CreateReader();
412
$itemlist[$key] = $reader->GetItemList();
416
$req = new DATARequest($opts);
417
$reader = $req->CreateReader();
418
$grps = $reader->GetGroups();
419
$glist = $reader->GetGroupList();
420
$mask = new MASK($opts);
422
foreach($glist as $grp => $det) {
423
$itemlist[$det['gid']] = $reader->GetItemList($grps[$i], $mask);
427
$window = $this->ParseReadableWindow($opts['window']);
428
if($opts['status'] == "ERROR") $download_props['error'] = "Something went wrong while preparing data.";
429
if($error = $opts['error']) $download_props['error'] = $error;
430
$download_props['window'] = $window;
431
$download_props['format'] = $format;
432
$download_props['size'] = $size;
433
$download_details['props'] = $download_props;
434
foreach($itemlist as $gid => $items) {
435
if(!is_numeric($gid)) {
436
foreach($items as $key => $item ) {
437
$download_details['groups'][$gid][$key] = array("id" =>"{$item['id']}", "name" => "{$item['name']}");
442
return $download_details;
445
function ParseReadableWindow($window, $single = NULL) {
447
$ret = @date("F j, Y, g:i:s a", $window);
448
if (!$ret) $ret = $window;
450
global $ADEI_TIMINGS;
451
if($window == 0) $start = $end = "All";
452
else if(!$midpos = stripos($window,'-',2)) {
453
foreach($ADEI_TIMINGS as $time => $secs) {
454
if($window == $secs) $start = "$time";
459
$to = substr($window, $midpos + 1);
460
$from = substr($window, 0, $midpos);
462
$start = date("F j, Y, g:i:s a", $to);
466
$start = date("F j, Y, g:i:s a", $from);
467
$end = date("F j, Y, g:i:s a", $to);
470
$ret = array('from' => $start, 'to' => $end);
475
function GetCacheImg($download) {
476
$filename = $this->downloads_path."/images/$download.png";
478
$req = new DATARequest();
479
$req->SetProp("width", "512");
480
$req->SetProp("height", "384");
481
$draw = $req->CreatePlotter();
483
$draw->Save($filename);
485
catch(ADEIException $ex) {
486
$ex->logInfo(NULL, $draw);
490
function SetContentType($type, $download) {
491
$this->cache->UpdateDownloadCol($download, "ctype", $type);
494
function GetContentType($download) {
495
$res = $this->cache->GetDownloads($download);
496
$row = mysql_fetch_assoc($res);
497
return $row['ctype'];
501
if(getenv("HTTP_CLIENT_IP")) $ip = getenv("HTTP_CLIENT_IP");
502
else if(getenv("REMOTE_ADDR")) $ip = getenv("REMOTE_ADDR");
503
else $ip = "Unknown IP";
504
if($ip == "::1") $ip = "127.0.0.1";
508
function Logit($msg) {
509
$time = date("F j, Y, g:i:s a");
510
$logfile = fopen($this->downloads_path ."/logit.txt", 'a') or die("can't open file");
514
$input = ob_get_contents();
517
fwrite($logfile, "$time :: MSG: $input\n");
521
function IsVirtual($download) {
523
$res = $this->cache->GetDownloads($download);
524
$row = mysql_fetch_assoc($res);
525
if($row['db_server'] == 'virtual') $virtual = true;
529
function ParseTitleDate($spec) {
530
global $GRAPH_SUBSECOND_THRESHOLD;
532
$spec['length'] = dsMathPreciseSubstract($spec['to'], $spec['from']);
534
$from = $spec['from'];
536
$length = $spec['length'];
538
$afrom = getdate($from);
541
if ($length > 315360000) { // 10 years
544
$date_title = $afrom['year'] . " - " . $ato['year'];
545
} elseif ($length > 31104000) { // 1 year
546
if ($afrom['year'] == $ato['year']) {
549
$date_title = $afrom['year'];
551
$date_format = 'M, Y';
553
$date_title = $afrom['year'] . " - " . $ato['year'];
555
} elseif ($length > 1036800) { // 12 days
556
$date_format = 'M d';
559
if ($afrom['year'] == $ato['year']) {
560
if ($afrom['mon'] == $ato['mon']) {
561
$date_title = $afrom['month'] . ", " . $afrom['year'];
563
$date_title = $afrom['year'];
566
$date_title = $afrom['year'] . " - " . $ato['year'];
568
} elseif ($length > 86400) { // 1 day
569
$date_format = 'M d, H:i';
572
if ($afrom['year'] == $ato['year']) {
573
if ($afrom['mon'] == $ato['mon']) {
574
$date_title = $afrom['month'] . ", " . $afrom['year'];
576
$date_title = $afrom['year'];
579
$date_title = $afrom['year'] . " - " . $ato['year'];
581
} elseif ($length > 14400) { // 4 hours
582
$date_format = 'H:i';
585
if ($afrom['year'] == $ato['year']) {
586
if ($afrom['mon'] == $ato['mon']) {
587
if ($afrom['mday'] == $ato['mday']) {
588
$date_title = $afrom['month'] . " " . $afrom['mday'] . ", " . $afrom['year'];
590
$date_title = $afrom['month'] . " " . $afrom['mday'] . " - " . $ato['mday'] . ", " . $afrom['year'];
593
$date_title = date("M", $from) . " " . $afrom['mday'] . " - " . date("M", $to) . " " . $ato['mday'] . ", " . $afrom['year'];
596
$date_title = date("M j, Y", $from) . " - " . date("M j, Y", $to);
598
} else if ($length > $GRAPH_SUBSECOND_THRESHOLD) {
599
$date_format = 'H:i:s';
602
if ($afrom['year'] == $ato['year']) {
603
if ($afrom['mon'] == $ato['mon']) {
604
if ($afrom['mday'] == $ato['mday']) {
605
$date_title = $afrom['month'] . " " . $afrom['mday'] . ", " . $afrom['year'];
607
$date_title = $afrom['month'] . " " . $afrom['mday'] . " - " . $ato['mday'] . ", " . $afrom['year'];
610
$date_title = date("M", $from) . " " . $afrom['mday'] . " - " . date("M", $to) . " " . $ato['mday'] . ", " . $afrom['year'];
613
$date_title = date("M j, Y", $from) . " - " . date("M j, Y", $to);
616
$ifrom = floor($from);
617
if (is_float($from)) $rfrom = substr(printf("%.9F", $from - $ifrom),2);
619
$pos = strpos($from, ".");
620
if ($pos === false) $rfrom = 0;
621
else $rfrom = substr($from, $pos + 1);
624
$date_title = date("M j, Y H:i:s", $ifrom);
626
$date_title .= "." . $rfrom;
631
if($date_title == "") $date_title = "All data";
b'\\ No newline at end of file'