Subversion Repositories oidplus

Rev

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