Issues (3627)

CoreBundle/Update/Step/RemoveDeletedFilesStep.php (1 issue)

1
<?php
2
3
/*
4
 * @copyright   2020 Mautic Contributors. All rights reserved
5
 * @author      Mautic
6
 *
7
 * @link        https://www.mautic.org
8
 *
9
 * @license     GNU/GPLv3 http://www.gnu.org/licenses/gpl-3.0.html
10
 */
11
12
namespace Mautic\CoreBundle\Update\Step;
13
14
use Mautic\CoreBundle\Helper\PathsHelper;
15
use Psr\Log\LoggerInterface;
16
use Symfony\Component\Console\Helper\ProgressBar;
17
use Symfony\Component\Console\Input\InputInterface;
18
use Symfony\Component\Console\Output\OutputInterface;
19
use Symfony\Component\Translation\TranslatorInterface;
20
21
final class RemoveDeletedFilesStep implements StepInterface
22
{
23
    /**
24
     * @var TranslatorInterface
25
     */
26
    private $translator;
27
28
    /**
29
     * @var LoggerInterface
30
     */
31
    private $logger;
32
33
    /**
34
     * @var string
35
     */
36
    private $appRoot;
37
38
    public function __construct(TranslatorInterface $translator, PathsHelper $pathsHelper, LoggerInterface $logger)
39
    {
40
        $this->translator = $translator;
41
        $this->appRoot    = $pathsHelper->getRootPath();
42
        $this->logger     = $logger;
43
    }
44
45
    public function getOrder(): int
46
    {
47
        return 10;
48
    }
49
50
    public function shouldExecuteInFinalStage(): bool
51
    {
52
        return false;
53
    }
54
55
    public function execute(ProgressBar $progressBar, InputInterface $input, OutputInterface $output): void
56
    {
57
        // Make sure we have a deleted_files list otherwise we can't process this step
58
        if (!file_exists($this->appRoot.'/deleted_files.txt')) {
59
            return;
60
        }
61
62
        $progressBar->setMessage($this->translator->trans('mautic.core.update.remove.deleted.files'));
63
        $progressBar->advance();
64
65
        $deletedFiles = json_decode(file_get_contents($this->appRoot.'/deleted_files.txt'), true);
66
67
        // Before looping over the deleted files, add in our upgrade specific files
68
        $deletedFiles += ['deleted_files.txt', 'upgrade.php'];
69
70
        foreach ($deletedFiles as $file) {
71
            $this->deleteFile($file);
72
        }
73
74
        @unlink($this->appRoot.'/deleted_files.txt');
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition for unlink(). This can introduce security issues, and is generally not recommended. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unhandled  annotation

74
        /** @scrutinizer ignore-unhandled */ @unlink($this->appRoot.'/deleted_files.txt');

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
75
    }
76
77
    private function deleteFile(string $file)
78
    {
79
        $path = $this->appRoot.'/'.$file;
80
81
        if (!file_exists($path)) {
82
            return;
83
        }
84
85
        // Try setting the permissions to 777 just to make sure we can get rid of the file
86
        @chmod($path, 0777);
87
88
        if (@unlink($path)) {
89
            return;
90
        }
91
92
        // Failed to delete, reset the permissions to 644 for safety
93
        @chmod($path, 0644);
94
95
        $this->logger->error(
96
            'UPDATE ERROR: '.$this->translator->trans('mautic.core.update.error.removing.file', ['%path%' => $file])
97
        );
98
    }
99
}
100