AbstractNotificationService.php
2 months ago
AbstractWhatsAppNotificationService.php
3 months ago
ApplicationNotificationService.php
2 months ago
AppointmentNotificationService.php
2 weeks ago
BasicWhatsAppNotificationService.php
6 months ago
EmailNotificationService.php
1 month ago
NotificationHelperService.php
6 months ago
SMSAPIService.php
2 months ago
SMSNotificationService.php
1 month ago
NotificationHelperService.php
113 lines
| 1 | <?php |
| 2 | |
| 3 | namespace AmeliaBooking\Application\Services\Notification; |
| 4 | |
| 5 | use AmeliaBooking\Domain\ValueObjects\String\Token; |
| 6 | use AmeliaBooking\Infrastructure\Common\Container; |
| 7 | use DOMDocument; |
| 8 | use DOMElement; |
| 9 | |
| 10 | class NotificationHelperService |
| 11 | { |
| 12 | /** @var Container */ |
| 13 | protected $container; |
| 14 | |
| 15 | /** |
| 16 | * NotificationHelperService constructor. |
| 17 | * |
| 18 | * @param Container $container |
| 19 | */ |
| 20 | public function __construct(Container $container) |
| 21 | { |
| 22 | $this->container = $container; |
| 23 | } |
| 24 | |
| 25 | public function parseAndReplace($content) |
| 26 | { |
| 27 | $parsedContent = null; |
| 28 | $newContent = $content; |
| 29 | |
| 30 | try { |
| 31 | $parsedContent = class_exists('DOMDocument') ? $this->parseContent($content) : $content; |
| 32 | } catch (\Exception $e) { |
| 33 | $newContent = $content; |
| 34 | } |
| 35 | |
| 36 | $newContent = str_replace( |
| 37 | [ |
| 38 | 'class="ql-align-center"', |
| 39 | 'class="ql-align-right"', |
| 40 | 'class="ql-align-left"', |
| 41 | 'class="ql-align-justify"' |
| 42 | ], |
| 43 | [ |
| 44 | 'style="text-align: center;"', |
| 45 | 'style="text-align: right;"', |
| 46 | 'style="text-align: left;"', |
| 47 | 'class="text-align: justify"' |
| 48 | ], |
| 49 | $parsedContent ?: $newContent |
| 50 | ); |
| 51 | return array($parsedContent, $newContent); |
| 52 | } |
| 53 | |
| 54 | /** |
| 55 | * @param $content |
| 56 | * @return string|null |
| 57 | */ |
| 58 | private function parseContent($content) |
| 59 | { |
| 60 | $html = new DOMDocument(); |
| 61 | $html->loadHTML($content); |
| 62 | |
| 63 | $html->preserveWhiteSpace = false; |
| 64 | |
| 65 | $hasParsedContent = false; |
| 66 | |
| 67 | /** @var DOMElement $image */ |
| 68 | foreach ($html->getElementsByTagName('img') as $image) { |
| 69 | $src = $image->getAttribute('src'); |
| 70 | |
| 71 | if (strpos($src, 'data:image/') === 0) { |
| 72 | $parts = explode(',', substr($src, 5), 2); |
| 73 | |
| 74 | $mimeSplitWithoutBase64 = explode(';', $parts[0], 2); |
| 75 | $mimeSplit = explode('/', $mimeSplitWithoutBase64[0], 2); |
| 76 | |
| 77 | $outputFile = ''; |
| 78 | |
| 79 | if (!in_array($mimeSplit[1], ['jpeg', 'jpg', 'png', 'gif'])) { |
| 80 | continue; |
| 81 | } |
| 82 | |
| 83 | if (count($mimeSplit) === 2) { |
| 84 | $token = new Token(); |
| 85 | |
| 86 | $outputFile = $token->getValue() . '.' . (($mimeSplit[1] === 'jpeg') ? 'jpg' : $mimeSplit[1]); |
| 87 | } |
| 88 | |
| 89 | $outputPath = AMELIA_UPLOADS_PATH . '/amelia/mail/'; |
| 90 | |
| 91 | !is_dir($outputPath) && !mkdir($outputPath, 0755, true) && !is_dir($outputPath); |
| 92 | |
| 93 | file_put_contents($outputPath . $outputFile, base64_decode($parts[1])); |
| 94 | |
| 95 | $content = preg_replace( |
| 96 | '/<img(.*?)src="data:image(.*?)"(.*?)>/', |
| 97 | '<IMG src="' . AMELIA_UPLOADS_URL . '/amelia/mail/' . $outputFile . '">', |
| 98 | $content, |
| 99 | 1 |
| 100 | ); |
| 101 | |
| 102 | $hasParsedContent = true; |
| 103 | } |
| 104 | } |
| 105 | |
| 106 | if ($hasParsedContent) { |
| 107 | return str_replace('<IMG src="', '<img src="', $content); |
| 108 | } |
| 109 | |
| 110 | return null; |
| 111 | } |
| 112 | } |
| 113 |