Skip to content
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@
"heimrichhannot/contao-multi-column-editor-bundle": "^2.4",
"heimrichhannot/contao-utils-bundle": "^2.135",
"heimrichhannot/contao-list_widget": "^2.1",
"symfony/stopwatch": "^3.4 || ^4.4"
"symfony/stopwatch": "^3.4 || ^4.4",
"php-http/guzzle6-adapter": "^1.1"
},
"require-dev": {
"contao/test-case": "1.1.*",
Expand All @@ -31,7 +32,6 @@
"phpunit/phpunit": ">=6.0 <6.5",
"phpunit/phpunit-mock-objects": "^4.0|^5.0",
"phpunit/php-token-stream": "^1.4|^2.0|^3.0",
"php-http/guzzle6-adapter": "^1.1",
"php-http/message-factory": "^1.0.2",
"satooshi/php-coveralls": "^2.0",
"symfony/phpunit-bridge": "^3.2"
Expand Down
13 changes: 10 additions & 3 deletions src/Resources/contao/dca/tl_entity_import_source.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@

// Subpalettes
'subpalettes' => [
'retrievalType_http' => 'sourceUrl,httpMethod,httpAuth,fileType',
'retrievalType_http' => 'sourceUrl,httpMethod,dontCheckSSL,httpAuth,fileType',
'retrievalType_contao_file_system' => 'fileSRC,fileType',
'retrievalType_absolute_path' => 'absolutePath',
'fileType_csv' => 'fileContent,csvHeaderRow,csvSkipEmptyLines,csvDelimiter,csvEnclosure,csvEscape,fieldMappingCopier,fieldMappingPresets,fieldMapping',
Expand Down Expand Up @@ -253,6 +253,13 @@
'eval' => ['submitOnChange' => true, 'includeBlankOption' => true, 'tl_class' => 'clr w50', 'rgxp' => 'url'],
'sql' => "varchar(255) NOT NULL default ''",
],
'dontCheckSSL' => [
'label' => &$GLOBALS['TL_LANG']['tl_entity_import_source']['dontCheckSSL'],
'exclude' => true,
'inputType' => 'checkbox',
'eval' => ['submitOnChange' => true, 'tl_class' => 'w50 m12 clr'],
'sql' => "char(1) NOT NULL default ''",
],
'httpMethod' => [
'label' => &$GLOBALS['TL_LANG']['tl_entity_import_source']['httpMethod'],
'exclude' => true,
Expand All @@ -277,15 +284,15 @@
'maxRowCount' => 1,
'sortable' => true,
'fields' => [
'name' => [
'username' => [
'label' => &$GLOBALS['TL_LANG']['tl_entity_import_source']['httpAuth']['username'],
'exclude' => true,
'inputType' => 'text',
'eval' => [
'groupStyle' => 'width: 49%',
],
],
'value' => [
'password' => [
'label' => &$GLOBALS['TL_LANG']['tl_entity_import_source']['httpAuth']['password'],
'exclude' => true,
'inputType' => 'text',
Expand Down
2 changes: 2 additions & 0 deletions src/Resources/contao/languages/de/tl_entity_import_source.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@
$lang['retrievalType'][\HeimrichHannot\EntityImportBundle\DataContainer\EntityImportSourceContainer::RETRIEVAL_TYPE_CONTAO_FILE_SYSTEM] = 'Contao Dateiverwaltung';
$lang['retrievalType'][\HeimrichHannot\EntityImportBundle\DataContainer\EntityImportSourceContainer::RETRIEVAL_TYPE_ABSOLUTE_PATH] = 'Absoluter Pfad';

$lang['dontCheckSSL'][0] = 'SSL-Verifikation deaktivieren';
$lang['dontCheckSSL'][1] = 'Wählen Sie diese Option, damit bei der Verbindung das SSL-Zertifikat nicht überprüft wird. Nutzen sie diese Option nur, wenn Sie wissen was Sie tun!';
$lang['sourceUrl'][0] = 'URL';
$lang['sourceUrl'][1] = 'Geben Sie hier die URL zur Datei ein.';
$lang['absolutePath'][0] = 'Absoluter Dateipfad';
Expand Down
2 changes: 2 additions & 0 deletions src/Resources/contao/languages/en/tl_entity_import_source.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@
$lang['type'][\HeimrichHannot\EntityImportBundle\DataContainer\EntityImportSourceContainer::TYPE_DATABASE] = 'Database';
$lang['type'][\HeimrichHannot\EntityImportBundle\DataContainer\EntityImportSourceContainer::TYPE_FILE] = 'File';

$lang['dontCheckSSL'][0] = 'Don\'t check SSL';
$lang['dontCheckSSL'][1] = 'Activate this option in order to prevent the checking of SSL certificates for this request. Use this with caution!';
$lang['sourceUrl'][0] = 'Url';
$lang['sourceUrl'][1] = 'Enter the URL to the file here.';
$lang['absolutePath'][0] = 'Absolute file path';
Expand Down
11 changes: 9 additions & 2 deletions src/Source/AbstractFileSource.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,10 +77,17 @@ public function getFileContent(bool $cache = false): string

case EntityImportSourceContainer::RETRIEVAL_TYPE_HTTP:
$auth = [];
$options = [];

if (null !== $this->sourceModel->httpAuth) {
$httpAuth = \Contao\StringUtil::deserialize($this->sourceModel->httpAuth, true);
$auth = ['auth' => [$httpAuth['username'], $httpAuth['password']]];
$options = $auth;
}

// Check if SSL verification should be done
if($this->sourceModel->dontCheckSSL) {
$options['verify'] = false;
}

$event = $this->eventDispatcher->dispatch(BeforeAuthenticationEvent::NAME, new BeforeAuthenticationEvent($auth, $this->sourceModel));

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you please replace auth by options and fix the getter and setter; Afterwards please pass $event->getOptions() to storeValueToRemoteCache() and getContentFromUrl(). Then I guess we have it :-) Thanks for your support!

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you please rename the event to ModifySourceHttpClientOptionsEvent since this makes more sense. This way we can modify all options of guzzle and not only auth settings.

Expand All @@ -91,14 +98,14 @@ public function getFileContent(bool $cache = false): string
$content = $this->getValueFromRemoteCache($cacheKey);

if (empty($content)) {
$this->storeValueToRemoteCache($this->sourceModel->sourceUrl, $cacheKey, $this->sourceModel->httpMethod, $event->getAuth());
$this->storeValueToRemoteCache($this->sourceModel->sourceUrl, $cacheKey, $this->sourceModel->httpMethod, $options);
$content = $this->getValueFromRemoteCache($cacheKey);
}

break;
}

$result = $this->getContentFromUrl($this->sourceModel->httpMethod, $this->sourceModel->sourceUrl, $event->getAuth());
$result = $this->getContentFromUrl($this->sourceModel->httpMethod, $this->sourceModel->sourceUrl, $options);
$content = $result['result'];

break;
Expand Down
19 changes: 11 additions & 8 deletions src/Source/AbstractSource.php
Original file line number Diff line number Diff line change
Expand Up @@ -100,17 +100,20 @@ protected function getMappedItemData(?array $element, array $mapping): array
return $result;
}

protected function getContentFromUrl(string $method, string $url, array $auth = []): array
protected function getContentFromUrl(string $method, string $url, array $options = []): array

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please rename $options to $httpClientOptions since we might have a general $options array sometimes. (same below)

{
$client = new Client();

try {
$response = $client->request($method, \Contao\StringUtil::decodeEntities($url), $auth);
$response = $client->request($method, \Contao\StringUtil::decodeEntities($url), $options);
} catch (RequestException $e) {
return [
'statusCode' => $e->getResponse()->getStatusCode(),
'result' => $e->getResponse()->getBody()->getContents(),
];
if($e->hasResponse()){
return [
'statusCode' => $e->getResponse()->getStatusCode(),
'result' => $e->getResponse()->getBody()->getContents(),
];
}
throw $e;
}

return [
Expand All @@ -133,11 +136,11 @@ protected function deleteValueFromRemoteCache(string $cacheKey): string
return $filesystemCache->deleteItem('entity-import-remote.'.$cacheKey);
}

protected function storeValueToRemoteCache(string $url, string $cacheKey, string $method, array $auth = [])
protected function storeValueToRemoteCache(string $url, string $cacheKey, string $method, array $options = [])
{
$filesystemCache = $this->getFilesystemCache();

$response = $this->getContentFromUrl($method, $url, $auth);
$response = $this->getContentFromUrl($method, $url, $options);

if (200 === $response['statusCode']) {
$filesystemCache->set('entity-import-remote.'.$cacheKey, $response['result']);
Expand Down