<?php
/*
* OIDplus 2.0
* Copyright 2019 - 2021 Daniel Marschall, ViaThinkSoft
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
function insertWhitespace($str, $index) {
return substr($str, 0, $index) . ' ' . substr($str, $index);
}
function js_escape($data) {
// TODO.... json_encode??
return "'" . $data . "'";
}
function trim_br($html) {
$count = 0;
do { $html = preg_replace('@^\s*<\s*br\s*/{0,1}\s*>@isU', '', $html, -1, $count); } while ($count > 0); // left trim
do { $html = preg_replace('@<\s*br\s*/{0,1}\s*>\s*$@isU', '', $html, -1, $count); } while ($count > 0); // right trim
return $html;
}
function generateRandomString($length) {
// Note: This function can be used in temporary file names, so you
// may not generate illegal file name characters.
$characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
$charactersLength = strlen($characters);
$randomString = '';
for ($i = 0; $i < $length; $i++) {
$randomString .= $characters[rand(0, $charactersLength - 1)];
}
return $randomString;
}
function verify_private_public_key($privKey, $pubKey) {
try {
if (empty($privKey)) return false;
if (empty($pubKey)) return false;
$data = generateRandomString(25);
$encrypted = '';
$decrypted = '';
return $decrypted == $data;
} catch (Exception $e) {
return false;
}
}
function smallhash($data) { // get 31 bits from SHA1. Values 0..2147483647
}
function split_firstname_lastname($name) {
return array($first_name, $last_name);
}
function originHeaders() {
// CORS
// Author: Till Wehowski
// TODO: add to class OIDplus
header("Access-Control-Allow-Credentials: true");
header("Access-Control-Allow-Origin: ".strip_tags(((isset($_SERVER['HTTP_ORIGIN'])) ?
$_SERVER['HTTP_ORIGIN'] : "*")));
header("Access-Control-Allow-Headers: If-None-Match, X-Requested-With, Origin, X-Frdlweb-Bugs, Etag, X-Forgery-Protection-Token, X-CSRF-Token");
if (isset($_SERVER['HTTP_ORIGIN'])) {
header('X-Frame-Options: ALLOW-FROM '.$_SERVER['HTTP_ORIGIN']);
} else {
header_remove("X-Frame-Options");
}
$expose = array('Etag', 'X-CSRF-Token');
}
}
function get_calling_function() {
$ex = new Exception();
$trace = $ex->getTrace();
if (!isset($trace[2])) return _L
('(main)');
$final_call = $trace[2];
return $final_call['file'].':'.$final_call['line'].'/'.$final_call['function'].'()';
}
function mb_wordwrap($str, $width = 75, $break = "\n", $cut = false) {
// https://stackoverflow.com/a/4988494/488539
foreach ($lines as &$line) {
continue;
}
$line = '';
$actual = '';
foreach ($words as $word) {
$actual .= $word.' ';
} else {
if ($actual != '') {
$line .= rtrim($actual).$break;
}
$actual = $word;
if ($cut) {
$line .= mb_substr($actual, 0, $width).$break;
}
}
$actual .= ' ';
}
}
}
}
}
function httpOutWithETag($out, $contentType, $filename='') {
header("Content-MD5: $etag"); // RFC 2616 clause 14.15
if (isset($_SERVER['HTTP_IF_NONE_MATCH']) && (trim($_SERVER['HTTP_IF_NONE_MATCH']) == $etag)) {
header("HTTP/1.1 304 Not Modified");
} else {
header("Content-Type: $contentType");
header('Content-Disposition:inline; filename="'.$filename.'"');
}
echo $out;
}
}
function my_vsprintf($str, $args) {
$n = 1;
foreach ($args as $val) {
$n++;
}
return $str;
}
function _L($str, ...$sprintfArgs) {
static
$translation_array = array();
static $translation_loaded = null;
return my_vsprintf($str, $sprintfArgs);
}
$lang = OIDplus::getCurrentLang();
$ta = OIDplus::getTranslationArray($lang);
$res = (isset($ta[$lang]) && isset($ta[$lang][$str])) ?
$ta[$lang][$str] : $str;
$res = str_replace('###', OIDplus
::baseConfig()->getValue('TABLENAME_PREFIX', ''), $res);
$res = my_vsprintf($res, $sprintfArgs);
return $res;
}
function _CheckParamExists($params, $key) {
if (!isset($params[$key])) throw new OIDplusException
(_L
('Parameter %1 is missing', $key));
}
function extractHtmlContents($cont) {
// make sure the program works even if the user provided HTML is not UTF-8
$bom = pack('H*','EFBBBF');
$out_js = '';
foreach ($m[1] as $x) {
$out_js = $x . "\n\n";
}
$out_css = '';
foreach ($m[1] as $x) {
$out_css = $x . "\n\n";
}
$out_html = $cont;
$out_html = preg_replace('@^(.+)<body[^>]*>@isU', '', $out_html);
$out_html = preg_replace('@<title>.+</title>@isU', '', $out_html);
$out_html = preg_replace('@<h1>.+</h1>@isU', '', $out_html, 1);
$out_html = preg_replace('@<script[^>]*>(.+)</script>@ismU', '', $out_html);
$out_html = preg_replace('@<style[^>]*>(.+)</style>@ismU', '', $out_html);
return array($out_html, $out_js, $out_css);
}
function sha3_512($password, $raw_output=false) {
return hash('sha3-512', $password, $raw_output);
} else {
return bb\Sha3\Sha3
::hash($password, 512, $raw_output);
}
}
function get_svn_revision($dir='') {
if (!empty($dir)) $dir .= '/';
// Try to get the version via SQLite3
try {
$db = new SQLite3($dir.'.svn/wc.db');
$results = $db->query('SELECT MIN(revision) AS rev FROM NODES_BASE');
while ($row = $results->fetchArray()) {
return ($cachedVersion = $row['rev']);
}
$db->close();
$db = null;
} catch (Exception $e) {
}
}
try {
$pdo = new PDO('sqlite:'.$dir.'.svn/wc.db');
$res = $pdo->query('SELECT MIN(revision) AS rev FROM NODES_BASE');
$row = $res->fetch();
if ($row !== false) {
return ($cachedVersion = $row['rev']);
}
$pdo = null;
} catch (Exception $e) {
}
}
// Try to find out the SVN version using the shell
// We don't prioritize this method, because a failed shell access will flood the apache error log with STDERR messages
return ($cachedVersion = $match[0]);
}
if (preg_match('/Revision:\s*(\d+)/m', $output, $match)) { // do not translate
return ($cachedVersion = $match[1]);
}
return false;
}
function get_gitsvn_revision($dir='') {
try {
// requires danielmarschall/git_utils.inc.php
$commit_msg = git_get_latest_commit_message($dir.'/.git');
} catch (Exception $e) {
// Try command-line
$ec = -1;
} else {
@exec('git log', $out, $ec);
}
if ($ec == 0) {
} else {
return false;
}
}
if (preg_match('%git-svn-id: (.+)@(\\d+) %ismU', $commit_msg, $m)) {
return $m[2];
} else {
return false;
}
}
// PHP 7.x compatibility
function str_ends_with($haystack, $needle) {
return $length > 0 ?
substr($haystack, -$length) === $needle : true;
}
}
// PHP 7.x compatibility
function str_starts_with($haystack, $needle) {
return strpos($haystack, $needle) === 0;
}
}
function rec_is_dir($dir) {
foreach ($dirs as $dir) {
if (is_dir($dir)) return true;
}
return false;
}
function isInternetExplorer() {
// see also includes/oidplus_base.js
$ua = isset($_SERVER['HTTP_USER_AGENT']) ?
$_SERVER['HTTP_USER_AGENT'] : '';
return ((strpos($ua,'MSIE ') !== false) || (strpos($ua,'Trident/') !== false));
}
function url_get_contents($url) {
if (ini_get('curl.cainfo') == '') curl_setopt($ch, CURLOPT_CAINFO
, OIDplus
::localpath() . 'vendor/cacert.pem');
} else {
if ($res === false) return false;
}
return $res;
}