/dev/adei-asec

To get this branch, use:
bzr branch http://darksoft.org/webbzr/dev/adei-asec

« back to all changes in this revision

Viewing changes to classes/download.php

  • Committer: Suren A. Chilingaryan
  • Date: 2011-03-15 02:47:05 UTC
  • mfrom: (210.1.3 adei)
  • Revision ID: csa@dside.dyndns.org-20110315024705-qljn30gwin8yrkne
Integration of work of students with fixes

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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;    
 
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);
 
88
   
 
89
    $download_props = 
 
90
      array("dl_id" =>           $download,
 
91
            "dl_name" =>         $name,
 
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'],
 
100
            "status" =>          "Queue",
 
101
            "startdate" =>       time(),
 
102
            "enddate" =>         "",
 
103
            "format" =>          $this->props['format'],
 
104
            "virtual" =>         $this->props['virtual'],
 
105
            "srctree" =>         $this->props['srctree'],
 
106
            "progress" =>        0,
 
107
            "user" =>            $ip,
 
108
            "filesize" =>        "",
 
109
            "ctype" =>           "",
 
110
            "filesremaining" =>  1,
 
111
            "readablewindow" =>  "",
 
112
            "error" =>           "",
 
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"
 
122
            );      
 
123
    $this->Logit(var_export($download_props,true));
 
124
    $this->cache->AddDownload($download_props); 
 
125
    $this->GetCacheImg($download);  
 
126
    $this->Response();  
 
127
    $this->RunBackground();      
 
128
  }
 
129
  
 
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);
 
135
    unlink("$file");
 
136
    unlink("$img");
 
137
    if(!$noresp)$this->Response();     
 
138
  }
 
139
  
 
140
  function GetDownloads() {
 
141
    session_start();
 
142
    $sqlres = $this->cache->GetDownloads();    
 
143
    $user = $this->GetIp();
 
144
    $download_list = array();
 
145
    
 
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;
 
156
            }              
 
157
            $handled = 1;
 
158
          }
 
159
        }
 
160
      }
 
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;
 
167
        }       
 
168
      } 
 
169
      else if(!$handled) {       
 
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;
 
175
          }      
 
176
      } 
 
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);
 
181
    }
 
182
    return $download_list;      
 
183
  }
 
184
  
 
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,
 
191
                  'file' => $file);
 
192
    return $props;
 
193
  }
 
194
  
 
195
  function UpdateFilesRemaining($download, $val) {
 
196
    $this->cache->UpdateDownloadCol($download, "filesremaining", $val);    
 
197
  }
 
198
  
 
199
  function GetFilesRemaining($download) {
 
200
    $res = $this->cache->GetDownloads($download);
 
201
    $row = mysql_fetch_assoc($res);    
 
202
    return $row['filesremaining'];      
 
203
  }
 
204
  
 
205
  function DlManagerRun() {           
 
206
    $res = $this->cache->GetDownloads("","ASC");
 
207
    $starting = 0;
 
208
    $finalizing = 0;
 
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');
 
218
        $finalizing = 1;
 
219
        $busy = 1;
 
220
      }
 
221
      else if($status == "Preparing" && $finalizing == 0) {
 
222
        $download = $row['dl_id'];
 
223
        $progress = $row['progress'];
 
224
        $frem = $row['filesremaining'];
 
225
        $busy = 1;
 
226
      }
 
227
      if($nextdownload == "" && $status == "Queue"){
 
228
        $nextdownload = $row['dl_id'];
 
229
        $frem = $row['filesremaining'];
 
230
      }
 
231
    }
 
232
    if($nextdownload != "" && $busy != 1) {       
 
233
      if($this->RunBackground() == 0) $st = "Done";      
 
234
    }        
 
