xref: /web-php/include/prepend.inc (revision 45d49c7e)
1<?php
2// Compress all pages, if ext/zlib is available on the mirror
3// XXX Deactivated by sas, causes errors towards delivery machines
4// ini_set("zlib.output_compression", 1);
5
6// See http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.9
7// for cache control header descriptions (used in many places on the site).
8
9// Provide default content-type, charset and language information
10// Manual pages will override this, and maybe others too
11header("Content-language: en");
12header("Content-type: text/html; charset=utf-8");
13
14// Opt out of FLoC
15header("Permissions-Policy: interest-cohort=()");
16
17/* Fix Silly Same Origin Policies */
18(function (): void {
19    if (!isset($_SERVER["HTTP_ORIGIN"])) {
20        return;
21    }
22
23    $host = parse_url($_SERVER["HTTP_ORIGIN"]);
24    if (strncmp(strrev($host["host"]), strrev("php.net"), strlen("php.net")) != 0) {
25        if ($host["host"] != $_SERVER["SERVER_NAME"]) {
26            exit(10);
27        }
28    }
29    if (isset($host["port"])) {
30        $hostname = $host["host"] . ":" . $host["port"];
31    } else {
32        $hostname = $host["host"];
33    }
34
35    header("Access-Control-Allow-Origin: http://$hostname");
36    if (isset($_SERVER["HTTP_ACCESS_CONTROL_REQUEST_HEADERS"])) {
37        $headers = $_SERVER["HTTP_ACCESS_CONTROL_REQUEST_HEADERS"];
38        $headers = str_replace(["\r", "\n", "\0"], "", $headers);
39        header("Access-Control-Allow-Headers: $headers");
40    }
41})();
42
43/* Clickjacking workaround. Nothing should be in a frame so it could technically be 'deny'
44 * but it doesn't make any difference anyway */
45header("X-Frame-Options: SAMEORIGIN");
46
47// Be 100% sure the timezone is set
48if (ini_get("date.timezone") === "" && function_exists("date_default_timezone_set")) {
49    date_default_timezone_set("UTC");
50}
51
52/* Compatibility with the PHP webserver.. */
53if (!isset($_SERVER["SERVER_ADDR"])) {
54    $_SERVER["SERVER_ADDR"] = "127.0.0.1";
55}
56
57// As of PHP 5.3.0 multibyte sequence errors are no longer
58// silent. Prior to that version this bitfield does not exist
59// so define it to prevent notices on older versions
60if (!defined("ENT_IGNORE")) {
61    define("ENT_IGNORE", 0);
62}
63
64// Prevent cross site scripting problems
65unset($RSIDEBAR_DATA);
66unset($SIDEBAR_DATA);
67unset($SEARCH_BASE);
68unset($LANG);
69unset($COUNTRY);
70unset($ONLOAD);
71unset($MYPHPNET);
72unset($LAST_UPDATED);
73
74// Load the My PHP.net settings before any includes
75myphpnet_load();
76
77// Site details (mirror site information)
78include __DIR__ . '/site.inc';
79
80// Choose language used for translated parts
81include __DIR__ . '/langchooser.inc';
82
83// Get country of the user and set it in a cookie
84include __DIR__ . '/ip-to-country.inc';
85
86// Common layout functions
87include __DIR__ . '/layout.inc';
88
89// This file is generated on rsync.php.net and propagated
90// from there. It just defines $LAST_UPDATED, which is the
91// mirror's last updated time.
92include __DIR__ . '/last_updated.inc';
93
94// -----------------------------------------------------------------------------
95
96// Load in the user preferences
97function myphpnet_load(): void
98{
99    global $MYPHPNET, $MYSITE;
100
101    // Empty the preferences array
102    $MYPHPNET = [false, false, "NONE", 0, false];
103
104    if ($MYSITE === 'http://docs.php.net/') {
105        $MYPHPNET[4] = true;
106    }
107
108    // If we have a cookie, set the values in the array
109    if (!empty($_COOKIE['MYPHPNET'])) {
110        $MYPHPNET = explode(",", $_COOKIE['MYPHPNET']);
111    }
112
113    // Ignore site part, and always use https://www.php.net
114    $MYPHPNET[2] = 'https://www.php.net';
115}
116
117// Get preferred language code
118function myphpnet_language(): string
119{
120    global $MYPHPNET;
121
122    // Return code
123    if (isset($MYPHPNET[0]) && $MYPHPNET[0]) {
124        return $MYPHPNET[0];
125    }
126    return '';
127}
128
129const MYPHPNET_URL_NONE = false;
130const MYPHPNET_URL_FUNC = 'quickref';
131const MYPHPNET_URL_MANUAL = 'manual';
132
133// Set URL search fallback preference
134function myphpnet_urlsearch($type = false)
135{
136    global $MYPHPNET;
137
138    // Set type if specified and if correct
139    if ($type && in_array($type, [MYPHPNET_URL_FUNC, MYPHPNET_URL_MANUAL], true)) {
140        $MYPHPNET[1] = $type;
141    }
142
143    // Return code or NONE
144    elseif (isset($MYPHPNET[1]) && !empty($MYPHPNET[1])) {
145        return $MYPHPNET[1];
146    } else { return MYPHPNET_URL_NONE; }
147}
148
149function myphpnet_showug($enable = null) {
150    global $MYPHPNET;
151
152    if (isset($_GET["showug"])) {
153        $enable = true;
154    }
155
156    if ($enable !== null) {
157        $MYPHPNET[3] = $enable;
158    }
159
160    if (isset($MYPHPNET[3])) {
161        return $MYPHPNET[3];
162    }
163
164    if ($_SERVER["REQUEST_TIME"] % 10) {
165        return true;
166    }
167
168    return false;
169}
170
171// Save user settings in cookie
172function myphpnet_save(): void
173{
174    global $MYPHPNET;
175
176    // Fill in values not specified
177    for ($i = 0; $i <= 3; $i++) {
178        if (!isset($MYPHPNET[$i])) { $MYPHPNET[$i] = false; }
179    }
180
181    // Set all the preferred values for a year
182    mirror_setcookie("MYPHPNET", join(",", $MYPHPNET), 60 * 60 * 24 * 365);
183
184}
185
186function google_cse(): void {
187?>
188<noscript>
189  php.net's search functionality requires JavaScript to operate. Please enable
190  JavaScript and reload to continue.
191</noscript>
192<script>
193    (function() {
194        var cx = '011570197911755000456:fip9wopfz_u';
195        var gcse = document.createElement('script');
196        gcse.type = 'text/javascript';
197        gcse.async = true;
198        gcse.src = 'https://cse.google.com/cse.js?cx=' + cx;
199        var s = document.getElementsByTagName('script')[0];
200        s.parentNode.insertBefore(gcse, s);
201    })();
202</script>
203<div class="gcse-search" data-linktarget></div>
204<?php
205}
206