xref: /dokuwiki/lib/plugins/config/core/Setting/SettingEmail.php (revision 73dc0a8919857718a3b64a4c0741b57580a34b2a)
1<?php
2
3namespace dokuwiki\plugin\config\core\Setting;
4
5use dokuwiki\MailUtils;
6
7/**
8 * Class setting_email
9 */
10class SettingEmail extends SettingString
11{
12    protected $multiple = false;
13    protected $placeholders = false;
14
15    /** @inheritdoc */
16    public function update($input)
17    {
18        if (is_null($input)) return false;
19        if ($this->isProtected()) return false;
20
21        $value = is_null($this->local) ? $this->default : $this->local;
22        if ($value == $input) return false;
23        if ($input === '') {
24            $this->local = $input;
25            return true;
26        }
27        $mail = $input;
28
29        if ($this->placeholders) {
30            // replace variables with pseudo values
31            $mail = str_replace('@USER@', 'joe', $mail);
32            $mail = str_replace('@NAME@', 'Joe Schmoe', $mail);
33            $mail = str_replace('@MAIL@', 'joe@example.com', $mail);
34        }
35
36        // multiple mail addresses?
37        if ($this->multiple) {
38            $mails = array_filter(array_map(trim(...), explode(',', $mail)));
39        } else {
40            $mails = [$mail];
41        }
42
43        // check them all
44        foreach ($mails as $mail) {
45            // only check the address part
46            if (preg_match('#(.*?)<(.*?)>#', $mail, $matches)) {
47                $addr = $matches[2];
48            } else {
49                $addr = $mail;
50            }
51
52            if (!MailUtils::isValid($addr)) {
53                $this->error = true;
54                $this->input = $input;
55                return false;
56            }
57        }
58
59        $this->local = $input;
60        return true;
61    }
62}
63