diff --git a/app/Models/SystemLog.php b/app/Models/SystemLog.php index 37f3f2518e21..0be18903a7d6 100644 --- a/app/Models/SystemLog.php +++ b/app/Models/SystemLog.php @@ -34,6 +34,7 @@ class SystemLog extends Model const CATEGORY_GATEWAY_RESPONSE = 1; const CATEGORY_MAIL = 2; const CATEGORY_WEBHOOK = 3; + const CATEGORY_PDF = 3; /* Event IDs*/ const EVENT_PAYMENT_RECONCILIATION_FAILURE = 10; @@ -47,7 +48,8 @@ class SystemLog extends Model const EVENT_MAIL_RETRY_QUEUE = 31; //we use this to queue emails that are spooled and not sent due to the email queue quota being exceeded. const EVENT_WEBHOOK_RESPONSE = 40; - + const EVENT_PDF_RESPONSE = 50; + /*Type IDs*/ const TYPE_PAYPAL = 300; const TYPE_STRIPE = 301; @@ -60,6 +62,8 @@ class SystemLog extends Model const TYPE_UPSTREAM_FAILURE = 401; const TYPE_WEBHOOK_RESPONSE = 500; + const TYPE_PDF_FAILURE = 600; + const TYPE_PDF_SUCCESS = 601; protected $fillable = [ 'client_id', diff --git a/app/Utils/PhantomJS/Phantom.php b/app/Utils/PhantomJS/Phantom.php index b3c10a274c94..50796ab5d381 100644 --- a/app/Utils/PhantomJS/Phantom.php +++ b/app/Utils/PhantomJS/Phantom.php @@ -11,10 +11,12 @@ namespace App\Utils\PhantomJS; +use App\Jobs\Util\SystemLogger; use App\Models\CreditInvitation; use App\Models\Design; use App\Models\InvoiceInvitation; use App\Models\QuoteInvitation; +use App\Models\SystemLog; use App\Services\PdfMaker\Design as PdfDesignModel; use App\Services\PdfMaker\Design as PdfMakerDesign; use App\Services\PdfMaker\PdfMaker as PdfMakerService; @@ -77,7 +79,7 @@ class Phantom $phantom_url = "https://phantomjscloud.com/api/browser/v2/{$key}/?request=%7Burl:%22{$url}%22,renderType:%22pdf%22%7D"; $pdf = CurlUtils::get($phantom_url); - // Storage::makeDirectory($path, 0775); + $this->checkMime($pdf, $invitation, $entity); $instance = Storage::disk(config('filesystems.default'))->put($file_path, $pdf); @@ -101,6 +103,36 @@ class Phantom return $response; } + /* Check if the returning PDF is valid. */ + private function checkMime($pdf, $invitation, $entity) + { + + $finfo = new \finfo(FILEINFO_MIME); + + if($finfo->buffer($pdf) != 'application/pdf; charset=binary') + { + SystemLogger::dispatch( + $pdf, + SystemLog::CATEGORY_PDF, + SystemLog::EVENT_PDF_RESPONSE, + SystemLog::TYPE_PDF_FAILURE, + $invitation->contact->client + ); + } + else { + + SystemLogger::dispatch( + "Entity PDF generated sucessfully => " . $invitation->{$entity}->number, + SystemLog::CATEGORY_PDF, + SystemLog::EVENT_PDF_RESPONSE, + SystemLog::TYPE_PDF_SUCCESS, + $invitation->contact->client + ); + + } + + } + public function displayInvitation(string $entity, string $invitation_key) { $key = $entity.'_id'; diff --git a/tests/Unit/Phantom/PhantomJsTest.php b/tests/Unit/Phantom/PhantomJsTest.php index 7ac11fdfbc6a..afb13d771dbf 100644 --- a/tests/Unit/Phantom/PhantomJsTest.php +++ b/tests/Unit/Phantom/PhantomJsTest.php @@ -35,6 +35,7 @@ class PhantomJsTest extends TestCase public function testInValidPdfMime() { + $pdf = file_get_contents(base_path('/tests/Unit/Phantom/invalid.pdf')); $finfo = new \finfo(FILEINFO_MIME);