Skip to content

Commit

Permalink
Merge pull request #55 from TheDMSGroup/ENG-49
Browse files Browse the repository at this point in the history
[Eng-49]
  • Loading branch information
heathdutton authored Jul 3, 2018
2 parents 2570f7d + ac7020f commit 8655bde
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 37 deletions.
68 changes: 43 additions & 25 deletions Command/UpdateCorrectAddressDataCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@

namespace MauticPlugin\MauticEnhancerBundle\Command;

use League\Flysystem\Filesystem;
use League\Flysystem\Sftp\SftpAdapter;
use Mautic\CoreBundle\Command\ModeratedCommand;
use MauticPlugin\MauticEnhancerBundle\Helper\EnhancerHelper;
use MauticPlugin\MauticEnhancerBundle\Integration\CorrectAddressIntegration as CAI;
Expand Down Expand Up @@ -43,34 +41,51 @@ protected function execute(InputInterface $input, OutputInterface $output)
$settings = $correctAddress->getIntegrationSettings()->getFeatureSettings();
$keys = $correctAddress->getKeys();

$sftpAdapter = new SftpAdapter([
'host' => $settings[CAI::CA_REMOTE_HOST],
'port' => $settings[CAI::CA_REMOTE_PORT],
'root' => $settings[CAI::CA_REMOTE_PATH],
'username' => $keys[CAI::CA_REMOTE_USER],
'password' => $keys[CAI::CA_REMOTE_PSWD],
'hostFingerprint' => $keys[CAI::CA_REMOTE_FNGR],
]);
$client = new Filesystem($sftpAdapter);
$output->writeln('<info>Created SFTP client.</info>');

//copy the remote archive locally
$tempfile = tempnam(sys_get_temp_dir(), 'ca_');
$client->copy($settings[CAI::CA_REMOTE_FILE], $tempfile);
$output->writeln('<info>Copied data archive to '.$tempfile.' on local filesystem.</info>');
if (function_exists('ssh2_connect')) {
$sconn = call_user_func('ssh2_connect', $settings[CAI::CA_REMOTE_HOST]);
call_user_func('ssh2_auth_password', $sconn, $keys[CAI::CA_REMOTE_USER], $keys[CAI::CA_REMOTE_PSWD]);
$sftp = call_user_func('ssh2_sftp', $sconn);
} else {
throw new \Exception(
'Required ssh2 extension is not installed',
-1
);
}
$output->writeln('<info>SFTP connection established, downloading data file</info>');

$source = 'ssh2.sftp://'.intval($sftp).$settings[CAI::CA_REMOTE_PATH].'/'.$settings[CAI::CA_REMOTE_FILE];
$dest = sys_get_temp_dir().'/ca_'.\date('Y-m-d').'.zip';
$rfp = fopen($source, 'r');
$wfp = fopen($dest, 'w');

$reads = 0;
do {
if (!fwrite($wfp, fread($rfp, 8388608))) {
break;
}
++$reads;
if (0 === ($reads % 100)) {
if (0 === ($reads % 10000)) {
$output->writeln('.');
} else {
$output->write('.');
}
}
} while (true);
$output->writeln('<info>Copied data archive to '.$dest.' on local filesystem.</info>');

//extract the new files
$buffer = '/tmp'.$settings[CAI::CA_CORRECTA_DATA];
$buffer = '/tmp/data/eq_correct_address';
$extractor = new ZipArchive();
$extractor->open($tempfile, ZipArchive::CHECKCONS);
$extractor->open($dest, ZipArchive::CHECKCONS);
$extractor->extractTo($buffer);
$extractor->close();
unlink($tempfile);
unlink($dest);
$output->writeln('<info>Archive extracted to '.$buffer.'.</info>');

//remove the old files
$this->cleanDir($settings[CAI::CA_CORRECTA_DATA]);
$output->writeln('<info>'.$settings[CAI::CA_CORRECTA_DATA].' removed.</info>');
$output->writeln('<info>'.$settings[CAI::CA_CORRECTA_DATA].' cleaned.</info>');

rename($buffer, $settings[CAI::CA_CORRECTA_DATA]);
$output->writeln('<info>Expirian data update complete.</info>');
Expand All @@ -94,16 +109,19 @@ protected function cleanDir($dirName)
{
if (file_exists($dirName)) {
if (is_dir($dirName)) {
$root = new \RecursiveDirectoryIterator($dirName, \RecursiveDirectoryIterator::SKIP_DOTS);
$ls = new \RecursiveIteratorIterator($root, \RecursiveIteratorIterator::CHILD_FIRST);
$rm_path = new \RecursiveDirectoryIterator($dirName, \RecursiveDirectoryIterator::SKIP_DOTS);
$rm_ls = new \RecursiveIteratorIterator($rm_path, \RecursiveIteratorIterator::CHILD_FIRST);

foreach ($ls as $file) {
$file->isDir() ? rmdir($file->getRealPath()) : unlink($file->getRealPath());
foreach ($rm_ls as $rm_file) {
$rm_file->isDir() ? rmdir($rm_file->getRealPath()) : unlink($rm_file->getRealPath());
}
rmdir($dirName);
} else {
unlink($dirName);
}
} else {
mkdir($dirName, 0755, true);
rmdir($dirName);
}

return true;
Expand Down
12 changes: 0 additions & 12 deletions Integration/CorrectAddressIntegration.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,11 @@
class CorrectAddressIntegration extends AbstractEnhancerIntegration
{
const CA_REMOTE_HOST = 'host';
const CA_REMOTE_PORT = 'port';
const CA_REMOTE_PATH = 'home';
const CA_REMOTE_FILE = 'file';

const CA_REMOTE_USER = 'username';
const CA_REMOTE_PSWD = 'password';
const CA_REMOTE_FNGR = 'fingerprint';

const CA_CORRECTA_PATH = 'work_dir';
const CA_CORRECTA_CMD = 'cmd';
Expand Down Expand Up @@ -65,7 +63,6 @@ public function getRequiredKeyFields()
return [
self::CA_REMOTE_USER => 'mautic.enhancer.correctaddress.username',
self::CA_REMOTE_PSWD => 'mautic.enhancer.correctaddress.password',
self::CA_REMOTE_FNGR => 'mautic.enhancer.correctaddress.fingerprint',
];
}

Expand Down Expand Up @@ -98,15 +95,6 @@ public function appendToForm(&$builder, $data, $formArea)
'data' => isset($data[self::CA_REMOTE_HOST]) ? $data[self::CA_REMOTE_HOST] : '',
]
)
->add(
self::CA_REMOTE_PORT,
TextType::class,
[
'required' => true,
'label' => $translator->trans('mautic.enhancer.correctaddress.data_port'),
'data' => isset($data[self::CA_REMOTE_PORT]) ? $data[self::CA_REMOTE_PORT] : '22',
]
)
->add(
self::CA_REMOTE_PATH,
TextType::class,
Expand Down

0 comments on commit 8655bde

Please sign in to comment.