@@ -770,7 +770,7 @@ |
||
| 770 | 770 | |
| 771 | 771 | /** |
| 772 | 772 | * @param Share[] $shares |
| 773 | - * @param $userId |
|
| 773 | + * @param string $userId |
|
| 774 | 774 | * @return Share[] The updates shares if no update is found for a share return the original |
| 775 | 775 | */ |
| 776 | 776 | private function resolveGroupShares($shares, $userId) { |
@@ -287,7 +287,7 @@ discard block |
||
| 287 | 287 | ->orderBy('id'); |
| 288 | 288 | |
| 289 | 289 | $cursor = $qb->execute(); |
| 290 | - while($data = $cursor->fetch()) { |
|
| 290 | + while ($data = $cursor->fetch()) { |
|
| 291 | 291 | $children[] = $this->createShare($data); |
| 292 | 292 | } |
| 293 | 293 | $cursor->closeCursor(); |
@@ -332,7 +332,7 @@ discard block |
||
| 332 | 332 | $user = $this->userManager->get($recipient); |
| 333 | 333 | |
| 334 | 334 | if (is_null($group)) { |
| 335 | - throw new ProviderException('Group "' . $share->getSharedWith() . '" does not exist'); |
|
| 335 | + throw new ProviderException('Group "'.$share->getSharedWith().'" does not exist'); |
|
| 336 | 336 | } |
| 337 | 337 | |
| 338 | 338 | if (!$group->inGroup($user)) { |
@@ -492,7 +492,7 @@ discard block |
||
| 492 | 492 | ); |
| 493 | 493 | } |
| 494 | 494 | |
| 495 | - $qb->innerJoin('s', 'filecache' ,'f', $qb->expr()->eq('s.file_source', 'f.fileid')); |
|
| 495 | + $qb->innerJoin('s', 'filecache', 'f', $qb->expr()->eq('s.file_source', 'f.fileid')); |
|
| 496 | 496 | $qb->andWhere($qb->expr()->eq('f.parent', $qb->createNamedParameter($node->getId()))); |
| 497 | 497 | |
| 498 | 498 | $qb->orderBy('id'); |
@@ -548,7 +548,7 @@ discard block |
||
| 548 | 548 | |
| 549 | 549 | $cursor = $qb->execute(); |
| 550 | 550 | $shares = []; |
| 551 | - while($data = $cursor->fetch()) { |
|
| 551 | + while ($data = $cursor->fetch()) { |
|
| 552 | 552 | $shares[] = $this->createShare($data); |
| 553 | 553 | } |
| 554 | 554 | $cursor->closeCursor(); |
@@ -627,7 +627,7 @@ discard block |
||
| 627 | 627 | ->execute(); |
| 628 | 628 | |
| 629 | 629 | $shares = []; |
| 630 | - while($data = $cursor->fetch()) { |
|
| 630 | + while ($data = $cursor->fetch()) { |
|
| 631 | 631 | $shares[] = $this->createShare($data); |
| 632 | 632 | } |
| 633 | 633 | $cursor->closeCursor(); |
@@ -698,7 +698,7 @@ discard block |
||
| 698 | 698 | |
| 699 | 699 | $cursor = $qb->execute(); |
| 700 | 700 | |
| 701 | - while($data = $cursor->fetch()) { |
|
| 701 | + while ($data = $cursor->fetch()) { |
|
| 702 | 702 | if ($this->isAccessibleResult($data)) { |
| 703 | 703 | $shares[] = $this->createShare($data); |
| 704 | 704 | } |
@@ -713,7 +713,7 @@ discard block |
||
| 713 | 713 | $shares2 = []; |
| 714 | 714 | |
| 715 | 715 | $start = 0; |
| 716 | - while(true) { |
|
| 716 | + while (true) { |
|
| 717 | 717 | $groups = array_slice($allGroups, $start, 100); |
| 718 | 718 | $start += 100; |
| 719 | 719 | |
@@ -758,7 +758,7 @@ discard block |
||
| 758 | 758 | )); |
| 759 | 759 | |
| 760 | 760 | $cursor = $qb->execute(); |
| 761 | - while($data = $cursor->fetch()) { |
|
| 761 | + while ($data = $cursor->fetch()) { |
|
| 762 | 762 | if ($offset > 0) { |
| 763 | 763 | $offset--; |
| 764 | 764 | continue; |
@@ -827,14 +827,14 @@ discard block |
||
| 827 | 827 | */ |
| 828 | 828 | private function createShare($data) { |
| 829 | 829 | $share = new Share($this->rootFolder, $this->userManager); |
| 830 | - $share->setId((int)$data['id']) |
|
| 831 | - ->setShareType((int)$data['share_type']) |
|
| 832 | - ->setPermissions((int)$data['permissions']) |
|
| 830 | + $share->setId((int) $data['id']) |
|
| 831 | + ->setShareType((int) $data['share_type']) |
|
| 832 | + ->setPermissions((int) $data['permissions']) |
|
| 833 | 833 | ->setTarget($data['file_target']) |
| 834 | - ->setMailSend((bool)$data['mail_send']); |
|
| 834 | + ->setMailSend((bool) $data['mail_send']); |
|
| 835 | 835 | |
| 836 | 836 | $shareTime = new \DateTime(); |
| 837 | - $shareTime->setTimestamp((int)$data['stime']); |
|
| 837 | + $shareTime->setTimestamp((int) $data['stime']); |
|
| 838 | 838 | $share->setShareTime($shareTime); |
| 839 | 839 | |
| 840 | 840 | if ($share->getShareType() === \OCP\Share::SHARE_TYPE_USER) { |
@@ -849,7 +849,7 @@ discard block |
||
| 849 | 849 | $share->setSharedBy($data['uid_initiator']); |
| 850 | 850 | $share->setShareOwner($data['uid_owner']); |
| 851 | 851 | |
| 852 | - $share->setNodeId((int)$data['file_source']); |
|
| 852 | + $share->setNodeId((int) $data['file_source']); |
|
| 853 | 853 | $share->setNodeType($data['item_type']); |
| 854 | 854 | |
| 855 | 855 | if ($data['expiration'] !== null) { |
@@ -879,7 +879,7 @@ discard block |
||
| 879 | 879 | $result = []; |
| 880 | 880 | |
| 881 | 881 | $start = 0; |
| 882 | - while(true) { |
|
| 882 | + while (true) { |
|
| 883 | 883 | /** @var Share[] $shareSlice */ |
| 884 | 884 | $shareSlice = array_slice($shares, $start, 100); |
| 885 | 885 | $start += 100; |
@@ -894,7 +894,7 @@ discard block |
||
| 894 | 894 | $shareMap = []; |
| 895 | 895 | |
| 896 | 896 | foreach ($shareSlice as $share) { |
| 897 | - $ids[] = (int)$share->getId(); |
|
| 897 | + $ids[] = (int) $share->getId(); |
|
| 898 | 898 | $shareMap[$share->getId()] = $share; |
| 899 | 899 | } |
| 900 | 900 | |
@@ -911,8 +911,8 @@ discard block |
||
| 911 | 911 | |
| 912 | 912 | $stmt = $query->execute(); |
| 913 | 913 | |
| 914 | - while($data = $stmt->fetch()) { |
|
| 915 | - $shareMap[$data['parent']]->setPermissions((int)$data['permissions']); |
|
| 914 | + while ($data = $stmt->fetch()) { |
|
| 915 | + $shareMap[$data['parent']]->setPermissions((int) $data['permissions']); |
|
| 916 | 916 | $shareMap[$data['parent']]->setTarget($data['file_target']); |
| 917 | 917 | } |
| 918 | 918 | |
@@ -1009,8 +1009,8 @@ discard block |
||
| 1009 | 1009 | |
| 1010 | 1010 | $cursor = $qb->execute(); |
| 1011 | 1011 | $ids = []; |
| 1012 | - while($row = $cursor->fetch()) { |
|
| 1013 | - $ids[] = (int)$row['id']; |
|
| 1012 | + while ($row = $cursor->fetch()) { |
|
| 1013 | + $ids[] = (int) $row['id']; |
|
| 1014 | 1014 | } |
| 1015 | 1015 | $cursor->closeCursor(); |
| 1016 | 1016 | |
@@ -1052,8 +1052,8 @@ discard block |
||
| 1052 | 1052 | |
| 1053 | 1053 | $cursor = $qb->execute(); |
| 1054 | 1054 | $ids = []; |
| 1055 | - while($row = $cursor->fetch()) { |
|
| 1056 | - $ids[] = (int)$row['id']; |
|
| 1055 | + while ($row = $cursor->fetch()) { |
|
| 1056 | + $ids[] = (int) $row['id']; |
|
| 1057 | 1057 | } |
| 1058 | 1058 | $cursor->closeCursor(); |
| 1059 | 1059 | |
@@ -1107,8 +1107,8 @@ discard block |
||
| 1107 | 1107 | |
| 1108 | 1108 | $users = []; |
| 1109 | 1109 | $link = false; |
| 1110 | - while($row = $cursor->fetch()) { |
|
| 1111 | - $type = (int)$row['share_type']; |
|
| 1110 | + while ($row = $cursor->fetch()) { |
|
| 1111 | + $type = (int) $row['share_type']; |
|
| 1112 | 1112 | if ($type === \OCP\Share::SHARE_TYPE_USER) { |
| 1113 | 1113 | $uid = $row['share_with']; |
| 1114 | 1114 | $users[$uid] = isset($users[$uid]) ? $users[$uid] : []; |
@@ -52,1143 +52,1143 @@ |
||
| 52 | 52 | */ |
| 53 | 53 | class DefaultShareProvider implements IShareProvider { |
| 54 | 54 | |
| 55 | - // Special share type for user modified group shares |
|
| 56 | - const SHARE_TYPE_USERGROUP = 2; |
|
| 57 | - |
|
| 58 | - /** @var IDBConnection */ |
|
| 59 | - private $dbConn; |
|
| 60 | - |
|
| 61 | - /** @var IUserManager */ |
|
| 62 | - private $userManager; |
|
| 63 | - |
|
| 64 | - /** @var IGroupManager */ |
|
| 65 | - private $groupManager; |
|
| 66 | - |
|
| 67 | - /** @var IRootFolder */ |
|
| 68 | - private $rootFolder; |
|
| 69 | - |
|
| 70 | - /** |
|
| 71 | - * DefaultShareProvider constructor. |
|
| 72 | - * |
|
| 73 | - * @param IDBConnection $connection |
|
| 74 | - * @param IUserManager $userManager |
|
| 75 | - * @param IGroupManager $groupManager |
|
| 76 | - * @param IRootFolder $rootFolder |
|
| 77 | - */ |
|
| 78 | - public function __construct( |
|
| 79 | - IDBConnection $connection, |
|
| 80 | - IUserManager $userManager, |
|
| 81 | - IGroupManager $groupManager, |
|
| 82 | - IRootFolder $rootFolder) { |
|
| 83 | - $this->dbConn = $connection; |
|
| 84 | - $this->userManager = $userManager; |
|
| 85 | - $this->groupManager = $groupManager; |
|
| 86 | - $this->rootFolder = $rootFolder; |
|
| 87 | - } |
|
| 88 | - |
|
| 89 | - /** |
|
| 90 | - * Return the identifier of this provider. |
|
| 91 | - * |
|
| 92 | - * @return string Containing only [a-zA-Z0-9] |
|
| 93 | - */ |
|
| 94 | - public function identifier() { |
|
| 95 | - return 'ocinternal'; |
|
| 96 | - } |
|
| 97 | - |
|
| 98 | - /** |
|
| 99 | - * Share a path |
|
| 100 | - * |
|
| 101 | - * @param \OCP\Share\IShare $share |
|
| 102 | - * @return \OCP\Share\IShare The share object |
|
| 103 | - * @throws ShareNotFound |
|
| 104 | - * @throws \Exception |
|
| 105 | - */ |
|
| 106 | - public function create(\OCP\Share\IShare $share) { |
|
| 107 | - $qb = $this->dbConn->getQueryBuilder(); |
|
| 108 | - |
|
| 109 | - $qb->insert('share'); |
|
| 110 | - $qb->setValue('share_type', $qb->createNamedParameter($share->getShareType())); |
|
| 111 | - |
|
| 112 | - if ($share->getShareType() === \OCP\Share::SHARE_TYPE_USER) { |
|
| 113 | - //Set the UID of the user we share with |
|
| 114 | - $qb->setValue('share_with', $qb->createNamedParameter($share->getSharedWith())); |
|
| 115 | - } else if ($share->getShareType() === \OCP\Share::SHARE_TYPE_GROUP) { |
|
| 116 | - //Set the GID of the group we share with |
|
| 117 | - $qb->setValue('share_with', $qb->createNamedParameter($share->getSharedWith())); |
|
| 118 | - } else if ($share->getShareType() === \OCP\Share::SHARE_TYPE_LINK) { |
|
| 119 | - //Set the token of the share |
|
| 120 | - $qb->setValue('token', $qb->createNamedParameter($share->getToken())); |
|
| 121 | - |
|
| 122 | - //If a password is set store it |
|
| 123 | - if ($share->getPassword() !== null) { |
|
| 124 | - $qb->setValue('password', $qb->createNamedParameter($share->getPassword())); |
|
| 125 | - } |
|
| 126 | - |
|
| 127 | - //If an expiration date is set store it |
|
| 128 | - if ($share->getExpirationDate() !== null) { |
|
| 129 | - $qb->setValue('expiration', $qb->createNamedParameter($share->getExpirationDate(), 'datetime')); |
|
| 130 | - } |
|
| 131 | - |
|
| 132 | - if (method_exists($share, 'getParent')) { |
|
| 133 | - $qb->setValue('parent', $qb->createNamedParameter($share->getParent())); |
|
| 134 | - } |
|
| 135 | - } else { |
|
| 136 | - throw new \Exception('invalid share type!'); |
|
| 137 | - } |
|
| 138 | - |
|
| 139 | - // Set what is shares |
|
| 140 | - $qb->setValue('item_type', $qb->createParameter('itemType')); |
|
| 141 | - if ($share->getNode() instanceof \OCP\Files\File) { |
|
| 142 | - $qb->setParameter('itemType', 'file'); |
|
| 143 | - } else { |
|
| 144 | - $qb->setParameter('itemType', 'folder'); |
|
| 145 | - } |
|
| 146 | - |
|
| 147 | - // Set the file id |
|
| 148 | - $qb->setValue('item_source', $qb->createNamedParameter($share->getNode()->getId())); |
|
| 149 | - $qb->setValue('file_source', $qb->createNamedParameter($share->getNode()->getId())); |
|
| 150 | - |
|
| 151 | - // set the permissions |
|
| 152 | - $qb->setValue('permissions', $qb->createNamedParameter($share->getPermissions())); |
|
| 153 | - |
|
| 154 | - // Set who created this share |
|
| 155 | - $qb->setValue('uid_initiator', $qb->createNamedParameter($share->getSharedBy())); |
|
| 156 | - |
|
| 157 | - // Set who is the owner of this file/folder (and this the owner of the share) |
|
| 158 | - $qb->setValue('uid_owner', $qb->createNamedParameter($share->getShareOwner())); |
|
| 159 | - |
|
| 160 | - // Set the file target |
|
| 161 | - $qb->setValue('file_target', $qb->createNamedParameter($share->getTarget())); |
|
| 162 | - |
|
| 163 | - // Set the time this share was created |
|
| 164 | - $qb->setValue('stime', $qb->createNamedParameter(time())); |
|
| 165 | - |
|
| 166 | - // insert the data and fetch the id of the share |
|
| 167 | - $this->dbConn->beginTransaction(); |
|
| 168 | - $qb->execute(); |
|
| 169 | - $id = $this->dbConn->lastInsertId('*PREFIX*share'); |
|
| 170 | - |
|
| 171 | - // Now fetch the inserted share and create a complete share object |
|
| 172 | - $qb = $this->dbConn->getQueryBuilder(); |
|
| 173 | - $qb->select('*') |
|
| 174 | - ->from('share') |
|
| 175 | - ->where($qb->expr()->eq('id', $qb->createNamedParameter($id))); |
|
| 176 | - |
|
| 177 | - $cursor = $qb->execute(); |
|
| 178 | - $data = $cursor->fetch(); |
|
| 179 | - $this->dbConn->commit(); |
|
| 180 | - $cursor->closeCursor(); |
|
| 181 | - |
|
| 182 | - if ($data === false) { |
|
| 183 | - throw new ShareNotFound(); |
|
| 184 | - } |
|
| 185 | - |
|
| 186 | - $mailSendValue = $share->getMailSend(); |
|
| 187 | - $data['mail_send'] = ($mailSendValue === null) ? true : $mailSendValue; |
|
| 188 | - |
|
| 189 | - $share = $this->createShare($data); |
|
| 190 | - return $share; |
|
| 191 | - } |
|
| 192 | - |
|
| 193 | - /** |
|
| 194 | - * Update a share |
|
| 195 | - * |
|
| 196 | - * @param \OCP\Share\IShare $share |
|
| 197 | - * @return \OCP\Share\IShare The share object |
|
| 198 | - */ |
|
| 199 | - public function update(\OCP\Share\IShare $share) { |
|
| 200 | - if ($share->getShareType() === \OCP\Share::SHARE_TYPE_USER) { |
|
| 201 | - /* |
|
| 55 | + // Special share type for user modified group shares |
|
| 56 | + const SHARE_TYPE_USERGROUP = 2; |
|
| 57 | + |
|
| 58 | + /** @var IDBConnection */ |
|
| 59 | + private $dbConn; |
|
| 60 | + |
|
| 61 | + /** @var IUserManager */ |
|
| 62 | + private $userManager; |
|
| 63 | + |
|
| 64 | + /** @var IGroupManager */ |
|
| 65 | + private $groupManager; |
|
| 66 | + |
|
| 67 | + /** @var IRootFolder */ |
|
| 68 | + private $rootFolder; |
|
| 69 | + |
|
| 70 | + /** |
|
| 71 | + * DefaultShareProvider constructor. |
|
| 72 | + * |
|
| 73 | + * @param IDBConnection $connection |
|
| 74 | + * @param IUserManager $userManager |
|
| 75 | + * @param IGroupManager $groupManager |
|
| 76 | + * @param IRootFolder $rootFolder |
|
| 77 | + */ |
|
| 78 | + public function __construct( |
|
| 79 | + IDBConnection $connection, |
|
| 80 | + IUserManager $userManager, |
|
| 81 | + IGroupManager $groupManager, |
|
| 82 | + IRootFolder $rootFolder) { |
|
| 83 | + $this->dbConn = $connection; |
|
| 84 | + $this->userManager = $userManager; |
|
| 85 | + $this->groupManager = $groupManager; |
|
| 86 | + $this->rootFolder = $rootFolder; |
|
| 87 | + } |
|
| 88 | + |
|
| 89 | + /** |
|
| 90 | + * Return the identifier of this provider. |
|
| 91 | + * |
|
| 92 | + * @return string Containing only [a-zA-Z0-9] |
|
| 93 | + */ |
|
| 94 | + public function identifier() { |
|
| 95 | + return 'ocinternal'; |
|
| 96 | + } |
|
| 97 | + |
|
| 98 | + /** |
|
| 99 | + * Share a path |
|
| 100 | + * |
|
| 101 | + * @param \OCP\Share\IShare $share |
|
| 102 | + * @return \OCP\Share\IShare The share object |
|
| 103 | + * @throws ShareNotFound |
|
| 104 | + * @throws \Exception |
|
| 105 | + */ |
|
| 106 | + public function create(\OCP\Share\IShare $share) { |
|
| 107 | + $qb = $this->dbConn->getQueryBuilder(); |
|
| 108 | + |
|
| 109 | + $qb->insert('share'); |
|
| 110 | + $qb->setValue('share_type', $qb->createNamedParameter($share->getShareType())); |
|
| 111 | + |
|
| 112 | + if ($share->getShareType() === \OCP\Share::SHARE_TYPE_USER) { |
|
| 113 | + //Set the UID of the user we share with |
|
| 114 | + $qb->setValue('share_with', $qb->createNamedParameter($share->getSharedWith())); |
|
| 115 | + } else if ($share->getShareType() === \OCP\Share::SHARE_TYPE_GROUP) { |
|
| 116 | + //Set the GID of the group we share with |
|
| 117 | + $qb->setValue('share_with', $qb->createNamedParameter($share->getSharedWith())); |
|
| 118 | + } else if ($share->getShareType() === \OCP\Share::SHARE_TYPE_LINK) { |
|
| 119 | + //Set the token of the share |
|
| 120 | + $qb->setValue('token', $qb->createNamedParameter($share->getToken())); |
|
| 121 | + |
|
| 122 | + //If a password is set store it |
|
| 123 | + if ($share->getPassword() !== null) { |
|
| 124 | + $qb->setValue('password', $qb->createNamedParameter($share->getPassword())); |
|
| 125 | + } |
|
| 126 | + |
|
| 127 | + //If an expiration date is set store it |
|
| 128 | + if ($share->getExpirationDate() !== null) { |
|
| 129 | + $qb->setValue('expiration', $qb->createNamedParameter($share->getExpirationDate(), 'datetime')); |
|
| 130 | + } |
|
| 131 | + |
|
| 132 | + if (method_exists($share, 'getParent')) { |
|
| 133 | + $qb->setValue('parent', $qb->createNamedParameter($share->getParent())); |
|
| 134 | + } |
|
| 135 | + } else { |
|
| 136 | + throw new \Exception('invalid share type!'); |
|
| 137 | + } |
|
| 138 | + |
|
| 139 | + // Set what is shares |
|
| 140 | + $qb->setValue('item_type', $qb->createParameter('itemType')); |
|
| 141 | + if ($share->getNode() instanceof \OCP\Files\File) { |
|
| 142 | + $qb->setParameter('itemType', 'file'); |
|
| 143 | + } else { |
|
| 144 | + $qb->setParameter('itemType', 'folder'); |
|
| 145 | + } |
|
| 146 | + |
|
| 147 | + // Set the file id |
|
| 148 | + $qb->setValue('item_source', $qb->createNamedParameter($share->getNode()->getId())); |
|
| 149 | + $qb->setValue('file_source', $qb->createNamedParameter($share->getNode()->getId())); |
|
| 150 | + |
|
| 151 | + // set the permissions |
|
| 152 | + $qb->setValue('permissions', $qb->createNamedParameter($share->getPermissions())); |
|
| 153 | + |
|
| 154 | + // Set who created this share |
|
| 155 | + $qb->setValue('uid_initiator', $qb->createNamedParameter($share->getSharedBy())); |
|
| 156 | + |
|
| 157 | + // Set who is the owner of this file/folder (and this the owner of the share) |
|
| 158 | + $qb->setValue('uid_owner', $qb->createNamedParameter($share->getShareOwner())); |
|
| 159 | + |
|
| 160 | + // Set the file target |
|
| 161 | + $qb->setValue('file_target', $qb->createNamedParameter($share->getTarget())); |
|
| 162 | + |
|
| 163 | + // Set the time this share was created |
|
| 164 | + $qb->setValue('stime', $qb->createNamedParameter(time())); |
|
| 165 | + |
|
| 166 | + // insert the data and fetch the id of the share |
|
| 167 | + $this->dbConn->beginTransaction(); |
|
| 168 | + $qb->execute(); |
|
| 169 | + $id = $this->dbConn->lastInsertId('*PREFIX*share'); |
|
| 170 | + |
|
| 171 | + // Now fetch the inserted share and create a complete share object |
|
| 172 | + $qb = $this->dbConn->getQueryBuilder(); |
|
| 173 | + $qb->select('*') |
|
| 174 | + ->from('share') |
|
| 175 | + ->where($qb->expr()->eq('id', $qb->createNamedParameter($id))); |
|
| 176 | + |
|
| 177 | + $cursor = $qb->execute(); |
|
| 178 | + $data = $cursor->fetch(); |
|
| 179 | + $this->dbConn->commit(); |
|
| 180 | + $cursor->closeCursor(); |
|
| 181 | + |
|
| 182 | + if ($data === false) { |
|
| 183 | + throw new ShareNotFound(); |
|
| 184 | + } |
|
| 185 | + |
|
| 186 | + $mailSendValue = $share->getMailSend(); |
|
| 187 | + $data['mail_send'] = ($mailSendValue === null) ? true : $mailSendValue; |
|
| 188 | + |
|
| 189 | + $share = $this->createShare($data); |
|
| 190 | + return $share; |
|
| 191 | + } |
|
| 192 | + |
|
| 193 | + /** |
|
| 194 | + * Update a share |
|
| 195 | + * |
|
| 196 | + * @param \OCP\Share\IShare $share |
|
| 197 | + * @return \OCP\Share\IShare The share object |
|
| 198 | + */ |
|
| 199 | + public function update(\OCP\Share\IShare $share) { |
|
| 200 | + if ($share->getShareType() === \OCP\Share::SHARE_TYPE_USER) { |
|
| 201 | + /* |
|
| 202 | 202 | * We allow updating the recipient on user shares. |
| 203 | 203 | */ |
| 204 | - $qb = $this->dbConn->getQueryBuilder(); |
|
| 205 | - $qb->update('share') |
|
| 206 | - ->where($qb->expr()->eq('id', $qb->createNamedParameter($share->getId()))) |
|
| 207 | - ->set('share_with', $qb->createNamedParameter($share->getSharedWith())) |
|
| 208 | - ->set('uid_owner', $qb->createNamedParameter($share->getShareOwner())) |
|
| 209 | - ->set('uid_initiator', $qb->createNamedParameter($share->getSharedBy())) |
|
| 210 | - ->set('permissions', $qb->createNamedParameter($share->getPermissions())) |
|
| 211 | - ->set('item_source', $qb->createNamedParameter($share->getNode()->getId())) |
|
| 212 | - ->set('file_source', $qb->createNamedParameter($share->getNode()->getId())) |
|
| 213 | - ->set('expiration', $qb->createNamedParameter($share->getExpirationDate(), IQueryBuilder::PARAM_DATE)) |
|
| 214 | - ->execute(); |
|
| 215 | - } else if ($share->getShareType() === \OCP\Share::SHARE_TYPE_GROUP) { |
|
| 216 | - $qb = $this->dbConn->getQueryBuilder(); |
|
| 217 | - $qb->update('share') |
|
| 218 | - ->where($qb->expr()->eq('id', $qb->createNamedParameter($share->getId()))) |
|
| 219 | - ->set('uid_owner', $qb->createNamedParameter($share->getShareOwner())) |
|
| 220 | - ->set('uid_initiator', $qb->createNamedParameter($share->getSharedBy())) |
|
| 221 | - ->set('permissions', $qb->createNamedParameter($share->getPermissions())) |
|
| 222 | - ->set('item_source', $qb->createNamedParameter($share->getNode()->getId())) |
|
| 223 | - ->set('file_source', $qb->createNamedParameter($share->getNode()->getId())) |
|
| 224 | - ->set('expiration', $qb->createNamedParameter($share->getExpirationDate(), IQueryBuilder::PARAM_DATE)) |
|
| 225 | - ->execute(); |
|
| 226 | - |
|
| 227 | - /* |
|
| 204 | + $qb = $this->dbConn->getQueryBuilder(); |
|
| 205 | + $qb->update('share') |
|
| 206 | + ->where($qb->expr()->eq('id', $qb->createNamedParameter($share->getId()))) |
|
| 207 | + ->set('share_with', $qb->createNamedParameter($share->getSharedWith())) |
|
| 208 | + ->set('uid_owner', $qb->createNamedParameter($share->getShareOwner())) |
|
| 209 | + ->set('uid_initiator', $qb->createNamedParameter($share->getSharedBy())) |
|
| 210 | + ->set('permissions', $qb->createNamedParameter($share->getPermissions())) |
|
| 211 | + ->set('item_source', $qb->createNamedParameter($share->getNode()->getId())) |
|
| 212 | + ->set('file_source', $qb->createNamedParameter($share->getNode()->getId())) |
|
| 213 | + ->set('expiration', $qb->createNamedParameter($share->getExpirationDate(), IQueryBuilder::PARAM_DATE)) |
|
| 214 | + ->execute(); |
|
| 215 | + } else if ($share->getShareType() === \OCP\Share::SHARE_TYPE_GROUP) { |
|
| 216 | + $qb = $this->dbConn->getQueryBuilder(); |
|
| 217 | + $qb->update('share') |
|
| 218 | + ->where($qb->expr()->eq('id', $qb->createNamedParameter($share->getId()))) |
|
| 219 | + ->set('uid_owner', $qb->createNamedParameter($share->getShareOwner())) |
|
| 220 | + ->set('uid_initiator', $qb->createNamedParameter($share->getSharedBy())) |
|
| 221 | + ->set('permissions', $qb->createNamedParameter($share->getPermissions())) |
|
| 222 | + ->set('item_source', $qb->createNamedParameter($share->getNode()->getId())) |
|
| 223 | + ->set('file_source', $qb->createNamedParameter($share->getNode()->getId())) |
|
| 224 | + ->set('expiration', $qb->createNamedParameter($share->getExpirationDate(), IQueryBuilder::PARAM_DATE)) |
|
| 225 | + ->execute(); |
|
| 226 | + |
|
| 227 | + /* |
|
| 228 | 228 | * Update all user defined group shares |
| 229 | 229 | */ |
| 230 | - $qb = $this->dbConn->getQueryBuilder(); |
|
| 231 | - $qb->update('share') |
|
| 232 | - ->where($qb->expr()->eq('parent', $qb->createNamedParameter($share->getId()))) |
|
| 233 | - ->set('uid_owner', $qb->createNamedParameter($share->getShareOwner())) |
|
| 234 | - ->set('uid_initiator', $qb->createNamedParameter($share->getSharedBy())) |
|
| 235 | - ->set('item_source', $qb->createNamedParameter($share->getNode()->getId())) |
|
| 236 | - ->set('file_source', $qb->createNamedParameter($share->getNode()->getId())) |
|
| 237 | - ->set('expiration', $qb->createNamedParameter($share->getExpirationDate(), IQueryBuilder::PARAM_DATE)) |
|
| 238 | - ->execute(); |
|
| 239 | - |
|
| 240 | - /* |
|
| 230 | + $qb = $this->dbConn->getQueryBuilder(); |
|
| 231 | + $qb->update('share') |
|
| 232 | + ->where($qb->expr()->eq('parent', $qb->createNamedParameter($share->getId()))) |
|
| 233 | + ->set('uid_owner', $qb->createNamedParameter($share->getShareOwner())) |
|
| 234 | + ->set('uid_initiator', $qb->createNamedParameter($share->getSharedBy())) |
|
| 235 | + ->set('item_source', $qb->createNamedParameter($share->getNode()->getId())) |
|
| 236 | + ->set('file_source', $qb->createNamedParameter($share->getNode()->getId())) |
|
| 237 | + ->set('expiration', $qb->createNamedParameter($share->getExpirationDate(), IQueryBuilder::PARAM_DATE)) |
|
| 238 | + ->execute(); |
|
| 239 | + |
|
| 240 | + /* |
|
| 241 | 241 | * Now update the permissions for all children that have not set it to 0 |
| 242 | 242 | */ |
| 243 | - $qb = $this->dbConn->getQueryBuilder(); |
|
| 244 | - $qb->update('share') |
|
| 245 | - ->where($qb->expr()->eq('parent', $qb->createNamedParameter($share->getId()))) |
|
| 246 | - ->andWhere($qb->expr()->neq('permissions', $qb->createNamedParameter(0))) |
|
| 247 | - ->set('permissions', $qb->createNamedParameter($share->getPermissions())) |
|
| 248 | - ->execute(); |
|
| 249 | - |
|
| 250 | - } else if ($share->getShareType() === \OCP\Share::SHARE_TYPE_LINK) { |
|
| 251 | - $qb = $this->dbConn->getQueryBuilder(); |
|
| 252 | - $qb->update('share') |
|
| 253 | - ->where($qb->expr()->eq('id', $qb->createNamedParameter($share->getId()))) |
|
| 254 | - ->set('password', $qb->createNamedParameter($share->getPassword())) |
|
| 255 | - ->set('uid_owner', $qb->createNamedParameter($share->getShareOwner())) |
|
| 256 | - ->set('uid_initiator', $qb->createNamedParameter($share->getSharedBy())) |
|
| 257 | - ->set('permissions', $qb->createNamedParameter($share->getPermissions())) |
|
| 258 | - ->set('item_source', $qb->createNamedParameter($share->getNode()->getId())) |
|
| 259 | - ->set('file_source', $qb->createNamedParameter($share->getNode()->getId())) |
|
| 260 | - ->set('token', $qb->createNamedParameter($share->getToken())) |
|
| 261 | - ->set('expiration', $qb->createNamedParameter($share->getExpirationDate(), IQueryBuilder::PARAM_DATE)) |
|
| 262 | - ->execute(); |
|
| 263 | - } |
|
| 264 | - |
|
| 265 | - return $share; |
|
| 266 | - } |
|
| 267 | - |
|
| 268 | - /** |
|
| 269 | - * Get all children of this share |
|
| 270 | - * FIXME: remove once https://github.com/owncloud/core/pull/21660 is in |
|
| 271 | - * |
|
| 272 | - * @param \OCP\Share\IShare $parent |
|
| 273 | - * @return \OCP\Share\IShare[] |
|
| 274 | - */ |
|
| 275 | - public function getChildren(\OCP\Share\IShare $parent) { |
|
| 276 | - $children = []; |
|
| 277 | - |
|
| 278 | - $qb = $this->dbConn->getQueryBuilder(); |
|
| 279 | - $qb->select('*') |
|
| 280 | - ->from('share') |
|
| 281 | - ->where($qb->expr()->eq('parent', $qb->createNamedParameter($parent->getId()))) |
|
| 282 | - ->andWhere( |
|
| 283 | - $qb->expr()->in( |
|
| 284 | - 'share_type', |
|
| 285 | - $qb->createNamedParameter([ |
|
| 286 | - \OCP\Share::SHARE_TYPE_USER, |
|
| 287 | - \OCP\Share::SHARE_TYPE_GROUP, |
|
| 288 | - \OCP\Share::SHARE_TYPE_LINK, |
|
| 289 | - ], IQueryBuilder::PARAM_INT_ARRAY) |
|
| 290 | - ) |
|
| 291 | - ) |
|
| 292 | - ->andWhere($qb->expr()->orX( |
|
| 293 | - $qb->expr()->eq('item_type', $qb->createNamedParameter('file')), |
|
| 294 | - $qb->expr()->eq('item_type', $qb->createNamedParameter('folder')) |
|
| 295 | - )) |
|
| 296 | - ->orderBy('id'); |
|
| 297 | - |
|
| 298 | - $cursor = $qb->execute(); |
|
| 299 | - while($data = $cursor->fetch()) { |
|
| 300 | - $children[] = $this->createShare($data); |
|
| 301 | - } |
|
| 302 | - $cursor->closeCursor(); |
|
| 303 | - |
|
| 304 | - return $children; |
|
| 305 | - } |
|
| 306 | - |
|
| 307 | - /** |
|
| 308 | - * Delete a share |
|
| 309 | - * |
|
| 310 | - * @param \OCP\Share\IShare $share |
|
| 311 | - */ |
|
| 312 | - public function delete(\OCP\Share\IShare $share) { |
|
| 313 | - $qb = $this->dbConn->getQueryBuilder(); |
|
| 314 | - $qb->delete('share') |
|
| 315 | - ->where($qb->expr()->eq('id', $qb->createNamedParameter($share->getId()))); |
|
| 316 | - |
|
| 317 | - /* |
|
| 243 | + $qb = $this->dbConn->getQueryBuilder(); |
|
| 244 | + $qb->update('share') |
|
| 245 | + ->where($qb->expr()->eq('parent', $qb->createNamedParameter($share->getId()))) |
|
| 246 | + ->andWhere($qb->expr()->neq('permissions', $qb->createNamedParameter(0))) |
|
| 247 | + ->set('permissions', $qb->createNamedParameter($share->getPermissions())) |
|
| 248 | + ->execute(); |
|
| 249 | + |
|
| 250 | + } else if ($share->getShareType() === \OCP\Share::SHARE_TYPE_LINK) { |
|
| 251 | + $qb = $this->dbConn->getQueryBuilder(); |
|
| 252 | + $qb->update('share') |
|
| 253 | + ->where($qb->expr()->eq('id', $qb->createNamedParameter($share->getId()))) |
|
| 254 | + ->set('password', $qb->createNamedParameter($share->getPassword())) |
|
| 255 | + ->set('uid_owner', $qb->createNamedParameter($share->getShareOwner())) |
|
| 256 | + ->set('uid_initiator', $qb->createNamedParameter($share->getSharedBy())) |
|
| 257 | + ->set('permissions', $qb->createNamedParameter($share->getPermissions())) |
|
| 258 | + ->set('item_source', $qb->createNamedParameter($share->getNode()->getId())) |
|
| 259 | + ->set('file_source', $qb->createNamedParameter($share->getNode()->getId())) |
|
| 260 | + ->set('token', $qb->createNamedParameter($share->getToken())) |
|
| 261 | + ->set('expiration', $qb->createNamedParameter($share->getExpirationDate(), IQueryBuilder::PARAM_DATE)) |
|
| 262 | + ->execute(); |
|
| 263 | + } |
|
| 264 | + |
|
| 265 | + return $share; |
|
| 266 | + } |
|
| 267 | + |
|
| 268 | + /** |
|
| 269 | + * Get all children of this share |
|
| 270 | + * FIXME: remove once https://github.com/owncloud/core/pull/21660 is in |
|
| 271 | + * |
|
| 272 | + * @param \OCP\Share\IShare $parent |
|
| 273 | + * @return \OCP\Share\IShare[] |
|
| 274 | + */ |
|
| 275 | + public function getChildren(\OCP\Share\IShare $parent) { |
|
| 276 | + $children = []; |
|
| 277 | + |
|
| 278 | + $qb = $this->dbConn->getQueryBuilder(); |
|
| 279 | + $qb->select('*') |
|
| 280 | + ->from('share') |
|
| 281 | + ->where($qb->expr()->eq('parent', $qb->createNamedParameter($parent->getId()))) |
|
| 282 | + ->andWhere( |
|
| 283 | + $qb->expr()->in( |
|
| 284 | + 'share_type', |
|
| 285 | + $qb->createNamedParameter([ |
|
| 286 | + \OCP\Share::SHARE_TYPE_USER, |
|
| 287 | + \OCP\Share::SHARE_TYPE_GROUP, |
|
| 288 | + \OCP\Share::SHARE_TYPE_LINK, |
|
| 289 | + ], IQueryBuilder::PARAM_INT_ARRAY) |
|
| 290 | + ) |
|
| 291 | + ) |
|
| 292 | + ->andWhere($qb->expr()->orX( |
|
| 293 | + $qb->expr()->eq('item_type', $qb->createNamedParameter('file')), |
|
| 294 | + $qb->expr()->eq('item_type', $qb->createNamedParameter('folder')) |
|
| 295 | + )) |
|
| 296 | + ->orderBy('id'); |
|
| 297 | + |
|
| 298 | + $cursor = $qb->execute(); |
|
| 299 | + while($data = $cursor->fetch()) { |
|
| 300 | + $children[] = $this->createShare($data); |
|
| 301 | + } |
|
| 302 | + $cursor->closeCursor(); |
|
| 303 | + |
|
| 304 | + return $children; |
|
| 305 | + } |
|
| 306 | + |
|
| 307 | + /** |
|
| 308 | + * Delete a share |
|
| 309 | + * |
|
| 310 | + * @param \OCP\Share\IShare $share |
|
| 311 | + */ |
|
| 312 | + public function delete(\OCP\Share\IShare $share) { |
|
| 313 | + $qb = $this->dbConn->getQueryBuilder(); |
|
| 314 | + $qb->delete('share') |
|
| 315 | + ->where($qb->expr()->eq('id', $qb->createNamedParameter($share->getId()))); |
|
| 316 | + |
|
| 317 | + /* |
|
| 318 | 318 | * If the share is a group share delete all possible |
| 319 | 319 | * user defined groups shares. |
| 320 | 320 | */ |
| 321 | - if ($share->getShareType() === \OCP\Share::SHARE_TYPE_GROUP) { |
|
| 322 | - $qb->orWhere($qb->expr()->eq('parent', $qb->createNamedParameter($share->getId()))); |
|
| 323 | - } |
|
| 324 | - |
|
| 325 | - $qb->execute(); |
|
| 326 | - } |
|
| 327 | - |
|
| 328 | - /** |
|
| 329 | - * Unshare a share from the recipient. If this is a group share |
|
| 330 | - * this means we need a special entry in the share db. |
|
| 331 | - * |
|
| 332 | - * @param \OCP\Share\IShare $share |
|
| 333 | - * @param string $recipient UserId of recipient |
|
| 334 | - * @throws BackendError |
|
| 335 | - * @throws ProviderException |
|
| 336 | - */ |
|
| 337 | - public function deleteFromSelf(\OCP\Share\IShare $share, $recipient) { |
|
| 338 | - if ($share->getShareType() === \OCP\Share::SHARE_TYPE_GROUP) { |
|
| 339 | - |
|
| 340 | - $group = $this->groupManager->get($share->getSharedWith()); |
|
| 341 | - $user = $this->userManager->get($recipient); |
|
| 342 | - |
|
| 343 | - if (is_null($group)) { |
|
| 344 | - throw new ProviderException('Group "' . $share->getSharedWith() . '" does not exist'); |
|
| 345 | - } |
|
| 346 | - |
|
| 347 | - if (!$group->inGroup($user)) { |
|
| 348 | - throw new ProviderException('Recipient not in receiving group'); |
|
| 349 | - } |
|
| 350 | - |
|
| 351 | - // Try to fetch user specific share |
|
| 352 | - $qb = $this->dbConn->getQueryBuilder(); |
|
| 353 | - $stmt = $qb->select('*') |
|
| 354 | - ->from('share') |
|
| 355 | - ->where($qb->expr()->eq('share_type', $qb->createNamedParameter(self::SHARE_TYPE_USERGROUP))) |
|
| 356 | - ->andWhere($qb->expr()->eq('share_with', $qb->createNamedParameter($recipient))) |
|
| 357 | - ->andWhere($qb->expr()->eq('parent', $qb->createNamedParameter($share->getId()))) |
|
| 358 | - ->andWhere($qb->expr()->orX( |
|
| 359 | - $qb->expr()->eq('item_type', $qb->createNamedParameter('file')), |
|
| 360 | - $qb->expr()->eq('item_type', $qb->createNamedParameter('folder')) |
|
| 361 | - )) |
|
| 362 | - ->execute(); |
|
| 363 | - |
|
| 364 | - $data = $stmt->fetch(); |
|
| 365 | - |
|
| 366 | - /* |
|
| 321 | + if ($share->getShareType() === \OCP\Share::SHARE_TYPE_GROUP) { |
|
| 322 | + $qb->orWhere($qb->expr()->eq('parent', $qb->createNamedParameter($share->getId()))); |
|
| 323 | + } |
|
| 324 | + |
|
| 325 | + $qb->execute(); |
|
| 326 | + } |
|
| 327 | + |
|
| 328 | + /** |
|
| 329 | + * Unshare a share from the recipient. If this is a group share |
|
| 330 | + * this means we need a special entry in the share db. |
|
| 331 | + * |
|
| 332 | + * @param \OCP\Share\IShare $share |
|
| 333 | + * @param string $recipient UserId of recipient |
|
| 334 | + * @throws BackendError |
|
| 335 | + * @throws ProviderException |
|
| 336 | + */ |
|
| 337 | + public function deleteFromSelf(\OCP\Share\IShare $share, $recipient) { |
|
| 338 | + if ($share->getShareType() === \OCP\Share::SHARE_TYPE_GROUP) { |
|
| 339 | + |
|
| 340 | + $group = $this->groupManager->get($share->getSharedWith()); |
|
| 341 | + $user = $this->userManager->get($recipient); |
|
| 342 | + |
|
| 343 | + if (is_null($group)) { |
|
| 344 | + throw new ProviderException('Group "' . $share->getSharedWith() . '" does not exist'); |
|
| 345 | + } |
|
| 346 | + |
|
| 347 | + if (!$group->inGroup($user)) { |
|
| 348 | + throw new ProviderException('Recipient not in receiving group'); |
|
| 349 | + } |
|
| 350 | + |
|
| 351 | + // Try to fetch user specific share |
|
| 352 | + $qb = $this->dbConn->getQueryBuilder(); |
|
| 353 | + $stmt = $qb->select('*') |
|
| 354 | + ->from('share') |
|
| 355 | + ->where($qb->expr()->eq('share_type', $qb->createNamedParameter(self::SHARE_TYPE_USERGROUP))) |
|
| 356 | + ->andWhere($qb->expr()->eq('share_with', $qb->createNamedParameter($recipient))) |
|
| 357 | + ->andWhere($qb->expr()->eq('parent', $qb->createNamedParameter($share->getId()))) |
|
| 358 | + ->andWhere($qb->expr()->orX( |
|
| 359 | + $qb->expr()->eq('item_type', $qb->createNamedParameter('file')), |
|
| 360 | + $qb->expr()->eq('item_type', $qb->createNamedParameter('folder')) |
|
| 361 | + )) |
|
| 362 | + ->execute(); |
|
| 363 | + |
|
| 364 | + $data = $stmt->fetch(); |
|
| 365 | + |
|
| 366 | + /* |
|
| 367 | 367 | * Check if there already is a user specific group share. |
| 368 | 368 | * If there is update it (if required). |
| 369 | 369 | */ |
| 370 | - if ($data === false) { |
|
| 371 | - $qb = $this->dbConn->getQueryBuilder(); |
|
| 372 | - |
|
| 373 | - $type = $share->getNodeType(); |
|
| 374 | - |
|
| 375 | - //Insert new share |
|
| 376 | - $qb->insert('share') |
|
| 377 | - ->values([ |
|
| 378 | - 'share_type' => $qb->createNamedParameter(self::SHARE_TYPE_USERGROUP), |
|
| 379 | - 'share_with' => $qb->createNamedParameter($recipient), |
|
| 380 | - 'uid_owner' => $qb->createNamedParameter($share->getShareOwner()), |
|
| 381 | - 'uid_initiator' => $qb->createNamedParameter($share->getSharedBy()), |
|
| 382 | - 'parent' => $qb->createNamedParameter($share->getId()), |
|
| 383 | - 'item_type' => $qb->createNamedParameter($type), |
|
| 384 | - 'item_source' => $qb->createNamedParameter($share->getNodeId()), |
|
| 385 | - 'file_source' => $qb->createNamedParameter($share->getNodeId()), |
|
| 386 | - 'file_target' => $qb->createNamedParameter($share->getTarget()), |
|
| 387 | - 'permissions' => $qb->createNamedParameter(0), |
|
| 388 | - 'stime' => $qb->createNamedParameter($share->getShareTime()->getTimestamp()), |
|
| 389 | - ])->execute(); |
|
| 390 | - |
|
| 391 | - } else if ($data['permissions'] !== 0) { |
|
| 392 | - |
|
| 393 | - // Update existing usergroup share |
|
| 394 | - $qb = $this->dbConn->getQueryBuilder(); |
|
| 395 | - $qb->update('share') |
|
| 396 | - ->set('permissions', $qb->createNamedParameter(0)) |
|
| 397 | - ->where($qb->expr()->eq('id', $qb->createNamedParameter($data['id']))) |
|
| 398 | - ->execute(); |
|
| 399 | - } |
|
| 400 | - |
|
| 401 | - } else if ($share->getShareType() === \OCP\Share::SHARE_TYPE_USER) { |
|
| 402 | - |
|
| 403 | - if ($share->getSharedWith() !== $recipient) { |
|
| 404 | - throw new ProviderException('Recipient does not match'); |
|
| 405 | - } |
|
| 406 | - |
|
| 407 | - // We can just delete user and link shares |
|
| 408 | - $this->delete($share); |
|
| 409 | - } else { |
|
| 410 | - throw new ProviderException('Invalid shareType'); |
|
| 411 | - } |
|
| 412 | - } |
|
| 413 | - |
|
| 414 | - /** |
|
| 415 | - * @inheritdoc |
|
| 416 | - */ |
|
| 417 | - public function move(\OCP\Share\IShare $share, $recipient) { |
|
| 418 | - if ($share->getShareType() === \OCP\Share::SHARE_TYPE_USER) { |
|
| 419 | - // Just update the target |
|
| 420 | - $qb = $this->dbConn->getQueryBuilder(); |
|
| 421 | - $qb->update('share') |
|
| 422 | - ->set('file_target', $qb->createNamedParameter($share->getTarget())) |
|
| 423 | - ->where($qb->expr()->eq('id', $qb->createNamedParameter($share->getId()))) |
|
| 424 | - ->execute(); |
|
| 425 | - |
|
| 426 | - } else if ($share->getShareType() === \OCP\Share::SHARE_TYPE_GROUP) { |
|
| 427 | - |
|
| 428 | - // Check if there is a usergroup share |
|
| 429 | - $qb = $this->dbConn->getQueryBuilder(); |
|
| 430 | - $stmt = $qb->select('id') |
|
| 431 | - ->from('share') |
|
| 432 | - ->where($qb->expr()->eq('share_type', $qb->createNamedParameter(self::SHARE_TYPE_USERGROUP))) |
|
| 433 | - ->andWhere($qb->expr()->eq('share_with', $qb->createNamedParameter($recipient))) |
|
| 434 | - ->andWhere($qb->expr()->eq('parent', $qb->createNamedParameter($share->getId()))) |
|
| 435 | - ->andWhere($qb->expr()->orX( |
|
| 436 | - $qb->expr()->eq('item_type', $qb->createNamedParameter('file')), |
|
| 437 | - $qb->expr()->eq('item_type', $qb->createNamedParameter('folder')) |
|
| 438 | - )) |
|
| 439 | - ->setMaxResults(1) |
|
| 440 | - ->execute(); |
|
| 441 | - |
|
| 442 | - $data = $stmt->fetch(); |
|
| 443 | - $stmt->closeCursor(); |
|
| 444 | - |
|
| 445 | - if ($data === false) { |
|
| 446 | - // No usergroup share yet. Create one. |
|
| 447 | - $qb = $this->dbConn->getQueryBuilder(); |
|
| 448 | - $qb->insert('share') |
|
| 449 | - ->values([ |
|
| 450 | - 'share_type' => $qb->createNamedParameter(self::SHARE_TYPE_USERGROUP), |
|
| 451 | - 'share_with' => $qb->createNamedParameter($recipient), |
|
| 452 | - 'uid_owner' => $qb->createNamedParameter($share->getShareOwner()), |
|
| 453 | - 'uid_initiator' => $qb->createNamedParameter($share->getSharedBy()), |
|
| 454 | - 'parent' => $qb->createNamedParameter($share->getId()), |
|
| 455 | - 'item_type' => $qb->createNamedParameter($share->getNodeType()), |
|
| 456 | - 'item_source' => $qb->createNamedParameter($share->getNodeId()), |
|
| 457 | - 'file_source' => $qb->createNamedParameter($share->getNodeId()), |
|
| 458 | - 'file_target' => $qb->createNamedParameter($share->getTarget()), |
|
| 459 | - 'permissions' => $qb->createNamedParameter($share->getPermissions()), |
|
| 460 | - 'stime' => $qb->createNamedParameter($share->getShareTime()->getTimestamp()), |
|
| 461 | - ])->execute(); |
|
| 462 | - } else { |
|
| 463 | - // Already a usergroup share. Update it. |
|
| 464 | - $qb = $this->dbConn->getQueryBuilder(); |
|
| 465 | - $qb->update('share') |
|
| 466 | - ->set('file_target', $qb->createNamedParameter($share->getTarget())) |
|
| 467 | - ->where($qb->expr()->eq('id', $qb->createNamedParameter($data['id']))) |
|
| 468 | - ->execute(); |
|
| 469 | - } |
|
| 470 | - } |
|
| 471 | - |
|
| 472 | - return $share; |
|
| 473 | - } |
|
| 474 | - |
|
| 475 | - public function getSharesInFolder($userId, Folder $node, $reshares) { |
|
| 476 | - $qb = $this->dbConn->getQueryBuilder(); |
|
| 477 | - $qb->select('*') |
|
| 478 | - ->from('share', 's') |
|
| 479 | - ->andWhere($qb->expr()->orX( |
|
| 480 | - $qb->expr()->eq('item_type', $qb->createNamedParameter('file')), |
|
| 481 | - $qb->expr()->eq('item_type', $qb->createNamedParameter('folder')) |
|
| 482 | - )); |
|
| 483 | - |
|
| 484 | - $qb->andWhere($qb->expr()->orX( |
|
| 485 | - $qb->expr()->eq('share_type', $qb->createNamedParameter(\OCP\Share::SHARE_TYPE_USER)), |
|
| 486 | - $qb->expr()->eq('share_type', $qb->createNamedParameter(\OCP\Share::SHARE_TYPE_GROUP)), |
|
| 487 | - $qb->expr()->eq('share_type', $qb->createNamedParameter(\OCP\Share::SHARE_TYPE_LINK)) |
|
| 488 | - )); |
|
| 489 | - |
|
| 490 | - /** |
|
| 491 | - * Reshares for this user are shares where they are the owner. |
|
| 492 | - */ |
|
| 493 | - if ($reshares === false) { |
|
| 494 | - $qb->andWhere($qb->expr()->eq('uid_initiator', $qb->createNamedParameter($userId))); |
|
| 495 | - } else { |
|
| 496 | - $qb->andWhere( |
|
| 497 | - $qb->expr()->orX( |
|
| 498 | - $qb->expr()->eq('uid_owner', $qb->createNamedParameter($userId)), |
|
| 499 | - $qb->expr()->eq('uid_initiator', $qb->createNamedParameter($userId)) |
|
| 500 | - ) |
|
| 501 | - ); |
|
| 502 | - } |
|
| 503 | - |
|
| 504 | - $qb->innerJoin('s', 'filecache' ,'f', $qb->expr()->eq('s.file_source', 'f.fileid')); |
|
| 505 | - $qb->andWhere($qb->expr()->eq('f.parent', $qb->createNamedParameter($node->getId()))); |
|
| 506 | - |
|
| 507 | - $qb->orderBy('id'); |
|
| 508 | - |
|
| 509 | - $cursor = $qb->execute(); |
|
| 510 | - $shares = []; |
|
| 511 | - while ($data = $cursor->fetch()) { |
|
| 512 | - $shares[$data['fileid']][] = $this->createShare($data); |
|
| 513 | - } |
|
| 514 | - $cursor->closeCursor(); |
|
| 515 | - |
|
| 516 | - return $shares; |
|
| 517 | - } |
|
| 518 | - |
|
| 519 | - /** |
|
| 520 | - * @inheritdoc |
|
| 521 | - */ |
|
| 522 | - public function getSharesBy($userId, $shareType, $node, $reshares, $limit, $offset) { |
|
| 523 | - $qb = $this->dbConn->getQueryBuilder(); |
|
| 524 | - $qb->select('*') |
|
| 525 | - ->from('share') |
|
| 526 | - ->andWhere($qb->expr()->orX( |
|
| 527 | - $qb->expr()->eq('item_type', $qb->createNamedParameter('file')), |
|
| 528 | - $qb->expr()->eq('item_type', $qb->createNamedParameter('folder')) |
|
| 529 | - )); |
|
| 530 | - |
|
| 531 | - $qb->andWhere($qb->expr()->eq('share_type', $qb->createNamedParameter($shareType))); |
|
| 532 | - |
|
| 533 | - /** |
|
| 534 | - * Reshares for this user are shares where they are the owner. |
|
| 535 | - */ |
|
| 536 | - if ($reshares === false) { |
|
| 537 | - $qb->andWhere($qb->expr()->eq('uid_initiator', $qb->createNamedParameter($userId))); |
|
| 538 | - } else { |
|
| 539 | - $qb->andWhere( |
|
| 540 | - $qb->expr()->orX( |
|
| 541 | - $qb->expr()->eq('uid_owner', $qb->createNamedParameter($userId)), |
|
| 542 | - $qb->expr()->eq('uid_initiator', $qb->createNamedParameter($userId)) |
|
| 543 | - ) |
|
| 544 | - ); |
|
| 545 | - } |
|
| 546 | - |
|
| 547 | - if ($node !== null) { |
|
| 548 | - $qb->andWhere($qb->expr()->eq('file_source', $qb->createNamedParameter($node->getId()))); |
|
| 549 | - } |
|
| 550 | - |
|
| 551 | - if ($limit !== -1) { |
|
| 552 | - $qb->setMaxResults($limit); |
|
| 553 | - } |
|
| 554 | - |
|
| 555 | - $qb->setFirstResult($offset); |
|
| 556 | - $qb->orderBy('id'); |
|
| 557 | - |
|
| 558 | - $cursor = $qb->execute(); |
|
| 559 | - $shares = []; |
|
| 560 | - while($data = $cursor->fetch()) { |
|
| 561 | - $shares[] = $this->createShare($data); |
|
| 562 | - } |
|
| 563 | - $cursor->closeCursor(); |
|
| 564 | - |
|
| 565 | - return $shares; |
|
| 566 | - } |
|
| 567 | - |
|
| 568 | - /** |
|
| 569 | - * @inheritdoc |
|
| 570 | - */ |
|
| 571 | - public function getShareById($id, $recipientId = null) { |
|
| 572 | - $qb = $this->dbConn->getQueryBuilder(); |
|
| 573 | - |
|
| 574 | - $qb->select('*') |
|
| 575 | - ->from('share') |
|
| 576 | - ->where($qb->expr()->eq('id', $qb->createNamedParameter($id))) |
|
| 577 | - ->andWhere( |
|
| 578 | - $qb->expr()->in( |
|
| 579 | - 'share_type', |
|
| 580 | - $qb->createNamedParameter([ |
|
| 581 | - \OCP\Share::SHARE_TYPE_USER, |
|
| 582 | - \OCP\Share::SHARE_TYPE_GROUP, |
|
| 583 | - \OCP\Share::SHARE_TYPE_LINK, |
|
| 584 | - ], IQueryBuilder::PARAM_INT_ARRAY) |
|
| 585 | - ) |
|
| 586 | - ) |
|
| 587 | - ->andWhere($qb->expr()->orX( |
|
| 588 | - $qb->expr()->eq('item_type', $qb->createNamedParameter('file')), |
|
| 589 | - $qb->expr()->eq('item_type', $qb->createNamedParameter('folder')) |
|
| 590 | - )); |
|
| 591 | - |
|
| 592 | - $cursor = $qb->execute(); |
|
| 593 | - $data = $cursor->fetch(); |
|
| 594 | - $cursor->closeCursor(); |
|
| 595 | - |
|
| 596 | - if ($data === false) { |
|
| 597 | - throw new ShareNotFound(); |
|
| 598 | - } |
|
| 599 | - |
|
| 600 | - try { |
|
| 601 | - $share = $this->createShare($data); |
|
| 602 | - } catch (InvalidShare $e) { |
|
| 603 | - throw new ShareNotFound(); |
|
| 604 | - } |
|
| 605 | - |
|
| 606 | - // If the recipient is set for a group share resolve to that user |
|
| 607 | - if ($recipientId !== null && $share->getShareType() === \OCP\Share::SHARE_TYPE_GROUP) { |
|
| 608 | - $share = $this->resolveGroupShares([$share], $recipientId)[0]; |
|
| 609 | - } |
|
| 610 | - |
|
| 611 | - return $share; |
|
| 612 | - } |
|
| 613 | - |
|
| 614 | - /** |
|
| 615 | - * Get shares for a given path |
|
| 616 | - * |
|
| 617 | - * @param \OCP\Files\Node $path |
|
| 618 | - * @return \OCP\Share\IShare[] |
|
| 619 | - */ |
|
| 620 | - public function getSharesByPath(Node $path) { |
|
| 621 | - $qb = $this->dbConn->getQueryBuilder(); |
|
| 622 | - |
|
| 623 | - $cursor = $qb->select('*') |
|
| 624 | - ->from('share') |
|
| 625 | - ->andWhere($qb->expr()->eq('file_source', $qb->createNamedParameter($path->getId()))) |
|
| 626 | - ->andWhere( |
|
| 627 | - $qb->expr()->orX( |
|
| 628 | - $qb->expr()->eq('share_type', $qb->createNamedParameter(\OCP\Share::SHARE_TYPE_USER)), |
|
| 629 | - $qb->expr()->eq('share_type', $qb->createNamedParameter(\OCP\Share::SHARE_TYPE_GROUP)) |
|
| 630 | - ) |
|
| 631 | - ) |
|
| 632 | - ->andWhere($qb->expr()->orX( |
|
| 633 | - $qb->expr()->eq('item_type', $qb->createNamedParameter('file')), |
|
| 634 | - $qb->expr()->eq('item_type', $qb->createNamedParameter('folder')) |
|
| 635 | - )) |
|
| 636 | - ->execute(); |
|
| 637 | - |
|
| 638 | - $shares = []; |
|
| 639 | - while($data = $cursor->fetch()) { |
|
| 640 | - $shares[] = $this->createShare($data); |
|
| 641 | - } |
|
| 642 | - $cursor->closeCursor(); |
|
| 643 | - |
|
| 644 | - return $shares; |
|
| 645 | - } |
|
| 646 | - |
|
| 647 | - /** |
|
| 648 | - * Returns whether the given database result can be interpreted as |
|
| 649 | - * a share with accessible file (not trashed, not deleted) |
|
| 650 | - */ |
|
| 651 | - private function isAccessibleResult($data) { |
|
| 652 | - // exclude shares leading to deleted file entries |
|
| 653 | - if ($data['fileid'] === null) { |
|
| 654 | - return false; |
|
| 655 | - } |
|
| 656 | - |
|
| 657 | - // exclude shares leading to trashbin on home storages |
|
| 658 | - $pathSections = explode('/', $data['path'], 2); |
|
| 659 | - // FIXME: would not detect rare md5'd home storage case properly |
|
| 660 | - if ($pathSections[0] !== 'files' |
|
| 661 | - && in_array(explode(':', $data['storage_string_id'], 2)[0], array('home', 'object'))) { |
|
| 662 | - return false; |
|
| 663 | - } |
|
| 664 | - return true; |
|
| 665 | - } |
|
| 666 | - |
|
| 667 | - /** |
|
| 668 | - * @inheritdoc |
|
| 669 | - */ |
|
| 670 | - public function getSharedWith($userId, $shareType, $node, $limit, $offset) { |
|
| 671 | - /** @var Share[] $shares */ |
|
| 672 | - $shares = []; |
|
| 673 | - |
|
| 674 | - if ($shareType === \OCP\Share::SHARE_TYPE_USER) { |
|
| 675 | - //Get shares directly with this user |
|
| 676 | - $qb = $this->dbConn->getQueryBuilder(); |
|
| 677 | - $qb->select('s.*', |
|
| 678 | - 'f.fileid', 'f.path', 'f.permissions AS f_permissions', 'f.storage', 'f.path_hash', |
|
| 679 | - 'f.parent AS f_parent', 'f.name', 'f.mimetype', 'f.mimepart', 'f.size', 'f.mtime', 'f.storage_mtime', |
|
| 680 | - 'f.encrypted', 'f.unencrypted_size', 'f.etag', 'f.checksum' |
|
| 681 | - ) |
|
| 682 | - ->selectAlias('st.id', 'storage_string_id') |
|
| 683 | - ->from('share', 's') |
|
| 684 | - ->leftJoin('s', 'filecache', 'f', $qb->expr()->eq('s.file_source', 'f.fileid')) |
|
| 685 | - ->leftJoin('f', 'storages', 'st', $qb->expr()->eq('f.storage', 'st.numeric_id')); |
|
| 686 | - |
|
| 687 | - // Order by id |
|
| 688 | - $qb->orderBy('s.id'); |
|
| 689 | - |
|
| 690 | - // Set limit and offset |
|
| 691 | - if ($limit !== -1) { |
|
| 692 | - $qb->setMaxResults($limit); |
|
| 693 | - } |
|
| 694 | - $qb->setFirstResult($offset); |
|
| 695 | - |
|
| 696 | - $qb->where($qb->expr()->eq('share_type', $qb->createNamedParameter(\OCP\Share::SHARE_TYPE_USER))) |
|
| 697 | - ->andWhere($qb->expr()->eq('share_with', $qb->createNamedParameter($userId))) |
|
| 698 | - ->andWhere($qb->expr()->orX( |
|
| 699 | - $qb->expr()->eq('item_type', $qb->createNamedParameter('file')), |
|
| 700 | - $qb->expr()->eq('item_type', $qb->createNamedParameter('folder')) |
|
| 701 | - )); |
|
| 702 | - |
|
| 703 | - // Filter by node if provided |
|
| 704 | - if ($node !== null) { |
|
| 705 | - $qb->andWhere($qb->expr()->eq('file_source', $qb->createNamedParameter($node->getId()))); |
|
| 706 | - } |
|
| 707 | - |
|
| 708 | - $cursor = $qb->execute(); |
|
| 709 | - |
|
| 710 | - while($data = $cursor->fetch()) { |
|
| 711 | - if ($this->isAccessibleResult($data)) { |
|
| 712 | - $shares[] = $this->createShare($data); |
|
| 713 | - } |
|
| 714 | - } |
|
| 715 | - $cursor->closeCursor(); |
|
| 716 | - |
|
| 717 | - } else if ($shareType === \OCP\Share::SHARE_TYPE_GROUP) { |
|
| 718 | - $user = $this->userManager->get($userId); |
|
| 719 | - $allGroups = $this->groupManager->getUserGroups($user); |
|
| 720 | - |
|
| 721 | - /** @var Share[] $shares2 */ |
|
| 722 | - $shares2 = []; |
|
| 723 | - |
|
| 724 | - $start = 0; |
|
| 725 | - while(true) { |
|
| 726 | - $groups = array_slice($allGroups, $start, 100); |
|
| 727 | - $start += 100; |
|
| 728 | - |
|
| 729 | - if ($groups === []) { |
|
| 730 | - break; |
|
| 731 | - } |
|
| 732 | - |
|
| 733 | - $qb = $this->dbConn->getQueryBuilder(); |
|
| 734 | - $qb->select('s.*', |
|
| 735 | - 'f.fileid', 'f.path', 'f.permissions AS f_permissions', 'f.storage', 'f.path_hash', |
|
| 736 | - 'f.parent AS f_parent', 'f.name', 'f.mimetype', 'f.mimepart', 'f.size', 'f.mtime', 'f.storage_mtime', |
|
| 737 | - 'f.encrypted', 'f.unencrypted_size', 'f.etag', 'f.checksum' |
|
| 738 | - ) |
|
| 739 | - ->selectAlias('st.id', 'storage_string_id') |
|
| 740 | - ->from('share', 's') |
|
| 741 | - ->leftJoin('s', 'filecache', 'f', $qb->expr()->eq('s.file_source', 'f.fileid')) |
|
| 742 | - ->leftJoin('f', 'storages', 'st', $qb->expr()->eq('f.storage', 'st.numeric_id')) |
|
| 743 | - ->orderBy('s.id') |
|
| 744 | - ->setFirstResult(0); |
|
| 745 | - |
|
| 746 | - if ($limit !== -1) { |
|
| 747 | - $qb->setMaxResults($limit - count($shares)); |
|
| 748 | - } |
|
| 749 | - |
|
| 750 | - // Filter by node if provided |
|
| 751 | - if ($node !== null) { |
|
| 752 | - $qb->andWhere($qb->expr()->eq('file_source', $qb->createNamedParameter($node->getId()))); |
|
| 753 | - } |
|
| 754 | - |
|
| 755 | - |
|
| 756 | - $groups = array_filter($groups, function($group) { return $group instanceof IGroup; }); |
|
| 757 | - $groups = array_map(function(IGroup $group) { return $group->getGID(); }, $groups); |
|
| 758 | - |
|
| 759 | - $qb->andWhere($qb->expr()->eq('share_type', $qb->createNamedParameter(\OCP\Share::SHARE_TYPE_GROUP))) |
|
| 760 | - ->andWhere($qb->expr()->in('share_with', $qb->createNamedParameter( |
|
| 761 | - $groups, |
|
| 762 | - IQueryBuilder::PARAM_STR_ARRAY |
|
| 763 | - ))) |
|
| 764 | - ->andWhere($qb->expr()->orX( |
|
| 765 | - $qb->expr()->eq('item_type', $qb->createNamedParameter('file')), |
|
| 766 | - $qb->expr()->eq('item_type', $qb->createNamedParameter('folder')) |
|
| 767 | - )); |
|
| 768 | - |
|
| 769 | - $cursor = $qb->execute(); |
|
| 770 | - while($data = $cursor->fetch()) { |
|
| 771 | - if ($offset > 0) { |
|
| 772 | - $offset--; |
|
| 773 | - continue; |
|
| 774 | - } |
|
| 775 | - |
|
| 776 | - if ($this->isAccessibleResult($data)) { |
|
| 777 | - $shares2[] = $this->createShare($data); |
|
| 778 | - } |
|
| 779 | - } |
|
| 780 | - $cursor->closeCursor(); |
|
| 781 | - } |
|
| 782 | - |
|
| 783 | - /* |
|
| 370 | + if ($data === false) { |
|
| 371 | + $qb = $this->dbConn->getQueryBuilder(); |
|
| 372 | + |
|
| 373 | + $type = $share->getNodeType(); |
|
| 374 | + |
|
| 375 | + //Insert new share |
|
| 376 | + $qb->insert('share') |
|
| 377 | + ->values([ |
|
| 378 | + 'share_type' => $qb->createNamedParameter(self::SHARE_TYPE_USERGROUP), |
|
| 379 | + 'share_with' => $qb->createNamedParameter($recipient), |
|
| 380 | + 'uid_owner' => $qb->createNamedParameter($share->getShareOwner()), |
|
| 381 | + 'uid_initiator' => $qb->createNamedParameter($share->getSharedBy()), |
|
| 382 | + 'parent' => $qb->createNamedParameter($share->getId()), |
|
| 383 | + 'item_type' => $qb->createNamedParameter($type), |
|
| 384 | + 'item_source' => $qb->createNamedParameter($share->getNodeId()), |
|
| 385 | + 'file_source' => $qb->createNamedParameter($share->getNodeId()), |
|
| 386 | + 'file_target' => $qb->createNamedParameter($share->getTarget()), |
|
| 387 | + 'permissions' => $qb->createNamedParameter(0), |
|
| 388 | + 'stime' => $qb->createNamedParameter($share->getShareTime()->getTimestamp()), |
|
| 389 | + ])->execute(); |
|
| 390 | + |
|
| 391 | + } else if ($data['permissions'] !== 0) { |
|
| 392 | + |
|
| 393 | + // Update existing usergroup share |
|
| 394 | + $qb = $this->dbConn->getQueryBuilder(); |
|
| 395 | + $qb->update('share') |
|
| 396 | + ->set('permissions', $qb->createNamedParameter(0)) |
|
| 397 | + ->where($qb->expr()->eq('id', $qb->createNamedParameter($data['id']))) |
|
| 398 | + ->execute(); |
|
| 399 | + } |
|
| 400 | + |
|
| 401 | + } else if ($share->getShareType() === \OCP\Share::SHARE_TYPE_USER) { |
|
| 402 | + |
|
| 403 | + if ($share->getSharedWith() !== $recipient) { |
|
| 404 | + throw new ProviderException('Recipient does not match'); |
|
| 405 | + } |
|
| 406 | + |
|
| 407 | + // We can just delete user and link shares |
|
| 408 | + $this->delete($share); |
|
| 409 | + } else { |
|
| 410 | + throw new ProviderException('Invalid shareType'); |
|
| 411 | + } |
|
| 412 | + } |
|
| 413 | + |
|
| 414 | + /** |
|
| 415 | + * @inheritdoc |
|
| 416 | + */ |
|
| 417 | + public function move(\OCP\Share\IShare $share, $recipient) { |
|
| 418 | + if ($share->getShareType() === \OCP\Share::SHARE_TYPE_USER) { |
|
| 419 | + // Just update the target |
|
| 420 | + $qb = $this->dbConn->getQueryBuilder(); |
|
| 421 | + $qb->update('share') |
|
| 422 | + ->set('file_target', $qb->createNamedParameter($share->getTarget())) |
|
| 423 | + ->where($qb->expr()->eq('id', $qb->createNamedParameter($share->getId()))) |
|
| 424 | + ->execute(); |
|
| 425 | + |
|
| 426 | + } else if ($share->getShareType() === \OCP\Share::SHARE_TYPE_GROUP) { |
|
| 427 | + |
|
| 428 | + // Check if there is a usergroup share |
|
| 429 | + $qb = $this->dbConn->getQueryBuilder(); |
|
| 430 | + $stmt = $qb->select('id') |
|
| 431 | + ->from('share') |
|
| 432 | + ->where($qb->expr()->eq('share_type', $qb->createNamedParameter(self::SHARE_TYPE_USERGROUP))) |
|
| 433 | + ->andWhere($qb->expr()->eq('share_with', $qb->createNamedParameter($recipient))) |
|
| 434 | + ->andWhere($qb->expr()->eq('parent', $qb->createNamedParameter($share->getId()))) |
|
| 435 | + ->andWhere($qb->expr()->orX( |
|
| 436 | + $qb->expr()->eq('item_type', $qb->createNamedParameter('file')), |
|
| 437 | + $qb->expr()->eq('item_type', $qb->createNamedParameter('folder')) |
|
| 438 | + )) |
|
| 439 | + ->setMaxResults(1) |
|
| 440 | + ->execute(); |
|
| 441 | + |
|
| 442 | + $data = $stmt->fetch(); |
|
| 443 | + $stmt->closeCursor(); |
|
| 444 | + |
|
| 445 | + if ($data === false) { |
|
| 446 | + // No usergroup share yet. Create one. |
|
| 447 | + $qb = $this->dbConn->getQueryBuilder(); |
|
| 448 | + $qb->insert('share') |
|
| 449 | + ->values([ |
|
| 450 | + 'share_type' => $qb->createNamedParameter(self::SHARE_TYPE_USERGROUP), |
|
| 451 | + 'share_with' => $qb->createNamedParameter($recipient), |
|
| 452 | + 'uid_owner' => $qb->createNamedParameter($share->getShareOwner()), |
|
| 453 | + 'uid_initiator' => $qb->createNamedParameter($share->getSharedBy()), |
|
| 454 | + 'parent' => $qb->createNamedParameter($share->getId()), |
|
| 455 | + 'item_type' => $qb->createNamedParameter($share->getNodeType()), |
|
| 456 | + 'item_source' => $qb->createNamedParameter($share->getNodeId()), |
|
| 457 | + 'file_source' => $qb->createNamedParameter($share->getNodeId()), |
|
| 458 | + 'file_target' => $qb->createNamedParameter($share->getTarget()), |
|
| 459 | + 'permissions' => $qb->createNamedParameter($share->getPermissions()), |
|
| 460 | + 'stime' => $qb->createNamedParameter($share->getShareTime()->getTimestamp()), |
|
| 461 | + ])->execute(); |
|
| 462 | + } else { |
|
| 463 | + // Already a usergroup share. Update it. |
|
| 464 | + $qb = $this->dbConn->getQueryBuilder(); |
|
| 465 | + $qb->update('share') |
|
| 466 | + ->set('file_target', $qb->createNamedParameter($share->getTarget())) |
|
| 467 | + ->where($qb->expr()->eq('id', $qb->createNamedParameter($data['id']))) |
|
| 468 | + ->execute(); |
|
| 469 | + } |
|
| 470 | + } |
|
| 471 | + |
|
| 472 | + return $share; |
|
| 473 | + } |
|
| 474 | + |
|
| 475 | + public function getSharesInFolder($userId, Folder $node, $reshares) { |
|
| 476 | + $qb = $this->dbConn->getQueryBuilder(); |
|
| 477 | + $qb->select('*') |
|
| 478 | + ->from('share', 's') |
|
| 479 | + ->andWhere($qb->expr()->orX( |
|
| 480 | + $qb->expr()->eq('item_type', $qb->createNamedParameter('file')), |
|
| 481 | + $qb->expr()->eq('item_type', $qb->createNamedParameter('folder')) |
|
| 482 | + )); |
|
| 483 | + |
|
| 484 | + $qb->andWhere($qb->expr()->orX( |
|
| 485 | + $qb->expr()->eq('share_type', $qb->createNamedParameter(\OCP\Share::SHARE_TYPE_USER)), |
|
| 486 | + $qb->expr()->eq('share_type', $qb->createNamedParameter(\OCP\Share::SHARE_TYPE_GROUP)), |
|
| 487 | + $qb->expr()->eq('share_type', $qb->createNamedParameter(\OCP\Share::SHARE_TYPE_LINK)) |
|
| 488 | + )); |
|
| 489 | + |
|
| 490 | + /** |
|
| 491 | + * Reshares for this user are shares where they are the owner. |
|
| 492 | + */ |
|
| 493 | + if ($reshares === false) { |
|
| 494 | + $qb->andWhere($qb->expr()->eq('uid_initiator', $qb->createNamedParameter($userId))); |
|
| 495 | + } else { |
|
| 496 | + $qb->andWhere( |
|
| 497 | + $qb->expr()->orX( |
|
| 498 | + $qb->expr()->eq('uid_owner', $qb->createNamedParameter($userId)), |
|
| 499 | + $qb->expr()->eq('uid_initiator', $qb->createNamedParameter($userId)) |
|
| 500 | + ) |
|
| 501 | + ); |
|
| 502 | + } |
|
| 503 | + |
|
| 504 | + $qb->innerJoin('s', 'filecache' ,'f', $qb->expr()->eq('s.file_source', 'f.fileid')); |
|
| 505 | + $qb->andWhere($qb->expr()->eq('f.parent', $qb->createNamedParameter($node->getId()))); |
|
| 506 | + |
|
| 507 | + $qb->orderBy('id'); |
|
| 508 | + |
|
| 509 | + $cursor = $qb->execute(); |
|
| 510 | + $shares = []; |
|
| 511 | + while ($data = $cursor->fetch()) { |
|
| 512 | + $shares[$data['fileid']][] = $this->createShare($data); |
|
| 513 | + } |
|
| 514 | + $cursor->closeCursor(); |
|
| 515 | + |
|
| 516 | + return $shares; |
|
| 517 | + } |
|
| 518 | + |
|
| 519 | + /** |
|
| 520 | + * @inheritdoc |
|
| 521 | + */ |
|
| 522 | + public function getSharesBy($userId, $shareType, $node, $reshares, $limit, $offset) { |
|
| 523 | + $qb = $this->dbConn->getQueryBuilder(); |
|
| 524 | + $qb->select('*') |
|
| 525 | + ->from('share') |
|
| 526 | + ->andWhere($qb->expr()->orX( |
|
| 527 | + $qb->expr()->eq('item_type', $qb->createNamedParameter('file')), |
|
| 528 | + $qb->expr()->eq('item_type', $qb->createNamedParameter('folder')) |
|
| 529 | + )); |
|
| 530 | + |
|
| 531 | + $qb->andWhere($qb->expr()->eq('share_type', $qb->createNamedParameter($shareType))); |
|
| 532 | + |
|
| 533 | + /** |
|
| 534 | + * Reshares for this user are shares where they are the owner. |
|
| 535 | + */ |
|
| 536 | + if ($reshares === false) { |
|
| 537 | + $qb->andWhere($qb->expr()->eq('uid_initiator', $qb->createNamedParameter($userId))); |
|
| 538 | + } else { |
|
| 539 | + $qb->andWhere( |
|
| 540 | + $qb->expr()->orX( |
|
| 541 | + $qb->expr()->eq('uid_owner', $qb->createNamedParameter($userId)), |
|
| 542 | + $qb->expr()->eq('uid_initiator', $qb->createNamedParameter($userId)) |
|
| 543 | + ) |
|
| 544 | + ); |
|
| 545 | + } |
|
| 546 | + |
|
| 547 | + if ($node !== null) { |
|
| 548 | + $qb->andWhere($qb->expr()->eq('file_source', $qb->createNamedParameter($node->getId()))); |
|
| 549 | + } |
|
| 550 | + |
|
| 551 | + if ($limit !== -1) { |
|
| 552 | + $qb->setMaxResults($limit); |
|
| 553 | + } |
|
| 554 | + |
|
| 555 | + $qb->setFirstResult($offset); |
|
| 556 | + $qb->orderBy('id'); |
|
| 557 | + |
|
| 558 | + $cursor = $qb->execute(); |
|
| 559 | + $shares = []; |
|
| 560 | + while($data = $cursor->fetch()) { |
|
| 561 | + $shares[] = $this->createShare($data); |
|
| 562 | + } |
|
| 563 | + $cursor->closeCursor(); |
|
| 564 | + |
|
| 565 | + return $shares; |
|
| 566 | + } |
|
| 567 | + |
|
| 568 | + /** |
|
| 569 | + * @inheritdoc |
|
| 570 | + */ |
|
| 571 | + public function getShareById($id, $recipientId = null) { |
|
| 572 | + $qb = $this->dbConn->getQueryBuilder(); |
|
| 573 | + |
|
| 574 | + $qb->select('*') |
|
| 575 | + ->from('share') |
|
| 576 | + ->where($qb->expr()->eq('id', $qb->createNamedParameter($id))) |
|
| 577 | + ->andWhere( |
|
| 578 | + $qb->expr()->in( |
|
| 579 | + 'share_type', |
|
| 580 | + $qb->createNamedParameter([ |
|
| 581 | + \OCP\Share::SHARE_TYPE_USER, |
|
| 582 | + \OCP\Share::SHARE_TYPE_GROUP, |
|
| 583 | + \OCP\Share::SHARE_TYPE_LINK, |
|
| 584 | + ], IQueryBuilder::PARAM_INT_ARRAY) |
|
| 585 | + ) |
|
| 586 | + ) |
|
| 587 | + ->andWhere($qb->expr()->orX( |
|
| 588 | + $qb->expr()->eq('item_type', $qb->createNamedParameter('file')), |
|
| 589 | + $qb->expr()->eq('item_type', $qb->createNamedParameter('folder')) |
|
| 590 | + )); |
|
| 591 | + |
|
| 592 | + $cursor = $qb->execute(); |
|
| 593 | + $data = $cursor->fetch(); |
|
| 594 | + $cursor->closeCursor(); |
|
| 595 | + |
|
| 596 | + if ($data === false) { |
|
| 597 | + throw new ShareNotFound(); |
|
| 598 | + } |
|
| 599 | + |
|
| 600 | + try { |
|
| 601 | + $share = $this->createShare($data); |
|
| 602 | + } catch (InvalidShare $e) { |
|
| 603 | + throw new ShareNotFound(); |
|
| 604 | + } |
|
| 605 | + |
|
| 606 | + // If the recipient is set for a group share resolve to that user |
|
| 607 | + if ($recipientId !== null && $share->getShareType() === \OCP\Share::SHARE_TYPE_GROUP) { |
|
| 608 | + $share = $this->resolveGroupShares([$share], $recipientId)[0]; |
|
| 609 | + } |
|
| 610 | + |
|
| 611 | + return $share; |
|
| 612 | + } |
|
| 613 | + |
|
| 614 | + /** |
|
| 615 | + * Get shares for a given path |
|
| 616 | + * |
|
| 617 | + * @param \OCP\Files\Node $path |
|
| 618 | + * @return \OCP\Share\IShare[] |
|
| 619 | + */ |
|
| 620 | + public function getSharesByPath(Node $path) { |
|
| 621 | + $qb = $this->dbConn->getQueryBuilder(); |
|
| 622 | + |
|
| 623 | + $cursor = $qb->select('*') |
|
| 624 | + ->from('share') |
|
| 625 | + ->andWhere($qb->expr()->eq('file_source', $qb->createNamedParameter($path->getId()))) |
|
| 626 | + ->andWhere( |
|
| 627 | + $qb->expr()->orX( |
|
| 628 | + $qb->expr()->eq('share_type', $qb->createNamedParameter(\OCP\Share::SHARE_TYPE_USER)), |
|
| 629 | + $qb->expr()->eq('share_type', $qb->createNamedParameter(\OCP\Share::SHARE_TYPE_GROUP)) |
|
| 630 | + ) |
|
| 631 | + ) |
|
| 632 | + ->andWhere($qb->expr()->orX( |
|
| 633 | + $qb->expr()->eq('item_type', $qb->createNamedParameter('file')), |
|
| 634 | + $qb->expr()->eq('item_type', $qb->createNamedParameter('folder')) |
|
| 635 | + )) |
|
| 636 | + ->execute(); |
|
| 637 | + |
|
| 638 | + $shares = []; |
|
| 639 | + while($data = $cursor->fetch()) { |
|
| 640 | + $shares[] = $this->createShare($data); |
|
| 641 | + } |
|
| 642 | + $cursor->closeCursor(); |
|
| 643 | + |
|
| 644 | + return $shares; |
|
| 645 | + } |
|
| 646 | + |
|
| 647 | + /** |
|
| 648 | + * Returns whether the given database result can be interpreted as |
|
| 649 | + * a share with accessible file (not trashed, not deleted) |
|
| 650 | + */ |
|
| 651 | + private function isAccessibleResult($data) { |
|
| 652 | + // exclude shares leading to deleted file entries |
|
| 653 | + if ($data['fileid'] === null) { |
|
| 654 | + return false; |
|
| 655 | + } |
|
| 656 | + |
|
| 657 | + // exclude shares leading to trashbin on home storages |
|
| 658 | + $pathSections = explode('/', $data['path'], 2); |
|
| 659 | + // FIXME: would not detect rare md5'd home storage case properly |
|
| 660 | + if ($pathSections[0] !== 'files' |
|
| 661 | + && in_array(explode(':', $data['storage_string_id'], 2)[0], array('home', 'object'))) { |
|
| 662 | + return false; |
|
| 663 | + } |
|
| 664 | + return true; |
|
| 665 | + } |
|
| 666 | + |
|
| 667 | + /** |
|
| 668 | + * @inheritdoc |
|
| 669 | + */ |
|
| 670 | + public function getSharedWith($userId, $shareType, $node, $limit, $offset) { |
|
| 671 | + /** @var Share[] $shares */ |
|
| 672 | + $shares = []; |
|
| 673 | + |
|
| 674 | + if ($shareType === \OCP\Share::SHARE_TYPE_USER) { |
|
| 675 | + //Get shares directly with this user |
|
| 676 | + $qb = $this->dbConn->getQueryBuilder(); |
|
| 677 | + $qb->select('s.*', |
|
| 678 | + 'f.fileid', 'f.path', 'f.permissions AS f_permissions', 'f.storage', 'f.path_hash', |
|
| 679 | + 'f.parent AS f_parent', 'f.name', 'f.mimetype', 'f.mimepart', 'f.size', 'f.mtime', 'f.storage_mtime', |
|
| 680 | + 'f.encrypted', 'f.unencrypted_size', 'f.etag', 'f.checksum' |
|
| 681 | + ) |
|
| 682 | + ->selectAlias('st.id', 'storage_string_id') |
|
| 683 | + ->from('share', 's') |
|
| 684 | + ->leftJoin('s', 'filecache', 'f', $qb->expr()->eq('s.file_source', 'f.fileid')) |
|
| 685 | + ->leftJoin('f', 'storages', 'st', $qb->expr()->eq('f.storage', 'st.numeric_id')); |
|
| 686 | + |
|
| 687 | + // Order by id |
|
| 688 | + $qb->orderBy('s.id'); |
|
| 689 | + |
|
| 690 | + // Set limit and offset |
|
| 691 | + if ($limit !== -1) { |
|
| 692 | + $qb->setMaxResults($limit); |
|
| 693 | + } |
|
| 694 | + $qb->setFirstResult($offset); |
|
| 695 | + |
|
| 696 | + $qb->where($qb->expr()->eq('share_type', $qb->createNamedParameter(\OCP\Share::SHARE_TYPE_USER))) |
|
| 697 | + ->andWhere($qb->expr()->eq('share_with', $qb->createNamedParameter($userId))) |
|
| 698 | + ->andWhere($qb->expr()->orX( |
|
| 699 | + $qb->expr()->eq('item_type', $qb->createNamedParameter('file')), |
|
| 700 | + $qb->expr()->eq('item_type', $qb->createNamedParameter('folder')) |
|
| 701 | + )); |
|
| 702 | + |
|
| 703 | + // Filter by node if provided |
|
| 704 | + if ($node !== null) { |
|
| 705 | + $qb->andWhere($qb->expr()->eq('file_source', $qb->createNamedParameter($node->getId()))); |
|
| 706 | + } |
|
| 707 | + |
|
| 708 | + $cursor = $qb->execute(); |
|
| 709 | + |
|
| 710 | + while($data = $cursor->fetch()) { |
|
| 711 | + if ($this->isAccessibleResult($data)) { |
|
| 712 | + $shares[] = $this->createShare($data); |
|
| 713 | + } |
|
| 714 | + } |
|
| 715 | + $cursor->closeCursor(); |
|
| 716 | + |
|
| 717 | + } else if ($shareType === \OCP\Share::SHARE_TYPE_GROUP) { |
|
| 718 | + $user = $this->userManager->get($userId); |
|
| 719 | + $allGroups = $this->groupManager->getUserGroups($user); |
|
| 720 | + |
|
| 721 | + /** @var Share[] $shares2 */ |
|
| 722 | + $shares2 = []; |
|
| 723 | + |
|
| 724 | + $start = 0; |
|
| 725 | + while(true) { |
|
| 726 | + $groups = array_slice($allGroups, $start, 100); |
|
| 727 | + $start += 100; |
|
| 728 | + |
|
| 729 | + if ($groups === []) { |
|
| 730 | + break; |
|
| 731 | + } |
|
| 732 | + |
|
| 733 | + $qb = $this->dbConn->getQueryBuilder(); |
|
| 734 | + $qb->select('s.*', |
|
| 735 | + 'f.fileid', 'f.path', 'f.permissions AS f_permissions', 'f.storage', 'f.path_hash', |
|
| 736 | + 'f.parent AS f_parent', 'f.name', 'f.mimetype', 'f.mimepart', 'f.size', 'f.mtime', 'f.storage_mtime', |
|
| 737 | + 'f.encrypted', 'f.unencrypted_size', 'f.etag', 'f.checksum' |
|
| 738 | + ) |
|
| 739 | + ->selectAlias('st.id', 'storage_string_id') |
|
| 740 | + ->from('share', 's') |
|
| 741 | + ->leftJoin('s', 'filecache', 'f', $qb->expr()->eq('s.file_source', 'f.fileid')) |
|
| 742 | + ->leftJoin('f', 'storages', 'st', $qb->expr()->eq('f.storage', 'st.numeric_id')) |
|
| 743 | + ->orderBy('s.id') |
|
| 744 | + ->setFirstResult(0); |
|
| 745 | + |
|
| 746 | + if ($limit !== -1) { |
|
| 747 | + $qb->setMaxResults($limit - count($shares)); |
|
| 748 | + } |
|
| 749 | + |
|
| 750 | + // Filter by node if provided |
|
| 751 | + if ($node !== null) { |
|
| 752 | + $qb->andWhere($qb->expr()->eq('file_source', $qb->createNamedParameter($node->getId()))); |
|
| 753 | + } |
|
| 754 | + |
|
| 755 | + |
|
| 756 | + $groups = array_filter($groups, function($group) { return $group instanceof IGroup; }); |
|
| 757 | + $groups = array_map(function(IGroup $group) { return $group->getGID(); }, $groups); |
|
| 758 | + |
|
| 759 | + $qb->andWhere($qb->expr()->eq('share_type', $qb->createNamedParameter(\OCP\Share::SHARE_TYPE_GROUP))) |
|
| 760 | + ->andWhere($qb->expr()->in('share_with', $qb->createNamedParameter( |
|
| 761 | + $groups, |
|
| 762 | + IQueryBuilder::PARAM_STR_ARRAY |
|
| 763 | + ))) |
|
| 764 | + ->andWhere($qb->expr()->orX( |
|
| 765 | + $qb->expr()->eq('item_type', $qb->createNamedParameter('file')), |
|
| 766 | + $qb->expr()->eq('item_type', $qb->createNamedParameter('folder')) |
|
| 767 | + )); |
|
| 768 | + |
|
| 769 | + $cursor = $qb->execute(); |
|
| 770 | + while($data = $cursor->fetch()) { |
|
| 771 | + if ($offset > 0) { |
|
| 772 | + $offset--; |
|
| 773 | + continue; |
|
| 774 | + } |
|
| 775 | + |
|
| 776 | + if ($this->isAccessibleResult($data)) { |
|
| 777 | + $shares2[] = $this->createShare($data); |
|
| 778 | + } |
|
| 779 | + } |
|
| 780 | + $cursor->closeCursor(); |
|
| 781 | + } |
|
| 782 | + |
|
| 783 | + /* |
|
| 784 | 784 | * Resolve all group shares to user specific shares |
| 785 | 785 | */ |
| 786 | - $shares = $this->resolveGroupShares($shares2, $userId); |
|
| 787 | - } else { |
|
| 788 | - throw new BackendError('Invalid backend'); |
|
| 789 | - } |
|
| 790 | - |
|
| 791 | - |
|
| 792 | - return $shares; |
|
| 793 | - } |
|
| 794 | - |
|
| 795 | - /** |
|
| 796 | - * Get a share by token |
|
| 797 | - * |
|
| 798 | - * @param string $token |
|
| 799 | - * @return \OCP\Share\IShare |
|
| 800 | - * @throws ShareNotFound |
|
| 801 | - */ |
|
| 802 | - public function getShareByToken($token) { |
|
| 803 | - $qb = $this->dbConn->getQueryBuilder(); |
|
| 804 | - |
|
| 805 | - $cursor = $qb->select('*') |
|
| 806 | - ->from('share') |
|
| 807 | - ->where($qb->expr()->eq('share_type', $qb->createNamedParameter(\OCP\Share::SHARE_TYPE_LINK))) |
|
| 808 | - ->andWhere($qb->expr()->eq('token', $qb->createNamedParameter($token))) |
|
| 809 | - ->andWhere($qb->expr()->orX( |
|
| 810 | - $qb->expr()->eq('item_type', $qb->createNamedParameter('file')), |
|
| 811 | - $qb->expr()->eq('item_type', $qb->createNamedParameter('folder')) |
|
| 812 | - )) |
|
| 813 | - ->execute(); |
|
| 814 | - |
|
| 815 | - $data = $cursor->fetch(); |
|
| 816 | - |
|
| 817 | - if ($data === false) { |
|
| 818 | - throw new ShareNotFound(); |
|
| 819 | - } |
|
| 820 | - |
|
| 821 | - try { |
|
| 822 | - $share = $this->createShare($data); |
|
| 823 | - } catch (InvalidShare $e) { |
|
| 824 | - throw new ShareNotFound(); |
|
| 825 | - } |
|
| 826 | - |
|
| 827 | - return $share; |
|
| 828 | - } |
|
| 829 | - |
|
| 830 | - /** |
|
| 831 | - * Create a share object from an database row |
|
| 832 | - * |
|
| 833 | - * @param mixed[] $data |
|
| 834 | - * @return \OCP\Share\IShare |
|
| 835 | - * @throws InvalidShare |
|
| 836 | - */ |
|
| 837 | - private function createShare($data) { |
|
| 838 | - $share = new Share($this->rootFolder, $this->userManager); |
|
| 839 | - $share->setId((int)$data['id']) |
|
| 840 | - ->setShareType((int)$data['share_type']) |
|
| 841 | - ->setPermissions((int)$data['permissions']) |
|
| 842 | - ->setTarget($data['file_target']) |
|
| 843 | - ->setMailSend((bool)$data['mail_send']); |
|
| 844 | - |
|
| 845 | - $shareTime = new \DateTime(); |
|
| 846 | - $shareTime->setTimestamp((int)$data['stime']); |
|
| 847 | - $share->setShareTime($shareTime); |
|
| 848 | - |
|
| 849 | - if ($share->getShareType() === \OCP\Share::SHARE_TYPE_USER) { |
|
| 850 | - $share->setSharedWith($data['share_with']); |
|
| 851 | - } else if ($share->getShareType() === \OCP\Share::SHARE_TYPE_GROUP) { |
|
| 852 | - $share->setSharedWith($data['share_with']); |
|
| 853 | - } else if ($share->getShareType() === \OCP\Share::SHARE_TYPE_LINK) { |
|
| 854 | - $share->setPassword($data['password']); |
|
| 855 | - $share->setToken($data['token']); |
|
| 856 | - } |
|
| 857 | - |
|
| 858 | - $share->setSharedBy($data['uid_initiator']); |
|
| 859 | - $share->setShareOwner($data['uid_owner']); |
|
| 860 | - |
|
| 861 | - $share->setNodeId((int)$data['file_source']); |
|
| 862 | - $share->setNodeType($data['item_type']); |
|
| 863 | - |
|
| 864 | - if ($data['expiration'] !== null) { |
|
| 865 | - $expiration = \DateTime::createFromFormat('Y-m-d H:i:s', $data['expiration']); |
|
| 866 | - $share->setExpirationDate($expiration); |
|
| 867 | - } |
|
| 868 | - |
|
| 869 | - if (isset($data['f_permissions'])) { |
|
| 870 | - $entryData = $data; |
|
| 871 | - $entryData['permissions'] = $entryData['f_permissions']; |
|
| 872 | - $entryData['parent'] = $entryData['f_parent']; |
|
| 873 | - $share->setNodeCacheEntry(Cache::cacheEntryFromData($entryData, |
|
| 874 | - \OC::$server->getMimeTypeLoader())); |
|
| 875 | - } |
|
| 876 | - |
|
| 877 | - $share->setProviderId($this->identifier()); |
|
| 878 | - |
|
| 879 | - return $share; |
|
| 880 | - } |
|
| 881 | - |
|
| 882 | - /** |
|
| 883 | - * @param Share[] $shares |
|
| 884 | - * @param $userId |
|
| 885 | - * @return Share[] The updates shares if no update is found for a share return the original |
|
| 886 | - */ |
|
| 887 | - private function resolveGroupShares($shares, $userId) { |
|
| 888 | - $result = []; |
|
| 889 | - |
|
| 890 | - $start = 0; |
|
| 891 | - while(true) { |
|
| 892 | - /** @var Share[] $shareSlice */ |
|
| 893 | - $shareSlice = array_slice($shares, $start, 100); |
|
| 894 | - $start += 100; |
|
| 895 | - |
|
| 896 | - if ($shareSlice === []) { |
|
| 897 | - break; |
|
| 898 | - } |
|
| 899 | - |
|
| 900 | - /** @var int[] $ids */ |
|
| 901 | - $ids = []; |
|
| 902 | - /** @var Share[] $shareMap */ |
|
| 903 | - $shareMap = []; |
|
| 904 | - |
|
| 905 | - foreach ($shareSlice as $share) { |
|
| 906 | - $ids[] = (int)$share->getId(); |
|
| 907 | - $shareMap[$share->getId()] = $share; |
|
| 908 | - } |
|
| 909 | - |
|
| 910 | - $qb = $this->dbConn->getQueryBuilder(); |
|
| 911 | - |
|
| 912 | - $query = $qb->select('*') |
|
| 913 | - ->from('share') |
|
| 914 | - ->where($qb->expr()->in('parent', $qb->createNamedParameter($ids, IQueryBuilder::PARAM_INT_ARRAY))) |
|
| 915 | - ->andWhere($qb->expr()->eq('share_with', $qb->createNamedParameter($userId))) |
|
| 916 | - ->andWhere($qb->expr()->orX( |
|
| 917 | - $qb->expr()->eq('item_type', $qb->createNamedParameter('file')), |
|
| 918 | - $qb->expr()->eq('item_type', $qb->createNamedParameter('folder')) |
|
| 919 | - )); |
|
| 920 | - |
|
| 921 | - $stmt = $query->execute(); |
|
| 922 | - |
|
| 923 | - while($data = $stmt->fetch()) { |
|
| 924 | - $shareMap[$data['parent']]->setPermissions((int)$data['permissions']); |
|
| 925 | - $shareMap[$data['parent']]->setTarget($data['file_target']); |
|
| 926 | - } |
|
| 927 | - |
|
| 928 | - $stmt->closeCursor(); |
|
| 929 | - |
|
| 930 | - foreach ($shareMap as $share) { |
|
| 931 | - $result[] = $share; |
|
| 932 | - } |
|
| 933 | - } |
|
| 934 | - |
|
| 935 | - return $result; |
|
| 936 | - } |
|
| 937 | - |
|
| 938 | - /** |
|
| 939 | - * A user is deleted from the system |
|
| 940 | - * So clean up the relevant shares. |
|
| 941 | - * |
|
| 942 | - * @param string $uid |
|
| 943 | - * @param int $shareType |
|
| 944 | - */ |
|
| 945 | - public function userDeleted($uid, $shareType) { |
|
| 946 | - $qb = $this->dbConn->getQueryBuilder(); |
|
| 947 | - |
|
| 948 | - $qb->delete('share'); |
|
| 949 | - |
|
| 950 | - if ($shareType === \OCP\Share::SHARE_TYPE_USER) { |
|
| 951 | - /* |
|
| 786 | + $shares = $this->resolveGroupShares($shares2, $userId); |
|
| 787 | + } else { |
|
| 788 | + throw new BackendError('Invalid backend'); |
|
| 789 | + } |
|
| 790 | + |
|
| 791 | + |
|
| 792 | + return $shares; |
|
| 793 | + } |
|
| 794 | + |
|
| 795 | + /** |
|
| 796 | + * Get a share by token |
|
| 797 | + * |
|
| 798 | + * @param string $token |
|
| 799 | + * @return \OCP\Share\IShare |
|
| 800 | + * @throws ShareNotFound |
|
| 801 | + */ |
|
| 802 | + public function getShareByToken($token) { |
|
| 803 | + $qb = $this->dbConn->getQueryBuilder(); |
|
| 804 | + |
|
| 805 | + $cursor = $qb->select('*') |
|
| 806 | + ->from('share') |
|
| 807 | + ->where($qb->expr()->eq('share_type', $qb->createNamedParameter(\OCP\Share::SHARE_TYPE_LINK))) |
|
| 808 | + ->andWhere($qb->expr()->eq('token', $qb->createNamedParameter($token))) |
|
| 809 | + ->andWhere($qb->expr()->orX( |
|
| 810 | + $qb->expr()->eq('item_type', $qb->createNamedParameter('file')), |
|
| 811 | + $qb->expr()->eq('item_type', $qb->createNamedParameter('folder')) |
|
| 812 | + )) |
|
| 813 | + ->execute(); |
|
| 814 | + |
|
| 815 | + $data = $cursor->fetch(); |
|
| 816 | + |
|
| 817 | + if ($data === false) { |
|
| 818 | + throw new ShareNotFound(); |
|
| 819 | + } |
|
| 820 | + |
|
| 821 | + try { |
|
| 822 | + $share = $this->createShare($data); |
|
| 823 | + } catch (InvalidShare $e) { |
|
| 824 | + throw new ShareNotFound(); |
|
| 825 | + } |
|
| 826 | + |
|
| 827 | + return $share; |
|
| 828 | + } |
|
| 829 | + |
|
| 830 | + /** |
|
| 831 | + * Create a share object from an database row |
|
| 832 | + * |
|
| 833 | + * @param mixed[] $data |
|
| 834 | + * @return \OCP\Share\IShare |
|
| 835 | + * @throws InvalidShare |
|
| 836 | + */ |
|
| 837 | + private function createShare($data) { |
|
| 838 | + $share = new Share($this->rootFolder, $this->userManager); |
|
| 839 | + $share->setId((int)$data['id']) |
|
| 840 | + ->setShareType((int)$data['share_type']) |
|
| 841 | + ->setPermissions((int)$data['permissions']) |
|
| 842 | + ->setTarget($data['file_target']) |
|
| 843 | + ->setMailSend((bool)$data['mail_send']); |
|
| 844 | + |
|
| 845 | + $shareTime = new \DateTime(); |
|
| 846 | + $shareTime->setTimestamp((int)$data['stime']); |
|
| 847 | + $share->setShareTime($shareTime); |
|
| 848 | + |
|
| 849 | + if ($share->getShareType() === \OCP\Share::SHARE_TYPE_USER) { |
|
| 850 | + $share->setSharedWith($data['share_with']); |
|
| 851 | + } else if ($share->getShareType() === \OCP\Share::SHARE_TYPE_GROUP) { |
|
| 852 | + $share->setSharedWith($data['share_with']); |
|
| 853 | + } else if ($share->getShareType() === \OCP\Share::SHARE_TYPE_LINK) { |
|
| 854 | + $share->setPassword($data['password']); |
|
| 855 | + $share->setToken($data['token']); |
|
| 856 | + } |
|
| 857 | + |
|
| 858 | + $share->setSharedBy($data['uid_initiator']); |
|
| 859 | + $share->setShareOwner($data['uid_owner']); |
|
| 860 | + |
|
| 861 | + $share->setNodeId((int)$data['file_source']); |
|
| 862 | + $share->setNodeType($data['item_type']); |
|
| 863 | + |
|
| 864 | + if ($data['expiration'] !== null) { |
|
| 865 | + $expiration = \DateTime::createFromFormat('Y-m-d H:i:s', $data['expiration']); |
|
| 866 | + $share->setExpirationDate($expiration); |
|
| 867 | + } |
|
| 868 | + |
|
| 869 | + if (isset($data['f_permissions'])) { |
|
| 870 | + $entryData = $data; |
|
| 871 | + $entryData['permissions'] = $entryData['f_permissions']; |
|
| 872 | + $entryData['parent'] = $entryData['f_parent']; |
|
| 873 | + $share->setNodeCacheEntry(Cache::cacheEntryFromData($entryData, |
|
| 874 | + \OC::$server->getMimeTypeLoader())); |
|
| 875 | + } |
|
| 876 | + |
|
| 877 | + $share->setProviderId($this->identifier()); |
|
| 878 | + |
|
| 879 | + return $share; |
|
| 880 | + } |
|
| 881 | + |
|
| 882 | + /** |
|
| 883 | + * @param Share[] $shares |
|
| 884 | + * @param $userId |
|
| 885 | + * @return Share[] The updates shares if no update is found for a share return the original |
|
| 886 | + */ |
|
| 887 | + private function resolveGroupShares($shares, $userId) { |
|
| 888 | + $result = []; |
|
| 889 | + |
|
| 890 | + $start = 0; |
|
| 891 | + while(true) { |
|
| 892 | + /** @var Share[] $shareSlice */ |
|
| 893 | + $shareSlice = array_slice($shares, $start, 100); |
|
| 894 | + $start += 100; |
|
| 895 | + |
|
| 896 | + if ($shareSlice === []) { |
|
| 897 | + break; |
|
| 898 | + } |
|
| 899 | + |
|
| 900 | + /** @var int[] $ids */ |
|
| 901 | + $ids = []; |
|
| 902 | + /** @var Share[] $shareMap */ |
|
| 903 | + $shareMap = []; |
|
| 904 | + |
|
| 905 | + foreach ($shareSlice as $share) { |
|
| 906 | + $ids[] = (int)$share->getId(); |
|
| 907 | + $shareMap[$share->getId()] = $share; |
|
| 908 | + } |
|
| 909 | + |
|
| 910 | + $qb = $this->dbConn->getQueryBuilder(); |
|
| 911 | + |
|
| 912 | + $query = $qb->select('*') |
|
| 913 | + ->from('share') |
|
| 914 | + ->where($qb->expr()->in('parent', $qb->createNamedParameter($ids, IQueryBuilder::PARAM_INT_ARRAY))) |
|
| 915 | + ->andWhere($qb->expr()->eq('share_with', $qb->createNamedParameter($userId))) |
|
| 916 | + ->andWhere($qb->expr()->orX( |
|
| 917 | + $qb->expr()->eq('item_type', $qb->createNamedParameter('file')), |
|
| 918 | + $qb->expr()->eq('item_type', $qb->createNamedParameter('folder')) |
|
| 919 | + )); |
|
| 920 | + |
|
| 921 | + $stmt = $query->execute(); |
|
| 922 | + |
|
| 923 | + while($data = $stmt->fetch()) { |
|
| 924 | + $shareMap[$data['parent']]->setPermissions((int)$data['permissions']); |
|
| 925 | + $shareMap[$data['parent']]->setTarget($data['file_target']); |
|
| 926 | + } |
|
| 927 | + |
|
| 928 | + $stmt->closeCursor(); |
|
| 929 | + |
|
| 930 | + foreach ($shareMap as $share) { |
|
| 931 | + $result[] = $share; |
|
| 932 | + } |
|
| 933 | + } |
|
| 934 | + |
|
| 935 | + return $result; |
|
| 936 | + } |
|
| 937 | + |
|
| 938 | + /** |
|
| 939 | + * A user is deleted from the system |
|
| 940 | + * So clean up the relevant shares. |
|
| 941 | + * |
|
| 942 | + * @param string $uid |
|
| 943 | + * @param int $shareType |
|
| 944 | + */ |
|
| 945 | + public function userDeleted($uid, $shareType) { |
|
| 946 | + $qb = $this->dbConn->getQueryBuilder(); |
|
| 947 | + |
|
| 948 | + $qb->delete('share'); |
|
| 949 | + |
|
| 950 | + if ($shareType === \OCP\Share::SHARE_TYPE_USER) { |
|
| 951 | + /* |
|
| 952 | 952 | * Delete all user shares that are owned by this user |
| 953 | 953 | * or that are received by this user |
| 954 | 954 | */ |
| 955 | 955 | |
| 956 | - $qb->where($qb->expr()->eq('share_type', $qb->createNamedParameter(\OCP\Share::SHARE_TYPE_USER))); |
|
| 956 | + $qb->where($qb->expr()->eq('share_type', $qb->createNamedParameter(\OCP\Share::SHARE_TYPE_USER))); |
|
| 957 | 957 | |
| 958 | - $qb->andWhere( |
|
| 959 | - $qb->expr()->orX( |
|
| 960 | - $qb->expr()->eq('uid_owner', $qb->createNamedParameter($uid)), |
|
| 961 | - $qb->expr()->eq('share_with', $qb->createNamedParameter($uid)) |
|
| 962 | - ) |
|
| 963 | - ); |
|
| 964 | - } else if ($shareType === \OCP\Share::SHARE_TYPE_GROUP) { |
|
| 965 | - /* |
|
| 958 | + $qb->andWhere( |
|
| 959 | + $qb->expr()->orX( |
|
| 960 | + $qb->expr()->eq('uid_owner', $qb->createNamedParameter($uid)), |
|
| 961 | + $qb->expr()->eq('share_with', $qb->createNamedParameter($uid)) |
|
| 962 | + ) |
|
| 963 | + ); |
|
| 964 | + } else if ($shareType === \OCP\Share::SHARE_TYPE_GROUP) { |
|
| 965 | + /* |
|
| 966 | 966 | * Delete all group shares that are owned by this user |
| 967 | 967 | * Or special user group shares that are received by this user |
| 968 | 968 | */ |
| 969 | - $qb->where( |
|
| 970 | - $qb->expr()->andX( |
|
| 971 | - $qb->expr()->orX( |
|
| 972 | - $qb->expr()->eq('share_type', $qb->createNamedParameter(\OCP\Share::SHARE_TYPE_GROUP)), |
|
| 973 | - $qb->expr()->eq('share_type', $qb->createNamedParameter(self::SHARE_TYPE_USERGROUP)) |
|
| 974 | - ), |
|
| 975 | - $qb->expr()->eq('uid_owner', $qb->createNamedParameter($uid)) |
|
| 976 | - ) |
|
| 977 | - ); |
|
| 978 | - |
|
| 979 | - $qb->orWhere( |
|
| 980 | - $qb->expr()->andX( |
|
| 981 | - $qb->expr()->eq('share_type', $qb->createNamedParameter(self::SHARE_TYPE_USERGROUP)), |
|
| 982 | - $qb->expr()->eq('share_with', $qb->createNamedParameter($uid)) |
|
| 983 | - ) |
|
| 984 | - ); |
|
| 985 | - } else if ($shareType === \OCP\Share::SHARE_TYPE_LINK) { |
|
| 986 | - /* |
|
| 969 | + $qb->where( |
|
| 970 | + $qb->expr()->andX( |
|
| 971 | + $qb->expr()->orX( |
|
| 972 | + $qb->expr()->eq('share_type', $qb->createNamedParameter(\OCP\Share::SHARE_TYPE_GROUP)), |
|
| 973 | + $qb->expr()->eq('share_type', $qb->createNamedParameter(self::SHARE_TYPE_USERGROUP)) |
|
| 974 | + ), |
|
| 975 | + $qb->expr()->eq('uid_owner', $qb->createNamedParameter($uid)) |
|
| 976 | + ) |
|
| 977 | + ); |
|
| 978 | + |
|
| 979 | + $qb->orWhere( |
|
| 980 | + $qb->expr()->andX( |
|
| 981 | + $qb->expr()->eq('share_type', $qb->createNamedParameter(self::SHARE_TYPE_USERGROUP)), |
|
| 982 | + $qb->expr()->eq('share_with', $qb->createNamedParameter($uid)) |
|
| 983 | + ) |
|
| 984 | + ); |
|
| 985 | + } else if ($shareType === \OCP\Share::SHARE_TYPE_LINK) { |
|
| 986 | + /* |
|
| 987 | 987 | * Delete all link shares owned by this user. |
| 988 | 988 | * And all link shares initiated by this user (until #22327 is in) |
| 989 | 989 | */ |
| 990 | - $qb->where($qb->expr()->eq('share_type', $qb->createNamedParameter(\OCP\Share::SHARE_TYPE_LINK))); |
|
| 991 | - |
|
| 992 | - $qb->andWhere( |
|
| 993 | - $qb->expr()->orX( |
|
| 994 | - $qb->expr()->eq('uid_owner', $qb->createNamedParameter($uid)), |
|
| 995 | - $qb->expr()->eq('uid_initiator', $qb->createNamedParameter($uid)) |
|
| 996 | - ) |
|
| 997 | - ); |
|
| 998 | - } |
|
| 999 | - |
|
| 1000 | - $qb->execute(); |
|
| 1001 | - } |
|
| 1002 | - |
|
| 1003 | - /** |
|
| 1004 | - * Delete all shares received by this group. As well as any custom group |
|
| 1005 | - * shares for group members. |
|
| 1006 | - * |
|
| 1007 | - * @param string $gid |
|
| 1008 | - */ |
|
| 1009 | - public function groupDeleted($gid) { |
|
| 1010 | - /* |
|
| 990 | + $qb->where($qb->expr()->eq('share_type', $qb->createNamedParameter(\OCP\Share::SHARE_TYPE_LINK))); |
|
| 991 | + |
|
| 992 | + $qb->andWhere( |
|
| 993 | + $qb->expr()->orX( |
|
| 994 | + $qb->expr()->eq('uid_owner', $qb->createNamedParameter($uid)), |
|
| 995 | + $qb->expr()->eq('uid_initiator', $qb->createNamedParameter($uid)) |
|
| 996 | + ) |
|
| 997 | + ); |
|
| 998 | + } |
|
| 999 | + |
|
| 1000 | + $qb->execute(); |
|
| 1001 | + } |
|
| 1002 | + |
|
| 1003 | + /** |
|
| 1004 | + * Delete all shares received by this group. As well as any custom group |
|
| 1005 | + * shares for group members. |
|
| 1006 | + * |
|
| 1007 | + * @param string $gid |
|
| 1008 | + */ |
|
| 1009 | + public function groupDeleted($gid) { |
|
| 1010 | + /* |
|
| 1011 | 1011 | * First delete all custom group shares for group members |
| 1012 | 1012 | */ |
| 1013 | - $qb = $this->dbConn->getQueryBuilder(); |
|
| 1014 | - $qb->select('id') |
|
| 1015 | - ->from('share') |
|
| 1016 | - ->where($qb->expr()->eq('share_type', $qb->createNamedParameter(\OCP\Share::SHARE_TYPE_GROUP))) |
|
| 1017 | - ->andWhere($qb->expr()->eq('share_with', $qb->createNamedParameter($gid))); |
|
| 1018 | - |
|
| 1019 | - $cursor = $qb->execute(); |
|
| 1020 | - $ids = []; |
|
| 1021 | - while($row = $cursor->fetch()) { |
|
| 1022 | - $ids[] = (int)$row['id']; |
|
| 1023 | - } |
|
| 1024 | - $cursor->closeCursor(); |
|
| 1025 | - |
|
| 1026 | - if (!empty($ids)) { |
|
| 1027 | - $chunks = array_chunk($ids, 100); |
|
| 1028 | - foreach ($chunks as $chunk) { |
|
| 1029 | - $qb->delete('share') |
|
| 1030 | - ->where($qb->expr()->eq('share_type', $qb->createNamedParameter(self::SHARE_TYPE_USERGROUP))) |
|
| 1031 | - ->andWhere($qb->expr()->in('parent', $qb->createNamedParameter($chunk, IQueryBuilder::PARAM_INT_ARRAY))); |
|
| 1032 | - $qb->execute(); |
|
| 1033 | - } |
|
| 1034 | - } |
|
| 1035 | - |
|
| 1036 | - /* |
|
| 1013 | + $qb = $this->dbConn->getQueryBuilder(); |
|
| 1014 | + $qb->select('id') |
|
| 1015 | + ->from('share') |
|
| 1016 | + ->where($qb->expr()->eq('share_type', $qb->createNamedParameter(\OCP\Share::SHARE_TYPE_GROUP))) |
|
| 1017 | + ->andWhere($qb->expr()->eq('share_with', $qb->createNamedParameter($gid))); |
|
| 1018 | + |
|
| 1019 | + $cursor = $qb->execute(); |
|
| 1020 | + $ids = []; |
|
| 1021 | + while($row = $cursor->fetch()) { |
|
| 1022 | + $ids[] = (int)$row['id']; |
|
| 1023 | + } |
|
| 1024 | + $cursor->closeCursor(); |
|
| 1025 | + |
|
| 1026 | + if (!empty($ids)) { |
|
| 1027 | + $chunks = array_chunk($ids, 100); |
|
| 1028 | + foreach ($chunks as $chunk) { |
|
| 1029 | + $qb->delete('share') |
|
| 1030 | + ->where($qb->expr()->eq('share_type', $qb->createNamedParameter(self::SHARE_TYPE_USERGROUP))) |
|
| 1031 | + ->andWhere($qb->expr()->in('parent', $qb->createNamedParameter($chunk, IQueryBuilder::PARAM_INT_ARRAY))); |
|
| 1032 | + $qb->execute(); |
|
| 1033 | + } |
|
| 1034 | + } |
|
| 1035 | + |
|
| 1036 | + /* |
|
| 1037 | 1037 | * Now delete all the group shares |
| 1038 | 1038 | */ |
| 1039 | - $qb = $this->dbConn->getQueryBuilder(); |
|
| 1040 | - $qb->delete('share') |
|
| 1041 | - ->where($qb->expr()->eq('share_type', $qb->createNamedParameter(\OCP\Share::SHARE_TYPE_GROUP))) |
|
| 1042 | - ->andWhere($qb->expr()->eq('share_with', $qb->createNamedParameter($gid))); |
|
| 1043 | - $qb->execute(); |
|
| 1044 | - } |
|
| 1045 | - |
|
| 1046 | - /** |
|
| 1047 | - * Delete custom group shares to this group for this user |
|
| 1048 | - * |
|
| 1049 | - * @param string $uid |
|
| 1050 | - * @param string $gid |
|
| 1051 | - */ |
|
| 1052 | - public function userDeletedFromGroup($uid, $gid) { |
|
| 1053 | - /* |
|
| 1039 | + $qb = $this->dbConn->getQueryBuilder(); |
|
| 1040 | + $qb->delete('share') |
|
| 1041 | + ->where($qb->expr()->eq('share_type', $qb->createNamedParameter(\OCP\Share::SHARE_TYPE_GROUP))) |
|
| 1042 | + ->andWhere($qb->expr()->eq('share_with', $qb->createNamedParameter($gid))); |
|
| 1043 | + $qb->execute(); |
|
| 1044 | + } |
|
| 1045 | + |
|
| 1046 | + /** |
|
| 1047 | + * Delete custom group shares to this group for this user |
|
| 1048 | + * |
|
| 1049 | + * @param string $uid |
|
| 1050 | + * @param string $gid |
|
| 1051 | + */ |
|
| 1052 | + public function userDeletedFromGroup($uid, $gid) { |
|
| 1053 | + /* |
|
| 1054 | 1054 | * Get all group shares |
| 1055 | 1055 | */ |
| 1056 | - $qb = $this->dbConn->getQueryBuilder(); |
|
| 1057 | - $qb->select('id') |
|
| 1058 | - ->from('share') |
|
| 1059 | - ->where($qb->expr()->eq('share_type', $qb->createNamedParameter(\OCP\Share::SHARE_TYPE_GROUP))) |
|
| 1060 | - ->andWhere($qb->expr()->eq('share_with', $qb->createNamedParameter($gid))); |
|
| 1061 | - |
|
| 1062 | - $cursor = $qb->execute(); |
|
| 1063 | - $ids = []; |
|
| 1064 | - while($row = $cursor->fetch()) { |
|
| 1065 | - $ids[] = (int)$row['id']; |
|
| 1066 | - } |
|
| 1067 | - $cursor->closeCursor(); |
|
| 1068 | - |
|
| 1069 | - if (!empty($ids)) { |
|
| 1070 | - $chunks = array_chunk($ids, 100); |
|
| 1071 | - foreach ($chunks as $chunk) { |
|
| 1072 | - /* |
|
| 1056 | + $qb = $this->dbConn->getQueryBuilder(); |
|
| 1057 | + $qb->select('id') |
|
| 1058 | + ->from('share') |
|
| 1059 | + ->where($qb->expr()->eq('share_type', $qb->createNamedParameter(\OCP\Share::SHARE_TYPE_GROUP))) |
|
| 1060 | + ->andWhere($qb->expr()->eq('share_with', $qb->createNamedParameter($gid))); |
|
| 1061 | + |
|
| 1062 | + $cursor = $qb->execute(); |
|
| 1063 | + $ids = []; |
|
| 1064 | + while($row = $cursor->fetch()) { |
|
| 1065 | + $ids[] = (int)$row['id']; |
|
| 1066 | + } |
|
| 1067 | + $cursor->closeCursor(); |
|
| 1068 | + |
|
| 1069 | + if (!empty($ids)) { |
|
| 1070 | + $chunks = array_chunk($ids, 100); |
|
| 1071 | + foreach ($chunks as $chunk) { |
|
| 1072 | + /* |
|
| 1073 | 1073 | * Delete all special shares wit this users for the found group shares |
| 1074 | 1074 | */ |
| 1075 | - $qb->delete('share') |
|
| 1076 | - ->where($qb->expr()->eq('share_type', $qb->createNamedParameter(self::SHARE_TYPE_USERGROUP))) |
|
| 1077 | - ->andWhere($qb->expr()->eq('share_with', $qb->createNamedParameter($uid))) |
|
| 1078 | - ->andWhere($qb->expr()->in('parent', $qb->createNamedParameter($chunk, IQueryBuilder::PARAM_INT_ARRAY))); |
|
| 1079 | - $qb->execute(); |
|
| 1080 | - } |
|
| 1081 | - } |
|
| 1082 | - } |
|
| 1083 | - |
|
| 1084 | - /** |
|
| 1085 | - * @inheritdoc |
|
| 1086 | - */ |
|
| 1087 | - public function getAccessList($nodes, $currentAccess) { |
|
| 1088 | - $ids = []; |
|
| 1089 | - foreach ($nodes as $node) { |
|
| 1090 | - $ids[] = $node->getId(); |
|
| 1091 | - } |
|
| 1092 | - |
|
| 1093 | - $qb = $this->dbConn->getQueryBuilder(); |
|
| 1094 | - |
|
| 1095 | - $or = $qb->expr()->orX( |
|
| 1096 | - $qb->expr()->eq('share_type', $qb->createNamedParameter(\OCP\Share::SHARE_TYPE_USER)), |
|
| 1097 | - $qb->expr()->eq('share_type', $qb->createNamedParameter(\OCP\Share::SHARE_TYPE_GROUP)), |
|
| 1098 | - $qb->expr()->eq('share_type', $qb->createNamedParameter(\OCP\Share::SHARE_TYPE_LINK)) |
|
| 1099 | - ); |
|
| 1100 | - |
|
| 1101 | - if ($currentAccess) { |
|
| 1102 | - $or->add($qb->expr()->eq('share_type', $qb->createNamedParameter(self::SHARE_TYPE_USERGROUP))); |
|
| 1103 | - } |
|
| 1104 | - |
|
| 1105 | - $qb->select('id', 'parent', 'share_type', 'share_with', 'file_source', 'file_target', 'permissions') |
|
| 1106 | - ->from('share') |
|
| 1107 | - ->where( |
|
| 1108 | - $or |
|
| 1109 | - ) |
|
| 1110 | - ->andWhere($qb->expr()->in('file_source', $qb->createNamedParameter($ids, IQueryBuilder::PARAM_INT_ARRAY))) |
|
| 1111 | - ->andWhere($qb->expr()->orX( |
|
| 1112 | - $qb->expr()->eq('item_type', $qb->createNamedParameter('file')), |
|
| 1113 | - $qb->expr()->eq('item_type', $qb->createNamedParameter('folder')) |
|
| 1114 | - )); |
|
| 1115 | - $cursor = $qb->execute(); |
|
| 1116 | - |
|
| 1117 | - $users = []; |
|
| 1118 | - $link = false; |
|
| 1119 | - while($row = $cursor->fetch()) { |
|
| 1120 | - $type = (int)$row['share_type']; |
|
| 1121 | - if ($type === \OCP\Share::SHARE_TYPE_USER) { |
|
| 1122 | - $uid = $row['share_with']; |
|
| 1123 | - $users[$uid] = isset($users[$uid]) ? $users[$uid] : []; |
|
| 1124 | - $users[$uid][$row['id']] = $row; |
|
| 1125 | - } else if ($type === \OCP\Share::SHARE_TYPE_GROUP) { |
|
| 1126 | - $gid = $row['share_with']; |
|
| 1127 | - $group = $this->groupManager->get($gid); |
|
| 1128 | - |
|
| 1129 | - if ($group === null) { |
|
| 1130 | - continue; |
|
| 1131 | - } |
|
| 1132 | - |
|
| 1133 | - $userList = $group->getUsers(); |
|
| 1134 | - foreach ($userList as $user) { |
|
| 1135 | - $uid = $user->getUID(); |
|
| 1136 | - $users[$uid] = isset($users[$uid]) ? $users[$uid] : []; |
|
| 1137 | - $users[$uid][$row['id']] = $row; |
|
| 1138 | - } |
|
| 1139 | - } else if ($type === \OCP\Share::SHARE_TYPE_LINK) { |
|
| 1140 | - $link = true; |
|
| 1141 | - } else if ($type === self::SHARE_TYPE_USERGROUP && $currentAccess === true) { |
|
| 1142 | - $uid = $row['share_with']; |
|
| 1143 | - $users[$uid] = isset($users[$uid]) ? $users[$uid] : []; |
|
| 1144 | - $users[$uid][$row['id']] = $row; |
|
| 1145 | - } |
|
| 1146 | - } |
|
| 1147 | - $cursor->closeCursor(); |
|
| 1148 | - |
|
| 1149 | - if ($currentAccess === true) { |
|
| 1150 | - $users = array_map([$this, 'filterSharesOfUser'], $users); |
|
| 1151 | - $users = array_filter($users); |
|
| 1152 | - } else { |
|
| 1153 | - $users = array_keys($users); |
|
| 1154 | - } |
|
| 1155 | - |
|
| 1156 | - return ['users' => $users, 'public' => $link]; |
|
| 1157 | - } |
|
| 1158 | - |
|
| 1159 | - /** |
|
| 1160 | - * For each user the path with the fewest slashes is returned |
|
| 1161 | - * @param array $shares |
|
| 1162 | - * @return array |
|
| 1163 | - */ |
|
| 1164 | - protected function filterSharesOfUser(array $shares) { |
|
| 1165 | - // Group shares when the user has a share exception |
|
| 1166 | - foreach ($shares as $id => $share) { |
|
| 1167 | - $type = (int) $share['share_type']; |
|
| 1168 | - $permissions = (int) $share['permissions']; |
|
| 1169 | - |
|
| 1170 | - if ($type === self::SHARE_TYPE_USERGROUP) { |
|
| 1171 | - unset($shares[$share['parent']]); |
|
| 1172 | - |
|
| 1173 | - if ($permissions === 0) { |
|
| 1174 | - unset($shares[$id]); |
|
| 1175 | - } |
|
| 1176 | - } |
|
| 1177 | - } |
|
| 1178 | - |
|
| 1179 | - $best = []; |
|
| 1180 | - $bestDepth = 0; |
|
| 1181 | - foreach ($shares as $id => $share) { |
|
| 1182 | - $depth = substr_count($share['file_target'], '/'); |
|
| 1183 | - if (empty($best) || $depth < $bestDepth) { |
|
| 1184 | - $bestDepth = $depth; |
|
| 1185 | - $best = [ |
|
| 1186 | - 'node_id' => $share['file_source'], |
|
| 1187 | - 'node_path' => $share['file_target'], |
|
| 1188 | - ]; |
|
| 1189 | - } |
|
| 1190 | - } |
|
| 1191 | - |
|
| 1192 | - return $best; |
|
| 1193 | - } |
|
| 1075 | + $qb->delete('share') |
|
| 1076 | + ->where($qb->expr()->eq('share_type', $qb->createNamedParameter(self::SHARE_TYPE_USERGROUP))) |
|
| 1077 | + ->andWhere($qb->expr()->eq('share_with', $qb->createNamedParameter($uid))) |
|
| 1078 | + ->andWhere($qb->expr()->in('parent', $qb->createNamedParameter($chunk, IQueryBuilder::PARAM_INT_ARRAY))); |
|
| 1079 | + $qb->execute(); |
|
| 1080 | + } |
|
| 1081 | + } |
|
| 1082 | + } |
|
| 1083 | + |
|
| 1084 | + /** |
|
| 1085 | + * @inheritdoc |
|
| 1086 | + */ |
|
| 1087 | + public function getAccessList($nodes, $currentAccess) { |
|
| 1088 | + $ids = []; |
|
| 1089 | + foreach ($nodes as $node) { |
|
| 1090 | + $ids[] = $node->getId(); |
|
| 1091 | + } |
|
| 1092 | + |
|
| 1093 | + $qb = $this->dbConn->getQueryBuilder(); |
|
| 1094 | + |
|
| 1095 | + $or = $qb->expr()->orX( |
|
| 1096 | + $qb->expr()->eq('share_type', $qb->createNamedParameter(\OCP\Share::SHARE_TYPE_USER)), |
|
| 1097 | + $qb->expr()->eq('share_type', $qb->createNamedParameter(\OCP\Share::SHARE_TYPE_GROUP)), |
|
| 1098 | + $qb->expr()->eq('share_type', $qb->createNamedParameter(\OCP\Share::SHARE_TYPE_LINK)) |
|
| 1099 | + ); |
|
| 1100 | + |
|
| 1101 | + if ($currentAccess) { |
|
| 1102 | + $or->add($qb->expr()->eq('share_type', $qb->createNamedParameter(self::SHARE_TYPE_USERGROUP))); |
|
| 1103 | + } |
|
| 1104 | + |
|
| 1105 | + $qb->select('id', 'parent', 'share_type', 'share_with', 'file_source', 'file_target', 'permissions') |
|
| 1106 | + ->from('share') |
|
| 1107 | + ->where( |
|
| 1108 | + $or |
|
| 1109 | + ) |
|
| 1110 | + ->andWhere($qb->expr()->in('file_source', $qb->createNamedParameter($ids, IQueryBuilder::PARAM_INT_ARRAY))) |
|
| 1111 | + ->andWhere($qb->expr()->orX( |
|
| 1112 | + $qb->expr()->eq('item_type', $qb->createNamedParameter('file')), |
|
| 1113 | + $qb->expr()->eq('item_type', $qb->createNamedParameter('folder')) |
|
| 1114 | + )); |
|
| 1115 | + $cursor = $qb->execute(); |
|
| 1116 | + |
|
| 1117 | + $users = []; |
|
| 1118 | + $link = false; |
|
| 1119 | + while($row = $cursor->fetch()) { |
|
| 1120 | + $type = (int)$row['share_type']; |
|
| 1121 | + if ($type === \OCP\Share::SHARE_TYPE_USER) { |
|
| 1122 | + $uid = $row['share_with']; |
|
| 1123 | + $users[$uid] = isset($users[$uid]) ? $users[$uid] : []; |
|
| 1124 | + $users[$uid][$row['id']] = $row; |
|
| 1125 | + } else if ($type === \OCP\Share::SHARE_TYPE_GROUP) { |
|
| 1126 | + $gid = $row['share_with']; |
|
| 1127 | + $group = $this->groupManager->get($gid); |
|
| 1128 | + |
|
| 1129 | + if ($group === null) { |
|
| 1130 | + continue; |
|
| 1131 | + } |
|
| 1132 | + |
|
| 1133 | + $userList = $group->getUsers(); |
|
| 1134 | + foreach ($userList as $user) { |
|
| 1135 | + $uid = $user->getUID(); |
|
| 1136 | + $users[$uid] = isset($users[$uid]) ? $users[$uid] : []; |
|
| 1137 | + $users[$uid][$row['id']] = $row; |
|
| 1138 | + } |
|
| 1139 | + } else if ($type === \OCP\Share::SHARE_TYPE_LINK) { |
|
| 1140 | + $link = true; |
|
| 1141 | + } else if ($type === self::SHARE_TYPE_USERGROUP && $currentAccess === true) { |
|
| 1142 | + $uid = $row['share_with']; |
|
| 1143 | + $users[$uid] = isset($users[$uid]) ? $users[$uid] : []; |
|
| 1144 | + $users[$uid][$row['id']] = $row; |
|
| 1145 | + } |
|
| 1146 | + } |
|
| 1147 | + $cursor->closeCursor(); |
|
| 1148 | + |
|
| 1149 | + if ($currentAccess === true) { |
|
| 1150 | + $users = array_map([$this, 'filterSharesOfUser'], $users); |
|
| 1151 | + $users = array_filter($users); |
|
| 1152 | + } else { |
|
| 1153 | + $users = array_keys($users); |
|
| 1154 | + } |
|
| 1155 | + |
|
| 1156 | + return ['users' => $users, 'public' => $link]; |
|
| 1157 | + } |
|
| 1158 | + |
|
| 1159 | + /** |
|
| 1160 | + * For each user the path with the fewest slashes is returned |
|
| 1161 | + * @param array $shares |
|
| 1162 | + * @return array |
|
| 1163 | + */ |
|
| 1164 | + protected function filterSharesOfUser(array $shares) { |
|
| 1165 | + // Group shares when the user has a share exception |
|
| 1166 | + foreach ($shares as $id => $share) { |
|
| 1167 | + $type = (int) $share['share_type']; |
|
| 1168 | + $permissions = (int) $share['permissions']; |
|
| 1169 | + |
|
| 1170 | + if ($type === self::SHARE_TYPE_USERGROUP) { |
|
| 1171 | + unset($shares[$share['parent']]); |
|
| 1172 | + |
|
| 1173 | + if ($permissions === 0) { |
|
| 1174 | + unset($shares[$id]); |
|
| 1175 | + } |
|
| 1176 | + } |
|
| 1177 | + } |
|
| 1178 | + |
|
| 1179 | + $best = []; |
|
| 1180 | + $bestDepth = 0; |
|
| 1181 | + foreach ($shares as $id => $share) { |
|
| 1182 | + $depth = substr_count($share['file_target'], '/'); |
|
| 1183 | + if (empty($best) || $depth < $bestDepth) { |
|
| 1184 | + $bestDepth = $depth; |
|
| 1185 | + $best = [ |
|
| 1186 | + 'node_id' => $share['file_source'], |
|
| 1187 | + 'node_path' => $share['file_target'], |
|
| 1188 | + ]; |
|
| 1189 | + } |
|
| 1190 | + } |
|
| 1191 | + |
|
| 1192 | + return $best; |
|
| 1193 | + } |
|
| 1194 | 1194 | } |
@@ -61,7 +61,7 @@ |
||
| 61 | 61 | * The deserialize method is called during xml parsing. |
| 62 | 62 | * |
| 63 | 63 | * @param Reader $reader |
| 64 | - * @return mixed |
|
| 64 | + * @return null|ShareTypeList |
|
| 65 | 65 | */ |
| 66 | 66 | static function xmlDeserialize(Reader $reader) { |
| 67 | 67 | $shareTypes = []; |
@@ -32,61 +32,61 @@ |
||
| 32 | 32 | * This property contains multiple "share-type" elements, each containing a share type. |
| 33 | 33 | */ |
| 34 | 34 | class ShareTypeList implements Element { |
| 35 | - const NS_OWNCLOUD = 'http://owncloud.org/ns'; |
|
| 35 | + const NS_OWNCLOUD = 'http://owncloud.org/ns'; |
|
| 36 | 36 | |
| 37 | - /** |
|
| 38 | - * Share types |
|
| 39 | - * |
|
| 40 | - * @var int[] |
|
| 41 | - */ |
|
| 42 | - private $shareTypes; |
|
| 37 | + /** |
|
| 38 | + * Share types |
|
| 39 | + * |
|
| 40 | + * @var int[] |
|
| 41 | + */ |
|
| 42 | + private $shareTypes; |
|
| 43 | 43 | |
| 44 | - /** |
|
| 45 | - * @param int[] $shareTypes |
|
| 46 | - */ |
|
| 47 | - public function __construct($shareTypes) { |
|
| 48 | - $this->shareTypes = $shareTypes; |
|
| 49 | - } |
|
| 44 | + /** |
|
| 45 | + * @param int[] $shareTypes |
|
| 46 | + */ |
|
| 47 | + public function __construct($shareTypes) { |
|
| 48 | + $this->shareTypes = $shareTypes; |
|
| 49 | + } |
|
| 50 | 50 | |
| 51 | - /** |
|
| 52 | - * Returns the share types |
|
| 53 | - * |
|
| 54 | - * @return int[] |
|
| 55 | - */ |
|
| 56 | - public function getShareTypes() { |
|
| 57 | - return $this->shareTypes; |
|
| 58 | - } |
|
| 51 | + /** |
|
| 52 | + * Returns the share types |
|
| 53 | + * |
|
| 54 | + * @return int[] |
|
| 55 | + */ |
|
| 56 | + public function getShareTypes() { |
|
| 57 | + return $this->shareTypes; |
|
| 58 | + } |
|
| 59 | 59 | |
| 60 | - /** |
|
| 61 | - * The deserialize method is called during xml parsing. |
|
| 62 | - * |
|
| 63 | - * @param Reader $reader |
|
| 64 | - * @return mixed |
|
| 65 | - */ |
|
| 66 | - static function xmlDeserialize(Reader $reader) { |
|
| 67 | - $shareTypes = []; |
|
| 60 | + /** |
|
| 61 | + * The deserialize method is called during xml parsing. |
|
| 62 | + * |
|
| 63 | + * @param Reader $reader |
|
| 64 | + * @return mixed |
|
| 65 | + */ |
|
| 66 | + static function xmlDeserialize(Reader $reader) { |
|
| 67 | + $shareTypes = []; |
|
| 68 | 68 | |
| 69 | - $tree = $reader->parseInnerTree(); |
|
| 70 | - if ($tree === null) { |
|
| 71 | - return null; |
|
| 72 | - } |
|
| 73 | - foreach ($tree as $elem) { |
|
| 74 | - if ($elem['name'] === '{' . self::NS_OWNCLOUD . '}share-type') { |
|
| 75 | - $shareTypes[] = (int)$elem['value']; |
|
| 76 | - } |
|
| 77 | - } |
|
| 78 | - return new self($shareTypes); |
|
| 79 | - } |
|
| 69 | + $tree = $reader->parseInnerTree(); |
|
| 70 | + if ($tree === null) { |
|
| 71 | + return null; |
|
| 72 | + } |
|
| 73 | + foreach ($tree as $elem) { |
|
| 74 | + if ($elem['name'] === '{' . self::NS_OWNCLOUD . '}share-type') { |
|
| 75 | + $shareTypes[] = (int)$elem['value']; |
|
| 76 | + } |
|
| 77 | + } |
|
| 78 | + return new self($shareTypes); |
|
| 79 | + } |
|
| 80 | 80 | |
| 81 | - /** |
|
| 82 | - * The xmlSerialize metod is called during xml writing. |
|
| 83 | - * |
|
| 84 | - * @param Writer $writer |
|
| 85 | - * @return void |
|
| 86 | - */ |
|
| 87 | - function xmlSerialize(Writer $writer) { |
|
| 88 | - foreach ($this->shareTypes as $shareType) { |
|
| 89 | - $writer->writeElement('{' . self::NS_OWNCLOUD . '}share-type', $shareType); |
|
| 90 | - } |
|
| 91 | - } |
|
| 81 | + /** |
|
| 82 | + * The xmlSerialize metod is called during xml writing. |
|
| 83 | + * |
|
| 84 | + * @param Writer $writer |
|
| 85 | + * @return void |
|
| 86 | + */ |
|
| 87 | + function xmlSerialize(Writer $writer) { |
|
| 88 | + foreach ($this->shareTypes as $shareType) { |
|
| 89 | + $writer->writeElement('{' . self::NS_OWNCLOUD . '}share-type', $shareType); |
|
| 90 | + } |
|
| 91 | + } |
|
| 92 | 92 | } |
@@ -71,8 +71,8 @@ discard block |
||
| 71 | 71 | return null; |
| 72 | 72 | } |
| 73 | 73 | foreach ($tree as $elem) { |
| 74 | - if ($elem['name'] === '{' . self::NS_OWNCLOUD . '}share-type') { |
|
| 75 | - $shareTypes[] = (int)$elem['value']; |
|
| 74 | + if ($elem['name'] === '{'.self::NS_OWNCLOUD.'}share-type') { |
|
| 75 | + $shareTypes[] = (int) $elem['value']; |
|
| 76 | 76 | } |
| 77 | 77 | } |
| 78 | 78 | return new self($shareTypes); |
@@ -86,7 +86,7 @@ discard block |
||
| 86 | 86 | */ |
| 87 | 87 | function xmlSerialize(Writer $writer) { |
| 88 | 88 | foreach ($this->shareTypes as $shareType) { |
| 89 | - $writer->writeElement('{' . self::NS_OWNCLOUD . '}share-type', $shareType); |
|
| 89 | + $writer->writeElement('{'.self::NS_OWNCLOUD.'}share-type', $shareType); |
|
| 90 | 90 | } |
| 91 | 91 | } |
| 92 | 92 | } |
@@ -80,7 +80,7 @@ |
||
| 80 | 80 | * the next element. |
| 81 | 81 | * |
| 82 | 82 | * @param Reader $reader |
| 83 | - * @return mixed |
|
| 83 | + * @return null|TagList |
|
| 84 | 84 | */ |
| 85 | 85 | static function xmlDeserialize(Reader $reader) { |
| 86 | 86 | $tags = []; |
@@ -34,92 +34,92 @@ |
||
| 34 | 34 | * This property contains multiple "tag" elements, each containing a tag name. |
| 35 | 35 | */ |
| 36 | 36 | class TagList implements Element { |
| 37 | - const NS_OWNCLOUD = 'http://owncloud.org/ns'; |
|
| 37 | + const NS_OWNCLOUD = 'http://owncloud.org/ns'; |
|
| 38 | 38 | |
| 39 | - /** |
|
| 40 | - * tags |
|
| 41 | - * |
|
| 42 | - * @var array |
|
| 43 | - */ |
|
| 44 | - private $tags; |
|
| 39 | + /** |
|
| 40 | + * tags |
|
| 41 | + * |
|
| 42 | + * @var array |
|
| 43 | + */ |
|
| 44 | + private $tags; |
|
| 45 | 45 | |
| 46 | - /** |
|
| 47 | - * @param array $tags |
|
| 48 | - */ |
|
| 49 | - public function __construct(array $tags) { |
|
| 50 | - $this->tags = $tags; |
|
| 51 | - } |
|
| 46 | + /** |
|
| 47 | + * @param array $tags |
|
| 48 | + */ |
|
| 49 | + public function __construct(array $tags) { |
|
| 50 | + $this->tags = $tags; |
|
| 51 | + } |
|
| 52 | 52 | |
| 53 | - /** |
|
| 54 | - * Returns the tags |
|
| 55 | - * |
|
| 56 | - * @return array |
|
| 57 | - */ |
|
| 58 | - public function getTags() { |
|
| 53 | + /** |
|
| 54 | + * Returns the tags |
|
| 55 | + * |
|
| 56 | + * @return array |
|
| 57 | + */ |
|
| 58 | + public function getTags() { |
|
| 59 | 59 | |
| 60 | - return $this->tags; |
|
| 60 | + return $this->tags; |
|
| 61 | 61 | |
| 62 | - } |
|
| 62 | + } |
|
| 63 | 63 | |
| 64 | - /** |
|
| 65 | - * The deserialize method is called during xml parsing. |
|
| 66 | - * |
|
| 67 | - * This method is called statictly, this is because in theory this method |
|
| 68 | - * may be used as a type of constructor, or factory method. |
|
| 69 | - * |
|
| 70 | - * Often you want to return an instance of the current class, but you are |
|
| 71 | - * free to return other data as well. |
|
| 72 | - * |
|
| 73 | - * You are responsible for advancing the reader to the next element. Not |
|
| 74 | - * doing anything will result in a never-ending loop. |
|
| 75 | - * |
|
| 76 | - * If you just want to skip parsing for this element altogether, you can |
|
| 77 | - * just call $reader->next(); |
|
| 78 | - * |
|
| 79 | - * $reader->parseInnerTree() will parse the entire sub-tree, and advance to |
|
| 80 | - * the next element. |
|
| 81 | - * |
|
| 82 | - * @param Reader $reader |
|
| 83 | - * @return mixed |
|
| 84 | - */ |
|
| 85 | - static function xmlDeserialize(Reader $reader) { |
|
| 86 | - $tags = []; |
|
| 64 | + /** |
|
| 65 | + * The deserialize method is called during xml parsing. |
|
| 66 | + * |
|
| 67 | + * This method is called statictly, this is because in theory this method |
|
| 68 | + * may be used as a type of constructor, or factory method. |
|
| 69 | + * |
|
| 70 | + * Often you want to return an instance of the current class, but you are |
|
| 71 | + * free to return other data as well. |
|
| 72 | + * |
|
| 73 | + * You are responsible for advancing the reader to the next element. Not |
|
| 74 | + * doing anything will result in a never-ending loop. |
|
| 75 | + * |
|
| 76 | + * If you just want to skip parsing for this element altogether, you can |
|
| 77 | + * just call $reader->next(); |
|
| 78 | + * |
|
| 79 | + * $reader->parseInnerTree() will parse the entire sub-tree, and advance to |
|
| 80 | + * the next element. |
|
| 81 | + * |
|
| 82 | + * @param Reader $reader |
|
| 83 | + * @return mixed |
|
| 84 | + */ |
|
| 85 | + static function xmlDeserialize(Reader $reader) { |
|
| 86 | + $tags = []; |
|
| 87 | 87 | |
| 88 | - $tree = $reader->parseInnerTree(); |
|
| 89 | - if ($tree === null) { |
|
| 90 | - return null; |
|
| 91 | - } |
|
| 92 | - foreach ($tree as $elem) { |
|
| 93 | - if ($elem['name'] === '{' . self::NS_OWNCLOUD . '}tag') { |
|
| 94 | - $tags[] = $elem['value']; |
|
| 95 | - } |
|
| 96 | - } |
|
| 97 | - return new self($tags); |
|
| 98 | - } |
|
| 88 | + $tree = $reader->parseInnerTree(); |
|
| 89 | + if ($tree === null) { |
|
| 90 | + return null; |
|
| 91 | + } |
|
| 92 | + foreach ($tree as $elem) { |
|
| 93 | + if ($elem['name'] === '{' . self::NS_OWNCLOUD . '}tag') { |
|
| 94 | + $tags[] = $elem['value']; |
|
| 95 | + } |
|
| 96 | + } |
|
| 97 | + return new self($tags); |
|
| 98 | + } |
|
| 99 | 99 | |
| 100 | - /** |
|
| 101 | - * The xmlSerialize metod is called during xml writing. |
|
| 102 | - * |
|
| 103 | - * Use the $writer argument to write its own xml serialization. |
|
| 104 | - * |
|
| 105 | - * An important note: do _not_ create a parent element. Any element |
|
| 106 | - * implementing XmlSerializble should only ever write what's considered |
|
| 107 | - * its 'inner xml'. |
|
| 108 | - * |
|
| 109 | - * The parent of the current element is responsible for writing a |
|
| 110 | - * containing element. |
|
| 111 | - * |
|
| 112 | - * This allows serializers to be re-used for different element names. |
|
| 113 | - * |
|
| 114 | - * If you are opening new elements, you must also close them again. |
|
| 115 | - * |
|
| 116 | - * @param Writer $writer |
|
| 117 | - * @return void |
|
| 118 | - */ |
|
| 119 | - function xmlSerialize(Writer $writer) { |
|
| 100 | + /** |
|
| 101 | + * The xmlSerialize metod is called during xml writing. |
|
| 102 | + * |
|
| 103 | + * Use the $writer argument to write its own xml serialization. |
|
| 104 | + * |
|
| 105 | + * An important note: do _not_ create a parent element. Any element |
|
| 106 | + * implementing XmlSerializble should only ever write what's considered |
|
| 107 | + * its 'inner xml'. |
|
| 108 | + * |
|
| 109 | + * The parent of the current element is responsible for writing a |
|
| 110 | + * containing element. |
|
| 111 | + * |
|
| 112 | + * This allows serializers to be re-used for different element names. |
|
| 113 | + * |
|
| 114 | + * If you are opening new elements, you must also close them again. |
|
| 115 | + * |
|
| 116 | + * @param Writer $writer |
|
| 117 | + * @return void |
|
| 118 | + */ |
|
| 119 | + function xmlSerialize(Writer $writer) { |
|
| 120 | 120 | |
| 121 | - foreach ($this->tags as $tag) { |
|
| 122 | - $writer->writeElement('{' . self::NS_OWNCLOUD . '}tag', $tag); |
|
| 123 | - } |
|
| 124 | - } |
|
| 121 | + foreach ($this->tags as $tag) { |
|
| 122 | + $writer->writeElement('{' . self::NS_OWNCLOUD . '}tag', $tag); |
|
| 123 | + } |
|
| 124 | + } |
|
| 125 | 125 | } |
@@ -90,7 +90,7 @@ discard block |
||
| 90 | 90 | return null; |
| 91 | 91 | } |
| 92 | 92 | foreach ($tree as $elem) { |
| 93 | - if ($elem['name'] === '{' . self::NS_OWNCLOUD . '}tag') { |
|
| 93 | + if ($elem['name'] === '{'.self::NS_OWNCLOUD.'}tag') { |
|
| 94 | 94 | $tags[] = $elem['value']; |
| 95 | 95 | } |
| 96 | 96 | } |
@@ -119,7 +119,7 @@ discard block |
||
| 119 | 119 | function xmlSerialize(Writer $writer) { |
| 120 | 120 | |
| 121 | 121 | foreach ($this->tags as $tag) { |
| 122 | - $writer->writeElement('{' . self::NS_OWNCLOUD . '}tag', $tag); |
|
| 122 | + $writer->writeElement('{'.self::NS_OWNCLOUD.'}tag', $tag); |
|
| 123 | 123 | } |
| 124 | 124 | } |
| 125 | 125 | } |
@@ -318,6 +318,11 @@ |
||
| 318 | 318 | */ |
| 319 | 319 | // FIXME This method is only public, until OC_L10N does not need it anymore, |
| 320 | 320 | // FIXME This is also the reason, why it is not in the public interface |
| 321 | + |
|
| 322 | + /** |
|
| 323 | + * @param string $app |
|
| 324 | + * @param string $lang |
|
| 325 | + */ |
|
| 321 | 326 | public function getL10nFilesForApp($app, $lang) { |
| 322 | 327 | $languageFiles = []; |
| 323 | 328 | |
@@ -143,7 +143,7 @@ discard block |
||
| 143 | 143 | * @link https://github.com/owncloud/core/issues/21955 |
| 144 | 144 | */ |
| 145 | 145 | if ($this->config->getSystemValue('installed', false)) { |
| 146 | - $userId = !is_null($this->userSession->getUser()) ? $this->userSession->getUser()->getUID() : null; |
|
| 146 | + $userId = !is_null($this->userSession->getUser()) ? $this->userSession->getUser()->getUID() : null; |
|
| 147 | 147 | if (!is_null($userId)) { |
| 148 | 148 | $userLang = $this->config->getUserValue($userId, 'core', 'lang', null); |
| 149 | 149 | } else { |
@@ -213,7 +213,7 @@ discard block |
||
| 213 | 213 | // merge with translations from theme |
| 214 | 214 | $theme = $this->config->getSystemValue('theme'); |
| 215 | 215 | if (!empty($theme)) { |
| 216 | - $themeDir = $this->serverRoot . '/themes/' . $theme . substr($dir, strlen($this->serverRoot)); |
|
| 216 | + $themeDir = $this->serverRoot.'/themes/'.$theme.substr($dir, strlen($this->serverRoot)); |
|
| 217 | 217 | |
| 218 | 218 | if (is_dir($themeDir)) { |
| 219 | 219 | $files = scandir($themeDir); |
@@ -340,12 +340,12 @@ discard block |
||
| 340 | 340 | $languageFiles = []; |
| 341 | 341 | |
| 342 | 342 | $i18nDir = $this->findL10nDir($app); |
| 343 | - $transFile = strip_tags($i18nDir) . strip_tags($lang) . '.json'; |
|
| 343 | + $transFile = strip_tags($i18nDir).strip_tags($lang).'.json'; |
|
| 344 | 344 | |
| 345 | - if (($this->isSubDirectory($transFile, $this->serverRoot . '/core/l10n/') |
|
| 346 | - || $this->isSubDirectory($transFile, $this->serverRoot . '/lib/l10n/') |
|
| 347 | - || $this->isSubDirectory($transFile, $this->serverRoot . '/settings/l10n/') |
|
| 348 | - || $this->isSubDirectory($transFile, \OC_App::getAppPath($app) . '/l10n/') |
|
| 345 | + if (($this->isSubDirectory($transFile, $this->serverRoot.'/core/l10n/') |
|
| 346 | + || $this->isSubDirectory($transFile, $this->serverRoot.'/lib/l10n/') |
|
| 347 | + || $this->isSubDirectory($transFile, $this->serverRoot.'/settings/l10n/') |
|
| 348 | + || $this->isSubDirectory($transFile, \OC_App::getAppPath($app).'/l10n/') |
|
| 349 | 349 | ) |
| 350 | 350 | && file_exists($transFile)) { |
| 351 | 351 | // load the translations file |
@@ -355,7 +355,7 @@ discard block |
||
| 355 | 355 | // merge with translations from theme |
| 356 | 356 | $theme = $this->config->getSystemValue('theme'); |
| 357 | 357 | if (!empty($theme)) { |
| 358 | - $transFile = $this->serverRoot . '/themes/' . $theme . substr($transFile, strlen($this->serverRoot)); |
|
| 358 | + $transFile = $this->serverRoot.'/themes/'.$theme.substr($transFile, strlen($this->serverRoot)); |
|
| 359 | 359 | if (file_exists($transFile)) { |
| 360 | 360 | $languageFiles[] = $transFile; |
| 361 | 361 | } |
@@ -372,14 +372,14 @@ discard block |
||
| 372 | 372 | */ |
| 373 | 373 | protected function findL10nDir($app = null) { |
| 374 | 374 | if (in_array($app, ['core', 'lib', 'settings'])) { |
| 375 | - if (file_exists($this->serverRoot . '/' . $app . '/l10n/')) { |
|
| 376 | - return $this->serverRoot . '/' . $app . '/l10n/'; |
|
| 375 | + if (file_exists($this->serverRoot.'/'.$app.'/l10n/')) { |
|
| 376 | + return $this->serverRoot.'/'.$app.'/l10n/'; |
|
| 377 | 377 | } |
| 378 | 378 | } else if ($app && \OC_App::getAppPath($app) !== false) { |
| 379 | 379 | // Check if the app is in the app folder |
| 380 | - return \OC_App::getAppPath($app) . '/l10n/'; |
|
| 380 | + return \OC_App::getAppPath($app).'/l10n/'; |
|
| 381 | 381 | } |
| 382 | - return $this->serverRoot . '/core/l10n/'; |
|
| 382 | + return $this->serverRoot.'/core/l10n/'; |
|
| 383 | 383 | } |
| 384 | 384 | |
| 385 | 385 | |
@@ -396,15 +396,15 @@ discard block |
||
| 396 | 396 | return $this->pluralFunctions[$string]; |
| 397 | 397 | } |
| 398 | 398 | |
| 399 | - if (preg_match( '/^\s*nplurals\s*=\s*(\d+)\s*;\s*plural=(.*)$/u', $string, $matches)) { |
|
| 399 | + if (preg_match('/^\s*nplurals\s*=\s*(\d+)\s*;\s*plural=(.*)$/u', $string, $matches)) { |
|
| 400 | 400 | // sanitize |
| 401 | - $nplurals = preg_replace( '/[^0-9]/', '', $matches[1] ); |
|
| 402 | - $plural = preg_replace( '#[^n0-9:\(\)\?\|\&=!<>+*/\%-]#', '', $matches[2] ); |
|
| 401 | + $nplurals = preg_replace('/[^0-9]/', '', $matches[1]); |
|
| 402 | + $plural = preg_replace('#[^n0-9:\(\)\?\|\&=!<>+*/\%-]#', '', $matches[2]); |
|
| 403 | 403 | |
| 404 | 404 | $body = str_replace( |
| 405 | - array( 'plural', 'n', '$n$plurals', ), |
|
| 406 | - array( '$plural', '$n', '$nplurals', ), |
|
| 407 | - 'nplurals='. $nplurals . '; plural=' . $plural |
|
| 405 | + array('plural', 'n', '$n$plurals',), |
|
| 406 | + array('$plural', '$n', '$nplurals',), |
|
| 407 | + 'nplurals='.$nplurals.'; plural='.$plural |
|
| 408 | 408 | ); |
| 409 | 409 | |
| 410 | 410 | // add parents |
@@ -413,9 +413,9 @@ discard block |
||
| 413 | 413 | $res = ''; |
| 414 | 414 | $p = 0; |
| 415 | 415 | $length = strlen($body); |
| 416 | - for($i = 0; $i < $length; $i++) { |
|
| 416 | + for ($i = 0; $i < $length; $i++) { |
|
| 417 | 417 | $ch = $body[$i]; |
| 418 | - switch ( $ch ) { |
|
| 418 | + switch ($ch) { |
|
| 419 | 419 | case '?': |
| 420 | 420 | $res .= ' ? ('; |
| 421 | 421 | $p++; |
@@ -424,7 +424,7 @@ discard block |
||
| 424 | 424 | $res .= ') : ('; |
| 425 | 425 | break; |
| 426 | 426 | case ';': |
| 427 | - $res .= str_repeat( ')', $p ) . ';'; |
|
| 427 | + $res .= str_repeat(')', $p).';'; |
|
| 428 | 428 | $p = 0; |
| 429 | 429 | break; |
| 430 | 430 | default: |
@@ -432,7 +432,7 @@ discard block |
||
| 432 | 432 | } |
| 433 | 433 | } |
| 434 | 434 | |
| 435 | - $body = $res . 'return ($plural>=$nplurals?$nplurals-1:$plural);'; |
|
| 435 | + $body = $res.'return ($plural>=$nplurals?$nplurals-1:$plural);'; |
|
| 436 | 436 | $function = create_function('$n', $body); |
| 437 | 437 | $this->pluralFunctions[$string] = $function; |
| 438 | 438 | return $function; |
@@ -464,7 +464,7 @@ discard block |
||
| 464 | 464 | $commonLanguages = []; |
| 465 | 465 | $languages = []; |
| 466 | 466 | |
| 467 | - foreach($languageCodes as $lang) { |
|
| 467 | + foreach ($languageCodes as $lang) { |
|
| 468 | 468 | $l = $this->get('lib', $lang); |
| 469 | 469 | // TRANSLATORS this is the language name for the language switcher in the personal settings and should be the localized version |
| 470 | 470 | $potentialName = (string) $l->t('__language_name__'); |
@@ -497,7 +497,7 @@ discard block |
||
| 497 | 497 | ksort($commonLanguages); |
| 498 | 498 | |
| 499 | 499 | // sort now by displayed language not the iso-code |
| 500 | - usort( $languages, function ($a, $b) { |
|
| 500 | + usort($languages, function($a, $b) { |
|
| 501 | 501 | if ($a['code'] === $a['name'] && $b['code'] !== $b['name']) { |
| 502 | 502 | // If a doesn't have a name, but b does, list b before a |
| 503 | 503 | return 1; |
@@ -40,485 +40,485 @@ |
||
| 40 | 40 | */ |
| 41 | 41 | class Factory implements IFactory { |
| 42 | 42 | |
| 43 | - /** @var string */ |
|
| 44 | - protected $requestLanguage = ''; |
|
| 45 | - |
|
| 46 | - /** |
|
| 47 | - * cached instances |
|
| 48 | - * @var array Structure: Lang => App => \OCP\IL10N |
|
| 49 | - */ |
|
| 50 | - protected $instances = []; |
|
| 51 | - |
|
| 52 | - /** |
|
| 53 | - * @var array Structure: App => string[] |
|
| 54 | - */ |
|
| 55 | - protected $availableLanguages = []; |
|
| 56 | - |
|
| 57 | - /** |
|
| 58 | - * @var array Structure: string => callable |
|
| 59 | - */ |
|
| 60 | - protected $pluralFunctions = []; |
|
| 61 | - |
|
| 62 | - const COMMON_LANGUAGE_CODES = [ |
|
| 63 | - 'en', 'es', 'fr', 'de', 'de_DE', 'ja', 'ar', 'ru', 'nl', 'it', |
|
| 64 | - 'pt_BR', 'pt_PT', 'da', 'fi_FI', 'nb_NO', 'sv', 'tr', 'zh_CN', 'ko' |
|
| 65 | - ]; |
|
| 66 | - |
|
| 67 | - /** @var IConfig */ |
|
| 68 | - protected $config; |
|
| 69 | - |
|
| 70 | - /** @var IRequest */ |
|
| 71 | - protected $request; |
|
| 72 | - |
|
| 73 | - /** @var IUserSession */ |
|
| 74 | - protected $userSession; |
|
| 75 | - |
|
| 76 | - /** @var string */ |
|
| 77 | - protected $serverRoot; |
|
| 78 | - |
|
| 79 | - /** |
|
| 80 | - * @param IConfig $config |
|
| 81 | - * @param IRequest $request |
|
| 82 | - * @param IUserSession $userSession |
|
| 83 | - * @param string $serverRoot |
|
| 84 | - */ |
|
| 85 | - public function __construct(IConfig $config, |
|
| 86 | - IRequest $request, |
|
| 87 | - IUserSession $userSession, |
|
| 88 | - $serverRoot) { |
|
| 89 | - $this->config = $config; |
|
| 90 | - $this->request = $request; |
|
| 91 | - $this->userSession = $userSession; |
|
| 92 | - $this->serverRoot = $serverRoot; |
|
| 93 | - } |
|
| 94 | - |
|
| 95 | - /** |
|
| 96 | - * Get a language instance |
|
| 97 | - * |
|
| 98 | - * @param string $app |
|
| 99 | - * @param string|null $lang |
|
| 100 | - * @return \OCP\IL10N |
|
| 101 | - */ |
|
| 102 | - public function get($app, $lang = null) { |
|
| 103 | - $app = \OC_App::cleanAppId($app); |
|
| 104 | - if ($lang !== null) { |
|
| 105 | - $lang = str_replace(array('\0', '/', '\\', '..'), '', (string) $lang); |
|
| 106 | - } |
|
| 107 | - |
|
| 108 | - $forceLang = $this->config->getSystemValue('force_language', false); |
|
| 109 | - if (is_string($forceLang)) { |
|
| 110 | - $lang = $forceLang; |
|
| 111 | - } |
|
| 112 | - |
|
| 113 | - if ($lang === null || !$this->languageExists($app, $lang)) { |
|
| 114 | - $lang = $this->findLanguage($app); |
|
| 115 | - } |
|
| 116 | - |
|
| 117 | - if (!isset($this->instances[$lang][$app])) { |
|
| 118 | - $this->instances[$lang][$app] = new L10N( |
|
| 119 | - $this, $app, $lang, |
|
| 120 | - $this->getL10nFilesForApp($app, $lang) |
|
| 121 | - ); |
|
| 122 | - } |
|
| 123 | - |
|
| 124 | - return $this->instances[$lang][$app]; |
|
| 125 | - } |
|
| 126 | - |
|
| 127 | - /** |
|
| 128 | - * Find the best language |
|
| 129 | - * |
|
| 130 | - * @param string|null $app App id or null for core |
|
| 131 | - * @return string language If nothing works it returns 'en' |
|
| 132 | - */ |
|
| 133 | - public function findLanguage($app = null) { |
|
| 134 | - $forceLang = $this->config->getSystemValue('force_language', false); |
|
| 135 | - if (is_string($forceLang)) { |
|
| 136 | - $this->requestLanguage = $forceLang; |
|
| 137 | - } |
|
| 138 | - |
|
| 139 | - if ($this->requestLanguage !== '' && $this->languageExists($app, $this->requestLanguage)) { |
|
| 140 | - return $this->requestLanguage; |
|
| 141 | - } |
|
| 142 | - |
|
| 143 | - /** |
|
| 144 | - * At this point Nextcloud might not yet be installed and thus the lookup |
|
| 145 | - * in the preferences table might fail. For this reason we need to check |
|
| 146 | - * whether the instance has already been installed |
|
| 147 | - * |
|
| 148 | - * @link https://github.com/owncloud/core/issues/21955 |
|
| 149 | - */ |
|
| 150 | - if ($this->config->getSystemValue('installed', false)) { |
|
| 151 | - $userId = !is_null($this->userSession->getUser()) ? $this->userSession->getUser()->getUID() : null; |
|
| 152 | - if (!is_null($userId)) { |
|
| 153 | - $userLang = $this->config->getUserValue($userId, 'core', 'lang', null); |
|
| 154 | - } else { |
|
| 155 | - $userLang = null; |
|
| 156 | - } |
|
| 157 | - } else { |
|
| 158 | - $userId = null; |
|
| 159 | - $userLang = null; |
|
| 160 | - } |
|
| 161 | - |
|
| 162 | - if ($userLang) { |
|
| 163 | - $this->requestLanguage = $userLang; |
|
| 164 | - if ($this->languageExists($app, $userLang)) { |
|
| 165 | - return $userLang; |
|
| 166 | - } |
|
| 167 | - } |
|
| 168 | - |
|
| 169 | - try { |
|
| 170 | - // Try to get the language from the Request |
|
| 171 | - $lang = $this->getLanguageFromRequest($app); |
|
| 172 | - if ($userId !== null && $app === null && !$userLang) { |
|
| 173 | - $this->config->setUserValue($userId, 'core', 'lang', $lang); |
|
| 174 | - } |
|
| 175 | - return $lang; |
|
| 176 | - } catch (LanguageNotFoundException $e) { |
|
| 177 | - // Finding language from request failed fall back to default language |
|
| 178 | - $defaultLanguage = $this->config->getSystemValue('default_language', false); |
|
| 179 | - if ($defaultLanguage !== false && $this->languageExists($app, $defaultLanguage)) { |
|
| 180 | - return $defaultLanguage; |
|
| 181 | - } |
|
| 182 | - } |
|
| 183 | - |
|
| 184 | - // We could not find any language so fall back to english |
|
| 185 | - return 'en'; |
|
| 186 | - } |
|
| 187 | - |
|
| 188 | - /** |
|
| 189 | - * Find all available languages for an app |
|
| 190 | - * |
|
| 191 | - * @param string|null $app App id or null for core |
|
| 192 | - * @return array an array of available languages |
|
| 193 | - */ |
|
| 194 | - public function findAvailableLanguages($app = null) { |
|
| 195 | - $key = $app; |
|
| 196 | - if ($key === null) { |
|
| 197 | - $key = 'null'; |
|
| 198 | - } |
|
| 199 | - |
|
| 200 | - // also works with null as key |
|
| 201 | - if (!empty($this->availableLanguages[$key])) { |
|
| 202 | - return $this->availableLanguages[$key]; |
|
| 203 | - } |
|
| 204 | - |
|
| 205 | - $available = ['en']; //english is always available |
|
| 206 | - $dir = $this->findL10nDir($app); |
|
| 207 | - if (is_dir($dir)) { |
|
| 208 | - $files = scandir($dir); |
|
| 209 | - if ($files !== false) { |
|
| 210 | - foreach ($files as $file) { |
|
| 211 | - if (substr($file, -5) === '.json' && substr($file, 0, 4) !== 'l10n') { |
|
| 212 | - $available[] = substr($file, 0, -5); |
|
| 213 | - } |
|
| 214 | - } |
|
| 215 | - } |
|
| 216 | - } |
|
| 217 | - |
|
| 218 | - // merge with translations from theme |
|
| 219 | - $theme = $this->config->getSystemValue('theme'); |
|
| 220 | - if (!empty($theme)) { |
|
| 221 | - $themeDir = $this->serverRoot . '/themes/' . $theme . substr($dir, strlen($this->serverRoot)); |
|
| 222 | - |
|
| 223 | - if (is_dir($themeDir)) { |
|
| 224 | - $files = scandir($themeDir); |
|
| 225 | - if ($files !== false) { |
|
| 226 | - foreach ($files as $file) { |
|
| 227 | - if (substr($file, -5) === '.json' && substr($file, 0, 4) !== 'l10n') { |
|
| 228 | - $available[] = substr($file, 0, -5); |
|
| 229 | - } |
|
| 230 | - } |
|
| 231 | - } |
|
| 232 | - } |
|
| 233 | - } |
|
| 234 | - |
|
| 235 | - $this->availableLanguages[$key] = $available; |
|
| 236 | - return $available; |
|
| 237 | - } |
|
| 238 | - |
|
| 239 | - /** |
|
| 240 | - * @param string|null $app App id or null for core |
|
| 241 | - * @param string $lang |
|
| 242 | - * @return bool |
|
| 243 | - */ |
|
| 244 | - public function languageExists($app, $lang) { |
|
| 245 | - if ($lang === 'en') {//english is always available |
|
| 246 | - return true; |
|
| 247 | - } |
|
| 248 | - |
|
| 249 | - $languages = $this->findAvailableLanguages($app); |
|
| 250 | - return array_search($lang, $languages) !== false; |
|
| 251 | - } |
|
| 252 | - |
|
| 253 | - /** |
|
| 254 | - * @param string|null $app |
|
| 255 | - * @return string |
|
| 256 | - * @throws LanguageNotFoundException |
|
| 257 | - */ |
|
| 258 | - private function getLanguageFromRequest($app) { |
|
| 259 | - $header = $this->request->getHeader('ACCEPT_LANGUAGE'); |
|
| 260 | - if ($header !== '') { |
|
| 261 | - $available = $this->findAvailableLanguages($app); |
|
| 262 | - |
|
| 263 | - // E.g. make sure that 'de' is before 'de_DE'. |
|
| 264 | - sort($available); |
|
| 265 | - |
|
| 266 | - $preferences = preg_split('/,\s*/', strtolower($header)); |
|
| 267 | - foreach ($preferences as $preference) { |
|
| 268 | - list($preferred_language) = explode(';', $preference); |
|
| 269 | - $preferred_language = str_replace('-', '_', $preferred_language); |
|
| 270 | - |
|
| 271 | - foreach ($available as $available_language) { |
|
| 272 | - if ($preferred_language === strtolower($available_language)) { |
|
| 273 | - return $this->respectDefaultLanguage($app, $available_language); |
|
| 274 | - } |
|
| 275 | - } |
|
| 276 | - |
|
| 277 | - // Fallback from de_De to de |
|
| 278 | - foreach ($available as $available_language) { |
|
| 279 | - if (substr($preferred_language, 0, 2) === $available_language) { |
|
| 280 | - return $available_language; |
|
| 281 | - } |
|
| 282 | - } |
|
| 283 | - } |
|
| 284 | - } |
|
| 285 | - |
|
| 286 | - throw new LanguageNotFoundException(); |
|
| 287 | - } |
|
| 288 | - |
|
| 289 | - /** |
|
| 290 | - * if default language is set to de_DE (formal German) this should be |
|
| 291 | - * preferred to 'de' (non-formal German) if possible |
|
| 292 | - * |
|
| 293 | - * @param string|null $app |
|
| 294 | - * @param string $lang |
|
| 295 | - * @return string |
|
| 296 | - */ |
|
| 297 | - protected function respectDefaultLanguage($app, $lang) { |
|
| 298 | - $result = $lang; |
|
| 299 | - $defaultLanguage = $this->config->getSystemValue('default_language', false); |
|
| 300 | - |
|
| 301 | - // use formal version of german ("Sie" instead of "Du") if the default |
|
| 302 | - // language is set to 'de_DE' if possible |
|
| 303 | - if (is_string($defaultLanguage) && |
|
| 304 | - strtolower($lang) === 'de' && |
|
| 305 | - strtolower($defaultLanguage) === 'de_de' && |
|
| 306 | - $this->languageExists($app, 'de_DE') |
|
| 307 | - ) { |
|
| 308 | - $result = 'de_DE'; |
|
| 309 | - } |
|
| 310 | - |
|
| 311 | - return $result; |
|
| 312 | - } |
|
| 313 | - |
|
| 314 | - /** |
|
| 315 | - * Checks if $sub is a subdirectory of $parent |
|
| 316 | - * |
|
| 317 | - * @param string $sub |
|
| 318 | - * @param string $parent |
|
| 319 | - * @return bool |
|
| 320 | - */ |
|
| 321 | - private function isSubDirectory($sub, $parent) { |
|
| 322 | - // Check whether $sub contains no ".." |
|
| 323 | - if (strpos($sub, '..') !== false) { |
|
| 324 | - return false; |
|
| 325 | - } |
|
| 326 | - |
|
| 327 | - // Check whether $sub is a subdirectory of $parent |
|
| 328 | - if (strpos($sub, $parent) === 0) { |
|
| 329 | - return true; |
|
| 330 | - } |
|
| 331 | - |
|
| 332 | - return false; |
|
| 333 | - } |
|
| 334 | - |
|
| 335 | - /** |
|
| 336 | - * Get a list of language files that should be loaded |
|
| 337 | - * |
|
| 338 | - * @param string $app |
|
| 339 | - * @param string $lang |
|
| 340 | - * @return string[] |
|
| 341 | - */ |
|
| 342 | - // FIXME This method is only public, until OC_L10N does not need it anymore, |
|
| 343 | - // FIXME This is also the reason, why it is not in the public interface |
|
| 344 | - public function getL10nFilesForApp($app, $lang) { |
|
| 345 | - $languageFiles = []; |
|
| 346 | - |
|
| 347 | - $i18nDir = $this->findL10nDir($app); |
|
| 348 | - $transFile = strip_tags($i18nDir) . strip_tags($lang) . '.json'; |
|
| 349 | - |
|
| 350 | - if (($this->isSubDirectory($transFile, $this->serverRoot . '/core/l10n/') |
|
| 351 | - || $this->isSubDirectory($transFile, $this->serverRoot . '/lib/l10n/') |
|
| 352 | - || $this->isSubDirectory($transFile, $this->serverRoot . '/settings/l10n/') |
|
| 353 | - || $this->isSubDirectory($transFile, \OC_App::getAppPath($app) . '/l10n/') |
|
| 354 | - ) |
|
| 355 | - && file_exists($transFile)) { |
|
| 356 | - // load the translations file |
|
| 357 | - $languageFiles[] = $transFile; |
|
| 358 | - } |
|
| 359 | - |
|
| 360 | - // merge with translations from theme |
|
| 361 | - $theme = $this->config->getSystemValue('theme'); |
|
| 362 | - if (!empty($theme)) { |
|
| 363 | - $transFile = $this->serverRoot . '/themes/' . $theme . substr($transFile, strlen($this->serverRoot)); |
|
| 364 | - if (file_exists($transFile)) { |
|
| 365 | - $languageFiles[] = $transFile; |
|
| 366 | - } |
|
| 367 | - } |
|
| 368 | - |
|
| 369 | - return $languageFiles; |
|
| 370 | - } |
|
| 371 | - |
|
| 372 | - /** |
|
| 373 | - * find the l10n directory |
|
| 374 | - * |
|
| 375 | - * @param string $app App id or empty string for core |
|
| 376 | - * @return string directory |
|
| 377 | - */ |
|
| 378 | - protected function findL10nDir($app = null) { |
|
| 379 | - if (in_array($app, ['core', 'lib', 'settings'])) { |
|
| 380 | - if (file_exists($this->serverRoot . '/' . $app . '/l10n/')) { |
|
| 381 | - return $this->serverRoot . '/' . $app . '/l10n/'; |
|
| 382 | - } |
|
| 383 | - } else if ($app && \OC_App::getAppPath($app) !== false) { |
|
| 384 | - // Check if the app is in the app folder |
|
| 385 | - return \OC_App::getAppPath($app) . '/l10n/'; |
|
| 386 | - } |
|
| 387 | - return $this->serverRoot . '/core/l10n/'; |
|
| 388 | - } |
|
| 389 | - |
|
| 390 | - |
|
| 391 | - /** |
|
| 392 | - * Creates a function from the plural string |
|
| 393 | - * |
|
| 394 | - * Parts of the code is copied from Habari: |
|
| 395 | - * https://github.com/habari/system/blob/master/classes/locale.php |
|
| 396 | - * @param string $string |
|
| 397 | - * @return string |
|
| 398 | - */ |
|
| 399 | - public function createPluralFunction($string) { |
|
| 400 | - if (isset($this->pluralFunctions[$string])) { |
|
| 401 | - return $this->pluralFunctions[$string]; |
|
| 402 | - } |
|
| 403 | - |
|
| 404 | - if (preg_match( '/^\s*nplurals\s*=\s*(\d+)\s*;\s*plural=(.*)$/u', $string, $matches)) { |
|
| 405 | - // sanitize |
|
| 406 | - $nplurals = preg_replace( '/[^0-9]/', '', $matches[1] ); |
|
| 407 | - $plural = preg_replace( '#[^n0-9:\(\)\?\|\&=!<>+*/\%-]#', '', $matches[2] ); |
|
| 408 | - |
|
| 409 | - $body = str_replace( |
|
| 410 | - array( 'plural', 'n', '$n$plurals', ), |
|
| 411 | - array( '$plural', '$n', '$nplurals', ), |
|
| 412 | - 'nplurals='. $nplurals . '; plural=' . $plural |
|
| 413 | - ); |
|
| 414 | - |
|
| 415 | - // add parents |
|
| 416 | - // important since PHP's ternary evaluates from left to right |
|
| 417 | - $body .= ';'; |
|
| 418 | - $res = ''; |
|
| 419 | - $p = 0; |
|
| 420 | - $length = strlen($body); |
|
| 421 | - for($i = 0; $i < $length; $i++) { |
|
| 422 | - $ch = $body[$i]; |
|
| 423 | - switch ( $ch ) { |
|
| 424 | - case '?': |
|
| 425 | - $res .= ' ? ('; |
|
| 426 | - $p++; |
|
| 427 | - break; |
|
| 428 | - case ':': |
|
| 429 | - $res .= ') : ('; |
|
| 430 | - break; |
|
| 431 | - case ';': |
|
| 432 | - $res .= str_repeat( ')', $p ) . ';'; |
|
| 433 | - $p = 0; |
|
| 434 | - break; |
|
| 435 | - default: |
|
| 436 | - $res .= $ch; |
|
| 437 | - } |
|
| 438 | - } |
|
| 439 | - |
|
| 440 | - $body = $res . 'return ($plural>=$nplurals?$nplurals-1:$plural);'; |
|
| 441 | - $function = create_function('$n', $body); |
|
| 442 | - $this->pluralFunctions[$string] = $function; |
|
| 443 | - return $function; |
|
| 444 | - } else { |
|
| 445 | - // default: one plural form for all cases but n==1 (english) |
|
| 446 | - $function = create_function( |
|
| 447 | - '$n', |
|
| 448 | - '$nplurals=2;$plural=($n==1?0:1);return ($plural>=$nplurals?$nplurals-1:$plural);' |
|
| 449 | - ); |
|
| 450 | - $this->pluralFunctions[$string] = $function; |
|
| 451 | - return $function; |
|
| 452 | - } |
|
| 453 | - } |
|
| 454 | - |
|
| 455 | - /** |
|
| 456 | - * returns the common language and other languages in an |
|
| 457 | - * associative array |
|
| 458 | - * |
|
| 459 | - * @return array |
|
| 460 | - */ |
|
| 461 | - public function getLanguages() { |
|
| 462 | - $forceLanguage = $this->config->getSystemValue('force_language', false); |
|
| 463 | - if ($forceLanguage !== false) { |
|
| 464 | - return []; |
|
| 465 | - } |
|
| 466 | - |
|
| 467 | - $languageCodes = $this->findAvailableLanguages(); |
|
| 468 | - |
|
| 469 | - $commonLanguages = []; |
|
| 470 | - $languages = []; |
|
| 471 | - |
|
| 472 | - foreach($languageCodes as $lang) { |
|
| 473 | - $l = $this->get('lib', $lang); |
|
| 474 | - // TRANSLATORS this is the language name for the language switcher in the personal settings and should be the localized version |
|
| 475 | - $potentialName = (string) $l->t('__language_name__'); |
|
| 476 | - if ($l->getLanguageCode() === $lang && $potentialName[0] !== '_') {//first check if the language name is in the translation file |
|
| 477 | - $ln = array( |
|
| 478 | - 'code' => $lang, |
|
| 479 | - 'name' => $potentialName |
|
| 480 | - ); |
|
| 481 | - } else if ($lang === 'en') { |
|
| 482 | - $ln = array( |
|
| 483 | - 'code' => $lang, |
|
| 484 | - 'name' => 'English (US)' |
|
| 485 | - ); |
|
| 486 | - } else {//fallback to language code |
|
| 487 | - $ln = array( |
|
| 488 | - 'code' => $lang, |
|
| 489 | - 'name' => $lang |
|
| 490 | - ); |
|
| 491 | - } |
|
| 492 | - |
|
| 493 | - // put appropriate languages into appropriate arrays, to print them sorted |
|
| 494 | - // common languages -> divider -> other languages |
|
| 495 | - if (in_array($lang, self::COMMON_LANGUAGE_CODES)) { |
|
| 496 | - $commonLanguages[array_search($lang, self::COMMON_LANGUAGE_CODES)] = $ln; |
|
| 497 | - } else { |
|
| 498 | - $languages[] = $ln; |
|
| 499 | - } |
|
| 500 | - } |
|
| 501 | - |
|
| 502 | - ksort($commonLanguages); |
|
| 503 | - |
|
| 504 | - // sort now by displayed language not the iso-code |
|
| 505 | - usort( $languages, function ($a, $b) { |
|
| 506 | - if ($a['code'] === $a['name'] && $b['code'] !== $b['name']) { |
|
| 507 | - // If a doesn't have a name, but b does, list b before a |
|
| 508 | - return 1; |
|
| 509 | - } |
|
| 510 | - if ($a['code'] !== $a['name'] && $b['code'] === $b['name']) { |
|
| 511 | - // If a does have a name, but b doesn't, list a before b |
|
| 512 | - return -1; |
|
| 513 | - } |
|
| 514 | - // Otherwise compare the names |
|
| 515 | - return strcmp($a['name'], $b['name']); |
|
| 516 | - }); |
|
| 517 | - |
|
| 518 | - return [ |
|
| 519 | - // reset indexes |
|
| 520 | - 'commonlanguages' => array_values($commonLanguages), |
|
| 521 | - 'languages' => $languages |
|
| 522 | - ]; |
|
| 523 | - } |
|
| 43 | + /** @var string */ |
|
| 44 | + protected $requestLanguage = ''; |
|
| 45 | + |
|
| 46 | + /** |
|
| 47 | + * cached instances |
|
| 48 | + * @var array Structure: Lang => App => \OCP\IL10N |
|
| 49 | + */ |
|
| 50 | + protected $instances = []; |
|
| 51 | + |
|
| 52 | + /** |
|
| 53 | + * @var array Structure: App => string[] |
|
| 54 | + */ |
|
| 55 | + protected $availableLanguages = []; |
|
| 56 | + |
|
| 57 | + /** |
|
| 58 | + * @var array Structure: string => callable |
|
| 59 | + */ |
|
| 60 | + protected $pluralFunctions = []; |
|
| 61 | + |
|
| 62 | + const COMMON_LANGUAGE_CODES = [ |
|
| 63 | + 'en', 'es', 'fr', 'de', 'de_DE', 'ja', 'ar', 'ru', 'nl', 'it', |
|
| 64 | + 'pt_BR', 'pt_PT', 'da', 'fi_FI', 'nb_NO', 'sv', 'tr', 'zh_CN', 'ko' |
|
| 65 | + ]; |
|
| 66 | + |
|
| 67 | + /** @var IConfig */ |
|
| 68 | + protected $config; |
|
| 69 | + |
|
| 70 | + /** @var IRequest */ |
|
| 71 | + protected $request; |
|
| 72 | + |
|
| 73 | + /** @var IUserSession */ |
|
| 74 | + protected $userSession; |
|
| 75 | + |
|
| 76 | + /** @var string */ |
|
| 77 | + protected $serverRoot; |
|
| 78 | + |
|
| 79 | + /** |
|
| 80 | + * @param IConfig $config |
|
| 81 | + * @param IRequest $request |
|
| 82 | + * @param IUserSession $userSession |
|
| 83 | + * @param string $serverRoot |
|
| 84 | + */ |
|
| 85 | + public function __construct(IConfig $config, |
|
| 86 | + IRequest $request, |
|
| 87 | + IUserSession $userSession, |
|
| 88 | + $serverRoot) { |
|
| 89 | + $this->config = $config; |
|
| 90 | + $this->request = $request; |
|
| 91 | + $this->userSession = $userSession; |
|
| 92 | + $this->serverRoot = $serverRoot; |
|
| 93 | + } |
|
| 94 | + |
|
| 95 | + /** |
|
| 96 | + * Get a language instance |
|
| 97 | + * |
|
| 98 | + * @param string $app |
|
| 99 | + * @param string|null $lang |
|
| 100 | + * @return \OCP\IL10N |
|
| 101 | + */ |
|
| 102 | + public function get($app, $lang = null) { |
|
| 103 | + $app = \OC_App::cleanAppId($app); |
|
| 104 | + if ($lang !== null) { |
|
| 105 | + $lang = str_replace(array('\0', '/', '\\', '..'), '', (string) $lang); |
|
| 106 | + } |
|
| 107 | + |
|
| 108 | + $forceLang = $this->config->getSystemValue('force_language', false); |
|
| 109 | + if (is_string($forceLang)) { |
|
| 110 | + $lang = $forceLang; |
|
| 111 | + } |
|
| 112 | + |
|
| 113 | + if ($lang === null || !$this->languageExists($app, $lang)) { |
|
| 114 | + $lang = $this->findLanguage($app); |
|
| 115 | + } |
|
| 116 | + |
|
| 117 | + if (!isset($this->instances[$lang][$app])) { |
|
| 118 | + $this->instances[$lang][$app] = new L10N( |
|
| 119 | + $this, $app, $lang, |
|
| 120 | + $this->getL10nFilesForApp($app, $lang) |
|
| 121 | + ); |
|
| 122 | + } |
|
| 123 | + |
|
| 124 | + return $this->instances[$lang][$app]; |
|
| 125 | + } |
|
| 126 | + |
|
| 127 | + /** |
|
| 128 | + * Find the best language |
|
| 129 | + * |
|
| 130 | + * @param string|null $app App id or null for core |
|
| 131 | + * @return string language If nothing works it returns 'en' |
|
| 132 | + */ |
|
| 133 | + public function findLanguage($app = null) { |
|
| 134 | + $forceLang = $this->config->getSystemValue('force_language', false); |
|
| 135 | + if (is_string($forceLang)) { |
|
| 136 | + $this->requestLanguage = $forceLang; |
|
| 137 | + } |
|
| 138 | + |
|
| 139 | + if ($this->requestLanguage !== '' && $this->languageExists($app, $this->requestLanguage)) { |
|
| 140 | + return $this->requestLanguage; |
|
| 141 | + } |
|
| 142 | + |
|
| 143 | + /** |
|
| 144 | + * At this point Nextcloud might not yet be installed and thus the lookup |
|
| 145 | + * in the preferences table might fail. For this reason we need to check |
|
| 146 | + * whether the instance has already been installed |
|
| 147 | + * |
|
| 148 | + * @link https://github.com/owncloud/core/issues/21955 |
|
| 149 | + */ |
|
| 150 | + if ($this->config->getSystemValue('installed', false)) { |
|
| 151 | + $userId = !is_null($this->userSession->getUser()) ? $this->userSession->getUser()->getUID() : null; |
|
| 152 | + if (!is_null($userId)) { |
|
| 153 | + $userLang = $this->config->getUserValue($userId, 'core', 'lang', null); |
|
| 154 | + } else { |
|
| 155 | + $userLang = null; |
|
| 156 | + } |
|
| 157 | + } else { |
|
| 158 | + $userId = null; |
|
| 159 | + $userLang = null; |
|
| 160 | + } |
|
| 161 | + |
|
| 162 | + if ($userLang) { |
|
| 163 | + $this->requestLanguage = $userLang; |
|
| 164 | + if ($this->languageExists($app, $userLang)) { |
|
| 165 | + return $userLang; |
|
| 166 | + } |
|
| 167 | + } |
|
| 168 | + |
|
| 169 | + try { |
|
| 170 | + // Try to get the language from the Request |
|
| 171 | + $lang = $this->getLanguageFromRequest($app); |
|
| 172 | + if ($userId !== null && $app === null && !$userLang) { |
|
| 173 | + $this->config->setUserValue($userId, 'core', 'lang', $lang); |
|
| 174 | + } |
|
| 175 | + return $lang; |
|
| 176 | + } catch (LanguageNotFoundException $e) { |
|
| 177 | + // Finding language from request failed fall back to default language |
|
| 178 | + $defaultLanguage = $this->config->getSystemValue('default_language', false); |
|
| 179 | + if ($defaultLanguage !== false && $this->languageExists($app, $defaultLanguage)) { |
|
| 180 | + return $defaultLanguage; |
|
| 181 | + } |
|
| 182 | + } |
|
| 183 | + |
|
| 184 | + // We could not find any language so fall back to english |
|
| 185 | + return 'en'; |
|
| 186 | + } |
|
| 187 | + |
|
| 188 | + /** |
|
| 189 | + * Find all available languages for an app |
|
| 190 | + * |
|
| 191 | + * @param string|null $app App id or null for core |
|
| 192 | + * @return array an array of available languages |
|
| 193 | + */ |
|
| 194 | + public function findAvailableLanguages($app = null) { |
|
| 195 | + $key = $app; |
|
| 196 | + if ($key === null) { |
|
| 197 | + $key = 'null'; |
|
| 198 | + } |
|
| 199 | + |
|
| 200 | + // also works with null as key |
|
| 201 | + if (!empty($this->availableLanguages[$key])) { |
|
| 202 | + return $this->availableLanguages[$key]; |
|
| 203 | + } |
|
| 204 | + |
|
| 205 | + $available = ['en']; //english is always available |
|
| 206 | + $dir = $this->findL10nDir($app); |
|
| 207 | + if (is_dir($dir)) { |
|
| 208 | + $files = scandir($dir); |
|
| 209 | + if ($files !== false) { |
|
| 210 | + foreach ($files as $file) { |
|
| 211 | + if (substr($file, -5) === '.json' && substr($file, 0, 4) !== 'l10n') { |
|
| 212 | + $available[] = substr($file, 0, -5); |
|
| 213 | + } |
|
| 214 | + } |
|
| 215 | + } |
|
| 216 | + } |
|
| 217 | + |
|
| 218 | + // merge with translations from theme |
|
| 219 | + $theme = $this->config->getSystemValue('theme'); |
|
| 220 | + if (!empty($theme)) { |
|
| 221 | + $themeDir = $this->serverRoot . '/themes/' . $theme . substr($dir, strlen($this->serverRoot)); |
|
| 222 | + |
|
| 223 | + if (is_dir($themeDir)) { |
|
| 224 | + $files = scandir($themeDir); |
|
| 225 | + if ($files !== false) { |
|
| 226 | + foreach ($files as $file) { |
|
| 227 | + if (substr($file, -5) === '.json' && substr($file, 0, 4) !== 'l10n') { |
|
| 228 | + $available[] = substr($file, 0, -5); |
|
| 229 | + } |
|
| 230 | + } |
|
| 231 | + } |
|
| 232 | + } |
|
| 233 | + } |
|
| 234 | + |
|
| 235 | + $this->availableLanguages[$key] = $available; |
|
| 236 | + return $available; |
|
| 237 | + } |
|
| 238 | + |
|
| 239 | + /** |
|
| 240 | + * @param string|null $app App id or null for core |
|
| 241 | + * @param string $lang |
|
| 242 | + * @return bool |
|
| 243 | + */ |
|
| 244 | + public function languageExists($app, $lang) { |
|
| 245 | + if ($lang === 'en') {//english is always available |
|
| 246 | + return true; |
|
| 247 | + } |
|
| 248 | + |
|
| 249 | + $languages = $this->findAvailableLanguages($app); |
|
| 250 | + return array_search($lang, $languages) !== false; |
|
| 251 | + } |
|
| 252 | + |
|
| 253 | + /** |
|
| 254 | + * @param string|null $app |
|
| 255 | + * @return string |
|
| 256 | + * @throws LanguageNotFoundException |
|
| 257 | + */ |
|
| 258 | + private function getLanguageFromRequest($app) { |
|
| 259 | + $header = $this->request->getHeader('ACCEPT_LANGUAGE'); |
|
| 260 | + if ($header !== '') { |
|
| 261 | + $available = $this->findAvailableLanguages($app); |
|
| 262 | + |
|
| 263 | + // E.g. make sure that 'de' is before 'de_DE'. |
|
| 264 | + sort($available); |
|
| 265 | + |
|
| 266 | + $preferences = preg_split('/,\s*/', strtolower($header)); |
|
| 267 | + foreach ($preferences as $preference) { |
|
| 268 | + list($preferred_language) = explode(';', $preference); |
|
| 269 | + $preferred_language = str_replace('-', '_', $preferred_language); |
|
| 270 | + |
|
| 271 | + foreach ($available as $available_language) { |
|
| 272 | + if ($preferred_language === strtolower($available_language)) { |
|
| 273 | + return $this->respectDefaultLanguage($app, $available_language); |
|
| 274 | + } |
|
| 275 | + } |
|
| 276 | + |
|
| 277 | + // Fallback from de_De to de |
|
| 278 | + foreach ($available as $available_language) { |
|
| 279 | + if (substr($preferred_language, 0, 2) === $available_language) { |
|
| 280 | + return $available_language; |
|
| 281 | + } |
|
| 282 | + } |
|
| 283 | + } |
|
| 284 | + } |
|
| 285 | + |
|
| 286 | + throw new LanguageNotFoundException(); |
|
| 287 | + } |
|
| 288 | + |
|
| 289 | + /** |
|
| 290 | + * if default language is set to de_DE (formal German) this should be |
|
| 291 | + * preferred to 'de' (non-formal German) if possible |
|
| 292 | + * |
|
| 293 | + * @param string|null $app |
|
| 294 | + * @param string $lang |
|
| 295 | + * @return string |
|
| 296 | + */ |
|
| 297 | + protected function respectDefaultLanguage($app, $lang) { |
|
| 298 | + $result = $lang; |
|
| 299 | + $defaultLanguage = $this->config->getSystemValue('default_language', false); |
|
| 300 | + |
|
| 301 | + // use formal version of german ("Sie" instead of "Du") if the default |
|
| 302 | + // language is set to 'de_DE' if possible |
|
| 303 | + if (is_string($defaultLanguage) && |
|
| 304 | + strtolower($lang) === 'de' && |
|
| 305 | + strtolower($defaultLanguage) === 'de_de' && |
|
| 306 | + $this->languageExists($app, 'de_DE') |
|
| 307 | + ) { |
|
| 308 | + $result = 'de_DE'; |
|
| 309 | + } |
|
| 310 | + |
|
| 311 | + return $result; |
|
| 312 | + } |
|
| 313 | + |
|
| 314 | + /** |
|
| 315 | + * Checks if $sub is a subdirectory of $parent |
|
| 316 | + * |
|
| 317 | + * @param string $sub |
|
| 318 | + * @param string $parent |
|
| 319 | + * @return bool |
|
| 320 | + */ |
|
| 321 | + private function isSubDirectory($sub, $parent) { |
|
| 322 | + // Check whether $sub contains no ".." |
|
| 323 | + if (strpos($sub, '..') !== false) { |
|
| 324 | + return false; |
|
| 325 | + } |
|
| 326 | + |
|
| 327 | + // Check whether $sub is a subdirectory of $parent |
|
| 328 | + if (strpos($sub, $parent) === 0) { |
|
| 329 | + return true; |
|
| 330 | + } |
|
| 331 | + |
|
| 332 | + return false; |
|
| 333 | + } |
|
| 334 | + |
|
| 335 | + /** |
|
| 336 | + * Get a list of language files that should be loaded |
|
| 337 | + * |
|
| 338 | + * @param string $app |
|
| 339 | + * @param string $lang |
|
| 340 | + * @return string[] |
|
| 341 | + */ |
|
| 342 | + // FIXME This method is only public, until OC_L10N does not need it anymore, |
|
| 343 | + // FIXME This is also the reason, why it is not in the public interface |
|
| 344 | + public function getL10nFilesForApp($app, $lang) { |
|
| 345 | + $languageFiles = []; |
|
| 346 | + |
|
| 347 | + $i18nDir = $this->findL10nDir($app); |
|
| 348 | + $transFile = strip_tags($i18nDir) . strip_tags($lang) . '.json'; |
|
| 349 | + |
|
| 350 | + if (($this->isSubDirectory($transFile, $this->serverRoot . '/core/l10n/') |
|
| 351 | + || $this->isSubDirectory($transFile, $this->serverRoot . '/lib/l10n/') |
|
| 352 | + || $this->isSubDirectory($transFile, $this->serverRoot . '/settings/l10n/') |
|
| 353 | + || $this->isSubDirectory($transFile, \OC_App::getAppPath($app) . '/l10n/') |
|
| 354 | + ) |
|
| 355 | + && file_exists($transFile)) { |
|
| 356 | + // load the translations file |
|
| 357 | + $languageFiles[] = $transFile; |
|
| 358 | + } |
|
| 359 | + |
|
| 360 | + // merge with translations from theme |
|
| 361 | + $theme = $this->config->getSystemValue('theme'); |
|
| 362 | + if (!empty($theme)) { |
|
| 363 | + $transFile = $this->serverRoot . '/themes/' . $theme . substr($transFile, strlen($this->serverRoot)); |
|
| 364 | + if (file_exists($transFile)) { |
|
| 365 | + $languageFiles[] = $transFile; |
|
| 366 | + } |
|
| 367 | + } |
|
| 368 | + |
|
| 369 | + return $languageFiles; |
|
| 370 | + } |
|
| 371 | + |
|
| 372 | + /** |
|
| 373 | + * find the l10n directory |
|
| 374 | + * |
|
| 375 | + * @param string $app App id or empty string for core |
|
| 376 | + * @return string directory |
|
| 377 | + */ |
|
| 378 | + protected function findL10nDir($app = null) { |
|
| 379 | + if (in_array($app, ['core', 'lib', 'settings'])) { |
|
| 380 | + if (file_exists($this->serverRoot . '/' . $app . '/l10n/')) { |
|
| 381 | + return $this->serverRoot . '/' . $app . '/l10n/'; |
|
| 382 | + } |
|
| 383 | + } else if ($app && \OC_App::getAppPath($app) !== false) { |
|
| 384 | + // Check if the app is in the app folder |
|
| 385 | + return \OC_App::getAppPath($app) . '/l10n/'; |
|
| 386 | + } |
|
| 387 | + return $this->serverRoot . '/core/l10n/'; |
|
| 388 | + } |
|
| 389 | + |
|
| 390 | + |
|
| 391 | + /** |
|
| 392 | + * Creates a function from the plural string |
|
| 393 | + * |
|
| 394 | + * Parts of the code is copied from Habari: |
|
| 395 | + * https://github.com/habari/system/blob/master/classes/locale.php |
|
| 396 | + * @param string $string |
|
| 397 | + * @return string |
|
| 398 | + */ |
|
| 399 | + public function createPluralFunction($string) { |
|
| 400 | + if (isset($this->pluralFunctions[$string])) { |
|
| 401 | + return $this->pluralFunctions[$string]; |
|
| 402 | + } |
|
| 403 | + |
|
| 404 | + if (preg_match( '/^\s*nplurals\s*=\s*(\d+)\s*;\s*plural=(.*)$/u', $string, $matches)) { |
|
| 405 | + // sanitize |
|
| 406 | + $nplurals = preg_replace( '/[^0-9]/', '', $matches[1] ); |
|
| 407 | + $plural = preg_replace( '#[^n0-9:\(\)\?\|\&=!<>+*/\%-]#', '', $matches[2] ); |
|
| 408 | + |
|
| 409 | + $body = str_replace( |
|
| 410 | + array( 'plural', 'n', '$n$plurals', ), |
|
| 411 | + array( '$plural', '$n', '$nplurals', ), |
|
| 412 | + 'nplurals='. $nplurals . '; plural=' . $plural |
|
| 413 | + ); |
|
| 414 | + |
|
| 415 | + // add parents |
|
| 416 | + // important since PHP's ternary evaluates from left to right |
|
| 417 | + $body .= ';'; |
|
| 418 | + $res = ''; |
|
| 419 | + $p = 0; |
|
| 420 | + $length = strlen($body); |
|
| 421 | + for($i = 0; $i < $length; $i++) { |
|
| 422 | + $ch = $body[$i]; |
|
| 423 | + switch ( $ch ) { |
|
| 424 | + case '?': |
|
| 425 | + $res .= ' ? ('; |
|
| 426 | + $p++; |
|
| 427 | + break; |
|
| 428 | + case ':': |
|
| 429 | + $res .= ') : ('; |
|
| 430 | + break; |
|
| 431 | + case ';': |
|
| 432 | + $res .= str_repeat( ')', $p ) . ';'; |
|
| 433 | + $p = 0; |
|
| 434 | + break; |
|
| 435 | + default: |
|
| 436 | + $res .= $ch; |
|
| 437 | + } |
|
| 438 | + } |
|
| 439 | + |
|
| 440 | + $body = $res . 'return ($plural>=$nplurals?$nplurals-1:$plural);'; |
|
| 441 | + $function = create_function('$n', $body); |
|
| 442 | + $this->pluralFunctions[$string] = $function; |
|
| 443 | + return $function; |
|
| 444 | + } else { |
|
| 445 | + // default: one plural form for all cases but n==1 (english) |
|
| 446 | + $function = create_function( |
|
| 447 | + '$n', |
|
| 448 | + '$nplurals=2;$plural=($n==1?0:1);return ($plural>=$nplurals?$nplurals-1:$plural);' |
|
| 449 | + ); |
|
| 450 | + $this->pluralFunctions[$string] = $function; |
|
| 451 | + return $function; |
|
| 452 | + } |
|
| 453 | + } |
|
| 454 | + |
|
| 455 | + /** |
|
| 456 | + * returns the common language and other languages in an |
|
| 457 | + * associative array |
|
| 458 | + * |
|
| 459 | + * @return array |
|
| 460 | + */ |
|
| 461 | + public function getLanguages() { |
|
| 462 | + $forceLanguage = $this->config->getSystemValue('force_language', false); |
|
| 463 | + if ($forceLanguage !== false) { |
|
| 464 | + return []; |
|
| 465 | + } |
|
| 466 | + |
|
| 467 | + $languageCodes = $this->findAvailableLanguages(); |
|
| 468 | + |
|
| 469 | + $commonLanguages = []; |
|
| 470 | + $languages = []; |
|
| 471 | + |
|
| 472 | + foreach($languageCodes as $lang) { |
|
| 473 | + $l = $this->get('lib', $lang); |
|
| 474 | + // TRANSLATORS this is the language name for the language switcher in the personal settings and should be the localized version |
|
| 475 | + $potentialName = (string) $l->t('__language_name__'); |
|
| 476 | + if ($l->getLanguageCode() === $lang && $potentialName[0] !== '_') {//first check if the language name is in the translation file |
|
| 477 | + $ln = array( |
|
| 478 | + 'code' => $lang, |
|
| 479 | + 'name' => $potentialName |
|
| 480 | + ); |
|
| 481 | + } else if ($lang === 'en') { |
|
| 482 | + $ln = array( |
|
| 483 | + 'code' => $lang, |
|
| 484 | + 'name' => 'English (US)' |
|
| 485 | + ); |
|
| 486 | + } else {//fallback to language code |
|
| 487 | + $ln = array( |
|
| 488 | + 'code' => $lang, |
|
| 489 | + 'name' => $lang |
|
| 490 | + ); |
|
| 491 | + } |
|
| 492 | + |
|
| 493 | + // put appropriate languages into appropriate arrays, to print them sorted |
|
| 494 | + // common languages -> divider -> other languages |
|
| 495 | + if (in_array($lang, self::COMMON_LANGUAGE_CODES)) { |
|
| 496 | + $commonLanguages[array_search($lang, self::COMMON_LANGUAGE_CODES)] = $ln; |
|
| 497 | + } else { |
|
| 498 | + $languages[] = $ln; |
|
| 499 | + } |
|
| 500 | + } |
|
| 501 | + |
|
| 502 | + ksort($commonLanguages); |
|
| 503 | + |
|
| 504 | + // sort now by displayed language not the iso-code |
|
| 505 | + usort( $languages, function ($a, $b) { |
|
| 506 | + if ($a['code'] === $a['name'] && $b['code'] !== $b['name']) { |
|
| 507 | + // If a doesn't have a name, but b does, list b before a |
|
| 508 | + return 1; |
|
| 509 | + } |
|
| 510 | + if ($a['code'] !== $a['name'] && $b['code'] === $b['name']) { |
|
| 511 | + // If a does have a name, but b doesn't, list a before b |
|
| 512 | + return -1; |
|
| 513 | + } |
|
| 514 | + // Otherwise compare the names |
|
| 515 | + return strcmp($a['name'], $b['name']); |
|
| 516 | + }); |
|
| 517 | + |
|
| 518 | + return [ |
|
| 519 | + // reset indexes |
|
| 520 | + 'commonlanguages' => array_values($commonLanguages), |
|
| 521 | + 'languages' => $languages |
|
| 522 | + ]; |
|
| 523 | + } |
|
| 524 | 524 | } |
@@ -101,7 +101,7 @@ |
||
| 101 | 101 | } |
| 102 | 102 | |
| 103 | 103 | /** |
| 104 | - * @param $username |
|
| 104 | + * @param string $username |
|
| 105 | 105 | * @param IDBConnection $connection |
| 106 | 106 | * @return array |
| 107 | 107 | */ |
@@ -34,143 +34,143 @@ |
||
| 34 | 34 | use OCP\ILogger; |
| 35 | 35 | |
| 36 | 36 | class MySQL extends AbstractDatabase { |
| 37 | - public $dbprettyname = 'MySQL/MariaDB'; |
|
| 38 | - |
|
| 39 | - public function setupDatabase($username) { |
|
| 40 | - //check if the database user has admin right |
|
| 41 | - $connection = $this->connect(['dbname' => null]); |
|
| 42 | - |
|
| 43 | - // detect mb4 |
|
| 44 | - $tools = new MySqlTools(); |
|
| 45 | - if ($tools->supports4ByteCharset($connection)) { |
|
| 46 | - $this->config->setValue('mysql.utf8mb4', true); |
|
| 47 | - $connection = $this->connect(['dbname' => null]); |
|
| 48 | - } |
|
| 49 | - |
|
| 50 | - $this->createSpecificUser($username, $connection); |
|
| 51 | - |
|
| 52 | - //create the database |
|
| 53 | - $this->createDatabase($connection); |
|
| 54 | - |
|
| 55 | - //fill the database if needed |
|
| 56 | - $query='select count(*) from information_schema.tables where table_schema=? AND table_name = ?'; |
|
| 57 | - $connection->executeQuery($query, [$this->dbName, $this->tablePrefix.'users']); |
|
| 58 | - } |
|
| 59 | - |
|
| 60 | - /** |
|
| 61 | - * @param \OC\DB\Connection $connection |
|
| 62 | - */ |
|
| 63 | - private function createDatabase($connection) { |
|
| 64 | - try{ |
|
| 65 | - $name = $this->dbName; |
|
| 66 | - $user = $this->dbUser; |
|
| 67 | - //we can't use OC_DB functions here because we need to connect as the administrative user. |
|
| 68 | - $characterSet = $this->config->getValue('mysql.utf8mb4', false) ? 'utf8mb4' : 'utf8'; |
|
| 69 | - $query = "CREATE DATABASE IF NOT EXISTS `$name` CHARACTER SET $characterSet COLLATE ${characterSet}_bin;"; |
|
| 70 | - $connection->executeUpdate($query); |
|
| 71 | - } catch (\Exception $ex) { |
|
| 72 | - $this->logger->logException($ex, [ |
|
| 73 | - 'message' => 'Database creation failed.', |
|
| 74 | - 'level' => ILogger::ERROR, |
|
| 75 | - 'app' => 'mysql.setup', |
|
| 76 | - ]); |
|
| 77 | - return; |
|
| 78 | - } |
|
| 79 | - |
|
| 80 | - try { |
|
| 81 | - //this query will fail if there aren't the right permissions, ignore the error |
|
| 82 | - $query="GRANT ALL PRIVILEGES ON `$name` . * TO '$user'"; |
|
| 83 | - $connection->executeUpdate($query); |
|
| 84 | - } catch (\Exception $ex) { |
|
| 85 | - $this->logger->logException($ex, [ |
|
| 86 | - 'message' => 'Could not automatically grant privileges, this can be ignored if database user already had privileges.', |
|
| 87 | - 'level' => ILogger::DEBUG, |
|
| 88 | - 'app' => 'mysql.setup', |
|
| 89 | - ]); |
|
| 90 | - } |
|
| 91 | - } |
|
| 92 | - |
|
| 93 | - /** |
|
| 94 | - * @param IDBConnection $connection |
|
| 95 | - * @throws \OC\DatabaseSetupException |
|
| 96 | - */ |
|
| 97 | - private function createDBUser($connection) { |
|
| 98 | - try{ |
|
| 99 | - $name = $this->dbUser; |
|
| 100 | - $password = $this->dbPassword; |
|
| 101 | - // we need to create 2 accounts, one for global use and one for local user. if we don't specify the local one, |
|
| 102 | - // the anonymous user would take precedence when there is one. |
|
| 103 | - $query = "CREATE USER '$name'@'localhost' IDENTIFIED BY '$password'"; |
|
| 104 | - $connection->executeUpdate($query); |
|
| 105 | - $query = "CREATE USER '$name'@'%' IDENTIFIED BY '$password'"; |
|
| 106 | - $connection->executeUpdate($query); |
|
| 107 | - } |
|
| 108 | - catch (\Exception $ex){ |
|
| 109 | - $this->logger->logException($ex, [ |
|
| 110 | - 'message' => 'Database user creation failed.', |
|
| 111 | - 'level' => ILogger::ERROR, |
|
| 112 | - 'app' => 'mysql.setup', |
|
| 113 | - ]); |
|
| 114 | - } |
|
| 115 | - } |
|
| 116 | - |
|
| 117 | - /** |
|
| 118 | - * @param $username |
|
| 119 | - * @param IDBConnection $connection |
|
| 120 | - * @return array |
|
| 121 | - */ |
|
| 122 | - private function createSpecificUser($username, $connection) { |
|
| 123 | - try { |
|
| 124 | - //user already specified in config |
|
| 125 | - $oldUser = $this->config->getValue('dbuser', false); |
|
| 126 | - |
|
| 127 | - //we don't have a dbuser specified in config |
|
| 128 | - if ($this->dbUser !== $oldUser) { |
|
| 129 | - //add prefix to the admin username to prevent collisions |
|
| 130 | - $adminUser = substr('oc_' . $username, 0, 16); |
|
| 131 | - |
|
| 132 | - $i = 1; |
|
| 133 | - while (true) { |
|
| 134 | - //this should be enough to check for admin rights in mysql |
|
| 135 | - $query = 'SELECT user FROM mysql.user WHERE user=?'; |
|
| 136 | - $result = $connection->executeQuery($query, [$adminUser]); |
|
| 137 | - |
|
| 138 | - //current dbuser has admin rights |
|
| 139 | - if ($result) { |
|
| 140 | - $data = $result->fetchAll(); |
|
| 141 | - //new dbuser does not exist |
|
| 142 | - if (count($data) === 0) { |
|
| 143 | - //use the admin login data for the new database user |
|
| 144 | - $this->dbUser = $adminUser; |
|
| 145 | - |
|
| 146 | - //create a random password so we don't need to store the admin password in the config file |
|
| 147 | - $this->dbPassword = $this->random->generate(30); |
|
| 148 | - |
|
| 149 | - $this->createDBUser($connection); |
|
| 150 | - |
|
| 151 | - break; |
|
| 152 | - } else { |
|
| 153 | - //repeat with different username |
|
| 154 | - $length = strlen((string)$i); |
|
| 155 | - $adminUser = substr('oc_' . $username, 0, 16 - $length) . $i; |
|
| 156 | - $i++; |
|
| 157 | - } |
|
| 158 | - } else { |
|
| 159 | - break; |
|
| 160 | - } |
|
| 161 | - } |
|
| 162 | - } |
|
| 163 | - } catch (\Exception $ex) { |
|
| 164 | - $this->logger->logException($ex, [ |
|
| 165 | - 'message' => 'Can not create a new MySQL user, will continue with the provided user.', |
|
| 166 | - 'level' => ILogger::INFO, |
|
| 167 | - 'app' => 'mysql.setup', |
|
| 168 | - ]); |
|
| 169 | - } |
|
| 170 | - |
|
| 171 | - $this->config->setValues([ |
|
| 172 | - 'dbuser' => $this->dbUser, |
|
| 173 | - 'dbpassword' => $this->dbPassword, |
|
| 174 | - ]); |
|
| 175 | - } |
|
| 37 | + public $dbprettyname = 'MySQL/MariaDB'; |
|
| 38 | + |
|
| 39 | + public function setupDatabase($username) { |
|
| 40 | + //check if the database user has admin right |
|
| 41 | + $connection = $this->connect(['dbname' => null]); |
|
| 42 | + |
|
| 43 | + // detect mb4 |
|
| 44 | + $tools = new MySqlTools(); |
|
| 45 | + if ($tools->supports4ByteCharset($connection)) { |
|
| 46 | + $this->config->setValue('mysql.utf8mb4', true); |
|
| 47 | + $connection = $this->connect(['dbname' => null]); |
|
| 48 | + } |
|
| 49 | + |
|
| 50 | + $this->createSpecificUser($username, $connection); |
|
| 51 | + |
|
| 52 | + //create the database |
|
| 53 | + $this->createDatabase($connection); |
|
| 54 | + |
|
| 55 | + //fill the database if needed |
|
| 56 | + $query='select count(*) from information_schema.tables where table_schema=? AND table_name = ?'; |
|
| 57 | + $connection->executeQuery($query, [$this->dbName, $this->tablePrefix.'users']); |
|
| 58 | + } |
|
| 59 | + |
|
| 60 | + /** |
|
| 61 | + * @param \OC\DB\Connection $connection |
|
| 62 | + */ |
|
| 63 | + private function createDatabase($connection) { |
|
| 64 | + try{ |
|
| 65 | + $name = $this->dbName; |
|
| 66 | + $user = $this->dbUser; |
|
| 67 | + //we can't use OC_DB functions here because we need to connect as the administrative user. |
|
| 68 | + $characterSet = $this->config->getValue('mysql.utf8mb4', false) ? 'utf8mb4' : 'utf8'; |
|
| 69 | + $query = "CREATE DATABASE IF NOT EXISTS `$name` CHARACTER SET $characterSet COLLATE ${characterSet}_bin;"; |
|
| 70 | + $connection->executeUpdate($query); |
|
| 71 | + } catch (\Exception $ex) { |
|
| 72 | + $this->logger->logException($ex, [ |
|
| 73 | + 'message' => 'Database creation failed.', |
|
| 74 | + 'level' => ILogger::ERROR, |
|
| 75 | + 'app' => 'mysql.setup', |
|
| 76 | + ]); |
|
| 77 | + return; |
|
| 78 | + } |
|
| 79 | + |
|
| 80 | + try { |
|
| 81 | + //this query will fail if there aren't the right permissions, ignore the error |
|
| 82 | + $query="GRANT ALL PRIVILEGES ON `$name` . * TO '$user'"; |
|
| 83 | + $connection->executeUpdate($query); |
|
| 84 | + } catch (\Exception $ex) { |
|
| 85 | + $this->logger->logException($ex, [ |
|
| 86 | + 'message' => 'Could not automatically grant privileges, this can be ignored if database user already had privileges.', |
|
| 87 | + 'level' => ILogger::DEBUG, |
|
| 88 | + 'app' => 'mysql.setup', |
|
| 89 | + ]); |
|
| 90 | + } |
|
| 91 | + } |
|
| 92 | + |
|
| 93 | + /** |
|
| 94 | + * @param IDBConnection $connection |
|
| 95 | + * @throws \OC\DatabaseSetupException |
|
| 96 | + */ |
|
| 97 | + private function createDBUser($connection) { |
|
| 98 | + try{ |
|
| 99 | + $name = $this->dbUser; |
|
| 100 | + $password = $this->dbPassword; |
|
| 101 | + // we need to create 2 accounts, one for global use and one for local user. if we don't specify the local one, |
|
| 102 | + // the anonymous user would take precedence when there is one. |
|
| 103 | + $query = "CREATE USER '$name'@'localhost' IDENTIFIED BY '$password'"; |
|
| 104 | + $connection->executeUpdate($query); |
|
| 105 | + $query = "CREATE USER '$name'@'%' IDENTIFIED BY '$password'"; |
|
| 106 | + $connection->executeUpdate($query); |
|
| 107 | + } |
|
| 108 | + catch (\Exception $ex){ |
|
| 109 | + $this->logger->logException($ex, [ |
|
| 110 | + 'message' => 'Database user creation failed.', |
|
| 111 | + 'level' => ILogger::ERROR, |
|
| 112 | + 'app' => 'mysql.setup', |
|
| 113 | + ]); |
|
| 114 | + } |
|
| 115 | + } |
|
| 116 | + |
|
| 117 | + /** |
|
| 118 | + * @param $username |
|
| 119 | + * @param IDBConnection $connection |
|
| 120 | + * @return array |
|
| 121 | + */ |
|
| 122 | + private function createSpecificUser($username, $connection) { |
|
| 123 | + try { |
|
| 124 | + //user already specified in config |
|
| 125 | + $oldUser = $this->config->getValue('dbuser', false); |
|
| 126 | + |
|
| 127 | + //we don't have a dbuser specified in config |
|
| 128 | + if ($this->dbUser !== $oldUser) { |
|
| 129 | + //add prefix to the admin username to prevent collisions |
|
| 130 | + $adminUser = substr('oc_' . $username, 0, 16); |
|
| 131 | + |
|
| 132 | + $i = 1; |
|
| 133 | + while (true) { |
|
| 134 | + //this should be enough to check for admin rights in mysql |
|
| 135 | + $query = 'SELECT user FROM mysql.user WHERE user=?'; |
|
| 136 | + $result = $connection->executeQuery($query, [$adminUser]); |
|
| 137 | + |
|
| 138 | + //current dbuser has admin rights |
|
| 139 | + if ($result) { |
|
| 140 | + $data = $result->fetchAll(); |
|
| 141 | + //new dbuser does not exist |
|
| 142 | + if (count($data) === 0) { |
|
| 143 | + //use the admin login data for the new database user |
|
| 144 | + $this->dbUser = $adminUser; |
|
| 145 | + |
|
| 146 | + //create a random password so we don't need to store the admin password in the config file |
|
| 147 | + $this->dbPassword = $this->random->generate(30); |
|
| 148 | + |
|
| 149 | + $this->createDBUser($connection); |
|
| 150 | + |
|
| 151 | + break; |
|
| 152 | + } else { |
|
| 153 | + //repeat with different username |
|
| 154 | + $length = strlen((string)$i); |
|
| 155 | + $adminUser = substr('oc_' . $username, 0, 16 - $length) . $i; |
|
| 156 | + $i++; |
|
| 157 | + } |
|
| 158 | + } else { |
|
| 159 | + break; |
|
| 160 | + } |
|
| 161 | + } |
|
| 162 | + } |
|
| 163 | + } catch (\Exception $ex) { |
|
| 164 | + $this->logger->logException($ex, [ |
|
| 165 | + 'message' => 'Can not create a new MySQL user, will continue with the provided user.', |
|
| 166 | + 'level' => ILogger::INFO, |
|
| 167 | + 'app' => 'mysql.setup', |
|
| 168 | + ]); |
|
| 169 | + } |
|
| 170 | + |
|
| 171 | + $this->config->setValues([ |
|
| 172 | + 'dbuser' => $this->dbUser, |
|
| 173 | + 'dbpassword' => $this->dbPassword, |
|
| 174 | + ]); |
|
| 175 | + } |
|
| 176 | 176 | } |
@@ -53,7 +53,7 @@ discard block |
||
| 53 | 53 | $this->createDatabase($connection); |
| 54 | 54 | |
| 55 | 55 | //fill the database if needed |
| 56 | - $query='select count(*) from information_schema.tables where table_schema=? AND table_name = ?'; |
|
| 56 | + $query = 'select count(*) from information_schema.tables where table_schema=? AND table_name = ?'; |
|
| 57 | 57 | $connection->executeQuery($query, [$this->dbName, $this->tablePrefix.'users']); |
| 58 | 58 | } |
| 59 | 59 | |
@@ -61,7 +61,7 @@ discard block |
||
| 61 | 61 | * @param \OC\DB\Connection $connection |
| 62 | 62 | */ |
| 63 | 63 | private function createDatabase($connection) { |
| 64 | - try{ |
|
| 64 | + try { |
|
| 65 | 65 | $name = $this->dbName; |
| 66 | 66 | $user = $this->dbUser; |
| 67 | 67 | //we can't use OC_DB functions here because we need to connect as the administrative user. |
@@ -79,7 +79,7 @@ discard block |
||
| 79 | 79 | |
| 80 | 80 | try { |
| 81 | 81 | //this query will fail if there aren't the right permissions, ignore the error |
| 82 | - $query="GRANT ALL PRIVILEGES ON `$name` . * TO '$user'"; |
|
| 82 | + $query = "GRANT ALL PRIVILEGES ON `$name` . * TO '$user'"; |
|
| 83 | 83 | $connection->executeUpdate($query); |
| 84 | 84 | } catch (\Exception $ex) { |
| 85 | 85 | $this->logger->logException($ex, [ |
@@ -95,7 +95,7 @@ discard block |
||
| 95 | 95 | * @throws \OC\DatabaseSetupException |
| 96 | 96 | */ |
| 97 | 97 | private function createDBUser($connection) { |
| 98 | - try{ |
|
| 98 | + try { |
|
| 99 | 99 | $name = $this->dbUser; |
| 100 | 100 | $password = $this->dbPassword; |
| 101 | 101 | // we need to create 2 accounts, one for global use and one for local user. if we don't specify the local one, |
@@ -105,7 +105,7 @@ discard block |
||
| 105 | 105 | $query = "CREATE USER '$name'@'%' IDENTIFIED BY '$password'"; |
| 106 | 106 | $connection->executeUpdate($query); |
| 107 | 107 | } |
| 108 | - catch (\Exception $ex){ |
|
| 108 | + catch (\Exception $ex) { |
|
| 109 | 109 | $this->logger->logException($ex, [ |
| 110 | 110 | 'message' => 'Database user creation failed.', |
| 111 | 111 | 'level' => ILogger::ERROR, |
@@ -127,7 +127,7 @@ discard block |
||
| 127 | 127 | //we don't have a dbuser specified in config |
| 128 | 128 | if ($this->dbUser !== $oldUser) { |
| 129 | 129 | //add prefix to the admin username to prevent collisions |
| 130 | - $adminUser = substr('oc_' . $username, 0, 16); |
|
| 130 | + $adminUser = substr('oc_'.$username, 0, 16); |
|
| 131 | 131 | |
| 132 | 132 | $i = 1; |
| 133 | 133 | while (true) { |
@@ -144,15 +144,15 @@ discard block |
||
| 144 | 144 | $this->dbUser = $adminUser; |
| 145 | 145 | |
| 146 | 146 | //create a random password so we don't need to store the admin password in the config file |
| 147 | - $this->dbPassword = $this->random->generate(30); |
|
| 147 | + $this->dbPassword = $this->random->generate(30); |
|
| 148 | 148 | |
| 149 | 149 | $this->createDBUser($connection); |
| 150 | 150 | |
| 151 | 151 | break; |
| 152 | 152 | } else { |
| 153 | 153 | //repeat with different username |
| 154 | - $length = strlen((string)$i); |
|
| 155 | - $adminUser = substr('oc_' . $username, 0, 16 - $length) . $i; |
|
| 154 | + $length = strlen((string) $i); |
|
| 155 | + $adminUser = substr('oc_'.$username, 0, 16 - $length).$i; |
|
| 156 | 156 | $i++; |
| 157 | 157 | } |
| 158 | 158 | } else { |
@@ -104,8 +104,7 @@ |
||
| 104 | 104 | $connection->executeUpdate($query); |
| 105 | 105 | $query = "CREATE USER '$name'@'%' IDENTIFIED BY '$password'"; |
| 106 | 106 | $connection->executeUpdate($query); |
| 107 | - } |
|
| 108 | - catch (\Exception $ex){ |
|
| 107 | + } catch (\Exception $ex){ |
|
| 109 | 108 | $this->logger->logException($ex, [ |
| 110 | 109 | 'message' => 'Database user creation failed.', |
| 111 | 110 | 'level' => ILogger::ERROR, |
@@ -360,6 +360,9 @@ discard block |
||
| 360 | 360 | } |
| 361 | 361 | } |
| 362 | 362 | |
| 363 | + /** |
|
| 364 | + * @param string $root |
|
| 365 | + */ |
|
| 363 | 366 | static public function init($user, $root) { |
| 364 | 367 | if (self::$defaultInstance) { |
| 365 | 368 | return false; |
@@ -528,7 +531,7 @@ discard block |
||
| 528 | 531 | /** |
| 529 | 532 | * mount an \OC\Files\Storage\Storage in our virtual filesystem |
| 530 | 533 | * |
| 531 | - * @param \OC\Files\Storage\Storage|string $class |
|
| 534 | + * @param string $class |
|
| 532 | 535 | * @param array $arguments |
| 533 | 536 | * @param string $mountpoint |
| 534 | 537 | */ |
@@ -660,6 +663,9 @@ discard block |
||
| 660 | 663 | return self::$defaultInstance->is_dir($path); |
| 661 | 664 | } |
| 662 | 665 | |
| 666 | + /** |
|
| 667 | + * @param string $path |
|
| 668 | + */ |
|
| 663 | 669 | static public function is_file($path) { |
| 664 | 670 | return self::$defaultInstance->is_file($path); |
| 665 | 671 | } |
@@ -672,6 +678,9 @@ discard block |
||
| 672 | 678 | return self::$defaultInstance->filetype($path); |
| 673 | 679 | } |
| 674 | 680 | |
| 681 | + /** |
|
| 682 | + * @param string $path |
|
| 683 | + */ |
|
| 675 | 684 | static public function filesize($path) { |
| 676 | 685 | return self::$defaultInstance->filesize($path); |
| 677 | 686 | } |
@@ -684,6 +693,9 @@ discard block |
||
| 684 | 693 | return self::$defaultInstance->isCreatable($path); |
| 685 | 694 | } |
| 686 | 695 | |
| 696 | + /** |
|
| 697 | + * @param string $path |
|
| 698 | + */ |
|
| 687 | 699 | static public function isReadable($path) { |
| 688 | 700 | return self::$defaultInstance->isReadable($path); |
| 689 | 701 | } |
@@ -696,6 +708,9 @@ discard block |
||
| 696 | 708 | return self::$defaultInstance->isDeletable($path); |
| 697 | 709 | } |
| 698 | 710 | |
| 711 | + /** |
|
| 712 | + * @param string $path |
|
| 713 | + */ |
|
| 699 | 714 | static public function isSharable($path) { |
| 700 | 715 | return self::$defaultInstance->isSharable($path); |
| 701 | 716 | } |
@@ -704,6 +719,9 @@ discard block |
||
| 704 | 719 | return self::$defaultInstance->file_exists($path); |
| 705 | 720 | } |
| 706 | 721 | |
| 722 | + /** |
|
| 723 | + * @param string $path |
|
| 724 | + */ |
|
| 707 | 725 | static public function filemtime($path) { |
| 708 | 726 | return self::$defaultInstance->filemtime($path); |
| 709 | 727 | } |
@@ -713,6 +731,7 @@ discard block |
||
| 713 | 731 | } |
| 714 | 732 | |
| 715 | 733 | /** |
| 734 | + * @param string $path |
|
| 716 | 735 | * @return string |
| 717 | 736 | */ |
| 718 | 737 | static public function file_get_contents($path) { |
@@ -735,6 +754,10 @@ discard block |
||
| 735 | 754 | return self::$defaultInstance->copy($path1, $path2); |
| 736 | 755 | } |
| 737 | 756 | |
| 757 | + /** |
|
| 758 | + * @param string $path |
|
| 759 | + * @param string $mode |
|
| 760 | + */ |
|
| 738 | 761 | static public function fopen($path, $mode) { |
| 739 | 762 | return self::$defaultInstance->fopen($path, $mode); |
| 740 | 763 | } |
@@ -750,6 +773,9 @@ discard block |
||
| 750 | 773 | return self::$defaultInstance->fromTmpFile($tmpFile, $path); |
| 751 | 774 | } |
| 752 | 775 | |
| 776 | + /** |
|
| 777 | + * @param string $path |
|
| 778 | + */ |
|
| 753 | 779 | static public function getMimeType($path) { |
| 754 | 780 | return self::$defaultInstance->getMimeType($path); |
| 755 | 781 | } |
@@ -762,6 +788,9 @@ discard block |
||
| 762 | 788 | return self::$defaultInstance->free_space($path); |
| 763 | 789 | } |
| 764 | 790 | |
| 791 | + /** |
|
| 792 | + * @param string $query |
|
| 793 | + */ |
|
| 765 | 794 | static public function search($query) { |
| 766 | 795 | return self::$defaultInstance->search($query); |
| 767 | 796 | } |
@@ -870,7 +899,7 @@ discard block |
||
| 870 | 899 | * @param string $path |
| 871 | 900 | * @param boolean $includeMountPoints whether to add mountpoint sizes, |
| 872 | 901 | * defaults to true |
| 873 | - * @return \OC\Files\FileInfo|bool False if file does not exist |
|
| 902 | + * @return \OCP\Files\FileInfo|null False if file does not exist |
|
| 874 | 903 | */ |
| 875 | 904 | public static function getFileInfo($path, $includeMountPoints = true) { |
| 876 | 905 | return self::$defaultInstance->getFileInfo($path, $includeMountPoints); |
@@ -70,857 +70,857 @@ |
||
| 70 | 70 | |
| 71 | 71 | class Filesystem { |
| 72 | 72 | |
| 73 | - /** |
|
| 74 | - * @var Mount\Manager $mounts |
|
| 75 | - */ |
|
| 76 | - private static $mounts; |
|
| 77 | - |
|
| 78 | - public static $loaded = false; |
|
| 79 | - /** |
|
| 80 | - * @var \OC\Files\View $defaultInstance |
|
| 81 | - */ |
|
| 82 | - static private $defaultInstance; |
|
| 83 | - |
|
| 84 | - static private $usersSetup = array(); |
|
| 85 | - |
|
| 86 | - static private $normalizedPathCache = null; |
|
| 87 | - |
|
| 88 | - static private $listeningForProviders = false; |
|
| 89 | - |
|
| 90 | - /** |
|
| 91 | - * classname which used for hooks handling |
|
| 92 | - * used as signalclass in OC_Hooks::emit() |
|
| 93 | - */ |
|
| 94 | - const CLASSNAME = 'OC_Filesystem'; |
|
| 95 | - |
|
| 96 | - /** |
|
| 97 | - * signalname emitted before file renaming |
|
| 98 | - * |
|
| 99 | - * @param string $oldpath |
|
| 100 | - * @param string $newpath |
|
| 101 | - */ |
|
| 102 | - const signal_rename = 'rename'; |
|
| 103 | - |
|
| 104 | - /** |
|
| 105 | - * signal emitted after file renaming |
|
| 106 | - * |
|
| 107 | - * @param string $oldpath |
|
| 108 | - * @param string $newpath |
|
| 109 | - */ |
|
| 110 | - const signal_post_rename = 'post_rename'; |
|
| 111 | - |
|
| 112 | - /** |
|
| 113 | - * signal emitted before file/dir creation |
|
| 114 | - * |
|
| 115 | - * @param string $path |
|
| 116 | - * @param bool $run changing this flag to false in hook handler will cancel event |
|
| 117 | - */ |
|
| 118 | - const signal_create = 'create'; |
|
| 119 | - |
|
| 120 | - /** |
|
| 121 | - * signal emitted after file/dir creation |
|
| 122 | - * |
|
| 123 | - * @param string $path |
|
| 124 | - * @param bool $run changing this flag to false in hook handler will cancel event |
|
| 125 | - */ |
|
| 126 | - const signal_post_create = 'post_create'; |
|
| 127 | - |
|
| 128 | - /** |
|
| 129 | - * signal emits before file/dir copy |
|
| 130 | - * |
|
| 131 | - * @param string $oldpath |
|
| 132 | - * @param string $newpath |
|
| 133 | - * @param bool $run changing this flag to false in hook handler will cancel event |
|
| 134 | - */ |
|
| 135 | - const signal_copy = 'copy'; |
|
| 136 | - |
|
| 137 | - /** |
|
| 138 | - * signal emits after file/dir copy |
|
| 139 | - * |
|
| 140 | - * @param string $oldpath |
|
| 141 | - * @param string $newpath |
|
| 142 | - */ |
|
| 143 | - const signal_post_copy = 'post_copy'; |
|
| 144 | - |
|
| 145 | - /** |
|
| 146 | - * signal emits before file/dir save |
|
| 147 | - * |
|
| 148 | - * @param string $path |
|
| 149 | - * @param bool $run changing this flag to false in hook handler will cancel event |
|
| 150 | - */ |
|
| 151 | - const signal_write = 'write'; |
|
| 152 | - |
|
| 153 | - /** |
|
| 154 | - * signal emits after file/dir save |
|
| 155 | - * |
|
| 156 | - * @param string $path |
|
| 157 | - */ |
|
| 158 | - const signal_post_write = 'post_write'; |
|
| 159 | - |
|
| 160 | - /** |
|
| 161 | - * signal emitted before file/dir update |
|
| 162 | - * |
|
| 163 | - * @param string $path |
|
| 164 | - * @param bool $run changing this flag to false in hook handler will cancel event |
|
| 165 | - */ |
|
| 166 | - const signal_update = 'update'; |
|
| 167 | - |
|
| 168 | - /** |
|
| 169 | - * signal emitted after file/dir update |
|
| 170 | - * |
|
| 171 | - * @param string $path |
|
| 172 | - * @param bool $run changing this flag to false in hook handler will cancel event |
|
| 173 | - */ |
|
| 174 | - const signal_post_update = 'post_update'; |
|
| 175 | - |
|
| 176 | - /** |
|
| 177 | - * signal emits when reading file/dir |
|
| 178 | - * |
|
| 179 | - * @param string $path |
|
| 180 | - */ |
|
| 181 | - const signal_read = 'read'; |
|
| 182 | - |
|
| 183 | - /** |
|
| 184 | - * signal emits when removing file/dir |
|
| 185 | - * |
|
| 186 | - * @param string $path |
|
| 187 | - */ |
|
| 188 | - const signal_delete = 'delete'; |
|
| 189 | - |
|
| 190 | - /** |
|
| 191 | - * parameters definitions for signals |
|
| 192 | - */ |
|
| 193 | - const signal_param_path = 'path'; |
|
| 194 | - const signal_param_oldpath = 'oldpath'; |
|
| 195 | - const signal_param_newpath = 'newpath'; |
|
| 196 | - |
|
| 197 | - /** |
|
| 198 | - * run - changing this flag to false in hook handler will cancel event |
|
| 199 | - */ |
|
| 200 | - const signal_param_run = 'run'; |
|
| 201 | - |
|
| 202 | - const signal_create_mount = 'create_mount'; |
|
| 203 | - const signal_delete_mount = 'delete_mount'; |
|
| 204 | - const signal_param_mount_type = 'mounttype'; |
|
| 205 | - const signal_param_users = 'users'; |
|
| 206 | - |
|
| 207 | - /** |
|
| 208 | - * @var \OC\Files\Storage\StorageFactory $loader |
|
| 209 | - */ |
|
| 210 | - private static $loader; |
|
| 211 | - |
|
| 212 | - /** @var bool */ |
|
| 213 | - private static $logWarningWhenAddingStorageWrapper = true; |
|
| 214 | - |
|
| 215 | - /** |
|
| 216 | - * @param bool $shouldLog |
|
| 217 | - * @return bool previous value |
|
| 218 | - * @internal |
|
| 219 | - */ |
|
| 220 | - public static function logWarningWhenAddingStorageWrapper($shouldLog) { |
|
| 221 | - $previousValue = self::$logWarningWhenAddingStorageWrapper; |
|
| 222 | - self::$logWarningWhenAddingStorageWrapper = (bool) $shouldLog; |
|
| 223 | - return $previousValue; |
|
| 224 | - } |
|
| 225 | - |
|
| 226 | - /** |
|
| 227 | - * @param string $wrapperName |
|
| 228 | - * @param callable $wrapper |
|
| 229 | - * @param int $priority |
|
| 230 | - */ |
|
| 231 | - public static function addStorageWrapper($wrapperName, $wrapper, $priority = 50) { |
|
| 232 | - if (self::$logWarningWhenAddingStorageWrapper) { |
|
| 233 | - \OC::$server->getLogger()->warning("Storage wrapper '{wrapper}' was not registered via the 'OC_Filesystem - preSetup' hook which could cause potential problems.", [ |
|
| 234 | - 'wrapper' => $wrapperName, |
|
| 235 | - 'app' => 'filesystem', |
|
| 236 | - ]); |
|
| 237 | - } |
|
| 238 | - |
|
| 239 | - $mounts = self::getMountManager()->getAll(); |
|
| 240 | - if (!self::getLoader()->addStorageWrapper($wrapperName, $wrapper, $priority, $mounts)) { |
|
| 241 | - // do not re-wrap if storage with this name already existed |
|
| 242 | - return; |
|
| 243 | - } |
|
| 244 | - } |
|
| 245 | - |
|
| 246 | - /** |
|
| 247 | - * Returns the storage factory |
|
| 248 | - * |
|
| 249 | - * @return \OCP\Files\Storage\IStorageFactory |
|
| 250 | - */ |
|
| 251 | - public static function getLoader() { |
|
| 252 | - if (!self::$loader) { |
|
| 253 | - self::$loader = new StorageFactory(); |
|
| 254 | - } |
|
| 255 | - return self::$loader; |
|
| 256 | - } |
|
| 257 | - |
|
| 258 | - /** |
|
| 259 | - * Returns the mount manager |
|
| 260 | - * |
|
| 261 | - * @return \OC\Files\Mount\Manager |
|
| 262 | - */ |
|
| 263 | - public static function getMountManager($user = '') { |
|
| 264 | - if (!self::$mounts) { |
|
| 265 | - \OC_Util::setupFS($user); |
|
| 266 | - } |
|
| 267 | - return self::$mounts; |
|
| 268 | - } |
|
| 269 | - |
|
| 270 | - /** |
|
| 271 | - * get the mountpoint of the storage object for a path |
|
| 272 | - * ( note: because a storage is not always mounted inside the fakeroot, the |
|
| 273 | - * returned mountpoint is relative to the absolute root of the filesystem |
|
| 274 | - * and doesn't take the chroot into account ) |
|
| 275 | - * |
|
| 276 | - * @param string $path |
|
| 277 | - * @return string |
|
| 278 | - */ |
|
| 279 | - static public function getMountPoint($path) { |
|
| 280 | - if (!self::$mounts) { |
|
| 281 | - \OC_Util::setupFS(); |
|
| 282 | - } |
|
| 283 | - $mount = self::$mounts->find($path); |
|
| 284 | - if ($mount) { |
|
| 285 | - return $mount->getMountPoint(); |
|
| 286 | - } else { |
|
| 287 | - return ''; |
|
| 288 | - } |
|
| 289 | - } |
|
| 290 | - |
|
| 291 | - /** |
|
| 292 | - * get a list of all mount points in a directory |
|
| 293 | - * |
|
| 294 | - * @param string $path |
|
| 295 | - * @return string[] |
|
| 296 | - */ |
|
| 297 | - static public function getMountPoints($path) { |
|
| 298 | - if (!self::$mounts) { |
|
| 299 | - \OC_Util::setupFS(); |
|
| 300 | - } |
|
| 301 | - $result = array(); |
|
| 302 | - $mounts = self::$mounts->findIn($path); |
|
| 303 | - foreach ($mounts as $mount) { |
|
| 304 | - $result[] = $mount->getMountPoint(); |
|
| 305 | - } |
|
| 306 | - return $result; |
|
| 307 | - } |
|
| 308 | - |
|
| 309 | - /** |
|
| 310 | - * get the storage mounted at $mountPoint |
|
| 311 | - * |
|
| 312 | - * @param string $mountPoint |
|
| 313 | - * @return \OC\Files\Storage\Storage |
|
| 314 | - */ |
|
| 315 | - public static function getStorage($mountPoint) { |
|
| 316 | - if (!self::$mounts) { |
|
| 317 | - \OC_Util::setupFS(); |
|
| 318 | - } |
|
| 319 | - $mount = self::$mounts->find($mountPoint); |
|
| 320 | - return $mount->getStorage(); |
|
| 321 | - } |
|
| 322 | - |
|
| 323 | - /** |
|
| 324 | - * @param string $id |
|
| 325 | - * @return Mount\MountPoint[] |
|
| 326 | - */ |
|
| 327 | - public static function getMountByStorageId($id) { |
|
| 328 | - if (!self::$mounts) { |
|
| 329 | - \OC_Util::setupFS(); |
|
| 330 | - } |
|
| 331 | - return self::$mounts->findByStorageId($id); |
|
| 332 | - } |
|
| 333 | - |
|
| 334 | - /** |
|
| 335 | - * @param int $id |
|
| 336 | - * @return Mount\MountPoint[] |
|
| 337 | - */ |
|
| 338 | - public static function getMountByNumericId($id) { |
|
| 339 | - if (!self::$mounts) { |
|
| 340 | - \OC_Util::setupFS(); |
|
| 341 | - } |
|
| 342 | - return self::$mounts->findByNumericId($id); |
|
| 343 | - } |
|
| 344 | - |
|
| 345 | - /** |
|
| 346 | - * resolve a path to a storage and internal path |
|
| 347 | - * |
|
| 348 | - * @param string $path |
|
| 349 | - * @return array an array consisting of the storage and the internal path |
|
| 350 | - */ |
|
| 351 | - static public function resolvePath($path) { |
|
| 352 | - if (!self::$mounts) { |
|
| 353 | - \OC_Util::setupFS(); |
|
| 354 | - } |
|
| 355 | - $mount = self::$mounts->find($path); |
|
| 356 | - if ($mount) { |
|
| 357 | - return array($mount->getStorage(), rtrim($mount->getInternalPath($path), '/')); |
|
| 358 | - } else { |
|
| 359 | - return array(null, null); |
|
| 360 | - } |
|
| 361 | - } |
|
| 362 | - |
|
| 363 | - static public function init($user, $root) { |
|
| 364 | - if (self::$defaultInstance) { |
|
| 365 | - return false; |
|
| 366 | - } |
|
| 367 | - self::getLoader(); |
|
| 368 | - self::$defaultInstance = new View($root); |
|
| 369 | - |
|
| 370 | - if (!self::$mounts) { |
|
| 371 | - self::$mounts = \OC::$server->getMountManager(); |
|
| 372 | - } |
|
| 373 | - |
|
| 374 | - //load custom mount config |
|
| 375 | - self::initMountPoints($user); |
|
| 376 | - |
|
| 377 | - self::$loaded = true; |
|
| 378 | - |
|
| 379 | - return true; |
|
| 380 | - } |
|
| 381 | - |
|
| 382 | - static public function initMountManager() { |
|
| 383 | - if (!self::$mounts) { |
|
| 384 | - self::$mounts = \OC::$server->getMountManager(); |
|
| 385 | - } |
|
| 386 | - } |
|
| 387 | - |
|
| 388 | - /** |
|
| 389 | - * Initialize system and personal mount points for a user |
|
| 390 | - * |
|
| 391 | - * @param string $user |
|
| 392 | - * @throws \OC\User\NoUserException if the user is not available |
|
| 393 | - */ |
|
| 394 | - public static function initMountPoints($user = '') { |
|
| 395 | - if ($user == '') { |
|
| 396 | - $user = \OC_User::getUser(); |
|
| 397 | - } |
|
| 398 | - if ($user === null || $user === false || $user === '') { |
|
| 399 | - throw new \OC\User\NoUserException('Attempted to initialize mount points for null user and no user in session'); |
|
| 400 | - } |
|
| 401 | - |
|
| 402 | - if (isset(self::$usersSetup[$user])) { |
|
| 403 | - return; |
|
| 404 | - } |
|
| 405 | - |
|
| 406 | - self::$usersSetup[$user] = true; |
|
| 407 | - |
|
| 408 | - $userManager = \OC::$server->getUserManager(); |
|
| 409 | - $userObject = $userManager->get($user); |
|
| 410 | - |
|
| 411 | - if (is_null($userObject)) { |
|
| 412 | - \OCP\Util::writeLog('files', ' Backends provided no user object for ' . $user, ILogger::ERROR); |
|
| 413 | - // reset flag, this will make it possible to rethrow the exception if called again |
|
| 414 | - unset(self::$usersSetup[$user]); |
|
| 415 | - throw new \OC\User\NoUserException('Backends provided no user object for ' . $user); |
|
| 416 | - } |
|
| 417 | - |
|
| 418 | - $realUid = $userObject->getUID(); |
|
| 419 | - // workaround in case of different casings |
|
| 420 | - if ($user !== $realUid) { |
|
| 421 | - $stack = json_encode(debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 50)); |
|
| 422 | - \OCP\Util::writeLog('files', 'initMountPoints() called with wrong user casing. This could be a bug. Expected: "' . $realUid . '" got "' . $user . '". Stack: ' . $stack, ILogger::WARN); |
|
| 423 | - $user = $realUid; |
|
| 424 | - |
|
| 425 | - // again with the correct casing |
|
| 426 | - if (isset(self::$usersSetup[$user])) { |
|
| 427 | - return; |
|
| 428 | - } |
|
| 429 | - |
|
| 430 | - self::$usersSetup[$user] = true; |
|
| 431 | - } |
|
| 432 | - |
|
| 433 | - if (\OC::$server->getLockdownManager()->canAccessFilesystem()) { |
|
| 434 | - /** @var \OC\Files\Config\MountProviderCollection $mountConfigManager */ |
|
| 435 | - $mountConfigManager = \OC::$server->getMountProviderCollection(); |
|
| 436 | - |
|
| 437 | - // home mounts are handled seperate since we need to ensure this is mounted before we call the other mount providers |
|
| 438 | - $homeMount = $mountConfigManager->getHomeMountForUser($userObject); |
|
| 439 | - |
|
| 440 | - self::getMountManager()->addMount($homeMount); |
|
| 441 | - |
|
| 442 | - \OC\Files\Filesystem::getStorage($user); |
|
| 443 | - |
|
| 444 | - // Chance to mount for other storages |
|
| 445 | - if ($userObject) { |
|
| 446 | - $mounts = $mountConfigManager->addMountForUser($userObject, self::getMountManager()); |
|
| 447 | - $mounts[] = $homeMount; |
|
| 448 | - $mountConfigManager->registerMounts($userObject, $mounts); |
|
| 449 | - } |
|
| 450 | - |
|
| 451 | - self::listenForNewMountProviders($mountConfigManager, $userManager); |
|
| 452 | - } else { |
|
| 453 | - self::getMountManager()->addMount(new MountPoint( |
|
| 454 | - new NullStorage([]), |
|
| 455 | - '/' . $user |
|
| 456 | - )); |
|
| 457 | - self::getMountManager()->addMount(new MountPoint( |
|
| 458 | - new NullStorage([]), |
|
| 459 | - '/' . $user . '/files' |
|
| 460 | - )); |
|
| 461 | - } |
|
| 462 | - \OC_Hook::emit('OC_Filesystem', 'post_initMountPoints', array('user' => $user)); |
|
| 463 | - } |
|
| 464 | - |
|
| 465 | - /** |
|
| 466 | - * Get mounts from mount providers that are registered after setup |
|
| 467 | - * |
|
| 468 | - * @param MountProviderCollection $mountConfigManager |
|
| 469 | - * @param IUserManager $userManager |
|
| 470 | - */ |
|
| 471 | - private static function listenForNewMountProviders(MountProviderCollection $mountConfigManager, IUserManager $userManager) { |
|
| 472 | - if (!self::$listeningForProviders) { |
|
| 473 | - self::$listeningForProviders = true; |
|
| 474 | - $mountConfigManager->listen('\OC\Files\Config', 'registerMountProvider', function (IMountProvider $provider) use ($userManager) { |
|
| 475 | - foreach (Filesystem::$usersSetup as $user => $setup) { |
|
| 476 | - $userObject = $userManager->get($user); |
|
| 477 | - if ($userObject) { |
|
| 478 | - $mounts = $provider->getMountsForUser($userObject, Filesystem::getLoader()); |
|
| 479 | - array_walk($mounts, array(self::$mounts, 'addMount')); |
|
| 480 | - } |
|
| 481 | - } |
|
| 482 | - }); |
|
| 483 | - } |
|
| 484 | - } |
|
| 485 | - |
|
| 486 | - /** |
|
| 487 | - * get the default filesystem view |
|
| 488 | - * |
|
| 489 | - * @return View |
|
| 490 | - */ |
|
| 491 | - static public function getView() { |
|
| 492 | - return self::$defaultInstance; |
|
| 493 | - } |
|
| 494 | - |
|
| 495 | - /** |
|
| 496 | - * tear down the filesystem, removing all storage providers |
|
| 497 | - */ |
|
| 498 | - static public function tearDown() { |
|
| 499 | - self::clearMounts(); |
|
| 500 | - self::$defaultInstance = null; |
|
| 501 | - } |
|
| 502 | - |
|
| 503 | - /** |
|
| 504 | - * get the relative path of the root data directory for the current user |
|
| 505 | - * |
|
| 506 | - * @return string |
|
| 507 | - * |
|
| 508 | - * Returns path like /admin/files |
|
| 509 | - */ |
|
| 510 | - static public function getRoot() { |
|
| 511 | - if (!self::$defaultInstance) { |
|
| 512 | - return null; |
|
| 513 | - } |
|
| 514 | - return self::$defaultInstance->getRoot(); |
|
| 515 | - } |
|
| 516 | - |
|
| 517 | - /** |
|
| 518 | - * clear all mounts and storage backends |
|
| 519 | - */ |
|
| 520 | - public static function clearMounts() { |
|
| 521 | - if (self::$mounts) { |
|
| 522 | - self::$usersSetup = array(); |
|
| 523 | - self::$mounts->clear(); |
|
| 524 | - } |
|
| 525 | - } |
|
| 526 | - |
|
| 527 | - /** |
|
| 528 | - * mount an \OC\Files\Storage\Storage in our virtual filesystem |
|
| 529 | - * |
|
| 530 | - * @param \OC\Files\Storage\Storage|string $class |
|
| 531 | - * @param array $arguments |
|
| 532 | - * @param string $mountpoint |
|
| 533 | - */ |
|
| 534 | - static public function mount($class, $arguments, $mountpoint) { |
|
| 535 | - if (!self::$mounts) { |
|
| 536 | - \OC_Util::setupFS(); |
|
| 537 | - } |
|
| 538 | - $mount = new Mount\MountPoint($class, $mountpoint, $arguments, self::getLoader()); |
|
| 539 | - self::$mounts->addMount($mount); |
|
| 540 | - } |
|
| 541 | - |
|
| 542 | - /** |
|
| 543 | - * return the path to a local version of the file |
|
| 544 | - * we need this because we can't know if a file is stored local or not from |
|
| 545 | - * outside the filestorage and for some purposes a local file is needed |
|
| 546 | - * |
|
| 547 | - * @param string $path |
|
| 548 | - * @return string |
|
| 549 | - */ |
|
| 550 | - static public function getLocalFile($path) { |
|
| 551 | - return self::$defaultInstance->getLocalFile($path); |
|
| 552 | - } |
|
| 553 | - |
|
| 554 | - /** |
|
| 555 | - * @param string $path |
|
| 556 | - * @return string |
|
| 557 | - */ |
|
| 558 | - static public function getLocalFolder($path) { |
|
| 559 | - return self::$defaultInstance->getLocalFolder($path); |
|
| 560 | - } |
|
| 561 | - |
|
| 562 | - /** |
|
| 563 | - * return path to file which reflects one visible in browser |
|
| 564 | - * |
|
| 565 | - * @param string $path |
|
| 566 | - * @return string |
|
| 567 | - */ |
|
| 568 | - static public function getLocalPath($path) { |
|
| 569 | - $datadir = \OC_User::getHome(\OC_User::getUser()) . '/files'; |
|
| 570 | - $newpath = $path; |
|
| 571 | - if (strncmp($newpath, $datadir, strlen($datadir)) == 0) { |
|
| 572 | - $newpath = substr($path, strlen($datadir)); |
|
| 573 | - } |
|
| 574 | - return $newpath; |
|
| 575 | - } |
|
| 576 | - |
|
| 577 | - /** |
|
| 578 | - * check if the requested path is valid |
|
| 579 | - * |
|
| 580 | - * @param string $path |
|
| 581 | - * @return bool |
|
| 582 | - */ |
|
| 583 | - static public function isValidPath($path) { |
|
| 584 | - $path = self::normalizePath($path); |
|
| 585 | - if (!$path || $path[0] !== '/') { |
|
| 586 | - $path = '/' . $path; |
|
| 587 | - } |
|
| 588 | - if (strpos($path, '/../') !== false || strrchr($path, '/') === '/..') { |
|
| 589 | - return false; |
|
| 590 | - } |
|
| 591 | - return true; |
|
| 592 | - } |
|
| 593 | - |
|
| 594 | - /** |
|
| 595 | - * checks if a file is blacklisted for storage in the filesystem |
|
| 596 | - * Listens to write and rename hooks |
|
| 597 | - * |
|
| 598 | - * @param array $data from hook |
|
| 599 | - */ |
|
| 600 | - static public function isBlacklisted($data) { |
|
| 601 | - if (isset($data['path'])) { |
|
| 602 | - $path = $data['path']; |
|
| 603 | - } else if (isset($data['newpath'])) { |
|
| 604 | - $path = $data['newpath']; |
|
| 605 | - } |
|
| 606 | - if (isset($path)) { |
|
| 607 | - if (self::isFileBlacklisted($path)) { |
|
| 608 | - $data['run'] = false; |
|
| 609 | - } |
|
| 610 | - } |
|
| 611 | - } |
|
| 612 | - |
|
| 613 | - /** |
|
| 614 | - * @param string $filename |
|
| 615 | - * @return bool |
|
| 616 | - */ |
|
| 617 | - static public function isFileBlacklisted($filename) { |
|
| 618 | - $filename = self::normalizePath($filename); |
|
| 619 | - |
|
| 620 | - $blacklist = \OC::$server->getConfig()->getSystemValue('blacklisted_files', array('.htaccess')); |
|
| 621 | - $filename = strtolower(basename($filename)); |
|
| 622 | - return in_array($filename, $blacklist); |
|
| 623 | - } |
|
| 624 | - |
|
| 625 | - /** |
|
| 626 | - * check if the directory should be ignored when scanning |
|
| 627 | - * NOTE: the special directories . and .. would cause never ending recursion |
|
| 628 | - * |
|
| 629 | - * @param String $dir |
|
| 630 | - * @return boolean |
|
| 631 | - */ |
|
| 632 | - static public function isIgnoredDir($dir) { |
|
| 633 | - if ($dir === '.' || $dir === '..') { |
|
| 634 | - return true; |
|
| 635 | - } |
|
| 636 | - return false; |
|
| 637 | - } |
|
| 638 | - |
|
| 639 | - /** |
|
| 640 | - * following functions are equivalent to their php builtin equivalents for arguments/return values. |
|
| 641 | - */ |
|
| 642 | - static public function mkdir($path) { |
|
| 643 | - return self::$defaultInstance->mkdir($path); |
|
| 644 | - } |
|
| 645 | - |
|
| 646 | - static public function rmdir($path) { |
|
| 647 | - return self::$defaultInstance->rmdir($path); |
|
| 648 | - } |
|
| 649 | - |
|
| 650 | - static public function is_dir($path) { |
|
| 651 | - return self::$defaultInstance->is_dir($path); |
|
| 652 | - } |
|
| 653 | - |
|
| 654 | - static public function is_file($path) { |
|
| 655 | - return self::$defaultInstance->is_file($path); |
|
| 656 | - } |
|
| 657 | - |
|
| 658 | - static public function stat($path) { |
|
| 659 | - return self::$defaultInstance->stat($path); |
|
| 660 | - } |
|
| 661 | - |
|
| 662 | - static public function filetype($path) { |
|
| 663 | - return self::$defaultInstance->filetype($path); |
|
| 664 | - } |
|
| 665 | - |
|
| 666 | - static public function filesize($path) { |
|
| 667 | - return self::$defaultInstance->filesize($path); |
|
| 668 | - } |
|
| 669 | - |
|
| 670 | - static public function readfile($path) { |
|
| 671 | - return self::$defaultInstance->readfile($path); |
|
| 672 | - } |
|
| 673 | - |
|
| 674 | - static public function isCreatable($path) { |
|
| 675 | - return self::$defaultInstance->isCreatable($path); |
|
| 676 | - } |
|
| 677 | - |
|
| 678 | - static public function isReadable($path) { |
|
| 679 | - return self::$defaultInstance->isReadable($path); |
|
| 680 | - } |
|
| 681 | - |
|
| 682 | - static public function isUpdatable($path) { |
|
| 683 | - return self::$defaultInstance->isUpdatable($path); |
|
| 684 | - } |
|
| 685 | - |
|
| 686 | - static public function isDeletable($path) { |
|
| 687 | - return self::$defaultInstance->isDeletable($path); |
|
| 688 | - } |
|
| 689 | - |
|
| 690 | - static public function isSharable($path) { |
|
| 691 | - return self::$defaultInstance->isSharable($path); |
|
| 692 | - } |
|
| 693 | - |
|
| 694 | - static public function file_exists($path) { |
|
| 695 | - return self::$defaultInstance->file_exists($path); |
|
| 696 | - } |
|
| 697 | - |
|
| 698 | - static public function filemtime($path) { |
|
| 699 | - return self::$defaultInstance->filemtime($path); |
|
| 700 | - } |
|
| 701 | - |
|
| 702 | - static public function touch($path, $mtime = null) { |
|
| 703 | - return self::$defaultInstance->touch($path, $mtime); |
|
| 704 | - } |
|
| 705 | - |
|
| 706 | - /** |
|
| 707 | - * @return string |
|
| 708 | - */ |
|
| 709 | - static public function file_get_contents($path) { |
|
| 710 | - return self::$defaultInstance->file_get_contents($path); |
|
| 711 | - } |
|
| 712 | - |
|
| 713 | - static public function file_put_contents($path, $data) { |
|
| 714 | - return self::$defaultInstance->file_put_contents($path, $data); |
|
| 715 | - } |
|
| 716 | - |
|
| 717 | - static public function unlink($path) { |
|
| 718 | - return self::$defaultInstance->unlink($path); |
|
| 719 | - } |
|
| 720 | - |
|
| 721 | - static public function rename($path1, $path2) { |
|
| 722 | - return self::$defaultInstance->rename($path1, $path2); |
|
| 723 | - } |
|
| 724 | - |
|
| 725 | - static public function copy($path1, $path2) { |
|
| 726 | - return self::$defaultInstance->copy($path1, $path2); |
|
| 727 | - } |
|
| 728 | - |
|
| 729 | - static public function fopen($path, $mode) { |
|
| 730 | - return self::$defaultInstance->fopen($path, $mode); |
|
| 731 | - } |
|
| 732 | - |
|
| 733 | - /** |
|
| 734 | - * @return string |
|
| 735 | - */ |
|
| 736 | - static public function toTmpFile($path) { |
|
| 737 | - return self::$defaultInstance->toTmpFile($path); |
|
| 738 | - } |
|
| 739 | - |
|
| 740 | - static public function fromTmpFile($tmpFile, $path) { |
|
| 741 | - return self::$defaultInstance->fromTmpFile($tmpFile, $path); |
|
| 742 | - } |
|
| 743 | - |
|
| 744 | - static public function getMimeType($path) { |
|
| 745 | - return self::$defaultInstance->getMimeType($path); |
|
| 746 | - } |
|
| 747 | - |
|
| 748 | - static public function hash($type, $path, $raw = false) { |
|
| 749 | - return self::$defaultInstance->hash($type, $path, $raw); |
|
| 750 | - } |
|
| 751 | - |
|
| 752 | - static public function free_space($path = '/') { |
|
| 753 | - return self::$defaultInstance->free_space($path); |
|
| 754 | - } |
|
| 755 | - |
|
| 756 | - static public function search($query) { |
|
| 757 | - return self::$defaultInstance->search($query); |
|
| 758 | - } |
|
| 759 | - |
|
| 760 | - /** |
|
| 761 | - * @param string $query |
|
| 762 | - */ |
|
| 763 | - static public function searchByMime($query) { |
|
| 764 | - return self::$defaultInstance->searchByMime($query); |
|
| 765 | - } |
|
| 766 | - |
|
| 767 | - /** |
|
| 768 | - * @param string|int $tag name or tag id |
|
| 769 | - * @param string $userId owner of the tags |
|
| 770 | - * @return FileInfo[] array or file info |
|
| 771 | - */ |
|
| 772 | - static public function searchByTag($tag, $userId) { |
|
| 773 | - return self::$defaultInstance->searchByTag($tag, $userId); |
|
| 774 | - } |
|
| 775 | - |
|
| 776 | - /** |
|
| 777 | - * check if a file or folder has been updated since $time |
|
| 778 | - * |
|
| 779 | - * @param string $path |
|
| 780 | - * @param int $time |
|
| 781 | - * @return bool |
|
| 782 | - */ |
|
| 783 | - static public function hasUpdated($path, $time) { |
|
| 784 | - return self::$defaultInstance->hasUpdated($path, $time); |
|
| 785 | - } |
|
| 786 | - |
|
| 787 | - /** |
|
| 788 | - * Fix common problems with a file path |
|
| 789 | - * |
|
| 790 | - * @param string $path |
|
| 791 | - * @param bool $stripTrailingSlash whether to strip the trailing slash |
|
| 792 | - * @param bool $isAbsolutePath whether the given path is absolute |
|
| 793 | - * @param bool $keepUnicode true to disable unicode normalization |
|
| 794 | - * @return string |
|
| 795 | - */ |
|
| 796 | - public static function normalizePath($path, $stripTrailingSlash = true, $isAbsolutePath = false, $keepUnicode = false) { |
|
| 797 | - if (is_null(self::$normalizedPathCache)) { |
|
| 798 | - self::$normalizedPathCache = new CappedMemoryCache(2048); |
|
| 799 | - } |
|
| 800 | - |
|
| 801 | - /** |
|
| 802 | - * FIXME: This is a workaround for existing classes and files which call |
|
| 803 | - * this function with another type than a valid string. This |
|
| 804 | - * conversion should get removed as soon as all existing |
|
| 805 | - * function calls have been fixed. |
|
| 806 | - */ |
|
| 807 | - $path = (string)$path; |
|
| 808 | - |
|
| 809 | - $cacheKey = json_encode([$path, $stripTrailingSlash, $isAbsolutePath, $keepUnicode]); |
|
| 810 | - |
|
| 811 | - if (isset(self::$normalizedPathCache[$cacheKey])) { |
|
| 812 | - return self::$normalizedPathCache[$cacheKey]; |
|
| 813 | - } |
|
| 814 | - |
|
| 815 | - if ($path == '') { |
|
| 816 | - return '/'; |
|
| 817 | - } |
|
| 818 | - |
|
| 819 | - //normalize unicode if possible |
|
| 820 | - if (!$keepUnicode) { |
|
| 821 | - $path = \OC_Util::normalizeUnicode($path); |
|
| 822 | - } |
|
| 823 | - |
|
| 824 | - //no windows style slashes |
|
| 825 | - $path = str_replace('\\', '/', $path); |
|
| 826 | - |
|
| 827 | - //add leading slash |
|
| 828 | - if ($path[0] !== '/') { |
|
| 829 | - $path = '/' . $path; |
|
| 830 | - } |
|
| 831 | - |
|
| 832 | - // remove '/./' |
|
| 833 | - // ugly, but str_replace() can't replace them all in one go |
|
| 834 | - // as the replacement itself is part of the search string |
|
| 835 | - // which will only be found during the next iteration |
|
| 836 | - while (strpos($path, '/./') !== false) { |
|
| 837 | - $path = str_replace('/./', '/', $path); |
|
| 838 | - } |
|
| 839 | - // remove sequences of slashes |
|
| 840 | - $path = preg_replace('#/{2,}#', '/', $path); |
|
| 841 | - |
|
| 842 | - //remove trailing slash |
|
| 843 | - if ($stripTrailingSlash and strlen($path) > 1) { |
|
| 844 | - $path = rtrim($path, '/'); |
|
| 845 | - } |
|
| 846 | - |
|
| 847 | - // remove trailing '/.' |
|
| 848 | - if (substr($path, -2) == '/.') { |
|
| 849 | - $path = substr($path, 0, -2); |
|
| 850 | - } |
|
| 851 | - |
|
| 852 | - $normalizedPath = $path; |
|
| 853 | - self::$normalizedPathCache[$cacheKey] = $normalizedPath; |
|
| 854 | - |
|
| 855 | - return $normalizedPath; |
|
| 856 | - } |
|
| 857 | - |
|
| 858 | - /** |
|
| 859 | - * get the filesystem info |
|
| 860 | - * |
|
| 861 | - * @param string $path |
|
| 862 | - * @param boolean $includeMountPoints whether to add mountpoint sizes, |
|
| 863 | - * defaults to true |
|
| 864 | - * @return \OC\Files\FileInfo|bool False if file does not exist |
|
| 865 | - */ |
|
| 866 | - public static function getFileInfo($path, $includeMountPoints = true) { |
|
| 867 | - return self::$defaultInstance->getFileInfo($path, $includeMountPoints); |
|
| 868 | - } |
|
| 869 | - |
|
| 870 | - /** |
|
| 871 | - * change file metadata |
|
| 872 | - * |
|
| 873 | - * @param string $path |
|
| 874 | - * @param array $data |
|
| 875 | - * @return int |
|
| 876 | - * |
|
| 877 | - * returns the fileid of the updated file |
|
| 878 | - */ |
|
| 879 | - public static function putFileInfo($path, $data) { |
|
| 880 | - return self::$defaultInstance->putFileInfo($path, $data); |
|
| 881 | - } |
|
| 882 | - |
|
| 883 | - /** |
|
| 884 | - * get the content of a directory |
|
| 885 | - * |
|
| 886 | - * @param string $directory path under datadirectory |
|
| 887 | - * @param string $mimetype_filter limit returned content to this mimetype or mimepart |
|
| 888 | - * @return \OC\Files\FileInfo[] |
|
| 889 | - */ |
|
| 890 | - public static function getDirectoryContent($directory, $mimetype_filter = '') { |
|
| 891 | - return self::$defaultInstance->getDirectoryContent($directory, $mimetype_filter); |
|
| 892 | - } |
|
| 893 | - |
|
| 894 | - /** |
|
| 895 | - * Get the path of a file by id |
|
| 896 | - * |
|
| 897 | - * Note that the resulting path is not guaranteed to be unique for the id, multiple paths can point to the same file |
|
| 898 | - * |
|
| 899 | - * @param int $id |
|
| 900 | - * @throws NotFoundException |
|
| 901 | - * @return string |
|
| 902 | - */ |
|
| 903 | - public static function getPath($id) { |
|
| 904 | - return self::$defaultInstance->getPath($id); |
|
| 905 | - } |
|
| 906 | - |
|
| 907 | - /** |
|
| 908 | - * Get the owner for a file or folder |
|
| 909 | - * |
|
| 910 | - * @param string $path |
|
| 911 | - * @return string |
|
| 912 | - */ |
|
| 913 | - public static function getOwner($path) { |
|
| 914 | - return self::$defaultInstance->getOwner($path); |
|
| 915 | - } |
|
| 916 | - |
|
| 917 | - /** |
|
| 918 | - * get the ETag for a file or folder |
|
| 919 | - * |
|
| 920 | - * @param string $path |
|
| 921 | - * @return string |
|
| 922 | - */ |
|
| 923 | - static public function getETag($path) { |
|
| 924 | - return self::$defaultInstance->getETag($path); |
|
| 925 | - } |
|
| 73 | + /** |
|
| 74 | + * @var Mount\Manager $mounts |
|
| 75 | + */ |
|
| 76 | + private static $mounts; |
|
| 77 | + |
|
| 78 | + public static $loaded = false; |
|
| 79 | + /** |
|
| 80 | + * @var \OC\Files\View $defaultInstance |
|
| 81 | + */ |
|
| 82 | + static private $defaultInstance; |
|
| 83 | + |
|
| 84 | + static private $usersSetup = array(); |
|
| 85 | + |
|
| 86 | + static private $normalizedPathCache = null; |
|
| 87 | + |
|
| 88 | + static private $listeningForProviders = false; |
|
| 89 | + |
|
| 90 | + /** |
|
| 91 | + * classname which used for hooks handling |
|
| 92 | + * used as signalclass in OC_Hooks::emit() |
|
| 93 | + */ |
|
| 94 | + const CLASSNAME = 'OC_Filesystem'; |
|
| 95 | + |
|
| 96 | + /** |
|
| 97 | + * signalname emitted before file renaming |
|
| 98 | + * |
|
| 99 | + * @param string $oldpath |
|
| 100 | + * @param string $newpath |
|
| 101 | + */ |
|
| 102 | + const signal_rename = 'rename'; |
|
| 103 | + |
|
| 104 | + /** |
|
| 105 | + * signal emitted after file renaming |
|
| 106 | + * |
|
| 107 | + * @param string $oldpath |
|
| 108 | + * @param string $newpath |
|
| 109 | + */ |
|
| 110 | + const signal_post_rename = 'post_rename'; |
|
| 111 | + |
|
| 112 | + /** |
|
| 113 | + * signal emitted before file/dir creation |
|
| 114 | + * |
|
| 115 | + * @param string $path |
|
| 116 | + * @param bool $run changing this flag to false in hook handler will cancel event |
|
| 117 | + */ |
|
| 118 | + const signal_create = 'create'; |
|
| 119 | + |
|
| 120 | + /** |
|
| 121 | + * signal emitted after file/dir creation |
|
| 122 | + * |
|
| 123 | + * @param string $path |
|
| 124 | + * @param bool $run changing this flag to false in hook handler will cancel event |
|
| 125 | + */ |
|
| 126 | + const signal_post_create = 'post_create'; |
|
| 127 | + |
|
| 128 | + /** |
|
| 129 | + * signal emits before file/dir copy |
|
| 130 | + * |
|
| 131 | + * @param string $oldpath |
|
| 132 | + * @param string $newpath |
|
| 133 | + * @param bool $run changing this flag to false in hook handler will cancel event |
|
| 134 | + */ |
|
| 135 | + const signal_copy = 'copy'; |
|
| 136 | + |
|
| 137 | + /** |
|
| 138 | + * signal emits after file/dir copy |
|
| 139 | + * |
|
| 140 | + * @param string $oldpath |
|
| 141 | + * @param string $newpath |
|
| 142 | + */ |
|
| 143 | + const signal_post_copy = 'post_copy'; |
|
| 144 | + |
|
| 145 | + /** |
|
| 146 | + * signal emits before file/dir save |
|
| 147 | + * |
|
| 148 | + * @param string $path |
|
| 149 | + * @param bool $run changing this flag to false in hook handler will cancel event |
|
| 150 | + */ |
|
| 151 | + const signal_write = 'write'; |
|
| 152 | + |
|
| 153 | + /** |
|
| 154 | + * signal emits after file/dir save |
|
| 155 | + * |
|
| 156 | + * @param string $path |
|
| 157 | + */ |
|
| 158 | + const signal_post_write = 'post_write'; |
|
| 159 | + |
|
| 160 | + /** |
|
| 161 | + * signal emitted before file/dir update |
|
| 162 | + * |
|
| 163 | + * @param string $path |
|
| 164 | + * @param bool $run changing this flag to false in hook handler will cancel event |
|
| 165 | + */ |
|
| 166 | + const signal_update = 'update'; |
|
| 167 | + |
|
| 168 | + /** |
|
| 169 | + * signal emitted after file/dir update |
|
| 170 | + * |
|
| 171 | + * @param string $path |
|
| 172 | + * @param bool $run changing this flag to false in hook handler will cancel event |
|
| 173 | + */ |
|
| 174 | + const signal_post_update = 'post_update'; |
|
| 175 | + |
|
| 176 | + /** |
|
| 177 | + * signal emits when reading file/dir |
|
| 178 | + * |
|
| 179 | + * @param string $path |
|
| 180 | + */ |
|
| 181 | + const signal_read = 'read'; |
|
| 182 | + |
|
| 183 | + /** |
|
| 184 | + * signal emits when removing file/dir |
|
| 185 | + * |
|
| 186 | + * @param string $path |
|
| 187 | + */ |
|
| 188 | + const signal_delete = 'delete'; |
|
| 189 | + |
|
| 190 | + /** |
|
| 191 | + * parameters definitions for signals |
|
| 192 | + */ |
|
| 193 | + const signal_param_path = 'path'; |
|
| 194 | + const signal_param_oldpath = 'oldpath'; |
|
| 195 | + const signal_param_newpath = 'newpath'; |
|
| 196 | + |
|
| 197 | + /** |
|
| 198 | + * run - changing this flag to false in hook handler will cancel event |
|
| 199 | + */ |
|
| 200 | + const signal_param_run = 'run'; |
|
| 201 | + |
|
| 202 | + const signal_create_mount = 'create_mount'; |
|
| 203 | + const signal_delete_mount = 'delete_mount'; |
|
| 204 | + const signal_param_mount_type = 'mounttype'; |
|
| 205 | + const signal_param_users = 'users'; |
|
| 206 | + |
|
| 207 | + /** |
|
| 208 | + * @var \OC\Files\Storage\StorageFactory $loader |
|
| 209 | + */ |
|
| 210 | + private static $loader; |
|
| 211 | + |
|
| 212 | + /** @var bool */ |
|
| 213 | + private static $logWarningWhenAddingStorageWrapper = true; |
|
| 214 | + |
|
| 215 | + /** |
|
| 216 | + * @param bool $shouldLog |
|
| 217 | + * @return bool previous value |
|
| 218 | + * @internal |
|
| 219 | + */ |
|
| 220 | + public static function logWarningWhenAddingStorageWrapper($shouldLog) { |
|
| 221 | + $previousValue = self::$logWarningWhenAddingStorageWrapper; |
|
| 222 | + self::$logWarningWhenAddingStorageWrapper = (bool) $shouldLog; |
|
| 223 | + return $previousValue; |
|
| 224 | + } |
|
| 225 | + |
|
| 226 | + /** |
|
| 227 | + * @param string $wrapperName |
|
| 228 | + * @param callable $wrapper |
|
| 229 | + * @param int $priority |
|
| 230 | + */ |
|
| 231 | + public static function addStorageWrapper($wrapperName, $wrapper, $priority = 50) { |
|
| 232 | + if (self::$logWarningWhenAddingStorageWrapper) { |
|
| 233 | + \OC::$server->getLogger()->warning("Storage wrapper '{wrapper}' was not registered via the 'OC_Filesystem - preSetup' hook which could cause potential problems.", [ |
|
| 234 | + 'wrapper' => $wrapperName, |
|
| 235 | + 'app' => 'filesystem', |
|
| 236 | + ]); |
|
| 237 | + } |
|
| 238 | + |
|
| 239 | + $mounts = self::getMountManager()->getAll(); |
|
| 240 | + if (!self::getLoader()->addStorageWrapper($wrapperName, $wrapper, $priority, $mounts)) { |
|
| 241 | + // do not re-wrap if storage with this name already existed |
|
| 242 | + return; |
|
| 243 | + } |
|
| 244 | + } |
|
| 245 | + |
|
| 246 | + /** |
|
| 247 | + * Returns the storage factory |
|
| 248 | + * |
|
| 249 | + * @return \OCP\Files\Storage\IStorageFactory |
|
| 250 | + */ |
|
| 251 | + public static function getLoader() { |
|
| 252 | + if (!self::$loader) { |
|
| 253 | + self::$loader = new StorageFactory(); |
|
| 254 | + } |
|
| 255 | + return self::$loader; |
|
| 256 | + } |
|
| 257 | + |
|
| 258 | + /** |
|
| 259 | + * Returns the mount manager |
|
| 260 | + * |
|
| 261 | + * @return \OC\Files\Mount\Manager |
|
| 262 | + */ |
|
| 263 | + public static function getMountManager($user = '') { |
|
| 264 | + if (!self::$mounts) { |
|
| 265 | + \OC_Util::setupFS($user); |
|
| 266 | + } |
|
| 267 | + return self::$mounts; |
|
| 268 | + } |
|
| 269 | + |
|
| 270 | + /** |
|
| 271 | + * get the mountpoint of the storage object for a path |
|
| 272 | + * ( note: because a storage is not always mounted inside the fakeroot, the |
|
| 273 | + * returned mountpoint is relative to the absolute root of the filesystem |
|
| 274 | + * and doesn't take the chroot into account ) |
|
| 275 | + * |
|
| 276 | + * @param string $path |
|
| 277 | + * @return string |
|
| 278 | + */ |
|
| 279 | + static public function getMountPoint($path) { |
|
| 280 | + if (!self::$mounts) { |
|
| 281 | + \OC_Util::setupFS(); |
|
| 282 | + } |
|
| 283 | + $mount = self::$mounts->find($path); |
|
| 284 | + if ($mount) { |
|
| 285 | + return $mount->getMountPoint(); |
|
| 286 | + } else { |
|
| 287 | + return ''; |
|
| 288 | + } |
|
| 289 | + } |
|
| 290 | + |
|
| 291 | + /** |
|
| 292 | + * get a list of all mount points in a directory |
|
| 293 | + * |
|
| 294 | + * @param string $path |
|
| 295 | + * @return string[] |
|
| 296 | + */ |
|
| 297 | + static public function getMountPoints($path) { |
|
| 298 | + if (!self::$mounts) { |
|
| 299 | + \OC_Util::setupFS(); |
|
| 300 | + } |
|
| 301 | + $result = array(); |
|
| 302 | + $mounts = self::$mounts->findIn($path); |
|
| 303 | + foreach ($mounts as $mount) { |
|
| 304 | + $result[] = $mount->getMountPoint(); |
|
| 305 | + } |
|
| 306 | + return $result; |
|
| 307 | + } |
|
| 308 | + |
|
| 309 | + /** |
|
| 310 | + * get the storage mounted at $mountPoint |
|
| 311 | + * |
|
| 312 | + * @param string $mountPoint |
|
| 313 | + * @return \OC\Files\Storage\Storage |
|
| 314 | + */ |
|
| 315 | + public static function getStorage($mountPoint) { |
|
| 316 | + if (!self::$mounts) { |
|
| 317 | + \OC_Util::setupFS(); |
|
| 318 | + } |
|
| 319 | + $mount = self::$mounts->find($mountPoint); |
|
| 320 | + return $mount->getStorage(); |
|
| 321 | + } |
|
| 322 | + |
|
| 323 | + /** |
|
| 324 | + * @param string $id |
|
| 325 | + * @return Mount\MountPoint[] |
|
| 326 | + */ |
|
| 327 | + public static function getMountByStorageId($id) { |
|
| 328 | + if (!self::$mounts) { |
|
| 329 | + \OC_Util::setupFS(); |
|
| 330 | + } |
|
| 331 | + return self::$mounts->findByStorageId($id); |
|
| 332 | + } |
|
| 333 | + |
|
| 334 | + /** |
|
| 335 | + * @param int $id |
|
| 336 | + * @return Mount\MountPoint[] |
|
| 337 | + */ |
|
| 338 | + public static function getMountByNumericId($id) { |
|
| 339 | + if (!self::$mounts) { |
|
| 340 | + \OC_Util::setupFS(); |
|
| 341 | + } |
|
| 342 | + return self::$mounts->findByNumericId($id); |
|
| 343 | + } |
|
| 344 | + |
|
| 345 | + /** |
|
| 346 | + * resolve a path to a storage and internal path |
|
| 347 | + * |
|
| 348 | + * @param string $path |
|
| 349 | + * @return array an array consisting of the storage and the internal path |
|
| 350 | + */ |
|
| 351 | + static public function resolvePath($path) { |
|
| 352 | + if (!self::$mounts) { |
|
| 353 | + \OC_Util::setupFS(); |
|
| 354 | + } |
|
| 355 | + $mount = self::$mounts->find($path); |
|
| 356 | + if ($mount) { |
|
| 357 | + return array($mount->getStorage(), rtrim($mount->getInternalPath($path), '/')); |
|
| 358 | + } else { |
|
| 359 | + return array(null, null); |
|
| 360 | + } |
|
| 361 | + } |
|
| 362 | + |
|
| 363 | + static public function init($user, $root) { |
|
| 364 | + if (self::$defaultInstance) { |
|
| 365 | + return false; |
|
| 366 | + } |
|
| 367 | + self::getLoader(); |
|
| 368 | + self::$defaultInstance = new View($root); |
|
| 369 | + |
|
| 370 | + if (!self::$mounts) { |
|
| 371 | + self::$mounts = \OC::$server->getMountManager(); |
|
| 372 | + } |
|
| 373 | + |
|
| 374 | + //load custom mount config |
|
| 375 | + self::initMountPoints($user); |
|
| 376 | + |
|
| 377 | + self::$loaded = true; |
|
| 378 | + |
|
| 379 | + return true; |
|
| 380 | + } |
|
| 381 | + |
|
| 382 | + static public function initMountManager() { |
|
| 383 | + if (!self::$mounts) { |
|
| 384 | + self::$mounts = \OC::$server->getMountManager(); |
|
| 385 | + } |
|
| 386 | + } |
|
| 387 | + |
|
| 388 | + /** |
|
| 389 | + * Initialize system and personal mount points for a user |
|
| 390 | + * |
|
| 391 | + * @param string $user |
|
| 392 | + * @throws \OC\User\NoUserException if the user is not available |
|
| 393 | + */ |
|
| 394 | + public static function initMountPoints($user = '') { |
|
| 395 | + if ($user == '') { |
|
| 396 | + $user = \OC_User::getUser(); |
|
| 397 | + } |
|
| 398 | + if ($user === null || $user === false || $user === '') { |
|
| 399 | + throw new \OC\User\NoUserException('Attempted to initialize mount points for null user and no user in session'); |
|
| 400 | + } |
|
| 401 | + |
|
| 402 | + if (isset(self::$usersSetup[$user])) { |
|
| 403 | + return; |
|
| 404 | + } |
|
| 405 | + |
|
| 406 | + self::$usersSetup[$user] = true; |
|
| 407 | + |
|
| 408 | + $userManager = \OC::$server->getUserManager(); |
|
| 409 | + $userObject = $userManager->get($user); |
|
| 410 | + |
|
| 411 | + if (is_null($userObject)) { |
|
| 412 | + \OCP\Util::writeLog('files', ' Backends provided no user object for ' . $user, ILogger::ERROR); |
|
| 413 | + // reset flag, this will make it possible to rethrow the exception if called again |
|
| 414 | + unset(self::$usersSetup[$user]); |
|
| 415 | + throw new \OC\User\NoUserException('Backends provided no user object for ' . $user); |
|
| 416 | + } |
|
| 417 | + |
|
| 418 | + $realUid = $userObject->getUID(); |
|
| 419 | + // workaround in case of different casings |
|
| 420 | + if ($user !== $realUid) { |
|
| 421 | + $stack = json_encode(debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 50)); |
|
| 422 | + \OCP\Util::writeLog('files', 'initMountPoints() called with wrong user casing. This could be a bug. Expected: "' . $realUid . '" got "' . $user . '". Stack: ' . $stack, ILogger::WARN); |
|
| 423 | + $user = $realUid; |
|
| 424 | + |
|
| 425 | + // again with the correct casing |
|
| 426 | + if (isset(self::$usersSetup[$user])) { |
|
| 427 | + return; |
|
| 428 | + } |
|
| 429 | + |
|
| 430 | + self::$usersSetup[$user] = true; |
|
| 431 | + } |
|
| 432 | + |
|
| 433 | + if (\OC::$server->getLockdownManager()->canAccessFilesystem()) { |
|
| 434 | + /** @var \OC\Files\Config\MountProviderCollection $mountConfigManager */ |
|
| 435 | + $mountConfigManager = \OC::$server->getMountProviderCollection(); |
|
| 436 | + |
|
| 437 | + // home mounts are handled seperate since we need to ensure this is mounted before we call the other mount providers |
|
| 438 | + $homeMount = $mountConfigManager->getHomeMountForUser($userObject); |
|
| 439 | + |
|
| 440 | + self::getMountManager()->addMount($homeMount); |
|
| 441 | + |
|
| 442 | + \OC\Files\Filesystem::getStorage($user); |
|
| 443 | + |
|
| 444 | + // Chance to mount for other storages |
|
| 445 | + if ($userObject) { |
|
| 446 | + $mounts = $mountConfigManager->addMountForUser($userObject, self::getMountManager()); |
|
| 447 | + $mounts[] = $homeMount; |
|
| 448 | + $mountConfigManager->registerMounts($userObject, $mounts); |
|
| 449 | + } |
|
| 450 | + |
|
| 451 | + self::listenForNewMountProviders($mountConfigManager, $userManager); |
|
| 452 | + } else { |
|
| 453 | + self::getMountManager()->addMount(new MountPoint( |
|
| 454 | + new NullStorage([]), |
|
| 455 | + '/' . $user |
|
| 456 | + )); |
|
| 457 | + self::getMountManager()->addMount(new MountPoint( |
|
| 458 | + new NullStorage([]), |
|
| 459 | + '/' . $user . '/files' |
|
| 460 | + )); |
|
| 461 | + } |
|
| 462 | + \OC_Hook::emit('OC_Filesystem', 'post_initMountPoints', array('user' => $user)); |
|
| 463 | + } |
|
| 464 | + |
|
| 465 | + /** |
|
| 466 | + * Get mounts from mount providers that are registered after setup |
|
| 467 | + * |
|
| 468 | + * @param MountProviderCollection $mountConfigManager |
|
| 469 | + * @param IUserManager $userManager |
|
| 470 | + */ |
|
| 471 | + private static function listenForNewMountProviders(MountProviderCollection $mountConfigManager, IUserManager $userManager) { |
|
| 472 | + if (!self::$listeningForProviders) { |
|
| 473 | + self::$listeningForProviders = true; |
|
| 474 | + $mountConfigManager->listen('\OC\Files\Config', 'registerMountProvider', function (IMountProvider $provider) use ($userManager) { |
|
| 475 | + foreach (Filesystem::$usersSetup as $user => $setup) { |
|
| 476 | + $userObject = $userManager->get($user); |
|
| 477 | + if ($userObject) { |
|
| 478 | + $mounts = $provider->getMountsForUser($userObject, Filesystem::getLoader()); |
|
| 479 | + array_walk($mounts, array(self::$mounts, 'addMount')); |
|
| 480 | + } |
|
| 481 | + } |
|
| 482 | + }); |
|
| 483 | + } |
|
| 484 | + } |
|
| 485 | + |
|
| 486 | + /** |
|
| 487 | + * get the default filesystem view |
|
| 488 | + * |
|
| 489 | + * @return View |
|
| 490 | + */ |
|
| 491 | + static public function getView() { |
|
| 492 | + return self::$defaultInstance; |
|
| 493 | + } |
|
| 494 | + |
|
| 495 | + /** |
|
| 496 | + * tear down the filesystem, removing all storage providers |
|
| 497 | + */ |
|
| 498 | + static public function tearDown() { |
|
| 499 | + self::clearMounts(); |
|
| 500 | + self::$defaultInstance = null; |
|
| 501 | + } |
|
| 502 | + |
|
| 503 | + /** |
|
| 504 | + * get the relative path of the root data directory for the current user |
|
| 505 | + * |
|
| 506 | + * @return string |
|
| 507 | + * |
|
| 508 | + * Returns path like /admin/files |
|
| 509 | + */ |
|
| 510 | + static public function getRoot() { |
|
| 511 | + if (!self::$defaultInstance) { |
|
| 512 | + return null; |
|
| 513 | + } |
|
| 514 | + return self::$defaultInstance->getRoot(); |
|
| 515 | + } |
|
| 516 | + |
|
| 517 | + /** |
|
| 518 | + * clear all mounts and storage backends |
|
| 519 | + */ |
|
| 520 | + public static function clearMounts() { |
|
| 521 | + if (self::$mounts) { |
|
| 522 | + self::$usersSetup = array(); |
|
| 523 | + self::$mounts->clear(); |
|
| 524 | + } |
|
| 525 | + } |
|
| 526 | + |
|
| 527 | + /** |
|
| 528 | + * mount an \OC\Files\Storage\Storage in our virtual filesystem |
|
| 529 | + * |
|
| 530 | + * @param \OC\Files\Storage\Storage|string $class |
|
| 531 | + * @param array $arguments |
|
| 532 | + * @param string $mountpoint |
|
| 533 | + */ |
|
| 534 | + static public function mount($class, $arguments, $mountpoint) { |
|
| 535 | + if (!self::$mounts) { |
|
| 536 | + \OC_Util::setupFS(); |
|
| 537 | + } |
|
| 538 | + $mount = new Mount\MountPoint($class, $mountpoint, $arguments, self::getLoader()); |
|
| 539 | + self::$mounts->addMount($mount); |
|
| 540 | + } |
|
| 541 | + |
|
| 542 | + /** |
|
| 543 | + * return the path to a local version of the file |
|
| 544 | + * we need this because we can't know if a file is stored local or not from |
|
| 545 | + * outside the filestorage and for some purposes a local file is needed |
|
| 546 | + * |
|
| 547 | + * @param string $path |
|
| 548 | + * @return string |
|
| 549 | + */ |
|
| 550 | + static public function getLocalFile($path) { |
|
| 551 | + return self::$defaultInstance->getLocalFile($path); |
|
| 552 | + } |
|
| 553 | + |
|
| 554 | + /** |
|
| 555 | + * @param string $path |
|
| 556 | + * @return string |
|
| 557 | + */ |
|
| 558 | + static public function getLocalFolder($path) { |
|
| 559 | + return self::$defaultInstance->getLocalFolder($path); |
|
| 560 | + } |
|
| 561 | + |
|
| 562 | + /** |
|
| 563 | + * return path to file which reflects one visible in browser |
|
| 564 | + * |
|
| 565 | + * @param string $path |
|
| 566 | + * @return string |
|
| 567 | + */ |
|
| 568 | + static public function getLocalPath($path) { |
|
| 569 | + $datadir = \OC_User::getHome(\OC_User::getUser()) . '/files'; |
|
| 570 | + $newpath = $path; |
|
| 571 | + if (strncmp($newpath, $datadir, strlen($datadir)) == 0) { |
|
| 572 | + $newpath = substr($path, strlen($datadir)); |
|
| 573 | + } |
|
| 574 | + return $newpath; |
|
| 575 | + } |
|
| 576 | + |
|
| 577 | + /** |
|
| 578 | + * check if the requested path is valid |
|
| 579 | + * |
|
| 580 | + * @param string $path |
|
| 581 | + * @return bool |
|
| 582 | + */ |
|
| 583 | + static public function isValidPath($path) { |
|
| 584 | + $path = self::normalizePath($path); |
|
| 585 | + if (!$path || $path[0] !== '/') { |
|
| 586 | + $path = '/' . $path; |
|
| 587 | + } |
|
| 588 | + if (strpos($path, '/../') !== false || strrchr($path, '/') === '/..') { |
|
| 589 | + return false; |
|
| 590 | + } |
|
| 591 | + return true; |
|
| 592 | + } |
|
| 593 | + |
|
| 594 | + /** |
|
| 595 | + * checks if a file is blacklisted for storage in the filesystem |
|
| 596 | + * Listens to write and rename hooks |
|
| 597 | + * |
|
| 598 | + * @param array $data from hook |
|
| 599 | + */ |
|
| 600 | + static public function isBlacklisted($data) { |
|
| 601 | + if (isset($data['path'])) { |
|
| 602 | + $path = $data['path']; |
|
| 603 | + } else if (isset($data['newpath'])) { |
|
| 604 | + $path = $data['newpath']; |
|
| 605 | + } |
|
| 606 | + if (isset($path)) { |
|
| 607 | + if (self::isFileBlacklisted($path)) { |
|
| 608 | + $data['run'] = false; |
|
| 609 | + } |
|
| 610 | + } |
|
| 611 | + } |
|
| 612 | + |
|
| 613 | + /** |
|
| 614 | + * @param string $filename |
|
| 615 | + * @return bool |
|
| 616 | + */ |
|
| 617 | + static public function isFileBlacklisted($filename) { |
|
| 618 | + $filename = self::normalizePath($filename); |
|
| 619 | + |
|
| 620 | + $blacklist = \OC::$server->getConfig()->getSystemValue('blacklisted_files', array('.htaccess')); |
|
| 621 | + $filename = strtolower(basename($filename)); |
|
| 622 | + return in_array($filename, $blacklist); |
|
| 623 | + } |
|
| 624 | + |
|
| 625 | + /** |
|
| 626 | + * check if the directory should be ignored when scanning |
|
| 627 | + * NOTE: the special directories . and .. would cause never ending recursion |
|
| 628 | + * |
|
| 629 | + * @param String $dir |
|
| 630 | + * @return boolean |
|
| 631 | + */ |
|
| 632 | + static public function isIgnoredDir($dir) { |
|
| 633 | + if ($dir === '.' || $dir === '..') { |
|
| 634 | + return true; |
|
| 635 | + } |
|
| 636 | + return false; |
|
| 637 | + } |
|
| 638 | + |
|
| 639 | + /** |
|
| 640 | + * following functions are equivalent to their php builtin equivalents for arguments/return values. |
|
| 641 | + */ |
|
| 642 | + static public function mkdir($path) { |
|
| 643 | + return self::$defaultInstance->mkdir($path); |
|
| 644 | + } |
|
| 645 | + |
|
| 646 | + static public function rmdir($path) { |
|
| 647 | + return self::$defaultInstance->rmdir($path); |
|
| 648 | + } |
|
| 649 | + |
|
| 650 | + static public function is_dir($path) { |
|
| 651 | + return self::$defaultInstance->is_dir($path); |
|
| 652 | + } |
|
| 653 | + |
|
| 654 | + static public function is_file($path) { |
|
| 655 | + return self::$defaultInstance->is_file($path); |
|
| 656 | + } |
|
| 657 | + |
|
| 658 | + static public function stat($path) { |
|
| 659 | + return self::$defaultInstance->stat($path); |
|
| 660 | + } |
|
| 661 | + |
|
| 662 | + static public function filetype($path) { |
|
| 663 | + return self::$defaultInstance->filetype($path); |
|
| 664 | + } |
|
| 665 | + |
|
| 666 | + static public function filesize($path) { |
|
| 667 | + return self::$defaultInstance->filesize($path); |
|
| 668 | + } |
|
| 669 | + |
|
| 670 | + static public function readfile($path) { |
|
| 671 | + return self::$defaultInstance->readfile($path); |
|
| 672 | + } |
|
| 673 | + |
|
| 674 | + static public function isCreatable($path) { |
|
| 675 | + return self::$defaultInstance->isCreatable($path); |
|
| 676 | + } |
|
| 677 | + |
|
| 678 | + static public function isReadable($path) { |
|
| 679 | + return self::$defaultInstance->isReadable($path); |
|
| 680 | + } |
|
| 681 | + |
|
| 682 | + static public function isUpdatable($path) { |
|
| 683 | + return self::$defaultInstance->isUpdatable($path); |
|
| 684 | + } |
|
| 685 | + |
|
| 686 | + static public function isDeletable($path) { |
|
| 687 | + return self::$defaultInstance->isDeletable($path); |
|
| 688 | + } |
|
| 689 | + |
|
| 690 | + static public function isSharable($path) { |
|
| 691 | + return self::$defaultInstance->isSharable($path); |
|
| 692 | + } |
|
| 693 | + |
|
| 694 | + static public function file_exists($path) { |
|
| 695 | + return self::$defaultInstance->file_exists($path); |
|
| 696 | + } |
|
| 697 | + |
|
| 698 | + static public function filemtime($path) { |
|
| 699 | + return self::$defaultInstance->filemtime($path); |
|
| 700 | + } |
|
| 701 | + |
|
| 702 | + static public function touch($path, $mtime = null) { |
|
| 703 | + return self::$defaultInstance->touch($path, $mtime); |
|
| 704 | + } |
|
| 705 | + |
|
| 706 | + /** |
|
| 707 | + * @return string |
|
| 708 | + */ |
|
| 709 | + static public function file_get_contents($path) { |
|
| 710 | + return self::$defaultInstance->file_get_contents($path); |
|
| 711 | + } |
|
| 712 | + |
|
| 713 | + static public function file_put_contents($path, $data) { |
|
| 714 | + return self::$defaultInstance->file_put_contents($path, $data); |
|
| 715 | + } |
|
| 716 | + |
|
| 717 | + static public function unlink($path) { |
|
| 718 | + return self::$defaultInstance->unlink($path); |
|
| 719 | + } |
|
| 720 | + |
|
| 721 | + static public function rename($path1, $path2) { |
|
| 722 | + return self::$defaultInstance->rename($path1, $path2); |
|
| 723 | + } |
|
| 724 | + |
|
| 725 | + static public function copy($path1, $path2) { |
|
| 726 | + return self::$defaultInstance->copy($path1, $path2); |
|
| 727 | + } |
|
| 728 | + |
|
| 729 | + static public function fopen($path, $mode) { |
|
| 730 | + return self::$defaultInstance->fopen($path, $mode); |
|
| 731 | + } |
|
| 732 | + |
|
| 733 | + /** |
|
| 734 | + * @return string |
|
| 735 | + */ |
|
| 736 | + static public function toTmpFile($path) { |
|
| 737 | + return self::$defaultInstance->toTmpFile($path); |
|
| 738 | + } |
|
| 739 | + |
|
| 740 | + static public function fromTmpFile($tmpFile, $path) { |
|
| 741 | + return self::$defaultInstance->fromTmpFile($tmpFile, $path); |
|
| 742 | + } |
|
| 743 | + |
|
| 744 | + static public function getMimeType($path) { |
|
| 745 | + return self::$defaultInstance->getMimeType($path); |
|
| 746 | + } |
|
| 747 | + |
|
| 748 | + static public function hash($type, $path, $raw = false) { |
|
| 749 | + return self::$defaultInstance->hash($type, $path, $raw); |
|
| 750 | + } |
|
| 751 | + |
|
| 752 | + static public function free_space($path = '/') { |
|
| 753 | + return self::$defaultInstance->free_space($path); |
|
| 754 | + } |
|
| 755 | + |
|
| 756 | + static public function search($query) { |
|
| 757 | + return self::$defaultInstance->search($query); |
|
| 758 | + } |
|
| 759 | + |
|
| 760 | + /** |
|
| 761 | + * @param string $query |
|
| 762 | + */ |
|
| 763 | + static public function searchByMime($query) { |
|
| 764 | + return self::$defaultInstance->searchByMime($query); |
|
| 765 | + } |
|
| 766 | + |
|
| 767 | + /** |
|
| 768 | + * @param string|int $tag name or tag id |
|
| 769 | + * @param string $userId owner of the tags |
|
| 770 | + * @return FileInfo[] array or file info |
|
| 771 | + */ |
|
| 772 | + static public function searchByTag($tag, $userId) { |
|
| 773 | + return self::$defaultInstance->searchByTag($tag, $userId); |
|
| 774 | + } |
|
| 775 | + |
|
| 776 | + /** |
|
| 777 | + * check if a file or folder has been updated since $time |
|
| 778 | + * |
|
| 779 | + * @param string $path |
|
| 780 | + * @param int $time |
|
| 781 | + * @return bool |
|
| 782 | + */ |
|
| 783 | + static public function hasUpdated($path, $time) { |
|
| 784 | + return self::$defaultInstance->hasUpdated($path, $time); |
|
| 785 | + } |
|
| 786 | + |
|
| 787 | + /** |
|
| 788 | + * Fix common problems with a file path |
|
| 789 | + * |
|
| 790 | + * @param string $path |
|
| 791 | + * @param bool $stripTrailingSlash whether to strip the trailing slash |
|
| 792 | + * @param bool $isAbsolutePath whether the given path is absolute |
|
| 793 | + * @param bool $keepUnicode true to disable unicode normalization |
|
| 794 | + * @return string |
|
| 795 | + */ |
|
| 796 | + public static function normalizePath($path, $stripTrailingSlash = true, $isAbsolutePath = false, $keepUnicode = false) { |
|
| 797 | + if (is_null(self::$normalizedPathCache)) { |
|
| 798 | + self::$normalizedPathCache = new CappedMemoryCache(2048); |
|
| 799 | + } |
|
| 800 | + |
|
| 801 | + /** |
|
| 802 | + * FIXME: This is a workaround for existing classes and files which call |
|
| 803 | + * this function with another type than a valid string. This |
|
| 804 | + * conversion should get removed as soon as all existing |
|
| 805 | + * function calls have been fixed. |
|
| 806 | + */ |
|
| 807 | + $path = (string)$path; |
|
| 808 | + |
|
| 809 | + $cacheKey = json_encode([$path, $stripTrailingSlash, $isAbsolutePath, $keepUnicode]); |
|
| 810 | + |
|
| 811 | + if (isset(self::$normalizedPathCache[$cacheKey])) { |
|
| 812 | + return self::$normalizedPathCache[$cacheKey]; |
|
| 813 | + } |
|
| 814 | + |
|
| 815 | + if ($path == '') { |
|
| 816 | + return '/'; |
|
| 817 | + } |
|
| 818 | + |
|
| 819 | + //normalize unicode if possible |
|
| 820 | + if (!$keepUnicode) { |
|
| 821 | + $path = \OC_Util::normalizeUnicode($path); |
|
| 822 | + } |
|
| 823 | + |
|
| 824 | + //no windows style slashes |
|
| 825 | + $path = str_replace('\\', '/', $path); |
|
| 826 | + |
|
| 827 | + //add leading slash |
|
| 828 | + if ($path[0] !== '/') { |
|
| 829 | + $path = '/' . $path; |
|
| 830 | + } |
|
| 831 | + |
|
| 832 | + // remove '/./' |
|
| 833 | + // ugly, but str_replace() can't replace them all in one go |
|
| 834 | + // as the replacement itself is part of the search string |
|
| 835 | + // which will only be found during the next iteration |
|
| 836 | + while (strpos($path, '/./') !== false) { |
|
| 837 | + $path = str_replace('/./', '/', $path); |
|
| 838 | + } |
|
| 839 | + // remove sequences of slashes |
|
| 840 | + $path = preg_replace('#/{2,}#', '/', $path); |
|
| 841 | + |
|
| 842 | + //remove trailing slash |
|
| 843 | + if ($stripTrailingSlash and strlen($path) > 1) { |
|
| 844 | + $path = rtrim($path, '/'); |
|
| 845 | + } |
|
| 846 | + |
|
| 847 | + // remove trailing '/.' |
|
| 848 | + if (substr($path, -2) == '/.') { |
|
| 849 | + $path = substr($path, 0, -2); |
|
| 850 | + } |
|
| 851 | + |
|
| 852 | + $normalizedPath = $path; |
|
| 853 | + self::$normalizedPathCache[$cacheKey] = $normalizedPath; |
|
| 854 | + |
|
| 855 | + return $normalizedPath; |
|
| 856 | + } |
|
| 857 | + |
|
| 858 | + /** |
|
| 859 | + * get the filesystem info |
|
| 860 | + * |
|
| 861 | + * @param string $path |
|
| 862 | + * @param boolean $includeMountPoints whether to add mountpoint sizes, |
|
| 863 | + * defaults to true |
|
| 864 | + * @return \OC\Files\FileInfo|bool False if file does not exist |
|
| 865 | + */ |
|
| 866 | + public static function getFileInfo($path, $includeMountPoints = true) { |
|
| 867 | + return self::$defaultInstance->getFileInfo($path, $includeMountPoints); |
|
| 868 | + } |
|
| 869 | + |
|
| 870 | + /** |
|
| 871 | + * change file metadata |
|
| 872 | + * |
|
| 873 | + * @param string $path |
|
| 874 | + * @param array $data |
|
| 875 | + * @return int |
|
| 876 | + * |
|
| 877 | + * returns the fileid of the updated file |
|
| 878 | + */ |
|
| 879 | + public static function putFileInfo($path, $data) { |
|
| 880 | + return self::$defaultInstance->putFileInfo($path, $data); |
|
| 881 | + } |
|
| 882 | + |
|
| 883 | + /** |
|
| 884 | + * get the content of a directory |
|
| 885 | + * |
|
| 886 | + * @param string $directory path under datadirectory |
|
| 887 | + * @param string $mimetype_filter limit returned content to this mimetype or mimepart |
|
| 888 | + * @return \OC\Files\FileInfo[] |
|
| 889 | + */ |
|
| 890 | + public static function getDirectoryContent($directory, $mimetype_filter = '') { |
|
| 891 | + return self::$defaultInstance->getDirectoryContent($directory, $mimetype_filter); |
|
| 892 | + } |
|
| 893 | + |
|
| 894 | + /** |
|
| 895 | + * Get the path of a file by id |
|
| 896 | + * |
|
| 897 | + * Note that the resulting path is not guaranteed to be unique for the id, multiple paths can point to the same file |
|
| 898 | + * |
|
| 899 | + * @param int $id |
|
| 900 | + * @throws NotFoundException |
|
| 901 | + * @return string |
|
| 902 | + */ |
|
| 903 | + public static function getPath($id) { |
|
| 904 | + return self::$defaultInstance->getPath($id); |
|
| 905 | + } |
|
| 906 | + |
|
| 907 | + /** |
|
| 908 | + * Get the owner for a file or folder |
|
| 909 | + * |
|
| 910 | + * @param string $path |
|
| 911 | + * @return string |
|
| 912 | + */ |
|
| 913 | + public static function getOwner($path) { |
|
| 914 | + return self::$defaultInstance->getOwner($path); |
|
| 915 | + } |
|
| 916 | + |
|
| 917 | + /** |
|
| 918 | + * get the ETag for a file or folder |
|
| 919 | + * |
|
| 920 | + * @param string $path |
|
| 921 | + * @return string |
|
| 922 | + */ |
|
| 923 | + static public function getETag($path) { |
|
| 924 | + return self::$defaultInstance->getETag($path); |
|
| 925 | + } |
|
| 926 | 926 | } |
@@ -409,17 +409,17 @@ discard block |
||
| 409 | 409 | $userObject = $userManager->get($user); |
| 410 | 410 | |
| 411 | 411 | if (is_null($userObject)) { |
| 412 | - \OCP\Util::writeLog('files', ' Backends provided no user object for ' . $user, ILogger::ERROR); |
|
| 412 | + \OCP\Util::writeLog('files', ' Backends provided no user object for '.$user, ILogger::ERROR); |
|
| 413 | 413 | // reset flag, this will make it possible to rethrow the exception if called again |
| 414 | 414 | unset(self::$usersSetup[$user]); |
| 415 | - throw new \OC\User\NoUserException('Backends provided no user object for ' . $user); |
|
| 415 | + throw new \OC\User\NoUserException('Backends provided no user object for '.$user); |
|
| 416 | 416 | } |
| 417 | 417 | |
| 418 | 418 | $realUid = $userObject->getUID(); |
| 419 | 419 | // workaround in case of different casings |
| 420 | 420 | if ($user !== $realUid) { |
| 421 | 421 | $stack = json_encode(debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 50)); |
| 422 | - \OCP\Util::writeLog('files', 'initMountPoints() called with wrong user casing. This could be a bug. Expected: "' . $realUid . '" got "' . $user . '". Stack: ' . $stack, ILogger::WARN); |
|
| 422 | + \OCP\Util::writeLog('files', 'initMountPoints() called with wrong user casing. This could be a bug. Expected: "'.$realUid.'" got "'.$user.'". Stack: '.$stack, ILogger::WARN); |
|
| 423 | 423 | $user = $realUid; |
| 424 | 424 | |
| 425 | 425 | // again with the correct casing |
@@ -452,11 +452,11 @@ discard block |
||
| 452 | 452 | } else { |
| 453 | 453 | self::getMountManager()->addMount(new MountPoint( |
| 454 | 454 | new NullStorage([]), |
| 455 | - '/' . $user |
|
| 455 | + '/'.$user |
|
| 456 | 456 | )); |
| 457 | 457 | self::getMountManager()->addMount(new MountPoint( |
| 458 | 458 | new NullStorage([]), |
| 459 | - '/' . $user . '/files' |
|
| 459 | + '/'.$user.'/files' |
|
| 460 | 460 | )); |
| 461 | 461 | } |
| 462 | 462 | \OC_Hook::emit('OC_Filesystem', 'post_initMountPoints', array('user' => $user)); |
@@ -471,7 +471,7 @@ discard block |
||
| 471 | 471 | private static function listenForNewMountProviders(MountProviderCollection $mountConfigManager, IUserManager $userManager) { |
| 472 | 472 | if (!self::$listeningForProviders) { |
| 473 | 473 | self::$listeningForProviders = true; |
| 474 | - $mountConfigManager->listen('\OC\Files\Config', 'registerMountProvider', function (IMountProvider $provider) use ($userManager) { |
|
| 474 | + $mountConfigManager->listen('\OC\Files\Config', 'registerMountProvider', function(IMountProvider $provider) use ($userManager) { |
|
| 475 | 475 | foreach (Filesystem::$usersSetup as $user => $setup) { |
| 476 | 476 | $userObject = $userManager->get($user); |
| 477 | 477 | if ($userObject) { |
@@ -566,7 +566,7 @@ discard block |
||
| 566 | 566 | * @return string |
| 567 | 567 | */ |
| 568 | 568 | static public function getLocalPath($path) { |
| 569 | - $datadir = \OC_User::getHome(\OC_User::getUser()) . '/files'; |
|
| 569 | + $datadir = \OC_User::getHome(\OC_User::getUser()).'/files'; |
|
| 570 | 570 | $newpath = $path; |
| 571 | 571 | if (strncmp($newpath, $datadir, strlen($datadir)) == 0) { |
| 572 | 572 | $newpath = substr($path, strlen($datadir)); |
@@ -583,7 +583,7 @@ discard block |
||
| 583 | 583 | static public function isValidPath($path) { |
| 584 | 584 | $path = self::normalizePath($path); |
| 585 | 585 | if (!$path || $path[0] !== '/') { |
| 586 | - $path = '/' . $path; |
|
| 586 | + $path = '/'.$path; |
|
| 587 | 587 | } |
| 588 | 588 | if (strpos($path, '/../') !== false || strrchr($path, '/') === '/..') { |
| 589 | 589 | return false; |
@@ -804,7 +804,7 @@ discard block |
||
| 804 | 804 | * conversion should get removed as soon as all existing |
| 805 | 805 | * function calls have been fixed. |
| 806 | 806 | */ |
| 807 | - $path = (string)$path; |
|
| 807 | + $path = (string) $path; |
|
| 808 | 808 | |
| 809 | 809 | $cacheKey = json_encode([$path, $stripTrailingSlash, $isAbsolutePath, $keepUnicode]); |
| 810 | 810 | |
@@ -826,7 +826,7 @@ discard block |
||
| 826 | 826 | |
| 827 | 827 | //add leading slash |
| 828 | 828 | if ($path[0] !== '/') { |
| 829 | - $path = '/' . $path; |
|
| 829 | + $path = '/'.$path; |
|
| 830 | 830 | } |
| 831 | 831 | |
| 832 | 832 | // remove '/./' |
@@ -984,7 +984,7 @@ discard block |
||
| 984 | 984 | /** |
| 985 | 985 | * check if path points to a files version |
| 986 | 986 | * |
| 987 | - * @param $path |
|
| 987 | + * @param string $path |
|
| 988 | 988 | * @return bool |
| 989 | 989 | */ |
| 990 | 990 | protected function isVersion($path) { |
@@ -995,7 +995,7 @@ discard block |
||
| 995 | 995 | /** |
| 996 | 996 | * check if the given storage should be encrypted or not |
| 997 | 997 | * |
| 998 | - * @param $path |
|
| 998 | + * @param string $path |
|
| 999 | 999 | * @return bool |
| 1000 | 1000 | */ |
| 1001 | 1001 | protected function shouldEncrypt($path) { |
@@ -48,981 +48,981 @@ |
||
| 48 | 48 | |
| 49 | 49 | class Encryption extends Wrapper { |
| 50 | 50 | |
| 51 | - use LocalTempFileTrait; |
|
| 52 | - |
|
| 53 | - /** @var string */ |
|
| 54 | - private $mountPoint; |
|
| 55 | - |
|
| 56 | - /** @var \OC\Encryption\Util */ |
|
| 57 | - private $util; |
|
| 58 | - |
|
| 59 | - /** @var \OCP\Encryption\IManager */ |
|
| 60 | - private $encryptionManager; |
|
| 61 | - |
|
| 62 | - /** @var \OCP\ILogger */ |
|
| 63 | - private $logger; |
|
| 64 | - |
|
| 65 | - /** @var string */ |
|
| 66 | - private $uid; |
|
| 67 | - |
|
| 68 | - /** @var array */ |
|
| 69 | - protected $unencryptedSize; |
|
| 70 | - |
|
| 71 | - /** @var \OCP\Encryption\IFile */ |
|
| 72 | - private $fileHelper; |
|
| 73 | - |
|
| 74 | - /** @var IMountPoint */ |
|
| 75 | - private $mount; |
|
| 76 | - |
|
| 77 | - /** @var IStorage */ |
|
| 78 | - private $keyStorage; |
|
| 79 | - |
|
| 80 | - /** @var Update */ |
|
| 81 | - private $update; |
|
| 82 | - |
|
| 83 | - /** @var Manager */ |
|
| 84 | - private $mountManager; |
|
| 85 | - |
|
| 86 | - /** @var array remember for which path we execute the repair step to avoid recursions */ |
|
| 87 | - private $fixUnencryptedSizeOf = array(); |
|
| 88 | - |
|
| 89 | - /** @var ArrayCache */ |
|
| 90 | - private $arrayCache; |
|
| 91 | - |
|
| 92 | - /** |
|
| 93 | - * @param array $parameters |
|
| 94 | - * @param IManager $encryptionManager |
|
| 95 | - * @param Util $util |
|
| 96 | - * @param ILogger $logger |
|
| 97 | - * @param IFile $fileHelper |
|
| 98 | - * @param string $uid |
|
| 99 | - * @param IStorage $keyStorage |
|
| 100 | - * @param Update $update |
|
| 101 | - * @param Manager $mountManager |
|
| 102 | - * @param ArrayCache $arrayCache |
|
| 103 | - */ |
|
| 104 | - public function __construct( |
|
| 105 | - $parameters, |
|
| 106 | - IManager $encryptionManager = null, |
|
| 107 | - Util $util = null, |
|
| 108 | - ILogger $logger = null, |
|
| 109 | - IFile $fileHelper = null, |
|
| 110 | - $uid = null, |
|
| 111 | - IStorage $keyStorage = null, |
|
| 112 | - Update $update = null, |
|
| 113 | - Manager $mountManager = null, |
|
| 114 | - ArrayCache $arrayCache = null |
|
| 115 | - ) { |
|
| 116 | - |
|
| 117 | - $this->mountPoint = $parameters['mountPoint']; |
|
| 118 | - $this->mount = $parameters['mount']; |
|
| 119 | - $this->encryptionManager = $encryptionManager; |
|
| 120 | - $this->util = $util; |
|
| 121 | - $this->logger = $logger; |
|
| 122 | - $this->uid = $uid; |
|
| 123 | - $this->fileHelper = $fileHelper; |
|
| 124 | - $this->keyStorage = $keyStorage; |
|
| 125 | - $this->unencryptedSize = array(); |
|
| 126 | - $this->update = $update; |
|
| 127 | - $this->mountManager = $mountManager; |
|
| 128 | - $this->arrayCache = $arrayCache; |
|
| 129 | - parent::__construct($parameters); |
|
| 130 | - } |
|
| 131 | - |
|
| 132 | - /** |
|
| 133 | - * see http://php.net/manual/en/function.filesize.php |
|
| 134 | - * The result for filesize when called on a folder is required to be 0 |
|
| 135 | - * |
|
| 136 | - * @param string $path |
|
| 137 | - * @return int |
|
| 138 | - */ |
|
| 139 | - public function filesize($path) { |
|
| 140 | - $fullPath = $this->getFullPath($path); |
|
| 141 | - |
|
| 142 | - /** @var CacheEntry $info */ |
|
| 143 | - $info = $this->getCache()->get($path); |
|
| 144 | - if (isset($this->unencryptedSize[$fullPath])) { |
|
| 145 | - $size = $this->unencryptedSize[$fullPath]; |
|
| 146 | - // update file cache |
|
| 147 | - if ($info instanceof ICacheEntry) { |
|
| 148 | - $info = $info->getData(); |
|
| 149 | - $info['encrypted'] = $info['encryptedVersion']; |
|
| 150 | - } else { |
|
| 151 | - if (!is_array($info)) { |
|
| 152 | - $info = []; |
|
| 153 | - } |
|
| 154 | - $info['encrypted'] = true; |
|
| 155 | - } |
|
| 156 | - |
|
| 157 | - $info['size'] = $size; |
|
| 158 | - $this->getCache()->put($path, $info); |
|
| 159 | - |
|
| 160 | - return $size; |
|
| 161 | - } |
|
| 162 | - |
|
| 163 | - if (isset($info['fileid']) && $info['encrypted']) { |
|
| 164 | - return $this->verifyUnencryptedSize($path, $info['size']); |
|
| 165 | - } |
|
| 166 | - |
|
| 167 | - return $this->storage->filesize($path); |
|
| 168 | - } |
|
| 169 | - |
|
| 170 | - /** |
|
| 171 | - * @param string $path |
|
| 172 | - * @return array |
|
| 173 | - */ |
|
| 174 | - public function getMetaData($path) { |
|
| 175 | - $data = $this->storage->getMetaData($path); |
|
| 176 | - if (is_null($data)) { |
|
| 177 | - return null; |
|
| 178 | - } |
|
| 179 | - $fullPath = $this->getFullPath($path); |
|
| 180 | - $info = $this->getCache()->get($path); |
|
| 181 | - |
|
| 182 | - if (isset($this->unencryptedSize[$fullPath])) { |
|
| 183 | - $data['encrypted'] = true; |
|
| 184 | - $data['size'] = $this->unencryptedSize[$fullPath]; |
|
| 185 | - } else { |
|
| 186 | - if (isset($info['fileid']) && $info['encrypted']) { |
|
| 187 | - $data['size'] = $this->verifyUnencryptedSize($path, $info['size']); |
|
| 188 | - $data['encrypted'] = true; |
|
| 189 | - } |
|
| 190 | - } |
|
| 191 | - |
|
| 192 | - if (isset($info['encryptedVersion']) && $info['encryptedVersion'] > 1) { |
|
| 193 | - $data['encryptedVersion'] = $info['encryptedVersion']; |
|
| 194 | - } |
|
| 195 | - |
|
| 196 | - return $data; |
|
| 197 | - } |
|
| 198 | - |
|
| 199 | - /** |
|
| 200 | - * see http://php.net/manual/en/function.file_get_contents.php |
|
| 201 | - * |
|
| 202 | - * @param string $path |
|
| 203 | - * @return string |
|
| 204 | - */ |
|
| 205 | - public function file_get_contents($path) { |
|
| 206 | - |
|
| 207 | - $encryptionModule = $this->getEncryptionModule($path); |
|
| 208 | - |
|
| 209 | - if ($encryptionModule) { |
|
| 210 | - $handle = $this->fopen($path, "r"); |
|
| 211 | - if (!$handle) { |
|
| 212 | - return false; |
|
| 213 | - } |
|
| 214 | - $data = stream_get_contents($handle); |
|
| 215 | - fclose($handle); |
|
| 216 | - return $data; |
|
| 217 | - } |
|
| 218 | - return $this->storage->file_get_contents($path); |
|
| 219 | - } |
|
| 220 | - |
|
| 221 | - /** |
|
| 222 | - * see http://php.net/manual/en/function.file_put_contents.php |
|
| 223 | - * |
|
| 224 | - * @param string $path |
|
| 225 | - * @param string $data |
|
| 226 | - * @return bool |
|
| 227 | - */ |
|
| 228 | - public function file_put_contents($path, $data) { |
|
| 229 | - // file put content will always be translated to a stream write |
|
| 230 | - $handle = $this->fopen($path, 'w'); |
|
| 231 | - if (is_resource($handle)) { |
|
| 232 | - $written = fwrite($handle, $data); |
|
| 233 | - fclose($handle); |
|
| 234 | - return $written; |
|
| 235 | - } |
|
| 236 | - |
|
| 237 | - return false; |
|
| 238 | - } |
|
| 239 | - |
|
| 240 | - /** |
|
| 241 | - * see http://php.net/manual/en/function.unlink.php |
|
| 242 | - * |
|
| 243 | - * @param string $path |
|
| 244 | - * @return bool |
|
| 245 | - */ |
|
| 246 | - public function unlink($path) { |
|
| 247 | - $fullPath = $this->getFullPath($path); |
|
| 248 | - if ($this->util->isExcluded($fullPath)) { |
|
| 249 | - return $this->storage->unlink($path); |
|
| 250 | - } |
|
| 251 | - |
|
| 252 | - $encryptionModule = $this->getEncryptionModule($path); |
|
| 253 | - if ($encryptionModule) { |
|
| 254 | - $this->keyStorage->deleteAllFileKeys($this->getFullPath($path)); |
|
| 255 | - } |
|
| 256 | - |
|
| 257 | - return $this->storage->unlink($path); |
|
| 258 | - } |
|
| 259 | - |
|
| 260 | - /** |
|
| 261 | - * see http://php.net/manual/en/function.rename.php |
|
| 262 | - * |
|
| 263 | - * @param string $path1 |
|
| 264 | - * @param string $path2 |
|
| 265 | - * @return bool |
|
| 266 | - */ |
|
| 267 | - public function rename($path1, $path2) { |
|
| 268 | - |
|
| 269 | - $result = $this->storage->rename($path1, $path2); |
|
| 270 | - |
|
| 271 | - if ($result && |
|
| 272 | - // versions always use the keys from the original file, so we can skip |
|
| 273 | - // this step for versions |
|
| 274 | - $this->isVersion($path2) === false && |
|
| 275 | - $this->encryptionManager->isEnabled()) { |
|
| 276 | - $source = $this->getFullPath($path1); |
|
| 277 | - if (!$this->util->isExcluded($source)) { |
|
| 278 | - $target = $this->getFullPath($path2); |
|
| 279 | - if (isset($this->unencryptedSize[$source])) { |
|
| 280 | - $this->unencryptedSize[$target] = $this->unencryptedSize[$source]; |
|
| 281 | - } |
|
| 282 | - $this->keyStorage->renameKeys($source, $target); |
|
| 283 | - $module = $this->getEncryptionModule($path2); |
|
| 284 | - if ($module) { |
|
| 285 | - $module->update($target, $this->uid, []); |
|
| 286 | - } |
|
| 287 | - } |
|
| 288 | - } |
|
| 289 | - |
|
| 290 | - return $result; |
|
| 291 | - } |
|
| 292 | - |
|
| 293 | - /** |
|
| 294 | - * see http://php.net/manual/en/function.rmdir.php |
|
| 295 | - * |
|
| 296 | - * @param string $path |
|
| 297 | - * @return bool |
|
| 298 | - */ |
|
| 299 | - public function rmdir($path) { |
|
| 300 | - $result = $this->storage->rmdir($path); |
|
| 301 | - $fullPath = $this->getFullPath($path); |
|
| 302 | - if ($result && |
|
| 303 | - $this->util->isExcluded($fullPath) === false && |
|
| 304 | - $this->encryptionManager->isEnabled() |
|
| 305 | - ) { |
|
| 306 | - $this->keyStorage->deleteAllFileKeys($fullPath); |
|
| 307 | - } |
|
| 308 | - |
|
| 309 | - return $result; |
|
| 310 | - } |
|
| 311 | - |
|
| 312 | - /** |
|
| 313 | - * check if a file can be read |
|
| 314 | - * |
|
| 315 | - * @param string $path |
|
| 316 | - * @return bool |
|
| 317 | - */ |
|
| 318 | - public function isReadable($path) { |
|
| 319 | - |
|
| 320 | - $isReadable = true; |
|
| 321 | - |
|
| 322 | - $metaData = $this->getMetaData($path); |
|
| 323 | - if ( |
|
| 324 | - !$this->is_dir($path) && |
|
| 325 | - isset($metaData['encrypted']) && |
|
| 326 | - $metaData['encrypted'] === true |
|
| 327 | - ) { |
|
| 328 | - $fullPath = $this->getFullPath($path); |
|
| 329 | - $module = $this->getEncryptionModule($path); |
|
| 330 | - $isReadable = $module->isReadable($fullPath, $this->uid); |
|
| 331 | - } |
|
| 332 | - |
|
| 333 | - return $this->storage->isReadable($path) && $isReadable; |
|
| 334 | - } |
|
| 335 | - |
|
| 336 | - /** |
|
| 337 | - * see http://php.net/manual/en/function.copy.php |
|
| 338 | - * |
|
| 339 | - * @param string $path1 |
|
| 340 | - * @param string $path2 |
|
| 341 | - * @return bool |
|
| 342 | - */ |
|
| 343 | - public function copy($path1, $path2) { |
|
| 344 | - |
|
| 345 | - $source = $this->getFullPath($path1); |
|
| 346 | - |
|
| 347 | - if ($this->util->isExcluded($source)) { |
|
| 348 | - return $this->storage->copy($path1, $path2); |
|
| 349 | - } |
|
| 350 | - |
|
| 351 | - // need to stream copy file by file in case we copy between a encrypted |
|
| 352 | - // and a unencrypted storage |
|
| 353 | - $this->unlink($path2); |
|
| 354 | - return $this->copyFromStorage($this, $path1, $path2); |
|
| 355 | - } |
|
| 356 | - |
|
| 357 | - /** |
|
| 358 | - * see http://php.net/manual/en/function.fopen.php |
|
| 359 | - * |
|
| 360 | - * @param string $path |
|
| 361 | - * @param string $mode |
|
| 362 | - * @return resource|bool |
|
| 363 | - * @throws GenericEncryptionException |
|
| 364 | - * @throws ModuleDoesNotExistsException |
|
| 365 | - */ |
|
| 366 | - public function fopen($path, $mode) { |
|
| 367 | - |
|
| 368 | - // check if the file is stored in the array cache, this means that we |
|
| 369 | - // copy a file over to the versions folder, in this case we don't want to |
|
| 370 | - // decrypt it |
|
| 371 | - if ($this->arrayCache->hasKey('encryption_copy_version_' . $path)) { |
|
| 372 | - $this->arrayCache->remove('encryption_copy_version_' . $path); |
|
| 373 | - return $this->storage->fopen($path, $mode); |
|
| 374 | - } |
|
| 375 | - |
|
| 376 | - $encryptionEnabled = $this->encryptionManager->isEnabled(); |
|
| 377 | - $shouldEncrypt = false; |
|
| 378 | - $encryptionModule = null; |
|
| 379 | - $header = $this->getHeader($path); |
|
| 380 | - $signed = isset($header['signed']) && $header['signed'] === 'true'; |
|
| 381 | - $fullPath = $this->getFullPath($path); |
|
| 382 | - $encryptionModuleId = $this->util->getEncryptionModuleId($header); |
|
| 383 | - |
|
| 384 | - if ($this->util->isExcluded($fullPath) === false) { |
|
| 385 | - |
|
| 386 | - $size = $unencryptedSize = 0; |
|
| 387 | - $realFile = $this->util->stripPartialFileExtension($path); |
|
| 388 | - $targetExists = $this->file_exists($realFile) || $this->file_exists($path); |
|
| 389 | - $targetIsEncrypted = false; |
|
| 390 | - if ($targetExists) { |
|
| 391 | - // in case the file exists we require the explicit module as |
|
| 392 | - // specified in the file header - otherwise we need to fail hard to |
|
| 393 | - // prevent data loss on client side |
|
| 394 | - if (!empty($encryptionModuleId)) { |
|
| 395 | - $targetIsEncrypted = true; |
|
| 396 | - $encryptionModule = $this->encryptionManager->getEncryptionModule($encryptionModuleId); |
|
| 397 | - } |
|
| 398 | - |
|
| 399 | - if ($this->file_exists($path)) { |
|
| 400 | - $size = $this->storage->filesize($path); |
|
| 401 | - $unencryptedSize = $this->filesize($path); |
|
| 402 | - } else { |
|
| 403 | - $size = $unencryptedSize = 0; |
|
| 404 | - } |
|
| 405 | - } |
|
| 406 | - |
|
| 407 | - try { |
|
| 408 | - |
|
| 409 | - if ( |
|
| 410 | - $mode === 'w' |
|
| 411 | - || $mode === 'w+' |
|
| 412 | - || $mode === 'wb' |
|
| 413 | - || $mode === 'wb+' |
|
| 414 | - ) { |
|
| 415 | - // don't overwrite encrypted files if encryption is not enabled |
|
| 416 | - if ($targetIsEncrypted && $encryptionEnabled === false) { |
|
| 417 | - throw new GenericEncryptionException('Tried to access encrypted file but encryption is not enabled'); |
|
| 418 | - } |
|
| 419 | - if ($encryptionEnabled) { |
|
| 420 | - // if $encryptionModuleId is empty, the default module will be used |
|
| 421 | - $encryptionModule = $this->encryptionManager->getEncryptionModule($encryptionModuleId); |
|
| 422 | - $shouldEncrypt = $encryptionModule->shouldEncrypt($fullPath); |
|
| 423 | - $signed = true; |
|
| 424 | - } |
|
| 425 | - } else { |
|
| 426 | - $info = $this->getCache()->get($path); |
|
| 427 | - // only get encryption module if we found one in the header |
|
| 428 | - // or if file should be encrypted according to the file cache |
|
| 429 | - if (!empty($encryptionModuleId)) { |
|
| 430 | - $encryptionModule = $this->encryptionManager->getEncryptionModule($encryptionModuleId); |
|
| 431 | - $shouldEncrypt = true; |
|
| 432 | - } else if (empty($encryptionModuleId) && $info['encrypted'] === true) { |
|
| 433 | - // we come from a old installation. No header and/or no module defined |
|
| 434 | - // but the file is encrypted. In this case we need to use the |
|
| 435 | - // OC_DEFAULT_MODULE to read the file |
|
| 436 | - $encryptionModule = $this->encryptionManager->getEncryptionModule('OC_DEFAULT_MODULE'); |
|
| 437 | - $shouldEncrypt = true; |
|
| 438 | - $targetIsEncrypted = true; |
|
| 439 | - } |
|
| 440 | - } |
|
| 441 | - } catch (ModuleDoesNotExistsException $e) { |
|
| 442 | - $this->logger->logException($e, [ |
|
| 443 | - 'message' => 'Encryption module "' . $encryptionModuleId . '" not found, file will be stored unencrypted', |
|
| 444 | - 'level' => ILogger::WARN, |
|
| 445 | - 'app' => 'core', |
|
| 446 | - ]); |
|
| 447 | - } |
|
| 448 | - |
|
| 449 | - // encryption disabled on write of new file and write to existing unencrypted file -> don't encrypt |
|
| 450 | - if (!$encryptionEnabled || !$this->shouldEncrypt($path)) { |
|
| 451 | - if (!$targetExists || !$targetIsEncrypted) { |
|
| 452 | - $shouldEncrypt = false; |
|
| 453 | - } |
|
| 454 | - } |
|
| 455 | - |
|
| 456 | - if ($shouldEncrypt === true && $encryptionModule !== null) { |
|
| 457 | - $headerSize = $this->getHeaderSize($path); |
|
| 458 | - $source = $this->storage->fopen($path, $mode); |
|
| 459 | - if (!is_resource($source)) { |
|
| 460 | - return false; |
|
| 461 | - } |
|
| 462 | - $handle = \OC\Files\Stream\Encryption::wrap($source, $path, $fullPath, $header, |
|
| 463 | - $this->uid, $encryptionModule, $this->storage, $this, $this->util, $this->fileHelper, $mode, |
|
| 464 | - $size, $unencryptedSize, $headerSize, $signed); |
|
| 465 | - return $handle; |
|
| 466 | - } |
|
| 467 | - |
|
| 468 | - } |
|
| 469 | - |
|
| 470 | - return $this->storage->fopen($path, $mode); |
|
| 471 | - } |
|
| 472 | - |
|
| 473 | - |
|
| 474 | - /** |
|
| 475 | - * perform some plausibility checks if the the unencrypted size is correct. |
|
| 476 | - * If not, we calculate the correct unencrypted size and return it |
|
| 477 | - * |
|
| 478 | - * @param string $path internal path relative to the storage root |
|
| 479 | - * @param int $unencryptedSize size of the unencrypted file |
|
| 480 | - * |
|
| 481 | - * @return int unencrypted size |
|
| 482 | - */ |
|
| 483 | - protected function verifyUnencryptedSize($path, $unencryptedSize) { |
|
| 484 | - |
|
| 485 | - $size = $this->storage->filesize($path); |
|
| 486 | - $result = $unencryptedSize; |
|
| 487 | - |
|
| 488 | - if ($unencryptedSize < 0 || |
|
| 489 | - ($size > 0 && $unencryptedSize === $size) |
|
| 490 | - ) { |
|
| 491 | - // check if we already calculate the unencrypted size for the |
|
| 492 | - // given path to avoid recursions |
|
| 493 | - if (isset($this->fixUnencryptedSizeOf[$this->getFullPath($path)]) === false) { |
|
| 494 | - $this->fixUnencryptedSizeOf[$this->getFullPath($path)] = true; |
|
| 495 | - try { |
|
| 496 | - $result = $this->fixUnencryptedSize($path, $size, $unencryptedSize); |
|
| 497 | - } catch (\Exception $e) { |
|
| 498 | - $this->logger->error('Couldn\'t re-calculate unencrypted size for '. $path); |
|
| 499 | - $this->logger->logException($e); |
|
| 500 | - } |
|
| 501 | - unset($this->fixUnencryptedSizeOf[$this->getFullPath($path)]); |
|
| 502 | - } |
|
| 503 | - } |
|
| 504 | - |
|
| 505 | - return $result; |
|
| 506 | - } |
|
| 507 | - |
|
| 508 | - /** |
|
| 509 | - * calculate the unencrypted size |
|
| 510 | - * |
|
| 511 | - * @param string $path internal path relative to the storage root |
|
| 512 | - * @param int $size size of the physical file |
|
| 513 | - * @param int $unencryptedSize size of the unencrypted file |
|
| 514 | - * |
|
| 515 | - * @return int calculated unencrypted size |
|
| 516 | - */ |
|
| 517 | - protected function fixUnencryptedSize($path, $size, $unencryptedSize) { |
|
| 518 | - |
|
| 519 | - $headerSize = $this->getHeaderSize($path); |
|
| 520 | - $header = $this->getHeader($path); |
|
| 521 | - $encryptionModule = $this->getEncryptionModule($path); |
|
| 522 | - |
|
| 523 | - $stream = $this->storage->fopen($path, 'r'); |
|
| 524 | - |
|
| 525 | - // if we couldn't open the file we return the old unencrypted size |
|
| 526 | - if (!is_resource($stream)) { |
|
| 527 | - $this->logger->error('Could not open ' . $path . '. Recalculation of unencrypted size aborted.'); |
|
| 528 | - return $unencryptedSize; |
|
| 529 | - } |
|
| 530 | - |
|
| 531 | - $newUnencryptedSize = 0; |
|
| 532 | - $size -= $headerSize; |
|
| 533 | - $blockSize = $this->util->getBlockSize(); |
|
| 534 | - |
|
| 535 | - // if a header exists we skip it |
|
| 536 | - if ($headerSize > 0) { |
|
| 537 | - fread($stream, $headerSize); |
|
| 538 | - } |
|
| 539 | - |
|
| 540 | - // fast path, else the calculation for $lastChunkNr is bogus |
|
| 541 | - if ($size === 0) { |
|
| 542 | - return 0; |
|
| 543 | - } |
|
| 544 | - |
|
| 545 | - $signed = isset($header['signed']) && $header['signed'] === 'true'; |
|
| 546 | - $unencryptedBlockSize = $encryptionModule->getUnencryptedBlockSize($signed); |
|
| 547 | - |
|
| 548 | - // calculate last chunk nr |
|
| 549 | - // next highest is end of chunks, one subtracted is last one |
|
| 550 | - // we have to read the last chunk, we can't just calculate it (because of padding etc) |
|
| 551 | - |
|
| 552 | - $lastChunkNr = ceil($size/ $blockSize)-1; |
|
| 553 | - // calculate last chunk position |
|
| 554 | - $lastChunkPos = ($lastChunkNr * $blockSize); |
|
| 555 | - // try to fseek to the last chunk, if it fails we have to read the whole file |
|
| 556 | - if (@fseek($stream, $lastChunkPos, SEEK_CUR) === 0) { |
|
| 557 | - $newUnencryptedSize += $lastChunkNr * $unencryptedBlockSize; |
|
| 558 | - } |
|
| 559 | - |
|
| 560 | - $lastChunkContentEncrypted=''; |
|
| 561 | - $count = $blockSize; |
|
| 562 | - |
|
| 563 | - while ($count > 0) { |
|
| 564 | - $data=fread($stream, $blockSize); |
|
| 565 | - $count=strlen($data); |
|
| 566 | - $lastChunkContentEncrypted .= $data; |
|
| 567 | - if(strlen($lastChunkContentEncrypted) > $blockSize) { |
|
| 568 | - $newUnencryptedSize += $unencryptedBlockSize; |
|
| 569 | - $lastChunkContentEncrypted=substr($lastChunkContentEncrypted, $blockSize); |
|
| 570 | - } |
|
| 571 | - } |
|
| 572 | - |
|
| 573 | - fclose($stream); |
|
| 574 | - |
|
| 575 | - // we have to decrypt the last chunk to get it actual size |
|
| 576 | - $encryptionModule->begin($this->getFullPath($path), $this->uid, 'r', $header, []); |
|
| 577 | - $decryptedLastChunk = $encryptionModule->decrypt($lastChunkContentEncrypted, $lastChunkNr . 'end'); |
|
| 578 | - $decryptedLastChunk .= $encryptionModule->end($this->getFullPath($path), $lastChunkNr . 'end'); |
|
| 579 | - |
|
| 580 | - // calc the real file size with the size of the last chunk |
|
| 581 | - $newUnencryptedSize += strlen($decryptedLastChunk); |
|
| 582 | - |
|
| 583 | - $this->updateUnencryptedSize($this->getFullPath($path), $newUnencryptedSize); |
|
| 584 | - |
|
| 585 | - // write to cache if applicable |
|
| 586 | - $cache = $this->storage->getCache(); |
|
| 587 | - if ($cache) { |
|
| 588 | - $entry = $cache->get($path); |
|
| 589 | - $cache->update($entry['fileid'], ['size' => $newUnencryptedSize]); |
|
| 590 | - } |
|
| 591 | - |
|
| 592 | - return $newUnencryptedSize; |
|
| 593 | - } |
|
| 594 | - |
|
| 595 | - /** |
|
| 596 | - * @param Storage\IStorage $sourceStorage |
|
| 597 | - * @param string $sourceInternalPath |
|
| 598 | - * @param string $targetInternalPath |
|
| 599 | - * @param bool $preserveMtime |
|
| 600 | - * @return bool |
|
| 601 | - */ |
|
| 602 | - public function moveFromStorage(Storage\IStorage $sourceStorage, $sourceInternalPath, $targetInternalPath, $preserveMtime = true) { |
|
| 603 | - if ($sourceStorage === $this) { |
|
| 604 | - return $this->rename($sourceInternalPath, $targetInternalPath); |
|
| 605 | - } |
|
| 606 | - |
|
| 607 | - // TODO clean this up once the underlying moveFromStorage in OC\Files\Storage\Wrapper\Common is fixed: |
|
| 608 | - // - call $this->storage->moveFromStorage() instead of $this->copyBetweenStorage |
|
| 609 | - // - copy the file cache update from $this->copyBetweenStorage to this method |
|
| 610 | - // - copy the copyKeys() call from $this->copyBetweenStorage to this method |
|
| 611 | - // - remove $this->copyBetweenStorage |
|
| 612 | - |
|
| 613 | - if (!$sourceStorage->isDeletable($sourceInternalPath)) { |
|
| 614 | - return false; |
|
| 615 | - } |
|
| 616 | - |
|
| 617 | - $result = $this->copyBetweenStorage($sourceStorage, $sourceInternalPath, $targetInternalPath, $preserveMtime, true); |
|
| 618 | - if ($result) { |
|
| 619 | - if ($sourceStorage->is_dir($sourceInternalPath)) { |
|
| 620 | - $result &= $sourceStorage->rmdir($sourceInternalPath); |
|
| 621 | - } else { |
|
| 622 | - $result &= $sourceStorage->unlink($sourceInternalPath); |
|
| 623 | - } |
|
| 624 | - } |
|
| 625 | - return $result; |
|
| 626 | - } |
|
| 627 | - |
|
| 628 | - |
|
| 629 | - /** |
|
| 630 | - * @param Storage\IStorage $sourceStorage |
|
| 631 | - * @param string $sourceInternalPath |
|
| 632 | - * @param string $targetInternalPath |
|
| 633 | - * @param bool $preserveMtime |
|
| 634 | - * @param bool $isRename |
|
| 635 | - * @return bool |
|
| 636 | - */ |
|
| 637 | - public function copyFromStorage(Storage\IStorage $sourceStorage, $sourceInternalPath, $targetInternalPath, $preserveMtime = false, $isRename = false) { |
|
| 638 | - |
|
| 639 | - // TODO clean this up once the underlying moveFromStorage in OC\Files\Storage\Wrapper\Common is fixed: |
|
| 640 | - // - call $this->storage->copyFromStorage() instead of $this->copyBetweenStorage |
|
| 641 | - // - copy the file cache update from $this->copyBetweenStorage to this method |
|
| 642 | - // - copy the copyKeys() call from $this->copyBetweenStorage to this method |
|
| 643 | - // - remove $this->copyBetweenStorage |
|
| 644 | - |
|
| 645 | - return $this->copyBetweenStorage($sourceStorage, $sourceInternalPath, $targetInternalPath, $preserveMtime, $isRename); |
|
| 646 | - } |
|
| 647 | - |
|
| 648 | - /** |
|
| 649 | - * Update the encrypted cache version in the database |
|
| 650 | - * |
|
| 651 | - * @param Storage\IStorage $sourceStorage |
|
| 652 | - * @param string $sourceInternalPath |
|
| 653 | - * @param string $targetInternalPath |
|
| 654 | - * @param bool $isRename |
|
| 655 | - * @param bool $keepEncryptionVersion |
|
| 656 | - */ |
|
| 657 | - private function updateEncryptedVersion(Storage\IStorage $sourceStorage, $sourceInternalPath, $targetInternalPath, $isRename, $keepEncryptionVersion) { |
|
| 658 | - $isEncrypted = $this->encryptionManager->isEnabled() && $this->shouldEncrypt($targetInternalPath); |
|
| 659 | - $cacheInformation = [ |
|
| 660 | - 'encrypted' => $isEncrypted, |
|
| 661 | - ]; |
|
| 662 | - if($isEncrypted) { |
|
| 663 | - $encryptedVersion = $sourceStorage->getCache()->get($sourceInternalPath)['encryptedVersion']; |
|
| 664 | - |
|
| 665 | - // In case of a move operation from an unencrypted to an encrypted |
|
| 666 | - // storage the old encrypted version would stay with "0" while the |
|
| 667 | - // correct value would be "1". Thus we manually set the value to "1" |
|
| 668 | - // for those cases. |
|
| 669 | - // See also https://github.com/owncloud/core/issues/23078 |
|
| 670 | - if($encryptedVersion === 0 || !$keepEncryptionVersion) { |
|
| 671 | - $encryptedVersion = 1; |
|
| 672 | - } |
|
| 673 | - |
|
| 674 | - $cacheInformation['encryptedVersion'] = $encryptedVersion; |
|
| 675 | - } |
|
| 676 | - |
|
| 677 | - // in case of a rename we need to manipulate the source cache because |
|
| 678 | - // this information will be kept for the new target |
|
| 679 | - if ($isRename) { |
|
| 680 | - $sourceStorage->getCache()->put($sourceInternalPath, $cacheInformation); |
|
| 681 | - } else { |
|
| 682 | - $this->getCache()->put($targetInternalPath, $cacheInformation); |
|
| 683 | - } |
|
| 684 | - } |
|
| 685 | - |
|
| 686 | - /** |
|
| 687 | - * copy file between two storages |
|
| 688 | - * |
|
| 689 | - * @param Storage\IStorage $sourceStorage |
|
| 690 | - * @param string $sourceInternalPath |
|
| 691 | - * @param string $targetInternalPath |
|
| 692 | - * @param bool $preserveMtime |
|
| 693 | - * @param bool $isRename |
|
| 694 | - * @return bool |
|
| 695 | - * @throws \Exception |
|
| 696 | - */ |
|
| 697 | - private function copyBetweenStorage(Storage\IStorage $sourceStorage, $sourceInternalPath, $targetInternalPath, $preserveMtime, $isRename) { |
|
| 698 | - |
|
| 699 | - // for versions we have nothing to do, because versions should always use the |
|
| 700 | - // key from the original file. Just create a 1:1 copy and done |
|
| 701 | - if ($this->isVersion($targetInternalPath) || |
|
| 702 | - $this->isVersion($sourceInternalPath)) { |
|
| 703 | - // remember that we try to create a version so that we can detect it during |
|
| 704 | - // fopen($sourceInternalPath) and by-pass the encryption in order to |
|
| 705 | - // create a 1:1 copy of the file |
|
| 706 | - $this->arrayCache->set('encryption_copy_version_' . $sourceInternalPath, true); |
|
| 707 | - $result = $this->storage->copyFromStorage($sourceStorage, $sourceInternalPath, $targetInternalPath); |
|
| 708 | - $this->arrayCache->remove('encryption_copy_version_' . $sourceInternalPath); |
|
| 709 | - if ($result) { |
|
| 710 | - $info = $this->getCache('', $sourceStorage)->get($sourceInternalPath); |
|
| 711 | - // make sure that we update the unencrypted size for the version |
|
| 712 | - if (isset($info['encrypted']) && $info['encrypted'] === true) { |
|
| 713 | - $this->updateUnencryptedSize( |
|
| 714 | - $this->getFullPath($targetInternalPath), |
|
| 715 | - $info['size'] |
|
| 716 | - ); |
|
| 717 | - } |
|
| 718 | - $this->updateEncryptedVersion($sourceStorage, $sourceInternalPath, $targetInternalPath, $isRename, true); |
|
| 719 | - } |
|
| 720 | - return $result; |
|
| 721 | - } |
|
| 722 | - |
|
| 723 | - // first copy the keys that we reuse the existing file key on the target location |
|
| 724 | - // and don't create a new one which would break versions for example. |
|
| 725 | - $mount = $this->mountManager->findByStorageId($sourceStorage->getId()); |
|
| 726 | - if (count($mount) === 1) { |
|
| 727 | - $mountPoint = $mount[0]->getMountPoint(); |
|
| 728 | - $source = $mountPoint . '/' . $sourceInternalPath; |
|
| 729 | - $target = $this->getFullPath($targetInternalPath); |
|
| 730 | - $this->copyKeys($source, $target); |
|
| 731 | - } else { |
|
| 732 | - $this->logger->error('Could not find mount point, can\'t keep encryption keys'); |
|
| 733 | - } |
|
| 734 | - |
|
| 735 | - if ($sourceStorage->is_dir($sourceInternalPath)) { |
|
| 736 | - $dh = $sourceStorage->opendir($sourceInternalPath); |
|
| 737 | - $result = $this->mkdir($targetInternalPath); |
|
| 738 | - if (is_resource($dh)) { |
|
| 739 | - while ($result and ($file = readdir($dh)) !== false) { |
|
| 740 | - if (!Filesystem::isIgnoredDir($file)) { |
|
| 741 | - $result &= $this->copyFromStorage($sourceStorage, $sourceInternalPath . '/' . $file, $targetInternalPath . '/' . $file, false, $isRename); |
|
| 742 | - } |
|
| 743 | - } |
|
| 744 | - } |
|
| 745 | - } else { |
|
| 746 | - try { |
|
| 747 | - $source = $sourceStorage->fopen($sourceInternalPath, 'r'); |
|
| 748 | - $target = $this->fopen($targetInternalPath, 'w'); |
|
| 749 | - list(, $result) = \OC_Helper::streamCopy($source, $target); |
|
| 750 | - fclose($source); |
|
| 751 | - fclose($target); |
|
| 752 | - } catch (\Exception $e) { |
|
| 753 | - fclose($source); |
|
| 754 | - fclose($target); |
|
| 755 | - throw $e; |
|
| 756 | - } |
|
| 757 | - if($result) { |
|
| 758 | - if ($preserveMtime) { |
|
| 759 | - $this->touch($targetInternalPath, $sourceStorage->filemtime($sourceInternalPath)); |
|
| 760 | - } |
|
| 761 | - $this->updateEncryptedVersion($sourceStorage, $sourceInternalPath, $targetInternalPath, $isRename, false); |
|
| 762 | - } else { |
|
| 763 | - // delete partially written target file |
|
| 764 | - $this->unlink($targetInternalPath); |
|
| 765 | - // delete cache entry that was created by fopen |
|
| 766 | - $this->getCache()->remove($targetInternalPath); |
|
| 767 | - } |
|
| 768 | - } |
|
| 769 | - return (bool)$result; |
|
| 770 | - |
|
| 771 | - } |
|
| 772 | - |
|
| 773 | - /** |
|
| 774 | - * get the path to a local version of the file. |
|
| 775 | - * The local version of the file can be temporary and doesn't have to be persistent across requests |
|
| 776 | - * |
|
| 777 | - * @param string $path |
|
| 778 | - * @return string |
|
| 779 | - */ |
|
| 780 | - public function getLocalFile($path) { |
|
| 781 | - if ($this->encryptionManager->isEnabled()) { |
|
| 782 | - $cachedFile = $this->getCachedFile($path); |
|
| 783 | - if (is_string($cachedFile)) { |
|
| 784 | - return $cachedFile; |
|
| 785 | - } |
|
| 786 | - } |
|
| 787 | - return $this->storage->getLocalFile($path); |
|
| 788 | - } |
|
| 789 | - |
|
| 790 | - /** |
|
| 791 | - * Returns the wrapped storage's value for isLocal() |
|
| 792 | - * |
|
| 793 | - * @return bool wrapped storage's isLocal() value |
|
| 794 | - */ |
|
| 795 | - public function isLocal() { |
|
| 796 | - if ($this->encryptionManager->isEnabled()) { |
|
| 797 | - return false; |
|
| 798 | - } |
|
| 799 | - return $this->storage->isLocal(); |
|
| 800 | - } |
|
| 801 | - |
|
| 802 | - /** |
|
| 803 | - * see http://php.net/manual/en/function.stat.php |
|
| 804 | - * only the following keys are required in the result: size and mtime |
|
| 805 | - * |
|
| 806 | - * @param string $path |
|
| 807 | - * @return array |
|
| 808 | - */ |
|
| 809 | - public function stat($path) { |
|
| 810 | - $stat = $this->storage->stat($path); |
|
| 811 | - $fileSize = $this->filesize($path); |
|
| 812 | - $stat['size'] = $fileSize; |
|
| 813 | - $stat[7] = $fileSize; |
|
| 814 | - return $stat; |
|
| 815 | - } |
|
| 816 | - |
|
| 817 | - /** |
|
| 818 | - * see http://php.net/manual/en/function.hash.php |
|
| 819 | - * |
|
| 820 | - * @param string $type |
|
| 821 | - * @param string $path |
|
| 822 | - * @param bool $raw |
|
| 823 | - * @return string |
|
| 824 | - */ |
|
| 825 | - public function hash($type, $path, $raw = false) { |
|
| 826 | - $fh = $this->fopen($path, 'rb'); |
|
| 827 | - $ctx = hash_init($type); |
|
| 828 | - hash_update_stream($ctx, $fh); |
|
| 829 | - fclose($fh); |
|
| 830 | - return hash_final($ctx, $raw); |
|
| 831 | - } |
|
| 832 | - |
|
| 833 | - /** |
|
| 834 | - * return full path, including mount point |
|
| 835 | - * |
|
| 836 | - * @param string $path relative to mount point |
|
| 837 | - * @return string full path including mount point |
|
| 838 | - */ |
|
| 839 | - protected function getFullPath($path) { |
|
| 840 | - return Filesystem::normalizePath($this->mountPoint . '/' . $path); |
|
| 841 | - } |
|
| 842 | - |
|
| 843 | - /** |
|
| 844 | - * read first block of encrypted file, typically this will contain the |
|
| 845 | - * encryption header |
|
| 846 | - * |
|
| 847 | - * @param string $path |
|
| 848 | - * @return string |
|
| 849 | - */ |
|
| 850 | - protected function readFirstBlock($path) { |
|
| 851 | - $firstBlock = ''; |
|
| 852 | - if ($this->storage->file_exists($path)) { |
|
| 853 | - $handle = $this->storage->fopen($path, 'r'); |
|
| 854 | - $firstBlock = fread($handle, $this->util->getHeaderSize()); |
|
| 855 | - fclose($handle); |
|
| 856 | - } |
|
| 857 | - return $firstBlock; |
|
| 858 | - } |
|
| 859 | - |
|
| 860 | - /** |
|
| 861 | - * return header size of given file |
|
| 862 | - * |
|
| 863 | - * @param string $path |
|
| 864 | - * @return int |
|
| 865 | - */ |
|
| 866 | - protected function getHeaderSize($path) { |
|
| 867 | - $headerSize = 0; |
|
| 868 | - $realFile = $this->util->stripPartialFileExtension($path); |
|
| 869 | - if ($this->storage->file_exists($realFile)) { |
|
| 870 | - $path = $realFile; |
|
| 871 | - } |
|
| 872 | - $firstBlock = $this->readFirstBlock($path); |
|
| 873 | - |
|
| 874 | - if (substr($firstBlock, 0, strlen(Util::HEADER_START)) === Util::HEADER_START) { |
|
| 875 | - $headerSize = $this->util->getHeaderSize(); |
|
| 876 | - } |
|
| 877 | - |
|
| 878 | - return $headerSize; |
|
| 879 | - } |
|
| 880 | - |
|
| 881 | - /** |
|
| 882 | - * parse raw header to array |
|
| 883 | - * |
|
| 884 | - * @param string $rawHeader |
|
| 885 | - * @return array |
|
| 886 | - */ |
|
| 887 | - protected function parseRawHeader($rawHeader) { |
|
| 888 | - $result = array(); |
|
| 889 | - if (substr($rawHeader, 0, strlen(Util::HEADER_START)) === Util::HEADER_START) { |
|
| 890 | - $header = $rawHeader; |
|
| 891 | - $endAt = strpos($header, Util::HEADER_END); |
|
| 892 | - if ($endAt !== false) { |
|
| 893 | - $header = substr($header, 0, $endAt + strlen(Util::HEADER_END)); |
|
| 894 | - |
|
| 895 | - // +1 to not start with an ':' which would result in empty element at the beginning |
|
| 896 | - $exploded = explode(':', substr($header, strlen(Util::HEADER_START)+1)); |
|
| 897 | - |
|
| 898 | - $element = array_shift($exploded); |
|
| 899 | - while ($element !== Util::HEADER_END) { |
|
| 900 | - $result[$element] = array_shift($exploded); |
|
| 901 | - $element = array_shift($exploded); |
|
| 902 | - } |
|
| 903 | - } |
|
| 904 | - } |
|
| 905 | - |
|
| 906 | - return $result; |
|
| 907 | - } |
|
| 908 | - |
|
| 909 | - /** |
|
| 910 | - * read header from file |
|
| 911 | - * |
|
| 912 | - * @param string $path |
|
| 913 | - * @return array |
|
| 914 | - */ |
|
| 915 | - protected function getHeader($path) { |
|
| 916 | - $realFile = $this->util->stripPartialFileExtension($path); |
|
| 917 | - $exists = $this->storage->file_exists($realFile); |
|
| 918 | - if ($exists) { |
|
| 919 | - $path = $realFile; |
|
| 920 | - } |
|
| 921 | - |
|
| 922 | - $firstBlock = $this->readFirstBlock($path); |
|
| 923 | - $result = $this->parseRawHeader($firstBlock); |
|
| 924 | - |
|
| 925 | - // if the header doesn't contain a encryption module we check if it is a |
|
| 926 | - // legacy file. If true, we add the default encryption module |
|
| 927 | - if (!isset($result[Util::HEADER_ENCRYPTION_MODULE_KEY])) { |
|
| 928 | - if (!empty($result)) { |
|
| 929 | - $result[Util::HEADER_ENCRYPTION_MODULE_KEY] = 'OC_DEFAULT_MODULE'; |
|
| 930 | - } else if ($exists) { |
|
| 931 | - // if the header was empty we have to check first if it is a encrypted file at all |
|
| 932 | - // We would do query to filecache only if we know that entry in filecache exists |
|
| 933 | - $info = $this->getCache()->get($path); |
|
| 934 | - if (isset($info['encrypted']) && $info['encrypted'] === true) { |
|
| 935 | - $result[Util::HEADER_ENCRYPTION_MODULE_KEY] = 'OC_DEFAULT_MODULE'; |
|
| 936 | - } |
|
| 937 | - } |
|
| 938 | - } |
|
| 939 | - |
|
| 940 | - return $result; |
|
| 941 | - } |
|
| 942 | - |
|
| 943 | - /** |
|
| 944 | - * read encryption module needed to read/write the file located at $path |
|
| 945 | - * |
|
| 946 | - * @param string $path |
|
| 947 | - * @return null|\OCP\Encryption\IEncryptionModule |
|
| 948 | - * @throws ModuleDoesNotExistsException |
|
| 949 | - * @throws \Exception |
|
| 950 | - */ |
|
| 951 | - protected function getEncryptionModule($path) { |
|
| 952 | - $encryptionModule = null; |
|
| 953 | - $header = $this->getHeader($path); |
|
| 954 | - $encryptionModuleId = $this->util->getEncryptionModuleId($header); |
|
| 955 | - if (!empty($encryptionModuleId)) { |
|
| 956 | - try { |
|
| 957 | - $encryptionModule = $this->encryptionManager->getEncryptionModule($encryptionModuleId); |
|
| 958 | - } catch (ModuleDoesNotExistsException $e) { |
|
| 959 | - $this->logger->critical('Encryption module defined in "' . $path . '" not loaded!'); |
|
| 960 | - throw $e; |
|
| 961 | - } |
|
| 962 | - } |
|
| 963 | - |
|
| 964 | - return $encryptionModule; |
|
| 965 | - } |
|
| 966 | - |
|
| 967 | - /** |
|
| 968 | - * @param string $path |
|
| 969 | - * @param int $unencryptedSize |
|
| 970 | - */ |
|
| 971 | - public function updateUnencryptedSize($path, $unencryptedSize) { |
|
| 972 | - $this->unencryptedSize[$path] = $unencryptedSize; |
|
| 973 | - } |
|
| 974 | - |
|
| 975 | - /** |
|
| 976 | - * copy keys to new location |
|
| 977 | - * |
|
| 978 | - * @param string $source path relative to data/ |
|
| 979 | - * @param string $target path relative to data/ |
|
| 980 | - * @return bool |
|
| 981 | - */ |
|
| 982 | - protected function copyKeys($source, $target) { |
|
| 983 | - if (!$this->util->isExcluded($source)) { |
|
| 984 | - return $this->keyStorage->copyKeys($source, $target); |
|
| 985 | - } |
|
| 986 | - |
|
| 987 | - return false; |
|
| 988 | - } |
|
| 989 | - |
|
| 990 | - /** |
|
| 991 | - * check if path points to a files version |
|
| 992 | - * |
|
| 993 | - * @param $path |
|
| 994 | - * @return bool |
|
| 995 | - */ |
|
| 996 | - protected function isVersion($path) { |
|
| 997 | - $normalized = Filesystem::normalizePath($path); |
|
| 998 | - return substr($normalized, 0, strlen('/files_versions/')) === '/files_versions/'; |
|
| 999 | - } |
|
| 1000 | - |
|
| 1001 | - /** |
|
| 1002 | - * check if the given storage should be encrypted or not |
|
| 1003 | - * |
|
| 1004 | - * @param $path |
|
| 1005 | - * @return bool |
|
| 1006 | - */ |
|
| 1007 | - protected function shouldEncrypt($path) { |
|
| 1008 | - $fullPath = $this->getFullPath($path); |
|
| 1009 | - $mountPointConfig = $this->mount->getOption('encrypt', true); |
|
| 1010 | - if ($mountPointConfig === false) { |
|
| 1011 | - return false; |
|
| 1012 | - } |
|
| 1013 | - |
|
| 1014 | - try { |
|
| 1015 | - $encryptionModule = $this->getEncryptionModule($fullPath); |
|
| 1016 | - } catch (ModuleDoesNotExistsException $e) { |
|
| 1017 | - return false; |
|
| 1018 | - } |
|
| 1019 | - |
|
| 1020 | - if ($encryptionModule === null) { |
|
| 1021 | - $encryptionModule = $this->encryptionManager->getEncryptionModule(); |
|
| 1022 | - } |
|
| 1023 | - |
|
| 1024 | - return $encryptionModule->shouldEncrypt($fullPath); |
|
| 1025 | - |
|
| 1026 | - } |
|
| 51 | + use LocalTempFileTrait; |
|
| 52 | + |
|
| 53 | + /** @var string */ |
|
| 54 | + private $mountPoint; |
|
| 55 | + |
|
| 56 | + /** @var \OC\Encryption\Util */ |
|
| 57 | + private $util; |
|
| 58 | + |
|
| 59 | + /** @var \OCP\Encryption\IManager */ |
|
| 60 | + private $encryptionManager; |
|
| 61 | + |
|
| 62 | + /** @var \OCP\ILogger */ |
|
| 63 | + private $logger; |
|
| 64 | + |
|
| 65 | + /** @var string */ |
|
| 66 | + private $uid; |
|
| 67 | + |
|
| 68 | + /** @var array */ |
|
| 69 | + protected $unencryptedSize; |
|
| 70 | + |
|
| 71 | + /** @var \OCP\Encryption\IFile */ |
|
| 72 | + private $fileHelper; |
|
| 73 | + |
|
| 74 | + /** @var IMountPoint */ |
|
| 75 | + private $mount; |
|
| 76 | + |
|
| 77 | + /** @var IStorage */ |
|
| 78 | + private $keyStorage; |
|
| 79 | + |
|
| 80 | + /** @var Update */ |
|
| 81 | + private $update; |
|
| 82 | + |
|
| 83 | + /** @var Manager */ |
|
| 84 | + private $mountManager; |
|
| 85 | + |
|
| 86 | + /** @var array remember for which path we execute the repair step to avoid recursions */ |
|
| 87 | + private $fixUnencryptedSizeOf = array(); |
|
| 88 | + |
|
| 89 | + /** @var ArrayCache */ |
|
| 90 | + private $arrayCache; |
|
| 91 | + |
|
| 92 | + /** |
|
| 93 | + * @param array $parameters |
|
| 94 | + * @param IManager $encryptionManager |
|
| 95 | + * @param Util $util |
|
| 96 | + * @param ILogger $logger |
|
| 97 | + * @param IFile $fileHelper |
|
| 98 | + * @param string $uid |
|
| 99 | + * @param IStorage $keyStorage |
|
| 100 | + * @param Update $update |
|
| 101 | + * @param Manager $mountManager |
|
| 102 | + * @param ArrayCache $arrayCache |
|
| 103 | + */ |
|
| 104 | + public function __construct( |
|
| 105 | + $parameters, |
|
| 106 | + IManager $encryptionManager = null, |
|
| 107 | + Util $util = null, |
|
| 108 | + ILogger $logger = null, |
|
| 109 | + IFile $fileHelper = null, |
|
| 110 | + $uid = null, |
|
| 111 | + IStorage $keyStorage = null, |
|
| 112 | + Update $update = null, |
|
| 113 | + Manager $mountManager = null, |
|
| 114 | + ArrayCache $arrayCache = null |
|
| 115 | + ) { |
|
| 116 | + |
|
| 117 | + $this->mountPoint = $parameters['mountPoint']; |
|
| 118 | + $this->mount = $parameters['mount']; |
|
| 119 | + $this->encryptionManager = $encryptionManager; |
|
| 120 | + $this->util = $util; |
|
| 121 | + $this->logger = $logger; |
|
| 122 | + $this->uid = $uid; |
|
| 123 | + $this->fileHelper = $fileHelper; |
|
| 124 | + $this->keyStorage = $keyStorage; |
|
| 125 | + $this->unencryptedSize = array(); |
|
| 126 | + $this->update = $update; |
|
| 127 | + $this->mountManager = $mountManager; |
|
| 128 | + $this->arrayCache = $arrayCache; |
|
| 129 | + parent::__construct($parameters); |
|
| 130 | + } |
|
| 131 | + |
|
| 132 | + /** |
|
| 133 | + * see http://php.net/manual/en/function.filesize.php |
|
| 134 | + * The result for filesize when called on a folder is required to be 0 |
|
| 135 | + * |
|
| 136 | + * @param string $path |
|
| 137 | + * @return int |
|
| 138 | + */ |
|
| 139 | + public function filesize($path) { |
|
| 140 | + $fullPath = $this->getFullPath($path); |
|
| 141 | + |
|
| 142 | + /** @var CacheEntry $info */ |
|
| 143 | + $info = $this->getCache()->get($path); |
|
| 144 | + if (isset($this->unencryptedSize[$fullPath])) { |
|
| 145 | + $size = $this->unencryptedSize[$fullPath]; |
|
| 146 | + // update file cache |
|
| 147 | + if ($info instanceof ICacheEntry) { |
|
| 148 | + $info = $info->getData(); |
|
| 149 | + $info['encrypted'] = $info['encryptedVersion']; |
|
| 150 | + } else { |
|
| 151 | + if (!is_array($info)) { |
|
| 152 | + $info = []; |
|
| 153 | + } |
|
| 154 | + $info['encrypted'] = true; |
|
| 155 | + } |
|
| 156 | + |
|
| 157 | + $info['size'] = $size; |
|
| 158 | + $this->getCache()->put($path, $info); |
|
| 159 | + |
|
| 160 | + return $size; |
|
| 161 | + } |
|
| 162 | + |
|
| 163 | + if (isset($info['fileid']) && $info['encrypted']) { |
|
| 164 | + return $this->verifyUnencryptedSize($path, $info['size']); |
|
| 165 | + } |
|
| 166 | + |
|
| 167 | + return $this->storage->filesize($path); |
|
| 168 | + } |
|
| 169 | + |
|
| 170 | + /** |
|
| 171 | + * @param string $path |
|
| 172 | + * @return array |
|
| 173 | + */ |
|
| 174 | + public function getMetaData($path) { |
|
| 175 | + $data = $this->storage->getMetaData($path); |
|
| 176 | + if (is_null($data)) { |
|
| 177 | + return null; |
|
| 178 | + } |
|
| 179 | + $fullPath = $this->getFullPath($path); |
|
| 180 | + $info = $this->getCache()->get($path); |
|
| 181 | + |
|
| 182 | + if (isset($this->unencryptedSize[$fullPath])) { |
|
| 183 | + $data['encrypted'] = true; |
|
| 184 | + $data['size'] = $this->unencryptedSize[$fullPath]; |
|
| 185 | + } else { |
|
| 186 | + if (isset($info['fileid']) && $info['encrypted']) { |
|
| 187 | + $data['size'] = $this->verifyUnencryptedSize($path, $info['size']); |
|
| 188 | + $data['encrypted'] = true; |
|
| 189 | + } |
|
| 190 | + } |
|
| 191 | + |
|
| 192 | + if (isset($info['encryptedVersion']) && $info['encryptedVersion'] > 1) { |
|
| 193 | + $data['encryptedVersion'] = $info['encryptedVersion']; |
|
| 194 | + } |
|
| 195 | + |
|
| 196 | + return $data; |
|
| 197 | + } |
|
| 198 | + |
|
| 199 | + /** |
|
| 200 | + * see http://php.net/manual/en/function.file_get_contents.php |
|
| 201 | + * |
|
| 202 | + * @param string $path |
|
| 203 | + * @return string |
|
| 204 | + */ |
|
| 205 | + public function file_get_contents($path) { |
|
| 206 | + |
|
| 207 | + $encryptionModule = $this->getEncryptionModule($path); |
|
| 208 | + |
|
| 209 | + if ($encryptionModule) { |
|
| 210 | + $handle = $this->fopen($path, "r"); |
|
| 211 | + if (!$handle) { |
|
| 212 | + return false; |
|
| 213 | + } |
|
| 214 | + $data = stream_get_contents($handle); |
|
| 215 | + fclose($handle); |
|
| 216 | + return $data; |
|
| 217 | + } |
|
| 218 | + return $this->storage->file_get_contents($path); |
|
| 219 | + } |
|
| 220 | + |
|
| 221 | + /** |
|
| 222 | + * see http://php.net/manual/en/function.file_put_contents.php |
|
| 223 | + * |
|
| 224 | + * @param string $path |
|
| 225 | + * @param string $data |
|
| 226 | + * @return bool |
|
| 227 | + */ |
|
| 228 | + public function file_put_contents($path, $data) { |
|
| 229 | + // file put content will always be translated to a stream write |
|
| 230 | + $handle = $this->fopen($path, 'w'); |
|
| 231 | + if (is_resource($handle)) { |
|
| 232 | + $written = fwrite($handle, $data); |
|
| 233 | + fclose($handle); |
|
| 234 | + return $written; |
|
| 235 | + } |
|
| 236 | + |
|
| 237 | + return false; |
|
| 238 | + } |
|
| 239 | + |
|
| 240 | + /** |
|
| 241 | + * see http://php.net/manual/en/function.unlink.php |
|
| 242 | + * |
|
| 243 | + * @param string $path |
|
| 244 | + * @return bool |
|
| 245 | + */ |
|
| 246 | + public function unlink($path) { |
|
| 247 | + $fullPath = $this->getFullPath($path); |
|
| 248 | + if ($this->util->isExcluded($fullPath)) { |
|
| 249 | + return $this->storage->unlink($path); |
|
| 250 | + } |
|
| 251 | + |
|
| 252 | + $encryptionModule = $this->getEncryptionModule($path); |
|
| 253 | + if ($encryptionModule) { |
|
| 254 | + $this->keyStorage->deleteAllFileKeys($this->getFullPath($path)); |
|
| 255 | + } |
|
| 256 | + |
|
| 257 | + return $this->storage->unlink($path); |
|
| 258 | + } |
|
| 259 | + |
|
| 260 | + /** |
|
| 261 | + * see http://php.net/manual/en/function.rename.php |
|
| 262 | + * |
|
| 263 | + * @param string $path1 |
|
| 264 | + * @param string $path2 |
|
| 265 | + * @return bool |
|
| 266 | + */ |
|
| 267 | + public function rename($path1, $path2) { |
|
| 268 | + |
|
| 269 | + $result = $this->storage->rename($path1, $path2); |
|
| 270 | + |
|
| 271 | + if ($result && |
|
| 272 | + // versions always use the keys from the original file, so we can skip |
|
| 273 | + // this step for versions |
|
| 274 | + $this->isVersion($path2) === false && |
|
| 275 | + $this->encryptionManager->isEnabled()) { |
|
| 276 | + $source = $this->getFullPath($path1); |
|
| 277 | + if (!$this->util->isExcluded($source)) { |
|
| 278 | + $target = $this->getFullPath($path2); |
|
| 279 | + if (isset($this->unencryptedSize[$source])) { |
|
| 280 | + $this->unencryptedSize[$target] = $this->unencryptedSize[$source]; |
|
| 281 | + } |
|
| 282 | + $this->keyStorage->renameKeys($source, $target); |
|
| 283 | + $module = $this->getEncryptionModule($path2); |
|
| 284 | + if ($module) { |
|
| 285 | + $module->update($target, $this->uid, []); |
|
| 286 | + } |
|
| 287 | + } |
|
| 288 | + } |
|
| 289 | + |
|
| 290 | + return $result; |
|
| 291 | + } |
|
| 292 | + |
|
| 293 | + /** |
|
| 294 | + * see http://php.net/manual/en/function.rmdir.php |
|
| 295 | + * |
|
| 296 | + * @param string $path |
|
| 297 | + * @return bool |
|
| 298 | + */ |
|
| 299 | + public function rmdir($path) { |
|
| 300 | + $result = $this->storage->rmdir($path); |
|
| 301 | + $fullPath = $this->getFullPath($path); |
|
| 302 | + if ($result && |
|
| 303 | + $this->util->isExcluded($fullPath) === false && |
|
| 304 | + $this->encryptionManager->isEnabled() |
|
| 305 | + ) { |
|
| 306 | + $this->keyStorage->deleteAllFileKeys($fullPath); |
|
| 307 | + } |
|
| 308 | + |
|
| 309 | + return $result; |
|
| 310 | + } |
|
| 311 | + |
|
| 312 | + /** |
|
| 313 | + * check if a file can be read |
|
| 314 | + * |
|
| 315 | + * @param string $path |
|
| 316 | + * @return bool |
|
| 317 | + */ |
|
| 318 | + public function isReadable($path) { |
|
| 319 | + |
|
| 320 | + $isReadable = true; |
|
| 321 | + |
|
| 322 | + $metaData = $this->getMetaData($path); |
|
| 323 | + if ( |
|
| 324 | + !$this->is_dir($path) && |
|
| 325 | + isset($metaData['encrypted']) && |
|
| 326 | + $metaData['encrypted'] === true |
|
| 327 | + ) { |
|
| 328 | + $fullPath = $this->getFullPath($path); |
|
| 329 | + $module = $this->getEncryptionModule($path); |
|
| 330 | + $isReadable = $module->isReadable($fullPath, $this->uid); |
|
| 331 | + } |
|
| 332 | + |
|
| 333 | + return $this->storage->isReadable($path) && $isReadable; |
|
| 334 | + } |
|
| 335 | + |
|
| 336 | + /** |
|
| 337 | + * see http://php.net/manual/en/function.copy.php |
|
| 338 | + * |
|
| 339 | + * @param string $path1 |
|
| 340 | + * @param string $path2 |
|
| 341 | + * @return bool |
|
| 342 | + */ |
|
| 343 | + public function copy($path1, $path2) { |
|
| 344 | + |
|
| 345 | + $source = $this->getFullPath($path1); |
|
| 346 | + |
|
| 347 | + if ($this->util->isExcluded($source)) { |
|
| 348 | + return $this->storage->copy($path1, $path2); |
|
| 349 | + } |
|
| 350 | + |
|
| 351 | + // need to stream copy file by file in case we copy between a encrypted |
|
| 352 | + // and a unencrypted storage |
|
| 353 | + $this->unlink($path2); |
|
| 354 | + return $this->copyFromStorage($this, $path1, $path2); |
|
| 355 | + } |
|
| 356 | + |
|
| 357 | + /** |
|
| 358 | + * see http://php.net/manual/en/function.fopen.php |
|
| 359 | + * |
|
| 360 | + * @param string $path |
|
| 361 | + * @param string $mode |
|
| 362 | + * @return resource|bool |
|
| 363 | + * @throws GenericEncryptionException |
|
| 364 | + * @throws ModuleDoesNotExistsException |
|
| 365 | + */ |
|
| 366 | + public function fopen($path, $mode) { |
|
| 367 | + |
|
| 368 | + // check if the file is stored in the array cache, this means that we |
|
| 369 | + // copy a file over to the versions folder, in this case we don't want to |
|
| 370 | + // decrypt it |
|
| 371 | + if ($this->arrayCache->hasKey('encryption_copy_version_' . $path)) { |
|
| 372 | + $this->arrayCache->remove('encryption_copy_version_' . $path); |
|
| 373 | + return $this->storage->fopen($path, $mode); |
|
| 374 | + } |
|
| 375 | + |
|
| 376 | + $encryptionEnabled = $this->encryptionManager->isEnabled(); |
|
| 377 | + $shouldEncrypt = false; |
|
| 378 | + $encryptionModule = null; |
|
| 379 | + $header = $this->getHeader($path); |
|
| 380 | + $signed = isset($header['signed']) && $header['signed'] === 'true'; |
|
| 381 | + $fullPath = $this->getFullPath($path); |
|
| 382 | + $encryptionModuleId = $this->util->getEncryptionModuleId($header); |
|
| 383 | + |
|
| 384 | + if ($this->util->isExcluded($fullPath) === false) { |
|
| 385 | + |
|
| 386 | + $size = $unencryptedSize = 0; |
|
| 387 | + $realFile = $this->util->stripPartialFileExtension($path); |
|
| 388 | + $targetExists = $this->file_exists($realFile) || $this->file_exists($path); |
|
| 389 | + $targetIsEncrypted = false; |
|
| 390 | + if ($targetExists) { |
|
| 391 | + // in case the file exists we require the explicit module as |
|
| 392 | + // specified in the file header - otherwise we need to fail hard to |
|
| 393 | + // prevent data loss on client side |
|
| 394 | + if (!empty($encryptionModuleId)) { |
|
| 395 | + $targetIsEncrypted = true; |
|
| 396 | + $encryptionModule = $this->encryptionManager->getEncryptionModule($encryptionModuleId); |
|
| 397 | + } |
|
| 398 | + |
|
| 399 | + if ($this->file_exists($path)) { |
|
| 400 | + $size = $this->storage->filesize($path); |
|
| 401 | + $unencryptedSize = $this->filesize($path); |
|
| 402 | + } else { |
|
| 403 | + $size = $unencryptedSize = 0; |
|
| 404 | + } |
|
| 405 | + } |
|
| 406 | + |
|
| 407 | + try { |
|
| 408 | + |
|
| 409 | + if ( |
|
| 410 | + $mode === 'w' |
|
| 411 | + || $mode === 'w+' |
|
| 412 | + || $mode === 'wb' |
|
| 413 | + || $mode === 'wb+' |
|
| 414 | + ) { |
|
| 415 | + // don't overwrite encrypted files if encryption is not enabled |
|
| 416 | + if ($targetIsEncrypted && $encryptionEnabled === false) { |
|
| 417 | + throw new GenericEncryptionException('Tried to access encrypted file but encryption is not enabled'); |
|
| 418 | + } |
|
| 419 | + if ($encryptionEnabled) { |
|
| 420 | + // if $encryptionModuleId is empty, the default module will be used |
|
| 421 | + $encryptionModule = $this->encryptionManager->getEncryptionModule($encryptionModuleId); |
|
| 422 | + $shouldEncrypt = $encryptionModule->shouldEncrypt($fullPath); |
|
| 423 | + $signed = true; |
|
| 424 | + } |
|
| 425 | + } else { |
|
| 426 | + $info = $this->getCache()->get($path); |
|
| 427 | + // only get encryption module if we found one in the header |
|
| 428 | + // or if file should be encrypted according to the file cache |
|
| 429 | + if (!empty($encryptionModuleId)) { |
|
| 430 | + $encryptionModule = $this->encryptionManager->getEncryptionModule($encryptionModuleId); |
|
| 431 | + $shouldEncrypt = true; |
|
| 432 | + } else if (empty($encryptionModuleId) && $info['encrypted'] === true) { |
|
| 433 | + // we come from a old installation. No header and/or no module defined |
|
| 434 | + // but the file is encrypted. In this case we need to use the |
|
| 435 | + // OC_DEFAULT_MODULE to read the file |
|
| 436 | + $encryptionModule = $this->encryptionManager->getEncryptionModule('OC_DEFAULT_MODULE'); |
|
| 437 | + $shouldEncrypt = true; |
|
| 438 | + $targetIsEncrypted = true; |
|
| 439 | + } |
|
| 440 | + } |
|
| 441 | + } catch (ModuleDoesNotExistsException $e) { |
|
| 442 | + $this->logger->logException($e, [ |
|
| 443 | + 'message' => 'Encryption module "' . $encryptionModuleId . '" not found, file will be stored unencrypted', |
|
| 444 | + 'level' => ILogger::WARN, |
|
| 445 | + 'app' => 'core', |
|
| 446 | + ]); |
|
| 447 | + } |
|
| 448 | + |
|
| 449 | + // encryption disabled on write of new file and write to existing unencrypted file -> don't encrypt |
|
| 450 | + if (!$encryptionEnabled || !$this->shouldEncrypt($path)) { |
|
| 451 | + if (!$targetExists || !$targetIsEncrypted) { |
|
| 452 | + $shouldEncrypt = false; |
|
| 453 | + } |
|
| 454 | + } |
|
| 455 | + |
|
| 456 | + if ($shouldEncrypt === true && $encryptionModule !== null) { |
|
| 457 | + $headerSize = $this->getHeaderSize($path); |
|
| 458 | + $source = $this->storage->fopen($path, $mode); |
|
| 459 | + if (!is_resource($source)) { |
|
| 460 | + return false; |
|
| 461 | + } |
|
| 462 | + $handle = \OC\Files\Stream\Encryption::wrap($source, $path, $fullPath, $header, |
|
| 463 | + $this->uid, $encryptionModule, $this->storage, $this, $this->util, $this->fileHelper, $mode, |
|
| 464 | + $size, $unencryptedSize, $headerSize, $signed); |
|
| 465 | + return $handle; |
|
| 466 | + } |
|
| 467 | + |
|
| 468 | + } |
|
| 469 | + |
|
| 470 | + return $this->storage->fopen($path, $mode); |
|
| 471 | + } |
|
| 472 | + |
|
| 473 | + |
|
| 474 | + /** |
|
| 475 | + * perform some plausibility checks if the the unencrypted size is correct. |
|
| 476 | + * If not, we calculate the correct unencrypted size and return it |
|
| 477 | + * |
|
| 478 | + * @param string $path internal path relative to the storage root |
|
| 479 | + * @param int $unencryptedSize size of the unencrypted file |
|
| 480 | + * |
|
| 481 | + * @return int unencrypted size |
|
| 482 | + */ |
|
| 483 | + protected function verifyUnencryptedSize($path, $unencryptedSize) { |
|
| 484 | + |
|
| 485 | + $size = $this->storage->filesize($path); |
|
| 486 | + $result = $unencryptedSize; |
|
| 487 | + |
|
| 488 | + if ($unencryptedSize < 0 || |
|
| 489 | + ($size > 0 && $unencryptedSize === $size) |
|
| 490 | + ) { |
|
| 491 | + // check if we already calculate the unencrypted size for the |
|
| 492 | + // given path to avoid recursions |
|
| 493 | + if (isset($this->fixUnencryptedSizeOf[$this->getFullPath($path)]) === false) { |
|
| 494 | + $this->fixUnencryptedSizeOf[$this->getFullPath($path)] = true; |
|
| 495 | + try { |
|
| 496 | + $result = $this->fixUnencryptedSize($path, $size, $unencryptedSize); |
|
| 497 | + } catch (\Exception $e) { |
|
| 498 | + $this->logger->error('Couldn\'t re-calculate unencrypted size for '. $path); |
|
| 499 | + $this->logger->logException($e); |
|
| 500 | + } |
|
| 501 | + unset($this->fixUnencryptedSizeOf[$this->getFullPath($path)]); |
|
| 502 | + } |
|
| 503 | + } |
|
| 504 | + |
|
| 505 | + return $result; |
|
| 506 | + } |
|
| 507 | + |
|
| 508 | + /** |
|
| 509 | + * calculate the unencrypted size |
|
| 510 | + * |
|
| 511 | + * @param string $path internal path relative to the storage root |
|
| 512 | + * @param int $size size of the physical file |
|
| 513 | + * @param int $unencryptedSize size of the unencrypted file |
|
| 514 | + * |
|
| 515 | + * @return int calculated unencrypted size |
|
| 516 | + */ |
|
| 517 | + protected function fixUnencryptedSize($path, $size, $unencryptedSize) { |
|
| 518 | + |
|
| 519 | + $headerSize = $this->getHeaderSize($path); |
|
| 520 | + $header = $this->getHeader($path); |
|
| 521 | + $encryptionModule = $this->getEncryptionModule($path); |
|
| 522 | + |
|
| 523 | + $stream = $this->storage->fopen($path, 'r'); |
|
| 524 | + |
|
| 525 | + // if we couldn't open the file we return the old unencrypted size |
|
| 526 | + if (!is_resource($stream)) { |
|
| 527 | + $this->logger->error('Could not open ' . $path . '. Recalculation of unencrypted size aborted.'); |
|
| 528 | + return $unencryptedSize; |
|
| 529 | + } |
|
| 530 | + |
|
| 531 | + $newUnencryptedSize = 0; |
|
| 532 | + $size -= $headerSize; |
|
| 533 | + $blockSize = $this->util->getBlockSize(); |
|
| 534 | + |
|
| 535 | + // if a header exists we skip it |
|
| 536 | + if ($headerSize > 0) { |
|
| 537 | + fread($stream, $headerSize); |
|
| 538 | + } |
|
| 539 | + |
|
| 540 | + // fast path, else the calculation for $lastChunkNr is bogus |
|
| 541 | + if ($size === 0) { |
|
| 542 | + return 0; |
|
| 543 | + } |
|
| 544 | + |
|
| 545 | + $signed = isset($header['signed']) && $header['signed'] === 'true'; |
|
| 546 | + $unencryptedBlockSize = $encryptionModule->getUnencryptedBlockSize($signed); |
|
| 547 | + |
|
| 548 | + // calculate last chunk nr |
|
| 549 | + // next highest is end of chunks, one subtracted is last one |
|
| 550 | + // we have to read the last chunk, we can't just calculate it (because of padding etc) |
|
| 551 | + |
|
| 552 | + $lastChunkNr = ceil($size/ $blockSize)-1; |
|
| 553 | + // calculate last chunk position |
|
| 554 | + $lastChunkPos = ($lastChunkNr * $blockSize); |
|
| 555 | + // try to fseek to the last chunk, if it fails we have to read the whole file |
|
| 556 | + if (@fseek($stream, $lastChunkPos, SEEK_CUR) === 0) { |
|
| 557 | + $newUnencryptedSize += $lastChunkNr * $unencryptedBlockSize; |
|
| 558 | + } |
|
| 559 | + |
|
| 560 | + $lastChunkContentEncrypted=''; |
|
| 561 | + $count = $blockSize; |
|
| 562 | + |
|
| 563 | + while ($count > 0) { |
|
| 564 | + $data=fread($stream, $blockSize); |
|
| 565 | + $count=strlen($data); |
|
| 566 | + $lastChunkContentEncrypted .= $data; |
|
| 567 | + if(strlen($lastChunkContentEncrypted) > $blockSize) { |
|
| 568 | + $newUnencryptedSize += $unencryptedBlockSize; |
|
| 569 | + $lastChunkContentEncrypted=substr($lastChunkContentEncrypted, $blockSize); |
|
| 570 | + } |
|
| 571 | + } |
|
| 572 | + |
|
| 573 | + fclose($stream); |
|
| 574 | + |
|
| 575 | + // we have to decrypt the last chunk to get it actual size |
|
| 576 | + $encryptionModule->begin($this->getFullPath($path), $this->uid, 'r', $header, []); |
|
| 577 | + $decryptedLastChunk = $encryptionModule->decrypt($lastChunkContentEncrypted, $lastChunkNr . 'end'); |
|
| 578 | + $decryptedLastChunk .= $encryptionModule->end($this->getFullPath($path), $lastChunkNr . 'end'); |
|
| 579 | + |
|
| 580 | + // calc the real file size with the size of the last chunk |
|
| 581 | + $newUnencryptedSize += strlen($decryptedLastChunk); |
|
| 582 | + |
|
| 583 | + $this->updateUnencryptedSize($this->getFullPath($path), $newUnencryptedSize); |
|
| 584 | + |
|
| 585 | + // write to cache if applicable |
|
| 586 | + $cache = $this->storage->getCache(); |
|
| 587 | + if ($cache) { |
|
| 588 | + $entry = $cache->get($path); |
|
| 589 | + $cache->update($entry['fileid'], ['size' => $newUnencryptedSize]); |
|
| 590 | + } |
|
| 591 | + |
|
| 592 | + return $newUnencryptedSize; |
|
| 593 | + } |
|
| 594 | + |
|
| 595 | + /** |
|
| 596 | + * @param Storage\IStorage $sourceStorage |
|
| 597 | + * @param string $sourceInternalPath |
|
| 598 | + * @param string $targetInternalPath |
|
| 599 | + * @param bool $preserveMtime |
|
| 600 | + * @return bool |
|
| 601 | + */ |
|
| 602 | + public function moveFromStorage(Storage\IStorage $sourceStorage, $sourceInternalPath, $targetInternalPath, $preserveMtime = true) { |
|
| 603 | + if ($sourceStorage === $this) { |
|
| 604 | + return $this->rename($sourceInternalPath, $targetInternalPath); |
|
| 605 | + } |
|
| 606 | + |
|
| 607 | + // TODO clean this up once the underlying moveFromStorage in OC\Files\Storage\Wrapper\Common is fixed: |
|
| 608 | + // - call $this->storage->moveFromStorage() instead of $this->copyBetweenStorage |
|
| 609 | + // - copy the file cache update from $this->copyBetweenStorage to this method |
|
| 610 | + // - copy the copyKeys() call from $this->copyBetweenStorage to this method |
|
| 611 | + // - remove $this->copyBetweenStorage |
|
| 612 | + |
|
| 613 | + if (!$sourceStorage->isDeletable($sourceInternalPath)) { |
|
| 614 | + return false; |
|
| 615 | + } |
|
| 616 | + |
|
| 617 | + $result = $this->copyBetweenStorage($sourceStorage, $sourceInternalPath, $targetInternalPath, $preserveMtime, true); |
|
| 618 | + if ($result) { |
|
| 619 | + if ($sourceStorage->is_dir($sourceInternalPath)) { |
|
| 620 | + $result &= $sourceStorage->rmdir($sourceInternalPath); |
|
| 621 | + } else { |
|
| 622 | + $result &= $sourceStorage->unlink($sourceInternalPath); |
|
| 623 | + } |
|
| 624 | + } |
|
| 625 | + return $result; |
|
| 626 | + } |
|
| 627 | + |
|
| 628 | + |
|
| 629 | + /** |
|
| 630 | + * @param Storage\IStorage $sourceStorage |
|
| 631 | + * @param string $sourceInternalPath |
|
| 632 | + * @param string $targetInternalPath |
|
| 633 | + * @param bool $preserveMtime |
|
| 634 | + * @param bool $isRename |
|
| 635 | + * @return bool |
|
| 636 | + */ |
|
| 637 | + public function copyFromStorage(Storage\IStorage $sourceStorage, $sourceInternalPath, $targetInternalPath, $preserveMtime = false, $isRename = false) { |
|
| 638 | + |
|
| 639 | + // TODO clean this up once the underlying moveFromStorage in OC\Files\Storage\Wrapper\Common is fixed: |
|
| 640 | + // - call $this->storage->copyFromStorage() instead of $this->copyBetweenStorage |
|
| 641 | + // - copy the file cache update from $this->copyBetweenStorage to this method |
|
| 642 | + // - copy the copyKeys() call from $this->copyBetweenStorage to this method |
|
| 643 | + // - remove $this->copyBetweenStorage |
|
| 644 | + |
|
| 645 | + return $this->copyBetweenStorage($sourceStorage, $sourceInternalPath, $targetInternalPath, $preserveMtime, $isRename); |
|
| 646 | + } |
|
| 647 | + |
|
| 648 | + /** |
|
| 649 | + * Update the encrypted cache version in the database |
|
| 650 | + * |
|
| 651 | + * @param Storage\IStorage $sourceStorage |
|
| 652 | + * @param string $sourceInternalPath |
|
| 653 | + * @param string $targetInternalPath |
|
| 654 | + * @param bool $isRename |
|
| 655 | + * @param bool $keepEncryptionVersion |
|
| 656 | + */ |
|
| 657 | + private function updateEncryptedVersion(Storage\IStorage $sourceStorage, $sourceInternalPath, $targetInternalPath, $isRename, $keepEncryptionVersion) { |
|
| 658 | + $isEncrypted = $this->encryptionManager->isEnabled() && $this->shouldEncrypt($targetInternalPath); |
|
| 659 | + $cacheInformation = [ |
|
| 660 | + 'encrypted' => $isEncrypted, |
|
| 661 | + ]; |
|
| 662 | + if($isEncrypted) { |
|
| 663 | + $encryptedVersion = $sourceStorage->getCache()->get($sourceInternalPath)['encryptedVersion']; |
|
| 664 | + |
|
| 665 | + // In case of a move operation from an unencrypted to an encrypted |
|
| 666 | + // storage the old encrypted version would stay with "0" while the |
|
| 667 | + // correct value would be "1". Thus we manually set the value to "1" |
|
| 668 | + // for those cases. |
|
| 669 | + // See also https://github.com/owncloud/core/issues/23078 |
|
| 670 | + if($encryptedVersion === 0 || !$keepEncryptionVersion) { |
|
| 671 | + $encryptedVersion = 1; |
|
| 672 | + } |
|
| 673 | + |
|
| 674 | + $cacheInformation['encryptedVersion'] = $encryptedVersion; |
|
| 675 | + } |
|
| 676 | + |
|
| 677 | + // in case of a rename we need to manipulate the source cache because |
|
| 678 | + // this information will be kept for the new target |
|
| 679 | + if ($isRename) { |
|
| 680 | + $sourceStorage->getCache()->put($sourceInternalPath, $cacheInformation); |
|
| 681 | + } else { |
|
| 682 | + $this->getCache()->put($targetInternalPath, $cacheInformation); |
|
| 683 | + } |
|
| 684 | + } |
|
| 685 | + |
|
| 686 | + /** |
|
| 687 | + * copy file between two storages |
|
| 688 | + * |
|
| 689 | + * @param Storage\IStorage $sourceStorage |
|
| 690 | + * @param string $sourceInternalPath |
|
| 691 | + * @param string $targetInternalPath |
|
| 692 | + * @param bool $preserveMtime |
|
| 693 | + * @param bool $isRename |
|
| 694 | + * @return bool |
|
| 695 | + * @throws \Exception |
|
| 696 | + */ |
|
| 697 | + private function copyBetweenStorage(Storage\IStorage $sourceStorage, $sourceInternalPath, $targetInternalPath, $preserveMtime, $isRename) { |
|
| 698 | + |
|
| 699 | + // for versions we have nothing to do, because versions should always use the |
|
| 700 | + // key from the original file. Just create a 1:1 copy and done |
|
| 701 | + if ($this->isVersion($targetInternalPath) || |
|
| 702 | + $this->isVersion($sourceInternalPath)) { |
|
| 703 | + // remember that we try to create a version so that we can detect it during |
|
| 704 | + // fopen($sourceInternalPath) and by-pass the encryption in order to |
|
| 705 | + // create a 1:1 copy of the file |
|
| 706 | + $this->arrayCache->set('encryption_copy_version_' . $sourceInternalPath, true); |
|
| 707 | + $result = $this->storage->copyFromStorage($sourceStorage, $sourceInternalPath, $targetInternalPath); |
|
| 708 | + $this->arrayCache->remove('encryption_copy_version_' . $sourceInternalPath); |
|
| 709 | + if ($result) { |
|
| 710 | + $info = $this->getCache('', $sourceStorage)->get($sourceInternalPath); |
|
| 711 | + // make sure that we update the unencrypted size for the version |
|
| 712 | + if (isset($info['encrypted']) && $info['encrypted'] === true) { |
|
| 713 | + $this->updateUnencryptedSize( |
|
| 714 | + $this->getFullPath($targetInternalPath), |
|
| 715 | + $info['size'] |
|
| 716 | + ); |
|
| 717 | + } |
|
| 718 | + $this->updateEncryptedVersion($sourceStorage, $sourceInternalPath, $targetInternalPath, $isRename, true); |
|
| 719 | + } |
|
| 720 | + return $result; |
|
| 721 | + } |
|
| 722 | + |
|
| 723 | + // first copy the keys that we reuse the existing file key on the target location |
|
| 724 | + // and don't create a new one which would break versions for example. |
|
| 725 | + $mount = $this->mountManager->findByStorageId($sourceStorage->getId()); |
|
| 726 | + if (count($mount) === 1) { |
|
| 727 | + $mountPoint = $mount[0]->getMountPoint(); |
|
| 728 | + $source = $mountPoint . '/' . $sourceInternalPath; |
|
| 729 | + $target = $this->getFullPath($targetInternalPath); |
|
| 730 | + $this->copyKeys($source, $target); |
|
| 731 | + } else { |
|
| 732 | + $this->logger->error('Could not find mount point, can\'t keep encryption keys'); |
|
| 733 | + } |
|
| 734 | + |
|
| 735 | + if ($sourceStorage->is_dir($sourceInternalPath)) { |
|
| 736 | + $dh = $sourceStorage->opendir($sourceInternalPath); |
|
| 737 | + $result = $this->mkdir($targetInternalPath); |
|
| 738 | + if (is_resource($dh)) { |
|
| 739 | + while ($result and ($file = readdir($dh)) !== false) { |
|
| 740 | + if (!Filesystem::isIgnoredDir($file)) { |
|
| 741 | + $result &= $this->copyFromStorage($sourceStorage, $sourceInternalPath . '/' . $file, $targetInternalPath . '/' . $file, false, $isRename); |
|
| 742 | + } |
|
| 743 | + } |
|
| 744 | + } |
|
| 745 | + } else { |
|
| 746 | + try { |
|
| 747 | + $source = $sourceStorage->fopen($sourceInternalPath, 'r'); |
|
| 748 | + $target = $this->fopen($targetInternalPath, 'w'); |
|
| 749 | + list(, $result) = \OC_Helper::streamCopy($source, $target); |
|
| 750 | + fclose($source); |
|
| 751 | + fclose($target); |
|
| 752 | + } catch (\Exception $e) { |
|
| 753 | + fclose($source); |
|
| 754 | + fclose($target); |
|
| 755 | + throw $e; |
|
| 756 | + } |
|
| 757 | + if($result) { |
|
| 758 | + if ($preserveMtime) { |
|
| 759 | + $this->touch($targetInternalPath, $sourceStorage->filemtime($sourceInternalPath)); |
|
| 760 | + } |
|
| 761 | + $this->updateEncryptedVersion($sourceStorage, $sourceInternalPath, $targetInternalPath, $isRename, false); |
|
| 762 | + } else { |
|
| 763 | + // delete partially written target file |
|
| 764 | + $this->unlink($targetInternalPath); |
|
| 765 | + // delete cache entry that was created by fopen |
|
| 766 | + $this->getCache()->remove($targetInternalPath); |
|
| 767 | + } |
|
| 768 | + } |
|
| 769 | + return (bool)$result; |
|
| 770 | + |
|
| 771 | + } |
|
| 772 | + |
|
| 773 | + /** |
|
| 774 | + * get the path to a local version of the file. |
|
| 775 | + * The local version of the file can be temporary and doesn't have to be persistent across requests |
|
| 776 | + * |
|
| 777 | + * @param string $path |
|
| 778 | + * @return string |
|
| 779 | + */ |
|
| 780 | + public function getLocalFile($path) { |
|
| 781 | + if ($this->encryptionManager->isEnabled()) { |
|
| 782 | + $cachedFile = $this->getCachedFile($path); |
|
| 783 | + if (is_string($cachedFile)) { |
|
| 784 | + return $cachedFile; |
|
| 785 | + } |
|
| 786 | + } |
|
| 787 | + return $this->storage->getLocalFile($path); |
|
| 788 | + } |
|
| 789 | + |
|
| 790 | + /** |
|
| 791 | + * Returns the wrapped storage's value for isLocal() |
|
| 792 | + * |
|
| 793 | + * @return bool wrapped storage's isLocal() value |
|
| 794 | + */ |
|
| 795 | + public function isLocal() { |
|
| 796 | + if ($this->encryptionManager->isEnabled()) { |
|
| 797 | + return false; |
|
| 798 | + } |
|
| 799 | + return $this->storage->isLocal(); |
|
| 800 | + } |
|
| 801 | + |
|
| 802 | + /** |
|
| 803 | + * see http://php.net/manual/en/function.stat.php |
|
| 804 | + * only the following keys are required in the result: size and mtime |
|
| 805 | + * |
|
| 806 | + * @param string $path |
|
| 807 | + * @return array |
|
| 808 | + */ |
|
| 809 | + public function stat($path) { |
|
| 810 | + $stat = $this->storage->stat($path); |
|
| 811 | + $fileSize = $this->filesize($path); |
|
| 812 | + $stat['size'] = $fileSize; |
|
| 813 | + $stat[7] = $fileSize; |
|
| 814 | + return $stat; |
|
| 815 | + } |
|
| 816 | + |
|
| 817 | + /** |
|
| 818 | + * see http://php.net/manual/en/function.hash.php |
|
| 819 | + * |
|
| 820 | + * @param string $type |
|
| 821 | + * @param string $path |
|
| 822 | + * @param bool $raw |
|
| 823 | + * @return string |
|
| 824 | + */ |
|
| 825 | + public function hash($type, $path, $raw = false) { |
|
| 826 | + $fh = $this->fopen($path, 'rb'); |
|
| 827 | + $ctx = hash_init($type); |
|
| 828 | + hash_update_stream($ctx, $fh); |
|
| 829 | + fclose($fh); |
|
| 830 | + return hash_final($ctx, $raw); |
|
| 831 | + } |
|
| 832 | + |
|
| 833 | + /** |
|
| 834 | + * return full path, including mount point |
|
| 835 | + * |
|
| 836 | + * @param string $path relative to mount point |
|
| 837 | + * @return string full path including mount point |
|
| 838 | + */ |
|
| 839 | + protected function getFullPath($path) { |
|
| 840 | + return Filesystem::normalizePath($this->mountPoint . '/' . $path); |
|
| 841 | + } |
|
| 842 | + |
|
| 843 | + /** |
|
| 844 | + * read first block of encrypted file, typically this will contain the |
|
| 845 | + * encryption header |
|
| 846 | + * |
|
| 847 | + * @param string $path |
|
| 848 | + * @return string |
|
| 849 | + */ |
|
| 850 | + protected function readFirstBlock($path) { |
|
| 851 | + $firstBlock = ''; |
|
| 852 | + if ($this->storage->file_exists($path)) { |
|
| 853 | + $handle = $this->storage->fopen($path, 'r'); |
|
| 854 | + $firstBlock = fread($handle, $this->util->getHeaderSize()); |
|
| 855 | + fclose($handle); |
|
| 856 | + } |
|
| 857 | + return $firstBlock; |
|
| 858 | + } |
|
| 859 | + |
|
| 860 | + /** |
|
| 861 | + * return header size of given file |
|
| 862 | + * |
|
| 863 | + * @param string $path |
|
| 864 | + * @return int |
|
| 865 | + */ |
|
| 866 | + protected function getHeaderSize($path) { |
|
| 867 | + $headerSize = 0; |
|
| 868 | + $realFile = $this->util->stripPartialFileExtension($path); |
|
| 869 | + if ($this->storage->file_exists($realFile)) { |
|
| 870 | + $path = $realFile; |
|
| 871 | + } |
|
| 872 | + $firstBlock = $this->readFirstBlock($path); |
|
| 873 | + |
|
| 874 | + if (substr($firstBlock, 0, strlen(Util::HEADER_START)) === Util::HEADER_START) { |
|
| 875 | + $headerSize = $this->util->getHeaderSize(); |
|
| 876 | + } |
|
| 877 | + |
|
| 878 | + return $headerSize; |
|
| 879 | + } |
|
| 880 | + |
|
| 881 | + /** |
|
| 882 | + * parse raw header to array |
|
| 883 | + * |
|
| 884 | + * @param string $rawHeader |
|
| 885 | + * @return array |
|
| 886 | + */ |
|
| 887 | + protected function parseRawHeader($rawHeader) { |
|
| 888 | + $result = array(); |
|
| 889 | + if (substr($rawHeader, 0, strlen(Util::HEADER_START)) === Util::HEADER_START) { |
|
| 890 | + $header = $rawHeader; |
|
| 891 | + $endAt = strpos($header, Util::HEADER_END); |
|
| 892 | + if ($endAt !== false) { |
|
| 893 | + $header = substr($header, 0, $endAt + strlen(Util::HEADER_END)); |
|
| 894 | + |
|
| 895 | + // +1 to not start with an ':' which would result in empty element at the beginning |
|
| 896 | + $exploded = explode(':', substr($header, strlen(Util::HEADER_START)+1)); |
|
| 897 | + |
|
| 898 | + $element = array_shift($exploded); |
|
| 899 | + while ($element !== Util::HEADER_END) { |
|
| 900 | + $result[$element] = array_shift($exploded); |
|
| 901 | + $element = array_shift($exploded); |
|
| 902 | + } |
|
| 903 | + } |
|
| 904 | + } |
|
| 905 | + |
|
| 906 | + return $result; |
|
| 907 | + } |
|
| 908 | + |
|
| 909 | + /** |
|
| 910 | + * read header from file |
|
| 911 | + * |
|
| 912 | + * @param string $path |
|
| 913 | + * @return array |
|
| 914 | + */ |
|
| 915 | + protected function getHeader($path) { |
|
| 916 | + $realFile = $this->util->stripPartialFileExtension($path); |
|
| 917 | + $exists = $this->storage->file_exists($realFile); |
|
| 918 | + if ($exists) { |
|
| 919 | + $path = $realFile; |
|
| 920 | + } |
|
| 921 | + |
|
| 922 | + $firstBlock = $this->readFirstBlock($path); |
|
| 923 | + $result = $this->parseRawHeader($firstBlock); |
|
| 924 | + |
|
| 925 | + // if the header doesn't contain a encryption module we check if it is a |
|
| 926 | + // legacy file. If true, we add the default encryption module |
|
| 927 | + if (!isset($result[Util::HEADER_ENCRYPTION_MODULE_KEY])) { |
|
| 928 | + if (!empty($result)) { |
|
| 929 | + $result[Util::HEADER_ENCRYPTION_MODULE_KEY] = 'OC_DEFAULT_MODULE'; |
|
| 930 | + } else if ($exists) { |
|
| 931 | + // if the header was empty we have to check first if it is a encrypted file at all |
|
| 932 | + // We would do query to filecache only if we know that entry in filecache exists |
|
| 933 | + $info = $this->getCache()->get($path); |
|
| 934 | + if (isset($info['encrypted']) && $info['encrypted'] === true) { |
|
| 935 | + $result[Util::HEADER_ENCRYPTION_MODULE_KEY] = 'OC_DEFAULT_MODULE'; |
|
| 936 | + } |
|
| 937 | + } |
|
| 938 | + } |
|
| 939 | + |
|
| 940 | + return $result; |
|
| 941 | + } |
|
| 942 | + |
|
| 943 | + /** |
|
| 944 | + * read encryption module needed to read/write the file located at $path |
|
| 945 | + * |
|
| 946 | + * @param string $path |
|
| 947 | + * @return null|\OCP\Encryption\IEncryptionModule |
|
| 948 | + * @throws ModuleDoesNotExistsException |
|
| 949 | + * @throws \Exception |
|
| 950 | + */ |
|
| 951 | + protected function getEncryptionModule($path) { |
|
| 952 | + $encryptionModule = null; |
|
| 953 | + $header = $this->getHeader($path); |
|
| 954 | + $encryptionModuleId = $this->util->getEncryptionModuleId($header); |
|
| 955 | + if (!empty($encryptionModuleId)) { |
|
| 956 | + try { |
|
| 957 | + $encryptionModule = $this->encryptionManager->getEncryptionModule($encryptionModuleId); |
|
| 958 | + } catch (ModuleDoesNotExistsException $e) { |
|
| 959 | + $this->logger->critical('Encryption module defined in "' . $path . '" not loaded!'); |
|
| 960 | + throw $e; |
|
| 961 | + } |
|
| 962 | + } |
|
| 963 | + |
|
| 964 | + return $encryptionModule; |
|
| 965 | + } |
|
| 966 | + |
|
| 967 | + /** |
|
| 968 | + * @param string $path |
|
| 969 | + * @param int $unencryptedSize |
|
| 970 | + */ |
|
| 971 | + public function updateUnencryptedSize($path, $unencryptedSize) { |
|
| 972 | + $this->unencryptedSize[$path] = $unencryptedSize; |
|
| 973 | + } |
|
| 974 | + |
|
| 975 | + /** |
|
| 976 | + * copy keys to new location |
|
| 977 | + * |
|
| 978 | + * @param string $source path relative to data/ |
|
| 979 | + * @param string $target path relative to data/ |
|
| 980 | + * @return bool |
|
| 981 | + */ |
|
| 982 | + protected function copyKeys($source, $target) { |
|
| 983 | + if (!$this->util->isExcluded($source)) { |
|
| 984 | + return $this->keyStorage->copyKeys($source, $target); |
|
| 985 | + } |
|
| 986 | + |
|
| 987 | + return false; |
|
| 988 | + } |
|
| 989 | + |
|
| 990 | + /** |
|
| 991 | + * check if path points to a files version |
|
| 992 | + * |
|
| 993 | + * @param $path |
|
| 994 | + * @return bool |
|
| 995 | + */ |
|
| 996 | + protected function isVersion($path) { |
|
| 997 | + $normalized = Filesystem::normalizePath($path); |
|
| 998 | + return substr($normalized, 0, strlen('/files_versions/')) === '/files_versions/'; |
|
| 999 | + } |
|
| 1000 | + |
|
| 1001 | + /** |
|
| 1002 | + * check if the given storage should be encrypted or not |
|
| 1003 | + * |
|
| 1004 | + * @param $path |
|
| 1005 | + * @return bool |
|
| 1006 | + */ |
|
| 1007 | + protected function shouldEncrypt($path) { |
|
| 1008 | + $fullPath = $this->getFullPath($path); |
|
| 1009 | + $mountPointConfig = $this->mount->getOption('encrypt', true); |
|
| 1010 | + if ($mountPointConfig === false) { |
|
| 1011 | + return false; |
|
| 1012 | + } |
|
| 1013 | + |
|
| 1014 | + try { |
|
| 1015 | + $encryptionModule = $this->getEncryptionModule($fullPath); |
|
| 1016 | + } catch (ModuleDoesNotExistsException $e) { |
|
| 1017 | + return false; |
|
| 1018 | + } |
|
| 1019 | + |
|
| 1020 | + if ($encryptionModule === null) { |
|
| 1021 | + $encryptionModule = $this->encryptionManager->getEncryptionModule(); |
|
| 1022 | + } |
|
| 1023 | + |
|
| 1024 | + return $encryptionModule->shouldEncrypt($fullPath); |
|
| 1025 | + |
|
| 1026 | + } |
|
| 1027 | 1027 | |
| 1028 | 1028 | } |
@@ -368,8 +368,8 @@ discard block |
||
| 368 | 368 | // check if the file is stored in the array cache, this means that we |
| 369 | 369 | // copy a file over to the versions folder, in this case we don't want to |
| 370 | 370 | // decrypt it |
| 371 | - if ($this->arrayCache->hasKey('encryption_copy_version_' . $path)) { |
|
| 372 | - $this->arrayCache->remove('encryption_copy_version_' . $path); |
|
| 371 | + if ($this->arrayCache->hasKey('encryption_copy_version_'.$path)) { |
|
| 372 | + $this->arrayCache->remove('encryption_copy_version_'.$path); |
|
| 373 | 373 | return $this->storage->fopen($path, $mode); |
| 374 | 374 | } |
| 375 | 375 | |
@@ -440,7 +440,7 @@ discard block |
||
| 440 | 440 | } |
| 441 | 441 | } catch (ModuleDoesNotExistsException $e) { |
| 442 | 442 | $this->logger->logException($e, [ |
| 443 | - 'message' => 'Encryption module "' . $encryptionModuleId . '" not found, file will be stored unencrypted', |
|
| 443 | + 'message' => 'Encryption module "'.$encryptionModuleId.'" not found, file will be stored unencrypted', |
|
| 444 | 444 | 'level' => ILogger::WARN, |
| 445 | 445 | 'app' => 'core', |
| 446 | 446 | ]); |
@@ -495,7 +495,7 @@ discard block |
||
| 495 | 495 | try { |
| 496 | 496 | $result = $this->fixUnencryptedSize($path, $size, $unencryptedSize); |
| 497 | 497 | } catch (\Exception $e) { |
| 498 | - $this->logger->error('Couldn\'t re-calculate unencrypted size for '. $path); |
|
| 498 | + $this->logger->error('Couldn\'t re-calculate unencrypted size for '.$path); |
|
| 499 | 499 | $this->logger->logException($e); |
| 500 | 500 | } |
| 501 | 501 | unset($this->fixUnencryptedSizeOf[$this->getFullPath($path)]); |
@@ -524,7 +524,7 @@ discard block |
||
| 524 | 524 | |
| 525 | 525 | // if we couldn't open the file we return the old unencrypted size |
| 526 | 526 | if (!is_resource($stream)) { |
| 527 | - $this->logger->error('Could not open ' . $path . '. Recalculation of unencrypted size aborted.'); |
|
| 527 | + $this->logger->error('Could not open '.$path.'. Recalculation of unencrypted size aborted.'); |
|
| 528 | 528 | return $unencryptedSize; |
| 529 | 529 | } |
| 530 | 530 | |
@@ -549,7 +549,7 @@ discard block |
||
| 549 | 549 | // next highest is end of chunks, one subtracted is last one |
| 550 | 550 | // we have to read the last chunk, we can't just calculate it (because of padding etc) |
| 551 | 551 | |
| 552 | - $lastChunkNr = ceil($size/ $blockSize)-1; |
|
| 552 | + $lastChunkNr = ceil($size / $blockSize) - 1; |
|
| 553 | 553 | // calculate last chunk position |
| 554 | 554 | $lastChunkPos = ($lastChunkNr * $blockSize); |
| 555 | 555 | // try to fseek to the last chunk, if it fails we have to read the whole file |
@@ -557,16 +557,16 @@ discard block |
||
| 557 | 557 | $newUnencryptedSize += $lastChunkNr * $unencryptedBlockSize; |
| 558 | 558 | } |
| 559 | 559 | |
| 560 | - $lastChunkContentEncrypted=''; |
|
| 560 | + $lastChunkContentEncrypted = ''; |
|
| 561 | 561 | $count = $blockSize; |
| 562 | 562 | |
| 563 | 563 | while ($count > 0) { |
| 564 | - $data=fread($stream, $blockSize); |
|
| 565 | - $count=strlen($data); |
|
| 564 | + $data = fread($stream, $blockSize); |
|
| 565 | + $count = strlen($data); |
|
| 566 | 566 | $lastChunkContentEncrypted .= $data; |
| 567 | - if(strlen($lastChunkContentEncrypted) > $blockSize) { |
|
| 567 | + if (strlen($lastChunkContentEncrypted) > $blockSize) { |
|
| 568 | 568 | $newUnencryptedSize += $unencryptedBlockSize; |
| 569 | - $lastChunkContentEncrypted=substr($lastChunkContentEncrypted, $blockSize); |
|
| 569 | + $lastChunkContentEncrypted = substr($lastChunkContentEncrypted, $blockSize); |
|
| 570 | 570 | } |
| 571 | 571 | } |
| 572 | 572 | |
@@ -574,8 +574,8 @@ discard block |
||
| 574 | 574 | |
| 575 | 575 | // we have to decrypt the last chunk to get it actual size |
| 576 | 576 | $encryptionModule->begin($this->getFullPath($path), $this->uid, 'r', $header, []); |
| 577 | - $decryptedLastChunk = $encryptionModule->decrypt($lastChunkContentEncrypted, $lastChunkNr . 'end'); |
|
| 578 | - $decryptedLastChunk .= $encryptionModule->end($this->getFullPath($path), $lastChunkNr . 'end'); |
|
| 577 | + $decryptedLastChunk = $encryptionModule->decrypt($lastChunkContentEncrypted, $lastChunkNr.'end'); |
|
| 578 | + $decryptedLastChunk .= $encryptionModule->end($this->getFullPath($path), $lastChunkNr.'end'); |
|
| 579 | 579 | |
| 580 | 580 | // calc the real file size with the size of the last chunk |
| 581 | 581 | $newUnencryptedSize += strlen($decryptedLastChunk); |
@@ -659,7 +659,7 @@ discard block |
||
| 659 | 659 | $cacheInformation = [ |
| 660 | 660 | 'encrypted' => $isEncrypted, |
| 661 | 661 | ]; |
| 662 | - if($isEncrypted) { |
|
| 662 | + if ($isEncrypted) { |
|
| 663 | 663 | $encryptedVersion = $sourceStorage->getCache()->get($sourceInternalPath)['encryptedVersion']; |
| 664 | 664 | |
| 665 | 665 | // In case of a move operation from an unencrypted to an encrypted |
@@ -667,7 +667,7 @@ discard block |
||
| 667 | 667 | // correct value would be "1". Thus we manually set the value to "1" |
| 668 | 668 | // for those cases. |
| 669 | 669 | // See also https://github.com/owncloud/core/issues/23078 |
| 670 | - if($encryptedVersion === 0 || !$keepEncryptionVersion) { |
|
| 670 | + if ($encryptedVersion === 0 || !$keepEncryptionVersion) { |
|
| 671 | 671 | $encryptedVersion = 1; |
| 672 | 672 | } |
| 673 | 673 | |
@@ -703,9 +703,9 @@ discard block |
||
| 703 | 703 | // remember that we try to create a version so that we can detect it during |
| 704 | 704 | // fopen($sourceInternalPath) and by-pass the encryption in order to |
| 705 | 705 | // create a 1:1 copy of the file |
| 706 | - $this->arrayCache->set('encryption_copy_version_' . $sourceInternalPath, true); |
|
| 706 | + $this->arrayCache->set('encryption_copy_version_'.$sourceInternalPath, true); |
|
| 707 | 707 | $result = $this->storage->copyFromStorage($sourceStorage, $sourceInternalPath, $targetInternalPath); |
| 708 | - $this->arrayCache->remove('encryption_copy_version_' . $sourceInternalPath); |
|
| 708 | + $this->arrayCache->remove('encryption_copy_version_'.$sourceInternalPath); |
|
| 709 | 709 | if ($result) { |
| 710 | 710 | $info = $this->getCache('', $sourceStorage)->get($sourceInternalPath); |
| 711 | 711 | // make sure that we update the unencrypted size for the version |
@@ -725,7 +725,7 @@ discard block |
||
| 725 | 725 | $mount = $this->mountManager->findByStorageId($sourceStorage->getId()); |
| 726 | 726 | if (count($mount) === 1) { |
| 727 | 727 | $mountPoint = $mount[0]->getMountPoint(); |
| 728 | - $source = $mountPoint . '/' . $sourceInternalPath; |
|
| 728 | + $source = $mountPoint.'/'.$sourceInternalPath; |
|
| 729 | 729 | $target = $this->getFullPath($targetInternalPath); |
| 730 | 730 | $this->copyKeys($source, $target); |
| 731 | 731 | } else { |
@@ -738,7 +738,7 @@ discard block |
||
| 738 | 738 | if (is_resource($dh)) { |
| 739 | 739 | while ($result and ($file = readdir($dh)) !== false) { |
| 740 | 740 | if (!Filesystem::isIgnoredDir($file)) { |
| 741 | - $result &= $this->copyFromStorage($sourceStorage, $sourceInternalPath . '/' . $file, $targetInternalPath . '/' . $file, false, $isRename); |
|
| 741 | + $result &= $this->copyFromStorage($sourceStorage, $sourceInternalPath.'/'.$file, $targetInternalPath.'/'.$file, false, $isRename); |
|
| 742 | 742 | } |
| 743 | 743 | } |
| 744 | 744 | } |
@@ -754,7 +754,7 @@ discard block |
||
| 754 | 754 | fclose($target); |
| 755 | 755 | throw $e; |
| 756 | 756 | } |
| 757 | - if($result) { |
|
| 757 | + if ($result) { |
|
| 758 | 758 | if ($preserveMtime) { |
| 759 | 759 | $this->touch($targetInternalPath, $sourceStorage->filemtime($sourceInternalPath)); |
| 760 | 760 | } |
@@ -766,7 +766,7 @@ discard block |
||
| 766 | 766 | $this->getCache()->remove($targetInternalPath); |
| 767 | 767 | } |
| 768 | 768 | } |
| 769 | - return (bool)$result; |
|
| 769 | + return (bool) $result; |
|
| 770 | 770 | |
| 771 | 771 | } |
| 772 | 772 | |
@@ -837,7 +837,7 @@ discard block |
||
| 837 | 837 | * @return string full path including mount point |
| 838 | 838 | */ |
| 839 | 839 | protected function getFullPath($path) { |
| 840 | - return Filesystem::normalizePath($this->mountPoint . '/' . $path); |
|
| 840 | + return Filesystem::normalizePath($this->mountPoint.'/'.$path); |
|
| 841 | 841 | } |
| 842 | 842 | |
| 843 | 843 | /** |
@@ -893,7 +893,7 @@ discard block |
||
| 893 | 893 | $header = substr($header, 0, $endAt + strlen(Util::HEADER_END)); |
| 894 | 894 | |
| 895 | 895 | // +1 to not start with an ':' which would result in empty element at the beginning |
| 896 | - $exploded = explode(':', substr($header, strlen(Util::HEADER_START)+1)); |
|
| 896 | + $exploded = explode(':', substr($header, strlen(Util::HEADER_START) + 1)); |
|
| 897 | 897 | |
| 898 | 898 | $element = array_shift($exploded); |
| 899 | 899 | while ($element !== Util::HEADER_END) { |
@@ -956,7 +956,7 @@ discard block |
||
| 956 | 956 | try { |
| 957 | 957 | $encryptionModule = $this->encryptionManager->getEncryptionModule($encryptionModuleId); |
| 958 | 958 | } catch (ModuleDoesNotExistsException $e) { |
| 959 | - $this->logger->critical('Encryption module defined in "' . $path . '" not loaded!'); |
|
| 959 | + $this->logger->critical('Encryption module defined in "'.$path.'" not loaded!'); |
|
| 960 | 960 | throw $e; |
| 961 | 961 | } |
| 962 | 962 | } |
@@ -162,6 +162,9 @@ discard block |
||
| 162 | 162 | return true; |
| 163 | 163 | } |
| 164 | 164 | |
| 165 | + /** |
|
| 166 | + * @param string $path |
|
| 167 | + */ |
|
| 165 | 168 | private function rmObjects($path) { |
| 166 | 169 | $children = $this->getCache()->getFolderContents($path); |
| 167 | 170 | foreach ($children as $child) { |
@@ -364,6 +367,9 @@ discard block |
||
| 364 | 367 | return true; |
| 365 | 368 | } |
| 366 | 369 | |
| 370 | + /** |
|
| 371 | + * @param string $path |
|
| 372 | + */ |
|
| 367 | 373 | public function writeBack($tmpFile, $path) { |
| 368 | 374 | $stat = $this->stat($path); |
| 369 | 375 | if (empty($stat)) { |
@@ -55,9 +55,9 @@ discard block |
||
| 55 | 55 | throw new \Exception('missing IObjectStore instance'); |
| 56 | 56 | } |
| 57 | 57 | if (isset($params['storageid'])) { |
| 58 | - $this->id = 'object::store:' . $params['storageid']; |
|
| 58 | + $this->id = 'object::store:'.$params['storageid']; |
|
| 59 | 59 | } else { |
| 60 | - $this->id = 'object::store:' . $this->objectStore->getStorageId(); |
|
| 60 | + $this->id = 'object::store:'.$this->objectStore->getStorageId(); |
|
| 61 | 61 | } |
| 62 | 62 | if (isset($params['objectPrefix'])) { |
| 63 | 63 | $this->objectPrefix = $params['objectPrefix']; |
@@ -191,7 +191,7 @@ discard block |
||
| 191 | 191 | if ($ex->getCode() !== 404) { |
| 192 | 192 | $this->logger->logException($ex, [ |
| 193 | 193 | 'app' => 'objectstore', |
| 194 | - 'message' => 'Could not delete object ' . $this->getURN($stat['fileid']) . ' for ' . $path, |
|
| 194 | + 'message' => 'Could not delete object '.$this->getURN($stat['fileid']).' for '.$path, |
|
| 195 | 195 | ]); |
| 196 | 196 | return false; |
| 197 | 197 | } |
@@ -223,7 +223,7 @@ discard block |
||
| 223 | 223 | */ |
| 224 | 224 | protected function getURN($fileId) { |
| 225 | 225 | if (is_numeric($fileId)) { |
| 226 | - return $this->objectPrefix . $fileId; |
|
| 226 | + return $this->objectPrefix.$fileId; |
|
| 227 | 227 | } |
| 228 | 228 | return null; |
| 229 | 229 | } |
@@ -277,7 +277,7 @@ discard block |
||
| 277 | 277 | } catch (\Exception $ex) { |
| 278 | 278 | $this->logger->logException($ex, [ |
| 279 | 279 | 'app' => 'objectstore', |
| 280 | - 'message' => 'Count not get object ' . $this->getURN($stat['fileid']) . ' for file ' . $path, |
|
| 280 | + 'message' => 'Count not get object '.$this->getURN($stat['fileid']).' for file '.$path, |
|
| 281 | 281 | ]); |
| 282 | 282 | return false; |
| 283 | 283 | } |
@@ -290,7 +290,7 @@ discard block |
||
| 290 | 290 | case 'wb+': |
| 291 | 291 | $tmpFile = \OC::$server->getTempManager()->getTemporaryFile($ext); |
| 292 | 292 | $handle = fopen($tmpFile, $mode); |
| 293 | - return CallbackWrapper::wrap($handle, null, null, function () use ($path, $tmpFile) { |
|
| 293 | + return CallbackWrapper::wrap($handle, null, null, function() use ($path, $tmpFile) { |
|
| 294 | 294 | $this->writeBack($tmpFile, $path); |
| 295 | 295 | }); |
| 296 | 296 | case 'a': |
@@ -307,7 +307,7 @@ discard block |
||
| 307 | 307 | file_put_contents($tmpFile, $source); |
| 308 | 308 | } |
| 309 | 309 | $handle = fopen($tmpFile, $mode); |
| 310 | - return CallbackWrapper::wrap($handle, null, null, function () use ($path, $tmpFile) { |
|
| 310 | + return CallbackWrapper::wrap($handle, null, null, function() use ($path, $tmpFile) { |
|
| 311 | 311 | $this->writeBack($tmpFile, $path); |
| 312 | 312 | }); |
| 313 | 313 | } |
@@ -316,7 +316,7 @@ discard block |
||
| 316 | 316 | |
| 317 | 317 | public function file_exists($path) { |
| 318 | 318 | $path = $this->normalizePath($path); |
| 319 | - return (bool)$this->stat($path); |
|
| 319 | + return (bool) $this->stat($path); |
|
| 320 | 320 | } |
| 321 | 321 | |
| 322 | 322 | public function rename($source, $target) { |
@@ -373,7 +373,7 @@ discard block |
||
| 373 | 373 | } catch (\Exception $ex) { |
| 374 | 374 | $this->logger->logException($ex, [ |
| 375 | 375 | 'app' => 'objectstore', |
| 376 | - 'message' => 'Could not create object for ' . $path, |
|
| 376 | + 'message' => 'Could not create object for '.$path, |
|
| 377 | 377 | ]); |
| 378 | 378 | throw $ex; |
| 379 | 379 | } |
@@ -413,7 +413,7 @@ discard block |
||
| 413 | 413 | $this->getCache()->remove($path); |
| 414 | 414 | $this->logger->logException($ex, [ |
| 415 | 415 | 'app' => 'objectstore', |
| 416 | - 'message' => 'Could not create object ' . $this->getURN($fileId) . ' for ' . $path, |
|
| 416 | + 'message' => 'Could not create object '.$this->getURN($fileId).' for '.$path, |
|
| 417 | 417 | ]); |
| 418 | 418 | throw $ex; // make this bubble up |
| 419 | 419 | } |
@@ -31,406 +31,406 @@ |
||
| 31 | 31 | use OCP\Files\ObjectStore\IObjectStore; |
| 32 | 32 | |
| 33 | 33 | class ObjectStoreStorage extends \OC\Files\Storage\Common { |
| 34 | - /** |
|
| 35 | - * @var \OCP\Files\ObjectStore\IObjectStore $objectStore |
|
| 36 | - */ |
|
| 37 | - protected $objectStore; |
|
| 38 | - /** |
|
| 39 | - * @var string $id |
|
| 40 | - */ |
|
| 41 | - protected $id; |
|
| 42 | - /** |
|
| 43 | - * @var \OC\User\User $user |
|
| 44 | - */ |
|
| 45 | - protected $user; |
|
| 46 | - |
|
| 47 | - private $objectPrefix = 'urn:oid:'; |
|
| 48 | - |
|
| 49 | - private $logger; |
|
| 50 | - |
|
| 51 | - public function __construct($params) { |
|
| 52 | - if (isset($params['objectstore']) && $params['objectstore'] instanceof IObjectStore) { |
|
| 53 | - $this->objectStore = $params['objectstore']; |
|
| 54 | - } else { |
|
| 55 | - throw new \Exception('missing IObjectStore instance'); |
|
| 56 | - } |
|
| 57 | - if (isset($params['storageid'])) { |
|
| 58 | - $this->id = 'object::store:' . $params['storageid']; |
|
| 59 | - } else { |
|
| 60 | - $this->id = 'object::store:' . $this->objectStore->getStorageId(); |
|
| 61 | - } |
|
| 62 | - if (isset($params['objectPrefix'])) { |
|
| 63 | - $this->objectPrefix = $params['objectPrefix']; |
|
| 64 | - } |
|
| 65 | - //initialize cache with root directory in cache |
|
| 66 | - if (!$this->is_dir('/')) { |
|
| 67 | - $this->mkdir('/'); |
|
| 68 | - } |
|
| 69 | - |
|
| 70 | - $this->logger = \OC::$server->getLogger(); |
|
| 71 | - } |
|
| 72 | - |
|
| 73 | - public function mkdir($path) { |
|
| 74 | - $path = $this->normalizePath($path); |
|
| 75 | - |
|
| 76 | - if ($this->file_exists($path)) { |
|
| 77 | - return false; |
|
| 78 | - } |
|
| 79 | - |
|
| 80 | - $mTime = time(); |
|
| 81 | - $data = [ |
|
| 82 | - 'mimetype' => 'httpd/unix-directory', |
|
| 83 | - 'size' => 0, |
|
| 84 | - 'mtime' => $mTime, |
|
| 85 | - 'storage_mtime' => $mTime, |
|
| 86 | - 'permissions' => \OCP\Constants::PERMISSION_ALL, |
|
| 87 | - ]; |
|
| 88 | - if ($path === '') { |
|
| 89 | - //create root on the fly |
|
| 90 | - $data['etag'] = $this->getETag(''); |
|
| 91 | - $this->getCache()->put('', $data); |
|
| 92 | - return true; |
|
| 93 | - } else { |
|
| 94 | - // if parent does not exist, create it |
|
| 95 | - $parent = $this->normalizePath(dirname($path)); |
|
| 96 | - $parentType = $this->filetype($parent); |
|
| 97 | - if ($parentType === false) { |
|
| 98 | - if (!$this->mkdir($parent)) { |
|
| 99 | - // something went wrong |
|
| 100 | - return false; |
|
| 101 | - } |
|
| 102 | - } else if ($parentType === 'file') { |
|
| 103 | - // parent is a file |
|
| 104 | - return false; |
|
| 105 | - } |
|
| 106 | - // finally create the new dir |
|
| 107 | - $mTime = time(); // update mtime |
|
| 108 | - $data['mtime'] = $mTime; |
|
| 109 | - $data['storage_mtime'] = $mTime; |
|
| 110 | - $data['etag'] = $this->getETag($path); |
|
| 111 | - $this->getCache()->put($path, $data); |
|
| 112 | - return true; |
|
| 113 | - } |
|
| 114 | - } |
|
| 115 | - |
|
| 116 | - /** |
|
| 117 | - * @param string $path |
|
| 118 | - * @return string |
|
| 119 | - */ |
|
| 120 | - private function normalizePath($path) { |
|
| 121 | - $path = trim($path, '/'); |
|
| 122 | - //FIXME why do we sometimes get a path like 'files//username'? |
|
| 123 | - $path = str_replace('//', '/', $path); |
|
| 124 | - |
|
| 125 | - // dirname('/folder') returns '.' but internally (in the cache) we store the root as '' |
|
| 126 | - if (!$path || $path === '.') { |
|
| 127 | - $path = ''; |
|
| 128 | - } |
|
| 129 | - |
|
| 130 | - return $path; |
|
| 131 | - } |
|
| 132 | - |
|
| 133 | - /** |
|
| 134 | - * Object Stores use a NoopScanner because metadata is directly stored in |
|
| 135 | - * the file cache and cannot really scan the filesystem. The storage passed in is not used anywhere. |
|
| 136 | - * |
|
| 137 | - * @param string $path |
|
| 138 | - * @param \OC\Files\Storage\Storage (optional) the storage to pass to the scanner |
|
| 139 | - * @return \OC\Files\ObjectStore\NoopScanner |
|
| 140 | - */ |
|
| 141 | - public function getScanner($path = '', $storage = null) { |
|
| 142 | - if (!$storage) { |
|
| 143 | - $storage = $this; |
|
| 144 | - } |
|
| 145 | - if (!isset($this->scanner)) { |
|
| 146 | - $this->scanner = new NoopScanner($storage); |
|
| 147 | - } |
|
| 148 | - return $this->scanner; |
|
| 149 | - } |
|
| 150 | - |
|
| 151 | - public function getId() { |
|
| 152 | - return $this->id; |
|
| 153 | - } |
|
| 154 | - |
|
| 155 | - public function rmdir($path) { |
|
| 156 | - $path = $this->normalizePath($path); |
|
| 157 | - |
|
| 158 | - if (!$this->is_dir($path)) { |
|
| 159 | - return false; |
|
| 160 | - } |
|
| 161 | - |
|
| 162 | - $this->rmObjects($path); |
|
| 163 | - |
|
| 164 | - $this->getCache()->remove($path); |
|
| 165 | - |
|
| 166 | - return true; |
|
| 167 | - } |
|
| 168 | - |
|
| 169 | - private function rmObjects($path) { |
|
| 170 | - $children = $this->getCache()->getFolderContents($path); |
|
| 171 | - foreach ($children as $child) { |
|
| 172 | - if ($child['mimetype'] === 'httpd/unix-directory') { |
|
| 173 | - $this->rmObjects($child['path']); |
|
| 174 | - } else { |
|
| 175 | - $this->unlink($child['path']); |
|
| 176 | - } |
|
| 177 | - } |
|
| 178 | - } |
|
| 179 | - |
|
| 180 | - public function unlink($path) { |
|
| 181 | - $path = $this->normalizePath($path); |
|
| 182 | - $stat = $this->stat($path); |
|
| 183 | - |
|
| 184 | - if ($stat && isset($stat['fileid'])) { |
|
| 185 | - if ($stat['mimetype'] === 'httpd/unix-directory') { |
|
| 186 | - return $this->rmdir($path); |
|
| 187 | - } |
|
| 188 | - try { |
|
| 189 | - $this->objectStore->deleteObject($this->getURN($stat['fileid'])); |
|
| 190 | - } catch (\Exception $ex) { |
|
| 191 | - if ($ex->getCode() !== 404) { |
|
| 192 | - $this->logger->logException($ex, [ |
|
| 193 | - 'app' => 'objectstore', |
|
| 194 | - 'message' => 'Could not delete object ' . $this->getURN($stat['fileid']) . ' for ' . $path, |
|
| 195 | - ]); |
|
| 196 | - return false; |
|
| 197 | - } |
|
| 198 | - //removing from cache is ok as it does not exist in the objectstore anyway |
|
| 199 | - } |
|
| 200 | - $this->getCache()->remove($path); |
|
| 201 | - return true; |
|
| 202 | - } |
|
| 203 | - return false; |
|
| 204 | - } |
|
| 205 | - |
|
| 206 | - public function stat($path) { |
|
| 207 | - $path = $this->normalizePath($path); |
|
| 208 | - $cacheEntry = $this->getCache()->get($path); |
|
| 209 | - if ($cacheEntry instanceof CacheEntry) { |
|
| 210 | - return $cacheEntry->getData(); |
|
| 211 | - } else { |
|
| 212 | - return false; |
|
| 213 | - } |
|
| 214 | - } |
|
| 215 | - |
|
| 216 | - /** |
|
| 217 | - * Override this method if you need a different unique resource identifier for your object storage implementation. |
|
| 218 | - * The default implementations just appends the fileId to 'urn:oid:'. Make sure the URN is unique over all users. |
|
| 219 | - * You may need a mapping table to store your URN if it cannot be generated from the fileid. |
|
| 220 | - * |
|
| 221 | - * @param int $fileId the fileid |
|
| 222 | - * @return null|string the unified resource name used to identify the object |
|
| 223 | - */ |
|
| 224 | - protected function getURN($fileId) { |
|
| 225 | - if (is_numeric($fileId)) { |
|
| 226 | - return $this->objectPrefix . $fileId; |
|
| 227 | - } |
|
| 228 | - return null; |
|
| 229 | - } |
|
| 230 | - |
|
| 231 | - public function opendir($path) { |
|
| 232 | - $path = $this->normalizePath($path); |
|
| 233 | - |
|
| 234 | - try { |
|
| 235 | - $files = array(); |
|
| 236 | - $folderContents = $this->getCache()->getFolderContents($path); |
|
| 237 | - foreach ($folderContents as $file) { |
|
| 238 | - $files[] = $file['name']; |
|
| 239 | - } |
|
| 240 | - |
|
| 241 | - return IteratorDirectory::wrap($files); |
|
| 242 | - } catch (\Exception $e) { |
|
| 243 | - $this->logger->logException($e); |
|
| 244 | - return false; |
|
| 245 | - } |
|
| 246 | - } |
|
| 247 | - |
|
| 248 | - public function filetype($path) { |
|
| 249 | - $path = $this->normalizePath($path); |
|
| 250 | - $stat = $this->stat($path); |
|
| 251 | - if ($stat) { |
|
| 252 | - if ($stat['mimetype'] === 'httpd/unix-directory') { |
|
| 253 | - return 'dir'; |
|
| 254 | - } |
|
| 255 | - return 'file'; |
|
| 256 | - } else { |
|
| 257 | - return false; |
|
| 258 | - } |
|
| 259 | - } |
|
| 260 | - |
|
| 261 | - public function fopen($path, $mode) { |
|
| 262 | - $path = $this->normalizePath($path); |
|
| 263 | - |
|
| 264 | - if (strrpos($path, '.') !== false) { |
|
| 265 | - $ext = substr($path, strrpos($path, '.')); |
|
| 266 | - } else { |
|
| 267 | - $ext = ''; |
|
| 268 | - } |
|
| 269 | - |
|
| 270 | - switch ($mode) { |
|
| 271 | - case 'r': |
|
| 272 | - case 'rb': |
|
| 273 | - $stat = $this->stat($path); |
|
| 274 | - if (is_array($stat)) { |
|
| 275 | - try { |
|
| 276 | - return $this->objectStore->readObject($this->getURN($stat['fileid'])); |
|
| 277 | - } catch (\Exception $ex) { |
|
| 278 | - $this->logger->logException($ex, [ |
|
| 279 | - 'app' => 'objectstore', |
|
| 280 | - 'message' => 'Count not get object ' . $this->getURN($stat['fileid']) . ' for file ' . $path, |
|
| 281 | - ]); |
|
| 282 | - return false; |
|
| 283 | - } |
|
| 284 | - } else { |
|
| 285 | - return false; |
|
| 286 | - } |
|
| 287 | - case 'w': |
|
| 288 | - case 'wb': |
|
| 289 | - case 'w+': |
|
| 290 | - case 'wb+': |
|
| 291 | - $tmpFile = \OC::$server->getTempManager()->getTemporaryFile($ext); |
|
| 292 | - $handle = fopen($tmpFile, $mode); |
|
| 293 | - return CallbackWrapper::wrap($handle, null, null, function () use ($path, $tmpFile) { |
|
| 294 | - $this->writeBack($tmpFile, $path); |
|
| 295 | - }); |
|
| 296 | - case 'a': |
|
| 297 | - case 'ab': |
|
| 298 | - case 'r+': |
|
| 299 | - case 'a+': |
|
| 300 | - case 'x': |
|
| 301 | - case 'x+': |
|
| 302 | - case 'c': |
|
| 303 | - case 'c+': |
|
| 304 | - $tmpFile = \OC::$server->getTempManager()->getTemporaryFile($ext); |
|
| 305 | - if ($this->file_exists($path)) { |
|
| 306 | - $source = $this->fopen($path, 'r'); |
|
| 307 | - file_put_contents($tmpFile, $source); |
|
| 308 | - } |
|
| 309 | - $handle = fopen($tmpFile, $mode); |
|
| 310 | - return CallbackWrapper::wrap($handle, null, null, function () use ($path, $tmpFile) { |
|
| 311 | - $this->writeBack($tmpFile, $path); |
|
| 312 | - }); |
|
| 313 | - } |
|
| 314 | - return false; |
|
| 315 | - } |
|
| 316 | - |
|
| 317 | - public function file_exists($path) { |
|
| 318 | - $path = $this->normalizePath($path); |
|
| 319 | - return (bool)$this->stat($path); |
|
| 320 | - } |
|
| 321 | - |
|
| 322 | - public function rename($source, $target) { |
|
| 323 | - $source = $this->normalizePath($source); |
|
| 324 | - $target = $this->normalizePath($target); |
|
| 325 | - $this->remove($target); |
|
| 326 | - $this->getCache()->move($source, $target); |
|
| 327 | - $this->touch(dirname($target)); |
|
| 328 | - return true; |
|
| 329 | - } |
|
| 330 | - |
|
| 331 | - public function getMimeType($path) { |
|
| 332 | - $path = $this->normalizePath($path); |
|
| 333 | - $stat = $this->stat($path); |
|
| 334 | - if (is_array($stat)) { |
|
| 335 | - return $stat['mimetype']; |
|
| 336 | - } else { |
|
| 337 | - return false; |
|
| 338 | - } |
|
| 339 | - } |
|
| 340 | - |
|
| 341 | - public function touch($path, $mtime = null) { |
|
| 342 | - if (is_null($mtime)) { |
|
| 343 | - $mtime = time(); |
|
| 344 | - } |
|
| 345 | - |
|
| 346 | - $path = $this->normalizePath($path); |
|
| 347 | - $dirName = dirname($path); |
|
| 348 | - $parentExists = $this->is_dir($dirName); |
|
| 349 | - if (!$parentExists) { |
|
| 350 | - return false; |
|
| 351 | - } |
|
| 352 | - |
|
| 353 | - $stat = $this->stat($path); |
|
| 354 | - if (is_array($stat)) { |
|
| 355 | - // update existing mtime in db |
|
| 356 | - $stat['mtime'] = $mtime; |
|
| 357 | - $this->getCache()->update($stat['fileid'], $stat); |
|
| 358 | - } else { |
|
| 359 | - try { |
|
| 360 | - //create a empty file, need to have at least on char to make it |
|
| 361 | - // work with all object storage implementations |
|
| 362 | - $this->file_put_contents($path, ' '); |
|
| 363 | - $mimeType = \OC::$server->getMimeTypeDetector()->detectPath($path); |
|
| 364 | - $stat = array( |
|
| 365 | - 'etag' => $this->getETag($path), |
|
| 366 | - 'mimetype' => $mimeType, |
|
| 367 | - 'size' => 0, |
|
| 368 | - 'mtime' => $mtime, |
|
| 369 | - 'storage_mtime' => $mtime, |
|
| 370 | - 'permissions' => \OCP\Constants::PERMISSION_ALL - \OCP\Constants::PERMISSION_CREATE, |
|
| 371 | - ); |
|
| 372 | - $this->getCache()->put($path, $stat); |
|
| 373 | - } catch (\Exception $ex) { |
|
| 374 | - $this->logger->logException($ex, [ |
|
| 375 | - 'app' => 'objectstore', |
|
| 376 | - 'message' => 'Could not create object for ' . $path, |
|
| 377 | - ]); |
|
| 378 | - throw $ex; |
|
| 379 | - } |
|
| 380 | - } |
|
| 381 | - return true; |
|
| 382 | - } |
|
| 383 | - |
|
| 384 | - public function writeBack($tmpFile, $path) { |
|
| 385 | - $stat = $this->stat($path); |
|
| 386 | - if (empty($stat)) { |
|
| 387 | - // create new file |
|
| 388 | - $stat = array( |
|
| 389 | - 'permissions' => \OCP\Constants::PERMISSION_ALL - \OCP\Constants::PERMISSION_CREATE, |
|
| 390 | - ); |
|
| 391 | - } |
|
| 392 | - // update stat with new data |
|
| 393 | - $mTime = time(); |
|
| 394 | - $stat['size'] = filesize($tmpFile); |
|
| 395 | - $stat['mtime'] = $mTime; |
|
| 396 | - $stat['storage_mtime'] = $mTime; |
|
| 397 | - |
|
| 398 | - // run path based detection first, to use file extension because $tmpFile is only a random string |
|
| 399 | - $mimetypeDetector = \OC::$server->getMimeTypeDetector(); |
|
| 400 | - $mimetype = $mimetypeDetector->detectPath($path); |
|
| 401 | - if ($mimetype === 'application/octet-stream') { |
|
| 402 | - $mimetype = $mimetypeDetector->detect($tmpFile); |
|
| 403 | - } |
|
| 404 | - |
|
| 405 | - $stat['mimetype'] = $mimetype; |
|
| 406 | - $stat['etag'] = $this->getETag($path); |
|
| 407 | - |
|
| 408 | - $fileId = $this->getCache()->put($path, $stat); |
|
| 409 | - try { |
|
| 410 | - //upload to object storage |
|
| 411 | - $this->objectStore->writeObject($this->getURN($fileId), fopen($tmpFile, 'r')); |
|
| 412 | - } catch (\Exception $ex) { |
|
| 413 | - $this->getCache()->remove($path); |
|
| 414 | - $this->logger->logException($ex, [ |
|
| 415 | - 'app' => 'objectstore', |
|
| 416 | - 'message' => 'Could not create object ' . $this->getURN($fileId) . ' for ' . $path, |
|
| 417 | - ]); |
|
| 418 | - throw $ex; // make this bubble up |
|
| 419 | - } |
|
| 420 | - } |
|
| 421 | - |
|
| 422 | - /** |
|
| 423 | - * external changes are not supported, exclusive access to the object storage is assumed |
|
| 424 | - * |
|
| 425 | - * @param string $path |
|
| 426 | - * @param int $time |
|
| 427 | - * @return false |
|
| 428 | - */ |
|
| 429 | - public function hasUpdated($path, $time) { |
|
| 430 | - return false; |
|
| 431 | - } |
|
| 432 | - |
|
| 433 | - public function needsPartFile() { |
|
| 434 | - return false; |
|
| 435 | - } |
|
| 34 | + /** |
|
| 35 | + * @var \OCP\Files\ObjectStore\IObjectStore $objectStore |
|
| 36 | + */ |
|
| 37 | + protected $objectStore; |
|
| 38 | + /** |
|
| 39 | + * @var string $id |
|
| 40 | + */ |
|
| 41 | + protected $id; |
|
| 42 | + /** |
|
| 43 | + * @var \OC\User\User $user |
|
| 44 | + */ |
|
| 45 | + protected $user; |
|
| 46 | + |
|
| 47 | + private $objectPrefix = 'urn:oid:'; |
|
| 48 | + |
|
| 49 | + private $logger; |
|
| 50 | + |
|
| 51 | + public function __construct($params) { |
|
| 52 | + if (isset($params['objectstore']) && $params['objectstore'] instanceof IObjectStore) { |
|
| 53 | + $this->objectStore = $params['objectstore']; |
|
| 54 | + } else { |
|
| 55 | + throw new \Exception('missing IObjectStore instance'); |
|
| 56 | + } |
|
| 57 | + if (isset($params['storageid'])) { |
|
| 58 | + $this->id = 'object::store:' . $params['storageid']; |
|
| 59 | + } else { |
|
| 60 | + $this->id = 'object::store:' . $this->objectStore->getStorageId(); |
|
| 61 | + } |
|
| 62 | + if (isset($params['objectPrefix'])) { |
|
| 63 | + $this->objectPrefix = $params['objectPrefix']; |
|
| 64 | + } |
|
| 65 | + //initialize cache with root directory in cache |
|
| 66 | + if (!$this->is_dir('/')) { |
|
| 67 | + $this->mkdir('/'); |
|
| 68 | + } |
|
| 69 | + |
|
| 70 | + $this->logger = \OC::$server->getLogger(); |
|
| 71 | + } |
|
| 72 | + |
|
| 73 | + public function mkdir($path) { |
|
| 74 | + $path = $this->normalizePath($path); |
|
| 75 | + |
|
| 76 | + if ($this->file_exists($path)) { |
|
| 77 | + return false; |
|
| 78 | + } |
|
| 79 | + |
|
| 80 | + $mTime = time(); |
|
| 81 | + $data = [ |
|
| 82 | + 'mimetype' => 'httpd/unix-directory', |
|
| 83 | + 'size' => 0, |
|
| 84 | + 'mtime' => $mTime, |
|
| 85 | + 'storage_mtime' => $mTime, |
|
| 86 | + 'permissions' => \OCP\Constants::PERMISSION_ALL, |
|
| 87 | + ]; |
|
| 88 | + if ($path === '') { |
|
| 89 | + //create root on the fly |
|
| 90 | + $data['etag'] = $this->getETag(''); |
|
| 91 | + $this->getCache()->put('', $data); |
|
| 92 | + return true; |
|
| 93 | + } else { |
|
| 94 | + // if parent does not exist, create it |
|
| 95 | + $parent = $this->normalizePath(dirname($path)); |
|
| 96 | + $parentType = $this->filetype($parent); |
|
| 97 | + if ($parentType === false) { |
|
| 98 | + if (!$this->mkdir($parent)) { |
|
| 99 | + // something went wrong |
|
| 100 | + return false; |
|
| 101 | + } |
|
| 102 | + } else if ($parentType === 'file') { |
|
| 103 | + // parent is a file |
|
| 104 | + return false; |
|
| 105 | + } |
|
| 106 | + // finally create the new dir |
|
| 107 | + $mTime = time(); // update mtime |
|
| 108 | + $data['mtime'] = $mTime; |
|
| 109 | + $data['storage_mtime'] = $mTime; |
|
| 110 | + $data['etag'] = $this->getETag($path); |
|
| 111 | + $this->getCache()->put($path, $data); |
|
| 112 | + return true; |
|
| 113 | + } |
|
| 114 | + } |
|
| 115 | + |
|
| 116 | + /** |
|
| 117 | + * @param string $path |
|
| 118 | + * @return string |
|
| 119 | + */ |
|
| 120 | + private function normalizePath($path) { |
|
| 121 | + $path = trim($path, '/'); |
|
| 122 | + //FIXME why do we sometimes get a path like 'files//username'? |
|
| 123 | + $path = str_replace('//', '/', $path); |
|
| 124 | + |
|
| 125 | + // dirname('/folder') returns '.' but internally (in the cache) we store the root as '' |
|
| 126 | + if (!$path || $path === '.') { |
|
| 127 | + $path = ''; |
|
| 128 | + } |
|
| 129 | + |
|
| 130 | + return $path; |
|
| 131 | + } |
|
| 132 | + |
|
| 133 | + /** |
|
| 134 | + * Object Stores use a NoopScanner because metadata is directly stored in |
|
| 135 | + * the file cache and cannot really scan the filesystem. The storage passed in is not used anywhere. |
|
| 136 | + * |
|
| 137 | + * @param string $path |
|
| 138 | + * @param \OC\Files\Storage\Storage (optional) the storage to pass to the scanner |
|
| 139 | + * @return \OC\Files\ObjectStore\NoopScanner |
|
| 140 | + */ |
|
| 141 | + public function getScanner($path = '', $storage = null) { |
|
| 142 | + if (!$storage) { |
|
| 143 | + $storage = $this; |
|
| 144 | + } |
|
| 145 | + if (!isset($this->scanner)) { |
|
| 146 | + $this->scanner = new NoopScanner($storage); |
|
| 147 | + } |
|
| 148 | + return $this->scanner; |
|
| 149 | + } |
|
| 150 | + |
|
| 151 | + public function getId() { |
|
| 152 | + return $this->id; |
|
| 153 | + } |
|
| 154 | + |
|
| 155 | + public function rmdir($path) { |
|
| 156 | + $path = $this->normalizePath($path); |
|
| 157 | + |
|
| 158 | + if (!$this->is_dir($path)) { |
|
| 159 | + return false; |
|
| 160 | + } |
|
| 161 | + |
|
| 162 | + $this->rmObjects($path); |
|
| 163 | + |
|
| 164 | + $this->getCache()->remove($path); |
|
| 165 | + |
|
| 166 | + return true; |
|
| 167 | + } |
|
| 168 | + |
|
| 169 | + private function rmObjects($path) { |
|
| 170 | + $children = $this->getCache()->getFolderContents($path); |
|
| 171 | + foreach ($children as $child) { |
|
| 172 | + if ($child['mimetype'] === 'httpd/unix-directory') { |
|
| 173 | + $this->rmObjects($child['path']); |
|
| 174 | + } else { |
|
| 175 | + $this->unlink($child['path']); |
|
| 176 | + } |
|
| 177 | + } |
|
| 178 | + } |
|
| 179 | + |
|
| 180 | + public function unlink($path) { |
|
| 181 | + $path = $this->normalizePath($path); |
|
| 182 | + $stat = $this->stat($path); |
|
| 183 | + |
|
| 184 | + if ($stat && isset($stat['fileid'])) { |
|
| 185 | + if ($stat['mimetype'] === 'httpd/unix-directory') { |
|
| 186 | + return $this->rmdir($path); |
|
| 187 | + } |
|
| 188 | + try { |
|
| 189 | + $this->objectStore->deleteObject($this->getURN($stat['fileid'])); |
|
| 190 | + } catch (\Exception $ex) { |
|
| 191 | + if ($ex->getCode() !== 404) { |
|
| 192 | + $this->logger->logException($ex, [ |
|
| 193 | + 'app' => 'objectstore', |
|
| 194 | + 'message' => 'Could not delete object ' . $this->getURN($stat['fileid']) . ' for ' . $path, |
|
| 195 | + ]); |
|
| 196 | + return false; |
|
| 197 | + } |
|
| 198 | + //removing from cache is ok as it does not exist in the objectstore anyway |
|
| 199 | + } |
|
| 200 | + $this->getCache()->remove($path); |
|
| 201 | + return true; |
|
| 202 | + } |
|
| 203 | + return false; |
|
| 204 | + } |
|
| 205 | + |
|
| 206 | + public function stat($path) { |
|
| 207 | + $path = $this->normalizePath($path); |
|
| 208 | + $cacheEntry = $this->getCache()->get($path); |
|
| 209 | + if ($cacheEntry instanceof CacheEntry) { |
|
| 210 | + return $cacheEntry->getData(); |
|
| 211 | + } else { |
|
| 212 | + return false; |
|
| 213 | + } |
|
| 214 | + } |
|
| 215 | + |
|
| 216 | + /** |
|
| 217 | + * Override this method if you need a different unique resource identifier for your object storage implementation. |
|
| 218 | + * The default implementations just appends the fileId to 'urn:oid:'. Make sure the URN is unique over all users. |
|
| 219 | + * You may need a mapping table to store your URN if it cannot be generated from the fileid. |
|
| 220 | + * |
|
| 221 | + * @param int $fileId the fileid |
|
| 222 | + * @return null|string the unified resource name used to identify the object |
|
| 223 | + */ |
|
| 224 | + protected function getURN($fileId) { |
|
| 225 | + if (is_numeric($fileId)) { |
|
| 226 | + return $this->objectPrefix . $fileId; |
|
| 227 | + } |
|
| 228 | + return null; |
|
| 229 | + } |
|
| 230 | + |
|
| 231 | + public function opendir($path) { |
|
| 232 | + $path = $this->normalizePath($path); |
|
| 233 | + |
|
| 234 | + try { |
|
| 235 | + $files = array(); |
|
| 236 | + $folderContents = $this->getCache()->getFolderContents($path); |
|
| 237 | + foreach ($folderContents as $file) { |
|
| 238 | + $files[] = $file['name']; |
|
| 239 | + } |
|
| 240 | + |
|
| 241 | + return IteratorDirectory::wrap($files); |
|
| 242 | + } catch (\Exception $e) { |
|
| 243 | + $this->logger->logException($e); |
|
| 244 | + return false; |
|
| 245 | + } |
|
| 246 | + } |
|
| 247 | + |
|
| 248 | + public function filetype($path) { |
|
| 249 | + $path = $this->normalizePath($path); |
|
| 250 | + $stat = $this->stat($path); |
|
| 251 | + if ($stat) { |
|
| 252 | + if ($stat['mimetype'] === 'httpd/unix-directory') { |
|
| 253 | + return 'dir'; |
|
| 254 | + } |
|
| 255 | + return 'file'; |
|
| 256 | + } else { |
|
| 257 | + return false; |
|
| 258 | + } |
|
| 259 | + } |
|
| 260 | + |
|
| 261 | + public function fopen($path, $mode) { |
|
| 262 | + $path = $this->normalizePath($path); |
|
| 263 | + |
|
| 264 | + if (strrpos($path, '.') !== false) { |
|
| 265 | + $ext = substr($path, strrpos($path, '.')); |
|
| 266 | + } else { |
|
| 267 | + $ext = ''; |
|
| 268 | + } |
|
| 269 | + |
|
| 270 | + switch ($mode) { |
|
| 271 | + case 'r': |
|
| 272 | + case 'rb': |
|
| 273 | + $stat = $this->stat($path); |
|
| 274 | + if (is_array($stat)) { |
|
| 275 | + try { |
|
| 276 | + return $this->objectStore->readObject($this->getURN($stat['fileid'])); |
|
| 277 | + } catch (\Exception $ex) { |
|
| 278 | + $this->logger->logException($ex, [ |
|
| 279 | + 'app' => 'objectstore', |
|
| 280 | + 'message' => 'Count not get object ' . $this->getURN($stat['fileid']) . ' for file ' . $path, |
|
| 281 | + ]); |
|
| 282 | + return false; |
|
| 283 | + } |
|
| 284 | + } else { |
|
| 285 | + return false; |
|
| 286 | + } |
|
| 287 | + case 'w': |
|
| 288 | + case 'wb': |
|
| 289 | + case 'w+': |
|
| 290 | + case 'wb+': |
|
| 291 | + $tmpFile = \OC::$server->getTempManager()->getTemporaryFile($ext); |
|
| 292 | + $handle = fopen($tmpFile, $mode); |
|
| 293 | + return CallbackWrapper::wrap($handle, null, null, function () use ($path, $tmpFile) { |
|
| 294 | + $this->writeBack($tmpFile, $path); |
|
| 295 | + }); |
|
| 296 | + case 'a': |
|
| 297 | + case 'ab': |
|
| 298 | + case 'r+': |
|
| 299 | + case 'a+': |
|
| 300 | + case 'x': |
|
| 301 | + case 'x+': |
|
| 302 | + case 'c': |
|
| 303 | + case 'c+': |
|
| 304 | + $tmpFile = \OC::$server->getTempManager()->getTemporaryFile($ext); |
|
| 305 | + if ($this->file_exists($path)) { |
|
| 306 | + $source = $this->fopen($path, 'r'); |
|
| 307 | + file_put_contents($tmpFile, $source); |
|
| 308 | + } |
|
| 309 | + $handle = fopen($tmpFile, $mode); |
|
| 310 | + return CallbackWrapper::wrap($handle, null, null, function () use ($path, $tmpFile) { |
|
| 311 | + $this->writeBack($tmpFile, $path); |
|
| 312 | + }); |
|
| 313 | + } |
|
| 314 | + return false; |
|
| 315 | + } |
|
| 316 | + |
|
| 317 | + public function file_exists($path) { |
|
| 318 | + $path = $this->normalizePath($path); |
|
| 319 | + return (bool)$this->stat($path); |
|
| 320 | + } |
|
| 321 | + |
|
| 322 | + public function rename($source, $target) { |
|
| 323 | + $source = $this->normalizePath($source); |
|
| 324 | + $target = $this->normalizePath($target); |
|
| 325 | + $this->remove($target); |
|
| 326 | + $this->getCache()->move($source, $target); |
|
| 327 | + $this->touch(dirname($target)); |
|
| 328 | + return true; |
|
| 329 | + } |
|
| 330 | + |
|
| 331 | + public function getMimeType($path) { |
|
| 332 | + $path = $this->normalizePath($path); |
|
| 333 | + $stat = $this->stat($path); |
|
| 334 | + if (is_array($stat)) { |
|
| 335 | + return $stat['mimetype']; |
|
| 336 | + } else { |
|
| 337 | + return false; |
|
| 338 | + } |
|
| 339 | + } |
|
| 340 | + |
|
| 341 | + public function touch($path, $mtime = null) { |
|
| 342 | + if (is_null($mtime)) { |
|
| 343 | + $mtime = time(); |
|
| 344 | + } |
|
| 345 | + |
|
| 346 | + $path = $this->normalizePath($path); |
|
| 347 | + $dirName = dirname($path); |
|
| 348 | + $parentExists = $this->is_dir($dirName); |
|
| 349 | + if (!$parentExists) { |
|
| 350 | + return false; |
|
| 351 | + } |
|
| 352 | + |
|
| 353 | + $stat = $this->stat($path); |
|
| 354 | + if (is_array($stat)) { |
|
| 355 | + // update existing mtime in db |
|
| 356 | + $stat['mtime'] = $mtime; |
|
| 357 | + $this->getCache()->update($stat['fileid'], $stat); |
|
| 358 | + } else { |
|
| 359 | + try { |
|
| 360 | + //create a empty file, need to have at least on char to make it |
|
| 361 | + // work with all object storage implementations |
|
| 362 | + $this->file_put_contents($path, ' '); |
|
| 363 | + $mimeType = \OC::$server->getMimeTypeDetector()->detectPath($path); |
|
| 364 | + $stat = array( |
|
| 365 | + 'etag' => $this->getETag($path), |
|
| 366 | + 'mimetype' => $mimeType, |
|
| 367 | + 'size' => 0, |
|
| 368 | + 'mtime' => $mtime, |
|
| 369 | + 'storage_mtime' => $mtime, |
|
| 370 | + 'permissions' => \OCP\Constants::PERMISSION_ALL - \OCP\Constants::PERMISSION_CREATE, |
|
| 371 | + ); |
|
| 372 | + $this->getCache()->put($path, $stat); |
|
| 373 | + } catch (\Exception $ex) { |
|
| 374 | + $this->logger->logException($ex, [ |
|
| 375 | + 'app' => 'objectstore', |
|
| 376 | + 'message' => 'Could not create object for ' . $path, |
|
| 377 | + ]); |
|
| 378 | + throw $ex; |
|
| 379 | + } |
|
| 380 | + } |
|
| 381 | + return true; |
|
| 382 | + } |
|
| 383 | + |
|
| 384 | + public function writeBack($tmpFile, $path) { |
|
| 385 | + $stat = $this->stat($path); |
|
| 386 | + if (empty($stat)) { |
|
| 387 | + // create new file |
|
| 388 | + $stat = array( |
|
| 389 | + 'permissions' => \OCP\Constants::PERMISSION_ALL - \OCP\Constants::PERMISSION_CREATE, |
|
| 390 | + ); |
|
| 391 | + } |
|
| 392 | + // update stat with new data |
|
| 393 | + $mTime = time(); |
|
| 394 | + $stat['size'] = filesize($tmpFile); |
|
| 395 | + $stat['mtime'] = $mTime; |
|
| 396 | + $stat['storage_mtime'] = $mTime; |
|
| 397 | + |
|
| 398 | + // run path based detection first, to use file extension because $tmpFile is only a random string |
|
| 399 | + $mimetypeDetector = \OC::$server->getMimeTypeDetector(); |
|
| 400 | + $mimetype = $mimetypeDetector->detectPath($path); |
|
| 401 | + if ($mimetype === 'application/octet-stream') { |
|
| 402 | + $mimetype = $mimetypeDetector->detect($tmpFile); |
|
| 403 | + } |
|
| 404 | + |
|
| 405 | + $stat['mimetype'] = $mimetype; |
|
| 406 | + $stat['etag'] = $this->getETag($path); |
|
| 407 | + |
|
| 408 | + $fileId = $this->getCache()->put($path, $stat); |
|
| 409 | + try { |
|
| 410 | + //upload to object storage |
|
| 411 | + $this->objectStore->writeObject($this->getURN($fileId), fopen($tmpFile, 'r')); |
|
| 412 | + } catch (\Exception $ex) { |
|
| 413 | + $this->getCache()->remove($path); |
|
| 414 | + $this->logger->logException($ex, [ |
|
| 415 | + 'app' => 'objectstore', |
|
| 416 | + 'message' => 'Could not create object ' . $this->getURN($fileId) . ' for ' . $path, |
|
| 417 | + ]); |
|
| 418 | + throw $ex; // make this bubble up |
|
| 419 | + } |
|
| 420 | + } |
|
| 421 | + |
|
| 422 | + /** |
|
| 423 | + * external changes are not supported, exclusive access to the object storage is assumed |
|
| 424 | + * |
|
| 425 | + * @param string $path |
|
| 426 | + * @param int $time |
|
| 427 | + * @return false |
|
| 428 | + */ |
|
| 429 | + public function hasUpdated($path, $time) { |
|
| 430 | + return false; |
|
| 431 | + } |
|
| 432 | + |
|
| 433 | + public function needsPartFile() { |
|
| 434 | + return false; |
|
| 435 | + } |
|
| 436 | 436 | } |
@@ -102,7 +102,7 @@ |
||
| 102 | 102 | |
| 103 | 103 | /** |
| 104 | 104 | * @param \Icewind\SMB\Change $change |
| 105 | - * @return IChange|null |
|
| 105 | + * @return null|Change |
|
| 106 | 106 | */ |
| 107 | 107 | private function mapChange(\Icewind\SMB\Change $change) { |
| 108 | 108 | $path = $this->relativePath($change->getPath()); |
@@ -29,122 +29,122 @@ |
||
| 29 | 29 | use OCP\Files\Notify\INotifyHandler; |
| 30 | 30 | |
| 31 | 31 | class SMBNotifyHandler implements INotifyHandler { |
| 32 | - /** |
|
| 33 | - * @var \Icewind\SMB\INotifyHandler |
|
| 34 | - */ |
|
| 35 | - private $shareNotifyHandler; |
|
| 32 | + /** |
|
| 33 | + * @var \Icewind\SMB\INotifyHandler |
|
| 34 | + */ |
|
| 35 | + private $shareNotifyHandler; |
|
| 36 | 36 | |
| 37 | - /** |
|
| 38 | - * @var string |
|
| 39 | - */ |
|
| 40 | - private $root; |
|
| 37 | + /** |
|
| 38 | + * @var string |
|
| 39 | + */ |
|
| 40 | + private $root; |
|
| 41 | 41 | |
| 42 | - private $oldRenamePath = null; |
|
| 42 | + private $oldRenamePath = null; |
|
| 43 | 43 | |
| 44 | - /** |
|
| 45 | - * SMBNotifyHandler constructor. |
|
| 46 | - * |
|
| 47 | - * @param \Icewind\SMB\INotifyHandler $shareNotifyHandler |
|
| 48 | - * @param string $root |
|
| 49 | - */ |
|
| 50 | - public function __construct(\Icewind\SMB\INotifyHandler $shareNotifyHandler, $root) { |
|
| 51 | - $this->shareNotifyHandler = $shareNotifyHandler; |
|
| 52 | - $this->root = $root; |
|
| 53 | - } |
|
| 44 | + /** |
|
| 45 | + * SMBNotifyHandler constructor. |
|
| 46 | + * |
|
| 47 | + * @param \Icewind\SMB\INotifyHandler $shareNotifyHandler |
|
| 48 | + * @param string $root |
|
| 49 | + */ |
|
| 50 | + public function __construct(\Icewind\SMB\INotifyHandler $shareNotifyHandler, $root) { |
|
| 51 | + $this->shareNotifyHandler = $shareNotifyHandler; |
|
| 52 | + $this->root = $root; |
|
| 53 | + } |
|
| 54 | 54 | |
| 55 | - private function relativePath($fullPath) { |
|
| 56 | - if ($fullPath === $this->root) { |
|
| 57 | - return ''; |
|
| 58 | - } else if (substr($fullPath, 0, strlen($this->root)) === $this->root) { |
|
| 59 | - return substr($fullPath, strlen($this->root)); |
|
| 60 | - } else { |
|
| 61 | - return null; |
|
| 62 | - } |
|
| 63 | - } |
|
| 55 | + private function relativePath($fullPath) { |
|
| 56 | + if ($fullPath === $this->root) { |
|
| 57 | + return ''; |
|
| 58 | + } else if (substr($fullPath, 0, strlen($this->root)) === $this->root) { |
|
| 59 | + return substr($fullPath, strlen($this->root)); |
|
| 60 | + } else { |
|
| 61 | + return null; |
|
| 62 | + } |
|
| 63 | + } |
|
| 64 | 64 | |
| 65 | - public function listen(callable $callback) { |
|
| 66 | - $oldRenamePath = null; |
|
| 67 | - $this->shareNotifyHandler->listen(function (\Icewind\SMB\Change $shareChange) use ($callback) { |
|
| 68 | - $change = $this->mapChange($shareChange); |
|
| 69 | - if (!is_null($change)) { |
|
| 70 | - return $callback($change); |
|
| 71 | - } else { |
|
| 72 | - return true; |
|
| 73 | - } |
|
| 74 | - }); |
|
| 75 | - } |
|
| 65 | + public function listen(callable $callback) { |
|
| 66 | + $oldRenamePath = null; |
|
| 67 | + $this->shareNotifyHandler->listen(function (\Icewind\SMB\Change $shareChange) use ($callback) { |
|
| 68 | + $change = $this->mapChange($shareChange); |
|
| 69 | + if (!is_null($change)) { |
|
| 70 | + return $callback($change); |
|
| 71 | + } else { |
|
| 72 | + return true; |
|
| 73 | + } |
|
| 74 | + }); |
|
| 75 | + } |
|
| 76 | 76 | |
| 77 | - /** |
|
| 78 | - * Get all changes detected since the start of the notify process or the last call to getChanges |
|
| 79 | - * |
|
| 80 | - * @return IChange[] |
|
| 81 | - */ |
|
| 82 | - public function getChanges() { |
|
| 83 | - $shareChanges = $this->shareNotifyHandler->getChanges(); |
|
| 84 | - $changes = []; |
|
| 85 | - foreach ($shareChanges as $shareChange) { |
|
| 86 | - $change = $this->mapChange($shareChange); |
|
| 87 | - if ($change) { |
|
| 88 | - $changes[] = $change; |
|
| 89 | - } |
|
| 90 | - } |
|
| 91 | - return $changes; |
|
| 92 | - } |
|
| 77 | + /** |
|
| 78 | + * Get all changes detected since the start of the notify process or the last call to getChanges |
|
| 79 | + * |
|
| 80 | + * @return IChange[] |
|
| 81 | + */ |
|
| 82 | + public function getChanges() { |
|
| 83 | + $shareChanges = $this->shareNotifyHandler->getChanges(); |
|
| 84 | + $changes = []; |
|
| 85 | + foreach ($shareChanges as $shareChange) { |
|
| 86 | + $change = $this->mapChange($shareChange); |
|
| 87 | + if ($change) { |
|
| 88 | + $changes[] = $change; |
|
| 89 | + } |
|
| 90 | + } |
|
| 91 | + return $changes; |
|
| 92 | + } |
|
| 93 | 93 | |
| 94 | - /** |
|
| 95 | - * Stop listening for changes |
|
| 96 | - * |
|
| 97 | - * Note that any pending changes will be discarded |
|
| 98 | - */ |
|
| 99 | - public function stop() { |
|
| 100 | - $this->shareNotifyHandler->stop(); |
|
| 101 | - } |
|
| 94 | + /** |
|
| 95 | + * Stop listening for changes |
|
| 96 | + * |
|
| 97 | + * Note that any pending changes will be discarded |
|
| 98 | + */ |
|
| 99 | + public function stop() { |
|
| 100 | + $this->shareNotifyHandler->stop(); |
|
| 101 | + } |
|
| 102 | 102 | |
| 103 | - /** |
|
| 104 | - * @param \Icewind\SMB\Change $change |
|
| 105 | - * @return IChange|null |
|
| 106 | - */ |
|
| 107 | - private function mapChange(\Icewind\SMB\Change $change) { |
|
| 108 | - $path = $this->relativePath($change->getPath()); |
|
| 109 | - if (is_null($path)) { |
|
| 110 | - return null; |
|
| 111 | - } |
|
| 112 | - if ($change->getCode() === \Icewind\SMB\INotifyHandler::NOTIFY_RENAMED_OLD) { |
|
| 113 | - $this->oldRenamePath = $path; |
|
| 114 | - return null; |
|
| 115 | - } |
|
| 116 | - $type = $this->mapNotifyType($change->getCode()); |
|
| 117 | - if (is_null($type)) { |
|
| 118 | - return null; |
|
| 119 | - } |
|
| 120 | - if ($type === IChange::RENAMED) { |
|
| 121 | - if (!is_null($this->oldRenamePath)) { |
|
| 122 | - $result = new RenameChange($type, $this->oldRenamePath, $path); |
|
| 123 | - $this->oldRenamePath = null; |
|
| 124 | - } else { |
|
| 125 | - $result = null; |
|
| 126 | - } |
|
| 127 | - } else { |
|
| 128 | - $result = new Change($type, $path); |
|
| 129 | - } |
|
| 130 | - return $result; |
|
| 131 | - } |
|
| 103 | + /** |
|
| 104 | + * @param \Icewind\SMB\Change $change |
|
| 105 | + * @return IChange|null |
|
| 106 | + */ |
|
| 107 | + private function mapChange(\Icewind\SMB\Change $change) { |
|
| 108 | + $path = $this->relativePath($change->getPath()); |
|
| 109 | + if (is_null($path)) { |
|
| 110 | + return null; |
|
| 111 | + } |
|
| 112 | + if ($change->getCode() === \Icewind\SMB\INotifyHandler::NOTIFY_RENAMED_OLD) { |
|
| 113 | + $this->oldRenamePath = $path; |
|
| 114 | + return null; |
|
| 115 | + } |
|
| 116 | + $type = $this->mapNotifyType($change->getCode()); |
|
| 117 | + if (is_null($type)) { |
|
| 118 | + return null; |
|
| 119 | + } |
|
| 120 | + if ($type === IChange::RENAMED) { |
|
| 121 | + if (!is_null($this->oldRenamePath)) { |
|
| 122 | + $result = new RenameChange($type, $this->oldRenamePath, $path); |
|
| 123 | + $this->oldRenamePath = null; |
|
| 124 | + } else { |
|
| 125 | + $result = null; |
|
| 126 | + } |
|
| 127 | + } else { |
|
| 128 | + $result = new Change($type, $path); |
|
| 129 | + } |
|
| 130 | + return $result; |
|
| 131 | + } |
|
| 132 | 132 | |
| 133 | - private function mapNotifyType($smbType) { |
|
| 134 | - switch ($smbType) { |
|
| 135 | - case \Icewind\SMB\INotifyHandler::NOTIFY_ADDED: |
|
| 136 | - return IChange::ADDED; |
|
| 137 | - case \Icewind\SMB\INotifyHandler::NOTIFY_REMOVED: |
|
| 138 | - return IChange::REMOVED; |
|
| 139 | - case \Icewind\SMB\INotifyHandler::NOTIFY_MODIFIED: |
|
| 140 | - case \Icewind\SMB\INotifyHandler::NOTIFY_ADDED_STREAM: |
|
| 141 | - case \Icewind\SMB\INotifyHandler::NOTIFY_MODIFIED_STREAM: |
|
| 142 | - case \Icewind\SMB\INotifyHandler::NOTIFY_REMOVED_STREAM: |
|
| 143 | - return IChange::MODIFIED; |
|
| 144 | - case \Icewind\SMB\INotifyHandler::NOTIFY_RENAMED_NEW: |
|
| 145 | - return IChange::RENAMED; |
|
| 146 | - default: |
|
| 147 | - return null; |
|
| 148 | - } |
|
| 149 | - } |
|
| 133 | + private function mapNotifyType($smbType) { |
|
| 134 | + switch ($smbType) { |
|
| 135 | + case \Icewind\SMB\INotifyHandler::NOTIFY_ADDED: |
|
| 136 | + return IChange::ADDED; |
|
| 137 | + case \Icewind\SMB\INotifyHandler::NOTIFY_REMOVED: |
|
| 138 | + return IChange::REMOVED; |
|
| 139 | + case \Icewind\SMB\INotifyHandler::NOTIFY_MODIFIED: |
|
| 140 | + case \Icewind\SMB\INotifyHandler::NOTIFY_ADDED_STREAM: |
|
| 141 | + case \Icewind\SMB\INotifyHandler::NOTIFY_MODIFIED_STREAM: |
|
| 142 | + case \Icewind\SMB\INotifyHandler::NOTIFY_REMOVED_STREAM: |
|
| 143 | + return IChange::MODIFIED; |
|
| 144 | + case \Icewind\SMB\INotifyHandler::NOTIFY_RENAMED_NEW: |
|
| 145 | + return IChange::RENAMED; |
|
| 146 | + default: |
|
| 147 | + return null; |
|
| 148 | + } |
|
| 149 | + } |
|
| 150 | 150 | } |
@@ -64,7 +64,7 @@ |
||
| 64 | 64 | |
| 65 | 65 | public function listen(callable $callback) { |
| 66 | 66 | $oldRenamePath = null; |
| 67 | - $this->shareNotifyHandler->listen(function (\Icewind\SMB\Change $shareChange) use ($callback) { |
|
| 67 | + $this->shareNotifyHandler->listen(function(\Icewind\SMB\Change $shareChange) use ($callback) { |
|
| 68 | 68 | $change = $this->mapChange($shareChange); |
| 69 | 69 | if (!is_null($change)) { |
| 70 | 70 | return $callback($change); |