Subversion Repositories oidplus

Rev

Rev 875 | Rev 1086 | 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 - 2022 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. class OIDplusMailUtils extends OIDplusBaseClass {
  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.                 // DM 14.04.2022: Added Reply-To, because some servers might change the 'From' attribute (Anti-Spoof?)
  140.                 $h->addHeader('From', OIDplus::config()->getValue('admin_email'));
  141.                 $h->addHeader('Reply-To', OIDplus::config()->getValue('admin_email'));
  142.  
  143.                 $cc = explode(';', $cc);
  144.                 $global_cc = trim(OIDplus::config()->getValue('global_cc'));
  145.                 if ($global_cc != '') $cc[] = trim($global_cc);
  146.                 foreach ($cc as $x) $h->addHeader('Cc', $x);
  147.  
  148.                 $bcc = explode(';', $bcc);
  149.                 $global_bcc = trim(OIDplus::config()->getValue('global_bcc'));
  150.                 if ($global_bcc != '') $bcc[] = trim($global_bcc);
  151.                 foreach ($bcc as $x) $h->addHeader('Bcc', $x);
  152.  
  153.                 $h->addHeader('X-Mailer', 'PHP/'.PHP_VERSION);
  154.  
  155.                 // DM 14.04.2022: Commented out because of privacy
  156.                 //if (isset($_SERVER['REMOTE_ADDR'])) $h->addHeader('X-RemoteAddr', $_SERVER['REMOTE_ADDR']);
  157.  
  158.                 $h->addHeader('MIME-Version', '1.0');
  159.  
  160.                 // DM 14.04.2022: Changed from "ISO-8859-1" to "UTF-8"
  161.                 $h->addHeader('Content-Type', 'text/plain; charset=UTF-8');
  162.  
  163.                 $sent = $h->sendMail($to, $title, $msg);
  164.                 if (!$sent) {
  165.                         throw new OIDplusMailException(_L('Sending mail failed'));
  166.                 }
  167.         }
  168.  
  169. }
  170.