Subversion Repositories oidplus

Rev

Rev 1050 | 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.         public static function validMailAddress($email) {
  29.                 return !empty(filter_var($email, FILTER_VALIDATE_EMAIL));
  30.         }
  31.  
  32.         public static function secureEmailAddress($email, $linktext, $level=1) {
  33.  
  34.                 // see http://www.spamspan.de/
  35.  
  36.                 /* Level 1 */
  37.                 /*
  38.                 <span class="spamspan">
  39.                 <span class="u">user</span>
  40.                 @
  41.                 <span class="d">beispiel.de</span>
  42.                 (<span class="t">Spam Hasser</span>)
  43.                 </span>
  44.                 */
  45.  
  46.                 if ($level == 1) {
  47.                         @list($user, $domain) = explode('@', $email);
  48.                         if (($linktext == $email) || empty($linktext)) {
  49.                                 return '<span class="spamspan"><span class="u">'.htmlentities($user).'</span>&#64;<span class="d">'.htmlentities($domain).'</span></span>';
  50.                         } else {
  51.                                 return '<span class="spamspan"><span class="u">'.htmlentities($user).'</span>&#64;<span class="d">'.htmlentities($domain).'</span>(<span class="t">'.htmlentities($linktext).'</span>)</span>';
  52.                         }
  53.                 }
  54.  
  55.                 /* Level 2 */
  56.                 /*
  57.                 <span class="spamspan">
  58.                         <span class="u">user</span>
  59.                         <img alt="at" width="10" src="@.png">
  60.                         <span class="d">beispiel.de</span>
  61.                 </span>
  62.                 */
  63.  
  64.                 if ($level == 2) {
  65.                         list($user, $domain) = explode('@', $email);
  66.                         return '<span class="spamspan"><span class="u">'.htmlentities($user).'</span><img alt="at" width="10" src="@.png"><span class="d">'.htmlentities($domain).'</span></span>';
  67.                 }
  68.  
  69.                 /* Level 3 */
  70.                 /*
  71.                 <span class="spamspan">
  72.                         <span class="u">user</span>
  73.                         [at]
  74.                         <span class="d">beispiel [dot] de</span>
  75.                 </span>
  76.                 */
  77.  
  78.                 if ($level == 3) {
  79.                         list($user, $domain) = explode('@', $email);
  80.                         $domain = str_replace('.', ' '._L('[dot]').' ', $domain);
  81.                         return '<span class="spamspan"><span class="u">'.htmlentities($user).'</span> '._L('[at]').' <span class="d">'.htmlentities($domain).'</span></span>';
  82.                 }
  83.  
  84.                 return null;
  85.  
  86.  
  87.                 // --- Old code ---
  88.  
  89.                 /*
  90.                 // Attention: document.write() JavaScript will damage the browser cache, which leads to bugs if you navigate back&forth with the browser navigation
  91.  
  92.                 $crypt_linktext = true;
  93.  
  94.                 // No new lines to avoid a JavaScript error!
  95.                 $linktext = str_replace("\r", ' ', $linktext);
  96.                 $linktext = str_replace("\n", ' ', $linktext);
  97.  
  98.                 if (!function_exists('alas_js_crypt'))
  99.                 {
  100.                         function alas_js_crypt($text)
  101.                         {
  102.                                 $tmp = '';
  103.                                 for ($i=0; $i<strlen($text); $i++)
  104.                                 {
  105.                                         $tmp .= 'document.write("&#'.ord(substr($text, $i, 1)).';");';
  106.                                 }
  107.                                 return $tmp;
  108.                         }
  109.                 }
  110.  
  111.                 if (!function_exists('alas_js_write'))
  112.                 {
  113.                         function alas_js_write($text)
  114.                         {
  115.                                 $text = str_replace('\\', '\\\\', $text);
  116.                                 $text = str_replace('"', '\"', $text);
  117.                                 $text = str_replace('/', '\/', $text); // W3C Validation </a> -> <\/a>
  118.                                 return 'document.write("'.$text.'");';
  119.                         }
  120.                 }
  121.  
  122.                 $aus = '';
  123.                 if ($email != '')
  124.                 {
  125.                         $aus .= '<script><!--'."\n"; // type="text/javascript" is not necessary in HTML5
  126.                         $aus .= alas_js_write('<a href="');
  127.                         $aus .= alas_js_crypt('mailto:'.$email);
  128.                         $aus .= alas_js_write('">');
  129.                         $aus .= $crypt_linktext ? alas_js_crypt($linktext) : alas_js_write($linktext);
  130.                         $aus .= alas_js_write('</a>').'// --></script>';
  131.                 }
  132.  
  133.                 if ($crypt_linktext) $linktext = str_replace('@', '&', $linktext);
  134.                 $email = str_replace('@', '&', $email);
  135.                 return $aus.'<noscript>'.htmlentities($linktext).' ('.htmlentities($email).')</noscript>';
  136.  
  137.                 */
  138.         }
  139.  
  140.         public static function sendMail($to, $title, $msg, $cc='', $bcc='') {
  141.                 $h = new \SecureMailer();
  142.  
  143.                 // DM 14.04.2022: Added Reply-To, because some servers might change the 'From' attribute (Anti-Spoof?)
  144.                 $h->addHeader('From', OIDplus::config()->getValue('admin_email'));
  145.                 $h->addHeader('Reply-To', OIDplus::config()->getValue('admin_email'));
  146.  
  147.                 $cc = explode(';', $cc);
  148.                 $global_cc = trim(OIDplus::config()->getValue('global_cc'));
  149.                 if ($global_cc != '') $cc[] = trim($global_cc);
  150.                 foreach ($cc as $x) $h->addHeader('Cc', $x);
  151.  
  152.                 $bcc = explode(';', $bcc);
  153.                 $global_bcc = trim(OIDplus::config()->getValue('global_bcc'));
  154.                 if ($global_bcc != '') $bcc[] = trim($global_bcc);
  155.                 foreach ($bcc as $x) $h->addHeader('Bcc', $x);
  156.  
  157.                 $h->addHeader('X-Mailer', 'PHP/'.PHP_VERSION);
  158.  
  159.                 // DM 14.04.2022: Commented out because of privacy
  160.                 //if (isset($_SERVER['REMOTE_ADDR'])) $h->addHeader('X-RemoteAddr', $_SERVER['REMOTE_ADDR']);
  161.  
  162.                 $h->addHeader('MIME-Version', '1.0');
  163.  
  164.                 // DM 14.04.2022: Changed from "ISO-8859-1" to "UTF-8"
  165.                 $h->addHeader('Content-Type', 'text/plain; charset=UTF-8');
  166.  
  167.                 $sent = $h->sendMail($to, $title, $msg);
  168.                 if (!$sent) {
  169.                         throw new OIDplusMailException(_L('Sending mail failed'));
  170.                 }
  171.         }
  172.  
  173. }
  174.