getFileType()) && !in_array($type, [PdfEntity::TYPE_LICENSE, PdfEntity::TYPE_RECEIPT])) { $this->_do404(); } $matches = []; if (null === $this->_restAlias || ($type == PdfEntity::TYPE_RECEIPT && !preg_match("/^{$type}\-([\d]+)\.pdf$/", $this->_restAlias, $matches)) || ($type == PdfEntity::TYPE_PREMIUM_RECEIPT && !preg_match("/^{$type}\-([\d]+)_([\d]+)\.pdf$/", $this->_restAlias, $matches)) || ($type == PdfEntity::TYPE_LICENSE && !preg_match("/^{$type}\-([\d]+)_([\d]+)\.pdf$/", $this->_restAlias, $matches)) ) { $this->_do404(); } $licenseId = $matches[1]; $tribeId = (isset($matches[2])) ? $matches[2] : null; $tribe = null; if (in_array($type, [PdfEntity::TYPE_LICENSE, PdfEntity::TYPE_PREMIUM_RECEIPT])) { if ($tribeId && ($tribe = (new \App\Tribe\Obj())->setPrimaryKey($tribeId)->getData())) { $this->_getDataObj()->setTribeId($tribeId); } else { $this->_do404(); } if (!($license = $this->_getDataObj()->setPrimaryKey($licenseId)->getData()) || $license['tribeStatus'] != LicenseEntity::TRIBE_STATUS_APPROVED || $license['status'] == LicenseEntity::STATUS_EXPIRED ) { $this->_do404(); } $license['tribe'] = $tribe; } else { if (!($license = $this->_getDataObj()->setPrimaryKey($licenseId)->getData()) || $license['status'] == LicenseEntity::STATUS_EXPIRED ) { $this->_do404(); } } if ($type == PdfEntity::TYPE_RECEIPT && $license['paymentMethod'] == LicenseEntity::PAYMENT_METHOD_CHECK) { $license['checkTypeTitle'] = $this->getConfigArray('checkTypes')[$license['checkType']]; } $file = constant('WWW_PATH') . '/' . self::getFileTypePage($type, 'fullAlias') . '/' . $this->_restAlias; $output = 'development' === APPLICATION_ENV && constant('DEBUG'); (new PdfRenderer())->render(compact('license'), $this->getTemplate("{$type}.tpl"), $file, $output, $type); if (!$output) { $this->outPdf($file); } return $this; } public static function getFileTypePage($type, $field = null) { return Qs_SiteMap::findFirst([], ['type' => 'License\\Pdf\\', 'fileType' => $type], [], $field); } private static function getFileUrl($type, $id, $tribeId = null) { if (in_array($type, [PdfEntity::TYPE_LICENSE, PdfEntity::TYPE_PREMIUM_RECEIPT])) { $url = self::getFileTypePage($type, 'url') . "/{$type}-{$id}_{$tribeId}.pdf"; } else { $url = self::getFileTypePage($type, 'url') . "/{$type}-{$id}.pdf"; } return $url; } public static function getLicenseUrl($id, $tribeId) { return self::getFileUrl(PdfEntity::TYPE_LICENSE, $id, $tribeId); } public static function getReceiptUrl($id) { return self::getFileUrl(PdfEntity::TYPE_RECEIPT, $id); } public static function getPremiumReceiptUrl($licenseId, $tribeId) { return self::getFileUrl(PdfEntity::TYPE_PREMIUM_RECEIPT, $licenseId, $tribeId); } public function getFileType() { return $this->fileType; } public function setFileType($fileType) { $this->fileType = $fileType; return $this; } private function outPdf($file) { if (false === ($handle = fopen($file, 'r'))) { throw new Exception('Failed to open file ' . $file); } header('Content-Type: application/pdf'); fpassthru($handle); exit; } }