Subversion Repositories oidplus

Rev

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