Refactor code structure for improved readability and maintainability

This commit is contained in:
2025-04-10 19:23:03 +07:00
parent e4705579f0
commit 0700a715e4
5693 changed files with 857475 additions and 0 deletions

165
lib/mpdf/qrcode/_LGPL.txt Normal file
View File

@ -0,0 +1,165 @@
GNU LESSER GENERAL PUBLIC LICENSE
Version 3, 29 June 2007
Copyright (C) 2007 Free Software Foundation, Inc. <http://fsf.org/>
Everyone is permitted to copy and distribute verbatim copies
of this license document, but changing it is not allowed.
This version of the GNU Lesser General Public License incorporates
the terms and conditions of version 3 of the GNU General Public
License, supplemented by the additional permissions listed below.
0. Additional Definitions.
As used herein, "this License" refers to version 3 of the GNU Lesser
General Public License, and the "GNU GPL" refers to version 3 of the GNU
General Public License.
"The Library" refers to a covered work governed by this License,
other than an Application or a Combined Work as defined below.
An "Application" is any work that makes use of an interface provided
by the Library, but which is not otherwise based on the Library.
Defining a subclass of a class defined by the Library is deemed a mode
of using an interface provided by the Library.
A "Combined Work" is a work produced by combining or linking an
Application with the Library. The particular version of the Library
with which the Combined Work was made is also called the "Linked
Version".
The "Minimal Corresponding Source" for a Combined Work means the
Corresponding Source for the Combined Work, excluding any source code
for portions of the Combined Work that, considered in isolation, are
based on the Application, and not on the Linked Version.
The "Corresponding Application Code" for a Combined Work means the
object code and/or source code for the Application, including any data
and utility programs needed for reproducing the Combined Work from the
Application, but excluding the System Libraries of the Combined Work.
1. Exception to Section 3 of the GNU GPL.
You may convey a covered work under sections 3 and 4 of this License
without being bound by section 3 of the GNU GPL.
2. Conveying Modified Versions.
If you modify a copy of the Library, and, in your modifications, a
facility refers to a function or data to be supplied by an Application
that uses the facility (other than as an argument passed when the
facility is invoked), then you may convey a copy of the modified
version:
a) under this License, provided that you make a good faith effort to
ensure that, in the event an Application does not supply the
function or data, the facility still operates, and performs
whatever part of its purpose remains meaningful, or
b) under the GNU GPL, with none of the additional permissions of
this License applicable to that copy.
3. Object Code Incorporating Material from Library Header Files.
The object code form of an Application may incorporate material from
a header file that is part of the Library. You may convey such object
code under terms of your choice, provided that, if the incorporated
material is not limited to numerical parameters, data structure
layouts and accessors, or small macros, inline functions and templates
(ten or fewer lines in length), you do both of the following:
a) Give prominent notice with each copy of the object code that the
Library is used in it and that the Library and its use are
covered by this License.
b) Accompany the object code with a copy of the GNU GPL and this license
document.
4. Combined Works.
You may convey a Combined Work under terms of your choice that,
taken together, effectively do not restrict modification of the
portions of the Library contained in the Combined Work and reverse
engineering for debugging such modifications, if you also do each of
the following:
a) Give prominent notice with each copy of the Combined Work that
the Library is used in it and that the Library and its use are
covered by this License.
b) Accompany the Combined Work with a copy of the GNU GPL and this license
document.
c) For a Combined Work that displays copyright notices during
execution, include the copyright notice for the Library among
these notices, as well as a reference directing the user to the
copies of the GNU GPL and this license document.
d) Do one of the following:
0) Convey the Minimal Corresponding Source under the terms of this
License, and the Corresponding Application Code in a form
suitable for, and under terms that permit, the user to
recombine or relink the Application with a modified version of
the Linked Version to produce a modified Combined Work, in the
manner specified by section 6 of the GNU GPL for conveying
Corresponding Source.
1) Use a suitable shared library mechanism for linking with the
Library. A suitable mechanism is one that (a) uses at run time
a copy of the Library already present on the user's computer
system, and (b) will operate properly with a modified version
of the Library that is interface-compatible with the Linked
Version.
e) Provide Installation Information, but only if you would otherwise
be required to provide such information under section 6 of the
GNU GPL, and only to the extent that such information is
necessary to install and execute a modified version of the
Combined Work produced by recombining or relinking the
Application with a modified version of the Linked Version. (If
you use option 4d0, the Installation Information must accompany
the Minimal Corresponding Source and Corresponding Application
Code. If you use option 4d1, you must provide the Installation
Information in the manner specified by section 6 of the GNU GPL
for conveying Corresponding Source.)
5. Combined Libraries.
You may place library facilities that are a work based on the
Library side by side in a single library together with other library
facilities that are not Applications and are not covered by this
License, and convey such a combined library under terms of your
choice, if you do both of the following:
a) Accompany the combined library with a copy of the same work based
on the Library, uncombined with any other library facilities,
conveyed under the terms of this License.
b) Give prominent notice with the combined library that part of it
is a work based on the Library, and explaining where to find the
accompanying uncombined form of the same work.
6. Revised Versions of the GNU Lesser General Public License.
The Free Software Foundation may publish revised and/or new versions
of the GNU Lesser General Public License from time to time. Such new
versions will be similar in spirit to the present version, but may
differ in detail to address new problems or concerns.
Each version is given a distinguishing version number. If the
Library as you received it specifies that a certain numbered version
of the GNU Lesser General Public License "or any later version"
applies to it, you have the option of following the terms and
conditions either of that published version or of any later version
published by the Free Software Foundation. If the Library as you
received it does not specify a version number of the GNU Lesser
General Public License, you may choose any version of the GNU Lesser
General Public License ever published by the Free Software Foundation.
If the Library as you received it specifies that a proxy can decide
whether future versions of the GNU Lesser General Public License shall
apply, that proxy's public statement of acceptance of any version is
permanent authorization for you to choose that version for the
Library.

