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