/**
  * HumHub API
  * 
  * @param string $action
  * @param array $params
  * @return array
  */
 public static function request($action, $params = [])
 {
     if (!Yii::$app->params['humhub']['apiEnabled']) {
         return [];
     }
     $url = Yii::$app->params['humhub']['apiUrl'] . '/' . $action;
     $params['version'] = urlencode(Yii::$app->version);
     $params['installId'] = Setting::Get('installationId', 'admin');
     $url .= '?';
     foreach ($params as $name => $value) {
         $url .= urlencode($name) . '=' . urlencode($value) . "&";
     }
     try {
         $http = new \Zend\Http\Client($url, array('adapter' => '\\Zend\\Http\\Client\\Adapter\\Curl', 'curloptions' => CURLHelper::getOptions(), 'timeout' => 30));
         $response = $http->send();
         $json = $response->getBody();
     } catch (\Zend\Http\Client\Adapter\Exception\RuntimeException $ex) {
         Yii::error('Could not connect to HumHub API! ' . $ex->getMessage());
         return [];
     } catch (Exception $ex) {
         Yii::error('Could not get HumHub API response! ' . $ex->getMessage());
         return [];
     }
     try {
         return Json::decode($json);
     } catch (\yii\base\InvalidParamException $ex) {
         Yii::error('Could not parse HumHub API response! ' . $ex->getMessage());
         return [];
     }
 }
 /**
  * Installs latest compatible module version
  *
  * @param type $moduleId
  */
 public function install($moduleId)
 {
     $modulePath = Yii::getAlias(Yii::$app->params['moduleMarketplacePath']);
     if (!is_writable($modulePath)) {
         throw new HttpException(500, Yii::t('AdminModule.libs_OnlineModuleManager', 'Module directory %modulePath% is not writeable!', array('%modulePath%' => $modulePath)));
     }
     $moduleInfo = $this->getModuleInfo($moduleId);
     if (!isset($moduleInfo['latestCompatibleVersion'])) {
         throw new Exception(Yii::t('AdminModule.libs_OnlineModuleManager', "No compatible module version found!"));
     }
     $moduleDir = $modulePath . DIRECTORY_SEPARATOR . $moduleId;
     if (is_dir($moduleDir)) {
         $files = new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($moduleDir, \RecursiveDirectoryIterator::SKIP_DOTS), \RecursiveIteratorIterator::CHILD_FIRST);
         foreach ($files as $fileinfo) {
             $todo = $fileinfo->isDir() ? 'rmdir' : 'unlink';
             $todo($fileinfo->getRealPath());
         }
         rmdir($moduleDir);
         #throw new HttpException(500, Yii::t('AdminModule.libs_OnlineModuleManager', 'Module directory for module %moduleId% already exists!', array('%moduleId%' => $moduleId)));
     }
     // Check Module Folder exists
     $moduleDownloadFolder = Yii::getAlias("@runtime/module_downloads");
     if (!is_dir($moduleDownloadFolder)) {
         if (!@mkdir($moduleDownloadFolder)) {
             throw new Exception("Could not create module download folder!");
         }
     }
     $version = $moduleInfo['latestCompatibleVersion'];
     // Download
     $downloadUrl = $version['downloadUrl'];
     $downloadTargetFileName = $moduleDownloadFolder . DIRECTORY_SEPARATOR . basename($downloadUrl);
     try {
         $http = new \Zend\Http\Client($downloadUrl, array('adapter' => '\\Zend\\Http\\Client\\Adapter\\Curl', 'curloptions' => CURLHelper::getOptions(), 'timeout' => 30));
         $response = $http->send();
         file_put_contents($downloadTargetFileName, $response->getBody());
     } catch (Exception $ex) {
         throw new HttpException('500', Yii::t('AdminModule.libs_OnlineModuleManager', 'Module download failed! (%error%)', array('%error%' => $ex->getMessage())));
     }
     // Extract Package
     if (file_exists($downloadTargetFileName)) {
         $zip = new ZipArchive();
         $res = $zip->open($downloadTargetFileName);
         if ($res === TRUE) {
             $zip->extractTo($modulePath);
             $zip->close();
         } else {
             throw new HttpException('500', Yii::t('AdminModule.libs_OnlineModuleManager', 'Could not extract module!'));
         }
     } else {
         throw new HttpException('500', Yii::t('AdminModule.libs_OnlineModuleManager', 'Download of module failed!'));
     }
     Yii::$app->moduleManager->flushCache();
     Yii::$app->moduleManager->register($modulePath . DIRECTORY_SEPARATOR . $moduleId);
 }
 public function getCurlOptions()
 {
     // Compatiblity for older versions
     if (!class_exists('humhub\\libs\\CURLHelper')) {
         $options = array(CURLOPT_SSL_VERIFYPEER => Yii::$app->getModule('admin')->marketplaceApiValidateSsl ? true : false, CURLOPT_SSL_VERIFYHOST => Yii::$app->getModule('admin')->marketplaceApiValidateSsl ? 2 : 0, CURLOPT_REDIR_PROTOCOLS => CURLPROTO_HTTP | CURLPROTO_HTTPS, CURLOPT_PROTOCOLS => CURLPROTO_HTTP | CURLPROTO_HTTPS, CURLOPT_CAINFO => Yii::getAlias('@humhub/config/cacert.pem'));
         if (Setting::Get('enabled', 'proxy')) {
             $options[CURLOPT_PROXY] = Setting::Get('server', 'proxy');
             $options[CURLOPT_PROXYPORT] = Setting::Get('port', 'proxy');
             if (defined('CURLOPT_PROXYUSERNAME')) {
                 $options[CURLOPT_PROXYUSERNAME] = Setting::Get('user', 'proxy');
             }
             if (defined('CURLOPT_PROXYPASSWORD')) {
                 $options[CURLOPT_PROXYPASSWORD] = Setting::Get('pass', 'proxy');
             }
             if (defined('CURLOPT_NOPROXY')) {
                 $options[CURLOPT_NOPROXY] = Setting::Get('noproxy', 'proxy');
             }
         }
         return $options;
     }
     return \humhub\libs\CURLHelper::getOptions();
 }