|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* @copyright Copyright (c) 2016, ownCloud, Inc. |
|
4
|
|
|
* |
|
5
|
|
|
* @author Bart Visscher <[email protected]> |
|
6
|
|
|
* @author Björn Schießle <[email protected]> |
|
7
|
|
|
* @author Jörn Friedrich Dreyer <[email protected]> |
|
8
|
|
|
* @author Morris Jobke <[email protected]> |
|
9
|
|
|
* @author Robin Appelman <[email protected]> |
|
10
|
|
|
* @author Robin McCorkell <[email protected]> |
|
11
|
|
|
* @author Sam Tuke <[email protected]> |
|
12
|
|
|
* @author Vincent Petry <[email protected]> |
|
13
|
|
|
* |
|
14
|
|
|
* @license AGPL-3.0 |
|
15
|
|
|
* |
|
16
|
|
|
* This code is free software: you can redistribute it and/or modify |
|
17
|
|
|
* it under the terms of the GNU Affero General Public License, version 3, |
|
18
|
|
|
* as published by the Free Software Foundation. |
|
19
|
|
|
* |
|
20
|
|
|
* This program is distributed in the hope that it will be useful, |
|
21
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
22
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
23
|
|
|
* GNU Affero General Public License for more details. |
|
24
|
|
|
* |
|
25
|
|
|
* You should have received a copy of the GNU Affero General Public License, version 3, |
|
26
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/> |
|
27
|
|
|
* |
|
28
|
|
|
*/ |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* This class contains all hooks. |
|
32
|
|
|
*/ |
|
33
|
|
|
|
|
34
|
|
|
namespace OCA\Files_Versions; |
|
35
|
|
|
|
|
36
|
|
|
class Hooks { |
|
37
|
|
|
|
|
38
|
|
|
public static function connectHooks() { |
|
39
|
|
|
// Listen to write signals |
|
40
|
|
|
\OCP\Util::connectHook('OC_Filesystem', 'write', 'OCA\Files_Versions\Hooks', 'write_hook'); |
|
41
|
|
|
// Listen to delete and rename signals |
|
42
|
|
|
\OCP\Util::connectHook('OC_Filesystem', 'post_delete', 'OCA\Files_Versions\Hooks', 'remove_hook'); |
|
43
|
|
|
\OCP\Util::connectHook('OC_Filesystem', 'delete', 'OCA\Files_Versions\Hooks', 'pre_remove_hook'); |
|
44
|
|
|
\OCP\Util::connectHook('OC_Filesystem', 'post_rename', 'OCA\Files_Versions\Hooks', 'rename_hook'); |
|
45
|
|
|
\OCP\Util::connectHook('OC_Filesystem', 'post_copy', 'OCA\Files_Versions\Hooks', 'copy_hook'); |
|
46
|
|
|
\OCP\Util::connectHook('OC_Filesystem', 'rename', 'OCA\Files_Versions\Hooks', 'pre_renameOrCopy_hook'); |
|
47
|
|
|
\OCP\Util::connectHook('OC_Filesystem', 'copy', 'OCA\Files_Versions\Hooks', 'pre_renameOrCopy_hook'); |
|
48
|
|
|
|
|
49
|
|
|
$eventDispatcher = \OC::$server->getEventDispatcher(); |
|
50
|
|
|
$eventDispatcher->addListener('OCA\Files::loadAdditionalScripts', ['OCA\Files_Versions\Hooks', 'onLoadFilesAppScripts']); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* listen to write event. |
|
55
|
|
|
*/ |
|
56
|
|
|
public static function write_hook( $params ) { |
|
57
|
|
|
$path = $params[\OC\Files\Filesystem::signal_param_path]; |
|
58
|
|
|
if($path !== '') { |
|
59
|
|
|
Storage::store($path); |
|
60
|
|
|
} |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* Erase versions of deleted file |
|
66
|
|
|
* @param array $params |
|
67
|
|
|
* |
|
68
|
|
|
* This function is connected to the delete signal of OC_Filesystem |
|
69
|
|
|
* cleanup the versions directory if the actual file gets deleted |
|
70
|
|
|
*/ |
|
71
|
|
|
public static function remove_hook($params) { |
|
72
|
|
|
$path = $params[\OC\Files\Filesystem::signal_param_path]; |
|
73
|
|
|
if($path !== '') { |
|
74
|
|
|
Storage::delete($path); |
|
75
|
|
|
} |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* mark file as "deleted" so that we can clean up the versions if the file is gone |
|
80
|
|
|
* @param array $params |
|
81
|
|
|
*/ |
|
82
|
|
|
public static function pre_remove_hook($params) { |
|
83
|
|
|
$path = $params[\OC\Files\Filesystem::signal_param_path]; |
|
84
|
|
|
if($path !== '') { |
|
85
|
|
|
Storage::markDeletedFile($path); |
|
86
|
|
|
} |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
/** |
|
90
|
|
|
* rename/move versions of renamed/moved files |
|
91
|
|
|
* @param array $params array with oldpath and newpath |
|
92
|
|
|
* |
|
93
|
|
|
* This function is connected to the rename signal of OC_Filesystem and adjust the name and location |
|
94
|
|
|
* of the stored versions along the actual file |
|
95
|
|
|
*/ |
|
96
|
|
View Code Duplication |
public static function rename_hook($params) { |
|
97
|
|
|
$oldpath = $params['oldpath']; |
|
98
|
|
|
$newpath = $params['newpath']; |
|
99
|
|
|
if($oldpath !== '' && $newpath !== '') { |
|
100
|
|
|
Storage::renameOrCopy($oldpath, $newpath, 'rename'); |
|
101
|
|
|
} |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
/** |
|
105
|
|
|
* copy versions of copied files |
|
106
|
|
|
* @param array $params array with oldpath and newpath |
|
107
|
|
|
* |
|
108
|
|
|
* This function is connected to the copy signal of OC_Filesystem and copies the |
|
109
|
|
|
* the stored versions to the new location |
|
110
|
|
|
*/ |
|
111
|
|
View Code Duplication |
public static function copy_hook($params) { |
|
112
|
|
|
$oldpath = $params['oldpath']; |
|
113
|
|
|
$newpath = $params['newpath']; |
|
114
|
|
|
if($oldpath !== '' && $newpath !== '') { |
|
115
|
|
|
Storage::renameOrCopy($oldpath, $newpath, 'copy'); |
|
116
|
|
|
} |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
/** |
|
120
|
|
|
* Remember owner and the owner path of the source file. |
|
121
|
|
|
* If the file already exists, then it was a upload of a existing file |
|
122
|
|
|
* over the web interface and we call Storage::store() directly |
|
123
|
|
|
* |
|
124
|
|
|
* @param array $params array with oldpath and newpath |
|
125
|
|
|
* |
|
126
|
|
|
*/ |
|
127
|
|
|
public static function pre_renameOrCopy_hook($params) { |
|
128
|
|
|
// if we rename a movable mount point, then the versions don't have |
|
129
|
|
|
// to be renamed |
|
130
|
|
|
$absOldPath = \OC\Files\Filesystem::normalizePath('/' . \OCP\User::getUser() . '/files' . $params['oldpath']); |
|
131
|
|
|
$manager = \OC\Files\Filesystem::getMountManager(); |
|
132
|
|
|
$mount = $manager->find($absOldPath); |
|
133
|
|
|
$internalPath = $mount->getInternalPath($absOldPath); |
|
134
|
|
|
if ($internalPath === '' and $mount instanceof \OC\Files\Mount\MoveableMount) { |
|
|
|
|
|
|
135
|
|
|
return; |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
|
|
$view = new \OC\Files\View(\OCP\User::getUser() . '/files'); |
|
139
|
|
|
if ($view->file_exists($params['newpath'])) { |
|
140
|
|
|
Storage::store($params['newpath']); |
|
141
|
|
|
} else { |
|
142
|
|
|
Storage::setSourcePathAndUser($params['oldpath']); |
|
143
|
|
|
} |
|
144
|
|
|
} |
|
145
|
|
|
|
|
146
|
|
|
/** |
|
147
|
|
|
* Load additional scripts when the files app is visible |
|
148
|
|
|
*/ |
|
149
|
|
|
public static function onLoadFilesAppScripts() { |
|
150
|
|
|
\OCP\Util::addScript('files_versions', 'merged'); |
|
151
|
|
|
} |
|
152
|
|
|
} |
|
153
|
|
|
|
PHP has two types of connecting operators (logical operators, and boolean operators):
and&&or||The difference between these is the order in which they are executed. In most cases, you would want to use a boolean operator like
&&, or||.Let’s take a look at a few examples:
Logical Operators are used for Control-Flow
One case where you explicitly want to use logical operators is for control-flow such as this:
Since
dieintroduces problems of its own, f.e. it makes our code hardly testable, and prevents any kind of more sophisticated error handling; you probably do not want to use this in real-world code. Unfortunately, logical operators cannot be combined withthrowat this point:These limitations lead to logical operators rarely being of use in current PHP code.