View File

@ -0,0 +1,49 @@
*******************************************************
** Ce programme est distribu<62> sous la licence LGPL, **
** reportez-vous au fichier _LGPL.txt ou <20> **
** http://www.gnu.org/licenses/lgpl.html **
** pour en savoir plus. **
** **
** Copyright 2000-2010 par Laurent Minguet **
*******************************************************
**************************
* QRCode generator v0.99 *
**************************
Utilisation :
------------
- necessite PHP5
- regardez le fichier index.php pour voir le fonctionnement.
- voici un exemple :
require_once('qrcode.class.php');
$qrcode = new QRcode('your message here', 'H'); // error level : L, M, Q, H
$qrcode->displayPNG();
- il existe la possibilit<69> de d<>sactiver le border et la zone de s<>curit<69> blanche entourant le code en utilisant :
$qrcode->disableBorder();
- il existe <20>galement une methode de sortie sp<73>cifique pour FPDF :
$qrcode->displayFPDF($fpdf, $x, $y, $w, $background, $color);
- il existe <20>galement une methode de sortie sp<73>cifique pour cr<63>er un tableau HTML :
$qrcode->displayHTML();
Informations :
-------------
Generateur de QRCode
(QR Code is registered trademark of DENSO WAVE INCORPORATED | http://www.denso-wave.com/qrcode/)
Fortement inspir<69> de "QRcode image PHP scripts version 0.50g (C)2000-2005,Y.Swetake"
Programmation en PHP5
Programmeur : Spipu
email : webmaster@spipu.net
site : http://prgm.spipu.net/php_qrcode
Remerciement :
-------------
* Y.Swetake pour "QRcode image PHP scripts version 0.50g"
* Josep S. pour l'ajout de displayPNG

14
lib/mpdf/qrcode/image.php Normal file
View File

@ -0,0 +1,14 @@
<?php
$msg = isset($_GET['msg']) ? $_GET['msg'] : '';
if (!$msg) $msg = "Le site du spipu\r\nhttp://spipu.net/";
$err = isset($_GET['err']) ? $_GET['err'] : '';
if (!in_array($err, array('L', 'M', 'Q', 'H'))) $err = 'L';
require_once('qrcode.class.php');
$qrcode = new QRcode(utf8_encode($msg), $err);
$qrcode->disableBorder();
$qrcode->displayPNG(200);
?>

68
lib/mpdf/qrcode/index.php Normal file
View File

@ -0,0 +1,68 @@
<?php
$msg = isset($_GET['msg']) ? $_GET['msg'] : '';
if (!$msg) $msg = "Le site du spipu\r\nhttp://spipu.net/";
$err = isset($_GET['err']) ? $_GET['err'] : '';
if (!in_array($err, array('L', 'M', 'Q', 'H'))) $err = 'L';
require_once('qrcode.class.php');
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Spipu Qrcode <?php echo __CLASS_QRCODE__; ?></title>
<meta name="Title" content="Spipu - Qrcode <?php echo __CLASS_QRCODE__; ?>" >
<meta name="Description" content="Spipu - Qrcode <?php echo __CLASS_QRCODE__; ?>" >
<meta name="Keywords" content="spipu">
<meta name="Author" content="spipu" >
<meta name="Reply-to" content="webmaster@spipu.net" >
<meta name="Copyright" content="(c)2009 Spipu" >
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252" >
<style type="text/css">
<!--
table.qr
{
border-collapse: collapse;
border: solid 1px black;
table-layout: fixed;
}
table.qr td
{
width: 5px;
height: 5px;
font-size: 2px;
}
table.qr td.on
{
background: #000000;
}
-->
</style>
</head>
<body>
<center>
<form method="GET" action="">
<textarea name="msg" cols="40" rows="7"><?php echo htmlentities($msg); ?></textarea><br>
Correction d'erreur :
<select name="err">
<option value="L" <?php echo $err=='L' ? 'selected' : ''; ?>>L</option>
<option value="M" <?php echo $err=='M' ? 'selected' : ''; ?>>M</option>
<option value="Q" <?php echo $err=='Q' ? 'selected' : ''; ?>>Q</option>
<option value="H" <?php echo $err=='H' ? 'selected' : ''; ?>>H</option>
</select> |
<input type="submit" value="Afficher">
</form>
<hr>
G<>n<EFBFBD>ration d'un tableau HTML :<br>
<?php
$qrcode = new QRcode(utf8_encode($msg), $err);
$qrcode->displayHTML();
?>
<br>
G<>n<EFBFBD>ration d'une image PNG : <br>
<img src="./image.php?msg=<?php echo urlencode($msg); ?>&amp;err=<?php echo urlencode($err); ?>" alt="generation qr-code" style="border: solid 1px black;">
</center>
</body>
</html>

View File

@ -0,0 +1,674 @@
<?php
/*
* Generateur de QRCode
* (QR Code is registered trademark of DENSO WAVE INCORPORATED | http://www.denso-wave.com/qrcode/)
* Fortement inspir<69> de "QRcode image PHP scripts version 0.50g (C)2000-2005,Y.Swetake"
*
* Distribu<62> sous la licence LGPL.
*
* @author Laurent MINGUET <webmaster@spipu.net>
* @version 0.99
*/
if (!defined('__CLASS_QRCODE__'))
{
define('__CLASS_QRCODE__', '0.99');
class QRcode
{
private $version_mx = 40; // numero de version maximal autoris<69>
private $type = 'bin'; // type de donn<6E>e
private $level = 'L'; // ECC
private $value = ''; // valeur a encoder
private $length = 0; // taille de la valeur
private $version = 0; // version
private $size = 0; // dimension de la zone data
private $qr_size = 0; // dimension du QRcode
private $data_bit = array(); // nb de bit de chacune des valeurs
private $data_val = array(); // liste des valeurs de bit diff<66>rents
private $data_word = array(); // liste des valeurs tout ramen<65> <20> 8bit
private $data_cur = 0; // position courante
private $data_num = 0; // position de la dimension
private $data_bits = 0; // nom de bit au total
private $max_data_bit = 0; // lilmite de nombre de bit maximal pour les datas
private $max_data_word = 0; // lilmite de nombre de mot maximal pour les datas
private $max_word = 0; // lilmite de nombre de mot maximal en global
private $ec = 0;
private $matrix = array();
private $matrix_remain = 0;
private $matrix_x_array = array();
private $matrix_y_array = array();
private $mask_array = array();
private $format_information_x1 = array();
private $format_information_y1 = array();
private $format_information_x2 = array();
private $format_information_y2 = array();
private $rs_block_order = array();
private $rs_ecc_codewords = 0;
private $byte_num = 0;
private $final = array();
private $disable_border = false;
/**
* Constructeur
*
* @param string message a encoder
* @param string niveau de correction d'erreur (ECC) : L, M, Q, H
* @return null
*/
public function __construct($value, $level='L')
{
if (!in_array($level, array('L', 'M', 'Q', 'H')))
$this->ERROR('ECC non reconnu : L, M, Q, H');
$this->length = strlen($value);
if (!$this->length)
$this->ERROR('pas de data...');
$this->level = $level;
$this->value = &$value;
$this->data_bit = array();
$this->data_val = array();
$this->data_cur = 0;
$this->data_bits= 0;
$this->encode();
$this->loadECC();
$this->makeECC();
$this->makeMatrix();
}
/**
* permet de recuperer la taille du QRcode (le nombre de case de c<>t<EFBFBD>)
*
* @return int size of qrcode
*/
public function getQrSize()
{
if ($this->disable_border)
return $this->qr_size-8;
else
return $this->qr_size;
}
public function disableBorder()
{
$this->disable_border = true;
}
/**
* permet d'afficher le QRcode dans un pdf via FPDF
*
* @param FPDF objet fpdf
* @param float position X
* @param float position Y
* @param float taille du qrcode
* @param array couleur du background (R,V,B)
* @param array couleur des cases et du border (R,V,B)
* @return boolean true;
*/
public function displayFPDF(&$fpdf, $x, $y, $w, $background=array(255,255,255), $color=array(0,0,0))
{
$size = $w;
$s = $size/$this->getQrSize();
$fpdf->SetDrawColor($color[0], $color[1], $color[2]);
$fpdf->SetFillColor($background[0], $background[1], $background[2]);
// rectangle de fond
if ($this->disable_border)
{
$s_min = 4;
$s_max = $this->qr_size-4;
$fpdf->Rect($x, $y, $size, $size, 'F');
}
else
{
$s_min = 0;
$s_max = $this->qr_size;
$fpdf->Rect($x, $y, $size, $size, 'FD');
}
$fpdf->SetFillColor($color[0], $color[1], $color[2]);
for($j=$s_min; $j<$s_max; $j++)
for($i=$s_min; $i<$s_max; $i++)
if ($this->final[$i + $j*$this->qr_size+1])
$fpdf->Rect($x+($i-$s_min)*$s, $y+($j-$s_min)*$s, $s, $s, 'F');
return true;
}
/**
* permet d'afficher le QRcode au format HTML, <20> utiliser avec un style CSS
*
* @return boolean true;
*/
public function displayHTML()
{
if ($this->disable_border)
{
$s_min = 4;
$s_max = $this->qr_size-4;
}
else
{
$s_min = 0;
$s_max = $this->qr_size;
}
echo '<table class="qr" cellpadding="0" cellspacing="0">'."\n";
for($y=$s_min; $y<$s_max; $y++)
{
echo '<tr>';
for($x=$s_min; $x<$s_max; $x++)
{
echo '<td class="'.($this->final[$x + $y*$this->qr_size+1] ? 'on' : 'off').'"></td>';
}
echo '</tr>'."\n";
}
echo '</table>';
return true;
}
/*
* permet d'obtenir une image PNG
*
* @param float taille du qrcode
* @param array couleur du background (R,V,B)
* @param array couleur des cases et du border (R,V,B)
* @param string nom du fichier de sortie. si null : sortie directe
* @param integer qualit<69> de 0 (aucune compression) a 9
* @return boolean true;
*/
public function displayPNG($w=100, $background=array(255,255,255), $color=array(0,0,0), $filename = null, $quality = 0)
{
if ($this->disable_border)
{
$s_min = 4;
$s_max = $this->qr_size-4;
}
else
{
$s_min = 0;
$s_max = $this->qr_size;
}
$size = $w;
$s = $size/($s_max-$s_min);
// rectangle de fond
$im = imagecreatetruecolor($size,$size);
$c_case = imagecolorallocate($im,$color[0],$color[1],$color[2]);
$c_back = imagecolorallocate($im,$background[0],$background[1],$background[2]);
imagefilledrectangle($im,0,0,$size,$size,$c_back);
for($j=$s_min; $j<$s_max; $j++)
for($i=$s_min; $i<$s_max; $i++)
if ($this->final[$i + $j*$this->qr_size+1])
imagefilledrectangle($im,($i-$s_min)*$s,($j-$s_min)*$s,($i-$s_min+1)*$s-1,($j-$s_min+1)*$s-1,$c_case);
if ($filename)
{
imagepng($im, $filename, $quality);
}
else
{
header("Content-type: image/png");
imagepng($im);
}
imagedestroy($im);
return true;
}
private function ERROR($msg)
{
echo 'ERROR : '.$msg;
exit;
}
private function addData($val, $bit, $next = true)
{
$this->data_val[$this->data_cur] = $val;
$this->data_bit[$this->data_cur] = $bit;
if ($next)
{
$this->data_cur++;
return $this->data_cur-1;
}
else
{
return $this->data_cur;
}
}
private function encode()
{
// conversion des datas
if (preg_match('/[^0-9]/',$this->value))
{
if (preg_match('/[^0-9A-Z \$\*\%\+\-\.\/\:]/',$this->value))
{
// type : bin
$this->type = 'bin';
$this->addData(4, 4);
// taille. il faut garder l'indice, car besoin de correction
$this->data_num = $this->addData($this->length, 8); /* #version 1-9 */
$data_num_correction=array(0,0,0,0,0,0,0,0,0,0,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8);
// datas
for ($i=0; $i<$this->length; $i++)
$this->addData(ord(substr($this->value, $i, 1)), 8);
}
else
{
// type : alphanum
$this->type = 'alphanum';
$this->addData(2, 4);
// taille. il faut garder l'indice, car besoin de correction
$this->data_num = $this->addData($this->length, 9); /* #version 1-9 */
$data_num_correction=array(0,0,0,0,0,0,0,0,0,0,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,4,4,4,4,4,4,4,4,4,4,4,4,4,4);
// datas
$an_hash=array(
'0'=>0,'1'=>1,'2'=>2,'3'=>3,'4'=>4,'5'=>5,'6'=>6,'7'=>7,'8'=>8,'9'=>9,
'A'=>10,'B'=>11,'C'=>12,'D'=>13,'E'=>14,'F'=>15,'G'=>16,'H'=>17,'I'=>18,'J'=>19,'K'=>20,'L'=>21,'M'=>22,
'N'=>23,'O'=>24,'P'=>25,'Q'=>26,'R'=>27,'S'=>28,'T'=>29,'U'=>30,'V'=>31,'W'=>32,'X'=>33,'Y'=>34,'Z'=>35,
' '=>36,'$'=>37,'%'=>38,'*'=>39,'+'=>40,'-'=>41,'.'=>42,'/'=>43,':'=>44);
for ($i=0; $i<$this->length; $i++)
{
if (($i %2)==0)
$this->addData($an_hash[substr($this->value,$i,1)], 6, false);
else
$this->addData($this->data_val[$this->data_cur]*45+$an_hash[substr($this->value,$i,1)], 11, true);
}
unset($an_hash);
if (isset($this->data_bit[$this->data_cur]))
$this->data_cur++;
}
}
else
{
// type : num
$this->type = 'num';
$this->addData(1, 4);
//taille. il faut garder l'indice, car besoin de correction
$this->data_num = $this->addData($this->length, 10); /* #version 1-9 */
$data_num_correction=array(0,0,0,0,0,0,0,0,0,0,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,4,4,4,4,4,4,4,4,4,4,4,4,4,4);
// datas
for ($i=0; $i<$this->length; $i++)
{
if (($i % 3)==0)
$this->addData(substr($this->value,$i,1), 4, false);
else if (($i % 3)==1)
$this->addData($this->data_val[$this->data_cur]*10+substr($this->value,$i,1), 7, false);
else
$this->addData($this->data_val[$this->data_cur]*10+substr($this->value,$i,1), 10);
}
if (isset($this->data_bit[$this->data_cur]))
$this->data_cur++;
}
// calcul du nombre de bits
$this->data_bits=0;
foreach($this->data_bit as $bit)
$this->data_bits+= $bit;
// code ECC
$ec_hash = array('L'=>1, 'M'=>0, 'Q'=>3, 'H'=>2);
$this->ec = $ec_hash[$this->level];
// tableau de taille limite de bits
$max_bits = array(
0,128,224,352,512,688,864,992,1232,1456,1728,2032,2320,2672,2920,3320,3624,4056,4504,5016,5352,
5712,6256,6880,7312,8000,8496,9024,9544,10136,10984,11640,12328,13048,13800,14496,15312,15936,16816,17728,18672,
152,272,440,640,864,1088,1248,1552,1856,2192,2592,2960,3424,3688,4184,4712,5176,5768,6360,6888,
7456,8048,8752,9392,10208,10960,11744,12248,13048,13880,14744,15640,16568,17528,18448,19472,20528,21616,22496,23648,
72,128,208,288,368,480,528,688,800,976,1120,1264,1440,1576,1784,2024,2264,2504,2728,3080,
3248,3536,3712,4112,4304,4768,5024,5288,5608,5960,6344,6760,7208,7688,7888,8432,8768,9136,9776,10208,
104,176,272,384,496,608,704,880,1056,1232,1440,1648,1952,2088,2360,2600,2936,3176,3560,3880,
4096,4544,4912,5312,5744,6032,6464,6968,7288,7880,8264,8920,9368,9848,10288,10832,11408,12016,12656,13328
);
// determination automatique de la version necessaire
$this->version=1;
$i=1+40*$this->ec;
$j=$i+39;
while ($i<=$j)
{
if ($max_bits[$i]>=$this->data_bits+$data_num_correction[$this->version])
{
$this->max_data_bit=$max_bits[$i];
break;
}
$i++;
$this->version++;
}
// verification max version
if ($this->version>$this->version_mx)
$this->ERROR('too large version.');
// correctif sur le nombre de bits du strlen de la valeur
$this->data_bits+=$data_num_correction[$this->version];
$this->data_bit[$this->data_num]+=$data_num_correction[$this->version];
$this->max_data_word = ($this->max_data_bit/8);
// nombre de mots maximal
$max_words_array=array(0,26,44,70,100,134,172,196,242,292,346,404,466,532,581,655,733,815,901,991,1085,1156,
1258,1364,1474,1588,1706,1828,1921,2051,2185,2323,2465,2611,2761,2876,3034,3196,3362,3532,3706);
$this->max_word = $max_words_array[$this->version];
$this->size = 17 + 4*$this->version;
// nettoyages divers
unset($max_bits);
unset($data_num_correction);
unset($max_words_array);
unset($ec_hash);
// terminator
if ($this->data_bits<=$this->max_data_bit-4)
$this->addData(0, 4);
elseif ($this->data_bits<$this->max_data_bit)
$this->addData(0, $this->max_data_bit-$this->data_bits);
elseif ($this->data_bits>$this->max_data_bit)
$this->ERROR('Overflow error');
// construction des mots de 8 bit
$this->data_word = array();
$this->data_word[0] = 0;
$nb_word = 0;
$remaining_bit=8;
for($i=0; $i<$this->data_cur; $i++)
{
$buffer_val=$this->data_val[$i];
$buffer_bit=$this->data_bit[$i];
$flag = true;
while ($flag)
{
if ($remaining_bit>$buffer_bit)
{
$this->data_word[$nb_word]=((@$this->data_word[$nb_word]<<$buffer_bit) | $buffer_val);
$remaining_bit-=$buffer_bit;
$flag=false;
}
else
{
$buffer_bit-=$remaining_bit;
$this->data_word[$nb_word]=((@$this->data_word[$nb_word] << $remaining_bit) | ($buffer_val >> $buffer_bit));
$nb_word++;
if ($buffer_bit==0)
$flag=false;
else
$buffer_val= ($buffer_val & ((1 << $buffer_bit)-1) );
if ($nb_word<$this->max_data_word-1)
$this->data_word[$nb_word]=0;
$remaining_bit=8;
}
}
}
// completion du dernier mot si incomplet
if ($remaining_bit<8)
$this->data_word[$nb_word]=$this->data_word[$nb_word] << $remaining_bit;
else
$nb_word--;
// remplissage du reste
if ($nb_word<$this->max_data_word-1)
{
$flag=true;
while ($nb_word<$this->max_data_word-1)
{
$nb_word++;
if ($flag)
$this->data_word[$nb_word]=236;
else
$this->data_word[$nb_word]=17;
$flag=!$flag;
}
}
}
private function loadECC()
{
$matrix_remain_bit=array(0,0,7,7,7,7,7,0,0,0,0,0,0,0,3,3,3,3,3,3,3,4,4,4,4,4,4,4,3,3,3,3,3,3,3,0,0,0,0,0,0);
$this->matrix_remain = $matrix_remain_bit[$this->version];
unset($matrix_remain_bit);
// lecture du fichier : data file of geometry & mask for version V ,ecc level N
$this->byte_num = $this->matrix_remain+ 8*$this->max_word;
$filename = dirname(__FILE__)."/data/qrv".$this->version."_".$this->ec.".dat";
$fp1 = fopen ($filename, "rb");
$this->matrix_x_array = unpack("C*", fread($fp1,$this->byte_num));
$this->matrix_y_array = unpack("C*", fread($fp1,$this->byte_num));
$this->mask_array = unpack("C*", fread($fp1,$this->byte_num));
$this->format_information_x2 = unpack("C*", fread($fp1,15));
$this->format_information_y2 = unpack("C*", fread($fp1,15));
$this->rs_ecc_codewords = ord(fread($fp1,1));
$this->rs_block_order = unpack("C*", fread($fp1,128));
fclose($fp1);
$this->format_information_x1 = array(0,1,2,3,4,5,7,8,8,8,8,8,8,8,8);
$this->format_information_y1 = array(8,8,8,8,8,8,8,8,7,5,4,3,2,1,0);
}
private function makeECC()
{
// lecture du fichier : data file of caluclatin tables for RS encoding
$rs_cal_table_array = array();
$filename = dirname(__FILE__)."/data/rsc".$this->rs_ecc_codewords.".dat";
$fp0 = fopen ($filename, "rb");
for($i=0; $i<256; $i++)
$rs_cal_table_array[$i]=fread ($fp0,$this->rs_ecc_codewords);
fclose ($fp0);
$max_data_codewords = count($this->data_word);
// preparation
$j=0;
$rs_block_number=0;
$rs_temp[0]="";
for($i=0; $i<$max_data_codewords; $i++)
{
$rs_temp[$rs_block_number].=chr($this->data_word[$i]);
$j++;
if ($j>=$this->rs_block_order[$rs_block_number+1]-$this->rs_ecc_codewords)
{
$j=0;
$rs_block_number++;
$rs_temp[$rs_block_number]="";
}
}
// make
$rs_block_order_num=count($this->rs_block_order);
for($rs_block_number=0; $rs_block_number<$rs_block_order_num; $rs_block_number++)
{
$rs_codewords=$this->rs_block_order[$rs_block_number+1];
$rs_data_codewords=$rs_codewords-$this->rs_ecc_codewords;
$rstemp=$rs_temp[$rs_block_number].str_repeat(chr(0),$this->rs_ecc_codewords);
$padding_data=str_repeat(chr(0),$rs_data_codewords);
$j=$rs_data_codewords;
while($j>0)
{
$first=ord(substr($rstemp,0,1));
if ($first)
{
$left_chr=substr($rstemp,1);
$cal=$rs_cal_table_array[$first].$padding_data;
$rstemp=$left_chr ^ $cal;
}
else
$rstemp=substr($rstemp,1);
$j--;
}
$this->data_word=array_merge($this->data_word,unpack("C*",$rstemp));
}
}
private function makeMatrix()
{
// preparation
$this->matrix = array_fill(0, $this->size, array_fill(0, $this->size, 0));
// mettre les words
for($i=0; $i<$this->max_word; $i++)
{
$word = $this->data_word[$i];
for($j=8; $j>0; $j--)
{
$bit_pos = ($i<<3) + $j;
$this->matrix[ $this->matrix_x_array[$bit_pos] ][ $this->matrix_y_array[$bit_pos] ] = ((255*($word & 1)) ^ $this->mask_array[$bit_pos] );
$word = $word >> 1;
}
}
for($k=$this->matrix_remain; $k>0; $k--)
{
$bit_pos = $k + ( $this->max_word <<3);
$this->matrix[ $this->matrix_x_array[$bit_pos] ][ $this->matrix_y_array[$bit_pos] ] = ( 255 ^ $this->mask_array[$bit_pos] );
}
// mask select
$min_demerit_score=0;
$hor_master="";
$ver_master="";
$k=0;
while($k<$this->size)
{
$l=0;
while($l<$this->size)
{
$hor_master=$hor_master.chr($this->matrix[$l][$k]);
$ver_master=$ver_master.chr($this->matrix[$k][$l]);
$l++;
}
$k++;
}
$i=0;
$all_matrix=$this->size * $this->size;
while ($i<8)
{
$demerit_n1=0;
$ptn_temp=array();
$bit= 1<< $i;
$bit_r=(~$bit)&255;
$bit_mask=str_repeat(chr($bit),$all_matrix);
$hor = $hor_master & $bit_mask;
$ver = $ver_master & $bit_mask;
$ver_shift1=$ver.str_repeat(chr(170),$this->size);
$ver_shift2=str_repeat(chr(170),$this->size).$ver;
$ver_shift1_0=$ver.str_repeat(chr(0),$this->size);
$ver_shift2_0=str_repeat(chr(0),$this->size).$ver;
$ver_or=chunk_split(~($ver_shift1 | $ver_shift2),$this->size,chr(170));
$ver_and=chunk_split(~($ver_shift1_0 & $ver_shift2_0),$this->size,chr(170));
$hor=chunk_split(~$hor,$this->size,chr(170));
$ver=chunk_split(~$ver,$this->size,chr(170));
$hor=$hor.chr(170).$ver;
$n1_search="/".str_repeat(chr(255),5)."+|".str_repeat(chr($bit_r),5)."+/";
$n3_search=chr($bit_r).chr(255).chr($bit_r).chr($bit_r).chr($bit_r).chr(255).chr($bit_r);
$demerit_n3=substr_count($hor,$n3_search)*40;
$demerit_n4=floor(abs(( (100* (substr_count($ver,chr($bit_r))/($this->byte_num)) )-50)/5))*10;
$n2_search1="/".chr($bit_r).chr($bit_r)."+/";
$n2_search2="/".chr(255).chr(255)."+/";
$demerit_n2=0;
preg_match_all($n2_search1,$ver_and,$ptn_temp);
foreach($ptn_temp[0] as $str_temp)
{
$demerit_n2+=(strlen($str_temp)-1);
}
$ptn_temp=array();
preg_match_all($n2_search2,$ver_or,$ptn_temp);
foreach($ptn_temp[0] as $str_temp)
{
$demerit_n2+=(strlen($str_temp)-1);
}
$demerit_n2*=3;
$ptn_temp=array();
preg_match_all($n1_search,$hor,$ptn_temp);
foreach($ptn_temp[0] as $str_temp)
{
$demerit_n1+=(strlen($str_temp)-2);
}
$demerit_score=$demerit_n1+$demerit_n2+$demerit_n3+$demerit_n4;
if ($demerit_score<=$min_demerit_score || $i==0)
{
$mask_number=$i;
$min_demerit_score=$demerit_score;
}
$i++;
}
$mask_content=1 << $mask_number;
$format_information_value=(($this->ec << 3) | $mask_number);
$format_information_array=array("101010000010010","101000100100101",
"101111001111100","101101101001011","100010111111001","100000011001110",
"100111110010111","100101010100000","111011111000100","111001011110011",
"111110110101010","111100010011101","110011000101111","110001100011000",
"110110001000001","110100101110110","001011010001001","001001110111110",
"001110011100111","001100111010000","000011101100010","000001001010101",
"000110100001100","000100000111011","011010101011111","011000001101000",
"011111100110001","011101000000110","010010010110100","010000110000011",
"010111011011010","010101111101101");
for($i=0; $i<15; $i++)
{
$content=substr($format_information_array[$format_information_value],$i,1);
$this->matrix[$this->format_information_x1[$i]][$this->format_information_y1[$i]]=$content * 255;
$this->matrix[$this->format_information_x2[$i+1]][$this->format_information_y2[$i+1]]=$content * 255;
}
$this->final = unpack("C*", file_get_contents(dirname(__FILE__).'/data/modele'.$this->version.'.dat'));
$this->qr_size = $this->size+8;
for($x=0; $x<$this->size; $x++)
{
for($y=0; $y<$this->size; $y++)
{
if ($this->matrix[$x][$y] & $mask_content)
$this->final[($x+4) + ($y+4)*$this->qr_size+1] = true;
}
}
}
}
}
?>