/adei/trunk

To get this branch, use:
bzr branch http://darksoft.org/webbzr/adei/trunk
210.1.2 by Suren A. Chilingaryan
Download manager by Aki Rimpelainen
1
<?php
2
class DOWNLOADMANAGER {  
3
  var $props;
4
  var $req;
5
  var $cache; 
6
 
7
  var $downloads_path;
8
  var $images_path;
9
  
10
  function __construct() {      
11
      global $TMP_PATH;
12
      global $ADEI;
13
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();            
23
  }
24
  
25
  function CheckAndCreateDirectories(){
26
    if (!is_dir($this->downloads_path)) {
27
	if (@mkdir($this->downloads_path, 0777, true)){
28
	    @chmod($this->downloads_path, 0777);
29
	}   
30
    }
31
    if (!is_dir($this->images_path)) {
32
	if (@mkdir($this->images_path, 0777, true)){
33
	    @chmod($this->images_path, 0777);
34
	}   
35
    }
36
  }
37
    
38
  public function DlmanagerUpdate($target, $progress = NULL, $download = NULL) {   
39
    switch ($target) {
40
      case "progress":	
41
	$this->UpdateProgress($progress, $download);	
42
	if(mysql_fetch_array($this->cache->GetDownloads($download)) == "") $ret = false;
43
	else $ret = true;    
44
	return $ret; 
45
      break;
46
      case "start":
47
	$download = $this->GetDownload();
48
	$this->UpdateProgress(1, $download);  
49
	return $download;
50
      break;      
51
      case "finish":	
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");
57
	}     
58
	else {
59
	  $this->UpdateFilesRemaining($download, $filesremaining - 1);
60
	  $this->UpdateProgress(1, $download);
61
	}
62
      break;      
63
    }
64
  }
65
  
66
  function GetDownload() {
67
    $res = $this->cache->GetDownloads("","ASC");
68
    while($row = mysql_fetch_array($res)) {
69
      if($row['status'] == "Preparing") {
70
	  return $row['dl_id'];
71
	  exit;
72
	} 
73
      }
74
  }
75
76
  function AddDownload() {      
77
    $ip = $this->GetIp();   
78
    if(!is_numeric($this->props['db_group'])) $db_group = " - " .ucfirst($this->props['db_group']);
79
    else $db_group = "";
80
    $name = ucfirst("{$this->props['db_server']} - ").ucfirst("{$this->props['db_name']}") .$db_group;    
405 by Suren A. Chilingaryan
Improve handling of names in download manager
81
    $download = md5(uniqid (rand(), true));
82
    
83
    if ($this->props['cache']) 
84
	$reader = $this->req->CreateCacheReader();
85
    else
86
	$reader = $this->req->CreateReader(); 
210.1.2 by Suren A. Chilingaryan
Download manager by Aki Rimpelainen
87
    //$this->Logit(var_export($reader,true));
88
    $iv = $reader->CreateInterval();
405 by Suren A. Chilingaryan
Improve handling of names in download manager
89
    $spec['from'] = $iv->GetRequestWindowStart();
90
    $spec['to'] = $iv->GetRequestWindowEnd();
210.1.2 by Suren A. Chilingaryan
Download manager by Aki Rimpelainen
91
    $detwin = $this->ParseTitleDate($spec);
92
   
93
    $download_props = 
94
      array("dl_id" => 		 $download,
95
	    "dl_name" => 	 $name,
96
	    "db_server" => 	 $this->props['db_server'],
97
	    "db_name" => 	 $this->props['db_name'],
98
	    "db_group" => 	 $this->props['db_group'],
99
	    "db_mask" => 	 $this->props['db_mask'],
100
	    "control_group" =>   $this->props['control_group'],
101
	    "resample" => 	 $this->props['resample'],
102
	    "experiment" => 	 $this->props['experiment'],
103
	    "window" =>  	 $this->props['window'],
104
	    "status" => 	 "Queue",
105
	    "startdate" => 	 time(),
106
	    "enddate" => 	 "",
107
	    "format" =>  	 $this->props['format'],
108
	    "virtual" => 	 $this->props['virtual'],
109
	    "srctree" => 	 $this->props['srctree'],
110
	    "progress" => 	 0,
111
	    "user" =>  		 $ip,
355 by Suren A. Chilingaryan
Fixes in Download Manager
112
	    "filesize" =>  	 0,
210.1.2 by Suren A. Chilingaryan
Download manager by Aki Rimpelainen
113
	    "ctype" =>  	 "",
114
	    "filesremaining" =>  1,
115
	    "readablewindow" =>  "",
116
	    "error" => 		 "",
117
	    "detwindow" => 	 $detwin,	    
118
	    "axis_range" =>      $this->props['axis_range'],
119
	    "temperature_axis_range" => $this->props['temperature_axis_range'],
120
	    "voltage_axis_range" => $this->props['voltage_axis_range'],
121
	    "aggregation" =>     $this->props['aggregation'],
122
	    "interpolate" =>     $this->props['interpolate'],
123
	    "show_gaps" =>       $this->props['show_gaps'],
124
	    "mask_mode" =>       $this->props['mask_mode'],
125
	    "auto_delete" =>	 "true"
126
	    );      
216 by Suren A. Chilingaryan
Quick fix (not a real one) of the system scripts belonging to download manager
127
    //$this->Logit(var_export($download_props,true));
210.1.2 by Suren A. Chilingaryan
Download manager by Aki Rimpelainen
128
    $this->cache->AddDownload($download_props); 
129
    $this->GetCacheImg($download);  
130
    $this->Response();  
131
    $this->RunBackground();      
132
  }
