QRinput

The Gravity Forms QRinput class.

Defined (1)

The class is defined in the following location(s).

/includes/phpqrcode/phpqrcode.php  
  1. class QRinput { 
  2.  
  3. public $items; 
  4.  
  5. private $version; 
  6. private $level; 
  7.  
  8. //---------------------------------------------------------------------- 
  9. public function __construct($version = 0, $level = QR_ECLEVEL_L) 
  10. if ($version < 0 || $version > QRSPEC_VERSION_MAX || $level > QR_ECLEVEL_H) { 
  11. throw new Exception('Invalid version no'); 
  12. return NULL; 
  13.  
  14. $this->version = $version; 
  15. $this->level = $level; 
  16.  
  17. //---------------------------------------------------------------------- 
  18. public function getVersion() 
  19. return $this->version; 
  20.  
  21. //---------------------------------------------------------------------- 
  22. public function setVersion($version) 
  23. if($version < 0 || $version > QRSPEC_VERSION_MAX) { 
  24. throw new Exception('Invalid version no'); 
  25. return -1; 
  26.  
  27. $this->version = $version; 
  28.  
  29. return 0; 
  30.  
  31. //---------------------------------------------------------------------- 
  32. public function getErrorCorrectionLevel() 
  33. return $this->level; 
  34.  
  35. //---------------------------------------------------------------------- 
  36. public function setErrorCorrectionLevel($level) 
  37. if($level > QR_ECLEVEL_H) { 
  38. throw new Exception('Invalid ECLEVEL'); 
  39. return -1; 
  40.  
  41. $this->level = $level; 
  42.  
  43. return 0; 
  44.  
  45. //---------------------------------------------------------------------- 
  46. public function appendEntry(QRinputItem $entry) 
  47. $this->items[] = $entry; 
  48.  
  49. //---------------------------------------------------------------------- 
  50. public function append($mode, $size, $data) 
  51. try { 
  52. $entry = new QRinputItem($mode, $size, $data); 
  53. $this->items[] = $entry; 
  54. return 0; 
  55. } catch (Exception $e) { 
  56. return -1; 
  57.  
  58. //---------------------------------------------------------------------- 
  59.  
  60. public function insertStructuredAppendHeader($size, $index, $parity) 
  61. if( $size > MAX_STRUCTURED_SYMBOLS ) { 
  62. throw new Exception('insertStructuredAppendHeader wrong size'); 
  63.  
  64. if( $index <= 0 || $index > MAX_STRUCTURED_SYMBOLS ) { 
  65. throw new Exception('insertStructuredAppendHeader wrong index'); 
  66.  
  67. $buf = array($size, $index, $parity); 
  68.  
  69. try { 
  70. $entry = new QRinputItem(QR_MODE_STRUCTURE, 3, buf); 
  71. array_unshift($this->items, $entry); 
  72. return 0; 
  73. } catch (Exception $e) { 
  74. return -1; 
  75.  
  76. //---------------------------------------------------------------------- 
  77. public function calcParity() 
  78. $parity = 0; 
  79.  
  80. foreach($this->items as $item) { 
  81. if($item->mode != QR_MODE_STRUCTURE) { 
  82. for($i=$item->size-1; $i>=0; $i--) { 
  83. $parity ^= $item->data[$i]; 
  84.  
  85. return $parity; 
  86.  
  87. //---------------------------------------------------------------------- 
  88. public static function checkModeNum($size, $data) 
  89. for($i=0; $i<$size; $i++) { 
  90. if((ord($data[$i]) < ord('0')) || (ord($data[$i]) > ord('9'))) { 
  91. return false; 
  92.  
  93. return true; 
  94.  
  95. //---------------------------------------------------------------------- 
  96. public static function estimateBitsModeNum($size) 
  97. $w = (int)$size / 3; 
  98. $bits = $w * 10; 
  99.  
  100. switch($size - $w * 3) { 
  101. case 1: 
  102. $bits += 4; 
  103. break; 
  104. case 2: 
  105. $bits += 7; 
  106. break; 
  107. default: 
  108. break; 
  109.  
  110. return $bits; 
  111.  
  112. //---------------------------------------------------------------------- 
  113. public static $anTable = array( 
  114. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,  
  115. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,  
  116. 36, -1, -1, -1, 37, 38, -1, -1, -1, -1, 39, 40, -1, 41, 42, 43,  
  117. 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 44, -1, -1, -1, -1, -1,  
  118. -1, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24,  
  119. 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, -1, -1, -1, -1, -1,  
  120. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,  
  121. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 
  122. ); 
  123.  
  124. //---------------------------------------------------------------------- 
  125. public static function lookAnTable($c) 
  126. return (($c > 127)?-1:self::$anTable[$c]); 
  127.  
  128. //---------------------------------------------------------------------- 
  129. public static function checkModeAn($size, $data) 
  130. for($i=0; $i<$size; $i++) { 
  131. if (self::lookAnTable(ord($data[$i])) == -1) { 
  132. return false; 
  133.  
  134. return true; 
  135.  
  136. //---------------------------------------------------------------------- 
  137. public static function estimateBitsModeAn($size) 
  138. $w = (int)($size / 2); 
  139. $bits = $w * 11; 
  140.  
  141. if($size & 1) { 
  142. $bits += 6; 
  143.  
  144. return $bits; 
  145.  
  146. //---------------------------------------------------------------------- 
  147. public static function estimateBitsMode8($size) 
  148. return $size * 8; 
  149.  
  150. //---------------------------------------------------------------------- 
  151. public function estimateBitsModeKanji($size) 
  152. return (int)(($size / 2) * 13); 
  153.  
  154. //---------------------------------------------------------------------- 
  155. public static function checkModeKanji($size, $data) 
  156. if($size & 1) 
  157. return false; 
  158.  
  159. for($i=0; $i<$size; $i+=2) { 
  160. $val = (ord($data[$i]) << 8) | ord($data[$i+1]); 
  161. if( $val < 0x8140 
  162. || ($val > 0x9ffc && $val < 0xe040) 
  163. || $val > 0xebbf) { 
  164. return false; 
  165.  
  166. return true; 
  167.  
  168. /*********************************************************************** 
  169. * Validation 
  170. **********************************************************************/ 
  171.  
  172. public static function check($mode, $size, $data) 
  173. if($size <= 0) 
  174. return false; 
  175.  
  176. switch($mode) { 
  177. case QR_MODE_NUM: return self::checkModeNum($size, $data); break; 
  178. case QR_MODE_AN: return self::checkModeAn($size, $data); break; 
  179. case QR_MODE_KANJI: return self::checkModeKanji($size, $data); break; 
  180. case QR_MODE_8: return true; break; 
  181. case QR_MODE_STRUCTURE: return true; break; 
  182.  
  183. default: 
  184. break; 
  185.  
  186. return false; 
  187.  
  188.  
  189. //---------------------------------------------------------------------- 
  190. public function estimateBitStreamSize($version) 
  191. $bits = 0; 
  192.  
  193. foreach($this->items as $item) { 
  194. $bits += $item->estimateBitStreamSizeOfEntry($version); 
  195.  
  196. return $bits; 
  197.  
  198. //---------------------------------------------------------------------- 
  199. public function estimateVersion() 
  200. $version = 0; 
  201. $prev = 0; 
  202. do { 
  203. $prev = $version; 
  204. $bits = $this->estimateBitStreamSize($prev); 
  205. $version = QRspec::getMinimumVersion((int)(($bits + 7) / 8), $this->level); 
  206. if ($version < 0) { 
  207. return -1; 
  208. } while ($version > $prev); 
  209.  
  210. return $version; 
  211.  
  212. //---------------------------------------------------------------------- 
  213. public static function lengthOfCode($mode, $version, $bits) 
  214. $payload = $bits - 4 - QRspec::lengthIndicator($mode, $version); 
  215. switch($mode) { 
  216. case QR_MODE_NUM: 
  217. $chunks = (int)($payload / 10); 
  218. $remain = $payload - $chunks * 10; 
  219. $size = $chunks * 3; 
  220. if($remain >= 7) { 
  221. $size += 2; 
  222. } else if($remain >= 4) { 
  223. $size += 1; 
  224. break; 
  225. case QR_MODE_AN: 
  226. $chunks = (int)($payload / 11); 
  227. $remain = $payload - $chunks * 11; 
  228. $size = $chunks * 2; 
  229. if($remain >= 6) 
  230. $size++; 
  231. break; 
  232. case QR_MODE_8: 
  233. $size = (int)($payload / 8); 
  234. break; 
  235. case QR_MODE_KANJI: 
  236. $size = (int)(($payload / 13) * 2); 
  237. break; 
  238. case QR_MODE_STRUCTURE: 
  239. $size = (int)($payload / 8); 
  240. break; 
  241. default: 
  242. $size = 0; 
  243. break; 
  244.  
  245. $maxsize = QRspec::maximumWords($mode, $version); 
  246. if($size < 0) $size = 0; 
  247. if($size > $maxsize) $size = $maxsize; 
  248.  
  249. return $size; 
  250.  
  251. //---------------------------------------------------------------------- 
  252. public function createBitStream() 
  253. $total = 0; 
  254.  
  255. foreach($this->items as $item) { 
  256. $bits = $item->encodeBitStream($this->version); 
  257.  
  258. if($bits < 0) 
  259. return -1; 
  260.  
  261. $total += $bits; 
  262.  
  263. return $total; 
  264.  
  265. //---------------------------------------------------------------------- 
  266. public function convertData() 
  267. $ver = $this->estimateVersion(); 
  268. if($ver > $this->getVersion()) { 
  269. $this->setVersion($ver); 
  270.  
  271. for(;;) { 
  272. $bits = $this->createBitStream(); 
  273.  
  274. if($bits < 0) 
  275. return -1; 
  276.  
  277. $ver = QRspec::getMinimumVersion((int)(($bits + 7) / 8), $this->level); 
  278. if($ver < 0) { 
  279. throw new Exception('WRONG VERSION'); 
  280. return -1; 
  281. } else if($ver > $this->getVersion()) { 
  282. $this->setVersion($ver); 
  283. } else { 
  284. break; 
  285.  
  286. return 0; 
  287.  
  288. //---------------------------------------------------------------------- 
  289. public function appendPaddingBit(&$bstream) 
  290. $bits = $bstream->size(); 
  291. $maxwords = QRspec::getDataLength($this->version, $this->level); 
  292. $maxbits = $maxwords * 8; 
  293.  
  294. if ($maxbits == $bits) { 
  295. return 0; 
  296.  
  297. if ($maxbits - $bits < 5) { 
  298. return $bstream->appendNum($maxbits - $bits, 0); 
  299.  
  300. $bits += 4; 
  301. $words = (int)(($bits + 7) / 8); 
  302.  
  303. $padding = new QRbitstream(); 
  304. $ret = $padding->appendNum($words * 8 - $bits + 4, 0); 
  305.  
  306. if($ret < 0) 
  307. return $ret; 
  308.  
  309. $padlen = $maxwords - $words; 
  310.  
  311. if($padlen > 0) { 
  312.  
  313. $padbuf = array(); 
  314. for($i=0; $i<$padlen; $i++) { 
  315. $padbuf[$i] = ($i&1)?0x11:0xec; 
  316.  
  317. $ret = $padding->appendBytes($padlen, $padbuf); 
  318.  
  319. if($ret < 0) 
  320. return $ret; 
  321.  
  322.  
  323. $ret = $bstream->append($padding); 
  324.  
  325. return $ret; 
  326.  
  327. //---------------------------------------------------------------------- 
  328. public function mergeBitStream() 
  329. if($this->convertData() < 0) { 
  330. return null; 
  331.  
  332. $bstream = new QRbitstream(); 
  333.  
  334. foreach($this->items as $item) { 
  335. $ret = $bstream->append($item->bstream); 
  336. if($ret < 0) { 
  337. return null; 
  338.  
  339. return $bstream; 
  340.  
  341. //---------------------------------------------------------------------- 
  342. public function getBitStream() 
  343.  
  344. $bstream = $this->mergeBitStream(); 
  345.  
  346. if($bstream == null) { 
  347. return null; 
  348.  
  349. $ret = $this->appendPaddingBit($bstream); 
  350. if($ret < 0) { 
  351. return null; 
  352.  
  353. return $bstream; 
  354.  
  355. //---------------------------------------------------------------------- 
  356. public function getByteStream() 
  357. $bstream = $this->getBitStream(); 
  358. if($bstream == null) { 
  359. return null; 
  360.  
  361. return $bstream->toByte();