File size: 5,257 Bytes
d2897cd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
<?php

namespace Mautic\CoreBundle\Command;

use Doctrine\ORM\EntityManager;
use Mautic\CoreBundle\IpLookup\DoNotSellList\MaxMindDoNotSellList;
use Mautic\LeadBundle\Entity\Lead;
use Mautic\LeadBundle\Entity\LeadRepository;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Helper\ProgressBar;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;

/**
 * CLI Command to purge data from Mautic that appears on the
 * MaxMind Do Not Sell list.
 */
class MaxMindDoNotSellPurgeCommand extends Command
{
    public function __construct(
        private EntityManager $em,
        private LeadRepository $leadRepository,
        private MaxMindDoNotSellList $doNotSellList
    ) {
        parent::__construct();
    }

    protected function configure()
    {
        $this->setName('mautic:max-mind:purge')
            ->addOption(
                'dry-run',
                'd',
                InputOption::VALUE_NONE,
                'Get a list of data that will be purged.'
            )
            ->setHelp(<<<'EOT'
The <info>%command.name%</info> command will purge all data from Mautic which is related to any IP found on the MaxMind Do Not Sell List.

<info>php %command.full_name% --dry-run</info>

Performs a dry-run which will not actually purge any data, but will produce a list of what would be purged.

<info>php %command.full_name% --batch-size</info>

Set the number of records to return in a batch when processing the Do Not Sell List. This option is ignored if IPs are passed as an argument.
EOT
            );
    }

    protected function execute(InputInterface $input, OutputInterface $output): int
    {
        try {
            $dryRun = $input->getOption('dry-run');

            $output->writeln('<info>Step 1: Searching for contacts with data from Do Not Sell List...</info>');

            $this->doNotSellList->loadList();
            $doNotSellListIPs = array_map(fn ($item): string =>
                // strip subnet mask characters
                $this->doNotSellList->stripCIDR($item['value']), $this->doNotSellList->getList());
            $doNotSellContacts = $this->findContactsFromIPs($doNotSellListIPs);

            if (0 == count($doNotSellContacts)) {
                $output->writeln('<info>No matches found.</info>');

                return Command::SUCCESS;
            }

            $output->writeln('Found '.count($doNotSellContacts)." contacts with an IP from the Do Not Sell list.\n");

            if ($dryRun) {
                $output->writeln('<info>Dry run; skipping purge.</info>');

                return Command::SUCCESS;
            }

            $output->writeln('<info>Step 2: Purging data...</info>');
            $purgeProgress = new ProgressBar($output, count($doNotSellContacts));

            foreach ($doNotSellContacts as $contact) {
                $this->purgeData($contact['id'], $contact['ip_address']);
                $purgeProgress->advance(1);
            }

            $purgeProgress->finish();
            $output->writeln("\n<info>Purge complete.</info>\n");

            return Command::SUCCESS;
        } catch (\Exception $e) {
            $output->writeln("\n<error>".$e->getMessage().'</error>');

            return Command::FAILURE;
        }
    }

    private function findContactsFromIPs(array $ips): array
    {
        $in  = "'".implode("','", $ips)."'";
        $sql =
            'SELECT x.lead_id AS id, ip.ip_address AS ip_address '.
             'FROM '.MAUTIC_TABLE_PREFIX.'lead_ips_xref x '.
             'JOIN '.MAUTIC_TABLE_PREFIX.'ip_addresses ip ON x.ip_id = ip.id '.
             'WHERE ip.ip_address IN ('.$in.')';

        $conn   = $this->em->getConnection();
        $stmt   = $conn->prepare($sql);
        $result = $stmt->executeQuery();

        return $result->fetchAllAssociative();
    }

    private function purgeData(string $contactId, string $ip): bool
    {
        /** @var Lead $lead */
        $lead       = $this->leadRepository->findOneBy(['id' => $contactId]);
        $matchedIps = array_filter($lead->getIpAddresses()->getValues(), fn ($item): bool => $item->getIpAddress() == $ip);

        // We only purge data from the contact if it matches the data in the IP details
        if ($ipDetails = $matchedIps[0]->getIpDetails()) {
            return false;
        }

        $changed = false;
        if (($ipDetails['city'] ?? '') == $lead->getCity()) {
            $lead->setCity(null);
            $changed = true;
        }
        if (($ipDetails['region'] ?? '') == $lead->getState()) {
            $lead->setState(null);
            $changed = true;
        }
        if (($ipDetails['country'] ?? '') == $lead->getCountry()) {
            $lead->setCountry(null);
            $changed = true;
        }
        if (($ipDetails['zipcode'] ?? '') == $lead->getZipcode()) {
            $lead->setZipcode(null);
            $changed = true;
        }

        if ($changed) {
            $this->leadRepository->saveEntity($lead);

            return true;
        }

        return false;
    }

    protected static $defaultDescription = 'Purge data connected to MaxMind Do Not Sell list.';
}