Skip to content

Commit

Permalink
Check if required builtin functions are enabled
Browse files Browse the repository at this point in the history
This library relies on `system` and `exec`. Solves issue #168
  • Loading branch information
thiagoalessio committed Aug 13, 2019
1 parent 01602bf commit bd02583
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 0 deletions.
20 changes: 20 additions & 0 deletions src/FriendlyErrors.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,29 @@
class ImageNotFoundException extends \Exception {}
class TesseractNotFoundException extends \Exception {}
class UnsuccessfulCommandException extends \Exception {}
class DisabledRequiredFunctionsException extends \Exception {}

class FriendlyErrors
{
public static function checkDisabledFunctions()
{
$requiredFuncs = array('exec', 'system');
$disabledFuncs = explode(',', ini_get('disabled_functions'));
$disabledFuncs = array_map('trim', $disabledFuncs);

foreach($requiredFuncs as $func)
{
if (function_exists($func) && !in_array($func, $disabledFuncs))
continue;

$msg = "Error! TesseractOCR library requires \"$func\", ";
$msg.= 'but it is disabled on your system.';
$msg.= PHP_EOL . PHP_EOL;
$msg.= 'Check "disabled_functions" on your php.ini settings.';
throw new DisabledRequiredFunctionsException($msg);
}
}

public static function checkImagePath($image)
{
if (file_exists($image)) return;
Expand Down
1 change: 1 addition & 0 deletions src/TesseractOCR.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ public function __construct($image=null, $command=null)

public function run()
{
FriendlyErrors::checkDisabledFunctions();
FriendlyErrors::checkTesseractPresence($this->command->executable);
FriendlyErrors::checkImagePath($this->command->image);

Expand Down
20 changes: 20 additions & 0 deletions tests/EndToEnd/FriendlyErrors.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,29 @@
use thiagoalessio\TesseractOCR\ImageNotFoundException;
use thiagoalessio\TesseractOCR\TesseractNotFoundException;
use thiagoalessio\TesseractOCR\UnsuccessfulCommandException;
use thiagoalessio\TesseractOCR\DisabledRequiredFunctionsException;

class FriendlyErrors extends TestCase
{
public function testDisabledRequiredFunctions()
{
$originalDisabledFunctions = ini_get('disabled_functions');
ini_set('disabled_functions', "${originalDisabledFunctions},exec");

$expected = "Error! TesseractOCR library requires \"exec\", ";
$expected.= 'but it is disabled on your system.';
$expected.= PHP_EOL . PHP_EOL;
$expected.= 'Check "disabled_functions" on your php.ini settings.';
try {
(new TesseractOCR('./tests/EndToEnd/images/text.png'))->run();
throw new \Exception('DisabledRequiredFunctionsException not thrown');
} catch (DisabledRequiredFunctionsException $e) {
$this->assertEquals($expected, $e->getMessage());
}

ini_set('disabled_functions', $originalDisabledFunctions);
}

public function testImageNotFound()
{
$currentDir = realpath(
Expand Down

0 comments on commit bd02583

Please sign in to comment.