Completed
Pull Request — master (#30913)
by Individual IT
17:12
created

AppManager   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 72
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
dl 0
loc 72
rs 10
c 0
b 0
f 0
wmc 12
lcom 1
cbo 2

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
C enableApp() 0 51 11
1
<?php
2
/**
3
 * ownCloud
4
 *
5
 * @author Artur Neumann <[email protected]>
6
 * @copyright Copyright (c) 2017 Artur Neumann [email protected]
7
 *
8
 * This code is free software: you can redistribute it and/or modify
9
 * it under the terms of the GNU Affero General Public License, version 3,
10
 * as published by the Free Software Foundation.
11
 *
12
 * This program is distributed in the hope that it will be useful,
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
 * GNU Affero General Public License for more details.
16
 *
17
 * You should have received a copy of the GNU Affero General Public License, version 3,
18
 * along with this program.  If not, see <http://www.gnu.org/licenses/>
19
 *
20
 */
21
22
namespace OCA\Testing;
23
24
use OCP\IRequest;
25
26
/**
27
 * managing apps through testing app
28
 *
29
 */
30
class AppManager {
31
	/**
32
	 *
33
	 * @var IRequest
34
	 */
35
	private $request;
36
	
37
	/**
38
	 * List of apps that can be installed
39
	 * @var array
40
	 */
41
	private $apps = ['notifications'];
42
	/**
43
	 *
44
	 * @param IRequest $request
45
	 */
46
	public function __construct(IRequest $request) {
47
		$this->request = $request;
48
	}
49
	
50
	public function enableApp($parameters) {
51
		$app = $parameters['appid'];
52
		if (!in_array($app, $this->apps)) {
53
			throw new \InvalidArgumentException("this app cannot be installed");
54
		}
55
		$download = $this->request->getParam("download", "");
56
		$branch = $this->request->getParam("branch", "");
57
		$branch = filter_var(
58
			$branch, FILTER_SANITIZE_STRING,
59
			FILTER_FLAG_STRIP_LOW |
60
			FILTER_FLAG_STRIP_HIGH |
61
			FILTER_FLAG_STRIP_BACKTICK
62
		);
63
		if ($branch === false) {
64
			throw new \InvalidArgumentException("invalid branch name");
65
		}
66
		if ($branch === "") {
67
			$branch = "master";
68
		}
69
		if ($download === "true") {
70
			$downloadedFile = tempnam(sys_get_temp_dir(), "download-");
71
			if ($downloadedFile === false) {
72
				throw new \Exception("could not create temporary file name");
73
			}
74
			$downloadedFile .= ".zip";
75
		
76
			$downloadUrl="https://github.com/owncloud/$app/archive/$branch.zip";
77
			$remoteRessource = fopen($downloadUrl, 'r');
78
			if ($remoteRessource === false) {
79
				throw new \Exception("could not download from $downloadUrl");
80
			}
81
			
82
			if (file_put_contents($downloadedFile, $remoteRessource) === false) {
83
				throw new \Exception("could not download from $downloadUrl");
84
			}
85
			$zip = new \ZipArchive;
86
			$res = $zip->open($downloadedFile);
87
			if ($res !== true) {
88
				throw new \Exception("could not app ZIP archive: $res");
89
			}
90
			$appPath = \OC_App::getInstallPath();
91
			if (!is_string($appPath)) {
92
				throw new \Exception("no valid installation path found");
93
			}
94
			if (!$zip->extractTo("$appPath/$app")) {
95
				throw new \Exception("could not extract app ZIP archive");
96
			}
97
			$zip->close();
98
		}
99
		\OC_App::enable($app);
100
	}
101
}
102