Subversion Repositories oidplus

Rev

Rev 370 | Rev 386 | 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??
289 daniel-mar 26
        $data = str_replace('\\', '\\\\', $data);
27
        $data = str_replace('\'', '\\\'', $data);
28
        return "'" . $data . "'";
2 daniel-mar 29
}
30
 
11 daniel-mar 31
function trim_br($html) {
32
        do { $html = preg_replace('@^\s*<\s*br\s*/{0,1}\s*>@isU', '', $html, -1, $count); } while ($count > 0); // left trim
33
        do { $html = preg_replace('@<\s*br\s*/{0,1}\s*>\s*$@isU', '', $html, -1, $count); } while ($count > 0); // right trim
34
        return $html;
35
}
74 daniel-mar 36
 
37
function verify_private_public_key($privKey, $pubKey) {
38
        try {
39
                if (empty($privKey)) return false;
40
                if (empty($pubKey)) return false;
41
                $data = 'TEST';
42
                if (!@openssl_public_encrypt($data, $encrypted, $pubKey)) return false;
43
                if (!@openssl_private_decrypt($encrypted, $decrypted, $privKey)) return false;
44
                return $decrypted == $data;
45
        } catch (Exception $e) {
46
                return false;
47
        }
48
}
49
 
50
function smallhash($data) { // get 31 bits from SHA1. Values 0..2147483647
250 daniel-mar 51
        return (hexdec(substr(sha1($data),-4*2)) & 0x7FFFFFFF);
74 daniel-mar 52
}
180 daniel-mar 53
 
182 daniel-mar 54
function split_firstname_lastname($name) {
55
        $ary = explode(' ', $name);
56
        $last_name = array_pop($ary);
57
        $first_name = implode(' ', $ary);
58
        return array($first_name, $last_name);
59
}
60
 
180 daniel-mar 61
function originHeaders() {
62
        // CORS
63
        // Author: Till Wehowski
182 daniel-mar 64
 
180 daniel-mar 65
        header("Access-Control-Allow-Credentials: true");
66
        header("Access-Control-Allow-Origin: ".strip_tags(((isset($_SERVER['HTTP_ORIGIN'])) ? $_SERVER['HTTP_ORIGIN'] : "*")));
67
 
68
        header("Access-Control-Allow-Headers: If-None-Match, X-Requested-With, Origin, X-Frdlweb-Bugs, Etag, X-Forgery-Protection-Token, X-CSRF-Token");
69
 
70
        if (isset($_SERVER['HTTP_ORIGIN'])) {
71
                header('X-Frame-Options: ALLOW-FROM '.$_SERVER['HTTP_ORIGIN']);
72
        } else {
73
                header_remove("X-Frame-Options");
74
        }
75
 
76
        $expose = array('Etag', 'X-CSRF-Token');
77
        foreach (headers_list() as $num => $header) {
78
                $h = explode(':', $header);
79
                $expose[] = trim($h[0]);
80
        }
81
        header("Access-Control-Expose-Headers: ".implode(',',$expose));
82
 
83
        header("Vary: Origin");
84
}
236 daniel-mar 85
 
86
function get_calling_function() {
87
        $ex = new Exception();
88
        $trace = $ex->getTrace();
360 daniel-mar 89
        if (!isset($trace[2])) return _L('(main)');
236 daniel-mar 90
        $final_call = $trace[2];
91
        return $final_call['file'].':'.$final_call['line'].'/'.$final_call['function'].'()';
92
}
346 daniel-mar 93
 
94
if (!function_exists('mb_wordwrap')) {
95
        function mb_wordwrap($str, $width = 75, $break = "\n", $cut = false) {
96
                // https://stackoverflow.com/a/4988494/488539
97
                $lines = explode($break, $str);
98
                foreach ($lines as &$line) {
99
                        $line = rtrim($line);
100
                        if (mb_strlen($line) <= $width) {
101
                                continue;
102
                        }
103
                        $words = explode(' ', $line);
104
                        $line = '';
105
                        $actual = '';
106
                        foreach ($words as $word) {
107
                                if (mb_strlen($actual.$word) <= $width) {
108
                                        $actual .= $word.' ';
109
                                } else {
110
                                        if ($actual != '') {
111
                                                $line .= rtrim($actual).$break;
112
                                        }
113
                                        $actual = $word;
114
                                        if ($cut) {
115
                                                while (mb_strlen($actual) > $width) {
116
                                                        $line .= mb_substr($actual, 0, $width).$break;
117
                                                        $actual = mb_substr($actual, $width);
118
                                                }
119
                                        }
120
                                        $actual .= ' ';
121
                                }
122
                        }
123
                        $line .= trim($actual);
124
                }
125
                return implode($break, $lines);
126
        }
127
}
355 daniel-mar 128
 
379 daniel-mar 129
function httpOutWithETag($out, $contentType, $filename='') {
130
        $etag = md5($out);
131
        header("Etag: $etag");
132
        header("Content-MD5: $etag"); // RFC 2616 clause 14.15
133
        if (isset($_SERVER['HTTP_IF_NONE_MATCH']) && (trim($_SERVER['HTTP_IF_NONE_MATCH']) == $etag)) {
134
                header("HTTP/1.1 304 Not Modified");
135
        } else {
136
                header("Content-Type: $contentType");
137
                if (!empty($filename)) {
138
                        header('Content-Disposition:inline; filename="'.$filename.'"');
139
                }
140
                echo $out;
141
        }
142
        die();
143
}
144
 
360 daniel-mar 145
function my_vsprintf($str, $args) {
146
        $n = 1;
147
        foreach ($args as $val) {
148
                $str = str_replace("%$n", $val, $str);
149
                $n++;
150
        }
370 daniel-mar 151
        $str = str_replace("%%", "%", $str);
360 daniel-mar 152
        return $str;
153
}
154
 
355 daniel-mar 155
function _L($str, ...$sprintfArgs) {
360 daniel-mar 156
        $lang = OIDplus::getCurrentLang();
157
 
158
        static $translation_array = array();
159
        static $translation_loaded = null;
160
        if ($lang != $translation_loaded) {
161
                $good = true;
162
                if (strpos($lang,'/') !== false) $good = false; // prevent attack (but actually, the sanitization in getCurrentLang should work)
163
                if (strpos($lang,'\\') !== false) $good = false; // prevent attack (but actually, the sanitization in getCurrentLang should work)
164
                if (strpos($lang,'..') !== false) $good = false; // prevent attack (but actually, the sanitization in getCurrentLang should work)
165
                $translation_file = __DIR__.'/../plugins/language/'.$lang.'/messages.xml';
166
                if ($good && !file_exists($translation_file)) $good = false;
167
                if ($good) {
168
                        $xml = simplexml_load_string(file_get_contents($translation_file));
169
                        foreach ($xml->message as $msg) {
170
                                $src = trim($msg->source->__toString());
171
                                $dst = trim($msg->target->__toString());
172
                                $translation_array[$src] = $dst;
173
                        }
174
                        $translation_loaded = $lang;
175
                }
176
        }
177
 
178
        if ($lang != $translation_loaded) {
179
                // Something bad happened (e.g. attack or message file not found)
180
                $res = $str;
181
        } else {
182
                $res = isset($translation_array[$str]) && !empty($translation_array[$str]) ? $translation_array[$str] : $str;
183
        }
184
 
185
        $res = str_replace('###', OIDplus::baseConfig()->getValue('TABLENAME_PREFIX', ''), $res);
186
 
187
        $res = my_vsprintf($res, $sprintfArgs);
188
 
189
        return $res;
370 daniel-mar 190
}