Rev 1186 | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
250 | daniel-mar | 1 | <?php |
2 | |||
3 | /* |
||
4 | * OIDplus 2.0 |
||
1086 | daniel-mar | 5 | * Copyright 2019 - 2023 Daniel Marschall, ViaThinkSoft |
250 | daniel-mar | 6 | * |
7 | * Licensed under the Apache License, Version 2.0 (the "License"); |
||
8 | * you may not use this file except in compliance with the License. |
||
9 | * You may obtain a copy of the License at |
||
10 | * |
||
11 | * http://www.apache.org/licenses/LICENSE-2.0 |
||
12 | * |
||
13 | * Unless required by applicable law or agreed to in writing, software |
||
14 | * distributed under the License is distributed on an "AS IS" BASIS, |
||
15 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||
16 | * See the License for the specific language governing permissions and |
||
17 | * limitations under the License. |
||
18 | */ |
||
19 | |||
1050 | daniel-mar | 20 | namespace ViaThinkSoft\OIDplus; |
511 | daniel-mar | 21 | |
1086 | daniel-mar | 22 | // phpcs:disable PSR1.Files.SideEffects |
23 | \defined('INSIDE_OIDPLUS') or die; |
||
24 | // phpcs:enable PSR1.Files.SideEffects |
||
25 | |||
730 | daniel-mar | 26 | class OIDplusMailUtils extends OIDplusBaseClass { |
250 | daniel-mar | 27 | |
1116 | daniel-mar | 28 | /** |
29 | * @param string $email |
||
30 | * @return bool |
||
31 | */ |
||
1186 | daniel-mar | 32 | public function validMailAddress(string $email): bool { |
250 | daniel-mar | 33 | return !empty(filter_var($email, FILTER_VALIDATE_EMAIL)); |
34 | } |
||
360 | daniel-mar | 35 | |
1116 | daniel-mar | 36 | /** |
37 | * @param string $email |
||
38 | * @param string $linktext |
||
39 | * @param int $level |
||
40 | * @return string|null |
||
41 | */ |
||
1186 | daniel-mar | 42 | public function secureEmailAddress(string $email, string $linktext, int $level=1)/*: ?string*/ { |
360 | daniel-mar | 43 | |
250 | daniel-mar | 44 | // see http://www.spamspan.de/ |
360 | daniel-mar | 45 | |
250 | daniel-mar | 46 | /* Level 1 */ |
47 | /* |
||
48 | <span class="spamspan"> |
||
49 | <span class="u">user</span> |
||
50 | @ |
||
51 | <span class="d">beispiel.de</span> |
||
52 | (<span class="t">Spam Hasser</span>) |
||
53 | </span> |
||
54 | */ |
||
360 | daniel-mar | 55 | |
250 | daniel-mar | 56 | if ($level == 1) { |
57 | @list($user, $domain) = explode('@', $email); |
||
58 | if (($linktext == $email) || empty($linktext)) { |
||
59 | return '<span class="spamspan"><span class="u">'.htmlentities($user).'</span>@<span class="d">'.htmlentities($domain).'</span></span>'; |
||
60 | } else { |
||
61 | return '<span class="spamspan"><span class="u">'.htmlentities($user).'</span>@<span class="d">'.htmlentities($domain).'</span>(<span class="t">'.htmlentities($linktext).'</span>)</span>'; |
||
62 | } |
||
63 | } |
||
360 | daniel-mar | 64 | |
250 | daniel-mar | 65 | /* Level 2 */ |
66 | /* |
||
67 | <span class="spamspan"> |
||
68 | <span class="u">user</span> |
||
69 | <img alt="at" width="10" src="@.png"> |
||
70 | <span class="d">beispiel.de</span> |
||
71 | </span> |
||
72 | */ |
||
360 | daniel-mar | 73 | |
250 | daniel-mar | 74 | if ($level == 2) { |
75 | list($user, $domain) = explode('@', $email); |
||
76 | return '<span class="spamspan"><span class="u">'.htmlentities($user).'</span><img alt="at" width="10" src="@.png"><span class="d">'.htmlentities($domain).'</span></span>'; |
||
77 | } |
||
360 | daniel-mar | 78 | |
250 | daniel-mar | 79 | /* Level 3 */ |
80 | /* |
||
81 | <span class="spamspan"> |
||
82 | <span class="u">user</span> |
||
83 | [at] |
||
84 | <span class="d">beispiel [dot] de</span> |
||
85 | </span> |
||
86 | */ |
||
360 | daniel-mar | 87 | |
250 | daniel-mar | 88 | if ($level == 3) { |
89 | list($user, $domain) = explode('@', $email); |
||
360 | daniel-mar | 90 | $domain = str_replace('.', ' '._L('[dot]').' ', $domain); |
91 | return '<span class="spamspan"><span class="u">'.htmlentities($user).'</span> '._L('[at]').' <span class="d">'.htmlentities($domain).'</span></span>'; |
||
250 | daniel-mar | 92 | } |
360 | daniel-mar | 93 | |
250 | daniel-mar | 94 | return null; |
360 | daniel-mar | 95 | |
96 | |||
250 | daniel-mar | 97 | // --- Old code --- |
360 | daniel-mar | 98 | |
386 | daniel-mar | 99 | /* |
250 | daniel-mar | 100 | // Attention: document.write() JavaScript will damage the browser cache, which leads to bugs if you navigate back&forth with the browser navigation |
400 | daniel-mar | 101 | |
386 | daniel-mar | 102 | $crypt_linktext = true; |
360 | daniel-mar | 103 | |
250 | daniel-mar | 104 | // No new lines to avoid a JavaScript error! |
105 | $linktext = str_replace("\r", ' ', $linktext); |
||
106 | $linktext = str_replace("\n", ' ', $linktext); |
||
360 | daniel-mar | 107 | |
250 | daniel-mar | 108 | if (!function_exists('alas_js_crypt')) |
109 | { |
||
110 | function alas_js_crypt($text) |
||
111 | { |
||
112 | $tmp = ''; |
||
113 | for ($i=0; $i<strlen($text); $i++) |
||
114 | { |
||
115 | $tmp .= 'document.write("&#'.ord(substr($text, $i, 1)).';");'; |
||
116 | } |
||
117 | return $tmp; |
||
118 | } |
||
119 | } |
||
360 | daniel-mar | 120 | |
250 | daniel-mar | 121 | if (!function_exists('alas_js_write')) |
122 | { |
||
123 | function alas_js_write($text) |
||
124 | { |
||
125 | $text = str_replace('\\', '\\\\', $text); |
||
126 | $text = str_replace('"', '\"', $text); |
||
127 | $text = str_replace('/', '\/', $text); // W3C Validation </a> -> <\/a> |
||
128 | return 'document.write("'.$text.'");'; |
||
129 | } |
||
130 | } |
||
360 | daniel-mar | 131 | |
250 | daniel-mar | 132 | $aus = ''; |
133 | if ($email != '') |
||
134 | { |
||
135 | $aus .= '<script><!--'."\n"; // type="text/javascript" is not necessary in HTML5 |
||
136 | $aus .= alas_js_write('<a href="'); |
||
137 | $aus .= alas_js_crypt('mailto:'.$email); |
||
138 | $aus .= alas_js_write('">'); |
||
139 | $aus .= $crypt_linktext ? alas_js_crypt($linktext) : alas_js_write($linktext); |
||
140 | $aus .= alas_js_write('</a>').'// --></script>'; |
||
141 | } |
||
360 | daniel-mar | 142 | |
250 | daniel-mar | 143 | if ($crypt_linktext) $linktext = str_replace('@', '&', $linktext); |
144 | $email = str_replace('@', '&', $email); |
||
145 | return $aus.'<noscript>'.htmlentities($linktext).' ('.htmlentities($email).')</noscript>'; |
||
400 | daniel-mar | 146 | |
386 | daniel-mar | 147 | */ |
250 | daniel-mar | 148 | } |
360 | daniel-mar | 149 | |
1116 | daniel-mar | 150 | /** |
151 | * @param string $to |
||
152 | * @param string $title |
||
153 | * @param string $msg |
||
154 | * @param string $cc |
||
155 | * @param string $bcc |
||
156 | * @return void |
||
157 | * @throws OIDplusException |
||
158 | * @throws OIDplusMailException |
||
159 | */ |
||
1186 | daniel-mar | 160 | public function sendMail(string $to, string $title, string $msg, string $cc='', string $bcc='') { |
1050 | daniel-mar | 161 | $h = new \SecureMailer(); |
360 | daniel-mar | 162 | |
841 | daniel-mar | 163 | // DM 14.04.2022: Added Reply-To, because some servers might change the 'From' attribute (Anti-Spoof?) |
250 | daniel-mar | 164 | $h->addHeader('From', OIDplus::config()->getValue('admin_email')); |
841 | daniel-mar | 165 | $h->addHeader('Reply-To', OIDplus::config()->getValue('admin_email')); |
360 | daniel-mar | 166 | |
875 | daniel-mar | 167 | $cc = explode(';', $cc); |
168 | $global_cc = trim(OIDplus::config()->getValue('global_cc')); |
||
169 | if ($global_cc != '') $cc[] = trim($global_cc); |
||
170 | foreach ($cc as $x) $h->addHeader('Cc', $x); |
||
360 | daniel-mar | 171 | |
875 | daniel-mar | 172 | $bcc = explode(';', $bcc); |
173 | $global_bcc = trim(OIDplus::config()->getValue('global_bcc')); |
||
174 | if ($global_bcc != '') $bcc[] = trim($global_bcc); |
||
175 | foreach ($bcc as $x) $h->addHeader('Bcc', $x); |
||
176 | |||
444 | daniel-mar | 177 | $h->addHeader('X-Mailer', 'PHP/'.PHP_VERSION); |
841 | daniel-mar | 178 | |
179 | // DM 14.04.2022: Commented out because of privacy |
||
1345 | daniel-mar | 180 | // $cur_ip = OIDplus::getClientIpAddress(); |
181 | //if ($cur_ip !== false) $h->addHeader('X-RemoteAddr', $cur_ip); |
||
841 | daniel-mar | 182 | |
250 | daniel-mar | 183 | $h->addHeader('MIME-Version', '1.0'); |
360 | daniel-mar | 184 | |
841 | daniel-mar | 185 | // DM 14.04.2022: Changed from "ISO-8859-1" to "UTF-8" |
186 | $h->addHeader('Content-Type', 'text/plain; charset=UTF-8'); |
||
187 | |||
250 | daniel-mar | 188 | $sent = $h->sendMail($to, $title, $msg); |
189 | if (!$sent) { |
||
360 | daniel-mar | 190 | throw new OIDplusMailException(_L('Sending mail failed')); |
250 | daniel-mar | 191 | } |
192 | } |
||
193 | |||
730 | daniel-mar | 194 | } |