Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Process string in case of special characters #167

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 25 additions & 6 deletions src/Joomlatools/Console/Command/Database/AbstractDatabase.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;

use Joomlatools\Console\Command\Site\AbstractSite;

abstract class AbstractDatabase extends AbstractSite
Expand Down Expand Up @@ -148,17 +147,21 @@ private function _executeMysqlWithCredentials(callable $callback)
$file = tmpfile();
$path = stream_get_meta_data($file)['uri'];

$user = $this->processString($this->mysql->user);
$password = $this->processString($this->mysql->password);
$host = $this->processString($this->mysql->host);
$port = $this->processString($this->mysql->port);

$contents = <<<STR
[client]
user={$this->mysql->user}
password={$this->mysql->password}
host={$this->mysql->host}
port={$this->mysql->port}
user={$user}
password={$password}
host={$host}
port={$port}
STR;

fwrite($file, $contents);


return exec($callback($path));
}
finally {
Expand All @@ -185,4 +188,20 @@ protected function _promptDatabaseDetails(InputInterface $input, OutputInterface
$input->setOption('mysql-database', $this->target_db);
$input->setOption('mysql-driver', $this->mysql->driver);
}

private function processString(string $input): string
{
// Undo escaped characters
$input = stripslashes($input);

// Check if the string starts and ends with single quotes and replace with double quotes
// or add double quotes if the string does not start and end with quotes
if (preg_match("/^'(.*)'$/", $input, $matches)) {
$input = '"' . $matches[1] . '"';
} elseif (!preg_match('/^["\'].*["\']$/', $input)) {
$input = '"' . $input . '"';
}

return $input;
}
}