From b81b8681c1d2307d3ad95c0aefc15fca3cb92aa2 Mon Sep 17 00:00:00 2001 From: John D'Orazio Date: Tue, 10 May 2022 22:09:26 +0200 Subject: [PATCH] unify DiocesanData and NationalData endpoints --- LitCalDiocesanData.php | 207 ------------------ ...RegionalData.php => LitCalRegionalData.php | 195 +++++++++++------ 2 files changed, 132 insertions(+), 270 deletions(-) delete mode 100644 LitCalDiocesanData.php rename LitCalNationalAndRegionalData.php => LitCalRegionalData.php (58%) diff --git a/LitCalDiocesanData.php b/LitCalDiocesanData.php deleted file mode 100644 index 64115987..00000000 --- a/LitCalDiocesanData.php +++ /dev/null @@ -1,207 +0,0 @@ -APICore->setAllowedOrigins( $allowedOrigins ); -$LitCalDiocesanData->APICore->setAllowedReferers( array_map( function($el){ return $el . "/"; }, $allowedOrigins ) ); - -$LitCalDiocesanData->APICore->setAllowedAcceptHeaders( [ AcceptHeader::JSON ] ); -$LitCalDiocesanData->APICore->setAllowedRequestContentTypes( [ RequestContentType::JSON, RequestContentType::FORMDATA ] ); -$LitCalDiocesanData->Init(); - -class LitCalDiocesanData { - - private object $DATA; - private object $RESPONSE; - 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 handleGetPostRequests( array $REQUEST ) { - - $this->APICore->validateAcceptHeader( true ); - if( $this->APICore->getRequestContentType() === 'application/json' ) { - $this->DATA = $this->APICore->retrieveRequestParamsFromJsonBody(); - } else { - $this->DATA = (object)$REQUEST; - } - $this->retrieveDiocesanCalendar(); - } - - 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->writeDiocesanCalendar(); - } elseif( RequestMethod::DELETE === $requestMethod ) { - $this->deleteDiocesanCalendar(); - } - - } 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 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 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; - } - } - - file_put_contents( "nations/index.json", json_encode( $this->GeneralIndex ) . PHP_EOL ); - } - - private function retrieveDiocesanCalendar() { - if( property_exists( $this->DATA, 'key' ) ) { - $key = $this->DATA->key; - $calendarPath = $this->GeneralIndex->$key->path; - if( file_exists( $calendarPath ) ) { - echo file_get_contents( $calendarPath ); - die(); - } - } - } - - private function writeDiocesanCalendar() { - if( !property_exists( $this->DATA, 'calendar' ) || !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 ); - $CalData = json_decode( $this->DATA->calendar ); - if( json_last_error() !== JSON_ERROR_NONE ) { - header( $_SERVER[ "SERVER_PROTOCOL" ]." 400 Bad request", true, 400 ); - die( '{"error":"Malformed data received in parameters"}' ); - } - if( property_exists( $this->DATA, 'overrides' ) ) { - $CalData->Overrides = $this->DATA->overrides; - } - $this->RESPONSE->Calendar = json_encode( $CalData ); - 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 ); - } - - file_put_contents( $path . "/{$this->RESPONSE->Diocese}.json", $this->RESPONSE->Calendar . PHP_EOL ); - - $this->createOrUpdateIndex( $path ); - header( $_SERVER[ "SERVER_PROTOCOL" ]." 201 Created", true, 201 ); - die( '{"success":"Diocesan calendar created or updated for diocese \"'. $this->RESPONSE->Diocese .'\""}' ); - - } - } - - private function deleteDiocesanCalendar() { - if( !property_exists( $this->DATA, 'calendar' ) || !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 diocese \"'. $this->RESPONSE->Diocese .'\""}' ); - - } - } - - public function Init() { - $this->APICore->Init(); - $this->APICore->setResponseContentTypeHeader(); - $this->loadIndex(); - $this->handleRequestedMethod(); - } - -} diff --git a/LitCalNationalAndRegionalData.php b/LitCalRegionalData.php similarity index 58% rename from LitCalNationalAndRegionalData.php rename to LitCalRegionalData.php index e598eb5a..0269a0ba 100644 --- a/LitCalNationalAndRegionalData.php +++ b/LitCalRegionalData.php @@ -23,19 +23,21 @@ $allowedOrigins = array_merge( $allowedOrigins, ALLOWED_ORIGINS ); } -$LitCalNationalData = new LitCalNationalData(); +$LitCalRegionalData = new LitCalRegionalData(); -$LitCalNationalData->APICore->setAllowedOrigins( $allowedOrigins ); -$LitCalNationalData->APICore->setAllowedReferers( array_map( function($el){ return $el . "/"; }, $allowedOrigins ) ); +$LitCalRegionalData->APICore->setAllowedOrigins( $allowedOrigins ); +$LitCalRegionalData->APICore->setAllowedReferers( array_map( function($el){ return $el . "/"; }, $allowedOrigins ) ); -$LitCalNationalData->APICore->setAllowedAcceptHeaders( [ AcceptHeader::JSON ] ); -$LitCalNationalData->APICore->setAllowedRequestContentTypes( [ RequestContentType::JSON, RequestContentType::FORMDATA ] ); -$LitCalNationalData->Init(); +$LitCalRegionalData->APICore->setAllowedAcceptHeaders( [ AcceptHeader::JSON ] ); +$LitCalRegionalData->APICore->setAllowedRequestContentTypes( [ RequestContentType::JSON, RequestContentType::FORMDATA ] ); +$LitCalRegionalData->Init(); -class LitCalNationalData { +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; @@ -45,34 +47,6 @@ public function __construct(){ $this->RESPONSE->requestHeadersReceived = $this->APICore->getJsonEncodedRequestHeaders(); } - 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->retrieveNationalCalendar(); - } - - 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->writeNationalCalendar(); - } elseif( RequestMethod::DELETE === $requestMethod ) { - $this->deleteNationalCalendar(); - } - } 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 handleRequestedMethod() { switch( strtoupper( $_SERVER[ "REQUEST_METHOD" ] ) ) { case RequestMethod::GET: @@ -100,43 +74,79 @@ private function handleRequestedMethod() { } } - private function retrieveNationalCalendar() { - if( property_exists( $this->DATA, 'category' ) ) { - $category = $this->DATA->category; //nationalCalendar or widerRegionCalendar - if( property_exists( $this->DATA, 'key' ) ) { - $key = $this->DATA->key; - if( $category === "widerRegionCalendar" ) { + 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"; - } - else if( $category === "nationalCalendar" ) { + break; + case "nationalCalendar": $calendarDataFile = "nations/{$key}/{$key}.json"; - } - if( file_exists( $calendarDataFile ) ) { - $response = json_decode( file_get_contents( $calendarDataFile ) ); + 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" ) { - $response->isMultilingual = is_dir( "nations/{$uKey}" ); + $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( $response->LitCal as $idx => $el ) { - $response->LitCal[$idx]->Festivity->name = $localeData->{$response->LitCal[$idx]->Festivity->tag}; + foreach( $this->RESPONSE->LitCal as $idx => $el ) { + $this->RESPONSE->LitCal[$idx]->Festivity->name = $localeData->{$response->LitCal[$idx]->Festivity->tag}; } } } - $responseStr = json_encode( $response ); - echo $responseStr; - die(); - } else { - header( $_SERVER[ "SERVER_PROTOCOL" ]." 404 Not Found", true, 404 ); - echo "{\"message\":\"file $calendarDataFile does not exist\"}"; + 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 writeNationalCalendar() { + 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' ) { @@ -176,34 +186,93 @@ private function writeNationalCalendar() { header( $_SERVER[ "SERVER_PROTOCOL" ]." 201 Created", true, 201 ); die( '{"success":"Wider region calendar created or updated for region \"'. $this->DATA->Metadata->WiderRegion .'\""}' ); } + 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 parameters"}' ); + } + if( property_exists( $this->DATA, 'Overrides' ) ) { + $CalData->Overrides = $this->DATA->Overrides; + } + $this->RESPONSE->Calendar = json_encode( $CalData ); + 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 ); + } + + file_put_contents( $path . "/{$this->RESPONSE->Diocese}.json", $this->RESPONSE->Calendar . PHP_EOL ); + + $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)"}' ); + die( '{"error":"Not all required parameters were received (LitCal, Metadata, Settings|NationalCalendars OR LitCal, diocese, nation)"}' ); } } - private function deleteNationalCalendar() { - if( !property_exists( $this->DATA, 'calendar' ) || !property_exists( $this->DATA, 'diocese' ) || !property_exists( $this->DATA, '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 ); + $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 ); + $this->createOrUpdateIndex( $path, true ); header( $_SERVER[ "SERVER_PROTOCOL" ]." 200 OK", true, 200 ); - die( '{"success":"National calendar deleted for nation \"'. $this->RESPONSE->Diocese .'\""}' ); + 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; + } + } + + file_put_contents( "nations/index.json", json_encode( $this->GeneralIndex ) . PHP_EOL ); + } + public function Init() { $this->APICore->Init(); $this->APICore->setResponseContentTypeHeader(); + $this->loadIndex(); $this->handleRequestedMethod(); }