133
  
134
  function RemoveDownload($download = NULL, $noresp = false) {
135
    if(!$download) $download = $_GET['dl_id'];   
136
    $file = $this->downloads_path."/".$this->Getfilename($download);
137
    $img = $this->downloads_path."/images/$download.png";
138
    $this->cache->RemoveDownload($download);
139
    unlink("$file");
140
    unlink("$img");
141
    if(!$noresp)$this->Response();     
142
  }
143
  
144
  function GetDownloads() {
145
    session_start();
146
    $sqlres = $this->cache->GetDownloads();    
147
    $user = $this->GetIp();
148
    $download_list = array();
149
    
150
    while($row = mysql_fetch_array($sqlres)) {    
151
      $download_props = array();
152
      if(is_array($_SESSION['sortby'])) {
153
	foreach($_SESSION['sortby'] as $sortby) {      
154
	  if($row['user'] == $sortby && $user != $sortby) {	   
155
	    $download_props['sort'] = "true"; 
156
	    $download_props['owner'] = "false";
157
	    foreach ($row as $key => $value) {	  
158
	      if($key == 'startdate') $value = $this->ParseReadableWindow($value, 1);
159
	      if(!is_numeric($key)) $download_props[$key] = $value;
160
	    }		   
161
	    $handled = 1;
162
	  }
163
	}
164
      }
165
      if($row['user'] == $user) {	
166
	$download_props['sort'] = "true";
167
	$download_props['owner'] = "true";
168
	foreach ($row as $key => $value) {
169
	  if($key == 'startdate') $value = $this->ParseReadableWindow($value, 1);
170
	  if(!is_numeric($key)) $download_props[$key] = $value;
171
	}	
172
      } 
173
      else if(!$handled) {	 
174
	  $download_props['sort'] = "rest";
175
	  $download_props['owner'] = "false";
176
	  foreach ($row as $key => $value) {
177
	    if($key == 'startdate') $value = $this->ParseReadableWindow($value, 1);
178
	    if(!is_numeric($key))  $download_props[$key] = $value;
179
	  }	 
180
      } 
181
	// if prepared file not found from server -> remove download.
182
      $file = $this->downloads_path."/".$this->Getfilename($download_props['dl_id']);
183
      if($download_props['status'] != "Queue" && !file_exists($file)) $this->RemoveDownload($download_props['dl_id'], 1);
184
      else array_push($download_list, $download_props);
185
    }
186
    return $download_list;    	
187
  }
