dolibarr 21.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 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 dol_syslog(get_class($this)."::buildBarCode $code,$encoding,$scale,$mode,$filebarcode");
163 if ($code) {
164 $result = barcode_print($code, $encoding, $scale, $mode, $filebarcode);
165 }
166
167 if (!is_array($result)) {
168 $this->error = $result;
169 if (empty($nooutputiferror)) {
170 print dol_escape_htmltag($this->error);
171 }
172 return -1;
173 }
174
175 return 1;
176 }
177
188 public function writeBarCode($code, $encoding, $readable = 'Y', $scale = 1, $nooutputiferror = 0)
189 {
190 global $conf, $langs;
191
192 dol_mkdir($conf->barcode->dir_temp);
193 if (!is_writable($conf->barcode->dir_temp)) {
194 if ($langs instanceof Translate) {
195 $this->error = $langs->transnoentities("ErrorFailedToWriteInTempDirectory", $conf->barcode->dir_temp);
196 } else {
197 $this->error = "ErrorFailedToWriteInTempDirectory ".$conf->barcode->dir_temp;
198 }
199 dol_syslog('Error in write_file: ' . $this->error, LOG_ERR);
200 return -1;
201 }
202
203 $newcode = $code;
204 if (!preg_match('/^\w+$/', $code) || dol_strlen($code) > 32) {
205 $newcode = dol_hash($newcode, 'md5'); // No need for security here, we can use md5
206 }
207
208 global $filebarcode;
209 $filebarcode = $conf->barcode->dir_temp . '/barcode_' . $newcode . '_' . $encoding . '.png';
210
211 $result = $this->buildBarCode($code, $encoding, $readable, $scale, $nooutputiferror);
212
213 return $result;
214 }
215}
if( $user->socid > 0) if(! $user->hasRight('accounting', 'chartofaccount')) $object
Definition card.php:66
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)
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...
global $conf
The following vars must be defined: $type2label $form $conf, $lang, The following vars may also be de...
Definition member.php:79
dol_hash($chain, $type='0', $nosalt=0)
Returns a hash (non reversible encryption) of a string.