/** * rename a file * * @param string $dir * @param string $oldname * @param string $newname * @return array */ public function rename($dir, $oldname, $newname) { $result = array('success' => false, 'data' => NULL); $normalizedOldPath = \OC\Files\Filesystem::normalizePath($dir . '/' . $oldname); $normalizedNewPath = \OC\Files\Filesystem::normalizePath($dir . '/' . $newname); // rename to non-existing folder is denied if (!$this->view->file_exists($normalizedOldPath)) { $result['data'] = array('message' => $this->l10n->t('%s could not be renamed as it has been deleted', array($oldname)), 'code' => 'sourcenotfound', 'oldname' => $oldname, 'newname' => $newname); } else { if (!$this->view->file_exists($dir)) { $result['data'] = array('message' => (string) $this->l10n->t('The target folder has been moved or deleted.', array($dir)), 'code' => 'targetnotfound'); // rename to existing file is denied } else { if ($this->view->file_exists($normalizedNewPath)) { $result['data'] = array('message' => $this->l10n->t("The name %s is already used in the folder %s. Please choose a different name.", array($newname, $dir))); } else { if ($newname !== '.' and $this->view->rename($normalizedOldPath, $normalizedNewPath)) { // successful rename $meta = $this->view->getFileInfo($normalizedNewPath); $meta = \OCA\Files\Helper::populateTags(array($meta)); $fileInfo = \OCA\Files\Helper::formatFileInfo(current($meta)); $fileInfo['path'] = dirname($normalizedNewPath); $result['success'] = true; $result['data'] = $fileInfo; } else { // rename failed $result['data'] = array('message' => $this->l10n->t('%s could not be renamed', array($oldname))); } } } } return $result; }
/** * Get the template for a specific activity-event in the activities * * @param array $activity An array with all the activity data in it * @return string */ public function show($activity) { $tmpl = new Template('activity', 'stream.item'); $tmpl->assign('formattedDate', $this->dateTimeFormatter->formatDateTime($activity['timestamp'])); $tmpl->assign('formattedTimestamp', Template::relative_modified_date($activity['timestamp'])); if (strpos($activity['subjectformatted']['markup']['trimmed'], '<a ') !== false) { // We do not link the subject as we create links for the parameters instead $activity['link'] = ''; } $tmpl->assign('event', $activity); if ($activity['file']) { $this->view->chroot('/' . $activity['affecteduser'] . '/files'); $exist = $this->view->file_exists($activity['file']); $is_dir = $this->view->is_dir($activity['file']); $tmpl->assign('previewLink', $this->getPreviewLink($activity['file'], $is_dir)); // show a preview image if the file still exists $mimeType = Files::getMimeType($activity['file']); if ($mimeType && !$is_dir && $this->preview->isMimeSupported($mimeType) && $exist) { $tmpl->assign('previewImageLink', $this->urlGenerator->linkToRoute('core_ajax_preview', array('file' => $activity['file'], 'x' => 150, 'y' => 150))); } else { $mimeTypeIcon = Template::mimetype_icon($is_dir ? 'dir' : $mimeType); $mimeTypeIcon = substr($mimeTypeIcon, -4) === '.png' ? substr($mimeTypeIcon, 0, -4) . '.svg' : $mimeTypeIcon; $tmpl->assign('previewImageLink', $mimeTypeIcon); $tmpl->assign('previewLinkIsDir', true); } } return $tmpl->fetchPage(); }
/** * @param string $user * @param string $path * @return array */ protected function findInfoByPath($user, $path) { $this->view->chroot('/' . $user . '/files'); $exists = $this->view->file_exists($path); $this->cachePath[$user][$path] = ['path' => $path, 'exists' => $exists, 'is_dir' => $exists ? $this->view->is_dir($path) : false, 'view' => '']; return $this->cachePath[$user][$path]; }
/** * rename a file * * @param string $dir * @param string $oldname * @param string $newname * @return array */ public function rename($dir, $oldname, $newname) { $result = array('success' => false, 'data' => NULL); // rename to "/Shared" is denied if ($dir === '/' and $newname === 'Shared') { $result['data'] = array('message' => $this->l10n->t("Invalid folder name. Usage of 'Shared' is reserved.")); // rename to existing file is denied } else { if ($this->view->file_exists($dir . '/' . $newname)) { $result['data'] = array('message' => $this->l10n->t("The name %s is already used in the folder %s. Please choose a different name.", array($newname, $dir))); } else { if ($newname !== '.' and !($dir === '/' and $oldname === 'Shared') and $this->view->rename($dir . '/' . $oldname, $dir . '/' . $newname)) { // successful rename $meta = $this->view->getFileInfo($dir . '/' . $newname); if ($meta['mimetype'] === 'httpd/unix-directory') { $meta['type'] = 'dir'; } else { $meta['type'] = 'file'; } $fileinfo = array('id' => $meta['fileid'], 'mime' => $meta['mimetype'], 'size' => $meta['size'], 'etag' => $meta['etag'], 'directory' => $dir, 'name' => $newname, 'isPreviewAvailable' => \OC::$server->getPreviewManager()->isMimeSupported($meta['mimetype']), 'icon' => \OCA\Files\Helper::determineIcon($meta)); $result['success'] = true; $result['data'] = $fileinfo; } else { // rename failed $result['data'] = array('message' => $this->l10n->t('%s could not be renamed', array($oldname))); } } } return $result; }
protected function verifyNewKeyPath($uid) { // private key if ($uid !== '') { $this->assertTrue($this->view->file_exists($uid . '/files_encryption/' . $this->moduleId . '/' . $uid . '.privateKey')); } // file keys $this->assertTrue($this->view->file_exists($uid . '/files_encryption/keys/files/folder1/folder2/folder3/file3/' . $this->moduleId . '/fileKey')); $this->assertTrue($this->view->file_exists($uid . '/files_encryption/keys/files/folder1/folder2/file2/' . $this->moduleId . '/fileKey')); $this->assertTrue($this->view->file_exists($uid . '/files_encryption/keys/files/folder1/file.1/' . $this->moduleId . '/fileKey')); $this->assertTrue($this->view->file_exists($uid . '/files_encryption/keys/files/folder2/file.2.1/' . $this->moduleId . '/fileKey')); // share keys $this->assertTrue($this->view->file_exists($uid . '/files_encryption/keys/files/folder1/folder2/folder3/file3/' . $this->moduleId . '/' . self::TEST_ENCRYPTION_MIGRATION_USER1 . '.shareKey')); $this->assertTrue($this->view->file_exists($uid . '/files_encryption/keys/files/folder1/folder2/folder3/file3/' . $this->moduleId . '/' . self::TEST_ENCRYPTION_MIGRATION_USER2 . '.shareKey')); $this->assertTrue($this->view->file_exists($uid . '/files_encryption/keys/files/folder1/folder2/folder3/file3/' . $this->moduleId . '/' . self::TEST_ENCRYPTION_MIGRATION_USER3 . '.shareKey')); $this->assertTrue($this->view->file_exists($uid . '/files_encryption/keys/files/folder1/folder2/file2/' . $this->moduleId . '/' . self::TEST_ENCRYPTION_MIGRATION_USER1 . '.shareKey')); $this->assertTrue($this->view->file_exists($uid . '/files_encryption/keys/files/folder1/folder2/file2/' . $this->moduleId . '/' . self::TEST_ENCRYPTION_MIGRATION_USER2 . '.shareKey')); $this->assertTrue($this->view->file_exists($uid . '/files_encryption/keys/files/folder1/folder2/file2/' . $this->moduleId . '/' . self::TEST_ENCRYPTION_MIGRATION_USER3 . '.shareKey')); $this->assertTrue($this->view->file_exists($uid . '/files_encryption/keys/files/folder1/file.1/' . $this->moduleId . '/' . self::TEST_ENCRYPTION_MIGRATION_USER1 . '.shareKey')); $this->assertTrue($this->view->file_exists($uid . '/files_encryption/keys/files/folder1/file.1/' . $this->moduleId . '/' . self::TEST_ENCRYPTION_MIGRATION_USER2 . '.shareKey')); $this->assertTrue($this->view->file_exists($uid . '/files_encryption/keys/files/folder1/file.1/' . $this->moduleId . '/' . self::TEST_ENCRYPTION_MIGRATION_USER3 . '.shareKey')); $this->assertTrue($this->view->file_exists($uid . '/files_encryption/keys/files/folder2/file.2.1/' . $this->moduleId . '/' . self::TEST_ENCRYPTION_MIGRATION_USER1 . '.shareKey')); $this->assertTrue($this->view->file_exists($uid . '/files_encryption/keys/files/folder2/file.2.1/' . $this->moduleId . '/' . self::TEST_ENCRYPTION_MIGRATION_USER2 . '.shareKey')); $this->assertTrue($this->view->file_exists($uid . '/files_encryption/keys/files/folder2/file.2.1/' . $this->moduleId . '/' . self::TEST_ENCRYPTION_MIGRATION_USER3 . '.shareKey')); if ($this->public_share_key_id) { $this->assertTrue($this->view->file_exists($uid . '/files_encryption/keys/files/folder2/file.2.1/' . $this->moduleId . '/' . $this->public_share_key_id . '.shareKey')); } if ($this->recovery_key_id) { $this->assertTrue($this->view->file_exists($uid . '/files_encryption/keys/files/folder2/file.2.1/' . $this->moduleId . '/' . $this->recovery_key_id . '.shareKey')); } }
public function testSizePropagationWhenRecipientChangesFile() { $this->loginHelper(self::TEST_FILES_SHARING_API_USER1); $recipientView = new View('/' . self::TEST_FILES_SHARING_API_USER1 . '/files'); $this->loginHelper(self::TEST_FILES_SHARING_API_USER2); $ownerView = new View('/' . self::TEST_FILES_SHARING_API_USER2 . '/files'); $ownerView->mkdir('/sharedfolder/subfolder'); $ownerView->file_put_contents('/sharedfolder/subfolder/foo.txt', 'bar'); $sharedFolderInfo = $ownerView->getFileInfo('/sharedfolder', false); $this->assertInstanceOf('\\OC\\Files\\FileInfo', $sharedFolderInfo); \OCP\Share::shareItem('folder', $sharedFolderInfo->getId(), \OCP\Share::SHARE_TYPE_USER, self::TEST_FILES_SHARING_API_USER1, 31); $ownerRootInfo = $ownerView->getFileInfo('', false); $this->loginHelper(self::TEST_FILES_SHARING_API_USER1); $this->assertTrue($recipientView->file_exists('/sharedfolder/subfolder/foo.txt')); $recipientRootInfo = $recipientView->getFileInfo('', false); // when file changed as recipient $recipientView->file_put_contents('/sharedfolder/subfolder/foo.txt', 'foobar'); // size of recipient's root stays the same $newRecipientRootInfo = $recipientView->getFileInfo('', false); $this->assertEquals($recipientRootInfo->getSize(), $newRecipientRootInfo->getSize()); // size of owner's root increases $this->loginHelper(self::TEST_FILES_SHARING_API_USER2); $newOwnerRootInfo = $ownerView->getFileInfo('', false); $this->assertEquals($ownerRootInfo->getSize() + 3, $newOwnerRootInfo->getSize()); }
private function doTestRestore() { $filePath = self::TEST_VERSIONS_USER . '/files/sub/test.txt'; $this->rootView->file_put_contents($filePath, 'test file'); $t0 = $this->rootView->filemtime($filePath); // not exactly the same timestamp as the file $t1 = time() - 60; // second version is two weeks older $t2 = $t1 - 60 * 60 * 24 * 14; // create some versions $v1 = self::USERS_VERSIONS_ROOT . '/sub/test.txt.v' . $t1; $v2 = self::USERS_VERSIONS_ROOT . '/sub/test.txt.v' . $t2; $this->rootView->mkdir(self::USERS_VERSIONS_ROOT . '/sub'); $this->rootView->file_put_contents($v1, 'version1'); $this->rootView->file_put_contents($v2, 'version2'); $oldVersions = \OCA\Files_Versions\Storage::getVersions(self::TEST_VERSIONS_USER, '/sub/test.txt'); $this->assertCount(2, $oldVersions); $this->assertEquals('test file', $this->rootView->file_get_contents($filePath)); $info1 = $this->rootView->getFileInfo($filePath); \OCA\Files_Versions\Storage::rollback('sub/test.txt', $t2); $this->assertEquals('version2', $this->rootView->file_get_contents($filePath)); $info2 = $this->rootView->getFileInfo($filePath); $this->assertNotEquals($info2['etag'], $info1['etag'], 'Etag must change after rolling back version'); $this->assertEquals($info2['fileid'], $info1['fileid'], 'File id must not change after rolling back version'); $this->assertEquals($info2['mtime'], $t2, 'Restored file has mtime from version'); $newVersions = \OCA\Files_Versions\Storage::getVersions(self::TEST_VERSIONS_USER, '/sub/test.txt'); $this->assertTrue($this->rootView->file_exists(self::USERS_VERSIONS_ROOT . '/sub/test.txt.v' . $t0), 'A version file was created for the file before restoration'); $this->assertTrue($this->rootView->file_exists($v1), 'Untouched version file is still there'); $this->assertFalse($this->rootView->file_exists($v2), 'Restored version file gone from files_version folder'); $this->assertCount(2, $newVersions, 'Additional version created'); $this->assertTrue(isset($newVersions[$t0 . '#' . 'test.txt']), 'A version was created for the file before restoration'); $this->assertTrue(isset($newVersions[$t1 . '#' . 'test.txt']), 'Untouched version is still there'); $this->assertFalse(isset($newVersions[$t2 . '#' . 'test.txt']), 'Restored version is not in the list any more'); }
/** * @return bool */ public function stream_close() { $this->flush(); // if there is no valid private key return false if ($this->privateKey === false) { // cleanup if ($this->meta['mode'] !== 'r' && $this->meta['mode'] !== 'rb' && !$this->isLocalTmpFile) { // Disable encryption proxy to prevent recursive calls $proxyStatus = \OC_FileProxy::$enabled; \OC_FileProxy::$enabled = false; if ($this->rootView->file_exists($this->rawPath) && $this->size === 0) { $this->rootView->unlink($this->rawPath); } // Re-enable proxy - our work is done \OC_FileProxy::$enabled = $proxyStatus; } // if private key is not valid redirect user to a error page \OCA\Encryption\Helper::redirectToErrorPage($this->session); } if ($this->meta['mode'] !== 'r' && $this->meta['mode'] !== 'rb' && $this->isLocalTmpFile === false && $this->size > 0 && $this->unencryptedSize > 0) { // only write keyfiles if it was a new file if ($this->newFile === true) { // Disable encryption proxy to prevent recursive calls $proxyStatus = \OC_FileProxy::$enabled; \OC_FileProxy::$enabled = false; // Fetch user's public key $this->publicKey = Keymanager::getPublicKey($this->rootView, $this->keyId); // Check if OC sharing api is enabled $sharingEnabled = \OCP\Share::isEnabled(); $util = new Util($this->rootView, $this->userId); // Get all users sharing the file includes current user $uniqueUserIds = $util->getSharingUsersArray($sharingEnabled, $this->relPath); $checkedUserIds = $util->filterShareReadyUsers($uniqueUserIds); // Fetch public keys for all sharing users $publicKeys = Keymanager::getPublicKeys($this->rootView, $checkedUserIds['ready']); // Encrypt enc key for all sharing users $this->encKeyfiles = Crypt::multiKeyEncrypt($this->plainKey, $publicKeys); // Save the new encrypted file key Keymanager::setFileKey($this->rootView, $util, $this->relPath, $this->encKeyfiles['data']); // Save the sharekeys Keymanager::setShareKeys($this->rootView, $util, $this->relPath, $this->encKeyfiles['keys']); // Re-enable proxy - our work is done \OC_FileProxy::$enabled = $proxyStatus; } // we need to update the file info for the real file, not for the // part file. $path = Helper::stripPartialFileExtension($this->rawPath); // get file info $fileInfo = $this->rootView->getFileInfo($path); if (is_array($fileInfo)) { // set encryption data $fileInfo['encrypted'] = true; $fileInfo['size'] = $this->size; $fileInfo['unencrypted_size'] = $this->unencryptedSize; // set fileinfo $this->rootView->putFileInfo($path, $fileInfo); } } return fclose($this->handle); }
public function testCopy() { \OC\Files\Filesystem::file_put_contents("test.txt", "test file"); $t1 = time(); // second version is two weeks older, this way we make sure that no // version will be expired $t2 = $t1 - 60 * 60 * 24 * 14; // create some versions $v1 = self::USERS_VERSIONS_ROOT . '/test.txt.v' . $t1; $v2 = self::USERS_VERSIONS_ROOT . '/test.txt.v' . $t2; $v1Copied = self::USERS_VERSIONS_ROOT . '/test2.txt.v' . $t1; $v2Copied = self::USERS_VERSIONS_ROOT . '/test2.txt.v' . $t2; $this->rootView->file_put_contents($v1, 'version1'); $this->rootView->file_put_contents($v2, 'version2'); // execute copy hook of versions app \OC\Files\Filesystem::copy("test.txt", "test2.txt"); $this->runCommands(); $this->assertTrue($this->rootView->file_exists($v1)); $this->assertTrue($this->rootView->file_exists($v2)); $this->assertTrue($this->rootView->file_exists($v1Copied)); $this->assertTrue($this->rootView->file_exists($v2Copied)); //cleanup \OC\Files\Filesystem::unlink('test.txt'); \OC\Files\Filesystem::unlink('test2.txt'); }
function testDeleteSharedFile() { // generate filename $filename = 'tmp-' . $this->getUniqueID() . '.txt'; // save file with content $cryptedFile = file_put_contents('crypt:///' . self::TEST_ENCRYPTION_TRASHBIN_USER1 . '/files/' . $filename, $this->dataShort); // test that data was successfully written $this->assertTrue(is_int($cryptedFile)); // get the file info from previous created file $fileInfo = $this->view->getFileInfo('/' . self::TEST_ENCRYPTION_TRASHBIN_USER1 . '/files/' . $filename); // check if we have a valid file info $this->assertTrue($fileInfo instanceof \OC\Files\FileInfo); // share the file $this->assertTrue(\OCP\Share::shareItem('file', $fileInfo['fileid'], \OCP\Share::SHARE_TYPE_USER, self::TEST_ENCRYPTION_TRASHBIN_USER2, OCP\PERMISSION_ALL)); self::loginHelper(self::TEST_ENCRYPTION_TRASHBIN_USER2); $this->assertTrue(\OC\Files\Filesystem::file_exists($filename)); \OCA\Files_Trashbin\Trashbin::move2trash($filename); $query = \OC_DB::prepare('SELECT `timestamp` FROM `*PREFIX*files_trash`' . ' WHERE `id`=?'); $result = $query->execute(array($filename))->fetchRow(); $this->assertNotEmpty($result); $timestamp = $result['timestamp']; // check if key for both users exists $this->assertTrue($this->view->file_exists('/' . self::TEST_ENCRYPTION_TRASHBIN_USER1 . '/files_trashbin/keyfiles/' . $filename . '.key.d' . $timestamp)); // check if key for admin exists $this->assertTrue($this->view->file_exists('/' . self::TEST_ENCRYPTION_TRASHBIN_USER2 . '/files_trashbin/keyfiles/' . $filename . '.key.d' . $timestamp)); // check if share key for both users exists $this->assertTrue($this->view->file_exists('/' . self::TEST_ENCRYPTION_TRASHBIN_USER1 . '/files_trashbin/share-keys/' . $filename . '.' . self::TEST_ENCRYPTION_TRASHBIN_USER1 . '.shareKey.d' . $timestamp)); $this->assertTrue($this->view->file_exists('/' . self::TEST_ENCRYPTION_TRASHBIN_USER2 . '/files_trashbin/share-keys/' . $filename . '.' . self::TEST_ENCRYPTION_TRASHBIN_USER2 . '.shareKey.d' . $timestamp)); }
/** * Get the template for a specific activity-event in the activities * * @param array $activity An array with all the activity data in it * @return string */ public static function show($activity) { $tmpl = new Template('activity', 'activity.box'); $tmpl->assign('formattedDate', Util::formatDate($activity['timestamp'])); $tmpl->assign('formattedTimestamp', \OCP\relative_modified_date($activity['timestamp'])); $tmpl->assign('user', $activity['user']); $tmpl->assign('displayName', User::getDisplayName($activity['user'])); if ($activity['app'] === 'files') { // We do not link the subject as we create links for the parameters instead $activity['link'] = ''; } $tmpl->assign('event', $activity); if ($activity['file']) { $rootView = new View('/' . $activity['affecteduser'] . '/files'); $exist = $rootView->file_exists($activity['file']); $is_dir = $rootView->is_dir($activity['file']); unset($rootView); // show a preview image if the file still exists $mimetype = \OC_Helper::getFileNameMimeType($activity['file']); if (!$is_dir && \OC::$server->getPreviewManager()->isMimeSupported($mimetype) && $exist) { $tmpl->assign('previewLink', Util::linkTo('files', 'index.php', array('dir' => dirname($activity['file'])))); $tmpl->assign('previewImageLink', Util::linkToRoute('core_ajax_preview', array('file' => $activity['file'], 'x' => 150, 'y' => 150))); } else { $tmpl->assign('previewLink', Util::linkTo('files', 'index.php', array('dir' => $activity['file']))); $tmpl->assign('previewImageLink', \OC_Helper::mimetypeIcon($is_dir ? 'dir' : $mimetype)); $tmpl->assign('previewLinkIsDir', true); } } return $tmpl->fetchPage(); }
/** * Test that versions are not auto-trashed when moving a file between * storages. This is because rename() between storages would call * unlink() which should NOT trigger the version deletion logic. */ public function testKeepFileAndVersionsWhenMovingFolderBetweenStorages() { \OCA\Files_Versions\Hooks::connectHooks(); $storage2 = new Temporary(array()); \OC\Files\Filesystem::mount($storage2, array(), $this->user . '/files/substorage'); // trigger a version (multiple would not work because of the expire logic) $this->userView->file_put_contents('folder/inside.txt', 'v1'); $results = $this->rootView->getDirectoryContent($this->user . '/files_trashbin/files'); $this->assertEquals(0, count($results)); $results = $this->rootView->getDirectoryContent($this->user . '/files_versions/folder/'); $this->assertEquals(1, count($results)); // move to another storage $this->userView->rename('folder', 'substorage/folder'); $this->assertTrue($this->userView->file_exists('substorage/folder/inside.txt')); // rescan trash storage list($rootStorage, ) = $this->rootView->resolvePath($this->user . '/files_trashbin'); $rootStorage->getScanner()->scan(''); // versions were moved too $results = $this->rootView->getDirectoryContent($this->user . '/files_versions/substorage/folder/'); $this->assertEquals(1, count($results)); // check that nothing got trashed by the rename's unlink() call $results = $this->rootView->getDirectoryContent($this->user . '/files_trashbin/files'); $this->assertEquals(0, count($results)); // check that versions were moved and not trashed $results = $this->rootView->getDirectoryContent($this->user . '/files_trashbin/versions/'); $this->assertEquals(0, count($results)); }
/** * check if target already exists * * @param $path * @return bool * @throws \Exception */ protected function targetExists($path) { if ($this->rootView->file_exists($path)) { throw new \Exception("new folder '{$path}' already exists"); } return false; }
/** * Test that deleting a file doesn't error when nobody is logged in */ public function testSingleStorageDeleteFileLoggedOut() { $this->logout(); if (!$this->userView->file_exists('test.txt')) { $this->markTestSkipped('Skipping since the current home storage backend requires the user to logged in'); } else { $this->userView->unlink('test.txt'); } }
public function url_stat($path) { $this->setup(); $path = substr($path, strlen('oc://')); if (self::$rootView->file_exists($path)) { return self::$rootView->stat($path); } else { return false; } }
/** * Check if genesis is valid * @param \OC\Files\View $view * @param string $path relative to the view * @throws \Exception */ protected function validate($view, $path) { if (!$view->file_exists($path)) { throw new \Exception('Document not found ' . $path); } if (!$view->is_file($path)) { throw new \Exception('Object ' . $path . ' is not a file.'); } //TODO check if it is a valid odt }
/** * Makes sure the preview which was just created has been saved to disk * * @param int $fileId * @param int $previewWidth * @param int $previewHeight */ private function checkCache($fileId, $previewWidth, $previewHeight) { $postfix = $this->getThumbnailPostfix($previewWidth, $previewHeight); $thumbCacheFile = $this->buildCachePath( $fileId, $previewWidth, $previewHeight, true, $postfix ); $this->assertSame( true, $this->rootView->file_exists($thumbCacheFile), "$thumbCacheFile \n" ); }
public function create($imagePath, $square) { $galleryDir = \OC_User::getHome($this->user) . '/gallery/'; $dir = dirname($imagePath); if (!is_dir($galleryDir . $dir)) { mkdir($galleryDir . $dir, 0755, true); } if (!$this->view->file_exists($imagePath)) { return; } $this->image = new \OC_Image($this->view->getLocalFile($imagePath)); if ($this->image->valid()) { $this->image->fixOrientation(); if ($square) { $this->image->centerCrop(200); } else { $this->image->fitIn(400, 200); } $this->image->save($this->path); } }
/** * @brief retrieve the ENCRYPTED private key from a user * * @param \OC\Files\View $view * @param string $user * @return string private key or false (hopefully) * @note the key returned by this method must be decrypted before use */ public static function getPrivateKey($view, $user) { $path = '/' . $user . '/' . 'files_encryption' . '/' . $user . '.private.key'; $key = false; $proxyStatus = \OC_FileProxy::$enabled; \OC_FileProxy::$enabled = false; if ($view->file_exists($path)) { $key = $view->file_get_contents($path); } \OC_FileProxy::$enabled = $proxyStatus; return $key; }
/** * Remove the certificate and re-generate the certificate bundle * * @param string $name * @return bool */ public function removeCertificate($name) { if (!Filesystem::isValidPath($name)) { return false; } $path = $this->getPathToCertificates() . 'uploads/'; if ($this->view->file_exists($path . $name)) { $this->view->unlink($path . $name); $this->createCertificateBundle(); } return true; }
function testDescryptAllWithBrokenFiles() { $file1 = "/decryptAll1" . $this->getUniqueID() . ".txt"; $file2 = "/decryptAll2" . $this->getUniqueID() . ".txt"; $util = new \OCA\Files_Encryption\Util($this->view, $this->userId); $this->view->file_put_contents($this->userId . '/files/' . $file1, $this->dataShort); $this->view->file_put_contents($this->userId . '/files/' . $file2, $this->dataShort); $fileInfoEncrypted1 = $this->view->getFileInfo($this->userId . '/files/' . $file1); $fileInfoEncrypted2 = $this->view->getFileInfo($this->userId . '/files/' . $file2); $this->assertTrue($fileInfoEncrypted1 instanceof \OC\Files\FileInfo); $this->assertTrue($fileInfoEncrypted2 instanceof \OC\Files\FileInfo); $this->assertEquals($fileInfoEncrypted1['encrypted'], 1); $this->assertEquals($fileInfoEncrypted2['encrypted'], 1); // rename keyfile for file1 so that the decryption for file1 fails // Expected behaviour: decryptAll() returns false, file2 gets decrypted anyway $this->view->rename($this->userId . '/files_encryption/keys/' . $file1 . '/fileKey', $this->userId . '/files_encryption/keys/' . $file1 . '/fileKey.moved'); // need to reset key cache that we don't use the cached key $this->resetKeyCache(); // decrypt all encrypted files $result = $util->decryptAll(); $this->assertFalse($result); $fileInfoUnencrypted1 = $this->view->getFileInfo($this->userId . '/files/' . $file1); $fileInfoUnencrypted2 = $this->view->getFileInfo($this->userId . '/files/' . $file2); $this->assertTrue($fileInfoUnencrypted1 instanceof \OC\Files\FileInfo); $this->assertTrue($fileInfoUnencrypted2 instanceof \OC\Files\FileInfo); // file1 should be still encrypted; file2 should be decrypted $this->assertEquals(1, $fileInfoUnencrypted1['encrypted']); $this->assertEquals(0, $fileInfoUnencrypted2['encrypted']); // keyfiles and share keys should still exist $this->assertTrue($this->view->is_dir($this->userId . '/files_encryption/keys/')); $this->assertTrue($this->view->file_exists($this->userId . '/files_encryption/keys/' . $file1 . '/fileKey.moved')); $this->assertTrue($this->view->file_exists($this->userId . '/files_encryption/keys/' . $file1 . '/' . $this->userId . '.shareKey')); // rename the keyfile for file1 back $this->view->rename($this->userId . '/files_encryption/keys/' . $file1 . '/fileKey.moved', $this->userId . '/files_encryption/keys/' . $file1 . '/fileKey'); // try again to decrypt all encrypted files $result = $util->decryptAll(); $this->assertTrue($result); $fileInfoUnencrypted1 = $this->view->getFileInfo($this->userId . '/files/' . $file1); $fileInfoUnencrypted2 = $this->view->getFileInfo($this->userId . '/files/' . $file2); $this->assertTrue($fileInfoUnencrypted1 instanceof \OC\Files\FileInfo); $this->assertTrue($fileInfoUnencrypted2 instanceof \OC\Files\FileInfo); // now both files should be decrypted $this->assertEquals(0, $fileInfoUnencrypted1['encrypted']); $this->assertEquals(0, $fileInfoUnencrypted2['encrypted']); // keyfiles and share keys should be deleted $this->assertFalse($this->view->is_dir($this->userId . '/files_encryption/keys/')); //cleanup $backupPath = $this->getBackupPath('decryptAll'); $this->view->unlink($this->userId . '/files/' . $file1); $this->view->unlink($this->userId . '/files/' . $file2); $this->view->deleteAll($backupPath); }
/** * @return Directory */ private function impl() { $rootView = new View(); $user = \OC::$server->getUserSession()->getUser(); Filesystem::initMountPoints($user->getUID()); if (!$rootView->file_exists('/' . $user->getUID() . '/uploads')) { $rootView->mkdir('/' . $user->getUID() . '/uploads'); } $view = new View('/' . $user->getUID() . '/uploads'); $rootInfo = $view->getFileInfo(''); $impl = new Directory($view, $rootInfo); return $impl; }
/** * create directories for the keys recursively * * @param string $path */ private function createPathForKeys($path) { if (!$this->view->file_exists($path)) { $sub_dirs = explode('/', $path); $dir = ''; foreach ($sub_dirs as $sub_dir) { $dir .= '/' . $sub_dir; if (!$this->view->is_dir($dir)) { $this->view->mkdir($dir); } } } }
/** * check if key storage is ready * * @return bool */ protected function isKeyStorageReady() { $rootDir = $this->util->getKeyStorageRoot(); // the default root is always valid if ($rootDir === '') { return true; } // check if key storage is mounted correctly if ($this->rootView->file_exists($rootDir . '/' . Storage::KEY_STORAGE_MARKER)) { return true; } return false; }
/** * Make preparations to filesystem for saving a key file * * @param string $path relative to the views root */ protected function keySetPreparation($path) { // If the file resides within a subdirectory, create it if (!$this->view->file_exists($path)) { $sub_dirs = explode('/', ltrim($path, '/')); $dir = ''; foreach ($sub_dirs as $sub_dir) { $dir .= '/' . $sub_dir; if (!$this->view->is_dir($dir)) { $this->view->mkdir($dir); } } } }
private function create($imagePath, $square) { $galleryDir = \OC_User::getHome($this->user) . '/gallery/' . $this->user . '/'; $dir = dirname($imagePath); if (!is_dir($galleryDir . $dir)) { mkdir($galleryDir . $dir, 0755, true); } if (!$this->view->file_exists($imagePath)) { return; } $handle = $this->view->fopen($imagePath, 'r'); $this->image = new \OCP\Image($handle); fclose($handle); if ($this->image->valid()) { $this->image->fixOrientation(); if ($square) { $this->image->centerCrop(200); } else { $this->image->fitIn(400, 200); } $this->image->save($this->path); } }
/** * restore backup * * @param string $backup complete name of the backup * @return boolean */ public function restoreBackup($backup) { $backupDir = $this->encryptionDir . '/backup.' . $backup . '/'; $fileKeysRestored = $this->view->rename($backupDir . 'keys', $this->encryptionDir . '/keys'); $pubKeyRestored = $privKeyRestored = true; if ($this->view->file_exists($backupDir . $this->userId . '.privateKey') && $this->view->file_exists($backupDir . $this->userId . '.privateKey')) { $pubKeyRestored = $this->view->rename($backupDir . $this->userId . '.publicKey', $this->publicKeyPath); $privKeyRestored = $this->view->rename($backupDir . $this->userId . '.privateKey', $this->privateKeyPath); } if ($fileKeysRestored && $pubKeyRestored && $privKeyRestored) { $this->view->deleteAll($backupDir); return true; } return false; }
/** * encrypt file * * @param string $path * @return bool */ protected function encryptFile($path) { $source = $path; $target = $path . '.encrypted.' . time(); try { $this->rootView->copy($source, $target); $this->rootView->rename($target, $source); } catch (DecryptionFailedException $e) { if ($this->rootView->file_exists($target)) { $this->rootView->unlink($target); } return false; } return true; }
/** * test webdav delete random file * @depends testWebdavGET */ function testWebdavDELETE($filename) { // set server vars $_SERVER['REQUEST_METHOD'] = 'DELETE'; $_SERVER['REQUEST_URI'] = '/remote.php/webdav' . $filename; $_SERVER['HTTP_AUTHORIZATION'] = 'Basic dGVzdC13ZWJkYXYtdXNlcjE6dGVzdC13ZWJkYXYtdXNlcjE='; $_SERVER['PATH_INFO'] = '/webdav' . $filename; // at the beginning the file should exist $this->assertTrue($this->view->file_exists('/' . $this->userId . '/files' . $filename)); // handle webdav request $content = $this->handleWebdavRequest(); // check if file was removed $this->assertFalse($this->view->file_exists('/' . $this->userId . '/files' . $filename)); // check if key-file was removed $this->assertFalse($this->view->file_exists('/' . $this->userId . '/files_encryption/keyfiles' . $filename . '.key')); // check if shareKey-file was removed $this->assertFalse($this->view->file_exists('/' . $this->userId . '/files_encryption/share-keys' . $filename . '.' . $this->userId . '.shareKey')); }
public function testCreationFromCached() { $sampleFile = '/' . $this->user . '/files/test.txt'; $this->rootView->file_put_contents($sampleFile, 'dummy file data'); // create base preview $x = 150; $y = 150; $preview = new \OC\Preview($this->user, 'files/', 'test.txt', $x, $y); $preview->getPreview(); $fileInfo = $this->rootView->getFileInfo($sampleFile); $fileId = $fileInfo['fileid']; $thumbCacheFile = '/' . $this->user . '/' . \OC\Preview::THUMBNAILS_FOLDER . '/' . $fileId . '/' . $x . '-' . $y . '.png'; $this->assertEquals($this->rootView->file_exists($thumbCacheFile), true); // create smaller previews $preview = new \OC\Preview($this->user, 'files/', 'test.txt', 50, 50); $isCached = $preview->isCached($fileId); $this->assertEquals($this->user . '/' . \OC\Preview::THUMBNAILS_FOLDER . '/' . $fileId . '/150-150.png', $isCached); }