From 93e9d6e0ec7db72624c5eeb69e1b46fda4afaca0 Mon Sep 17 00:00:00 2001 From: thomasmars Date: Thu, 16 Mar 2017 09:51:46 +0100 Subject: [PATCH 1/3] Register site with new endpoint if no site is already registered HFP-489 --- h5p.classes.php | 32 +++++++++++++++++++++++++++++++- 1 file changed, 31 insertions(+), 1 deletion(-) diff --git a/h5p.classes.php b/h5p.classes.php index 628d30a..589d456 100644 --- a/h5p.classes.php +++ b/h5p.classes.php @@ -2426,6 +2426,8 @@ class H5PCore { * implementation is responsible for invoking this, eg using cron * * @param bool $fetchingDisabled + * + * @return bool|object Returns endpoint data if found, otherwise FALSE */ public function fetchLibrariesMetadata($fetchingDisabled = FALSE) { // Gather data @@ -2453,6 +2455,26 @@ class H5PCore { ))) ); + // Register site if it is not registered + if (empty($uuid)) { + $protocol = (extension_loaded('openssl') ? 'https' : 'http'); + $endpoint = 'api.h5p.org/v1/sites'; + $registration = $this->h5pF->fetchExternalData("{$protocol}://{$endpoint}", $data); + + // Failed retrieving uuid + if (!$registration) { + return H5PCore::ajaxError( + t('Site could not be registered with the hub. Please contact your site administrator.'), + 'SITE_REGISTRATION_FAILED' + ); + } + + // Successfully retrieved new uuid + $json = json_decode($registration); + $data['uuid'] = $json->uuid; + $this->h5pF->setOption('site_uuid', $json->uuid); + } + $result = $this->updateContentTypeCache($data); // No data received @@ -2783,8 +2805,16 @@ class H5PCore { // Set uuid if (!$postData) { + $site_uuid = $this->h5pF->getOption('site_uuid', ''); + + // Register site with site uuid if we don't already have it + // and try to update content type cache again when this is done + if (empty($site_uuid)) { + return $this->fetchLibrariesMetadata(); + } + $postData = array( - 'uuid' => $this->h5pF->getOption('site_uuid', '') + 'uuid' => $site_uuid ); } From 675c87df6876ee17230eccac4ed0ecf82734b35e Mon Sep 17 00:00:00 2001 From: thomasmars Date: Thu, 16 Mar 2017 10:01:41 +0100 Subject: [PATCH 2/3] Added more detailed messages when site is or fails to register HFP-489 --- h5p.classes.php | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/h5p.classes.php b/h5p.classes.php index 589d456..5c6c394 100644 --- a/h5p.classes.php +++ b/h5p.classes.php @@ -2463,16 +2463,27 @@ class H5PCore { // Failed retrieving uuid if (!$registration) { + $errorMessage = $this->h5pF->t('Site could not be registered with the hub. Please contact your site administrator.'); return H5PCore::ajaxError( - t('Site could not be registered with the hub. Please contact your site administrator.'), + $errorMessage, 'SITE_REGISTRATION_FAILED' ); + $this->h5pF->setErrorMessage($errorMessage); + $this->h5pF->setErrorMessage( + $this->h5pF->t('The H5P Hub has been disabled until this problem can be resolved. You may still upload libraries through the "H5P Libraries" page.') + ); } // Successfully retrieved new uuid $json = json_decode($registration); $data['uuid'] = $json->uuid; $this->h5pF->setOption('site_uuid', $json->uuid); + $this->h5pF->setInfoMessage( + $this->h5pF->t('Your site was successfully registered with the H5P Hub.') + ); + $this->h5pF->setInfoMessage( + $this->h5pF->t('You have been provided a unique key that identifies you with the Hub when receiving new updates. The key is available for viewing in the "H5P Settings" page.') + ); } $result = $this->updateContentTypeCache($data); From fd93b30a388a4146cc52a15332342fa18c400ece Mon Sep 17 00:00:00 2001 From: thomasmars Date: Thu, 16 Mar 2017 10:09:52 +0100 Subject: [PATCH 3/3] Fixed return statement of fetch libraries metadata HFP-489 --- h5p.classes.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/h5p.classes.php b/h5p.classes.php index 5c6c394..3889c72 100644 --- a/h5p.classes.php +++ b/h5p.classes.php @@ -2464,7 +2464,7 @@ class H5PCore { // Failed retrieving uuid if (!$registration) { $errorMessage = $this->h5pF->t('Site could not be registered with the hub. Please contact your site administrator.'); - return H5PCore::ajaxError( + H5PCore::ajaxError( $errorMessage, 'SITE_REGISTRATION_FAILED' ); @@ -2472,6 +2472,7 @@ class H5PCore { $this->h5pF->setErrorMessage( $this->h5pF->t('The H5P Hub has been disabled until this problem can be resolved. You may still upload libraries through the "H5P Libraries" page.') ); + return FALSE; } // Successfully retrieved new uuid