Subversion Repositories oidplus

Rev

Rev 236 | Rev 289 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
2 daniel-mar 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 insertWhitespace($str, $index) {
21
        return substr($str, 0, $index) . ' ' . substr($str, $index);
22
}
23
 
24
function js_escape($data) {
25
        // TODO.... json_encode??
26
        return "'" . str_replace('\\', '\\\\', $data) . "'";
27
}
28
 
11 daniel-mar 29
function trim_br($html) {
30
        do { $html = preg_replace('@^\s*<\s*br\s*/{0,1}\s*>@isU', '', $html, -1, $count); } while ($count > 0); // left trim
31
        do { $html = preg_replace('@<\s*br\s*/{0,1}\s*>\s*$@isU', '', $html, -1, $count); } while ($count > 0); // right trim
32
        return $html;
33
}
74 daniel-mar 34
 
35
function verify_private_public_key($privKey, $pubKey) {
36
        try {
37
                if (empty($privKey)) return false;
38
                if (empty($pubKey)) return false;
39
                $data = 'TEST';
40
                if (!@openssl_public_encrypt($data, $encrypted, $pubKey)) return false;
41
                if (!@openssl_private_decrypt($encrypted, $decrypted, $privKey)) return false;
42
                return $decrypted == $data;
43
        } catch (Exception $e) {
44
                return false;
45
        }
46
}
47
 
48
function smallhash($data) { // get 31 bits from SHA1. Values 0..2147483647
250 daniel-mar 49
        return (hexdec(substr(sha1($data),-4*2)) & 0x7FFFFFFF);
74 daniel-mar 50
}
180 daniel-mar 51
 
182 daniel-mar 52
function split_firstname_lastname($name) {
53
        $ary = explode(' ', $name);
54
        $last_name = array_pop($ary);
55
        $first_name = implode(' ', $ary);
56
        return array($first_name, $last_name);
57
}
58
 
180 daniel-mar 59
function originHeaders() {
60
        // CORS
61
        // Author: Till Wehowski
182 daniel-mar 62
 
180 daniel-mar 63
        header("Access-Control-Allow-Credentials: true");
64
        header("Access-Control-Allow-Origin: ".strip_tags(((isset($_SERVER['HTTP_ORIGIN'])) ? $_SERVER['HTTP_ORIGIN'] : "*")));
65
 
66
        header("Access-Control-Allow-Headers: If-None-Match, X-Requested-With, Origin, X-Frdlweb-Bugs, Etag, X-Forgery-Protection-Token, X-CSRF-Token");
67
 
68
        if (isset($_SERVER['HTTP_ORIGIN'])) {
69
                header('X-Frame-Options: ALLOW-FROM '.$_SERVER['HTTP_ORIGIN']);
70
        } else {
71
                header_remove("X-Frame-Options");
72
        }
73
 
74
        $expose = array('Etag', 'X-CSRF-Token');
75
        foreach (headers_list() as $num => $header) {
76
                $h = explode(':', $header);
77
                $expose[] = trim($h[0]);
78
        }
79
        header("Access-Control-Expose-Headers: ".implode(',',$expose));
80
 
81
        header("Vary: Origin");
82
}
236 daniel-mar 83
 
84
function get_calling_function() {
85
        $ex = new Exception();
86
        $trace = $ex->getTrace();
87
        if (!isset($trace[2])) return '(main)';
88
        $final_call = $trace[2];
89
        return $final_call['file'].':'.$final_call['line'].'/'.$final_call['function'].'()';
90
}