summaryrefslogtreecommitdiffstats
path: root/command/verifyinstall.php
blob: 31c700b176df0c38a9c378273537ce13e7285555 (plain)
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
<?php
/**
 * ownCloud - News
 *
 * This file is licensed under the Affero General Public License version 3 or
 * later. See the COPYING file.
 *
 * @author Bernhard Posselt <dev@bernhard-posselt.com>
 * @copyright Bernhard Posselt 2015
 */

namespace OCA\News\Command;

use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Helper\ProgressBar;
use Riimu\Kit\PathJoin\Path;

use OCA\News\Utility\FileChecksumValidator;


class VerifyInstall extends Command {

    private $fileChecksums;

    public function __construct($fileChecksums) {
        parent::__construct();
        $this->fileChecksums = $fileChecksums;
    }

    protected function configure() {
        $this->setName('news:verify-install')
             ->setDescription('Run this command to check if your News ' .
                              'installation has outdated or missing files.');
    }

    protected function execute(InputInterface $input, OutputInterface $output) {
        $checksums = json_decode($this->fileChecksums, true);
        $root = __DIR__ . '/../';
        $progressbar = new ProgressBar($output, count($checksums));
        $errors = [];
        $missing = [];

        foreach($checksums as $file => $checksum) {
            $progressbar->advance();
            $absPath = Path::normalize($root . $file);

            if (!file_exists($absPath)) {
                $missing[] = $absPath;
            } elseif (md5(file_get_contents($absPath)) !== $checksum) {
                $errors[] = $absPath;
            }
        }

        $output->writeln("\n");

        if (count($errors) > 0 || count($missing) > 0) {
            foreach ($missing as $path) {
                $output->writeln('<error>Missing file:</error> ' . $path);
            }
            foreach ($errors as $path) {
                $output->writeln('<error>Invalid checksum:</error> ' . $path);
            }
            $output->writeln("\nYour News installation does not " .
                             'match the recorded files and versions. This ' .
                             'is either caused by missing or old files or an ' .
                             'invalid or out of date appinfo/checksum.json ' .
                             'file.');
            $output->writeln('Either way, please make sure that the contents ' .
                             'of the News app\'s directory match the contents ' .
                             'of the installed tarball.');
            exit(1);
        } else {
            $output->writeln('<info>Installation verified, everything OK!' .
                             '</info>');
        }
    }

}