Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
40 / 40
100.00% covered (success)
100.00%
3 / 3
CRAP
100.00% covered (success)
100.00%
1 / 1
controller
100.00% covered (success)
100.00%
40 / 40
100.00% covered (success)
100.00%
3 / 3
14
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
1
 handle
100.00% covered (success)
100.00%
32 / 32
100.00% covered (success)
100.00%
1 / 1
12
 error_response
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2/**
3 *
4 * This file is part of the phpBB Forum Software package.
5 *
6 * @copyright (c) phpBB Limited <https://www.phpbb.com>
7 * @license GNU General Public License, version 2 (GPL-2.0)
8 *
9 * For full copyright and license information, please see
10 * the docs/CREDITS.txt file.
11 *
12 */
13
14namespace phpbb\update;
15
16use phpbb\filesystem\filesystem_interface;
17use phpbb\language\language;
18
19class controller
20{
21    /** @var filesystem_interface Filesystem manager */
22    private filesystem_interface $filesystem;
23
24    /** @var get_updates Updater class */
25    private get_updates $updater;
26
27    /** @var language Translation handler */
28    private language $language;
29
30    /** @var string phpBB root path */
31    private string $phpbb_root_path;
32
33    /**
34     * Constructor.
35     *
36     * @param filesystem_interface $filesystem
37     * @param get_updates $updater
38     * @param language $language
39     * @param string $phpbb_root_path
40     */
41    public function __construct(
42        filesystem_interface $filesystem,
43        get_updates $updater,
44        language $language,
45        string $phpbb_root_path)
46    {
47        $this->filesystem = $filesystem;
48        $this->language = $language;
49        $this->updater = $updater;
50        $this->phpbb_root_path = $phpbb_root_path;
51    }
52
53    /**
54     * Handle requests.
55     *
56     * @param string $download The download URL.
57     *
58     * @return string[] Unencoded json response.
59     */
60    public function handle(string $download): array
61    {
62        $update_path = $this->phpbb_root_path . 'store/update.zip';
63        $status = ['status' => 'continue'];
64        if (!$this->filesystem->exists($update_path))
65        {
66            $result = $this->updater->download($download, $update_path);
67            if (!$result)
68            {
69                return $this->error_response('UPDATE_PACKAGE_DOWNLOAD_FAILURE');
70            }
71
72            return $status;
73        }
74
75        if (!$this->filesystem->exists($update_path . '.sig'))
76        {
77            $result = $this->updater->download($download . '.sig', $update_path . '.sig');
78            if (!$result)
79            {
80                return $this->error_response('UPDATE_SIGNATURE_DOWNLOAD_FAILURE');
81            }
82
83            return $status;
84        }
85
86        if (!$this->filesystem->exists($this->phpbb_root_path . 'store/update') || !is_dir($this->phpbb_root_path . 'store/update'))
87        {
88            $result = $this->updater->validate($update_path, $update_path . '.sig');
89            if (!$result)
90            {
91                return $this->error_response('UPDATE_SIGNATURE_INVALID');
92            }
93
94            $result = $this->updater->extract($update_path, $this->phpbb_root_path . 'store/update');
95            if (!$result)
96            {
97                return $this->error_response('UPDATE_PACKAGE_EXTRACT_FAILURE');
98            }
99
100            return $status;
101        }
102
103        if (!$this->filesystem->exists($this->phpbb_root_path . 'install') || !is_dir($this->phpbb_root_path . 'install'))
104        {
105            $result = $this->updater->copy($this->phpbb_root_path . 'store/update');
106            if (!$result)
107            {
108                return $this->error_response('UPDATE_FILES_COPY_FAILURE');
109            }
110
111            return $status;
112        }
113
114        $this->filesystem->remove([
115            $this->phpbb_root_path . 'store/update',
116            $update_path,
117            $update_path . '.sig'
118        ]);
119
120        $status['status'] = 'done';
121        return $status;
122    }
123
124    /**
125     * Create error response
126     *
127     * @param string $error_key
128     * @return array Error response
129     */
130    protected function error_response(string $error_key): array
131    {
132        return [
133            'status' => 'error',
134            'error' => $this->language->lang($error_key),
135        ];
136    }
137}