dolibarr 24.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 * Copyright (C) 2024-2025 MDW <mdeweerd@users.noreply.github.com>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <https://www.gnu.org/licenses/>.
18 * or see https://www.gnu.org/
19 */
20
27require_once DOL_DOCUMENT_ROOT.'/core/modules/barcode/modules_barcode.class.php';
28require_once DOL_DOCUMENT_ROOT.'/core/lib/barcode.lib.php'; // This is to include def like $genbarcode_loc and $font_loc
29
30
35{
40 public $version = 'dolibarr';
41
45 public $error = '';
46
47
53 public function isEnabled()
54 {
55 return true;
56 }
57
58
65 public function info($langs)
66 {
67 global $langs;
68
69 $key = 'BarcodeInternalEngine';
70 $trans = $langs->trans('BarcodeInternalEngine');
71
72 return ($trans != $key) ? $trans : 'Internal engine';
73 }
74
82 public function canBeActivated($object)
83 {
84 global $langs;
85
86 return true;
87 }
88
89
96 public function encodingIsSupported($encoding)
97 {
98 global $genbarcode_loc;
99 //print 'genbarcode_loc='.$genbarcode_loc.' encoding='.$encoding;exit;
100
101 $supported = 0;
102 if ($encoding == 'EAN13') {
103 $supported = 1;
104 }
105 if ($encoding == 'ISBN') {
106 $supported = 1;
107 }
108 if ($encoding == 'UPC') {
109 $supported = 1;
110 }
111 // Formats that hangs on Windows (when genbarcode.exe for Windows is called, so they are not
112 // activated on Windows)
113 if (file_exists($genbarcode_loc) && empty($_SERVER["WINDIR"])) {
114 if ($encoding == 'EAN8') {
115 $supported = 1;
116 }
117 if ($encoding == 'C39') {
118 $supported = 1;
119 }
120 if ($encoding == 'C128') {
121 $supported = 1;
122 }
123 }
124 return $supported;
125 }
126
137 public function buildBarCode($code, $encoding, $readable = 'Y', $scale = 1, $nooutputiferror = 0)
138 {
139 if (!$this->encodingIsSupported($encoding)) {
140 return -1;
141 }
142
143 if ($encoding == 'EAN8' || $encoding == 'EAN13') {
144 $encoding = 'EAN';
145 }
146 if ($encoding == 'C39' || $encoding == 'C128') {
147 $encoding = substr($encoding, 1);
148 }
149
150 $mode = 'png';
151
152 $_GET["code"] = $code;
153 $_GET["encoding"] = $encoding;
154 $_GET["scale"] = $scale;
155 $_GET["mode"] = $mode;
156
157 global $filebarcode; // Retrieve variable where to store filename
158 if (empty($filebarcode)) {
159 $filebarcode = '';
160 }
161
162 $result = "No barcode ($code) to print";
163 dol_syslog(get_class($this)."::buildBarCode $code,$encoding,$scale,$mode,$filebarcode");
164 if ($code) {
165 $result = barcode_print($code, $encoding, $scale, $mode, $filebarcode);
166 }
167
168 if (!is_array($result)) {
169 $this->error = $result;
170 if (empty($nooutputiferror)) {
171 print dol_escape_htmltag($this->error);
172 }
173 return -1;
174 }
175
176 return 1;
177 }
178
189 public function writeBarCode($code, $encoding, $readable = 'Y', $scale = 1, $nooutputiferror = 0)
190 {
191 global $conf, $langs;
192
193 dol_mkdir($conf->barcode->dir_temp);
194 if (!is_writable($conf->barcode->dir_temp)) {
195 if ($langs instanceof Translate) {
196 $this->error = $langs->transnoentities("ErrorFailedToWriteInTempDirectory", $conf->barcode->dir_temp);
197 } else {
198 $this->error = "ErrorFailedToWriteInTempDirectory ".$conf->barcode->dir_temp;
199 }
200 dol_syslog('Error in write_file: ' . $this->error, LOG_ERR);
201 return -1;
202 }
203
204 $newcode = $code;
205 if (!preg_match('/^\w+$/', $code) || dol_strlen($code) > 32) {
206 $newcode = dol_hash($newcode, 'md5'); // No need for security here, we can use md5
207 }
208
209 global $filebarcode;
210 $filebarcode = $conf->barcode->dir_temp . '/barcode_' . $newcode . '_' . $encoding . '.png';
211
212 $result = $this->buildBarCode($code, $encoding, $readable, $scale, $nooutputiferror);
213
214 return $result;
215 }
216}
if(! $sortfield) if(! $sortorder) $object
Definition account.php:100
if(getDolGlobalString( 'GENBARCODE_LOCATION')) barcode_print($code, $encoding="ANY", $scale=2, $mode="png", $filebarcode='')
Print barcode.
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)
if(!isModEnabled('ai')||!getDolGlobalString('AI_ASSISTANT_ENABLED')) global $conf
The main.inc.php has been included so the following variable are now defined:
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, $mode=0)
Returns a hash (non reversible encryption) of a string.