backupDownloadKey(); } return true; } /** * Postflight hook runs after any installation/update/uninstallation action. */ public function postflight($type, $parent): void { if ($type === 'install') { $this->installSuccessful(); $this->warnMissingLicenseKey(); } if ($type === 'update') { $this->restoreDownloadKey(); $this->installSuccessful(); } } public function install($parent): void {} public function update($parent): void {} public function uninstall($parent): void {} /** * Cache the existing download key from the update sites table before update runs. */ private function backupDownloadKey(): void { try { $db = Factory::getDbo(); $query = $db->getQuery(true) ->select($db->quoteName('us.extra_query')) ->from($db->quoteName('#__update_sites', 'us')) ->join( 'INNER', $db->quoteName('#__update_sites_extensions', 'use') . ' ON ' . $db->quoteName('use.update_site_id') . ' = ' . $db->quoteName('us.update_site_id') ) ->join( 'INNER', $db->quoteName('#__extensions', 'e') . ' ON ' . $db->quoteName('e.extension_id') . ' = ' . $db->quoteName('use.extension_id') ) ->where($db->quoteName('e.element') . ' = ' . $db->quote('{PACKAGENAME}')) ->where($db->quoteName('e.type') . ' = ' . $db->quote('module')) ->setLimit(1); $db->setQuery($query); $extraQuery = (string) $db->loadResult(); if (!empty($extraQuery)) { parse_str($extraQuery, $output); $this->savedDownloadKey = $output['dlid'] ?? $extraQuery; } } catch (\Exception $e) { Log::add('{REPONAME}: Could not backup download key: ' . $e->getMessage(), Log::WARNING, 'jerror'); } } /** * Restore the download key to the (possibly new) update site record. */ private function restoreDownloadKey(): void { try { $db = Factory::getDbo(); $query = $db->getQuery(true) ->select($db->quoteName('us.update_site_id')) ->from($db->quoteName('#__update_sites', 'us')) ->join( 'INNER', $db->quoteName('#__update_sites_extensions', 'use') . ' ON ' . $db->quoteName('use.update_site_id') . ' = ' . $db->quoteName('us.update_site_id') ) ->join( 'INNER', $db->quoteName('#__extensions', 'e') . ' ON ' . $db->quoteName('e.extension_id') . ' = ' . $db->quoteName('use.extension_id') ) ->where($db->quoteName('e.element') . ' = ' . $db->quote('{PACKAGENAME}')) ->where($db->quoteName('e.type') . ' = ' . $db->quote('module')) ->setLimit(1); $db->setQuery($query); $updateSiteId = (int) $db->loadResult(); if ($updateSiteId > 0 && !empty($this->savedDownloadKey)) { $query = $db->getQuery(true) ->update($db->quoteName('#__update_sites')) ->set($db->quoteName('extra_query') . ' = ' . $db->quote('dlid=' . $this->savedDownloadKey)) ->where($db->quoteName('update_site_id') . ' = ' . $updateSiteId); $db->setQuery($query); $db->execute(); } } catch (\Exception $e) { Log::add('{REPONAME}: Could not restore download key: ' . $e->getMessage(), Log::WARNING, 'jerror'); Factory::getApplication()->enqueueMessage( '
Your download/license key could not be preserved during the update.
' . 'Please re-enter it in the Update Sites manager to continue receiving updates.
', 'warning' ); } } /** * Show post-install license key prompt */ private function warnMissingLicenseKey(): void { try { Factory::getApplication()->enqueueMessage( 'A download/license key (DLID) is required to receive updates.
' . 'Enter your key in the Update Sites manager ' . 'or contact Moko Consulting Support to obtain one.
', 'warning' ); } catch (\Exception $e) {} } /** * Show install successful prompt */ private function installSuccessful(): void { try { Factory::getApplication()->enqueueMessage( '