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,42 +2455,61 @@ 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,
'patch' => $this->getLibrariesInstalled(), 'local_id' => hash('crc32', $this->fullPluginPath),
'content' => $this->h5pF->getLibraryContentCount(), 'type' => $this->h5pF->getOption('site_type', 'local'),
'loaded' => $this->h5pF->getLibraryStats('library'), 'libraries' => $this->combineArrayValues(array(
'created' => $this->h5pF->getLibraryStats('content', 'create'), 'patch' => $this->getLibrariesInstalled(),
'createdUpload' => $this->h5pF->getLibraryStats('content', 'create upload'), 'content' => $this->h5pF->getLibraryContentCount(),
'deleted' => $this->h5pF->getLibraryStats('content', 'deleted'), 'loaded' => $this->h5pF->getLibraryStats('library'),
'resultViews' => $this->h5pF->getLibraryStats('results', 'content'), 'created' => $this->h5pF->getLibraryStats('content', 'create'),
'shortcodeInserts' => $this->h5pF->getLibraryStats('content', 'shortcode insert') 'createdUpload' => $this->h5pF->getLibraryStats('content', 'create upload'),
)); 'deleted' => $this->h5pF->getLibraryStats('content', 'deleted'),
'resultViews' => $this->h5pF->getLibraryStats('results', 'content'),
'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;
if (isset($json->libraries)) { }
foreach ($json->libraries as $machineName => $libInfo) {
$this->h5pF->setLibraryTutorialUrl($machineName, $libInfo->tutorialUrl); // Process results
} $json = json_decode($result);
} if (empty($json)) {
if($platformInfo['uuid'] === '' && isset($json->uuid)) { return;
$this->h5pF->setOption('site_uuid', $json->uuid); }
}
if (isset($json->latest) && !empty($json->latest)) { // Handle libraries metadata
$this->h5pF->setOption('update_available', $json->latest->releasedAt); if (isset($json->libraries)) {
$this->h5pF->setOption('update_available_path', $json->latest->path); foreach ($json->libraries as $machineName => $libInfo) {
$this->h5pF->setLibraryTutorialUrl($machineName, $libInfo->tutorialUrl);
} }
} }
// Handle new uuid
if ($uuid === '' && isset($json->uuid)) {
$this->h5pF->setOption('site_uuid', $json->uuid);
}
// Handle lastest version of H5P
if (!empty($json->latest)) {
$this->h5pF->setOption('update_available', $json->latest->releasedAt);
$this->h5pF->setOption('update_available_path', $json->latest->path);
}
} }
/**
*
*/
public function getGlobalDisable() { public function getGlobalDisable() {
$disable = self::DISABLE_NONE; $disable = self::DISABLE_NONE;