Use POST to get H5P updates

HFJ-1610
pull/17/head
Frode Petterson 2016-02-08 11:39:21 +01:00
parent 70baeb55a1
commit c4011e3bbb
1 changed files with 50 additions and 31 deletions

View File

@ -2455,12 +2455,15 @@ class H5PCore {
* is responsible for invoking this, eg using cron * is responsible for invoking this, eg using cron
*/ */
public function fetchLibrariesMetadata($fetchingDisabled = FALSE) { public function fetchLibrariesMetadata($fetchingDisabled = FALSE) {
$platformInfo = $this->h5pF->getPlatformInfo(); // Gather data
$platformInfo['autoFetchingDisabled'] = $fetchingDisabled; $uuid = $this->h5pF->getOption('site_uuid', '');
$platformInfo['uuid'] = $this->h5pF->getOption('site_uuid', ''); $data = array(
$platformInfo['siteType'] = $this->h5pF->getOption('site_type', 'local'); 'api_version' => 2,
$platformInfo['localID'] = hash('crc32', $this->fullPluginPath); 'platform' => $this->h5pF->getPlatformInfo(),
$platformInfo['libraryStats'] = $this->combineArrayValues(array( 'uuid' => $uuid,
'local_id' => hash('crc32', $this->fullPluginPath),
'type' => $this->h5pF->getOption('site_type', 'local'),
'libraries' => $this->combineArrayValues(array(
'patch' => $this->getLibrariesInstalled(), 'patch' => $this->getLibrariesInstalled(),
'content' => $this->h5pF->getLibraryContentCount(), 'content' => $this->h5pF->getLibraryContentCount(),
'loaded' => $this->h5pF->getLibraryStats('library'), 'loaded' => $this->h5pF->getLibraryStats('library'),
@ -2469,28 +2472,44 @@ class H5PCore {
'deleted' => $this->h5pF->getLibraryStats('content', 'deleted'), 'deleted' => $this->h5pF->getLibraryStats('content', 'deleted'),
'resultViews' => $this->h5pF->getLibraryStats('results', 'content'), 'resultViews' => $this->h5pF->getLibraryStats('results', 'content'),
'shortcodeInserts' => $this->h5pF->getLibraryStats('content', 'shortcode insert') 'shortcodeInserts' => $this->h5pF->getLibraryStats('content', 'shortcode insert')
)); ))
);
// Adding random string to GET to be sure nothing is cached // Send request
$random = substr(str_shuffle("0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"), 0, 5); $protocol = (extension_loaded('openssl') ? 'https' : 'http');
$json = $this->h5pF->fetchExternalData('http://h5p.org/libraries-metadata.json?api=1&platform=' . urlencode(json_encode($platformInfo)) . '&x=' . urlencode($random)); $result = $this->h5pF->fetchExternalData("{$protocol}://h5p.org/libraries-metadata.json", $data);
if ($json !== NULL) { if (empty($result)) {
$json = json_decode($json); return;
}
// Process results
$json = json_decode($result);
if (empty($json)) {
return;
}
// Handle libraries metadata
if (isset($json->libraries)) { if (isset($json->libraries)) {
foreach ($json->libraries as $machineName => $libInfo) { foreach ($json->libraries as $machineName => $libInfo) {
$this->h5pF->setLibraryTutorialUrl($machineName, $libInfo->tutorialUrl); $this->h5pF->setLibraryTutorialUrl($machineName, $libInfo->tutorialUrl);
} }
} }
if($platformInfo['uuid'] === '' && isset($json->uuid)) {
// Handle new uuid
if ($uuid === '' && isset($json->uuid)) {
$this->h5pF->setOption('site_uuid', $json->uuid); $this->h5pF->setOption('site_uuid', $json->uuid);
} }
if (isset($json->latest) && !empty($json->latest)) {
// Handle lastest version of H5P
if (!empty($json->latest)) {
$this->h5pF->setOption('update_available', $json->latest->releasedAt); $this->h5pF->setOption('update_available', $json->latest->releasedAt);
$this->h5pF->setOption('update_available_path', $json->latest->path); $this->h5pF->setOption('update_available_path', $json->latest->path);
} }
} }
}
/**
*
*/
public function getGlobalDisable() { public function getGlobalDisable() {
$disable = self::DISABLE_NONE; $disable = self::DISABLE_NONE;