|
1 | 1 | <?php |
2 | | -error_reporting(E_ALL); |
3 | | -ini_set("display_errors", 1); |
| 2 | + |
| 3 | +ini_set('date.timezone', 'Europe/Vatican'); |
| 4 | + |
4 | 5 | require_once 'vendor/autoload.php'; |
5 | 6 | require_once 'includes/auth.php'; |
6 | 7 |
|
7 | 8 | use LiturgicalCalendar\Components\CalendarSelect; |
| 9 | +use LiturgicalCalendar\Components\CalendarSelect\OptionsType; |
| 10 | +use Dotenv\Dotenv; |
8 | 11 |
|
9 | 12 | if (!authenticated()) { |
10 | 13 | header("WWW-Authenticate: Basic realm=\"Please insert your credentials\""); |
|
13 | 16 | die(); |
14 | 17 | } |
15 | 18 |
|
16 | | -$apiVersion = isset($_GET['apiVersion']) ? $_GET['apiVersion'] : 'dev'; |
| 19 | +/** |
| 20 | + * Returns true if the server is running on localhost. |
| 21 | + * |
| 22 | + * @return bool true if the server is running on localhost, false otherwise |
| 23 | + */ |
| 24 | +function isLocalhost(): bool |
| 25 | +{ |
| 26 | + $serverAddress = isset($_SERVER['SERVER_ADDR']) ? $_SERVER['SERVER_ADDR'] : ''; |
| 27 | + $remoteAddress = isset($_SERVER['REMOTE_ADDR']) ? $_SERVER['REMOTE_ADDR'] : ''; |
| 28 | + $serverName = isset($_SERVER['SERVER_NAME']) ? $_SERVER['SERVER_NAME'] : ''; |
| 29 | + $localhostAddresses = ['127.0.0.1', '::1', '0.0.0.0']; |
| 30 | + $localhostNames = ['localhost', ...$localhostAddresses]; |
| 31 | + return in_array($serverAddress, $localhostAddresses) || |
| 32 | + in_array($remoteAddress, $localhostAddresses) || |
| 33 | + in_array($serverName, $localhostNames); |
| 34 | +} |
17 | 35 |
|
18 | | -$ch = curl_init(); |
| 36 | +$dotenv = Dotenv::createMutable($projectFolder, ['.env', '.env.local', '.env.development', '.env.production'], false); |
| 37 | +$dotenv->safeLoad(); |
| 38 | + |
| 39 | +$dotenv->ifPresent(['API_PROTOCOL', 'API_HOST'])->notEmpty(); |
| 40 | +$dotenv->ifPresent(['API_PROTOCOL'])->allowedValues(['http', 'https']); |
| 41 | +$dotenv->ifPresent(['API_PORT'])->isInteger(); |
| 42 | +$dotenv->ifPresent(['APP_ENV'])->notEmpty()->allowedValues(['development', 'production']); |
| 43 | + |
| 44 | +$logsFolder = 'logs'; |
| 45 | +if (!file_exists($logsFolder)) { |
| 46 | + if (!mkdir($logsFolder, 0755, true)) { |
| 47 | + throw new RuntimeException('Failed to create logs directory: ' . $logsFolder); |
| 48 | + } |
| 49 | +} |
| 50 | +$logFile = $logsFolder . DIRECTORY_SEPARATOR . 'litcaltests-error.log'; |
| 51 | + |
| 52 | +if ( |
| 53 | + isLocalhost() || ( isset($_ENV['APP_ENV']) && $_ENV['APP_ENV'] === 'development' ) |
| 54 | +) { |
| 55 | + ini_set('display_errors', 1); |
| 56 | + ini_set('display_startup_errors', 1); |
| 57 | + ini_set('log_errors', 1); |
| 58 | + ini_set('error_log', $logFile); |
| 59 | + error_reporting(E_ALL); |
| 60 | +} else { |
| 61 | + ini_set('display_errors', 0); |
| 62 | + ini_set('display_startup_errors', 0); |
| 63 | + ini_set('log_errors', 1); |
| 64 | + ini_set('error_log', $logFile); |
| 65 | + error_reporting(E_ALL & ~E_NOTICE & ~E_DEPRECATED); |
| 66 | +} |
19 | 67 |
|
20 | | -curl_setopt($ch, CURLOPT_URL, "https://litcal.johnromanodorazio.com/api/$apiVersion/tests"); |
| 68 | +$schema = isset($_ENV['API_PROTOCOL']) ? $_ENV['API_PROTOCOL'] : (isLocalhost() ? 'http' : 'https'); |
| 69 | +$host = isset($_ENV['API_HOST']) ? $_ENV['API_HOST'] : (isLocalhost() ? 'localhost' : 'litcal.johnromanodorazio.com'); |
| 70 | +$port = isset($_ENV['API_PORT']) ? (int) $_ENV['API_PORT'] : (isLocalhost() ? 8000 : 443); |
| 71 | +$apiVersion = isset($_GET['apiVersion']) ? $_GET['apiVersion'] : 'dev'; |
| 72 | +$ch = curl_init(); |
| 73 | +$baseUrl = isLocalhost() ? "$schema://$host:$port" : "$schema://$host/api/$apiVersion"; |
| 74 | + |
| 75 | +curl_setopt($ch, CURLOPT_URL, "$baseUrl/tests"); |
21 | 76 | curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); |
22 | 77 | $response = curl_exec($ch); |
23 | 78 | if (curl_errno($ch)) { |
|
26 | 81 | die($error_msg); |
27 | 82 | } |
28 | 83 | curl_close($ch); |
29 | | -if ($apiVersion === 'dev' || $apiVersion === 'v4') { |
30 | | - $LitCalTests = json_decode($response)->litcal_tests; |
31 | | -} else { |
32 | | - $LitCalTests = json_decode($response); |
33 | | -} |
| 84 | + |
| 85 | +$LitCalTests = json_decode($response)->litcal_tests; |
34 | 86 |
|
35 | 87 | include_once 'layout/head.php'; |
36 | 88 | include_once 'layout/sidebar.php'; |
|
57 | 109 | <div class="row justify-content-center align-items-start"> |
58 | 110 | <div class="form-group col col-md-3 border border-top-0 border-bottom-0 border-end-0 border-secondary"> |
59 | 111 | <?php |
60 | | - $options = ['locale' => $i18n->locale]; |
61 | | - $CalendarSelect = new CalendarSelect($options); |
62 | | - echo $CalendarSelect->getSelect([ |
63 | | - 'class' => 'form-select', |
64 | | - 'id' => 'APICalendarSelect', |
65 | | - 'options' => 'all', |
66 | | - 'label' => true, |
67 | | - 'labelStr' => _('Calendar to test') |
68 | | - ]); |
69 | | - ?> |
| 112 | + $options = ['locale' => $i18n->locale, 'url' => $baseUrl]; |
| 113 | + $CalendarSelect = new CalendarSelect($options) |
| 114 | + ->class('form-select') |
| 115 | + ->id('APICalendarSelect') |
| 116 | + ->label(true) |
| 117 | + ->labelText(_('Calendar to test')) |
| 118 | + ->setOptions(OptionsType::ALL); |
| 119 | + echo $CalendarSelect->getSelect(); |
| 120 | + ?> |
70 | 121 | </div> |
71 | 122 | <div class="form-group col col-md-9"> |
72 | 123 | <label><?php echo _("Test Type"); ?></label> |
|
127 | 178 | </main> |
128 | 179 | <!-- End of Main Content --> |
129 | 180 | <?php |
130 | | -if ($apiVersion === 'dev' || $apiVersion === 'v4') { |
131 | | - $eventsEndpoint = "https://litcal.johnromanodorazio.com/api/dev/events"; |
132 | | - $ch = curl_init(); |
133 | | - curl_setopt($ch, CURLOPT_URL, $eventsEndpoint); |
134 | | - curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); |
135 | | - curl_setopt($ch, CURLOPT_HTTPHEADER, ["Accept-Language: " . $i18n->locale]); |
136 | | - $eventsRaw = curl_exec($ch); |
137 | | - if (curl_errno($ch)) { |
138 | | - die('Could not fetch data from ' . $eventsEndpoint); |
139 | | - } |
140 | | - [ "litcal_events" => $LitCalAllLitEvents ] = json_decode( |
141 | | - $eventsRaw, |
142 | | - true |
143 | | - ); |
144 | | - if (JSON_ERROR_NONE !== json_last_error()) { |
145 | | - die('Could not parse JSON from ' . $eventsEndpoint . ' : ' . json_last_error_msg()); |
146 | | - } |
| 181 | +$eventsEndpoint = "$baseUrl/events"; |
| 182 | +$ch = curl_init(); |
| 183 | +curl_setopt($ch, CURLOPT_URL, $eventsEndpoint); |
| 184 | +curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); |
| 185 | +curl_setopt($ch, CURLOPT_HTTPHEADER, ["Accept-Language: " . $i18n->locale]); |
| 186 | +$eventsRaw = curl_exec($ch); |
| 187 | +if (curl_errno($ch)) { |
| 188 | + die('Could not fetch data from ' . $eventsEndpoint); |
| 189 | +} |
| 190 | +[ "litcal_events" => $LitCalAllLitEvents ] = json_decode( |
| 191 | + $eventsRaw, |
| 192 | + true |
| 193 | +); |
| 194 | +if (JSON_ERROR_NONE !== json_last_error()) { |
| 195 | + die('Could not parse JSON from ' . $eventsEndpoint . ' : ' . json_last_error_msg()); |
147 | 196 | } |
148 | 197 | include_once 'components/NewTestModal.php'; |
149 | 198 | ?> |
@@ -181,8 +230,15 @@ class="bi bi-exclamation-triangle-fill flex-shrink-0 me-2" viewBox="0 0 16 16" |
181 | 230 | <div id="responseMessage"></div> |
182 | 231 | </div> |
183 | 232 | <?php |
184 | | -$testsIndex = json_encode($LitCalTests); |
| 233 | +$testsIndex = json_encode($LitCalTests); |
185 | 234 | $litcal_events = json_encode($LitCalAllLitEvents); |
186 | | -echo "<script>const LitCalTests = Object.freeze($testsIndex); const LitcalEvents = Object.freeze($litcal_events);</script>"; |
| 235 | +$javascript = <<<SCRIPT |
| 236 | + <script> |
| 237 | + const LitCalTests = Object.freeze($testsIndex); |
| 238 | + const LitcalEvents = Object.freeze($litcal_events); |
| 239 | + const baseUrl = '$baseUrl'; |
| 240 | + </script> |
| 241 | +SCRIPT; |
| 242 | +echo $javascript; |
187 | 243 | include_once 'layout/footer.php'; |
188 | 244 | ?> |
0 commit comments