/adei/trunk

To get this branch, use:
bzr branch http://darksoft.org/webbzr/adei/trunk
1 by Suren A. Chilingaryan
Initial import
1
<?php
2
3
$default_precision = 120;
4
$i_multiplyer = 5;
5
6
function do_exit() {
7
    global $f1, $f2;
8
9
    if ($f1) fclose($f1);
10
    if ($f2) fclose($f2);
11
    exit;
12
}
13
14
function get_string($f, $exit = true) {
15
    $t = false;
16
    
17
    while ((!$t)&&(!feof($f)))
18
	$t = preg_replace("/\s+$/", "", fgets($f)); 
19
20
    if ((!$t)&&($exit)) do_exit();
21
    return $t;
22
}
23
24
function just_t1($t1) {
25
    global $size2;
26
    
27
    echo $t1;
28
    for ($i=0;$i<$size2;$i++) echo ", 0";
29
    echo "\r\n";
30
}	
31
32
if ((isset($_FILES))&&(isset($_FILES['file1']))&&(isset($_FILES['file2']))) {
33
    header("Content-type: application/binary"); 
34
    header("Content-Disposition: attachment; filename=\"merged.csv\"");
35
    header("Cache-Control: no-cache, must-revalidate");
36
    header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
37
38
    if (isset($_POST['precision'])) {
39
	$precision = intval($_POST['precision']);
40
	if ($precision <= 0) $precision = $default_precision;
41
    } else $precision = $default_precision;
42
    $iprecision = $i_multiplyer * $precision;
43
44
    $f1 = fopen($_FILES['file1']['tmp_name'], "r");
45
    $f2 = fopen($_FILES['file2']['tmp_name'], "r");
46
    if ((!$f1)||(!$f2)) do_exit();
47
48
    $t1 = get_string($f1);    
49
    $t2 = get_string($f2);    
50
51
    $size1 = sizeof(split(",",$t1)) - 1;
52
    $size2 = sizeof(split(",",$t2)) - 1;
53
54
    $t2 = substr($t2, strpos(",", $t2) + 1);
55
    echo $t1 . $t2 . "\r\n";
56
57
58
    $t1 = get_string($f1);
59
    $d1 = split (",", $t1, 2); $time1 = strtotime($d1[0]);
60
	
61
		
62
    $t2 = get_string($f2);    
63
    $d2 = split (",", $t2, 2); $time2 = strtotime($d2[0]);
64
    $tpre = $time2; $pre = $d2[1]; 
65
    
66
    
67
    $cprecision = $precision;
68
    while (($t1)&&(($time2-$cprecision)>$time1)) {
69
	just_t1($t1); $cprecision = 0;
70
    
71
	$t1 = get_string($f1, false);
72
	if (!$t1) break;
73
	
74
	$d1 = split (",", $t1, 2); $time1 = strtotime($d1[0]);
75
    }
76
    
77
    
78
    while ($t1) {
79
	while (($f2)&&($time2 < $time1)) {
80
	    $tpre = $time2; $pre = $d2[1];
81
	
82
	    $t2 = preg_replace("/\s+$/", "", fgets($f2));
83
	    if (feof($f2)) {
84
		fclose($f2);
85
		$f2 = false;
86
	    }
87
	    if (!$t2) continue;
88
	    
89
	    $d2 = split (",", $t2, 2); $time2 = strtotime($d2[0]);
90
	}
91
    
92
	if ($time2 > $time1) {
93
	    if (($tpre + $iprecision) > $time1) 
94
		echo $t1 . ", " . $pre . "\r\n";
95
	    elseif (($time2 - $precision) < $time1)
96
		echo $t1 . ", " . $d2[1] . "\r\n"; /* = */
97
	    else
98
		just_t1($t1);
99
	}
100
	elseif (($t2)||(($time2 + $precision) > $time1))
101
	    echo $t1 . ", " . $d2[1] . "\r\n";
102
	else
103
	    just_t1($t1);
104
    
105
	$t1 = get_string($f1);
106
	if (!$t1) break;
107
108
	$d1 = split (",", $t1, 2); $time1 = strtotime($d1[0]);
109
    }
110
111
    fclose($f1);
112
    if ($f2) fclose($f2);
113
114
    exit;
115
}
116
117
header("Content-Type: text/html; charset=UTF-8");
118
header("Cache-Control: no-cache, must-revalidate");
119
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
120
121
?>
122
<html>
123
<head>
124
</head>
125
<body><div><table>
126
<form action="csvmerge.php" enctype="multipart/form-data" method="POST">
127
  <tr><td>Master File (CSV): </td><td><input type="file" name="file1"/></td></tr>
128
  <tr><td>Complementary File (CSV): </td><td><input type="file" name="file2"/></td></tr>
129
  <tr><td>Precision (sec): </td><td><input name="precision" type="text" value="<?echo $default_precision;?>"/></td></tr>
130
  <tr><td colspan="2"><input type="submit" value="Merge"/></td></tr>
131
</form>
132
</table></div></body>
133
</html>
134
<?
135
136
137
138
139
?>