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