helper.php
5.23 KB
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
<?php
/**
* ownCloud - Updater plugin
*
* @author Victor Dubiniuk
* @copyright 2012-2013 Victor Dubiniuk victor.dubiniuk@gmail.com
*
* This file is licensed under the Affero General Public License version 3 or
* later.
*/
namespace OCA\Updater;
class Helper {
const APP_DIRNAME = 'apps';
const THIRDPARTY_DIRNAME = '3rdparty';
const CORE_DIRNAME = 'core';
/**
* Moves file/directory
* @param string $src - source path
* @param string $dest - destination path
* @throws \Exception on error
*/
public static function move($src, $dest) {
if (!@rename($src, $dest)) {
throw new \Exception("Unable to move $src to $dest");
}
}
/**
* Copy recoursive
* @param string $src - source path
* @param string $dest - destination path
* @throws \Exception on error
*/
public static function copyr($src, $dest, $stopOnError = true) {
if(is_dir($src)) {
if(!is_dir($dest)) {
try {
self::mkdir($dest);
} catch (\Exception $e){
if ($stopOnError){
throw $e;
}
}
}
$files = scandir($src);
foreach ($files as $file) {
if ($file != "." && $file != "..") {
self::copyr("$src/$file", "$dest/$file", $stopOnError);
}
}
}elseif(file_exists($src)) {
if (!@copy($src, $dest) && $stopOnError) {
throw new \Exception("Unable copy $src to $dest");
}
}
}
/**
* Wrapper for mkdir
* @param string $path
* @param bool $isRecoursive
* @throws \Exception on error
*/
public static function mkdir($path, $isRecoursive = false) {
if (!@mkdir($path, 0755, $isRecoursive)) {
throw new \Exception("Unable to create $path");
}
}
/**
* Get directory content as array
* @param string $path
* @return array
* @throws \Exception on error
*/
public static function scandir($path) {
$content = @scandir($path);
if (!is_array($content)) {
throw new \Exception("Unable to list $path content");
}
return $content;
}
/**
* Silently remove the filesystem item
* Used for cleanup
* @param string $path
*/
public static function removeIfExists($path) {
if (!file_exists($path)) {
return;
}
if (is_dir($path)) {
self::rmdirr($path);
} else {
@unlink($path);
}
}
protected static function rmdirr($dir) {
if(is_dir($dir)) {
$files = @scandir($dir);
foreach($files as $file) {
if ($file != "." && $file != "..") {
self::rmdirr("$dir/$file");
}
}
@rmdir($dir);
}elseif(file_exists($dir)) {
@unlink($dir);
}
if(file_exists($dir)) {
return false;
}else{
return true;
}
}
/**
* Get the final list of files/directories to be replaced
* e.g. ['core']['lib'] = '/path/to/lib'
* @return array
*/
public static function getPreparedLocations() {
$preparedLocations = array();
foreach (self::getDirectories() as $type => $path) {
$preparedLocations[$type] = self::getFilteredContent($path);
}
return $preparedLocations;
}
/**
* Lists directory content as an array
* ['basename']=>'full path'
* e.g.['lib'] = '/path/to/lib'
* @param string $path
* @return array
*/
public static function getFilteredContent($path){
$result = array();
$filtered = self::filterLocations(self::scandir($path), $path);
foreach ($filtered as $dirName){
$result [$dirName] = $path . '/' . $dirName;
}
return $result;
}
public static function filterLocations($locations, $basePath) {
$fullPath = array_values(self::getDirectories());
$fullPath[] = rtrim(App::getBackupBase(), '/');
$fullPath[] = \OCP\Config::getSystemValue( "datadirectory", \OC::$SERVERROOT."/data" );
$fullPath[] = \OC::$SERVERROOT."/themes";
$exclusions = array(
'full' => $fullPath,
'relative' => array('.', '..')
);
foreach ($locations as $key => $location) {
$fullPath = $basePath . '/' .$location;
if (!is_dir($fullPath)) {
continue;
}
if (in_array($fullPath, $exclusions['full'])
|| in_array($location, $exclusions['relative'])
) {
unset($locations[$key]);
}
}
return $locations;
}
/**
* Get the list of directories to be replaced on update
* @return array
*/
public static function getDirectories() {
$dirs = array();
$dirs[self::THIRDPARTY_DIRNAME] = \OC::$THIRDPARTYROOT . '/' . self::THIRDPARTY_DIRNAME;
//Long, long ago we had single app location
if (isset(\OC::$APPSROOTS)) {
foreach (\OC::$APPSROOTS as $i => $approot){
$index = $i ? $i : '';
$dirs[self::APP_DIRNAME . $index] = $approot['path'];
}
} else {
$dirs[self::APP_DIRNAME] = \OC::$APPSROOT . '/' . self::APP_DIRNAME;
}
$dirs[self::CORE_DIRNAME] = \OC::$SERVERROOT;
return $dirs;
}
public static function getSources($version) {
$base = Downloader::getPackageDir($version);
return array (
self::APP_DIRNAME => $base . '/' . self::APP_DIRNAME,
self::THIRDPARTY_DIRNAME => $base . '/' . self::THIRDPARTY_DIRNAME,
self::CORE_DIRNAME => $base . '/' . self::CORE_DIRNAME,
);
}
public static function addDirectoryToZip($zip, $dir, $base) {
$newFolder = str_replace($base, '', $dir);
$zip->addEmptyDir($newFolder);
foreach(glob($dir . '/*') as $file) {
if(is_dir($file)) {
$zip = self::addDirectoryToZip($zip, $file, $base);
} else {
$newFile = str_replace($base, '', $file);
$zip->addFile($file, $newFile);
}
}
return $zip;
}
}