1<?php
2
3namespace Safe;
4
5use Safe\Exceptions\ApacheException;
6
7/**
8 * Fetch the Apache version.
9 *
10 * @return string Returns the Apache version on success.
11 * @throws ApacheException
12 *
13 */
14function apache_get_version(): string
15{
16    error_clear_last();
17    $result = \apache_get_version();
18    if ($result === false) {
19        throw ApacheException::createFromPhpError();
20    }
21    return $result;
22}
23
24
25/**
26 * Retrieve an Apache environment variable specified by
27 * variable.
28 *
29 * This function requires Apache 2 otherwise it's undefined.
30 *
31 * @param string $variable The Apache environment variable
32 * @param bool $walk_to_top Whether to get the top-level variable available to all Apache layers.
33 * @return string The value of the Apache environment variable on success
34 * @throws ApacheException
35 *
36 */
37function apache_getenv(string $variable, bool $walk_to_top = false): string
38{
39    error_clear_last();
40    $result = \apache_getenv($variable, $walk_to_top);
41    if ($result === false) {
42        throw ApacheException::createFromPhpError();
43    }
44    return $result;
45}
46
47
48/**
49 * Fetches all HTTP request headers from the current request. Works in the
50 * Apache, FastCGI, CLI, FPM and NSAPI server module
51 * in Netscape/iPlanet/SunONE webservers.
52 *
53 * @return array An associative array of all the HTTP headers in the current request.
54 * @throws ApacheException
55 *
56 */
57function apache_request_headers(): array
58{
59    error_clear_last();
60    $result = \apache_request_headers();
61    if ($result === false) {
62        throw ApacheException::createFromPhpError();
63    }
64    return $result;
65}
66
67
68/**
69 * apache_reset_timeout resets the Apache write timer,
70 * which defaults to 300 seconds. With set_time_limit(0);
71 * ignore_user_abort(true) and periodic
72 * apache_reset_timeout calls, Apache can theoretically
73 * run forever.
74 *
75 * This function requires Apache 1.
76 *
77 * @throws ApacheException
78 *
79 */
80function apache_reset_timeout(): void
81{
82    error_clear_last();
83    $result = \apache_reset_timeout();
84    if ($result === false) {
85        throw ApacheException::createFromPhpError();
86    }
87}
88
89
90/**
91 * Fetch all HTTP response headers.  Works in the
92 * Apache, FastCGI, CLI, FPM and NSAPI server module
93 * in Netscape/iPlanet/SunONE webservers.
94 *
95 * @return array An array of all Apache response headers on success.
96 * @throws ApacheException
97 *
98 */
99function apache_response_headers(): array
100{
101    error_clear_last();
102    $result = \apache_response_headers();
103    if ($result === false) {
104        throw ApacheException::createFromPhpError();
105    }
106    return $result;
107}
108
109
110/**
111 * apache_setenv sets the value of the Apache
112 * environment variable specified by
113 * variable.
114 *
115 * @param string $variable The environment variable that's being set.
116 * @param string $value The new variable value.
117 * @param bool $walk_to_top Whether to set the top-level variable available to all Apache layers.
118 * @throws ApacheException
119 *
120 */
121function apache_setenv(string $variable, string $value, bool $walk_to_top = false): void
122{
123    error_clear_last();
124    $result = \apache_setenv($variable, $value, $walk_to_top);
125    if ($result === false) {
126        throw ApacheException::createFromPhpError();
127    }
128}
129
130
131/**
132 * Fetches all HTTP headers from the current request.
133 *
134 * This function is an alias for apache_request_headers.
135 * Please read the apache_request_headers
136 * documentation for more information on how this function works.
137 *
138 * @return array An associative array of all the HTTP headers in the current request.
139 * @throws ApacheException
140 *
141 */
142function getallheaders(): array
143{
144    error_clear_last();
145    $result = \getallheaders();
146    if ($result === false) {
147        throw ApacheException::createFromPhpError();
148    }
149    return $result;
150}
151
152
153/**
154 * virtual is an Apache-specific function which
155 * is similar to &lt;!--#include virtual...--&gt; in
156 * mod_include.
157 * It performs an Apache sub-request.  It is useful for including
158 * CGI scripts or .shtml files, or anything else that you would
159 * parse through Apache. Note that for a CGI script, the script
160 * must generate valid CGI headers.  At the minimum that means it
161 * must generate a Content-Type header.
162 *
163 * To run the sub-request, all buffers are terminated and flushed to the
164 * browser, pending headers are sent too.
165 *
166 * @param string $filename The file that the virtual command will be performed on.
167 * @throws ApacheException
168 *
169 */
170function virtual(string $filename): void
171{
172    error_clear_last();
173    $result = \virtual($filename);
174    if ($result === false) {
175        throw ApacheException::createFromPhpError();
176    }
177}
178