235
    if($progress != "") {     
 
236
        $extra = "dl_id=\"$download\" progress=\"$progress\" frem=\"$frem\" finalizing=\"$finalizing\"";
 
237
        $st = "Showprogress";
 
238
    } else if(!$st) {
 
239
        $st = "Idle";
 
240
        if ($finalizing) $extra = "finalizing=\"$finalizing\"";
 
241
    } else {
 
242
        if ($finalizing) $extra = "finalizing=\"$finalizing\"";
 
243
    }
 
244
    
 
245
    $this->Response($st, $extra);           
 
246
  }
 
247
  
 
248
  function RunBackground() {
 
249
   global $PHP_BINARY;   
 
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 &");
 
254
   return $res;
 
255
  }
 
256
 
 
257
  function DataRequest($opts){
 
258
    global $ADEI;
 
259
    $ADEI->RequireClass("export");    
 
260
    $req = new DATARequest($opts);      
 
261
    $export = new EXPORT($req, $h, $format, $this);
 
262
    $export->Export();      
 
263
  }
 
264
 
 
265
  function GetFile() {
 
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\"");
 
277
      readfile($file);
 
278
    }
 
279
    else throw new ADEIException(translate("Error while starting file download"));
 
280
  }
 
281
 
 
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");
 
290
    return $output;         
 
291
  }
 
292
  
 
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;     
 
299
    }    
 
300
    return $opts;  
 
301
  } 
 
302
  
 
303
  function UpdateProgress($prog, $download) {
 
304
    if($prog == 100) {
 
305
      $dl_name = $this->Getfilename($download); 
 
306
      $fsize = $this->GetFilesize($dl_name);
 
307
      $this->cache->UpdateDownloadCol($download, "status", "Ready", $fsize);           
 
308
    }
 
309
    else $this->cache->UpdateDownloadCol($download, "progress", $prog); 
 
310
  }  
 
311
  
 
312
  function ChangeStatus($download = NULL, $status = NULL) {     
 
313
    if($status == "Ready") {    
 
314
      $fsize = $this->GetFilesize($this->Getfilename($download));
 
315
    }        
 
316
    $this->cache->UpdateDownloadCol($download, "status", $status, $fsize);
 
317
  }
 
318
    
 
319
  function SetError($download, $error) {
 
320
   $this->cache->UpdateDownloadCol($download, "error", $error);       
 
321
  }
 
322
    
 
323
  function CheckStatus($download) {
 
324
    if(mysql_fetch_array($this->cache->GetDownloads($download)) == "") $ret = 0;
 
325
    else $ret = 1;    
 
326
    return $ret;
 
327
  }
 
328
 
 
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>";
 
332
  }
 
333
    
 
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);
 
339
      $dl_name .= ".$ext";
 
340
    }
 
341
    return $dl_name;
 
342
  }
 
343
  
 
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);     
 
349
      }
 
350
    }
 
351
  }
 
352
  
 
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;    
 
359
    return $fsize;
 
360
  }      
 
361
    
 
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);  
 
368
    $this->Response();
 
369
  }
 
370
 
 
371
  function SortBy() {
 
372
    session_start();   
 
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]);
 
377
          $push = true;
 
378
        }
 
379
      }     
 
380
      if(!$push) {
 
381
        array_push($_SESSION['sortby'], $_GET['sortby']);       
 
382
      }
 
383
    }
 
384
    else {
 
385
      $arr = array($_GET['sortby']);
 
386
      $_SESSION['sortby'] = $arr;
 
387
    }
 
388
    $this->Response();   
 
389
  }
 
390
  
 
391
  function GetDownloadDetails() {
 
392
    $i = 0;
 
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;     
 
399
    }
 
400
    
 
401
    if(!$size = $opts['filesize']) $size = "n/a";
 
402
    else if($size < 1) $size = "&#60;1 MB";
 
403
    else $size .= " MB";
 
404
    if($opts['ctype'] == "application/x-zip-compressed") $format = "zip/{$opts['format']}";
 
405
    else $format = $opts['format'];
 
406
         
 
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();         
 
413
      }
 
414
    }   
 
