Subversion Repositories oidplus

Rev

Rev 1116 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed

  1. <?php
  2.  
  3. /*
  4.  * OIDplus 2.0
  5.  * Copyright 2019 - 2023 Daniel Marschall, ViaThinkSoft
  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.  
  20. namespace ViaThinkSoft\OIDplus;
  21.  
  22. // phpcs:disable PSR1.Files.SideEffects
  23. \defined('INSIDE_OIDPLUS') or die;
  24. // phpcs:enable PSR1.Files.SideEffects
  25.  
  26. class OIDplusMailUtils extends OIDplusBaseClass {
  27.  
  28.         /**
  29.          * @param string $email
  30.          * @return bool
  31.          */
  32.         public function validMailAddress(string $email): bool {
  33.                 return !empty(filter_var($email, FILTER_VALIDATE_EMAIL));
  34.         }
  35.  
  36.         /**
  37.          * @param string $email
  38.          * @param string $linktext
  39.          * @param int $level
  40.          * @return string|null
  41.          */
  42.         public function secureEmailAddress(string $email, string $linktext, int $level=1)/*: ?string*/ {
  43.  
  44.                 // see http://www.spamspan.de/
  45.  
  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.                 */
  55.  
  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>&#64;<span class="d">'.htmlentities($domain).'</span></span>';
  60.                         } else {
  61.                                 return '<span class="spamspan"><span class="u">'.htmlentities($user).'</span>&#64;<span class="d">'.htmlentities($domain).'</span>(<span class="t">'.htmlentities($linktext).'</span>)</span>';
  62.                         }
  63.                 }
  64.  
  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.                 */
  73.  
  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.                 }
  78.  
  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.                 */
  87.  
  88.                 if ($level == 3) {
  89.                         list($user, $domain) = explode('@', $email);
  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>';
  92.                 }
  93.  
  94.                 return null;
  95.  
  96.  
  97.                 // --- Old code ---
  98.  
  99.                 /*
  100.                 // Attention: document.write() JavaScript will damage the browser cache, which leads to bugs if you navigate back&forth with the browser navigation
  101.  
  102.                 $crypt_linktext = true;
  103.  
  104.                 // No new lines to avoid a JavaScript error!
  105.                 $linktext = str_replace("\r", ' ', $linktext);
  106.                 $linktext = str_replace("\n", ' ', $linktext);
  107.  
  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.                 }
  120.  
  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.                 }
  131.  
  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.                 }
  142.  
  143.                 if ($crypt_linktext) $linktext = str_replace('@', '&', $linktext);
  144.                 $email = str_replace('@', '&', $email);
  145.                 return $aus.'<noscript>'.htmlentities($linktext).' ('.htmlentities($email).')</noscript>';
  146.  
  147.                 */
  148.         }
  149.  
  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.          */
  160.         public function sendMail(string $to, string $title, string $msg, string $cc='', string $bcc='') {
  161.                 $h = new \SecureMailer();
  162.  
  163.                 // DM 14.04.2022: Added Reply-To, because some servers might change the 'From' attribute (Anti-Spoof?)
  164.                 $h->addHeader('From', OIDplus::config()->getValue('admin_email'));
  165.                 $h->addHeader('Reply-To', OIDplus::config()->getValue('admin_email'));
  166.  
  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);
  171.  
  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.  
  177.                 $h->addHeader('X-Mailer', 'PHP/'.PHP_VERSION);
  178.  
  179.                 // DM 14.04.2022: Commented out because of privacy
  180.                 //if (isset($_SERVER['REMOTE_ADDR'])) $h->addHeader('X-RemoteAddr', $_SERVER['REMOTE_ADDR']);
  181.  
  182.                 $h->addHeader('MIME-Version', '1.0');
  183.  
  184.                 // DM 14.04.2022: Changed from "ISO-8859-1" to "UTF-8"
  185.                 $h->addHeader('Content-Type', 'text/plain; charset=UTF-8');
  186.  
  187.                 $sent = $h->sendMail($to, $title, $msg);
  188.                 if (!$sent) {
  189.                         throw new OIDplusMailException(_L('Sending mail failed'));
  190.                 }
  191.         }
  192.  
  193. }
  194.