188
  
189
  function GetDOwnloadProps($download) {   
405 by Suren A. Chilingaryan
Improve handling of names in download manager
190
    $res = $this->cache->GetDownloads($download);
191
    $row = mysql_fetch_assoc($res);    
192
210.1.2 by Suren A. Chilingaryan
Download manager by Aki Rimpelainen
193
    $filename = $this->Getfilename($download);
194
    $content = $this->GetContentType($download);     
195
    $file = $this->downloads_path."/$filename";    
405 by Suren A. Chilingaryan
Improve handling of names in download manager
196
    $props = array_merge($row, array('filename' => $filename,
210.1.2 by Suren A. Chilingaryan
Download manager by Aki Rimpelainen
197
		  'content_type' => $content,
405 by Suren A. Chilingaryan
Improve handling of names in download manager
198
		  'file' => $file));
210.1.2 by Suren A. Chilingaryan
Download manager by Aki Rimpelainen
199
    return $props;
200
  }
201
  
202
  function UpdateFilesRemaining($download, $val) {
203
    $this->cache->UpdateDownloadCol($download, "filesremaining", $val);    
204
  }
205
  
206
  function GetFilesRemaining($download) {
207
    $res = $this->cache->GetDownloads($download);
208
    $row = mysql_fetch_assoc($res);    
209
    return $row['filesremaining'];      
210
  }
211
  
212
  function DlManagerRun() {           
213
    $res = $this->cache->GetDownloads("","ASC");
214
    $starting = 0;
215
    $finalizing = 0;
216
    while($row = mysql_fetch_array($res)) {
217
      $status = $row['status'];
218
      if($status == "Finalizing") {
219
	$download = $row['dl_id'];
220
	$name = $this->Getfilename($download); 
221
	$file = $this->downloads_path."/".$name;
222
	$fsize = filesize($file);	
223
	$lastmodified = time() - filemtime($file);
224
	if($fsize > 1 && $lastmodified > 1) $this->ChangeStatus($row['dl_id'], 'Ready');
225
	$finalizing = 1;
226
	$busy = 1;
227
      }
228
      else if($status == "Preparing" && $finalizing == 0) {
229
	$download = $row['dl_id'];
230
	$progress = $row['progress'];
231
	$frem = $row['filesremaining'];
232
	$busy = 1;
233
      }
234
      if($nextdownload == "" && $status == "Queue"){
235
	$nextdownload = $row['dl_id'];
236
	$frem = $row['filesremaining'];
237
      }
238
    }
239
    if($nextdownload != "" && $busy != 1) {       
240
      if($this->RunBackground() == 0) $st = "Done";      
241
    }        
242
    if($progress != "") {     
243
	$extra = "dl_id=\"$download\" progress=\"$progress\" frem=\"$frem\" finalizing=\"$finalizing\"";
244
	$st = "Showprogress";
245
    } else if(!$st) {
246
	$st = "Idle";
247
	if ($finalizing) $extra = "finalizing=\"$finalizing\"";
248
    } else {
249
	if ($finalizing) $extra = "finalizing=\"$finalizing\"";
250
    }
251
    
252
    $this->Response($st, $extra);	    
253
  }
254
  
255
  function RunBackground() {
256
   global $PHP_BINARY;   
257
   global $ADEI_ROOTDIR;
258
   $res = exec('ps xa | grep "downloads_check.php" | grep -v grep | wc -l');    
259
   if($res == 0) exec("$PHP_BINARY $ADEI_ROOTDIR"."system/downloads_check.php &");
260
//   if($res == 0) exec("$PHP_BINARY $ADEI_ROOTDIR"."system/downloads_check.php >/tmp/xxx &");
261
   return $res;
262
  }
263
264
  function DataRequest($opts){
265
    global $ADEI;
266
    $ADEI->RequireClass("export");    
267
    $req = new DATARequest($opts);      
268
    $export = new EXPORT($req, $h, $format, $this);
269
    $export->Export();	    
270
  }
