-
Notifications
You must be signed in to change notification settings - Fork 1
/
http-colors.pl
executable file
·303 lines (273 loc) · 8.65 KB
/
http-colors.pl
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
#!/usr/bin/env perl
use strict;
use warnings;
use HTTP::Daemon;
use HTTP::Status;
use MIME::Base64;
use CAM::PDF;
use CAM::PDF::PageText;
use constant HTTP_HOST => '127.0.0.1';
use constant HTTP_PORT => 8888;
use constant DEFAULT_SPEED => 18;
use constant APP_VER => '5';
my $html_header = <<HTML;
<!DOCTYPE html>
<html lang="bg">
<head>
<meta charset="utf-8">
<title>Обработка на цветове</title>
<meta name="description" content="The HTML5 Herald">
<meta name="author" content="Vladimir Borisov">
</head>
<body style="background-color:ButtonFace;">
HTML
my $html_frame = <<HTML;
<iframe src="about:blank" id="ifu" style="width:0;height:0;position:absolute;top:-1000px;"></iframe>
<script type="text/javascript">
(function(d) {
var ifu = document.getElementById('ifu');
ifu = (ifu.contentWindow) ? ifu.contentWindow : (ifu.contentDocument.document) ? ifu.contentDocument.document : ifu.contentDocument;
ifu.document.open();
ifu.document.write('<form id="upload-form" action="" method="post" enctype="multipart/form-data" target="_top"><input type="file" name="colors" id="colors"></form>');
ifu.document.close();
var colors = ifu.document.getElementById('colors');
colors.onchange = function() {
ifu.document.getElementById('upload-form').submit();
};
ifu.document.getElementById('upload-form').onsubmit = function() {
if(! colors.value) {
colors.click();
return false;
}
};
document.getElementById('feed').onclick = function(){
colors.click();
};
})(document);
</script>
HTML
my $html_footer = <<'HTML';
</body>
</html>
HTML
my %color_map = ('Black', 1, 'Cyan', 2, 'Magenta', 3, 'Yellow', 4);
#my %color_sp = ('Black', 0x0e, 'Cyan', 0x14, 'Magenta', 0x10, 'Yellow', 0x16);
my %color_sp = ('Black', 0x07, 'Cyan', 0x0a, 'Magenta', 0x8, 'Yellow', 0x0b);
my $d = HTTP::Daemon->new(LocalAddr => HTTP_HOST, LocalPort => HTTP_PORT, Reuse => 1);
die "can't setup server" unless $d;
print "Server started at ".$d->url."\n";
while (my $c = $d->accept) {
while (my $r = $c->get_request) {
# Upload form
unless($r->url->query or $r->method eq 'POST') {
my $h = <<HTML;
$html_header
<div style="text-align: center;">
<input type="submit" value="Качи PDF" id="feed" style="font-size: 2em;margin-top:1em;"/>
</div>
$html_frame
$html_footer
HTML
my $rs = new HTTP::Response;
$rs->header('Content-Type' => 'text/html');
$rs->content($h);
$c->send_response($rs);
$c->close;
goto LAST;
# Generateing the .ry4 file
} elsif($r->url->query) {
my $tra_la_la = '';
my %query = split /[=&]/, $r->url->query;
my %colors = (); # data for colors
my @cids = ();
my $colidx = ''; # index of colors
unless ($query{'f'}) {
$c->send_redirect("/");
goto LAST;
}
foreach (keys %query) {
next if /(-speed$|-index$|^f$)/;
my $key = lc $_;
unless ($query{$key.'-index'} and $query{$key.'-speed'}) {
$c->send_redirect("/");
goto LAST;
}
$colidx .= pack 'C', $color_map{$_} ? $color_map{$_} : $query{$key.'-index'};
$colors{$_} = $query{$_};
# Unescape the URL
$colors{$_} =~ s/\%([A-Fa-f0-9]{2})/pack('C', hex($1))/seg;
# Decode
$colors{$_} = decode_base64 $colors{$_};
# Test if valid
if (length $colors{$_} != 46) {
$c->send_redirect("/");
goto LAST;
}
# Add speed
$colors{$_} = pack 'a194ss', $colors{$_}, 0x0a, $query{$key.'-speed'} * 2;
push @cids, $_;
}
unless (@cids and ! ($colidx =~ /(.).*\1/)) {
$c->send_redirect("/");
goto LAST;
}
my $template = pack 'a16a4784a6a2', '000023 RAMP BAND', $colidx, 'V1.01','UU';
my $offset = 0x30;
my $step = 0xc6;
foreach (@cids) {
substr($template, $offset, $step) = $colors{$_};
$offset+=$step;
}
my $rs = new HTTP::Response;
$rs->header("Cache-Control" => "public");
$rs->header("Content-Description" => "File Transfer");
$rs->header("Content-Disposition" => "attachment; filename=$query{'f'}.ry4");
$rs->header("Content-Type" => "application/octet-stream");
$rs->header("Content-Transfer-Encoding" => "binary");
$rs->content($template);
$c->send_response($rs);
$c->close;
goto LAST;
}
# Parsing the PDF
my $input = $r->content;
my @secs = split /\r\n\r\n/, $input;
my %headers = (shift(@secs) =~ m/\n([^:]+): (.*)(?:\r|$)/g);
# Headers validation
unless ($headers{'Content-Type'} eq 'application/pdf' and $headers{'Content-Disposition'}) {
$c->send_redirect("/");
goto LAST;
}
# File validation
my $doc;
unless ($doc = CAM::PDF->new(shift(@secs)))
{
$c->send_redirect("/");
goto LAST;
}
(my $filename) = ($headers{'Content-Disposition'} =~ m/filename="(.+).pdf"/i);
my $html_body = '';
my $color_index = 5;
foreach my $p ( 2 .. $doc->numPages()) {
my $str = $doc->getPageText($p);
my @row = split /\n/, $str;
(my $color) = ($row[0] =~ /- (.*?) Layer/);
my @cols;
my $values = '';
my $max = 0;
foreach (26 .. 48) {
my $val = $row[$_]*4;
push @cols, $val;
$max = $val unless $max > $val;
$values .= pack 's', $val;
}
# Na nula ne se deli, taka che ni trqbva neshto blizko
$max = .0000000000000000000000000001 unless ($max);
$values = encode_base64 $values;
chop $values;
my $checked = defined $color_map{$color} ? ' checked=checked' : '';
$html_body .= <<HTML;
<tr>
<td><input type="checkbox"$checked name="$color" id="$color" value="$values"/><label unselectable="on" for="$color">$color</label></td>
<td><table style="display:inline-table;background-color:lightgray;height:1.3em;"><tr>
HTML
$html_body .= sprintf <<'HTML', $_/4, $_, defined $color_map{$color} ? $color : 'darkgray', ($_ / $max) foreach (@cols);
<td style="vertical-align:bottom;height=1em;" title="%d, (hex*4 0x%04x)">
<div style="cursor:default;background-color:%s;height:%fem"> </div>
</td>
HTML
$html_body .= sprintf <<'HTML', lc $color, $color_sp{$color} ? $color_sp{$color} : DEFAULT_SPEED;
</tr></table>
</td>
<td>
<label unselectable="on" for="%s-speed">Скорост</label>
<input type="number" size="6" id="%1$s-speed" name="%1$s-speed" value="%d"/>
</td>
HTML
$html_body .= sprintf <<'HTML', lc $color, $color_map{$color} ? ' readonly="readonly"' : '', $color_map{$color} ? $color_map{$color} : $color_index++;
<td>
<label unselectable="on" for="%s-index">Индекс</label>
<input type="number" size="6" id="%1$s-index" name="%1$s-index"%s value="%d"/>
</td>
</tr>
HTML
}
$c->send_redirect("/") and goto LAST unless $html_body;
my $h = sprintf <<'HTML', $html_header, $filename, $html_body, APP_VER, $html_frame, $html_footer;
%s
<style>
label {
-webkit-touch-callout: none;
-webkit-user-select: none;
-khtml-user-select: none;
-moz-user-select: none;
-ms-user-select: none;
user-select: none;
}
[readonly="readonly"], [readonly] {
color: gray;
}
</style>
<form method="get" action="" id="upload-form">
<input type="hidden" name="f" value="%s"/>
<table style="margin: 2em auto;background-color: ButtonHighlight; box-shadow: 0 0 1em;">
<tr>
<td colspan="4">
<h1 style="text-align: center;">%2$s.pdf</h1><hr/>
</td>
</tr>
%s
<tr>
<td colspan="4">
<table style="width: 100%%;"><tr>
<td width="50%%">
<button type="button" style="font-size:1.2em;width: 100%%;" id="feed">Качи PDF</button>
</td><td width="50%%">
<button type="submit" style="font-size:1.2em; width: 100%%;">Изтегли</button>
</td>
</tr></table>
</td>
</tr>
<tr>
<td colspan="4" style="text-align: right;"><small>вер. %s</small></td>
</tr>
</table>
</form>
%s
<script type="text/javascript">
(function(d, foreach) {
if (! d.querySelectorAll || ! foreach) return;
var maxchecked = 6
, totalchecked = 0;
foreach.call(d.querySelectorAll('input[type=checkbox]'), function(el) {
totalchecked += el.checked;
el.onclick = function() {
totalchecked += el.checked ? 1 : -1;
foreach.call(d.querySelectorAll('input[type=checkbox]:not(:checked)'), function(en) {
en.disabled = totalchecked >= maxchecked && ! en.checked;
});
}
});
if(typeof(Storage)==="undefined") return;
foreach.call(d.querySelectorAll('input[type=number][id$=speed]'), function(el) {
if(typeof localStorage[el.id] !== 'undefined') el.value = localStorage[el.id];
});
d.getElementById('upload-form').onsubmit = function() {
foreach.call(d.querySelectorAll('input[type=number][id$=speed]'), function(el) {
localStorage[el.id] = el.value;
});
}
})(document, Array.prototype.forEach);
</script>
%s
HTML
my $rs = new HTTP::Response;
$rs->header('Content-Type' => 'text/html');
$rs->content($h);
$c->send_response($rs);
goto LAST;
}
LAST:
$c->close;
undef($c);
}