-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.php
207 lines (179 loc) · 5.3 KB
/
index.php
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
<?php
/*
* PHP QR Code encoder
*
* Exemplatory usage
*
* PHP QR Code is distributed under LGPL 3
* Copyright (C) 2010 Dominik Dzienia <deltalab at poczta dot fm>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
// Convert AAA -> 1, etc.
function numberToAlphabet($n) {
// start at AAA for 1
$n = $n + 701;//25 + 26 * 26;
$r = '';
for ($i = 1; $n >= 0 && $i < 10; $i++) {
$r = chr(0x41 + ($n % pow(26, $i) / pow(26, $i - 1))) . $r;
$n -= pow(26, $i);
}
return $r;
}
// Convert 1 -> AAA, etc.
function alphabetToNumber($a) {
$r = 0;
$l = strlen($a);
for ($i = 0; $i < $l; $i++) {
$r += pow(26, $i) * (ord($a[$l - $i - 1]) - 0x40);
}
// expect AAA to be 1
return $r - 702;//-(26 + 26 * 26);
}
// items per page (10 x 10 = 100)
$ipp = 100;
// items per line
$ipl = 10;
function renderCodes($page = 1, $showCodes = false) {
global $ipp;
//set it to writable location, a place for temp generated PNG files
$PNG_TEMP_DIR = dirname(__FILE__).DIRECTORY_SEPARATOR.'temp'.DIRECTORY_SEPARATOR;
//html PNG location prefix
$PNG_WEB_DIR = 'temp/';
include "qrlib.php";
//ofcourse we need rights to create temp dir
if (!file_exists($PNG_TEMP_DIR))
mkdir($PNG_TEMP_DIR);
$filename = $PNG_TEMP_DIR.'error.png';
//processing form input
//remember to sanitize user input in real-life solution !!!
//$errorCorrectionLevel = 'L';
$errorCorrectionLevel = 'H';
if (isset($_GET['level']) && in_array($_GET['level'], array('L','M','Q','H')))
$errorCorrectionLevel = $_GET['level'];
//$matrixPointSize = 4;
$matrixPointSize = 7;
$from = ($page-1)*$ipp+1;
$to = $page*$ipp;
$qrDataArray = array();
for($i = $from; $i <= $to; $i++) {
array_push($qrDataArray, numberToAlphabet($i));
//echo "<br/>... $i";
}
foreach ($qrDataArray as &$qr) {
if ($qr != numberToAlphabet(alphabetToNumber($qr)) || $qr == "") {
continue;
}
// user data
$qrData = "http://flipdot.org/".$qr;
//$qrData = $qr;
//$filename = $PNG_TEMP_DIR.'test'.md5($qr.'|'.$errorCorrectionLevel.'|'.$matrixPointSize).'.png';
$filename = $PNG_TEMP_DIR.$qr.'.png';
if (!file_exists($filename))
QRcode::png($qrData, $filename, $errorCorrectionLevel, $matrixPointSize, 2);
//display generated file
if ($showCodes)
echo '<p><img src="'.$PNG_WEB_DIR.basename($filename).'" /></p>';
}
echo "QR-Codes wurden erstellt.<br>Hier gehts zur druckbaren PDF:<br>";
echo "<form action=\"".htmlspecialchars($_SERVER["PHP_SELF"], ENT_QUOTES, "utf-8")."\" method='post'>
<input type='hidden' name='pdf' value='$page'>
<input type='submit' name='submit' value='Drucken'>
</form>";
}
function render_form () {
echo "<h2>QR-Codes drucken</h2>
<form action=\"".htmlspecialchars($_SERVER["PHP_SELF"], ENT_QUOTES, "utf-8")."\" method='post'>
<label for='pagenum'>Seitennummer:</label><input type='text' name='pagenum'></input>
<input type='submit' name='submit' value='Druckvorlage'><br>
<i>Es kann einige Sekunden dauern, bis die Druckvorlage erstellt wurde.</i><br>
</form>";
}
function renderPDF($page = 1) {
global $ipp;
global $ipl;
require_once 'fpdf/fpdf.php';
$pdf = new FPDF("P", "mm", "A4");
$pdf->AddPage();
$from = ($page-1)*$ipp+1;
$to = $page*$ipp;
// in pixels
//$w = 248;
//$h = 351;
// in mm
$w = 21;
$h = 29.7;
for($i = $from; $i <= $to; $i++) {
$x = ($i-1)%$ipl;
$y = floor(($i-$from)/$ipl);
//$pdf->Image('temp/'.numberToAlphabet($i).'.png', 5, 70, 33.78);
$pdf->Image('temp/'.numberToAlphabet($i).'.png', $x*$w, $y*$h, $w, $h);
}
$pdf->Output();
}
function renderHeader() {
echo <<<END
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN">
<html>
<head>
<title>QR Code Generator</title>
<style type="text/css">
/** {
padding: 0!important;
margin: 0!important;
}
p {
float: left;
width: 248px;
height: 351px;
}*/
</style>
</head>
<body>
END;
}
function renderFooter() {
echo <<<END
</body>
</html>
END;
}
$pagenum = false;
if (isset($_GET['data'])) {
// ze trimmage
if (trim($_GET['data']) == '')
die('Data cannot be empty.');
if ($_GET['data'] != (int)$_GET['data'])
die('Page number must be... yeah you guessed it... a number, duh.');
$pagenum = $_GET['data'];
} else if ($_POST['pagenum'] == (int)$_POST['pagenum']) {
$pagenum = $_POST['pagenum'];
}
if($_POST['pdf']) {
renderPDF($_POST['pdf']);
} else if(!$pagenum) {
renderHeader();
render_form();
renderFooter();
} else if ($pagenum > 0) {
renderHeader();
renderCodes($pagenum);
renderFooter();
} else {
renderHeader();
echo "Bitte eine Nummer eingeben...";
renderFooter();
}
?>