<?php
 namespace App\Utils\Wkhtmltopdf; class Installer { private $libParentPath = '3parties/wkhtmltopdf/'; private $libExecpath = 'bin/'; private $libFullPath = ''; private $installed = false; private static $instance; private function __construct() { $CFG = \Engine\Loader_Cfg::getInstance()->getGlobal(); $this->libFullPath = APP_REAL_PATH.$CFG['paths']['files'].$this->libParentPath.$this->libExecpath; if (\Engine\Dir::check($this->libFullPath)) { $this->installed = true; } } static public function getInstance() { if (!self::$instance) { self::$instance = new self(); } return self::$instance; } public function install() { if ($this->installed) { return true; } $CFG = \Engine\Loader_Cfg::getInstance()->getGlobal(); $folderWkhtmltopdf = APP_REAL_PATH.$CFG['paths']['files'].$this->libParentPath; if ( !\Engine\Dir::create( $folderWkhtmltopdf ) ) { throw new \Exception\IO\Base("The folder '{$this->libFullPath}' couldn't be created."); } $requestObject = new \App\Lidia\GetWkhtmltopdfVersions\Request(); $requestObject->operation = 'getWkhtmltopdfVersions'; $responseObject = new \App\Lidia\GetWkhtmltopdfVersions\Response(); $apiObject = new \App\Lidia\GetWkhtmltopdfVersions\Api($CFG['global']['newLicenseServerAddress']); $apiObject->executeOperation($requestObject, $responseObject); $responseObject->check(); $operatingSystem = php_uname("m"); if ($operatingSystem == 'x86_64' || $operatingSystem == 'ia64' || $operatingSystem == 'amd64') { $arch = 'amd64'; } else { $arch = 'i386'; } $mkhtmltopdfVersions = $responseObject->getVersions(); $mkhtmltopdfVersions = array_filter($mkhtmltopdfVersions, function($mkhtmltopdfVersions) use ($arch) { if ($mkhtmltopdfVersions['version'] == '0.12.4' && $mkhtmltopdfVersions['arch'] == $arch && $mkhtmltopdfVersions['compression'] == 'zip' && count($mkhtmltopdfVersions['support']) == 1 && $mkhtmltopdfVersions['support'][0] == 'pdf' ) { return true; } return false; }); if (count($mkhtmltopdfVersions) != 1) { throw new \Exception\IO\Base("Lidia returned more versions of mkhtmltopdf available for plugin 'StatementOfAccount'."); } $mkhtmltopdfVersions = reset($mkhtmltopdfVersions); $conn = new \App\Utils\FileTransfer\HttpTransfer(); $tempFileName = \Engine\File::tempnam(sprintf("wkhtmltopdf-%s.zip", \Engine\RandomGenerator::randomString(10))); $conn->downloadFile($mkhtmltopdfVersions['url'], $tempFileName); \Log::getInstance()->info(sprintf("[%s] Downloaded the wkhtmltopdf compress library.", __METHOD__), [ 'version' => new \Engine\Logger\Context\ArrayAssoc($mkhtmltopdfVersions), ]); \App\Archive\Uncompress\Zip::create() ->open($tempFileName) ->extractTo($folderWkhtmltopdf) ->close(); \Log::getInstance()->info(sprintf("[%s] Uncompress the wkhtmltopdf library.", __METHOD__)); \Engine\Dir::changePermissions($folderWkhtmltopdf); \Engine\File::setDefaultPermissions($folderWkhtmltopdf."/bin/wkhtmltopdf"); \Log::getInstance()->info(sprintf("[%s] Change permissions for wkhtmltopdf library.", __METHOD__)); \Engine\File::delete($tempFileName); \Engine\File::create($folderWkhtmltopdf."version.json", json_encode($mkhtmltopdfVersions)); $this->installed = true; return true; } public function isInstalled() { return $this->installed; } public function getLibFullPath() { return $this->libFullPath; } } 