271
272
  function GetFile() {
273
    if($download = $_GET["dl_id"]) {            
274
      $props = $this->GetDownloadProps($download);	
275
      $content_type = $props['content_type'];
405 by Suren A. Chilingaryan
Improve handling of names in download manager
276
      $filename = $props['dl_name'];
277
      $file = $props['file'];
210.1.2 by Suren A. Chilingaryan
Download manager by Aki Rimpelainen
278
      $fsize = filesize($file);
279
      header("Cache-Control: no-cache, must-revalidate");
280
      header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
281
      header("Content-type: $content_type");
282
      header("Content-length: $fsize");
283
      header("Content-Disposition: attachment; filename=\"$filename\"");
284
      readfile($file);
285
    }
286
    else throw new ADEIException(translate("Error while starting file download"));
287
  }
288
289
  function GetDmOutput($multimode, $ext, $req) {
290
    $props = $req->GetProps();
291
    $download = $props["dl_id"];
292
    $dl_name = "{$props['db_server']}_{$props['db_name']}_$download"; 
293
    if($multimode)$ext = "zip";	
294
    $astring = $this->downloads_path."/$dl_name.$ext";
295
    $output = new IO($astring);	
296
    $this->ChangeStatus($download, "Preparing");
297
    return $output;	    
298
  }
299
  
300
  function CreateDataRequestOpts($download) {
301
    $res = $this->cache->GetDownloads($download);
302
    $row = mysql_fetch_assoc($res);   
303
    $opts = array('dl_id' => '');
304
    foreach ($row as $key => $value) {      
305
      $opts[$key] = $value;	
306
    }    
307
    return $opts;  
308
  } 
309
  
310
  function UpdateProgress($prog, $download) {
311
    if($prog == 100) {
312
      $dl_name = $this->Getfilename($download); 
313
      $fsize = $this->GetFilesize($dl_name);
314
      $this->cache->UpdateDownloadCol($download, "status", "Ready", $fsize);           
315
    }
316
    else $this->cache->UpdateDownloadCol($download, "progress", $prog); 
317
  }  
318
  
319
  function ChangeStatus($download = NULL, $status = NULL) {     
320
    if($status == "Ready") {	
321
      $fsize = $this->GetFilesize($this->Getfilename($download));
322
    }        
323
    $this->cache->UpdateDownloadCol($download, "status", $status, $fsize);
324
  }
325
    
326
  function SetError($download, $error) {
327
   $this->cache->UpdateDownloadCol($download, "error", $error);       
328
  }
329
    
330
  function CheckStatus($download) {
331
    if(mysql_fetch_array($this->cache->GetDownloads($download)) == "") $ret = 0;
332
    else $ret = 1;    
333
    return $ret;
334
  }
335
336
  function Response($st = NULL, $extra = NULL) {	
337
    if(!$st) $st = "Done";    
338
    echo "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<result job=\"".$st."\" $extra></result>";
339
  }
340
    
341
  function Getfilename($download) {
342
    $res = $this->cache->GetDownloads($download);
343
    while($row = mysql_fetch_array($res)) {    
344
      $dl_name = "{$row['db_server']}_{$row['db_name']}_$download";     
345
      $ext = $this->GetFileExtension($dl_name);
346
      $dl_name .= ".$ext";
347
    }
348
    return $dl_name;
349
  }
350
  
351
  function GetFileExtension($dl_name) {	  
352
    $dir = opendir($this->downloads_path);
353
    while ($file = readdir($dir)) { 
212 by Suren A. Chilingaryan
Integration of work of students with fixes
354
      if (preg_match("/".$dl_name."./i", $file)) {
210.1.2 by Suren A. Chilingaryan
Download manager by Aki Rimpelainen
355
	return pathinfo($file, PATHINFO_EXTENSION);	
356
      }
357
    }
358
  }
359
  
