diff --git a/protected/humhub/libs/BaseSettingsManager.php b/protected/humhub/libs/BaseSettingsManager.php index 1abead8574..bfbea8b342 100644 --- a/protected/humhub/libs/BaseSettingsManager.php +++ b/protected/humhub/libs/BaseSettingsManager.php @@ -12,6 +12,7 @@ use Yii; use yii\base\Component; use yii\base\Exception; use yii\db\conditions\LikeCondition; +use yii\db\StaleObjectException; use yii\helpers\Json; /** @@ -140,14 +141,18 @@ abstract class BaseSettingsManager extends Component * Deletes setting * * @param string $name - * @throws \Throwable - * @throws \yii\db\StaleObjectException */ public function delete($name) { $record = $this->find()->andWhere(['name' => $name])->one(); if ($record !== null) { - $record->delete(); + try { + $record->delete(); + } catch (StaleObjectException $e) { + Yii::error('Could not delete setting "' . $name . '". Error: ' . $e->getMessage(), 'base'); + } catch (\Throwable $e) { + Yii::error('Could not delete setting "' . $name . '". Error: ' . $e->getMessage(), 'base'); + } } if (isset($this->_loaded[$name])) { @@ -246,8 +251,8 @@ abstract class BaseSettingsManager extends Component /** * Checks if settings table exists or application is not installed yet * - * @since 1.3 * @return bool + * @since 1.3 */ public static function isDatabaseInstalled() { diff --git a/protected/humhub/modules/marketplace/Module.php b/protected/humhub/modules/marketplace/Module.php index 009c020bf2..e62ba259f3 100644 --- a/protected/humhub/modules/marketplace/Module.php +++ b/protected/humhub/modules/marketplace/Module.php @@ -10,6 +10,7 @@ namespace humhub\modules\marketplace; use humhub\components\Module as BaseModule; use humhub\models\Setting; +use humhub\modules\marketplace\components\LicenceManager; use humhub\modules\marketplace\models\Licence; use humhub\modules\marketplace\components\OnlineModuleManager; use Yii; @@ -84,32 +85,12 @@ class Module extends BaseModule /** + * Returns the currently active licence object + * * @return Licence */ public function getLicence() { - Licence::fetch(); - - $l = new Licence(); - - $l->licenceKey = $this->settings->get('licenceKey'); - $l->licencedTo = $this->settings->get('licencedTo'); - - if (!empty($l->licencedTo)) { - $l->maxUsers = (int)$this->settings->get('maxUsers'); - $l->type = Licence::LICENCE_TYPE_PRO; - } else { - $l->type = Licence::LICENCE_TYPE_CE; - - if (Yii::$app->hasModule('enterprise')) { - /** @var \humhub\modules\enterprise\Module $enterprise */ - $enterprise = Yii::$app->getModule('enterprise'); - if ($enterprise->settings->get('licence') !== null && $enterprise->settings->get('licence_valid') == 1) { - $l->type = Licence::LICENCE_TYPE_EE; - } - } - } - - return $l; + return LicenceManager::get(); } } diff --git a/protected/humhub/modules/marketplace/components/LicenceManager.php b/protected/humhub/modules/marketplace/components/LicenceManager.php new file mode 100644 index 0000000000..0c12e14764 --- /dev/null +++ b/protected/humhub/modules/marketplace/components/LicenceManager.php @@ -0,0 +1,179 @@ +settings; + + $licence = new Licence(['type' => Licence::LICENCE_TYPE_CE]); + + $lastFetch = (int)$settings->get(static::SETTING_KEY_PE_LAST_FETCH); + if (!empty($settings->get(static::SETTING_KEY_PE_LICENCE_KEY))) { + + // Update + if ($lastFetch + static::PE_FETCH_INTERVAL < time()) { + if (!static::fetch() && $lastFetch + static::PE_FETCH_TOLERANCE < time()) { + $lastFetchDateTime = 'empty'; + try { + $lastFetchDateTime = Yii::$app->formatter->asDatetime($lastFetch, 'full'); + } catch (InvalidConfigException $e) { + Yii::error($e->getMessage(), 'marketplace'); + } + Yii::error('Could not fetch PE licence since: ' . $lastFetchDateTime, 'marketplace'); + return $licence; + } + } + + if (!empty($settings->get(static::SETTING_KEY_PE_LICENCED_TO)) && !empty($settings->get(static::SETTING_KEY_PE_MAX_USERS))) { + $licence->type = Licence::LICENCE_TYPE_PRO; + $licence->maxUsers = $settings->get(static::SETTING_KEY_PE_MAX_USERS); + $licence->licencedTo = $settings->get(static::SETTING_KEY_PE_LICENCED_TO); + $licence->licenceKey = $settings->get(static::SETTING_KEY_PE_LICENCE_KEY); + return $licence; + } + } + + if (isset(Yii::$app->params['hosting'])) { + // In our demo hosting, we allow pro licences without registration + $licence->type = Licence::LICENCE_TYPE_PRO; + } elseif (Yii::$app->hasModule('enterprise')) { + /** @var \humhub\modules\enterprise\Module $enterprise */ + $enterprise = Yii::$app->getModule('enterprise'); + if ($enterprise->settings->get('licence') !== null && $enterprise->settings->get('licence_valid') == 1) { + $licence->type = Licence::LICENCE_TYPE_EE; + } + } + + return $licence; + + } + + /** + * Fetches the licence from the HumHub API + * + * @return bool The retrieval of the license worked, whether it is valid or not. + */ + public static function fetch() + { + $result = static::request('v1/pro/get'); + + if (empty($result) || !is_array($result) || !isset($result['status'])) { + // Connection failure + return false; + } + + if ($result['status'] === 'ok') { + static::getModule()->settings->set(static::SETTING_KEY_PE_LICENCE_KEY, $result['licence']['licenceKey']); + static::getModule()->settings->set(static::SETTING_KEY_PE_LICENCED_TO, $result['licence']['licencedTo']); + static::getModule()->settings->set(static::SETTING_KEY_PE_MAX_USERS, $result['licence']['maxUsers']); + static::getModule()->settings->set(static::SETTING_KEY_PE_LAST_FETCH, time()); + + return true; + } elseif ($result['status'] === 'not-found') { + try { + if (static::remove()) { + return true; + } + } catch (\Throwable $e) { + Yii::error('Could not fetch/remove licence: ' . $e->getMessage()); + } + } + + return false; + } + + + /** + * Removes the licence from this installation and the HumHub Marketplace + * + * @return boolean + */ + public static function remove() + { + $licenceKey = static::getModule()->settings->get('licenceKey'); + if (!empty($licenceKey)) { + $result = static::request('v1/pro/unregister', ['licenceKey' => $licenceKey]); + } + + static::getModule()->settings->delete(static::SETTING_KEY_PE_LICENCE_KEY); + static::getModule()->settings->delete(static::SETTING_KEY_PE_LICENCED_TO); + static::getModule()->settings->delete(static::SETTING_KEY_PE_MAX_USERS); + static::getModule()->settings->delete(static::SETTING_KEY_PE_LAST_FETCH); + + return true; + } + + /** + * Request HumHub API backend + * + * @param $url + * @param array $params + * @return array + */ + public static function request($url, $params = []) + { + return HumHubAPI::request($url, array_merge($params, static::getStats())); + } + + + /** + * @return array some basic stats + */ + private static function getStats() + { + return [ + 'tua' => User::find()->andWhere(['status' => User::STATUS_ENABLED])->count(), + 'tu' => User::find()->count(), + 'ts' => Space::find()->count(), + ]; + } + + /** + * @return Module the marketplace module + */ + private static function getModule() + { + return Yii::$app->getModule('marketplace'); + } + + +} diff --git a/protected/humhub/modules/marketplace/controllers/LicenceController.php b/protected/humhub/modules/marketplace/controllers/LicenceController.php index 368091cb4d..29b72b4315 100644 --- a/protected/humhub/modules/marketplace/controllers/LicenceController.php +++ b/protected/humhub/modules/marketplace/controllers/LicenceController.php @@ -8,7 +8,7 @@ namespace humhub\modules\marketplace\controllers; use humhub\modules\admin\components\Controller; -use humhub\modules\marketplace\models\Licence; +use humhub\modules\marketplace\components\LicenceManager; use humhub\modules\marketplace\Module; use Yii; @@ -26,6 +26,7 @@ class LicenceController extends Controller $model = $this->module->getLicence(); if ($model->load(Yii::$app->request->post()) && $model->register()) { + LicenceManager::fetch(); return $this->redirect(['index']); } @@ -35,8 +36,7 @@ class LicenceController extends Controller public function actionRemove() { - Licence::remove(); - + LicenceManager::remove(); return $this->redirect(['/marketplace/licence']); } diff --git a/protected/humhub/modules/marketplace/models/Licence.php b/protected/humhub/modules/marketplace/models/Licence.php index a9e1f032c4..fad306e267 100644 --- a/protected/humhub/modules/marketplace/models/Licence.php +++ b/protected/humhub/modules/marketplace/models/Licence.php @@ -1,20 +1,15 @@ $this->licenceKey], $this->getStats()); - $result = HumHubAPI::request('v1/pro/register', $params); + $result = LicenceManager::request('v1/pro/register', ['licenceKey' => $this->licenceKey]); if (empty($result) || !is_array($result) || !isset($result['status'])) { $this->addError('licenceKey', Yii::t('MarketplaceModule.base', 'Could not connect to licence server!')); return false; } - if ($result['status'] === 'ok' && static::fetch()) { + if ($result['status'] === 'ok') { return true; } + LicenceManager::remove(); $this->addError('licenceKey', Yii::t('MarketplaceModule.base', 'Could not update licence. Error: ') . $result['message']); return false; } - - /** - * Removes the licence from this installation - * - * @throws StaleObjectException - * @throws \Throwable - */ - public static function remove() - { - $licenceKey = static::getModule()->settings->get('licenceKey'); - - if (!empty($licenceKey)) { - $params = array_merge(['licenceKey' => $licenceKey], static::getStats()); - $result = HumHubAPI::request('v1/pro/unregister', $params); - } - - static::getModule()->settings->delete('licenceKey'); - static::getModule()->settings->delete('licencedTo'); - static::getModule()->settings->delete('maxUsers'); - static::getModule()->settings->delete('lastSave'); - } - - /** - * @return Module - */ - private static function getModule() - { - return Yii::$app->getModule('marketplace'); - } - - - /** - * @return array some basic stats - */ - private static function getStats() - { - return [ - 'tua' => User::find()->andWhere(['status' => User::STATUS_ENABLED])->count(), - 'tu' => User::find()->count(), - 'ts' => Space::find()->count(), - ]; - } - - /** - * Fetches the licence from the server - * - * @return bool - */ - public static function fetch() - { - $result = HumHubAPI::request('v1/pro/get', static::getStats()); - - if (empty($result) || !is_array($result) || !isset($result['status'])) { - return false; - } - - if ($result['status'] === 'ok') { - static::getModule()->settings->set('licenceKey', $result['licence']['licenceKey']); - static::getModule()->settings->set('licencedTo', $result['licence']['licencedTo']); - static::getModule()->settings->set('maxUsers', $result['licence']['maxUsers']); - static::getModule()->settings->set('lastFetch', time()); - - return true; - } elseif ($result['status'] === 'not-found') { - try { - Licence::remove(); - } catch (StaleObjectException $e) { - } catch (\Throwable $e) { - } - } - - return false; - } - } diff --git a/protected/humhub/modules/marketplace/widgets/AboutVersion.php b/protected/humhub/modules/marketplace/widgets/AboutVersion.php index 04e49d0c99..8f9f7fc710 100644 --- a/protected/humhub/modules/marketplace/widgets/AboutVersion.php +++ b/protected/humhub/modules/marketplace/widgets/AboutVersion.php @@ -26,7 +26,11 @@ class AboutVersion extends Widget $licence = $module->getLicence(); if ($licence->type === Licence::LICENCE_TYPE_PRO) { - return $this->render('about_version_pro', ['licence' => $licence]); + if (isset(Yii::$app->params['hosting'])) { + return $this->render('about_version_pro_cloud', ['licence' => $licence]); + } else { + return $this->render('about_version_pro', ['licence' => $licence]); + } } elseif ($licence->type === Licence::LICENCE_TYPE_EE) { return $this->render('about_version_ee'); } else { diff --git a/protected/humhub/modules/marketplace/widgets/views/about_version_pro_cloud.php b/protected/humhub/modules/marketplace/widgets/views/about_version_pro_cloud.php new file mode 100644 index 0000000000..1d5d0f618d --- /dev/null +++ b/protected/humhub/modules/marketplace/widgets/views/about_version_pro_cloud.php @@ -0,0 +1,26 @@ + +
+
+ +
+ HumHub  Professional Edition - SaaS
+ version ?>
+ licencedTo)): ?> + licencedTo; ?> +
+ + maxUsers)): ?> + maxUsers ?> + +
+ +