2013-02-26 10:21:48 +04:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* ownCloud
|
|
|
|
*
|
|
|
|
* @author Michael Gapczynski
|
|
|
|
* @copyright 2013 Michael Gapczynski mtgap@owncloud.com
|
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 3 of the License, or any later version.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU AFFERO GENERAL PUBLIC LICENSE for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Affero General Public
|
|
|
|
* License along with this library. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace OC\Files\Cache;
|
|
|
|
|
|
|
|
class Shared_Updater {
|
|
|
|
|
2013-11-28 16:17:19 +04:00
|
|
|
// shares which can be removed from oc_share after the delete operation was successful
|
|
|
|
static private $toRemove = array();
|
|
|
|
|
2013-02-26 10:21:48 +04:00
|
|
|
/**
|
|
|
|
* Correct the parent folders' ETags for all users shared the file at $target
|
|
|
|
*
|
|
|
|
* @param string $target
|
|
|
|
*/
|
|
|
|
static public function correctFolders($target) {
|
|
|
|
$uid = \OCP\User::getUser();
|
|
|
|
$uidOwner = \OC\Files\Filesystem::getOwner($target);
|
|
|
|
$info = \OC\Files\Filesystem::getFileInfo($target);
|
2013-10-18 19:35:56 +04:00
|
|
|
$checkedUser = array($uidOwner);
|
2013-02-26 10:21:48 +04:00
|
|
|
// Correct Shared folders of other users shared with
|
2013-10-18 19:48:35 +04:00
|
|
|
$users = \OCP\Share::getUsersItemShared('file', $info['fileid'], $uidOwner, true);
|
2013-02-26 10:21:48 +04:00
|
|
|
if (!empty($users)) {
|
2013-03-07 02:33:27 +04:00
|
|
|
while (!empty($users)) {
|
|
|
|
$reshareUsers = array();
|
|
|
|
foreach ($users as $user) {
|
2013-10-18 19:47:01 +04:00
|
|
|
if ( !in_array($user, $checkedUser) ) {
|
2013-03-20 15:58:39 +04:00
|
|
|
$etag = \OC\Files\Filesystem::getETag('');
|
|
|
|
\OCP\Config::setUserValue($user, 'files_sharing', 'etag', $etag);
|
|
|
|
// Look for reshares
|
|
|
|
$reshareUsers = array_merge($reshareUsers, \OCP\Share::getUsersItemShared('file', $info['fileid'], $user, true));
|
2013-10-18 19:35:56 +04:00
|
|
|
$checkedUser[] = $user;
|
2013-03-20 15:58:39 +04:00
|
|
|
}
|
2013-02-26 10:21:48 +04:00
|
|
|
}
|
2013-03-07 02:33:27 +04:00
|
|
|
$users = $reshareUsers;
|
2013-02-26 10:21:48 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-10-04 18:50:53 +04:00
|
|
|
/**
|
|
|
|
* @brief remove all shares for a given file if the file was deleted
|
|
|
|
*
|
|
|
|
* @param string $path
|
|
|
|
*/
|
2013-10-02 15:26:38 +04:00
|
|
|
private static function removeShare($path) {
|
2013-11-28 16:17:19 +04:00
|
|
|
$fileSource = self::$toRemove[$path];
|
2013-10-02 15:26:38 +04:00
|
|
|
|
2013-11-28 16:17:19 +04:00
|
|
|
if (!\OC\Files\Filesystem::file_exists($path)) {
|
|
|
|
$query = \OC_DB::prepare('DELETE FROM `*PREFIX*share` WHERE `file_source`=?');
|
|
|
|
try {
|
|
|
|
\OC_DB::executeAudited($query, array($fileSource));
|
|
|
|
} catch (\Exception $e) {
|
|
|
|
\OCP\Util::writeLog('files_sharing', "can't remove share: " . $e->getMessage(), \OCP\Util::WARN);
|
|
|
|
}
|
2013-10-07 13:06:24 +04:00
|
|
|
}
|
2013-11-28 16:17:19 +04:00
|
|
|
unset(self::$toRemove[$path]);
|
2013-10-02 15:26:38 +04:00
|
|
|
}
|
|
|
|
|
2013-02-26 10:21:48 +04:00
|
|
|
/**
|
|
|
|
* @param array $params
|
|
|
|
*/
|
|
|
|
static public function writeHook($params) {
|
|
|
|
self::correctFolders($params['path']);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param array $params
|
|
|
|
*/
|
|
|
|
static public function renameHook($params) {
|
|
|
|
self::correctFolders($params['newpath']);
|
2013-03-22 19:20:40 +04:00
|
|
|
self::correctFolders(pathinfo($params['oldpath'], PATHINFO_DIRNAME));
|
2013-02-26 10:21:48 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param array $params
|
|
|
|
*/
|
|
|
|
static public function deleteHook($params) {
|
|
|
|
self::correctFolders($params['path']);
|
2013-11-28 16:17:19 +04:00
|
|
|
$fileInfo = \OC\Files\Filesystem::getFileInfo($params['path']);
|
|
|
|
// mark file as deleted so that we can clean up the share table if
|
|
|
|
// the file was deleted successfully
|
|
|
|
self::$toRemove[$params['path']] = $fileInfo['fileid'];
|
2013-02-26 10:21:48 +04:00
|
|
|
}
|
|
|
|
|
2013-11-27 20:22:48 +04:00
|
|
|
/**
|
|
|
|
* @param array $params
|
|
|
|
*/
|
|
|
|
static public function postDeleteHook($params) {
|
|
|
|
self::removeShare($params['path']);
|
|
|
|
}
|
2013-10-02 15:26:38 +04:00
|
|
|
|
2013-03-02 21:57:29 +04:00
|
|
|
/**
|
|
|
|
* @param array $params
|
|
|
|
*/
|
|
|
|
static public function shareHook($params) {
|
2013-03-02 22:11:57 +04:00
|
|
|
if ($params['itemType'] === 'file' || $params['itemType'] === 'folder') {
|
2013-12-05 20:25:01 +04:00
|
|
|
if (isset($params['uidOwner'])) {
|
|
|
|
$uidOwner = $params['uidOwner'];
|
|
|
|
} else {
|
|
|
|
$uidOwner = \OCP\User::getUser();
|
|
|
|
}
|
|
|
|
$users = \OCP\Share::getUsersItemShared($params['itemType'], $params['fileSource'], $uidOwner, true, false);
|
2013-03-08 19:59:22 +04:00
|
|
|
if (!empty($users)) {
|
|
|
|
while (!empty($users)) {
|
|
|
|
$reshareUsers = array();
|
|
|
|
foreach ($users as $user) {
|
2013-03-20 15:58:39 +04:00
|
|
|
if ($user !== $uidOwner) {
|
|
|
|
$etag = \OC\Files\Filesystem::getETag('');
|
|
|
|
\OCP\Config::setUserValue($user, 'files_sharing', 'etag', $etag);
|
|
|
|
// Look for reshares
|
|
|
|
$reshareUsers = array_merge($reshareUsers, \OCP\Share::getUsersItemShared('file', $params['fileSource'], $user, true));
|
|
|
|
}
|
2013-03-08 19:59:22 +04:00
|
|
|
}
|
|
|
|
$users = $reshareUsers;
|
|
|
|
}
|
|
|
|
}
|
2013-03-02 21:57:29 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-02-20 18:21:53 +04:00
|
|
|
/**
|
|
|
|
* clean up oc_share table from files which are no longer exists
|
|
|
|
*
|
|
|
|
* This fixes issues from updates from files_sharing < 0.3.5.6 (ownCloud 4.5)
|
|
|
|
* It will just be called during the update of the app
|
|
|
|
*/
|
|
|
|
static public function fixBrokenSharesOnAppUpdate() {
|
|
|
|
// delete all shares where the original file no longer exists
|
2014-03-03 22:59:25 +04:00
|
|
|
$findAndRemoveShares = \OC_DB::prepare('DELETE FROM `*PREFIX*share` ' .
|
|
|
|
'WHERE `file_source` NOT IN ( ' .
|
|
|
|
'SELECT `fileid` FROM `*PREFIX*filecache` WHERE `item_type` IN (\'file\', \'folder\'))'
|
|
|
|
);
|
|
|
|
$findAndRemoveShares->execute(array());
|
2014-02-20 18:21:53 +04:00
|
|
|
}
|
|
|
|
|
2013-03-07 19:00:03 +04:00
|
|
|
}
|