From f01d002f66cdd22df360d69aa7bf3b6b4ce55d5f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?P=C3=A5l=20J=C3=B8rgensen?= Date: Sat, 1 Nov 2014 17:05:15 +0100 Subject: [PATCH 1/4] Major post review changes --- h5p.classes.php | 102 +++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 84 insertions(+), 18 deletions(-) diff --git a/h5p.classes.php b/h5p.classes.php index dffb390..2df9901 100644 --- a/h5p.classes.php +++ b/h5p.classes.php @@ -11,9 +11,18 @@ interface H5PFrameworkInterface { * An associative array containing: * - name: The name of the plattform, for instance "Wordpress" * - version: The version of the pattform, for instance "4.0" + * - uuid: UUID for site installation */ public function getPlatformInfo(); + /** + * Set the tutorial URL for a library. All versions of the library is set + * + * @param string $machineName + * @param string $tutorialUrl + */ + public function setLibraryTutorialUrl($machineName, $tutorialUrl); + /** * Show the user an error message * @@ -2028,6 +2037,34 @@ class H5PCore { return $obj ? (object) $newArr : $newArr; } + /** + * Generates a UUID v4. Taken from: http://php.net/manual/en/function.uniqid.php + * + * @return string uuid + */ + public static function generateUUID() { + return sprintf('%04x%04x-%04x-%04x-%04x-%04x%04x%04x', + + // 32 bits for "time_low" + mt_rand(0, 0xffff), mt_rand(0, 0xffff), + + // 16 bits for "time_mid" + mt_rand(0, 0xffff), + + // 16 bits for "time_hi_and_version", + // four most significant bits holds version number 4 + mt_rand(0, 0x0fff) | 0x4000, + + // 16 bits, 8 bits for "clk_seq_hi_res", + // 8 bits for "clk_seq_low", + // two most significant bits holds zero and one for variant DCE1.1 + mt_rand(0, 0x3fff) | 0x8000, + + // 48 bits for "node" + mt_rand(0, 0xffff), mt_rand(0, 0xffff), mt_rand(0, 0xffff) + ); + } + /** * Check if currently installed H5P libraries are supported by * the current versjon of core. Which versions of which libraries are supported is @@ -2157,27 +2194,56 @@ class H5PCore { } /** - * Get a list of libraries' metadata from h5p.org. Cache it, and refetch once a week. - * - * @return mixed An object of objects keyed by machineName + * Fetch a list of libraries' metadata from h5p.org. + * Save URL tutorial to database. Each platform implementation + * is responsible for invoking this, eg using cron */ - public function getLibrariesMetadata() { - // Fetch from cache: - $metadata = $this->h5pF->cacheGet('libraries','metadata'); - - // If not available in cache, or older than a week => refetch! - if ($metadata === NULL || $metadata->lastTimeFetched < (time() - self::SECONDS_IN_WEEK)) { - $platformInfo = $this->h5pF->getPlatformInfo(); - $json = file_get_contents('http://h5p.org/libraries-metadata.json?platform=' . json_encode($platformInfo)); - - $metadata = new stdClass(); - $metadata->json = ($json === FALSE ? NULL : json_decode($json)); - $metadata->lastTimeFetched = time(); - - $this->h5pF->cacheSet('libraries','metadata', $metadata); + public function fetchLibrariesMetadata($fetchingDisabled = FALSE) { + $platformInfo = $this->h5pF->getPlatformInfo(); + $platformInfo['autoFetchingDisabled'] = $fetchingDisabled; + $json = H5PExternalDataFetcher::fetchJson('http://h5p.lvh.me/h5p.org/libraries-metadata.json?api=1&platform=' . urlencode(json_encode($platformInfo))); + if ($json !== NULL) { + foreach ($json as $machineName => $libInfo) { + $this->h5pF->setLibraryTutorialUrl($machineName, $libInfo->tutorialUrl); + } } + } +} - return $metadata->json; +/** + * Class responsible for fetching external data (using http) + */ +class H5PExternalDataFetcher { + + public static function fetchJson($url) { + $json = self::fetch($url); + return ($json === NULL) ? NULL : json_decode($json); + } + + public static function fetch($url) { + if (ini_get('allow_url_fopen') == TRUE) { + return self::fetchFopen($url); + } + else if (function_exists('curl_init')) { + return self::fetchCurl($url); + } + else { + // Enable 'allow_url_fopen' or install cURL. + // TODO - this could be reported to the user. + return NULL; + } + } + + private static function fetchFopen($url) { + return file_get_contents($url); + } + + private static function fetchCurl($url) { + $curl = curl_init($url); + curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); + $result = curl_exec($curl); + curl_close($curl); + return $result; } } From f306d24f23dfe6a5df2d49f8fecc7ce669caf82d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?P=C3=A5l=20J=C3=B8rgensen?= Date: Sun, 2 Nov 2014 13:46:09 +0100 Subject: [PATCH 2/4] Moved UUID-generation to backend Added h5pVersion in platforminfo Avoiding json beeing cached both locally and on backend --- h5p.classes.php | 52 ++++++++++++++++--------------------------------- 1 file changed, 17 insertions(+), 35 deletions(-) diff --git a/h5p.classes.php b/h5p.classes.php index 398dbc7..f719f6d 100644 --- a/h5p.classes.php +++ b/h5p.classes.php @@ -11,7 +11,7 @@ interface H5PFrameworkInterface { * An associative array containing: * - name: The name of the plattform, for instance "Wordpress" * - version: The version of the pattform, for instance "4.0" - * - uuid: UUID for site installation + * - h5pVersion: The version of the H5P plugin/module */ public function getPlatformInfo(); @@ -2037,34 +2037,6 @@ class H5PCore { return $obj ? (object) $newArr : $newArr; } - /** - * Generates a UUID v4. Taken from: http://php.net/manual/en/function.uniqid.php - * - * @return string uuid - */ - public static function generateUUID() { - return sprintf('%04x%04x-%04x-%04x-%04x-%04x%04x%04x', - - // 32 bits for "time_low" - mt_rand(0, 0xffff), mt_rand(0, 0xffff), - - // 16 bits for "time_mid" - mt_rand(0, 0xffff), - - // 16 bits for "time_hi_and_version", - // four most significant bits holds version number 4 - mt_rand(0, 0x0fff) | 0x4000, - - // 16 bits, 8 bits for "clk_seq_hi_res", - // 8 bits for "clk_seq_low", - // two most significant bits holds zero and one for variant DCE1.1 - mt_rand(0, 0x3fff) | 0x8000, - - // 48 bits for "node" - mt_rand(0, 0xffff), mt_rand(0, 0xffff), mt_rand(0, 0xffff) - ); - } - /** * Check if currently installed H5P libraries are supported by * the current versjon of core. Which versions of which libraries are supported is @@ -2201,10 +2173,18 @@ class H5PCore { public function fetchLibrariesMetadata($fetchingDisabled = FALSE) { $platformInfo = $this->h5pF->getPlatformInfo(); $platformInfo['autoFetchingDisabled'] = $fetchingDisabled; - $json = H5PExternalDataFetcher::fetchJson('http://h5p.lvh.me/h5p.org/libraries-metadata.json?api=1&platform=' . urlencode(json_encode($platformInfo))); + $platformInfo['uuid'] = $this->h5pF->getOption('h5p_site_uuid', ''); + // Adding random string to GET to be sure nothing is cached + $random = substr(str_shuffle("0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"), 0, 5); + $json = H5PExternalDataFetcher::fetchJson('http://h5p.lvh.me/h5p.org/libraries-metadata.json?api=1&platform=' . urlencode(json_encode($platformInfo)) . '&x=' . urlencode($random)); if ($json !== NULL) { - foreach ($json as $machineName => $libInfo) { - $this->h5pF->setLibraryTutorialUrl($machineName, $libInfo->tutorialUrl); + if (isset($json->libraries)) { + foreach ($json->libraries as $machineName => $libInfo) { + $this->h5pF->setLibraryTutorialUrl($machineName, $libInfo->tutorialUrl); + } + } + if($platformInfo['uuid'] === '' && isset($json->uuid)) { + $this->h5pF->setOption('h5p_site_uuid', $json->uuid); } } } @@ -2228,8 +2208,8 @@ class H5PExternalDataFetcher { return self::fetchCurl($url); } else { - // Enable 'allow_url_fopen' or install cURL. - // TODO - this could be reported to the user. + // We have no transport mechanisme to use for communicating with h5p.org + // This may be reported to the user in future versions. return NULL; } } @@ -2241,6 +2221,8 @@ class H5PExternalDataFetcher { private static function fetchCurl($url) { $curl = curl_init($url); curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); + // To be sure curl does not cache anything + curl_setopt($curl, CURLOPT_FRESH_CONNECT, TRUE); $result = curl_exec($curl); curl_close($curl); return $result; @@ -2364,7 +2346,7 @@ class H5PContentValidator { */ public function validateContentFiles($contentPath, $isLibrary = FALSE) { if ($this->h5pC->disableFileCheck === TRUE) { - return TRUE; + return TRUE; } // Scan content directory for files, recurse into sub directories. From a2c84189b01d14cef4ca2995fc41014861faa9ab Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?P=C3=A5l=20J=C3=B8rgensen?= Date: Sun, 2 Nov 2014 14:29:13 +0100 Subject: [PATCH 3/4] Moving external file fetching out of core --- h5p.classes.php | 50 ++++++++++--------------------------------------- 1 file changed, 10 insertions(+), 40 deletions(-) diff --git a/h5p.classes.php b/h5p.classes.php index f719f6d..1fe6d6c 100644 --- a/h5p.classes.php +++ b/h5p.classes.php @@ -15,6 +15,14 @@ interface H5PFrameworkInterface { */ public function getPlatformInfo(); + /** + * Fetches a file from a remote server using HTTP GET + * + * @param $url + * @return string The content (response body). NULL if something went wrong + */ + public function fetchExternalData($url); + /** * Set the tutorial URL for a library. All versions of the library is set * @@ -2176,8 +2184,9 @@ class H5PCore { $platformInfo['uuid'] = $this->h5pF->getOption('h5p_site_uuid', ''); // Adding random string to GET to be sure nothing is cached $random = substr(str_shuffle("0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"), 0, 5); - $json = H5PExternalDataFetcher::fetchJson('http://h5p.lvh.me/h5p.org/libraries-metadata.json?api=1&platform=' . urlencode(json_encode($platformInfo)) . '&x=' . urlencode($random)); + $json = $this->h5pF->fetchExternalFile('http://h5p.lvh.me/h5p.org/libraries-metadata.json?api=1&platform=' . urlencode(json_encode($platformInfo)) . '&x=' . urlencode($random)); if ($json !== NULL) { + $json = json_decode($json); if (isset($json->libraries)) { foreach ($json->libraries as $machineName => $libInfo) { $this->h5pF->setLibraryTutorialUrl($machineName, $libInfo->tutorialUrl); @@ -2190,45 +2199,6 @@ class H5PCore { } } -/** - * Class responsible for fetching external data (using http) - */ -class H5PExternalDataFetcher { - - public static function fetchJson($url) { - $json = self::fetch($url); - return ($json === NULL) ? NULL : json_decode($json); - } - - public static function fetch($url) { - if (ini_get('allow_url_fopen') == TRUE) { - return self::fetchFopen($url); - } - else if (function_exists('curl_init')) { - return self::fetchCurl($url); - } - else { - // We have no transport mechanisme to use for communicating with h5p.org - // This may be reported to the user in future versions. - return NULL; - } - } - - private static function fetchFopen($url) { - return file_get_contents($url); - } - - private static function fetchCurl($url) { - $curl = curl_init($url); - curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); - // To be sure curl does not cache anything - curl_setopt($curl, CURLOPT_FRESH_CONNECT, TRUE); - $result = curl_exec($curl); - curl_close($curl); - return $result; - } -} - /** * Functions for validating basic types from H5P library semantics. */ From 3d370da1e108ec84c4c73419ce8439bb6514056d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?P=C3=A5l=20J=C3=B8rgensen?= Date: Sun, 2 Nov 2014 16:22:27 +0100 Subject: [PATCH 4/4] Fixed usage of non-existent function --- h5p.classes.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/h5p.classes.php b/h5p.classes.php index 1fe6d6c..bfd76ce 100644 --- a/h5p.classes.php +++ b/h5p.classes.php @@ -2184,7 +2184,7 @@ class H5PCore { $platformInfo['uuid'] = $this->h5pF->getOption('h5p_site_uuid', ''); // Adding random string to GET to be sure nothing is cached $random = substr(str_shuffle("0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"), 0, 5); - $json = $this->h5pF->fetchExternalFile('http://h5p.lvh.me/h5p.org/libraries-metadata.json?api=1&platform=' . urlencode(json_encode($platformInfo)) . '&x=' . urlencode($random)); + $json = $this->h5pF->fetchExternalData('http://h5p.lvh.me/h5p.org/libraries-metadata.json?api=1&platform=' . urlencode(json_encode($platformInfo)) . '&x=' . urlencode($random)); if ($json !== NULL) { $json = json_decode($json); if (isset($json->libraries)) {