Subversion Repositories oidplus

Rev

Rev 506 | Rev 552 | 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. function insertWhitespace($str, $index) {
  21.         return substr($str, 0, $index) . ' ' . substr($str, $index);
  22. }
  23.  
  24. function js_escape($data) {
  25.         // TODO.... json_encode??
  26.         $data = str_replace('\\', '\\\\', $data);
  27.         $data = str_replace('\'', '\\\'', $data);
  28.         return "'" . $data . "'";
  29. }
  30.  
  31. function trim_br($html) {
  32.         $count = 0;
  33.         do { $html = preg_replace('@^\s*<\s*br\s*/{0,1}\s*>@isU', '', $html, -1, $count); } while ($count > 0); // left trim
  34.         do { $html = preg_replace('@<\s*br\s*/{0,1}\s*>\s*$@isU', '', $html, -1, $count); } while ($count > 0); // right trim
  35.         return $html;
  36. }
  37.  
  38. function generateRandomString($length) {
  39.         $characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
  40.         $charactersLength = strlen($characters);
  41.         $randomString = '';
  42.         for ($i = 0; $i < $length; $i++) {
  43.                 $randomString .= $characters[rand(0, $charactersLength - 1)];
  44.         }
  45.         return $randomString;
  46. }
  47.  
  48. function verify_private_public_key($privKey, $pubKey) {
  49.         try {
  50.                 if (empty($privKey)) return false;
  51.                 if (empty($pubKey)) return false;
  52.                 $data = generateRandomString(25);
  53.                 $encrypted = '';
  54.                 $decrypted = '';
  55.                 if (!@openssl_public_encrypt($data, $encrypted, $pubKey)) return false;
  56.                 if (!@openssl_private_decrypt($encrypted, $decrypted, $privKey)) return false;
  57.                 return $decrypted == $data;
  58.         } catch (Exception $e) {
  59.                 return false;
  60.         }
  61. }
  62.  
  63. function smallhash($data) { // get 31 bits from SHA1. Values 0..2147483647
  64.         return (hexdec(substr(sha1($data),-4*2)) & 0x7FFFFFFF);
  65. }
  66.  
  67. function split_firstname_lastname($name) {
  68.         $ary = explode(' ', $name);
  69.         $last_name = array_pop($ary);
  70.         $first_name = implode(' ', $ary);
  71.         return array($first_name, $last_name);
  72. }
  73.  
  74. function originHeaders() {
  75.         // CORS
  76.         // Author: Till Wehowski
  77.         // TODO: add to class OIDplus
  78.  
  79.         header("Access-Control-Allow-Credentials: true");
  80.         header("Access-Control-Allow-Origin: ".strip_tags(((isset($_SERVER['HTTP_ORIGIN'])) ? $_SERVER['HTTP_ORIGIN'] : "*")));
  81.  
  82.         header("Access-Control-Allow-Headers: If-None-Match, X-Requested-With, Origin, X-Frdlweb-Bugs, Etag, X-Forgery-Protection-Token, X-CSRF-Token");
  83.  
  84.         if (isset($_SERVER['HTTP_ORIGIN'])) {
  85.                 header('X-Frame-Options: ALLOW-FROM '.$_SERVER['HTTP_ORIGIN']);
  86.         } else {
  87.                 header_remove("X-Frame-Options");
  88.         }
  89.  
  90.         $expose = array('Etag', 'X-CSRF-Token');
  91.         foreach (headers_list() as $num => $header) {
  92.                 $h = explode(':', $header);
  93.                 $expose[] = trim($h[0]);
  94.         }
  95.         header("Access-Control-Expose-Headers: ".implode(',',$expose));
  96.  
  97.         header("Vary: Origin");
  98. }
  99.  
  100. function get_calling_function() {
  101.         $ex = new Exception();
  102.         $trace = $ex->getTrace();
  103.         if (!isset($trace[2])) return _L('(main)');
  104.         $final_call = $trace[2];
  105.         return $final_call['file'].':'.$final_call['line'].'/'.$final_call['function'].'()';
  106. }
  107.  
  108. if (!function_exists('mb_wordwrap')) {
  109.         function mb_wordwrap($str, $width = 75, $break = "\n", $cut = false) {
  110.                 // https://stackoverflow.com/a/4988494/488539
  111.                 $lines = explode($break, $str);
  112.                 foreach ($lines as &$line) {
  113.                         $line = rtrim($line);
  114.                         if (mb_strlen($line) <= $width) {
  115.                                 continue;
  116.                         }
  117.                         $words = explode(' ', $line);
  118.                         $line = '';
  119.                         $actual = '';
  120.                         foreach ($words as $word) {
  121.                                 if (mb_strlen($actual.$word) <= $width) {
  122.                                         $actual .= $word.' ';
  123.                                 } else {
  124.                                         if ($actual != '') {
  125.                                                 $line .= rtrim($actual).$break;
  126.                                         }
  127.                                         $actual = $word;
  128.                                         if ($cut) {
  129.                                                 while (mb_strlen($actual) > $width) {
  130.                                                         $line .= mb_substr($actual, 0, $width).$break;
  131.                                                         $actual = mb_substr($actual, $width);
  132.                                                 }
  133.                                         }
  134.                                         $actual .= ' ';
  135.                                 }
  136.                         }
  137.                         $line .= trim($actual);
  138.                 }
  139.                 return implode($break, $lines);
  140.         }
  141. }
  142.  
  143. function httpOutWithETag($out, $contentType, $filename='') {
  144.         $etag = md5($out);
  145.         header("Etag: $etag");
  146.         header("Content-MD5: $etag"); // RFC 2616 clause 14.15
  147.         if (isset($_SERVER['HTTP_IF_NONE_MATCH']) && (trim($_SERVER['HTTP_IF_NONE_MATCH']) == $etag)) {
  148.                 header("HTTP/1.1 304 Not Modified");
  149.         } else {
  150.                 header("Content-Type: $contentType");
  151.                 if (!empty($filename)) {
  152.                         header('Content-Disposition:inline; filename="'.$filename.'"');
  153.                 }
  154.                 echo $out;
  155.         }
  156.         die();
  157. }
  158.  
  159. function my_vsprintf($str, $args) {
  160.         $n = 1;
  161.         foreach ($args as $val) {
  162.                 $str = str_replace("%$n", $val, $str);
  163.                 $n++;
  164.         }
  165.         $str = str_replace("%%", "%", $str);
  166.         return $str;
  167. }
  168.  
  169. function _L($str, ...$sprintfArgs) {
  170.         static $translation_array = array();
  171.         static $translation_loaded = null;
  172.  
  173.         $str = trim($str);
  174.  
  175.         if (!class_exists('OIDplus')) {
  176.                 return my_vsprintf($str, $sprintfArgs);
  177.         }
  178.  
  179.         $lang = OIDplus::getCurrentLang();
  180.         $ta = OIDplus::getTranslationArray($lang);
  181.         $res = (isset($ta[$lang]) && isset($ta[$lang][$str])) ? $ta[$lang][$str] : $str;
  182.  
  183.         $res = str_replace('###', OIDplus::baseConfig()->getValue('TABLENAME_PREFIX', ''), $res);
  184.  
  185.         $res = my_vsprintf($res, $sprintfArgs);
  186.  
  187.         return $res;
  188. }
  189.  
  190. function extractHtmlContents($cont) {
  191.         // make sure the program works even if the user provided HTML is not UTF-8
  192.         $cont = iconv(mb_detect_encoding($cont, mb_detect_order(), true), 'UTF-8//IGNORE', $cont);
  193.         $bom = pack('H*','EFBBBF');
  194.         $cont = preg_replace("/^$bom/", '', $cont);
  195.  
  196.         $out_js = '';
  197.         $m = array();
  198.         preg_match_all('@<script[^>]*>(.+)</script>@ismU', $cont, $m);
  199.         foreach ($m[1] as $x) {
  200.                 $out_js = $x . "\n\n";
  201.         }
  202.  
  203.         $out_css = '';
  204.         $m = array();
  205.         preg_match_all('@<style[^>]*>(.+)</style>@ismU', $cont, $m);
  206.         foreach ($m[1] as $x) {
  207.                 $out_css = $x . "\n\n";
  208.         }
  209.  
  210.         $out_html = $cont;
  211.         $out_html = preg_replace('@^(.+)<body[^>]*>@isU', '', $out_html);
  212.         $out_html = preg_replace('@</body>.+$@isU', '', $out_html);
  213.         $out_html = preg_replace('@<title>.+</title>@isU', '', $out_html);
  214.         $out_html = preg_replace('@<h1>.+</h1>@isU', '', $out_html, 1);
  215.         $out_html = preg_replace('@<script[^>]*>(.+)</script>@ismU', '', $out_html);
  216.         $out_html = preg_replace('@<style[^>]*>(.+)</style>@ismU', '', $out_html);
  217.  
  218.         return array($out_html, $out_js, $out_css);
  219. }
  220.  
  221. function sha3_512($password, $raw_output=false) {
  222.         if (version_compare(PHP_VERSION, '7.1.0') >= 0) {
  223.                 return hash('sha3-512', $password, $raw_output);
  224.         } else {
  225.                 return bb\Sha3\Sha3::hash($password, 512, $raw_output);
  226.         }
  227. }
  228.  
  229. function get_svn_revision($dir='') {
  230.         if (!empty($dir)) $dir .= '/';
  231.  
  232.         // Try to get the version via SQLite3
  233.         if (class_exists('SQLite3')) {
  234.                 try {
  235.                         $db = new SQLite3($dir.'.svn/wc.db');
  236.                         $results = $db->query('SELECT MIN(revision) AS rev FROM NODES_BASE');
  237.                         while ($row = $results->fetchArray()) {
  238.                                 return ($cachedVersion = $row['rev']);
  239.                         }
  240.                         $db->close();
  241.                         $db = null;
  242.                 } catch (Exception $e) {
  243.                 }
  244.         }
  245.         if (class_exists('PDO')) {
  246.                 try {
  247.                         $pdo = new PDO('sqlite:'.$dir.'.svn/wc.db');
  248.                         $res = $pdo->query('SELECT MIN(revision) AS rev FROM NODES_BASE');
  249.                         $row = $res->fetch();
  250.                         if ($row !== false) {
  251.                                 return ($cachedVersion = $row['rev']);
  252.                         }
  253.                         $pdo = null;
  254.                 } catch (Exception $e) {
  255.                 }
  256.         }
  257.  
  258.         // Try to find out the SVN version using the shell
  259.         // We don't prioritize this method, because a failed shell access will flood the apache error log with STDERR messages
  260.         $output = @shell_exec('svnversion '.escapeshellarg($dir));
  261.         $match = array();
  262.         if (preg_match('/\d+/', $output, $match)) {
  263.                 return ($cachedVersion = $match[0]);
  264.         }
  265.  
  266.         $output = @shell_exec('svn info '.escapeshellarg($dir));
  267.         if (preg_match('/Revision:\s*(\d+)/m', $output, $match)) { // do not translate
  268.                 return ($cachedVersion = $match[1]);
  269.         }
  270.  
  271.         return false;
  272. }
  273.  
  274. function get_gitsvn_revision($dir='') {
  275.         $ec = -1;
  276.         $out = array();
  277.         if (!empty($dir)) {
  278.                 @exec('cd '.escapeshellarg($dir).' && git log', $out, $ec);
  279.         } else {
  280.                 @exec('git log', $out, $ec);
  281.         }
  282.         if ($ec == 0) {
  283.                 $out = implode("\n", $out);
  284.                 $m = array();
  285.                 if (preg_match('%git-svn-id: (.+)@(\\d+) %ismU', $out, $m)) {
  286.                         return $m[2];
  287.                 }
  288.         }
  289.         return false;
  290. }
  291.