1 | <?php |
||
2 | |||
3 | namespace LeKoala\EmailTemplates\Models; |
||
4 | |||
5 | use LeKoala\CmsActions\CustomAction; |
||
0 ignored issues
–
show
|
|||
6 | use SilverStripe\Admin\AdminRootController; |
||
7 | use LeKoala\EmailTemplates\Admin\EmailTemplatesAdmin; |
||
8 | use LeKoala\EmailTemplates\Email\BetterEmail; |
||
9 | use SilverStripe\Control\Director; |
||
10 | use SilverStripe\Control\Email\Email; |
||
11 | use SilverStripe\Forms\FieldList; |
||
12 | use SilverStripe\Forms\LiteralField; |
||
13 | use SilverStripe\Forms\ReadonlyField; |
||
14 | use SilverStripe\ORM\DB; |
||
15 | use SilverStripe\ORM\DataObject; |
||
16 | use SilverStripe\Security\Member; |
||
17 | use SilverStripe\Security\Permission; |
||
18 | |||
19 | /** |
||
20 | * Defines a record that stores an email |
||
21 | * |
||
22 | * @link https://github.com/nyeholt/silverstripe-mailcapture/blob/master/src/Model/CapturedEmail.php |
||
23 | * @property string $To |
||
24 | * @property string $From |
||
25 | * @property string $Subject |
||
26 | * @property string $Body |
||
27 | * @property string $CC |
||
28 | * @property string $BCC |
||
29 | * @property string|null $Results |
||
30 | * @property string $ReplyTo |
||
31 | * @property string $Headers |
||
32 | * @author lekoala |
||
33 | */ |
||
34 | class SentEmail extends DataObject |
||
35 | { |
||
36 | private static $table_name = 'SentEmail'; |
||
0 ignored issues
–
show
|
|||
37 | |||
38 | private static $db = [ |
||
0 ignored issues
–
show
|
|||
39 | 'To' => 'Varchar(191)', |
||
40 | 'From' => 'Varchar(191)', |
||
41 | 'ReplyTo' => 'Varchar(191)', |
||
42 | 'Subject' => 'Varchar(191)', |
||
43 | 'Body' => 'HTMLText', |
||
44 | 'Headers' => 'Text', |
||
45 | 'CC' => 'Text', |
||
46 | 'BCC' => 'Text', |
||
47 | 'Results' => 'Text', |
||
48 | ]; |
||
49 | private static $summary_fields = [ |
||
0 ignored issues
–
show
|
|||
50 | 'Created.Nice' => 'Date', |
||
51 | 'To' => 'To', |
||
52 | 'Subject' => 'Subject', |
||
53 | 'IsSuccess' => 'Success', |
||
54 | ]; |
||
55 | |||
56 | private static $default_sort = 'Created DESC'; |
||
0 ignored issues
–
show
|
|||
57 | |||
58 | private static $compress_body = false; |
||
0 ignored issues
–
show
|
|||
59 | |||
60 | public function setBody(string $v) |
||
61 | { |
||
62 | if ($this->config()->get('compress_body') && function_exists('gzdeflate')) { |
||
63 | $compressed = self::COMPRESSED_SIGNATURE . base64_encode(gzdeflate($v, 9)); // Use maximum compression level |
||
64 | if ($compressed !== false) { |
||
0 ignored issues
–
show
|
|||
65 | $this->record['Body'] = $compressed; |
||
66 | return; |
||
67 | } |
||
68 | } |
||
69 | $this->record['Body'] = $v; |
||
70 | } |
||
71 | |||
72 | const COMPRESSED_SIGNATURE = 'base64/deflate:'; |
||
73 | public function getBody(): string |
||
74 | { |
||
75 | $body = $this->record['Body']; |
||
76 | |||
77 | if (substr($body, 0, strlen(self::COMPRESSED_SIGNATURE)) == self::COMPRESSED_SIGNATURE) { |
||
78 | if (!function_exists('gzinflate')) { |
||
79 | return 'This email body is compressed with zlib. Please install zlib module to see it.'; |
||
80 | } |
||
81 | $raw = base64_decode(substr($body, strlen(self::COMPRESSED_SIGNATURE))); |
||
82 | if ($raw !== false) { |
||
83 | return gzinflate($raw); |
||
84 | } |
||
85 | } |
||
86 | |||
87 | return $body; |
||
88 | } |
||
89 | |||
90 | /** |
||
91 | * Gets a list of actions for the ModelAdmin interface |
||
92 | * @return FieldList |
||
93 | */ |
||
94 | public function getCMSActions() |
||
95 | { |
||
96 | $fields = parent::getCMSActions(); |
||
97 | |||
98 | if (class_exists(CustomAction::class)) { |
||
99 | $fields->push( |
||
100 | CustomAction::create('resend', 'Resend') |
||
101 | ); |
||
102 | } |
||
103 | |||
104 | return $fields; |
||
105 | } |
||
106 | |||
107 | /** |
||
108 | * Gets a list of form fields for editing the record. |
||
109 | * These records should never be edited, so a readonly list of fields |
||
110 | * is forced. |
||
111 | * |
||
112 | * @return FieldList |
||
113 | */ |
||
114 | public function getCMSFields() |
||
115 | { |
||
116 | $f = FieldList::create(); |
||
117 | |||
118 | $f->push(ReadonlyField::create('Created')); |
||
119 | $f->push(ReadonlyField::create('From')); |
||
120 | $f->push(ReadonlyField::create('To')); |
||
121 | $f->push(ReadonlyField::create('Subject')); |
||
122 | |||
123 | if ($this->BCC) { |
||
124 | $f->push(ReadonlyField::create('BCC')); |
||
125 | } |
||
126 | if ($this->CC) { |
||
127 | $f->push(ReadonlyField::create('CC')); |
||
128 | } |
||
129 | |||
130 | $f->push(ReadonlyField::create('Results')); |
||
131 | |||
132 | $sanitisedModel = str_replace('\\', '-', SentEmail::class); |
||
133 | $adminSegment = EmailTemplatesAdmin::config()->get('url_segment'); |
||
134 | $adminBaseSegment = AdminRootController::config()->get('url_base'); |
||
135 | $iframeSrc = Director::baseURL() . $adminBaseSegment . '/' . $adminSegment . '/' . $sanitisedModel . '/ViewSentEmail/?id=' . $this->ID; |
||
136 | $iframe = new LiteralField('iframe', '<iframe src="' . $iframeSrc . '" style="width:800px;background:#fff;border:1px solid #ccc;min-height:500px;vertical-align:top"></iframe>'); |
||
137 | $f->push($iframe); |
||
138 | |||
139 | $this->extend('updateCMSFields', $f); |
||
140 | |||
141 | return $f; |
||
142 | } |
||
143 | |||
144 | /** |
||
145 | * @return bool |
||
146 | */ |
||
147 | public function IsSuccess() |
||
148 | { |
||
149 | return $this->Results == 'true'; |
||
150 | } |
||
151 | |||
152 | /** |
||
153 | * Gets the BetterEmail object that was used to send this email |
||
154 | * @return BetterEmail |
||
155 | */ |
||
156 | public function getEmail() |
||
157 | { |
||
158 | /** @var BetterEmail */ |
||
159 | $email = Email::create(); |
||
160 | |||
161 | $email->setTo($this->To); |
||
162 | $email->setCc($this->CC); |
||
163 | $email->setBCC($this->BCC); |
||
164 | $email->setSubject($this->Subject); |
||
165 | $email->setReplyTo($this->ReplyTo); |
||
166 | $email->setBody($this->Body); |
||
167 | |||
168 | return $email; |
||
169 | } |
||
170 | |||
171 | /** |
||
172 | * A BetterButtons custom action that allows the email to be resent |
||
173 | */ |
||
174 | public function resend() |
||
175 | { |
||
176 | if ($e = $this->getEmail()) { |
||
177 | $results = $e->doSend(); |
||
178 | |||
179 | // Update results |
||
180 | $this->Results = json_encode($results); |
||
181 | $this->write(); |
||
182 | |||
183 | return 'Sent'; |
||
184 | } |
||
185 | |||
186 | return 'Could not send email'; |
||
187 | } |
||
188 | |||
189 | |||
190 | /** |
||
191 | * Cleanup sent emails based on your config |
||
192 | * |
||
193 | * @return void |
||
194 | */ |
||
195 | public static function cleanup() |
||
196 | { |
||
197 | $max = self::config()->get('max_records'); |
||
198 | if ($max && self::get()->count() > $max) { |
||
199 | $method = self::config()->get('cleanup_method'); |
||
200 | |||
201 | // Delete all records older than cleanup_time (7 days by default) |
||
202 | if ($method == 'time') { |
||
203 | $time = self::config()->get('cleanup_time'); |
||
204 | $date = date('Y-m-d H:i:s', strtotime($time)); |
||
205 | DB::query("DELETE FROM \"SentEmail\" WHERE Created < '$date'"); |
||
206 | } |
||
207 | |||
208 | // Delete all records that are after half the maximum number of records |
||
209 | if ($method == 'max') { |
||
210 | $maxID = SentEmail::get()->max('ID') - ($max / 2); |
||
211 | DB::query("DELETE FROM \"SentEmail\" WHERE ID < '$maxID'"); |
||
212 | } |
||
213 | } |
||
214 | } |
||
215 | |||
216 | /** |
||
217 | * Defines the view permission |
||
218 | * @param Member $member |
||
219 | * @return boolean |
||
220 | */ |
||
221 | public function canView($member = null) |
||
222 | { |
||
223 | return Permission::check('CMS_ACCESS'); |
||
224 | } |
||
225 | |||
226 | /** |
||
227 | * Defines the edit permission |
||
228 | * @param Member $member |
||
229 | * @return boolean |
||
230 | */ |
||
231 | public function canEdit($member = null) |
||
232 | { |
||
233 | return false; |
||
234 | } |
||
235 | |||
236 | /** |
||
237 | * Defines the create permission |
||
238 | * @param Member $member |
||
239 | * @return boolean |
||
240 | */ |
||
241 | public function canCreate($member = null, $context = []) |
||
242 | { |
||
243 | return false; |
||
244 | } |
||
245 | |||
246 | /** |
||
247 | * Defines the delete permission |
||
248 | * @param Member $member |
||
249 | * @return boolean |
||
250 | */ |
||
251 | public function canDelete($member = null) |
||
252 | { |
||
253 | return Permission::check('CMS_ACCESS'); |
||
254 | } |
||
255 | } |
||
256 |
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"]
, you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths