@@ -27,7 +27,7 @@ |
||
27 | 27 | use OCP\Encryption\Exceptions\GenericEncryptionException; |
28 | 28 | |
29 | 29 | class EncryptionHeaderToLargeException extends GenericEncryptionException { |
30 | - public function __construct() { |
|
31 | - parent::__construct('max header size exceeded'); |
|
32 | - } |
|
30 | + public function __construct() { |
|
31 | + parent::__construct('max header size exceeded'); |
|
32 | + } |
|
33 | 33 | } |
@@ -73,7 +73,7 @@ |
||
73 | 73 | // always add owner to the list of users with access to the file |
74 | 74 | $userIds = [$owner]; |
75 | 75 | |
76 | - if (!$this->util->isFile($owner . '/' . $ownerPath)) { |
|
76 | + if (!$this->util->isFile($owner.'/'.$ownerPath)) { |
|
77 | 77 | return ['users' => $userIds, 'public' => false]; |
78 | 78 | } |
79 | 79 |
@@ -35,94 +35,94 @@ |
||
35 | 35 | |
36 | 36 | class File implements \OCP\Encryption\IFile { |
37 | 37 | |
38 | - /** @var Util */ |
|
39 | - protected $util; |
|
40 | - |
|
41 | - /** @var IRootFolder */ |
|
42 | - private $rootFolder; |
|
43 | - |
|
44 | - /** @var IManager */ |
|
45 | - private $shareManager; |
|
46 | - |
|
47 | - /** |
|
48 | - * cache results of already checked folders |
|
49 | - * |
|
50 | - * @var array |
|
51 | - */ |
|
52 | - protected $cache; |
|
53 | - |
|
54 | - public function __construct(Util $util, |
|
55 | - IRootFolder $rootFolder, |
|
56 | - IManager $shareManager) { |
|
57 | - $this->util = $util; |
|
58 | - $this->cache = new CappedMemoryCache(); |
|
59 | - $this->rootFolder = $rootFolder; |
|
60 | - $this->shareManager = $shareManager; |
|
61 | - } |
|
62 | - |
|
63 | - |
|
64 | - /** |
|
65 | - * get list of users with access to the file |
|
66 | - * |
|
67 | - * @param string $path to the file |
|
68 | - * @return array ['users' => $uniqueUserIds, 'public' => $public] |
|
69 | - */ |
|
70 | - public function getAccessList($path) { |
|
71 | - |
|
72 | - // Make sure that a share key is generated for the owner too |
|
73 | - [$owner, $ownerPath] = $this->util->getUidAndFilename($path); |
|
74 | - |
|
75 | - // always add owner to the list of users with access to the file |
|
76 | - $userIds = [$owner]; |
|
77 | - |
|
78 | - if (!$this->util->isFile($owner . '/' . $ownerPath)) { |
|
79 | - return ['users' => $userIds, 'public' => false]; |
|
80 | - } |
|
81 | - |
|
82 | - $ownerPath = substr($ownerPath, strlen('/files')); |
|
83 | - $userFolder = $this->rootFolder->getUserFolder($owner); |
|
84 | - try { |
|
85 | - $file = $userFolder->get($ownerPath); |
|
86 | - } catch (NotFoundException $e) { |
|
87 | - $file = null; |
|
88 | - } |
|
89 | - $ownerPath = $this->util->stripPartialFileExtension($ownerPath); |
|
90 | - |
|
91 | - // first get the shares for the parent and cache the result so that we don't |
|
92 | - // need to check all parents for every file |
|
93 | - $parent = dirname($ownerPath); |
|
94 | - $parentNode = $userFolder->get($parent); |
|
95 | - if (isset($this->cache[$parent])) { |
|
96 | - $resultForParents = $this->cache[$parent]; |
|
97 | - } else { |
|
98 | - $resultForParents = $this->shareManager->getAccessList($parentNode); |
|
99 | - $this->cache[$parent] = $resultForParents; |
|
100 | - } |
|
101 | - $userIds = array_merge($userIds, $resultForParents['users']); |
|
102 | - $public = $resultForParents['public'] || $resultForParents['remote']; |
|
103 | - |
|
104 | - |
|
105 | - // Find out who, if anyone, is sharing the file |
|
106 | - if ($file !== null) { |
|
107 | - $resultForFile = $this->shareManager->getAccessList($file, false); |
|
108 | - $userIds = array_merge($userIds, $resultForFile['users']); |
|
109 | - $public = $resultForFile['public'] || $resultForFile['remote'] || $public; |
|
110 | - } |
|
111 | - |
|
112 | - // check if it is a group mount |
|
113 | - if (\OCP\App::isEnabled("files_external")) { |
|
114 | - $mounts = \OCA\Files_External\MountConfig::getSystemMountPoints(); |
|
115 | - foreach ($mounts as $mount) { |
|
116 | - if ($mount['mountpoint'] == substr($ownerPath, 1, strlen($mount['mountpoint']))) { |
|
117 | - $mountedFor = $this->util->getUserWithAccessToMountPoint($mount['applicable']['users'], $mount['applicable']['groups']); |
|
118 | - $userIds = array_merge($userIds, $mountedFor); |
|
119 | - } |
|
120 | - } |
|
121 | - } |
|
122 | - |
|
123 | - // Remove duplicate UIDs |
|
124 | - $uniqueUserIds = array_unique($userIds); |
|
125 | - |
|
126 | - return ['users' => $uniqueUserIds, 'public' => $public]; |
|
127 | - } |
|
38 | + /** @var Util */ |
|
39 | + protected $util; |
|
40 | + |
|
41 | + /** @var IRootFolder */ |
|
42 | + private $rootFolder; |
|
43 | + |
|
44 | + /** @var IManager */ |
|
45 | + private $shareManager; |
|
46 | + |
|
47 | + /** |
|
48 | + * cache results of already checked folders |
|
49 | + * |
|
50 | + * @var array |
|
51 | + */ |
|
52 | + protected $cache; |
|
53 | + |
|
54 | + public function __construct(Util $util, |
|
55 | + IRootFolder $rootFolder, |
|
56 | + IManager $shareManager) { |
|
57 | + $this->util = $util; |
|
58 | + $this->cache = new CappedMemoryCache(); |
|
59 | + $this->rootFolder = $rootFolder; |
|
60 | + $this->shareManager = $shareManager; |
|
61 | + } |
|
62 | + |
|
63 | + |
|
64 | + /** |
|
65 | + * get list of users with access to the file |
|
66 | + * |
|
67 | + * @param string $path to the file |
|
68 | + * @return array ['users' => $uniqueUserIds, 'public' => $public] |
|
69 | + */ |
|
70 | + public function getAccessList($path) { |
|
71 | + |
|
72 | + // Make sure that a share key is generated for the owner too |
|
73 | + [$owner, $ownerPath] = $this->util->getUidAndFilename($path); |
|
74 | + |
|
75 | + // always add owner to the list of users with access to the file |
|
76 | + $userIds = [$owner]; |
|
77 | + |
|
78 | + if (!$this->util->isFile($owner . '/' . $ownerPath)) { |
|
79 | + return ['users' => $userIds, 'public' => false]; |
|
80 | + } |
|
81 | + |
|
82 | + $ownerPath = substr($ownerPath, strlen('/files')); |
|
83 | + $userFolder = $this->rootFolder->getUserFolder($owner); |
|
84 | + try { |
|
85 | + $file = $userFolder->get($ownerPath); |
|
86 | + } catch (NotFoundException $e) { |
|
87 | + $file = null; |
|
88 | + } |
|
89 | + $ownerPath = $this->util->stripPartialFileExtension($ownerPath); |
|
90 | + |
|
91 | + // first get the shares for the parent and cache the result so that we don't |
|
92 | + // need to check all parents for every file |
|
93 | + $parent = dirname($ownerPath); |
|
94 | + $parentNode = $userFolder->get($parent); |
|
95 | + if (isset($this->cache[$parent])) { |
|
96 | + $resultForParents = $this->cache[$parent]; |
|
97 | + } else { |
|
98 | + $resultForParents = $this->shareManager->getAccessList($parentNode); |
|
99 | + $this->cache[$parent] = $resultForParents; |
|
100 | + } |
|
101 | + $userIds = array_merge($userIds, $resultForParents['users']); |
|
102 | + $public = $resultForParents['public'] || $resultForParents['remote']; |
|
103 | + |
|
104 | + |
|
105 | + // Find out who, if anyone, is sharing the file |
|
106 | + if ($file !== null) { |
|
107 | + $resultForFile = $this->shareManager->getAccessList($file, false); |
|
108 | + $userIds = array_merge($userIds, $resultForFile['users']); |
|
109 | + $public = $resultForFile['public'] || $resultForFile['remote'] || $public; |
|
110 | + } |
|
111 | + |
|
112 | + // check if it is a group mount |
|
113 | + if (\OCP\App::isEnabled("files_external")) { |
|
114 | + $mounts = \OCA\Files_External\MountConfig::getSystemMountPoints(); |
|
115 | + foreach ($mounts as $mount) { |
|
116 | + if ($mount['mountpoint'] == substr($ownerPath, 1, strlen($mount['mountpoint']))) { |
|
117 | + $mountedFor = $this->util->getUserWithAccessToMountPoint($mount['applicable']['users'], $mount['applicable']['groups']); |
|
118 | + $userIds = array_merge($userIds, $mountedFor); |
|
119 | + } |
|
120 | + } |
|
121 | + } |
|
122 | + |
|
123 | + // Remove duplicate UIDs |
|
124 | + $uniqueUserIds = array_unique($userIds); |
|
125 | + |
|
126 | + return ['users' => $uniqueUserIds, 'public' => $public]; |
|
127 | + } |
|
128 | 128 | } |
@@ -66,12 +66,12 @@ discard block |
||
66 | 66 | */ |
67 | 67 | private function buildFileNameWithSuffix($absolutePath, $postFix = '') { |
68 | 68 | if ($postFix !== '') { |
69 | - $postFix = '.' . ltrim($postFix, '.'); |
|
69 | + $postFix = '.'.ltrim($postFix, '.'); |
|
70 | 70 | $postFix = str_replace(['\\', '/'], '', $postFix); |
71 | 71 | $absolutePath .= '-'; |
72 | 72 | } |
73 | 73 | |
74 | - return $absolutePath . $postFix; |
|
74 | + return $absolutePath.$postFix; |
|
75 | 75 | } |
76 | 76 | |
77 | 77 | /** |
@@ -127,11 +127,11 @@ discard block |
||
127 | 127 | $this->current[] = $uniqueFileName; |
128 | 128 | |
129 | 129 | // Build a name without postfix |
130 | - $path = $this->buildFileNameWithSuffix($uniqueFileName . '-folder', $postFix); |
|
130 | + $path = $this->buildFileNameWithSuffix($uniqueFileName.'-folder', $postFix); |
|
131 | 131 | mkdir($path, 0700); |
132 | 132 | $this->current[] = $path; |
133 | 133 | |
134 | - return $path . '/'; |
|
134 | + return $path.'/'; |
|
135 | 135 | } else { |
136 | 136 | $this->log->warning( |
137 | 137 | 'Can not create a temporary folder in directory {dir}. Check it exists and has correct permissions', |
@@ -190,7 +190,7 @@ discard block |
||
190 | 190 | if ($dh) { |
191 | 191 | while (($file = readdir($dh)) !== false) { |
192 | 192 | if (substr($file, 0, 7) === self::TMP_PREFIX) { |
193 | - $path = $this->tmpBaseDir . '/' . $file; |
|
193 | + $path = $this->tmpBaseDir.'/'.$file; |
|
194 | 194 | $mtime = filemtime($path); |
195 | 195 | if ($mtime < $cutOfTime) { |
196 | 196 | $files[] = $path; |
@@ -37,244 +37,244 @@ |
||
37 | 37 | use Psr\Log\LoggerInterface; |
38 | 38 | |
39 | 39 | class TempManager implements ITempManager { |
40 | - /** @var string[] Current temporary files and folders, used for cleanup */ |
|
41 | - protected $current = []; |
|
42 | - /** @var string i.e. /tmp on linux systems */ |
|
43 | - protected $tmpBaseDir; |
|
44 | - /** @var LoggerInterface */ |
|
45 | - protected $log; |
|
46 | - /** @var IConfig */ |
|
47 | - protected $config; |
|
48 | - /** @var IniGetWrapper */ |
|
49 | - protected $iniGetWrapper; |
|
40 | + /** @var string[] Current temporary files and folders, used for cleanup */ |
|
41 | + protected $current = []; |
|
42 | + /** @var string i.e. /tmp on linux systems */ |
|
43 | + protected $tmpBaseDir; |
|
44 | + /** @var LoggerInterface */ |
|
45 | + protected $log; |
|
46 | + /** @var IConfig */ |
|
47 | + protected $config; |
|
48 | + /** @var IniGetWrapper */ |
|
49 | + protected $iniGetWrapper; |
|
50 | 50 | |
51 | - /** Prefix */ |
|
52 | - public const TMP_PREFIX = 'oc_tmp_'; |
|
51 | + /** Prefix */ |
|
52 | + public const TMP_PREFIX = 'oc_tmp_'; |
|
53 | 53 | |
54 | - public function __construct(LoggerInterface $logger, IConfig $config, IniGetWrapper $iniGetWrapper) { |
|
55 | - $this->log = $logger; |
|
56 | - $this->config = $config; |
|
57 | - $this->iniGetWrapper = $iniGetWrapper; |
|
58 | - $this->tmpBaseDir = $this->getTempBaseDir(); |
|
59 | - } |
|
54 | + public function __construct(LoggerInterface $logger, IConfig $config, IniGetWrapper $iniGetWrapper) { |
|
55 | + $this->log = $logger; |
|
56 | + $this->config = $config; |
|
57 | + $this->iniGetWrapper = $iniGetWrapper; |
|
58 | + $this->tmpBaseDir = $this->getTempBaseDir(); |
|
59 | + } |
|
60 | 60 | |
61 | - /** |
|
62 | - * Builds the filename with suffix and removes potential dangerous characters |
|
63 | - * such as directory separators. |
|
64 | - * |
|
65 | - * @param string $absolutePath Absolute path to the file / folder |
|
66 | - * @param string $postFix Postfix appended to the temporary file name, may be user controlled |
|
67 | - * @return string |
|
68 | - */ |
|
69 | - private function buildFileNameWithSuffix($absolutePath, $postFix = '') { |
|
70 | - if ($postFix !== '') { |
|
71 | - $postFix = '.' . ltrim($postFix, '.'); |
|
72 | - $postFix = str_replace(['\\', '/'], '', $postFix); |
|
73 | - $absolutePath .= '-'; |
|
74 | - } |
|
61 | + /** |
|
62 | + * Builds the filename with suffix and removes potential dangerous characters |
|
63 | + * such as directory separators. |
|
64 | + * |
|
65 | + * @param string $absolutePath Absolute path to the file / folder |
|
66 | + * @param string $postFix Postfix appended to the temporary file name, may be user controlled |
|
67 | + * @return string |
|
68 | + */ |
|
69 | + private function buildFileNameWithSuffix($absolutePath, $postFix = '') { |
|
70 | + if ($postFix !== '') { |
|
71 | + $postFix = '.' . ltrim($postFix, '.'); |
|
72 | + $postFix = str_replace(['\\', '/'], '', $postFix); |
|
73 | + $absolutePath .= '-'; |
|
74 | + } |
|
75 | 75 | |
76 | - return $absolutePath . $postFix; |
|
77 | - } |
|
76 | + return $absolutePath . $postFix; |
|
77 | + } |
|
78 | 78 | |
79 | - /** |
|
80 | - * Create a temporary file and return the path |
|
81 | - * |
|
82 | - * @param string $postFix Postfix appended to the temporary file name |
|
83 | - * @return string |
|
84 | - */ |
|
85 | - public function getTemporaryFile($postFix = '') { |
|
86 | - if (is_writable($this->tmpBaseDir)) { |
|
87 | - // To create an unique file and prevent the risk of race conditions |
|
88 | - // or duplicated temporary files by other means such as collisions |
|
89 | - // we need to create the file using `tempnam` and append a possible |
|
90 | - // postfix to it later |
|
91 | - $file = tempnam($this->tmpBaseDir, self::TMP_PREFIX); |
|
92 | - $this->current[] = $file; |
|
79 | + /** |
|
80 | + * Create a temporary file and return the path |
|
81 | + * |
|
82 | + * @param string $postFix Postfix appended to the temporary file name |
|
83 | + * @return string |
|
84 | + */ |
|
85 | + public function getTemporaryFile($postFix = '') { |
|
86 | + if (is_writable($this->tmpBaseDir)) { |
|
87 | + // To create an unique file and prevent the risk of race conditions |
|
88 | + // or duplicated temporary files by other means such as collisions |
|
89 | + // we need to create the file using `tempnam` and append a possible |
|
90 | + // postfix to it later |
|
91 | + $file = tempnam($this->tmpBaseDir, self::TMP_PREFIX); |
|
92 | + $this->current[] = $file; |
|
93 | 93 | |
94 | - // If a postfix got specified sanitize it and create a postfixed |
|
95 | - // temporary file |
|
96 | - if ($postFix !== '') { |
|
97 | - $fileNameWithPostfix = $this->buildFileNameWithSuffix($file, $postFix); |
|
98 | - touch($fileNameWithPostfix); |
|
99 | - chmod($fileNameWithPostfix, 0600); |
|
100 | - $this->current[] = $fileNameWithPostfix; |
|
101 | - return $fileNameWithPostfix; |
|
102 | - } |
|
94 | + // If a postfix got specified sanitize it and create a postfixed |
|
95 | + // temporary file |
|
96 | + if ($postFix !== '') { |
|
97 | + $fileNameWithPostfix = $this->buildFileNameWithSuffix($file, $postFix); |
|
98 | + touch($fileNameWithPostfix); |
|
99 | + chmod($fileNameWithPostfix, 0600); |
|
100 | + $this->current[] = $fileNameWithPostfix; |
|
101 | + return $fileNameWithPostfix; |
|
102 | + } |
|
103 | 103 | |
104 | - return $file; |
|
105 | - } else { |
|
106 | - $this->log->warning( |
|
107 | - 'Can not create a temporary file in directory {dir}. Check it exists and has correct permissions', |
|
108 | - [ |
|
109 | - 'dir' => $this->tmpBaseDir, |
|
110 | - ] |
|
111 | - ); |
|
112 | - return false; |
|
113 | - } |
|
114 | - } |
|
104 | + return $file; |
|
105 | + } else { |
|
106 | + $this->log->warning( |
|
107 | + 'Can not create a temporary file in directory {dir}. Check it exists and has correct permissions', |
|
108 | + [ |
|
109 | + 'dir' => $this->tmpBaseDir, |
|
110 | + ] |
|
111 | + ); |
|
112 | + return false; |
|
113 | + } |
|
114 | + } |
|
115 | 115 | |
116 | - /** |
|
117 | - * Create a temporary folder and return the path |
|
118 | - * |
|
119 | - * @param string $postFix Postfix appended to the temporary folder name |
|
120 | - * @return string |
|
121 | - */ |
|
122 | - public function getTemporaryFolder($postFix = '') { |
|
123 | - if (is_writable($this->tmpBaseDir)) { |
|
124 | - // To create an unique directory and prevent the risk of race conditions |
|
125 | - // or duplicated temporary files by other means such as collisions |
|
126 | - // we need to create the file using `tempnam` and append a possible |
|
127 | - // postfix to it later |
|
128 | - $uniqueFileName = tempnam($this->tmpBaseDir, self::TMP_PREFIX); |
|
129 | - $this->current[] = $uniqueFileName; |
|
116 | + /** |
|
117 | + * Create a temporary folder and return the path |
|
118 | + * |
|
119 | + * @param string $postFix Postfix appended to the temporary folder name |
|
120 | + * @return string |
|
121 | + */ |
|
122 | + public function getTemporaryFolder($postFix = '') { |
|
123 | + if (is_writable($this->tmpBaseDir)) { |
|
124 | + // To create an unique directory and prevent the risk of race conditions |
|
125 | + // or duplicated temporary files by other means such as collisions |
|
126 | + // we need to create the file using `tempnam` and append a possible |
|
127 | + // postfix to it later |
|
128 | + $uniqueFileName = tempnam($this->tmpBaseDir, self::TMP_PREFIX); |
|
129 | + $this->current[] = $uniqueFileName; |
|
130 | 130 | |
131 | - // Build a name without postfix |
|
132 | - $path = $this->buildFileNameWithSuffix($uniqueFileName . '-folder', $postFix); |
|
133 | - mkdir($path, 0700); |
|
134 | - $this->current[] = $path; |
|
131 | + // Build a name without postfix |
|
132 | + $path = $this->buildFileNameWithSuffix($uniqueFileName . '-folder', $postFix); |
|
133 | + mkdir($path, 0700); |
|
134 | + $this->current[] = $path; |
|
135 | 135 | |
136 | - return $path . '/'; |
|
137 | - } else { |
|
138 | - $this->log->warning( |
|
139 | - 'Can not create a temporary folder in directory {dir}. Check it exists and has correct permissions', |
|
140 | - [ |
|
141 | - 'dir' => $this->tmpBaseDir, |
|
142 | - ] |
|
143 | - ); |
|
144 | - return false; |
|
145 | - } |
|
146 | - } |
|
136 | + return $path . '/'; |
|
137 | + } else { |
|
138 | + $this->log->warning( |
|
139 | + 'Can not create a temporary folder in directory {dir}. Check it exists and has correct permissions', |
|
140 | + [ |
|
141 | + 'dir' => $this->tmpBaseDir, |
|
142 | + ] |
|
143 | + ); |
|
144 | + return false; |
|
145 | + } |
|
146 | + } |
|
147 | 147 | |
148 | - /** |
|
149 | - * Remove the temporary files and folders generated during this request |
|
150 | - */ |
|
151 | - public function clean() { |
|
152 | - $this->cleanFiles($this->current); |
|
153 | - } |
|
148 | + /** |
|
149 | + * Remove the temporary files and folders generated during this request |
|
150 | + */ |
|
151 | + public function clean() { |
|
152 | + $this->cleanFiles($this->current); |
|
153 | + } |
|
154 | 154 | |
155 | - /** |
|
156 | - * @param string[] $files |
|
157 | - */ |
|
158 | - protected function cleanFiles($files) { |
|
159 | - foreach ($files as $file) { |
|
160 | - if (file_exists($file)) { |
|
161 | - try { |
|
162 | - \OC_Helper::rmdirr($file); |
|
163 | - } catch (\UnexpectedValueException $ex) { |
|
164 | - $this->log->warning( |
|
165 | - "Error deleting temporary file/folder: {file} - Reason: {error}", |
|
166 | - [ |
|
167 | - 'file' => $file, |
|
168 | - 'error' => $ex->getMessage(), |
|
169 | - ] |
|
170 | - ); |
|
171 | - } |
|
172 | - } |
|
173 | - } |
|
174 | - } |
|
155 | + /** |
|
156 | + * @param string[] $files |
|
157 | + */ |
|
158 | + protected function cleanFiles($files) { |
|
159 | + foreach ($files as $file) { |
|
160 | + if (file_exists($file)) { |
|
161 | + try { |
|
162 | + \OC_Helper::rmdirr($file); |
|
163 | + } catch (\UnexpectedValueException $ex) { |
|
164 | + $this->log->warning( |
|
165 | + "Error deleting temporary file/folder: {file} - Reason: {error}", |
|
166 | + [ |
|
167 | + 'file' => $file, |
|
168 | + 'error' => $ex->getMessage(), |
|
169 | + ] |
|
170 | + ); |
|
171 | + } |
|
172 | + } |
|
173 | + } |
|
174 | + } |
|
175 | 175 | |
176 | - /** |
|
177 | - * Remove old temporary files and folders that were failed to be cleaned |
|
178 | - */ |
|
179 | - public function cleanOld() { |
|
180 | - $this->cleanFiles($this->getOldFiles()); |
|
181 | - } |
|
176 | + /** |
|
177 | + * Remove old temporary files and folders that were failed to be cleaned |
|
178 | + */ |
|
179 | + public function cleanOld() { |
|
180 | + $this->cleanFiles($this->getOldFiles()); |
|
181 | + } |
|
182 | 182 | |
183 | - /** |
|
184 | - * Get all temporary files and folders generated by oc older than an hour |
|
185 | - * |
|
186 | - * @return string[] |
|
187 | - */ |
|
188 | - protected function getOldFiles() { |
|
189 | - $cutOfTime = time() - 3600; |
|
190 | - $files = []; |
|
191 | - $dh = opendir($this->tmpBaseDir); |
|
192 | - if ($dh) { |
|
193 | - while (($file = readdir($dh)) !== false) { |
|
194 | - if (substr($file, 0, 7) === self::TMP_PREFIX) { |
|
195 | - $path = $this->tmpBaseDir . '/' . $file; |
|
196 | - $mtime = filemtime($path); |
|
197 | - if ($mtime < $cutOfTime) { |
|
198 | - $files[] = $path; |
|
199 | - } |
|
200 | - } |
|
201 | - } |
|
202 | - } |
|
203 | - return $files; |
|
204 | - } |
|
183 | + /** |
|
184 | + * Get all temporary files and folders generated by oc older than an hour |
|
185 | + * |
|
186 | + * @return string[] |
|
187 | + */ |
|
188 | + protected function getOldFiles() { |
|
189 | + $cutOfTime = time() - 3600; |
|
190 | + $files = []; |
|
191 | + $dh = opendir($this->tmpBaseDir); |
|
192 | + if ($dh) { |
|
193 | + while (($file = readdir($dh)) !== false) { |
|
194 | + if (substr($file, 0, 7) === self::TMP_PREFIX) { |
|
195 | + $path = $this->tmpBaseDir . '/' . $file; |
|
196 | + $mtime = filemtime($path); |
|
197 | + if ($mtime < $cutOfTime) { |
|
198 | + $files[] = $path; |
|
199 | + } |
|
200 | + } |
|
201 | + } |
|
202 | + } |
|
203 | + return $files; |
|
204 | + } |
|
205 | 205 | |
206 | - /** |
|
207 | - * Get the temporary base directory configured on the server |
|
208 | - * |
|
209 | - * @return string Path to the temporary directory or null |
|
210 | - * @throws \UnexpectedValueException |
|
211 | - */ |
|
212 | - public function getTempBaseDir() { |
|
213 | - if ($this->tmpBaseDir) { |
|
214 | - return $this->tmpBaseDir; |
|
215 | - } |
|
206 | + /** |
|
207 | + * Get the temporary base directory configured on the server |
|
208 | + * |
|
209 | + * @return string Path to the temporary directory or null |
|
210 | + * @throws \UnexpectedValueException |
|
211 | + */ |
|
212 | + public function getTempBaseDir() { |
|
213 | + if ($this->tmpBaseDir) { |
|
214 | + return $this->tmpBaseDir; |
|
215 | + } |
|
216 | 216 | |
217 | - $directories = []; |
|
218 | - if ($temp = $this->config->getSystemValue('tempdirectory', null)) { |
|
219 | - $directories[] = $temp; |
|
220 | - } |
|
221 | - if ($temp = $this->iniGetWrapper->get('upload_tmp_dir')) { |
|
222 | - $directories[] = $temp; |
|
223 | - } |
|
224 | - if ($temp = getenv('TMP')) { |
|
225 | - $directories[] = $temp; |
|
226 | - } |
|
227 | - if ($temp = getenv('TEMP')) { |
|
228 | - $directories[] = $temp; |
|
229 | - } |
|
230 | - if ($temp = getenv('TMPDIR')) { |
|
231 | - $directories[] = $temp; |
|
232 | - } |
|
233 | - if ($temp = sys_get_temp_dir()) { |
|
234 | - $directories[] = $temp; |
|
235 | - } |
|
217 | + $directories = []; |
|
218 | + if ($temp = $this->config->getSystemValue('tempdirectory', null)) { |
|
219 | + $directories[] = $temp; |
|
220 | + } |
|
221 | + if ($temp = $this->iniGetWrapper->get('upload_tmp_dir')) { |
|
222 | + $directories[] = $temp; |
|
223 | + } |
|
224 | + if ($temp = getenv('TMP')) { |
|
225 | + $directories[] = $temp; |
|
226 | + } |
|
227 | + if ($temp = getenv('TEMP')) { |
|
228 | + $directories[] = $temp; |
|
229 | + } |
|
230 | + if ($temp = getenv('TMPDIR')) { |
|
231 | + $directories[] = $temp; |
|
232 | + } |
|
233 | + if ($temp = sys_get_temp_dir()) { |
|
234 | + $directories[] = $temp; |
|
235 | + } |
|
236 | 236 | |
237 | - foreach ($directories as $dir) { |
|
238 | - if ($this->checkTemporaryDirectory($dir)) { |
|
239 | - return $dir; |
|
240 | - } |
|
241 | - } |
|
237 | + foreach ($directories as $dir) { |
|
238 | + if ($this->checkTemporaryDirectory($dir)) { |
|
239 | + return $dir; |
|
240 | + } |
|
241 | + } |
|
242 | 242 | |
243 | - $temp = tempnam(dirname(__FILE__), ''); |
|
244 | - if (file_exists($temp)) { |
|
245 | - unlink($temp); |
|
246 | - return dirname($temp); |
|
247 | - } |
|
248 | - throw new \UnexpectedValueException('Unable to detect system temporary directory'); |
|
249 | - } |
|
243 | + $temp = tempnam(dirname(__FILE__), ''); |
|
244 | + if (file_exists($temp)) { |
|
245 | + unlink($temp); |
|
246 | + return dirname($temp); |
|
247 | + } |
|
248 | + throw new \UnexpectedValueException('Unable to detect system temporary directory'); |
|
249 | + } |
|
250 | 250 | |
251 | - /** |
|
252 | - * Check if a temporary directory is ready for use |
|
253 | - * |
|
254 | - * @param mixed $directory |
|
255 | - * @return bool |
|
256 | - */ |
|
257 | - private function checkTemporaryDirectory($directory) { |
|
258 | - // suppress any possible errors caused by is_writable |
|
259 | - // checks missing or invalid path or characters, wrong permissions etc |
|
260 | - try { |
|
261 | - if (is_writable($directory)) { |
|
262 | - return true; |
|
263 | - } |
|
264 | - } catch (\Exception $e) { |
|
265 | - } |
|
266 | - $this->log->warning('Temporary directory {dir} is not present or writable', |
|
267 | - ['dir' => $directory] |
|
268 | - ); |
|
269 | - return false; |
|
270 | - } |
|
251 | + /** |
|
252 | + * Check if a temporary directory is ready for use |
|
253 | + * |
|
254 | + * @param mixed $directory |
|
255 | + * @return bool |
|
256 | + */ |
|
257 | + private function checkTemporaryDirectory($directory) { |
|
258 | + // suppress any possible errors caused by is_writable |
|
259 | + // checks missing or invalid path or characters, wrong permissions etc |
|
260 | + try { |
|
261 | + if (is_writable($directory)) { |
|
262 | + return true; |
|
263 | + } |
|
264 | + } catch (\Exception $e) { |
|
265 | + } |
|
266 | + $this->log->warning('Temporary directory {dir} is not present or writable', |
|
267 | + ['dir' => $directory] |
|
268 | + ); |
|
269 | + return false; |
|
270 | + } |
|
271 | 271 | |
272 | - /** |
|
273 | - * Override the temporary base directory |
|
274 | - * |
|
275 | - * @param string $directory |
|
276 | - */ |
|
277 | - public function overrideTempBaseDir($directory) { |
|
278 | - $this->tmpBaseDir = $directory; |
|
279 | - } |
|
272 | + /** |
|
273 | + * Override the temporary base directory |
|
274 | + * |
|
275 | + * @param string $directory |
|
276 | + */ |
|
277 | + public function overrideTempBaseDir($directory) { |
|
278 | + $this->tmpBaseDir = $directory; |
|
279 | + } |
|
280 | 280 | } |
@@ -36,54 +36,54 @@ |
||
36 | 36 | * @method void setName(string $name) |
37 | 37 | */ |
38 | 38 | class Tag extends Entity { |
39 | - protected $owner; |
|
40 | - protected $type; |
|
41 | - protected $name; |
|
39 | + protected $owner; |
|
40 | + protected $type; |
|
41 | + protected $name; |
|
42 | 42 | |
43 | - /** |
|
44 | - * Constructor. |
|
45 | - * |
|
46 | - * @param string $owner The tag's owner |
|
47 | - * @param string $type The type of item this tag is used for |
|
48 | - * @param string $name The tag's name |
|
49 | - */ |
|
50 | - public function __construct($owner = null, $type = null, $name = null) { |
|
51 | - $this->setOwner($owner); |
|
52 | - $this->setType($type); |
|
53 | - $this->setName($name); |
|
54 | - } |
|
43 | + /** |
|
44 | + * Constructor. |
|
45 | + * |
|
46 | + * @param string $owner The tag's owner |
|
47 | + * @param string $type The type of item this tag is used for |
|
48 | + * @param string $name The tag's name |
|
49 | + */ |
|
50 | + public function __construct($owner = null, $type = null, $name = null) { |
|
51 | + $this->setOwner($owner); |
|
52 | + $this->setType($type); |
|
53 | + $this->setName($name); |
|
54 | + } |
|
55 | 55 | |
56 | - /** |
|
57 | - * Transform a database columnname to a property |
|
58 | - * |
|
59 | - * @param string $columnName the name of the column |
|
60 | - * @return string the property name |
|
61 | - * @todo migrate existing database columns to the correct names |
|
62 | - * to be able to drop this direct mapping |
|
63 | - */ |
|
64 | - public function columnToProperty($columnName) { |
|
65 | - if ($columnName === 'category') { |
|
66 | - return 'name'; |
|
67 | - } elseif ($columnName === 'uid') { |
|
68 | - return 'owner'; |
|
69 | - } else { |
|
70 | - return parent::columnToProperty($columnName); |
|
71 | - } |
|
72 | - } |
|
56 | + /** |
|
57 | + * Transform a database columnname to a property |
|
58 | + * |
|
59 | + * @param string $columnName the name of the column |
|
60 | + * @return string the property name |
|
61 | + * @todo migrate existing database columns to the correct names |
|
62 | + * to be able to drop this direct mapping |
|
63 | + */ |
|
64 | + public function columnToProperty($columnName) { |
|
65 | + if ($columnName === 'category') { |
|
66 | + return 'name'; |
|
67 | + } elseif ($columnName === 'uid') { |
|
68 | + return 'owner'; |
|
69 | + } else { |
|
70 | + return parent::columnToProperty($columnName); |
|
71 | + } |
|
72 | + } |
|
73 | 73 | |
74 | - /** |
|
75 | - * Transform a property to a database column name |
|
76 | - * |
|
77 | - * @param string $property the name of the property |
|
78 | - * @return string the column name |
|
79 | - */ |
|
80 | - public function propertyToColumn($property) { |
|
81 | - if ($property === 'name') { |
|
82 | - return 'category'; |
|
83 | - } elseif ($property === 'owner') { |
|
84 | - return 'uid'; |
|
85 | - } else { |
|
86 | - return parent::propertyToColumn($property); |
|
87 | - } |
|
88 | - } |
|
74 | + /** |
|
75 | + * Transform a property to a database column name |
|
76 | + * |
|
77 | + * @param string $property the name of the property |
|
78 | + * @return string the column name |
|
79 | + */ |
|
80 | + public function propertyToColumn($property) { |
|
81 | + if ($property === 'name') { |
|
82 | + return 'category'; |
|
83 | + } elseif ($property === 'owner') { |
|
84 | + return 'uid'; |
|
85 | + } else { |
|
86 | + return parent::propertyToColumn($property); |
|
87 | + } |
|
88 | + } |
|
89 | 89 | } |
@@ -54,8 +54,8 @@ discard block |
||
54 | 54 | $owners = [$owners]; |
55 | 55 | } |
56 | 56 | |
57 | - $sql = 'SELECT `id`, `uid`, `type`, `category` FROM `' . $this->getTableName() . '` ' |
|
58 | - . 'WHERE `uid` IN (' . str_repeat('?,', count($owners) - 1) . '?) AND `type` = ? ORDER BY `category`'; |
|
57 | + $sql = 'SELECT `id`, `uid`, `type`, `category` FROM `'.$this->getTableName().'` ' |
|
58 | + . 'WHERE `uid` IN ('.str_repeat('?,', count($owners) - 1).'?) AND `type` = ? ORDER BY `category`'; |
|
59 | 59 | return $this->findEntities($sql, array_merge($owners, [$type])); |
60 | 60 | } |
61 | 61 | |
@@ -66,7 +66,7 @@ discard block |
||
66 | 66 | * @return bool |
67 | 67 | */ |
68 | 68 | public function tagExists($tag) { |
69 | - $sql = 'SELECT `id`, `uid`, `type`, `category` FROM `' . $this->getTableName() . '` ' |
|
69 | + $sql = 'SELECT `id`, `uid`, `type`, `category` FROM `'.$this->getTableName().'` ' |
|
70 | 70 | . 'WHERE `uid` = ? AND `type` = ? AND `category` = ?'; |
71 | 71 | try { |
72 | 72 | $this->findEntity($sql, [$tag->getOwner(), $tag->getType(), $tag->getName()]); |
@@ -35,46 +35,46 @@ |
||
35 | 35 | */ |
36 | 36 | class TagMapper extends Mapper { |
37 | 37 | |
38 | - /** |
|
39 | - * Constructor. |
|
40 | - * |
|
41 | - * @param IDBConnection $db Instance of the Db abstraction layer. |
|
42 | - */ |
|
43 | - public function __construct(IDBConnection $db) { |
|
44 | - parent::__construct($db, 'vcategory', Tag::class); |
|
45 | - } |
|
38 | + /** |
|
39 | + * Constructor. |
|
40 | + * |
|
41 | + * @param IDBConnection $db Instance of the Db abstraction layer. |
|
42 | + */ |
|
43 | + public function __construct(IDBConnection $db) { |
|
44 | + parent::__construct($db, 'vcategory', Tag::class); |
|
45 | + } |
|
46 | 46 | |
47 | - /** |
|
48 | - * Load tags from the database. |
|
49 | - * |
|
50 | - * @param array|string $owners The user(s) whose tags we are going to load. |
|
51 | - * @param string $type The type of item for which we are loading tags. |
|
52 | - * @return array An array of Tag objects. |
|
53 | - */ |
|
54 | - public function loadTags($owners, $type) { |
|
55 | - if (!is_array($owners)) { |
|
56 | - $owners = [$owners]; |
|
57 | - } |
|
47 | + /** |
|
48 | + * Load tags from the database. |
|
49 | + * |
|
50 | + * @param array|string $owners The user(s) whose tags we are going to load. |
|
51 | + * @param string $type The type of item for which we are loading tags. |
|
52 | + * @return array An array of Tag objects. |
|
53 | + */ |
|
54 | + public function loadTags($owners, $type) { |
|
55 | + if (!is_array($owners)) { |
|
56 | + $owners = [$owners]; |
|
57 | + } |
|
58 | 58 | |
59 | - $sql = 'SELECT `id`, `uid`, `type`, `category` FROM `' . $this->getTableName() . '` ' |
|
60 | - . 'WHERE `uid` IN (' . str_repeat('?,', count($owners) - 1) . '?) AND `type` = ? ORDER BY `category`'; |
|
61 | - return $this->findEntities($sql, array_merge($owners, [$type])); |
|
62 | - } |
|
59 | + $sql = 'SELECT `id`, `uid`, `type`, `category` FROM `' . $this->getTableName() . '` ' |
|
60 | + . 'WHERE `uid` IN (' . str_repeat('?,', count($owners) - 1) . '?) AND `type` = ? ORDER BY `category`'; |
|
61 | + return $this->findEntities($sql, array_merge($owners, [$type])); |
|
62 | + } |
|
63 | 63 | |
64 | - /** |
|
65 | - * Check if a given Tag object already exists in the database. |
|
66 | - * |
|
67 | - * @param Tag $tag The tag to look for in the database. |
|
68 | - * @return bool |
|
69 | - */ |
|
70 | - public function tagExists($tag) { |
|
71 | - $sql = 'SELECT `id`, `uid`, `type`, `category` FROM `' . $this->getTableName() . '` ' |
|
72 | - . 'WHERE `uid` = ? AND `type` = ? AND `category` = ?'; |
|
73 | - try { |
|
74 | - $this->findEntity($sql, [$tag->getOwner(), $tag->getType(), $tag->getName()]); |
|
75 | - } catch (DoesNotExistException $e) { |
|
76 | - return false; |
|
77 | - } |
|
78 | - return true; |
|
79 | - } |
|
64 | + /** |
|
65 | + * Check if a given Tag object already exists in the database. |
|
66 | + * |
|
67 | + * @param Tag $tag The tag to look for in the database. |
|
68 | + * @return bool |
|
69 | + */ |
|
70 | + public function tagExists($tag) { |
|
71 | + $sql = 'SELECT `id`, `uid`, `type`, `category` FROM `' . $this->getTableName() . '` ' |
|
72 | + . 'WHERE `uid` = ? AND `type` = ? AND `category` = ?'; |
|
73 | + try { |
|
74 | + $this->findEntity($sql, [$tag->getOwner(), $tag->getType(), $tag->getName()]); |
|
75 | + } catch (DoesNotExistException $e) { |
|
76 | + return false; |
|
77 | + } |
|
78 | + return true; |
|
79 | + } |
|
80 | 80 | } |
@@ -32,7 +32,7 @@ |
||
32 | 32 | * @package OC\AppFramework\Middleware\Security\Exceptions |
33 | 33 | */ |
34 | 34 | class StrictCookieMissingException extends SecurityException { |
35 | - public function __construct() { |
|
36 | - parent::__construct('Strict Cookie has not been found in request.', Http::STATUS_PRECONDITION_FAILED); |
|
37 | - } |
|
35 | + public function __construct() { |
|
36 | + parent::__construct('Strict Cookie has not been found in request.', Http::STATUS_PRECONDITION_FAILED); |
|
37 | + } |
|
38 | 38 | } |
@@ -70,8 +70,8 @@ |
||
70 | 70 | 'message' => $this->getOCSStatus() === 100 ? 'OK' : $this->statusMessage, |
71 | 71 | ]; |
72 | 72 | |
73 | - $meta['totalitems'] = $this->itemsCount !== null ? (string)$this->itemsCount : ''; |
|
74 | - $meta['itemsperpage'] = $this->itemsPerPage !== null ? (string)$this->itemsPerPage: ''; |
|
73 | + $meta['totalitems'] = $this->itemsCount !== null ? (string) $this->itemsCount : ''; |
|
74 | + $meta['itemsperpage'] = $this->itemsPerPage !== null ? (string) $this->itemsPerPage : ''; |
|
75 | 75 | |
76 | 76 | return $this->renderResult($meta); |
77 | 77 | } |
@@ -29,52 +29,52 @@ |
||
29 | 29 | |
30 | 30 | class V1Response extends BaseResponse { |
31 | 31 | |
32 | - /** |
|
33 | - * The V1 endpoint has very limited http status codes basically everything |
|
34 | - * is status 200 except 401 |
|
35 | - * |
|
36 | - * @return int |
|
37 | - */ |
|
38 | - public function getStatus() { |
|
39 | - $status = parent::getStatus(); |
|
40 | - if ($status === Http::STATUS_FORBIDDEN || $status === OCSController::RESPOND_UNAUTHORISED) { |
|
41 | - return Http::STATUS_UNAUTHORIZED; |
|
42 | - } |
|
32 | + /** |
|
33 | + * The V1 endpoint has very limited http status codes basically everything |
|
34 | + * is status 200 except 401 |
|
35 | + * |
|
36 | + * @return int |
|
37 | + */ |
|
38 | + public function getStatus() { |
|
39 | + $status = parent::getStatus(); |
|
40 | + if ($status === Http::STATUS_FORBIDDEN || $status === OCSController::RESPOND_UNAUTHORISED) { |
|
41 | + return Http::STATUS_UNAUTHORIZED; |
|
42 | + } |
|
43 | 43 | |
44 | - return Http::STATUS_OK; |
|
45 | - } |
|
44 | + return Http::STATUS_OK; |
|
45 | + } |
|
46 | 46 | |
47 | - /** |
|
48 | - * In v1 all OK is 100 |
|
49 | - * |
|
50 | - * @return int |
|
51 | - */ |
|
52 | - public function getOCSStatus() { |
|
53 | - $status = parent::getOCSStatus(); |
|
47 | + /** |
|
48 | + * In v1 all OK is 100 |
|
49 | + * |
|
50 | + * @return int |
|
51 | + */ |
|
52 | + public function getOCSStatus() { |
|
53 | + $status = parent::getOCSStatus(); |
|
54 | 54 | |
55 | - if ($status === Http::STATUS_OK) { |
|
56 | - return 100; |
|
57 | - } |
|
55 | + if ($status === Http::STATUS_OK) { |
|
56 | + return 100; |
|
57 | + } |
|
58 | 58 | |
59 | - return $status; |
|
60 | - } |
|
59 | + return $status; |
|
60 | + } |
|
61 | 61 | |
62 | - /** |
|
63 | - * Construct the meta part of the response |
|
64 | - * And then late the base class render |
|
65 | - * |
|
66 | - * @return string |
|
67 | - */ |
|
68 | - public function render() { |
|
69 | - $meta = [ |
|
70 | - 'status' => $this->getOCSStatus() === 100 ? 'ok' : 'failure', |
|
71 | - 'statuscode' => $this->getOCSStatus(), |
|
72 | - 'message' => $this->getOCSStatus() === 100 ? 'OK' : $this->statusMessage, |
|
73 | - ]; |
|
62 | + /** |
|
63 | + * Construct the meta part of the response |
|
64 | + * And then late the base class render |
|
65 | + * |
|
66 | + * @return string |
|
67 | + */ |
|
68 | + public function render() { |
|
69 | + $meta = [ |
|
70 | + 'status' => $this->getOCSStatus() === 100 ? 'ok' : 'failure', |
|
71 | + 'statuscode' => $this->getOCSStatus(), |
|
72 | + 'message' => $this->getOCSStatus() === 100 ? 'OK' : $this->statusMessage, |
|
73 | + ]; |
|
74 | 74 | |
75 | - $meta['totalitems'] = $this->itemsCount !== null ? (string)$this->itemsCount : ''; |
|
76 | - $meta['itemsperpage'] = $this->itemsPerPage !== null ? (string)$this->itemsPerPage: ''; |
|
75 | + $meta['totalitems'] = $this->itemsCount !== null ? (string)$this->itemsCount : ''; |
|
76 | + $meta['itemsperpage'] = $this->itemsPerPage !== null ? (string)$this->itemsPerPage: ''; |
|
77 | 77 | |
78 | - return $this->renderResult($meta); |
|
79 | - } |
|
78 | + return $this->renderResult($meta); |
|
79 | + } |
|
80 | 80 | } |
@@ -147,7 +147,7 @@ |
||
147 | 147 | if (isset($this->entityTypeCollections[$name])) { |
148 | 148 | return $this->entityTypeCollections[$name]; |
149 | 149 | } |
150 | - throw new NotFound('Entity type "' . $name . '" not found."'); |
|
150 | + throw new NotFound('Entity type "'.$name.'" not found."'); |
|
151 | 151 | } |
152 | 152 | |
153 | 153 | /** |
@@ -37,176 +37,176 @@ |
||
37 | 37 | |
38 | 38 | class RootCollection implements ICollection { |
39 | 39 | |
40 | - /** @var EntityTypeCollection[]|null */ |
|
41 | - private $entityTypeCollections; |
|
42 | - |
|
43 | - /** @var ICommentsManager */ |
|
44 | - protected $commentsManager; |
|
45 | - |
|
46 | - /** @var string */ |
|
47 | - protected $name = 'comments'; |
|
48 | - |
|
49 | - /** @var ILogger */ |
|
50 | - protected $logger; |
|
51 | - |
|
52 | - /** @var IUserManager */ |
|
53 | - protected $userManager; |
|
54 | - |
|
55 | - /** @var IUserSession */ |
|
56 | - protected $userSession; |
|
57 | - |
|
58 | - /** @var EventDispatcherInterface */ |
|
59 | - protected $dispatcher; |
|
60 | - |
|
61 | - /** |
|
62 | - * @param ICommentsManager $commentsManager |
|
63 | - * @param IUserManager $userManager |
|
64 | - * @param IUserSession $userSession |
|
65 | - * @param EventDispatcherInterface $dispatcher |
|
66 | - * @param ILogger $logger |
|
67 | - */ |
|
68 | - public function __construct( |
|
69 | - ICommentsManager $commentsManager, |
|
70 | - IUserManager $userManager, |
|
71 | - IUserSession $userSession, |
|
72 | - EventDispatcherInterface $dispatcher, |
|
73 | - ILogger $logger) { |
|
74 | - $this->commentsManager = $commentsManager; |
|
75 | - $this->logger = $logger; |
|
76 | - $this->userManager = $userManager; |
|
77 | - $this->userSession = $userSession; |
|
78 | - $this->dispatcher = $dispatcher; |
|
79 | - } |
|
80 | - |
|
81 | - /** |
|
82 | - * initializes the collection. At this point of time, we need the logged in |
|
83 | - * user. Since it is not the case when the instance is created, we cannot |
|
84 | - * have this in the constructor. |
|
85 | - * |
|
86 | - * @throws NotAuthenticated |
|
87 | - */ |
|
88 | - protected function initCollections() { |
|
89 | - if ($this->entityTypeCollections !== null) { |
|
90 | - return; |
|
91 | - } |
|
92 | - $user = $this->userSession->getUser(); |
|
93 | - if (is_null($user)) { |
|
94 | - throw new NotAuthenticated(); |
|
95 | - } |
|
96 | - |
|
97 | - $event = new CommentsEntityEvent(CommentsEntityEvent::EVENT_ENTITY); |
|
98 | - $this->dispatcher->dispatch(CommentsEntityEvent::EVENT_ENTITY, $event); |
|
99 | - |
|
100 | - $this->entityTypeCollections = []; |
|
101 | - foreach ($event->getEntityCollections() as $entity => $entityExistsFunction) { |
|
102 | - $this->entityTypeCollections[$entity] = new EntityTypeCollection( |
|
103 | - $entity, |
|
104 | - $this->commentsManager, |
|
105 | - $this->userManager, |
|
106 | - $this->userSession, |
|
107 | - $this->logger, |
|
108 | - $entityExistsFunction |
|
109 | - ); |
|
110 | - } |
|
111 | - } |
|
112 | - |
|
113 | - /** |
|
114 | - * Creates a new file in the directory |
|
115 | - * |
|
116 | - * @param string $name Name of the file |
|
117 | - * @param resource|string $data Initial payload |
|
118 | - * @return null|string |
|
119 | - * @throws Forbidden |
|
120 | - */ |
|
121 | - public function createFile($name, $data = null) { |
|
122 | - throw new Forbidden('Cannot create comments by id'); |
|
123 | - } |
|
124 | - |
|
125 | - /** |
|
126 | - * Creates a new subdirectory |
|
127 | - * |
|
128 | - * @param string $name |
|
129 | - * @throws Forbidden |
|
130 | - */ |
|
131 | - public function createDirectory($name) { |
|
132 | - throw new Forbidden('Permission denied to create collections'); |
|
133 | - } |
|
134 | - |
|
135 | - /** |
|
136 | - * Returns a specific child node, referenced by its name |
|
137 | - * |
|
138 | - * This method must throw Sabre\DAV\Exception\NotFound if the node does not |
|
139 | - * exist. |
|
140 | - * |
|
141 | - * @param string $name |
|
142 | - * @return \Sabre\DAV\INode |
|
143 | - * @throws NotFound |
|
144 | - */ |
|
145 | - public function getChild($name) { |
|
146 | - $this->initCollections(); |
|
147 | - if (isset($this->entityTypeCollections[$name])) { |
|
148 | - return $this->entityTypeCollections[$name]; |
|
149 | - } |
|
150 | - throw new NotFound('Entity type "' . $name . '" not found."'); |
|
151 | - } |
|
152 | - |
|
153 | - /** |
|
154 | - * Returns an array with all the child nodes |
|
155 | - * |
|
156 | - * @return \Sabre\DAV\INode[] |
|
157 | - */ |
|
158 | - public function getChildren() { |
|
159 | - $this->initCollections(); |
|
160 | - return $this->entityTypeCollections; |
|
161 | - } |
|
162 | - |
|
163 | - /** |
|
164 | - * Checks if a child-node with the specified name exists |
|
165 | - * |
|
166 | - * @param string $name |
|
167 | - * @return bool |
|
168 | - */ |
|
169 | - public function childExists($name) { |
|
170 | - $this->initCollections(); |
|
171 | - return isset($this->entityTypeCollections[$name]); |
|
172 | - } |
|
173 | - |
|
174 | - /** |
|
175 | - * Deleted the current node |
|
176 | - * |
|
177 | - * @throws Forbidden |
|
178 | - */ |
|
179 | - public function delete() { |
|
180 | - throw new Forbidden('Permission denied to delete this collection'); |
|
181 | - } |
|
182 | - |
|
183 | - /** |
|
184 | - * Returns the name of the node. |
|
185 | - * |
|
186 | - * This is used to generate the url. |
|
187 | - * |
|
188 | - * @return string |
|
189 | - */ |
|
190 | - public function getName() { |
|
191 | - return $this->name; |
|
192 | - } |
|
193 | - |
|
194 | - /** |
|
195 | - * Renames the node |
|
196 | - * |
|
197 | - * @param string $name The new name |
|
198 | - * @throws Forbidden |
|
199 | - */ |
|
200 | - public function setName($name) { |
|
201 | - throw new Forbidden('Permission denied to rename this collection'); |
|
202 | - } |
|
203 | - |
|
204 | - /** |
|
205 | - * Returns the last modification time, as a unix timestamp |
|
206 | - * |
|
207 | - * @return int |
|
208 | - */ |
|
209 | - public function getLastModified() { |
|
210 | - return null; |
|
211 | - } |
|
40 | + /** @var EntityTypeCollection[]|null */ |
|
41 | + private $entityTypeCollections; |
|
42 | + |
|
43 | + /** @var ICommentsManager */ |
|
44 | + protected $commentsManager; |
|
45 | + |
|
46 | + /** @var string */ |
|
47 | + protected $name = 'comments'; |
|
48 | + |
|
49 | + /** @var ILogger */ |
|
50 | + protected $logger; |
|
51 | + |
|
52 | + /** @var IUserManager */ |
|
53 | + protected $userManager; |
|
54 | + |
|
55 | + /** @var IUserSession */ |
|
56 | + protected $userSession; |
|
57 | + |
|
58 | + /** @var EventDispatcherInterface */ |
|
59 | + protected $dispatcher; |
|
60 | + |
|
61 | + /** |
|
62 | + * @param ICommentsManager $commentsManager |
|
63 | + * @param IUserManager $userManager |
|
64 | + * @param IUserSession $userSession |
|
65 | + * @param EventDispatcherInterface $dispatcher |
|
66 | + * @param ILogger $logger |
|
67 | + */ |
|
68 | + public function __construct( |
|
69 | + ICommentsManager $commentsManager, |
|
70 | + IUserManager $userManager, |
|
71 | + IUserSession $userSession, |
|
72 | + EventDispatcherInterface $dispatcher, |
|
73 | + ILogger $logger) { |
|
74 | + $this->commentsManager = $commentsManager; |
|
75 | + $this->logger = $logger; |
|
76 | + $this->userManager = $userManager; |
|
77 | + $this->userSession = $userSession; |
|
78 | + $this->dispatcher = $dispatcher; |
|
79 | + } |
|
80 | + |
|
81 | + /** |
|
82 | + * initializes the collection. At this point of time, we need the logged in |
|
83 | + * user. Since it is not the case when the instance is created, we cannot |
|
84 | + * have this in the constructor. |
|
85 | + * |
|
86 | + * @throws NotAuthenticated |
|
87 | + */ |
|
88 | + protected function initCollections() { |
|
89 | + if ($this->entityTypeCollections !== null) { |
|
90 | + return; |
|
91 | + } |
|
92 | + $user = $this->userSession->getUser(); |
|
93 | + if (is_null($user)) { |
|
94 | + throw new NotAuthenticated(); |
|
95 | + } |
|
96 | + |
|
97 | + $event = new CommentsEntityEvent(CommentsEntityEvent::EVENT_ENTITY); |
|
98 | + $this->dispatcher->dispatch(CommentsEntityEvent::EVENT_ENTITY, $event); |
|
99 | + |
|
100 | + $this->entityTypeCollections = []; |
|
101 | + foreach ($event->getEntityCollections() as $entity => $entityExistsFunction) { |
|
102 | + $this->entityTypeCollections[$entity] = new EntityTypeCollection( |
|
103 | + $entity, |
|
104 | + $this->commentsManager, |
|
105 | + $this->userManager, |
|
106 | + $this->userSession, |
|
107 | + $this->logger, |
|
108 | + $entityExistsFunction |
|
109 | + ); |
|
110 | + } |
|
111 | + } |
|
112 | + |
|
113 | + /** |
|
114 | + * Creates a new file in the directory |
|
115 | + * |
|
116 | + * @param string $name Name of the file |
|
117 | + * @param resource|string $data Initial payload |
|
118 | + * @return null|string |
|
119 | + * @throws Forbidden |
|
120 | + */ |
|
121 | + public function createFile($name, $data = null) { |
|
122 | + throw new Forbidden('Cannot create comments by id'); |
|
123 | + } |
|
124 | + |
|
125 | + /** |
|
126 | + * Creates a new subdirectory |
|
127 | + * |
|
128 | + * @param string $name |
|
129 | + * @throws Forbidden |
|
130 | + */ |
|
131 | + public function createDirectory($name) { |
|
132 | + throw new Forbidden('Permission denied to create collections'); |
|
133 | + } |
|
134 | + |
|
135 | + /** |
|
136 | + * Returns a specific child node, referenced by its name |
|
137 | + * |
|
138 | + * This method must throw Sabre\DAV\Exception\NotFound if the node does not |
|
139 | + * exist. |
|
140 | + * |
|
141 | + * @param string $name |
|
142 | + * @return \Sabre\DAV\INode |
|
143 | + * @throws NotFound |
|
144 | + */ |
|
145 | + public function getChild($name) { |
|
146 | + $this->initCollections(); |
|
147 | + if (isset($this->entityTypeCollections[$name])) { |
|
148 | + return $this->entityTypeCollections[$name]; |
|
149 | + } |
|
150 | + throw new NotFound('Entity type "' . $name . '" not found."'); |
|
151 | + } |
|
152 | + |
|
153 | + /** |
|
154 | + * Returns an array with all the child nodes |
|
155 | + * |
|
156 | + * @return \Sabre\DAV\INode[] |
|
157 | + */ |
|
158 | + public function getChildren() { |
|
159 | + $this->initCollections(); |
|
160 | + return $this->entityTypeCollections; |
|
161 | + } |
|
162 | + |
|
163 | + /** |
|
164 | + * Checks if a child-node with the specified name exists |
|
165 | + * |
|
166 | + * @param string $name |
|
167 | + * @return bool |
|
168 | + */ |
|
169 | + public function childExists($name) { |
|
170 | + $this->initCollections(); |
|
171 | + return isset($this->entityTypeCollections[$name]); |
|
172 | + } |
|
173 | + |
|
174 | + /** |
|
175 | + * Deleted the current node |
|
176 | + * |
|
177 | + * @throws Forbidden |
|
178 | + */ |
|
179 | + public function delete() { |
|
180 | + throw new Forbidden('Permission denied to delete this collection'); |
|
181 | + } |
|
182 | + |
|
183 | + /** |
|
184 | + * Returns the name of the node. |
|
185 | + * |
|
186 | + * This is used to generate the url. |
|
187 | + * |
|
188 | + * @return string |
|
189 | + */ |
|
190 | + public function getName() { |
|
191 | + return $this->name; |
|
192 | + } |
|
193 | + |
|
194 | + /** |
|
195 | + * Renames the node |
|
196 | + * |
|
197 | + * @param string $name The new name |
|
198 | + * @throws Forbidden |
|
199 | + */ |
|
200 | + public function setName($name) { |
|
201 | + throw new Forbidden('Permission denied to rename this collection'); |
|
202 | + } |
|
203 | + |
|
204 | + /** |
|
205 | + * Returns the last modification time, as a unix timestamp |
|
206 | + * |
|
207 | + * @return int |
|
208 | + */ |
|
209 | + public function getLastModified() { |
|
210 | + return null; |
|
211 | + } |
|
212 | 212 | } |
@@ -43,84 +43,84 @@ |
||
43 | 43 | */ |
44 | 44 | class EntityTypeCollection extends RootCollection { |
45 | 45 | |
46 | - /** @var ILogger */ |
|
47 | - protected $logger; |
|
46 | + /** @var ILogger */ |
|
47 | + protected $logger; |
|
48 | 48 | |
49 | - /** @var IUserManager */ |
|
50 | - protected $userManager; |
|
49 | + /** @var IUserManager */ |
|
50 | + protected $userManager; |
|
51 | 51 | |
52 | - /** @var \Closure */ |
|
53 | - protected $childExistsFunction; |
|
52 | + /** @var \Closure */ |
|
53 | + protected $childExistsFunction; |
|
54 | 54 | |
55 | - /** |
|
56 | - * @param string $name |
|
57 | - * @param ICommentsManager $commentsManager |
|
58 | - * @param IUserManager $userManager |
|
59 | - * @param IUserSession $userSession |
|
60 | - * @param ILogger $logger |
|
61 | - * @param \Closure $childExistsFunction |
|
62 | - */ |
|
63 | - public function __construct( |
|
64 | - $name, |
|
65 | - ICommentsManager $commentsManager, |
|
66 | - IUserManager $userManager, |
|
67 | - IUserSession $userSession, |
|
68 | - ILogger $logger, |
|
69 | - \Closure $childExistsFunction |
|
70 | - ) { |
|
71 | - $name = trim($name); |
|
72 | - if (empty($name) || !is_string($name)) { |
|
73 | - throw new \InvalidArgumentException('"name" parameter must be non-empty string'); |
|
74 | - } |
|
75 | - $this->name = $name; |
|
76 | - $this->commentsManager = $commentsManager; |
|
77 | - $this->logger = $logger; |
|
78 | - $this->userManager = $userManager; |
|
79 | - $this->userSession = $userSession; |
|
80 | - $this->childExistsFunction = $childExistsFunction; |
|
81 | - } |
|
55 | + /** |
|
56 | + * @param string $name |
|
57 | + * @param ICommentsManager $commentsManager |
|
58 | + * @param IUserManager $userManager |
|
59 | + * @param IUserSession $userSession |
|
60 | + * @param ILogger $logger |
|
61 | + * @param \Closure $childExistsFunction |
|
62 | + */ |
|
63 | + public function __construct( |
|
64 | + $name, |
|
65 | + ICommentsManager $commentsManager, |
|
66 | + IUserManager $userManager, |
|
67 | + IUserSession $userSession, |
|
68 | + ILogger $logger, |
|
69 | + \Closure $childExistsFunction |
|
70 | + ) { |
|
71 | + $name = trim($name); |
|
72 | + if (empty($name) || !is_string($name)) { |
|
73 | + throw new \InvalidArgumentException('"name" parameter must be non-empty string'); |
|
74 | + } |
|
75 | + $this->name = $name; |
|
76 | + $this->commentsManager = $commentsManager; |
|
77 | + $this->logger = $logger; |
|
78 | + $this->userManager = $userManager; |
|
79 | + $this->userSession = $userSession; |
|
80 | + $this->childExistsFunction = $childExistsFunction; |
|
81 | + } |
|
82 | 82 | |
83 | - /** |
|
84 | - * Returns a specific child node, referenced by its name |
|
85 | - * |
|
86 | - * This method must throw Sabre\DAV\Exception\NotFound if the node does not |
|
87 | - * exist. |
|
88 | - * |
|
89 | - * @param string $name |
|
90 | - * @return \Sabre\DAV\INode |
|
91 | - * @throws NotFound |
|
92 | - */ |
|
93 | - public function getChild($name) { |
|
94 | - if (!$this->childExists($name)) { |
|
95 | - throw new NotFound('Entity does not exist or is not available'); |
|
96 | - } |
|
97 | - return new EntityCollection( |
|
98 | - $name, |
|
99 | - $this->name, |
|
100 | - $this->commentsManager, |
|
101 | - $this->userManager, |
|
102 | - $this->userSession, |
|
103 | - $this->logger |
|
104 | - ); |
|
105 | - } |
|
83 | + /** |
|
84 | + * Returns a specific child node, referenced by its name |
|
85 | + * |
|
86 | + * This method must throw Sabre\DAV\Exception\NotFound if the node does not |
|
87 | + * exist. |
|
88 | + * |
|
89 | + * @param string $name |
|
90 | + * @return \Sabre\DAV\INode |
|
91 | + * @throws NotFound |
|
92 | + */ |
|
93 | + public function getChild($name) { |
|
94 | + if (!$this->childExists($name)) { |
|
95 | + throw new NotFound('Entity does not exist or is not available'); |
|
96 | + } |
|
97 | + return new EntityCollection( |
|
98 | + $name, |
|
99 | + $this->name, |
|
100 | + $this->commentsManager, |
|
101 | + $this->userManager, |
|
102 | + $this->userSession, |
|
103 | + $this->logger |
|
104 | + ); |
|
105 | + } |
|
106 | 106 | |
107 | - /** |
|
108 | - * Returns an array with all the child nodes |
|
109 | - * |
|
110 | - * @return \Sabre\DAV\INode[] |
|
111 | - * @throws MethodNotAllowed |
|
112 | - */ |
|
113 | - public function getChildren() { |
|
114 | - throw new MethodNotAllowed('No permission to list folder contents'); |
|
115 | - } |
|
107 | + /** |
|
108 | + * Returns an array with all the child nodes |
|
109 | + * |
|
110 | + * @return \Sabre\DAV\INode[] |
|
111 | + * @throws MethodNotAllowed |
|
112 | + */ |
|
113 | + public function getChildren() { |
|
114 | + throw new MethodNotAllowed('No permission to list folder contents'); |
|
115 | + } |
|
116 | 116 | |
117 | - /** |
|
118 | - * Checks if a child-node with the specified name exists |
|
119 | - * |
|
120 | - * @param string $name |
|
121 | - * @return bool |
|
122 | - */ |
|
123 | - public function childExists($name) { |
|
124 | - return call_user_func($this->childExistsFunction, $name); |
|
125 | - } |
|
117 | + /** |
|
118 | + * Checks if a child-node with the specified name exists |
|
119 | + * |
|
120 | + * @param string $name |
|
121 | + * @return bool |
|
122 | + */ |
|
123 | + public function childExists($name) { |
|
124 | + return call_user_func($this->childExistsFunction, $name); |
|
125 | + } |
|
126 | 126 | } |