415
    else {
 
416
      $req = new DATARequest($opts);      
 
417
      $reader = $req->CreateReader(); 
 
418
      $grps = $reader->GetGroups();
 
419
      $glist = $reader->GetGroupList();      
 
420
      $mask = new MASK($opts);
 
421
     
 
422
      foreach($glist as $grp => $det) {         
 
423
        $itemlist[$det['gid']] = $reader->GetItemList($grps[$i], $mask);         
 
424
        $i = $i+1;
 
425
      }
 
426
    }
 
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']}");  
 
438
        }               
 
439
      }
 
440
    }
 
441
    
 
442
    return $download_details;
 
443
  }  
 
444
 
 
445
  function ParseReadableWindow($window, $single = NULL) {
 
446
    if($single) {
 
447
        $ret = @date("F j, Y, g:i:s a", $window);
 
448
        if (!$ret) $ret = $window;
 
449
    } else {
 
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";
 
455
        }
 
456
        $end = "All";
 
457
      }
 
458
      else {
 
459
        $to = substr($window, $midpos + 1);
 
460
        $from = substr($window, 0, $midpos);    
 
461
        if($midpos < 3) {
 
462
          $start = date("F j, Y, g:i:s a", $to);
 
463
          $end = "All";
 
464
        }
 
465
        else {
 
466
          $start = date("F j, Y, g:i:s a", $from);
 
467
          $end = date("F j, Y, g:i:s a", $to);
 
468
        }
 
469
      }  
 
470
      $ret = array('from' => $start, 'to' => $end); 
 
471
    }
 
472
    return $ret;
 
473
  }
 
474
  
 
475
  function GetCacheImg($download) {    
 
476
    $filename = $this->downloads_path."/images/$download.png";    
 
477
    try {
 
478
        $req = new DATARequest();
 
479
        $req->SetProp("width", "512");
 
480
        $req->SetProp("height", "384");
 
481
        $draw = $req->CreatePlotter();
 
482
        $draw->Create();        
 
483
        $draw->Save($filename); 
 
484
    }
 
485
    catch(ADEIException $ex) {
 
486
        $ex->logInfo(NULL, $draw);            
 
487
    }   
 
488
  }
 
489
  
 
490
  function SetContentType($type, $download) {
 
491
    $this->cache->UpdateDownloadCol($download, "ctype", $type);  
 
492
  }
 
493
  
 
494
  function GetContentType($download) {
 
495
    $res = $this->cache->GetDownloads($download);
 
496
    $row = mysql_fetch_assoc($res);       
 
497
    return $row['ctype']; 
 
498
  }
 
499
 
 
500
  function GetIp() {
 
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";   
 
505
    return $ip;
 
506
  } 
 
507
 
 
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");
 
511
    if(is_array($msg)) {
 
512
      ob_start();
 
513
      print_r($msg); 
 
514
      $input = ob_get_contents();
 
515
    }
 
516
    else $input = $msg;
 
517
    fwrite($logfile, "$time :: MSG:  $input\n");
 
518
    fclose($logfile);
 
519
  }
 
520
 
 
521
  function IsVirtual($download) {
 
522
    $virtual = false;
 
523
    $res = $this->cache->GetDownloads($download);
 
524
    $row = mysql_fetch_assoc($res);    
 
525
    if($row['db_server'] == 'virtual') $virtual = true;
 
526
    return $virtual;
 
527
  }
 
