From ac5e063a1cc0375dc628c888864a91743400707f Mon Sep 17 00:00:00 2001 From: Sebastian Schendel Date: Fri, 13 Sep 2024 22:08:54 +0200 Subject: [PATCH] Add hash support --- README.md | 5 +++++ Twack.module.php | 2 +- TwackApiAccess.class.php | 21 +++++++++++++-------- 3 files changed, 19 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index d7b9c80..c5de7ff 100755 --- a/README.md +++ b/README.md @@ -185,6 +185,11 @@ We now have created a basic Twack-component and you now the general concepts how ## Changelog +### Changes in 2.3.3(2024-09-12) + +- Add hash to page requests +- Return 204 (No Content) if matching hash is sent via GET param + ### Changes in 2.3.1(2024-03-04) - Use AppApi getAjaxOf() if available diff --git a/Twack.module.php b/Twack.module.php index 7f8b411..effb4d6 100755 --- a/Twack.module.php +++ b/Twack.module.php @@ -45,7 +45,7 @@ public static function getModuleInfo() { return [ 'title' => 'Twack', 'author' => 'Sebastian Schendel', - 'version' => '2.3.2', + 'version' => '2.3.3', 'summary' => 'Reusable components for your ProcessWire-templates.', 'singular' => true, 'autoload' => true, diff --git a/TwackApiAccess.class.php b/TwackApiAccess.class.php index 21e91bf..d0e1ef6 100644 --- a/TwackApiAccess.class.php +++ b/TwackApiAccess.class.php @@ -1,17 +1,16 @@ get('id=' . $data->id); - return self::pageRequest($page, ''); + return self::pageRequest($page, '', $data); } - public static function dashboardRequest() { + public static function dashboardRequest($data) { $page = wire('pages')->get('/'); - return self::pageRequest($page, ''); + return self::pageRequest($page, '', $data); } public static function pagePathRequest($data) { @@ -26,19 +25,19 @@ public static function pagePathRequest($data) { if ($value !== $path) { continue; } - return self::pageRequest($rootPage, $key); + return self::pageRequest($rootPage, $key, $data); } } $info = wire('pages')->pathFinder()->get($path); if (!empty($info['language']['name'])) { - return self::pageRequest($page, $info['language']['name']); + return self::pageRequest($page, $info['language']['name'], $data); } - return self::pageRequest($page, ''); + return self::pageRequest($page, '', $data); } - protected static function pageRequest(Page $page, $languageFromPath) { + protected static function pageRequest(Page $page, $languageFromPath, $data) { if (!wire('modules')->isInstalled('Twack')) { throw new InternalServererrorException('Twack module not found.'); } @@ -75,6 +74,12 @@ protected static function pageRequest(Page $page, $languageFromPath) { $ajaxOutput = $page->render(); $results = json_decode($ajaxOutput, true); + $results['hash'] = md5($ajaxOutput); + + if (!empty($data->hash) && !empty($results['hash']) && $results['hash'] === $data->hash) { + throw new AppApiException('No new contents', 204, ['errorcode' => 'no_new_contents']); + } + return $results; }