360
  function GetFilesize($dl_name) {
361
    $file = $this->downloads_path."/".$dl_name;      
362
    $fsize = round((filesize($file) / 1048576), 2);
363
    if($fsize >= 1)$fsize = round($fsize, 1);
364
    else if($fsize >= 2)$fsize = round($fsize, -1);
365
    if($fsize < 1) $fsize = 0.1;    
366
    return $fsize;
367
  }      
368
    
369
  function Toggleautodelete($download) {
370
    $res = $this->cache->GetDownloads($download);
371
    $row = mysql_fetch_assoc($res);     
372
    if($row["auto_delete"] == "true") $status = "no";
373
    else $status = "true";
374
    $this->cache->UpdateDownloadCol($download,"auto_delete", $status);	
375
    $this->Response();
376
  }
377
378
  function SortBy() {
379
    session_start();   
380
    if(isset($_SESSION['sortby']) && is_array($_SESSION['sortby'])) {     
381
      foreach($_SESSION['sortby'] as $key=>$value) {
382
	if($value == $_GET['sortby']) {
383
	  unset($_SESSION['sortby'][$key]);
384
	  $push = true;
385
	}
386
      }     
387
      if(!$push) {
388
	array_push($_SESSION['sortby'], $_GET['sortby']);	
389
      }
390
    }
391
    else {
392
      $arr = array($_GET['sortby']);
393
      $_SESSION['sortby'] = $arr;
394
    }
395
    $this->Response();   
396
  }
397
  
398
  function GetDownloadDetails() {
399
    $i = 0;
400
    $download = $_GET['dl_id'];          
401
    $res = $this->cache->GetDownloads($download);
402
    $row = mysql_fetch_assoc($res);   
403
    $opts = array('dl_id' => '');
404
    foreach ($row as $key => $value) {      
405
      $opts[$key] = $value;	
406
    }
407
    
408
    if(!$size = $opts['filesize']) $size = "n/a";
409
    else if($size < 1) $size = "&#60;1 MB";
410
    else $size .= " MB";
411
    if($opts['ctype'] == "application/x-zip-compressed") $format = "zip/{$opts['format']}";
412
    else $format = $opts['format'];
413
         
414
    if($row['srctree'] != "") {
415
      $res = SOURCETree::Parse($row['srctree']);
416
      foreach($res as $key => $grp) {		
417
	$req = new DATARequest($grp['props']);
418
	$reader = $req->CreateReader();	
419
	$itemlist[$key] = $reader->GetItemList();	  
420
      }
421
    } 	
422
    else {
423
      $req = new DATARequest($opts);      
424
      $reader = $req->CreateReader(); 
425
      $grps = $reader->GetGroups();
426
      $glist = $reader->GetGroupList();      
427
      $mask = new MASK($opts);
428
     
429
      foreach($glist as $grp => $det) { 	
430
	$itemlist[$det['gid']] = $reader->GetItemList($grps[$i], $mask);	 
431
	$i = $i+1;
432
      }
433
    }
434
    $window = $this->ParseReadableWindow($opts['window']);
435
    if($opts['status'] == "ERROR") $download_props['error'] = "Something went wrong while preparing data.";
436
    if($error = $opts['error']) $download_props['error'] = $error;    
437
    $download_props['window'] = $window;     
438
    $download_props['format'] = $format;
439
    $download_props['size'] = $size;
440
    $download_details['props'] = $download_props;
441
    foreach($itemlist as $gid => $items) {
442
      if(!is_numeric($gid)) {		
443
	foreach($items as $key => $item ) {	  
444
	  $download_details['groups'][$gid][$key] = array("id" =>"{$item['id']}", "name" => "{$item['name']}");	 
445
	}		
446
      }
447
    }
448
    
449
    return $download_details;
450
  }  
