phpbarcode.modules.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. <?php
  2. /* Copyright (C) 2005-2013 Laurent Destailleur <eldy@users.sourceforge.net>
  3. * Copyright (C) 2005 Regis Houssin <regis.houssin@inodbox.com>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  17. * or see https://www.gnu.org/
  18. */
  19. /**
  20. * \file htdocs/core/modules/barcode/doc/phpbarcode.modules.php
  21. * \ingroup barcode
  22. * \brief File with class to generate barcode images using php internal lib barcode generator
  23. */
  24. require_once DOL_DOCUMENT_ROOT.'/core/modules/barcode/modules_barcode.class.php';
  25. require_once DOL_DOCUMENT_ROOT.'/core/lib/barcode.lib.php'; // This is to include def like $genbarcode_loc and $font_loc
  26. /**
  27. * Class to generate barcode images using php barcode generator
  28. */
  29. class modPhpbarcode extends ModeleBarCode
  30. {
  31. /**
  32. * Dolibarr version of the loaded document
  33. * @var string
  34. */
  35. public $version = 'dolibarr'; // 'development', 'experimental', 'dolibarr'
  36. /**
  37. * @var string Error code (or message)
  38. */
  39. public $error = '';
  40. /**
  41. * Return if a module can be used or not
  42. *
  43. * @return boolean true if module can be used
  44. */
  45. public function isEnabled()
  46. {
  47. return true;
  48. }
  49. /**
  50. * Return description
  51. *
  52. * @return string Texte descripif
  53. */
  54. public function info()
  55. {
  56. global $langs;
  57. $key = 'BarcodeInternalEngine';
  58. $trans = $langs->trans('BarcodeInternalEngine');
  59. return ($trans != $key) ? $trans : 'Internal engine';
  60. }
  61. /**
  62. * Checks if the numbers already in the database do not
  63. * cause conflicts that would prevent this numbering working.
  64. *
  65. * @return boolean false if conflict, true if ok
  66. */
  67. public function canBeActivated()
  68. {
  69. global $langs;
  70. return true;
  71. }
  72. /**
  73. * Return true if encoding is supported
  74. *
  75. * @param string $encoding Encoding norm
  76. * @return int >0 if supported, 0 if not
  77. */
  78. public function encodingIsSupported($encoding)
  79. {
  80. global $genbarcode_loc;
  81. //print 'genbarcode_loc='.$genbarcode_loc.' encoding='.$encoding;exit;
  82. $supported = 0;
  83. if ($encoding == 'EAN13') {
  84. $supported = 1;
  85. }
  86. if ($encoding == 'ISBN') {
  87. $supported = 1;
  88. }
  89. if ($encoding == 'UPC') {
  90. $supported = 1;
  91. }
  92. // Formats that hangs on Windows (when genbarcode.exe for Windows is called, so they are not
  93. // activated on Windows)
  94. if (file_exists($genbarcode_loc) && empty($_SERVER["WINDIR"])) {
  95. if ($encoding == 'EAN8') {
  96. $supported = 1;
  97. }
  98. if ($encoding == 'C39') {
  99. $supported = 1;
  100. }
  101. if ($encoding == 'C128') {
  102. $supported = 1;
  103. }
  104. }
  105. return $supported;
  106. }
  107. /**
  108. * Return an image file on the fly (no need to write on disk)
  109. *
  110. * @param string $code Value to encode
  111. * @param string $encoding Mode of encoding
  112. * @param string $readable Code can be read (What is this ? is this used ?)
  113. * @param integer $scale Scale
  114. * @param integer $nooutputiferror No output if error
  115. * @return int <0 if KO, >0 if OK
  116. */
  117. public function buildBarCode($code, $encoding, $readable = 'Y', $scale = 1, $nooutputiferror = 0)
  118. {
  119. global $_GET, $_SERVER;
  120. global $conf;
  121. global $genbarcode_loc, $bar_color, $bg_color, $text_color, $font_loc;
  122. if (!$this->encodingIsSupported($encoding)) {
  123. return -1;
  124. }
  125. if ($encoding == 'EAN8' || $encoding == 'EAN13') {
  126. $encoding = 'EAN';
  127. }
  128. if ($encoding == 'C39' || $encoding == 'C128') {
  129. $encoding = substr($encoding, 1);
  130. }
  131. $mode = 'png';
  132. $_GET["code"] = $code;
  133. $_GET["encoding"] = $encoding;
  134. $_GET["scale"] = $scale;
  135. $_GET["mode"] = $mode;
  136. dol_syslog(get_class($this)."::buildBarCode $code,$encoding,$scale,$mode");
  137. if ($code) {
  138. $result = barcode_print($code, $encoding, $scale, $mode);
  139. }
  140. if (!is_array($result)) {
  141. $this->error = $result;
  142. if (empty($nooutputiferror)) {
  143. print dol_escape_htmltag($this->error);
  144. }
  145. return -1;
  146. }
  147. return 1;
  148. }
  149. /**
  150. * Save an image file on disk (with no output)
  151. *
  152. * @param string $code Value to encode
  153. * @param string $encoding Mode of encoding
  154. * @param string $readable Code can be read
  155. * @param integer $scale Scale
  156. * @param integer $nooutputiferror No output if error
  157. * @return int <0 if KO, >0 if OK
  158. */
  159. public function writeBarCode($code, $encoding, $readable = 'Y', $scale = 1, $nooutputiferror = 0)
  160. {
  161. global $conf, $filebarcode, $langs;
  162. dol_mkdir($conf->barcode->dir_temp);
  163. if (!is_writable($conf->barcode->dir_temp)) {
  164. $this->error = $langs->transnoentities("ErrorFailedToWriteInTempDirectory", $conf->barcode->dir_temp);
  165. dol_syslog('Error in write_file: ' . $this->error, LOG_ERR);
  166. return -1;
  167. }
  168. $file = $conf->barcode->dir_temp . '/barcode_' . $code . '_' . $encoding . '.png';
  169. $filebarcode = $file; // global var to be used in barcode_outimage called by barcode_print in buildBarCode
  170. $result = $this->buildBarCode($code, $encoding, $readable, $scale, $nooutputiferror);
  171. return $result;
  172. }
  173. }