From c555102b5f508abc04c2c8487ef08408d195bfaf Mon Sep 17 00:00:00 2001 From: Yuri Kuznetsov Date: Sun, 7 Feb 2021 12:28:00 +0200 Subject: [PATCH] refactoring --- application/Espo/Core/EntryPointManager.php | 1 + application/Espo/EntryPoints/Attachment.php | 5 ++- application/Espo/EntryPoints/Avatar.php | 6 +-- application/Espo/EntryPoints/ConfirmOptIn.php | 15 ++++--- application/Espo/EntryPoints/Download.php | 27 +++++++++---- application/Espo/EntryPoints/Image.php | 3 +- application/Espo/EntryPoints/LogoImage.php | 10 ++--- application/Espo/EntryPoints/Pdf.php | 3 +- .../Crm/EntryPoints/CampaignTrackOpened.php | 40 ++++++++++++++----- .../Crm/EntryPoints/EventConfirmation.php | 8 ++-- .../Crm/EntryPoints/SubscribeAgain.php | 21 +++++----- .../Modules/Crm/EntryPoints/Unsubscribe.php | 22 +++++----- 12 files changed, 101 insertions(+), 60 deletions(-) diff --git a/application/Espo/Core/EntryPointManager.php b/application/Espo/Core/EntryPointManager.php index 633c335913..c0d6d4abb9 100644 --- a/application/Espo/Core/EntryPointManager.php +++ b/application/Espo/Core/EntryPointManager.php @@ -94,6 +94,7 @@ class EntryPointManager protected function getClassName(string $name) : ?string { $name = ucfirst($name); + return $this->classFinder->find('EntryPoints', $name); } } diff --git a/application/Espo/EntryPoints/Attachment.php b/application/Espo/EntryPoints/Attachment.php index 8817a3b0be..2c4d53bba7 100644 --- a/application/Espo/EntryPoints/Attachment.php +++ b/application/Espo/EntryPoints/Attachment.php @@ -37,6 +37,7 @@ use Espo\Core\EntryPoints\EntryPoint; use Espo\Core\Di; use Espo\Core\Api\Request; +use Espo\Core\Api\Response; class Attachment implements EntryPoint, Di\EntityManagerAware, @@ -52,7 +53,7 @@ class Attachment implements EntryPoint, 'image/webp', ]; - public function run(Request $request) + public function run(Request $request, Response $response) : void { $id = $request->get('id'); @@ -88,7 +89,9 @@ class Attachment implements EntryPoint, header('Pragma: public'); header('Content-Length: ' . filesize($fileName)); + readfile($fileName); + exit; } } diff --git a/application/Espo/EntryPoints/Avatar.php b/application/Espo/EntryPoints/Avatar.php index 32d408e970..ff9eb3fc2f 100644 --- a/application/Espo/EntryPoints/Avatar.php +++ b/application/Espo/EntryPoints/Avatar.php @@ -29,15 +29,13 @@ namespace Espo\EntryPoints; -use Espo\Core\Exceptions\NotFound; -use Espo\Core\Exceptions\Forbidden; use Espo\Core\Exceptions\BadRequest; -use Espo\Core\Exceptions\Error; use Espo\Core\EntryPoints\NotStrictAuth; use Espo\Core\Di; use Espo\Core\Api\Request; +use Espo\Core\Api\Response; class Avatar extends Image implements Di\MetadataAware { @@ -73,7 +71,7 @@ class Avatar extends Image implements Di\MetadataAware return $colorList[$index]; } - public function run(Request $request) + public function run(Request $request, Response $response) : void { $userId = $request->get('id'); $size = $request->get('size') ?? null; diff --git a/application/Espo/EntryPoints/ConfirmOptIn.php b/application/Espo/EntryPoints/ConfirmOptIn.php index 587b245605..56a4761a76 100644 --- a/application/Espo/EntryPoints/ConfirmOptIn.php +++ b/application/Espo/EntryPoints/ConfirmOptIn.php @@ -29,8 +29,6 @@ namespace Espo\EntryPoints; -use Espo\Core\Exceptions\NotFound; -use Espo\Core\Exceptions\Forbidden; use Espo\Core\Exceptions\BadRequest; use Espo\Core\Exceptions\Error; @@ -43,6 +41,7 @@ use Espo\Core\{ Utils\ClientManager, ServiceFactory, Api\Request, + Api\Response, }; class ConfirmOptIn implements EntryPoint @@ -58,19 +57,23 @@ class ConfirmOptIn implements EntryPoint $this->serviceFactory = $serviceFactory; } - public function run(Request $request) + public function run(Request $request, Response $response) : void { $id = $request->get('id'); - if (!$id) throw new BadRequest(); + if (!$id) { + throw new BadRequest(); + } $data = $this->serviceFactory->create('LeadCapture')->confirmOptIn($id); if ($data->status === 'success') { $action = 'optInConfirmationSuccess'; - } else if ($data->status === 'expired') { + } + else if ($data->status === 'expired') { $action = 'optInConfirmationExpired'; - } else { + } + else { throw new Error(); } diff --git a/application/Espo/EntryPoints/Download.php b/application/Espo/EntryPoints/Download.php index b038cf9f89..0331aa3902 100644 --- a/application/Espo/EntryPoints/Download.php +++ b/application/Espo/EntryPoints/Download.php @@ -40,10 +40,10 @@ use Espo\Core\EntryPoints\{ use Espo\Core\{ Acl, ORM\EntityManager, + Api\Request, + Api\Response, }; -use Espo\Core\Api\Request; - class Download implements EntryPoint { protected $fileTypesToShowInline = [ @@ -66,11 +66,13 @@ class Download implements EntryPoint $this->entityManager = $entityManager; } - public function run(Request $request) + public function run(Request $request, Response $response) : void { $id = $request->get('id'); - if (!$id) throw new BadRequest(); + if (!$id) { + throw new BadRequest(); + } $attachment = $this->entityManager->getEntity('Attachment', $id); @@ -82,34 +84,42 @@ class Download implements EntryPoint throw new Forbidden(); } - $sourceId = $attachment->getSourceId(); - if ($this->entityManager->getRepository('Attachment')->hasDownloadUrl($attachment)) { - $downloadUrl = $this->entityManager->getRepository('Attachment')->getDownloadUrl($attachment); + $downloadUrl = $this->entityManager + ->getRepository('Attachment') + ->getDownloadUrl($attachment); + header('Location: ' . $downloadUrl); + exit; } - $fileName = $this->entityManager->getRepository('Attachment')->getFilePath($attachment); + $fileName = $this->entityManager + ->getRepository('Attachment') + ->getFilePath($attachment); if (!file_exists($fileName)) { throw new NotFound(); } $outputFileName = $attachment->get('name'); + $outputFileName = str_replace("\"", "\\\"", $outputFileName); $type = $attachment->get('type'); $disposition = 'attachment'; + if (in_array($type, $this->fileTypesToShowInline)) { $disposition = 'inline'; } header('Content-Description: File Transfer'); + if ($type) { header('Content-Type: ' . $type); } + header("Content-Disposition: " . $disposition . ";filename=\"" . $outputFileName . "\""); header('Expires: 0'); header('Cache-Control: must-revalidate'); @@ -117,6 +127,7 @@ class Download implements EntryPoint header('Content-Length: ' . filesize($fileName)); readfile($fileName); + exit; } } diff --git a/application/Espo/EntryPoints/Image.php b/application/Espo/EntryPoints/Image.php index 3cfdbb7d70..1da526c6fb 100644 --- a/application/Espo/EntryPoints/Image.php +++ b/application/Espo/EntryPoints/Image.php @@ -38,6 +38,7 @@ use Espo\Core\Exceptions\Error; use Espo\Core\EntryPoints\EntryPoint; use Espo\Core\Api\Request; +use Espo\Core\Api\Response; use Espo\Core\Di; @@ -76,7 +77,7 @@ class Image implements EntryPoint, protected $allowedFieldList = null; - public function run(Request $request) + public function run(Request $request, Response $response) : void { $id = $request->get('id'); $size = $request->get('size') ?? null; diff --git a/application/Espo/EntryPoints/LogoImage.php b/application/Espo/EntryPoints/LogoImage.php index 32981dd3e0..2cd60dd659 100644 --- a/application/Espo/EntryPoints/LogoImage.php +++ b/application/Espo/EntryPoints/LogoImage.php @@ -30,15 +30,15 @@ namespace Espo\EntryPoints; use Espo\Core\Exceptions\NotFound; -use Espo\Core\Exceptions\Forbidden; -use Espo\Core\Exceptions\BadRequest; -use Espo\Core\Exceptions\Error; use Espo\Core\EntryPoints\{ NoAuth, }; -use Espo\Core\Api\Request; +use Espo\Core\{ + Api\Request, + Api\Response, +}; use Espo\Core\Di; @@ -51,7 +51,7 @@ class LogoImage extends Image implements Di\ConfigAware protected $allowedFieldList = ['companyLogo']; - public function run(Request $request) + public function run(Request $request, Response $response) : void { $id = $request->get('id'); $size = $request->get('size') ?? null; diff --git a/application/Espo/EntryPoints/Pdf.php b/application/Espo/EntryPoints/Pdf.php index e75fb93718..c3508f0366 100644 --- a/application/Espo/EntryPoints/Pdf.php +++ b/application/Espo/EntryPoints/Pdf.php @@ -40,6 +40,7 @@ use Espo\Core\{ ORM\EntityManager, ServiceFactory, Api\Request, + Api\Response, }; class Pdf implements EntryPoint @@ -53,7 +54,7 @@ class Pdf implements EntryPoint $this->serviceFactory = $serviceFactory; } - public function run(Request $request) + public function run(Request $request, Response $response) : void { $entityId = $request->get('entityId'); $entityType = $request->get('entityType'); diff --git a/application/Espo/Modules/Crm/EntryPoints/CampaignTrackOpened.php b/application/Espo/Modules/Crm/EntryPoints/CampaignTrackOpened.php index eca781ad4d..8d91d32c67 100644 --- a/application/Espo/Modules/Crm/EntryPoints/CampaignTrackOpened.php +++ b/application/Espo/Modules/Crm/EntryPoints/CampaignTrackOpened.php @@ -17,7 +17,7 @@ * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License - * along with EspoCRM. If not, see http://www.gnu.org/licenses/.phpppph + * along with EspoCRM. If not, see http://www.gnu.org/licenses/. * * The interactive user interfaces in modified source and object code versions * of this program must display Appropriate Legal Notices, as required under @@ -29,12 +29,8 @@ namespace Espo\Modules\Crm\EntryPoints; -use Espo\Core\Utils\Util; - use Espo\Core\Exceptions\NotFound; -use Espo\Core\Exceptions\Forbidden; use Espo\Core\Exceptions\BadRequest; -use Espo\Core\Exceptions\Error; use Espo\Core\EntryPoints\{ EntryPoint, @@ -42,6 +38,8 @@ use Espo\Core\EntryPoints\{ }; use Espo\Core\{ + Api\Request, + Api\Response, ORM\EntityManager, ServiceFactory, }; @@ -59,10 +57,13 @@ class CampaignTrackOpened implements EntryPoint $this->serviceFactory = $serviceFactory; } - public function run($request) + public function run(Request $request, Response $response) : void { $id = $request->get('id'); - if (!$id) throw new BadRequest(); + + if (!$id) { + throw new BadRequest(); + } $queueItemId = $id; @@ -83,28 +84,45 @@ class CampaignTrackOpened implements EntryPoint } $massEmailId = $queueItem->get('massEmailId'); - if (!$massEmailId) return; + + if (!$massEmailId) { + return; + } + $massEmail = $this->entityManager->getEntity('MassEmail', $massEmailId); - if (!$massEmail) return; + + if (!$massEmail) { + return; + } $campaignId = $massEmail->get('campaignId'); - if (!$campaignId) return; + + if (!$campaignId) { + return; + } $campaign = $this->entityManager->getEntity('Campaign', $campaignId); - if (!$campaign) return; + if (!$campaign) { + return; + } if (!$target) { return; } + $campaignService = $this->serviceFactory->create('Campaign'); + $campaignService->logOpened($campaignId, $queueItemId, $target, null, $queueItem->get('isTest')); header('Content-Type: image/png'); $img = imagecreatetruecolor(1, 1); + imagesavealpha($img, true); + $color = imagecolorallocatealpha($img, 127, 127, 127, 127); + imagefill($img, 0, 0, $color); imagepng($img); diff --git a/application/Espo/Modules/Crm/EntryPoints/EventConfirmation.php b/application/Espo/Modules/Crm/EntryPoints/EventConfirmation.php index 006edc18e4..50c5080b83 100644 --- a/application/Espo/Modules/Crm/EntryPoints/EventConfirmation.php +++ b/application/Espo/Modules/Crm/EntryPoints/EventConfirmation.php @@ -29,10 +29,7 @@ namespace Espo\Modules\Crm\EntryPoints; -use Espo\Core\Utils\Util; - use Espo\Core\Exceptions\NotFound; -use Espo\Core\Exceptions\Forbidden; use Espo\Core\Exceptions\BadRequest; use Espo\Core\Exceptions\Error; @@ -42,6 +39,8 @@ use Espo\Core\EntryPoints\{ }; use Espo\Core\{ + Api\Request, + Api\Response, ORM\EntityManager, Utils\ClientManager, HookManager, @@ -61,7 +60,7 @@ class EventConfirmation implements EntryPoint $this->hookManager = $hookManager; } - public function run($request) + public function run(Request $request, Response $response) : void { $uid = $request->get('uid') ?? null; $action = $request->get('action') ?? null; @@ -102,6 +101,7 @@ class EventConfirmation implements EntryPoint $status = 'None'; $hookMethodName = 'afterConfirmation'; + if ($action == 'accept') { $status = 'Accepted'; } else if ($action == 'decline') { diff --git a/application/Espo/Modules/Crm/EntryPoints/SubscribeAgain.php b/application/Espo/Modules/Crm/EntryPoints/SubscribeAgain.php index 16c6fc50c0..39b9f2d8e4 100644 --- a/application/Espo/Modules/Crm/EntryPoints/SubscribeAgain.php +++ b/application/Espo/Modules/Crm/EntryPoints/SubscribeAgain.php @@ -17,7 +17,7 @@ * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License - * along with EspoCRM. If not, see http://www.gnu.org/licenses/.phpppph + * along with EspoCRM. If not, see http://www.gnu.org/licenses/. * * The interactive user interfaces in modified source and object code versions * of this program must display Appropriate Legal Notices, as required under @@ -29,12 +29,8 @@ namespace Espo\Modules\Crm\EntryPoints; -use Espo\Core\Utils\Util; - use Espo\Core\Exceptions\NotFound; -use Espo\Core\Exceptions\Forbidden; use Espo\Core\Exceptions\BadRequest; -use Espo\Core\Exceptions\Error; use Espo\Core\EntryPoints\{ EntryPoint, @@ -42,6 +38,8 @@ use Espo\Core\EntryPoints\{ }; use Espo\Core\{ + Api\Request, + Api\Response, ORM\EntityManager, Utils\ClientManager, HookManager, @@ -77,7 +75,7 @@ class SubscribeAgain implements EntryPoint $this->hasher = $hasher; } - public function run($request) + public function run(Request $request, Response $response) : void { $id = $request->get('id') ?? null; $emailAddress = $request->get('emailAddress') ?? null; @@ -92,6 +90,7 @@ class SubscribeAgain implements EntryPoint if (!$id) { throw new BadRequest(); } + $queueItemId = $id; $queueItem = $this->entityManager->getEntity('EmailQueueItem', $queueItemId); @@ -104,10 +103,13 @@ class SubscribeAgain implements EntryPoint $target = null; $massEmailId = $queueItem->get('massEmailId'); + if ($massEmailId) { $massEmail = $this->entityManager->getEntity('MassEmail', $massEmailId); + if ($massEmail) { $campaignId = $massEmail->get('campaignId'); + if ($campaignId) { $campaign = $this->entityManager->getEntity('Campaign', $campaignId); } @@ -124,6 +126,7 @@ class SubscribeAgain implements EntryPoint if ($massEmail->get('optOutEntirely')) { $emailAddress = $target->get('emailAddress'); + if ($emailAddress) { $ea = $this->entityManager->getRepository('EmailAddress')->getByAddress($emailAddress); @@ -190,7 +193,6 @@ class SubscribeAgain implements EntryPoint $this->entityManager->removeEntity($logRecord); } } - } protected function display(array $actionData) @@ -214,8 +216,6 @@ class SubscribeAgain implements EntryPoint protected function processWithHash(string $emailAddress, string $hash) { - $secretKey = $this->config->get('hashSecretKey'); - $hash2 = $this->hasher->hash($emailAddress); if ($hash2 !== $hash) { @@ -242,7 +242,8 @@ class SubscribeAgain implements EntryPoint 'emailAddress' => $emailAddress, 'hash' => $hash, ]); - } else { + } + else { throw new NotFound(); } } diff --git a/application/Espo/Modules/Crm/EntryPoints/Unsubscribe.php b/application/Espo/Modules/Crm/EntryPoints/Unsubscribe.php index 0487ff40b5..27fd5048c1 100644 --- a/application/Espo/Modules/Crm/EntryPoints/Unsubscribe.php +++ b/application/Espo/Modules/Crm/EntryPoints/Unsubscribe.php @@ -29,12 +29,8 @@ namespace Espo\Modules\Crm\EntryPoints; -use Espo\Core\Utils\Util; - use Espo\Core\Exceptions\NotFound; -use Espo\Core\Exceptions\Forbidden; use Espo\Core\Exceptions\BadRequest; -use Espo\Core\Exceptions\Error; use Espo\Core\EntryPoints\{ EntryPoint, @@ -42,6 +38,8 @@ use Espo\Core\EntryPoints\{ }; use Espo\Core\{ + Api\Request, + Api\Response, ORM\EntityManager, Utils\ClientManager, HookManager, @@ -81,7 +79,7 @@ class Unsubscribe implements EntryPoint $this->serviceFactory = $serviceFactory; } - public function run($request) + public function run(Request $request, Response $response) : void { $id = $request->get('id') ?? null; $emailAddress = $request->get('emailAddress') ?? null; @@ -89,12 +87,14 @@ class Unsubscribe implements EntryPoint if ($emailAddress && $hash) { $this->processWithHash($emailAddress, $hash); + return; } if (!$id) { throw new BadRequest(); } + $queueItemId = $id; $queueItem = $this->entityManager->getEntity('EmailQueueItem', $queueItemId); @@ -107,10 +107,13 @@ class Unsubscribe implements EntryPoint $target = null; $massEmailId = $queueItem->get('massEmailId'); + if ($massEmailId) { $massEmail = $this->entityManager->getEntity('MassEmail', $massEmailId); + if ($massEmail) { $campaignId = $massEmail->get('campaignId'); + if ($campaignId) { $campaign = $this->entityManager->getEntity('Campaign', $campaignId); } @@ -127,8 +130,10 @@ class Unsubscribe implements EntryPoint if ($massEmail->get('optOutEntirely')) { $emailAddress = $target->get('emailAddress'); + if ($emailAddress) { $ea = $this->entityManager->getRepository('EmailAddress')->getByAddress($emailAddress); + if ($ea) { $ea->set('optOut', true); $this->entityManager->saveEntity($ea); @@ -181,6 +186,7 @@ class Unsubscribe implements EntryPoint if ($campaign && $target) { $campaignService = $this->serviceFactory->create('Campaign'); + $campaignService->logOptedOut( $campaignId, $queueItemId, $target, $queueItem->get('emailAddress'), null, $queueItem->get('isTest') ); @@ -208,8 +214,6 @@ class Unsubscribe implements EntryPoint protected function processWithHash(string $emailAddress, string $hash) { - $secretKey = $this->config->get('hashSecretKey'); - $hash2 = $this->hasher->hash($emailAddress); if ($hash2 !== $hash) { @@ -236,8 +240,8 @@ class Unsubscribe implements EntryPoint 'emailAddress' => $emailAddress, 'hash' => $hash, ]); - - } else { + } + else { throw new NotFound(); } }