-
-
Notifications
You must be signed in to change notification settings - Fork 11
/
LitCalRegionalData.php
323 lines (286 loc) · 15.2 KB
/
LitCalRegionalData.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
<?php
include_once( 'includes/enums/AcceptHeader.php' );
include_once( 'includes/enums/LitSchema.php' );
include_once( 'includes/enums/RequestMethod.php' );
include_once( 'includes/enums/RequestContentType.php' );
include_once( 'includes/enums/ReturnType.php' );
include_once( 'includes/APICore.php' );
include_once( 'vendor/autoload.php' );
use Swaggest\JsonSchema\Schema;
if( file_exists("allowedOrigins.php") ) {
include_once( 'allowedOrigins.php' );
}
$allowedOrigins = [
"https://johnromanodorazio.com",
"https://www.johnromanodorazio.com",
"https://litcal.johnromanodorazio.com",
"https://litcal-staging.johnromanodorazio.com"
];
if( defined('ALLOWED_ORIGINS') && is_array( ALLOWED_ORIGINS ) ) {
$allowedOrigins = array_merge( $allowedOrigins, ALLOWED_ORIGINS );
}
$LitCalRegionalData = new LitCalRegionalData();
$LitCalRegionalData->APICore->setAllowedOrigins( $allowedOrigins );
$LitCalRegionalData->APICore->setAllowedReferers( array_map( function($el){ return $el . "/"; }, $allowedOrigins ) );
$LitCalRegionalData->APICore->setAllowedAcceptHeaders( [ AcceptHeader::JSON ] );
$LitCalRegionalData->APICore->setAllowedRequestContentTypes( [ RequestContentType::JSON, RequestContentType::FORMDATA ] );
$LitCalRegionalData->Init();
class LitCalRegionalData {
private object $DATA;
private object $RESPONSE;
//The General Index is currently only used for diocesan calendars
private ?stdClass $GeneralIndex = null;
public APICore $APICore;
public function __construct(){
$this->APICore = new APICore();
$this->RESPONSE = new stdClass();
$this->RESPONSE->requestHeadersReceived = $this->APICore->getJsonEncodedRequestHeaders();
}
private function handleRequestedMethod() {
switch( strtoupper( $_SERVER[ "REQUEST_METHOD" ] ) ) {
case RequestMethod::GET:
$this->handleGetPostRequests( $_GET );
break;
case RequestMethod::POST:
$this->handleGetPostRequests( $_POST );
break;
case RequestMethod::PUT:
case RequestMethod::PATCH:
$this->handlePutPatchDeleteRequests( RequestMethod::PUT );
break;
case RequestMethod::DELETE:
$this->handlePutPatchDeleteRequests( RequestMethod::DELETE );
break;
case RequestMethod::OPTIONS:
//continue;
break;
default:
header( $_SERVER[ "SERVER_PROTOCOL" ]." 405 Method Not Allowed", true, 405 );
$errorMessage = '{"error":"You seem to be forming a strange kind of request? Allowed Request Methods are ';
$errorMessage .= implode( ' and ', $this->AllowedRequestMethods );
$errorMessage .= ', but your Request Method was ' . strtoupper( $_SERVER[ 'REQUEST_METHOD' ] ) . '"}';
die( $errorMessage );
}
}
private function handleGetPostRequests( array $REQUEST ) {
$this->APICore->validateAcceptHeader( true );
if( $this->APICore->getRequestContentType() === 'application/json' ) {
$this->DATA = $this->APICore->retrieveRequestParamsFromJsonBody();
} else {
$this->DATA = (object)$REQUEST;
}
$this->retrieveRegionalCalendar();
}
private function handlePutPatchDeleteRequests( string $requestMethod ) {
$this->APICore->validateAcceptHeader( false );
$this->APICore->enforceAjaxRequest();
$this->APICore->enforceReferer();
if( $this->APICore->getRequestContentType() === 'application/json' ) {
$this->DATA = $this->APICore->retrieveRequestParamsFromJsonBody();
if( RequestMethod::PUT === $requestMethod ) {
$this->writeRegionalCalendar();
} elseif( RequestMethod::DELETE === $requestMethod ) {
$this->deleteRegionalCalendar();
}
} else{
header( $_SERVER[ "SERVER_PROTOCOL" ]." 415 Unsupported Media Type", true, 415 );
die( '{"error":"You seem to be forming a strange kind of request? Only \'application/json\' is allowed as the Content Type for the body of the Request when using Request Methods PUT, PATCH, or DELETE: the Content Type for the body of your Request was '.$_SERVER[ 'CONTENT_TYPE' ].' and you are using Request Method ' . $_SERVER[ 'REQUEST_METHOD' ] . '"}' );
}
}
private function retrieveRegionalCalendar() {
if( property_exists( $this->DATA, 'category' ) && property_exists( $this->DATA, 'key' ) ) {
$category = $this->DATA->category;
$key = $this->DATA->key;
switch( $category ) {
case "diocesanCalendar":
$calendarDataFile = $this->GeneralIndex->$key->path;
break;
case "widerRegionCalendar":
$calendarDataFile = "nations/{$key}.json";
break;
case "nationalCalendar":
$calendarDataFile = "nations/{$key}/{$key}.json";
break;
}
if( file_exists( $calendarDataFile ) ) {
if( $category === "diocesanCalendar" ) {
echo file_get_contents( $calendarDataFile );
die();
} else {
$this->RESPONSE = json_decode( file_get_contents( $calendarDataFile ) );
$uKey = strtoupper( $key );
if( $category === "widerRegionCalendar" ) {
$this->RESPONSE->isMultilingual = is_dir( "nations/{$uKey}" );
$locale = strtolower( $this->DATA->locale );
if( file_exists( "nations/{$uKey}/{$locale}.json" ) ) {
$localeData = json_decode( file_get_contents( "nations/{$uKey}/{$locale}.json" ) );
foreach( $this->RESPONSE->LitCal as $idx => $el ) {
$this->RESPONSE->LitCal[$idx]->Festivity->name = $localeData->{$this->RESPONSE->LitCal[$idx]->Festivity->tag};
}
}
}
echo json_encode( $this->RESPONSE );
die();
}
} else {
header( $_SERVER[ "SERVER_PROTOCOL" ]." 404 Not Found", true, 404 );
echo "{\"message\":\"file $calendarDataFile does not exist\"}";
die();
}
}
}
private function writeRegionalCalendar() {
if( property_exists( $this->DATA, 'LitCal' ) && property_exists( $this->DATA, 'Metadata' ) && property_exists( $this->DATA, 'Settings' ) ) {
$region = $this->DATA->Metadata->Region;
if( $region === 'UNITED STATES' ) {
$region = 'USA';
}
$path = "nations/{$region}";
if( !file_exists( $path ) ) {
mkdir( $path, 0755, true );
}
$test = $this->validateDataAgainstSchema( $this->DATA, LitSchema::NATIONAL );
if( $test === true ) {
$data = json_encode( $this->DATA, JSON_PRETTY_PRINT|JSON_UNESCAPED_UNICODE );
file_put_contents( $path . "/{$region}.json", $data . PHP_EOL );
header( $_SERVER[ "SERVER_PROTOCOL" ]." 201 Created", true, 201 );
die( '{"success":"National calendar created or updated for nation \"'. $this->DATA->Metadata->Region .'\""}' );
} else {
header( $_SERVER[ "SERVER_PROTOCOL" ]." 422 Unprocessable Entity", true, 422 );
die( json_encode( $test ) );
}
}
else if ( property_exists( $this->DATA, 'LitCal' ) && property_exists( $this->DATA, 'Metadata' ) && property_exists( $this->DATA, 'NationalCalendars' ) ) {
$this->DATA->Metadata->WiderRegion = ucfirst( strtolower( $this->DATA->Metadata->WiderRegion ) );
$widerRegion = strtoupper( $this->DATA->Metadata->WiderRegion );
if( $this->DATA->Metadata->IsMultilingual === true ) {
$path = "nations/{$widerRegion}";
if( !file_exists( $path ) ) {
mkdir( $path, 0755, true );
}
$translationJSON = new stdClass();
foreach( $this->DATA->LitCal as $CalEvent ) {
$translationJSON->{ $CalEvent->Festivity->tag } = '';
}
if( count( $this->DATA->Metadata->Languages ) > 0 ) {
foreach( $this->DATA->Metadata->Languages as $iso ) {
if( !file_exists( "nations/{$widerRegion}/{$iso}.json" ) ) {
file_put_contents( "nations/{$widerRegion}/{$iso}.json", json_encode( $translationJSON, JSON_PRETTY_PRINT|JSON_UNESCAPED_UNICODE ) );
}
}
}
}
$test = $this->validateDataAgainstSchema( $this->DATA, LitSchema::WIDERREGION );
if( $test === true ) {
$data = json_encode( $this->DATA, JSON_PRETTY_PRINT|JSON_UNESCAPED_UNICODE );
file_put_contents( "nations/{$this->DATA->Metadata->WiderRegion}.json", $data . PHP_EOL );
header( $_SERVER[ "SERVER_PROTOCOL" ]." 201 Created", true, 201 );
die( '{"success":"Wider region calendar created or updated for region \"'. $this->DATA->Metadata->WiderRegion .'\""}' );
} else {
header( $_SERVER[ "SERVER_PROTOCOL" ]." 422 Unprocessable Entity", true, 422 );
die( json_encode( $test ) );
}
}
else if ( property_exists( $this->DATA, 'LitCal' ) && property_exists( $this->DATA, 'Diocese' ) && property_exists( $this->DATA, 'Nation' ) ) {
$this->RESPONSE->Nation = strip_tags( $this->DATA->Nation );
$this->RESPONSE->Diocese = strip_tags( $this->DATA->Diocese );
$CalData = json_decode( $this->DATA->LitCal );
if( json_last_error() !== JSON_ERROR_NONE ) {
header( $_SERVER[ "SERVER_PROTOCOL" ]." 400 Bad request", true, 400 );
die( '{"error":"Malformed data received in <LitCal> parameters"}' );
}
if( property_exists( $this->DATA, 'Overrides' ) ) {
$CalData->Overrides = $this->DATA->Overrides;
}
$this->RESPONSE->Calendar = json_encode( $CalData, JSON_PRETTY_PRINT|JSON_UNESCAPED_UNICODE );
if( property_exists( $this->DATA, 'group' ) ) {
$this->RESPONSE->Group = strip_tags( $this->DATA->group );
}
$path = "nations/{$this->RESPONSE->Nation}";
if( !file_exists( $path ) ){
mkdir( $path, 0755, true );
}
$test = $this->validateDataAgainstSchema( $CalData, LitSchema::DIOCESAN );
if( $test === true ) {
file_put_contents( $path . "/{$this->RESPONSE->Diocese}.json", $this->RESPONSE->Calendar . PHP_EOL );
} else {
header( $_SERVER[ "SERVER_PROTOCOL" ]." 422 Unprocessable Entity", true, 422 );
die( json_encode( $test ) );
}
$this->createOrUpdateIndex( $path );
header( $_SERVER[ "SERVER_PROTOCOL" ]." 201 Created", true, 201 );
die( '{"success":"Diocesan calendar created or updated for diocese \"'. $this->RESPONSE->Diocese .'\""}' );
}
else {
header( $_SERVER[ "SERVER_PROTOCOL" ]." 400 Bad request", true, 400 );
die( '{"error":"Not all required parameters were received (LitCal, Metadata, Settings|NationalCalendars OR LitCal, diocese, nation)"}' );
}
}
private function deleteRegionalCalendar() {
if( !property_exists( $this->DATA, 'LitCal' ) || !property_exists( $this->DATA, 'Diocese' ) || !property_exists( $this->DATA, 'Nation' ) ) {
header( $_SERVER[ "SERVER_PROTOCOL" ]." 400 Bad request", true, 400 );
die( '{"error":"Required parameters were not received"}' );
} else {
$this->RESPONSE->Nation = strip_tags( $this->DATA->Nation );
$this->RESPONSE->Diocese = strip_tags( $this->DATA->Diocese );
$path = "nations/{$this->RESPONSE->Nation}";
if( file_exists( $path . "/{$this->RESPONSE->Diocese}.json" ) ){
unlink($path . "/{$this->RESPONSE->Diocese}.json");
}
$this->createOrUpdateIndex( $path, true );
header( $_SERVER[ "SERVER_PROTOCOL" ]." 200 OK", true, 200 );
die( '{"success":"Diocesan calendar deleted for nation \"'. $this->RESPONSE->Diocese .'\""}' );
}
}
private function loadIndex() {
if( file_exists( "nations/index.json" ) ){
$this->GeneralIndex = json_decode( file_get_contents( "nations/index.json" ) );
}
}
private function createOrUpdateIndex( string $path, bool $delete = false ) {
if( null === $this->GeneralIndex ){
$this->GeneralIndex = new stdClass();
}
$key = strtoupper(preg_replace("/[^a-zA-Z]/","",$this->RESPONSE->Diocese));
if( $delete ) {
if( property_exists( $this->GeneralIndex, $key ) ) {
unset( $this->GeneralIndex->$key );
}
} else {
if( !property_exists( $this->GeneralIndex, $key ) ){
$this->GeneralIndex->$key = new stdClass();
}
$this->GeneralIndex->$key->path = $path . "/{$this->RESPONSE->Diocese}.json";
$this->GeneralIndex->$key->nation = $this->RESPONSE->Nation;
$this->GeneralIndex->$key->diocese = $this->RESPONSE->Diocese;
if(property_exists($this->RESPONSE,'Group')){
$this->GeneralIndex->$key->group = $this->RESPONSE->Group;
}
}
$test = $this->validateDataAgainstSchema( $this->GeneralIndex, LitSchema::INDEX );
if( $test === true ) {
file_put_contents( "nations/index.json", json_encode( $this->GeneralIndex, JSON_PRETTY_PRINT|JSON_UNESCAPED_UNICODE ) . PHP_EOL );
} else {
header( $_SERVER[ "SERVER_PROTOCOL" ]." 422 Unprocessable Entity", true, 422 );
die( json_encode( $test ) );
}
}
private function validateDataAgainstSchema( object $data, string $schemaUrl ) : bool {
$result = new stdClass();
$schema = Schema::import( $schemaUrl );
try {
$validation = $schema->in($data);
return true;
} catch (Exception $e) {
$result->error = LitSchema::ERROR_MESSAGES[ $schemaUrl ] . PHP_EOL . $e->getMessage();
header( $_SERVER[ "SERVER_PROTOCOL" ]." 422 Unprocessable Entity", true, 422 );
die( json_encode( $result ) );
}
}
public function Init() {
$this->APICore->Init();
$this->APICore->setResponseContentTypeHeader();
$this->loadIndex();
$this->handleRequestedMethod();
}
}