Subversion Repositories oidplus

Rev

Rev 103 | Rev 107 | 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. function oidplus_valid_email($email) {
  21.         return !empty(filter_var($email, FILTER_VALIDATE_EMAIL));
  22. }
  23.  
  24. function secure_email($email, $linktext, $level=1) {
  25.  
  26.         // see http://www.spamspan.de/
  27.  
  28.         /* Level 1 */
  29.         /*
  30.         <span class="spamspan">
  31.         <span class="u">user</span>
  32.         @
  33.         <span class="d">beispiel.de</span>
  34.         (<span class="t">Spam Hasser</span>)
  35.         </span>
  36.         */
  37.  
  38.         if ($level == 1) {
  39.                 @list($user, $domain) = explode('@', $email);
  40.                 if (($linktext == $email) || empty($linktext)) {
  41.                         return '<span class="spamspan"><span class="u">'.htmlentities($user).'</span>&#64;<span class="d">'.htmlentities($domain).'</span></span>';
  42.                 } else {
  43.                         return '<span class="spamspan"><span class="u">'.htmlentities($user).'</span>&#64;<span class="d">'.htmlentities($domain).'</span>(<span class="t">'.htmlentities($linktext).'</span>)</span>';
  44.                 }
  45.         }
  46.  
  47.         /* Level 2 */
  48.         /*
  49.         <span class="spamspan">
  50.                 <span class="u">user</span>
  51.                 <img alt="at" width="10" src="@.png">
  52.                 <span class="d">beispiel.de</span>
  53.         </span>
  54.         */
  55.  
  56.         if ($level == 2) {
  57.                 list($user, $domain) = explode('@', $email);
  58.                 return '<span class="spamspan"><span class="u">'.htmlentities($user).'</span><img alt="at" width="10" src="@.png"><span class="d">'.htmlentities($domain).'</span></span>';
  59.         }
  60.  
  61.         /* Level 3 */
  62.         /*
  63.         <span class="spamspan">
  64.                 <span class="u">user</span>
  65.                 [at]
  66.                 <span class="d">beispiel [dot] de</span>
  67.         </span>
  68.         */
  69.  
  70.         if ($level == 3) {
  71.                 list($user, $domain) = explode('@', $email);
  72.                 $domain = str_replace('.', ' [dot] ', $domain);
  73.                 return '<span class="spamspan"><span class="u">'.htmlentities($user).'</span> [at] <span class="d">'.htmlentities($domain).'</span></span>';
  74.         }
  75.  
  76.         return null;
  77.  
  78.  
  79.         // --- Old code ---
  80.  
  81.         // Attention: document.write() JavaScript will damage the browser cache, which leads to bugs if you navigate back&forth with the browser navigation
  82.  
  83.         // No new lines to avoid a JavaScript error!
  84.         $linktext = str_replace("\r", ' ', $linktext);
  85.         $linktext = str_replace("\n", ' ', $linktext);
  86.  
  87.         if (!function_exists('alas_js_crypt'))
  88.         {
  89.                 function alas_js_crypt($text)
  90.                 {
  91.                         $tmp = '';
  92.                         for ($i=0; $i<strlen($text); $i++)
  93.                         {
  94.                                 $tmp .= 'document.write("&#'.ord(substr($text, $i, 1)).';");';
  95.                         }
  96.                         return $tmp;
  97.                 }
  98.         }
  99.  
  100.         if (!function_exists('alas_js_write'))
  101.         {
  102.                 function alas_js_write($text)
  103.                 {
  104.                         $text = str_replace('\\', '\\\\', $text);
  105.                         $text = str_replace('"', '\"', $text);
  106.                         $text = str_replace('/', '\/', $text); // W3C Validation </a> -> <\/a>
  107.                         return 'document.write("'.$text.'");';
  108.                 }
  109.         }
  110.  
  111.         $aus = '';
  112.         if ($email != '')
  113.         {
  114.                 $aus .= '<script><!--'."\n"; // type="text/javascript" is not necessary in HTML5
  115.                 $aus .= alas_js_write('<a href="');
  116.                 $aus .= alas_js_crypt('mailto:'.$email);
  117.                 $aus .= alas_js_write('">');
  118.                 $aus .= $crypt_linktext ? alas_js_crypt($linktext) : alas_js_write($linktext);
  119.                 $aus .= alas_js_write('</a>').'// --></script>';
  120.         }
  121.  
  122.         if ($crypt_linktext) $linktext = str_replace('@', '&', $linktext);
  123.         $email = str_replace('@', '&', $email);
  124.         return $aus.'<noscript>'.htmlentities($linktext).' ('.htmlentities($email).')</noscript>';
  125. }
  126.  
  127. function insertWhitespace($str, $index) {
  128.         return substr($str, 0, $index) . ' ' . substr($str, $index);
  129. }
  130.  
  131. function js_escape($data) {
  132.         // TODO.... json_encode??
  133.         return "'" . str_replace('\\', '\\\\', $data) . "'";
  134. }
  135.  
  136. function oidplus_formatdate($date) {
  137.         $date = explode(' ', $date)[0];
  138.         if ($date == '0000-00-00') $date = '';
  139.         return $date;
  140. }
  141.  
  142.  
  143. class MailException extends Exception {}
  144.  
  145. function my_mail($to, $title, $msg, $cc='', $bcc='') {
  146.         $h = new SecureMailer();
  147.  
  148.         $title = $title;
  149.  
  150.         $h->addHeader('From', OIDplus::config()->getValue('admin_email'));
  151.  
  152.         if (!empty($cc)) $h->addHeader('Cc',  $cc);
  153.         if (!empty($bcc)) $h->addHeader('Bcc',  $bcc);
  154.  
  155.         $h->addHeader('X-Mailer', 'PHP/'.phpversion());
  156.         if (isset($_SERVER['REMOTE_ADDR'])) $h->addHeader('X-RemoteAddr', $_SERVER['REMOTE_ADDR']);
  157.  
  158.         $sent = $h->sendMail($to, $title, $msg);
  159.         if (!$sent) {
  160.                 throw new MailException('Sending mail failed');
  161.         }
  162. }
  163.  
  164. function trim_br($html) {
  165.         do { $html = preg_replace('@^\s*<\s*br\s*/{0,1}\s*>@isU', '', $html, -1, $count); } while ($count > 0); // left trim
  166.         do { $html = preg_replace('@<\s*br\s*/{0,1}\s*>\s*$@isU', '', $html, -1, $count); } while ($count > 0); // right trim
  167.         return $html;
  168. }
  169.  
  170. function verify_private_public_key($privKey, $pubKey) {
  171.         try {
  172.                 if (empty($privKey)) return false;
  173.                 if (empty($pubKey)) return false;
  174.                 $data = 'TEST';
  175.                 if (!@openssl_public_encrypt($data, $encrypted, $pubKey)) return false;
  176.                 if (!@openssl_private_decrypt($encrypted, $decrypted, $privKey)) return false;
  177.                 return $decrypted == $data;
  178.         } catch (Exception $e) {
  179.                 return false;
  180.         }
  181. }
  182.  
  183. function smallhash($data) { // get 31 bits from SHA1. Values 0..2147483647
  184.         return (hexdec(substr(sha1($data),-4*2)) & 2147483647);
  185. }
  186.  
  187. function isMobile() {
  188.         // If the page "index_mobile.php" is called, the user is explicitly requesting a mobile page
  189.         if (basename($_SERVER['SCRIPT_NAME']) == 'index_mobile.php') return true;
  190.  
  191.         // If the page "index_desktop.php" is called, the user is explicitly requesting a desktop page
  192.         if (basename($_SERVER['SCRIPT_NAME']) == 'index_desktop.php') return true;
  193.  
  194.         // Otherwise (for index.php), we check the user agent to see if the device is a mobile phone
  195.         // see https://deviceatlas.com/blog/list-of-user-agent-strings
  196.         if (!isset($_SERVER['HTTP_USER_AGENT'])) return false;
  197.         return
  198.                 (stripos($_SERVER['HTTP_USER_AGENT'], 'mobile') !== false) ||
  199.                 (stripos($_SERVER['HTTP_USER_AGENT'], 'iphone') !== false) ||
  200.                 (stripos($_SERVER['HTTP_USER_AGENT'], 'android') !== false) ||
  201.                 (stripos($_SERVER['HTTP_USER_AGENT'], 'windows phone') !== false);
  202. }
  203.