@@ -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) { |
@@ -24,7 +24,6 @@ |
||
| 24 | 24 | namespace OC\Share20; |
| 25 | 25 | |
| 26 | 26 | use OC\Files\Cache\Cache; |
| 27 | -use OC\Files\Cache\CacheEntry; |
|
| 28 | 27 | use OCP\Files\File; |
| 29 | 28 | use OCP\Files\Folder; |
| 30 | 29 | use OCP\Share\IShareProvider; |
@@ -47,1024 +47,1024 @@ |
||
| 47 | 47 | */ |
| 48 | 48 | class DefaultShareProvider implements IShareProvider { |
| 49 | 49 | |
| 50 | - // Special share type for user modified group shares |
|
| 51 | - const SHARE_TYPE_USERGROUP = 2; |
|
| 52 | - |
|
| 53 | - /** @var IDBConnection */ |
|
| 54 | - private $dbConn; |
|
| 55 | - |
|
| 56 | - /** @var IUserManager */ |
|
| 57 | - private $userManager; |
|
| 58 | - |
|
| 59 | - /** @var IGroupManager */ |
|
| 60 | - private $groupManager; |
|
| 61 | - |
|
| 62 | - /** @var IRootFolder */ |
|
| 63 | - private $rootFolder; |
|
| 64 | - |
|
| 65 | - /** |
|
| 66 | - * DefaultShareProvider constructor. |
|
| 67 | - * |
|
| 68 | - * @param IDBConnection $connection |
|
| 69 | - * @param IUserManager $userManager |
|
| 70 | - * @param IGroupManager $groupManager |
|
| 71 | - * @param IRootFolder $rootFolder |
|
| 72 | - */ |
|
| 73 | - public function __construct( |
|
| 74 | - IDBConnection $connection, |
|
| 75 | - IUserManager $userManager, |
|
| 76 | - IGroupManager $groupManager, |
|
| 77 | - IRootFolder $rootFolder) { |
|
| 78 | - $this->dbConn = $connection; |
|
| 79 | - $this->userManager = $userManager; |
|
| 80 | - $this->groupManager = $groupManager; |
|
| 81 | - $this->rootFolder = $rootFolder; |
|
| 82 | - } |
|
| 83 | - |
|
| 84 | - /** |
|
| 85 | - * Return the identifier of this provider. |
|
| 86 | - * |
|
| 87 | - * @return string Containing only [a-zA-Z0-9] |
|
| 88 | - */ |
|
| 89 | - public function identifier() { |
|
| 90 | - return 'ocinternal'; |
|
| 91 | - } |
|
| 92 | - |
|
| 93 | - /** |
|
| 94 | - * Share a path |
|
| 95 | - * |
|
| 96 | - * @param \OCP\Share\IShare $share |
|
| 97 | - * @return \OCP\Share\IShare The share object |
|
| 98 | - * @throws ShareNotFound |
|
| 99 | - * @throws \Exception |
|
| 100 | - */ |
|
| 101 | - public function create(\OCP\Share\IShare $share) { |
|
| 102 | - $qb = $this->dbConn->getQueryBuilder(); |
|
| 103 | - |
|
| 104 | - $qb->insert('share'); |
|
| 105 | - $qb->setValue('share_type', $qb->createNamedParameter($share->getShareType())); |
|
| 106 | - |
|
| 107 | - if ($share->getShareType() === \OCP\Share::SHARE_TYPE_USER) { |
|
| 108 | - //Set the UID of the user we share with |
|
| 109 | - $qb->setValue('share_with', $qb->createNamedParameter($share->getSharedWith())); |
|
| 110 | - } else if ($share->getShareType() === \OCP\Share::SHARE_TYPE_GROUP) { |
|
| 111 | - //Set the GID of the group we share with |
|
| 112 | - $qb->setValue('share_with', $qb->createNamedParameter($share->getSharedWith())); |
|
| 113 | - } else if ($share->getShareType() === \OCP\Share::SHARE_TYPE_LINK) { |
|
| 114 | - //Set the token of the share |
|
| 115 | - $qb->setValue('token', $qb->createNamedParameter($share->getToken())); |
|
| 116 | - |
|
| 117 | - //If a password is set store it |
|
| 118 | - if ($share->getPassword() !== null) { |
|
| 119 | - $qb->setValue('share_with', $qb->createNamedParameter($share->getPassword())); |
|
| 120 | - } |
|
| 121 | - |
|
| 122 | - //If an expiration date is set store it |
|
| 123 | - if ($share->getExpirationDate() !== null) { |
|
| 124 | - $qb->setValue('expiration', $qb->createNamedParameter($share->getExpirationDate(), 'datetime')); |
|
| 125 | - } |
|
| 126 | - |
|
| 127 | - if (method_exists($share, 'getParent')) { |
|
| 128 | - $qb->setValue('parent', $qb->createNamedParameter($share->getParent())); |
|
| 129 | - } |
|
| 130 | - } else { |
|
| 131 | - throw new \Exception('invalid share type!'); |
|
| 132 | - } |
|
| 133 | - |
|
| 134 | - // Set what is shares |
|
| 135 | - $qb->setValue('item_type', $qb->createParameter('itemType')); |
|
| 136 | - if ($share->getNode() instanceof \OCP\Files\File) { |
|
| 137 | - $qb->setParameter('itemType', 'file'); |
|
| 138 | - } else { |
|
| 139 | - $qb->setParameter('itemType', 'folder'); |
|
| 140 | - } |
|
| 141 | - |
|
| 142 | - // Set the file id |
|
| 143 | - $qb->setValue('item_source', $qb->createNamedParameter($share->getNode()->getId())); |
|
| 144 | - $qb->setValue('file_source', $qb->createNamedParameter($share->getNode()->getId())); |
|
| 145 | - |
|
| 146 | - // set the permissions |
|
| 147 | - $qb->setValue('permissions', $qb->createNamedParameter($share->getPermissions())); |
|
| 148 | - |
|
| 149 | - // Set who created this share |
|
| 150 | - $qb->setValue('uid_initiator', $qb->createNamedParameter($share->getSharedBy())); |
|
| 151 | - |
|
| 152 | - // Set who is the owner of this file/folder (and this the owner of the share) |
|
| 153 | - $qb->setValue('uid_owner', $qb->createNamedParameter($share->getShareOwner())); |
|
| 154 | - |
|
| 155 | - // Set the file target |
|
| 156 | - $qb->setValue('file_target', $qb->createNamedParameter($share->getTarget())); |
|
| 157 | - |
|
| 158 | - // Set the time this share was created |
|
| 159 | - $qb->setValue('stime', $qb->createNamedParameter(time())); |
|
| 160 | - |
|
| 161 | - // insert the data and fetch the id of the share |
|
| 162 | - $this->dbConn->beginTransaction(); |
|
| 163 | - $qb->execute(); |
|
| 164 | - $id = $this->dbConn->lastInsertId('*PREFIX*share'); |
|
| 165 | - |
|
| 166 | - // Now fetch the inserted share and create a complete share object |
|
| 167 | - $qb = $this->dbConn->getQueryBuilder(); |
|
| 168 | - $qb->select('*') |
|
| 169 | - ->from('share') |
|
| 170 | - ->where($qb->expr()->eq('id', $qb->createNamedParameter($id))); |
|
| 171 | - |
|
| 172 | - $cursor = $qb->execute(); |
|
| 173 | - $data = $cursor->fetch(); |
|
| 174 | - $this->dbConn->commit(); |
|
| 175 | - $cursor->closeCursor(); |
|
| 176 | - |
|
| 177 | - if ($data === false) { |
|
| 178 | - throw new ShareNotFound(); |
|
| 179 | - } |
|
| 180 | - |
|
| 181 | - $share = $this->createShare($data); |
|
| 182 | - return $share; |
|
| 183 | - } |
|
| 184 | - |
|
| 185 | - /** |
|
| 186 | - * Update a share |
|
| 187 | - * |
|
| 188 | - * @param \OCP\Share\IShare $share |
|
| 189 | - * @return \OCP\Share\IShare The share object |
|
| 190 | - */ |
|
| 191 | - public function update(\OCP\Share\IShare $share) { |
|
| 192 | - if ($share->getShareType() === \OCP\Share::SHARE_TYPE_USER) { |
|
| 193 | - /* |
|
| 50 | + // Special share type for user modified group shares |
|
| 51 | + const SHARE_TYPE_USERGROUP = 2; |
|
| 52 | + |
|
| 53 | + /** @var IDBConnection */ |
|
| 54 | + private $dbConn; |
|
| 55 | + |
|
| 56 | + /** @var IUserManager */ |
|
| 57 | + private $userManager; |
|
| 58 | + |
|
| 59 | + /** @var IGroupManager */ |
|
| 60 | + private $groupManager; |
|
| 61 | + |
|
| 62 | + /** @var IRootFolder */ |
|
| 63 | + private $rootFolder; |
|
| 64 | + |
|
| 65 | + /** |
|
| 66 | + * DefaultShareProvider constructor. |
|
| 67 | + * |
|
| 68 | + * @param IDBConnection $connection |
|
| 69 | + * @param IUserManager $userManager |
|
| 70 | + * @param IGroupManager $groupManager |
|
| 71 | + * @param IRootFolder $rootFolder |
|
| 72 | + */ |
|
| 73 | + public function __construct( |
|
| 74 | + IDBConnection $connection, |
|
| 75 | + IUserManager $userManager, |
|
| 76 | + IGroupManager $groupManager, |
|
| 77 | + IRootFolder $rootFolder) { |
|
| 78 | + $this->dbConn = $connection; |
|
| 79 | + $this->userManager = $userManager; |
|
| 80 | + $this->groupManager = $groupManager; |
|
| 81 | + $this->rootFolder = $rootFolder; |
|
| 82 | + } |
|
| 83 | + |
|
| 84 | + /** |
|
| 85 | + * Return the identifier of this provider. |
|
| 86 | + * |
|
| 87 | + * @return string Containing only [a-zA-Z0-9] |
|
| 88 | + */ |
|
| 89 | + public function identifier() { |
|
| 90 | + return 'ocinternal'; |
|
| 91 | + } |
|
| 92 | + |
|
| 93 | + /** |
|
| 94 | + * Share a path |
|
| 95 | + * |
|
| 96 | + * @param \OCP\Share\IShare $share |
|
| 97 | + * @return \OCP\Share\IShare The share object |
|
| 98 | + * @throws ShareNotFound |
|
| 99 | + * @throws \Exception |
|
| 100 | + */ |
|
| 101 | + public function create(\OCP\Share\IShare $share) { |
|
| 102 | + $qb = $this->dbConn->getQueryBuilder(); |
|
| 103 | + |
|
| 104 | + $qb->insert('share'); |
|
| 105 | + $qb->setValue('share_type', $qb->createNamedParameter($share->getShareType())); |
|
| 106 | + |
|
| 107 | + if ($share->getShareType() === \OCP\Share::SHARE_TYPE_USER) { |
|
| 108 | + //Set the UID of the user we share with |
|
| 109 | + $qb->setValue('share_with', $qb->createNamedParameter($share->getSharedWith())); |
|
| 110 | + } else if ($share->getShareType() === \OCP\Share::SHARE_TYPE_GROUP) { |
|
| 111 | + //Set the GID of the group we share with |
|
| 112 | + $qb->setValue('share_with', $qb->createNamedParameter($share->getSharedWith())); |
|
| 113 | + } else if ($share->getShareType() === \OCP\Share::SHARE_TYPE_LINK) { |
|
| 114 | + //Set the token of the share |
|
| 115 | + $qb->setValue('token', $qb->createNamedParameter($share->getToken())); |
|
| 116 | + |
|
| 117 | + //If a password is set store it |
|
| 118 | + if ($share->getPassword() !== null) { |
|
| 119 | + $qb->setValue('share_with', $qb->createNamedParameter($share->getPassword())); |
|
| 120 | + } |
|
| 121 | + |
|
| 122 | + //If an expiration date is set store it |
|
| 123 | + if ($share->getExpirationDate() !== null) { |
|
| 124 | + $qb->setValue('expiration', $qb->createNamedParameter($share->getExpirationDate(), 'datetime')); |
|
| 125 | + } |
|
| 126 | + |
|
| 127 | + if (method_exists($share, 'getParent')) { |
|
| 128 | + $qb->setValue('parent', $qb->createNamedParameter($share->getParent())); |
|
| 129 | + } |
|
| 130 | + } else { |
|
| 131 | + throw new \Exception('invalid share type!'); |
|
| 132 | + } |
|
| 133 | + |
|
| 134 | + // Set what is shares |
|
| 135 | + $qb->setValue('item_type', $qb->createParameter('itemType')); |
|
| 136 | + if ($share->getNode() instanceof \OCP\Files\File) { |
|
| 137 | + $qb->setParameter('itemType', 'file'); |
|
| 138 | + } else { |
|
| 139 | + $qb->setParameter('itemType', 'folder'); |
|
| 140 | + } |
|
| 141 | + |
|
| 142 | + // Set the file id |
|
| 143 | + $qb->setValue('item_source', $qb->createNamedParameter($share->getNode()->getId())); |
|
| 144 | + $qb->setValue('file_source', $qb->createNamedParameter($share->getNode()->getId())); |
|
| 145 | + |
|
| 146 | + // set the permissions |
|
| 147 | + $qb->setValue('permissions', $qb->createNamedParameter($share->getPermissions())); |
|
| 148 | + |
|
| 149 | + // Set who created this share |
|
| 150 | + $qb->setValue('uid_initiator', $qb->createNamedParameter($share->getSharedBy())); |
|
| 151 | + |
|
| 152 | + // Set who is the owner of this file/folder (and this the owner of the share) |
|
| 153 | + $qb->setValue('uid_owner', $qb->createNamedParameter($share->getShareOwner())); |
|
| 154 | + |
|
| 155 | + // Set the file target |
|
| 156 | + $qb->setValue('file_target', $qb->createNamedParameter($share->getTarget())); |
|
| 157 | + |
|
| 158 | + // Set the time this share was created |
|
| 159 | + $qb->setValue('stime', $qb->createNamedParameter(time())); |
|
| 160 | + |
|
| 161 | + // insert the data and fetch the id of the share |
|
| 162 | + $this->dbConn->beginTransaction(); |
|
| 163 | + $qb->execute(); |
|
| 164 | + $id = $this->dbConn->lastInsertId('*PREFIX*share'); |
|
| 165 | + |
|
| 166 | + // Now fetch the inserted share and create a complete share object |
|
| 167 | + $qb = $this->dbConn->getQueryBuilder(); |
|
| 168 | + $qb->select('*') |
|
| 169 | + ->from('share') |
|
| 170 | + ->where($qb->expr()->eq('id', $qb->createNamedParameter($id))); |
|
| 171 | + |
|
| 172 | + $cursor = $qb->execute(); |
|
| 173 | + $data = $cursor->fetch(); |
|
| 174 | + $this->dbConn->commit(); |
|
| 175 | + $cursor->closeCursor(); |
|
| 176 | + |
|
| 177 | + if ($data === false) { |
|
| 178 | + throw new ShareNotFound(); |
|
| 179 | + } |
|
| 180 | + |
|
| 181 | + $share = $this->createShare($data); |
|
| 182 | + return $share; |
|
| 183 | + } |
|
| 184 | + |
|
| 185 | + /** |
|
| 186 | + * Update a share |
|
| 187 | + * |
|
| 188 | + * @param \OCP\Share\IShare $share |
|
| 189 | + * @return \OCP\Share\IShare The share object |
|
| 190 | + */ |
|
| 191 | + public function update(\OCP\Share\IShare $share) { |
|
| 192 | + if ($share->getShareType() === \OCP\Share::SHARE_TYPE_USER) { |
|
| 193 | + /* |
|
| 194 | 194 | * We allow updating the recipient on user shares. |
| 195 | 195 | */ |
| 196 | - $qb = $this->dbConn->getQueryBuilder(); |
|
| 197 | - $qb->update('share') |
|
| 198 | - ->where($qb->expr()->eq('id', $qb->createNamedParameter($share->getId()))) |
|
| 199 | - ->set('share_with', $qb->createNamedParameter($share->getSharedWith())) |
|
| 200 | - ->set('uid_owner', $qb->createNamedParameter($share->getShareOwner())) |
|
| 201 | - ->set('uid_initiator', $qb->createNamedParameter($share->getSharedBy())) |
|
| 202 | - ->set('permissions', $qb->createNamedParameter($share->getPermissions())) |
|
| 203 | - ->set('item_source', $qb->createNamedParameter($share->getNode()->getId())) |
|
| 204 | - ->set('file_source', $qb->createNamedParameter($share->getNode()->getId())) |
|
| 205 | - ->execute(); |
|
| 206 | - } else if ($share->getShareType() === \OCP\Share::SHARE_TYPE_GROUP) { |
|
| 207 | - $qb = $this->dbConn->getQueryBuilder(); |
|
| 208 | - $qb->update('share') |
|
| 209 | - ->where($qb->expr()->eq('id', $qb->createNamedParameter($share->getId()))) |
|
| 210 | - ->set('uid_owner', $qb->createNamedParameter($share->getShareOwner())) |
|
| 211 | - ->set('uid_initiator', $qb->createNamedParameter($share->getSharedBy())) |
|
| 212 | - ->set('permissions', $qb->createNamedParameter($share->getPermissions())) |
|
| 213 | - ->set('item_source', $qb->createNamedParameter($share->getNode()->getId())) |
|
| 214 | - ->set('file_source', $qb->createNamedParameter($share->getNode()->getId())) |
|
| 215 | - ->execute(); |
|
| 216 | - |
|
| 217 | - /* |
|
| 196 | + $qb = $this->dbConn->getQueryBuilder(); |
|
| 197 | + $qb->update('share') |
|
| 198 | + ->where($qb->expr()->eq('id', $qb->createNamedParameter($share->getId()))) |
|
| 199 | + ->set('share_with', $qb->createNamedParameter($share->getSharedWith())) |
|
| 200 | + ->set('uid_owner', $qb->createNamedParameter($share->getShareOwner())) |
|
| 201 | + ->set('uid_initiator', $qb->createNamedParameter($share->getSharedBy())) |
|
| 202 | + ->set('permissions', $qb->createNamedParameter($share->getPermissions())) |
|
| 203 | + ->set('item_source', $qb->createNamedParameter($share->getNode()->getId())) |
|
| 204 | + ->set('file_source', $qb->createNamedParameter($share->getNode()->getId())) |
|
| 205 | + ->execute(); |
|
| 206 | + } else if ($share->getShareType() === \OCP\Share::SHARE_TYPE_GROUP) { |
|
| 207 | + $qb = $this->dbConn->getQueryBuilder(); |
|
| 208 | + $qb->update('share') |
|
| 209 | + ->where($qb->expr()->eq('id', $qb->createNamedParameter($share->getId()))) |
|
| 210 | + ->set('uid_owner', $qb->createNamedParameter($share->getShareOwner())) |
|
| 211 | + ->set('uid_initiator', $qb->createNamedParameter($share->getSharedBy())) |
|
| 212 | + ->set('permissions', $qb->createNamedParameter($share->getPermissions())) |
|
| 213 | + ->set('item_source', $qb->createNamedParameter($share->getNode()->getId())) |
|
| 214 | + ->set('file_source', $qb->createNamedParameter($share->getNode()->getId())) |
|
| 215 | + ->execute(); |
|
| 216 | + |
|
| 217 | + /* |
|
| 218 | 218 | * Update all user defined group shares |
| 219 | 219 | */ |
| 220 | - $qb = $this->dbConn->getQueryBuilder(); |
|
| 221 | - $qb->update('share') |
|
| 222 | - ->where($qb->expr()->eq('parent', $qb->createNamedParameter($share->getId()))) |
|
| 223 | - ->set('uid_owner', $qb->createNamedParameter($share->getShareOwner())) |
|
| 224 | - ->set('uid_initiator', $qb->createNamedParameter($share->getSharedBy())) |
|
| 225 | - ->set('item_source', $qb->createNamedParameter($share->getNode()->getId())) |
|
| 226 | - ->set('file_source', $qb->createNamedParameter($share->getNode()->getId())) |
|
| 227 | - ->execute(); |
|
| 228 | - |
|
| 229 | - /* |
|
| 220 | + $qb = $this->dbConn->getQueryBuilder(); |
|
| 221 | + $qb->update('share') |
|
| 222 | + ->where($qb->expr()->eq('parent', $qb->createNamedParameter($share->getId()))) |
|
| 223 | + ->set('uid_owner', $qb->createNamedParameter($share->getShareOwner())) |
|
| 224 | + ->set('uid_initiator', $qb->createNamedParameter($share->getSharedBy())) |
|
| 225 | + ->set('item_source', $qb->createNamedParameter($share->getNode()->getId())) |
|
| 226 | + ->set('file_source', $qb->createNamedParameter($share->getNode()->getId())) |
|
| 227 | + ->execute(); |
|
| 228 | + |
|
| 229 | + /* |
|
| 230 | 230 | * Now update the permissions for all children that have not set it to 0 |
| 231 | 231 | */ |
| 232 | - $qb = $this->dbConn->getQueryBuilder(); |
|
| 233 | - $qb->update('share') |
|
| 234 | - ->where($qb->expr()->eq('parent', $qb->createNamedParameter($share->getId()))) |
|
| 235 | - ->andWhere($qb->expr()->neq('permissions', $qb->createNamedParameter(0))) |
|
| 236 | - ->set('permissions', $qb->createNamedParameter($share->getPermissions())) |
|
| 237 | - ->execute(); |
|
| 238 | - |
|
| 239 | - } else if ($share->getShareType() === \OCP\Share::SHARE_TYPE_LINK) { |
|
| 240 | - $qb = $this->dbConn->getQueryBuilder(); |
|
| 241 | - $qb->update('share') |
|
| 242 | - ->where($qb->expr()->eq('id', $qb->createNamedParameter($share->getId()))) |
|
| 243 | - ->set('share_with', $qb->createNamedParameter($share->getPassword())) |
|
| 244 | - ->set('uid_owner', $qb->createNamedParameter($share->getShareOwner())) |
|
| 245 | - ->set('uid_initiator', $qb->createNamedParameter($share->getSharedBy())) |
|
| 246 | - ->set('permissions', $qb->createNamedParameter($share->getPermissions())) |
|
| 247 | - ->set('item_source', $qb->createNamedParameter($share->getNode()->getId())) |
|
| 248 | - ->set('file_source', $qb->createNamedParameter($share->getNode()->getId())) |
|
| 249 | - ->set('token', $qb->createNamedParameter($share->getToken())) |
|
| 250 | - ->set('expiration', $qb->createNamedParameter($share->getExpirationDate(), IQueryBuilder::PARAM_DATE)) |
|
| 251 | - ->execute(); |
|
| 252 | - } |
|
| 253 | - |
|
| 254 | - return $share; |
|
| 255 | - } |
|
| 256 | - |
|
| 257 | - /** |
|
| 258 | - * Get all children of this share |
|
| 259 | - * FIXME: remove once https://github.com/owncloud/core/pull/21660 is in |
|
| 260 | - * |
|
| 261 | - * @param \OCP\Share\IShare $parent |
|
| 262 | - * @return \OCP\Share\IShare[] |
|
| 263 | - */ |
|
| 264 | - public function getChildren(\OCP\Share\IShare $parent) { |
|
| 265 | - $children = []; |
|
| 266 | - |
|
| 267 | - $qb = $this->dbConn->getQueryBuilder(); |
|
| 268 | - $qb->select('*') |
|
| 269 | - ->from('share') |
|
| 270 | - ->where($qb->expr()->eq('parent', $qb->createNamedParameter($parent->getId()))) |
|
| 271 | - ->andWhere( |
|
| 272 | - $qb->expr()->in( |
|
| 273 | - 'share_type', |
|
| 274 | - $qb->createNamedParameter([ |
|
| 275 | - \OCP\Share::SHARE_TYPE_USER, |
|
| 276 | - \OCP\Share::SHARE_TYPE_GROUP, |
|
| 277 | - \OCP\Share::SHARE_TYPE_LINK, |
|
| 278 | - ], IQueryBuilder::PARAM_INT_ARRAY) |
|
| 279 | - ) |
|
| 280 | - ) |
|
| 281 | - ->andWhere($qb->expr()->orX( |
|
| 282 | - $qb->expr()->eq('item_type', $qb->createNamedParameter('file')), |
|
| 283 | - $qb->expr()->eq('item_type', $qb->createNamedParameter('folder')) |
|
| 284 | - )) |
|
| 285 | - ->orderBy('id'); |
|
| 286 | - |
|
| 287 | - $cursor = $qb->execute(); |
|
| 288 | - while($data = $cursor->fetch()) { |
|
| 289 | - $children[] = $this->createShare($data); |
|
| 290 | - } |
|
| 291 | - $cursor->closeCursor(); |
|
| 292 | - |
|
| 293 | - return $children; |
|
| 294 | - } |
|
| 295 | - |
|
| 296 | - /** |
|
| 297 | - * Delete a share |
|
| 298 | - * |
|
| 299 | - * @param \OCP\Share\IShare $share |
|
| 300 | - */ |
|
| 301 | - public function delete(\OCP\Share\IShare $share) { |
|
| 302 | - $qb = $this->dbConn->getQueryBuilder(); |
|
| 303 | - $qb->delete('share') |
|
| 304 | - ->where($qb->expr()->eq('id', $qb->createNamedParameter($share->getId()))); |
|
| 305 | - |
|
| 306 | - /* |
|
| 232 | + $qb = $this->dbConn->getQueryBuilder(); |
|
| 233 | + $qb->update('share') |
|
| 234 | + ->where($qb->expr()->eq('parent', $qb->createNamedParameter($share->getId()))) |
|
| 235 | + ->andWhere($qb->expr()->neq('permissions', $qb->createNamedParameter(0))) |
|
| 236 | + ->set('permissions', $qb->createNamedParameter($share->getPermissions())) |
|
| 237 | + ->execute(); |
|
| 238 | + |
|
| 239 | + } else if ($share->getShareType() === \OCP\Share::SHARE_TYPE_LINK) { |
|
| 240 | + $qb = $this->dbConn->getQueryBuilder(); |
|
| 241 | + $qb->update('share') |
|
| 242 | + ->where($qb->expr()->eq('id', $qb->createNamedParameter($share->getId()))) |
|
| 243 | + ->set('share_with', $qb->createNamedParameter($share->getPassword())) |
|
| 244 | + ->set('uid_owner', $qb->createNamedParameter($share->getShareOwner())) |
|
| 245 | + ->set('uid_initiator', $qb->createNamedParameter($share->getSharedBy())) |
|
| 246 | + ->set('permissions', $qb->createNamedParameter($share->getPermissions())) |
|
| 247 | + ->set('item_source', $qb->createNamedParameter($share->getNode()->getId())) |
|
| 248 | + ->set('file_source', $qb->createNamedParameter($share->getNode()->getId())) |
|
| 249 | + ->set('token', $qb->createNamedParameter($share->getToken())) |
|
| 250 | + ->set('expiration', $qb->createNamedParameter($share->getExpirationDate(), IQueryBuilder::PARAM_DATE)) |
|
| 251 | + ->execute(); |
|
| 252 | + } |
|
| 253 | + |
|
| 254 | + return $share; |
|
| 255 | + } |
|
| 256 | + |
|
| 257 | + /** |
|
| 258 | + * Get all children of this share |
|
| 259 | + * FIXME: remove once https://github.com/owncloud/core/pull/21660 is in |
|
| 260 | + * |
|
| 261 | + * @param \OCP\Share\IShare $parent |
|
| 262 | + * @return \OCP\Share\IShare[] |
|
| 263 | + */ |
|
| 264 | + public function getChildren(\OCP\Share\IShare $parent) { |
|
| 265 | + $children = []; |
|
| 266 | + |
|
| 267 | + $qb = $this->dbConn->getQueryBuilder(); |
|
| 268 | + $qb->select('*') |
|
| 269 | + ->from('share') |
|
| 270 | + ->where($qb->expr()->eq('parent', $qb->createNamedParameter($parent->getId()))) |
|
| 271 | + ->andWhere( |
|
| 272 | + $qb->expr()->in( |
|
| 273 | + 'share_type', |
|
| 274 | + $qb->createNamedParameter([ |
|
| 275 | + \OCP\Share::SHARE_TYPE_USER, |
|
| 276 | + \OCP\Share::SHARE_TYPE_GROUP, |
|
| 277 | + \OCP\Share::SHARE_TYPE_LINK, |
|
| 278 | + ], IQueryBuilder::PARAM_INT_ARRAY) |
|
| 279 | + ) |
|
| 280 | + ) |
|
| 281 | + ->andWhere($qb->expr()->orX( |
|
| 282 | + $qb->expr()->eq('item_type', $qb->createNamedParameter('file')), |
|
| 283 | + $qb->expr()->eq('item_type', $qb->createNamedParameter('folder')) |
|
| 284 | + )) |
|
| 285 | + ->orderBy('id'); |
|
| 286 | + |
|
| 287 | + $cursor = $qb->execute(); |
|
| 288 | + while($data = $cursor->fetch()) { |
|
| 289 | + $children[] = $this->createShare($data); |
|
| 290 | + } |
|
| 291 | + $cursor->closeCursor(); |
|
| 292 | + |
|
| 293 | + return $children; |
|
| 294 | + } |
|
| 295 | + |
|
| 296 | + /** |
|
| 297 | + * Delete a share |
|
| 298 | + * |
|
| 299 | + * @param \OCP\Share\IShare $share |
|
| 300 | + */ |
|
| 301 | + public function delete(\OCP\Share\IShare $share) { |
|
| 302 | + $qb = $this->dbConn->getQueryBuilder(); |
|
| 303 | + $qb->delete('share') |
|
| 304 | + ->where($qb->expr()->eq('id', $qb->createNamedParameter($share->getId()))); |
|
| 305 | + |
|
| 306 | + /* |
|
| 307 | 307 | * If the share is a group share delete all possible |
| 308 | 308 | * user defined groups shares. |
| 309 | 309 | */ |
| 310 | - if ($share->getShareType() === \OCP\Share::SHARE_TYPE_GROUP) { |
|
| 311 | - $qb->orWhere($qb->expr()->eq('parent', $qb->createNamedParameter($share->getId()))); |
|
| 312 | - } |
|
| 313 | - |
|
| 314 | - $qb->execute(); |
|
| 315 | - } |
|
| 316 | - |
|
| 317 | - /** |
|
| 318 | - * Unshare a share from the recipient. If this is a group share |
|
| 319 | - * this means we need a special entry in the share db. |
|
| 320 | - * |
|
| 321 | - * @param \OCP\Share\IShare $share |
|
| 322 | - * @param string $recipient UserId of recipient |
|
| 323 | - * @throws BackendError |
|
| 324 | - * @throws ProviderException |
|
| 325 | - */ |
|
| 326 | - public function deleteFromSelf(\OCP\Share\IShare $share, $recipient) { |
|
| 327 | - if ($share->getShareType() === \OCP\Share::SHARE_TYPE_GROUP) { |
|
| 328 | - |
|
| 329 | - $group = $this->groupManager->get($share->getSharedWith()); |
|
| 330 | - $user = $this->userManager->get($recipient); |
|
| 331 | - |
|
| 332 | - if (is_null($group)) { |
|
| 333 | - throw new ProviderException('Group "' . $share->getSharedWith() . '" does not exist'); |
|
| 334 | - } |
|
| 335 | - |
|
| 336 | - if (!$group->inGroup($user)) { |
|
| 337 | - throw new ProviderException('Recipient not in receiving group'); |
|
| 338 | - } |
|
| 339 | - |
|
| 340 | - // Try to fetch user specific share |
|
| 341 | - $qb = $this->dbConn->getQueryBuilder(); |
|
| 342 | - $stmt = $qb->select('*') |
|
| 343 | - ->from('share') |
|
| 344 | - ->where($qb->expr()->eq('share_type', $qb->createNamedParameter(self::SHARE_TYPE_USERGROUP))) |
|
| 345 | - ->andWhere($qb->expr()->eq('share_with', $qb->createNamedParameter($recipient))) |
|
| 346 | - ->andWhere($qb->expr()->eq('parent', $qb->createNamedParameter($share->getId()))) |
|
| 347 | - ->andWhere($qb->expr()->orX( |
|
| 348 | - $qb->expr()->eq('item_type', $qb->createNamedParameter('file')), |
|
| 349 | - $qb->expr()->eq('item_type', $qb->createNamedParameter('folder')) |
|
| 350 | - )) |
|
| 351 | - ->execute(); |
|
| 352 | - |
|
| 353 | - $data = $stmt->fetch(); |
|
| 354 | - |
|
| 355 | - /* |
|
| 310 | + if ($share->getShareType() === \OCP\Share::SHARE_TYPE_GROUP) { |
|
| 311 | + $qb->orWhere($qb->expr()->eq('parent', $qb->createNamedParameter($share->getId()))); |
|
| 312 | + } |
|
| 313 | + |
|
| 314 | + $qb->execute(); |
|
| 315 | + } |
|
| 316 | + |
|
| 317 | + /** |
|
| 318 | + * Unshare a share from the recipient. If this is a group share |
|
| 319 | + * this means we need a special entry in the share db. |
|
| 320 | + * |
|
| 321 | + * @param \OCP\Share\IShare $share |
|
| 322 | + * @param string $recipient UserId of recipient |
|
| 323 | + * @throws BackendError |
|
| 324 | + * @throws ProviderException |
|
| 325 | + */ |
|
| 326 | + public function deleteFromSelf(\OCP\Share\IShare $share, $recipient) { |
|
| 327 | + if ($share->getShareType() === \OCP\Share::SHARE_TYPE_GROUP) { |
|
| 328 | + |
|
| 329 | + $group = $this->groupManager->get($share->getSharedWith()); |
|
| 330 | + $user = $this->userManager->get($recipient); |
|
| 331 | + |
|
| 332 | + if (is_null($group)) { |
|
| 333 | + throw new ProviderException('Group "' . $share->getSharedWith() . '" does not exist'); |
|
| 334 | + } |
|
| 335 | + |
|
| 336 | + if (!$group->inGroup($user)) { |
|
| 337 | + throw new ProviderException('Recipient not in receiving group'); |
|
| 338 | + } |
|
| 339 | + |
|
| 340 | + // Try to fetch user specific share |
|
| 341 | + $qb = $this->dbConn->getQueryBuilder(); |
|
| 342 | + $stmt = $qb->select('*') |
|
| 343 | + ->from('share') |
|
| 344 | + ->where($qb->expr()->eq('share_type', $qb->createNamedParameter(self::SHARE_TYPE_USERGROUP))) |
|
| 345 | + ->andWhere($qb->expr()->eq('share_with', $qb->createNamedParameter($recipient))) |
|
| 346 | + ->andWhere($qb->expr()->eq('parent', $qb->createNamedParameter($share->getId()))) |
|
| 347 | + ->andWhere($qb->expr()->orX( |
|
| 348 | + $qb->expr()->eq('item_type', $qb->createNamedParameter('file')), |
|
| 349 | + $qb->expr()->eq('item_type', $qb->createNamedParameter('folder')) |
|
| 350 | + )) |
|
| 351 | + ->execute(); |
|
| 352 | + |
|
| 353 | + $data = $stmt->fetch(); |
|
| 354 | + |
|
| 355 | + /* |
|
| 356 | 356 | * Check if there already is a user specific group share. |
| 357 | 357 | * If there is update it (if required). |
| 358 | 358 | */ |
| 359 | - if ($data === false) { |
|
| 360 | - $qb = $this->dbConn->getQueryBuilder(); |
|
| 361 | - |
|
| 362 | - $type = $share->getNode() instanceof \OCP\Files\File ? 'file' : 'folder'; |
|
| 363 | - |
|
| 364 | - //Insert new share |
|
| 365 | - $qb->insert('share') |
|
| 366 | - ->values([ |
|
| 367 | - 'share_type' => $qb->createNamedParameter(self::SHARE_TYPE_USERGROUP), |
|
| 368 | - 'share_with' => $qb->createNamedParameter($recipient), |
|
| 369 | - 'uid_owner' => $qb->createNamedParameter($share->getShareOwner()), |
|
| 370 | - 'uid_initiator' => $qb->createNamedParameter($share->getSharedBy()), |
|
| 371 | - 'parent' => $qb->createNamedParameter($share->getId()), |
|
| 372 | - 'item_type' => $qb->createNamedParameter($type), |
|
| 373 | - 'item_source' => $qb->createNamedParameter($share->getNode()->getId()), |
|
| 374 | - 'file_source' => $qb->createNamedParameter($share->getNode()->getId()), |
|
| 375 | - 'file_target' => $qb->createNamedParameter($share->getTarget()), |
|
| 376 | - 'permissions' => $qb->createNamedParameter(0), |
|
| 377 | - 'stime' => $qb->createNamedParameter($share->getShareTime()->getTimestamp()), |
|
| 378 | - ])->execute(); |
|
| 379 | - |
|
| 380 | - } else if ($data['permissions'] !== 0) { |
|
| 381 | - |
|
| 382 | - // Update existing usergroup share |
|
| 383 | - $qb = $this->dbConn->getQueryBuilder(); |
|
| 384 | - $qb->update('share') |
|
| 385 | - ->set('permissions', $qb->createNamedParameter(0)) |
|
| 386 | - ->where($qb->expr()->eq('id', $qb->createNamedParameter($data['id']))) |
|
| 387 | - ->execute(); |
|
| 388 | - } |
|
| 389 | - |
|
| 390 | - } else if ($share->getShareType() === \OCP\Share::SHARE_TYPE_USER) { |
|
| 391 | - |
|
| 392 | - if ($share->getSharedWith() !== $recipient) { |
|
| 393 | - throw new ProviderException('Recipient does not match'); |
|
| 394 | - } |
|
| 395 | - |
|
| 396 | - // We can just delete user and link shares |
|
| 397 | - $this->delete($share); |
|
| 398 | - } else { |
|
| 399 | - throw new ProviderException('Invalid shareType'); |
|
| 400 | - } |
|
| 401 | - } |
|
| 402 | - |
|
| 403 | - /** |
|
| 404 | - * @inheritdoc |
|
| 405 | - */ |
|
| 406 | - public function move(\OCP\Share\IShare $share, $recipient) { |
|
| 407 | - if ($share->getShareType() === \OCP\Share::SHARE_TYPE_USER) { |
|
| 408 | - // Just update the target |
|
| 409 | - $qb = $this->dbConn->getQueryBuilder(); |
|
| 410 | - $qb->update('share') |
|
| 411 | - ->set('file_target', $qb->createNamedParameter($share->getTarget())) |
|
| 412 | - ->where($qb->expr()->eq('id', $qb->createNamedParameter($share->getId()))) |
|
| 413 | - ->execute(); |
|
| 414 | - |
|
| 415 | - } else if ($share->getShareType() === \OCP\Share::SHARE_TYPE_GROUP) { |
|
| 416 | - |
|
| 417 | - // Check if there is a usergroup share |
|
| 418 | - $qb = $this->dbConn->getQueryBuilder(); |
|
| 419 | - $stmt = $qb->select('id') |
|
| 420 | - ->from('share') |
|
| 421 | - ->where($qb->expr()->eq('share_type', $qb->createNamedParameter(self::SHARE_TYPE_USERGROUP))) |
|
| 422 | - ->andWhere($qb->expr()->eq('share_with', $qb->createNamedParameter($recipient))) |
|
| 423 | - ->andWhere($qb->expr()->eq('parent', $qb->createNamedParameter($share->getId()))) |
|
| 424 | - ->andWhere($qb->expr()->orX( |
|
| 425 | - $qb->expr()->eq('item_type', $qb->createNamedParameter('file')), |
|
| 426 | - $qb->expr()->eq('item_type', $qb->createNamedParameter('folder')) |
|
| 427 | - )) |
|
| 428 | - ->setMaxResults(1) |
|
| 429 | - ->execute(); |
|
| 430 | - |
|
| 431 | - $data = $stmt->fetch(); |
|
| 432 | - $stmt->closeCursor(); |
|
| 433 | - |
|
| 434 | - if ($data === false) { |
|
| 435 | - // No usergroup share yet. Create one. |
|
| 436 | - $qb = $this->dbConn->getQueryBuilder(); |
|
| 437 | - $qb->insert('share') |
|
| 438 | - ->values([ |
|
| 439 | - 'share_type' => $qb->createNamedParameter(self::SHARE_TYPE_USERGROUP), |
|
| 440 | - 'share_with' => $qb->createNamedParameter($recipient), |
|
| 441 | - 'uid_owner' => $qb->createNamedParameter($share->getShareOwner()), |
|
| 442 | - 'uid_initiator' => $qb->createNamedParameter($share->getSharedBy()), |
|
| 443 | - 'parent' => $qb->createNamedParameter($share->getId()), |
|
| 444 | - 'item_type' => $qb->createNamedParameter($share->getNode() instanceof File ? 'file' : 'folder'), |
|
| 445 | - 'item_source' => $qb->createNamedParameter($share->getNode()->getId()), |
|
| 446 | - 'file_source' => $qb->createNamedParameter($share->getNode()->getId()), |
|
| 447 | - 'file_target' => $qb->createNamedParameter($share->getTarget()), |
|
| 448 | - 'permissions' => $qb->createNamedParameter($share->getPermissions()), |
|
| 449 | - 'stime' => $qb->createNamedParameter($share->getShareTime()->getTimestamp()), |
|
| 450 | - ])->execute(); |
|
| 451 | - } else { |
|
| 452 | - // Already a usergroup share. Update it. |
|
| 453 | - $qb = $this->dbConn->getQueryBuilder(); |
|
| 454 | - $qb->update('share') |
|
| 455 | - ->set('file_target', $qb->createNamedParameter($share->getTarget())) |
|
| 456 | - ->where($qb->expr()->eq('id', $qb->createNamedParameter($data['id']))) |
|
| 457 | - ->execute(); |
|
| 458 | - } |
|
| 459 | - } |
|
| 460 | - |
|
| 461 | - return $share; |
|
| 462 | - } |
|
| 463 | - |
|
| 464 | - public function getSharesInFolder($userId, Folder $node, $reshares) { |
|
| 465 | - $qb = $this->dbConn->getQueryBuilder(); |
|
| 466 | - $qb->select('*') |
|
| 467 | - ->from('share', 's') |
|
| 468 | - ->andWhere($qb->expr()->orX( |
|
| 469 | - $qb->expr()->eq('item_type', $qb->createNamedParameter('file')), |
|
| 470 | - $qb->expr()->eq('item_type', $qb->createNamedParameter('folder')) |
|
| 471 | - )); |
|
| 472 | - |
|
| 473 | - $qb->andWhere($qb->expr()->orX( |
|
| 474 | - $qb->expr()->eq('share_type', $qb->createNamedParameter(\OCP\Share::SHARE_TYPE_USER)), |
|
| 475 | - $qb->expr()->eq('share_type', $qb->createNamedParameter(\OCP\Share::SHARE_TYPE_GROUP)), |
|
| 476 | - $qb->expr()->eq('share_type', $qb->createNamedParameter(\OCP\Share::SHARE_TYPE_LINK)) |
|
| 477 | - )); |
|
| 478 | - |
|
| 479 | - /** |
|
| 480 | - * Reshares for this user are shares where they are the owner. |
|
| 481 | - */ |
|
| 482 | - if ($reshares === false) { |
|
| 483 | - $qb->andWhere($qb->expr()->eq('uid_initiator', $qb->createNamedParameter($userId))); |
|
| 484 | - } else { |
|
| 485 | - $qb->andWhere( |
|
| 486 | - $qb->expr()->orX( |
|
| 487 | - $qb->expr()->eq('uid_owner', $qb->createNamedParameter($userId)), |
|
| 488 | - $qb->expr()->eq('uid_initiator', $qb->createNamedParameter($userId)) |
|
| 489 | - ) |
|
| 490 | - ); |
|
| 491 | - } |
|
| 492 | - |
|
| 493 | - $qb->innerJoin('s', 'filecache' ,'f', 's.file_source = f.fileid'); |
|
| 494 | - $qb->andWhere($qb->expr()->eq('f.parent', $qb->createNamedParameter($node->getId()))); |
|
| 495 | - |
|
| 496 | - $qb->orderBy('id'); |
|
| 497 | - |
|
| 498 | - $cursor = $qb->execute(); |
|
| 499 | - $shares = []; |
|
| 500 | - while ($data = $cursor->fetch()) { |
|
| 501 | - $shares[$data['fileid']][] = $this->createShare($data); |
|
| 502 | - } |
|
| 503 | - $cursor->closeCursor(); |
|
| 504 | - |
|
| 505 | - return $shares; |
|
| 506 | - } |
|
| 507 | - |
|
| 508 | - /** |
|
| 509 | - * @inheritdoc |
|
| 510 | - */ |
|
| 511 | - public function getSharesBy($userId, $shareType, $node, $reshares, $limit, $offset) { |
|
| 512 | - $qb = $this->dbConn->getQueryBuilder(); |
|
| 513 | - $qb->select('*') |
|
| 514 | - ->from('share') |
|
| 515 | - ->andWhere($qb->expr()->orX( |
|
| 516 | - $qb->expr()->eq('item_type', $qb->createNamedParameter('file')), |
|
| 517 | - $qb->expr()->eq('item_type', $qb->createNamedParameter('folder')) |
|
| 518 | - )); |
|
| 519 | - |
|
| 520 | - $qb->andWhere($qb->expr()->eq('share_type', $qb->createNamedParameter($shareType))); |
|
| 521 | - |
|
| 522 | - /** |
|
| 523 | - * Reshares for this user are shares where they are the owner. |
|
| 524 | - */ |
|
| 525 | - if ($reshares === false) { |
|
| 526 | - $qb->andWhere($qb->expr()->eq('uid_initiator', $qb->createNamedParameter($userId))); |
|
| 527 | - } else { |
|
| 528 | - $qb->andWhere( |
|
| 529 | - $qb->expr()->orX( |
|
| 530 | - $qb->expr()->eq('uid_owner', $qb->createNamedParameter($userId)), |
|
| 531 | - $qb->expr()->eq('uid_initiator', $qb->createNamedParameter($userId)) |
|
| 532 | - ) |
|
| 533 | - ); |
|
| 534 | - } |
|
| 535 | - |
|
| 536 | - if ($node !== null) { |
|
| 537 | - $qb->andWhere($qb->expr()->eq('file_source', $qb->createNamedParameter($node->getId()))); |
|
| 538 | - } |
|
| 539 | - |
|
| 540 | - if ($limit !== -1) { |
|
| 541 | - $qb->setMaxResults($limit); |
|
| 542 | - } |
|
| 543 | - |
|
| 544 | - $qb->setFirstResult($offset); |
|
| 545 | - $qb->orderBy('id'); |
|
| 546 | - |
|
| 547 | - $cursor = $qb->execute(); |
|
| 548 | - $shares = []; |
|
| 549 | - while($data = $cursor->fetch()) { |
|
| 550 | - $shares[] = $this->createShare($data); |
|
| 551 | - } |
|
| 552 | - $cursor->closeCursor(); |
|
| 553 | - |
|
| 554 | - return $shares; |
|
| 555 | - } |
|
| 556 | - |
|
| 557 | - /** |
|
| 558 | - * @inheritdoc |
|
| 559 | - */ |
|
| 560 | - public function getShareById($id, $recipientId = null) { |
|
| 561 | - $qb = $this->dbConn->getQueryBuilder(); |
|
| 562 | - |
|
| 563 | - $qb->select('*') |
|
| 564 | - ->from('share') |
|
| 565 | - ->where($qb->expr()->eq('id', $qb->createNamedParameter($id))) |
|
| 566 | - ->andWhere( |
|
| 567 | - $qb->expr()->in( |
|
| 568 | - 'share_type', |
|
| 569 | - $qb->createNamedParameter([ |
|
| 570 | - \OCP\Share::SHARE_TYPE_USER, |
|
| 571 | - \OCP\Share::SHARE_TYPE_GROUP, |
|
| 572 | - \OCP\Share::SHARE_TYPE_LINK, |
|
| 573 | - ], IQueryBuilder::PARAM_INT_ARRAY) |
|
| 574 | - ) |
|
| 575 | - ) |
|
| 576 | - ->andWhere($qb->expr()->orX( |
|
| 577 | - $qb->expr()->eq('item_type', $qb->createNamedParameter('file')), |
|
| 578 | - $qb->expr()->eq('item_type', $qb->createNamedParameter('folder')) |
|
| 579 | - )); |
|
| 580 | - |
|
| 581 | - $cursor = $qb->execute(); |
|
| 582 | - $data = $cursor->fetch(); |
|
| 583 | - $cursor->closeCursor(); |
|
| 584 | - |
|
| 585 | - if ($data === false) { |
|
| 586 | - throw new ShareNotFound(); |
|
| 587 | - } |
|
| 588 | - |
|
| 589 | - try { |
|
| 590 | - $share = $this->createShare($data); |
|
| 591 | - } catch (InvalidShare $e) { |
|
| 592 | - throw new ShareNotFound(); |
|
| 593 | - } |
|
| 594 | - |
|
| 595 | - // If the recipient is set for a group share resolve to that user |
|
| 596 | - if ($recipientId !== null && $share->getShareType() === \OCP\Share::SHARE_TYPE_GROUP) { |
|
| 597 | - $share = $this->resolveGroupShares([$share], $recipientId)[0]; |
|
| 598 | - } |
|
| 599 | - |
|
| 600 | - return $share; |
|
| 601 | - } |
|
| 602 | - |
|
| 603 | - /** |
|
| 604 | - * Get shares for a given path |
|
| 605 | - * |
|
| 606 | - * @param \OCP\Files\Node $path |
|
| 607 | - * @return \OCP\Share\IShare[] |
|
| 608 | - */ |
|
| 609 | - public function getSharesByPath(Node $path) { |
|
| 610 | - $qb = $this->dbConn->getQueryBuilder(); |
|
| 611 | - |
|
| 612 | - $cursor = $qb->select('*') |
|
| 613 | - ->from('share') |
|
| 614 | - ->andWhere($qb->expr()->eq('file_source', $qb->createNamedParameter($path->getId()))) |
|
| 615 | - ->andWhere( |
|
| 616 | - $qb->expr()->orX( |
|
| 617 | - $qb->expr()->eq('share_type', $qb->createNamedParameter(\OCP\Share::SHARE_TYPE_USER)), |
|
| 618 | - $qb->expr()->eq('share_type', $qb->createNamedParameter(\OCP\Share::SHARE_TYPE_GROUP)) |
|
| 619 | - ) |
|
| 620 | - ) |
|
| 621 | - ->andWhere($qb->expr()->orX( |
|
| 622 | - $qb->expr()->eq('item_type', $qb->createNamedParameter('file')), |
|
| 623 | - $qb->expr()->eq('item_type', $qb->createNamedParameter('folder')) |
|
| 624 | - )) |
|
| 625 | - ->execute(); |
|
| 626 | - |
|
| 627 | - $shares = []; |
|
| 628 | - while($data = $cursor->fetch()) { |
|
| 629 | - $shares[] = $this->createShare($data); |
|
| 630 | - } |
|
| 631 | - $cursor->closeCursor(); |
|
| 632 | - |
|
| 633 | - return $shares; |
|
| 634 | - } |
|
| 635 | - |
|
| 636 | - /** |
|
| 637 | - * Returns whether the given database result can be interpreted as |
|
| 638 | - * a share with accessible file (not trashed, not deleted) |
|
| 639 | - */ |
|
| 640 | - private function isAccessibleResult($data) { |
|
| 641 | - // exclude shares leading to deleted file entries |
|
| 642 | - if ($data['fileid'] === null) { |
|
| 643 | - return false; |
|
| 644 | - } |
|
| 645 | - |
|
| 646 | - // exclude shares leading to trashbin on home storages |
|
| 647 | - $pathSections = explode('/', $data['path'], 2); |
|
| 648 | - // FIXME: would not detect rare md5'd home storage case properly |
|
| 649 | - if ($pathSections[0] !== 'files' |
|
| 650 | - && in_array(explode(':', $data['storage_string_id'], 2)[0], array('home', 'object'))) { |
|
| 651 | - return false; |
|
| 652 | - } |
|
| 653 | - return true; |
|
| 654 | - } |
|
| 655 | - |
|
| 656 | - /** |
|
| 657 | - * @inheritdoc |
|
| 658 | - */ |
|
| 659 | - public function getSharedWith($userId, $shareType, $node, $limit, $offset) { |
|
| 660 | - /** @var Share[] $shares */ |
|
| 661 | - $shares = []; |
|
| 662 | - |
|
| 663 | - if ($shareType === \OCP\Share::SHARE_TYPE_USER) { |
|
| 664 | - //Get shares directly with this user |
|
| 665 | - $qb = $this->dbConn->getQueryBuilder(); |
|
| 666 | - $qb->select('s.*', |
|
| 667 | - 'f.fileid', 'f.path', 'f.permissions AS f_permissions', 'f.storage', 'f.path_hash', |
|
| 668 | - 'f.parent AS f_parent', 'f.name', 'f.mimetype', 'f.mimepart', 'f.size', 'f.mtime', 'f.storage_mtime', |
|
| 669 | - 'f.encrypted', 'f.unencrypted_size', 'f.etag', 'f.checksum' |
|
| 670 | - ) |
|
| 671 | - ->selectAlias('st.id', 'storage_string_id') |
|
| 672 | - ->from('share', 's') |
|
| 673 | - ->leftJoin('s', 'filecache', 'f', $qb->expr()->eq('s.file_source', 'f.fileid')) |
|
| 674 | - ->leftJoin('f', 'storages', 'st', $qb->expr()->eq('f.storage', 'st.numeric_id')); |
|
| 675 | - |
|
| 676 | - // Order by id |
|
| 677 | - $qb->orderBy('s.id'); |
|
| 678 | - |
|
| 679 | - // Set limit and offset |
|
| 680 | - if ($limit !== -1) { |
|
| 681 | - $qb->setMaxResults($limit); |
|
| 682 | - } |
|
| 683 | - $qb->setFirstResult($offset); |
|
| 684 | - |
|
| 685 | - $qb->where($qb->expr()->eq('share_type', $qb->createNamedParameter(\OCP\Share::SHARE_TYPE_USER))) |
|
| 686 | - ->andWhere($qb->expr()->eq('share_with', $qb->createNamedParameter($userId))) |
|
| 687 | - ->andWhere($qb->expr()->orX( |
|
| 688 | - $qb->expr()->eq('item_type', $qb->createNamedParameter('file')), |
|
| 689 | - $qb->expr()->eq('item_type', $qb->createNamedParameter('folder')) |
|
| 690 | - )); |
|
| 691 | - |
|
| 692 | - // Filter by node if provided |
|
| 693 | - if ($node !== null) { |
|
| 694 | - $qb->andWhere($qb->expr()->eq('file_source', $qb->createNamedParameter($node->getId()))); |
|
| 695 | - } |
|
| 696 | - |
|
| 697 | - $cursor = $qb->execute(); |
|
| 698 | - |
|
| 699 | - while($data = $cursor->fetch()) { |
|
| 700 | - if ($this->isAccessibleResult($data)) { |
|
| 701 | - $shares[] = $this->createShare($data); |
|
| 702 | - } |
|
| 703 | - } |
|
| 704 | - $cursor->closeCursor(); |
|
| 705 | - |
|
| 706 | - } else if ($shareType === \OCP\Share::SHARE_TYPE_GROUP) { |
|
| 707 | - $user = $this->userManager->get($userId); |
|
| 708 | - $allGroups = $this->groupManager->getUserGroups($user); |
|
| 709 | - |
|
| 710 | - /** @var Share[] $shares2 */ |
|
| 711 | - $shares2 = []; |
|
| 712 | - |
|
| 713 | - $start = 0; |
|
| 714 | - while(true) { |
|
| 715 | - $groups = array_slice($allGroups, $start, 100); |
|
| 716 | - $start += 100; |
|
| 717 | - |
|
| 718 | - if ($groups === []) { |
|
| 719 | - break; |
|
| 720 | - } |
|
| 721 | - |
|
| 722 | - $qb = $this->dbConn->getQueryBuilder(); |
|
| 723 | - $qb->select('s.*', |
|
| 724 | - 'f.fileid', 'f.path', 'f.permissions AS f_permissions', 'f.storage', 'f.path_hash', |
|
| 725 | - 'f.parent AS f_parent', 'f.name', 'f.mimetype', 'f.mimepart', 'f.size', 'f.mtime', 'f.storage_mtime', |
|
| 726 | - 'f.encrypted', 'f.unencrypted_size', 'f.etag', 'f.checksum' |
|
| 727 | - ) |
|
| 728 | - ->selectAlias('st.id', 'storage_string_id') |
|
| 729 | - ->from('share', 's') |
|
| 730 | - ->leftJoin('s', 'filecache', 'f', $qb->expr()->eq('s.file_source', 'f.fileid')) |
|
| 731 | - ->leftJoin('f', 'storages', 'st', $qb->expr()->eq('f.storage', 'st.numeric_id')) |
|
| 732 | - ->orderBy('s.id') |
|
| 733 | - ->setFirstResult(0); |
|
| 734 | - |
|
| 735 | - if ($limit !== -1) { |
|
| 736 | - $qb->setMaxResults($limit - count($shares)); |
|
| 737 | - } |
|
| 738 | - |
|
| 739 | - // Filter by node if provided |
|
| 740 | - if ($node !== null) { |
|
| 741 | - $qb->andWhere($qb->expr()->eq('file_source', $qb->createNamedParameter($node->getId()))); |
|
| 742 | - } |
|
| 743 | - |
|
| 744 | - $groups = array_map(function(IGroup $group) { return $group->getGID(); }, $groups); |
|
| 745 | - |
|
| 746 | - $qb->andWhere($qb->expr()->eq('share_type', $qb->createNamedParameter(\OCP\Share::SHARE_TYPE_GROUP))) |
|
| 747 | - ->andWhere($qb->expr()->in('share_with', $qb->createNamedParameter( |
|
| 748 | - $groups, |
|
| 749 | - IQueryBuilder::PARAM_STR_ARRAY |
|
| 750 | - ))) |
|
| 751 | - ->andWhere($qb->expr()->orX( |
|
| 752 | - $qb->expr()->eq('item_type', $qb->createNamedParameter('file')), |
|
| 753 | - $qb->expr()->eq('item_type', $qb->createNamedParameter('folder')) |
|
| 754 | - )); |
|
| 755 | - |
|
| 756 | - $cursor = $qb->execute(); |
|
| 757 | - while($data = $cursor->fetch()) { |
|
| 758 | - if ($offset > 0) { |
|
| 759 | - $offset--; |
|
| 760 | - continue; |
|
| 761 | - } |
|
| 762 | - |
|
| 763 | - if ($this->isAccessibleResult($data)) { |
|
| 764 | - $shares2[] = $this->createShare($data); |
|
| 765 | - } |
|
| 766 | - } |
|
| 767 | - $cursor->closeCursor(); |
|
| 768 | - } |
|
| 769 | - |
|
| 770 | - /* |
|
| 359 | + if ($data === false) { |
|
| 360 | + $qb = $this->dbConn->getQueryBuilder(); |
|
| 361 | + |
|
| 362 | + $type = $share->getNode() instanceof \OCP\Files\File ? 'file' : 'folder'; |
|
| 363 | + |
|
| 364 | + //Insert new share |
|
| 365 | + $qb->insert('share') |
|
| 366 | + ->values([ |
|
| 367 | + 'share_type' => $qb->createNamedParameter(self::SHARE_TYPE_USERGROUP), |
|
| 368 | + 'share_with' => $qb->createNamedParameter($recipient), |
|
| 369 | + 'uid_owner' => $qb->createNamedParameter($share->getShareOwner()), |
|
| 370 | + 'uid_initiator' => $qb->createNamedParameter($share->getSharedBy()), |
|
| 371 | + 'parent' => $qb->createNamedParameter($share->getId()), |
|
| 372 | + 'item_type' => $qb->createNamedParameter($type), |
|
| 373 | + 'item_source' => $qb->createNamedParameter($share->getNode()->getId()), |
|
| 374 | + 'file_source' => $qb->createNamedParameter($share->getNode()->getId()), |
|
| 375 | + 'file_target' => $qb->createNamedParameter($share->getTarget()), |
|
| 376 | + 'permissions' => $qb->createNamedParameter(0), |
|
| 377 | + 'stime' => $qb->createNamedParameter($share->getShareTime()->getTimestamp()), |
|
| 378 | + ])->execute(); |
|
| 379 | + |
|
| 380 | + } else if ($data['permissions'] !== 0) { |
|
| 381 | + |
|
| 382 | + // Update existing usergroup share |
|
| 383 | + $qb = $this->dbConn->getQueryBuilder(); |
|
| 384 | + $qb->update('share') |
|
| 385 | + ->set('permissions', $qb->createNamedParameter(0)) |
|
| 386 | + ->where($qb->expr()->eq('id', $qb->createNamedParameter($data['id']))) |
|
| 387 | + ->execute(); |
|
| 388 | + } |
|
| 389 | + |
|
| 390 | + } else if ($share->getShareType() === \OCP\Share::SHARE_TYPE_USER) { |
|
| 391 | + |
|
| 392 | + if ($share->getSharedWith() !== $recipient) { |
|
| 393 | + throw new ProviderException('Recipient does not match'); |
|
| 394 | + } |
|
| 395 | + |
|
| 396 | + // We can just delete user and link shares |
|
| 397 | + $this->delete($share); |
|
| 398 | + } else { |
|
| 399 | + throw new ProviderException('Invalid shareType'); |
|
| 400 | + } |
|
| 401 | + } |
|
| 402 | + |
|
| 403 | + /** |
|
| 404 | + * @inheritdoc |
|
| 405 | + */ |
|
| 406 | + public function move(\OCP\Share\IShare $share, $recipient) { |
|
| 407 | + if ($share->getShareType() === \OCP\Share::SHARE_TYPE_USER) { |
|
| 408 | + // Just update the target |
|
| 409 | + $qb = $this->dbConn->getQueryBuilder(); |
|
| 410 | + $qb->update('share') |
|
| 411 | + ->set('file_target', $qb->createNamedParameter($share->getTarget())) |
|
| 412 | + ->where($qb->expr()->eq('id', $qb->createNamedParameter($share->getId()))) |
|
| 413 | + ->execute(); |
|
| 414 | + |
|
| 415 | + } else if ($share->getShareType() === \OCP\Share::SHARE_TYPE_GROUP) { |
|
| 416 | + |
|
| 417 | + // Check if there is a usergroup share |
|
| 418 | + $qb = $this->dbConn->getQueryBuilder(); |
|
| 419 | + $stmt = $qb->select('id') |
|
| 420 | + ->from('share') |
|
| 421 | + ->where($qb->expr()->eq('share_type', $qb->createNamedParameter(self::SHARE_TYPE_USERGROUP))) |
|
| 422 | + ->andWhere($qb->expr()->eq('share_with', $qb->createNamedParameter($recipient))) |
|
| 423 | + ->andWhere($qb->expr()->eq('parent', $qb->createNamedParameter($share->getId()))) |
|
| 424 | + ->andWhere($qb->expr()->orX( |
|
| 425 | + $qb->expr()->eq('item_type', $qb->createNamedParameter('file')), |
|
| 426 | + $qb->expr()->eq('item_type', $qb->createNamedParameter('folder')) |
|
| 427 | + )) |
|
| 428 | + ->setMaxResults(1) |
|
| 429 | + ->execute(); |
|
| 430 | + |
|
| 431 | + $data = $stmt->fetch(); |
|
| 432 | + $stmt->closeCursor(); |
|
| 433 | + |
|
| 434 | + if ($data === false) { |
|
| 435 | + // No usergroup share yet. Create one. |
|
| 436 | + $qb = $this->dbConn->getQueryBuilder(); |
|
| 437 | + $qb->insert('share') |
|
| 438 | + ->values([ |
|
| 439 | + 'share_type' => $qb->createNamedParameter(self::SHARE_TYPE_USERGROUP), |
|
| 440 | + 'share_with' => $qb->createNamedParameter($recipient), |
|
| 441 | + 'uid_owner' => $qb->createNamedParameter($share->getShareOwner()), |
|
| 442 | + 'uid_initiator' => $qb->createNamedParameter($share->getSharedBy()), |
|
| 443 | + 'parent' => $qb->createNamedParameter($share->getId()), |
|
| 444 | + 'item_type' => $qb->createNamedParameter($share->getNode() instanceof File ? 'file' : 'folder'), |
|
| 445 | + 'item_source' => $qb->createNamedParameter($share->getNode()->getId()), |
|
| 446 | + 'file_source' => $qb->createNamedParameter($share->getNode()->getId()), |
|
| 447 | + 'file_target' => $qb->createNamedParameter($share->getTarget()), |
|
| 448 | + 'permissions' => $qb->createNamedParameter($share->getPermissions()), |
|
| 449 | + 'stime' => $qb->createNamedParameter($share->getShareTime()->getTimestamp()), |
|
| 450 | + ])->execute(); |
|
| 451 | + } else { |
|
| 452 | + // Already a usergroup share. Update it. |
|
| 453 | + $qb = $this->dbConn->getQueryBuilder(); |
|
| 454 | + $qb->update('share') |
|
| 455 | + ->set('file_target', $qb->createNamedParameter($share->getTarget())) |
|
| 456 | + ->where($qb->expr()->eq('id', $qb->createNamedParameter($data['id']))) |
|
| 457 | + ->execute(); |
|
| 458 | + } |
|
| 459 | + } |
|
| 460 | + |
|
| 461 | + return $share; |
|
| 462 | + } |
|
| 463 | + |
|
| 464 | + public function getSharesInFolder($userId, Folder $node, $reshares) { |
|
| 465 | + $qb = $this->dbConn->getQueryBuilder(); |
|
| 466 | + $qb->select('*') |
|
| 467 | + ->from('share', 's') |
|
| 468 | + ->andWhere($qb->expr()->orX( |
|
| 469 | + $qb->expr()->eq('item_type', $qb->createNamedParameter('file')), |
|
| 470 | + $qb->expr()->eq('item_type', $qb->createNamedParameter('folder')) |
|
| 471 | + )); |
|
| 472 | + |
|
| 473 | + $qb->andWhere($qb->expr()->orX( |
|
| 474 | + $qb->expr()->eq('share_type', $qb->createNamedParameter(\OCP\Share::SHARE_TYPE_USER)), |
|
| 475 | + $qb->expr()->eq('share_type', $qb->createNamedParameter(\OCP\Share::SHARE_TYPE_GROUP)), |
|
| 476 | + $qb->expr()->eq('share_type', $qb->createNamedParameter(\OCP\Share::SHARE_TYPE_LINK)) |
|
| 477 | + )); |
|
| 478 | + |
|
| 479 | + /** |
|
| 480 | + * Reshares for this user are shares where they are the owner. |
|
| 481 | + */ |
|
| 482 | + if ($reshares === false) { |
|
| 483 | + $qb->andWhere($qb->expr()->eq('uid_initiator', $qb->createNamedParameter($userId))); |
|
| 484 | + } else { |
|
| 485 | + $qb->andWhere( |
|
| 486 | + $qb->expr()->orX( |
|
| 487 | + $qb->expr()->eq('uid_owner', $qb->createNamedParameter($userId)), |
|
| 488 | + $qb->expr()->eq('uid_initiator', $qb->createNamedParameter($userId)) |
|
| 489 | + ) |
|
| 490 | + ); |
|
| 491 | + } |
|
| 492 | + |
|
| 493 | + $qb->innerJoin('s', 'filecache' ,'f', 's.file_source = f.fileid'); |
|
| 494 | + $qb->andWhere($qb->expr()->eq('f.parent', $qb->createNamedParameter($node->getId()))); |
|
| 495 | + |
|
| 496 | + $qb->orderBy('id'); |
|
| 497 | + |
|
| 498 | + $cursor = $qb->execute(); |
|
| 499 | + $shares = []; |
|
| 500 | + while ($data = $cursor->fetch()) { |
|
| 501 | + $shares[$data['fileid']][] = $this->createShare($data); |
|
| 502 | + } |
|
| 503 | + $cursor->closeCursor(); |
|
| 504 | + |
|
| 505 | + return $shares; |
|
| 506 | + } |
|
| 507 | + |
|
| 508 | + /** |
|
| 509 | + * @inheritdoc |
|
| 510 | + */ |
|
| 511 | + public function getSharesBy($userId, $shareType, $node, $reshares, $limit, $offset) { |
|
| 512 | + $qb = $this->dbConn->getQueryBuilder(); |
|
| 513 | + $qb->select('*') |
|
| 514 | + ->from('share') |
|
| 515 | + ->andWhere($qb->expr()->orX( |
|
| 516 | + $qb->expr()->eq('item_type', $qb->createNamedParameter('file')), |
|
| 517 | + $qb->expr()->eq('item_type', $qb->createNamedParameter('folder')) |
|
| 518 | + )); |
|
| 519 | + |
|
| 520 | + $qb->andWhere($qb->expr()->eq('share_type', $qb->createNamedParameter($shareType))); |
|
| 521 | + |
|
| 522 | + /** |
|
| 523 | + * Reshares for this user are shares where they are the owner. |
|
| 524 | + */ |
|
| 525 | + if ($reshares === false) { |
|
| 526 | + $qb->andWhere($qb->expr()->eq('uid_initiator', $qb->createNamedParameter($userId))); |
|
| 527 | + } else { |
|
| 528 | + $qb->andWhere( |
|
| 529 | + $qb->expr()->orX( |
|
| 530 | + $qb->expr()->eq('uid_owner', $qb->createNamedParameter($userId)), |
|
| 531 | + $qb->expr()->eq('uid_initiator', $qb->createNamedParameter($userId)) |
|
| 532 | + ) |
|
| 533 | + ); |
|
| 534 | + } |
|
| 535 | + |
|
| 536 | + if ($node !== null) { |
|
| 537 | + $qb->andWhere($qb->expr()->eq('file_source', $qb->createNamedParameter($node->getId()))); |
|
| 538 | + } |
|
| 539 | + |
|
| 540 | + if ($limit !== -1) { |
|
| 541 | + $qb->setMaxResults($limit); |
|
| 542 | + } |
|
| 543 | + |
|
| 544 | + $qb->setFirstResult($offset); |
|
| 545 | + $qb->orderBy('id'); |
|
| 546 | + |
|
| 547 | + $cursor = $qb->execute(); |
|
| 548 | + $shares = []; |
|
| 549 | + while($data = $cursor->fetch()) { |
|
| 550 | + $shares[] = $this->createShare($data); |
|
| 551 | + } |
|
| 552 | + $cursor->closeCursor(); |
|
| 553 | + |
|
| 554 | + return $shares; |
|
| 555 | + } |
|
| 556 | + |
|
| 557 | + /** |
|
| 558 | + * @inheritdoc |
|
| 559 | + */ |
|
| 560 | + public function getShareById($id, $recipientId = null) { |
|
| 561 | + $qb = $this->dbConn->getQueryBuilder(); |
|
| 562 | + |
|
| 563 | + $qb->select('*') |
|
| 564 | + ->from('share') |
|
| 565 | + ->where($qb->expr()->eq('id', $qb->createNamedParameter($id))) |
|
| 566 | + ->andWhere( |
|
| 567 | + $qb->expr()->in( |
|
| 568 | + 'share_type', |
|
| 569 | + $qb->createNamedParameter([ |
|
| 570 | + \OCP\Share::SHARE_TYPE_USER, |
|
| 571 | + \OCP\Share::SHARE_TYPE_GROUP, |
|
| 572 | + \OCP\Share::SHARE_TYPE_LINK, |
|
| 573 | + ], IQueryBuilder::PARAM_INT_ARRAY) |
|
| 574 | + ) |
|
| 575 | + ) |
|
| 576 | + ->andWhere($qb->expr()->orX( |
|
| 577 | + $qb->expr()->eq('item_type', $qb->createNamedParameter('file')), |
|
| 578 | + $qb->expr()->eq('item_type', $qb->createNamedParameter('folder')) |
|
| 579 | + )); |
|
| 580 | + |
|
| 581 | + $cursor = $qb->execute(); |
|
| 582 | + $data = $cursor->fetch(); |
|
| 583 | + $cursor->closeCursor(); |
|
| 584 | + |
|
| 585 | + if ($data === false) { |
|
| 586 | + throw new ShareNotFound(); |
|
| 587 | + } |
|
| 588 | + |
|
| 589 | + try { |
|
| 590 | + $share = $this->createShare($data); |
|
| 591 | + } catch (InvalidShare $e) { |
|
| 592 | + throw new ShareNotFound(); |
|
| 593 | + } |
|
| 594 | + |
|
| 595 | + // If the recipient is set for a group share resolve to that user |
|
| 596 | + if ($recipientId !== null && $share->getShareType() === \OCP\Share::SHARE_TYPE_GROUP) { |
|
| 597 | + $share = $this->resolveGroupShares([$share], $recipientId)[0]; |
|
| 598 | + } |
|
| 599 | + |
|
| 600 | + return $share; |
|
| 601 | + } |
|
| 602 | + |
|
| 603 | + /** |
|
| 604 | + * Get shares for a given path |
|
| 605 | + * |
|
| 606 | + * @param \OCP\Files\Node $path |
|
| 607 | + * @return \OCP\Share\IShare[] |
|
| 608 | + */ |
|
| 609 | + public function getSharesByPath(Node $path) { |
|
| 610 | + $qb = $this->dbConn->getQueryBuilder(); |
|
| 611 | + |
|
| 612 | + $cursor = $qb->select('*') |
|
| 613 | + ->from('share') |
|
| 614 | + ->andWhere($qb->expr()->eq('file_source', $qb->createNamedParameter($path->getId()))) |
|
| 615 | + ->andWhere( |
|
| 616 | + $qb->expr()->orX( |
|
| 617 | + $qb->expr()->eq('share_type', $qb->createNamedParameter(\OCP\Share::SHARE_TYPE_USER)), |
|
| 618 | + $qb->expr()->eq('share_type', $qb->createNamedParameter(\OCP\Share::SHARE_TYPE_GROUP)) |
|
| 619 | + ) |
|
| 620 | + ) |
|
| 621 | + ->andWhere($qb->expr()->orX( |
|
| 622 | + $qb->expr()->eq('item_type', $qb->createNamedParameter('file')), |
|
| 623 | + $qb->expr()->eq('item_type', $qb->createNamedParameter('folder')) |
|
| 624 | + )) |
|
| 625 | + ->execute(); |
|
| 626 | + |
|
| 627 | + $shares = []; |
|
| 628 | + while($data = $cursor->fetch()) { |
|
| 629 | + $shares[] = $this->createShare($data); |
|
| 630 | + } |
|
| 631 | + $cursor->closeCursor(); |
|
| 632 | + |
|
| 633 | + return $shares; |
|
| 634 | + } |
|
| 635 | + |
|
| 636 | + /** |
|
| 637 | + * Returns whether the given database result can be interpreted as |
|
| 638 | + * a share with accessible file (not trashed, not deleted) |
|
| 639 | + */ |
|
| 640 | + private function isAccessibleResult($data) { |
|
| 641 | + // exclude shares leading to deleted file entries |
|
| 642 | + if ($data['fileid'] === null) { |
|
| 643 | + return false; |
|
| 644 | + } |
|
| 645 | + |
|
| 646 | + // exclude shares leading to trashbin on home storages |
|
| 647 | + $pathSections = explode('/', $data['path'], 2); |
|
| 648 | + // FIXME: would not detect rare md5'd home storage case properly |
|
| 649 | + if ($pathSections[0] !== 'files' |
|
| 650 | + && in_array(explode(':', $data['storage_string_id'], 2)[0], array('home', 'object'))) { |
|
| 651 | + return false; |
|
| 652 | + } |
|
| 653 | + return true; |
|
| 654 | + } |
|
| 655 | + |
|
| 656 | + /** |
|
| 657 | + * @inheritdoc |
|
| 658 | + */ |
|
| 659 | + public function getSharedWith($userId, $shareType, $node, $limit, $offset) { |
|
| 660 | + /** @var Share[] $shares */ |
|
| 661 | + $shares = []; |
|
| 662 | + |
|
| 663 | + if ($shareType === \OCP\Share::SHARE_TYPE_USER) { |
|
| 664 | + //Get shares directly with this user |
|
| 665 | + $qb = $this->dbConn->getQueryBuilder(); |
|
| 666 | + $qb->select('s.*', |
|
| 667 | + 'f.fileid', 'f.path', 'f.permissions AS f_permissions', 'f.storage', 'f.path_hash', |
|
| 668 | + 'f.parent AS f_parent', 'f.name', 'f.mimetype', 'f.mimepart', 'f.size', 'f.mtime', 'f.storage_mtime', |
|
| 669 | + 'f.encrypted', 'f.unencrypted_size', 'f.etag', 'f.checksum' |
|
| 670 | + ) |
|
| 671 | + ->selectAlias('st.id', 'storage_string_id') |
|
| 672 | + ->from('share', 's') |
|
| 673 | + ->leftJoin('s', 'filecache', 'f', $qb->expr()->eq('s.file_source', 'f.fileid')) |
|
| 674 | + ->leftJoin('f', 'storages', 'st', $qb->expr()->eq('f.storage', 'st.numeric_id')); |
|
| 675 | + |
|
| 676 | + // Order by id |
|
| 677 | + $qb->orderBy('s.id'); |
|
| 678 | + |
|
| 679 | + // Set limit and offset |
|
| 680 | + if ($limit !== -1) { |
|
| 681 | + $qb->setMaxResults($limit); |
|
| 682 | + } |
|
| 683 | + $qb->setFirstResult($offset); |
|
| 684 | + |
|
| 685 | + $qb->where($qb->expr()->eq('share_type', $qb->createNamedParameter(\OCP\Share::SHARE_TYPE_USER))) |
|
| 686 | + ->andWhere($qb->expr()->eq('share_with', $qb->createNamedParameter($userId))) |
|
| 687 | + ->andWhere($qb->expr()->orX( |
|
| 688 | + $qb->expr()->eq('item_type', $qb->createNamedParameter('file')), |
|
| 689 | + $qb->expr()->eq('item_type', $qb->createNamedParameter('folder')) |
|
| 690 | + )); |
|
| 691 | + |
|
| 692 | + // Filter by node if provided |
|
| 693 | + if ($node !== null) { |
|
| 694 | + $qb->andWhere($qb->expr()->eq('file_source', $qb->createNamedParameter($node->getId()))); |
|
| 695 | + } |
|
| 696 | + |
|
| 697 | + $cursor = $qb->execute(); |
|
| 698 | + |
|
| 699 | + while($data = $cursor->fetch()) { |
|
| 700 | + if ($this->isAccessibleResult($data)) { |
|
| 701 | + $shares[] = $this->createShare($data); |
|
| 702 | + } |
|
| 703 | + } |
|
| 704 | + $cursor->closeCursor(); |
|
| 705 | + |
|
| 706 | + } else if ($shareType === \OCP\Share::SHARE_TYPE_GROUP) { |
|
| 707 | + $user = $this->userManager->get($userId); |
|
| 708 | + $allGroups = $this->groupManager->getUserGroups($user); |
|
| 709 | + |
|
| 710 | + /** @var Share[] $shares2 */ |
|
| 711 | + $shares2 = []; |
|
| 712 | + |
|
| 713 | + $start = 0; |
|
| 714 | + while(true) { |
|
| 715 | + $groups = array_slice($allGroups, $start, 100); |
|
| 716 | + $start += 100; |
|
| 717 | + |
|
| 718 | + if ($groups === []) { |
|
| 719 | + break; |
|
| 720 | + } |
|
| 721 | + |
|
| 722 | + $qb = $this->dbConn->getQueryBuilder(); |
|
| 723 | + $qb->select('s.*', |
|
| 724 | + 'f.fileid', 'f.path', 'f.permissions AS f_permissions', 'f.storage', 'f.path_hash', |
|
| 725 | + 'f.parent AS f_parent', 'f.name', 'f.mimetype', 'f.mimepart', 'f.size', 'f.mtime', 'f.storage_mtime', |
|
| 726 | + 'f.encrypted', 'f.unencrypted_size', 'f.etag', 'f.checksum' |
|
| 727 | + ) |
|
| 728 | + ->selectAlias('st.id', 'storage_string_id') |
|
| 729 | + ->from('share', 's') |
|
| 730 | + ->leftJoin('s', 'filecache', 'f', $qb->expr()->eq('s.file_source', 'f.fileid')) |
|
| 731 | + ->leftJoin('f', 'storages', 'st', $qb->expr()->eq('f.storage', 'st.numeric_id')) |
|
| 732 | + ->orderBy('s.id') |
|
| 733 | + ->setFirstResult(0); |
|
| 734 | + |
|
| 735 | + if ($limit !== -1) { |
|
| 736 | + $qb->setMaxResults($limit - count($shares)); |
|
| 737 | + } |
|
| 738 | + |
|
| 739 | + // Filter by node if provided |
|
| 740 | + if ($node !== null) { |
|
| 741 | + $qb->andWhere($qb->expr()->eq('file_source', $qb->createNamedParameter($node->getId()))); |
|
| 742 | + } |
|
| 743 | + |
|
| 744 | + $groups = array_map(function(IGroup $group) { return $group->getGID(); }, $groups); |
|
| 745 | + |
|
| 746 | + $qb->andWhere($qb->expr()->eq('share_type', $qb->createNamedParameter(\OCP\Share::SHARE_TYPE_GROUP))) |
|
| 747 | + ->andWhere($qb->expr()->in('share_with', $qb->createNamedParameter( |
|
| 748 | + $groups, |
|
| 749 | + IQueryBuilder::PARAM_STR_ARRAY |
|
| 750 | + ))) |
|
| 751 | + ->andWhere($qb->expr()->orX( |
|
| 752 | + $qb->expr()->eq('item_type', $qb->createNamedParameter('file')), |
|
| 753 | + $qb->expr()->eq('item_type', $qb->createNamedParameter('folder')) |
|
| 754 | + )); |
|
| 755 | + |
|
| 756 | + $cursor = $qb->execute(); |
|
| 757 | + while($data = $cursor->fetch()) { |
|
| 758 | + if ($offset > 0) { |
|
| 759 | + $offset--; |
|
| 760 | + continue; |
|
| 761 | + } |
|
| 762 | + |
|
| 763 | + if ($this->isAccessibleResult($data)) { |
|
| 764 | + $shares2[] = $this->createShare($data); |
|
| 765 | + } |
|
| 766 | + } |
|
| 767 | + $cursor->closeCursor(); |
|
| 768 | + } |
|
| 769 | + |
|
| 770 | + /* |
|
| 771 | 771 | * Resolve all group shares to user specific shares |
| 772 | 772 | */ |
| 773 | - $shares = $this->resolveGroupShares($shares2, $userId); |
|
| 774 | - } else { |
|
| 775 | - throw new BackendError('Invalid backend'); |
|
| 776 | - } |
|
| 777 | - |
|
| 778 | - |
|
| 779 | - return $shares; |
|
| 780 | - } |
|
| 781 | - |
|
| 782 | - /** |
|
| 783 | - * Get a share by token |
|
| 784 | - * |
|
| 785 | - * @param string $token |
|
| 786 | - * @return \OCP\Share\IShare |
|
| 787 | - * @throws ShareNotFound |
|
| 788 | - */ |
|
| 789 | - public function getShareByToken($token) { |
|
| 790 | - $qb = $this->dbConn->getQueryBuilder(); |
|
| 791 | - |
|
| 792 | - $cursor = $qb->select('*') |
|
| 793 | - ->from('share') |
|
| 794 | - ->where($qb->expr()->eq('share_type', $qb->createNamedParameter(\OCP\Share::SHARE_TYPE_LINK))) |
|
| 795 | - ->andWhere($qb->expr()->eq('token', $qb->createNamedParameter($token))) |
|
| 796 | - ->andWhere($qb->expr()->orX( |
|
| 797 | - $qb->expr()->eq('item_type', $qb->createNamedParameter('file')), |
|
| 798 | - $qb->expr()->eq('item_type', $qb->createNamedParameter('folder')) |
|
| 799 | - )) |
|
| 800 | - ->execute(); |
|
| 801 | - |
|
| 802 | - $data = $cursor->fetch(); |
|
| 803 | - |
|
| 804 | - if ($data === false) { |
|
| 805 | - throw new ShareNotFound(); |
|
| 806 | - } |
|
| 807 | - |
|
| 808 | - try { |
|
| 809 | - $share = $this->createShare($data); |
|
| 810 | - } catch (InvalidShare $e) { |
|
| 811 | - throw new ShareNotFound(); |
|
| 812 | - } |
|
| 813 | - |
|
| 814 | - return $share; |
|
| 815 | - } |
|
| 816 | - |
|
| 817 | - /** |
|
| 818 | - * Create a share object from an database row |
|
| 819 | - * |
|
| 820 | - * @param mixed[] $data |
|
| 821 | - * @return \OCP\Share\IShare |
|
| 822 | - * @throws InvalidShare |
|
| 823 | - */ |
|
| 824 | - private function createShare($data) { |
|
| 825 | - $share = new Share($this->rootFolder, $this->userManager); |
|
| 826 | - $share->setId((int)$data['id']) |
|
| 827 | - ->setShareType((int)$data['share_type']) |
|
| 828 | - ->setPermissions((int)$data['permissions']) |
|
| 829 | - ->setTarget($data['file_target']) |
|
| 830 | - ->setMailSend((bool)$data['mail_send']); |
|
| 831 | - |
|
| 832 | - $shareTime = new \DateTime(); |
|
| 833 | - $shareTime->setTimestamp((int)$data['stime']); |
|
| 834 | - $share->setShareTime($shareTime); |
|
| 835 | - |
|
| 836 | - if ($share->getShareType() === \OCP\Share::SHARE_TYPE_USER) { |
|
| 837 | - $share->setSharedWith($data['share_with']); |
|
| 838 | - } else if ($share->getShareType() === \OCP\Share::SHARE_TYPE_GROUP) { |
|
| 839 | - $share->setSharedWith($data['share_with']); |
|
| 840 | - } else if ($share->getShareType() === \OCP\Share::SHARE_TYPE_LINK) { |
|
| 841 | - $share->setPassword($data['share_with']); |
|
| 842 | - $share->setToken($data['token']); |
|
| 843 | - } |
|
| 844 | - |
|
| 845 | - $share->setSharedBy($data['uid_initiator']); |
|
| 846 | - $share->setShareOwner($data['uid_owner']); |
|
| 847 | - |
|
| 848 | - $share->setNodeId((int)$data['file_source']); |
|
| 849 | - $share->setNodeType($data['item_type']); |
|
| 850 | - |
|
| 851 | - if ($data['expiration'] !== null) { |
|
| 852 | - $expiration = \DateTime::createFromFormat('Y-m-d H:i:s', $data['expiration']); |
|
| 853 | - $share->setExpirationDate($expiration); |
|
| 854 | - } |
|
| 855 | - |
|
| 856 | - if (isset($data['f_permissions'])) { |
|
| 857 | - $entryData = $data; |
|
| 858 | - $entryData['permissions'] = $entryData['f_permissions']; |
|
| 859 | - $entryData['parent'] = $entryData['f_parent'];; |
|
| 860 | - $share->setNodeCacheEntry(Cache::cacheEntryFromData($entryData, |
|
| 861 | - \OC::$server->getMimeTypeLoader())); |
|
| 862 | - } |
|
| 863 | - |
|
| 864 | - $share->setProviderId($this->identifier()); |
|
| 865 | - |
|
| 866 | - return $share; |
|
| 867 | - } |
|
| 868 | - |
|
| 869 | - /** |
|
| 870 | - * @param Share[] $shares |
|
| 871 | - * @param $userId |
|
| 872 | - * @return Share[] The updates shares if no update is found for a share return the original |
|
| 873 | - */ |
|
| 874 | - private function resolveGroupShares($shares, $userId) { |
|
| 875 | - $result = []; |
|
| 876 | - |
|
| 877 | - $start = 0; |
|
| 878 | - while(true) { |
|
| 879 | - /** @var Share[] $shareSlice */ |
|
| 880 | - $shareSlice = array_slice($shares, $start, 100); |
|
| 881 | - $start += 100; |
|
| 882 | - |
|
| 883 | - if ($shareSlice === []) { |
|
| 884 | - break; |
|
| 885 | - } |
|
| 886 | - |
|
| 887 | - /** @var int[] $ids */ |
|
| 888 | - $ids = []; |
|
| 889 | - /** @var Share[] $shareMap */ |
|
| 890 | - $shareMap = []; |
|
| 891 | - |
|
| 892 | - foreach ($shareSlice as $share) { |
|
| 893 | - $ids[] = (int)$share->getId(); |
|
| 894 | - $shareMap[$share->getId()] = $share; |
|
| 895 | - } |
|
| 896 | - |
|
| 897 | - $qb = $this->dbConn->getQueryBuilder(); |
|
| 898 | - |
|
| 899 | - $query = $qb->select('*') |
|
| 900 | - ->from('share') |
|
| 901 | - ->where($qb->expr()->in('parent', $qb->createNamedParameter($ids, IQueryBuilder::PARAM_INT_ARRAY))) |
|
| 902 | - ->andWhere($qb->expr()->eq('share_with', $qb->createNamedParameter($userId))) |
|
| 903 | - ->andWhere($qb->expr()->orX( |
|
| 904 | - $qb->expr()->eq('item_type', $qb->createNamedParameter('file')), |
|
| 905 | - $qb->expr()->eq('item_type', $qb->createNamedParameter('folder')) |
|
| 906 | - )); |
|
| 907 | - |
|
| 908 | - $stmt = $query->execute(); |
|
| 909 | - |
|
| 910 | - while($data = $stmt->fetch()) { |
|
| 911 | - $shareMap[$data['parent']]->setPermissions((int)$data['permissions']); |
|
| 912 | - $shareMap[$data['parent']]->setTarget($data['file_target']); |
|
| 913 | - } |
|
| 914 | - |
|
| 915 | - $stmt->closeCursor(); |
|
| 916 | - |
|
| 917 | - foreach ($shareMap as $share) { |
|
| 918 | - $result[] = $share; |
|
| 919 | - } |
|
| 920 | - } |
|
| 921 | - |
|
| 922 | - return $result; |
|
| 923 | - } |
|
| 924 | - |
|
| 925 | - /** |
|
| 926 | - * A user is deleted from the system |
|
| 927 | - * So clean up the relevant shares. |
|
| 928 | - * |
|
| 929 | - * @param string $uid |
|
| 930 | - * @param int $shareType |
|
| 931 | - */ |
|
| 932 | - public function userDeleted($uid, $shareType) { |
|
| 933 | - $qb = $this->dbConn->getQueryBuilder(); |
|
| 934 | - |
|
| 935 | - $qb->delete('share'); |
|
| 936 | - |
|
| 937 | - if ($shareType === \OCP\Share::SHARE_TYPE_USER) { |
|
| 938 | - /* |
|
| 773 | + $shares = $this->resolveGroupShares($shares2, $userId); |
|
| 774 | + } else { |
|
| 775 | + throw new BackendError('Invalid backend'); |
|
| 776 | + } |
|
| 777 | + |
|
| 778 | + |
|
| 779 | + return $shares; |
|
| 780 | + } |
|
| 781 | + |
|
| 782 | + /** |
|
| 783 | + * Get a share by token |
|
| 784 | + * |
|
| 785 | + * @param string $token |
|
| 786 | + * @return \OCP\Share\IShare |
|
| 787 | + * @throws ShareNotFound |
|
| 788 | + */ |
|
| 789 | + public function getShareByToken($token) { |
|
| 790 | + $qb = $this->dbConn->getQueryBuilder(); |
|
| 791 | + |
|
| 792 | + $cursor = $qb->select('*') |
|
| 793 | + ->from('share') |
|
| 794 | + ->where($qb->expr()->eq('share_type', $qb->createNamedParameter(\OCP\Share::SHARE_TYPE_LINK))) |
|
| 795 | + ->andWhere($qb->expr()->eq('token', $qb->createNamedParameter($token))) |
|
| 796 | + ->andWhere($qb->expr()->orX( |
|
| 797 | + $qb->expr()->eq('item_type', $qb->createNamedParameter('file')), |
|
| 798 | + $qb->expr()->eq('item_type', $qb->createNamedParameter('folder')) |
|
| 799 | + )) |
|
| 800 | + ->execute(); |
|
| 801 | + |
|
| 802 | + $data = $cursor->fetch(); |
|
| 803 | + |
|
| 804 | + if ($data === false) { |
|
| 805 | + throw new ShareNotFound(); |
|
| 806 | + } |
|
| 807 | + |
|
| 808 | + try { |
|
| 809 | + $share = $this->createShare($data); |
|
| 810 | + } catch (InvalidShare $e) { |
|
| 811 | + throw new ShareNotFound(); |
|
| 812 | + } |
|
| 813 | + |
|
| 814 | + return $share; |
|
| 815 | + } |
|
| 816 | + |
|
| 817 | + /** |
|
| 818 | + * Create a share object from an database row |
|
| 819 | + * |
|
| 820 | + * @param mixed[] $data |
|
| 821 | + * @return \OCP\Share\IShare |
|
| 822 | + * @throws InvalidShare |
|
| 823 | + */ |
|
| 824 | + private function createShare($data) { |
|
| 825 | + $share = new Share($this->rootFolder, $this->userManager); |
|
| 826 | + $share->setId((int)$data['id']) |
|
| 827 | + ->setShareType((int)$data['share_type']) |
|
| 828 | + ->setPermissions((int)$data['permissions']) |
|
| 829 | + ->setTarget($data['file_target']) |
|
| 830 | + ->setMailSend((bool)$data['mail_send']); |
|
| 831 | + |
|
| 832 | + $shareTime = new \DateTime(); |
|
| 833 | + $shareTime->setTimestamp((int)$data['stime']); |
|
| 834 | + $share->setShareTime($shareTime); |
|
| 835 | + |
|
| 836 | + if ($share->getShareType() === \OCP\Share::SHARE_TYPE_USER) { |
|
| 837 | + $share->setSharedWith($data['share_with']); |
|
| 838 | + } else if ($share->getShareType() === \OCP\Share::SHARE_TYPE_GROUP) { |
|
| 839 | + $share->setSharedWith($data['share_with']); |
|
| 840 | + } else if ($share->getShareType() === \OCP\Share::SHARE_TYPE_LINK) { |
|
| 841 | + $share->setPassword($data['share_with']); |
|
| 842 | + $share->setToken($data['token']); |
|
| 843 | + } |
|
| 844 | + |
|
| 845 | + $share->setSharedBy($data['uid_initiator']); |
|
| 846 | + $share->setShareOwner($data['uid_owner']); |
|
| 847 | + |
|
| 848 | + $share->setNodeId((int)$data['file_source']); |
|
| 849 | + $share->setNodeType($data['item_type']); |
|
| 850 | + |
|
| 851 | + if ($data['expiration'] !== null) { |
|
| 852 | + $expiration = \DateTime::createFromFormat('Y-m-d H:i:s', $data['expiration']); |
|
| 853 | + $share->setExpirationDate($expiration); |
|
| 854 | + } |
|
| 855 | + |
|
| 856 | + if (isset($data['f_permissions'])) { |
|
| 857 | + $entryData = $data; |
|
| 858 | + $entryData['permissions'] = $entryData['f_permissions']; |
|
| 859 | + $entryData['parent'] = $entryData['f_parent'];; |
|
| 860 | + $share->setNodeCacheEntry(Cache::cacheEntryFromData($entryData, |
|
| 861 | + \OC::$server->getMimeTypeLoader())); |
|
| 862 | + } |
|
| 863 | + |
|
| 864 | + $share->setProviderId($this->identifier()); |
|
| 865 | + |
|
| 866 | + return $share; |
|
| 867 | + } |
|
| 868 | + |
|
| 869 | + /** |
|
| 870 | + * @param Share[] $shares |
|
| 871 | + * @param $userId |
|
| 872 | + * @return Share[] The updates shares if no update is found for a share return the original |
|
| 873 | + */ |
|
| 874 | + private function resolveGroupShares($shares, $userId) { |
|
| 875 | + $result = []; |
|
| 876 | + |
|
| 877 | + $start = 0; |
|
| 878 | + while(true) { |
|
| 879 | + /** @var Share[] $shareSlice */ |
|
| 880 | + $shareSlice = array_slice($shares, $start, 100); |
|
| 881 | + $start += 100; |
|
| 882 | + |
|
| 883 | + if ($shareSlice === []) { |
|
| 884 | + break; |
|
| 885 | + } |
|
| 886 | + |
|
| 887 | + /** @var int[] $ids */ |
|
| 888 | + $ids = []; |
|
| 889 | + /** @var Share[] $shareMap */ |
|
| 890 | + $shareMap = []; |
|
| 891 | + |
|
| 892 | + foreach ($shareSlice as $share) { |
|
| 893 | + $ids[] = (int)$share->getId(); |
|
| 894 | + $shareMap[$share->getId()] = $share; |
|
| 895 | + } |
|
| 896 | + |
|
| 897 | + $qb = $this->dbConn->getQueryBuilder(); |
|
| 898 | + |
|
| 899 | + $query = $qb->select('*') |
|
| 900 | + ->from('share') |
|
| 901 | + ->where($qb->expr()->in('parent', $qb->createNamedParameter($ids, IQueryBuilder::PARAM_INT_ARRAY))) |
|
| 902 | + ->andWhere($qb->expr()->eq('share_with', $qb->createNamedParameter($userId))) |
|
| 903 | + ->andWhere($qb->expr()->orX( |
|
| 904 | + $qb->expr()->eq('item_type', $qb->createNamedParameter('file')), |
|
| 905 | + $qb->expr()->eq('item_type', $qb->createNamedParameter('folder')) |
|
| 906 | + )); |
|
| 907 | + |
|
| 908 | + $stmt = $query->execute(); |
|
| 909 | + |
|
| 910 | + while($data = $stmt->fetch()) { |
|
| 911 | + $shareMap[$data['parent']]->setPermissions((int)$data['permissions']); |
|
| 912 | + $shareMap[$data['parent']]->setTarget($data['file_target']); |
|
| 913 | + } |
|
| 914 | + |
|
| 915 | + $stmt->closeCursor(); |
|
| 916 | + |
|
| 917 | + foreach ($shareMap as $share) { |
|
| 918 | + $result[] = $share; |
|
| 919 | + } |
|
| 920 | + } |
|
| 921 | + |
|
| 922 | + return $result; |
|
| 923 | + } |
|
| 924 | + |
|
| 925 | + /** |
|
| 926 | + * A user is deleted from the system |
|
| 927 | + * So clean up the relevant shares. |
|
| 928 | + * |
|
| 929 | + * @param string $uid |
|
| 930 | + * @param int $shareType |
|
| 931 | + */ |
|
| 932 | + public function userDeleted($uid, $shareType) { |
|
| 933 | + $qb = $this->dbConn->getQueryBuilder(); |
|
| 934 | + |
|
| 935 | + $qb->delete('share'); |
|
| 936 | + |
|
| 937 | + if ($shareType === \OCP\Share::SHARE_TYPE_USER) { |
|
| 938 | + /* |
|
| 939 | 939 | * Delete all user shares that are owned by this user |
| 940 | 940 | * or that are received by this user |
| 941 | 941 | */ |
| 942 | 942 | |
| 943 | - $qb->where($qb->expr()->eq('share_type', $qb->createNamedParameter(\OCP\Share::SHARE_TYPE_USER))); |
|
| 943 | + $qb->where($qb->expr()->eq('share_type', $qb->createNamedParameter(\OCP\Share::SHARE_TYPE_USER))); |
|
| 944 | 944 | |
| 945 | - $qb->andWhere( |
|
| 946 | - $qb->expr()->orX( |
|
| 947 | - $qb->expr()->eq('uid_owner', $qb->createNamedParameter($uid)), |
|
| 948 | - $qb->expr()->eq('share_with', $qb->createNamedParameter($uid)) |
|
| 949 | - ) |
|
| 950 | - ); |
|
| 951 | - } else if ($shareType === \OCP\Share::SHARE_TYPE_GROUP) { |
|
| 952 | - /* |
|
| 945 | + $qb->andWhere( |
|
| 946 | + $qb->expr()->orX( |
|
| 947 | + $qb->expr()->eq('uid_owner', $qb->createNamedParameter($uid)), |
|
| 948 | + $qb->expr()->eq('share_with', $qb->createNamedParameter($uid)) |
|
| 949 | + ) |
|
| 950 | + ); |
|
| 951 | + } else if ($shareType === \OCP\Share::SHARE_TYPE_GROUP) { |
|
| 952 | + /* |
|
| 953 | 953 | * Delete all group shares that are owned by this user |
| 954 | 954 | * Or special user group shares that are received by this user |
| 955 | 955 | */ |
| 956 | - $qb->where( |
|
| 957 | - $qb->expr()->andX( |
|
| 958 | - $qb->expr()->orX( |
|
| 959 | - $qb->expr()->eq('share_type', $qb->createNamedParameter(\OCP\Share::SHARE_TYPE_GROUP)), |
|
| 960 | - $qb->expr()->eq('share_type', $qb->createNamedParameter(self::SHARE_TYPE_USERGROUP)) |
|
| 961 | - ), |
|
| 962 | - $qb->expr()->eq('uid_owner', $qb->createNamedParameter($uid)) |
|
| 963 | - ) |
|
| 964 | - ); |
|
| 965 | - |
|
| 966 | - $qb->orWhere( |
|
| 967 | - $qb->expr()->andX( |
|
| 968 | - $qb->expr()->eq('share_type', $qb->createNamedParameter(self::SHARE_TYPE_USERGROUP)), |
|
| 969 | - $qb->expr()->eq('share_with', $qb->createNamedParameter($uid)) |
|
| 970 | - ) |
|
| 971 | - ); |
|
| 972 | - } else if ($shareType === \OCP\Share::SHARE_TYPE_LINK) { |
|
| 973 | - /* |
|
| 956 | + $qb->where( |
|
| 957 | + $qb->expr()->andX( |
|
| 958 | + $qb->expr()->orX( |
|
| 959 | + $qb->expr()->eq('share_type', $qb->createNamedParameter(\OCP\Share::SHARE_TYPE_GROUP)), |
|
| 960 | + $qb->expr()->eq('share_type', $qb->createNamedParameter(self::SHARE_TYPE_USERGROUP)) |
|
| 961 | + ), |
|
| 962 | + $qb->expr()->eq('uid_owner', $qb->createNamedParameter($uid)) |
|
| 963 | + ) |
|
| 964 | + ); |
|
| 965 | + |
|
| 966 | + $qb->orWhere( |
|
| 967 | + $qb->expr()->andX( |
|
| 968 | + $qb->expr()->eq('share_type', $qb->createNamedParameter(self::SHARE_TYPE_USERGROUP)), |
|
| 969 | + $qb->expr()->eq('share_with', $qb->createNamedParameter($uid)) |
|
| 970 | + ) |
|
| 971 | + ); |
|
| 972 | + } else if ($shareType === \OCP\Share::SHARE_TYPE_LINK) { |
|
| 973 | + /* |
|
| 974 | 974 | * Delete all link shares owned by this user. |
| 975 | 975 | * And all link shares initiated by this user (until #22327 is in) |
| 976 | 976 | */ |
| 977 | - $qb->where($qb->expr()->eq('share_type', $qb->createNamedParameter(\OCP\Share::SHARE_TYPE_LINK))); |
|
| 978 | - |
|
| 979 | - $qb->andWhere( |
|
| 980 | - $qb->expr()->orX( |
|
| 981 | - $qb->expr()->eq('uid_owner', $qb->createNamedParameter($uid)), |
|
| 982 | - $qb->expr()->eq('uid_initiator', $qb->createNamedParameter($uid)) |
|
| 983 | - ) |
|
| 984 | - ); |
|
| 985 | - } |
|
| 986 | - |
|
| 987 | - $qb->execute(); |
|
| 988 | - } |
|
| 989 | - |
|
| 990 | - /** |
|
| 991 | - * Delete all shares received by this group. As well as any custom group |
|
| 992 | - * shares for group members. |
|
| 993 | - * |
|
| 994 | - * @param string $gid |
|
| 995 | - */ |
|
| 996 | - public function groupDeleted($gid) { |
|
| 997 | - /* |
|
| 977 | + $qb->where($qb->expr()->eq('share_type', $qb->createNamedParameter(\OCP\Share::SHARE_TYPE_LINK))); |
|
| 978 | + |
|
| 979 | + $qb->andWhere( |
|
| 980 | + $qb->expr()->orX( |
|
| 981 | + $qb->expr()->eq('uid_owner', $qb->createNamedParameter($uid)), |
|
| 982 | + $qb->expr()->eq('uid_initiator', $qb->createNamedParameter($uid)) |
|
| 983 | + ) |
|
| 984 | + ); |
|
| 985 | + } |
|
| 986 | + |
|
| 987 | + $qb->execute(); |
|
| 988 | + } |
|
| 989 | + |
|
| 990 | + /** |
|
| 991 | + * Delete all shares received by this group. As well as any custom group |
|
| 992 | + * shares for group members. |
|
| 993 | + * |
|
| 994 | + * @param string $gid |
|
| 995 | + */ |
|
| 996 | + public function groupDeleted($gid) { |
|
| 997 | + /* |
|
| 998 | 998 | * First delete all custom group shares for group members |
| 999 | 999 | */ |
| 1000 | - $qb = $this->dbConn->getQueryBuilder(); |
|
| 1001 | - $qb->select('id') |
|
| 1002 | - ->from('share') |
|
| 1003 | - ->where($qb->expr()->eq('share_type', $qb->createNamedParameter(\OCP\Share::SHARE_TYPE_GROUP))) |
|
| 1004 | - ->andWhere($qb->expr()->eq('share_with', $qb->createNamedParameter($gid))); |
|
| 1005 | - |
|
| 1006 | - $cursor = $qb->execute(); |
|
| 1007 | - $ids = []; |
|
| 1008 | - while($row = $cursor->fetch()) { |
|
| 1009 | - $ids[] = (int)$row['id']; |
|
| 1010 | - } |
|
| 1011 | - $cursor->closeCursor(); |
|
| 1012 | - |
|
| 1013 | - if (!empty($ids)) { |
|
| 1014 | - $chunks = array_chunk($ids, 100); |
|
| 1015 | - foreach ($chunks as $chunk) { |
|
| 1016 | - $qb->delete('share') |
|
| 1017 | - ->where($qb->expr()->eq('share_type', $qb->createNamedParameter(self::SHARE_TYPE_USERGROUP))) |
|
| 1018 | - ->andWhere($qb->expr()->in('parent', $qb->createNamedParameter($chunk, IQueryBuilder::PARAM_INT_ARRAY))); |
|
| 1019 | - $qb->execute(); |
|
| 1020 | - } |
|
| 1021 | - } |
|
| 1022 | - |
|
| 1023 | - /* |
|
| 1000 | + $qb = $this->dbConn->getQueryBuilder(); |
|
| 1001 | + $qb->select('id') |
|
| 1002 | + ->from('share') |
|
| 1003 | + ->where($qb->expr()->eq('share_type', $qb->createNamedParameter(\OCP\Share::SHARE_TYPE_GROUP))) |
|
| 1004 | + ->andWhere($qb->expr()->eq('share_with', $qb->createNamedParameter($gid))); |
|
| 1005 | + |
|
| 1006 | + $cursor = $qb->execute(); |
|
| 1007 | + $ids = []; |
|
| 1008 | + while($row = $cursor->fetch()) { |
|
| 1009 | + $ids[] = (int)$row['id']; |
|
| 1010 | + } |
|
| 1011 | + $cursor->closeCursor(); |
|
| 1012 | + |
|
| 1013 | + if (!empty($ids)) { |
|
| 1014 | + $chunks = array_chunk($ids, 100); |
|
| 1015 | + foreach ($chunks as $chunk) { |
|
| 1016 | + $qb->delete('share') |
|
| 1017 | + ->where($qb->expr()->eq('share_type', $qb->createNamedParameter(self::SHARE_TYPE_USERGROUP))) |
|
| 1018 | + ->andWhere($qb->expr()->in('parent', $qb->createNamedParameter($chunk, IQueryBuilder::PARAM_INT_ARRAY))); |
|
| 1019 | + $qb->execute(); |
|
| 1020 | + } |
|
| 1021 | + } |
|
| 1022 | + |
|
| 1023 | + /* |
|
| 1024 | 1024 | * Now delete all the group shares |
| 1025 | 1025 | */ |
| 1026 | - $qb = $this->dbConn->getQueryBuilder(); |
|
| 1027 | - $qb->delete('share') |
|
| 1028 | - ->where($qb->expr()->eq('share_type', $qb->createNamedParameter(\OCP\Share::SHARE_TYPE_GROUP))) |
|
| 1029 | - ->andWhere($qb->expr()->eq('share_with', $qb->createNamedParameter($gid))); |
|
| 1030 | - $qb->execute(); |
|
| 1031 | - } |
|
| 1032 | - |
|
| 1033 | - /** |
|
| 1034 | - * Delete custom group shares to this group for this user |
|
| 1035 | - * |
|
| 1036 | - * @param string $uid |
|
| 1037 | - * @param string $gid |
|
| 1038 | - */ |
|
| 1039 | - public function userDeletedFromGroup($uid, $gid) { |
|
| 1040 | - /* |
|
| 1026 | + $qb = $this->dbConn->getQueryBuilder(); |
|
| 1027 | + $qb->delete('share') |
|
| 1028 | + ->where($qb->expr()->eq('share_type', $qb->createNamedParameter(\OCP\Share::SHARE_TYPE_GROUP))) |
|
| 1029 | + ->andWhere($qb->expr()->eq('share_with', $qb->createNamedParameter($gid))); |
|
| 1030 | + $qb->execute(); |
|
| 1031 | + } |
|
| 1032 | + |
|
| 1033 | + /** |
|
| 1034 | + * Delete custom group shares to this group for this user |
|
| 1035 | + * |
|
| 1036 | + * @param string $uid |
|
| 1037 | + * @param string $gid |
|
| 1038 | + */ |
|
| 1039 | + public function userDeletedFromGroup($uid, $gid) { |
|
| 1040 | + /* |
|
| 1041 | 1041 | * Get all group shares |
| 1042 | 1042 | */ |
| 1043 | - $qb = $this->dbConn->getQueryBuilder(); |
|
| 1044 | - $qb->select('id') |
|
| 1045 | - ->from('share') |
|
| 1046 | - ->where($qb->expr()->eq('share_type', $qb->createNamedParameter(\OCP\Share::SHARE_TYPE_GROUP))) |
|
| 1047 | - ->andWhere($qb->expr()->eq('share_with', $qb->createNamedParameter($gid))); |
|
| 1048 | - |
|
| 1049 | - $cursor = $qb->execute(); |
|
| 1050 | - $ids = []; |
|
| 1051 | - while($row = $cursor->fetch()) { |
|
| 1052 | - $ids[] = (int)$row['id']; |
|
| 1053 | - } |
|
| 1054 | - $cursor->closeCursor(); |
|
| 1055 | - |
|
| 1056 | - if (!empty($ids)) { |
|
| 1057 | - $chunks = array_chunk($ids, 100); |
|
| 1058 | - foreach ($chunks as $chunk) { |
|
| 1059 | - /* |
|
| 1043 | + $qb = $this->dbConn->getQueryBuilder(); |
|
| 1044 | + $qb->select('id') |
|
| 1045 | + ->from('share') |
|
| 1046 | + ->where($qb->expr()->eq('share_type', $qb->createNamedParameter(\OCP\Share::SHARE_TYPE_GROUP))) |
|
| 1047 | + ->andWhere($qb->expr()->eq('share_with', $qb->createNamedParameter($gid))); |
|
| 1048 | + |
|
| 1049 | + $cursor = $qb->execute(); |
|
| 1050 | + $ids = []; |
|
| 1051 | + while($row = $cursor->fetch()) { |
|
| 1052 | + $ids[] = (int)$row['id']; |
|
| 1053 | + } |
|
| 1054 | + $cursor->closeCursor(); |
|
| 1055 | + |
|
| 1056 | + if (!empty($ids)) { |
|
| 1057 | + $chunks = array_chunk($ids, 100); |
|
| 1058 | + foreach ($chunks as $chunk) { |
|
| 1059 | + /* |
|
| 1060 | 1060 | * Delete all special shares wit this users for the found group shares |
| 1061 | 1061 | */ |
| 1062 | - $qb->delete('share') |
|
| 1063 | - ->where($qb->expr()->eq('share_type', $qb->createNamedParameter(self::SHARE_TYPE_USERGROUP))) |
|
| 1064 | - ->andWhere($qb->expr()->eq('share_with', $qb->createNamedParameter($uid))) |
|
| 1065 | - ->andWhere($qb->expr()->in('parent', $qb->createNamedParameter($chunk, IQueryBuilder::PARAM_INT_ARRAY))); |
|
| 1066 | - $qb->execute(); |
|
| 1067 | - } |
|
| 1068 | - } |
|
| 1069 | - } |
|
| 1062 | + $qb->delete('share') |
|
| 1063 | + ->where($qb->expr()->eq('share_type', $qb->createNamedParameter(self::SHARE_TYPE_USERGROUP))) |
|
| 1064 | + ->andWhere($qb->expr()->eq('share_with', $qb->createNamedParameter($uid))) |
|
| 1065 | + ->andWhere($qb->expr()->in('parent', $qb->createNamedParameter($chunk, IQueryBuilder::PARAM_INT_ARRAY))); |
|
| 1066 | + $qb->execute(); |
|
| 1067 | + } |
|
| 1068 | + } |
|
| 1069 | + } |
|
| 1070 | 1070 | } |
@@ -285,7 +285,7 @@ discard block |
||
| 285 | 285 | ->orderBy('id'); |
| 286 | 286 | |
| 287 | 287 | $cursor = $qb->execute(); |
| 288 | - while($data = $cursor->fetch()) { |
|
| 288 | + while ($data = $cursor->fetch()) { |
|
| 289 | 289 | $children[] = $this->createShare($data); |
| 290 | 290 | } |
| 291 | 291 | $cursor->closeCursor(); |
@@ -330,7 +330,7 @@ discard block |
||
| 330 | 330 | $user = $this->userManager->get($recipient); |
| 331 | 331 | |
| 332 | 332 | if (is_null($group)) { |
| 333 | - throw new ProviderException('Group "' . $share->getSharedWith() . '" does not exist'); |
|
| 333 | + throw new ProviderException('Group "'.$share->getSharedWith().'" does not exist'); |
|
| 334 | 334 | } |
| 335 | 335 | |
| 336 | 336 | if (!$group->inGroup($user)) { |
@@ -490,7 +490,7 @@ discard block |
||
| 490 | 490 | ); |
| 491 | 491 | } |
| 492 | 492 | |
| 493 | - $qb->innerJoin('s', 'filecache' ,'f', 's.file_source = f.fileid'); |
|
| 493 | + $qb->innerJoin('s', 'filecache', 'f', 's.file_source = f.fileid'); |
|
| 494 | 494 | $qb->andWhere($qb->expr()->eq('f.parent', $qb->createNamedParameter($node->getId()))); |
| 495 | 495 | |
| 496 | 496 | $qb->orderBy('id'); |
@@ -546,7 +546,7 @@ discard block |
||
| 546 | 546 | |
| 547 | 547 | $cursor = $qb->execute(); |
| 548 | 548 | $shares = []; |
| 549 | - while($data = $cursor->fetch()) { |
|
| 549 | + while ($data = $cursor->fetch()) { |
|
| 550 | 550 | $shares[] = $this->createShare($data); |
| 551 | 551 | } |
| 552 | 552 | $cursor->closeCursor(); |
@@ -625,7 +625,7 @@ discard block |
||
| 625 | 625 | ->execute(); |
| 626 | 626 | |
| 627 | 627 | $shares = []; |
| 628 | - while($data = $cursor->fetch()) { |
|
| 628 | + while ($data = $cursor->fetch()) { |
|
| 629 | 629 | $shares[] = $this->createShare($data); |
| 630 | 630 | } |
| 631 | 631 | $cursor->closeCursor(); |
@@ -696,7 +696,7 @@ discard block |
||
| 696 | 696 | |
| 697 | 697 | $cursor = $qb->execute(); |
| 698 | 698 | |
| 699 | - while($data = $cursor->fetch()) { |
|
| 699 | + while ($data = $cursor->fetch()) { |
|
| 700 | 700 | if ($this->isAccessibleResult($data)) { |
| 701 | 701 | $shares[] = $this->createShare($data); |
| 702 | 702 | } |
@@ -711,7 +711,7 @@ discard block |
||
| 711 | 711 | $shares2 = []; |
| 712 | 712 | |
| 713 | 713 | $start = 0; |
| 714 | - while(true) { |
|
| 714 | + while (true) { |
|
| 715 | 715 | $groups = array_slice($allGroups, $start, 100); |
| 716 | 716 | $start += 100; |
| 717 | 717 | |
@@ -754,7 +754,7 @@ discard block |
||
| 754 | 754 | )); |
| 755 | 755 | |
| 756 | 756 | $cursor = $qb->execute(); |
| 757 | - while($data = $cursor->fetch()) { |
|
| 757 | + while ($data = $cursor->fetch()) { |
|
| 758 | 758 | if ($offset > 0) { |
| 759 | 759 | $offset--; |
| 760 | 760 | continue; |
@@ -823,14 +823,14 @@ discard block |
||
| 823 | 823 | */ |
| 824 | 824 | private function createShare($data) { |
| 825 | 825 | $share = new Share($this->rootFolder, $this->userManager); |
| 826 | - $share->setId((int)$data['id']) |
|
| 827 | - ->setShareType((int)$data['share_type']) |
|
| 828 | - ->setPermissions((int)$data['permissions']) |
|
| 826 | + $share->setId((int) $data['id']) |
|
| 827 | + ->setShareType((int) $data['share_type']) |
|
| 828 | + ->setPermissions((int) $data['permissions']) |
|
| 829 | 829 | ->setTarget($data['file_target']) |
| 830 | - ->setMailSend((bool)$data['mail_send']); |
|
| 830 | + ->setMailSend((bool) $data['mail_send']); |
|
| 831 | 831 | |
| 832 | 832 | $shareTime = new \DateTime(); |
| 833 | - $shareTime->setTimestamp((int)$data['stime']); |
|
| 833 | + $shareTime->setTimestamp((int) $data['stime']); |
|
| 834 | 834 | $share->setShareTime($shareTime); |
| 835 | 835 | |
| 836 | 836 | if ($share->getShareType() === \OCP\Share::SHARE_TYPE_USER) { |
@@ -845,7 +845,7 @@ discard block |
||
| 845 | 845 | $share->setSharedBy($data['uid_initiator']); |
| 846 | 846 | $share->setShareOwner($data['uid_owner']); |
| 847 | 847 | |
| 848 | - $share->setNodeId((int)$data['file_source']); |
|
| 848 | + $share->setNodeId((int) $data['file_source']); |
|
| 849 | 849 | $share->setNodeType($data['item_type']); |
| 850 | 850 | |
| 851 | 851 | if ($data['expiration'] !== null) { |
@@ -856,7 +856,7 @@ discard block |
||
| 856 | 856 | if (isset($data['f_permissions'])) { |
| 857 | 857 | $entryData = $data; |
| 858 | 858 | $entryData['permissions'] = $entryData['f_permissions']; |
| 859 | - $entryData['parent'] = $entryData['f_parent'];; |
|
| 859 | + $entryData['parent'] = $entryData['f_parent']; ; |
|
| 860 | 860 | $share->setNodeCacheEntry(Cache::cacheEntryFromData($entryData, |
| 861 | 861 | \OC::$server->getMimeTypeLoader())); |
| 862 | 862 | } |
@@ -875,7 +875,7 @@ discard block |
||
| 875 | 875 | $result = []; |
| 876 | 876 | |
| 877 | 877 | $start = 0; |
| 878 | - while(true) { |
|
| 878 | + while (true) { |
|
| 879 | 879 | /** @var Share[] $shareSlice */ |
| 880 | 880 | $shareSlice = array_slice($shares, $start, 100); |
| 881 | 881 | $start += 100; |
@@ -890,7 +890,7 @@ discard block |
||
| 890 | 890 | $shareMap = []; |
| 891 | 891 | |
| 892 | 892 | foreach ($shareSlice as $share) { |
| 893 | - $ids[] = (int)$share->getId(); |
|
| 893 | + $ids[] = (int) $share->getId(); |
|
| 894 | 894 | $shareMap[$share->getId()] = $share; |
| 895 | 895 | } |
| 896 | 896 | |
@@ -907,8 +907,8 @@ discard block |
||
| 907 | 907 | |
| 908 | 908 | $stmt = $query->execute(); |
| 909 | 909 | |
| 910 | - while($data = $stmt->fetch()) { |
|
| 911 | - $shareMap[$data['parent']]->setPermissions((int)$data['permissions']); |
|
| 910 | + while ($data = $stmt->fetch()) { |
|
| 911 | + $shareMap[$data['parent']]->setPermissions((int) $data['permissions']); |
|
| 912 | 912 | $shareMap[$data['parent']]->setTarget($data['file_target']); |
| 913 | 913 | } |
| 914 | 914 | |
@@ -1005,8 +1005,8 @@ discard block |
||
| 1005 | 1005 | |
| 1006 | 1006 | $cursor = $qb->execute(); |
| 1007 | 1007 | $ids = []; |
| 1008 | - while($row = $cursor->fetch()) { |
|
| 1009 | - $ids[] = (int)$row['id']; |
|
| 1008 | + while ($row = $cursor->fetch()) { |
|
| 1009 | + $ids[] = (int) $row['id']; |
|
| 1010 | 1010 | } |
| 1011 | 1011 | $cursor->closeCursor(); |
| 1012 | 1012 | |
@@ -1048,8 +1048,8 @@ discard block |
||
| 1048 | 1048 | |
| 1049 | 1049 | $cursor = $qb->execute(); |
| 1050 | 1050 | $ids = []; |
| 1051 | - while($row = $cursor->fetch()) { |
|
| 1052 | - $ids[] = (int)$row['id']; |
|
| 1051 | + while ($row = $cursor->fetch()) { |
|
| 1052 | + $ids[] = (int) $row['id']; |
|
| 1053 | 1053 | } |
| 1054 | 1054 | $cursor->closeCursor(); |
| 1055 | 1055 | |
@@ -90,7 +90,7 @@ |
||
| 90 | 90 | } |
| 91 | 91 | |
| 92 | 92 | /** |
| 93 | - * @param \Doctrine\DBAL\Connection $connection |
|
| 93 | + * @param IDBConnection $connection |
|
| 94 | 94 | * @return string[] |
| 95 | 95 | */ |
| 96 | 96 | protected function getAllNonUTF8BinTables($connection) { |
@@ -76,7 +76,7 @@ discard block |
||
| 76 | 76 | $tables = $this->getAllNonUTF8BinTables($this->connection); |
| 77 | 77 | foreach ($tables as $table) { |
| 78 | 78 | $output->info("Change row format for $table ..."); |
| 79 | - $query = $this->connection->prepare('ALTER TABLE `' . $table . '` ROW_FORMAT = DYNAMIC;'); |
|
| 79 | + $query = $this->connection->prepare('ALTER TABLE `'.$table.'` ROW_FORMAT = DYNAMIC;'); |
|
| 80 | 80 | try { |
| 81 | 81 | $query->execute(); |
| 82 | 82 | } catch (DriverException $e) { |
@@ -88,7 +88,7 @@ discard block |
||
| 88 | 88 | } |
| 89 | 89 | |
| 90 | 90 | $output->info("Change collation for $table ..."); |
| 91 | - $query = $this->connection->prepare('ALTER TABLE `' . $table . '` CONVERT TO CHARACTER SET ' . $characterSet . ' COLLATE ' . $characterSet . '_bin;'); |
|
| 91 | + $query = $this->connection->prepare('ALTER TABLE `'.$table.'` CONVERT TO CHARACTER SET '.$characterSet.' COLLATE '.$characterSet.'_bin;'); |
|
| 92 | 92 | try { |
| 93 | 93 | $query->execute(); |
| 94 | 94 | } catch (DriverException $e) { |
@@ -109,10 +109,10 @@ discard block |
||
| 109 | 109 | $dbName = $this->config->getSystemValue("dbname"); |
| 110 | 110 | $characterSet = $this->config->getSystemValue('mysql.utf8mb4', false) ? 'utf8mb4' : 'utf8'; |
| 111 | 111 | $rows = $connection->fetchAll( |
| 112 | - "SELECT DISTINCT(TABLE_NAME) AS `table`" . |
|
| 113 | - " FROM INFORMATION_SCHEMA . COLUMNS" . |
|
| 114 | - " WHERE TABLE_SCHEMA = ?" . |
|
| 115 | - " AND (COLLATION_NAME <> '" . $characterSet . "_bin' OR CHARACTER_SET_NAME <> '" . $characterSet . "')" . |
|
| 112 | + "SELECT DISTINCT(TABLE_NAME) AS `table`". |
|
| 113 | + " FROM INFORMATION_SCHEMA . COLUMNS". |
|
| 114 | + " WHERE TABLE_SCHEMA = ?". |
|
| 115 | + " AND (COLLATION_NAME <> '".$characterSet."_bin' OR CHARACTER_SET_NAME <> '".$characterSet."')". |
|
| 116 | 116 | " AND TABLE_NAME LIKE \"*PREFIX*%\"", |
| 117 | 117 | array($dbName) |
| 118 | 118 | ); |
@@ -33,94 +33,94 @@ |
||
| 33 | 33 | use OCP\Migration\IRepairStep; |
| 34 | 34 | |
| 35 | 35 | class Collation implements IRepairStep { |
| 36 | - /** @var IConfig */ |
|
| 37 | - protected $config; |
|
| 36 | + /** @var IConfig */ |
|
| 37 | + protected $config; |
|
| 38 | 38 | |
| 39 | - /** @var ILogger */ |
|
| 40 | - protected $logger; |
|
| 39 | + /** @var ILogger */ |
|
| 40 | + protected $logger; |
|
| 41 | 41 | |
| 42 | - /** @var IDBConnection */ |
|
| 43 | - protected $connection; |
|
| 42 | + /** @var IDBConnection */ |
|
| 43 | + protected $connection; |
|
| 44 | 44 | |
| 45 | - /** @var bool */ |
|
| 46 | - protected $ignoreFailures; |
|
| 45 | + /** @var bool */ |
|
| 46 | + protected $ignoreFailures; |
|
| 47 | 47 | |
| 48 | - /** |
|
| 49 | - * @param IConfig $config |
|
| 50 | - * @param ILogger $logger |
|
| 51 | - * @param IDBConnection $connection |
|
| 52 | - * @param bool $ignoreFailures |
|
| 53 | - */ |
|
| 54 | - public function __construct(IConfig $config, ILogger $logger, IDBConnection $connection, $ignoreFailures) { |
|
| 55 | - $this->connection = $connection; |
|
| 56 | - $this->config = $config; |
|
| 57 | - $this->logger = $logger; |
|
| 58 | - $this->ignoreFailures = $ignoreFailures; |
|
| 59 | - } |
|
| 48 | + /** |
|
| 49 | + * @param IConfig $config |
|
| 50 | + * @param ILogger $logger |
|
| 51 | + * @param IDBConnection $connection |
|
| 52 | + * @param bool $ignoreFailures |
|
| 53 | + */ |
|
| 54 | + public function __construct(IConfig $config, ILogger $logger, IDBConnection $connection, $ignoreFailures) { |
|
| 55 | + $this->connection = $connection; |
|
| 56 | + $this->config = $config; |
|
| 57 | + $this->logger = $logger; |
|
| 58 | + $this->ignoreFailures = $ignoreFailures; |
|
| 59 | + } |
|
| 60 | 60 | |
| 61 | - public function getName() { |
|
| 62 | - return 'Repair MySQL collation'; |
|
| 63 | - } |
|
| 61 | + public function getName() { |
|
| 62 | + return 'Repair MySQL collation'; |
|
| 63 | + } |
|
| 64 | 64 | |
| 65 | - /** |
|
| 66 | - * Fix mime types |
|
| 67 | - */ |
|
| 68 | - public function run(IOutput $output) { |
|
| 69 | - if (!$this->connection->getDatabasePlatform() instanceof MySqlPlatform) { |
|
| 70 | - $output->info('Not a mysql database -> nothing to do'); |
|
| 71 | - return; |
|
| 72 | - } |
|
| 65 | + /** |
|
| 66 | + * Fix mime types |
|
| 67 | + */ |
|
| 68 | + public function run(IOutput $output) { |
|
| 69 | + if (!$this->connection->getDatabasePlatform() instanceof MySqlPlatform) { |
|
| 70 | + $output->info('Not a mysql database -> nothing to do'); |
|
| 71 | + return; |
|
| 72 | + } |
|
| 73 | 73 | |
| 74 | - $characterSet = $this->config->getSystemValue('mysql.utf8mb4', false) ? 'utf8mb4' : 'utf8'; |
|
| 74 | + $characterSet = $this->config->getSystemValue('mysql.utf8mb4', false) ? 'utf8mb4' : 'utf8'; |
|
| 75 | 75 | |
| 76 | - $tables = $this->getAllNonUTF8BinTables($this->connection); |
|
| 77 | - foreach ($tables as $table) { |
|
| 78 | - $output->info("Change row format for $table ..."); |
|
| 79 | - $query = $this->connection->prepare('ALTER TABLE `' . $table . '` ROW_FORMAT = DYNAMIC;'); |
|
| 80 | - try { |
|
| 81 | - $query->execute(); |
|
| 82 | - } catch (DriverException $e) { |
|
| 83 | - // Just log this |
|
| 84 | - $this->logger->logException($e); |
|
| 85 | - if (!$this->ignoreFailures) { |
|
| 86 | - throw $e; |
|
| 87 | - } |
|
| 88 | - } |
|
| 76 | + $tables = $this->getAllNonUTF8BinTables($this->connection); |
|
| 77 | + foreach ($tables as $table) { |
|
| 78 | + $output->info("Change row format for $table ..."); |
|
| 79 | + $query = $this->connection->prepare('ALTER TABLE `' . $table . '` ROW_FORMAT = DYNAMIC;'); |
|
| 80 | + try { |
|
| 81 | + $query->execute(); |
|
| 82 | + } catch (DriverException $e) { |
|
| 83 | + // Just log this |
|
| 84 | + $this->logger->logException($e); |
|
| 85 | + if (!$this->ignoreFailures) { |
|
| 86 | + throw $e; |
|
| 87 | + } |
|
| 88 | + } |
|
| 89 | 89 | |
| 90 | - $output->info("Change collation for $table ..."); |
|
| 91 | - $query = $this->connection->prepare('ALTER TABLE `' . $table . '` CONVERT TO CHARACTER SET ' . $characterSet . ' COLLATE ' . $characterSet . '_bin;'); |
|
| 92 | - try { |
|
| 93 | - $query->execute(); |
|
| 94 | - } catch (DriverException $e) { |
|
| 95 | - // Just log this |
|
| 96 | - $this->logger->logException($e); |
|
| 97 | - if (!$this->ignoreFailures) { |
|
| 98 | - throw $e; |
|
| 99 | - } |
|
| 100 | - } |
|
| 101 | - } |
|
| 102 | - } |
|
| 90 | + $output->info("Change collation for $table ..."); |
|
| 91 | + $query = $this->connection->prepare('ALTER TABLE `' . $table . '` CONVERT TO CHARACTER SET ' . $characterSet . ' COLLATE ' . $characterSet . '_bin;'); |
|
| 92 | + try { |
|
| 93 | + $query->execute(); |
|
| 94 | + } catch (DriverException $e) { |
|
| 95 | + // Just log this |
|
| 96 | + $this->logger->logException($e); |
|
| 97 | + if (!$this->ignoreFailures) { |
|
| 98 | + throw $e; |
|
| 99 | + } |
|
| 100 | + } |
|
| 101 | + } |
|
| 102 | + } |
|
| 103 | 103 | |
| 104 | - /** |
|
| 105 | - * @param \Doctrine\DBAL\Connection $connection |
|
| 106 | - * @return string[] |
|
| 107 | - */ |
|
| 108 | - protected function getAllNonUTF8BinTables($connection) { |
|
| 109 | - $dbName = $this->config->getSystemValue("dbname"); |
|
| 110 | - $characterSet = $this->config->getSystemValue('mysql.utf8mb4', false) ? 'utf8mb4' : 'utf8'; |
|
| 111 | - $rows = $connection->fetchAll( |
|
| 112 | - "SELECT DISTINCT(TABLE_NAME) AS `table`" . |
|
| 113 | - " FROM INFORMATION_SCHEMA . COLUMNS" . |
|
| 114 | - " WHERE TABLE_SCHEMA = ?" . |
|
| 115 | - " AND (COLLATION_NAME <> '" . $characterSet . "_bin' OR CHARACTER_SET_NAME <> '" . $characterSet . "')" . |
|
| 116 | - " AND TABLE_NAME LIKE \"*PREFIX*%\"", |
|
| 117 | - array($dbName) |
|
| 118 | - ); |
|
| 119 | - $result = array(); |
|
| 120 | - foreach ($rows as $row) { |
|
| 121 | - $result[] = $row['table']; |
|
| 122 | - } |
|
| 123 | - return $result; |
|
| 124 | - } |
|
| 104 | + /** |
|
| 105 | + * @param \Doctrine\DBAL\Connection $connection |
|
| 106 | + * @return string[] |
|
| 107 | + */ |
|
| 108 | + protected function getAllNonUTF8BinTables($connection) { |
|
| 109 | + $dbName = $this->config->getSystemValue("dbname"); |
|
| 110 | + $characterSet = $this->config->getSystemValue('mysql.utf8mb4', false) ? 'utf8mb4' : 'utf8'; |
|
| 111 | + $rows = $connection->fetchAll( |
|
| 112 | + "SELECT DISTINCT(TABLE_NAME) AS `table`" . |
|
| 113 | + " FROM INFORMATION_SCHEMA . COLUMNS" . |
|
| 114 | + " WHERE TABLE_SCHEMA = ?" . |
|
| 115 | + " AND (COLLATION_NAME <> '" . $characterSet . "_bin' OR CHARACTER_SET_NAME <> '" . $characterSet . "')" . |
|
| 116 | + " AND TABLE_NAME LIKE \"*PREFIX*%\"", |
|
| 117 | + array($dbName) |
|
| 118 | + ); |
|
| 119 | + $result = array(); |
|
| 120 | + foreach ($rows as $row) { |
|
| 121 | + $result[] = $row['table']; |
|
| 122 | + } |
|
| 123 | + return $result; |
|
| 124 | + } |
|
| 125 | 125 | } |
| 126 | 126 | |
@@ -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 | } |
@@ -30,7 +30,6 @@ |
||
| 30 | 30 | |
| 31 | 31 | namespace OC\Share; |
| 32 | 32 | |
| 33 | -use DateTime; |
|
| 34 | 33 | use OCP\IL10N; |
| 35 | 34 | use OCP\IURLGenerator; |
| 36 | 35 | use OCP\IUser; |
@@ -46,113 +46,113 @@ |
||
| 46 | 46 | */ |
| 47 | 47 | class MailNotifications { |
| 48 | 48 | |
| 49 | - /** @var IUser sender userId */ |
|
| 50 | - private $user; |
|
| 51 | - /** @var string sender email address */ |
|
| 52 | - private $replyTo; |
|
| 53 | - /** @var string */ |
|
| 54 | - private $senderDisplayName; |
|
| 55 | - /** @var IL10N */ |
|
| 56 | - private $l; |
|
| 57 | - /** @var IMailer */ |
|
| 58 | - private $mailer; |
|
| 59 | - /** @var Defaults */ |
|
| 60 | - private $defaults; |
|
| 61 | - /** @var ILogger */ |
|
| 62 | - private $logger; |
|
| 63 | - /** @var IURLGenerator */ |
|
| 64 | - private $urlGenerator; |
|
| 49 | + /** @var IUser sender userId */ |
|
| 50 | + private $user; |
|
| 51 | + /** @var string sender email address */ |
|
| 52 | + private $replyTo; |
|
| 53 | + /** @var string */ |
|
| 54 | + private $senderDisplayName; |
|
| 55 | + /** @var IL10N */ |
|
| 56 | + private $l; |
|
| 57 | + /** @var IMailer */ |
|
| 58 | + private $mailer; |
|
| 59 | + /** @var Defaults */ |
|
| 60 | + private $defaults; |
|
| 61 | + /** @var ILogger */ |
|
| 62 | + private $logger; |
|
| 63 | + /** @var IURLGenerator */ |
|
| 64 | + private $urlGenerator; |
|
| 65 | 65 | |
| 66 | - /** |
|
| 67 | - * @param IUser $user |
|
| 68 | - * @param IL10N $l10n |
|
| 69 | - * @param IMailer $mailer |
|
| 70 | - * @param ILogger $logger |
|
| 71 | - * @param Defaults $defaults |
|
| 72 | - * @param IURLGenerator $urlGenerator |
|
| 73 | - */ |
|
| 74 | - public function __construct(IUser $user, |
|
| 75 | - IL10N $l10n, |
|
| 76 | - IMailer $mailer, |
|
| 77 | - ILogger $logger, |
|
| 78 | - Defaults $defaults, |
|
| 79 | - IURLGenerator $urlGenerator) { |
|
| 80 | - $this->l = $l10n; |
|
| 81 | - $this->user = $user; |
|
| 82 | - $this->mailer = $mailer; |
|
| 83 | - $this->logger = $logger; |
|
| 84 | - $this->defaults = $defaults; |
|
| 85 | - $this->urlGenerator = $urlGenerator; |
|
| 66 | + /** |
|
| 67 | + * @param IUser $user |
|
| 68 | + * @param IL10N $l10n |
|
| 69 | + * @param IMailer $mailer |
|
| 70 | + * @param ILogger $logger |
|
| 71 | + * @param Defaults $defaults |
|
| 72 | + * @param IURLGenerator $urlGenerator |
|
| 73 | + */ |
|
| 74 | + public function __construct(IUser $user, |
|
| 75 | + IL10N $l10n, |
|
| 76 | + IMailer $mailer, |
|
| 77 | + ILogger $logger, |
|
| 78 | + Defaults $defaults, |
|
| 79 | + IURLGenerator $urlGenerator) { |
|
| 80 | + $this->l = $l10n; |
|
| 81 | + $this->user = $user; |
|
| 82 | + $this->mailer = $mailer; |
|
| 83 | + $this->logger = $logger; |
|
| 84 | + $this->defaults = $defaults; |
|
| 85 | + $this->urlGenerator = $urlGenerator; |
|
| 86 | 86 | |
| 87 | - $this->replyTo = $this->user->getEMailAddress(); |
|
| 88 | - $this->senderDisplayName = $this->user->getDisplayName(); |
|
| 89 | - } |
|
| 87 | + $this->replyTo = $this->user->getEMailAddress(); |
|
| 88 | + $this->senderDisplayName = $this->user->getDisplayName(); |
|
| 89 | + } |
|
| 90 | 90 | |
| 91 | - /** |
|
| 92 | - * inform recipient about public link share |
|
| 93 | - * |
|
| 94 | - * @param string $recipient recipient email address |
|
| 95 | - * @param string $filename the shared file |
|
| 96 | - * @param string $link the public link |
|
| 97 | - * @param int $expiration expiration date (timestamp) |
|
| 98 | - * @return string[] $result of failed recipients |
|
| 99 | - */ |
|
| 100 | - public function sendLinkShareMail($recipient, $filename, $link, $expiration) { |
|
| 101 | - $subject = (string)$this->l->t('%s shared »%s« with you', [$this->senderDisplayName, $filename]); |
|
| 102 | - list($htmlBody, $textBody) = $this->createMailBody($filename, $link, $expiration); |
|
| 91 | + /** |
|
| 92 | + * inform recipient about public link share |
|
| 93 | + * |
|
| 94 | + * @param string $recipient recipient email address |
|
| 95 | + * @param string $filename the shared file |
|
| 96 | + * @param string $link the public link |
|
| 97 | + * @param int $expiration expiration date (timestamp) |
|
| 98 | + * @return string[] $result of failed recipients |
|
| 99 | + */ |
|
| 100 | + public function sendLinkShareMail($recipient, $filename, $link, $expiration) { |
|
| 101 | + $subject = (string)$this->l->t('%s shared »%s« with you', [$this->senderDisplayName, $filename]); |
|
| 102 | + list($htmlBody, $textBody) = $this->createMailBody($filename, $link, $expiration); |
|
| 103 | 103 | |
| 104 | - $recipient = str_replace([', ', '; ', ',', ';', ' '], ',', $recipient); |
|
| 105 | - $recipients = explode(',', $recipient); |
|
| 106 | - try { |
|
| 107 | - $message = $this->mailer->createMessage(); |
|
| 108 | - $message->setSubject($subject); |
|
| 109 | - $message->setTo($recipients); |
|
| 110 | - $message->setHtmlBody($htmlBody); |
|
| 111 | - $message->setPlainBody($textBody); |
|
| 112 | - $message->setFrom([ |
|
| 113 | - Util::getDefaultEmailAddress('sharing-noreply') => |
|
| 114 | - (string)$this->l->t('%s via %s', [ |
|
| 115 | - $this->senderDisplayName, |
|
| 116 | - $this->defaults->getName() |
|
| 117 | - ]), |
|
| 118 | - ]); |
|
| 119 | - if(!is_null($this->replyTo)) { |
|
| 120 | - $message->setReplyTo([$this->replyTo]); |
|
| 121 | - } |
|
| 104 | + $recipient = str_replace([', ', '; ', ',', ';', ' '], ',', $recipient); |
|
| 105 | + $recipients = explode(',', $recipient); |
|
| 106 | + try { |
|
| 107 | + $message = $this->mailer->createMessage(); |
|
| 108 | + $message->setSubject($subject); |
|
| 109 | + $message->setTo($recipients); |
|
| 110 | + $message->setHtmlBody($htmlBody); |
|
| 111 | + $message->setPlainBody($textBody); |
|
| 112 | + $message->setFrom([ |
|
| 113 | + Util::getDefaultEmailAddress('sharing-noreply') => |
|
| 114 | + (string)$this->l->t('%s via %s', [ |
|
| 115 | + $this->senderDisplayName, |
|
| 116 | + $this->defaults->getName() |
|
| 117 | + ]), |
|
| 118 | + ]); |
|
| 119 | + if(!is_null($this->replyTo)) { |
|
| 120 | + $message->setReplyTo([$this->replyTo]); |
|
| 121 | + } |
|
| 122 | 122 | |
| 123 | - return $this->mailer->send($message); |
|
| 124 | - } catch (\Exception $e) { |
|
| 125 | - $this->logger->error("Can't send mail with public link to $recipient: ".$e->getMessage(), ['app' => 'sharing']); |
|
| 126 | - return [$recipient]; |
|
| 127 | - } |
|
| 128 | - } |
|
| 123 | + return $this->mailer->send($message); |
|
| 124 | + } catch (\Exception $e) { |
|
| 125 | + $this->logger->error("Can't send mail with public link to $recipient: ".$e->getMessage(), ['app' => 'sharing']); |
|
| 126 | + return [$recipient]; |
|
| 127 | + } |
|
| 128 | + } |
|
| 129 | 129 | |
| 130 | - /** |
|
| 131 | - * create mail body for plain text and html mail |
|
| 132 | - * |
|
| 133 | - * @param string $filename the shared file |
|
| 134 | - * @param string $link link to the shared file |
|
| 135 | - * @param int $expiration expiration date (timestamp) |
|
| 136 | - * @param string $prefix prefix of mail template files |
|
| 137 | - * @return array an array of the html mail body and the plain text mail body |
|
| 138 | - */ |
|
| 139 | - private function createMailBody($filename, $link, $expiration, $prefix = '') { |
|
| 140 | - $formattedDate = $expiration ? $this->l->l('date', $expiration) : null; |
|
| 130 | + /** |
|
| 131 | + * create mail body for plain text and html mail |
|
| 132 | + * |
|
| 133 | + * @param string $filename the shared file |
|
| 134 | + * @param string $link link to the shared file |
|
| 135 | + * @param int $expiration expiration date (timestamp) |
|
| 136 | + * @param string $prefix prefix of mail template files |
|
| 137 | + * @return array an array of the html mail body and the plain text mail body |
|
| 138 | + */ |
|
| 139 | + private function createMailBody($filename, $link, $expiration, $prefix = '') { |
|
| 140 | + $formattedDate = $expiration ? $this->l->l('date', $expiration) : null; |
|
| 141 | 141 | |
| 142 | - $html = new \OC_Template('core', $prefix . 'mail', ''); |
|
| 143 | - $html->assign ('link', $link); |
|
| 144 | - $html->assign ('user_displayname', $this->senderDisplayName); |
|
| 145 | - $html->assign ('filename', $filename); |
|
| 146 | - $html->assign('expiration', $formattedDate); |
|
| 147 | - $htmlMail = $html->fetchPage(); |
|
| 142 | + $html = new \OC_Template('core', $prefix . 'mail', ''); |
|
| 143 | + $html->assign ('link', $link); |
|
| 144 | + $html->assign ('user_displayname', $this->senderDisplayName); |
|
| 145 | + $html->assign ('filename', $filename); |
|
| 146 | + $html->assign('expiration', $formattedDate); |
|
| 147 | + $htmlMail = $html->fetchPage(); |
|
| 148 | 148 | |
| 149 | - $plainText = new \OC_Template('core', $prefix . 'altmail', ''); |
|
| 150 | - $plainText->assign ('link', $link); |
|
| 151 | - $plainText->assign ('user_displayname', $this->senderDisplayName); |
|
| 152 | - $plainText->assign ('filename', $filename); |
|
| 153 | - $plainText->assign('expiration', $formattedDate); |
|
| 154 | - $plainTextMail = $plainText->fetchPage(); |
|
| 149 | + $plainText = new \OC_Template('core', $prefix . 'altmail', ''); |
|
| 150 | + $plainText->assign ('link', $link); |
|
| 151 | + $plainText->assign ('user_displayname', $this->senderDisplayName); |
|
| 152 | + $plainText->assign ('filename', $filename); |
|
| 153 | + $plainText->assign('expiration', $formattedDate); |
|
| 154 | + $plainTextMail = $plainText->fetchPage(); |
|
| 155 | 155 | |
| 156 | - return [$htmlMail, $plainTextMail]; |
|
| 157 | - } |
|
| 156 | + return [$htmlMail, $plainTextMail]; |
|
| 157 | + } |
|
| 158 | 158 | } |
@@ -98,7 +98,7 @@ discard block |
||
| 98 | 98 | * @return string[] $result of failed recipients |
| 99 | 99 | */ |
| 100 | 100 | public function sendLinkShareMail($recipient, $filename, $link, $expiration) { |
| 101 | - $subject = (string)$this->l->t('%s shared »%s« with you', [$this->senderDisplayName, $filename]); |
|
| 101 | + $subject = (string) $this->l->t('%s shared »%s« with you', [$this->senderDisplayName, $filename]); |
|
| 102 | 102 | list($htmlBody, $textBody) = $this->createMailBody($filename, $link, $expiration); |
| 103 | 103 | |
| 104 | 104 | $recipient = str_replace([', ', '; ', ',', ';', ' '], ',', $recipient); |
@@ -111,12 +111,12 @@ discard block |
||
| 111 | 111 | $message->setPlainBody($textBody); |
| 112 | 112 | $message->setFrom([ |
| 113 | 113 | Util::getDefaultEmailAddress('sharing-noreply') => |
| 114 | - (string)$this->l->t('%s via %s', [ |
|
| 114 | + (string) $this->l->t('%s via %s', [ |
|
| 115 | 115 | $this->senderDisplayName, |
| 116 | 116 | $this->defaults->getName() |
| 117 | 117 | ]), |
| 118 | 118 | ]); |
| 119 | - if(!is_null($this->replyTo)) { |
|
| 119 | + if (!is_null($this->replyTo)) { |
|
| 120 | 120 | $message->setReplyTo([$this->replyTo]); |
| 121 | 121 | } |
| 122 | 122 | |
@@ -139,17 +139,17 @@ discard block |
||
| 139 | 139 | private function createMailBody($filename, $link, $expiration, $prefix = '') { |
| 140 | 140 | $formattedDate = $expiration ? $this->l->l('date', $expiration) : null; |
| 141 | 141 | |
| 142 | - $html = new \OC_Template('core', $prefix . 'mail', ''); |
|
| 143 | - $html->assign ('link', $link); |
|
| 144 | - $html->assign ('user_displayname', $this->senderDisplayName); |
|
| 145 | - $html->assign ('filename', $filename); |
|
| 146 | - $html->assign('expiration', $formattedDate); |
|
| 142 | + $html = new \OC_Template('core', $prefix.'mail', ''); |
|
| 143 | + $html->assign('link', $link); |
|
| 144 | + $html->assign('user_displayname', $this->senderDisplayName); |
|
| 145 | + $html->assign('filename', $filename); |
|
| 146 | + $html->assign('expiration', $formattedDate); |
|
| 147 | 147 | $htmlMail = $html->fetchPage(); |
| 148 | 148 | |
| 149 | - $plainText = new \OC_Template('core', $prefix . 'altmail', ''); |
|
| 150 | - $plainText->assign ('link', $link); |
|
| 151 | - $plainText->assign ('user_displayname', $this->senderDisplayName); |
|
| 152 | - $plainText->assign ('filename', $filename); |
|
| 149 | + $plainText = new \OC_Template('core', $prefix.'altmail', ''); |
|
| 150 | + $plainText->assign('link', $link); |
|
| 151 | + $plainText->assign('user_displayname', $this->senderDisplayName); |
|
| 152 | + $plainText->assign('filename', $filename); |
|
| 153 | 153 | $plainText->assign('expiration', $formattedDate); |
| 154 | 154 | $plainTextMail = $plainText->fetchPage(); |
| 155 | 155 | |
@@ -51,7 +51,6 @@ |
||
| 51 | 51 | use OC\App\InfoParser; |
| 52 | 52 | use OC\App\Platform; |
| 53 | 53 | use OC\Installer; |
| 54 | -use OC\OCSClient; |
|
| 55 | 54 | use OC\Repair; |
| 56 | 55 | use OCP\App\ManagerEvent; |
| 57 | 56 | |
@@ -1063,7 +1063,7 @@ discard block |
||
| 1063 | 1063 | * @param string $app |
| 1064 | 1064 | * @param \OCP\IConfig $config |
| 1065 | 1065 | * @param \OCP\IL10N $l |
| 1066 | - * @return bool |
|
| 1066 | + * @return string |
|
| 1067 | 1067 | * |
| 1068 | 1068 | * @throws Exception if app is not compatible with this version of ownCloud |
| 1069 | 1069 | * @throws Exception if no app-name was specified |
@@ -1243,6 +1243,11 @@ discard block |
||
| 1243 | 1243 | } |
| 1244 | 1244 | } |
| 1245 | 1245 | |
| 1246 | + /** |
|
| 1247 | + * @param string $lang |
|
| 1248 | + * |
|
| 1249 | + * @return string |
|
| 1250 | + */ |
|
| 1246 | 1251 | protected static function findBestL10NOption($options, $lang) { |
| 1247 | 1252 | $fallback = $similarLangFallback = $englishFallback = false; |
| 1248 | 1253 | |
@@ -61,1337 +61,1337 @@ |
||
| 61 | 61 | * upgrading and removing apps. |
| 62 | 62 | */ |
| 63 | 63 | class OC_App { |
| 64 | - static private $appVersion = []; |
|
| 65 | - static private $adminForms = array(); |
|
| 66 | - static private $personalForms = array(); |
|
| 67 | - static private $appInfo = array(); |
|
| 68 | - static private $appTypes = array(); |
|
| 69 | - static private $loadedApps = array(); |
|
| 70 | - static private $altLogin = array(); |
|
| 71 | - static private $alreadyRegistered = []; |
|
| 72 | - const officialApp = 200; |
|
| 73 | - |
|
| 74 | - /** |
|
| 75 | - * clean the appId |
|
| 76 | - * |
|
| 77 | - * @param string|boolean $app AppId that needs to be cleaned |
|
| 78 | - * @return string |
|
| 79 | - */ |
|
| 80 | - public static function cleanAppId($app) { |
|
| 81 | - return str_replace(array('\0', '/', '\\', '..'), '', $app); |
|
| 82 | - } |
|
| 83 | - |
|
| 84 | - /** |
|
| 85 | - * Check if an app is loaded |
|
| 86 | - * |
|
| 87 | - * @param string $app |
|
| 88 | - * @return bool |
|
| 89 | - */ |
|
| 90 | - public static function isAppLoaded($app) { |
|
| 91 | - return in_array($app, self::$loadedApps, true); |
|
| 92 | - } |
|
| 93 | - |
|
| 94 | - /** |
|
| 95 | - * loads all apps |
|
| 96 | - * |
|
| 97 | - * @param string[] | string | null $types |
|
| 98 | - * @return bool |
|
| 99 | - * |
|
| 100 | - * This function walks through the ownCloud directory and loads all apps |
|
| 101 | - * it can find. A directory contains an app if the file /appinfo/info.xml |
|
| 102 | - * exists. |
|
| 103 | - * |
|
| 104 | - * if $types is set, only apps of those types will be loaded |
|
| 105 | - */ |
|
| 106 | - public static function loadApps($types = null) { |
|
| 107 | - if (\OC::$server->getSystemConfig()->getValue('maintenance', false)) { |
|
| 108 | - return false; |
|
| 109 | - } |
|
| 110 | - // Load the enabled apps here |
|
| 111 | - $apps = self::getEnabledApps(); |
|
| 112 | - |
|
| 113 | - // Add each apps' folder as allowed class path |
|
| 114 | - foreach($apps as $app) { |
|
| 115 | - $path = self::getAppPath($app); |
|
| 116 | - if($path !== false) { |
|
| 117 | - self::registerAutoloading($app, $path); |
|
| 118 | - } |
|
| 119 | - } |
|
| 120 | - |
|
| 121 | - // prevent app.php from printing output |
|
| 122 | - ob_start(); |
|
| 123 | - foreach ($apps as $app) { |
|
| 124 | - if ((is_null($types) or self::isType($app, $types)) && !in_array($app, self::$loadedApps)) { |
|
| 125 | - self::loadApp($app); |
|
| 126 | - } |
|
| 127 | - } |
|
| 128 | - ob_end_clean(); |
|
| 129 | - |
|
| 130 | - return true; |
|
| 131 | - } |
|
| 132 | - |
|
| 133 | - /** |
|
| 134 | - * load a single app |
|
| 135 | - * |
|
| 136 | - * @param string $app |
|
| 137 | - */ |
|
| 138 | - public static function loadApp($app) { |
|
| 139 | - self::$loadedApps[] = $app; |
|
| 140 | - $appPath = self::getAppPath($app); |
|
| 141 | - if($appPath === false) { |
|
| 142 | - return; |
|
| 143 | - } |
|
| 144 | - |
|
| 145 | - // in case someone calls loadApp() directly |
|
| 146 | - self::registerAutoloading($app, $appPath); |
|
| 147 | - |
|
| 148 | - if (is_file($appPath . '/appinfo/app.php')) { |
|
| 149 | - \OC::$server->getEventLogger()->start('load_app_' . $app, 'Load app: ' . $app); |
|
| 150 | - self::requireAppFile($app); |
|
| 151 | - if (self::isType($app, array('authentication'))) { |
|
| 152 | - // since authentication apps affect the "is app enabled for group" check, |
|
| 153 | - // the enabled apps cache needs to be cleared to make sure that the |
|
| 154 | - // next time getEnableApps() is called it will also include apps that were |
|
| 155 | - // enabled for groups |
|
| 156 | - self::$enabledAppsCache = array(); |
|
| 157 | - } |
|
| 158 | - \OC::$server->getEventLogger()->end('load_app_' . $app); |
|
| 159 | - } |
|
| 160 | - |
|
| 161 | - $info = self::getAppInfo($app); |
|
| 162 | - if (!empty($info['activity']['filters'])) { |
|
| 163 | - foreach ($info['activity']['filters'] as $filter) { |
|
| 164 | - \OC::$server->getActivityManager()->registerFilter($filter); |
|
| 165 | - } |
|
| 166 | - } |
|
| 167 | - if (!empty($info['activity']['settings'])) { |
|
| 168 | - foreach ($info['activity']['settings'] as $setting) { |
|
| 169 | - \OC::$server->getActivityManager()->registerSetting($setting); |
|
| 170 | - } |
|
| 171 | - } |
|
| 172 | - if (!empty($info['activity']['providers'])) { |
|
| 173 | - foreach ($info['activity']['providers'] as $provider) { |
|
| 174 | - \OC::$server->getActivityManager()->registerProvider($provider); |
|
| 175 | - } |
|
| 176 | - } |
|
| 177 | - } |
|
| 178 | - |
|
| 179 | - /** |
|
| 180 | - * @internal |
|
| 181 | - * @param string $app |
|
| 182 | - * @param string $path |
|
| 183 | - */ |
|
| 184 | - public static function registerAutoloading($app, $path) { |
|
| 185 | - $key = $app . '-' . $path; |
|
| 186 | - if(isset(self::$alreadyRegistered[$key])) { |
|
| 187 | - return; |
|
| 188 | - } |
|
| 189 | - self::$alreadyRegistered[$key] = true; |
|
| 190 | - // Register on PSR-4 composer autoloader |
|
| 191 | - $appNamespace = \OC\AppFramework\App::buildAppNamespace($app); |
|
| 192 | - \OC::$composerAutoloader->addPsr4($appNamespace . '\\', $path . '/lib/', true); |
|
| 193 | - if (defined('PHPUNIT_RUN') || defined('CLI_TEST_RUN')) { |
|
| 194 | - \OC::$composerAutoloader->addPsr4($appNamespace . '\\Tests\\', $path . '/tests/', true); |
|
| 195 | - } |
|
| 196 | - |
|
| 197 | - // Register on legacy autoloader |
|
| 198 | - \OC::$loader->addValidRoot($path); |
|
| 199 | - } |
|
| 200 | - |
|
| 201 | - /** |
|
| 202 | - * Load app.php from the given app |
|
| 203 | - * |
|
| 204 | - * @param string $app app name |
|
| 205 | - */ |
|
| 206 | - private static function requireAppFile($app) { |
|
| 207 | - try { |
|
| 208 | - // encapsulated here to avoid variable scope conflicts |
|
| 209 | - require_once $app . '/appinfo/app.php'; |
|
| 210 | - } catch (Error $ex) { |
|
| 211 | - \OC::$server->getLogger()->logException($ex); |
|
| 212 | - $blacklist = \OC::$server->getAppManager()->getAlwaysEnabledApps(); |
|
| 213 | - if (!in_array($app, $blacklist)) { |
|
| 214 | - self::disable($app); |
|
| 215 | - } |
|
| 216 | - } |
|
| 217 | - } |
|
| 218 | - |
|
| 219 | - /** |
|
| 220 | - * check if an app is of a specific type |
|
| 221 | - * |
|
| 222 | - * @param string $app |
|
| 223 | - * @param string|array $types |
|
| 224 | - * @return bool |
|
| 225 | - */ |
|
| 226 | - public static function isType($app, $types) { |
|
| 227 | - if (is_string($types)) { |
|
| 228 | - $types = array($types); |
|
| 229 | - } |
|
| 230 | - $appTypes = self::getAppTypes($app); |
|
| 231 | - foreach ($types as $type) { |
|
| 232 | - if (array_search($type, $appTypes) !== false) { |
|
| 233 | - return true; |
|
| 234 | - } |
|
| 235 | - } |
|
| 236 | - return false; |
|
| 237 | - } |
|
| 238 | - |
|
| 239 | - /** |
|
| 240 | - * get the types of an app |
|
| 241 | - * |
|
| 242 | - * @param string $app |
|
| 243 | - * @return array |
|
| 244 | - */ |
|
| 245 | - private static function getAppTypes($app) { |
|
| 246 | - //load the cache |
|
| 247 | - if (count(self::$appTypes) == 0) { |
|
| 248 | - self::$appTypes = \OC::$server->getAppConfig()->getValues(false, 'types'); |
|
| 249 | - } |
|
| 250 | - |
|
| 251 | - if (isset(self::$appTypes[$app])) { |
|
| 252 | - return explode(',', self::$appTypes[$app]); |
|
| 253 | - } else { |
|
| 254 | - return array(); |
|
| 255 | - } |
|
| 256 | - } |
|
| 257 | - |
|
| 258 | - /** |
|
| 259 | - * read app types from info.xml and cache them in the database |
|
| 260 | - */ |
|
| 261 | - public static function setAppTypes($app) { |
|
| 262 | - $appData = self::getAppInfo($app); |
|
| 263 | - if(!is_array($appData)) { |
|
| 264 | - return; |
|
| 265 | - } |
|
| 266 | - |
|
| 267 | - if (isset($appData['types'])) { |
|
| 268 | - $appTypes = implode(',', $appData['types']); |
|
| 269 | - } else { |
|
| 270 | - $appTypes = ''; |
|
| 271 | - $appData['types'] = []; |
|
| 272 | - } |
|
| 273 | - |
|
| 274 | - \OC::$server->getAppConfig()->setValue($app, 'types', $appTypes); |
|
| 275 | - |
|
| 276 | - if (\OC::$server->getAppManager()->hasProtectedAppType($appData['types'])) { |
|
| 277 | - $enabled = \OC::$server->getAppConfig()->getValue($app, 'enabled', 'yes'); |
|
| 278 | - if ($enabled !== 'yes' && $enabled !== 'no') { |
|
| 279 | - \OC::$server->getAppConfig()->setValue($app, 'enabled', 'yes'); |
|
| 280 | - } |
|
| 281 | - } |
|
| 282 | - } |
|
| 283 | - |
|
| 284 | - /** |
|
| 285 | - * check if app is shipped |
|
| 286 | - * |
|
| 287 | - * @param string $appId the id of the app to check |
|
| 288 | - * @return bool |
|
| 289 | - * |
|
| 290 | - * Check if an app that is installed is a shipped app or installed from the appstore. |
|
| 291 | - */ |
|
| 292 | - public static function isShipped($appId) { |
|
| 293 | - return \OC::$server->getAppManager()->isShipped($appId); |
|
| 294 | - } |
|
| 295 | - |
|
| 296 | - /** |
|
| 297 | - * get all enabled apps |
|
| 298 | - */ |
|
| 299 | - protected static $enabledAppsCache = array(); |
|
| 300 | - |
|
| 301 | - /** |
|
| 302 | - * Returns apps enabled for the current user. |
|
| 303 | - * |
|
| 304 | - * @param bool $forceRefresh whether to refresh the cache |
|
| 305 | - * @param bool $all whether to return apps for all users, not only the |
|
| 306 | - * currently logged in one |
|
| 307 | - * @return string[] |
|
| 308 | - */ |
|
| 309 | - public static function getEnabledApps($forceRefresh = false, $all = false) { |
|
| 310 | - if (!\OC::$server->getSystemConfig()->getValue('installed', false)) { |
|
| 311 | - return array(); |
|
| 312 | - } |
|
| 313 | - // in incognito mode or when logged out, $user will be false, |
|
| 314 | - // which is also the case during an upgrade |
|
| 315 | - $appManager = \OC::$server->getAppManager(); |
|
| 316 | - if ($all) { |
|
| 317 | - $user = null; |
|
| 318 | - } else { |
|
| 319 | - $user = \OC::$server->getUserSession()->getUser(); |
|
| 320 | - } |
|
| 321 | - |
|
| 322 | - if (is_null($user)) { |
|
| 323 | - $apps = $appManager->getInstalledApps(); |
|
| 324 | - } else { |
|
| 325 | - $apps = $appManager->getEnabledAppsForUser($user); |
|
| 326 | - } |
|
| 327 | - $apps = array_filter($apps, function ($app) { |
|
| 328 | - return $app !== 'files';//we add this manually |
|
| 329 | - }); |
|
| 330 | - sort($apps); |
|
| 331 | - array_unshift($apps, 'files'); |
|
| 332 | - return $apps; |
|
| 333 | - } |
|
| 334 | - |
|
| 335 | - /** |
|
| 336 | - * checks whether or not an app is enabled |
|
| 337 | - * |
|
| 338 | - * @param string $app app |
|
| 339 | - * @return bool |
|
| 340 | - * |
|
| 341 | - * This function checks whether or not an app is enabled. |
|
| 342 | - */ |
|
| 343 | - public static function isEnabled($app) { |
|
| 344 | - return \OC::$server->getAppManager()->isEnabledForUser($app); |
|
| 345 | - } |
|
| 346 | - |
|
| 347 | - /** |
|
| 348 | - * enables an app |
|
| 349 | - * |
|
| 350 | - * @param string $appId |
|
| 351 | - * @param array $groups (optional) when set, only these groups will have access to the app |
|
| 352 | - * @throws \Exception |
|
| 353 | - * @return void |
|
| 354 | - * |
|
| 355 | - * This function set an app as enabled in appconfig. |
|
| 356 | - */ |
|
| 357 | - public function enable($appId, |
|
| 358 | - $groups = null) { |
|
| 359 | - self::$enabledAppsCache = []; // flush |
|
| 360 | - $l = \OC::$server->getL10N('core'); |
|
| 361 | - $config = \OC::$server->getConfig(); |
|
| 362 | - |
|
| 363 | - // Check if app is already downloaded |
|
| 364 | - $installer = new Installer( |
|
| 365 | - \OC::$server->getAppFetcher(), |
|
| 366 | - \OC::$server->getHTTPClientService(), |
|
| 367 | - \OC::$server->getTempManager(), |
|
| 368 | - \OC::$server->getLogger() |
|
| 369 | - ); |
|
| 370 | - $isDownloaded = $installer->isDownloaded($appId); |
|
| 371 | - |
|
| 372 | - if(!$isDownloaded) { |
|
| 373 | - $installer->downloadApp($appId); |
|
| 374 | - } |
|
| 375 | - |
|
| 376 | - if (!Installer::isInstalled($appId)) { |
|
| 377 | - $appId = self::installApp( |
|
| 378 | - $appId, |
|
| 379 | - $config, |
|
| 380 | - $l |
|
| 381 | - ); |
|
| 382 | - $appPath = self::getAppPath($appId); |
|
| 383 | - self::registerAutoloading($appId, $appPath); |
|
| 384 | - $installer->installApp($appId); |
|
| 385 | - } else { |
|
| 386 | - // check for required dependencies |
|
| 387 | - $info = self::getAppInfo($appId); |
|
| 388 | - self::checkAppDependencies($config, $l, $info); |
|
| 389 | - $appPath = self::getAppPath($appId); |
|
| 390 | - self::registerAutoloading($appId, $appPath); |
|
| 391 | - $installer->installApp($appId); |
|
| 392 | - } |
|
| 393 | - |
|
| 394 | - $appManager = \OC::$server->getAppManager(); |
|
| 395 | - if (!is_null($groups)) { |
|
| 396 | - $groupManager = \OC::$server->getGroupManager(); |
|
| 397 | - $groupsList = []; |
|
| 398 | - foreach ($groups as $group) { |
|
| 399 | - $groupItem = $groupManager->get($group); |
|
| 400 | - if ($groupItem instanceof \OCP\IGroup) { |
|
| 401 | - $groupsList[] = $groupManager->get($group); |
|
| 402 | - } |
|
| 403 | - } |
|
| 404 | - $appManager->enableAppForGroups($appId, $groupsList); |
|
| 405 | - } else { |
|
| 406 | - $appManager->enableApp($appId); |
|
| 407 | - } |
|
| 408 | - |
|
| 409 | - $info = self::getAppInfo($appId); |
|
| 410 | - if(isset($info['settings']) && is_array($info['settings'])) { |
|
| 411 | - $appPath = self::getAppPath($appId); |
|
| 412 | - self::registerAutoloading($appId, $appPath); |
|
| 413 | - \OC::$server->getSettingsManager()->setupSettings($info['settings']); |
|
| 414 | - } |
|
| 415 | - } |
|
| 416 | - |
|
| 417 | - /** |
|
| 418 | - * @param string $app |
|
| 419 | - * @return bool |
|
| 420 | - */ |
|
| 421 | - public static function removeApp($app) { |
|
| 422 | - if (self::isShipped($app)) { |
|
| 423 | - return false; |
|
| 424 | - } |
|
| 425 | - |
|
| 426 | - $installer = new Installer( |
|
| 427 | - \OC::$server->getAppFetcher(), |
|
| 428 | - \OC::$server->getHTTPClientService(), |
|
| 429 | - \OC::$server->getTempManager(), |
|
| 430 | - \OC::$server->getLogger() |
|
| 431 | - ); |
|
| 432 | - return $installer->removeApp($app); |
|
| 433 | - } |
|
| 434 | - |
|
| 435 | - /** |
|
| 436 | - * This function set an app as disabled in appconfig. |
|
| 437 | - * |
|
| 438 | - * @param string $app app |
|
| 439 | - * @throws Exception |
|
| 440 | - */ |
|
| 441 | - public static function disable($app) { |
|
| 442 | - // flush |
|
| 443 | - self::$enabledAppsCache = array(); |
|
| 444 | - |
|
| 445 | - // run uninstall steps |
|
| 446 | - $appData = OC_App::getAppInfo($app); |
|
| 447 | - if (!is_null($appData)) { |
|
| 448 | - OC_App::executeRepairSteps($app, $appData['repair-steps']['uninstall']); |
|
| 449 | - } |
|
| 450 | - |
|
| 451 | - // emit disable hook - needed anymore ? |
|
| 452 | - \OC_Hook::emit('OC_App', 'pre_disable', array('app' => $app)); |
|
| 453 | - |
|
| 454 | - // finally disable it |
|
| 455 | - $appManager = \OC::$server->getAppManager(); |
|
| 456 | - $appManager->disableApp($app); |
|
| 457 | - } |
|
| 458 | - |
|
| 459 | - /** |
|
| 460 | - * Returns the Settings Navigation |
|
| 461 | - * |
|
| 462 | - * @return string[] |
|
| 463 | - * |
|
| 464 | - * This function returns an array containing all settings pages added. The |
|
| 465 | - * entries are sorted by the key 'order' ascending. |
|
| 466 | - */ |
|
| 467 | - public static function getSettingsNavigation() { |
|
| 468 | - $l = \OC::$server->getL10N('lib'); |
|
| 469 | - $urlGenerator = \OC::$server->getURLGenerator(); |
|
| 470 | - |
|
| 471 | - $settings = array(); |
|
| 472 | - // by default, settings only contain the help menu |
|
| 473 | - if (\OC::$server->getSystemConfig()->getValue('knowledgebaseenabled', true)) { |
|
| 474 | - $settings = array( |
|
| 475 | - array( |
|
| 476 | - "id" => "help", |
|
| 477 | - "order" => 4, |
|
| 478 | - "href" => $urlGenerator->linkToRoute('settings_help'), |
|
| 479 | - "name" => $l->t("Help"), |
|
| 480 | - "icon" => $urlGenerator->imagePath("settings", "help.svg") |
|
| 481 | - ) |
|
| 482 | - ); |
|
| 483 | - } |
|
| 484 | - |
|
| 485 | - // if the user is logged-in |
|
| 486 | - if (\OC::$server->getUserSession()->isLoggedIn()) { |
|
| 487 | - // personal menu |
|
| 488 | - $settings[] = array( |
|
| 489 | - "id" => "personal", |
|
| 490 | - "order" => 1, |
|
| 491 | - "href" => $urlGenerator->linkToRoute('settings_personal'), |
|
| 492 | - "name" => $l->t("Personal"), |
|
| 493 | - "icon" => $urlGenerator->imagePath("settings", "personal.svg") |
|
| 494 | - ); |
|
| 495 | - |
|
| 496 | - //SubAdmins are also allowed to access user management |
|
| 497 | - $userObject = \OC::$server->getUserSession()->getUser(); |
|
| 498 | - $isSubAdmin = false; |
|
| 499 | - if($userObject !== null) { |
|
| 500 | - $isSubAdmin = \OC::$server->getGroupManager()->getSubAdmin()->isSubAdmin($userObject); |
|
| 501 | - } |
|
| 502 | - if ($isSubAdmin) { |
|
| 503 | - // admin users menu |
|
| 504 | - $settings[] = array( |
|
| 505 | - "id" => "core_users", |
|
| 506 | - "order" => 3, |
|
| 507 | - "href" => $urlGenerator->linkToRoute('settings_users'), |
|
| 508 | - "name" => $l->t("Users"), |
|
| 509 | - "icon" => $urlGenerator->imagePath("settings", "users.svg") |
|
| 510 | - ); |
|
| 511 | - } |
|
| 512 | - |
|
| 513 | - // if the user is an admin |
|
| 514 | - if (OC_User::isAdminUser(OC_User::getUser())) { |
|
| 515 | - // admin settings |
|
| 516 | - $settings[] = array( |
|
| 517 | - "id" => "admin", |
|
| 518 | - "order" => 2, |
|
| 519 | - "href" => $urlGenerator->linkToRoute('settings.AdminSettings.index'), |
|
| 520 | - "name" => $l->t("Admin"), |
|
| 521 | - "icon" => $urlGenerator->imagePath("settings", "admin.svg") |
|
| 522 | - ); |
|
| 523 | - } |
|
| 524 | - } |
|
| 525 | - |
|
| 526 | - $navigation = self::proceedNavigation($settings); |
|
| 527 | - return $navigation; |
|
| 528 | - } |
|
| 529 | - |
|
| 530 | - // This is private as well. It simply works, so don't ask for more details |
|
| 531 | - private static function proceedNavigation($list) { |
|
| 532 | - $headerIconCount = 8; |
|
| 533 | - if(OC_User::isAdminUser(OC_User::getUser())) { |
|
| 534 | - $headerIconCount--; |
|
| 535 | - } |
|
| 536 | - usort($list, function($a, $b) { |
|
| 537 | - if (isset($a['order']) && isset($b['order'])) { |
|
| 538 | - return ($a['order'] < $b['order']) ? -1 : 1; |
|
| 539 | - } else if (isset($a['order']) || isset($b['order'])) { |
|
| 540 | - return isset($a['order']) ? -1 : 1; |
|
| 541 | - } else { |
|
| 542 | - return ($a['name'] < $b['name']) ? -1 : 1; |
|
| 543 | - } |
|
| 544 | - }); |
|
| 545 | - |
|
| 546 | - $activeAppIndex = -1; |
|
| 547 | - $activeApp = OC::$server->getNavigationManager()->getActiveEntry(); |
|
| 548 | - foreach ($list as $index => &$navEntry) { |
|
| 549 | - if ($navEntry['id'] == $activeApp) { |
|
| 550 | - $navEntry['active'] = true; |
|
| 551 | - $activeAppIndex = $index; |
|
| 552 | - } else { |
|
| 553 | - $navEntry['active'] = false; |
|
| 554 | - } |
|
| 555 | - } |
|
| 556 | - unset($navEntry); |
|
| 557 | - |
|
| 558 | - if($activeAppIndex > ($headerIconCount-1)) { |
|
| 559 | - $active = $list[$activeAppIndex]; |
|
| 560 | - $lastInHeader = $list[$headerIconCount-1]; |
|
| 561 | - $list[$headerIconCount-1] = $active; |
|
| 562 | - $list[$activeAppIndex] = $lastInHeader; |
|
| 563 | - } |
|
| 564 | - |
|
| 565 | - foreach ($list as $index => &$navEntry) { |
|
| 566 | - $navEntry['showInHeader'] = false; |
|
| 567 | - if($index < $headerIconCount) { |
|
| 568 | - $navEntry['showInHeader'] = true; |
|
| 569 | - } |
|
| 570 | - } |
|
| 571 | - |
|
| 572 | - |
|
| 573 | - |
|
| 574 | - return $list; |
|
| 575 | - } |
|
| 576 | - |
|
| 577 | - public static function proceedAppNavigation($entries) { |
|
| 578 | - $headerIconCount = 8; |
|
| 579 | - if(OC_User::isAdminUser(OC_User::getUser())) { |
|
| 580 | - $headerIconCount--; |
|
| 581 | - } |
|
| 582 | - $activeAppIndex = -1; |
|
| 583 | - $list = self::proceedNavigation($entries); |
|
| 584 | - |
|
| 585 | - $activeApp = OC::$server->getNavigationManager()->getActiveEntry(); |
|
| 586 | - foreach ($list as $index => &$navEntry) { |
|
| 587 | - if ($navEntry['id'] == $activeApp) { |
|
| 588 | - $navEntry['active'] = true; |
|
| 589 | - $activeAppIndex = $index; |
|
| 590 | - } else { |
|
| 591 | - $navEntry['active'] = false; |
|
| 592 | - } |
|
| 593 | - } |
|
| 594 | - // move active item to last position |
|
| 595 | - if($activeAppIndex > ($headerIconCount-1)) { |
|
| 596 | - $active = $list[$activeAppIndex]; |
|
| 597 | - $lastInHeader = $list[$headerIconCount-1]; |
|
| 598 | - $list[$headerIconCount-1] = $active; |
|
| 599 | - $list[$activeAppIndex] = $lastInHeader; |
|
| 600 | - } |
|
| 601 | - $list = array_slice($list, 0, $headerIconCount); |
|
| 602 | - |
|
| 603 | - return $list; |
|
| 604 | - } |
|
| 605 | - |
|
| 606 | - /** |
|
| 607 | - * Get the path where to install apps |
|
| 608 | - * |
|
| 609 | - * @return string|false |
|
| 610 | - */ |
|
| 611 | - public static function getInstallPath() { |
|
| 612 | - if (\OC::$server->getSystemConfig()->getValue('appstoreenabled', true) == false) { |
|
| 613 | - return false; |
|
| 614 | - } |
|
| 615 | - |
|
| 616 | - foreach (OC::$APPSROOTS as $dir) { |
|
| 617 | - if (isset($dir['writable']) && $dir['writable'] === true) { |
|
| 618 | - return $dir['path']; |
|
| 619 | - } |
|
| 620 | - } |
|
| 621 | - |
|
| 622 | - \OCP\Util::writeLog('core', 'No application directories are marked as writable.', \OCP\Util::ERROR); |
|
| 623 | - return null; |
|
| 624 | - } |
|
| 625 | - |
|
| 626 | - |
|
| 627 | - /** |
|
| 628 | - * search for an app in all app-directories |
|
| 629 | - * |
|
| 630 | - * @param string $appId |
|
| 631 | - * @return false|string |
|
| 632 | - */ |
|
| 633 | - public static function findAppInDirectories($appId) { |
|
| 634 | - $sanitizedAppId = self::cleanAppId($appId); |
|
| 635 | - if($sanitizedAppId !== $appId) { |
|
| 636 | - return false; |
|
| 637 | - } |
|
| 638 | - static $app_dir = array(); |
|
| 639 | - |
|
| 640 | - if (isset($app_dir[$appId])) { |
|
| 641 | - return $app_dir[$appId]; |
|
| 642 | - } |
|
| 643 | - |
|
| 644 | - $possibleApps = array(); |
|
| 645 | - foreach (OC::$APPSROOTS as $dir) { |
|
| 646 | - if (file_exists($dir['path'] . '/' . $appId)) { |
|
| 647 | - $possibleApps[] = $dir; |
|
| 648 | - } |
|
| 649 | - } |
|
| 650 | - |
|
| 651 | - if (empty($possibleApps)) { |
|
| 652 | - return false; |
|
| 653 | - } elseif (count($possibleApps) === 1) { |
|
| 654 | - $dir = array_shift($possibleApps); |
|
| 655 | - $app_dir[$appId] = $dir; |
|
| 656 | - return $dir; |
|
| 657 | - } else { |
|
| 658 | - $versionToLoad = array(); |
|
| 659 | - foreach ($possibleApps as $possibleApp) { |
|
| 660 | - $version = self::getAppVersionByPath($possibleApp['path']); |
|
| 661 | - if (empty($versionToLoad) || version_compare($version, $versionToLoad['version'], '>')) { |
|
| 662 | - $versionToLoad = array( |
|
| 663 | - 'dir' => $possibleApp, |
|
| 664 | - 'version' => $version, |
|
| 665 | - ); |
|
| 666 | - } |
|
| 667 | - } |
|
| 668 | - $app_dir[$appId] = $versionToLoad['dir']; |
|
| 669 | - return $versionToLoad['dir']; |
|
| 670 | - //TODO - write test |
|
| 671 | - } |
|
| 672 | - } |
|
| 673 | - |
|
| 674 | - /** |
|
| 675 | - * Get the directory for the given app. |
|
| 676 | - * If the app is defined in multiple directories, the first one is taken. (false if not found) |
|
| 677 | - * |
|
| 678 | - * @param string $appId |
|
| 679 | - * @return string|false |
|
| 680 | - */ |
|
| 681 | - public static function getAppPath($appId) { |
|
| 682 | - if ($appId === null || trim($appId) === '') { |
|
| 683 | - return false; |
|
| 684 | - } |
|
| 685 | - |
|
| 686 | - if (($dir = self::findAppInDirectories($appId)) != false) { |
|
| 687 | - return $dir['path'] . '/' . $appId; |
|
| 688 | - } |
|
| 689 | - return false; |
|
| 690 | - } |
|
| 691 | - |
|
| 692 | - /** |
|
| 693 | - * Get the path for the given app on the access |
|
| 694 | - * If the app is defined in multiple directories, the first one is taken. (false if not found) |
|
| 695 | - * |
|
| 696 | - * @param string $appId |
|
| 697 | - * @return string|false |
|
| 698 | - */ |
|
| 699 | - public static function getAppWebPath($appId) { |
|
| 700 | - if (($dir = self::findAppInDirectories($appId)) != false) { |
|
| 701 | - return OC::$WEBROOT . $dir['url'] . '/' . $appId; |
|
| 702 | - } |
|
| 703 | - return false; |
|
| 704 | - } |
|
| 705 | - |
|
| 706 | - /** |
|
| 707 | - * get the last version of the app from appinfo/info.xml |
|
| 708 | - * |
|
| 709 | - * @param string $appId |
|
| 710 | - * @param bool $useCache |
|
| 711 | - * @return string |
|
| 712 | - */ |
|
| 713 | - public static function getAppVersion($appId, $useCache = true) { |
|
| 714 | - if($useCache && isset(self::$appVersion[$appId])) { |
|
| 715 | - return self::$appVersion[$appId]; |
|
| 716 | - } |
|
| 717 | - |
|
| 718 | - $file = self::getAppPath($appId); |
|
| 719 | - self::$appVersion[$appId] = ($file !== false) ? self::getAppVersionByPath($file) : '0'; |
|
| 720 | - return self::$appVersion[$appId]; |
|
| 721 | - } |
|
| 722 | - |
|
| 723 | - /** |
|
| 724 | - * get app's version based on it's path |
|
| 725 | - * |
|
| 726 | - * @param string $path |
|
| 727 | - * @return string |
|
| 728 | - */ |
|
| 729 | - public static function getAppVersionByPath($path) { |
|
| 730 | - $infoFile = $path . '/appinfo/info.xml'; |
|
| 731 | - $appData = self::getAppInfo($infoFile, true); |
|
| 732 | - return isset($appData['version']) ? $appData['version'] : ''; |
|
| 733 | - } |
|
| 734 | - |
|
| 735 | - |
|
| 736 | - /** |
|
| 737 | - * Read all app metadata from the info.xml file |
|
| 738 | - * |
|
| 739 | - * @param string $appId id of the app or the path of the info.xml file |
|
| 740 | - * @param bool $path |
|
| 741 | - * @param string $lang |
|
| 742 | - * @return array|null |
|
| 743 | - * @note all data is read from info.xml, not just pre-defined fields |
|
| 744 | - */ |
|
| 745 | - public static function getAppInfo($appId, $path = false, $lang = null) { |
|
| 746 | - if ($path) { |
|
| 747 | - $file = $appId; |
|
| 748 | - } else { |
|
| 749 | - if ($lang === null && isset(self::$appInfo[$appId])) { |
|
| 750 | - return self::$appInfo[$appId]; |
|
| 751 | - } |
|
| 752 | - $appPath = self::getAppPath($appId); |
|
| 753 | - if($appPath === false) { |
|
| 754 | - return null; |
|
| 755 | - } |
|
| 756 | - $file = $appPath . '/appinfo/info.xml'; |
|
| 757 | - } |
|
| 758 | - |
|
| 759 | - $parser = new InfoParser(\OC::$server->getMemCacheFactory()->create('core.appinfo')); |
|
| 760 | - $data = $parser->parse($file); |
|
| 761 | - |
|
| 762 | - if (is_array($data)) { |
|
| 763 | - $data = OC_App::parseAppInfo($data, $lang); |
|
| 764 | - } |
|
| 765 | - if(isset($data['ocsid'])) { |
|
| 766 | - $storedId = \OC::$server->getConfig()->getAppValue($appId, 'ocsid'); |
|
| 767 | - if($storedId !== '' && $storedId !== $data['ocsid']) { |
|
| 768 | - $data['ocsid'] = $storedId; |
|
| 769 | - } |
|
| 770 | - } |
|
| 771 | - |
|
| 772 | - if ($lang === null) { |
|
| 773 | - self::$appInfo[$appId] = $data; |
|
| 774 | - } |
|
| 775 | - |
|
| 776 | - return $data; |
|
| 777 | - } |
|
| 778 | - |
|
| 779 | - /** |
|
| 780 | - * Returns the navigation |
|
| 781 | - * |
|
| 782 | - * @return array |
|
| 783 | - * |
|
| 784 | - * This function returns an array containing all entries added. The |
|
| 785 | - * entries are sorted by the key 'order' ascending. Additional to the keys |
|
| 786 | - * given for each app the following keys exist: |
|
| 787 | - * - active: boolean, signals if the user is on this navigation entry |
|
| 788 | - */ |
|
| 789 | - public static function getNavigation() { |
|
| 790 | - $entries = OC::$server->getNavigationManager()->getAll(); |
|
| 791 | - $navigation = self::proceedNavigation($entries); |
|
| 792 | - return $navigation; |
|
| 793 | - } |
|
| 794 | - |
|
| 795 | - /** |
|
| 796 | - * Returns the navigation inside the header bar |
|
| 797 | - * |
|
| 798 | - * @return array |
|
| 799 | - * |
|
| 800 | - * This function returns an array containing all entries added. The |
|
| 801 | - * entries are sorted by the key 'order' ascending. Additional to the keys |
|
| 802 | - * given for each app the following keys exist: |
|
| 803 | - * - active: boolean, signals if the user is on this navigation entry |
|
| 804 | - */ |
|
| 805 | - public static function getHeaderNavigation() { |
|
| 806 | - $entries = OC::$server->getNavigationManager()->getAll(); |
|
| 807 | - $navigation = self::proceedAppNavigation($entries); |
|
| 808 | - return $navigation; |
|
| 809 | - } |
|
| 810 | - |
|
| 811 | - /** |
|
| 812 | - * get the id of loaded app |
|
| 813 | - * |
|
| 814 | - * @return string |
|
| 815 | - */ |
|
| 816 | - public static function getCurrentApp() { |
|
| 817 | - $request = \OC::$server->getRequest(); |
|
| 818 | - $script = substr($request->getScriptName(), strlen(OC::$WEBROOT) + 1); |
|
| 819 | - $topFolder = substr($script, 0, strpos($script, '/')); |
|
| 820 | - if (empty($topFolder)) { |
|
| 821 | - $path_info = $request->getPathInfo(); |
|
| 822 | - if ($path_info) { |
|
| 823 | - $topFolder = substr($path_info, 1, strpos($path_info, '/', 1) - 1); |
|
| 824 | - } |
|
| 825 | - } |
|
| 826 | - if ($topFolder == 'apps') { |
|
| 827 | - $length = strlen($topFolder); |
|
| 828 | - return substr($script, $length + 1, strpos($script, '/', $length + 1) - $length - 1); |
|
| 829 | - } else { |
|
| 830 | - return $topFolder; |
|
| 831 | - } |
|
| 832 | - } |
|
| 833 | - |
|
| 834 | - /** |
|
| 835 | - * @param string $type |
|
| 836 | - * @return array |
|
| 837 | - */ |
|
| 838 | - public static function getForms($type) { |
|
| 839 | - $forms = array(); |
|
| 840 | - switch ($type) { |
|
| 841 | - case 'admin': |
|
| 842 | - $source = self::$adminForms; |
|
| 843 | - break; |
|
| 844 | - case 'personal': |
|
| 845 | - $source = self::$personalForms; |
|
| 846 | - break; |
|
| 847 | - default: |
|
| 848 | - return array(); |
|
| 849 | - } |
|
| 850 | - foreach ($source as $form) { |
|
| 851 | - $forms[] = include $form; |
|
| 852 | - } |
|
| 853 | - return $forms; |
|
| 854 | - } |
|
| 855 | - |
|
| 856 | - /** |
|
| 857 | - * register an admin form to be shown |
|
| 858 | - * |
|
| 859 | - * @param string $app |
|
| 860 | - * @param string $page |
|
| 861 | - */ |
|
| 862 | - public static function registerAdmin($app, $page) { |
|
| 863 | - self::$adminForms[] = $app . '/' . $page . '.php'; |
|
| 864 | - } |
|
| 865 | - |
|
| 866 | - /** |
|
| 867 | - * register a personal form to be shown |
|
| 868 | - * @param string $app |
|
| 869 | - * @param string $page |
|
| 870 | - */ |
|
| 871 | - public static function registerPersonal($app, $page) { |
|
| 872 | - self::$personalForms[] = $app . '/' . $page . '.php'; |
|
| 873 | - } |
|
| 874 | - |
|
| 875 | - /** |
|
| 876 | - * @param array $entry |
|
| 877 | - */ |
|
| 878 | - public static function registerLogIn(array $entry) { |
|
| 879 | - self::$altLogin[] = $entry; |
|
| 880 | - } |
|
| 881 | - |
|
| 882 | - /** |
|
| 883 | - * @return array |
|
| 884 | - */ |
|
| 885 | - public static function getAlternativeLogIns() { |
|
| 886 | - return self::$altLogin; |
|
| 887 | - } |
|
| 888 | - |
|
| 889 | - /** |
|
| 890 | - * get a list of all apps in the apps folder |
|
| 891 | - * |
|
| 892 | - * @return array an array of app names (string IDs) |
|
| 893 | - * @todo: change the name of this method to getInstalledApps, which is more accurate |
|
| 894 | - */ |
|
| 895 | - public static function getAllApps() { |
|
| 896 | - |
|
| 897 | - $apps = array(); |
|
| 898 | - |
|
| 899 | - foreach (OC::$APPSROOTS as $apps_dir) { |
|
| 900 | - if (!is_readable($apps_dir['path'])) { |
|
| 901 | - \OCP\Util::writeLog('core', 'unable to read app folder : ' . $apps_dir['path'], \OCP\Util::WARN); |
|
| 902 | - continue; |
|
| 903 | - } |
|
| 904 | - $dh = opendir($apps_dir['path']); |
|
| 905 | - |
|
| 906 | - if (is_resource($dh)) { |
|
| 907 | - while (($file = readdir($dh)) !== false) { |
|
| 908 | - |
|
| 909 | - if ($file[0] != '.' and is_dir($apps_dir['path'] . '/' . $file) and is_file($apps_dir['path'] . '/' . $file . '/appinfo/info.xml')) { |
|
| 910 | - |
|
| 911 | - $apps[] = $file; |
|
| 912 | - } |
|
| 913 | - } |
|
| 914 | - } |
|
| 915 | - } |
|
| 916 | - |
|
| 917 | - return $apps; |
|
| 918 | - } |
|
| 919 | - |
|
| 920 | - /** |
|
| 921 | - * List all apps, this is used in apps.php |
|
| 922 | - * |
|
| 923 | - * @return array |
|
| 924 | - */ |
|
| 925 | - public function listAllApps() { |
|
| 926 | - $installedApps = OC_App::getAllApps(); |
|
| 927 | - |
|
| 928 | - //we don't want to show configuration for these |
|
| 929 | - $blacklist = \OC::$server->getAppManager()->getAlwaysEnabledApps(); |
|
| 930 | - $appList = array(); |
|
| 931 | - $langCode = \OC::$server->getL10N('core')->getLanguageCode(); |
|
| 932 | - $urlGenerator = \OC::$server->getURLGenerator(); |
|
| 933 | - |
|
| 934 | - foreach ($installedApps as $app) { |
|
| 935 | - if (array_search($app, $blacklist) === false) { |
|
| 936 | - |
|
| 937 | - $info = OC_App::getAppInfo($app, false, $langCode); |
|
| 938 | - if (!is_array($info)) { |
|
| 939 | - \OCP\Util::writeLog('core', 'Could not read app info file for app "' . $app . '"', \OCP\Util::ERROR); |
|
| 940 | - continue; |
|
| 941 | - } |
|
| 942 | - |
|
| 943 | - if (!isset($info['name'])) { |
|
| 944 | - \OCP\Util::writeLog('core', 'App id "' . $app . '" has no name in appinfo', \OCP\Util::ERROR); |
|
| 945 | - continue; |
|
| 946 | - } |
|
| 947 | - |
|
| 948 | - $enabled = \OC::$server->getAppConfig()->getValue($app, 'enabled', 'no'); |
|
| 949 | - $info['groups'] = null; |
|
| 950 | - if ($enabled === 'yes') { |
|
| 951 | - $active = true; |
|
| 952 | - } else if ($enabled === 'no') { |
|
| 953 | - $active = false; |
|
| 954 | - } else { |
|
| 955 | - $active = true; |
|
| 956 | - $info['groups'] = $enabled; |
|
| 957 | - } |
|
| 958 | - |
|
| 959 | - $info['active'] = $active; |
|
| 960 | - |
|
| 961 | - if (self::isShipped($app)) { |
|
| 962 | - $info['internal'] = true; |
|
| 963 | - $info['level'] = self::officialApp; |
|
| 964 | - $info['removable'] = false; |
|
| 965 | - } else { |
|
| 966 | - $info['internal'] = false; |
|
| 967 | - $info['removable'] = true; |
|
| 968 | - } |
|
| 969 | - |
|
| 970 | - $appPath = self::getAppPath($app); |
|
| 971 | - if($appPath !== false) { |
|
| 972 | - $appIcon = $appPath . '/img/' . $app . '.svg'; |
|
| 973 | - if (file_exists($appIcon)) { |
|
| 974 | - $info['preview'] = \OC::$server->getURLGenerator()->imagePath($app, $app . '.svg'); |
|
| 975 | - $info['previewAsIcon'] = true; |
|
| 976 | - } else { |
|
| 977 | - $appIcon = $appPath . '/img/app.svg'; |
|
| 978 | - if (file_exists($appIcon)) { |
|
| 979 | - $info['preview'] = \OC::$server->getURLGenerator()->imagePath($app, 'app.svg'); |
|
| 980 | - $info['previewAsIcon'] = true; |
|
| 981 | - } |
|
| 982 | - } |
|
| 983 | - } |
|
| 984 | - // fix documentation |
|
| 985 | - if (isset($info['documentation']) && is_array($info['documentation'])) { |
|
| 986 | - foreach ($info['documentation'] as $key => $url) { |
|
| 987 | - // If it is not an absolute URL we assume it is a key |
|
| 988 | - // i.e. admin-ldap will get converted to go.php?to=admin-ldap |
|
| 989 | - if (stripos($url, 'https://') !== 0 && stripos($url, 'http://') !== 0) { |
|
| 990 | - $url = $urlGenerator->linkToDocs($url); |
|
| 991 | - } |
|
| 992 | - |
|
| 993 | - $info['documentation'][$key] = $url; |
|
| 994 | - } |
|
| 995 | - } |
|
| 996 | - |
|
| 997 | - $info['version'] = OC_App::getAppVersion($app); |
|
| 998 | - $appList[] = $info; |
|
| 999 | - } |
|
| 1000 | - } |
|
| 1001 | - |
|
| 1002 | - return $appList; |
|
| 1003 | - } |
|
| 1004 | - |
|
| 1005 | - /** |
|
| 1006 | - * Returns the internal app ID or false |
|
| 1007 | - * @param string $ocsID |
|
| 1008 | - * @return string|false |
|
| 1009 | - */ |
|
| 1010 | - public static function getInternalAppIdByOcs($ocsID) { |
|
| 1011 | - if(is_numeric($ocsID)) { |
|
| 1012 | - $idArray = \OC::$server->getAppConfig()->getValues(false, 'ocsid'); |
|
| 1013 | - if(array_search($ocsID, $idArray)) { |
|
| 1014 | - return array_search($ocsID, $idArray); |
|
| 1015 | - } |
|
| 1016 | - } |
|
| 1017 | - return false; |
|
| 1018 | - } |
|
| 1019 | - |
|
| 1020 | - public static function shouldUpgrade($app) { |
|
| 1021 | - $versions = self::getAppVersions(); |
|
| 1022 | - $currentVersion = OC_App::getAppVersion($app); |
|
| 1023 | - if ($currentVersion && isset($versions[$app])) { |
|
| 1024 | - $installedVersion = $versions[$app]; |
|
| 1025 | - if (!version_compare($currentVersion, $installedVersion, '=')) { |
|
| 1026 | - return true; |
|
| 1027 | - } |
|
| 1028 | - } |
|
| 1029 | - return false; |
|
| 1030 | - } |
|
| 1031 | - |
|
| 1032 | - /** |
|
| 1033 | - * Adjust the number of version parts of $version1 to match |
|
| 1034 | - * the number of version parts of $version2. |
|
| 1035 | - * |
|
| 1036 | - * @param string $version1 version to adjust |
|
| 1037 | - * @param string $version2 version to take the number of parts from |
|
| 1038 | - * @return string shortened $version1 |
|
| 1039 | - */ |
|
| 1040 | - private static function adjustVersionParts($version1, $version2) { |
|
| 1041 | - $version1 = explode('.', $version1); |
|
| 1042 | - $version2 = explode('.', $version2); |
|
| 1043 | - // reduce $version1 to match the number of parts in $version2 |
|
| 1044 | - while (count($version1) > count($version2)) { |
|
| 1045 | - array_pop($version1); |
|
| 1046 | - } |
|
| 1047 | - // if $version1 does not have enough parts, add some |
|
| 1048 | - while (count($version1) < count($version2)) { |
|
| 1049 | - $version1[] = '0'; |
|
| 1050 | - } |
|
| 1051 | - return implode('.', $version1); |
|
| 1052 | - } |
|
| 1053 | - |
|
| 1054 | - /** |
|
| 1055 | - * Check whether the current ownCloud version matches the given |
|
| 1056 | - * application's version requirements. |
|
| 1057 | - * |
|
| 1058 | - * The comparison is made based on the number of parts that the |
|
| 1059 | - * app info version has. For example for ownCloud 6.0.3 if the |
|
| 1060 | - * app info version is expecting version 6.0, the comparison is |
|
| 1061 | - * made on the first two parts of the ownCloud version. |
|
| 1062 | - * This means that it's possible to specify "requiremin" => 6 |
|
| 1063 | - * and "requiremax" => 6 and it will still match ownCloud 6.0.3. |
|
| 1064 | - * |
|
| 1065 | - * @param string $ocVersion ownCloud version to check against |
|
| 1066 | - * @param array $appInfo app info (from xml) |
|
| 1067 | - * |
|
| 1068 | - * @return boolean true if compatible, otherwise false |
|
| 1069 | - */ |
|
| 1070 | - public static function isAppCompatible($ocVersion, $appInfo) { |
|
| 1071 | - $requireMin = ''; |
|
| 1072 | - $requireMax = ''; |
|
| 1073 | - if (isset($appInfo['dependencies']['nextcloud']['@attributes']['min-version'])) { |
|
| 1074 | - $requireMin = $appInfo['dependencies']['nextcloud']['@attributes']['min-version']; |
|
| 1075 | - } elseif (isset($appInfo['dependencies']['owncloud']['@attributes']['min-version'])) { |
|
| 1076 | - $requireMin = $appInfo['dependencies']['owncloud']['@attributes']['min-version']; |
|
| 1077 | - } else if (isset($appInfo['requiremin'])) { |
|
| 1078 | - $requireMin = $appInfo['requiremin']; |
|
| 1079 | - } else if (isset($appInfo['require'])) { |
|
| 1080 | - $requireMin = $appInfo['require']; |
|
| 1081 | - } |
|
| 1082 | - |
|
| 1083 | - if (isset($appInfo['dependencies']['nextcloud']['@attributes']['max-version'])) { |
|
| 1084 | - $requireMax = $appInfo['dependencies']['nextcloud']['@attributes']['max-version']; |
|
| 1085 | - } elseif (isset($appInfo['dependencies']['owncloud']['@attributes']['max-version'])) { |
|
| 1086 | - $requireMax = $appInfo['dependencies']['owncloud']['@attributes']['max-version']; |
|
| 1087 | - } else if (isset($appInfo['requiremax'])) { |
|
| 1088 | - $requireMax = $appInfo['requiremax']; |
|
| 1089 | - } |
|
| 1090 | - |
|
| 1091 | - if (is_array($ocVersion)) { |
|
| 1092 | - $ocVersion = implode('.', $ocVersion); |
|
| 1093 | - } |
|
| 1094 | - |
|
| 1095 | - if (!empty($requireMin) |
|
| 1096 | - && version_compare(self::adjustVersionParts($ocVersion, $requireMin), $requireMin, '<') |
|
| 1097 | - ) { |
|
| 1098 | - |
|
| 1099 | - return false; |
|
| 1100 | - } |
|
| 1101 | - |
|
| 1102 | - if (!empty($requireMax) |
|
| 1103 | - && version_compare(self::adjustVersionParts($ocVersion, $requireMax), $requireMax, '>') |
|
| 1104 | - ) { |
|
| 1105 | - return false; |
|
| 1106 | - } |
|
| 1107 | - |
|
| 1108 | - return true; |
|
| 1109 | - } |
|
| 1110 | - |
|
| 1111 | - /** |
|
| 1112 | - * get the installed version of all apps |
|
| 1113 | - */ |
|
| 1114 | - public static function getAppVersions() { |
|
| 1115 | - static $versions; |
|
| 1116 | - |
|
| 1117 | - if(!$versions) { |
|
| 1118 | - $appConfig = \OC::$server->getAppConfig(); |
|
| 1119 | - $versions = $appConfig->getValues(false, 'installed_version'); |
|
| 1120 | - } |
|
| 1121 | - return $versions; |
|
| 1122 | - } |
|
| 1123 | - |
|
| 1124 | - /** |
|
| 1125 | - * @param string $app |
|
| 1126 | - * @param \OCP\IConfig $config |
|
| 1127 | - * @param \OCP\IL10N $l |
|
| 1128 | - * @return bool |
|
| 1129 | - * |
|
| 1130 | - * @throws Exception if app is not compatible with this version of ownCloud |
|
| 1131 | - * @throws Exception if no app-name was specified |
|
| 1132 | - */ |
|
| 1133 | - public function installApp($app, |
|
| 1134 | - \OCP\IConfig $config, |
|
| 1135 | - \OCP\IL10N $l) { |
|
| 1136 | - if ($app !== false) { |
|
| 1137 | - // check if the app is compatible with this version of ownCloud |
|
| 1138 | - $info = self::getAppInfo($app); |
|
| 1139 | - if(!is_array($info)) { |
|
| 1140 | - throw new \Exception( |
|
| 1141 | - $l->t('App "%s" cannot be installed because appinfo file cannot be read.', |
|
| 1142 | - [$info['name']] |
|
| 1143 | - ) |
|
| 1144 | - ); |
|
| 1145 | - } |
|
| 1146 | - |
|
| 1147 | - $version = \OCP\Util::getVersion(); |
|
| 1148 | - if (!self::isAppCompatible($version, $info)) { |
|
| 1149 | - throw new \Exception( |
|
| 1150 | - $l->t('App "%s" cannot be installed because it is not compatible with this version of the server.', |
|
| 1151 | - array($info['name']) |
|
| 1152 | - ) |
|
| 1153 | - ); |
|
| 1154 | - } |
|
| 1155 | - |
|
| 1156 | - // check for required dependencies |
|
| 1157 | - self::checkAppDependencies($config, $l, $info); |
|
| 1158 | - |
|
| 1159 | - $config->setAppValue($app, 'enabled', 'yes'); |
|
| 1160 | - if (isset($appData['id'])) { |
|
| 1161 | - $config->setAppValue($app, 'ocsid', $appData['id']); |
|
| 1162 | - } |
|
| 1163 | - |
|
| 1164 | - if(isset($info['settings']) && is_array($info['settings'])) { |
|
| 1165 | - $appPath = self::getAppPath($app); |
|
| 1166 | - self::registerAutoloading($app, $appPath); |
|
| 1167 | - \OC::$server->getSettingsManager()->setupSettings($info['settings']); |
|
| 1168 | - } |
|
| 1169 | - |
|
| 1170 | - \OC_Hook::emit('OC_App', 'post_enable', array('app' => $app)); |
|
| 1171 | - } else { |
|
| 1172 | - if(empty($appName) ) { |
|
| 1173 | - throw new \Exception($l->t("No app name specified")); |
|
| 1174 | - } else { |
|
| 1175 | - throw new \Exception($l->t("App '%s' could not be installed!", $appName)); |
|
| 1176 | - } |
|
| 1177 | - } |
|
| 1178 | - |
|
| 1179 | - return $app; |
|
| 1180 | - } |
|
| 1181 | - |
|
| 1182 | - /** |
|
| 1183 | - * update the database for the app and call the update script |
|
| 1184 | - * |
|
| 1185 | - * @param string $appId |
|
| 1186 | - * @return bool |
|
| 1187 | - */ |
|
| 1188 | - public static function updateApp($appId) { |
|
| 1189 | - $appPath = self::getAppPath($appId); |
|
| 1190 | - if($appPath === false) { |
|
| 1191 | - return false; |
|
| 1192 | - } |
|
| 1193 | - $appData = self::getAppInfo($appId); |
|
| 1194 | - self::executeRepairSteps($appId, $appData['repair-steps']['pre-migration']); |
|
| 1195 | - if (file_exists($appPath . '/appinfo/database.xml')) { |
|
| 1196 | - OC_DB::updateDbFromStructure($appPath . '/appinfo/database.xml'); |
|
| 1197 | - } |
|
| 1198 | - self::executeRepairSteps($appId, $appData['repair-steps']['post-migration']); |
|
| 1199 | - self::setupLiveMigrations($appId, $appData['repair-steps']['live-migration']); |
|
| 1200 | - unset(self::$appVersion[$appId]); |
|
| 1201 | - // run upgrade code |
|
| 1202 | - if (file_exists($appPath . '/appinfo/update.php')) { |
|
| 1203 | - self::loadApp($appId); |
|
| 1204 | - include $appPath . '/appinfo/update.php'; |
|
| 1205 | - } |
|
| 1206 | - self::setupBackgroundJobs($appData['background-jobs']); |
|
| 1207 | - if(isset($appData['settings']) && is_array($appData['settings'])) { |
|
| 1208 | - $appPath = self::getAppPath($appId); |
|
| 1209 | - self::registerAutoloading($appId, $appPath); |
|
| 1210 | - \OC::$server->getSettingsManager()->setupSettings($appData['settings']); |
|
| 1211 | - } |
|
| 1212 | - |
|
| 1213 | - //set remote/public handlers |
|
| 1214 | - if (array_key_exists('ocsid', $appData)) { |
|
| 1215 | - \OC::$server->getConfig()->setAppValue($appId, 'ocsid', $appData['ocsid']); |
|
| 1216 | - } elseif(\OC::$server->getConfig()->getAppValue($appId, 'ocsid', null) !== null) { |
|
| 1217 | - \OC::$server->getConfig()->deleteAppValue($appId, 'ocsid'); |
|
| 1218 | - } |
|
| 1219 | - foreach ($appData['remote'] as $name => $path) { |
|
| 1220 | - \OC::$server->getConfig()->setAppValue('core', 'remote_' . $name, $appId . '/' . $path); |
|
| 1221 | - } |
|
| 1222 | - foreach ($appData['public'] as $name => $path) { |
|
| 1223 | - \OC::$server->getConfig()->setAppValue('core', 'public_' . $name, $appId . '/' . $path); |
|
| 1224 | - } |
|
| 1225 | - |
|
| 1226 | - self::setAppTypes($appId); |
|
| 1227 | - |
|
| 1228 | - $version = \OC_App::getAppVersion($appId); |
|
| 1229 | - \OC::$server->getAppConfig()->setValue($appId, 'installed_version', $version); |
|
| 1230 | - |
|
| 1231 | - \OC::$server->getEventDispatcher()->dispatch(ManagerEvent::EVENT_APP_UPDATE, new ManagerEvent( |
|
| 1232 | - ManagerEvent::EVENT_APP_UPDATE, $appId |
|
| 1233 | - )); |
|
| 1234 | - |
|
| 1235 | - return true; |
|
| 1236 | - } |
|
| 1237 | - |
|
| 1238 | - /** |
|
| 1239 | - * @param string $appId |
|
| 1240 | - * @param string[] $steps |
|
| 1241 | - * @throws \OC\NeedsUpdateException |
|
| 1242 | - */ |
|
| 1243 | - public static function executeRepairSteps($appId, array $steps) { |
|
| 1244 | - if (empty($steps)) { |
|
| 1245 | - return; |
|
| 1246 | - } |
|
| 1247 | - // load the app |
|
| 1248 | - self::loadApp($appId); |
|
| 1249 | - |
|
| 1250 | - $dispatcher = OC::$server->getEventDispatcher(); |
|
| 1251 | - |
|
| 1252 | - // load the steps |
|
| 1253 | - $r = new Repair([], $dispatcher); |
|
| 1254 | - foreach ($steps as $step) { |
|
| 1255 | - try { |
|
| 1256 | - $r->addStep($step); |
|
| 1257 | - } catch (Exception $ex) { |
|
| 1258 | - $r->emit('\OC\Repair', 'error', [$ex->getMessage()]); |
|
| 1259 | - \OC::$server->getLogger()->logException($ex); |
|
| 1260 | - } |
|
| 1261 | - } |
|
| 1262 | - // run the steps |
|
| 1263 | - $r->run(); |
|
| 1264 | - } |
|
| 1265 | - |
|
| 1266 | - public static function setupBackgroundJobs(array $jobs) { |
|
| 1267 | - $queue = \OC::$server->getJobList(); |
|
| 1268 | - foreach ($jobs as $job) { |
|
| 1269 | - $queue->add($job); |
|
| 1270 | - } |
|
| 1271 | - } |
|
| 1272 | - |
|
| 1273 | - /** |
|
| 1274 | - * @param string $appId |
|
| 1275 | - * @param string[] $steps |
|
| 1276 | - */ |
|
| 1277 | - private static function setupLiveMigrations($appId, array $steps) { |
|
| 1278 | - $queue = \OC::$server->getJobList(); |
|
| 1279 | - foreach ($steps as $step) { |
|
| 1280 | - $queue->add('OC\Migration\BackgroundRepair', [ |
|
| 1281 | - 'app' => $appId, |
|
| 1282 | - 'step' => $step]); |
|
| 1283 | - } |
|
| 1284 | - } |
|
| 1285 | - |
|
| 1286 | - /** |
|
| 1287 | - * @param string $appId |
|
| 1288 | - * @return \OC\Files\View|false |
|
| 1289 | - */ |
|
| 1290 | - public static function getStorage($appId) { |
|
| 1291 | - if (OC_App::isEnabled($appId)) { //sanity check |
|
| 1292 | - if (\OC::$server->getUserSession()->isLoggedIn()) { |
|
| 1293 | - $view = new \OC\Files\View('/' . OC_User::getUser()); |
|
| 1294 | - if (!$view->file_exists($appId)) { |
|
| 1295 | - $view->mkdir($appId); |
|
| 1296 | - } |
|
| 1297 | - return new \OC\Files\View('/' . OC_User::getUser() . '/' . $appId); |
|
| 1298 | - } else { |
|
| 1299 | - \OCP\Util::writeLog('core', 'Can\'t get app storage, app ' . $appId . ', user not logged in', \OCP\Util::ERROR); |
|
| 1300 | - return false; |
|
| 1301 | - } |
|
| 1302 | - } else { |
|
| 1303 | - \OCP\Util::writeLog('core', 'Can\'t get app storage, app ' . $appId . ' not enabled', \OCP\Util::ERROR); |
|
| 1304 | - return false; |
|
| 1305 | - } |
|
| 1306 | - } |
|
| 1307 | - |
|
| 1308 | - protected static function findBestL10NOption($options, $lang) { |
|
| 1309 | - $fallback = $similarLangFallback = $englishFallback = false; |
|
| 1310 | - |
|
| 1311 | - $lang = strtolower($lang); |
|
| 1312 | - $similarLang = $lang; |
|
| 1313 | - if (strpos($similarLang, '_')) { |
|
| 1314 | - // For "de_DE" we want to find "de" and the other way around |
|
| 1315 | - $similarLang = substr($lang, 0, strpos($lang, '_')); |
|
| 1316 | - } |
|
| 1317 | - |
|
| 1318 | - foreach ($options as $option) { |
|
| 1319 | - if (is_array($option)) { |
|
| 1320 | - if ($fallback === false) { |
|
| 1321 | - $fallback = $option['@value']; |
|
| 1322 | - } |
|
| 1323 | - |
|
| 1324 | - if (!isset($option['@attributes']['lang'])) { |
|
| 1325 | - continue; |
|
| 1326 | - } |
|
| 1327 | - |
|
| 1328 | - $attributeLang = strtolower($option['@attributes']['lang']); |
|
| 1329 | - if ($attributeLang === $lang) { |
|
| 1330 | - return $option['@value']; |
|
| 1331 | - } |
|
| 1332 | - |
|
| 1333 | - if ($attributeLang === $similarLang) { |
|
| 1334 | - $similarLangFallback = $option['@value']; |
|
| 1335 | - } else if (strpos($attributeLang, $similarLang . '_') === 0) { |
|
| 1336 | - if ($similarLangFallback === false) { |
|
| 1337 | - $similarLangFallback = $option['@value']; |
|
| 1338 | - } |
|
| 1339 | - } |
|
| 1340 | - } else { |
|
| 1341 | - $englishFallback = $option; |
|
| 1342 | - } |
|
| 1343 | - } |
|
| 1344 | - |
|
| 1345 | - if ($similarLangFallback !== false) { |
|
| 1346 | - return $similarLangFallback; |
|
| 1347 | - } else if ($englishFallback !== false) { |
|
| 1348 | - return $englishFallback; |
|
| 1349 | - } |
|
| 1350 | - return (string) $fallback; |
|
| 1351 | - } |
|
| 1352 | - |
|
| 1353 | - /** |
|
| 1354 | - * parses the app data array and enhanced the 'description' value |
|
| 1355 | - * |
|
| 1356 | - * @param array $data the app data |
|
| 1357 | - * @param string $lang |
|
| 1358 | - * @return array improved app data |
|
| 1359 | - */ |
|
| 1360 | - public static function parseAppInfo(array $data, $lang = null) { |
|
| 1361 | - |
|
| 1362 | - if ($lang && isset($data['name']) && is_array($data['name'])) { |
|
| 1363 | - $data['name'] = self::findBestL10NOption($data['name'], $lang); |
|
| 1364 | - } |
|
| 1365 | - if ($lang && isset($data['summary']) && is_array($data['summary'])) { |
|
| 1366 | - $data['summary'] = self::findBestL10NOption($data['summary'], $lang); |
|
| 1367 | - } |
|
| 1368 | - if ($lang && isset($data['description']) && is_array($data['description'])) { |
|
| 1369 | - $data['description'] = trim(self::findBestL10NOption($data['description'], $lang)); |
|
| 1370 | - } else if (isset($data['description']) && is_string($data['description'])) { |
|
| 1371 | - $data['description'] = trim($data['description']); |
|
| 1372 | - } else { |
|
| 1373 | - $data['description'] = ''; |
|
| 1374 | - } |
|
| 1375 | - |
|
| 1376 | - return $data; |
|
| 1377 | - } |
|
| 1378 | - |
|
| 1379 | - /** |
|
| 1380 | - * @param \OCP\IConfig $config |
|
| 1381 | - * @param \OCP\IL10N $l |
|
| 1382 | - * @param array $info |
|
| 1383 | - * @throws \Exception |
|
| 1384 | - */ |
|
| 1385 | - protected static function checkAppDependencies($config, $l, $info) { |
|
| 1386 | - $dependencyAnalyzer = new DependencyAnalyzer(new Platform($config), $l); |
|
| 1387 | - $missing = $dependencyAnalyzer->analyze($info); |
|
| 1388 | - if (!empty($missing)) { |
|
| 1389 | - $missingMsg = join(PHP_EOL, $missing); |
|
| 1390 | - throw new \Exception( |
|
| 1391 | - $l->t('App "%s" cannot be installed because the following dependencies are not fulfilled: %s', |
|
| 1392 | - [$info['name'], $missingMsg] |
|
| 1393 | - ) |
|
| 1394 | - ); |
|
| 1395 | - } |
|
| 1396 | - } |
|
| 64 | + static private $appVersion = []; |
|
| 65 | + static private $adminForms = array(); |
|
| 66 | + static private $personalForms = array(); |
|
| 67 | + static private $appInfo = array(); |
|
| 68 | + static private $appTypes = array(); |
|
| 69 | + static private $loadedApps = array(); |
|
| 70 | + static private $altLogin = array(); |
|
| 71 | + static private $alreadyRegistered = []; |
|
| 72 | + const officialApp = 200; |
|
| 73 | + |
|
| 74 | + /** |
|
| 75 | + * clean the appId |
|
| 76 | + * |
|
| 77 | + * @param string|boolean $app AppId that needs to be cleaned |
|
| 78 | + * @return string |
|
| 79 | + */ |
|
| 80 | + public static function cleanAppId($app) { |
|
| 81 | + return str_replace(array('\0', '/', '\\', '..'), '', $app); |
|
| 82 | + } |
|
| 83 | + |
|
| 84 | + /** |
|
| 85 | + * Check if an app is loaded |
|
| 86 | + * |
|
| 87 | + * @param string $app |
|
| 88 | + * @return bool |
|
| 89 | + */ |
|
| 90 | + public static function isAppLoaded($app) { |
|
| 91 | + return in_array($app, self::$loadedApps, true); |
|
| 92 | + } |
|
| 93 | + |
|
| 94 | + /** |
|
| 95 | + * loads all apps |
|
| 96 | + * |
|
| 97 | + * @param string[] | string | null $types |
|
| 98 | + * @return bool |
|
| 99 | + * |
|
| 100 | + * This function walks through the ownCloud directory and loads all apps |
|
| 101 | + * it can find. A directory contains an app if the file /appinfo/info.xml |
|
| 102 | + * exists. |
|
| 103 | + * |
|
| 104 | + * if $types is set, only apps of those types will be loaded |
|
| 105 | + */ |
|
| 106 | + public static function loadApps($types = null) { |
|
| 107 | + if (\OC::$server->getSystemConfig()->getValue('maintenance', false)) { |
|
| 108 | + return false; |
|
| 109 | + } |
|
| 110 | + // Load the enabled apps here |
|
| 111 | + $apps = self::getEnabledApps(); |
|
| 112 | + |
|
| 113 | + // Add each apps' folder as allowed class path |
|
| 114 | + foreach($apps as $app) { |
|
| 115 | + $path = self::getAppPath($app); |
|
| 116 | + if($path !== false) { |
|
| 117 | + self::registerAutoloading($app, $path); |
|
| 118 | + } |
|
| 119 | + } |
|
| 120 | + |
|
| 121 | + // prevent app.php from printing output |
|
| 122 | + ob_start(); |
|
| 123 | + foreach ($apps as $app) { |
|
| 124 | + if ((is_null($types) or self::isType($app, $types)) && !in_array($app, self::$loadedApps)) { |
|
| 125 | + self::loadApp($app); |
|
| 126 | + } |
|
| 127 | + } |
|
| 128 | + ob_end_clean(); |
|
| 129 | + |
|
| 130 | + return true; |
|
| 131 | + } |
|
| 132 | + |
|
| 133 | + /** |
|
| 134 | + * load a single app |
|
| 135 | + * |
|
| 136 | + * @param string $app |
|
| 137 | + */ |
|
| 138 | + public static function loadApp($app) { |
|
| 139 | + self::$loadedApps[] = $app; |
|
| 140 | + $appPath = self::getAppPath($app); |
|
| 141 | + if($appPath === false) { |
|
| 142 | + return; |
|
| 143 | + } |
|
| 144 | + |
|
| 145 | + // in case someone calls loadApp() directly |
|
| 146 | + self::registerAutoloading($app, $appPath); |
|
| 147 | + |
|
| 148 | + if (is_file($appPath . '/appinfo/app.php')) { |
|
| 149 | + \OC::$server->getEventLogger()->start('load_app_' . $app, 'Load app: ' . $app); |
|
| 150 | + self::requireAppFile($app); |
|
| 151 | + if (self::isType($app, array('authentication'))) { |
|
| 152 | + // since authentication apps affect the "is app enabled for group" check, |
|
| 153 | + // the enabled apps cache needs to be cleared to make sure that the |
|
| 154 | + // next time getEnableApps() is called it will also include apps that were |
|
| 155 | + // enabled for groups |
|
| 156 | + self::$enabledAppsCache = array(); |
|
| 157 | + } |
|
| 158 | + \OC::$server->getEventLogger()->end('load_app_' . $app); |
|
| 159 | + } |
|
| 160 | + |
|
| 161 | + $info = self::getAppInfo($app); |
|
| 162 | + if (!empty($info['activity']['filters'])) { |
|
| 163 | + foreach ($info['activity']['filters'] as $filter) { |
|
| 164 | + \OC::$server->getActivityManager()->registerFilter($filter); |
|
| 165 | + } |
|
| 166 | + } |
|
| 167 | + if (!empty($info['activity']['settings'])) { |
|
| 168 | + foreach ($info['activity']['settings'] as $setting) { |
|
| 169 | + \OC::$server->getActivityManager()->registerSetting($setting); |
|
| 170 | + } |
|
| 171 | + } |
|
| 172 | + if (!empty($info['activity']['providers'])) { |
|
| 173 | + foreach ($info['activity']['providers'] as $provider) { |
|
| 174 | + \OC::$server->getActivityManager()->registerProvider($provider); |
|
| 175 | + } |
|
| 176 | + } |
|
| 177 | + } |
|
| 178 | + |
|
| 179 | + /** |
|
| 180 | + * @internal |
|
| 181 | + * @param string $app |
|
| 182 | + * @param string $path |
|
| 183 | + */ |
|
| 184 | + public static function registerAutoloading($app, $path) { |
|
| 185 | + $key = $app . '-' . $path; |
|
| 186 | + if(isset(self::$alreadyRegistered[$key])) { |
|
| 187 | + return; |
|
| 188 | + } |
|
| 189 | + self::$alreadyRegistered[$key] = true; |
|
| 190 | + // Register on PSR-4 composer autoloader |
|
| 191 | + $appNamespace = \OC\AppFramework\App::buildAppNamespace($app); |
|
| 192 | + \OC::$composerAutoloader->addPsr4($appNamespace . '\\', $path . '/lib/', true); |
|
| 193 | + if (defined('PHPUNIT_RUN') || defined('CLI_TEST_RUN')) { |
|
| 194 | + \OC::$composerAutoloader->addPsr4($appNamespace . '\\Tests\\', $path . '/tests/', true); |
|
| 195 | + } |
|
| 196 | + |
|
| 197 | + // Register on legacy autoloader |
|
| 198 | + \OC::$loader->addValidRoot($path); |
|
| 199 | + } |
|
| 200 | + |
|
| 201 | + /** |
|
| 202 | + * Load app.php from the given app |
|
| 203 | + * |
|
| 204 | + * @param string $app app name |
|
| 205 | + */ |
|
| 206 | + private static function requireAppFile($app) { |
|
| 207 | + try { |
|
| 208 | + // encapsulated here to avoid variable scope conflicts |
|
| 209 | + require_once $app . '/appinfo/app.php'; |
|
| 210 | + } catch (Error $ex) { |
|
| 211 | + \OC::$server->getLogger()->logException($ex); |
|
| 212 | + $blacklist = \OC::$server->getAppManager()->getAlwaysEnabledApps(); |
|
| 213 | + if (!in_array($app, $blacklist)) { |
|
| 214 | + self::disable($app); |
|
| 215 | + } |
|
| 216 | + } |
|
| 217 | + } |
|
| 218 | + |
|
| 219 | + /** |
|
| 220 | + * check if an app is of a specific type |
|
| 221 | + * |
|
| 222 | + * @param string $app |
|
| 223 | + * @param string|array $types |
|
| 224 | + * @return bool |
|
| 225 | + */ |
|
| 226 | + public static function isType($app, $types) { |
|
| 227 | + if (is_string($types)) { |
|
| 228 | + $types = array($types); |
|
| 229 | + } |
|
| 230 | + $appTypes = self::getAppTypes($app); |
|
| 231 | + foreach ($types as $type) { |
|
| 232 | + if (array_search($type, $appTypes) !== false) { |
|
| 233 | + return true; |
|
| 234 | + } |
|
| 235 | + } |
|
| 236 | + return false; |
|
| 237 | + } |
|
| 238 | + |
|
| 239 | + /** |
|
| 240 | + * get the types of an app |
|
| 241 | + * |
|
| 242 | + * @param string $app |
|
| 243 | + * @return array |
|
| 244 | + */ |
|
| 245 | + private static function getAppTypes($app) { |
|
| 246 | + //load the cache |
|
| 247 | + if (count(self::$appTypes) == 0) { |
|
| 248 | + self::$appTypes = \OC::$server->getAppConfig()->getValues(false, 'types'); |
|
| 249 | + } |
|
| 250 | + |
|
| 251 | + if (isset(self::$appTypes[$app])) { |
|
| 252 | + return explode(',', self::$appTypes[$app]); |
|
| 253 | + } else { |
|
| 254 | + return array(); |
|
| 255 | + } |
|
| 256 | + } |
|
| 257 | + |
|
| 258 | + /** |
|
| 259 | + * read app types from info.xml and cache them in the database |
|
| 260 | + */ |
|
| 261 | + public static function setAppTypes($app) { |
|
| 262 | + $appData = self::getAppInfo($app); |
|
| 263 | + if(!is_array($appData)) { |
|
| 264 | + return; |
|
| 265 | + } |
|
| 266 | + |
|
| 267 | + if (isset($appData['types'])) { |
|
| 268 | + $appTypes = implode(',', $appData['types']); |
|
| 269 | + } else { |
|
| 270 | + $appTypes = ''; |
|
| 271 | + $appData['types'] = []; |
|
| 272 | + } |
|
| 273 | + |
|
| 274 | + \OC::$server->getAppConfig()->setValue($app, 'types', $appTypes); |
|
| 275 | + |
|
| 276 | + if (\OC::$server->getAppManager()->hasProtectedAppType($appData['types'])) { |
|
| 277 | + $enabled = \OC::$server->getAppConfig()->getValue($app, 'enabled', 'yes'); |
|
| 278 | + if ($enabled !== 'yes' && $enabled !== 'no') { |
|
| 279 | + \OC::$server->getAppConfig()->setValue($app, 'enabled', 'yes'); |
|
| 280 | + } |
|
| 281 | + } |
|
| 282 | + } |
|
| 283 | + |
|
| 284 | + /** |
|
| 285 | + * check if app is shipped |
|
| 286 | + * |
|
| 287 | + * @param string $appId the id of the app to check |
|
| 288 | + * @return bool |
|
| 289 | + * |
|
| 290 | + * Check if an app that is installed is a shipped app or installed from the appstore. |
|
| 291 | + */ |
|
| 292 | + public static function isShipped($appId) { |
|
| 293 | + return \OC::$server->getAppManager()->isShipped($appId); |
|
| 294 | + } |
|
| 295 | + |
|
| 296 | + /** |
|
| 297 | + * get all enabled apps |
|
| 298 | + */ |
|
| 299 | + protected static $enabledAppsCache = array(); |
|
| 300 | + |
|
| 301 | + /** |
|
| 302 | + * Returns apps enabled for the current user. |
|
| 303 | + * |
|
| 304 | + * @param bool $forceRefresh whether to refresh the cache |
|
| 305 | + * @param bool $all whether to return apps for all users, not only the |
|
| 306 | + * currently logged in one |
|
| 307 | + * @return string[] |
|
| 308 | + */ |
|
| 309 | + public static function getEnabledApps($forceRefresh = false, $all = false) { |
|
| 310 | + if (!\OC::$server->getSystemConfig()->getValue('installed', false)) { |
|
| 311 | + return array(); |
|
| 312 | + } |
|
| 313 | + // in incognito mode or when logged out, $user will be false, |
|
| 314 | + // which is also the case during an upgrade |
|
| 315 | + $appManager = \OC::$server->getAppManager(); |
|
| 316 | + if ($all) { |
|
| 317 | + $user = null; |
|
| 318 | + } else { |
|
| 319 | + $user = \OC::$server->getUserSession()->getUser(); |
|
| 320 | + } |
|
| 321 | + |
|
| 322 | + if (is_null($user)) { |
|
| 323 | + $apps = $appManager->getInstalledApps(); |
|
| 324 | + } else { |
|
| 325 | + $apps = $appManager->getEnabledAppsForUser($user); |
|
| 326 | + } |
|
| 327 | + $apps = array_filter($apps, function ($app) { |
|
| 328 | + return $app !== 'files';//we add this manually |
|
| 329 | + }); |
|
| 330 | + sort($apps); |
|
| 331 | + array_unshift($apps, 'files'); |
|
| 332 | + return $apps; |
|
| 333 | + } |
|
| 334 | + |
|
| 335 | + /** |
|
| 336 | + * checks whether or not an app is enabled |
|
| 337 | + * |
|
| 338 | + * @param string $app app |
|
| 339 | + * @return bool |
|
| 340 | + * |
|
| 341 | + * This function checks whether or not an app is enabled. |
|
| 342 | + */ |
|
| 343 | + public static function isEnabled($app) { |
|
| 344 | + return \OC::$server->getAppManager()->isEnabledForUser($app); |
|
| 345 | + } |
|
| 346 | + |
|
| 347 | + /** |
|
| 348 | + * enables an app |
|
| 349 | + * |
|
| 350 | + * @param string $appId |
|
| 351 | + * @param array $groups (optional) when set, only these groups will have access to the app |
|
| 352 | + * @throws \Exception |
|
| 353 | + * @return void |
|
| 354 | + * |
|
| 355 | + * This function set an app as enabled in appconfig. |
|
| 356 | + */ |
|
| 357 | + public function enable($appId, |
|
| 358 | + $groups = null) { |
|
| 359 | + self::$enabledAppsCache = []; // flush |
|
| 360 | + $l = \OC::$server->getL10N('core'); |
|
| 361 | + $config = \OC::$server->getConfig(); |
|
| 362 | + |
|
| 363 | + // Check if app is already downloaded |
|
| 364 | + $installer = new Installer( |
|
| 365 | + \OC::$server->getAppFetcher(), |
|
| 366 | + \OC::$server->getHTTPClientService(), |
|
| 367 | + \OC::$server->getTempManager(), |
|
| 368 | + \OC::$server->getLogger() |
|
| 369 | + ); |
|
| 370 | + $isDownloaded = $installer->isDownloaded($appId); |
|
| 371 | + |
|
| 372 | + if(!$isDownloaded) { |
|
| 373 | + $installer->downloadApp($appId); |
|
| 374 | + } |
|
| 375 | + |
|
| 376 | + if (!Installer::isInstalled($appId)) { |
|
| 377 | + $appId = self::installApp( |
|
| 378 | + $appId, |
|
| 379 | + $config, |
|
| 380 | + $l |
|
| 381 | + ); |
|
| 382 | + $appPath = self::getAppPath($appId); |
|
| 383 | + self::registerAutoloading($appId, $appPath); |
|
| 384 | + $installer->installApp($appId); |
|
| 385 | + } else { |
|
| 386 | + // check for required dependencies |
|
| 387 | + $info = self::getAppInfo($appId); |
|
| 388 | + self::checkAppDependencies($config, $l, $info); |
|
| 389 | + $appPath = self::getAppPath($appId); |
|
| 390 | + self::registerAutoloading($appId, $appPath); |
|
| 391 | + $installer->installApp($appId); |
|
| 392 | + } |
|
| 393 | + |
|
| 394 | + $appManager = \OC::$server->getAppManager(); |
|
| 395 | + if (!is_null($groups)) { |
|
| 396 | + $groupManager = \OC::$server->getGroupManager(); |
|
| 397 | + $groupsList = []; |
|
| 398 | + foreach ($groups as $group) { |
|
| 399 | + $groupItem = $groupManager->get($group); |
|
| 400 | + if ($groupItem instanceof \OCP\IGroup) { |
|
| 401 | + $groupsList[] = $groupManager->get($group); |
|
| 402 | + } |
|
| 403 | + } |
|
| 404 | + $appManager->enableAppForGroups($appId, $groupsList); |
|
| 405 | + } else { |
|
| 406 | + $appManager->enableApp($appId); |
|
| 407 | + } |
|
| 408 | + |
|
| 409 | + $info = self::getAppInfo($appId); |
|
| 410 | + if(isset($info['settings']) && is_array($info['settings'])) { |
|
| 411 | + $appPath = self::getAppPath($appId); |
|
| 412 | + self::registerAutoloading($appId, $appPath); |
|
| 413 | + \OC::$server->getSettingsManager()->setupSettings($info['settings']); |
|
| 414 | + } |
|
| 415 | + } |
|
| 416 | + |
|
| 417 | + /** |
|
| 418 | + * @param string $app |
|
| 419 | + * @return bool |
|
| 420 | + */ |
|
| 421 | + public static function removeApp($app) { |
|
| 422 | + if (self::isShipped($app)) { |
|
| 423 | + return false; |
|
| 424 | + } |
|
| 425 | + |
|
| 426 | + $installer = new Installer( |
|
| 427 | + \OC::$server->getAppFetcher(), |
|
| 428 | + \OC::$server->getHTTPClientService(), |
|
| 429 | + \OC::$server->getTempManager(), |
|
| 430 | + \OC::$server->getLogger() |
|
| 431 | + ); |
|
| 432 | + return $installer->removeApp($app); |
|
| 433 | + } |
|
| 434 | + |
|
| 435 | + /** |
|
| 436 | + * This function set an app as disabled in appconfig. |
|
| 437 | + * |
|
| 438 | + * @param string $app app |
|
| 439 | + * @throws Exception |
|
| 440 | + */ |
|
| 441 | + public static function disable($app) { |
|
| 442 | + // flush |
|
| 443 | + self::$enabledAppsCache = array(); |
|
| 444 | + |
|
| 445 | + // run uninstall steps |
|
| 446 | + $appData = OC_App::getAppInfo($app); |
|
| 447 | + if (!is_null($appData)) { |
|
| 448 | + OC_App::executeRepairSteps($app, $appData['repair-steps']['uninstall']); |
|
| 449 | + } |
|
| 450 | + |
|
| 451 | + // emit disable hook - needed anymore ? |
|
| 452 | + \OC_Hook::emit('OC_App', 'pre_disable', array('app' => $app)); |
|
| 453 | + |
|
| 454 | + // finally disable it |
|
| 455 | + $appManager = \OC::$server->getAppManager(); |
|
| 456 | + $appManager->disableApp($app); |
|
| 457 | + } |
|
| 458 | + |
|
| 459 | + /** |
|
| 460 | + * Returns the Settings Navigation |
|
| 461 | + * |
|
| 462 | + * @return string[] |
|
| 463 | + * |
|
| 464 | + * This function returns an array containing all settings pages added. The |
|
| 465 | + * entries are sorted by the key 'order' ascending. |
|
| 466 | + */ |
|
| 467 | + public static function getSettingsNavigation() { |
|
| 468 | + $l = \OC::$server->getL10N('lib'); |
|
| 469 | + $urlGenerator = \OC::$server->getURLGenerator(); |
|
| 470 | + |
|
| 471 | + $settings = array(); |
|
| 472 | + // by default, settings only contain the help menu |
|
| 473 | + if (\OC::$server->getSystemConfig()->getValue('knowledgebaseenabled', true)) { |
|
| 474 | + $settings = array( |
|
| 475 | + array( |
|
| 476 | + "id" => "help", |
|
| 477 | + "order" => 4, |
|
| 478 | + "href" => $urlGenerator->linkToRoute('settings_help'), |
|
| 479 | + "name" => $l->t("Help"), |
|
| 480 | + "icon" => $urlGenerator->imagePath("settings", "help.svg") |
|
| 481 | + ) |
|
| 482 | + ); |
|
| 483 | + } |
|
| 484 | + |
|
| 485 | + // if the user is logged-in |
|
| 486 | + if (\OC::$server->getUserSession()->isLoggedIn()) { |
|
| 487 | + // personal menu |
|
| 488 | + $settings[] = array( |
|
| 489 | + "id" => "personal", |
|
| 490 | + "order" => 1, |
|
| 491 | + "href" => $urlGenerator->linkToRoute('settings_personal'), |
|
| 492 | + "name" => $l->t("Personal"), |
|
| 493 | + "icon" => $urlGenerator->imagePath("settings", "personal.svg") |
|
| 494 | + ); |
|
| 495 | + |
|
| 496 | + //SubAdmins are also allowed to access user management |
|
| 497 | + $userObject = \OC::$server->getUserSession()->getUser(); |
|
| 498 | + $isSubAdmin = false; |
|
| 499 | + if($userObject !== null) { |
|
| 500 | + $isSubAdmin = \OC::$server->getGroupManager()->getSubAdmin()->isSubAdmin($userObject); |
|
| 501 | + } |
|
| 502 | + if ($isSubAdmin) { |
|
| 503 | + // admin users menu |
|
| 504 | + $settings[] = array( |
|
| 505 | + "id" => "core_users", |
|
| 506 | + "order" => 3, |
|
| 507 | + "href" => $urlGenerator->linkToRoute('settings_users'), |
|
| 508 | + "name" => $l->t("Users"), |
|
| 509 | + "icon" => $urlGenerator->imagePath("settings", "users.svg") |
|
| 510 | + ); |
|
| 511 | + } |
|
| 512 | + |
|
| 513 | + // if the user is an admin |
|
| 514 | + if (OC_User::isAdminUser(OC_User::getUser())) { |
|
| 515 | + // admin settings |
|
| 516 | + $settings[] = array( |
|
| 517 | + "id" => "admin", |
|
| 518 | + "order" => 2, |
|
| 519 | + "href" => $urlGenerator->linkToRoute('settings.AdminSettings.index'), |
|
| 520 | + "name" => $l->t("Admin"), |
|
| 521 | + "icon" => $urlGenerator->imagePath("settings", "admin.svg") |
|
| 522 | + ); |
|
| 523 | + } |
|
| 524 | + } |
|
| 525 | + |
|
| 526 | + $navigation = self::proceedNavigation($settings); |
|
| 527 | + return $navigation; |
|
| 528 | + } |
|
| 529 | + |
|
| 530 | + // This is private as well. It simply works, so don't ask for more details |
|
| 531 | + private static function proceedNavigation($list) { |
|
| 532 | + $headerIconCount = 8; |
|
| 533 | + if(OC_User::isAdminUser(OC_User::getUser())) { |
|
| 534 | + $headerIconCount--; |
|
| 535 | + } |
|
| 536 | + usort($list, function($a, $b) { |
|
| 537 | + if (isset($a['order']) && isset($b['order'])) { |
|
| 538 | + return ($a['order'] < $b['order']) ? -1 : 1; |
|
| 539 | + } else if (isset($a['order']) || isset($b['order'])) { |
|
| 540 | + return isset($a['order']) ? -1 : 1; |
|
| 541 | + } else { |
|
| 542 | + return ($a['name'] < $b['name']) ? -1 : 1; |
|
| 543 | + } |
|
| 544 | + }); |
|
| 545 | + |
|
| 546 | + $activeAppIndex = -1; |
|
| 547 | + $activeApp = OC::$server->getNavigationManager()->getActiveEntry(); |
|
| 548 | + foreach ($list as $index => &$navEntry) { |
|
| 549 | + if ($navEntry['id'] == $activeApp) { |
|
| 550 | + $navEntry['active'] = true; |
|
| 551 | + $activeAppIndex = $index; |
|
| 552 | + } else { |
|
| 553 | + $navEntry['active'] = false; |
|
| 554 | + } |
|
| 555 | + } |
|
| 556 | + unset($navEntry); |
|
| 557 | + |
|
| 558 | + if($activeAppIndex > ($headerIconCount-1)) { |
|
| 559 | + $active = $list[$activeAppIndex]; |
|
| 560 | + $lastInHeader = $list[$headerIconCount-1]; |
|
| 561 | + $list[$headerIconCount-1] = $active; |
|
| 562 | + $list[$activeAppIndex] = $lastInHeader; |
|
| 563 | + } |
|
| 564 | + |
|
| 565 | + foreach ($list as $index => &$navEntry) { |
|
| 566 | + $navEntry['showInHeader'] = false; |
|
| 567 | + if($index < $headerIconCount) { |
|
| 568 | + $navEntry['showInHeader'] = true; |
|
| 569 | + } |
|
| 570 | + } |
|
| 571 | + |
|
| 572 | + |
|
| 573 | + |
|
| 574 | + return $list; |
|
| 575 | + } |
|
| 576 | + |
|
| 577 | + public static function proceedAppNavigation($entries) { |
|
| 578 | + $headerIconCount = 8; |
|
| 579 | + if(OC_User::isAdminUser(OC_User::getUser())) { |
|
| 580 | + $headerIconCount--; |
|
| 581 | + } |
|
| 582 | + $activeAppIndex = -1; |
|
| 583 | + $list = self::proceedNavigation($entries); |
|
| 584 | + |
|
| 585 | + $activeApp = OC::$server->getNavigationManager()->getActiveEntry(); |
|
| 586 | + foreach ($list as $index => &$navEntry) { |
|
| 587 | + if ($navEntry['id'] == $activeApp) { |
|
| 588 | + $navEntry['active'] = true; |
|
| 589 | + $activeAppIndex = $index; |
|
| 590 | + } else { |
|
| 591 | + $navEntry['active'] = false; |
|
| 592 | + } |
|
| 593 | + } |
|
| 594 | + // move active item to last position |
|
| 595 | + if($activeAppIndex > ($headerIconCount-1)) { |
|
| 596 | + $active = $list[$activeAppIndex]; |
|
| 597 | + $lastInHeader = $list[$headerIconCount-1]; |
|
| 598 | + $list[$headerIconCount-1] = $active; |
|
| 599 | + $list[$activeAppIndex] = $lastInHeader; |
|
| 600 | + } |
|
| 601 | + $list = array_slice($list, 0, $headerIconCount); |
|
| 602 | + |
|
| 603 | + return $list; |
|
| 604 | + } |
|
| 605 | + |
|
| 606 | + /** |
|
| 607 | + * Get the path where to install apps |
|
| 608 | + * |
|
| 609 | + * @return string|false |
|
| 610 | + */ |
|
| 611 | + public static function getInstallPath() { |
|
| 612 | + if (\OC::$server->getSystemConfig()->getValue('appstoreenabled', true) == false) { |
|
| 613 | + return false; |
|
| 614 | + } |
|
| 615 | + |
|
| 616 | + foreach (OC::$APPSROOTS as $dir) { |
|
| 617 | + if (isset($dir['writable']) && $dir['writable'] === true) { |
|
| 618 | + return $dir['path']; |
|
| 619 | + } |
|
| 620 | + } |
|
| 621 | + |
|
| 622 | + \OCP\Util::writeLog('core', 'No application directories are marked as writable.', \OCP\Util::ERROR); |
|
| 623 | + return null; |
|
| 624 | + } |
|
| 625 | + |
|
| 626 | + |
|
| 627 | + /** |
|
| 628 | + * search for an app in all app-directories |
|
| 629 | + * |
|
| 630 | + * @param string $appId |
|
| 631 | + * @return false|string |
|
| 632 | + */ |
|
| 633 | + public static function findAppInDirectories($appId) { |
|
| 634 | + $sanitizedAppId = self::cleanAppId($appId); |
|
| 635 | + if($sanitizedAppId !== $appId) { |
|
| 636 | + return false; |
|
| 637 | + } |
|
| 638 | + static $app_dir = array(); |
|
| 639 | + |
|
| 640 | + if (isset($app_dir[$appId])) { |
|
| 641 | + return $app_dir[$appId]; |
|
| 642 | + } |
|
| 643 | + |
|
| 644 | + $possibleApps = array(); |
|
| 645 | + foreach (OC::$APPSROOTS as $dir) { |
|
| 646 | + if (file_exists($dir['path'] . '/' . $appId)) { |
|
| 647 | + $possibleApps[] = $dir; |
|
| 648 | + } |
|
| 649 | + } |
|
| 650 | + |
|
| 651 | + if (empty($possibleApps)) { |
|
| 652 | + return false; |
|
| 653 | + } elseif (count($possibleApps) === 1) { |
|
| 654 | + $dir = array_shift($possibleApps); |
|
| 655 | + $app_dir[$appId] = $dir; |
|
| 656 | + return $dir; |
|
| 657 | + } else { |
|
| 658 | + $versionToLoad = array(); |
|
| 659 | + foreach ($possibleApps as $possibleApp) { |
|
| 660 | + $version = self::getAppVersionByPath($possibleApp['path']); |
|
| 661 | + if (empty($versionToLoad) || version_compare($version, $versionToLoad['version'], '>')) { |
|
| 662 | + $versionToLoad = array( |
|
| 663 | + 'dir' => $possibleApp, |
|
| 664 | + 'version' => $version, |
|
| 665 | + ); |
|
| 666 | + } |
|
| 667 | + } |
|
| 668 | + $app_dir[$appId] = $versionToLoad['dir']; |
|
| 669 | + return $versionToLoad['dir']; |
|
| 670 | + //TODO - write test |
|
| 671 | + } |
|
| 672 | + } |
|
| 673 | + |
|
| 674 | + /** |
|
| 675 | + * Get the directory for the given app. |
|
| 676 | + * If the app is defined in multiple directories, the first one is taken. (false if not found) |
|
| 677 | + * |
|
| 678 | + * @param string $appId |
|
| 679 | + * @return string|false |
|
| 680 | + */ |
|
| 681 | + public static function getAppPath($appId) { |
|
| 682 | + if ($appId === null || trim($appId) === '') { |
|
| 683 | + return false; |
|
| 684 | + } |
|
| 685 | + |
|
| 686 | + if (($dir = self::findAppInDirectories($appId)) != false) { |
|
| 687 | + return $dir['path'] . '/' . $appId; |
|
| 688 | + } |
|
| 689 | + return false; |
|
| 690 | + } |
|
| 691 | + |
|
| 692 | + /** |
|
| 693 | + * Get the path for the given app on the access |
|
| 694 | + * If the app is defined in multiple directories, the first one is taken. (false if not found) |
|
| 695 | + * |
|
| 696 | + * @param string $appId |
|
| 697 | + * @return string|false |
|
| 698 | + */ |
|
| 699 | + public static function getAppWebPath($appId) { |
|
| 700 | + if (($dir = self::findAppInDirectories($appId)) != false) { |
|
| 701 | + return OC::$WEBROOT . $dir['url'] . '/' . $appId; |
|
| 702 | + } |
|
| 703 | + return false; |
|
| 704 | + } |
|
| 705 | + |
|
| 706 | + /** |
|
| 707 | + * get the last version of the app from appinfo/info.xml |
|
| 708 | + * |
|
| 709 | + * @param string $appId |
|
| 710 | + * @param bool $useCache |
|
| 711 | + * @return string |
|
| 712 | + */ |
|
| 713 | + public static function getAppVersion($appId, $useCache = true) { |
|
| 714 | + if($useCache && isset(self::$appVersion[$appId])) { |
|
| 715 | + return self::$appVersion[$appId]; |
|
| 716 | + } |
|
| 717 | + |
|
| 718 | + $file = self::getAppPath($appId); |
|
| 719 | + self::$appVersion[$appId] = ($file !== false) ? self::getAppVersionByPath($file) : '0'; |
|
| 720 | + return self::$appVersion[$appId]; |
|
| 721 | + } |
|
| 722 | + |
|
| 723 | + /** |
|
| 724 | + * get app's version based on it's path |
|
| 725 | + * |
|
| 726 | + * @param string $path |
|
| 727 | + * @return string |
|
| 728 | + */ |
|
| 729 | + public static function getAppVersionByPath($path) { |
|
| 730 | + $infoFile = $path . '/appinfo/info.xml'; |
|
| 731 | + $appData = self::getAppInfo($infoFile, true); |
|
| 732 | + return isset($appData['version']) ? $appData['version'] : ''; |
|
| 733 | + } |
|
| 734 | + |
|
| 735 | + |
|
| 736 | + /** |
|
| 737 | + * Read all app metadata from the info.xml file |
|
| 738 | + * |
|
| 739 | + * @param string $appId id of the app or the path of the info.xml file |
|
| 740 | + * @param bool $path |
|
| 741 | + * @param string $lang |
|
| 742 | + * @return array|null |
|
| 743 | + * @note all data is read from info.xml, not just pre-defined fields |
|
| 744 | + */ |
|
| 745 | + public static function getAppInfo($appId, $path = false, $lang = null) { |
|
| 746 | + if ($path) { |
|
| 747 | + $file = $appId; |
|
| 748 | + } else { |
|
| 749 | + if ($lang === null && isset(self::$appInfo[$appId])) { |
|
| 750 | + return self::$appInfo[$appId]; |
|
| 751 | + } |
|
| 752 | + $appPath = self::getAppPath($appId); |
|
| 753 | + if($appPath === false) { |
|
| 754 | + return null; |
|
| 755 | + } |
|
| 756 | + $file = $appPath . '/appinfo/info.xml'; |
|
| 757 | + } |
|
| 758 | + |
|
| 759 | + $parser = new InfoParser(\OC::$server->getMemCacheFactory()->create('core.appinfo')); |
|
| 760 | + $data = $parser->parse($file); |
|
| 761 | + |
|
| 762 | + if (is_array($data)) { |
|
| 763 | + $data = OC_App::parseAppInfo($data, $lang); |
|
| 764 | + } |
|
| 765 | + if(isset($data['ocsid'])) { |
|
| 766 | + $storedId = \OC::$server->getConfig()->getAppValue($appId, 'ocsid'); |
|
| 767 | + if($storedId !== '' && $storedId !== $data['ocsid']) { |
|
| 768 | + $data['ocsid'] = $storedId; |
|
| 769 | + } |
|
| 770 | + } |
|
| 771 | + |
|
| 772 | + if ($lang === null) { |
|
| 773 | + self::$appInfo[$appId] = $data; |
|
| 774 | + } |
|
| 775 | + |
|
| 776 | + return $data; |
|
| 777 | + } |
|
| 778 | + |
|
| 779 | + /** |
|
| 780 | + * Returns the navigation |
|
| 781 | + * |
|
| 782 | + * @return array |
|
| 783 | + * |
|
| 784 | + * This function returns an array containing all entries added. The |
|
| 785 | + * entries are sorted by the key 'order' ascending. Additional to the keys |
|
| 786 | + * given for each app the following keys exist: |
|
| 787 | + * - active: boolean, signals if the user is on this navigation entry |
|
| 788 | + */ |
|
| 789 | + public static function getNavigation() { |
|
| 790 | + $entries = OC::$server->getNavigationManager()->getAll(); |
|
| 791 | + $navigation = self::proceedNavigation($entries); |
|
| 792 | + return $navigation; |
|
| 793 | + } |
|
| 794 | + |
|
| 795 | + /** |
|
| 796 | + * Returns the navigation inside the header bar |
|
| 797 | + * |
|
| 798 | + * @return array |
|
| 799 | + * |
|
| 800 | + * This function returns an array containing all entries added. The |
|
| 801 | + * entries are sorted by the key 'order' ascending. Additional to the keys |
|
| 802 | + * given for each app the following keys exist: |
|
| 803 | + * - active: boolean, signals if the user is on this navigation entry |
|
| 804 | + */ |
|
| 805 | + public static function getHeaderNavigation() { |
|
| 806 | + $entries = OC::$server->getNavigationManager()->getAll(); |
|
| 807 | + $navigation = self::proceedAppNavigation($entries); |
|
| 808 | + return $navigation; |
|
| 809 | + } |
|
| 810 | + |
|
| 811 | + /** |
|
| 812 | + * get the id of loaded app |
|
| 813 | + * |
|
| 814 | + * @return string |
|
| 815 | + */ |
|
| 816 | + public static function getCurrentApp() { |
|
| 817 | + $request = \OC::$server->getRequest(); |
|
| 818 | + $script = substr($request->getScriptName(), strlen(OC::$WEBROOT) + 1); |
|
| 819 | + $topFolder = substr($script, 0, strpos($script, '/')); |
|
| 820 | + if (empty($topFolder)) { |
|
| 821 | + $path_info = $request->getPathInfo(); |
|
| 822 | + if ($path_info) { |
|
| 823 | + $topFolder = substr($path_info, 1, strpos($path_info, '/', 1) - 1); |
|
| 824 | + } |
|
| 825 | + } |
|
| 826 | + if ($topFolder == 'apps') { |
|
| 827 | + $length = strlen($topFolder); |
|
| 828 | + return substr($script, $length + 1, strpos($script, '/', $length + 1) - $length - 1); |
|
| 829 | + } else { |
|
| 830 | + return $topFolder; |
|
| 831 | + } |
|
| 832 | + } |
|
| 833 | + |
|
| 834 | + /** |
|
| 835 | + * @param string $type |
|
| 836 | + * @return array |
|
| 837 | + */ |
|
| 838 | + public static function getForms($type) { |
|
| 839 | + $forms = array(); |
|
| 840 | + switch ($type) { |
|
| 841 | + case 'admin': |
|
| 842 | + $source = self::$adminForms; |
|
| 843 | + break; |
|
| 844 | + case 'personal': |
|
| 845 | + $source = self::$personalForms; |
|
| 846 | + break; |
|
| 847 | + default: |
|
| 848 | + return array(); |
|
| 849 | + } |
|
| 850 | + foreach ($source as $form) { |
|
| 851 | + $forms[] = include $form; |
|
| 852 | + } |
|
| 853 | + return $forms; |
|
| 854 | + } |
|
| 855 | + |
|
| 856 | + /** |
|
| 857 | + * register an admin form to be shown |
|
| 858 | + * |
|
| 859 | + * @param string $app |
|
| 860 | + * @param string $page |
|
| 861 | + */ |
|
| 862 | + public static function registerAdmin($app, $page) { |
|
| 863 | + self::$adminForms[] = $app . '/' . $page . '.php'; |
|
| 864 | + } |
|
| 865 | + |
|
| 866 | + /** |
|
| 867 | + * register a personal form to be shown |
|
| 868 | + * @param string $app |
|
| 869 | + * @param string $page |
|
| 870 | + */ |
|
| 871 | + public static function registerPersonal($app, $page) { |
|
| 872 | + self::$personalForms[] = $app . '/' . $page . '.php'; |
|
| 873 | + } |
|
| 874 | + |
|
| 875 | + /** |
|
| 876 | + * @param array $entry |
|
| 877 | + */ |
|
| 878 | + public static function registerLogIn(array $entry) { |
|
| 879 | + self::$altLogin[] = $entry; |
|
| 880 | + } |
|
| 881 | + |
|
| 882 | + /** |
|
| 883 | + * @return array |
|
| 884 | + */ |
|
| 885 | + public static function getAlternativeLogIns() { |
|
| 886 | + return self::$altLogin; |
|
| 887 | + } |
|
| 888 | + |
|
| 889 | + /** |
|
| 890 | + * get a list of all apps in the apps folder |
|
| 891 | + * |
|
| 892 | + * @return array an array of app names (string IDs) |
|
| 893 | + * @todo: change the name of this method to getInstalledApps, which is more accurate |
|
| 894 | + */ |
|
| 895 | + public static function getAllApps() { |
|
| 896 | + |
|
| 897 | + $apps = array(); |
|
| 898 | + |
|
| 899 | + foreach (OC::$APPSROOTS as $apps_dir) { |
|
| 900 | + if (!is_readable($apps_dir['path'])) { |
|
| 901 | + \OCP\Util::writeLog('core', 'unable to read app folder : ' . $apps_dir['path'], \OCP\Util::WARN); |
|
| 902 | + continue; |
|
| 903 | + } |
|
| 904 | + $dh = opendir($apps_dir['path']); |
|
| 905 | + |
|
| 906 | + if (is_resource($dh)) { |
|
| 907 | + while (($file = readdir($dh)) !== false) { |
|
| 908 | + |
|
| 909 | + if ($file[0] != '.' and is_dir($apps_dir['path'] . '/' . $file) and is_file($apps_dir['path'] . '/' . $file . '/appinfo/info.xml')) { |
|
| 910 | + |
|
| 911 | + $apps[] = $file; |
|
| 912 | + } |
|
| 913 | + } |
|
| 914 | + } |
|
| 915 | + } |
|
| 916 | + |
|
| 917 | + return $apps; |
|
| 918 | + } |
|
| 919 | + |
|
| 920 | + /** |
|
| 921 | + * List all apps, this is used in apps.php |
|
| 922 | + * |
|
| 923 | + * @return array |
|
| 924 | + */ |
|
| 925 | + public function listAllApps() { |
|
| 926 | + $installedApps = OC_App::getAllApps(); |
|
| 927 | + |
|
| 928 | + //we don't want to show configuration for these |
|
| 929 | + $blacklist = \OC::$server->getAppManager()->getAlwaysEnabledApps(); |
|
| 930 | + $appList = array(); |
|
| 931 | + $langCode = \OC::$server->getL10N('core')->getLanguageCode(); |
|
| 932 | + $urlGenerator = \OC::$server->getURLGenerator(); |
|
| 933 | + |
|
| 934 | + foreach ($installedApps as $app) { |
|
| 935 | + if (array_search($app, $blacklist) === false) { |
|
| 936 | + |
|
| 937 | + $info = OC_App::getAppInfo($app, false, $langCode); |
|
| 938 | + if (!is_array($info)) { |
|
| 939 | + \OCP\Util::writeLog('core', 'Could not read app info file for app "' . $app . '"', \OCP\Util::ERROR); |
|
| 940 | + continue; |
|
| 941 | + } |
|
| 942 | + |
|
| 943 | + if (!isset($info['name'])) { |
|
| 944 | + \OCP\Util::writeLog('core', 'App id "' . $app . '" has no name in appinfo', \OCP\Util::ERROR); |
|
| 945 | + continue; |
|
| 946 | + } |
|
| 947 | + |
|
| 948 | + $enabled = \OC::$server->getAppConfig()->getValue($app, 'enabled', 'no'); |
|
| 949 | + $info['groups'] = null; |
|
| 950 | + if ($enabled === 'yes') { |
|
| 951 | + $active = true; |
|
| 952 | + } else if ($enabled === 'no') { |
|
| 953 | + $active = false; |
|
| 954 | + } else { |
|
| 955 | + $active = true; |
|
| 956 | + $info['groups'] = $enabled; |
|
| 957 | + } |
|
| 958 | + |
|
| 959 | + $info['active'] = $active; |
|
| 960 | + |
|
| 961 | + if (self::isShipped($app)) { |
|
| 962 | + $info['internal'] = true; |
|
| 963 | + $info['level'] = self::officialApp; |
|
| 964 | + $info['removable'] = false; |
|
| 965 | + } else { |
|
| 966 | + $info['internal'] = false; |
|
| 967 | + $info['removable'] = true; |
|
| 968 | + } |
|
| 969 | + |
|
| 970 | + $appPath = self::getAppPath($app); |
|
| 971 | + if($appPath !== false) { |
|
| 972 | + $appIcon = $appPath . '/img/' . $app . '.svg'; |
|
| 973 | + if (file_exists($appIcon)) { |
|
| 974 | + $info['preview'] = \OC::$server->getURLGenerator()->imagePath($app, $app . '.svg'); |
|
| 975 | + $info['previewAsIcon'] = true; |
|
| 976 | + } else { |
|
| 977 | + $appIcon = $appPath . '/img/app.svg'; |
|
| 978 | + if (file_exists($appIcon)) { |
|
| 979 | + $info['preview'] = \OC::$server->getURLGenerator()->imagePath($app, 'app.svg'); |
|
| 980 | + $info['previewAsIcon'] = true; |
|
| 981 | + } |
|
| 982 | + } |
|
| 983 | + } |
|
| 984 | + // fix documentation |
|
| 985 | + if (isset($info['documentation']) && is_array($info['documentation'])) { |
|
| 986 | + foreach ($info['documentation'] as $key => $url) { |
|
| 987 | + // If it is not an absolute URL we assume it is a key |
|
| 988 | + // i.e. admin-ldap will get converted to go.php?to=admin-ldap |
|
| 989 | + if (stripos($url, 'https://') !== 0 && stripos($url, 'http://') !== 0) { |
|
| 990 | + $url = $urlGenerator->linkToDocs($url); |
|
| 991 | + } |
|
| 992 | + |
|
| 993 | + $info['documentation'][$key] = $url; |
|
| 994 | + } |
|
| 995 | + } |
|
| 996 | + |
|
| 997 | + $info['version'] = OC_App::getAppVersion($app); |
|
| 998 | + $appList[] = $info; |
|
| 999 | + } |
|
| 1000 | + } |
|
| 1001 | + |
|
| 1002 | + return $appList; |
|
| 1003 | + } |
|
| 1004 | + |
|
| 1005 | + /** |
|
| 1006 | + * Returns the internal app ID or false |
|
| 1007 | + * @param string $ocsID |
|
| 1008 | + * @return string|false |
|
| 1009 | + */ |
|
| 1010 | + public static function getInternalAppIdByOcs($ocsID) { |
|
| 1011 | + if(is_numeric($ocsID)) { |
|
| 1012 | + $idArray = \OC::$server->getAppConfig()->getValues(false, 'ocsid'); |
|
| 1013 | + if(array_search($ocsID, $idArray)) { |
|
| 1014 | + return array_search($ocsID, $idArray); |
|
| 1015 | + } |
|
| 1016 | + } |
|
| 1017 | + return false; |
|
| 1018 | + } |
|
| 1019 | + |
|
| 1020 | + public static function shouldUpgrade($app) { |
|
| 1021 | + $versions = self::getAppVersions(); |
|
| 1022 | + $currentVersion = OC_App::getAppVersion($app); |
|
| 1023 | + if ($currentVersion && isset($versions[$app])) { |
|
| 1024 | + $installedVersion = $versions[$app]; |
|
| 1025 | + if (!version_compare($currentVersion, $installedVersion, '=')) { |
|
| 1026 | + return true; |
|
| 1027 | + } |
|
| 1028 | + } |
|
| 1029 | + return false; |
|
| 1030 | + } |
|
| 1031 | + |
|
| 1032 | + /** |
|
| 1033 | + * Adjust the number of version parts of $version1 to match |
|
| 1034 | + * the number of version parts of $version2. |
|
| 1035 | + * |
|
| 1036 | + * @param string $version1 version to adjust |
|
| 1037 | + * @param string $version2 version to take the number of parts from |
|
| 1038 | + * @return string shortened $version1 |
|
| 1039 | + */ |
|
| 1040 | + private static function adjustVersionParts($version1, $version2) { |
|
| 1041 | + $version1 = explode('.', $version1); |
|
| 1042 | + $version2 = explode('.', $version2); |
|
| 1043 | + // reduce $version1 to match the number of parts in $version2 |
|
| 1044 | + while (count($version1) > count($version2)) { |
|
| 1045 | + array_pop($version1); |
|
| 1046 | + } |
|
| 1047 | + // if $version1 does not have enough parts, add some |
|
| 1048 | + while (count($version1) < count($version2)) { |
|
| 1049 | + $version1[] = '0'; |
|
| 1050 | + } |
|
| 1051 | + return implode('.', $version1); |
|
| 1052 | + } |
|
| 1053 | + |
|
| 1054 | + /** |
|
| 1055 | + * Check whether the current ownCloud version matches the given |
|
| 1056 | + * application's version requirements. |
|
| 1057 | + * |
|
| 1058 | + * The comparison is made based on the number of parts that the |
|
| 1059 | + * app info version has. For example for ownCloud 6.0.3 if the |
|
| 1060 | + * app info version is expecting version 6.0, the comparison is |
|
| 1061 | + * made on the first two parts of the ownCloud version. |
|
| 1062 | + * This means that it's possible to specify "requiremin" => 6 |
|
| 1063 | + * and "requiremax" => 6 and it will still match ownCloud 6.0.3. |
|
| 1064 | + * |
|
| 1065 | + * @param string $ocVersion ownCloud version to check against |
|
| 1066 | + * @param array $appInfo app info (from xml) |
|
| 1067 | + * |
|
| 1068 | + * @return boolean true if compatible, otherwise false |
|
| 1069 | + */ |
|
| 1070 | + public static function isAppCompatible($ocVersion, $appInfo) { |
|
| 1071 | + $requireMin = ''; |
|
| 1072 | + $requireMax = ''; |
|
| 1073 | + if (isset($appInfo['dependencies']['nextcloud']['@attributes']['min-version'])) { |
|
| 1074 | + $requireMin = $appInfo['dependencies']['nextcloud']['@attributes']['min-version']; |
|
| 1075 | + } elseif (isset($appInfo['dependencies']['owncloud']['@attributes']['min-version'])) { |
|
| 1076 | + $requireMin = $appInfo['dependencies']['owncloud']['@attributes']['min-version']; |
|
| 1077 | + } else if (isset($appInfo['requiremin'])) { |
|
| 1078 | + $requireMin = $appInfo['requiremin']; |
|
| 1079 | + } else if (isset($appInfo['require'])) { |
|
| 1080 | + $requireMin = $appInfo['require']; |
|
| 1081 | + } |
|
| 1082 | + |
|
| 1083 | + if (isset($appInfo['dependencies']['nextcloud']['@attributes']['max-version'])) { |
|
| 1084 | + $requireMax = $appInfo['dependencies']['nextcloud']['@attributes']['max-version']; |
|
| 1085 | + } elseif (isset($appInfo['dependencies']['owncloud']['@attributes']['max-version'])) { |
|
| 1086 | + $requireMax = $appInfo['dependencies']['owncloud']['@attributes']['max-version']; |
|
| 1087 | + } else if (isset($appInfo['requiremax'])) { |
|
| 1088 | + $requireMax = $appInfo['requiremax']; |
|
| 1089 | + } |
|
| 1090 | + |
|
| 1091 | + if (is_array($ocVersion)) { |
|
| 1092 | + $ocVersion = implode('.', $ocVersion); |
|
| 1093 | + } |
|
| 1094 | + |
|
| 1095 | + if (!empty($requireMin) |
|
| 1096 | + && version_compare(self::adjustVersionParts($ocVersion, $requireMin), $requireMin, '<') |
|
| 1097 | + ) { |
|
| 1098 | + |
|
| 1099 | + return false; |
|
| 1100 | + } |
|
| 1101 | + |
|
| 1102 | + if (!empty($requireMax) |
|
| 1103 | + && version_compare(self::adjustVersionParts($ocVersion, $requireMax), $requireMax, '>') |
|
| 1104 | + ) { |
|
| 1105 | + return false; |
|
| 1106 | + } |
|
| 1107 | + |
|
| 1108 | + return true; |
|
| 1109 | + } |
|
| 1110 | + |
|
| 1111 | + /** |
|
| 1112 | + * get the installed version of all apps |
|
| 1113 | + */ |
|
| 1114 | + public static function getAppVersions() { |
|
| 1115 | + static $versions; |
|
| 1116 | + |
|
| 1117 | + if(!$versions) { |
|
| 1118 | + $appConfig = \OC::$server->getAppConfig(); |
|
| 1119 | + $versions = $appConfig->getValues(false, 'installed_version'); |
|
| 1120 | + } |
|
| 1121 | + return $versions; |
|
| 1122 | + } |
|
| 1123 | + |
|
| 1124 | + /** |
|
| 1125 | + * @param string $app |
|
| 1126 | + * @param \OCP\IConfig $config |
|
| 1127 | + * @param \OCP\IL10N $l |
|
| 1128 | + * @return bool |
|
| 1129 | + * |
|
| 1130 | + * @throws Exception if app is not compatible with this version of ownCloud |
|
| 1131 | + * @throws Exception if no app-name was specified |
|
| 1132 | + */ |
|
| 1133 | + public function installApp($app, |
|
| 1134 | + \OCP\IConfig $config, |
|
| 1135 | + \OCP\IL10N $l) { |
|
| 1136 | + if ($app !== false) { |
|
| 1137 | + // check if the app is compatible with this version of ownCloud |
|
| 1138 | + $info = self::getAppInfo($app); |
|
| 1139 | + if(!is_array($info)) { |
|
| 1140 | + throw new \Exception( |
|
| 1141 | + $l->t('App "%s" cannot be installed because appinfo file cannot be read.', |
|
| 1142 | + [$info['name']] |
|
| 1143 | + ) |
|
| 1144 | + ); |
|
| 1145 | + } |
|
| 1146 | + |
|
| 1147 | + $version = \OCP\Util::getVersion(); |
|
| 1148 | + if (!self::isAppCompatible($version, $info)) { |
|
| 1149 | + throw new \Exception( |
|
| 1150 | + $l->t('App "%s" cannot be installed because it is not compatible with this version of the server.', |
|
| 1151 | + array($info['name']) |
|
| 1152 | + ) |
|
| 1153 | + ); |
|
| 1154 | + } |
|
| 1155 | + |
|
| 1156 | + // check for required dependencies |
|
| 1157 | + self::checkAppDependencies($config, $l, $info); |
|
| 1158 | + |
|
| 1159 | + $config->setAppValue($app, 'enabled', 'yes'); |
|
| 1160 | + if (isset($appData['id'])) { |
|
| 1161 | + $config->setAppValue($app, 'ocsid', $appData['id']); |
|
| 1162 | + } |
|
| 1163 | + |
|
| 1164 | + if(isset($info['settings']) && is_array($info['settings'])) { |
|
| 1165 | + $appPath = self::getAppPath($app); |
|
| 1166 | + self::registerAutoloading($app, $appPath); |
|
| 1167 | + \OC::$server->getSettingsManager()->setupSettings($info['settings']); |
|
| 1168 | + } |
|
| 1169 | + |
|
| 1170 | + \OC_Hook::emit('OC_App', 'post_enable', array('app' => $app)); |
|
| 1171 | + } else { |
|
| 1172 | + if(empty($appName) ) { |
|
| 1173 | + throw new \Exception($l->t("No app name specified")); |
|
| 1174 | + } else { |
|
| 1175 | + throw new \Exception($l->t("App '%s' could not be installed!", $appName)); |
|
| 1176 | + } |
|
| 1177 | + } |
|
| 1178 | + |
|
| 1179 | + return $app; |
|
| 1180 | + } |
|
| 1181 | + |
|
| 1182 | + /** |
|
| 1183 | + * update the database for the app and call the update script |
|
| 1184 | + * |
|
| 1185 | + * @param string $appId |
|
| 1186 | + * @return bool |
|
| 1187 | + */ |
|
| 1188 | + public static function updateApp($appId) { |
|
| 1189 | + $appPath = self::getAppPath($appId); |
|
| 1190 | + if($appPath === false) { |
|
| 1191 | + return false; |
|
| 1192 | + } |
|
| 1193 | + $appData = self::getAppInfo($appId); |
|
| 1194 | + self::executeRepairSteps($appId, $appData['repair-steps']['pre-migration']); |
|
| 1195 | + if (file_exists($appPath . '/appinfo/database.xml')) { |
|
| 1196 | + OC_DB::updateDbFromStructure($appPath . '/appinfo/database.xml'); |
|
| 1197 | + } |
|
| 1198 | + self::executeRepairSteps($appId, $appData['repair-steps']['post-migration']); |
|
| 1199 | + self::setupLiveMigrations($appId, $appData['repair-steps']['live-migration']); |
|
| 1200 | + unset(self::$appVersion[$appId]); |
|
| 1201 | + // run upgrade code |
|
| 1202 | + if (file_exists($appPath . '/appinfo/update.php')) { |
|
| 1203 | + self::loadApp($appId); |
|
| 1204 | + include $appPath . '/appinfo/update.php'; |
|
| 1205 | + } |
|
| 1206 | + self::setupBackgroundJobs($appData['background-jobs']); |
|
| 1207 | + if(isset($appData['settings']) && is_array($appData['settings'])) { |
|
| 1208 | + $appPath = self::getAppPath($appId); |
|
| 1209 | + self::registerAutoloading($appId, $appPath); |
|
| 1210 | + \OC::$server->getSettingsManager()->setupSettings($appData['settings']); |
|
| 1211 | + } |
|
| 1212 | + |
|
| 1213 | + //set remote/public handlers |
|
| 1214 | + if (array_key_exists('ocsid', $appData)) { |
|
| 1215 | + \OC::$server->getConfig()->setAppValue($appId, 'ocsid', $appData['ocsid']); |
|
| 1216 | + } elseif(\OC::$server->getConfig()->getAppValue($appId, 'ocsid', null) !== null) { |
|
| 1217 | + \OC::$server->getConfig()->deleteAppValue($appId, 'ocsid'); |
|
| 1218 | + } |
|
| 1219 | + foreach ($appData['remote'] as $name => $path) { |
|
| 1220 | + \OC::$server->getConfig()->setAppValue('core', 'remote_' . $name, $appId . '/' . $path); |
|
| 1221 | + } |
|
| 1222 | + foreach ($appData['public'] as $name => $path) { |
|
| 1223 | + \OC::$server->getConfig()->setAppValue('core', 'public_' . $name, $appId . '/' . $path); |
|
| 1224 | + } |
|
| 1225 | + |
|
| 1226 | + self::setAppTypes($appId); |
|
| 1227 | + |
|
| 1228 | + $version = \OC_App::getAppVersion($appId); |
|
| 1229 | + \OC::$server->getAppConfig()->setValue($appId, 'installed_version', $version); |
|
| 1230 | + |
|
| 1231 | + \OC::$server->getEventDispatcher()->dispatch(ManagerEvent::EVENT_APP_UPDATE, new ManagerEvent( |
|
| 1232 | + ManagerEvent::EVENT_APP_UPDATE, $appId |
|
| 1233 | + )); |
|
| 1234 | + |
|
| 1235 | + return true; |
|
| 1236 | + } |
|
| 1237 | + |
|
| 1238 | + /** |
|
| 1239 | + * @param string $appId |
|
| 1240 | + * @param string[] $steps |
|
| 1241 | + * @throws \OC\NeedsUpdateException |
|
| 1242 | + */ |
|
| 1243 | + public static function executeRepairSteps($appId, array $steps) { |
|
| 1244 | + if (empty($steps)) { |
|
| 1245 | + return; |
|
| 1246 | + } |
|
| 1247 | + // load the app |
|
| 1248 | + self::loadApp($appId); |
|
| 1249 | + |
|
| 1250 | + $dispatcher = OC::$server->getEventDispatcher(); |
|
| 1251 | + |
|
| 1252 | + // load the steps |
|
| 1253 | + $r = new Repair([], $dispatcher); |
|
| 1254 | + foreach ($steps as $step) { |
|
| 1255 | + try { |
|
| 1256 | + $r->addStep($step); |
|
| 1257 | + } catch (Exception $ex) { |
|
| 1258 | + $r->emit('\OC\Repair', 'error', [$ex->getMessage()]); |
|
| 1259 | + \OC::$server->getLogger()->logException($ex); |
|
| 1260 | + } |
|
| 1261 | + } |
|
| 1262 | + // run the steps |
|
| 1263 | + $r->run(); |
|
| 1264 | + } |
|
| 1265 | + |
|
| 1266 | + public static function setupBackgroundJobs(array $jobs) { |
|
| 1267 | + $queue = \OC::$server->getJobList(); |
|
| 1268 | + foreach ($jobs as $job) { |
|
| 1269 | + $queue->add($job); |
|
| 1270 | + } |
|
| 1271 | + } |
|
| 1272 | + |
|
| 1273 | + /** |
|
| 1274 | + * @param string $appId |
|
| 1275 | + * @param string[] $steps |
|
| 1276 | + */ |
|
| 1277 | + private static function setupLiveMigrations($appId, array $steps) { |
|
| 1278 | + $queue = \OC::$server->getJobList(); |
|
| 1279 | + foreach ($steps as $step) { |
|
| 1280 | + $queue->add('OC\Migration\BackgroundRepair', [ |
|
| 1281 | + 'app' => $appId, |
|
| 1282 | + 'step' => $step]); |
|
| 1283 | + } |
|
| 1284 | + } |
|
| 1285 | + |
|
| 1286 | + /** |
|
| 1287 | + * @param string $appId |
|
| 1288 | + * @return \OC\Files\View|false |
|
| 1289 | + */ |
|
| 1290 | + public static function getStorage($appId) { |
|
| 1291 | + if (OC_App::isEnabled($appId)) { //sanity check |
|
| 1292 | + if (\OC::$server->getUserSession()->isLoggedIn()) { |
|
| 1293 | + $view = new \OC\Files\View('/' . OC_User::getUser()); |
|
| 1294 | + if (!$view->file_exists($appId)) { |
|
| 1295 | + $view->mkdir($appId); |
|
| 1296 | + } |
|
| 1297 | + return new \OC\Files\View('/' . OC_User::getUser() . '/' . $appId); |
|
| 1298 | + } else { |
|
| 1299 | + \OCP\Util::writeLog('core', 'Can\'t get app storage, app ' . $appId . ', user not logged in', \OCP\Util::ERROR); |
|
| 1300 | + return false; |
|
| 1301 | + } |
|
| 1302 | + } else { |
|
| 1303 | + \OCP\Util::writeLog('core', 'Can\'t get app storage, app ' . $appId . ' not enabled', \OCP\Util::ERROR); |
|
| 1304 | + return false; |
|
| 1305 | + } |
|
| 1306 | + } |
|
| 1307 | + |
|
| 1308 | + protected static function findBestL10NOption($options, $lang) { |
|
| 1309 | + $fallback = $similarLangFallback = $englishFallback = false; |
|
| 1310 | + |
|
| 1311 | + $lang = strtolower($lang); |
|
| 1312 | + $similarLang = $lang; |
|
| 1313 | + if (strpos($similarLang, '_')) { |
|
| 1314 | + // For "de_DE" we want to find "de" and the other way around |
|
| 1315 | + $similarLang = substr($lang, 0, strpos($lang, '_')); |
|
| 1316 | + } |
|
| 1317 | + |
|
| 1318 | + foreach ($options as $option) { |
|
| 1319 | + if (is_array($option)) { |
|
| 1320 | + if ($fallback === false) { |
|
| 1321 | + $fallback = $option['@value']; |
|
| 1322 | + } |
|
| 1323 | + |
|
| 1324 | + if (!isset($option['@attributes']['lang'])) { |
|
| 1325 | + continue; |
|
| 1326 | + } |
|
| 1327 | + |
|
| 1328 | + $attributeLang = strtolower($option['@attributes']['lang']); |
|
| 1329 | + if ($attributeLang === $lang) { |
|
| 1330 | + return $option['@value']; |
|
| 1331 | + } |
|
| 1332 | + |
|
| 1333 | + if ($attributeLang === $similarLang) { |
|
| 1334 | + $similarLangFallback = $option['@value']; |
|
| 1335 | + } else if (strpos($attributeLang, $similarLang . '_') === 0) { |
|
| 1336 | + if ($similarLangFallback === false) { |
|
| 1337 | + $similarLangFallback = $option['@value']; |
|
| 1338 | + } |
|
| 1339 | + } |
|
| 1340 | + } else { |
|
| 1341 | + $englishFallback = $option; |
|
| 1342 | + } |
|
| 1343 | + } |
|
| 1344 | + |
|
| 1345 | + if ($similarLangFallback !== false) { |
|
| 1346 | + return $similarLangFallback; |
|
| 1347 | + } else if ($englishFallback !== false) { |
|
| 1348 | + return $englishFallback; |
|
| 1349 | + } |
|
| 1350 | + return (string) $fallback; |
|
| 1351 | + } |
|
| 1352 | + |
|
| 1353 | + /** |
|
| 1354 | + * parses the app data array and enhanced the 'description' value |
|
| 1355 | + * |
|
| 1356 | + * @param array $data the app data |
|
| 1357 | + * @param string $lang |
|
| 1358 | + * @return array improved app data |
|
| 1359 | + */ |
|
| 1360 | + public static function parseAppInfo(array $data, $lang = null) { |
|
| 1361 | + |
|
| 1362 | + if ($lang && isset($data['name']) && is_array($data['name'])) { |
|
| 1363 | + $data['name'] = self::findBestL10NOption($data['name'], $lang); |
|
| 1364 | + } |
|
| 1365 | + if ($lang && isset($data['summary']) && is_array($data['summary'])) { |
|
| 1366 | + $data['summary'] = self::findBestL10NOption($data['summary'], $lang); |
|
| 1367 | + } |
|
| 1368 | + if ($lang && isset($data['description']) && is_array($data['description'])) { |
|
| 1369 | + $data['description'] = trim(self::findBestL10NOption($data['description'], $lang)); |
|
| 1370 | + } else if (isset($data['description']) && is_string($data['description'])) { |
|
| 1371 | + $data['description'] = trim($data['description']); |
|
| 1372 | + } else { |
|
| 1373 | + $data['description'] = ''; |
|
| 1374 | + } |
|
| 1375 | + |
|
| 1376 | + return $data; |
|
| 1377 | + } |
|
| 1378 | + |
|
| 1379 | + /** |
|
| 1380 | + * @param \OCP\IConfig $config |
|
| 1381 | + * @param \OCP\IL10N $l |
|
| 1382 | + * @param array $info |
|
| 1383 | + * @throws \Exception |
|
| 1384 | + */ |
|
| 1385 | + protected static function checkAppDependencies($config, $l, $info) { |
|
| 1386 | + $dependencyAnalyzer = new DependencyAnalyzer(new Platform($config), $l); |
|
| 1387 | + $missing = $dependencyAnalyzer->analyze($info); |
|
| 1388 | + if (!empty($missing)) { |
|
| 1389 | + $missingMsg = join(PHP_EOL, $missing); |
|
| 1390 | + throw new \Exception( |
|
| 1391 | + $l->t('App "%s" cannot be installed because the following dependencies are not fulfilled: %s', |
|
| 1392 | + [$info['name'], $missingMsg] |
|
| 1393 | + ) |
|
| 1394 | + ); |
|
| 1395 | + } |
|
| 1396 | + } |
|
| 1397 | 1397 | } |
@@ -111,9 +111,9 @@ discard block |
||
| 111 | 111 | $apps = self::getEnabledApps(); |
| 112 | 112 | |
| 113 | 113 | // Add each apps' folder as allowed class path |
| 114 | - foreach($apps as $app) { |
|
| 114 | + foreach ($apps as $app) { |
|
| 115 | 115 | $path = self::getAppPath($app); |
| 116 | - if($path !== false) { |
|
| 116 | + if ($path !== false) { |
|
| 117 | 117 | self::registerAutoloading($app, $path); |
| 118 | 118 | } |
| 119 | 119 | } |
@@ -138,15 +138,15 @@ discard block |
||
| 138 | 138 | public static function loadApp($app) { |
| 139 | 139 | self::$loadedApps[] = $app; |
| 140 | 140 | $appPath = self::getAppPath($app); |
| 141 | - if($appPath === false) { |
|
| 141 | + if ($appPath === false) { |
|
| 142 | 142 | return; |
| 143 | 143 | } |
| 144 | 144 | |
| 145 | 145 | // in case someone calls loadApp() directly |
| 146 | 146 | self::registerAutoloading($app, $appPath); |
| 147 | 147 | |
| 148 | - if (is_file($appPath . '/appinfo/app.php')) { |
|
| 149 | - \OC::$server->getEventLogger()->start('load_app_' . $app, 'Load app: ' . $app); |
|
| 148 | + if (is_file($appPath.'/appinfo/app.php')) { |
|
| 149 | + \OC::$server->getEventLogger()->start('load_app_'.$app, 'Load app: '.$app); |
|
| 150 | 150 | self::requireAppFile($app); |
| 151 | 151 | if (self::isType($app, array('authentication'))) { |
| 152 | 152 | // since authentication apps affect the "is app enabled for group" check, |
@@ -155,7 +155,7 @@ discard block |
||
| 155 | 155 | // enabled for groups |
| 156 | 156 | self::$enabledAppsCache = array(); |
| 157 | 157 | } |
| 158 | - \OC::$server->getEventLogger()->end('load_app_' . $app); |
|
| 158 | + \OC::$server->getEventLogger()->end('load_app_'.$app); |
|
| 159 | 159 | } |
| 160 | 160 | |
| 161 | 161 | $info = self::getAppInfo($app); |
@@ -182,16 +182,16 @@ discard block |
||
| 182 | 182 | * @param string $path |
| 183 | 183 | */ |
| 184 | 184 | public static function registerAutoloading($app, $path) { |
| 185 | - $key = $app . '-' . $path; |
|
| 186 | - if(isset(self::$alreadyRegistered[$key])) { |
|
| 185 | + $key = $app.'-'.$path; |
|
| 186 | + if (isset(self::$alreadyRegistered[$key])) { |
|
| 187 | 187 | return; |
| 188 | 188 | } |
| 189 | 189 | self::$alreadyRegistered[$key] = true; |
| 190 | 190 | // Register on PSR-4 composer autoloader |
| 191 | 191 | $appNamespace = \OC\AppFramework\App::buildAppNamespace($app); |
| 192 | - \OC::$composerAutoloader->addPsr4($appNamespace . '\\', $path . '/lib/', true); |
|
| 192 | + \OC::$composerAutoloader->addPsr4($appNamespace.'\\', $path.'/lib/', true); |
|
| 193 | 193 | if (defined('PHPUNIT_RUN') || defined('CLI_TEST_RUN')) { |
| 194 | - \OC::$composerAutoloader->addPsr4($appNamespace . '\\Tests\\', $path . '/tests/', true); |
|
| 194 | + \OC::$composerAutoloader->addPsr4($appNamespace.'\\Tests\\', $path.'/tests/', true); |
|
| 195 | 195 | } |
| 196 | 196 | |
| 197 | 197 | // Register on legacy autoloader |
@@ -206,7 +206,7 @@ discard block |
||
| 206 | 206 | private static function requireAppFile($app) { |
| 207 | 207 | try { |
| 208 | 208 | // encapsulated here to avoid variable scope conflicts |
| 209 | - require_once $app . '/appinfo/app.php'; |
|
| 209 | + require_once $app.'/appinfo/app.php'; |
|
| 210 | 210 | } catch (Error $ex) { |
| 211 | 211 | \OC::$server->getLogger()->logException($ex); |
| 212 | 212 | $blacklist = \OC::$server->getAppManager()->getAlwaysEnabledApps(); |
@@ -260,7 +260,7 @@ discard block |
||
| 260 | 260 | */ |
| 261 | 261 | public static function setAppTypes($app) { |
| 262 | 262 | $appData = self::getAppInfo($app); |
| 263 | - if(!is_array($appData)) { |
|
| 263 | + if (!is_array($appData)) { |
|
| 264 | 264 | return; |
| 265 | 265 | } |
| 266 | 266 | |
@@ -324,8 +324,8 @@ discard block |
||
| 324 | 324 | } else { |
| 325 | 325 | $apps = $appManager->getEnabledAppsForUser($user); |
| 326 | 326 | } |
| 327 | - $apps = array_filter($apps, function ($app) { |
|
| 328 | - return $app !== 'files';//we add this manually |
|
| 327 | + $apps = array_filter($apps, function($app) { |
|
| 328 | + return $app !== 'files'; //we add this manually |
|
| 329 | 329 | }); |
| 330 | 330 | sort($apps); |
| 331 | 331 | array_unshift($apps, 'files'); |
@@ -369,7 +369,7 @@ discard block |
||
| 369 | 369 | ); |
| 370 | 370 | $isDownloaded = $installer->isDownloaded($appId); |
| 371 | 371 | |
| 372 | - if(!$isDownloaded) { |
|
| 372 | + if (!$isDownloaded) { |
|
| 373 | 373 | $installer->downloadApp($appId); |
| 374 | 374 | } |
| 375 | 375 | |
@@ -407,7 +407,7 @@ discard block |
||
| 407 | 407 | } |
| 408 | 408 | |
| 409 | 409 | $info = self::getAppInfo($appId); |
| 410 | - if(isset($info['settings']) && is_array($info['settings'])) { |
|
| 410 | + if (isset($info['settings']) && is_array($info['settings'])) { |
|
| 411 | 411 | $appPath = self::getAppPath($appId); |
| 412 | 412 | self::registerAutoloading($appId, $appPath); |
| 413 | 413 | \OC::$server->getSettingsManager()->setupSettings($info['settings']); |
@@ -496,7 +496,7 @@ discard block |
||
| 496 | 496 | //SubAdmins are also allowed to access user management |
| 497 | 497 | $userObject = \OC::$server->getUserSession()->getUser(); |
| 498 | 498 | $isSubAdmin = false; |
| 499 | - if($userObject !== null) { |
|
| 499 | + if ($userObject !== null) { |
|
| 500 | 500 | $isSubAdmin = \OC::$server->getGroupManager()->getSubAdmin()->isSubAdmin($userObject); |
| 501 | 501 | } |
| 502 | 502 | if ($isSubAdmin) { |
@@ -530,7 +530,7 @@ discard block |
||
| 530 | 530 | // This is private as well. It simply works, so don't ask for more details |
| 531 | 531 | private static function proceedNavigation($list) { |
| 532 | 532 | $headerIconCount = 8; |
| 533 | - if(OC_User::isAdminUser(OC_User::getUser())) { |
|
| 533 | + if (OC_User::isAdminUser(OC_User::getUser())) { |
|
| 534 | 534 | $headerIconCount--; |
| 535 | 535 | } |
| 536 | 536 | usort($list, function($a, $b) { |
@@ -555,16 +555,16 @@ discard block |
||
| 555 | 555 | } |
| 556 | 556 | unset($navEntry); |
| 557 | 557 | |
| 558 | - if($activeAppIndex > ($headerIconCount-1)) { |
|
| 558 | + if ($activeAppIndex > ($headerIconCount - 1)) { |
|
| 559 | 559 | $active = $list[$activeAppIndex]; |
| 560 | - $lastInHeader = $list[$headerIconCount-1]; |
|
| 561 | - $list[$headerIconCount-1] = $active; |
|
| 560 | + $lastInHeader = $list[$headerIconCount - 1]; |
|
| 561 | + $list[$headerIconCount - 1] = $active; |
|
| 562 | 562 | $list[$activeAppIndex] = $lastInHeader; |
| 563 | 563 | } |
| 564 | 564 | |
| 565 | 565 | foreach ($list as $index => &$navEntry) { |
| 566 | 566 | $navEntry['showInHeader'] = false; |
| 567 | - if($index < $headerIconCount) { |
|
| 567 | + if ($index < $headerIconCount) { |
|
| 568 | 568 | $navEntry['showInHeader'] = true; |
| 569 | 569 | } |
| 570 | 570 | } |
@@ -576,7 +576,7 @@ discard block |
||
| 576 | 576 | |
| 577 | 577 | public static function proceedAppNavigation($entries) { |
| 578 | 578 | $headerIconCount = 8; |
| 579 | - if(OC_User::isAdminUser(OC_User::getUser())) { |
|
| 579 | + if (OC_User::isAdminUser(OC_User::getUser())) { |
|
| 580 | 580 | $headerIconCount--; |
| 581 | 581 | } |
| 582 | 582 | $activeAppIndex = -1; |
@@ -592,10 +592,10 @@ discard block |
||
| 592 | 592 | } |
| 593 | 593 | } |
| 594 | 594 | // move active item to last position |
| 595 | - if($activeAppIndex > ($headerIconCount-1)) { |
|
| 595 | + if ($activeAppIndex > ($headerIconCount - 1)) { |
|
| 596 | 596 | $active = $list[$activeAppIndex]; |
| 597 | - $lastInHeader = $list[$headerIconCount-1]; |
|
| 598 | - $list[$headerIconCount-1] = $active; |
|
| 597 | + $lastInHeader = $list[$headerIconCount - 1]; |
|
| 598 | + $list[$headerIconCount - 1] = $active; |
|
| 599 | 599 | $list[$activeAppIndex] = $lastInHeader; |
| 600 | 600 | } |
| 601 | 601 | $list = array_slice($list, 0, $headerIconCount); |
@@ -632,7 +632,7 @@ discard block |
||
| 632 | 632 | */ |
| 633 | 633 | public static function findAppInDirectories($appId) { |
| 634 | 634 | $sanitizedAppId = self::cleanAppId($appId); |
| 635 | - if($sanitizedAppId !== $appId) { |
|
| 635 | + if ($sanitizedAppId !== $appId) { |
|
| 636 | 636 | return false; |
| 637 | 637 | } |
| 638 | 638 | static $app_dir = array(); |
@@ -643,7 +643,7 @@ discard block |
||
| 643 | 643 | |
| 644 | 644 | $possibleApps = array(); |
| 645 | 645 | foreach (OC::$APPSROOTS as $dir) { |
| 646 | - if (file_exists($dir['path'] . '/' . $appId)) { |
|
| 646 | + if (file_exists($dir['path'].'/'.$appId)) { |
|
| 647 | 647 | $possibleApps[] = $dir; |
| 648 | 648 | } |
| 649 | 649 | } |
@@ -684,7 +684,7 @@ discard block |
||
| 684 | 684 | } |
| 685 | 685 | |
| 686 | 686 | if (($dir = self::findAppInDirectories($appId)) != false) { |
| 687 | - return $dir['path'] . '/' . $appId; |
|
| 687 | + return $dir['path'].'/'.$appId; |
|
| 688 | 688 | } |
| 689 | 689 | return false; |
| 690 | 690 | } |
@@ -698,7 +698,7 @@ discard block |
||
| 698 | 698 | */ |
| 699 | 699 | public static function getAppWebPath($appId) { |
| 700 | 700 | if (($dir = self::findAppInDirectories($appId)) != false) { |
| 701 | - return OC::$WEBROOT . $dir['url'] . '/' . $appId; |
|
| 701 | + return OC::$WEBROOT.$dir['url'].'/'.$appId; |
|
| 702 | 702 | } |
| 703 | 703 | return false; |
| 704 | 704 | } |
@@ -711,7 +711,7 @@ discard block |
||
| 711 | 711 | * @return string |
| 712 | 712 | */ |
| 713 | 713 | public static function getAppVersion($appId, $useCache = true) { |
| 714 | - if($useCache && isset(self::$appVersion[$appId])) { |
|
| 714 | + if ($useCache && isset(self::$appVersion[$appId])) { |
|
| 715 | 715 | return self::$appVersion[$appId]; |
| 716 | 716 | } |
| 717 | 717 | |
@@ -727,7 +727,7 @@ discard block |
||
| 727 | 727 | * @return string |
| 728 | 728 | */ |
| 729 | 729 | public static function getAppVersionByPath($path) { |
| 730 | - $infoFile = $path . '/appinfo/info.xml'; |
|
| 730 | + $infoFile = $path.'/appinfo/info.xml'; |
|
| 731 | 731 | $appData = self::getAppInfo($infoFile, true); |
| 732 | 732 | return isset($appData['version']) ? $appData['version'] : ''; |
| 733 | 733 | } |
@@ -750,10 +750,10 @@ discard block |
||
| 750 | 750 | return self::$appInfo[$appId]; |
| 751 | 751 | } |
| 752 | 752 | $appPath = self::getAppPath($appId); |
| 753 | - if($appPath === false) { |
|
| 753 | + if ($appPath === false) { |
|
| 754 | 754 | return null; |
| 755 | 755 | } |
| 756 | - $file = $appPath . '/appinfo/info.xml'; |
|
| 756 | + $file = $appPath.'/appinfo/info.xml'; |
|
| 757 | 757 | } |
| 758 | 758 | |
| 759 | 759 | $parser = new InfoParser(\OC::$server->getMemCacheFactory()->create('core.appinfo')); |
@@ -762,9 +762,9 @@ discard block |
||
| 762 | 762 | if (is_array($data)) { |
| 763 | 763 | $data = OC_App::parseAppInfo($data, $lang); |
| 764 | 764 | } |
| 765 | - if(isset($data['ocsid'])) { |
|
| 765 | + if (isset($data['ocsid'])) { |
|
| 766 | 766 | $storedId = \OC::$server->getConfig()->getAppValue($appId, 'ocsid'); |
| 767 | - if($storedId !== '' && $storedId !== $data['ocsid']) { |
|
| 767 | + if ($storedId !== '' && $storedId !== $data['ocsid']) { |
|
| 768 | 768 | $data['ocsid'] = $storedId; |
| 769 | 769 | } |
| 770 | 770 | } |
@@ -860,7 +860,7 @@ discard block |
||
| 860 | 860 | * @param string $page |
| 861 | 861 | */ |
| 862 | 862 | public static function registerAdmin($app, $page) { |
| 863 | - self::$adminForms[] = $app . '/' . $page . '.php'; |
|
| 863 | + self::$adminForms[] = $app.'/'.$page.'.php'; |
|
| 864 | 864 | } |
| 865 | 865 | |
| 866 | 866 | /** |
@@ -869,7 +869,7 @@ discard block |
||
| 869 | 869 | * @param string $page |
| 870 | 870 | */ |
| 871 | 871 | public static function registerPersonal($app, $page) { |
| 872 | - self::$personalForms[] = $app . '/' . $page . '.php'; |
|
| 872 | + self::$personalForms[] = $app.'/'.$page.'.php'; |
|
| 873 | 873 | } |
| 874 | 874 | |
| 875 | 875 | /** |
@@ -898,7 +898,7 @@ discard block |
||
| 898 | 898 | |
| 899 | 899 | foreach (OC::$APPSROOTS as $apps_dir) { |
| 900 | 900 | if (!is_readable($apps_dir['path'])) { |
| 901 | - \OCP\Util::writeLog('core', 'unable to read app folder : ' . $apps_dir['path'], \OCP\Util::WARN); |
|
| 901 | + \OCP\Util::writeLog('core', 'unable to read app folder : '.$apps_dir['path'], \OCP\Util::WARN); |
|
| 902 | 902 | continue; |
| 903 | 903 | } |
| 904 | 904 | $dh = opendir($apps_dir['path']); |
@@ -906,7 +906,7 @@ discard block |
||
| 906 | 906 | if (is_resource($dh)) { |
| 907 | 907 | while (($file = readdir($dh)) !== false) { |
| 908 | 908 | |
| 909 | - if ($file[0] != '.' and is_dir($apps_dir['path'] . '/' . $file) and is_file($apps_dir['path'] . '/' . $file . '/appinfo/info.xml')) { |
|
| 909 | + if ($file[0] != '.' and is_dir($apps_dir['path'].'/'.$file) and is_file($apps_dir['path'].'/'.$file.'/appinfo/info.xml')) { |
|
| 910 | 910 | |
| 911 | 911 | $apps[] = $file; |
| 912 | 912 | } |
@@ -936,12 +936,12 @@ discard block |
||
| 936 | 936 | |
| 937 | 937 | $info = OC_App::getAppInfo($app, false, $langCode); |
| 938 | 938 | if (!is_array($info)) { |
| 939 | - \OCP\Util::writeLog('core', 'Could not read app info file for app "' . $app . '"', \OCP\Util::ERROR); |
|
| 939 | + \OCP\Util::writeLog('core', 'Could not read app info file for app "'.$app.'"', \OCP\Util::ERROR); |
|
| 940 | 940 | continue; |
| 941 | 941 | } |
| 942 | 942 | |
| 943 | 943 | if (!isset($info['name'])) { |
| 944 | - \OCP\Util::writeLog('core', 'App id "' . $app . '" has no name in appinfo', \OCP\Util::ERROR); |
|
| 944 | + \OCP\Util::writeLog('core', 'App id "'.$app.'" has no name in appinfo', \OCP\Util::ERROR); |
|
| 945 | 945 | continue; |
| 946 | 946 | } |
| 947 | 947 | |
@@ -968,13 +968,13 @@ discard block |
||
| 968 | 968 | } |
| 969 | 969 | |
| 970 | 970 | $appPath = self::getAppPath($app); |
| 971 | - if($appPath !== false) { |
|
| 972 | - $appIcon = $appPath . '/img/' . $app . '.svg'; |
|
| 971 | + if ($appPath !== false) { |
|
| 972 | + $appIcon = $appPath.'/img/'.$app.'.svg'; |
|
| 973 | 973 | if (file_exists($appIcon)) { |
| 974 | - $info['preview'] = \OC::$server->getURLGenerator()->imagePath($app, $app . '.svg'); |
|
| 974 | + $info['preview'] = \OC::$server->getURLGenerator()->imagePath($app, $app.'.svg'); |
|
| 975 | 975 | $info['previewAsIcon'] = true; |
| 976 | 976 | } else { |
| 977 | - $appIcon = $appPath . '/img/app.svg'; |
|
| 977 | + $appIcon = $appPath.'/img/app.svg'; |
|
| 978 | 978 | if (file_exists($appIcon)) { |
| 979 | 979 | $info['preview'] = \OC::$server->getURLGenerator()->imagePath($app, 'app.svg'); |
| 980 | 980 | $info['previewAsIcon'] = true; |
@@ -1008,9 +1008,9 @@ discard block |
||
| 1008 | 1008 | * @return string|false |
| 1009 | 1009 | */ |
| 1010 | 1010 | public static function getInternalAppIdByOcs($ocsID) { |
| 1011 | - if(is_numeric($ocsID)) { |
|
| 1011 | + if (is_numeric($ocsID)) { |
|
| 1012 | 1012 | $idArray = \OC::$server->getAppConfig()->getValues(false, 'ocsid'); |
| 1013 | - if(array_search($ocsID, $idArray)) { |
|
| 1013 | + if (array_search($ocsID, $idArray)) { |
|
| 1014 | 1014 | return array_search($ocsID, $idArray); |
| 1015 | 1015 | } |
| 1016 | 1016 | } |
@@ -1114,7 +1114,7 @@ discard block |
||
| 1114 | 1114 | public static function getAppVersions() { |
| 1115 | 1115 | static $versions; |
| 1116 | 1116 | |
| 1117 | - if(!$versions) { |
|
| 1117 | + if (!$versions) { |
|
| 1118 | 1118 | $appConfig = \OC::$server->getAppConfig(); |
| 1119 | 1119 | $versions = $appConfig->getValues(false, 'installed_version'); |
| 1120 | 1120 | } |
@@ -1136,7 +1136,7 @@ discard block |
||
| 1136 | 1136 | if ($app !== false) { |
| 1137 | 1137 | // check if the app is compatible with this version of ownCloud |
| 1138 | 1138 | $info = self::getAppInfo($app); |
| 1139 | - if(!is_array($info)) { |
|
| 1139 | + if (!is_array($info)) { |
|
| 1140 | 1140 | throw new \Exception( |
| 1141 | 1141 | $l->t('App "%s" cannot be installed because appinfo file cannot be read.', |
| 1142 | 1142 | [$info['name']] |
@@ -1161,7 +1161,7 @@ discard block |
||
| 1161 | 1161 | $config->setAppValue($app, 'ocsid', $appData['id']); |
| 1162 | 1162 | } |
| 1163 | 1163 | |
| 1164 | - if(isset($info['settings']) && is_array($info['settings'])) { |
|
| 1164 | + if (isset($info['settings']) && is_array($info['settings'])) { |
|
| 1165 | 1165 | $appPath = self::getAppPath($app); |
| 1166 | 1166 | self::registerAutoloading($app, $appPath); |
| 1167 | 1167 | \OC::$server->getSettingsManager()->setupSettings($info['settings']); |
@@ -1169,7 +1169,7 @@ discard block |
||
| 1169 | 1169 | |
| 1170 | 1170 | \OC_Hook::emit('OC_App', 'post_enable', array('app' => $app)); |
| 1171 | 1171 | } else { |
| 1172 | - if(empty($appName) ) { |
|
| 1172 | + if (empty($appName)) { |
|
| 1173 | 1173 | throw new \Exception($l->t("No app name specified")); |
| 1174 | 1174 | } else { |
| 1175 | 1175 | throw new \Exception($l->t("App '%s' could not be installed!", $appName)); |
@@ -1187,24 +1187,24 @@ discard block |
||
| 1187 | 1187 | */ |
| 1188 | 1188 | public static function updateApp($appId) { |
| 1189 | 1189 | $appPath = self::getAppPath($appId); |
| 1190 | - if($appPath === false) { |
|
| 1190 | + if ($appPath === false) { |
|
| 1191 | 1191 | return false; |
| 1192 | 1192 | } |
| 1193 | 1193 | $appData = self::getAppInfo($appId); |
| 1194 | 1194 | self::executeRepairSteps($appId, $appData['repair-steps']['pre-migration']); |
| 1195 | - if (file_exists($appPath . '/appinfo/database.xml')) { |
|
| 1196 | - OC_DB::updateDbFromStructure($appPath . '/appinfo/database.xml'); |
|
| 1195 | + if (file_exists($appPath.'/appinfo/database.xml')) { |
|
| 1196 | + OC_DB::updateDbFromStructure($appPath.'/appinfo/database.xml'); |
|
| 1197 | 1197 | } |
| 1198 | 1198 | self::executeRepairSteps($appId, $appData['repair-steps']['post-migration']); |
| 1199 | 1199 | self::setupLiveMigrations($appId, $appData['repair-steps']['live-migration']); |
| 1200 | 1200 | unset(self::$appVersion[$appId]); |
| 1201 | 1201 | // run upgrade code |
| 1202 | - if (file_exists($appPath . '/appinfo/update.php')) { |
|
| 1202 | + if (file_exists($appPath.'/appinfo/update.php')) { |
|
| 1203 | 1203 | self::loadApp($appId); |
| 1204 | - include $appPath . '/appinfo/update.php'; |
|
| 1204 | + include $appPath.'/appinfo/update.php'; |
|
| 1205 | 1205 | } |
| 1206 | 1206 | self::setupBackgroundJobs($appData['background-jobs']); |
| 1207 | - if(isset($appData['settings']) && is_array($appData['settings'])) { |
|
| 1207 | + if (isset($appData['settings']) && is_array($appData['settings'])) { |
|
| 1208 | 1208 | $appPath = self::getAppPath($appId); |
| 1209 | 1209 | self::registerAutoloading($appId, $appPath); |
| 1210 | 1210 | \OC::$server->getSettingsManager()->setupSettings($appData['settings']); |
@@ -1213,14 +1213,14 @@ discard block |
||
| 1213 | 1213 | //set remote/public handlers |
| 1214 | 1214 | if (array_key_exists('ocsid', $appData)) { |
| 1215 | 1215 | \OC::$server->getConfig()->setAppValue($appId, 'ocsid', $appData['ocsid']); |
| 1216 | - } elseif(\OC::$server->getConfig()->getAppValue($appId, 'ocsid', null) !== null) { |
|
| 1216 | + } elseif (\OC::$server->getConfig()->getAppValue($appId, 'ocsid', null) !== null) { |
|
| 1217 | 1217 | \OC::$server->getConfig()->deleteAppValue($appId, 'ocsid'); |
| 1218 | 1218 | } |
| 1219 | 1219 | foreach ($appData['remote'] as $name => $path) { |
| 1220 | - \OC::$server->getConfig()->setAppValue('core', 'remote_' . $name, $appId . '/' . $path); |
|
| 1220 | + \OC::$server->getConfig()->setAppValue('core', 'remote_'.$name, $appId.'/'.$path); |
|
| 1221 | 1221 | } |
| 1222 | 1222 | foreach ($appData['public'] as $name => $path) { |
| 1223 | - \OC::$server->getConfig()->setAppValue('core', 'public_' . $name, $appId . '/' . $path); |
|
| 1223 | + \OC::$server->getConfig()->setAppValue('core', 'public_'.$name, $appId.'/'.$path); |
|
| 1224 | 1224 | } |
| 1225 | 1225 | |
| 1226 | 1226 | self::setAppTypes($appId); |
@@ -1290,17 +1290,17 @@ discard block |
||
| 1290 | 1290 | public static function getStorage($appId) { |
| 1291 | 1291 | if (OC_App::isEnabled($appId)) { //sanity check |
| 1292 | 1292 | if (\OC::$server->getUserSession()->isLoggedIn()) { |
| 1293 | - $view = new \OC\Files\View('/' . OC_User::getUser()); |
|
| 1293 | + $view = new \OC\Files\View('/'.OC_User::getUser()); |
|
| 1294 | 1294 | if (!$view->file_exists($appId)) { |
| 1295 | 1295 | $view->mkdir($appId); |
| 1296 | 1296 | } |
| 1297 | - return new \OC\Files\View('/' . OC_User::getUser() . '/' . $appId); |
|
| 1297 | + return new \OC\Files\View('/'.OC_User::getUser().'/'.$appId); |
|
| 1298 | 1298 | } else { |
| 1299 | - \OCP\Util::writeLog('core', 'Can\'t get app storage, app ' . $appId . ', user not logged in', \OCP\Util::ERROR); |
|
| 1299 | + \OCP\Util::writeLog('core', 'Can\'t get app storage, app '.$appId.', user not logged in', \OCP\Util::ERROR); |
|
| 1300 | 1300 | return false; |
| 1301 | 1301 | } |
| 1302 | 1302 | } else { |
| 1303 | - \OCP\Util::writeLog('core', 'Can\'t get app storage, app ' . $appId . ' not enabled', \OCP\Util::ERROR); |
|
| 1303 | + \OCP\Util::writeLog('core', 'Can\'t get app storage, app '.$appId.' not enabled', \OCP\Util::ERROR); |
|
| 1304 | 1304 | return false; |
| 1305 | 1305 | } |
| 1306 | 1306 | } |
@@ -1332,9 +1332,9 @@ discard block |
||
| 1332 | 1332 | |
| 1333 | 1333 | if ($attributeLang === $similarLang) { |
| 1334 | 1334 | $similarLangFallback = $option['@value']; |
| 1335 | - } else if (strpos($attributeLang, $similarLang . '_') === 0) { |
|
| 1335 | + } else if (strpos($attributeLang, $similarLang.'_') === 0) { |
|
| 1336 | 1336 | if ($similarLangFallback === false) { |
| 1337 | - $similarLangFallback = $option['@value']; |
|
| 1337 | + $similarLangFallback = $option['@value']; |
|
| 1338 | 1338 | } |
| 1339 | 1339 | } |
| 1340 | 1340 | } else { |
@@ -1369,7 +1369,7 @@ discard block |
||
| 1369 | 1369 | $data['description'] = trim(self::findBestL10NOption($data['description'], $lang)); |
| 1370 | 1370 | } else if (isset($data['description']) && is_string($data['description'])) { |
| 1371 | 1371 | $data['description'] = trim($data['description']); |
| 1372 | - } else { |
|
| 1372 | + } else { |
|
| 1373 | 1373 | $data['description'] = ''; |
| 1374 | 1374 | } |
| 1375 | 1375 | |
@@ -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 | |
@@ -40,391 +40,391 @@ |
||
| 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 | - /** @var IConfig */ |
|
| 63 | - protected $config; |
|
| 64 | - |
|
| 65 | - /** @var IRequest */ |
|
| 66 | - protected $request; |
|
| 67 | - |
|
| 68 | - /** @var IUserSession */ |
|
| 69 | - protected $userSession; |
|
| 70 | - |
|
| 71 | - /** @var string */ |
|
| 72 | - protected $serverRoot; |
|
| 73 | - |
|
| 74 | - /** |
|
| 75 | - * @param IConfig $config |
|
| 76 | - * @param IRequest $request |
|
| 77 | - * @param IUserSession $userSession |
|
| 78 | - * @param string $serverRoot |
|
| 79 | - */ |
|
| 80 | - public function __construct(IConfig $config, |
|
| 81 | - IRequest $request, |
|
| 82 | - IUserSession $userSession, |
|
| 83 | - $serverRoot) { |
|
| 84 | - $this->config = $config; |
|
| 85 | - $this->request = $request; |
|
| 86 | - $this->userSession = $userSession; |
|
| 87 | - $this->serverRoot = $serverRoot; |
|
| 88 | - } |
|
| 89 | - |
|
| 90 | - /** |
|
| 91 | - * Get a language instance |
|
| 92 | - * |
|
| 93 | - * @param string $app |
|
| 94 | - * @param string|null $lang |
|
| 95 | - * @return \OCP\IL10N |
|
| 96 | - */ |
|
| 97 | - public function get($app, $lang = null) { |
|
| 98 | - $app = \OC_App::cleanAppId($app); |
|
| 99 | - if ($lang !== null) { |
|
| 100 | - $lang = str_replace(array('\0', '/', '\\', '..'), '', (string) $lang); |
|
| 101 | - } |
|
| 102 | - if ($lang === null || !$this->languageExists($app, $lang)) { |
|
| 103 | - $lang = $this->findLanguage($app); |
|
| 104 | - } |
|
| 105 | - |
|
| 106 | - if (!isset($this->instances[$lang][$app])) { |
|
| 107 | - $this->instances[$lang][$app] = new L10N( |
|
| 108 | - $this, $app, $lang, |
|
| 109 | - $this->getL10nFilesForApp($app, $lang) |
|
| 110 | - ); |
|
| 111 | - } |
|
| 112 | - |
|
| 113 | - return $this->instances[$lang][$app]; |
|
| 114 | - } |
|
| 115 | - |
|
| 116 | - /** |
|
| 117 | - * Find the best language |
|
| 118 | - * |
|
| 119 | - * @param string|null $app App id or null for core |
|
| 120 | - * @return string language If nothing works it returns 'en' |
|
| 121 | - */ |
|
| 122 | - public function findLanguage($app = null) { |
|
| 123 | - if ($this->requestLanguage !== '' && $this->languageExists($app, $this->requestLanguage)) { |
|
| 124 | - return $this->requestLanguage; |
|
| 125 | - } |
|
| 126 | - |
|
| 127 | - /** |
|
| 128 | - * At this point ownCloud might not yet be installed and thus the lookup |
|
| 129 | - * in the preferences table might fail. For this reason we need to check |
|
| 130 | - * whether the instance has already been installed |
|
| 131 | - * |
|
| 132 | - * @link https://github.com/owncloud/core/issues/21955 |
|
| 133 | - */ |
|
| 134 | - if($this->config->getSystemValue('installed', false)) { |
|
| 135 | - $userId = !is_null($this->userSession->getUser()) ? $this->userSession->getUser()->getUID() : null; |
|
| 136 | - if(!is_null($userId)) { |
|
| 137 | - $userLang = $this->config->getUserValue($userId, 'core', 'lang', null); |
|
| 138 | - } else { |
|
| 139 | - $userLang = null; |
|
| 140 | - } |
|
| 141 | - } else { |
|
| 142 | - $userId = null; |
|
| 143 | - $userLang = null; |
|
| 144 | - } |
|
| 145 | - |
|
| 146 | - if ($userLang) { |
|
| 147 | - $this->requestLanguage = $userLang; |
|
| 148 | - if ($this->languageExists($app, $userLang)) { |
|
| 149 | - return $userLang; |
|
| 150 | - } |
|
| 151 | - } |
|
| 152 | - |
|
| 153 | - try { |
|
| 154 | - // Try to get the language from the Request |
|
| 155 | - $lang = $this->getLanguageFromRequest($app); |
|
| 156 | - if ($userId !== null && $app === null && !$userLang) { |
|
| 157 | - $this->config->setUserValue($userId, 'core', 'lang', $lang); |
|
| 158 | - } |
|
| 159 | - return $lang; |
|
| 160 | - } catch (LanguageNotFoundException $e) { |
|
| 161 | - // Finding language from request failed fall back to default language |
|
| 162 | - $defaultLanguage = $this->config->getSystemValue('default_language', false); |
|
| 163 | - if ($defaultLanguage !== false && $this->languageExists($app, $defaultLanguage)) { |
|
| 164 | - return $defaultLanguage; |
|
| 165 | - } |
|
| 166 | - } |
|
| 167 | - |
|
| 168 | - // We could not find any language so fall back to english |
|
| 169 | - return 'en'; |
|
| 170 | - } |
|
| 171 | - |
|
| 172 | - /** |
|
| 173 | - * Find all available languages for an app |
|
| 174 | - * |
|
| 175 | - * @param string|null $app App id or null for core |
|
| 176 | - * @return array an array of available languages |
|
| 177 | - */ |
|
| 178 | - public function findAvailableLanguages($app = null) { |
|
| 179 | - $key = $app; |
|
| 180 | - if ($key === null) { |
|
| 181 | - $key = 'null'; |
|
| 182 | - } |
|
| 183 | - |
|
| 184 | - // also works with null as key |
|
| 185 | - if (!empty($this->availableLanguages[$key])) { |
|
| 186 | - return $this->availableLanguages[$key]; |
|
| 187 | - } |
|
| 188 | - |
|
| 189 | - $available = ['en']; //english is always available |
|
| 190 | - $dir = $this->findL10nDir($app); |
|
| 191 | - if (is_dir($dir)) { |
|
| 192 | - $files = scandir($dir); |
|
| 193 | - if ($files !== false) { |
|
| 194 | - foreach ($files as $file) { |
|
| 195 | - if (substr($file, -5) === '.json' && substr($file, 0, 4) !== 'l10n') { |
|
| 196 | - $available[] = substr($file, 0, -5); |
|
| 197 | - } |
|
| 198 | - } |
|
| 199 | - } |
|
| 200 | - } |
|
| 201 | - |
|
| 202 | - // merge with translations from theme |
|
| 203 | - $theme = $this->config->getSystemValue('theme'); |
|
| 204 | - if (!empty($theme)) { |
|
| 205 | - $themeDir = $this->serverRoot . '/themes/' . $theme . substr($dir, strlen($this->serverRoot)); |
|
| 206 | - |
|
| 207 | - if (is_dir($themeDir)) { |
|
| 208 | - $files = scandir($themeDir); |
|
| 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 | - |
|
| 219 | - $this->availableLanguages[$key] = $available; |
|
| 220 | - return $available; |
|
| 221 | - } |
|
| 222 | - |
|
| 223 | - /** |
|
| 224 | - * @param string|null $app App id or null for core |
|
| 225 | - * @param string $lang |
|
| 226 | - * @return bool |
|
| 227 | - */ |
|
| 228 | - public function languageExists($app, $lang) { |
|
| 229 | - if ($lang === 'en') {//english is always available |
|
| 230 | - return true; |
|
| 231 | - } |
|
| 232 | - |
|
| 233 | - $languages = $this->findAvailableLanguages($app); |
|
| 234 | - return array_search($lang, $languages) !== false; |
|
| 235 | - } |
|
| 236 | - |
|
| 237 | - /** |
|
| 238 | - * @param string|null $app |
|
| 239 | - * @return string |
|
| 240 | - * @throws LanguageNotFoundException |
|
| 241 | - */ |
|
| 242 | - private function getLanguageFromRequest($app) { |
|
| 243 | - $header = $this->request->getHeader('ACCEPT_LANGUAGE'); |
|
| 244 | - if ($header) { |
|
| 245 | - $available = $this->findAvailableLanguages($app); |
|
| 246 | - |
|
| 247 | - // E.g. make sure that 'de' is before 'de_DE'. |
|
| 248 | - sort($available); |
|
| 249 | - |
|
| 250 | - $preferences = preg_split('/,\s*/', strtolower($header)); |
|
| 251 | - foreach ($preferences as $preference) { |
|
| 252 | - list($preferred_language) = explode(';', $preference); |
|
| 253 | - $preferred_language = str_replace('-', '_', $preferred_language); |
|
| 254 | - |
|
| 255 | - foreach ($available as $available_language) { |
|
| 256 | - if ($preferred_language === strtolower($available_language)) { |
|
| 257 | - return $available_language; |
|
| 258 | - } |
|
| 259 | - } |
|
| 260 | - |
|
| 261 | - // Fallback from de_De to de |
|
| 262 | - foreach ($available as $available_language) { |
|
| 263 | - if (substr($preferred_language, 0, 2) === $available_language) { |
|
| 264 | - return $available_language; |
|
| 265 | - } |
|
| 266 | - } |
|
| 267 | - } |
|
| 268 | - } |
|
| 269 | - |
|
| 270 | - throw new LanguageNotFoundException(); |
|
| 271 | - } |
|
| 272 | - |
|
| 273 | - /** |
|
| 274 | - * @param string|null $app App id or null for core |
|
| 275 | - * @return string |
|
| 276 | - */ |
|
| 277 | - public function setLanguageFromRequest($app = null) { |
|
| 278 | - |
|
| 279 | - try { |
|
| 280 | - $requestLanguage = $this->getLanguageFromRequest($app); |
|
| 281 | - } catch (LanguageNotFoundException $e) { |
|
| 282 | - $requestLanguage = 'en'; |
|
| 283 | - } |
|
| 284 | - |
|
| 285 | - if ($app === null && !$this->requestLanguage) { |
|
| 286 | - $this->requestLanguage = $requestLanguage; |
|
| 287 | - } |
|
| 288 | - return $requestLanguage; |
|
| 289 | - } |
|
| 290 | - |
|
| 291 | - /** |
|
| 292 | - * Checks if $sub is a subdirectory of $parent |
|
| 293 | - * |
|
| 294 | - * @param string $sub |
|
| 295 | - * @param string $parent |
|
| 296 | - * @return bool |
|
| 297 | - */ |
|
| 298 | - private function isSubDirectory($sub, $parent) { |
|
| 299 | - // Check whether $sub contains no ".." |
|
| 300 | - if(strpos($sub, '..') !== false) { |
|
| 301 | - return false; |
|
| 302 | - } |
|
| 303 | - |
|
| 304 | - // Check whether $sub is a subdirectory of $parent |
|
| 305 | - if (strpos($sub, $parent) === 0) { |
|
| 306 | - return true; |
|
| 307 | - } |
|
| 308 | - |
|
| 309 | - return false; |
|
| 310 | - } |
|
| 311 | - |
|
| 312 | - /** |
|
| 313 | - * Get a list of language files that should be loaded |
|
| 314 | - * |
|
| 315 | - * @param string $app |
|
| 316 | - * @param string $lang |
|
| 317 | - * @return string[] |
|
| 318 | - */ |
|
| 319 | - // FIXME This method is only public, until OC_L10N does not need it anymore, |
|
| 320 | - // FIXME This is also the reason, why it is not in the public interface |
|
| 321 | - public function getL10nFilesForApp($app, $lang) { |
|
| 322 | - $languageFiles = []; |
|
| 323 | - |
|
| 324 | - $i18nDir = $this->findL10nDir($app); |
|
| 325 | - $transFile = strip_tags($i18nDir) . strip_tags($lang) . '.json'; |
|
| 326 | - |
|
| 327 | - if (($this->isSubDirectory($transFile, $this->serverRoot . '/core/l10n/') |
|
| 328 | - || $this->isSubDirectory($transFile, $this->serverRoot . '/lib/l10n/') |
|
| 329 | - || $this->isSubDirectory($transFile, $this->serverRoot . '/settings/l10n/') |
|
| 330 | - || $this->isSubDirectory($transFile, \OC_App::getAppPath($app) . '/l10n/') |
|
| 331 | - ) |
|
| 332 | - && file_exists($transFile)) { |
|
| 333 | - // load the translations file |
|
| 334 | - $languageFiles[] = $transFile; |
|
| 335 | - } |
|
| 336 | - |
|
| 337 | - // merge with translations from theme |
|
| 338 | - $theme = $this->config->getSystemValue('theme'); |
|
| 339 | - if (!empty($theme)) { |
|
| 340 | - $transFile = $this->serverRoot . '/themes/' . $theme . substr($transFile, strlen($this->serverRoot)); |
|
| 341 | - if (file_exists($transFile)) { |
|
| 342 | - $languageFiles[] = $transFile; |
|
| 343 | - } |
|
| 344 | - } |
|
| 345 | - |
|
| 346 | - return $languageFiles; |
|
| 347 | - } |
|
| 348 | - |
|
| 349 | - /** |
|
| 350 | - * find the l10n directory |
|
| 351 | - * |
|
| 352 | - * @param string $app App id or empty string for core |
|
| 353 | - * @return string directory |
|
| 354 | - */ |
|
| 355 | - protected function findL10nDir($app = null) { |
|
| 356 | - if (in_array($app, ['core', 'lib', 'settings'])) { |
|
| 357 | - if (file_exists($this->serverRoot . '/' . $app . '/l10n/')) { |
|
| 358 | - return $this->serverRoot . '/' . $app . '/l10n/'; |
|
| 359 | - } |
|
| 360 | - } else if ($app && \OC_App::getAppPath($app) !== false) { |
|
| 361 | - // Check if the app is in the app folder |
|
| 362 | - return \OC_App::getAppPath($app) . '/l10n/'; |
|
| 363 | - } |
|
| 364 | - return $this->serverRoot . '/core/l10n/'; |
|
| 365 | - } |
|
| 366 | - |
|
| 367 | - |
|
| 368 | - /** |
|
| 369 | - * Creates a function from the plural string |
|
| 370 | - * |
|
| 371 | - * Parts of the code is copied from Habari: |
|
| 372 | - * https://github.com/habari/system/blob/master/classes/locale.php |
|
| 373 | - * @param string $string |
|
| 374 | - * @return string |
|
| 375 | - */ |
|
| 376 | - public function createPluralFunction($string) { |
|
| 377 | - if (isset($this->pluralFunctions[$string])) { |
|
| 378 | - return $this->pluralFunctions[$string]; |
|
| 379 | - } |
|
| 380 | - |
|
| 381 | - if (preg_match( '/^\s*nplurals\s*=\s*(\d+)\s*;\s*plural=(.*)$/u', $string, $matches)) { |
|
| 382 | - // sanitize |
|
| 383 | - $nplurals = preg_replace( '/[^0-9]/', '', $matches[1] ); |
|
| 384 | - $plural = preg_replace( '#[^n0-9:\(\)\?\|\&=!<>+*/\%-]#', '', $matches[2] ); |
|
| 385 | - |
|
| 386 | - $body = str_replace( |
|
| 387 | - array( 'plural', 'n', '$n$plurals', ), |
|
| 388 | - array( '$plural', '$n', '$nplurals', ), |
|
| 389 | - 'nplurals='. $nplurals . '; plural=' . $plural |
|
| 390 | - ); |
|
| 391 | - |
|
| 392 | - // add parents |
|
| 393 | - // important since PHP's ternary evaluates from left to right |
|
| 394 | - $body .= ';'; |
|
| 395 | - $res = ''; |
|
| 396 | - $p = 0; |
|
| 397 | - for($i = 0; $i < strlen($body); $i++) { |
|
| 398 | - $ch = $body[$i]; |
|
| 399 | - switch ( $ch ) { |
|
| 400 | - case '?': |
|
| 401 | - $res .= ' ? ('; |
|
| 402 | - $p++; |
|
| 403 | - break; |
|
| 404 | - case ':': |
|
| 405 | - $res .= ') : ('; |
|
| 406 | - break; |
|
| 407 | - case ';': |
|
| 408 | - $res .= str_repeat( ')', $p ) . ';'; |
|
| 409 | - $p = 0; |
|
| 410 | - break; |
|
| 411 | - default: |
|
| 412 | - $res .= $ch; |
|
| 413 | - } |
|
| 414 | - } |
|
| 415 | - |
|
| 416 | - $body = $res . 'return ($plural>=$nplurals?$nplurals-1:$plural);'; |
|
| 417 | - $function = create_function('$n', $body); |
|
| 418 | - $this->pluralFunctions[$string] = $function; |
|
| 419 | - return $function; |
|
| 420 | - } else { |
|
| 421 | - // default: one plural form for all cases but n==1 (english) |
|
| 422 | - $function = create_function( |
|
| 423 | - '$n', |
|
| 424 | - '$nplurals=2;$plural=($n==1?0:1);return ($plural>=$nplurals?$nplurals-1:$plural);' |
|
| 425 | - ); |
|
| 426 | - $this->pluralFunctions[$string] = $function; |
|
| 427 | - return $function; |
|
| 428 | - } |
|
| 429 | - } |
|
| 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 | + /** @var IConfig */ |
|
| 63 | + protected $config; |
|
| 64 | + |
|
| 65 | + /** @var IRequest */ |
|
| 66 | + protected $request; |
|
| 67 | + |
|
| 68 | + /** @var IUserSession */ |
|
| 69 | + protected $userSession; |
|
| 70 | + |
|
| 71 | + /** @var string */ |
|
| 72 | + protected $serverRoot; |
|
| 73 | + |
|
| 74 | + /** |
|
| 75 | + * @param IConfig $config |
|
| 76 | + * @param IRequest $request |
|
| 77 | + * @param IUserSession $userSession |
|
| 78 | + * @param string $serverRoot |
|
| 79 | + */ |
|
| 80 | + public function __construct(IConfig $config, |
|
| 81 | + IRequest $request, |
|
| 82 | + IUserSession $userSession, |
|
| 83 | + $serverRoot) { |
|
| 84 | + $this->config = $config; |
|
| 85 | + $this->request = $request; |
|
| 86 | + $this->userSession = $userSession; |
|
| 87 | + $this->serverRoot = $serverRoot; |
|
| 88 | + } |
|
| 89 | + |
|
| 90 | + /** |
|
| 91 | + * Get a language instance |
|
| 92 | + * |
|
| 93 | + * @param string $app |
|
| 94 | + * @param string|null $lang |
|
| 95 | + * @return \OCP\IL10N |
|
| 96 | + */ |
|
| 97 | + public function get($app, $lang = null) { |
|
| 98 | + $app = \OC_App::cleanAppId($app); |
|
| 99 | + if ($lang !== null) { |
|
| 100 | + $lang = str_replace(array('\0', '/', '\\', '..'), '', (string) $lang); |
|
| 101 | + } |
|
| 102 | + if ($lang === null || !$this->languageExists($app, $lang)) { |
|
| 103 | + $lang = $this->findLanguage($app); |
|
| 104 | + } |
|
| 105 | + |
|
| 106 | + if (!isset($this->instances[$lang][$app])) { |
|
| 107 | + $this->instances[$lang][$app] = new L10N( |
|
| 108 | + $this, $app, $lang, |
|
| 109 | + $this->getL10nFilesForApp($app, $lang) |
|
| 110 | + ); |
|
| 111 | + } |
|
| 112 | + |
|
| 113 | + return $this->instances[$lang][$app]; |
|
| 114 | + } |
|
| 115 | + |
|
| 116 | + /** |
|
| 117 | + * Find the best language |
|
| 118 | + * |
|
| 119 | + * @param string|null $app App id or null for core |
|
| 120 | + * @return string language If nothing works it returns 'en' |
|
| 121 | + */ |
|
| 122 | + public function findLanguage($app = null) { |
|
| 123 | + if ($this->requestLanguage !== '' && $this->languageExists($app, $this->requestLanguage)) { |
|
| 124 | + return $this->requestLanguage; |
|
| 125 | + } |
|
| 126 | + |
|
| 127 | + /** |
|
| 128 | + * At this point ownCloud might not yet be installed and thus the lookup |
|
| 129 | + * in the preferences table might fail. For this reason we need to check |
|
| 130 | + * whether the instance has already been installed |
|
| 131 | + * |
|
| 132 | + * @link https://github.com/owncloud/core/issues/21955 |
|
| 133 | + */ |
|
| 134 | + if($this->config->getSystemValue('installed', false)) { |
|
| 135 | + $userId = !is_null($this->userSession->getUser()) ? $this->userSession->getUser()->getUID() : null; |
|
| 136 | + if(!is_null($userId)) { |
|
| 137 | + $userLang = $this->config->getUserValue($userId, 'core', 'lang', null); |
|
| 138 | + } else { |
|
| 139 | + $userLang = null; |
|
| 140 | + } |
|
| 141 | + } else { |
|
| 142 | + $userId = null; |
|
| 143 | + $userLang = null; |
|
| 144 | + } |
|
| 145 | + |
|
| 146 | + if ($userLang) { |
|
| 147 | + $this->requestLanguage = $userLang; |
|
| 148 | + if ($this->languageExists($app, $userLang)) { |
|
| 149 | + return $userLang; |
|
| 150 | + } |
|
| 151 | + } |
|
| 152 | + |
|
| 153 | + try { |
|
| 154 | + // Try to get the language from the Request |
|
| 155 | + $lang = $this->getLanguageFromRequest($app); |
|
| 156 | + if ($userId !== null && $app === null && !$userLang) { |
|
| 157 | + $this->config->setUserValue($userId, 'core', 'lang', $lang); |
|
| 158 | + } |
|
| 159 | + return $lang; |
|
| 160 | + } catch (LanguageNotFoundException $e) { |
|
| 161 | + // Finding language from request failed fall back to default language |
|
| 162 | + $defaultLanguage = $this->config->getSystemValue('default_language', false); |
|
| 163 | + if ($defaultLanguage !== false && $this->languageExists($app, $defaultLanguage)) { |
|
| 164 | + return $defaultLanguage; |
|
| 165 | + } |
|
| 166 | + } |
|
| 167 | + |
|
| 168 | + // We could not find any language so fall back to english |
|
| 169 | + return 'en'; |
|
| 170 | + } |
|
| 171 | + |
|
| 172 | + /** |
|
| 173 | + * Find all available languages for an app |
|
| 174 | + * |
|
| 175 | + * @param string|null $app App id or null for core |
|
| 176 | + * @return array an array of available languages |
|
| 177 | + */ |
|
| 178 | + public function findAvailableLanguages($app = null) { |
|
| 179 | + $key = $app; |
|
| 180 | + if ($key === null) { |
|
| 181 | + $key = 'null'; |
|
| 182 | + } |
|
| 183 | + |
|
| 184 | + // also works with null as key |
|
| 185 | + if (!empty($this->availableLanguages[$key])) { |
|
| 186 | + return $this->availableLanguages[$key]; |
|
| 187 | + } |
|
| 188 | + |
|
| 189 | + $available = ['en']; //english is always available |
|
| 190 | + $dir = $this->findL10nDir($app); |
|
| 191 | + if (is_dir($dir)) { |
|
| 192 | + $files = scandir($dir); |
|
| 193 | + if ($files !== false) { |
|
| 194 | + foreach ($files as $file) { |
|
| 195 | + if (substr($file, -5) === '.json' && substr($file, 0, 4) !== 'l10n') { |
|
| 196 | + $available[] = substr($file, 0, -5); |
|
| 197 | + } |
|
| 198 | + } |
|
| 199 | + } |
|
| 200 | + } |
|
| 201 | + |
|
| 202 | + // merge with translations from theme |
|
| 203 | + $theme = $this->config->getSystemValue('theme'); |
|
| 204 | + if (!empty($theme)) { |
|
| 205 | + $themeDir = $this->serverRoot . '/themes/' . $theme . substr($dir, strlen($this->serverRoot)); |
|
| 206 | + |
|
| 207 | + if (is_dir($themeDir)) { |
|
| 208 | + $files = scandir($themeDir); |
|
| 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 | + |
|
| 219 | + $this->availableLanguages[$key] = $available; |
|
| 220 | + return $available; |
|
| 221 | + } |
|
| 222 | + |
|
| 223 | + /** |
|
| 224 | + * @param string|null $app App id or null for core |
|
| 225 | + * @param string $lang |
|
| 226 | + * @return bool |
|
| 227 | + */ |
|
| 228 | + public function languageExists($app, $lang) { |
|
| 229 | + if ($lang === 'en') {//english is always available |
|
| 230 | + return true; |
|
| 231 | + } |
|
| 232 | + |
|
| 233 | + $languages = $this->findAvailableLanguages($app); |
|
| 234 | + return array_search($lang, $languages) !== false; |
|
| 235 | + } |
|
| 236 | + |
|
| 237 | + /** |
|
| 238 | + * @param string|null $app |
|
| 239 | + * @return string |
|
| 240 | + * @throws LanguageNotFoundException |
|
| 241 | + */ |
|
| 242 | + private function getLanguageFromRequest($app) { |
|
| 243 | + $header = $this->request->getHeader('ACCEPT_LANGUAGE'); |
|
| 244 | + if ($header) { |
|
| 245 | + $available = $this->findAvailableLanguages($app); |
|
| 246 | + |
|
| 247 | + // E.g. make sure that 'de' is before 'de_DE'. |
|
| 248 | + sort($available); |
|
| 249 | + |
|
| 250 | + $preferences = preg_split('/,\s*/', strtolower($header)); |
|
| 251 | + foreach ($preferences as $preference) { |
|
| 252 | + list($preferred_language) = explode(';', $preference); |
|
| 253 | + $preferred_language = str_replace('-', '_', $preferred_language); |
|
| 254 | + |
|
| 255 | + foreach ($available as $available_language) { |
|
| 256 | + if ($preferred_language === strtolower($available_language)) { |
|
| 257 | + return $available_language; |
|
| 258 | + } |
|
| 259 | + } |
|
| 260 | + |
|
| 261 | + // Fallback from de_De to de |
|
| 262 | + foreach ($available as $available_language) { |
|
| 263 | + if (substr($preferred_language, 0, 2) === $available_language) { |
|
| 264 | + return $available_language; |
|
| 265 | + } |
|
| 266 | + } |
|
| 267 | + } |
|
| 268 | + } |
|
| 269 | + |
|
| 270 | + throw new LanguageNotFoundException(); |
|
| 271 | + } |
|
| 272 | + |
|
| 273 | + /** |
|
| 274 | + * @param string|null $app App id or null for core |
|
| 275 | + * @return string |
|
| 276 | + */ |
|
| 277 | + public function setLanguageFromRequest($app = null) { |
|
| 278 | + |
|
| 279 | + try { |
|
| 280 | + $requestLanguage = $this->getLanguageFromRequest($app); |
|
| 281 | + } catch (LanguageNotFoundException $e) { |
|
| 282 | + $requestLanguage = 'en'; |
|
| 283 | + } |
|
| 284 | + |
|
| 285 | + if ($app === null && !$this->requestLanguage) { |
|
| 286 | + $this->requestLanguage = $requestLanguage; |
|
| 287 | + } |
|
| 288 | + return $requestLanguage; |
|
| 289 | + } |
|
| 290 | + |
|
| 291 | + /** |
|
| 292 | + * Checks if $sub is a subdirectory of $parent |
|
| 293 | + * |
|
| 294 | + * @param string $sub |
|
| 295 | + * @param string $parent |
|
| 296 | + * @return bool |
|
| 297 | + */ |
|
| 298 | + private function isSubDirectory($sub, $parent) { |
|
| 299 | + // Check whether $sub contains no ".." |
|
| 300 | + if(strpos($sub, '..') !== false) { |
|
| 301 | + return false; |
|
| 302 | + } |
|
| 303 | + |
|
| 304 | + // Check whether $sub is a subdirectory of $parent |
|
| 305 | + if (strpos($sub, $parent) === 0) { |
|
| 306 | + return true; |
|
| 307 | + } |
|
| 308 | + |
|
| 309 | + return false; |
|
| 310 | + } |
|
| 311 | + |
|
| 312 | + /** |
|
| 313 | + * Get a list of language files that should be loaded |
|
| 314 | + * |
|
| 315 | + * @param string $app |
|
| 316 | + * @param string $lang |
|
| 317 | + * @return string[] |
|
| 318 | + */ |
|
| 319 | + // FIXME This method is only public, until OC_L10N does not need it anymore, |
|
| 320 | + // FIXME This is also the reason, why it is not in the public interface |
|
| 321 | + public function getL10nFilesForApp($app, $lang) { |
|
| 322 | + $languageFiles = []; |
|
| 323 | + |
|
| 324 | + $i18nDir = $this->findL10nDir($app); |
|
| 325 | + $transFile = strip_tags($i18nDir) . strip_tags($lang) . '.json'; |
|
| 326 | + |
|
| 327 | + if (($this->isSubDirectory($transFile, $this->serverRoot . '/core/l10n/') |
|
| 328 | + || $this->isSubDirectory($transFile, $this->serverRoot . '/lib/l10n/') |
|
| 329 | + || $this->isSubDirectory($transFile, $this->serverRoot . '/settings/l10n/') |
|
| 330 | + || $this->isSubDirectory($transFile, \OC_App::getAppPath($app) . '/l10n/') |
|
| 331 | + ) |
|
| 332 | + && file_exists($transFile)) { |
|
| 333 | + // load the translations file |
|
| 334 | + $languageFiles[] = $transFile; |
|
| 335 | + } |
|
| 336 | + |
|
| 337 | + // merge with translations from theme |
|
| 338 | + $theme = $this->config->getSystemValue('theme'); |
|
| 339 | + if (!empty($theme)) { |
|
| 340 | + $transFile = $this->serverRoot . '/themes/' . $theme . substr($transFile, strlen($this->serverRoot)); |
|
| 341 | + if (file_exists($transFile)) { |
|
| 342 | + $languageFiles[] = $transFile; |
|
| 343 | + } |
|
| 344 | + } |
|
| 345 | + |
|
| 346 | + return $languageFiles; |
|
| 347 | + } |
|
| 348 | + |
|
| 349 | + /** |
|
| 350 | + * find the l10n directory |
|
| 351 | + * |
|
| 352 | + * @param string $app App id or empty string for core |
|
| 353 | + * @return string directory |
|
| 354 | + */ |
|
| 355 | + protected function findL10nDir($app = null) { |
|
| 356 | + if (in_array($app, ['core', 'lib', 'settings'])) { |
|
| 357 | + if (file_exists($this->serverRoot . '/' . $app . '/l10n/')) { |
|
| 358 | + return $this->serverRoot . '/' . $app . '/l10n/'; |
|
| 359 | + } |
|
| 360 | + } else if ($app && \OC_App::getAppPath($app) !== false) { |
|
| 361 | + // Check if the app is in the app folder |
|
| 362 | + return \OC_App::getAppPath($app) . '/l10n/'; |
|
| 363 | + } |
|
| 364 | + return $this->serverRoot . '/core/l10n/'; |
|
| 365 | + } |
|
| 366 | + |
|
| 367 | + |
|
| 368 | + /** |
|
| 369 | + * Creates a function from the plural string |
|
| 370 | + * |
|
| 371 | + * Parts of the code is copied from Habari: |
|
| 372 | + * https://github.com/habari/system/blob/master/classes/locale.php |
|
| 373 | + * @param string $string |
|
| 374 | + * @return string |
|
| 375 | + */ |
|
| 376 | + public function createPluralFunction($string) { |
|
| 377 | + if (isset($this->pluralFunctions[$string])) { |
|
| 378 | + return $this->pluralFunctions[$string]; |
|
| 379 | + } |
|
| 380 | + |
|
| 381 | + if (preg_match( '/^\s*nplurals\s*=\s*(\d+)\s*;\s*plural=(.*)$/u', $string, $matches)) { |
|
| 382 | + // sanitize |
|
| 383 | + $nplurals = preg_replace( '/[^0-9]/', '', $matches[1] ); |
|
| 384 | + $plural = preg_replace( '#[^n0-9:\(\)\?\|\&=!<>+*/\%-]#', '', $matches[2] ); |
|
| 385 | + |
|
| 386 | + $body = str_replace( |
|
| 387 | + array( 'plural', 'n', '$n$plurals', ), |
|
| 388 | + array( '$plural', '$n', '$nplurals', ), |
|
| 389 | + 'nplurals='. $nplurals . '; plural=' . $plural |
|
| 390 | + ); |
|
| 391 | + |
|
| 392 | + // add parents |
|
| 393 | + // important since PHP's ternary evaluates from left to right |
|
| 394 | + $body .= ';'; |
|
| 395 | + $res = ''; |
|
| 396 | + $p = 0; |
|
| 397 | + for($i = 0; $i < strlen($body); $i++) { |
|
| 398 | + $ch = $body[$i]; |
|
| 399 | + switch ( $ch ) { |
|
| 400 | + case '?': |
|
| 401 | + $res .= ' ? ('; |
|
| 402 | + $p++; |
|
| 403 | + break; |
|
| 404 | + case ':': |
|
| 405 | + $res .= ') : ('; |
|
| 406 | + break; |
|
| 407 | + case ';': |
|
| 408 | + $res .= str_repeat( ')', $p ) . ';'; |
|
| 409 | + $p = 0; |
|
| 410 | + break; |
|
| 411 | + default: |
|
| 412 | + $res .= $ch; |
|
| 413 | + } |
|
| 414 | + } |
|
| 415 | + |
|
| 416 | + $body = $res . 'return ($plural>=$nplurals?$nplurals-1:$plural);'; |
|
| 417 | + $function = create_function('$n', $body); |
|
| 418 | + $this->pluralFunctions[$string] = $function; |
|
| 419 | + return $function; |
|
| 420 | + } else { |
|
| 421 | + // default: one plural form for all cases but n==1 (english) |
|
| 422 | + $function = create_function( |
|
| 423 | + '$n', |
|
| 424 | + '$nplurals=2;$plural=($n==1?0:1);return ($plural>=$nplurals?$nplurals-1:$plural);' |
|
| 425 | + ); |
|
| 426 | + $this->pluralFunctions[$string] = $function; |
|
| 427 | + return $function; |
|
| 428 | + } |
|
| 429 | + } |
|
| 430 | 430 | } |
@@ -131,9 +131,9 @@ discard block |
||
| 131 | 131 | * |
| 132 | 132 | * @link https://github.com/owncloud/core/issues/21955 |
| 133 | 133 | */ |
| 134 | - if($this->config->getSystemValue('installed', false)) { |
|
| 135 | - $userId = !is_null($this->userSession->getUser()) ? $this->userSession->getUser()->getUID() : null; |
|
| 136 | - if(!is_null($userId)) { |
|
| 134 | + if ($this->config->getSystemValue('installed', false)) { |
|
| 135 | + $userId = !is_null($this->userSession->getUser()) ? $this->userSession->getUser()->getUID() : null; |
|
| 136 | + if (!is_null($userId)) { |
|
| 137 | 137 | $userLang = $this->config->getUserValue($userId, 'core', 'lang', null); |
| 138 | 138 | } else { |
| 139 | 139 | $userLang = null; |
@@ -202,7 +202,7 @@ discard block |
||
| 202 | 202 | // merge with translations from theme |
| 203 | 203 | $theme = $this->config->getSystemValue('theme'); |
| 204 | 204 | if (!empty($theme)) { |
| 205 | - $themeDir = $this->serverRoot . '/themes/' . $theme . substr($dir, strlen($this->serverRoot)); |
|
| 205 | + $themeDir = $this->serverRoot.'/themes/'.$theme.substr($dir, strlen($this->serverRoot)); |
|
| 206 | 206 | |
| 207 | 207 | if (is_dir($themeDir)) { |
| 208 | 208 | $files = scandir($themeDir); |
@@ -297,7 +297,7 @@ discard block |
||
| 297 | 297 | */ |
| 298 | 298 | private function isSubDirectory($sub, $parent) { |
| 299 | 299 | // Check whether $sub contains no ".." |
| 300 | - if(strpos($sub, '..') !== false) { |
|
| 300 | + if (strpos($sub, '..') !== false) { |
|
| 301 | 301 | return false; |
| 302 | 302 | } |
| 303 | 303 | |
@@ -322,12 +322,12 @@ discard block |
||
| 322 | 322 | $languageFiles = []; |
| 323 | 323 | |
| 324 | 324 | $i18nDir = $this->findL10nDir($app); |
| 325 | - $transFile = strip_tags($i18nDir) . strip_tags($lang) . '.json'; |
|
| 325 | + $transFile = strip_tags($i18nDir).strip_tags($lang).'.json'; |
|
| 326 | 326 | |
| 327 | - if (($this->isSubDirectory($transFile, $this->serverRoot . '/core/l10n/') |
|
| 328 | - || $this->isSubDirectory($transFile, $this->serverRoot . '/lib/l10n/') |
|
| 329 | - || $this->isSubDirectory($transFile, $this->serverRoot . '/settings/l10n/') |
|
| 330 | - || $this->isSubDirectory($transFile, \OC_App::getAppPath($app) . '/l10n/') |
|
| 327 | + if (($this->isSubDirectory($transFile, $this->serverRoot.'/core/l10n/') |
|
| 328 | + || $this->isSubDirectory($transFile, $this->serverRoot.'/lib/l10n/') |
|
| 329 | + || $this->isSubDirectory($transFile, $this->serverRoot.'/settings/l10n/') |
|
| 330 | + || $this->isSubDirectory($transFile, \OC_App::getAppPath($app).'/l10n/') |
|
| 331 | 331 | ) |
| 332 | 332 | && file_exists($transFile)) { |
| 333 | 333 | // load the translations file |
@@ -337,7 +337,7 @@ discard block |
||
| 337 | 337 | // merge with translations from theme |
| 338 | 338 | $theme = $this->config->getSystemValue('theme'); |
| 339 | 339 | if (!empty($theme)) { |
| 340 | - $transFile = $this->serverRoot . '/themes/' . $theme . substr($transFile, strlen($this->serverRoot)); |
|
| 340 | + $transFile = $this->serverRoot.'/themes/'.$theme.substr($transFile, strlen($this->serverRoot)); |
|
| 341 | 341 | if (file_exists($transFile)) { |
| 342 | 342 | $languageFiles[] = $transFile; |
| 343 | 343 | } |
@@ -354,14 +354,14 @@ discard block |
||
| 354 | 354 | */ |
| 355 | 355 | protected function findL10nDir($app = null) { |
| 356 | 356 | if (in_array($app, ['core', 'lib', 'settings'])) { |
| 357 | - if (file_exists($this->serverRoot . '/' . $app . '/l10n/')) { |
|
| 358 | - return $this->serverRoot . '/' . $app . '/l10n/'; |
|
| 357 | + if (file_exists($this->serverRoot.'/'.$app.'/l10n/')) { |
|
| 358 | + return $this->serverRoot.'/'.$app.'/l10n/'; |
|
| 359 | 359 | } |
| 360 | 360 | } else if ($app && \OC_App::getAppPath($app) !== false) { |
| 361 | 361 | // Check if the app is in the app folder |
| 362 | - return \OC_App::getAppPath($app) . '/l10n/'; |
|
| 362 | + return \OC_App::getAppPath($app).'/l10n/'; |
|
| 363 | 363 | } |
| 364 | - return $this->serverRoot . '/core/l10n/'; |
|
| 364 | + return $this->serverRoot.'/core/l10n/'; |
|
| 365 | 365 | } |
| 366 | 366 | |
| 367 | 367 | |
@@ -378,15 +378,15 @@ discard block |
||
| 378 | 378 | return $this->pluralFunctions[$string]; |
| 379 | 379 | } |
| 380 | 380 | |
| 381 | - if (preg_match( '/^\s*nplurals\s*=\s*(\d+)\s*;\s*plural=(.*)$/u', $string, $matches)) { |
|
| 381 | + if (preg_match('/^\s*nplurals\s*=\s*(\d+)\s*;\s*plural=(.*)$/u', $string, $matches)) { |
|
| 382 | 382 | // sanitize |
| 383 | - $nplurals = preg_replace( '/[^0-9]/', '', $matches[1] ); |
|
| 384 | - $plural = preg_replace( '#[^n0-9:\(\)\?\|\&=!<>+*/\%-]#', '', $matches[2] ); |
|
| 383 | + $nplurals = preg_replace('/[^0-9]/', '', $matches[1]); |
|
| 384 | + $plural = preg_replace('#[^n0-9:\(\)\?\|\&=!<>+*/\%-]#', '', $matches[2]); |
|
| 385 | 385 | |
| 386 | 386 | $body = str_replace( |
| 387 | - array( 'plural', 'n', '$n$plurals', ), |
|
| 388 | - array( '$plural', '$n', '$nplurals', ), |
|
| 389 | - 'nplurals='. $nplurals . '; plural=' . $plural |
|
| 387 | + array('plural', 'n', '$n$plurals',), |
|
| 388 | + array('$plural', '$n', '$nplurals',), |
|
| 389 | + 'nplurals='.$nplurals.'; plural='.$plural |
|
| 390 | 390 | ); |
| 391 | 391 | |
| 392 | 392 | // add parents |
@@ -394,9 +394,9 @@ discard block |
||
| 394 | 394 | $body .= ';'; |
| 395 | 395 | $res = ''; |
| 396 | 396 | $p = 0; |
| 397 | - for($i = 0; $i < strlen($body); $i++) { |
|
| 397 | + for ($i = 0; $i < strlen($body); $i++) { |
|
| 398 | 398 | $ch = $body[$i]; |
| 399 | - switch ( $ch ) { |
|
| 399 | + switch ($ch) { |
|
| 400 | 400 | case '?': |
| 401 | 401 | $res .= ' ? ('; |
| 402 | 402 | $p++; |
@@ -405,7 +405,7 @@ discard block |
||
| 405 | 405 | $res .= ') : ('; |
| 406 | 406 | break; |
| 407 | 407 | case ';': |
| 408 | - $res .= str_repeat( ')', $p ) . ';'; |
|
| 408 | + $res .= str_repeat(')', $p).';'; |
|
| 409 | 409 | $p = 0; |
| 410 | 410 | break; |
| 411 | 411 | default: |
@@ -413,7 +413,7 @@ discard block |
||
| 413 | 413 | } |
| 414 | 414 | } |
| 415 | 415 | |
| 416 | - $body = $res . 'return ($plural>=$nplurals?$nplurals-1:$plural);'; |
|
| 416 | + $body = $res.'return ($plural>=$nplurals?$nplurals-1:$plural);'; |
|
| 417 | 417 | $function = create_function('$n', $body); |
| 418 | 418 | $this->pluralFunctions[$string] = $function; |
| 419 | 419 | return $function; |
@@ -45,7 +45,6 @@ |
||
| 45 | 45 | use OC\App\CodeChecker\CodeChecker; |
| 46 | 46 | use OC\App\CodeChecker\EmptyCheck; |
| 47 | 47 | use OC\App\CodeChecker\PrivateCheck; |
| 48 | -use OC\Archive\Archive; |
|
| 49 | 48 | use OC\Archive\TAR; |
| 50 | 49 | use OC_App; |
| 51 | 50 | use OC_DB; |
@@ -60,493 +60,493 @@ |
||
| 60 | 60 | * This class provides the functionality needed to install, update and remove apps |
| 61 | 61 | */ |
| 62 | 62 | class Installer { |
| 63 | - /** @var AppFetcher */ |
|
| 64 | - private $appFetcher; |
|
| 65 | - /** @var IClientService */ |
|
| 66 | - private $clientService; |
|
| 67 | - /** @var ITempManager */ |
|
| 68 | - private $tempManager; |
|
| 69 | - /** @var ILogger */ |
|
| 70 | - private $logger; |
|
| 71 | - |
|
| 72 | - /** |
|
| 73 | - * @param AppFetcher $appFetcher |
|
| 74 | - * @param IClientService $clientService |
|
| 75 | - * @param ITempManager $tempManager |
|
| 76 | - * @param ILogger $logger |
|
| 77 | - */ |
|
| 78 | - public function __construct(AppFetcher $appFetcher, |
|
| 79 | - IClientService $clientService, |
|
| 80 | - ITempManager $tempManager, |
|
| 81 | - ILogger $logger) { |
|
| 82 | - $this->appFetcher = $appFetcher; |
|
| 83 | - $this->clientService = $clientService; |
|
| 84 | - $this->tempManager = $tempManager; |
|
| 85 | - $this->logger = $logger; |
|
| 86 | - } |
|
| 87 | - |
|
| 88 | - /** |
|
| 89 | - * Installs an app that is located in one of the app folders already |
|
| 90 | - * |
|
| 91 | - * @param string $appId App to install |
|
| 92 | - * @throws \Exception |
|
| 93 | - * @return integer |
|
| 94 | - */ |
|
| 95 | - public function installApp($appId) { |
|
| 96 | - $app = \OC_App::findAppInDirectories($appId); |
|
| 97 | - if($app === false) { |
|
| 98 | - throw new \Exception('App not found in any app directory'); |
|
| 99 | - } |
|
| 100 | - |
|
| 101 | - $basedir = $app['path'].'/'.$appId; |
|
| 102 | - $info = OC_App::getAppInfo($basedir.'/appinfo/info.xml', true); |
|
| 103 | - |
|
| 104 | - //install the database |
|
| 105 | - if(is_file($basedir.'/appinfo/database.xml')) { |
|
| 106 | - if (\OC::$server->getAppConfig()->getValue($info['id'], 'installed_version') === null) { |
|
| 107 | - OC_DB::createDbFromStructure($basedir.'/appinfo/database.xml'); |
|
| 108 | - } else { |
|
| 109 | - OC_DB::updateDbFromStructure($basedir.'/appinfo/database.xml'); |
|
| 110 | - } |
|
| 111 | - } |
|
| 112 | - |
|
| 113 | - \OC_App::setupBackgroundJobs($info['background-jobs']); |
|
| 114 | - |
|
| 115 | - //run appinfo/install.php |
|
| 116 | - if((!isset($data['noinstall']) or $data['noinstall']==false)) { |
|
| 117 | - self::includeAppScript($basedir . '/appinfo/install.php'); |
|
| 118 | - } |
|
| 119 | - |
|
| 120 | - $appData = OC_App::getAppInfo($appId); |
|
| 121 | - OC_App::executeRepairSteps($appId, $appData['repair-steps']['install']); |
|
| 122 | - |
|
| 123 | - //set the installed version |
|
| 124 | - \OC::$server->getConfig()->setAppValue($info['id'], 'installed_version', OC_App::getAppVersion($info['id'], false)); |
|
| 125 | - \OC::$server->getConfig()->setAppValue($info['id'], 'enabled', 'no'); |
|
| 126 | - |
|
| 127 | - //set remote/public handlers |
|
| 128 | - foreach($info['remote'] as $name=>$path) { |
|
| 129 | - \OC::$server->getConfig()->setAppValue('core', 'remote_'.$name, $info['id'].'/'.$path); |
|
| 130 | - } |
|
| 131 | - foreach($info['public'] as $name=>$path) { |
|
| 132 | - \OC::$server->getConfig()->setAppValue('core', 'public_'.$name, $info['id'].'/'.$path); |
|
| 133 | - } |
|
| 134 | - |
|
| 135 | - OC_App::setAppTypes($info['id']); |
|
| 136 | - |
|
| 137 | - return $info['id']; |
|
| 138 | - } |
|
| 139 | - |
|
| 140 | - /** |
|
| 141 | - * @brief checks whether or not an app is installed |
|
| 142 | - * @param string $app app |
|
| 143 | - * @returns bool |
|
| 144 | - * |
|
| 145 | - * Checks whether or not an app is installed, i.e. registered in apps table. |
|
| 146 | - */ |
|
| 147 | - public static function isInstalled( $app ) { |
|
| 148 | - return (\OC::$server->getConfig()->getAppValue($app, "installed_version", null) !== null); |
|
| 149 | - } |
|
| 150 | - |
|
| 151 | - /** |
|
| 152 | - * Updates the specified app from the appstore |
|
| 153 | - * |
|
| 154 | - * @param string $appId |
|
| 155 | - * @return bool |
|
| 156 | - */ |
|
| 157 | - public function updateAppstoreApp($appId) { |
|
| 158 | - if(self::isUpdateAvailable($appId, $this->appFetcher)) { |
|
| 159 | - try { |
|
| 160 | - $this->downloadApp($appId); |
|
| 161 | - } catch (\Exception $e) { |
|
| 162 | - $this->logger->error($e->getMessage(), ['app' => 'core']); |
|
| 163 | - return false; |
|
| 164 | - } |
|
| 165 | - return OC_App::updateApp($appId); |
|
| 166 | - } |
|
| 167 | - |
|
| 168 | - return false; |
|
| 169 | - } |
|
| 170 | - |
|
| 171 | - /** |
|
| 172 | - * Downloads an app and puts it into the app directory |
|
| 173 | - * |
|
| 174 | - * @param string $appId |
|
| 175 | - * |
|
| 176 | - * @throws \Exception If the installation was not successful |
|
| 177 | - */ |
|
| 178 | - public function downloadApp($appId) { |
|
| 179 | - $appId = strtolower($appId); |
|
| 180 | - |
|
| 181 | - $apps = $this->appFetcher->get(); |
|
| 182 | - foreach($apps as $app) { |
|
| 183 | - if($app['id'] === $appId) { |
|
| 184 | - // Load the certificate |
|
| 185 | - $certificate = new X509(); |
|
| 186 | - $certificate->loadCA(file_get_contents(__DIR__ . '/../../resources/codesigning/root.crt')); |
|
| 187 | - $loadedCertificate = $certificate->loadX509($app['certificate']); |
|
| 188 | - |
|
| 189 | - // Verify if the certificate has been revoked |
|
| 190 | - $crl = new X509(); |
|
| 191 | - $crl->loadCA(file_get_contents(__DIR__ . '/../../resources/codesigning/root.crt')); |
|
| 192 | - $crl->loadCRL(file_get_contents(__DIR__ . '/../../resources/codesigning/root.crl')); |
|
| 193 | - if($crl->validateSignature() !== true) { |
|
| 194 | - throw new \Exception('Could not validate CRL signature'); |
|
| 195 | - } |
|
| 196 | - $csn = $loadedCertificate['tbsCertificate']['serialNumber']->toString(); |
|
| 197 | - $revoked = $crl->getRevoked($csn); |
|
| 198 | - if ($revoked !== false) { |
|
| 199 | - throw new \Exception( |
|
| 200 | - sprintf( |
|
| 201 | - 'Certificate "%s" has been revoked', |
|
| 202 | - $csn |
|
| 203 | - ) |
|
| 204 | - ); |
|
| 205 | - } |
|
| 206 | - |
|
| 207 | - // Verify if the certificate has been issued by the Nextcloud Code Authority CA |
|
| 208 | - if($certificate->validateSignature() !== true) { |
|
| 209 | - throw new \Exception( |
|
| 210 | - sprintf( |
|
| 211 | - 'App with id %s has a certificate not issued by a trusted Code Signing Authority', |
|
| 212 | - $appId |
|
| 213 | - ) |
|
| 214 | - ); |
|
| 215 | - } |
|
| 216 | - |
|
| 217 | - // Verify if the certificate is issued for the requested app id |
|
| 218 | - $certInfo = openssl_x509_parse($app['certificate']); |
|
| 219 | - if(!isset($certInfo['subject']['CN'])) { |
|
| 220 | - throw new \Exception( |
|
| 221 | - sprintf( |
|
| 222 | - 'App with id %s has a cert with no CN', |
|
| 223 | - $appId |
|
| 224 | - ) |
|
| 225 | - ); |
|
| 226 | - } |
|
| 227 | - if($certInfo['subject']['CN'] !== $appId) { |
|
| 228 | - throw new \Exception( |
|
| 229 | - sprintf( |
|
| 230 | - 'App with id %s has a cert issued to %s', |
|
| 231 | - $appId, |
|
| 232 | - $certInfo['subject']['CN'] |
|
| 233 | - ) |
|
| 234 | - ); |
|
| 235 | - } |
|
| 236 | - |
|
| 237 | - // Download the release |
|
| 238 | - $tempFile = $this->tempManager->getTemporaryFile('.tar.gz'); |
|
| 239 | - $client = $this->clientService->newClient(); |
|
| 240 | - $client->get($app['releases'][0]['download'], ['save_to' => $tempFile]); |
|
| 241 | - |
|
| 242 | - // Check if the signature actually matches the downloaded content |
|
| 243 | - $certificate = openssl_get_publickey($app['certificate']); |
|
| 244 | - $verified = (bool)openssl_verify(file_get_contents($tempFile), base64_decode($app['releases'][0]['signature']), $certificate, OPENSSL_ALGO_SHA512); |
|
| 245 | - openssl_free_key($certificate); |
|
| 246 | - |
|
| 247 | - if($verified === true) { |
|
| 248 | - // Seems to match, let's proceed |
|
| 249 | - $extractDir = $this->tempManager->getTemporaryFolder(); |
|
| 250 | - $archive = new TAR($tempFile); |
|
| 251 | - |
|
| 252 | - if($archive) { |
|
| 253 | - $archive->extract($extractDir); |
|
| 254 | - $allFiles = scandir($extractDir); |
|
| 255 | - $folders = array_diff($allFiles, ['.', '..']); |
|
| 256 | - $folders = array_values($folders); |
|
| 257 | - |
|
| 258 | - if(count($folders) > 1) { |
|
| 259 | - throw new \Exception( |
|
| 260 | - sprintf( |
|
| 261 | - 'Extracted app %s has more than 1 folder', |
|
| 262 | - $appId |
|
| 263 | - ) |
|
| 264 | - ); |
|
| 265 | - } |
|
| 266 | - |
|
| 267 | - // Check if appinfo/info.xml has the same app ID as well |
|
| 268 | - $loadEntities = libxml_disable_entity_loader(false); |
|
| 269 | - $xml = simplexml_load_file($extractDir . '/' . $folders[0] . '/appinfo/info.xml'); |
|
| 270 | - libxml_disable_entity_loader($loadEntities); |
|
| 271 | - if((string)$xml->id !== $appId) { |
|
| 272 | - throw new \Exception( |
|
| 273 | - sprintf( |
|
| 274 | - 'App for id %s has a wrong app ID in info.xml: %s', |
|
| 275 | - $appId, |
|
| 276 | - (string)$xml->id |
|
| 277 | - ) |
|
| 278 | - ); |
|
| 279 | - } |
|
| 280 | - |
|
| 281 | - // Check if the version is lower than before |
|
| 282 | - $currentVersion = OC_App::getAppVersion($appId); |
|
| 283 | - $newVersion = (string)$xml->version; |
|
| 284 | - if(version_compare($currentVersion, $newVersion) === 1) { |
|
| 285 | - throw new \Exception( |
|
| 286 | - sprintf( |
|
| 287 | - 'App for id %s has version %s and tried to update to lower version %s', |
|
| 288 | - $appId, |
|
| 289 | - $currentVersion, |
|
| 290 | - $newVersion |
|
| 291 | - ) |
|
| 292 | - ); |
|
| 293 | - } |
|
| 294 | - |
|
| 295 | - $baseDir = OC_App::getInstallPath() . '/' . $appId; |
|
| 296 | - // Remove old app with the ID if existent |
|
| 297 | - OC_Helper::rmdirr($baseDir); |
|
| 298 | - // Move to app folder |
|
| 299 | - if(@mkdir($baseDir)) { |
|
| 300 | - $extractDir .= '/' . $folders[0]; |
|
| 301 | - OC_Helper::copyr($extractDir, $baseDir); |
|
| 302 | - } |
|
| 303 | - OC_Helper::copyr($extractDir, $baseDir); |
|
| 304 | - OC_Helper::rmdirr($extractDir); |
|
| 305 | - return; |
|
| 306 | - } else { |
|
| 307 | - throw new \Exception( |
|
| 308 | - sprintf( |
|
| 309 | - 'Could not extract app with ID %s to %s', |
|
| 310 | - $appId, |
|
| 311 | - $extractDir |
|
| 312 | - ) |
|
| 313 | - ); |
|
| 314 | - } |
|
| 315 | - } else { |
|
| 316 | - // Signature does not match |
|
| 317 | - throw new \Exception( |
|
| 318 | - sprintf( |
|
| 319 | - 'App with id %s has invalid signature', |
|
| 320 | - $appId |
|
| 321 | - ) |
|
| 322 | - ); |
|
| 323 | - } |
|
| 324 | - } |
|
| 325 | - } |
|
| 326 | - |
|
| 327 | - throw new \Exception( |
|
| 328 | - sprintf( |
|
| 329 | - 'Could not download app %s', |
|
| 330 | - $appId |
|
| 331 | - ) |
|
| 332 | - ); |
|
| 333 | - } |
|
| 334 | - |
|
| 335 | - /** |
|
| 336 | - * Check if an update for the app is available |
|
| 337 | - * |
|
| 338 | - * @param string $appId |
|
| 339 | - * @param AppFetcher $appFetcher |
|
| 340 | - * @return string|false false or the version number of the update |
|
| 341 | - */ |
|
| 342 | - public static function isUpdateAvailable($appId, |
|
| 343 | - AppFetcher $appFetcher) { |
|
| 344 | - static $isInstanceReadyForUpdates = null; |
|
| 345 | - |
|
| 346 | - if ($isInstanceReadyForUpdates === null) { |
|
| 347 | - $installPath = OC_App::getInstallPath(); |
|
| 348 | - if ($installPath === false || $installPath === null) { |
|
| 349 | - $isInstanceReadyForUpdates = false; |
|
| 350 | - } else { |
|
| 351 | - $isInstanceReadyForUpdates = true; |
|
| 352 | - } |
|
| 353 | - } |
|
| 354 | - |
|
| 355 | - if ($isInstanceReadyForUpdates === false) { |
|
| 356 | - return false; |
|
| 357 | - } |
|
| 358 | - |
|
| 359 | - $apps = $appFetcher->get(); |
|
| 360 | - foreach($apps as $app) { |
|
| 361 | - if($app['id'] === $appId) { |
|
| 362 | - $currentVersion = OC_App::getAppVersion($appId); |
|
| 363 | - $newestVersion = $app['releases'][0]['version']; |
|
| 364 | - if (version_compare($newestVersion, $currentVersion, '>')) { |
|
| 365 | - return $newestVersion; |
|
| 366 | - } else { |
|
| 367 | - return false; |
|
| 368 | - } |
|
| 369 | - } |
|
| 370 | - } |
|
| 371 | - |
|
| 372 | - return false; |
|
| 373 | - } |
|
| 374 | - |
|
| 375 | - /** |
|
| 376 | - * Check if app is already downloaded |
|
| 377 | - * @param string $name name of the application to remove |
|
| 378 | - * @return boolean |
|
| 379 | - * |
|
| 380 | - * The function will check if the app is already downloaded in the apps repository |
|
| 381 | - */ |
|
| 382 | - public function isDownloaded($name) { |
|
| 383 | - foreach(\OC::$APPSROOTS as $dir) { |
|
| 384 | - $dirToTest = $dir['path']; |
|
| 385 | - $dirToTest .= '/'; |
|
| 386 | - $dirToTest .= $name; |
|
| 387 | - $dirToTest .= '/'; |
|
| 388 | - |
|
| 389 | - if (is_dir($dirToTest)) { |
|
| 390 | - return true; |
|
| 391 | - } |
|
| 392 | - } |
|
| 393 | - |
|
| 394 | - return false; |
|
| 395 | - } |
|
| 396 | - |
|
| 397 | - /** |
|
| 398 | - * Removes an app |
|
| 399 | - * @param string $appId ID of the application to remove |
|
| 400 | - * @return boolean |
|
| 401 | - * |
|
| 402 | - * |
|
| 403 | - * This function works as follows |
|
| 404 | - * -# call uninstall repair steps |
|
| 405 | - * -# removing the files |
|
| 406 | - * |
|
| 407 | - * The function will not delete preferences, tables and the configuration, |
|
| 408 | - * this has to be done by the function oc_app_uninstall(). |
|
| 409 | - */ |
|
| 410 | - public function removeApp($appId) { |
|
| 411 | - if($this->isDownloaded( $appId )) { |
|
| 412 | - $appDir = OC_App::getInstallPath() . '/' . $appId; |
|
| 413 | - OC_Helper::rmdirr($appDir); |
|
| 414 | - return true; |
|
| 415 | - }else{ |
|
| 416 | - \OCP\Util::writeLog('core', 'can\'t remove app '.$appId.'. It is not installed.', \OCP\Util::ERROR); |
|
| 417 | - |
|
| 418 | - return false; |
|
| 419 | - } |
|
| 420 | - |
|
| 421 | - } |
|
| 422 | - |
|
| 423 | - /** |
|
| 424 | - * Installs shipped apps |
|
| 425 | - * |
|
| 426 | - * This function installs all apps found in the 'apps' directory that should be enabled by default; |
|
| 427 | - * @param bool $softErrors When updating we ignore errors and simply log them, better to have a |
|
| 428 | - * working ownCloud at the end instead of an aborted update. |
|
| 429 | - * @return array Array of error messages (appid => Exception) |
|
| 430 | - */ |
|
| 431 | - public static function installShippedApps($softErrors = false) { |
|
| 432 | - $errors = []; |
|
| 433 | - foreach(\OC::$APPSROOTS as $app_dir) { |
|
| 434 | - if($dir = opendir( $app_dir['path'] )) { |
|
| 435 | - while( false !== ( $filename = readdir( $dir ))) { |
|
| 436 | - if( substr( $filename, 0, 1 ) != '.' and is_dir($app_dir['path']."/$filename") ) { |
|
| 437 | - if( file_exists( $app_dir['path']."/$filename/appinfo/info.xml" )) { |
|
| 438 | - if(!Installer::isInstalled($filename)) { |
|
| 439 | - $info=OC_App::getAppInfo($filename); |
|
| 440 | - $enabled = isset($info['default_enable']); |
|
| 441 | - if (($enabled || in_array($filename, \OC::$server->getAppManager()->getAlwaysEnabledApps())) |
|
| 442 | - && \OC::$server->getConfig()->getAppValue($filename, 'enabled') !== 'no') { |
|
| 443 | - if ($softErrors) { |
|
| 444 | - try { |
|
| 445 | - Installer::installShippedApp($filename); |
|
| 446 | - } catch (HintException $e) { |
|
| 447 | - if ($e->getPrevious() instanceof TableExistsException) { |
|
| 448 | - $errors[$filename] = $e; |
|
| 449 | - continue; |
|
| 450 | - } |
|
| 451 | - throw $e; |
|
| 452 | - } |
|
| 453 | - } else { |
|
| 454 | - Installer::installShippedApp($filename); |
|
| 455 | - } |
|
| 456 | - \OC::$server->getConfig()->setAppValue($filename, 'enabled', 'yes'); |
|
| 457 | - } |
|
| 458 | - } |
|
| 459 | - } |
|
| 460 | - } |
|
| 461 | - } |
|
| 462 | - closedir( $dir ); |
|
| 463 | - } |
|
| 464 | - } |
|
| 465 | - |
|
| 466 | - return $errors; |
|
| 467 | - } |
|
| 468 | - |
|
| 469 | - /** |
|
| 470 | - * install an app already placed in the app folder |
|
| 471 | - * @param string $app id of the app to install |
|
| 472 | - * @return integer |
|
| 473 | - */ |
|
| 474 | - public static function installShippedApp($app) { |
|
| 475 | - //install the database |
|
| 476 | - $appPath = OC_App::getAppPath($app); |
|
| 477 | - if(is_file("$appPath/appinfo/database.xml")) { |
|
| 478 | - try { |
|
| 479 | - OC_DB::createDbFromStructure("$appPath/appinfo/database.xml"); |
|
| 480 | - } catch (TableExistsException $e) { |
|
| 481 | - throw new HintException( |
|
| 482 | - 'Failed to enable app ' . $app, |
|
| 483 | - 'Please ask for help via one of our <a href="https://nextcloud.com/support/" target="_blank" rel="noreferrer">support channels</a>.', |
|
| 484 | - 0, $e |
|
| 485 | - ); |
|
| 486 | - } |
|
| 487 | - } |
|
| 488 | - |
|
| 489 | - //run appinfo/install.php |
|
| 490 | - \OC_App::registerAutoloading($app, $appPath); |
|
| 491 | - self::includeAppScript("$appPath/appinfo/install.php"); |
|
| 492 | - |
|
| 493 | - $info = OC_App::getAppInfo($app); |
|
| 494 | - if (is_null($info)) { |
|
| 495 | - return false; |
|
| 496 | - } |
|
| 497 | - \OC_App::setupBackgroundJobs($info['background-jobs']); |
|
| 498 | - |
|
| 499 | - OC_App::executeRepairSteps($app, $info['repair-steps']['install']); |
|
| 500 | - |
|
| 501 | - $config = \OC::$server->getConfig(); |
|
| 502 | - |
|
| 503 | - $config->setAppValue($app, 'installed_version', OC_App::getAppVersion($app)); |
|
| 504 | - if (array_key_exists('ocsid', $info)) { |
|
| 505 | - $config->setAppValue($app, 'ocsid', $info['ocsid']); |
|
| 506 | - } |
|
| 507 | - |
|
| 508 | - //set remote/public handlers |
|
| 509 | - foreach($info['remote'] as $name=>$path) { |
|
| 510 | - $config->setAppValue('core', 'remote_'.$name, $app.'/'.$path); |
|
| 511 | - } |
|
| 512 | - foreach($info['public'] as $name=>$path) { |
|
| 513 | - $config->setAppValue('core', 'public_'.$name, $app.'/'.$path); |
|
| 514 | - } |
|
| 515 | - |
|
| 516 | - OC_App::setAppTypes($info['id']); |
|
| 517 | - |
|
| 518 | - if(isset($info['settings']) && is_array($info['settings'])) { |
|
| 519 | - // requires that autoloading was registered for the app, |
|
| 520 | - // as happens before running the install.php some lines above |
|
| 521 | - \OC::$server->getSettingsManager()->setupSettings($info['settings']); |
|
| 522 | - } |
|
| 523 | - |
|
| 524 | - return $info['id']; |
|
| 525 | - } |
|
| 526 | - |
|
| 527 | - /** |
|
| 528 | - * check the code of an app with some static code checks |
|
| 529 | - * @param string $folder the folder of the app to check |
|
| 530 | - * @return boolean true for app is o.k. and false for app is not o.k. |
|
| 531 | - */ |
|
| 532 | - public static function checkCode($folder) { |
|
| 533 | - // is the code checker enabled? |
|
| 534 | - if(!\OC::$server->getConfig()->getSystemValue('appcodechecker', false)) { |
|
| 535 | - return true; |
|
| 536 | - } |
|
| 537 | - |
|
| 538 | - $codeChecker = new CodeChecker(new PrivateCheck(new EmptyCheck())); |
|
| 539 | - $errors = $codeChecker->analyseFolder(basename($folder), $folder); |
|
| 540 | - |
|
| 541 | - return empty($errors); |
|
| 542 | - } |
|
| 543 | - |
|
| 544 | - /** |
|
| 545 | - * @param string $script |
|
| 546 | - */ |
|
| 547 | - private static function includeAppScript($script) { |
|
| 548 | - if ( file_exists($script) ){ |
|
| 549 | - include $script; |
|
| 550 | - } |
|
| 551 | - } |
|
| 63 | + /** @var AppFetcher */ |
|
| 64 | + private $appFetcher; |
|
| 65 | + /** @var IClientService */ |
|
| 66 | + private $clientService; |
|
| 67 | + /** @var ITempManager */ |
|
| 68 | + private $tempManager; |
|
| 69 | + /** @var ILogger */ |
|
| 70 | + private $logger; |
|
| 71 | + |
|
| 72 | + /** |
|
| 73 | + * @param AppFetcher $appFetcher |
|
| 74 | + * @param IClientService $clientService |
|
| 75 | + * @param ITempManager $tempManager |
|
| 76 | + * @param ILogger $logger |
|
| 77 | + */ |
|
| 78 | + public function __construct(AppFetcher $appFetcher, |
|
| 79 | + IClientService $clientService, |
|
| 80 | + ITempManager $tempManager, |
|
| 81 | + ILogger $logger) { |
|
| 82 | + $this->appFetcher = $appFetcher; |
|
| 83 | + $this->clientService = $clientService; |
|
| 84 | + $this->tempManager = $tempManager; |
|
| 85 | + $this->logger = $logger; |
|
| 86 | + } |
|
| 87 | + |
|
| 88 | + /** |
|
| 89 | + * Installs an app that is located in one of the app folders already |
|
| 90 | + * |
|
| 91 | + * @param string $appId App to install |
|
| 92 | + * @throws \Exception |
|
| 93 | + * @return integer |
|
| 94 | + */ |
|
| 95 | + public function installApp($appId) { |
|
| 96 | + $app = \OC_App::findAppInDirectories($appId); |
|
| 97 | + if($app === false) { |
|
| 98 | + throw new \Exception('App not found in any app directory'); |
|
| 99 | + } |
|
| 100 | + |
|
| 101 | + $basedir = $app['path'].'/'.$appId; |
|
| 102 | + $info = OC_App::getAppInfo($basedir.'/appinfo/info.xml', true); |
|
| 103 | + |
|
| 104 | + //install the database |
|
| 105 | + if(is_file($basedir.'/appinfo/database.xml')) { |
|
| 106 | + if (\OC::$server->getAppConfig()->getValue($info['id'], 'installed_version') === null) { |
|
| 107 | + OC_DB::createDbFromStructure($basedir.'/appinfo/database.xml'); |
|
| 108 | + } else { |
|
| 109 | + OC_DB::updateDbFromStructure($basedir.'/appinfo/database.xml'); |
|
| 110 | + } |
|
| 111 | + } |
|
| 112 | + |
|
| 113 | + \OC_App::setupBackgroundJobs($info['background-jobs']); |
|
| 114 | + |
|
| 115 | + //run appinfo/install.php |
|
| 116 | + if((!isset($data['noinstall']) or $data['noinstall']==false)) { |
|
| 117 | + self::includeAppScript($basedir . '/appinfo/install.php'); |
|
| 118 | + } |
|
| 119 | + |
|
| 120 | + $appData = OC_App::getAppInfo($appId); |
|
| 121 | + OC_App::executeRepairSteps($appId, $appData['repair-steps']['install']); |
|
| 122 | + |
|
| 123 | + //set the installed version |
|
| 124 | + \OC::$server->getConfig()->setAppValue($info['id'], 'installed_version', OC_App::getAppVersion($info['id'], false)); |
|
| 125 | + \OC::$server->getConfig()->setAppValue($info['id'], 'enabled', 'no'); |
|
| 126 | + |
|
| 127 | + //set remote/public handlers |
|
| 128 | + foreach($info['remote'] as $name=>$path) { |
|
| 129 | + \OC::$server->getConfig()->setAppValue('core', 'remote_'.$name, $info['id'].'/'.$path); |
|
| 130 | + } |
|
| 131 | + foreach($info['public'] as $name=>$path) { |
|
| 132 | + \OC::$server->getConfig()->setAppValue('core', 'public_'.$name, $info['id'].'/'.$path); |
|
| 133 | + } |
|
| 134 | + |
|
| 135 | + OC_App::setAppTypes($info['id']); |
|
| 136 | + |
|
| 137 | + return $info['id']; |
|
| 138 | + } |
|
| 139 | + |
|
| 140 | + /** |
|
| 141 | + * @brief checks whether or not an app is installed |
|
| 142 | + * @param string $app app |
|
| 143 | + * @returns bool |
|
| 144 | + * |
|
| 145 | + * Checks whether or not an app is installed, i.e. registered in apps table. |
|
| 146 | + */ |
|
| 147 | + public static function isInstalled( $app ) { |
|
| 148 | + return (\OC::$server->getConfig()->getAppValue($app, "installed_version", null) !== null); |
|
| 149 | + } |
|
| 150 | + |
|
| 151 | + /** |
|
| 152 | + * Updates the specified app from the appstore |
|
| 153 | + * |
|
| 154 | + * @param string $appId |
|
| 155 | + * @return bool |
|
| 156 | + */ |
|
| 157 | + public function updateAppstoreApp($appId) { |
|
| 158 | + if(self::isUpdateAvailable($appId, $this->appFetcher)) { |
|
| 159 | + try { |
|
| 160 | + $this->downloadApp($appId); |
|
| 161 | + } catch (\Exception $e) { |
|
| 162 | + $this->logger->error($e->getMessage(), ['app' => 'core']); |
|
| 163 | + return false; |
|
| 164 | + } |
|
| 165 | + return OC_App::updateApp($appId); |
|
| 166 | + } |
|
| 167 | + |
|
| 168 | + return false; |
|
| 169 | + } |
|
| 170 | + |
|
| 171 | + /** |
|
| 172 | + * Downloads an app and puts it into the app directory |
|
| 173 | + * |
|
| 174 | + * @param string $appId |
|
| 175 | + * |
|
| 176 | + * @throws \Exception If the installation was not successful |
|
| 177 | + */ |
|
| 178 | + public function downloadApp($appId) { |
|
| 179 | + $appId = strtolower($appId); |
|
| 180 | + |
|
| 181 | + $apps = $this->appFetcher->get(); |
|
| 182 | + foreach($apps as $app) { |
|
| 183 | + if($app['id'] === $appId) { |
|
| 184 | + // Load the certificate |
|
| 185 | + $certificate = new X509(); |
|
| 186 | + $certificate->loadCA(file_get_contents(__DIR__ . '/../../resources/codesigning/root.crt')); |
|
| 187 | + $loadedCertificate = $certificate->loadX509($app['certificate']); |
|
| 188 | + |
|
| 189 | + // Verify if the certificate has been revoked |
|
| 190 | + $crl = new X509(); |
|
| 191 | + $crl->loadCA(file_get_contents(__DIR__ . '/../../resources/codesigning/root.crt')); |
|
| 192 | + $crl->loadCRL(file_get_contents(__DIR__ . '/../../resources/codesigning/root.crl')); |
|
| 193 | + if($crl->validateSignature() !== true) { |
|
| 194 | + throw new \Exception('Could not validate CRL signature'); |
|
| 195 | + } |
|
| 196 | + $csn = $loadedCertificate['tbsCertificate']['serialNumber']->toString(); |
|
| 197 | + $revoked = $crl->getRevoked($csn); |
|
| 198 | + if ($revoked !== false) { |
|
| 199 | + throw new \Exception( |
|
| 200 | + sprintf( |
|
| 201 | + 'Certificate "%s" has been revoked', |
|
| 202 | + $csn |
|
| 203 | + ) |
|
| 204 | + ); |
|
| 205 | + } |
|
| 206 | + |
|
| 207 | + // Verify if the certificate has been issued by the Nextcloud Code Authority CA |
|
| 208 | + if($certificate->validateSignature() !== true) { |
|
| 209 | + throw new \Exception( |
|
| 210 | + sprintf( |
|
| 211 | + 'App with id %s has a certificate not issued by a trusted Code Signing Authority', |
|
| 212 | + $appId |
|
| 213 | + ) |
|
| 214 | + ); |
|
| 215 | + } |
|
| 216 | + |
|
| 217 | + // Verify if the certificate is issued for the requested app id |
|
| 218 | + $certInfo = openssl_x509_parse($app['certificate']); |
|
| 219 | + if(!isset($certInfo['subject']['CN'])) { |
|
| 220 | + throw new \Exception( |
|
| 221 | + sprintf( |
|
| 222 | + 'App with id %s has a cert with no CN', |
|
| 223 | + $appId |
|
| 224 | + ) |
|
| 225 | + ); |
|
| 226 | + } |
|
| 227 | + if($certInfo['subject']['CN'] !== $appId) { |
|
| 228 | + throw new \Exception( |
|
| 229 | + sprintf( |
|
| 230 | + 'App with id %s has a cert issued to %s', |
|
| 231 | + $appId, |
|
| 232 | + $certInfo['subject']['CN'] |
|
| 233 | + ) |
|
| 234 | + ); |
|
| 235 | + } |
|
| 236 | + |
|
| 237 | + // Download the release |
|
| 238 | + $tempFile = $this->tempManager->getTemporaryFile('.tar.gz'); |
|
| 239 | + $client = $this->clientService->newClient(); |
|
| 240 | + $client->get($app['releases'][0]['download'], ['save_to' => $tempFile]); |
|
| 241 | + |
|
| 242 | + // Check if the signature actually matches the downloaded content |
|
| 243 | + $certificate = openssl_get_publickey($app['certificate']); |
|
| 244 | + $verified = (bool)openssl_verify(file_get_contents($tempFile), base64_decode($app['releases'][0]['signature']), $certificate, OPENSSL_ALGO_SHA512); |
|
| 245 | + openssl_free_key($certificate); |
|
| 246 | + |
|
| 247 | + if($verified === true) { |
|
| 248 | + // Seems to match, let's proceed |
|
| 249 | + $extractDir = $this->tempManager->getTemporaryFolder(); |
|
| 250 | + $archive = new TAR($tempFile); |
|
| 251 | + |
|
| 252 | + if($archive) { |
|
| 253 | + $archive->extract($extractDir); |
|
| 254 | + $allFiles = scandir($extractDir); |
|
| 255 | + $folders = array_diff($allFiles, ['.', '..']); |
|
| 256 | + $folders = array_values($folders); |
|
| 257 | + |
|
| 258 | + if(count($folders) > 1) { |
|
| 259 | + throw new \Exception( |
|
| 260 | + sprintf( |
|
| 261 | + 'Extracted app %s has more than 1 folder', |
|
| 262 | + $appId |
|
| 263 | + ) |
|
| 264 | + ); |
|
| 265 | + } |
|
| 266 | + |
|
| 267 | + // Check if appinfo/info.xml has the same app ID as well |
|
| 268 | + $loadEntities = libxml_disable_entity_loader(false); |
|
| 269 | + $xml = simplexml_load_file($extractDir . '/' . $folders[0] . '/appinfo/info.xml'); |
|
| 270 | + libxml_disable_entity_loader($loadEntities); |
|
| 271 | + if((string)$xml->id !== $appId) { |
|
| 272 | + throw new \Exception( |
|
| 273 | + sprintf( |
|
| 274 | + 'App for id %s has a wrong app ID in info.xml: %s', |
|
| 275 | + $appId, |
|
| 276 | + (string)$xml->id |
|
| 277 | + ) |
|
| 278 | + ); |
|
| 279 | + } |
|
| 280 | + |
|
| 281 | + // Check if the version is lower than before |
|
| 282 | + $currentVersion = OC_App::getAppVersion($appId); |
|
| 283 | + $newVersion = (string)$xml->version; |
|
| 284 | + if(version_compare($currentVersion, $newVersion) === 1) { |
|
| 285 | + throw new \Exception( |
|
| 286 | + sprintf( |
|
| 287 | + 'App for id %s has version %s and tried to update to lower version %s', |
|
| 288 | + $appId, |
|
| 289 | + $currentVersion, |
|
| 290 | + $newVersion |
|
| 291 | + ) |
|
| 292 | + ); |
|
| 293 | + } |
|
| 294 | + |
|
| 295 | + $baseDir = OC_App::getInstallPath() . '/' . $appId; |
|
| 296 | + // Remove old app with the ID if existent |
|
| 297 | + OC_Helper::rmdirr($baseDir); |
|
| 298 | + // Move to app folder |
|
| 299 | + if(@mkdir($baseDir)) { |
|
| 300 | + $extractDir .= '/' . $folders[0]; |
|
| 301 | + OC_Helper::copyr($extractDir, $baseDir); |
|
| 302 | + } |
|
| 303 | + OC_Helper::copyr($extractDir, $baseDir); |
|
| 304 | + OC_Helper::rmdirr($extractDir); |
|
| 305 | + return; |
|
| 306 | + } else { |
|
| 307 | + throw new \Exception( |
|
| 308 | + sprintf( |
|
| 309 | + 'Could not extract app with ID %s to %s', |
|
| 310 | + $appId, |
|
| 311 | + $extractDir |
|
| 312 | + ) |
|
| 313 | + ); |
|
| 314 | + } |
|
| 315 | + } else { |
|
| 316 | + // Signature does not match |
|
| 317 | + throw new \Exception( |
|
| 318 | + sprintf( |
|
| 319 | + 'App with id %s has invalid signature', |
|
| 320 | + $appId |
|
| 321 | + ) |
|
| 322 | + ); |
|
| 323 | + } |
|
| 324 | + } |
|
| 325 | + } |
|
| 326 | + |
|
| 327 | + throw new \Exception( |
|
| 328 | + sprintf( |
|
| 329 | + 'Could not download app %s', |
|
| 330 | + $appId |
|
| 331 | + ) |
|
| 332 | + ); |
|
| 333 | + } |
|
| 334 | + |
|
| 335 | + /** |
|
| 336 | + * Check if an update for the app is available |
|
| 337 | + * |
|
| 338 | + * @param string $appId |
|
| 339 | + * @param AppFetcher $appFetcher |
|
| 340 | + * @return string|false false or the version number of the update |
|
| 341 | + */ |
|
| 342 | + public static function isUpdateAvailable($appId, |
|
| 343 | + AppFetcher $appFetcher) { |
|
| 344 | + static $isInstanceReadyForUpdates = null; |
|
| 345 | + |
|
| 346 | + if ($isInstanceReadyForUpdates === null) { |
|
| 347 | + $installPath = OC_App::getInstallPath(); |
|
| 348 | + if ($installPath === false || $installPath === null) { |
|
| 349 | + $isInstanceReadyForUpdates = false; |
|
| 350 | + } else { |
|
| 351 | + $isInstanceReadyForUpdates = true; |
|
| 352 | + } |
|
| 353 | + } |
|
| 354 | + |
|
| 355 | + if ($isInstanceReadyForUpdates === false) { |
|
| 356 | + return false; |
|
| 357 | + } |
|
| 358 | + |
|
| 359 | + $apps = $appFetcher->get(); |
|
| 360 | + foreach($apps as $app) { |
|
| 361 | + if($app['id'] === $appId) { |
|
| 362 | + $currentVersion = OC_App::getAppVersion($appId); |
|
| 363 | + $newestVersion = $app['releases'][0]['version']; |
|
| 364 | + if (version_compare($newestVersion, $currentVersion, '>')) { |
|
| 365 | + return $newestVersion; |
|
| 366 | + } else { |
|
| 367 | + return false; |
|
| 368 | + } |
|
| 369 | + } |
|
| 370 | + } |
|
| 371 | + |
|
| 372 | + return false; |
|
| 373 | + } |
|
| 374 | + |
|
| 375 | + /** |
|
| 376 | + * Check if app is already downloaded |
|
| 377 | + * @param string $name name of the application to remove |
|
| 378 | + * @return boolean |
|
| 379 | + * |
|
| 380 | + * The function will check if the app is already downloaded in the apps repository |
|
| 381 | + */ |
|
| 382 | + public function isDownloaded($name) { |
|
| 383 | + foreach(\OC::$APPSROOTS as $dir) { |
|
| 384 | + $dirToTest = $dir['path']; |
|
| 385 | + $dirToTest .= '/'; |
|
| 386 | + $dirToTest .= $name; |
|
| 387 | + $dirToTest .= '/'; |
|
| 388 | + |
|
| 389 | + if (is_dir($dirToTest)) { |
|
| 390 | + return true; |
|
| 391 | + } |
|
| 392 | + } |
|
| 393 | + |
|
| 394 | + return false; |
|
| 395 | + } |
|
| 396 | + |
|
| 397 | + /** |
|
| 398 | + * Removes an app |
|
| 399 | + * @param string $appId ID of the application to remove |
|
| 400 | + * @return boolean |
|
| 401 | + * |
|
| 402 | + * |
|
| 403 | + * This function works as follows |
|
| 404 | + * -# call uninstall repair steps |
|
| 405 | + * -# removing the files |
|
| 406 | + * |
|
| 407 | + * The function will not delete preferences, tables and the configuration, |
|
| 408 | + * this has to be done by the function oc_app_uninstall(). |
|
| 409 | + */ |
|
| 410 | + public function removeApp($appId) { |
|
| 411 | + if($this->isDownloaded( $appId )) { |
|
| 412 | + $appDir = OC_App::getInstallPath() . '/' . $appId; |
|
| 413 | + OC_Helper::rmdirr($appDir); |
|
| 414 | + return true; |
|
| 415 | + }else{ |
|
| 416 | + \OCP\Util::writeLog('core', 'can\'t remove app '.$appId.'. It is not installed.', \OCP\Util::ERROR); |
|
| 417 | + |
|
| 418 | + return false; |
|
| 419 | + } |
|
| 420 | + |
|
| 421 | + } |
|
| 422 | + |
|
| 423 | + /** |
|
| 424 | + * Installs shipped apps |
|
| 425 | + * |
|
| 426 | + * This function installs all apps found in the 'apps' directory that should be enabled by default; |
|
| 427 | + * @param bool $softErrors When updating we ignore errors and simply log them, better to have a |
|
| 428 | + * working ownCloud at the end instead of an aborted update. |
|
| 429 | + * @return array Array of error messages (appid => Exception) |
|
| 430 | + */ |
|
| 431 | + public static function installShippedApps($softErrors = false) { |
|
| 432 | + $errors = []; |
|
| 433 | + foreach(\OC::$APPSROOTS as $app_dir) { |
|
| 434 | + if($dir = opendir( $app_dir['path'] )) { |
|
| 435 | + while( false !== ( $filename = readdir( $dir ))) { |
|
| 436 | + if( substr( $filename, 0, 1 ) != '.' and is_dir($app_dir['path']."/$filename") ) { |
|
| 437 | + if( file_exists( $app_dir['path']."/$filename/appinfo/info.xml" )) { |
|
| 438 | + if(!Installer::isInstalled($filename)) { |
|
| 439 | + $info=OC_App::getAppInfo($filename); |
|
| 440 | + $enabled = isset($info['default_enable']); |
|
| 441 | + if (($enabled || in_array($filename, \OC::$server->getAppManager()->getAlwaysEnabledApps())) |
|
| 442 | + && \OC::$server->getConfig()->getAppValue($filename, 'enabled') !== 'no') { |
|
| 443 | + if ($softErrors) { |
|
| 444 | + try { |
|
| 445 | + Installer::installShippedApp($filename); |
|
| 446 | + } catch (HintException $e) { |
|
| 447 | + if ($e->getPrevious() instanceof TableExistsException) { |
|
| 448 | + $errors[$filename] = $e; |
|
| 449 | + continue; |
|
| 450 | + } |
|
| 451 | + throw $e; |
|
| 452 | + } |
|
| 453 | + } else { |
|
| 454 | + Installer::installShippedApp($filename); |
|
| 455 | + } |
|
| 456 | + \OC::$server->getConfig()->setAppValue($filename, 'enabled', 'yes'); |
|
| 457 | + } |
|
| 458 | + } |
|
| 459 | + } |
|
| 460 | + } |
|
| 461 | + } |
|
| 462 | + closedir( $dir ); |
|
| 463 | + } |
|
| 464 | + } |
|
| 465 | + |
|
| 466 | + return $errors; |
|
| 467 | + } |
|
| 468 | + |
|
| 469 | + /** |
|
| 470 | + * install an app already placed in the app folder |
|
| 471 | + * @param string $app id of the app to install |
|
| 472 | + * @return integer |
|
| 473 | + */ |
|
| 474 | + public static function installShippedApp($app) { |
|
| 475 | + //install the database |
|
| 476 | + $appPath = OC_App::getAppPath($app); |
|
| 477 | + if(is_file("$appPath/appinfo/database.xml")) { |
|
| 478 | + try { |
|
| 479 | + OC_DB::createDbFromStructure("$appPath/appinfo/database.xml"); |
|
| 480 | + } catch (TableExistsException $e) { |
|
| 481 | + throw new HintException( |
|
| 482 | + 'Failed to enable app ' . $app, |
|
| 483 | + 'Please ask for help via one of our <a href="https://nextcloud.com/support/" target="_blank" rel="noreferrer">support channels</a>.', |
|
| 484 | + 0, $e |
|
| 485 | + ); |
|
| 486 | + } |
|
| 487 | + } |
|
| 488 | + |
|
| 489 | + //run appinfo/install.php |
|
| 490 | + \OC_App::registerAutoloading($app, $appPath); |
|
| 491 | + self::includeAppScript("$appPath/appinfo/install.php"); |
|
| 492 | + |
|
| 493 | + $info = OC_App::getAppInfo($app); |
|
| 494 | + if (is_null($info)) { |
|
| 495 | + return false; |
|
| 496 | + } |
|
| 497 | + \OC_App::setupBackgroundJobs($info['background-jobs']); |
|
| 498 | + |
|
| 499 | + OC_App::executeRepairSteps($app, $info['repair-steps']['install']); |
|
| 500 | + |
|
| 501 | + $config = \OC::$server->getConfig(); |
|
| 502 | + |
|
| 503 | + $config->setAppValue($app, 'installed_version', OC_App::getAppVersion($app)); |
|
| 504 | + if (array_key_exists('ocsid', $info)) { |
|
| 505 | + $config->setAppValue($app, 'ocsid', $info['ocsid']); |
|
| 506 | + } |
|
| 507 | + |
|
| 508 | + //set remote/public handlers |
|
| 509 | + foreach($info['remote'] as $name=>$path) { |
|
| 510 | + $config->setAppValue('core', 'remote_'.$name, $app.'/'.$path); |
|
| 511 | + } |
|
| 512 | + foreach($info['public'] as $name=>$path) { |
|
| 513 | + $config->setAppValue('core', 'public_'.$name, $app.'/'.$path); |
|
| 514 | + } |
|
| 515 | + |
|
| 516 | + OC_App::setAppTypes($info['id']); |
|
| 517 | + |
|
| 518 | + if(isset($info['settings']) && is_array($info['settings'])) { |
|
| 519 | + // requires that autoloading was registered for the app, |
|
| 520 | + // as happens before running the install.php some lines above |
|
| 521 | + \OC::$server->getSettingsManager()->setupSettings($info['settings']); |
|
| 522 | + } |
|
| 523 | + |
|
| 524 | + return $info['id']; |
|
| 525 | + } |
|
| 526 | + |
|
| 527 | + /** |
|
| 528 | + * check the code of an app with some static code checks |
|
| 529 | + * @param string $folder the folder of the app to check |
|
| 530 | + * @return boolean true for app is o.k. and false for app is not o.k. |
|
| 531 | + */ |
|
| 532 | + public static function checkCode($folder) { |
|
| 533 | + // is the code checker enabled? |
|
| 534 | + if(!\OC::$server->getConfig()->getSystemValue('appcodechecker', false)) { |
|
| 535 | + return true; |
|
| 536 | + } |
|
| 537 | + |
|
| 538 | + $codeChecker = new CodeChecker(new PrivateCheck(new EmptyCheck())); |
|
| 539 | + $errors = $codeChecker->analyseFolder(basename($folder), $folder); |
|
| 540 | + |
|
| 541 | + return empty($errors); |
|
| 542 | + } |
|
| 543 | + |
|
| 544 | + /** |
|
| 545 | + * @param string $script |
|
| 546 | + */ |
|
| 547 | + private static function includeAppScript($script) { |
|
| 548 | + if ( file_exists($script) ){ |
|
| 549 | + include $script; |
|
| 550 | + } |
|
| 551 | + } |
|
| 552 | 552 | } |
@@ -94,7 +94,7 @@ discard block |
||
| 94 | 94 | */ |
| 95 | 95 | public function installApp($appId) { |
| 96 | 96 | $app = \OC_App::findAppInDirectories($appId); |
| 97 | - if($app === false) { |
|
| 97 | + if ($app === false) { |
|
| 98 | 98 | throw new \Exception('App not found in any app directory'); |
| 99 | 99 | } |
| 100 | 100 | |
@@ -102,7 +102,7 @@ discard block |
||
| 102 | 102 | $info = OC_App::getAppInfo($basedir.'/appinfo/info.xml', true); |
| 103 | 103 | |
| 104 | 104 | //install the database |
| 105 | - if(is_file($basedir.'/appinfo/database.xml')) { |
|
| 105 | + if (is_file($basedir.'/appinfo/database.xml')) { |
|
| 106 | 106 | if (\OC::$server->getAppConfig()->getValue($info['id'], 'installed_version') === null) { |
| 107 | 107 | OC_DB::createDbFromStructure($basedir.'/appinfo/database.xml'); |
| 108 | 108 | } else { |
@@ -113,8 +113,8 @@ discard block |
||
| 113 | 113 | \OC_App::setupBackgroundJobs($info['background-jobs']); |
| 114 | 114 | |
| 115 | 115 | //run appinfo/install.php |
| 116 | - if((!isset($data['noinstall']) or $data['noinstall']==false)) { |
|
| 117 | - self::includeAppScript($basedir . '/appinfo/install.php'); |
|
| 116 | + if ((!isset($data['noinstall']) or $data['noinstall'] == false)) { |
|
| 117 | + self::includeAppScript($basedir.'/appinfo/install.php'); |
|
| 118 | 118 | } |
| 119 | 119 | |
| 120 | 120 | $appData = OC_App::getAppInfo($appId); |
@@ -125,10 +125,10 @@ discard block |
||
| 125 | 125 | \OC::$server->getConfig()->setAppValue($info['id'], 'enabled', 'no'); |
| 126 | 126 | |
| 127 | 127 | //set remote/public handlers |
| 128 | - foreach($info['remote'] as $name=>$path) { |
|
| 128 | + foreach ($info['remote'] as $name=>$path) { |
|
| 129 | 129 | \OC::$server->getConfig()->setAppValue('core', 'remote_'.$name, $info['id'].'/'.$path); |
| 130 | 130 | } |
| 131 | - foreach($info['public'] as $name=>$path) { |
|
| 131 | + foreach ($info['public'] as $name=>$path) { |
|
| 132 | 132 | \OC::$server->getConfig()->setAppValue('core', 'public_'.$name, $info['id'].'/'.$path); |
| 133 | 133 | } |
| 134 | 134 | |
@@ -144,7 +144,7 @@ discard block |
||
| 144 | 144 | * |
| 145 | 145 | * Checks whether or not an app is installed, i.e. registered in apps table. |
| 146 | 146 | */ |
| 147 | - public static function isInstalled( $app ) { |
|
| 147 | + public static function isInstalled($app) { |
|
| 148 | 148 | return (\OC::$server->getConfig()->getAppValue($app, "installed_version", null) !== null); |
| 149 | 149 | } |
| 150 | 150 | |
@@ -155,7 +155,7 @@ discard block |
||
| 155 | 155 | * @return bool |
| 156 | 156 | */ |
| 157 | 157 | public function updateAppstoreApp($appId) { |
| 158 | - if(self::isUpdateAvailable($appId, $this->appFetcher)) { |
|
| 158 | + if (self::isUpdateAvailable($appId, $this->appFetcher)) { |
|
| 159 | 159 | try { |
| 160 | 160 | $this->downloadApp($appId); |
| 161 | 161 | } catch (\Exception $e) { |
@@ -179,18 +179,18 @@ discard block |
||
| 179 | 179 | $appId = strtolower($appId); |
| 180 | 180 | |
| 181 | 181 | $apps = $this->appFetcher->get(); |
| 182 | - foreach($apps as $app) { |
|
| 183 | - if($app['id'] === $appId) { |
|
| 182 | + foreach ($apps as $app) { |
|
| 183 | + if ($app['id'] === $appId) { |
|
| 184 | 184 | // Load the certificate |
| 185 | 185 | $certificate = new X509(); |
| 186 | - $certificate->loadCA(file_get_contents(__DIR__ . '/../../resources/codesigning/root.crt')); |
|
| 186 | + $certificate->loadCA(file_get_contents(__DIR__.'/../../resources/codesigning/root.crt')); |
|
| 187 | 187 | $loadedCertificate = $certificate->loadX509($app['certificate']); |
| 188 | 188 | |
| 189 | 189 | // Verify if the certificate has been revoked |
| 190 | 190 | $crl = new X509(); |
| 191 | - $crl->loadCA(file_get_contents(__DIR__ . '/../../resources/codesigning/root.crt')); |
|
| 192 | - $crl->loadCRL(file_get_contents(__DIR__ . '/../../resources/codesigning/root.crl')); |
|
| 193 | - if($crl->validateSignature() !== true) { |
|
| 191 | + $crl->loadCA(file_get_contents(__DIR__.'/../../resources/codesigning/root.crt')); |
|
| 192 | + $crl->loadCRL(file_get_contents(__DIR__.'/../../resources/codesigning/root.crl')); |
|
| 193 | + if ($crl->validateSignature() !== true) { |
|
| 194 | 194 | throw new \Exception('Could not validate CRL signature'); |
| 195 | 195 | } |
| 196 | 196 | $csn = $loadedCertificate['tbsCertificate']['serialNumber']->toString(); |
@@ -205,7 +205,7 @@ discard block |
||
| 205 | 205 | } |
| 206 | 206 | |
| 207 | 207 | // Verify if the certificate has been issued by the Nextcloud Code Authority CA |
| 208 | - if($certificate->validateSignature() !== true) { |
|
| 208 | + if ($certificate->validateSignature() !== true) { |
|
| 209 | 209 | throw new \Exception( |
| 210 | 210 | sprintf( |
| 211 | 211 | 'App with id %s has a certificate not issued by a trusted Code Signing Authority', |
@@ -216,7 +216,7 @@ discard block |
||
| 216 | 216 | |
| 217 | 217 | // Verify if the certificate is issued for the requested app id |
| 218 | 218 | $certInfo = openssl_x509_parse($app['certificate']); |
| 219 | - if(!isset($certInfo['subject']['CN'])) { |
|
| 219 | + if (!isset($certInfo['subject']['CN'])) { |
|
| 220 | 220 | throw new \Exception( |
| 221 | 221 | sprintf( |
| 222 | 222 | 'App with id %s has a cert with no CN', |
@@ -224,7 +224,7 @@ discard block |
||
| 224 | 224 | ) |
| 225 | 225 | ); |
| 226 | 226 | } |
| 227 | - if($certInfo['subject']['CN'] !== $appId) { |
|
| 227 | + if ($certInfo['subject']['CN'] !== $appId) { |
|
| 228 | 228 | throw new \Exception( |
| 229 | 229 | sprintf( |
| 230 | 230 | 'App with id %s has a cert issued to %s', |
@@ -241,21 +241,21 @@ discard block |
||
| 241 | 241 | |
| 242 | 242 | // Check if the signature actually matches the downloaded content |
| 243 | 243 | $certificate = openssl_get_publickey($app['certificate']); |
| 244 | - $verified = (bool)openssl_verify(file_get_contents($tempFile), base64_decode($app['releases'][0]['signature']), $certificate, OPENSSL_ALGO_SHA512); |
|
| 244 | + $verified = (bool) openssl_verify(file_get_contents($tempFile), base64_decode($app['releases'][0]['signature']), $certificate, OPENSSL_ALGO_SHA512); |
|
| 245 | 245 | openssl_free_key($certificate); |
| 246 | 246 | |
| 247 | - if($verified === true) { |
|
| 247 | + if ($verified === true) { |
|
| 248 | 248 | // Seems to match, let's proceed |
| 249 | 249 | $extractDir = $this->tempManager->getTemporaryFolder(); |
| 250 | 250 | $archive = new TAR($tempFile); |
| 251 | 251 | |
| 252 | - if($archive) { |
|
| 252 | + if ($archive) { |
|
| 253 | 253 | $archive->extract($extractDir); |
| 254 | 254 | $allFiles = scandir($extractDir); |
| 255 | 255 | $folders = array_diff($allFiles, ['.', '..']); |
| 256 | 256 | $folders = array_values($folders); |
| 257 | 257 | |
| 258 | - if(count($folders) > 1) { |
|
| 258 | + if (count($folders) > 1) { |
|
| 259 | 259 | throw new \Exception( |
| 260 | 260 | sprintf( |
| 261 | 261 | 'Extracted app %s has more than 1 folder', |
@@ -266,22 +266,22 @@ discard block |
||
| 266 | 266 | |
| 267 | 267 | // Check if appinfo/info.xml has the same app ID as well |
| 268 | 268 | $loadEntities = libxml_disable_entity_loader(false); |
| 269 | - $xml = simplexml_load_file($extractDir . '/' . $folders[0] . '/appinfo/info.xml'); |
|
| 269 | + $xml = simplexml_load_file($extractDir.'/'.$folders[0].'/appinfo/info.xml'); |
|
| 270 | 270 | libxml_disable_entity_loader($loadEntities); |
| 271 | - if((string)$xml->id !== $appId) { |
|
| 271 | + if ((string) $xml->id !== $appId) { |
|
| 272 | 272 | throw new \Exception( |
| 273 | 273 | sprintf( |
| 274 | 274 | 'App for id %s has a wrong app ID in info.xml: %s', |
| 275 | 275 | $appId, |
| 276 | - (string)$xml->id |
|
| 276 | + (string) $xml->id |
|
| 277 | 277 | ) |
| 278 | 278 | ); |
| 279 | 279 | } |
| 280 | 280 | |
| 281 | 281 | // Check if the version is lower than before |
| 282 | 282 | $currentVersion = OC_App::getAppVersion($appId); |
| 283 | - $newVersion = (string)$xml->version; |
|
| 284 | - if(version_compare($currentVersion, $newVersion) === 1) { |
|
| 283 | + $newVersion = (string) $xml->version; |
|
| 284 | + if (version_compare($currentVersion, $newVersion) === 1) { |
|
| 285 | 285 | throw new \Exception( |
| 286 | 286 | sprintf( |
| 287 | 287 | 'App for id %s has version %s and tried to update to lower version %s', |
@@ -292,12 +292,12 @@ discard block |
||
| 292 | 292 | ); |
| 293 | 293 | } |
| 294 | 294 | |
| 295 | - $baseDir = OC_App::getInstallPath() . '/' . $appId; |
|
| 295 | + $baseDir = OC_App::getInstallPath().'/'.$appId; |
|
| 296 | 296 | // Remove old app with the ID if existent |
| 297 | 297 | OC_Helper::rmdirr($baseDir); |
| 298 | 298 | // Move to app folder |
| 299 | - if(@mkdir($baseDir)) { |
|
| 300 | - $extractDir .= '/' . $folders[0]; |
|
| 299 | + if (@mkdir($baseDir)) { |
|
| 300 | + $extractDir .= '/'.$folders[0]; |
|
| 301 | 301 | OC_Helper::copyr($extractDir, $baseDir); |
| 302 | 302 | } |
| 303 | 303 | OC_Helper::copyr($extractDir, $baseDir); |
@@ -357,8 +357,8 @@ discard block |
||
| 357 | 357 | } |
| 358 | 358 | |
| 359 | 359 | $apps = $appFetcher->get(); |
| 360 | - foreach($apps as $app) { |
|
| 361 | - if($app['id'] === $appId) { |
|
| 360 | + foreach ($apps as $app) { |
|
| 361 | + if ($app['id'] === $appId) { |
|
| 362 | 362 | $currentVersion = OC_App::getAppVersion($appId); |
| 363 | 363 | $newestVersion = $app['releases'][0]['version']; |
| 364 | 364 | if (version_compare($newestVersion, $currentVersion, '>')) { |
@@ -380,7 +380,7 @@ discard block |
||
| 380 | 380 | * The function will check if the app is already downloaded in the apps repository |
| 381 | 381 | */ |
| 382 | 382 | public function isDownloaded($name) { |
| 383 | - foreach(\OC::$APPSROOTS as $dir) { |
|
| 383 | + foreach (\OC::$APPSROOTS as $dir) { |
|
| 384 | 384 | $dirToTest = $dir['path']; |
| 385 | 385 | $dirToTest .= '/'; |
| 386 | 386 | $dirToTest .= $name; |
@@ -408,11 +408,11 @@ discard block |
||
| 408 | 408 | * this has to be done by the function oc_app_uninstall(). |
| 409 | 409 | */ |
| 410 | 410 | public function removeApp($appId) { |
| 411 | - if($this->isDownloaded( $appId )) { |
|
| 412 | - $appDir = OC_App::getInstallPath() . '/' . $appId; |
|
| 411 | + if ($this->isDownloaded($appId)) { |
|
| 412 | + $appDir = OC_App::getInstallPath().'/'.$appId; |
|
| 413 | 413 | OC_Helper::rmdirr($appDir); |
| 414 | 414 | return true; |
| 415 | - }else{ |
|
| 415 | + } else { |
|
| 416 | 416 | \OCP\Util::writeLog('core', 'can\'t remove app '.$appId.'. It is not installed.', \OCP\Util::ERROR); |
| 417 | 417 | |
| 418 | 418 | return false; |
@@ -430,13 +430,13 @@ discard block |
||
| 430 | 430 | */ |
| 431 | 431 | public static function installShippedApps($softErrors = false) { |
| 432 | 432 | $errors = []; |
| 433 | - foreach(\OC::$APPSROOTS as $app_dir) { |
|
| 434 | - if($dir = opendir( $app_dir['path'] )) { |
|
| 435 | - while( false !== ( $filename = readdir( $dir ))) { |
|
| 436 | - if( substr( $filename, 0, 1 ) != '.' and is_dir($app_dir['path']."/$filename") ) { |
|
| 437 | - if( file_exists( $app_dir['path']."/$filename/appinfo/info.xml" )) { |
|
| 438 | - if(!Installer::isInstalled($filename)) { |
|
| 439 | - $info=OC_App::getAppInfo($filename); |
|
| 433 | + foreach (\OC::$APPSROOTS as $app_dir) { |
|
| 434 | + if ($dir = opendir($app_dir['path'])) { |
|
| 435 | + while (false !== ($filename = readdir($dir))) { |
|
| 436 | + if (substr($filename, 0, 1) != '.' and is_dir($app_dir['path']."/$filename")) { |
|
| 437 | + if (file_exists($app_dir['path']."/$filename/appinfo/info.xml")) { |
|
| 438 | + if (!Installer::isInstalled($filename)) { |
|
| 439 | + $info = OC_App::getAppInfo($filename); |
|
| 440 | 440 | $enabled = isset($info['default_enable']); |
| 441 | 441 | if (($enabled || in_array($filename, \OC::$server->getAppManager()->getAlwaysEnabledApps())) |
| 442 | 442 | && \OC::$server->getConfig()->getAppValue($filename, 'enabled') !== 'no') { |
@@ -459,7 +459,7 @@ discard block |
||
| 459 | 459 | } |
| 460 | 460 | } |
| 461 | 461 | } |
| 462 | - closedir( $dir ); |
|
| 462 | + closedir($dir); |
|
| 463 | 463 | } |
| 464 | 464 | } |
| 465 | 465 | |
@@ -474,12 +474,12 @@ discard block |
||
| 474 | 474 | public static function installShippedApp($app) { |
| 475 | 475 | //install the database |
| 476 | 476 | $appPath = OC_App::getAppPath($app); |
| 477 | - if(is_file("$appPath/appinfo/database.xml")) { |
|
| 477 | + if (is_file("$appPath/appinfo/database.xml")) { |
|
| 478 | 478 | try { |
| 479 | 479 | OC_DB::createDbFromStructure("$appPath/appinfo/database.xml"); |
| 480 | 480 | } catch (TableExistsException $e) { |
| 481 | 481 | throw new HintException( |
| 482 | - 'Failed to enable app ' . $app, |
|
| 482 | + 'Failed to enable app '.$app, |
|
| 483 | 483 | 'Please ask for help via one of our <a href="https://nextcloud.com/support/" target="_blank" rel="noreferrer">support channels</a>.', |
| 484 | 484 | 0, $e |
| 485 | 485 | ); |
@@ -506,16 +506,16 @@ discard block |
||
| 506 | 506 | } |
| 507 | 507 | |
| 508 | 508 | //set remote/public handlers |
| 509 | - foreach($info['remote'] as $name=>$path) { |
|
| 509 | + foreach ($info['remote'] as $name=>$path) { |
|
| 510 | 510 | $config->setAppValue('core', 'remote_'.$name, $app.'/'.$path); |
| 511 | 511 | } |
| 512 | - foreach($info['public'] as $name=>$path) { |
|
| 512 | + foreach ($info['public'] as $name=>$path) { |
|
| 513 | 513 | $config->setAppValue('core', 'public_'.$name, $app.'/'.$path); |
| 514 | 514 | } |
| 515 | 515 | |
| 516 | 516 | OC_App::setAppTypes($info['id']); |
| 517 | 517 | |
| 518 | - if(isset($info['settings']) && is_array($info['settings'])) { |
|
| 518 | + if (isset($info['settings']) && is_array($info['settings'])) { |
|
| 519 | 519 | // requires that autoloading was registered for the app, |
| 520 | 520 | // as happens before running the install.php some lines above |
| 521 | 521 | \OC::$server->getSettingsManager()->setupSettings($info['settings']); |
@@ -531,7 +531,7 @@ discard block |
||
| 531 | 531 | */ |
| 532 | 532 | public static function checkCode($folder) { |
| 533 | 533 | // is the code checker enabled? |
| 534 | - if(!\OC::$server->getConfig()->getSystemValue('appcodechecker', false)) { |
|
| 534 | + if (!\OC::$server->getConfig()->getSystemValue('appcodechecker', false)) { |
|
| 535 | 535 | return true; |
| 536 | 536 | } |
| 537 | 537 | |
@@ -545,7 +545,7 @@ discard block |
||
| 545 | 545 | * @param string $script |
| 546 | 546 | */ |
| 547 | 547 | private static function includeAppScript($script) { |
| 548 | - if ( file_exists($script) ){ |
|
| 548 | + if (file_exists($script)) { |
|
| 549 | 549 | include $script; |
| 550 | 550 | } |
| 551 | 551 | } |
@@ -412,7 +412,7 @@ |
||
| 412 | 412 | $appDir = OC_App::getInstallPath() . '/' . $appId; |
| 413 | 413 | OC_Helper::rmdirr($appDir); |
| 414 | 414 | return true; |
| 415 | - }else{ |
|
| 415 | + } else{ |
|
| 416 | 416 | \OCP\Util::writeLog('core', 'can\'t remove app '.$appId.'. It is not installed.', \OCP\Util::ERROR); |
| 417 | 417 | |
| 418 | 418 | return false; |
@@ -28,7 +28,6 @@ |
||
| 28 | 28 | use OCP\IUserManager; |
| 29 | 29 | use OCP\Util; |
| 30 | 30 | use Symfony\Component\EventDispatcher\EventDispatcher; |
| 31 | -use Symfony\Component\EventDispatcher\GenericEvent; |
|
| 32 | 31 | |
| 33 | 32 | class HookManager { |
| 34 | 33 | |
@@ -32,115 +32,115 @@ |
||
| 32 | 32 | |
| 33 | 33 | class HookManager { |
| 34 | 34 | |
| 35 | - /** @var IUserManager */ |
|
| 36 | - private $userManager; |
|
| 37 | - |
|
| 38 | - /** @var SyncService */ |
|
| 39 | - private $syncService; |
|
| 40 | - |
|
| 41 | - /** @var IUser[] */ |
|
| 42 | - private $usersToDelete; |
|
| 43 | - |
|
| 44 | - /** @var CalDavBackend */ |
|
| 45 | - private $calDav; |
|
| 46 | - |
|
| 47 | - /** @var CardDavBackend */ |
|
| 48 | - private $cardDav; |
|
| 49 | - |
|
| 50 | - /** @var array */ |
|
| 51 | - private $calendarsToDelete; |
|
| 52 | - |
|
| 53 | - /** @var array */ |
|
| 54 | - private $addressBooksToDelete; |
|
| 55 | - |
|
| 56 | - /** @var EventDispatcher */ |
|
| 57 | - private $eventDispatcher; |
|
| 58 | - |
|
| 59 | - public function __construct(IUserManager $userManager, |
|
| 60 | - SyncService $syncService, |
|
| 61 | - CalDavBackend $calDav, |
|
| 62 | - CardDavBackend $cardDav, |
|
| 63 | - EventDispatcher $eventDispatcher) { |
|
| 64 | - $this->userManager = $userManager; |
|
| 65 | - $this->syncService = $syncService; |
|
| 66 | - $this->calDav = $calDav; |
|
| 67 | - $this->cardDav = $cardDav; |
|
| 68 | - $this->eventDispatcher = $eventDispatcher; |
|
| 69 | - } |
|
| 70 | - |
|
| 71 | - public function setup() { |
|
| 72 | - Util::connectHook('OC_User', |
|
| 73 | - 'post_createUser', |
|
| 74 | - $this, |
|
| 75 | - 'postCreateUser'); |
|
| 76 | - Util::connectHook('OC_User', |
|
| 77 | - 'pre_deleteUser', |
|
| 78 | - $this, |
|
| 79 | - 'preDeleteUser'); |
|
| 80 | - Util::connectHook('OC_User', |
|
| 81 | - 'post_deleteUser', |
|
| 82 | - $this, |
|
| 83 | - 'postDeleteUser'); |
|
| 84 | - Util::connectHook('OC_User', |
|
| 85 | - 'changeUser', |
|
| 86 | - $this, |
|
| 87 | - 'changeUser'); |
|
| 88 | - } |
|
| 89 | - |
|
| 90 | - public function postCreateUser($params) { |
|
| 91 | - $user = $this->userManager->get($params['uid']); |
|
| 92 | - $this->syncService->updateUser($user); |
|
| 93 | - } |
|
| 94 | - |
|
| 95 | - public function preDeleteUser($params) { |
|
| 96 | - $uid = $params['uid']; |
|
| 97 | - $this->usersToDelete[$uid] = $this->userManager->get($uid); |
|
| 98 | - $this->calendarsToDelete = $this->calDav->getUsersOwnCalendars('principals/users/' . $uid); |
|
| 99 | - $this->addressBooksToDelete = $this->cardDav->getUsersOwnAddressBooks('principals/users/' . $uid); |
|
| 100 | - } |
|
| 101 | - |
|
| 102 | - public function postDeleteUser($params) { |
|
| 103 | - $uid = $params['uid']; |
|
| 104 | - if (isset($this->usersToDelete[$uid])){ |
|
| 105 | - $this->syncService->deleteUser($this->usersToDelete[$uid]); |
|
| 106 | - } |
|
| 107 | - |
|
| 108 | - foreach ($this->calendarsToDelete as $calendar) { |
|
| 109 | - $this->calDav->deleteCalendar($calendar['id']); |
|
| 110 | - } |
|
| 111 | - $this->calDav->deleteAllSharesByUser('principals/users/' . $uid); |
|
| 112 | - |
|
| 113 | - foreach ($this->addressBooksToDelete as $addressBook) { |
|
| 114 | - $this->cardDav->deleteAddressBook($addressBook['id']); |
|
| 115 | - } |
|
| 116 | - } |
|
| 117 | - |
|
| 118 | - public function changeUser($params) { |
|
| 119 | - $user = $params['user']; |
|
| 120 | - $this->syncService->updateUser($user); |
|
| 121 | - } |
|
| 122 | - |
|
| 123 | - public function firstLogin(IUser $user = null) { |
|
| 124 | - if (!is_null($user)) { |
|
| 125 | - $principal = 'principals/users/' . $user->getUID(); |
|
| 126 | - if ($this->calDav->getCalendarsForUserCount($principal) === 0) { |
|
| 127 | - try { |
|
| 128 | - $this->calDav->createCalendar($principal, CalDavBackend::PERSONAL_CALENDAR_URI, [ |
|
| 129 | - '{DAV:}displayname' => CalDavBackend::PERSONAL_CALENDAR_NAME, |
|
| 130 | - ]); |
|
| 131 | - } catch (\Exception $ex) { |
|
| 132 | - \OC::$server->getLogger()->logException($ex); |
|
| 133 | - } |
|
| 134 | - } |
|
| 135 | - if ($this->cardDav->getAddressBooksForUserCount($principal) === 0) { |
|
| 136 | - try { |
|
| 137 | - $this->cardDav->createAddressBook($principal, CardDavBackend::PERSONAL_ADDRESSBOOK_URI, [ |
|
| 138 | - '{DAV:}displayname' => CardDavBackend::PERSONAL_ADDRESSBOOK_NAME, |
|
| 139 | - ]); |
|
| 140 | - } catch (\Exception $ex) { |
|
| 141 | - \OC::$server->getLogger()->logException($ex); |
|
| 142 | - } |
|
| 143 | - } |
|
| 144 | - } |
|
| 145 | - } |
|
| 35 | + /** @var IUserManager */ |
|
| 36 | + private $userManager; |
|
| 37 | + |
|
| 38 | + /** @var SyncService */ |
|
| 39 | + private $syncService; |
|
| 40 | + |
|
| 41 | + /** @var IUser[] */ |
|
| 42 | + private $usersToDelete; |
|
| 43 | + |
|
| 44 | + /** @var CalDavBackend */ |
|
| 45 | + private $calDav; |
|
| 46 | + |
|
| 47 | + /** @var CardDavBackend */ |
|
| 48 | + private $cardDav; |
|
| 49 | + |
|
| 50 | + /** @var array */ |
|
| 51 | + private $calendarsToDelete; |
|
| 52 | + |
|
| 53 | + /** @var array */ |
|
| 54 | + private $addressBooksToDelete; |
|
| 55 | + |
|
| 56 | + /** @var EventDispatcher */ |
|
| 57 | + private $eventDispatcher; |
|
| 58 | + |
|
| 59 | + public function __construct(IUserManager $userManager, |
|
| 60 | + SyncService $syncService, |
|
| 61 | + CalDavBackend $calDav, |
|
| 62 | + CardDavBackend $cardDav, |
|
| 63 | + EventDispatcher $eventDispatcher) { |
|
| 64 | + $this->userManager = $userManager; |
|
| 65 | + $this->syncService = $syncService; |
|
| 66 | + $this->calDav = $calDav; |
|
| 67 | + $this->cardDav = $cardDav; |
|
| 68 | + $this->eventDispatcher = $eventDispatcher; |
|
| 69 | + } |
|
| 70 | + |
|
| 71 | + public function setup() { |
|
| 72 | + Util::connectHook('OC_User', |
|
| 73 | + 'post_createUser', |
|
| 74 | + $this, |
|
| 75 | + 'postCreateUser'); |
|
| 76 | + Util::connectHook('OC_User', |
|
| 77 | + 'pre_deleteUser', |
|
| 78 | + $this, |
|
| 79 | + 'preDeleteUser'); |
|
| 80 | + Util::connectHook('OC_User', |
|
| 81 | + 'post_deleteUser', |
|
| 82 | + $this, |
|
| 83 | + 'postDeleteUser'); |
|
| 84 | + Util::connectHook('OC_User', |
|
| 85 | + 'changeUser', |
|
| 86 | + $this, |
|
| 87 | + 'changeUser'); |
|
| 88 | + } |
|
| 89 | + |
|
| 90 | + public function postCreateUser($params) { |
|
| 91 | + $user = $this->userManager->get($params['uid']); |
|
| 92 | + $this->syncService->updateUser($user); |
|
| 93 | + } |
|
| 94 | + |
|
| 95 | + public function preDeleteUser($params) { |
|
| 96 | + $uid = $params['uid']; |
|
| 97 | + $this->usersToDelete[$uid] = $this->userManager->get($uid); |
|
| 98 | + $this->calendarsToDelete = $this->calDav->getUsersOwnCalendars('principals/users/' . $uid); |
|
| 99 | + $this->addressBooksToDelete = $this->cardDav->getUsersOwnAddressBooks('principals/users/' . $uid); |
|
| 100 | + } |
|
| 101 | + |
|
| 102 | + public function postDeleteUser($params) { |
|
| 103 | + $uid = $params['uid']; |
|
| 104 | + if (isset($this->usersToDelete[$uid])){ |
|
| 105 | + $this->syncService->deleteUser($this->usersToDelete[$uid]); |
|
| 106 | + } |
|
| 107 | + |
|
| 108 | + foreach ($this->calendarsToDelete as $calendar) { |
|
| 109 | + $this->calDav->deleteCalendar($calendar['id']); |
|
| 110 | + } |
|
| 111 | + $this->calDav->deleteAllSharesByUser('principals/users/' . $uid); |
|
| 112 | + |
|
| 113 | + foreach ($this->addressBooksToDelete as $addressBook) { |
|
| 114 | + $this->cardDav->deleteAddressBook($addressBook['id']); |
|
| 115 | + } |
|
| 116 | + } |
|
| 117 | + |
|
| 118 | + public function changeUser($params) { |
|
| 119 | + $user = $params['user']; |
|
| 120 | + $this->syncService->updateUser($user); |
|
| 121 | + } |
|
| 122 | + |
|
| 123 | + public function firstLogin(IUser $user = null) { |
|
| 124 | + if (!is_null($user)) { |
|
| 125 | + $principal = 'principals/users/' . $user->getUID(); |
|
| 126 | + if ($this->calDav->getCalendarsForUserCount($principal) === 0) { |
|
| 127 | + try { |
|
| 128 | + $this->calDav->createCalendar($principal, CalDavBackend::PERSONAL_CALENDAR_URI, [ |
|
| 129 | + '{DAV:}displayname' => CalDavBackend::PERSONAL_CALENDAR_NAME, |
|
| 130 | + ]); |
|
| 131 | + } catch (\Exception $ex) { |
|
| 132 | + \OC::$server->getLogger()->logException($ex); |
|
| 133 | + } |
|
| 134 | + } |
|
| 135 | + if ($this->cardDav->getAddressBooksForUserCount($principal) === 0) { |
|
| 136 | + try { |
|
| 137 | + $this->cardDav->createAddressBook($principal, CardDavBackend::PERSONAL_ADDRESSBOOK_URI, [ |
|
| 138 | + '{DAV:}displayname' => CardDavBackend::PERSONAL_ADDRESSBOOK_NAME, |
|
| 139 | + ]); |
|
| 140 | + } catch (\Exception $ex) { |
|
| 141 | + \OC::$server->getLogger()->logException($ex); |
|
| 142 | + } |
|
| 143 | + } |
|
| 144 | + } |
|
| 145 | + } |
|
| 146 | 146 | } |
@@ -95,20 +95,20 @@ discard block |
||
| 95 | 95 | public function preDeleteUser($params) { |
| 96 | 96 | $uid = $params['uid']; |
| 97 | 97 | $this->usersToDelete[$uid] = $this->userManager->get($uid); |
| 98 | - $this->calendarsToDelete = $this->calDav->getUsersOwnCalendars('principals/users/' . $uid); |
|
| 99 | - $this->addressBooksToDelete = $this->cardDav->getUsersOwnAddressBooks('principals/users/' . $uid); |
|
| 98 | + $this->calendarsToDelete = $this->calDav->getUsersOwnCalendars('principals/users/'.$uid); |
|
| 99 | + $this->addressBooksToDelete = $this->cardDav->getUsersOwnAddressBooks('principals/users/'.$uid); |
|
| 100 | 100 | } |
| 101 | 101 | |
| 102 | 102 | public function postDeleteUser($params) { |
| 103 | 103 | $uid = $params['uid']; |
| 104 | - if (isset($this->usersToDelete[$uid])){ |
|
| 104 | + if (isset($this->usersToDelete[$uid])) { |
|
| 105 | 105 | $this->syncService->deleteUser($this->usersToDelete[$uid]); |
| 106 | 106 | } |
| 107 | 107 | |
| 108 | 108 | foreach ($this->calendarsToDelete as $calendar) { |
| 109 | 109 | $this->calDav->deleteCalendar($calendar['id']); |
| 110 | 110 | } |
| 111 | - $this->calDav->deleteAllSharesByUser('principals/users/' . $uid); |
|
| 111 | + $this->calDav->deleteAllSharesByUser('principals/users/'.$uid); |
|
| 112 | 112 | |
| 113 | 113 | foreach ($this->addressBooksToDelete as $addressBook) { |
| 114 | 114 | $this->cardDav->deleteAddressBook($addressBook['id']); |
@@ -122,7 +122,7 @@ discard block |
||
| 122 | 122 | |
| 123 | 123 | public function firstLogin(IUser $user = null) { |
| 124 | 124 | if (!is_null($user)) { |
| 125 | - $principal = 'principals/users/' . $user->getUID(); |
|
| 125 | + $principal = 'principals/users/'.$user->getUID(); |
|
| 126 | 126 | if ($this->calDav->getCalendarsForUserCount($principal) === 0) { |
| 127 | 127 | try { |
| 128 | 128 | $this->calDav->createCalendar($principal, CalDavBackend::PERSONAL_CALENDAR_URI, [ |