Skip to content

Commit 698865f

Browse files
committed
v1.0.5 adding exception patch to upstream php-barcode-generator; catching exception for invalid barcode; little code cleanning; chmod disabled for now (chown was already), until rebuilt as an option; more tests and errors detected
1 parent 1bdba21 commit 698865f

File tree

2 files changed

+109
-99
lines changed

2 files changed

+109
-99
lines changed

barcode.php

Lines changed: 108 additions & 99 deletions
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,11 @@
1414

1515
require_once 'vendor/autoload.php';
1616

17-
define("_BC_VERSION", "1.0.4");
17+
define("_BC_VERSION", "1.0.5");
1818

19-
# permission to set to barcode files
20-
define("_BC_PERMISSION", 0644);
21-
# group to set to barcode files (disabled at bot)
22-
define("_BC_SYSGROUP", "yourGrpHere");
23-
# default padding for barcode
19+
# default padding for cli messages
2420
define("_BC_PADDING", 30);
2521

26-
2722
$verbose = false;
2823
$quiet = false;
2924

@@ -37,6 +32,9 @@
3732
$height = 30;
3833
$color = '#000000';
3934

35+
#$_defPermission = $false; # to be implemented
36+
#$_defGroup = $false; # to be implemented
37+
4038
$encodings_list = array(
4139
'CODE_39',
4240
'CODE_39_CHECKSUM',
@@ -80,12 +78,105 @@
8078
sort($formats_list);
8179

8280

81+
/////////////////// PRINT HELP
82+
83+
// prints help information
84+
function print_help($getopt) {
85+
global $encodings_list;
86+
global $formats_list;
87+
88+
echo $getopt->getHelpText(_BC_PADDING);
89+
echo "\nRequired Options and Parameters:\n";
90+
echo " -e <encoding>\n";
91+
echo " -f <output format>\n";
92+
echo " <input string>\n";
93+
echo " <output file>\n";
94+
echo "\nOutput Formats:\n";
95+
foreach($formats_list as $for) {
96+
echo " $for\n";
97+
}
98+
echo "\nEncodings:\n";
99+
foreach($encodings_list as $enc) {
100+
echo " $enc\n";
101+
}
102+
echo "\nExamples:\n";
103+
echo " barcode -f HTML -e CODE_39 \"1234567890\" \"/tmp/1234567890.html\"\n";
104+
echo " barcode -e CODE_128 -f PNG -c \"#888\" -w 3 -h 50 \"AGREATBAR\" \"/tmp/AGREATBAR.png\"\n";
105+
echo " barcode \"1234567890\" \"/tmp/mybar.svg\" --encoding EAN_13 --format SVG\n";
106+
}
107+
108+
109+
/////////////////// CREATE BASH SCRIPT
110+
111+
// creates a bash script named barcode that will run this script
112+
// from anywhere on the system. Assumes barcode.php is running
113+
// on its final installation location
114+
function create_bash_script() {
115+
$error = true;
116+
$bc_path = __FILE__;
117+
$bash_path = dirname($bc_path) . DIRECTORY_SEPARATOR . "barcode";
118+
119+
$bash_script = <<<EOF
120+
#!/usr/bin/env bash
121+
122+
##################################################
123+
# Gustavo Arnosti Neves - 2016 Jul 11
124+
# Simple bash script for global barcode executable
125+
# PHP cli-barcode-generator
126+
#
127+
# Please run "./barcode.php --create-bash" to update this script
128+
# You can then use sudo make install to copy it to /usr/local/bin
129+
#
130+
# This won't work on windows
131+
#
132+
133+
BARCODE_LOCATION="$bc_path" # enter full path here
134+
/usr/bin/env php "\$BARCODE_LOCATION" "\$@"
135+
136+
EOF;
137+
138+
if (file_exists($bash_path)) {
139+
unlink($bash_path) or die("Could not remove old barcode script, are you running from it?");
140+
}
141+
142+
$error = file_put_contents($bash_path, $bash_script) === true ? false : true;
143+
$error = chmod($bash_path, 0755) === true ? false : true;
144+
145+
if ($error) {
146+
echo "\nAn error was detected during the process.\n";
147+
echo "Please check for permissions and try again.\n\n";
148+
exit(2);
149+
}
150+
echo "\nThe file \"$bash_path\" was successfully created.\n";
151+
echo "You may perform a system install to /usr/local/bin by issuing\n";
152+
echo "the command \"sudo make install\"\n\n";
153+
exit(0);
154+
}
155+
156+
83157
/////////////////// GETOPT STARTS
84158

85159
use Ulrichsg\Getopt\Getopt;
86160
use Ulrichsg\Getopt\Option;
87161
use Ulrichsg\Getopt\Argument;
88162

163+
// check if encoding callback
164+
function isEncoding($enc=null) {
165+
global $encodings_list;
166+
return in_array(strtoupper($enc), $encodings_list);
167+
}
168+
169+
// check if format callback
170+
function isFormat($format=null) {
171+
global $formats_list;
172+
return in_array(strtoupper($format), $formats_list);
173+
}
174+
175+
// check if empty callback
176+
function not_empty($str) {
177+
return (!empty($str));
178+
}
179+
89180
// define and configure options
90181
$getopt = new Getopt(array(
91182
(new Option('e', 'encoding', Getopt::REQUIRED_ARGUMENT))
@@ -171,52 +262,7 @@
171262
/////////////////// GETOPT ENDS
172263

173264

174-
/////////////////// CREATE BARCODE
175-
176-
// creates a bash script named barcode that will run this script
177-
// from anywhere on the system. Assumes barcode.php is running
178-
// on its final installation location
179-
function create_bash_script() {
180-
$error = true;
181-
$bc_path = __FILE__;
182-
$bash_path = dirname($bc_path) . DIRECTORY_SEPARATOR . "barcode";
183-
184-
$bash_script = <<<EOF
185-
#!/usr/bin/env bash
186-
187-
##################################################
188-
# Gustavo Arnosti Neves - 2016 Jul 11
189-
# Simple bash script for global barcode executable
190-
# PHP cli-barcode-generator
191-
#
192-
# Please run "./barcode.php --create-bash" to update this script
193-
# You can then use sudo make install to copy it to /usr/local/bin
194-
#
195-
# This won't work on windows
196-
#
197-
198-
BARCODE_LOCATION="$bc_path" # enter full path here
199-
/usr/bin/env php "\$BARCODE_LOCATION" "\$@"
200-
201-
EOF;
202-
203-
if (file_exists($bash_path)) {
204-
unlink($bash_path) or die("Could not remove old barcode script, are you running from it?");
205-
}
206-
207-
$error = file_put_contents($bash_path, $bash_script) === true ? false : true;
208-
$error = chmod($bash_path, 0755) === true ? false : true;
209-
210-
if ($error) {
211-
echo "\nAn error was detected during the process.\n";
212-
echo "Please check for permissions and try again.\n\n";
213-
exit(2);
214-
}
215-
echo "\nThe file \"$bash_path\" was successfully created.\n";
216-
echo "You may perform a system install to /usr/local/bin by issuing\n";
217-
echo "the command \"sudo make install\"\n\n";
218-
exit(0);
219-
}
265+
/////////////////// CREATE BARCODE STARTS
220266

221267
// get actual barcode type string
222268
$bc_type = constant('Picqer\Barcode\BarcodeGenerator::TYPE_'.$encoding);
@@ -240,7 +286,12 @@ function create_bash_script() {
240286
}
241287

242288
// generate de barcode
243-
$bc_data = $generator->getBarcode($bc_string, $bc_type, $width, $height, $color);
289+
try {
290+
$bc_data = $generator->getBarcode($bc_string, $bc_type, $width, $height, $color);
291+
} catch (Exception $e) {
292+
echo "Error: ".$e->getMessage()."\n";
293+
exit(1);
294+
}
244295

245296
// sanity check
246297
$tgtDir = dirname($bc_file);
@@ -256,52 +307,10 @@ function create_bash_script() {
256307
}
257308

258309
// set permissions and group
259-
chmod($bc_file, _BC_PERMISSION);
260-
#chgrp($bc_file, _BC_SYSGROUP);
261-
262-
263-
// prints help information
264-
function print_help($getopt) {
265-
global $encodings_list;
266-
global $formats_list;
267-
268-
echo $getopt->getHelpText(_BC_PADDING);
269-
echo "\nRequired Options and Parameters:\n";
270-
echo " -e <encoding>\n";
271-
echo " -f <output format>\n";
272-
echo " <input string>\n";
273-
echo " <output file>\n";
274-
echo "\nOutput Formats:\n";
275-
foreach($formats_list as $for) {
276-
echo " $for\n";
277-
}
278-
echo "\nEncodings:\n";
279-
foreach($encodings_list as $enc) {
280-
echo " $enc\n";
281-
}
282-
echo "\nExamples:\n";
283-
echo " barcode -f HTML -e CODE_39 \"1234567890\" \"/tmp/1234567890.html\"\n";
284-
echo " barcode -e CODE_128 -f PNG -c \"#888\" -w 3 -h 50 \"AGREATBAR\" \"/tmp/AGREATBAR.png\"\n";
285-
echo " barcode \"1234567890\" \"/tmp/mybar.svg\" --encoding EAN_13 --format SVG\n";
286-
}
287-
288-
// check if encoding callback
289-
function isEncoding($enc=null) {
290-
global $encodings_list;
291-
return in_array(strtoupper($enc), $encodings_list);
292-
}
293-
294-
// check if format callback
295-
function isFormat($format=null) {
296-
global $formats_list;
297-
return in_array(strtoupper($format), $formats_list);
298-
}
299-
300-
// check if empty callback
301-
function not_empty($str) {
302-
return (!empty($str));
303-
}
310+
#chmod($bc_file, $_defPermission);
311+
#chgrp($bc_file, $_defGroup);
304312

313+
/////////////////// CREATE BARCODE ENDS
305314

306315
// done
307316
exit(0);

vendor/picqer/php-barcode-generator/src/BarcodeGenerator.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2812,6 +2812,7 @@ protected function imb_tables($n, $size)
28122812

28132813
protected function convertBarcodeArrayToNewStyle($oldBarcodeArray)
28142814
{
2815+
if (empty($oldBarcodeArray['bcode'])) throw new \Exception("Could not process barcode! Seems like your barcode string is invalid for the proposed format.");
28152816
$newBarcodeArray = [];
28162817
$newBarcodeArray['code'] = $oldBarcodeArray['code'];
28172818
$newBarcodeArray['maxWidth'] = $oldBarcodeArray['maxw'];

0 commit comments

Comments
 (0)