dolibarr  20.0.0-beta
phpbarcode.modules.php
Go to the documentation of this file.
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 
26 require_once DOL_DOCUMENT_ROOT.'/core/modules/barcode/modules_barcode.class.php';
27 require_once DOL_DOCUMENT_ROOT.'/core/lib/barcode.lib.php'; // This is to include def like $genbarcode_loc and $font_loc
28 
29 
34 {
39  public $version = 'dolibarr'; // 'development', 'experimental', 'dolibarr'
40 
44  public $error = '';
45 
46 
52  public function isEnabled()
53  {
54  return true;
55  }
56 
57 
64  public function info($langs)
65  {
66  global $langs;
67 
68  $key = 'BarcodeInternalEngine';
69  $trans = $langs->trans('BarcodeInternalEngine');
70 
71  return ($trans != $key) ? $trans : 'Internal engine';
72  }
73 
81  public function canBeActivated($object)
82  {
83  global $langs;
84 
85  return true;
86  }
87 
88 
95  public function encodingIsSupported($encoding)
96  {
97  global $genbarcode_loc;
98  //print 'genbarcode_loc='.$genbarcode_loc.' encoding='.$encoding;exit;
99 
100  $supported = 0;
101  if ($encoding == 'EAN13') {
102  $supported = 1;
103  }
104  if ($encoding == 'ISBN') {
105  $supported = 1;
106  }
107  if ($encoding == 'UPC') {
108  $supported = 1;
109  }
110  // Formats that hangs on Windows (when genbarcode.exe for Windows is called, so they are not
111  // activated on Windows)
112  if (file_exists($genbarcode_loc) && empty($_SERVER["WINDIR"])) {
113  if ($encoding == 'EAN8') {
114  $supported = 1;
115  }
116  if ($encoding == 'C39') {
117  $supported = 1;
118  }
119  if ($encoding == 'C128') {
120  $supported = 1;
121  }
122  }
123  return $supported;
124  }
125 
136  public function buildBarCode($code, $encoding, $readable = 'Y', $scale = 1, $nooutputiferror = 0)
137  {
138  if (!$this->encodingIsSupported($encoding)) {
139  return -1;
140  }
141 
142  if ($encoding == 'EAN8' || $encoding == 'EAN13') {
143  $encoding = 'EAN';
144  }
145  if ($encoding == 'C39' || $encoding == 'C128') {
146  $encoding = substr($encoding, 1);
147  }
148 
149  $mode = 'png';
150 
151  $_GET["code"] = $code;
152  $_GET["encoding"] = $encoding;
153  $_GET["scale"] = $scale;
154  $_GET["mode"] = $mode;
155 
156  global $filebarcode; // Retrieve variable where to store filename
157  if (empty($filebarcode)) {
158  $filebarcode = '';
159  }
160 
161  dol_syslog(get_class($this)."::buildBarCode $code,$encoding,$scale,$mode,$filebarcode");
162  if ($code) {
163  $result = barcode_print($code, $encoding, $scale, $mode, $filebarcode);
164  }
165 
166  if (!is_array($result)) {
167  $this->error = $result;
168  if (empty($nooutputiferror)) {
169  print dol_escape_htmltag($this->error);
170  }
171  return -1;
172  }
173 
174  return 1;
175  }
176 
187  public function writeBarCode($code, $encoding, $readable = 'Y', $scale = 1, $nooutputiferror = 0)
188  {
189  global $conf, $langs;
190 
191  dol_mkdir($conf->barcode->dir_temp);
192  if (!is_writable($conf->barcode->dir_temp)) {
193  if ($langs instanceof Translate) {
194  $this->error = $langs->transnoentities("ErrorFailedToWriteInTempDirectory", $conf->barcode->dir_temp);
195  } else {
196  $this->error = "ErrorFailedToWriteInTempDirectory ".$conf->barcode->dir_temp;
197  }
198  dol_syslog('Error in write_file: ' . $this->error, LOG_ERR);
199  return -1;
200  }
201 
202  $newcode = $code;
203  if (!preg_match('/^\w+$/', $code) || dol_strlen($code) > 32) {
204  $newcode = dol_hash($newcode, 'md5'); // No need for security here, we can use md5
205  }
206 
207  global $filebarcode;
208  $filebarcode = $conf->barcode->dir_temp . '/barcode_' . $newcode . '_' . $encoding . '.png';
209 
210  $result = $this->buildBarCode($code, $encoding, $readable, $scale, $nooutputiferror);
211 
212  return $result;
213  }
214 }
if($user->socid > 0) if(! $user->hasRight('accounting', 'chartofaccount')) $object
Definition: card.php:58
if(getDolGlobalString('GENBARCODE_LOCATION')) barcode_print($code, $encoding="ANY", $scale=2, $mode="png", $filebarcode='')
Print barcode.
Definition: barcode.lib.php:83
Parent class for barcode document generators (image)
Class to manage translations.
Class to generate barcode images using php barcode generator.
encodingIsSupported($encoding)
Return true if encoding is supported.
isEnabled()
Return if a module can be used or not.
buildBarCode($code, $encoding, $readable='Y', $scale=1, $nooutputiferror=0)
Return an image file on the fly (no need to write on disk) with the HTTP content-type of image.
info($langs)
Return description.
canBeActivated($object)
Checks if the numbers already in the database do not cause conflicts that would prevent this numberin...
writeBarCode($code, $encoding, $readable='Y', $scale=1, $nooutputiferror=0)
Save an image file on disk (with no output)
dol_strlen($string, $stringencoding='UTF-8')
Make a strlen call.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.
dol_mkdir($dir, $dataroot='', $newmask='')
Creation of a directory (this can create recursive subdir)
dol_escape_htmltag($stringtoescape, $keepb=0, $keepn=0, $noescapetags='', $escapeonlyhtmltags=0, $cleanalsojavascript=0)
Returns text escaped for inclusion in HTML alt or title or value tags, or into values of HTML input f...
dol_hash($chain, $type='0', $nosalt=0)
Returns a hash (non reversible encryption) of a string.