451
452
  function ParseReadableWindow($window, $single = NULL) {
453
    if($single) {
454
	$ret = @date("F j, Y, g:i:s a", $window);
455
	if (!$ret) $ret = $window;
456
    } else {
457
      global $ADEI_TIMINGS;
458
      if($window == 0) $start = $end = "All";
459
      else if(!$midpos = stripos($window,'-',2)) {
460
	foreach($ADEI_TIMINGS as $time => $secs) {
461
	  if($window == $secs) $start = "$time";
462
	}
463
	$end = "All";
464
      }
465
      else {
466
	$to = substr($window, $midpos + 1);
467
	$from = substr($window, 0, $midpos);    
468
	if($midpos < 3) {
469
	  $start = date("F j, Y, g:i:s a", $to);
470
	  $end = "All";
471
	}
472
	else {
473
	  $start = date("F j, Y, g:i:s a", $from);
474
	  $end = date("F j, Y, g:i:s a", $to);
475
	}
476
      }  
477
      $ret = array('from' => $start, 'to' => $end); 
478
    }
479
    return $ret;
480
  }
481
  
482
  function GetCacheImg($download) {    
483
    $filename = $this->downloads_path."/images/$download.png";    
484
    try {
485
        $req = new DATARequest();
486
	$req->SetProp("width", "512");
487
	$req->SetProp("height", "384");
488
        $draw = $req->CreatePlotter();
489
        $draw->Create();	
490
        $draw->Save($filename);	
491
    }
492
    catch(ADEIException $ex) {
493
        $ex->logInfo(NULL, $draw);            
494
    }   
495
  }
496
  
405 by Suren A. Chilingaryan
Improve handling of names in download manager
497
  function SetContentType($type, $name, $download) {
210.1.2 by Suren A. Chilingaryan
Download manager by Aki Rimpelainen
498
    $this->cache->UpdateDownloadCol($download, "ctype", $type);  
405 by Suren A. Chilingaryan
Improve handling of names in download manager
499
    $this->cache->UpdateDownloadCol($download, "dl_name", $name);  
210.1.2 by Suren A. Chilingaryan
Download manager by Aki Rimpelainen
500
  }
501
  
502
  function GetContentType($download) {
503
    $res = $this->cache->GetDownloads($download);
504
    $row = mysql_fetch_assoc($res);       
505
    return $row['ctype']; 
506
  }
507
508
  function GetIp() {
509
    if(getenv("HTTP_CLIENT_IP"))   $ip = getenv("HTTP_CLIENT_IP");
510
    else if(getenv("REMOTE_ADDR")) $ip = getenv("REMOTE_ADDR");   
511
    else $ip = "Unknown IP";
512
    if($ip == "::1") $ip = "127.0.0.1";   
513
    return $ip;
514
  } 
515
516
  function Logit($msg) {  
517
    $time = date("F j, Y, g:i:s a");
518
    $logfile = fopen($this->downloads_path ."/logit.txt", 'a') or die("can't open file");
519
    if(is_array($msg)) {
520
      ob_start();
521
      print_r($msg); 
522
      $input = ob_get_contents();
523
    }
524
    else $input = $msg;
525
    fwrite($logfile, "$time :: MSG:  $input\n");
526
    fclose($logfile);
527
  }
528
529
  function IsVirtual($download) {
530
    $virtual = false;
531
    $res = $this->cache->GetDownloads($download);
532
    $row = mysql_fetch_assoc($res);    
533
    if($row['db_server'] == 'virtual') $virtual = true;
534
    return $virtual;
535
  }
