xref: /web-php/include/site.inc (revision 9482f9bd)
1<?php
2
3// Define some constants, and $MIRRORS array
4// Mirror type constants
5const MIRROR_DOWNLOAD = 0;
6const MIRROR_STANDARD = 1;
7const MIRROR_SPECIAL = 2;
8const MIRROR_VIRTUAL = 3;
9
10// Mirror status constants
11const MIRROR_OK = 0;
12const MIRROR_NOTACTIVE = 1;
13const MIRROR_OUTDATED = 2;
14const MIRROR_DOESNOTWORK = 3;
15
16$MIRRORS = [
17    "https://www.php.net/" => [
18        "DEU",
19        "MyraCloud",
20        false,
21        "https://myracloud.com/en/",
22        MIRROR_SPECIAL,
23        true,
24        "en",
25        MIRROR_OK,
26    ],
27];
28
29/**
30 * @var array<string, string> $COUNTRIES
31 */
32$COUNTRIES = include __DIR__ . '/countries.inc';
33
34// Define $LANGUAGES array
35include __DIR__ . '/languages.inc';
36
37// Returns true if the current (or specified)
38// site is the primary mirror site
39function is_primary_site($site = false)
40{
41    global $MYSITE;
42    if (!$site) { $site = $MYSITE; }
43    return ($site == "https://www.php.net/" );
44}
45
46// Returns true if the current (or specified)
47// mirror site is an official mirror site
48function is_official_mirror($site = false)
49{
50    return (mirror_type($site) != MIRROR_VIRTUAL);
51}
52
53// Returns the current (or specified)
54// mirror site's default language
55function default_language($site = false)
56{
57    global $MIRRORS, $MYSITE;
58    if (!$site) { $site = $MYSITE; }
59    return (isset($MIRRORS[$site]) ? $MIRRORS[$site][6] : false);
60}
61
62// Returns the current (or specified)
63// mirror site's provider's name
64function mirror_provider($site = false)
65{
66    global $MIRRORS, $MYSITE;
67    if (!$site) { $site = $MYSITE; }
68
69    if (isset($MIRRORS[$site])) {
70       return $MIRRORS[$site][1];
71    }
72
73    if (isset($MIRRORS[$_SERVER['SERVER_ADDR']])) {
74       return $MIRRORS[$_SERVER['SERVER_ADDR']][1];
75    }
76
77    return false;
78}
79
80// Returns the current (or specified)
81// mirror site's provider's URL
82function mirror_provider_url($site = false)
83{
84    global $MIRRORS,$MYSITE;
85    if (!$site) { $site = $MYSITE; }
86
87    if (isset($MIRRORS[$site])) {
88        return $MIRRORS[$site][3];
89    }
90
91    if (isset($MIRRORS[$_SERVER['SERVER_ADDR']])) {
92        return $MIRRORS[$_SERVER['SERVER_ADDR']][3];
93    }
94
95    return false;
96}
97
98// Returns the current (or specified)
99// mirror site's type (use the constants!)
100function mirror_type($site = false)
101{
102    global $MIRRORS, $MYSITE;
103    if (!$site) { $site = $MYSITE; }
104
105    if (isset($MIRRORS[$site])) {
106        return $MIRRORS[$site][4];
107    }
108
109    if (isset($MIRRORS[$_SERVER['SERVER_ADDR']])) {
110        return $MIRRORS[$_SERVER['SERVER_ADDR']][4];
111    }
112
113    return false;
114}
115
116// Redirect to an URI on this mirror site or outside this site
117function mirror_redirect($absoluteURI): void
118{
119    global $MYSITE;
120
121    // Test if there is no protocol spec
122    // in the URL, then prepend local site URI
123    if (!preg_match("!^[a-z]+://!", $absoluteURI)) {
124        $absoluteURI = substr($MYSITE, 0, -1) . $absoluteURI;
125    }
126
127    // Do a redirection, if headers are not sent
128    if (!headers_sent()) {
129        header("Location: $absoluteURI");
130    }
131
132    // Always exit (the page is not to be displayed)
133    exit;
134}
135
136// Set a cookie for all the .php.net sites for the root
137// dir with $name and $content. Provide $exptime relatively
138// to the current time!
139function mirror_setcookie($name, $content, $exptime)
140{
141    if (!headers_sent()) {
142        if (is_official_mirror()) {
143            return setcookie($name, $content, time() + $exptime, '/', '.php.net');
144        }
145
146        return setcookie($name, $content, time() + $exptime, '/');
147    }
148
149    return false;
150}
151
152// Use this function to write out proper headers on
153// pages where the content should not be publicly cached
154function header_nocache(): void
155{
156    // Only try to send out headers in case
157    // those were not sent already
158    if (!headers_sent()) {
159        header("Cache-Control: private");
160        header("Pragma: no-cache");
161    }
162}
163
164function get_available_sqlites() {
165
166    $allsqlites = [1 => 'sqlite', 2 => 'sqlite3', 4 => 'pdo_sqlite', 8 => 'pdo_sqlite2'];
167    $avail = 0;
168
169    if (function_exists('sqlite_open')) {
170        $avail += 1;
171    }
172    if (class_exists('sqlite3')) {
173        $avail += 2;
174    }
175    if (method_exists('PDO', 'getavailabledrivers')) {
176        foreach (PDO::getavailabledrivers() as $driver) {
177            switch ($driver) {
178                case 'sqlite':
179                    $avail += 4;
180                    break;
181                case 'sqlite2':
182                    $avail += 8;
183                    break;
184            }
185        }
186    }
187    return $avail;
188}
189
190// Get all manual prefix search sections
191function get_manual_search_sections() {
192    return [
193        "", "book.", "ref.", "function.", "class.",
194        "features.", "control-structures.", "language.",
195        "about.", "faq.",
196    ];
197}
198
199function get_shortname($page) {
200    // We can at the very least kill the .php
201    $shorturl = substr($page, 0, -4);
202
203    // If its a "root page", we can't shorten it more
204    if (strpos($shorturl, "/") === false) {
205        return $shorturl;
206    }
207
208    // Manual pages get be quite short
209    if (strpos($page, "manual/") !== false) {
210        $sections = get_manual_search_sections();
211        // Kill the first entry (empty)
212        array_shift($sections);
213
214        // We can atleast remove manual/xx/
215        $shorturl = substr($page, strrpos($page, "/") + 1);
216
217        foreach ($sections as $section) {
218            // If we know this section
219            if (strpos($shorturl, $section) === 0) {
220                // We can make it even shorter
221                return substr($shorturl, strlen($section), -4);
222            }
223        }
224
225        // Didn't recognize the section, we better not shorten it at all
226        return $page;
227    }
228
229    // /conferences/index.php can be shortened to /conferences
230    if (strpos($page, "conferences/") !== false) {
231        return "conferences";
232    }
233
234    return $shorturl;
235}
236
237// Guess the current site from what Apache tells us.
238// This should be the main name of the mirror (in case
239// it works under more then one name). SERVER_NAME is
240// the name of the Apache vhost.
241
242if (!isset($_SERVER["HTTPS"]) || $_SERVER["HTTPS"] != "on") {
243    $proto = "http";
244} else {
245    $proto = "https";
246}
247
248if ($_SERVER["SERVER_PORT"] != '80' && $_SERVER["SERVER_PORT"] != 443) {
249    $MYSITE = $proto . '://' . $_SERVER["SERVER_NAME"] . ':' . (int)$_SERVER["SERVER_PORT"] . '/';
250    $msite = 'http://' . $_SERVER["SERVER_NAME"] . ':' . (int)$_SERVER["SERVER_PORT"] . '/';
251} else {
252    $MYSITE = $proto . '://' . $_SERVER["SERVER_NAME"] . '/';
253    $msite = 'https://' . $_SERVER["SERVER_NAME"] . '/';
254}
255
256// If the mirror is not registered with this name, provide defaults
257// (no country code, no search, no stats, English default language ...)
258if (!isset($MIRRORS[$MYSITE])) {
259    $MIRRORS[$MYSITE] = ["xx", $MYSITE, false, $MYSITE, MIRROR_VIRTUAL, false, "en", MIRROR_OK];
260}
261
262// Override mirror language with local preference
263if (isset($_SERVER['MIRROR_LANGUAGE'])) {
264    if (isset($LANGUAGES[$_SERVER['MIRROR_LANGUAGE']])) {
265        $MIRRORS[$MYSITE][6] = $_SERVER['MIRROR_LANGUAGE'];
266    }
267}
268
269// Fallback to English in case the language
270// set is definitely not supported
271if (!isset($LANGUAGES[$MIRRORS[$MYSITE][6]])) {
272    $MIRRORS[$MYSITE][6] = "en";
273}
274
275// Provide base href information to make relative links on
276// shortcut URL accessed pages work without redirection
277if (isset($_SERVER['BASE_PAGE'])) {
278    $_SERVER['BASE_HREF'] = $MYSITE . $_SERVER['BASE_PAGE'];
279} else { unset($_SERVER['BASE_HREF']); }
280