ILIAS  release_7 Revision v7.30-3-g800a261c036
class.ilMailOptions.php
Go to the documentation of this file.
1<?php declare(strict_types=1);
2/* Copyright (c) 1998-2012 ILIAS open source, Extended GPL, see docs/LICENSE */
3
11{
12 const INCOMING_LOCAL = 0;
13 const INCOMING_EMAIL = 1;
14 const INCOMING_BOTH = 2;
15
16 const FIRST_EMAIL = 3;
17 const SECOND_EMAIL = 4;
18 const BOTH_EMAIL = 5;
19
21
23 protected $ilias;
25 protected $db;
27 protected $usrId = 0;
29 protected $settings;
31 protected $table_mail_options = 'mail_options';
33 protected $linebreak = 0;
35 protected $signature = '';
49 protected $firstEmailAddress = '';
51 protected $secondEmailAddress = '';
52
57 public function __construct(
58 int $usrId,
60 ilSetting $settings = null,
61 ilDBInterface $db = null
62 ) {
63 global $DIC;
64
65 $this->usrId = $usrId;
66
67 $this->db = $db ?? $DIC->database();
68 $this->settings = $settings ?? $DIC->settings();
69
70 $this->incomingType = self::INCOMING_LOCAL;
71 $default_incoming_type = $this->settings->get('mail_incoming_mail', '');
72 if ($default_incoming_type !== '') {
73 $this->default_incoming_type = (int) $default_incoming_type;
74 $this->incomingType = $this->default_incoming_type;
75 }
76
77 $this->emailAddressMode = self::FIRST_EMAIL;
78 $default_email_address_mode = $this->settings->get('mail_address_option', '');
79 if ($default_email_address_mode !== '') {
80 $this->default_email_address_mode = (int) $default_email_address_mode;
81 $this->emailAddressMode = $this->default_email_address_mode;
82 }
83
84 $this->linebreak = self::DEFAULT_LINE_BREAK;
85 $this->isCronJobNotificationEnabled = false;
86 $this->signature = '';
87
88 if ($mailTransportSettings === null) {
90 }
91 $this->mailTransportSettings = $mailTransportSettings;
92
93 $this->read();
94 }
95
100 public function createMailOptionsEntry() : void
101 {
102 $this->db->replace(
103 $this->table_mail_options,
104 [
105 'user_id' => ['integer', $this->usrId],
106 ],
107 [
108 'linebreak' => ['integer', $this->linebreak],
109 'signature' => ['text', $this->signature],
110 'incoming_type' => ['integer', $this->default_incoming_type],
111 'mail_address_option' => ['integer', $this->default_email_address_mode],
112 'cronjob_notification' => ['integer', (int) $this->isCronJobNotificationEnabled]
113 ]
114 );
115 }
116
118 {
119 return (
122 $this->settings->get('usr_settings_disable_mail_incoming_mail') !== '1'
123 );
124 }
125
126 public function maySeeIndividualTransportSettings() : bool
127 {
128 return $this->settings->get('usr_settings_hide_mail_incoming_mail') !== '1';
129 }
130
131 public function mayManageInvididualSettings() : bool
132 {
133 return $this->settings->get('show_mail_settings') === '1';
134 }
135
136 protected function read() : void
137 {
138 $query = implode(' ', [
139 'SELECT mail_options.cronjob_notification,',
140 'mail_options.signature, mail_options.linebreak, mail_options.incoming_type,',
141 'mail_options.mail_address_option, usr_data.email, usr_data.second_email',
142 'FROM mail_options',
143 'INNER JOIN usr_data ON mail_options.user_id = usr_data.usr_id',
144 'WHERE mail_options.user_id = %s',
145 ]);
146 $res = $this->db->queryF(
147 $query,
148 ['integer'],
149 [$this->usrId]
150 );
151 $row = $this->db->fetchObject($res);
152 if ($row === null) {
153 $this->mailTransportSettings->adjust($this->firstEmailAddress, $this->secondEmailAddress, false);
154 return;
155 }
156
157 $this->firstEmailAddress = (string) $row->email;
158 $this->secondEmailAddress = (string) $row->second_email;
159
160 if ($this->mayManageInvididualSettings()) {
161 $this->signature = (string) $row->signature;
162 $this->linebreak = (int) $row->linebreak;
163 $this->isCronJobNotificationEnabled = (bool) $row->cronjob_notification;
164 }
165
167 $this->incomingType = (int) $row->incoming_type;
168 $this->emailAddressMode = (int) $row->mail_address_option;
169
170 if (false === filter_var(
171 $this->incomingType,
172 FILTER_VALIDATE_INT,
173 ['options' => ['min_range' => self::INCOMING_LOCAL, 'max_range' => self::INCOMING_BOTH]]
174 )) {
175 $this->incomingType = self::INCOMING_LOCAL;
176 }
177
178 if (false === filter_var(
179 $this->emailAddressMode,
180 FILTER_VALIDATE_INT,
181 ['options' => ['min_range' => self::FIRST_EMAIL, 'max_range' => self::BOTH_EMAIL]]
182 )) {
183 $this->emailAddressMode = self::FIRST_EMAIL;
184 }
185 }
186
187 $this->mailTransportSettings->adjust($this->firstEmailAddress, $this->secondEmailAddress);
188 }
189
190 public function updateOptions()
191 {
192 $data = [
193 'signature' => ['text', $this->getSignature()],
194 'linebreak' => ['integer', (int) $this->getLinebreak()],
195 'incoming_type' => ['integer', $this->getIncomingType()],
196 'mail_address_option' => ['integer', $this->getEmailAddressMode()]
197 ];
198
199 if ($this->settings->get('mail_notification')) {
200 $data['cronjob_notification'] = ['integer', (int) $this->isCronJobNotificationEnabled()];
201 } else {
202 $data['cronjob_notification'] = ['integer', $this->lookupNotificationSetting($this->usrId)];
203 }
204
205 return $this->db->replace(
206 $this->table_mail_options,
207 [
208 'user_id' => ['integer', $this->usrId]
209 ],
210 $data
211 );
212 }
213
217 public function getLinebreak() : int
218 {
219 return $this->linebreak;
220 }
221
225 public function getSignature() : string
226 {
227 return $this->signature;
228 }
229
233 public function getIncomingType() : int
234 {
235 return $this->incomingType;
236 }
237
241 public function setLinebreak(int $linebreak) : void
242 {
243 $this->linebreak = $linebreak;
244 }
245
249 public function setSignature(string $signature) : void
250 {
251 $this->signature = $signature;
252 }
253
257 public function setIncomingType(int $incomingType) : void
258 {
259 $this->incomingType = $incomingType;
260 }
261
266 {
268 }
269
273 public function isCronJobNotificationEnabled() : bool
274 {
276 }
277
281 public function getEmailAddressMode() : int
282 {
284 }
285
289 public function setEmailAddressMode(int $emailAddressMode) : void
290 {
291 $this->emailAddressMode = $emailAddressMode;
292 }
293
298 protected function lookupNotificationSetting(int $usrId) : int
299 {
300 global $DIC;
301
302 $row = $DIC->database()->fetchAssoc($DIC->database()->queryF(
303 'SELECT cronjob_notification FROM mail_options WHERE user_id = %s',
304 ['integer'],
305 [$usrId]
306 ));
307
308 return (int) $row['cronjob_notification'];
309 }
310
314 public function getExternalEmailAddresses() : array
315 {
316 $emailAddresses = [];
317
318 switch ($this->getEmailAddressMode()) {
320 if (strlen($this->secondEmailAddress)) {
321 $emailAddresses[] = $this->secondEmailAddress;
322 } elseif (strlen($this->firstEmailAddress)) {
323 // fallback, use first email address
324 $emailAddresses[] = $this->firstEmailAddress;
325 }
326 break;
327
328 case self::BOTH_EMAIL:
329 if (strlen($this->firstEmailAddress)) {
330 $emailAddresses[] = $this->firstEmailAddress;
331 }
332 if (strlen($this->secondEmailAddress)) {
333 $emailAddresses[] = $this->secondEmailAddress;
334 }
335 break;
336
338 default:
339 if (strlen($this->firstEmailAddress)) {
340 $emailAddresses[] = $this->firstEmailAddress;
341 } elseif (strlen($this->secondEmailAddress)) {
342 // fallback, use first email address
343 $emailAddresses[] = $this->secondEmailAddress;
344 }
345 break;
346 }
347
348 return $emailAddresses;
349 }
350}
An exception for terminatinating execution or to throw for unit testing.
Class ilMailOptions this class handles user mails.
lookupNotificationSetting(int $usrId)
setSignature(string $signature)
createMailOptionsEntry()
create entry in table_mail_options for a new user this method should only be called from createUser()
setEmailAddressMode(int $emailAddressMode)
setLinebreak(int $linebreak)
setIsCronJobNotificationStatus(bool $isCronJobNotificationEnabled)
__construct(int $usrId, ilMailTransportSettings $mailTransportSettings=null, ilSetting $settings=null, ilDBInterface $db=null)
setIncomingType(int $incomingType)
ILIAS Setting Class.
global $DIC
Definition: goto.php:24
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
$query
foreach($_POST as $key=> $value) $res
settings()
Definition: settings.php:2
$data
Definition: storeScorm.php:23