536
537
  function ParseTitleDate($spec) {
538
    global $GRAPH_SUBSECOND_THRESHOLD;
539
540
    $spec['length'] = dsMathPreciseSubstract($spec['to'], $spec['from']);
541
542
    $from = $spec['from'];
543
    $to = $spec['to'];
544
    $length = $spec['length'];
545
546
    $afrom = getdate($from);
547
    $ato = getdate($to);
548
549
   if ($length > 315360000) { // 10 years 
550
        $date_format = 'Y';
551
        $label_interval = 1;
552
	$date_title = $afrom['year'] . " - " . $ato['year'];
553
    } elseif ($length > 31104000) { // 1 year	
554
	if ($afrom['year'] == $ato['year']) {
555
	    $date_format = 'M';
556
	    $label_interval = 2;
557
	    $date_title = $afrom['year'];
558
	} else {
559
	    $date_format = 'M, Y';
560
	    $label_interval = 2;	    
561
	    $date_title = $afrom['year'] . " - " . $ato['year'];
562
	}
563
    } elseif ($length > 1036800) { // 12 days
564
	$date_format = 'M d';
565
	$label_interval = 3;
566
	
567
	if ($afrom['year'] == $ato['year']) {
568
	    if ($afrom['mon'] == $ato['mon']) {
569
		$date_title = $afrom['month'] . ", " . $afrom['year'];	   
570
	    } else {
571
		$date_title = $afrom['year'];
572
	    }
573
	} else {
574
	    $date_title = $afrom['year'] . " - " . $ato['year'];
575
	}
576
    } elseif ($length > 86400) { // 1 day
577
	$date_format = 'M d, H:i';
578
	$label_interval = 4;
579
580
	if ($afrom['year'] == $ato['year']) {
581
	    if ($afrom['mon'] == $ato['mon']) {
582
		$date_title = $afrom['month'] . ", " . $afrom['year'];
583
	    } else {
584
		$date_title = $afrom['year'];
585
	    }
586
	} else {
587
	    $date_title = $afrom['year'] . " - " . $ato['year'];
588
	}
589
    } elseif ($length > 14400) { // 4 hours
590
	$date_format = 'H:i';
591
	$label_interval = 2;
592
593
	if ($afrom['year'] == $ato['year']) {
594
	    if ($afrom['mon'] == $ato['mon']) {
595
		if ($afrom['mday'] == $ato['mday']) {
596
		    $date_title = $afrom['month'] . " " . $afrom['mday'] . ", " . $afrom['year'];
597
		} else {
598
		    $date_title = $afrom['month'] . " " . $afrom['mday'] . " - " . $ato['mday'] . ", " . $afrom['year'];
599
		}
600
	    } else {
601
		$date_title = date("M", $from) . " " . $afrom['mday'] . " - " . date("M", $to) . " " . $ato['mday'] . ", " . $afrom['year'];
602
	    }
603
	} else {
604
	    $date_title = date("M j, Y", $from) . " - " . date("M j, Y", $to);
605
	}
606
    } else if ($length > $GRAPH_SUBSECOND_THRESHOLD) {
607
	$date_format = 'H:i:s';
608
	$label_interval = 4;
609
610
	if ($afrom['year'] == $ato['year']) {
611
	    if ($afrom['mon'] == $ato['mon']) {
612
		if ($afrom['mday'] == $ato['mday']) {
613
		    $date_title = $afrom['month'] . " " . $afrom['mday'] . ", " . $afrom['year'];
614
		} else {
615
		    $date_title = $afrom['month'] . " " . $afrom['mday'] . " - " . $ato['mday'] . ", " . $afrom['year'];
616
		}
617
	    } else {
618
		$date_title = date("M", $from) . " " . $afrom['mday'] . " - " . date("M", $to) . " " . $ato['mday'] . ", " . $afrom['year'];
619
	    }
620
	} else {
621
	    $date_title = date("M j, Y", $from) . " - " . date("M j, Y", $to);
622
	}
623
    } else {
624
	$ifrom = floor($from);
625
	if (is_float($from)) $rfrom = substr(printf("%.9F", $from - $ifrom),2);
626
	else {
627
	    $pos = strpos($from, ".");
628
	    if ($pos === false) $rfrom = 0;
629
	    else $rfrom = substr($from, $pos + 1);
630
	}
631
632
	$date_title = date("M j, Y H:i:s", $ifrom);
633
	if ($rfrom) {
634
	    $date_title .= "." . $rfrom;
635
	    $rfrom = "0.$rfrom";
636
	}	
637
    }
638
    
639
  if($date_title == "") $date_title = "All data";
640
  
641
  return $date_title;   
642
  } 
643
}
644
?>