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