|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Spatie\EmailConcealerCLI; |
|
4
|
|
|
|
|
5
|
|
|
use Spatie\EmailConcealer\Concealer; |
|
6
|
|
|
use Symfony\Component\Console\Command\Command; |
|
7
|
|
|
use Symfony\Component\Console\Input\InputOption; |
|
8
|
|
|
use Symfony\Component\Console\Input\InputArgument; |
|
9
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
|
10
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
|
11
|
|
|
|
|
12
|
|
|
class ConcealCommand extends Command |
|
13
|
|
|
{ |
|
14
|
|
|
protected function configure() |
|
15
|
|
|
{ |
|
16
|
|
|
$this->setName('conceal'); |
|
17
|
|
|
|
|
18
|
|
|
$this->setDescription('Conceal all e-mail addresses in a file.'); |
|
19
|
|
|
|
|
20
|
|
|
$this->addArgument('src', InputArgument::REQUIRED, 'Path to the input file'); |
|
21
|
|
|
$this->addArgument('dest', InputArgument::OPTIONAL, 'Path to the destination file. Optional, leave empty to print output', null); |
|
22
|
|
|
|
|
23
|
|
|
$this->addOption('overwrite', 'o', InputOption::VALUE_NONE, 'Overwrite the input file'); |
|
24
|
|
|
$this->addOption('domain', 'd', InputOption::VALUE_REQUIRED, |
|
25
|
|
|
'The domain that should be used to concealed. Default: "example.com"', 'example.com'); |
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
|
|
protected function execute(InputInterface $input, OutputInterface $output): int |
|
29
|
|
|
{ |
|
30
|
|
|
if ($input->getOption('overwrite') && $input->getArgument('dest')) { |
|
31
|
|
|
$output->writeln("Can't specify a destination and overwrite the source file at the same time"); |
|
32
|
|
|
|
|
33
|
|
|
return 1; |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
$source = $input->getArgument('src'); |
|
37
|
|
|
$destination = $input->getOption('overwrite') ? $source : $input->getArgument('dest'); |
|
38
|
|
|
|
|
39
|
|
|
if (! file_exists($source)) { |
|
40
|
|
|
$output->writeln("File not found at path `$source`"); |
|
41
|
|
|
|
|
42
|
|
|
return 1; |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
$concealer = Concealer::create()->domain( |
|
46
|
|
|
$input->getOption('domain') |
|
47
|
|
|
); |
|
48
|
|
|
|
|
49
|
|
|
$concealed = $concealer->conceal( |
|
50
|
|
|
file_get_contents($source) |
|
51
|
|
|
); |
|
52
|
|
|
|
|
53
|
|
|
if ($destination) { |
|
54
|
|
|
file_put_contents($destination, $concealed); |
|
55
|
|
|
} else { |
|
56
|
|
|
$output->write($concealed); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
return 0; |
|
60
|
|
|
} |
|
61
|
|
|
} |
|
62
|
|
|
|