1: <?php
2:
3: /**
4: * functions/common.php
5: *
6: * Some common internet functions.
7: *
8: * PHP version 8
9: *
10: * LICENSE: This program is free software: you can redistribute it and/or modify
11: * it under the terms of the GNU Affero General Public License as
12: * published by the Free Software Foundation, either version 3 of the
13: * License, or (at your option) any later version.
14: * This program is distributed in the hope that it will be useful,
15: * but WITHOUT ANY WARRANTY; without even the implied warranty of
16: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17: * GNU Affero General Public License for more details.
18: * You should have received a copy of the GNU Affero General Public License
19: * along with this program. If not, see <http://www.gnu.org/licenses/>.
20: *
21: * @category Internet
22: * @package Common
23: * @author Henri Schumacher <henri.hulski@gazeta.pl>
24: * @copyright 2007-2014 Henri Schumacher
25: * @license http://www.gnu.org/licenses/agpl.html GNU Affero General Public License v3
26: * @version 1.0
27: * @link https://research.openhomeo.info/download/OpenHomeopath_1.0.2.tar.gz
28: */
29:
30: function url_exists($url) {
31: $ch = curl_init($url);
32: curl_setopt($ch, CURLOPT_NOBODY, true); // set to HEAD request
33: curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // don't output the response
34: curl_exec($ch);
35: $code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
36: curl_close($ch);
37: return ($code >= 200 && $code < 400) ? true : false;
38: }
39:
40: function bot_detected() {
41: if (isset($_SERVER['HTTP_USER_AGENT']) && preg_match('/bot|crawl|slurp|spider/i', $_SERVER['HTTP_USER_AGENT'])) {
42: return true;
43: }
44: else {
45: return false;
46: }
47: }
48:
49: // retrieve the browser language
50: function get_browser_language($allowed_languages, $default_language, $lang_variable = null, $strict_mode = true) {
51: // Use $_SERVER['HTTP_ACCEPT_LANGUAGE'] when no $lang_variable was parsed
52: if ($lang_variable === null) {
53: $lang_variable = $_SERVER['HTTP_ACCEPT_LANGUAGE'];
54: }
55:
56: // Some information send with $_SERVER['HTTP_ACCEPT_LANGUAGE']?
57: if (empty($lang_variable)) {
58: // No? => use default language
59: return $default_language;
60: }
61:
62: // split the header
63: $accepted_languages = preg_split('/,\s*/', $lang_variable);
64:
65: // set the default language
66: $current_lang = $default_language;
67: $current_q = 0;
68:
69: // parse all sent languages
70: foreach ($accepted_languages as $accepted_language) {
71: // get all info about this language
72: $res = preg_match ('/^([a-z]{1,8}(?:-[a-z]{1,8})*)'.
73: '(?:;\s*q=(0(?:\.[0-9]{1,3})?|1(?:\.0{1,3})?))?$/i', $accepted_language, $matches);
74:
75: // is the syntax valid?
76: if (!$res) {
77: // No? 0 => ignore
78: continue;
79: }
80:
81: // split the language code in parts
82: $lang_code = explode ('-', $matches[1]);
83:
84: // was a quality given?
85: if (isset($matches[2])) {
86: // use the quality
87: $lang_quality = (float)$matches[2];
88: } else {
89: // compatibility mode: use quality 1
90: $lang_quality = 1.0;
91: }
92:
93: // until the language code is empty...
94: while (count ($lang_code)) {
95: // check if the language is allowed
96: if (in_array (strtolower (join ('-', $lang_code)), $allowed_languages)) {
97: // compare quality
98: if ($lang_quality > $current_q) {
99: // use this language
100: $current_lang = strtolower (join ('-', $lang_code));
101: $current_q = $lang_quality;
102: break;
103: }
104: }
105: // in strict mode we're not minimalizing the language code
106: if ($strict_mode) {
107: break;
108: }
109: // delete the rightest part of the language code
110: array_pop ($lang_code);
111: }
112: }
113:
114: // return the found language
115: return $current_lang;
116: }
117: