Rev 401 | Rev 438 | 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) { |
386 | daniel-mar | 32 | $count = 0; |
11 | daniel-mar | 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 | } |
||
74 | daniel-mar | 37 | |
38 | function verify_private_public_key($privKey, $pubKey) { |
||
39 | try { |
||
40 | if (empty($privKey)) return false; |
||
41 | if (empty($pubKey)) return false; |
||
42 | $data = 'TEST'; |
||
386 | daniel-mar | 43 | $encrypted = ''; |
44 | $decrypted = ''; |
||
74 | daniel-mar | 45 | if (!@openssl_public_encrypt($data, $encrypted, $pubKey)) return false; |
46 | if (!@openssl_private_decrypt($encrypted, $decrypted, $privKey)) return false; |
||
47 | return $decrypted == $data; |
||
48 | } catch (Exception $e) { |
||
49 | return false; |
||
50 | } |
||
51 | } |
||
52 | |||
53 | function smallhash($data) { // get 31 bits from SHA1. Values 0..2147483647 |
||
250 | daniel-mar | 54 | return (hexdec(substr(sha1($data),-4*2)) & 0x7FFFFFFF); |
74 | daniel-mar | 55 | } |
180 | daniel-mar | 56 | |
182 | daniel-mar | 57 | function split_firstname_lastname($name) { |
58 | $ary = explode(' ', $name); |
||
59 | $last_name = array_pop($ary); |
||
60 | $first_name = implode(' ', $ary); |
||
61 | return array($first_name, $last_name); |
||
62 | } |
||
63 | |||
180 | daniel-mar | 64 | function originHeaders() { |
65 | // CORS |
||
66 | // Author: Till Wehowski |
||
426 | daniel-mar | 67 | // TODO: add to class OIDplus |
182 | daniel-mar | 68 | |
180 | daniel-mar | 69 | header("Access-Control-Allow-Credentials: true"); |
70 | header("Access-Control-Allow-Origin: ".strip_tags(((isset($_SERVER['HTTP_ORIGIN'])) ? $_SERVER['HTTP_ORIGIN'] : "*"))); |
||
71 | |||
72 | header("Access-Control-Allow-Headers: If-None-Match, X-Requested-With, Origin, X-Frdlweb-Bugs, Etag, X-Forgery-Protection-Token, X-CSRF-Token"); |
||
73 | |||
74 | if (isset($_SERVER['HTTP_ORIGIN'])) { |
||
75 | header('X-Frame-Options: ALLOW-FROM '.$_SERVER['HTTP_ORIGIN']); |
||
76 | } else { |
||
77 | header_remove("X-Frame-Options"); |
||
78 | } |
||
79 | |||
80 | $expose = array('Etag', 'X-CSRF-Token'); |
||
81 | foreach (headers_list() as $num => $header) { |
||
82 | $h = explode(':', $header); |
||
83 | $expose[] = trim($h[0]); |
||
84 | } |
||
85 | header("Access-Control-Expose-Headers: ".implode(',',$expose)); |
||
86 | |||
87 | header("Vary: Origin"); |
||
88 | } |
||
236 | daniel-mar | 89 | |
90 | function get_calling_function() { |
||
91 | $ex = new Exception(); |
||
92 | $trace = $ex->getTrace(); |
||
360 | daniel-mar | 93 | if (!isset($trace[2])) return _L('(main)'); |
236 | daniel-mar | 94 | $final_call = $trace[2]; |
95 | return $final_call['file'].':'.$final_call['line'].'/'.$final_call['function'].'()'; |
||
96 | } |
||
346 | daniel-mar | 97 | |
98 | if (!function_exists('mb_wordwrap')) { |
||
99 | function mb_wordwrap($str, $width = 75, $break = "\n", $cut = false) { |
||
100 | // https://stackoverflow.com/a/4988494/488539 |
||
101 | $lines = explode($break, $str); |
||
102 | foreach ($lines as &$line) { |
||
103 | $line = rtrim($line); |
||
104 | if (mb_strlen($line) <= $width) { |
||
105 | continue; |
||
106 | } |
||
107 | $words = explode(' ', $line); |
||
108 | $line = ''; |
||
109 | $actual = ''; |
||
110 | foreach ($words as $word) { |
||
111 | if (mb_strlen($actual.$word) <= $width) { |
||
112 | $actual .= $word.' '; |
||
113 | } else { |
||
114 | if ($actual != '') { |
||
115 | $line .= rtrim($actual).$break; |
||
116 | } |
||
117 | $actual = $word; |
||
118 | if ($cut) { |
||
119 | while (mb_strlen($actual) > $width) { |
||
120 | $line .= mb_substr($actual, 0, $width).$break; |
||
121 | $actual = mb_substr($actual, $width); |
||
122 | } |
||
123 | } |
||
124 | $actual .= ' '; |
||
125 | } |
||
126 | } |
||
127 | $line .= trim($actual); |
||
128 | } |
||
129 | return implode($break, $lines); |
||
130 | } |
||
131 | } |
||
355 | daniel-mar | 132 | |
379 | daniel-mar | 133 | function httpOutWithETag($out, $contentType, $filename='') { |
134 | $etag = md5($out); |
||
135 | header("Etag: $etag"); |
||
136 | header("Content-MD5: $etag"); // RFC 2616 clause 14.15 |
||
137 | if (isset($_SERVER['HTTP_IF_NONE_MATCH']) && (trim($_SERVER['HTTP_IF_NONE_MATCH']) == $etag)) { |
||
138 | header("HTTP/1.1 304 Not Modified"); |
||
139 | } else { |
||
140 | header("Content-Type: $contentType"); |
||
141 | if (!empty($filename)) { |
||
142 | header('Content-Disposition:inline; filename="'.$filename.'"'); |
||
143 | } |
||
144 | echo $out; |
||
145 | } |
||
146 | die(); |
||
147 | } |
||
148 | |||
360 | daniel-mar | 149 | function my_vsprintf($str, $args) { |
150 | $n = 1; |
||
151 | foreach ($args as $val) { |
||
152 | $str = str_replace("%$n", $val, $str); |
||
153 | $n++; |
||
154 | } |
||
370 | daniel-mar | 155 | $str = str_replace("%%", "%", $str); |
360 | daniel-mar | 156 | return $str; |
157 | } |
||
158 | |||
355 | daniel-mar | 159 | function _L($str, ...$sprintfArgs) { |
401 | daniel-mar | 160 | static $translation_array = array(); |
161 | static $translation_loaded = null; |
||
162 | |||
360 | daniel-mar | 163 | $lang = OIDplus::getCurrentLang(); |
164 | |||
401 | daniel-mar | 165 | foreach (OIDplus::getAllPluginManifests('language') as $pluginManifest) { |
166 | $test_lang = $pluginManifest->getLanguageCode(); |
||
167 | |||
168 | if ($test_lang == $lang) { |
||
169 | if ($lang != $translation_loaded) { |
||
170 | $good = true; |
||
171 | if (strpos($lang,'/') !== false) $good = false; // prevent attack (but actually, the sanitization in getCurrentLang should work) |
||
172 | if (strpos($lang,'\\') !== false) $good = false; // prevent attack (but actually, the sanitization in getCurrentLang should work) |
||
173 | if (strpos($lang,'..') !== false) $good = false; // prevent attack (but actually, the sanitization in getCurrentLang should work) |
||
174 | |||
175 | if ($good) { |
||
176 | $wildcard = $pluginManifest->getLanguageMessages(); |
||
177 | if (strpos($wildcard,'/') !== false) $good = false; // just to be sure |
||
178 | if (strpos($wildcard,'\\') !== false) $good = false; // just to be sure |
||
179 | if (strpos($wildcard,'..') !== false) $good = false; // just to be sure |
||
180 | |||
181 | if ($good) { |
||
182 | $translation_files = glob(__DIR__.'/../plugins/language/'.$lang.'/'.$wildcard); |
||
183 | sort($translation_files); |
||
184 | foreach ($translation_files as $translation_file) { |
||
185 | $xml = simplexml_load_string(file_get_contents($translation_file)); |
||
186 | foreach ($xml->message as $msg) { |
||
187 | $src = trim($msg->source->__toString()); |
||
188 | $dst = trim($msg->target->__toString()); |
||
189 | $translation_array[$src] = $dst; |
||
190 | } |
||
191 | } |
||
192 | |||
193 | $translation_loaded = $lang; |
||
194 | } |
||
195 | } |
||
360 | daniel-mar | 196 | } |
197 | } |
||
198 | } |
||
199 | |||
200 | if ($lang != $translation_loaded) { |
||
201 | // Something bad happened (e.g. attack or message file not found) |
||
202 | $res = $str; |
||
203 | } else { |
||
204 | $res = isset($translation_array[$str]) && !empty($translation_array[$str]) ? $translation_array[$str] : $str; |
||
205 | } |
||
206 | |||
207 | $res = str_replace('###', OIDplus::baseConfig()->getValue('TABLENAME_PREFIX', ''), $res); |
||
208 | |||
209 | $res = my_vsprintf($res, $sprintfArgs); |
||
210 | |||
211 | return $res; |
||
370 | daniel-mar | 212 | } |
386 | daniel-mar | 213 | |
214 | function extractHtmlContents($cont) { |
||
215 | // make sure the program works even if the user provided HTML is not UTF-8 |
||
216 | $cont = iconv(mb_detect_encoding($cont, mb_detect_order(), true), 'UTF-8//IGNORE', $cont); |
||
217 | $bom = pack('H*','EFBBBF'); |
||
218 | $cont = preg_replace("/^$bom/", '', $cont); |
||
219 | |||
220 | $out_js = ''; |
||
221 | $m = array(); |
||
222 | preg_match_all('@<script[^>]*>(.+)</script>@ismU', $cont, $m); |
||
223 | foreach ($m[1] as $x) { |
||
224 | $out_js = $x . "\n\n"; |
||
225 | } |
||
226 | |||
227 | $out_css = ''; |
||
228 | $m = array(); |
||
229 | preg_match_all('@<style[^>]*>(.+)</style>@ismU', $cont, $m); |
||
230 | foreach ($m[1] as $x) { |
||
231 | $out_css = $x . "\n\n"; |
||
232 | } |
||
233 | |||
234 | $out_html = $cont; |
||
235 | $out_html = preg_replace('@^(.+)<body[^>]*>@isU', '', $out_html); |
||
236 | $out_html = preg_replace('@</body>.+$@isU', '', $out_html); |
||
237 | $out_html = preg_replace('@<title>.+</title>@isU', '', $out_html); |
||
238 | $out_html = preg_replace('@<h1>.+</h1>@isU', '', $out_html, 1); |
||
239 | $out_html = preg_replace('@<script[^>]*>(.+)</script>@ismU', '', $out_html); |
||
240 | $out_html = preg_replace('@<style[^>]*>(.+)</style>@ismU', '', $out_html); |
||
241 | |||
242 | return array($out_html, $out_js, $out_css); |
||
392 | daniel-mar | 243 | } |
244 | |||
400 | daniel-mar | 245 | function sha3_512($password, $raw_output=false) { |
392 | daniel-mar | 246 | if (version_compare(PHP_VERSION, '7.1.0') >= 0) { |
400 | daniel-mar | 247 | return hash('sha3-512', $password, $raw_output); |
392 | daniel-mar | 248 | } else { |
400 | daniel-mar | 249 | return bb\Sha3\Sha3::hash($password, 512, $raw_output); |
392 | daniel-mar | 250 | } |
251 | } |