qrcode.class.php 62 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676
  1. <?php
  2. /**
  3. * [Discuz!] (C)2001-2099 Comsenz Inc.
  4. * This is NOT a freeware, use is subject to license terms
  5. *
  6. * $Id: qrcode.class.php 33590 2013-07-12 06:39:08Z andyzheng $
  7. */
  8. if(!defined('IN_DISCUZ')) {
  9. exit('Access Denied');
  10. }
  11. define('QR_MODE_NUL', -1);
  12. define('QR_MODE_NUM', 0);
  13. define('QR_MODE_AN', 1);
  14. define('QR_MODE_8', 2);
  15. define('QR_MODE_KANJI', 3);
  16. define('QR_MODE_STRUCTURE', 4);
  17. define('QR_ECLEVEL_L', 0);
  18. define('QR_ECLEVEL_M', 1);
  19. define('QR_ECLEVEL_Q', 2);
  20. define('QR_ECLEVEL_H', 3);
  21. define('QR_FORMAT_TEXT', 0);
  22. define('QR_FORMAT_PNG', 1);
  23. class qrstr {
  24. public static function set(&$srctab, $x, $y, $repl, $replLen = false) {
  25. $srctab[$y] = substr_replace($srctab[$y], ($replLen !== false) ? substr($repl, 0, $replLen) : $repl, $x, ($replLen !== false) ? $replLen : strlen($repl));
  26. }
  27. }
  28. define('QR_CACHEABLE', false);
  29. define('QR_CACHE_DIR', false);
  30. define('QR_LOG_DIR', false);
  31. define('QR_FIND_BEST_MASK', true);
  32. define('QR_FIND_FROM_RANDOM', 2);
  33. define('QR_DEFAULT_MASK', 2);
  34. define('QR_PNG_MAXIMUM_SIZE', 1024);
  35. class QRtools {
  36. public static function binarize($frame) {
  37. $len = count($frame);
  38. foreach ($frame as &$frameLine) {
  39. for ($i = 0; $i < $len; $i++) {
  40. $frameLine[$i] = (ord($frameLine[$i]) & 1) ? '1' : '0';
  41. }
  42. }
  43. return $frame;
  44. }
  45. public static function tcpdfBarcodeArray($code, $mode = 'QR,L', $tcPdfVersion = '4.5.037') {
  46. $barcode_array = array();
  47. if (!is_array($mode))
  48. $mode = explode(',', $mode);
  49. $eccLevel = 'L';
  50. if (count($mode) > 1) {
  51. $eccLevel = $mode[1];
  52. }
  53. $qrTab = QRcode::text($code, false, $eccLevel);
  54. $size = count($qrTab);
  55. $barcode_array['num_rows'] = $size;
  56. $barcode_array['num_cols'] = $size;
  57. $barcode_array['bcode'] = array();
  58. foreach ($qrTab as $line) {
  59. $arrAdd = array();
  60. foreach (str_split($line) as $char)
  61. $arrAdd[] = ($char == '1') ? 1 : 0;
  62. $barcode_array['bcode'][] = $arrAdd;
  63. }
  64. return $barcode_array;
  65. }
  66. public static function clearCache() {
  67. self::$frames = array();
  68. }
  69. public static function buildCache() {
  70. QRtools::markTime('before_build_cache');
  71. $mask = new QRmask();
  72. for ($a = 1; $a <= QRSPEC_VERSION_MAX; $a++) {
  73. $frame = QRspec::newFrame($a);
  74. if (QR_IMAGE) {
  75. $fileName = QR_CACHE_DIR . 'frame_' . $a . '.png';
  76. QRimage::png(self::binarize($frame), $fileName, 1, 0);
  77. }
  78. $width = count($frame);
  79. $bitMask = array_fill(0, $width, array_fill(0, $width, 0));
  80. for ($maskNo = 0; $maskNo < 8; $maskNo++)
  81. $mask->makeMaskNo($maskNo, $width, $frame, $bitMask, true);
  82. }
  83. QRtools::markTime('after_build_cache');
  84. }
  85. public static function log($outfile, $err) {
  86. if (QR_LOG_DIR !== false) {
  87. if ($err != '') {
  88. if ($outfile !== false) {
  89. file_put_contents(QR_LOG_DIR . basename($outfile) . '-errors.txt', date('Y-m-d H:i:s') . ': ' . $err, FILE_APPEND);
  90. } else {
  91. file_put_contents(QR_LOG_DIR . 'errors.txt', date('Y-m-d H:i:s') . ': ' . $err, FILE_APPEND);
  92. }
  93. }
  94. }
  95. }
  96. public static function dumpMask($frame) {
  97. $width = count($frame);
  98. for ($y = 0; $y < $width; $y++) {
  99. for ($x = 0; $x < $width; $x++) {
  100. echo ord($frame[$y][$x]) . ',';
  101. }
  102. }
  103. }
  104. public static function markTime($markerId) {
  105. list($usec, $sec) = explode(" ", microtime());
  106. $time = ((float) $usec + (float) $sec);
  107. if (!isset($GLOBALS['qr_time_bench']))
  108. $GLOBALS['qr_time_bench'] = array();
  109. $GLOBALS['qr_time_bench'][$markerId] = $time;
  110. }
  111. public static function timeBenchmark() {
  112. self::markTime('finish');
  113. $lastTime = 0;
  114. $startTime = 0;
  115. $p = 0;
  116. echo '<table cellpadding="3" cellspacing="1">
  117. <thead><tr style="border-bottom:1px solid silver"><td colspan="2" style="text-align:center">BENCHMARK</td></tr></thead>
  118. <tbody>';
  119. foreach ($GLOBALS['qr_time_bench'] as $markerId => $thisTime) {
  120. if ($p > 0) {
  121. echo '<tr><th style="text-align:right">till ' . $markerId . ': </th><td>' . number_format($thisTime - $lastTime, 6) . 's</td></tr>';
  122. } else {
  123. $startTime = $thisTime;
  124. }
  125. $p++;
  126. $lastTime = $thisTime;
  127. }
  128. echo '</tbody><tfoot>
  129. <tr style="border-top:2px solid black"><th style="text-align:right">TOTAL: </th><td>' . number_format($lastTime - $startTime, 6) . 's</td></tr>
  130. </tfoot>
  131. </table>';
  132. }
  133. }
  134. QRtools::markTime('start');
  135. define('QRSPEC_VERSION_MAX', 40);
  136. define('QRSPEC_WIDTH_MAX', 177);
  137. define('QRCAP_WIDTH', 0);
  138. define('QRCAP_WORDS', 1);
  139. define('QRCAP_REMINDER', 2);
  140. define('QRCAP_EC', 3);
  141. class QRspec {
  142. public static $capacity = array(
  143. array(0, 0, 0, array(0, 0, 0, 0)),
  144. array(21, 26, 0, array(7, 10, 13, 17)),
  145. array(25, 44, 7, array(10, 16, 22, 28)),
  146. array(29, 70, 7, array(15, 26, 36, 44)),
  147. array(33, 100, 7, array(20, 36, 52, 64)),
  148. array(37, 134, 7, array(26, 48, 72, 88)),
  149. array(41, 172, 7, array(36, 64, 96, 112)),
  150. array(45, 196, 0, array(40, 72, 108, 130)),
  151. array(49, 242, 0, array(48, 88, 132, 156)),
  152. array(53, 292, 0, array(60, 110, 160, 192)),
  153. array(57, 346, 0, array(72, 130, 192, 224)),
  154. array(61, 404, 0, array(80, 150, 224, 264)),
  155. array(65, 466, 0, array(96, 176, 260, 308)),
  156. array(69, 532, 0, array(104, 198, 288, 352)),
  157. array(73, 581, 3, array(120, 216, 320, 384)),
  158. array(77, 655, 3, array(132, 240, 360, 432)),
  159. array(81, 733, 3, array(144, 280, 408, 480)),
  160. array(85, 815, 3, array(168, 308, 448, 532)),
  161. array(89, 901, 3, array(180, 338, 504, 588)),
  162. array(93, 991, 3, array(196, 364, 546, 650)),
  163. array(97, 1085, 3, array(224, 416, 600, 700)),
  164. array(101, 1156, 4, array(224, 442, 644, 750)),
  165. array(105, 1258, 4, array(252, 476, 690, 816)),
  166. array(109, 1364, 4, array(270, 504, 750, 900)),
  167. array(113, 1474, 4, array(300, 560, 810, 960)),
  168. array(117, 1588, 4, array(312, 588, 870, 1050)),
  169. array(121, 1706, 4, array(336, 644, 952, 1110)),
  170. array(125, 1828, 4, array(360, 700, 1020, 1200)),
  171. array(129, 1921, 3, array(390, 728, 1050, 1260)),
  172. array(133, 2051, 3, array(420, 784, 1140, 1350)),
  173. array(137, 2185, 3, array(450, 812, 1200, 1440)),
  174. array(141, 2323, 3, array(480, 868, 1290, 1530)),
  175. array(145, 2465, 3, array(510, 924, 1350, 1620)),
  176. array(149, 2611, 3, array(540, 980, 1440, 1710)),
  177. array(153, 2761, 3, array(570, 1036, 1530, 1800)),
  178. array(157, 2876, 0, array(570, 1064, 1590, 1890)),
  179. array(161, 3034, 0, array(600, 1120, 1680, 1980)),
  180. array(165, 3196, 0, array(630, 1204, 1770, 2100)),
  181. array(169, 3362, 0, array(660, 1260, 1860, 2220)),
  182. array(173, 3532, 0, array(720, 1316, 1950, 2310)),
  183. array(177, 3706, 0, array(750, 1372, 2040, 2430))
  184. );
  185. public static function getDataLength($version, $level) {
  186. return self::$capacity[$version][QRCAP_WORDS] - self::$capacity[$version][QRCAP_EC][$level];
  187. }
  188. public static function getECCLength($version, $level) {
  189. return self::$capacity[$version][QRCAP_EC][$level];
  190. }
  191. public static function getWidth($version) {
  192. return self::$capacity[$version][QRCAP_WIDTH];
  193. }
  194. public static function getRemainder($version) {
  195. return self::$capacity[$version][QRCAP_REMINDER];
  196. }
  197. public static function getMinimumVersion($size, $level) {
  198. for ($i = 1; $i <= QRSPEC_VERSION_MAX; $i++) {
  199. $words = self::$capacity[$i][QRCAP_WORDS] - self::$capacity[$i][QRCAP_EC][$level];
  200. if ($words >= $size)
  201. return $i;
  202. }
  203. return -1;
  204. }
  205. public static $lengthTableBits = array(
  206. array(10, 12, 14),
  207. array(9, 11, 13),
  208. array(8, 16, 16),
  209. array(8, 10, 12)
  210. );
  211. public static function lengthIndicator($mode, $version) {
  212. if ($mode == QR_MODE_STRUCTURE)
  213. return 0;
  214. if ($version <= 9) {
  215. $l = 0;
  216. } else if ($version <= 26) {
  217. $l = 1;
  218. } else {
  219. $l = 2;
  220. }
  221. return self::$lengthTableBits[$mode][$l];
  222. }
  223. public static function maximumWords($mode, $version) {
  224. if ($mode == QR_MODE_STRUCTURE)
  225. return 3;
  226. if ($version <= 9) {
  227. $l = 0;
  228. } else if ($version <= 26) {
  229. $l = 1;
  230. } else {
  231. $l = 2;
  232. }
  233. $bits = self::$lengthTableBits[$mode][$l];
  234. $words = (1 << $bits) - 1;
  235. if ($mode == QR_MODE_KANJI) {
  236. $words *= 2;
  237. }
  238. return $words;
  239. }
  240. public static $eccTable = array(
  241. array(array(0, 0), array(0, 0), array(0, 0), array(0, 0)),
  242. array(array(1, 0), array(1, 0), array(1, 0), array(1, 0)),
  243. array(array(1, 0), array(1, 0), array(1, 0), array(1, 0)),
  244. array(array(1, 0), array(1, 0), array(2, 0), array(2, 0)),
  245. array(array(1, 0), array(2, 0), array(2, 0), array(4, 0)),
  246. array(array(1, 0), array(2, 0), array(2, 2), array(2, 2)),
  247. array(array(2, 0), array(4, 0), array(4, 0), array(4, 0)),
  248. array(array(2, 0), array(4, 0), array(2, 4), array(4, 1)),
  249. array(array(2, 0), array(2, 2), array(4, 2), array(4, 2)),
  250. array(array(2, 0), array(3, 2), array(4, 4), array(4, 4)),
  251. array(array(2, 2), array(4, 1), array(6, 2), array(6, 2)),
  252. array(array(4, 0), array(1, 4), array(4, 4), array(3, 8)),
  253. array(array(2, 2), array(6, 2), array(4, 6), array(7, 4)),
  254. array(array(4, 0), array(8, 1), array(8, 4), array(12, 4)),
  255. array(array(3, 1), array(4, 5), array(11, 5), array(11, 5)),
  256. array(array(5, 1), array(5, 5), array(5, 7), array(11, 7)),
  257. array(array(5, 1), array(7, 3), array(15, 2), array(3, 13)),
  258. array(array(1, 5), array(10, 1), array(1, 15), array(2, 17)),
  259. array(array(5, 1), array(9, 4), array(17, 1), array(2, 19)),
  260. array(array(3, 4), array(3, 11), array(17, 4), array(9, 16)),
  261. array(array(3, 5), array(3, 13), array(15, 5), array(15, 10)),
  262. array(array(4, 4), array(17, 0), array(17, 6), array(19, 6)),
  263. array(array(2, 7), array(17, 0), array(7, 16), array(34, 0)),
  264. array(array(4, 5), array(4, 14), array(11, 14), array(16, 14)),
  265. array(array(6, 4), array(6, 14), array(11, 16), array(30, 2)),
  266. array(array(8, 4), array(8, 13), array(7, 22), array(22, 13)),
  267. array(array(10, 2), array(19, 4), array(28, 6), array(33, 4)),
  268. array(array(8, 4), array(22, 3), array(8, 26), array(12, 28)),
  269. array(array(3, 10), array(3, 23), array(4, 31), array(11, 31)),
  270. array(array(7, 7), array(21, 7), array(1, 37), array(19, 26)),
  271. array(array(5, 10), array(19, 10), array(15, 25), array(23, 25)),
  272. array(array(13, 3), array(2, 29), array(42, 1), array(23, 28)),
  273. array(array(17, 0), array(10, 23), array(10, 35), array(19, 35)),
  274. array(array(17, 1), array(14, 21), array(29, 19), array(11, 46)),
  275. array(array(13, 6), array(14, 23), array(44, 7), array(59, 1)),
  276. array(array(12, 7), array(12, 26), array(39, 14), array(22, 41)),
  277. array(array(6, 14), array(6, 34), array(46, 10), array(2, 64)),
  278. array(array(17, 4), array(29, 14), array(49, 10), array(24, 46)),
  279. array(array(4, 18), array(13, 32), array(48, 14), array(42, 32)),
  280. array(array(20, 4), array(40, 7), array(43, 22), array(10, 67)),
  281. array(array(19, 6), array(18, 31), array(34, 34), array(20, 61)),
  282. );
  283. public static function getEccSpec($version, $level, array &$spec) {
  284. if (count($spec) < 5) {
  285. $spec = array(0, 0, 0, 0, 0);
  286. }
  287. $b1 = self::$eccTable[$version][$level][0];
  288. $b2 = self::$eccTable[$version][$level][1];
  289. $data = self::getDataLength($version, $level);
  290. $ecc = self::getECCLength($version, $level);
  291. if ($b2 == 0) {
  292. $spec[0] = $b1;
  293. $spec[1] = (int) ($data / $b1);
  294. $spec[2] = (int) ($ecc / $b1);
  295. $spec[3] = 0;
  296. $spec[4] = 0;
  297. } else {
  298. $spec[0] = $b1;
  299. $spec[1] = (int) ($data / ($b1 + $b2));
  300. $spec[2] = (int) ($ecc / ($b1 + $b2));
  301. $spec[3] = $b2;
  302. $spec[4] = $spec[1] + 1;
  303. }
  304. }
  305. public static $alignmentPattern = array(
  306. array(0, 0),
  307. array(0, 0), array(18, 0), array(22, 0), array(26, 0), array(30, 0),
  308. array(34, 0), array(22, 38), array(24, 42), array(26, 46), array(28, 50),
  309. array(30, 54), array(32, 58), array(34, 62), array(26, 46), array(26, 48),
  310. array(26, 50), array(30, 54), array(30, 56), array(30, 58), array(34, 62),
  311. array(28, 50), array(26, 50), array(30, 54), array(28, 54), array(32, 58),
  312. array(30, 58), array(34, 62), array(26, 50), array(30, 54), array(26, 52),
  313. array(30, 56), array(34, 60), array(30, 58), array(34, 62), array(30, 54),
  314. array(24, 50), array(28, 54), array(32, 58), array(26, 54), array(30, 58),
  315. );
  316. public static function putAlignmentMarker(array &$frame, $ox, $oy) {
  317. $finder = array(
  318. "\xa1\xa1\xa1\xa1\xa1",
  319. "\xa1\xa0\xa0\xa0\xa1",
  320. "\xa1\xa0\xa1\xa0\xa1",
  321. "\xa1\xa0\xa0\xa0\xa1",
  322. "\xa1\xa1\xa1\xa1\xa1"
  323. );
  324. $yStart = $oy - 2;
  325. $xStart = $ox - 2;
  326. for ($y = 0; $y < 5; $y++) {
  327. QRstr::set($frame, $xStart, $yStart + $y, $finder[$y]);
  328. }
  329. }
  330. public static function putAlignmentPattern($version, &$frame, $width) {
  331. if ($version < 2)
  332. return;
  333. $d = self::$alignmentPattern[$version][1] - self::$alignmentPattern[$version][0];
  334. if ($d < 0) {
  335. $w = 2;
  336. } else {
  337. $w = (int) (($width - self::$alignmentPattern[$version][0]) / $d + 2);
  338. }
  339. if ($w * $w - 3 == 1) {
  340. $x = self::$alignmentPattern[$version][0];
  341. $y = self::$alignmentPattern[$version][0];
  342. self::putAlignmentMarker($frame, $x, $y);
  343. return;
  344. }
  345. $cx = self::$alignmentPattern[$version][0];
  346. for ($x = 1; $x < $w - 1; $x++) {
  347. self::putAlignmentMarker($frame, 6, $cx);
  348. self::putAlignmentMarker($frame, $cx, 6);
  349. $cx += $d;
  350. }
  351. $cy = self::$alignmentPattern[$version][0];
  352. for ($y = 0; $y < $w - 1; $y++) {
  353. $cx = self::$alignmentPattern[$version][0];
  354. for ($x = 0; $x < $w - 1; $x++) {
  355. self::putAlignmentMarker($frame, $cx, $cy);
  356. $cx += $d;
  357. }
  358. $cy += $d;
  359. }
  360. }
  361. public static $versionPattern = array(
  362. 0x07c94, 0x085bc, 0x09a99, 0x0a4d3, 0x0bbf6, 0x0c762, 0x0d847, 0x0e60d,
  363. 0x0f928, 0x10b78, 0x1145d, 0x12a17, 0x13532, 0x149a6, 0x15683, 0x168c9,
  364. 0x177ec, 0x18ec4, 0x191e1, 0x1afab, 0x1b08e, 0x1cc1a, 0x1d33f, 0x1ed75,
  365. 0x1f250, 0x209d5, 0x216f0, 0x228ba, 0x2379f, 0x24b0b, 0x2542e, 0x26a64,
  366. 0x27541, 0x28c69
  367. );
  368. public static function getVersionPattern($version) {
  369. if ($version < 7 || $version > QRSPEC_VERSION_MAX)
  370. return 0;
  371. return self::$versionPattern[$version - 7];
  372. }
  373. public static $formatInfo = array(
  374. array(0x77c4, 0x72f3, 0x7daa, 0x789d, 0x662f, 0x6318, 0x6c41, 0x6976),
  375. array(0x5412, 0x5125, 0x5e7c, 0x5b4b, 0x45f9, 0x40ce, 0x4f97, 0x4aa0),
  376. array(0x355f, 0x3068, 0x3f31, 0x3a06, 0x24b4, 0x2183, 0x2eda, 0x2bed),
  377. array(0x1689, 0x13be, 0x1ce7, 0x19d0, 0x0762, 0x0255, 0x0d0c, 0x083b)
  378. );
  379. public static function getFormatInfo($mask, $level) {
  380. if ($mask < 0 || $mask > 7)
  381. return 0;
  382. if ($level < 0 || $level > 3)
  383. return 0;
  384. return self::$formatInfo[$level][$mask];
  385. }
  386. public static $frames = array();
  387. public static function putFinderPattern(&$frame, $ox, $oy) {
  388. $finder = array(
  389. "\xc1\xc1\xc1\xc1\xc1\xc1\xc1",
  390. "\xc1\xc0\xc0\xc0\xc0\xc0\xc1",
  391. "\xc1\xc0\xc1\xc1\xc1\xc0\xc1",
  392. "\xc1\xc0\xc1\xc1\xc1\xc0\xc1",
  393. "\xc1\xc0\xc1\xc1\xc1\xc0\xc1",
  394. "\xc1\xc0\xc0\xc0\xc0\xc0\xc1",
  395. "\xc1\xc1\xc1\xc1\xc1\xc1\xc1"
  396. );
  397. for ($y = 0; $y < 7; $y++) {
  398. QRstr::set($frame, $ox, $oy + $y, $finder[$y]);
  399. }
  400. }
  401. public static function createFrame($version) {
  402. $width = self::$capacity[$version][QRCAP_WIDTH];
  403. $frameLine = str_repeat("\0", $width);
  404. $frame = array_fill(0, $width, $frameLine);
  405. self::putFinderPattern($frame, 0, 0);
  406. self::putFinderPattern($frame, $width - 7, 0);
  407. self::putFinderPattern($frame, 0, $width - 7);
  408. $yOffset = $width - 7;
  409. for ($y = 0; $y < 7; $y++) {
  410. $frame[$y][7] = "\xc0";
  411. $frame[$y][$width - 8] = "\xc0";
  412. $frame[$yOffset][7] = "\xc0";
  413. $yOffset++;
  414. }
  415. $setPattern = str_repeat("\xc0", 8);
  416. QRstr::set($frame, 0, 7, $setPattern);
  417. QRstr::set($frame, $width - 8, 7, $setPattern);
  418. QRstr::set($frame, 0, $width - 8, $setPattern);
  419. $setPattern = str_repeat("\x84", 9);
  420. QRstr::set($frame, 0, 8, $setPattern);
  421. QRstr::set($frame, $width - 8, 8, $setPattern, 8);
  422. $yOffset = $width - 8;
  423. for ($y = 0; $y < 8; $y++, $yOffset++) {
  424. $frame[$y][8] = "\x84";
  425. $frame[$yOffset][8] = "\x84";
  426. }
  427. for ($i = 1; $i < $width - 15; $i++) {
  428. $frame[6][7 + $i] = chr(0x90 | ($i & 1));
  429. $frame[7 + $i][6] = chr(0x90 | ($i & 1));
  430. }
  431. self::putAlignmentPattern($version, $frame, $width);
  432. if ($version >= 7) {
  433. $vinf = self::getVersionPattern($version);
  434. $v = $vinf;
  435. for ($x = 0; $x < 6; $x++) {
  436. for ($y = 0; $y < 3; $y++) {
  437. $frame[($width - 11) + $y][$x] = chr(0x88 | ($v & 1));
  438. $v = $v >> 1;
  439. }
  440. }
  441. $v = $vinf;
  442. for ($y = 0; $y < 6; $y++) {
  443. for ($x = 0; $x < 3; $x++) {
  444. $frame[$y][$x + ($width - 11)] = chr(0x88 | ($v & 1));
  445. $v = $v >> 1;
  446. }
  447. }
  448. }
  449. $frame[$width - 8][8] = "\x81";
  450. return $frame;
  451. }
  452. public static function debug($frame, $binary_mode = false) {
  453. if ($binary_mode) {
  454. foreach ($frame as &$frameLine) {
  455. $frameLine = join('<span class="m">&nbsp;&nbsp;</span>', explode('0', $frameLine));
  456. $frameLine = join('&#9608;&#9608;', explode('1', $frameLine));
  457. }
  458. ?>
  459. <style>
  460. .m { background-color: white; }
  461. </style>
  462. <?php
  463. echo '<pre><tt><br/ ><br/ ><br/ >&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;';
  464. echo join("<br/ >&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;", $frame);
  465. echo '</tt></pre><br/ ><br/ ><br/ ><br/ ><br/ ><br/ >';
  466. } else {
  467. foreach ($frame as &$frameLine) {
  468. $frameLine = join('<span class="m">&nbsp;</span>', explode("\xc0", $frameLine));
  469. $frameLine = join('<span class="m">&#9618;</span>', explode("\xc1", $frameLine));
  470. $frameLine = join('<span class="p">&nbsp;</span>', explode("\xa0", $frameLine));
  471. $frameLine = join('<span class="p">&#9618;</span>', explode("\xa1", $frameLine));
  472. $frameLine = join('<span class="s">&#9671;</span>', explode("\x84", $frameLine));
  473. $frameLine = join('<span class="s">&#9670;</span>', explode("\x85", $frameLine));
  474. $frameLine = join('<span class="x">&#9762;</span>', explode("\x81", $frameLine));
  475. $frameLine = join('<span class="c">&nbsp;</span>', explode("\x90", $frameLine));
  476. $frameLine = join('<span class="c">&#9719;</span>', explode("\x91", $frameLine));
  477. $frameLine = join('<span class="f">&nbsp;</span>', explode("\x88", $frameLine));
  478. $frameLine = join('<span class="f">&#9618;</span>', explode("\x89", $frameLine));
  479. $frameLine = join('&#9830;', explode("\x01", $frameLine));
  480. $frameLine = join('&#8901;', explode("\0", $frameLine));
  481. }
  482. ?>
  483. <style>
  484. .p { background-color: yellow; }
  485. .m { background-color: #00FF00; }
  486. .s { background-color: #FF0000; }
  487. .c { background-color: aqua; }
  488. .x { background-color: pink; }
  489. .f { background-color: gold; }
  490. </style>
  491. <?php
  492. echo "<pre><tt>";
  493. echo join("<br/ >", $frame);
  494. echo "</tt></pre>";
  495. }
  496. }
  497. public static function serial($frame) {
  498. return gzcompress(join("\n", $frame), 9);
  499. }
  500. public static function unserial($code) {
  501. return explode("\n", gzuncompress($code));
  502. }
  503. public static function newFrame($version) {
  504. if ($version < 1 || $version > QRSPEC_VERSION_MAX)
  505. return null;
  506. if (!isset(self::$frames[$version])) {
  507. $fileName = QR_CACHE_DIR . 'frame_' . $version . '.dat';
  508. if (QR_CACHEABLE) {
  509. if (file_exists($fileName)) {
  510. self::$frames[$version] = self::unserial(file_get_contents($fileName));
  511. } else {
  512. self::$frames[$version] = self::createFrame($version);
  513. file_put_contents($fileName, self::serial(self::$frames[$version]));
  514. }
  515. } else {
  516. self::$frames[$version] = self::createFrame($version);
  517. }
  518. }
  519. if (is_null(self::$frames[$version]))
  520. return null;
  521. return self::$frames[$version];
  522. }
  523. public static function rsBlockNum($spec) {
  524. return $spec[0] + $spec[3];
  525. }
  526. public static function rsBlockNum1($spec) {
  527. return $spec[0];
  528. }
  529. public static function rsDataCodes1($spec) {
  530. return $spec[1];
  531. }
  532. public static function rsEccCodes1($spec) {
  533. return $spec[2];
  534. }
  535. public static function rsBlockNum2($spec) {
  536. return $spec[3];
  537. }
  538. public static function rsDataCodes2($spec) {
  539. return $spec[4];
  540. }
  541. public static function rsEccCodes2($spec) {
  542. return $spec[2];
  543. }
  544. public static function rsDataLength($spec) {
  545. return ($spec[0] * $spec[1]) + ($spec[3] * $spec[4]);
  546. }
  547. public static function rsEccLength($spec) {
  548. return ($spec[0] + $spec[3]) * $spec[2];
  549. }
  550. }
  551. define('QR_IMAGE', true);
  552. class QRimage {
  553. public static function png($frame, $filename = false, $pixelPerPoint = 4, $outerFrame = 4, $saveandprint = FALSE) {
  554. $image = self::image($frame, $pixelPerPoint, $outerFrame);
  555. if ($filename === false) {
  556. Header("Content-type: image/png");
  557. ImagePng($image);
  558. } else {
  559. if ($saveandprint === TRUE) {
  560. ImagePng($image, $filename);
  561. header("Content-type: image/png");
  562. ImagePng($image);
  563. } else {
  564. ImagePng($image, $filename);
  565. }
  566. }
  567. ImageDestroy($image);
  568. }
  569. public static function jpg($frame, $filename = false, $pixelPerPoint = 8, $outerFrame = 4, $q = 85) {
  570. $image = self::image($frame, $pixelPerPoint, $outerFrame);
  571. if ($filename === false) {
  572. Header("Content-type: image/jpeg");
  573. ImageJpeg($image, null, $q);
  574. } else {
  575. ImageJpeg($image, $filename, $q);
  576. }
  577. ImageDestroy($image);
  578. }
  579. private static function image($frame, $pixelPerPoint = 4, $outerFrame = 4) {
  580. $h = count($frame);
  581. $w = strlen($frame[0]);
  582. $imgW = $w + 2 * $outerFrame;
  583. $imgH = $h + 2 * $outerFrame;
  584. $base_image = ImageCreate($imgW, $imgH);
  585. $col[0] = ImageColorAllocate($base_image, 255, 255, 255);
  586. $col[1] = ImageColorAllocate($base_image, 0, 0, 0);
  587. imagefill($base_image, 0, 0, $col[0]);
  588. for ($y = 0; $y < $h; $y++) {
  589. for ($x = 0; $x < $w; $x++) {
  590. if ($frame[$y][$x] == '1') {
  591. ImageSetPixel($base_image, $x + $outerFrame, $y + $outerFrame, $col[1]);
  592. }
  593. }
  594. }
  595. $target_image = ImageCreate($imgW * $pixelPerPoint, $imgH * $pixelPerPoint);
  596. ImageCopyResized($target_image, $base_image, 0, 0, 0, 0, $imgW * $pixelPerPoint, $imgH * $pixelPerPoint, $imgW, $imgH);
  597. ImageDestroy($base_image);
  598. return $target_image;
  599. }
  600. }
  601. define('STRUCTURE_HEADER_BITS', 20);
  602. define('MAX_STRUCTURED_SYMBOLS', 16);
  603. class QRinputItem {
  604. public $mode;
  605. public $size;
  606. public $data;
  607. public $bstream;
  608. public function __construct($mode, $size, $data, $bstream = null) {
  609. $setData = array_slice($data, 0, $size);
  610. if (count($setData) < $size) {
  611. $setData = array_merge($setData, array_fill(0, $size - count($setData), 0));
  612. }
  613. if (!QRinput::check($mode, $size, $setData)) {
  614. throw new Exception('Error m:' . $mode . ',s:' . $size . ',d:' . join(',', $setData));
  615. return null;
  616. }
  617. $this->mode = $mode;
  618. $this->size = $size;
  619. $this->data = $setData;
  620. $this->bstream = $bstream;
  621. }
  622. public function encodeModeNum($version) {
  623. try {
  624. $words = (int) ($this->size / 3);
  625. $bs = new QRbitstream();
  626. $val = 0x1;
  627. $bs->appendNum(4, $val);
  628. $bs->appendNum(QRspec::lengthIndicator(QR_MODE_NUM, $version), $this->size);
  629. for ($i = 0; $i < $words; $i++) {
  630. $val = (ord($this->data[$i * 3]) - ord('0')) * 100;
  631. $val += (ord($this->data[$i * 3 + 1]) - ord('0')) * 10;
  632. $val += (ord($this->data[$i * 3 + 2]) - ord('0'));
  633. $bs->appendNum(10, $val);
  634. }
  635. if ($this->size - $words * 3 == 1) {
  636. $val = ord($this->data[$words * 3]) - ord('0');
  637. $bs->appendNum(4, $val);
  638. } else if ($this->size - $words * 3 == 2) {
  639. $val = (ord($this->data[$words * 3]) - ord('0')) * 10;
  640. $val += (ord($this->data[$words * 3 + 1]) - ord('0'));
  641. $bs->appendNum(7, $val);
  642. }
  643. $this->bstream = $bs;
  644. return 0;
  645. } catch (Exception $e) {
  646. return -1;
  647. }
  648. }
  649. public function encodeModeAn($version) {
  650. try {
  651. $words = (int) ($this->size / 2);
  652. $bs = new QRbitstream();
  653. $bs->appendNum(4, 0x02);
  654. $bs->appendNum(QRspec::lengthIndicator(QR_MODE_AN, $version), $this->size);
  655. for ($i = 0; $i < $words; $i++) {
  656. $val = (int) QRinput::lookAnTable(ord($this->data[$i * 2])) * 45;
  657. $val += (int) QRinput::lookAnTable(ord($this->data[$i * 2 + 1]));
  658. $bs->appendNum(11, $val);
  659. }
  660. if ($this->size & 1) {
  661. $val = QRinput::lookAnTable(ord($this->data[$words * 2]));
  662. $bs->appendNum(6, $val);
  663. }
  664. $this->bstream = $bs;
  665. return 0;
  666. } catch (Exception $e) {
  667. return -1;
  668. }
  669. }
  670. public function encodeMode8($version) {
  671. try {
  672. $bs = new QRbitstream();
  673. $bs->appendNum(4, 0x4);
  674. $bs->appendNum(QRspec::lengthIndicator(QR_MODE_8, $version), $this->size);
  675. for ($i = 0; $i < $this->size; $i++) {
  676. $bs->appendNum(8, ord($this->data[$i]));
  677. }
  678. $this->bstream = $bs;
  679. return 0;
  680. } catch (Exception $e) {
  681. return -1;
  682. }
  683. }
  684. public function encodeModeKanji($version) {
  685. try {
  686. $bs = new QRbitrtream();
  687. $bs->appendNum(4, 0x8);
  688. $bs->appendNum(QRspec::lengthIndicator(QR_MODE_KANJI, $version), (int) ($this->size / 2));
  689. for ($i = 0; $i < $this->size; $i+=2) {
  690. $val = (ord($this->data[$i]) << 8) | ord($this->data[$i + 1]);
  691. if ($val <= 0x9ffc) {
  692. $val -= 0x8140;
  693. } else {
  694. $val -= 0xc140;
  695. }
  696. $h = ($val >> 8) * 0xc0;
  697. $val = ($val & 0xff) + $h;
  698. $bs->appendNum(13, $val);
  699. }
  700. $this->bstream = $bs;
  701. return 0;
  702. } catch (Exception $e) {
  703. return -1;
  704. }
  705. }
  706. public function encodeModeStructure() {
  707. try {
  708. $bs = new QRbitstream();
  709. $bs->appendNum(4, 0x03);
  710. $bs->appendNum(4, ord($this->data[1]) - 1);
  711. $bs->appendNum(4, ord($this->data[0]) - 1);
  712. $bs->appendNum(8, ord($this->data[2]));
  713. $this->bstream = $bs;
  714. return 0;
  715. } catch (Exception $e) {
  716. return -1;
  717. }
  718. }
  719. public function estimateBitStreamSizeOfEntry($version) {
  720. $bits = 0;
  721. if ($version == 0)
  722. $version = 1;
  723. switch ($this->mode) {
  724. case QR_MODE_NUM: $bits = QRinput::estimateBitsModeNum($this->size);
  725. break;
  726. case QR_MODE_AN: $bits = QRinput::estimateBitsModeAn($this->size);
  727. break;
  728. case QR_MODE_8: $bits = QRinput::estimateBitsMode8($this->size);
  729. break;
  730. case QR_MODE_KANJI: $bits = QRinput::estimateBitsModeKanji($this->size);
  731. break;
  732. case QR_MODE_STRUCTURE: return STRUCTURE_HEADER_BITS;
  733. default:
  734. return 0;
  735. }
  736. $l = QRspec::lengthIndicator($this->mode, $version);
  737. $m = 1 << $l;
  738. $num = (int) (($this->size + $m - 1) / $m);
  739. $bits += $num * (4 + $l);
  740. return $bits;
  741. }
  742. public function encodeBitStream($version) {
  743. try {
  744. unset($this->bstream);
  745. $words = QRspec::maximumWords($this->mode, $version);
  746. if ($this->size > $words) {
  747. $st1 = new QRinputItem($this->mode, $words, $this->data);
  748. $st2 = new QRinputItem($this->mode, $this->size - $words, array_slice($this->data, $words));
  749. $st1->encodeBitStream($version);
  750. $st2->encodeBitStream($version);
  751. $this->bstream = new QRbitstream();
  752. $this->bstream->append($st1->bstream);
  753. $this->bstream->append($st2->bstream);
  754. unset($st1);
  755. unset($st2);
  756. } else {
  757. $ret = 0;
  758. switch ($this->mode) {
  759. case QR_MODE_NUM: $ret = $this->encodeModeNum($version);
  760. break;
  761. case QR_MODE_AN: $ret = $this->encodeModeAn($version);
  762. break;
  763. case QR_MODE_8: $ret = $this->encodeMode8($version);
  764. break;
  765. case QR_MODE_KANJI: $ret = $this->encodeModeKanji($version);
  766. break;
  767. case QR_MODE_STRUCTURE: $ret = $this->encodeModeStructure();
  768. break;
  769. default:
  770. break;
  771. }
  772. if ($ret < 0)
  773. return -1;
  774. }
  775. return $this->bstream->size();
  776. } catch (Exception $e) {
  777. return -1;
  778. }
  779. }
  780. }
  781. ;
  782. class QRinput {
  783. public $items;
  784. private $version;
  785. private $level;
  786. public function __construct($version = 0, $level = QR_ECLEVEL_L) {
  787. if ($version < 0 || $version > QRSPEC_VERSION_MAX || $level > QR_ECLEVEL_H) {
  788. throw new Exception('Invalid version no');
  789. return NULL;
  790. }
  791. $this->version = $version;
  792. $this->level = $level;
  793. }
  794. public function getVersion() {
  795. return $this->version;
  796. }
  797. public function setVersion($version) {
  798. if ($version < 0 || $version > QRSPEC_VERSION_MAX) {
  799. throw new Exception('Invalid version no');
  800. return -1;
  801. }
  802. $this->version = $version;
  803. return 0;
  804. }
  805. public function getErrorCorrectionLevel() {
  806. return $this->level;
  807. }
  808. public function setErrorCorrectionLevel($level) {
  809. if ($level > QR_ECLEVEL_H) {
  810. throw new Exception('Invalid ECLEVEL');
  811. return -1;
  812. }
  813. $this->level = $level;
  814. return 0;
  815. }
  816. public function appendEntry(QRinputItem $entry) {
  817. $this->items[] = $entry;
  818. }
  819. public function append($mode, $size, $data) {
  820. try {
  821. $entry = new QRinputItem($mode, $size, $data);
  822. $this->items[] = $entry;
  823. return 0;
  824. } catch (Exception $e) {
  825. return -1;
  826. }
  827. }
  828. public function insertStructuredAppendHeader($size, $index, $parity) {
  829. if ($size > MAX_STRUCTURED_SYMBOLS) {
  830. throw new Exception('insertStructuredAppendHeader wrong size');
  831. }
  832. if ($index <= 0 || $index > MAX_STRUCTURED_SYMBOLS) {
  833. throw new Exception('insertStructuredAppendHeader wrong index');
  834. }
  835. $buf = array($size, $index, $parity);
  836. try {
  837. $entry = new QRinputItem(QR_MODE_STRUCTURE, 3, buf);
  838. array_unshift($this->items, $entry);
  839. return 0;
  840. } catch (Exception $e) {
  841. return -1;
  842. }
  843. }
  844. public function calcParity() {
  845. $parity = 0;
  846. foreach ($this->items as $item) {
  847. if ($item->mode != QR_MODE_STRUCTURE) {
  848. for ($i = $item->size - 1; $i >= 0; $i--) {
  849. $parity ^= $item->data[$i];
  850. }
  851. }
  852. }
  853. return $parity;
  854. }
  855. public static function checkModeNum($size, $data) {
  856. for ($i = 0; $i < $size; $i++) {
  857. if ((ord($data[$i]) < ord('0')) || (ord($data[$i]) > ord('9'))) {
  858. return false;
  859. }
  860. }
  861. return true;
  862. }
  863. public static function estimateBitsModeNum($size) {
  864. $w = (int) $size / 3;
  865. $bits = $w * 10;
  866. switch ($size - $w * 3) {
  867. case 1:
  868. $bits += 4;
  869. break;
  870. case 2:
  871. $bits += 7;
  872. break;
  873. default:
  874. break;
  875. }
  876. return $bits;
  877. }
  878. public static $anTable = array(
  879. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  880. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  881. 36, -1, -1, -1, 37, 38, -1, -1, -1, -1, 39, 40, -1, 41, 42, 43,
  882. 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 44, -1, -1, -1, -1, -1,
  883. -1, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24,
  884. 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, -1, -1, -1, -1, -1,
  885. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  886. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1
  887. );
  888. public static function lookAnTable($c) {
  889. return (($c > 127) ? -1 : self::$anTable[$c]);
  890. }
  891. public static function checkModeAn($size, $data) {
  892. for ($i = 0; $i < $size; $i++) {
  893. if (self::lookAnTable(ord($data[$i])) == -1) {
  894. return false;
  895. }
  896. }
  897. return true;
  898. }
  899. public static function estimateBitsModeAn($size) {
  900. $w = (int) ($size / 2);
  901. $bits = $w * 11;
  902. if ($size & 1) {
  903. $bits += 6;
  904. }
  905. return $bits;
  906. }
  907. public static function estimateBitsMode8($size) {
  908. return $size * 8;
  909. }
  910. public function estimateBitsModeKanji($size) {
  911. return (int) (($size / 2) * 13);
  912. }
  913. public static function checkModeKanji($size, $data) {
  914. if ($size & 1)
  915. return false;
  916. for ($i = 0; $i < $size; $i+=2) {
  917. $val = (ord($data[$i]) << 8) | ord($data[$i + 1]);
  918. if ($val < 0x8140
  919. || ($val > 0x9ffc && $val < 0xe040)
  920. || $val > 0xebbf) {
  921. return false;
  922. }
  923. }
  924. return true;
  925. }
  926. public static function check($mode, $size, $data) {
  927. if ($size <= 0)
  928. return false;
  929. switch ($mode) {
  930. case QR_MODE_NUM: return self::checkModeNum($size, $data);
  931. break;
  932. case QR_MODE_AN: return self::checkModeAn($size, $data);
  933. break;
  934. case QR_MODE_KANJI: return self::checkModeKanji($size, $data);
  935. break;
  936. case QR_MODE_8: return true;
  937. break;
  938. case QR_MODE_STRUCTURE: return true;
  939. break;
  940. default:
  941. break;
  942. }
  943. return false;
  944. }
  945. public function estimateBitStreamSize($version) {
  946. $bits = 0;
  947. foreach ($this->items as $item) {
  948. $bits += $item->estimateBitStreamSizeOfEntry($version);
  949. }
  950. return $bits;
  951. }
  952. public function estimateVersion() {
  953. $version = 0;
  954. $prev = 0;
  955. do {
  956. $prev = $version;
  957. $bits = $this->estimateBitStreamSize($prev);
  958. $version = QRspec::getMinimumVersion((int) (($bits + 7) / 8), $this->level);
  959. if ($version < 0) {
  960. return -1;
  961. }
  962. } while ($version > $prev);
  963. return $version;
  964. }
  965. public static function lengthOfCode($mode, $version, $bits) {
  966. $payload = $bits - 4 - QRspec::lengthIndicator($mode, $version);
  967. switch ($mode) {
  968. case QR_MODE_NUM:
  969. $chunks = (int) ($payload / 10);
  970. $remain = $payload - $chunks * 10;
  971. $size = $chunks * 3;
  972. if ($remain >= 7) {
  973. $size += 2;
  974. } else if ($remain >= 4) {
  975. $size += 1;
  976. }
  977. break;
  978. case QR_MODE_AN:
  979. $chunks = (int) ($payload / 11);
  980. $remain = $payload - $chunks * 11;
  981. $size = $chunks * 2;
  982. if ($remain >= 6)
  983. $size++;
  984. break;
  985. case QR_MODE_8:
  986. $size = (int) ($payload / 8);
  987. break;
  988. case QR_MODE_KANJI:
  989. $size = (int) (($payload / 13) * 2);
  990. break;
  991. case QR_MODE_STRUCTURE:
  992. $size = (int) ($payload / 8);
  993. break;
  994. default:
  995. $size = 0;
  996. break;
  997. }
  998. $maxsize = QRspec::maximumWords($mode, $version);
  999. if ($size < 0)
  1000. $size = 0;
  1001. if ($size > $maxsize)
  1002. $size = $maxsize;
  1003. return $size;
  1004. }
  1005. public function createBitStream() {
  1006. $total = 0;
  1007. foreach ($this->items as $item) {
  1008. $bits = $item->encodeBitStream($this->version);
  1009. if ($bits < 0)
  1010. return -1;
  1011. $total += $bits;
  1012. }
  1013. return $total;
  1014. }
  1015. public function convertData() {
  1016. $ver = $this->estimateVersion();
  1017. if ($ver > $this->getVersion()) {
  1018. $this->setVersion($ver);
  1019. }
  1020. for (;;) {
  1021. $bits = $this->createBitStream();
  1022. if ($bits < 0)
  1023. return -1;
  1024. $ver = QRspec::getMinimumVersion((int) (($bits + 7) / 8), $this->level);
  1025. if ($ver < 0) {
  1026. throw new Exception('WRONG VERSION');
  1027. return -1;
  1028. } else if ($ver > $this->getVersion()) {
  1029. $this->setVersion($ver);
  1030. } else {
  1031. break;
  1032. }
  1033. }
  1034. return 0;
  1035. }
  1036. public function appendPaddingBit(&$bstream) {
  1037. $bits = $bstream->size();
  1038. $maxwords = QRspec::getDataLength($this->version, $this->level);
  1039. $maxbits = $maxwords * 8;
  1040. if ($maxbits == $bits) {
  1041. return 0;
  1042. }
  1043. if ($maxbits - $bits < 5) {
  1044. return $bstream->appendNum($maxbits - $bits, 0);
  1045. }
  1046. $bits += 4;
  1047. $words = (int) (($bits + 7) / 8);
  1048. $padding = new QRbitstream();
  1049. $ret = $padding->appendNum($words * 8 - $bits + 4, 0);
  1050. if ($ret < 0)
  1051. return $ret;
  1052. $padlen = $maxwords - $words;
  1053. if ($padlen > 0) {
  1054. $padbuf = array();
  1055. for ($i = 0; $i < $padlen; $i++) {
  1056. $padbuf[$i] = ($i & 1) ? 0x11 : 0xec;
  1057. }
  1058. $ret = $padding->appendBytes($padlen, $padbuf);
  1059. if ($ret < 0)
  1060. return $ret;
  1061. }
  1062. $ret = $bstream->append($padding);
  1063. return $ret;
  1064. }
  1065. public function mergeBitStream() {
  1066. if ($this->convertData() < 0) {
  1067. return null;
  1068. }
  1069. $bstream = new QRbitstream();
  1070. foreach ($this->items as $item) {
  1071. $ret = $bstream->append($item->bstream);
  1072. if ($ret < 0) {
  1073. return null;
  1074. }
  1075. }
  1076. return $bstream;
  1077. }
  1078. public function getBitStream() {
  1079. $bstream = $this->mergeBitStream();
  1080. if ($bstream == null) {
  1081. return null;
  1082. }
  1083. $ret = $this->appendPaddingBit($bstream);
  1084. if ($ret < 0) {
  1085. return null;
  1086. }
  1087. return $bstream;
  1088. }
  1089. public function getByteStream() {
  1090. $bstream = $this->getBitStream();
  1091. if ($bstream == null) {
  1092. return null;
  1093. }
  1094. return $bstream->toByte();
  1095. }
  1096. }
  1097. class QRbitstream {
  1098. public $data = array();
  1099. public function size() {
  1100. return count($this->data);
  1101. }
  1102. public function allocate($setLength) {
  1103. $this->data = array_fill(0, $setLength, 0);
  1104. return 0;
  1105. }
  1106. public static function newFromNum($bits, $num) {
  1107. $bstream = new QRbitstream();
  1108. $bstream->allocate($bits);
  1109. $mask = 1 << ($bits - 1);
  1110. for ($i = 0; $i < $bits; $i++) {
  1111. if ($num & $mask) {
  1112. $bstream->data[$i] = 1;
  1113. } else {
  1114. $bstream->data[$i] = 0;
  1115. }
  1116. $mask = $mask >> 1;
  1117. }
  1118. return $bstream;
  1119. }
  1120. public static function newFromBytes($size, $data) {
  1121. $bstream = new QRbitstream();
  1122. $bstream->allocate($size * 8);
  1123. $p = 0;
  1124. for ($i = 0; $i < $size; $i++) {
  1125. $mask = 0x80;
  1126. for ($j = 0; $j < 8; $j++) {
  1127. if ($data[$i] & $mask) {
  1128. $bstream->data[$p] = 1;
  1129. } else {
  1130. $bstream->data[$p] = 0;
  1131. }
  1132. $p++;
  1133. $mask = $mask >> 1;
  1134. }
  1135. }
  1136. return $bstream;
  1137. }
  1138. public function append(QRbitstream $arg) {
  1139. if (is_null($arg)) {
  1140. return -1;
  1141. }
  1142. if ($arg->size() == 0) {
  1143. return 0;
  1144. }
  1145. if ($this->size() == 0) {
  1146. $this->data = $arg->data;
  1147. return 0;
  1148. }
  1149. $this->data = array_values(array_merge($this->data, $arg->data));
  1150. return 0;
  1151. }
  1152. public function appendNum($bits, $num) {
  1153. if ($bits == 0)
  1154. return 0;
  1155. $b = QRbitstream::newFromNum($bits, $num);
  1156. if (is_null($b))
  1157. return -1;
  1158. $ret = $this->append($b);
  1159. unset($b);
  1160. return $ret;
  1161. }
  1162. public function appendBytes($size, $data) {
  1163. if ($size == 0)
  1164. return 0;
  1165. $b = QRbitstream::newFromBytes($size, $data);
  1166. if (is_null($b))
  1167. return -1;
  1168. $ret = $this->append($b);
  1169. unset($b);
  1170. return $ret;
  1171. }
  1172. public function toByte() {
  1173. $size = $this->size();
  1174. if ($size == 0) {
  1175. return array();
  1176. }
  1177. $data = array_fill(0, (int) (($size + 7) / 8), 0);
  1178. $bytes = (int) ($size / 8);
  1179. $p = 0;
  1180. for ($i = 0; $i < $bytes; $i++) {
  1181. $v = 0;
  1182. for ($j = 0; $j < 8; $j++) {
  1183. $v = $v << 1;
  1184. $v |= $this->data[$p];
  1185. $p++;
  1186. }
  1187. $data[$i] = $v;
  1188. }
  1189. if ($size & 7) {
  1190. $v = 0;
  1191. for ($j = 0; $j < ($size & 7); $j++) {
  1192. $v = $v << 1;
  1193. $v |= $this->data[$p];
  1194. $p++;
  1195. }
  1196. $data[$bytes] = $v;
  1197. }
  1198. return $data;
  1199. }
  1200. }
  1201. class QRsplit {
  1202. public $dataStr = '';
  1203. public $input;
  1204. public $modeHint;
  1205. public function __construct($dataStr, $input, $modeHint) {
  1206. $this->dataStr = $dataStr;
  1207. $this->input = $input;
  1208. $this->modeHint = $modeHint;
  1209. }
  1210. public static function isdigitat($str, $pos) {
  1211. if ($pos >= strlen($str))
  1212. return false;
  1213. return ((ord($str[$pos]) >= ord('0')) && (ord($str[$pos]) <= ord('9')));
  1214. }
  1215. public static function isalnumat($str, $pos) {
  1216. if ($pos >= strlen($str))
  1217. return false;
  1218. return (QRinput::lookAnTable(ord($str[$pos])) >= 0);
  1219. }
  1220. public function identifyMode($pos) {
  1221. if ($pos >= strlen($this->dataStr))
  1222. return QR_MODE_NUL;
  1223. $c = $this->dataStr[$pos];
  1224. if (self::isdigitat($this->dataStr, $pos)) {
  1225. return QR_MODE_NUM;
  1226. } else if (self::isalnumat($this->dataStr, $pos)) {
  1227. return QR_MODE_AN;
  1228. } else if ($this->modeHint == QR_MODE_KANJI) {
  1229. if ($pos + 1 < strlen($this->dataStr)) {
  1230. $d = $this->dataStr[$pos + 1];
  1231. $word = (ord($c) << 8) | ord($d);
  1232. if (($word >= 0x8140 && $word <= 0x9ffc) || ($word >= 0xe040 && $word <= 0xebbf)) {
  1233. return QR_MODE_KANJI;
  1234. }
  1235. }
  1236. }
  1237. return QR_MODE_8;
  1238. }
  1239. public function eatNum() {
  1240. $ln = QRspec::lengthIndicator(QR_MODE_NUM, $this->input->getVersion());
  1241. $p = 0;
  1242. while (self::isdigitat($this->dataStr, $p)) {
  1243. $p++;
  1244. }
  1245. $run = $p;
  1246. $mode = $this->identifyMode($p);
  1247. if ($mode == QR_MODE_8) {
  1248. $dif = QRinput::estimateBitsModeNum($run) + 4 + $ln
  1249. + QRinput::estimateBitsMode8(1)
  1250. - QRinput::estimateBitsMode8($run + 1);
  1251. if ($dif > 0) {
  1252. return $this->eat8();
  1253. }
  1254. }
  1255. if ($mode == QR_MODE_AN) {
  1256. $dif = QRinput::estimateBitsModeNum($run) + 4 + $ln
  1257. + QRinput::estimateBitsModeAn(1)
  1258. - QRinput::estimateBitsModeAn($run + 1);
  1259. if ($dif > 0) {
  1260. return $this->eatAn();
  1261. }
  1262. }
  1263. $ret = $this->input->append(QR_MODE_NUM, $run, str_split($this->dataStr));
  1264. if ($ret < 0)
  1265. return -1;
  1266. return $run;
  1267. }
  1268. public function eatAn() {
  1269. $la = QRspec::lengthIndicator(QR_MODE_AN, $this->input->getVersion());
  1270. $ln = QRspec::lengthIndicator(QR_MODE_NUM, $this->input->getVersion());
  1271. $p = 0;
  1272. while (self::isalnumat($this->dataStr, $p)) {
  1273. if (self::isdigitat($this->dataStr, $p)) {
  1274. $q = $p;
  1275. while (self::isdigitat($this->dataStr, $q)) {
  1276. $q++;
  1277. }
  1278. $dif = QRinput::estimateBitsModeAn($p)
  1279. + QRinput::estimateBitsModeNum($q - $p) + 4 + $ln
  1280. - QRinput::estimateBitsModeAn($q);
  1281. if ($dif < 0) {
  1282. break;
  1283. } else {
  1284. $p = $q;
  1285. }
  1286. } else {
  1287. $p++;
  1288. }
  1289. }
  1290. $run = $p;
  1291. if (!self::isalnumat($this->dataStr, $p)) {
  1292. $dif = QRinput::estimateBitsModeAn($run) + 4 + $la
  1293. + QRinput::estimateBitsMode8(1)
  1294. - QRinput::estimateBitsMode8($run + 1);
  1295. if ($dif > 0) {
  1296. return $this->eat8();
  1297. }
  1298. }
  1299. $ret = $this->input->append(QR_MODE_AN, $run, str_split($this->dataStr));
  1300. if ($ret < 0)
  1301. return -1;
  1302. return $run;
  1303. }
  1304. public function eatKanji() {
  1305. $p = 0;
  1306. while ($this->identifyMode($p) == QR_MODE_KANJI) {
  1307. $p += 2;
  1308. }
  1309. $ret = $this->input->append(QR_MODE_KANJI, $p, str_split($this->dataStr));
  1310. if ($ret < 0)
  1311. return -1;
  1312. return $run;
  1313. }
  1314. public function eat8() {
  1315. $la = QRspec::lengthIndicator(QR_MODE_AN, $this->input->getVersion());
  1316. $ln = QRspec::lengthIndicator(QR_MODE_NUM, $this->input->getVersion());
  1317. $p = 1;
  1318. $dataStrLen = strlen($this->dataStr);
  1319. while ($p < $dataStrLen) {
  1320. $mode = $this->identifyMode($p);
  1321. if ($mode == QR_MODE_KANJI) {
  1322. break;
  1323. }
  1324. if ($mode == QR_MODE_NUM) {
  1325. $q = $p;
  1326. while (self::isdigitat($this->dataStr, $q)) {
  1327. $q++;
  1328. }
  1329. $dif = QRinput::estimateBitsMode8($p)
  1330. + QRinput::estimateBitsModeNum($q - $p) + 4 + $ln
  1331. - QRinput::estimateBitsMode8($q);
  1332. if ($dif < 0) {
  1333. break;
  1334. } else {
  1335. $p = $q;
  1336. }
  1337. } else if ($mode == QR_MODE_AN) {
  1338. $q = $p;
  1339. while (self::isalnumat($this->dataStr, $q)) {
  1340. $q++;
  1341. }
  1342. $dif = QRinput::estimateBitsMode8($p)
  1343. + QRinput::estimateBitsModeAn($q - $p) + 4 + $la
  1344. - QRinput::estimateBitsMode8($q);
  1345. if ($dif < 0) {
  1346. break;
  1347. } else {
  1348. $p = $q;
  1349. }
  1350. } else {
  1351. $p++;
  1352. }
  1353. }
  1354. $run = $p;
  1355. $ret = $this->input->append(QR_MODE_8, $run, str_split($this->dataStr));
  1356. if ($ret < 0)
  1357. return -1;
  1358. return $run;
  1359. }
  1360. public function splitString() {
  1361. while (strlen($this->dataStr) > 0) {
  1362. if ($this->dataStr == '')
  1363. return 0;
  1364. $mode = $this->identifyMode(0);
  1365. switch ($mode) {
  1366. case QR_MODE_NUM: $length = $this->eatNum();
  1367. break;
  1368. case QR_MODE_AN: $length = $this->eatAn();
  1369. break;
  1370. case QR_MODE_KANJI:
  1371. if ($hint == QR_MODE_KANJI)
  1372. $length = $this->eatKanji();
  1373. else
  1374. $length = $this->eat8();
  1375. break;
  1376. default: $length = $this->eat8();
  1377. break;
  1378. }
  1379. if ($length == 0)
  1380. return 0;
  1381. if ($length < 0)
  1382. return -1;
  1383. $this->dataStr = substr($this->dataStr, $length);
  1384. }
  1385. }
  1386. public function toUpper() {
  1387. $stringLen = strlen($this->dataStr);
  1388. $p = 0;
  1389. while ($p < $stringLen) {
  1390. $mode = self::identifyMode(substr($this->dataStr, $p), $this->modeHint);
  1391. if ($mode == QR_MODE_KANJI) {
  1392. $p += 2;
  1393. } else {
  1394. if (ord($this->dataStr[$p]) >= ord('a') && ord($this->dataStr[$p]) <= ord('z')) {
  1395. $this->dataStr[$p] = chr(ord($this->dataStr[$p]) - 32);
  1396. }
  1397. $p++;
  1398. }
  1399. }
  1400. return $this->dataStr;
  1401. }
  1402. public static function splitStringToQRinput($string, QRinput $input, $modeHint, $casesensitive = true) {
  1403. if (is_null($string) || $string == '\0' || $string == '') {
  1404. throw new Exception('empty string!!!');
  1405. }
  1406. $split = new QRsplit($string, $input, $modeHint);
  1407. if (!$casesensitive)
  1408. $split->toUpper();
  1409. return $split->splitString();
  1410. }
  1411. }
  1412. class QRrsItem {
  1413. public $mm;
  1414. public $nn;
  1415. public $alpha_to = array();
  1416. public $index_of = array();
  1417. public $genpoly = array();
  1418. public $nroots;
  1419. public $fcr;
  1420. public $prim;
  1421. public $iprim;
  1422. public $pad;
  1423. public $gfpoly;
  1424. public function modnn($x) {
  1425. while ($x >= $this->nn) {
  1426. $x -= $this->nn;
  1427. $x = ($x >> $this->mm) + ($x & $this->nn);
  1428. }
  1429. return $x;
  1430. }
  1431. public static function init_rs_char($symsize, $gfpoly, $fcr, $prim, $nroots, $pad) {
  1432. $rs = null;
  1433. if ($symsize < 0 || $symsize > 8)
  1434. return $rs;
  1435. if ($fcr < 0 || $fcr >= (1 << $symsize))
  1436. return $rs;
  1437. if ($prim <= 0 || $prim >= (1 << $symsize))
  1438. return $rs;
  1439. if ($nroots < 0 || $nroots >= (1 << $symsize))
  1440. return $rs;
  1441. if ($pad < 0 || $pad >= ((1 << $symsize) - 1 - $nroots))
  1442. return $rs;
  1443. $rs = new QRrsItem();
  1444. $rs->mm = $symsize;
  1445. $rs->nn = (1 << $symsize) - 1;
  1446. $rs->pad = $pad;
  1447. $rs->alpha_to = array_fill(0, $rs->nn + 1, 0);
  1448. $rs->index_of = array_fill(0, $rs->nn + 1, 0);
  1449. $NN = & $rs->nn;
  1450. $A0 = & $NN;
  1451. $rs->index_of[0] = $A0;
  1452. $rs->alpha_to[$A0] = 0;
  1453. $sr = 1;
  1454. for ($i = 0; $i < $rs->nn; $i++) {
  1455. $rs->index_of[$sr] = $i;
  1456. $rs->alpha_to[$i] = $sr;
  1457. $sr <<= 1;
  1458. if ($sr & (1 << $symsize)) {
  1459. $sr ^= $gfpoly;
  1460. }
  1461. $sr &= $rs->nn;
  1462. }
  1463. if ($sr != 1) {
  1464. $rs = NULL;
  1465. return $rs;
  1466. }
  1467. $rs->genpoly = array_fill(0, $nroots + 1, 0);
  1468. $rs->fcr = $fcr;
  1469. $rs->prim = $prim;
  1470. $rs->nroots = $nroots;
  1471. $rs->gfpoly = $gfpoly;
  1472. for ($iprim = 1; ($iprim % $prim) != 0; $iprim += $rs->nn)
  1473. ;
  1474. $rs->iprim = (int) ($iprim / $prim);
  1475. $rs->genpoly[0] = 1;
  1476. for ($i = 0, $root = $fcr * $prim; $i < $nroots; $i++, $root += $prim) {
  1477. $rs->genpoly[$i + 1] = 1;
  1478. for ($j = $i; $j > 0; $j--) {
  1479. if ($rs->genpoly[$j] != 0) {
  1480. $rs->genpoly[$j] = $rs->genpoly[$j - 1] ^ $rs->alpha_to[$rs->modnn($rs->index_of[$rs->genpoly[$j]] + $root)];
  1481. } else {
  1482. $rs->genpoly[$j] = $rs->genpoly[$j - 1];
  1483. }
  1484. }
  1485. $rs->genpoly[0] = $rs->alpha_to[$rs->modnn($rs->index_of[$rs->genpoly[0]] + $root)];
  1486. }
  1487. for ($i = 0; $i <= $nroots; $i++)
  1488. $rs->genpoly[$i] = $rs->index_of[$rs->genpoly[$i]];
  1489. return $rs;
  1490. }
  1491. public function encode_rs_char($data, &$parity) {
  1492. $MM = & $this->mm;
  1493. $NN = & $this->nn;
  1494. $ALPHA_TO = & $this->alpha_to;
  1495. $INDEX_OF = & $this->index_of;
  1496. $GENPOLY = & $this->genpoly;
  1497. $NROOTS = & $this->nroots;
  1498. $FCR = & $this->fcr;
  1499. $PRIM = & $this->prim;
  1500. $IPRIM = & $this->iprim;
  1501. $PAD = & $this->pad;
  1502. $A0 = & $NN;
  1503. $parity = array_fill(0, $NROOTS, 0);
  1504. for ($i = 0; $i < ($NN - $NROOTS - $PAD); $i++) {
  1505. $feedback = $INDEX_OF[$data[$i] ^ $parity[0]];
  1506. if ($feedback != $A0) {
  1507. $feedback = $this->modnn($NN - $GENPOLY[$NROOTS] + $feedback);
  1508. for ($j = 1; $j < $NROOTS; $j++) {
  1509. $parity[$j] ^= $ALPHA_TO[$this->modnn($feedback + $GENPOLY[$NROOTS - $j])];
  1510. }
  1511. }
  1512. array_shift($parity);
  1513. if ($feedback != $A0) {
  1514. array_push($parity, $ALPHA_TO[$this->modnn($feedback + $GENPOLY[0])]);
  1515. } else {
  1516. array_push($parity, 0);
  1517. }
  1518. }
  1519. }
  1520. }
  1521. class QRrs {
  1522. public static $items = array();
  1523. public static function init_rs($symsize, $gfpoly, $fcr, $prim, $nroots, $pad) {
  1524. foreach (self::$items as $rs) {
  1525. if ($rs->pad != $pad)
  1526. continue;
  1527. if ($rs->nroots != $nroots)
  1528. continue;
  1529. if ($rs->mm != $symsize)
  1530. continue;
  1531. if ($rs->gfpoly != $gfpoly)
  1532. continue;
  1533. if ($rs->fcr != $fcr)
  1534. continue;
  1535. if ($rs->prim != $prim)
  1536. continue;
  1537. return $rs;
  1538. }
  1539. $rs = QRrsItem::init_rs_char($symsize, $gfpoly, $fcr, $prim, $nroots, $pad);
  1540. array_unshift(self::$items, $rs);
  1541. return $rs;
  1542. }
  1543. }
  1544. define('N1', 3);
  1545. define('N2', 3);
  1546. define('N3', 40);
  1547. define('N4', 10);
  1548. class QRmask {
  1549. public $runLength = array();
  1550. public function __construct() {
  1551. $this->runLength = array_fill(0, QRSPEC_WIDTH_MAX + 1, 0);
  1552. }
  1553. public function writeFormatInformation($width, &$frame, $mask, $level) {
  1554. $blacks = 0;
  1555. $format = QRspec::getFormatInfo($mask, $level);
  1556. for ($i = 0; $i < 8; $i++) {
  1557. if ($format & 1) {
  1558. $blacks += 2;
  1559. $v = 0x85;
  1560. } else {
  1561. $v = 0x84;
  1562. }
  1563. $frame[8][$width - 1 - $i] = chr($v);
  1564. if ($i < 6) {
  1565. $frame[$i][8] = chr($v);
  1566. } else {
  1567. $frame[$i + 1][8] = chr($v);
  1568. }
  1569. $format = $format >> 1;
  1570. }
  1571. for ($i = 0; $i < 7; $i++) {
  1572. if ($format & 1) {
  1573. $blacks += 2;
  1574. $v = 0x85;
  1575. } else {
  1576. $v = 0x84;
  1577. }
  1578. $frame[$width - 7 + $i][8] = chr($v);
  1579. if ($i == 0) {
  1580. $frame[8][7] = chr($v);
  1581. } else {
  1582. $frame[8][6 - $i] = chr($v);
  1583. }
  1584. $format = $format >> 1;
  1585. }
  1586. return $blacks;
  1587. }
  1588. public function mask0($x, $y) {
  1589. return ($x + $y) & 1;
  1590. }
  1591. public function mask1($x, $y) {
  1592. return ($y & 1);
  1593. }
  1594. public function mask2($x, $y) {
  1595. return ($x % 3);
  1596. }
  1597. public function mask3($x, $y) {
  1598. return ($x + $y) % 3;
  1599. }
  1600. public function mask4($x, $y) {
  1601. return (((int) ($y / 2)) + ((int) ($x / 3))) & 1;
  1602. }
  1603. public function mask5($x, $y) {
  1604. return (($x * $y) & 1) + ($x * $y) % 3;
  1605. }
  1606. public function mask6($x, $y) {
  1607. return ((($x * $y) & 1) + ($x * $y) % 3) & 1;
  1608. }
  1609. public function mask7($x, $y) {
  1610. return ((($x * $y) % 3) + (($x + $y) & 1)) & 1;
  1611. }
  1612. private function generateMaskNo($maskNo, $width, $frame) {
  1613. $bitMask = array_fill(0, $width, array_fill(0, $width, 0));
  1614. for ($y = 0; $y < $width; $y++) {
  1615. for ($x = 0; $x < $width; $x++) {
  1616. if (ord($frame[$y][$x]) & 0x80) {
  1617. $bitMask[$y][$x] = 0;
  1618. } else {
  1619. $maskFunc = call_user_func(array($this, 'mask' . $maskNo), $x, $y);
  1620. $bitMask[$y][$x] = ($maskFunc == 0) ? 1 : 0;
  1621. }
  1622. }
  1623. }
  1624. return $bitMask;
  1625. }
  1626. public static function serial($bitFrame) {
  1627. $codeArr = array();
  1628. foreach ($bitFrame as $line)
  1629. $codeArr[] = join('', $line);
  1630. return gzcompress(join("\n", $codeArr), 9);
  1631. }
  1632. public static function unserial($code) {
  1633. $codeArr = array();
  1634. $codeLines = explode("\n", gzuncompress($code));
  1635. foreach ($codeLines as $line)
  1636. $codeArr[] = str_split($line);
  1637. return $codeArr;
  1638. }
  1639. public function makeMaskNo($maskNo, $width, $s, &$d, $maskGenOnly = false) {
  1640. $b = 0;
  1641. $bitMask = array();
  1642. $fileName = QR_CACHE_DIR . 'mask_' . $maskNo . DIRECTORY_SEPARATOR . 'mask_' . $width . '_' . $maskNo . '.dat';
  1643. if (QR_CACHEABLE) {
  1644. if (file_exists($fileName)) {
  1645. $bitMask = self::unserial(file_get_contents($fileName));
  1646. } else {
  1647. $bitMask = $this->generateMaskNo($maskNo, $width, $s, $d);
  1648. if (!file_exists(QR_CACHE_DIR . 'mask_' . $maskNo))
  1649. mkdir(QR_CACHE_DIR . 'mask_' . $maskNo);
  1650. file_put_contents($fileName, self::serial($bitMask));
  1651. }
  1652. } else {
  1653. $bitMask = $this->generateMaskNo($maskNo, $width, $s, $d);
  1654. }
  1655. if ($maskGenOnly)
  1656. return;
  1657. $d = $s;
  1658. for ($y = 0; $y < $width; $y++) {
  1659. for ($x = 0; $x < $width; $x++) {
  1660. if ($bitMask[$y][$x] == 1) {
  1661. $d[$y][$x] = chr(ord($s[$y][$x]) ^ (int) $bitMask[$y][$x]);
  1662. }
  1663. $b += (int) (ord($d[$y][$x]) & 1);
  1664. }
  1665. }
  1666. return $b;
  1667. }
  1668. public function makeMask($width, $frame, $maskNo, $level) {
  1669. $masked = array_fill(0, $width, str_repeat("\0", $width));
  1670. $this->makeMaskNo($maskNo, $width, $frame, $masked);
  1671. $this->writeFormatInformation($width, $masked, $maskNo, $level);
  1672. return $masked;
  1673. }
  1674. public function calcN1N3($length) {
  1675. $demerit = 0;
  1676. for ($i = 0; $i < $length; $i++) {
  1677. if ($this->runLength[$i] >= 5) {
  1678. $demerit += (N1 + ($this->runLength[$i] - 5));
  1679. }
  1680. if ($i & 1) {
  1681. if (($i >= 3) && ($i < ($length - 2)) && ($this->runLength[$i] % 3 == 0)) {
  1682. $fact = (int) ($this->runLength[$i] / 3);
  1683. if (($this->runLength[$i - 2] == $fact) &&
  1684. ($this->runLength[$i - 1] == $fact) &&
  1685. ($this->runLength[$i + 1] == $fact) &&
  1686. ($this->runLength[$i + 2] == $fact)) {
  1687. if (($this->runLength[$i - 3] < 0) || ($this->runLength[$i - 3] >= (4 * $fact))) {
  1688. $demerit += N3;
  1689. } else if ((($i + 3) >= $length) || ($this->runLength[$i + 3] >= (4 * $fact))) {
  1690. $demerit += N3;
  1691. }
  1692. }
  1693. }
  1694. }
  1695. }
  1696. return $demerit;
  1697. }
  1698. public function evaluateSymbol($width, $frame) {
  1699. $head = 0;
  1700. $demerit = 0;
  1701. for ($y = 0; $y < $width; $y++) {
  1702. $head = 0;
  1703. $this->runLength[0] = 1;
  1704. $frameY = $frame[$y];
  1705. if ($y > 0)
  1706. $frameYM = $frame[$y - 1];
  1707. for ($x = 0; $x < $width; $x++) {
  1708. if (($x > 0) && ($y > 0)) {
  1709. $b22 = ord($frameY[$x]) & ord($frameY[$x - 1]) & ord($frameYM[$x]) & ord($frameYM[$x - 1]);
  1710. $w22 = ord($frameY[$x]) | ord($frameY[$x - 1]) | ord($frameYM[$x]) | ord($frameYM[$x - 1]);
  1711. if (($b22 | ($w22 ^ 1)) & 1) {
  1712. $demerit += N2;
  1713. }
  1714. }
  1715. if (($x == 0) && (ord($frameY[$x]) & 1)) {
  1716. $this->runLength[0] = -1;
  1717. $head = 1;
  1718. $this->runLength[$head] = 1;
  1719. } else if ($x > 0) {
  1720. if ((ord($frameY[$x]) ^ ord($frameY[$x - 1])) & 1) {
  1721. $head++;
  1722. $this->runLength[$head] = 1;
  1723. } else {
  1724. $this->runLength[$head]++;
  1725. }
  1726. }
  1727. }
  1728. $demerit += $this->calcN1N3($head + 1);
  1729. }
  1730. for ($x = 0; $x < $width; $x++) {
  1731. $head = 0;
  1732. $this->runLength[0] = 1;
  1733. for ($y = 0; $y < $width; $y++) {
  1734. if ($y == 0 && (ord($frame[$y][$x]) & 1)) {
  1735. $this->runLength[0] = -1;
  1736. $head = 1;
  1737. $this->runLength[$head] = 1;
  1738. } else if ($y > 0) {
  1739. if ((ord($frame[$y][$x]) ^ ord($frame[$y - 1][$x])) & 1) {
  1740. $head++;
  1741. $this->runLength[$head] = 1;
  1742. } else {
  1743. $this->runLength[$head]++;
  1744. }
  1745. }
  1746. }
  1747. $demerit += $this->calcN1N3($head + 1);
  1748. }
  1749. return $demerit;
  1750. }
  1751. public function mask($width, $frame, $level) {
  1752. $minDemerit = PHP_INT_MAX;
  1753. $bestMaskNum = 0;
  1754. $bestMask = array();
  1755. $checked_masks = array(0, 1, 2, 3, 4, 5, 6, 7);
  1756. if (QR_FIND_FROM_RANDOM !== false) {
  1757. $howManuOut = 8 - (QR_FIND_FROM_RANDOM % 9);
  1758. for ($i = 0; $i < $howManuOut; $i++) {
  1759. $remPos = rand(0, count($checked_masks) - 1);
  1760. unset($checked_masks[$remPos]);
  1761. $checked_masks = array_values($checked_masks);
  1762. }
  1763. }
  1764. $bestMask = $frame;
  1765. foreach ($checked_masks as $i) {
  1766. $mask = array_fill(0, $width, str_repeat("\0", $width));
  1767. $demerit = 0;
  1768. $blacks = 0;
  1769. $blacks = $this->makeMaskNo($i, $width, $frame, $mask);
  1770. $blacks += $this->writeFormatInformation($width, $mask, $i, $level);
  1771. $blacks = (int) (100 * $blacks / ($width * $width));
  1772. $demerit = (int) ((int) (abs($blacks - 50) / 5) * N4);
  1773. $demerit += $this->evaluateSymbol($width, $mask);
  1774. if ($demerit < $minDemerit) {
  1775. $minDemerit = $demerit;
  1776. $bestMask = $mask;
  1777. $bestMaskNum = $i;
  1778. }
  1779. }
  1780. return $bestMask;
  1781. }
  1782. }
  1783. class QRrsblock {
  1784. public $dataLength;
  1785. public $data = array();
  1786. public $eccLength;
  1787. public $ecc = array();
  1788. public function __construct($dl, $data, $el, &$ecc, QRrsItem $rs) {
  1789. $rs->encode_rs_char($data, $ecc);
  1790. $this->dataLength = $dl;
  1791. $this->data = $data;
  1792. $this->eccLength = $el;
  1793. $this->ecc = $ecc;
  1794. }
  1795. }
  1796. ;
  1797. class QRrawcode {
  1798. public $version;
  1799. public $datacode = array();
  1800. public $ecccode = array();
  1801. public $blocks;
  1802. public $rsblocks = array();
  1803. public $count;
  1804. public $dataLength;
  1805. public $eccLength;
  1806. public $b1;
  1807. public function __construct(QRinput $input) {
  1808. $spec = array(0, 0, 0, 0, 0);
  1809. $this->datacode = $input->getByteStream();
  1810. if (is_null($this->datacode)) {
  1811. throw new Exception('null imput string');
  1812. }
  1813. QRspec::getEccSpec($input->getVersion(), $input->getErrorCorrectionLevel(), $spec);
  1814. $this->version = $input->getVersion();
  1815. $this->b1 = QRspec::rsBlockNum1($spec);
  1816. $this->dataLength = QRspec::rsDataLength($spec);
  1817. $this->eccLength = QRspec::rsEccLength($spec);
  1818. $this->ecccode = array_fill(0, $this->eccLength, 0);
  1819. $this->blocks = QRspec::rsBlockNum($spec);
  1820. $ret = $this->init($spec);
  1821. if ($ret < 0) {
  1822. throw new Exception('block alloc error');
  1823. return null;
  1824. }
  1825. $this->count = 0;
  1826. }
  1827. public function init(array $spec) {
  1828. $dl = QRspec::rsDataCodes1($spec);
  1829. $el = QRspec::rsEccCodes1($spec);
  1830. $rs = QRrs::init_rs(8, 0x11d, 0, 1, $el, 255 - $dl - $el);
  1831. $blockNo = 0;
  1832. $dataPos = 0;
  1833. $eccPos = 0;
  1834. for ($i = 0; $i < QRspec::rsBlockNum1($spec); $i++) {
  1835. $ecc = array_slice($this->ecccode, $eccPos);
  1836. $this->rsblocks[$blockNo] = new QRrsblock($dl, array_slice($this->datacode, $dataPos), $el, $ecc, $rs);
  1837. $this->ecccode = array_merge(array_slice($this->ecccode, 0, $eccPos), $ecc);
  1838. $dataPos += $dl;
  1839. $eccPos += $el;
  1840. $blockNo++;
  1841. }
  1842. if (QRspec::rsBlockNum2($spec) == 0)
  1843. return 0;
  1844. $dl = QRspec::rsDataCodes2($spec);
  1845. $el = QRspec::rsEccCodes2($spec);
  1846. $rs = QRrs::init_rs(8, 0x11d, 0, 1, $el, 255 - $dl - $el);
  1847. if ($rs == NULL)
  1848. return -1;
  1849. for ($i = 0; $i < QRspec::rsBlockNum2($spec); $i++) {
  1850. $ecc = array_slice($this->ecccode, $eccPos);
  1851. $this->rsblocks[$blockNo] = new QRrsblock($dl, array_slice($this->datacode, $dataPos), $el, $ecc, $rs);
  1852. $this->ecccode = array_merge(array_slice($this->ecccode, 0, $eccPos), $ecc);
  1853. $dataPos += $dl;
  1854. $eccPos += $el;
  1855. $blockNo++;
  1856. }
  1857. return 0;
  1858. }
  1859. public function getCode() {
  1860. $ret;
  1861. if ($this->count < $this->dataLength) {
  1862. $row = $this->count % $this->blocks;
  1863. $col = $this->count / $this->blocks;
  1864. if ($col >= $this->rsblocks[0]->dataLength) {
  1865. $row += $this->b1;
  1866. }
  1867. $ret = $this->rsblocks[$row]->data[$col];
  1868. } else if ($this->count < $this->dataLength + $this->eccLength) {
  1869. $row = ($this->count - $this->dataLength) % $this->blocks;
  1870. $col = ($this->count - $this->dataLength) / $this->blocks;
  1871. $ret = $this->rsblocks[$row]->ecc[$col];
  1872. } else {
  1873. return 0;
  1874. }
  1875. $this->count++;
  1876. return $ret;
  1877. }
  1878. }
  1879. class QRcode {
  1880. public $version;
  1881. public $width;
  1882. public $data;
  1883. public function encodeMask(QRinput $input, $mask) {
  1884. if ($input->getVersion() < 0 || $input->getVersion() > QRSPEC_VERSION_MAX) {
  1885. throw new Exception('wrong version');
  1886. }
  1887. if ($input->getErrorCorrectionLevel() > QR_ECLEVEL_H) {
  1888. throw new Exception('wrong level');
  1889. }
  1890. $raw = new QRrawcode($input);
  1891. QRtools::markTime('after_raw');
  1892. $version = $raw->version;
  1893. $width = QRspec::getWidth($version);
  1894. $frame = QRspec::newFrame($version);
  1895. $filler = new FrameFiller($width, $frame);
  1896. if (is_null($filler)) {
  1897. return NULL;
  1898. }
  1899. for ($i = 0; $i < $raw->dataLength + $raw->eccLength; $i++) {
  1900. $code = $raw->getCode();
  1901. $bit = 0x80;
  1902. for ($j = 0; $j < 8; $j++) {
  1903. $addr = $filler->next();
  1904. $filler->setFrameAt($addr, 0x02 | (($bit & $code) != 0));
  1905. $bit = $bit >> 1;
  1906. }
  1907. }
  1908. QRtools::markTime('after_filler');
  1909. unset($raw);
  1910. $j = QRspec::getRemainder($version);
  1911. for ($i = 0; $i < $j; $i++) {
  1912. $addr = $filler->next();
  1913. $filler->setFrameAt($addr, 0x02);
  1914. }
  1915. $frame = $filler->frame;
  1916. unset($filler);
  1917. $maskObj = new QRmask();
  1918. if ($mask < 0) {
  1919. if (QR_FIND_BEST_MASK) {
  1920. $masked = $maskObj->mask($width, $frame, $input->getErrorCorrectionLevel());
  1921. } else {
  1922. $masked = $maskObj->makeMask($width, $frame, (intval(QR_DEFAULT_MASK) % 8), $input->getErrorCorrectionLevel());
  1923. }
  1924. } else {
  1925. $masked = $maskObj->makeMask($width, $frame, $mask, $input->getErrorCorrectionLevel());
  1926. }
  1927. if ($masked == NULL) {
  1928. return NULL;
  1929. }
  1930. QRtools::markTime('after_mask');
  1931. $this->version = $version;
  1932. $this->width = $width;
  1933. $this->data = $masked;
  1934. return $this;
  1935. }
  1936. public function encodeInput(QRinput $input) {
  1937. return $this->encodeMask($input, -1);
  1938. }
  1939. public function encodeString8bit($string, $version, $level) {
  1940. if (string == NULL) {
  1941. throw new Exception('empty string!');
  1942. return NULL;
  1943. }
  1944. $input = new QRinput($version, $level);
  1945. if ($input == NULL)
  1946. return NULL;
  1947. $ret = $input->append($input, QR_MODE_8, strlen($string), str_split($string));
  1948. if ($ret < 0) {
  1949. unset($input);
  1950. return NULL;
  1951. }
  1952. return $this->encodeInput($input);
  1953. }
  1954. public function encodeString($string, $version, $level, $hint, $casesensitive) {
  1955. if ($hint != QR_MODE_8 && $hint != QR_MODE_KANJI) {
  1956. throw new Exception('bad hint');
  1957. return NULL;
  1958. }
  1959. $input = new QRinput($version, $level);
  1960. if ($input == NULL)
  1961. return NULL;
  1962. $ret = QRsplit::splitStringToQRinput($string, $input, $hint, $casesensitive);
  1963. if ($ret < 0) {
  1964. return NULL;
  1965. }
  1966. return $this->encodeInput($input);
  1967. }
  1968. public static function png($text, $outfile = false, $level = QR_ECLEVEL_L, $size = 3, $margin = 4, $saveandprint = false) {
  1969. $enc = QRencode::factory($level, $size, $margin);
  1970. return $enc->encodePNG($text, $outfile, $saveandprint = false);
  1971. }
  1972. public static function text($text, $outfile = false, $level = QR_ECLEVEL_L, $size = 3, $margin = 4) {
  1973. $enc = QRencode::factory($level, $size, $margin);
  1974. return $enc->encode($text, $outfile);
  1975. }
  1976. public static function raw($text, $outfile = false, $level = QR_ECLEVEL_L, $size = 3, $margin = 4) {
  1977. $enc = QRencode::factory($level, $size, $margin);
  1978. return $enc->encodeRAW($text, $outfile);
  1979. }
  1980. }
  1981. class FrameFiller {
  1982. public $width;
  1983. public $frame;
  1984. public $x;
  1985. public $y;
  1986. public $dir;
  1987. public $bit;
  1988. public function __construct($width, &$frame) {
  1989. $this->width = $width;
  1990. $this->frame = $frame;
  1991. $this->x = $width - 1;
  1992. $this->y = $width - 1;
  1993. $this->dir = -1;
  1994. $this->bit = -1;
  1995. }
  1996. public function setFrameAt($at, $val) {
  1997. $this->frame[$at['y']][$at['x']] = chr($val);
  1998. }
  1999. public function getFrameAt($at) {
  2000. return ord($this->frame[$at['y']][$at['x']]);
  2001. }
  2002. public function next() {
  2003. do {
  2004. if ($this->bit == -1) {
  2005. $this->bit = 0;
  2006. return array('x' => $this->x, 'y' => $this->y);
  2007. }
  2008. $x = $this->x;
  2009. $y = $this->y;
  2010. $w = $this->width;
  2011. if ($this->bit == 0) {
  2012. $x--;
  2013. $this->bit++;
  2014. } else {
  2015. $x++;
  2016. $y += $this->dir;
  2017. $this->bit--;
  2018. }
  2019. if ($this->dir < 0) {
  2020. if ($y < 0) {
  2021. $y = 0;
  2022. $x -= 2;
  2023. $this->dir = 1;
  2024. if ($x == 6) {
  2025. $x--;
  2026. $y = 9;
  2027. }
  2028. }
  2029. } else {
  2030. if ($y == $w) {
  2031. $y = $w - 1;
  2032. $x -= 2;
  2033. $this->dir = -1;
  2034. if ($x == 6) {
  2035. $x--;
  2036. $y -= 8;
  2037. }
  2038. }
  2039. }
  2040. if ($x < 0 || $y < 0)
  2041. return null;
  2042. $this->x = $x;
  2043. $this->y = $y;
  2044. } while (ord($this->frame[$y][$x]) & 0x80);
  2045. return array('x' => $x, 'y' => $y);
  2046. }
  2047. }
  2048. ;
  2049. class QRencode {
  2050. public $casesensitive = true;
  2051. public $eightbit = false;
  2052. public $version = 0;
  2053. public $size = 3;
  2054. public $margin = 4;
  2055. public $structured = 0;
  2056. public $level = QR_ECLEVEL_L;
  2057. public $hint = QR_MODE_8;
  2058. public static function factory($level = QR_ECLEVEL_L, $size = 3, $margin = 4) {
  2059. $enc = new QRencode();
  2060. $enc->size = $size;
  2061. $enc->margin = $margin;
  2062. switch ($level . '') {
  2063. case '0':
  2064. case '1':
  2065. case '2':
  2066. case '3':
  2067. $enc->level = $level;
  2068. break;
  2069. case 'l':
  2070. case 'L':
  2071. $enc->level = QR_ECLEVEL_L;
  2072. break;
  2073. case 'm':
  2074. case 'M':
  2075. $enc->level = QR_ECLEVEL_M;
  2076. break;
  2077. case 'q':
  2078. case 'Q':
  2079. $enc->level = QR_ECLEVEL_Q;
  2080. break;
  2081. case 'h':
  2082. case 'H':
  2083. $enc->level = QR_ECLEVEL_H;
  2084. break;
  2085. }
  2086. return $enc;
  2087. }
  2088. public function encodeRAW($intext, $outfile = false) {
  2089. $code = new QRcode();
  2090. if ($this->eightbit) {
  2091. $code->encodeString8bit($intext, $this->version, $this->level);
  2092. } else {
  2093. $code->encodeString($intext, $this->version, $this->level, $this->hint, $this->casesensitive);
  2094. }
  2095. return $code->data;
  2096. }
  2097. public function encode($intext, $outfile = false) {
  2098. $code = new QRcode();
  2099. if ($this->eightbit) {
  2100. $code->encodeString8bit($intext, $this->version, $this->level);
  2101. } else {
  2102. $code->encodeString($intext, $this->version, $this->level, $this->hint, $this->casesensitive);
  2103. }
  2104. QRtools::markTime('after_encode');
  2105. if ($outfile !== false) {
  2106. file_put_contents($outfile, join("\n", QRtools::binarize($code->data)));
  2107. } else {
  2108. return QRtools::binarize($code->data);
  2109. }
  2110. }
  2111. public function encodePNG($intext, $outfile = false, $saveandprint = false) {
  2112. try {
  2113. ob_start();
  2114. $tab = $this->encode($intext);
  2115. $err = ob_get_contents();
  2116. ob_end_clean();
  2117. if ($err != '')
  2118. QRtools::log($outfile, $err);
  2119. $maxSize = (int) (QR_PNG_MAXIMUM_SIZE / (count($tab) + 2 * $this->margin));
  2120. QRimage::png($tab, $outfile, min(max(1, $this->size), $maxSize), $this->margin, $saveandprint);
  2121. } catch (Exception $e) {
  2122. QRtools::log($outfile, $e->getMessage());
  2123. }
  2124. }
  2125. }
  2126. ?>