528
 
 
529
  function ParseTitleDate($spec) {
 
530
    global $GRAPH_SUBSECOND_THRESHOLD;
 
531
 
 
532
    $spec['length'] = dsMathPreciseSubstract($spec['to'], $spec['from']);
 
533
 
 
534
    $from = $spec['from'];
 
535
    $to = $spec['to'];
 
536
    $length = $spec['length'];
 
537
 
 
538
    $afrom = getdate($from);
 
539
    $ato = getdate($to);
 
540
 
 
541
   if ($length > 315360000) { // 10 years 
 
542
        $date_format = 'Y';
 
543
        $label_interval = 1;
 
544
        $date_title = $afrom['year'] . " - " . $ato['year'];
 
545
    } elseif ($length > 31104000) { // 1 year   
 
546
        if ($afrom['year'] == $ato['year']) {
 
547
            $date_format = 'M';
 
548
            $label_interval = 2;
 
549
            $date_title = $afrom['year'];
 
550
        } else {
 
551
            $date_format = 'M, Y';
 
552
            $label_interval = 2;            
 
553
            $date_title = $afrom['year'] . " - " . $ato['year'];
 
554
        }
 
555
    } elseif ($length > 1036800) { // 12 days
 
556
        $date_format = 'M d';
 
557
        $label_interval = 3;
 
558
        
 
559
        if ($afrom['year'] == $ato['year']) {
 
560
            if ($afrom['mon'] == $ato['mon']) {
 
561
                $date_title = $afrom['month'] . ", " . $afrom['year'];     
 
562
            } else {
 
563
                $date_title = $afrom['year'];
 
564
            }
 
565
        } else {
 
566
            $date_title = $afrom['year'] . " - " . $ato['year'];
 
567
        }
 
568
    } elseif ($length > 86400) { // 1 day
 
569
        $date_format = 'M d, H:i';
 
570
        $label_interval = 4;
 
571
 
 
572
        if ($afrom['year'] == $ato['year']) {
 
573
            if ($afrom['mon'] == $ato['mon']) {
 
574
                $date_title = $afrom['month'] . ", " . $afrom['year'];
 
575
            } else {
 
576
                $date_title = $afrom['year'];
 
577
            }
 
578
        } else {
 
579
            $date_title = $afrom['year'] . " - " . $ato['year'];
 
580
        }
 
581
    } elseif ($length > 14400) { // 4 hours
 
582
        $date_format = 'H:i';
 
583
        $label_interval = 2;
 
584
 
 
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'];
 
589
                } else {
 
590
                    $date_title = $afrom['month'] . " " . $afrom['mday'] . " - " . $ato['mday'] . ", " . $afrom['year'];
 
591
                }
 
592
            } else {
 
593
                $date_title = date("M", $from) . " " . $afrom['mday'] . " - " . date("M", $to) . " " . $ato['mday'] . ", " . $afrom['year'];
 
594
            }
 
595
        } else {
 
596
            $date_title = date("M j, Y", $from) . " - " . date("M j, Y", $to);
 
597
        }
 
598
    } else if ($length > $GRAPH_SUBSECOND_THRESHOLD) {
 
599
        $date_format = 'H:i:s';
 
600
        $label_interval = 4;
 
601
 
 
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'];
 
606
                } else {
 
607
                    $date_title = $afrom['month'] . " " . $afrom['mday'] . " - " . $ato['mday'] . ", " . $afrom['year'];
 
608
                }
 
609
            } else {
 
610
                $date_title = date("M", $from) . " " . $afrom['mday'] . " - " . date("M", $to) . " " . $ato['mday'] . ", " . $afrom['year'];
 
611
            }
 
612
        } else {
 
613
            $date_title = date("M j, Y", $from) . " - " . date("M j, Y", $to);
 
614
        }
 
615
    } else {
 
616
        $ifrom = floor($from);
 
617
        if (is_float($from)) $rfrom = substr(printf("%.9F", $from - $ifrom),2);
 
618
        else {
 
619
            $pos = strpos($from, ".");
 
620
            if ($pos === false) $rfrom = 0;
 
621
            else $rfrom = substr($from, $pos + 1);
 
622
        }
 
623
 
 
624
        $date_title = date("M j, Y H:i:s", $ifrom);
 
625
        if ($rfrom) {
 
626
            $date_title .= "." . $rfrom;
 
627
            $rfrom = "0.$rfrom";
 
628
        }       
 
629
    }
 
630
    
 
631
  if($date_title == "") $date_title = "All data";
 
632
  
 
633
  return $date_title;   
 
634
  } 
 
635
}
 
636
?>
 
 
b'\\ No newline at end of file'