@@ -37,57 +37,57 @@ |
||
37 | 37 | */ |
38 | 38 | class ConsoleOutput implements IOutput { |
39 | 39 | |
40 | - /** @var OutputInterface */ |
|
41 | - private $output; |
|
40 | + /** @var OutputInterface */ |
|
41 | + private $output; |
|
42 | 42 | |
43 | - /** @var ProgressBar */ |
|
44 | - private $progressBar; |
|
43 | + /** @var ProgressBar */ |
|
44 | + private $progressBar; |
|
45 | 45 | |
46 | - public function __construct(OutputInterface $output) { |
|
47 | - $this->output = $output; |
|
48 | - } |
|
46 | + public function __construct(OutputInterface $output) { |
|
47 | + $this->output = $output; |
|
48 | + } |
|
49 | 49 | |
50 | - /** |
|
51 | - * @param string $message |
|
52 | - */ |
|
53 | - public function info($message) { |
|
54 | - $this->output->writeln("<info>$message</info>"); |
|
55 | - } |
|
50 | + /** |
|
51 | + * @param string $message |
|
52 | + */ |
|
53 | + public function info($message) { |
|
54 | + $this->output->writeln("<info>$message</info>"); |
|
55 | + } |
|
56 | 56 | |
57 | - /** |
|
58 | - * @param string $message |
|
59 | - */ |
|
60 | - public function warning($message) { |
|
61 | - $this->output->writeln("<comment>$message</comment>"); |
|
62 | - } |
|
57 | + /** |
|
58 | + * @param string $message |
|
59 | + */ |
|
60 | + public function warning($message) { |
|
61 | + $this->output->writeln("<comment>$message</comment>"); |
|
62 | + } |
|
63 | 63 | |
64 | - /** |
|
65 | - * @param int $max |
|
66 | - */ |
|
67 | - public function startProgress($max = 0) { |
|
68 | - if (!is_null($this->progressBar)) { |
|
69 | - $this->progressBar->finish(); |
|
70 | - } |
|
71 | - $this->progressBar = new ProgressBar($this->output); |
|
72 | - $this->progressBar->start($max); |
|
73 | - } |
|
64 | + /** |
|
65 | + * @param int $max |
|
66 | + */ |
|
67 | + public function startProgress($max = 0) { |
|
68 | + if (!is_null($this->progressBar)) { |
|
69 | + $this->progressBar->finish(); |
|
70 | + } |
|
71 | + $this->progressBar = new ProgressBar($this->output); |
|
72 | + $this->progressBar->start($max); |
|
73 | + } |
|
74 | 74 | |
75 | - /** |
|
76 | - * @param int $step |
|
77 | - * @param string $description |
|
78 | - */ |
|
79 | - public function advance($step = 1, $description = '') { |
|
80 | - if (!is_null($this->progressBar)) { |
|
81 | - $this->progressBar = new ProgressBar($this->output); |
|
82 | - $this->progressBar->start(); |
|
83 | - } |
|
84 | - $this->progressBar->advance($step); |
|
85 | - } |
|
75 | + /** |
|
76 | + * @param int $step |
|
77 | + * @param string $description |
|
78 | + */ |
|
79 | + public function advance($step = 1, $description = '') { |
|
80 | + if (!is_null($this->progressBar)) { |
|
81 | + $this->progressBar = new ProgressBar($this->output); |
|
82 | + $this->progressBar->start(); |
|
83 | + } |
|
84 | + $this->progressBar->advance($step); |
|
85 | + } |
|
86 | 86 | |
87 | - public function finishProgress() { |
|
88 | - if (is_null($this->progressBar)) { |
|
89 | - return; |
|
90 | - } |
|
91 | - $this->progressBar->finish(); |
|
92 | - } |
|
87 | + public function finishProgress() { |
|
88 | + if (is_null($this->progressBar)) { |
|
89 | + return; |
|
90 | + } |
|
91 | + $this->progressBar->finish(); |
|
92 | + } |
|
93 | 93 | } |
@@ -34,102 +34,102 @@ |
||
34 | 34 | |
35 | 35 | class EncryptAll extends Command { |
36 | 36 | |
37 | - /** @var IManager */ |
|
38 | - protected $encryptionManager; |
|
39 | - |
|
40 | - /** @var IAppManager */ |
|
41 | - protected $appManager; |
|
42 | - |
|
43 | - /** @var IConfig */ |
|
44 | - protected $config; |
|
45 | - |
|
46 | - /** @var QuestionHelper */ |
|
47 | - protected $questionHelper; |
|
48 | - |
|
49 | - /** @var bool */ |
|
50 | - protected $wasTrashbinEnabled; |
|
51 | - |
|
52 | - /** @var bool */ |
|
53 | - protected $wasMaintenanceModeEnabled; |
|
54 | - |
|
55 | - /** |
|
56 | - * @param IManager $encryptionManager |
|
57 | - * @param IAppManager $appManager |
|
58 | - * @param IConfig $config |
|
59 | - * @param QuestionHelper $questionHelper |
|
60 | - */ |
|
61 | - public function __construct( |
|
62 | - IManager $encryptionManager, |
|
63 | - IAppManager $appManager, |
|
64 | - IConfig $config, |
|
65 | - QuestionHelper $questionHelper |
|
66 | - ) { |
|
67 | - parent::__construct(); |
|
68 | - $this->appManager = $appManager; |
|
69 | - $this->encryptionManager = $encryptionManager; |
|
70 | - $this->config = $config; |
|
71 | - $this->questionHelper = $questionHelper; |
|
72 | - } |
|
73 | - |
|
74 | - /** |
|
75 | - * Set maintenance mode and disable the trashbin app |
|
76 | - */ |
|
77 | - protected function forceMaintenanceAndTrashbin() { |
|
78 | - $this->wasTrashbinEnabled = $this->appManager->isEnabledForUser('files_trashbin'); |
|
79 | - $this->wasMaintenanceModeEnabled = $this->config->getSystemValue('maintenance', false); |
|
80 | - $this->config->setSystemValue('maintenance', true); |
|
81 | - $this->appManager->disableApp('files_trashbin'); |
|
82 | - } |
|
83 | - |
|
84 | - /** |
|
85 | - * Reset the maintenance mode and re-enable the trashbin app |
|
86 | - */ |
|
87 | - protected function resetMaintenanceAndTrashbin() { |
|
88 | - $this->config->setSystemValue('maintenance', $this->wasMaintenanceModeEnabled); |
|
89 | - if ($this->wasTrashbinEnabled) { |
|
90 | - $this->appManager->enableApp('files_trashbin'); |
|
91 | - } |
|
92 | - } |
|
93 | - |
|
94 | - protected function configure() { |
|
95 | - parent::configure(); |
|
96 | - |
|
97 | - $this->setName('encryption:encrypt-all'); |
|
98 | - $this->setDescription('Encrypt all files for all users'); |
|
99 | - $this->setHelp( |
|
100 | - 'This will encrypt all files for all users. ' |
|
101 | - . 'Please make sure that no user access his files during this process!' |
|
102 | - ); |
|
103 | - } |
|
104 | - |
|
105 | - protected function execute(InputInterface $input, OutputInterface $output) { |
|
106 | - |
|
107 | - if ($this->encryptionManager->isEnabled() === false) { |
|
108 | - throw new \Exception('Server side encryption is not enabled'); |
|
109 | - } |
|
110 | - |
|
111 | - $output->writeln("\n"); |
|
112 | - $output->writeln('You are about to encrypt all files stored in your Nextcloud installation.'); |
|
113 | - $output->writeln('Depending on the number of available files, and their size, this may take quite some time.'); |
|
114 | - $output->writeln('Please ensure that no user accesses their files during this time!'); |
|
115 | - $output->writeln('Note: The encryption module you use determines which files get encrypted.'); |
|
116 | - $output->writeln(''); |
|
117 | - $question = new ConfirmationQuestion('Do you really want to continue? (y/n) ', false); |
|
118 | - if ($this->questionHelper->ask($input, $output, $question)) { |
|
119 | - $this->forceMaintenanceAndTrashbin(); |
|
120 | - |
|
121 | - try { |
|
122 | - $defaultModule = $this->encryptionManager->getEncryptionModule(); |
|
123 | - $defaultModule->encryptAll($input, $output); |
|
124 | - } catch (\Exception $ex) { |
|
125 | - $this->resetMaintenanceAndTrashbin(); |
|
126 | - throw $ex; |
|
127 | - } |
|
128 | - |
|
129 | - $this->resetMaintenanceAndTrashbin(); |
|
130 | - } else { |
|
131 | - $output->writeln('aborted'); |
|
132 | - } |
|
133 | - } |
|
37 | + /** @var IManager */ |
|
38 | + protected $encryptionManager; |
|
39 | + |
|
40 | + /** @var IAppManager */ |
|
41 | + protected $appManager; |
|
42 | + |
|
43 | + /** @var IConfig */ |
|
44 | + protected $config; |
|
45 | + |
|
46 | + /** @var QuestionHelper */ |
|
47 | + protected $questionHelper; |
|
48 | + |
|
49 | + /** @var bool */ |
|
50 | + protected $wasTrashbinEnabled; |
|
51 | + |
|
52 | + /** @var bool */ |
|
53 | + protected $wasMaintenanceModeEnabled; |
|
54 | + |
|
55 | + /** |
|
56 | + * @param IManager $encryptionManager |
|
57 | + * @param IAppManager $appManager |
|
58 | + * @param IConfig $config |
|
59 | + * @param QuestionHelper $questionHelper |
|
60 | + */ |
|
61 | + public function __construct( |
|
62 | + IManager $encryptionManager, |
|
63 | + IAppManager $appManager, |
|
64 | + IConfig $config, |
|
65 | + QuestionHelper $questionHelper |
|
66 | + ) { |
|
67 | + parent::__construct(); |
|
68 | + $this->appManager = $appManager; |
|
69 | + $this->encryptionManager = $encryptionManager; |
|
70 | + $this->config = $config; |
|
71 | + $this->questionHelper = $questionHelper; |
|
72 | + } |
|
73 | + |
|
74 | + /** |
|
75 | + * Set maintenance mode and disable the trashbin app |
|
76 | + */ |
|
77 | + protected function forceMaintenanceAndTrashbin() { |
|
78 | + $this->wasTrashbinEnabled = $this->appManager->isEnabledForUser('files_trashbin'); |
|
79 | + $this->wasMaintenanceModeEnabled = $this->config->getSystemValue('maintenance', false); |
|
80 | + $this->config->setSystemValue('maintenance', true); |
|
81 | + $this->appManager->disableApp('files_trashbin'); |
|
82 | + } |
|
83 | + |
|
84 | + /** |
|
85 | + * Reset the maintenance mode and re-enable the trashbin app |
|
86 | + */ |
|
87 | + protected function resetMaintenanceAndTrashbin() { |
|
88 | + $this->config->setSystemValue('maintenance', $this->wasMaintenanceModeEnabled); |
|
89 | + if ($this->wasTrashbinEnabled) { |
|
90 | + $this->appManager->enableApp('files_trashbin'); |
|
91 | + } |
|
92 | + } |
|
93 | + |
|
94 | + protected function configure() { |
|
95 | + parent::configure(); |
|
96 | + |
|
97 | + $this->setName('encryption:encrypt-all'); |
|
98 | + $this->setDescription('Encrypt all files for all users'); |
|
99 | + $this->setHelp( |
|
100 | + 'This will encrypt all files for all users. ' |
|
101 | + . 'Please make sure that no user access his files during this process!' |
|
102 | + ); |
|
103 | + } |
|
104 | + |
|
105 | + protected function execute(InputInterface $input, OutputInterface $output) { |
|
106 | + |
|
107 | + if ($this->encryptionManager->isEnabled() === false) { |
|
108 | + throw new \Exception('Server side encryption is not enabled'); |
|
109 | + } |
|
110 | + |
|
111 | + $output->writeln("\n"); |
|
112 | + $output->writeln('You are about to encrypt all files stored in your Nextcloud installation.'); |
|
113 | + $output->writeln('Depending on the number of available files, and their size, this may take quite some time.'); |
|
114 | + $output->writeln('Please ensure that no user accesses their files during this time!'); |
|
115 | + $output->writeln('Note: The encryption module you use determines which files get encrypted.'); |
|
116 | + $output->writeln(''); |
|
117 | + $question = new ConfirmationQuestion('Do you really want to continue? (y/n) ', false); |
|
118 | + if ($this->questionHelper->ask($input, $output, $question)) { |
|
119 | + $this->forceMaintenanceAndTrashbin(); |
|
120 | + |
|
121 | + try { |
|
122 | + $defaultModule = $this->encryptionManager->getEncryptionModule(); |
|
123 | + $defaultModule->encryptAll($input, $output); |
|
124 | + } catch (\Exception $ex) { |
|
125 | + $this->resetMaintenanceAndTrashbin(); |
|
126 | + throw $ex; |
|
127 | + } |
|
128 | + |
|
129 | + $this->resetMaintenanceAndTrashbin(); |
|
130 | + } else { |
|
131 | + $output->writeln('aborted'); |
|
132 | + } |
|
133 | + } |
|
134 | 134 | |
135 | 135 | } |
@@ -39,98 +39,98 @@ |
||
39 | 39 | |
40 | 40 | class ResetPassword extends Command { |
41 | 41 | |
42 | - /** @var IUserManager */ |
|
43 | - protected $userManager; |
|
44 | - |
|
45 | - public function __construct(IUserManager $userManager) { |
|
46 | - $this->userManager = $userManager; |
|
47 | - parent::__construct(); |
|
48 | - } |
|
49 | - |
|
50 | - protected function configure() { |
|
51 | - $this |
|
52 | - ->setName('user:resetpassword') |
|
53 | - ->setDescription('Resets the password of the named user') |
|
54 | - ->addArgument( |
|
55 | - 'user', |
|
56 | - InputArgument::REQUIRED, |
|
57 | - 'Username to reset password' |
|
58 | - ) |
|
59 | - ->addOption( |
|
60 | - 'password-from-env', |
|
61 | - null, |
|
62 | - InputOption::VALUE_NONE, |
|
63 | - 'read password from environment variable OC_PASS' |
|
64 | - ) |
|
65 | - ; |
|
66 | - } |
|
67 | - |
|
68 | - protected function execute(InputInterface $input, OutputInterface $output) { |
|
69 | - $username = $input->getArgument('user'); |
|
70 | - |
|
71 | - /** @var $user \OCP\IUser */ |
|
72 | - $user = $this->userManager->get($username); |
|
73 | - if (is_null($user)) { |
|
74 | - $output->writeln('<error>User does not exist</error>'); |
|
75 | - return 1; |
|
76 | - } |
|
77 | - |
|
78 | - if ($input->getOption('password-from-env')) { |
|
79 | - $password = getenv('OC_PASS'); |
|
80 | - if (!$password) { |
|
81 | - $output->writeln('<error>--password-from-env given, but OC_PASS is empty!</error>'); |
|
82 | - return 1; |
|
83 | - } |
|
84 | - } elseif ($input->isInteractive()) { |
|
85 | - /** @var QuestionHelper $helper */ |
|
86 | - $helper = $this->getHelper('question'); |
|
87 | - |
|
88 | - if (\OCP\App::isEnabled('encryption')) { |
|
89 | - $output->writeln( |
|
90 | - '<error>Warning: Resetting the password when using encryption will result in data loss!</error>' |
|
91 | - ); |
|
92 | - |
|
93 | - $question = new ConfirmationQuestion('Do you want to continue?'); |
|
94 | - if (!$helper->ask($input, $output, $question)) { |
|
95 | - return 1; |
|
96 | - } |
|
97 | - } |
|
98 | - |
|
99 | - $question = new Question('Enter a new password: '); |
|
100 | - $question->setHidden(true); |
|
101 | - $password = $helper->ask($input, $output, $question); |
|
102 | - |
|
103 | - if ($password === null) { |
|
104 | - $output->writeln("<error>Password cannot be empty!</error>"); |
|
105 | - return 1; |
|
106 | - } |
|
107 | - |
|
108 | - $question = new Question('Confirm the new password: '); |
|
109 | - $question->setHidden(true); |
|
110 | - $confirm = $helper->ask($input, $output, $question); |
|
111 | - |
|
112 | - if ($password !== $confirm) { |
|
113 | - $output->writeln("<error>Passwords did not match!</error>"); |
|
114 | - return 1; |
|
115 | - } |
|
116 | - } else { |
|
117 | - $output->writeln("<error>Interactive input or --password-from-env is needed for entering a new password!</error>"); |
|
118 | - return 1; |
|
119 | - } |
|
120 | - |
|
121 | - |
|
122 | - try { |
|
123 | - $success = $user->setPassword($password); |
|
124 | - } catch (\Exception $e) { |
|
125 | - $output->writeln('<error>' . $e->getMessage() . '</error>'); |
|
126 | - return 1; |
|
127 | - } |
|
128 | - |
|
129 | - if ($success) { |
|
130 | - $output->writeln("<info>Successfully reset password for " . $username . "</info>"); |
|
131 | - } else { |
|
132 | - $output->writeln("<error>Error while resetting password!</error>"); |
|
133 | - return 1; |
|
134 | - } |
|
135 | - } |
|
42 | + /** @var IUserManager */ |
|
43 | + protected $userManager; |
|
44 | + |
|
45 | + public function __construct(IUserManager $userManager) { |
|
46 | + $this->userManager = $userManager; |
|
47 | + parent::__construct(); |
|
48 | + } |
|
49 | + |
|
50 | + protected function configure() { |
|
51 | + $this |
|
52 | + ->setName('user:resetpassword') |
|
53 | + ->setDescription('Resets the password of the named user') |
|
54 | + ->addArgument( |
|
55 | + 'user', |
|
56 | + InputArgument::REQUIRED, |
|
57 | + 'Username to reset password' |
|
58 | + ) |
|
59 | + ->addOption( |
|
60 | + 'password-from-env', |
|
61 | + null, |
|
62 | + InputOption::VALUE_NONE, |
|
63 | + 'read password from environment variable OC_PASS' |
|
64 | + ) |
|
65 | + ; |
|
66 | + } |
|
67 | + |
|
68 | + protected function execute(InputInterface $input, OutputInterface $output) { |
|
69 | + $username = $input->getArgument('user'); |
|
70 | + |
|
71 | + /** @var $user \OCP\IUser */ |
|
72 | + $user = $this->userManager->get($username); |
|
73 | + if (is_null($user)) { |
|
74 | + $output->writeln('<error>User does not exist</error>'); |
|
75 | + return 1; |
|
76 | + } |
|
77 | + |
|
78 | + if ($input->getOption('password-from-env')) { |
|
79 | + $password = getenv('OC_PASS'); |
|
80 | + if (!$password) { |
|
81 | + $output->writeln('<error>--password-from-env given, but OC_PASS is empty!</error>'); |
|
82 | + return 1; |
|
83 | + } |
|
84 | + } elseif ($input->isInteractive()) { |
|
85 | + /** @var QuestionHelper $helper */ |
|
86 | + $helper = $this->getHelper('question'); |
|
87 | + |
|
88 | + if (\OCP\App::isEnabled('encryption')) { |
|
89 | + $output->writeln( |
|
90 | + '<error>Warning: Resetting the password when using encryption will result in data loss!</error>' |
|
91 | + ); |
|
92 | + |
|
93 | + $question = new ConfirmationQuestion('Do you want to continue?'); |
|
94 | + if (!$helper->ask($input, $output, $question)) { |
|
95 | + return 1; |
|
96 | + } |
|
97 | + } |
|
98 | + |
|
99 | + $question = new Question('Enter a new password: '); |
|
100 | + $question->setHidden(true); |
|
101 | + $password = $helper->ask($input, $output, $question); |
|
102 | + |
|
103 | + if ($password === null) { |
|
104 | + $output->writeln("<error>Password cannot be empty!</error>"); |
|
105 | + return 1; |
|
106 | + } |
|
107 | + |
|
108 | + $question = new Question('Confirm the new password: '); |
|
109 | + $question->setHidden(true); |
|
110 | + $confirm = $helper->ask($input, $output, $question); |
|
111 | + |
|
112 | + if ($password !== $confirm) { |
|
113 | + $output->writeln("<error>Passwords did not match!</error>"); |
|
114 | + return 1; |
|
115 | + } |
|
116 | + } else { |
|
117 | + $output->writeln("<error>Interactive input or --password-from-env is needed for entering a new password!</error>"); |
|
118 | + return 1; |
|
119 | + } |
|
120 | + |
|
121 | + |
|
122 | + try { |
|
123 | + $success = $user->setPassword($password); |
|
124 | + } catch (\Exception $e) { |
|
125 | + $output->writeln('<error>' . $e->getMessage() . '</error>'); |
|
126 | + return 1; |
|
127 | + } |
|
128 | + |
|
129 | + if ($success) { |
|
130 | + $output->writeln("<info>Successfully reset password for " . $username . "</info>"); |
|
131 | + } else { |
|
132 | + $output->writeln("<error>Error while resetting password!</error>"); |
|
133 | + return 1; |
|
134 | + } |
|
135 | + } |
|
136 | 136 | } |
@@ -33,174 +33,174 @@ |
||
33 | 33 | use OCP\Share\IManager; |
34 | 34 | |
35 | 35 | class MountProvider implements IMountProvider { |
36 | - /** |
|
37 | - * @var \OCP\IConfig |
|
38 | - */ |
|
39 | - protected $config; |
|
40 | - |
|
41 | - /** |
|
42 | - * @var IManager |
|
43 | - */ |
|
44 | - protected $shareManager; |
|
45 | - |
|
46 | - /** |
|
47 | - * @var ILogger |
|
48 | - */ |
|
49 | - protected $logger; |
|
50 | - |
|
51 | - /** |
|
52 | - * @param \OCP\IConfig $config |
|
53 | - * @param IManager $shareManager |
|
54 | - * @param ILogger $logger |
|
55 | - */ |
|
56 | - public function __construct(IConfig $config, IManager $shareManager, ILogger $logger) { |
|
57 | - $this->config = $config; |
|
58 | - $this->shareManager = $shareManager; |
|
59 | - $this->logger = $logger; |
|
60 | - } |
|
61 | - |
|
62 | - |
|
63 | - /** |
|
64 | - * Get all mountpoints applicable for the user and check for shares where we need to update the etags |
|
65 | - * |
|
66 | - * @param \OCP\IUser $user |
|
67 | - * @param \OCP\Files\Storage\IStorageFactory $storageFactory |
|
68 | - * @return \OCP\Files\Mount\IMountPoint[] |
|
69 | - */ |
|
70 | - public function getMountsForUser(IUser $user, IStorageFactory $storageFactory) { |
|
71 | - |
|
72 | - $shares = $this->shareManager->getSharedWith($user->getUID(), \OCP\Share::SHARE_TYPE_USER, null, -1); |
|
73 | - $shares = array_merge($shares, $this->shareManager->getSharedWith($user->getUID(), \OCP\Share::SHARE_TYPE_GROUP, null, -1)); |
|
74 | - $shares = array_merge($shares, $this->shareManager->getSharedWith($user->getUID(), \OCP\Share::SHARE_TYPE_CIRCLE, null, -1)); |
|
75 | - |
|
76 | - // filter out excluded shares and group shares that includes self |
|
77 | - $shares = array_filter($shares, function (\OCP\Share\IShare $share) use ($user) { |
|
78 | - return $share->getPermissions() > 0 && $share->getShareOwner() !== $user->getUID(); |
|
79 | - }); |
|
80 | - |
|
81 | - $superShares = $this->buildSuperShares($shares, $user); |
|
82 | - |
|
83 | - $mounts = []; |
|
84 | - foreach ($superShares as $share) { |
|
85 | - try { |
|
86 | - $mounts[] = new SharedMount( |
|
87 | - '\OCA\Files_Sharing\SharedStorage', |
|
88 | - $mounts, |
|
89 | - [ |
|
90 | - 'user' => $user->getUID(), |
|
91 | - // parent share |
|
92 | - 'superShare' => $share[0], |
|
93 | - // children/component of the superShare |
|
94 | - 'groupedShares' => $share[1], |
|
95 | - ], |
|
96 | - $storageFactory |
|
97 | - ); |
|
98 | - } catch (\Exception $e) { |
|
99 | - $this->logger->logException($e); |
|
100 | - $this->logger->error('Error while trying to create shared mount'); |
|
101 | - } |
|
102 | - } |
|
103 | - |
|
104 | - // array_filter removes the null values from the array |
|
105 | - return array_filter($mounts); |
|
106 | - } |
|
107 | - |
|
108 | - /** |
|
109 | - * Groups shares by path (nodeId) and target path |
|
110 | - * |
|
111 | - * @param \OCP\Share\IShare[] $shares |
|
112 | - * @return \OCP\Share\IShare[][] array of grouped shares, each element in the |
|
113 | - * array is a group which itself is an array of shares |
|
114 | - */ |
|
115 | - private function groupShares(array $shares) { |
|
116 | - $tmp = []; |
|
117 | - |
|
118 | - foreach ($shares as $share) { |
|
119 | - if (!isset($tmp[$share->getNodeId()])) { |
|
120 | - $tmp[$share->getNodeId()] = []; |
|
121 | - } |
|
122 | - $tmp[$share->getNodeId()][] = $share; |
|
123 | - } |
|
124 | - |
|
125 | - $result = []; |
|
126 | - // sort by stime, the super share will be based on the least recent share |
|
127 | - foreach ($tmp as &$tmp2) { |
|
128 | - @usort($tmp2, function($a, $b) { |
|
129 | - if ($a->getShareTime() <= $b->getShareTime()) { |
|
130 | - return -1; |
|
131 | - } |
|
132 | - return 1; |
|
133 | - }); |
|
134 | - $result[] = $tmp2; |
|
135 | - } |
|
136 | - |
|
137 | - return array_values($result); |
|
138 | - } |
|
139 | - |
|
140 | - /** |
|
141 | - * Build super shares (virtual share) by grouping them by node id and target, |
|
142 | - * then for each group compute the super share and return it along with the matching |
|
143 | - * grouped shares. The most permissive permissions are used based on the permissions |
|
144 | - * of all shares within the group. |
|
145 | - * |
|
146 | - * @param \OCP\Share\IShare[] $allShares |
|
147 | - * @param \OCP\IUser $user user |
|
148 | - * @return array Tuple of [superShare, groupedShares] |
|
149 | - */ |
|
150 | - private function buildSuperShares(array $allShares, \OCP\IUser $user) { |
|
151 | - $result = []; |
|
152 | - |
|
153 | - $groupedShares = $this->groupShares($allShares); |
|
154 | - |
|
155 | - /** @var \OCP\Share\IShare[] $shares */ |
|
156 | - foreach ($groupedShares as $shares) { |
|
157 | - if (count($shares) === 0) { |
|
158 | - continue; |
|
159 | - } |
|
160 | - |
|
161 | - $superShare = $this->shareManager->newShare(); |
|
162 | - |
|
163 | - // compute super share based on first entry of the group |
|
164 | - $superShare->setId($shares[0]->getId()) |
|
165 | - ->setShareOwner($shares[0]->getShareOwner()) |
|
166 | - ->setNodeId($shares[0]->getNodeId()) |
|
167 | - ->setTarget($shares[0]->getTarget()); |
|
168 | - |
|
169 | - // use most permissive permissions |
|
170 | - $permissions = 0; |
|
171 | - foreach ($shares as $share) { |
|
172 | - $permissions |= $share->getPermissions(); |
|
173 | - if ($share->getTarget() !== $superShare->getTarget()) { |
|
174 | - // adjust target, for database consistency |
|
175 | - $share->setTarget($superShare->getTarget()); |
|
176 | - try { |
|
177 | - $this->shareManager->moveShare($share, $user->getUID()); |
|
178 | - } catch (\InvalidArgumentException $e) { |
|
179 | - // ignore as it is not important and we don't want to |
|
180 | - // block FS setup |
|
181 | - |
|
182 | - // the subsequent code anyway only uses the target of the |
|
183 | - // super share |
|
184 | - |
|
185 | - // such issue can usually happen when dealing with |
|
186 | - // null groups which usually appear with group backend |
|
187 | - // caching inconsistencies |
|
188 | - $this->logger->debug( |
|
189 | - 'Could not adjust share target for share ' . $share->getId() . ' to make it consistent: ' . $e->getMessage(), |
|
190 | - ['app' => 'files_sharing'] |
|
191 | - ); |
|
192 | - } |
|
193 | - } |
|
194 | - if (!is_null($share->getNodeCacheEntry())) { |
|
195 | - $superShare->setNodeCacheEntry($share->getNodeCacheEntry()); |
|
196 | - } |
|
197 | - } |
|
198 | - |
|
199 | - $superShare->setPermissions($permissions); |
|
200 | - |
|
201 | - $result[] = [$superShare, $shares]; |
|
202 | - } |
|
203 | - |
|
204 | - return $result; |
|
205 | - } |
|
36 | + /** |
|
37 | + * @var \OCP\IConfig |
|
38 | + */ |
|
39 | + protected $config; |
|
40 | + |
|
41 | + /** |
|
42 | + * @var IManager |
|
43 | + */ |
|
44 | + protected $shareManager; |
|
45 | + |
|
46 | + /** |
|
47 | + * @var ILogger |
|
48 | + */ |
|
49 | + protected $logger; |
|
50 | + |
|
51 | + /** |
|
52 | + * @param \OCP\IConfig $config |
|
53 | + * @param IManager $shareManager |
|
54 | + * @param ILogger $logger |
|
55 | + */ |
|
56 | + public function __construct(IConfig $config, IManager $shareManager, ILogger $logger) { |
|
57 | + $this->config = $config; |
|
58 | + $this->shareManager = $shareManager; |
|
59 | + $this->logger = $logger; |
|
60 | + } |
|
61 | + |
|
62 | + |
|
63 | + /** |
|
64 | + * Get all mountpoints applicable for the user and check for shares where we need to update the etags |
|
65 | + * |
|
66 | + * @param \OCP\IUser $user |
|
67 | + * @param \OCP\Files\Storage\IStorageFactory $storageFactory |
|
68 | + * @return \OCP\Files\Mount\IMountPoint[] |
|
69 | + */ |
|
70 | + public function getMountsForUser(IUser $user, IStorageFactory $storageFactory) { |
|
71 | + |
|
72 | + $shares = $this->shareManager->getSharedWith($user->getUID(), \OCP\Share::SHARE_TYPE_USER, null, -1); |
|
73 | + $shares = array_merge($shares, $this->shareManager->getSharedWith($user->getUID(), \OCP\Share::SHARE_TYPE_GROUP, null, -1)); |
|
74 | + $shares = array_merge($shares, $this->shareManager->getSharedWith($user->getUID(), \OCP\Share::SHARE_TYPE_CIRCLE, null, -1)); |
|
75 | + |
|
76 | + // filter out excluded shares and group shares that includes self |
|
77 | + $shares = array_filter($shares, function (\OCP\Share\IShare $share) use ($user) { |
|
78 | + return $share->getPermissions() > 0 && $share->getShareOwner() !== $user->getUID(); |
|
79 | + }); |
|
80 | + |
|
81 | + $superShares = $this->buildSuperShares($shares, $user); |
|
82 | + |
|
83 | + $mounts = []; |
|
84 | + foreach ($superShares as $share) { |
|
85 | + try { |
|
86 | + $mounts[] = new SharedMount( |
|
87 | + '\OCA\Files_Sharing\SharedStorage', |
|
88 | + $mounts, |
|
89 | + [ |
|
90 | + 'user' => $user->getUID(), |
|
91 | + // parent share |
|
92 | + 'superShare' => $share[0], |
|
93 | + // children/component of the superShare |
|
94 | + 'groupedShares' => $share[1], |
|
95 | + ], |
|
96 | + $storageFactory |
|
97 | + ); |
|
98 | + } catch (\Exception $e) { |
|
99 | + $this->logger->logException($e); |
|
100 | + $this->logger->error('Error while trying to create shared mount'); |
|
101 | + } |
|
102 | + } |
|
103 | + |
|
104 | + // array_filter removes the null values from the array |
|
105 | + return array_filter($mounts); |
|
106 | + } |
|
107 | + |
|
108 | + /** |
|
109 | + * Groups shares by path (nodeId) and target path |
|
110 | + * |
|
111 | + * @param \OCP\Share\IShare[] $shares |
|
112 | + * @return \OCP\Share\IShare[][] array of grouped shares, each element in the |
|
113 | + * array is a group which itself is an array of shares |
|
114 | + */ |
|
115 | + private function groupShares(array $shares) { |
|
116 | + $tmp = []; |
|
117 | + |
|
118 | + foreach ($shares as $share) { |
|
119 | + if (!isset($tmp[$share->getNodeId()])) { |
|
120 | + $tmp[$share->getNodeId()] = []; |
|
121 | + } |
|
122 | + $tmp[$share->getNodeId()][] = $share; |
|
123 | + } |
|
124 | + |
|
125 | + $result = []; |
|
126 | + // sort by stime, the super share will be based on the least recent share |
|
127 | + foreach ($tmp as &$tmp2) { |
|
128 | + @usort($tmp2, function($a, $b) { |
|
129 | + if ($a->getShareTime() <= $b->getShareTime()) { |
|
130 | + return -1; |
|
131 | + } |
|
132 | + return 1; |
|
133 | + }); |
|
134 | + $result[] = $tmp2; |
|
135 | + } |
|
136 | + |
|
137 | + return array_values($result); |
|
138 | + } |
|
139 | + |
|
140 | + /** |
|
141 | + * Build super shares (virtual share) by grouping them by node id and target, |
|
142 | + * then for each group compute the super share and return it along with the matching |
|
143 | + * grouped shares. The most permissive permissions are used based on the permissions |
|
144 | + * of all shares within the group. |
|
145 | + * |
|
146 | + * @param \OCP\Share\IShare[] $allShares |
|
147 | + * @param \OCP\IUser $user user |
|
148 | + * @return array Tuple of [superShare, groupedShares] |
|
149 | + */ |
|
150 | + private function buildSuperShares(array $allShares, \OCP\IUser $user) { |
|
151 | + $result = []; |
|
152 | + |
|
153 | + $groupedShares = $this->groupShares($allShares); |
|
154 | + |
|
155 | + /** @var \OCP\Share\IShare[] $shares */ |
|
156 | + foreach ($groupedShares as $shares) { |
|
157 | + if (count($shares) === 0) { |
|
158 | + continue; |
|
159 | + } |
|
160 | + |
|
161 | + $superShare = $this->shareManager->newShare(); |
|
162 | + |
|
163 | + // compute super share based on first entry of the group |
|
164 | + $superShare->setId($shares[0]->getId()) |
|
165 | + ->setShareOwner($shares[0]->getShareOwner()) |
|
166 | + ->setNodeId($shares[0]->getNodeId()) |
|
167 | + ->setTarget($shares[0]->getTarget()); |
|
168 | + |
|
169 | + // use most permissive permissions |
|
170 | + $permissions = 0; |
|
171 | + foreach ($shares as $share) { |
|
172 | + $permissions |= $share->getPermissions(); |
|
173 | + if ($share->getTarget() !== $superShare->getTarget()) { |
|
174 | + // adjust target, for database consistency |
|
175 | + $share->setTarget($superShare->getTarget()); |
|
176 | + try { |
|
177 | + $this->shareManager->moveShare($share, $user->getUID()); |
|
178 | + } catch (\InvalidArgumentException $e) { |
|
179 | + // ignore as it is not important and we don't want to |
|
180 | + // block FS setup |
|
181 | + |
|
182 | + // the subsequent code anyway only uses the target of the |
|
183 | + // super share |
|
184 | + |
|
185 | + // such issue can usually happen when dealing with |
|
186 | + // null groups which usually appear with group backend |
|
187 | + // caching inconsistencies |
|
188 | + $this->logger->debug( |
|
189 | + 'Could not adjust share target for share ' . $share->getId() . ' to make it consistent: ' . $e->getMessage(), |
|
190 | + ['app' => 'files_sharing'] |
|
191 | + ); |
|
192 | + } |
|
193 | + } |
|
194 | + if (!is_null($share->getNodeCacheEntry())) { |
|
195 | + $superShare->setNodeCacheEntry($share->getNodeCacheEntry()); |
|
196 | + } |
|
197 | + } |
|
198 | + |
|
199 | + $superShare->setPermissions($permissions); |
|
200 | + |
|
201 | + $result[] = [$superShare, $shares]; |
|
202 | + } |
|
203 | + |
|
204 | + return $result; |
|
205 | + } |
|
206 | 206 | } |
@@ -29,141 +29,141 @@ |
||
29 | 29 | |
30 | 30 | class Entry implements IEntry { |
31 | 31 | |
32 | - /** @var string|int|null */ |
|
33 | - private $id = null; |
|
34 | - |
|
35 | - /** @var string */ |
|
36 | - private $fullName = ''; |
|
37 | - |
|
38 | - /** @var string[] */ |
|
39 | - private $emailAddresses = []; |
|
40 | - |
|
41 | - /** @var string|null */ |
|
42 | - private $avatar; |
|
43 | - |
|
44 | - /** @var IAction[] */ |
|
45 | - private $actions = []; |
|
46 | - |
|
47 | - /** @var array */ |
|
48 | - private $properties = []; |
|
49 | - |
|
50 | - /** |
|
51 | - * @param string $id |
|
52 | - */ |
|
53 | - public function setId($id) { |
|
54 | - $this->id = $id; |
|
55 | - } |
|
56 | - |
|
57 | - /** |
|
58 | - * @param string $displayName |
|
59 | - */ |
|
60 | - public function setFullName($displayName) { |
|
61 | - $this->fullName = $displayName; |
|
62 | - } |
|
63 | - |
|
64 | - /** |
|
65 | - * @return string |
|
66 | - */ |
|
67 | - public function getFullName() { |
|
68 | - return $this->fullName; |
|
69 | - } |
|
70 | - |
|
71 | - /** |
|
72 | - * @param string $address |
|
73 | - */ |
|
74 | - public function addEMailAddress($address) { |
|
75 | - $this->emailAddresses[] = $address; |
|
76 | - } |
|
77 | - |
|
78 | - /** |
|
79 | - * @return string |
|
80 | - */ |
|
81 | - public function getEMailAddresses() { |
|
82 | - return $this->emailAddresses; |
|
83 | - } |
|
84 | - |
|
85 | - /** |
|
86 | - * @param string $avatar |
|
87 | - */ |
|
88 | - public function setAvatar($avatar) { |
|
89 | - $this->avatar = $avatar; |
|
90 | - } |
|
91 | - |
|
92 | - /** |
|
93 | - * @return string |
|
94 | - */ |
|
95 | - public function getAvatar() { |
|
96 | - return $this->avatar; |
|
97 | - } |
|
98 | - |
|
99 | - /** |
|
100 | - * @param IAction $action |
|
101 | - */ |
|
102 | - public function addAction(IAction $action) { |
|
103 | - $this->actions[] = $action; |
|
104 | - $this->sortActions(); |
|
105 | - } |
|
106 | - |
|
107 | - /** |
|
108 | - * @return IAction[] |
|
109 | - */ |
|
110 | - public function getActions() { |
|
111 | - return $this->actions; |
|
112 | - } |
|
113 | - |
|
114 | - /** |
|
115 | - * sort the actions by priority and name |
|
116 | - */ |
|
117 | - private function sortActions() { |
|
118 | - usort($this->actions, function(IAction $action1, IAction $action2) { |
|
119 | - $prio1 = $action1->getPriority(); |
|
120 | - $prio2 = $action2->getPriority(); |
|
121 | - |
|
122 | - if ($prio1 === $prio2) { |
|
123 | - // Ascending order for same priority |
|
124 | - return strcasecmp($action1->getName(), $action2->getName()); |
|
125 | - } |
|
126 | - |
|
127 | - // Descending order when priority differs |
|
128 | - return $prio2 - $prio1; |
|
129 | - }); |
|
130 | - } |
|
131 | - |
|
132 | - /** |
|
133 | - * @param array $contact key-value array containing additional properties |
|
134 | - */ |
|
135 | - public function setProperties(array $contact) { |
|
136 | - $this->properties = $contact; |
|
137 | - } |
|
138 | - |
|
139 | - /** |
|
140 | - * @param string $key |
|
141 | - * @return mixed |
|
142 | - */ |
|
143 | - public function getProperty($key) { |
|
144 | - if (!isset($this->properties[$key])) { |
|
145 | - return null; |
|
146 | - } |
|
147 | - return $this->properties[$key]; |
|
148 | - } |
|
149 | - |
|
150 | - /** |
|
151 | - * @return array |
|
152 | - */ |
|
153 | - public function jsonSerialize() { |
|
154 | - $topAction = !empty($this->actions) ? $this->actions[0]->jsonSerialize() : null; |
|
155 | - $otherActions = array_map(function(IAction $action) { |
|
156 | - return $action->jsonSerialize(); |
|
157 | - }, array_slice($this->actions, 1)); |
|
158 | - |
|
159 | - return [ |
|
160 | - 'id' => $this->id, |
|
161 | - 'fullName' => $this->fullName, |
|
162 | - 'avatar' => $this->getAvatar(), |
|
163 | - 'topAction' => $topAction, |
|
164 | - 'actions' => $otherActions, |
|
165 | - 'lastMessage' => '', |
|
166 | - ]; |
|
167 | - } |
|
32 | + /** @var string|int|null */ |
|
33 | + private $id = null; |
|
34 | + |
|
35 | + /** @var string */ |
|
36 | + private $fullName = ''; |
|
37 | + |
|
38 | + /** @var string[] */ |
|
39 | + private $emailAddresses = []; |
|
40 | + |
|
41 | + /** @var string|null */ |
|
42 | + private $avatar; |
|
43 | + |
|
44 | + /** @var IAction[] */ |
|
45 | + private $actions = []; |
|
46 | + |
|
47 | + /** @var array */ |
|
48 | + private $properties = []; |
|
49 | + |
|
50 | + /** |
|
51 | + * @param string $id |
|
52 | + */ |
|
53 | + public function setId($id) { |
|
54 | + $this->id = $id; |
|
55 | + } |
|
56 | + |
|
57 | + /** |
|
58 | + * @param string $displayName |
|
59 | + */ |
|
60 | + public function setFullName($displayName) { |
|
61 | + $this->fullName = $displayName; |
|
62 | + } |
|
63 | + |
|
64 | + /** |
|
65 | + * @return string |
|
66 | + */ |
|
67 | + public function getFullName() { |
|
68 | + return $this->fullName; |
|
69 | + } |
|
70 | + |
|
71 | + /** |
|
72 | + * @param string $address |
|
73 | + */ |
|
74 | + public function addEMailAddress($address) { |
|
75 | + $this->emailAddresses[] = $address; |
|
76 | + } |
|
77 | + |
|
78 | + /** |
|
79 | + * @return string |
|
80 | + */ |
|
81 | + public function getEMailAddresses() { |
|
82 | + return $this->emailAddresses; |
|
83 | + } |
|
84 | + |
|
85 | + /** |
|
86 | + * @param string $avatar |
|
87 | + */ |
|
88 | + public function setAvatar($avatar) { |
|
89 | + $this->avatar = $avatar; |
|
90 | + } |
|
91 | + |
|
92 | + /** |
|
93 | + * @return string |
|
94 | + */ |
|
95 | + public function getAvatar() { |
|
96 | + return $this->avatar; |
|
97 | + } |
|
98 | + |
|
99 | + /** |
|
100 | + * @param IAction $action |
|
101 | + */ |
|
102 | + public function addAction(IAction $action) { |
|
103 | + $this->actions[] = $action; |
|
104 | + $this->sortActions(); |
|
105 | + } |
|
106 | + |
|
107 | + /** |
|
108 | + * @return IAction[] |
|
109 | + */ |
|
110 | + public function getActions() { |
|
111 | + return $this->actions; |
|
112 | + } |
|
113 | + |
|
114 | + /** |
|
115 | + * sort the actions by priority and name |
|
116 | + */ |
|
117 | + private function sortActions() { |
|
118 | + usort($this->actions, function(IAction $action1, IAction $action2) { |
|
119 | + $prio1 = $action1->getPriority(); |
|
120 | + $prio2 = $action2->getPriority(); |
|
121 | + |
|
122 | + if ($prio1 === $prio2) { |
|
123 | + // Ascending order for same priority |
|
124 | + return strcasecmp($action1->getName(), $action2->getName()); |
|
125 | + } |
|
126 | + |
|
127 | + // Descending order when priority differs |
|
128 | + return $prio2 - $prio1; |
|
129 | + }); |
|
130 | + } |
|
131 | + |
|
132 | + /** |
|
133 | + * @param array $contact key-value array containing additional properties |
|
134 | + */ |
|
135 | + public function setProperties(array $contact) { |
|
136 | + $this->properties = $contact; |
|
137 | + } |
|
138 | + |
|
139 | + /** |
|
140 | + * @param string $key |
|
141 | + * @return mixed |
|
142 | + */ |
|
143 | + public function getProperty($key) { |
|
144 | + if (!isset($this->properties[$key])) { |
|
145 | + return null; |
|
146 | + } |
|
147 | + return $this->properties[$key]; |
|
148 | + } |
|
149 | + |
|
150 | + /** |
|
151 | + * @return array |
|
152 | + */ |
|
153 | + public function jsonSerialize() { |
|
154 | + $topAction = !empty($this->actions) ? $this->actions[0]->jsonSerialize() : null; |
|
155 | + $otherActions = array_map(function(IAction $action) { |
|
156 | + return $action->jsonSerialize(); |
|
157 | + }, array_slice($this->actions, 1)); |
|
158 | + |
|
159 | + return [ |
|
160 | + 'id' => $this->id, |
|
161 | + 'fullName' => $this->fullName, |
|
162 | + 'avatar' => $this->getAvatar(), |
|
163 | + 'topAction' => $topAction, |
|
164 | + 'actions' => $otherActions, |
|
165 | + 'lastMessage' => '', |
|
166 | + ]; |
|
167 | + } |
|
168 | 168 | |
169 | 169 | } |
@@ -31,36 +31,36 @@ |
||
31 | 31 | */ |
32 | 32 | interface IEntry extends JsonSerializable { |
33 | 33 | |
34 | - /** |
|
35 | - * @since 12.0 |
|
36 | - * @return string |
|
37 | - */ |
|
38 | - public function getFullName(); |
|
34 | + /** |
|
35 | + * @since 12.0 |
|
36 | + * @return string |
|
37 | + */ |
|
38 | + public function getFullName(); |
|
39 | 39 | |
40 | - /** |
|
41 | - * @since 12.0 |
|
42 | - * @return string[] |
|
43 | - */ |
|
44 | - public function getEMailAddresses(); |
|
40 | + /** |
|
41 | + * @since 12.0 |
|
42 | + * @return string[] |
|
43 | + */ |
|
44 | + public function getEMailAddresses(); |
|
45 | 45 | |
46 | - /** |
|
47 | - * @since 12.0 |
|
48 | - * @return string|null image URI |
|
49 | - */ |
|
50 | - public function getAvatar(); |
|
46 | + /** |
|
47 | + * @since 12.0 |
|
48 | + * @return string|null image URI |
|
49 | + */ |
|
50 | + public function getAvatar(); |
|
51 | 51 | |
52 | - /** |
|
53 | - * @since 12.0 |
|
54 | - * @param IAction $action an action to show in the contacts menu |
|
55 | - */ |
|
56 | - public function addAction(IAction $action); |
|
52 | + /** |
|
53 | + * @since 12.0 |
|
54 | + * @param IAction $action an action to show in the contacts menu |
|
55 | + */ |
|
56 | + public function addAction(IAction $action); |
|
57 | 57 | |
58 | - /** |
|
59 | - * Get an arbitrary property from the contact |
|
60 | - * |
|
61 | - * @since 12.0 |
|
62 | - * @param string $key |
|
63 | - * @return mixed the value of the property or null |
|
64 | - */ |
|
65 | - public function getProperty($key); |
|
58 | + /** |
|
59 | + * Get an arbitrary property from the contact |
|
60 | + * |
|
61 | + * @since 12.0 |
|
62 | + * @param string $key |
|
63 | + * @return mixed the value of the property or null |
|
64 | + */ |
|
65 | + public function getProperty($key); |
|
66 | 66 | } |
@@ -39,313 +39,313 @@ |
||
39 | 39 | |
40 | 40 | class MDB2SchemaReader { |
41 | 41 | |
42 | - /** |
|
43 | - * @var string $DBTABLEPREFIX |
|
44 | - */ |
|
45 | - protected $DBTABLEPREFIX; |
|
42 | + /** |
|
43 | + * @var string $DBTABLEPREFIX |
|
44 | + */ |
|
45 | + protected $DBTABLEPREFIX; |
|
46 | 46 | |
47 | - /** |
|
48 | - * @var \Doctrine\DBAL\Platforms\AbstractPlatform $platform |
|
49 | - */ |
|
50 | - protected $platform; |
|
47 | + /** |
|
48 | + * @var \Doctrine\DBAL\Platforms\AbstractPlatform $platform |
|
49 | + */ |
|
50 | + protected $platform; |
|
51 | 51 | |
52 | - /** @var IConfig */ |
|
53 | - protected $config; |
|
52 | + /** @var IConfig */ |
|
53 | + protected $config; |
|
54 | 54 | |
55 | - /** |
|
56 | - * @param \OCP\IConfig $config |
|
57 | - * @param \Doctrine\DBAL\Platforms\AbstractPlatform $platform |
|
58 | - */ |
|
59 | - public function __construct(IConfig $config, AbstractPlatform $platform) { |
|
60 | - $this->platform = $platform; |
|
61 | - $this->config = $config; |
|
62 | - $this->DBTABLEPREFIX = $config->getSystemValue('dbtableprefix', 'oc_'); |
|
63 | - } |
|
55 | + /** |
|
56 | + * @param \OCP\IConfig $config |
|
57 | + * @param \Doctrine\DBAL\Platforms\AbstractPlatform $platform |
|
58 | + */ |
|
59 | + public function __construct(IConfig $config, AbstractPlatform $platform) { |
|
60 | + $this->platform = $platform; |
|
61 | + $this->config = $config; |
|
62 | + $this->DBTABLEPREFIX = $config->getSystemValue('dbtableprefix', 'oc_'); |
|
63 | + } |
|
64 | 64 | |
65 | - /** |
|
66 | - * @param string $file |
|
67 | - * @param Schema $schema |
|
68 | - * @return Schema |
|
69 | - * @throws \DomainException |
|
70 | - */ |
|
71 | - public function loadSchemaFromFile($file, Schema $schema) { |
|
72 | - $loadEntities = libxml_disable_entity_loader(false); |
|
73 | - $xml = simplexml_load_file($file); |
|
74 | - libxml_disable_entity_loader($loadEntities); |
|
75 | - foreach ($xml->children() as $child) { |
|
76 | - /** |
|
77 | - * @var \SimpleXMLElement $child |
|
78 | - */ |
|
79 | - switch ($child->getName()) { |
|
80 | - case 'name': |
|
81 | - case 'create': |
|
82 | - case 'overwrite': |
|
83 | - case 'charset': |
|
84 | - break; |
|
85 | - case 'table': |
|
86 | - $this->loadTable($schema, $child); |
|
87 | - break; |
|
88 | - default: |
|
89 | - throw new \DomainException('Unknown element: ' . $child->getName()); |
|
65 | + /** |
|
66 | + * @param string $file |
|
67 | + * @param Schema $schema |
|
68 | + * @return Schema |
|
69 | + * @throws \DomainException |
|
70 | + */ |
|
71 | + public function loadSchemaFromFile($file, Schema $schema) { |
|
72 | + $loadEntities = libxml_disable_entity_loader(false); |
|
73 | + $xml = simplexml_load_file($file); |
|
74 | + libxml_disable_entity_loader($loadEntities); |
|
75 | + foreach ($xml->children() as $child) { |
|
76 | + /** |
|
77 | + * @var \SimpleXMLElement $child |
|
78 | + */ |
|
79 | + switch ($child->getName()) { |
|
80 | + case 'name': |
|
81 | + case 'create': |
|
82 | + case 'overwrite': |
|
83 | + case 'charset': |
|
84 | + break; |
|
85 | + case 'table': |
|
86 | + $this->loadTable($schema, $child); |
|
87 | + break; |
|
88 | + default: |
|
89 | + throw new \DomainException('Unknown element: ' . $child->getName()); |
|
90 | 90 | |
91 | - } |
|
92 | - } |
|
93 | - return $schema; |
|
94 | - } |
|
91 | + } |
|
92 | + } |
|
93 | + return $schema; |
|
94 | + } |
|
95 | 95 | |
96 | - /** |
|
97 | - * @param \Doctrine\DBAL\Schema\Schema $schema |
|
98 | - * @param \SimpleXMLElement $xml |
|
99 | - * @throws \DomainException |
|
100 | - */ |
|
101 | - private function loadTable($schema, $xml) { |
|
102 | - $table = null; |
|
103 | - foreach ($xml->children() as $child) { |
|
104 | - /** |
|
105 | - * @var \SimpleXMLElement $child |
|
106 | - */ |
|
107 | - switch ($child->getName()) { |
|
108 | - case 'name': |
|
109 | - $name = (string)$child; |
|
110 | - $name = str_replace('*dbprefix*', $this->DBTABLEPREFIX, $name); |
|
111 | - $name = $this->platform->quoteIdentifier($name); |
|
112 | - $table = $schema->createTable($name); |
|
113 | - break; |
|
114 | - case 'create': |
|
115 | - case 'overwrite': |
|
116 | - case 'charset': |
|
117 | - break; |
|
118 | - case 'declaration': |
|
119 | - if (is_null($table)) { |
|
120 | - throw new \DomainException('Table declaration before table name'); |
|
121 | - } |
|
122 | - $this->loadDeclaration($table, $child); |
|
123 | - break; |
|
124 | - default: |
|
125 | - throw new \DomainException('Unknown element: ' . $child->getName()); |
|
96 | + /** |
|
97 | + * @param \Doctrine\DBAL\Schema\Schema $schema |
|
98 | + * @param \SimpleXMLElement $xml |
|
99 | + * @throws \DomainException |
|
100 | + */ |
|
101 | + private function loadTable($schema, $xml) { |
|
102 | + $table = null; |
|
103 | + foreach ($xml->children() as $child) { |
|
104 | + /** |
|
105 | + * @var \SimpleXMLElement $child |
|
106 | + */ |
|
107 | + switch ($child->getName()) { |
|
108 | + case 'name': |
|
109 | + $name = (string)$child; |
|
110 | + $name = str_replace('*dbprefix*', $this->DBTABLEPREFIX, $name); |
|
111 | + $name = $this->platform->quoteIdentifier($name); |
|
112 | + $table = $schema->createTable($name); |
|
113 | + break; |
|
114 | + case 'create': |
|
115 | + case 'overwrite': |
|
116 | + case 'charset': |
|
117 | + break; |
|
118 | + case 'declaration': |
|
119 | + if (is_null($table)) { |
|
120 | + throw new \DomainException('Table declaration before table name'); |
|
121 | + } |
|
122 | + $this->loadDeclaration($table, $child); |
|
123 | + break; |
|
124 | + default: |
|
125 | + throw new \DomainException('Unknown element: ' . $child->getName()); |
|
126 | 126 | |
127 | - } |
|
128 | - } |
|
129 | - } |
|
127 | + } |
|
128 | + } |
|
129 | + } |
|
130 | 130 | |
131 | - /** |
|
132 | - * @param \Doctrine\DBAL\Schema\Table $table |
|
133 | - * @param \SimpleXMLElement $xml |
|
134 | - * @throws \DomainException |
|
135 | - */ |
|
136 | - private function loadDeclaration($table, $xml) { |
|
137 | - foreach ($xml->children() as $child) { |
|
138 | - /** |
|
139 | - * @var \SimpleXMLElement $child |
|
140 | - */ |
|
141 | - switch ($child->getName()) { |
|
142 | - case 'field': |
|
143 | - $this->loadField($table, $child); |
|
144 | - break; |
|
145 | - case 'index': |
|
146 | - $this->loadIndex($table, $child); |
|
147 | - break; |
|
148 | - default: |
|
149 | - throw new \DomainException('Unknown element: ' . $child->getName()); |
|
131 | + /** |
|
132 | + * @param \Doctrine\DBAL\Schema\Table $table |
|
133 | + * @param \SimpleXMLElement $xml |
|
134 | + * @throws \DomainException |
|
135 | + */ |
|
136 | + private function loadDeclaration($table, $xml) { |
|
137 | + foreach ($xml->children() as $child) { |
|
138 | + /** |
|
139 | + * @var \SimpleXMLElement $child |
|
140 | + */ |
|
141 | + switch ($child->getName()) { |
|
142 | + case 'field': |
|
143 | + $this->loadField($table, $child); |
|
144 | + break; |
|
145 | + case 'index': |
|
146 | + $this->loadIndex($table, $child); |
|
147 | + break; |
|
148 | + default: |
|
149 | + throw new \DomainException('Unknown element: ' . $child->getName()); |
|
150 | 150 | |
151 | - } |
|
152 | - } |
|
153 | - } |
|
151 | + } |
|
152 | + } |
|
153 | + } |
|
154 | 154 | |
155 | - /** |
|
156 | - * @param \Doctrine\DBAL\Schema\Table $table |
|
157 | - * @param \SimpleXMLElement $xml |
|
158 | - * @throws \DomainException |
|
159 | - */ |
|
160 | - private function loadField($table, $xml) { |
|
161 | - $options = array( 'notnull' => false ); |
|
162 | - foreach ($xml->children() as $child) { |
|
163 | - /** |
|
164 | - * @var \SimpleXMLElement $child |
|
165 | - */ |
|
166 | - switch ($child->getName()) { |
|
167 | - case 'name': |
|
168 | - $name = (string)$child; |
|
169 | - $name = $this->platform->quoteIdentifier($name); |
|
170 | - break; |
|
171 | - case 'type': |
|
172 | - $type = (string)$child; |
|
173 | - switch ($type) { |
|
174 | - case 'text': |
|
175 | - $type = 'string'; |
|
176 | - break; |
|
177 | - case 'clob': |
|
178 | - $type = 'text'; |
|
179 | - break; |
|
180 | - case 'timestamp': |
|
181 | - $type = 'datetime'; |
|
182 | - break; |
|
183 | - case 'numeric': |
|
184 | - $type = 'decimal'; |
|
185 | - break; |
|
186 | - } |
|
187 | - break; |
|
188 | - case 'length': |
|
189 | - $length = (string)$child; |
|
190 | - $options['length'] = $length; |
|
191 | - break; |
|
192 | - case 'unsigned': |
|
193 | - $unsigned = $this->asBool($child); |
|
194 | - $options['unsigned'] = $unsigned; |
|
195 | - break; |
|
196 | - case 'notnull': |
|
197 | - $notnull = $this->asBool($child); |
|
198 | - $options['notnull'] = $notnull; |
|
199 | - break; |
|
200 | - case 'autoincrement': |
|
201 | - $autoincrement = $this->asBool($child); |
|
202 | - $options['autoincrement'] = $autoincrement; |
|
203 | - break; |
|
204 | - case 'default': |
|
205 | - $default = (string)$child; |
|
206 | - $options['default'] = $default; |
|
207 | - break; |
|
208 | - case 'comments': |
|
209 | - $comment = (string)$child; |
|
210 | - $options['comment'] = $comment; |
|
211 | - break; |
|
212 | - case 'primary': |
|
213 | - $primary = $this->asBool($child); |
|
214 | - $options['primary'] = $primary; |
|
215 | - break; |
|
216 | - case 'precision': |
|
217 | - $precision = (string)$child; |
|
218 | - $options['precision'] = $precision; |
|
219 | - break; |
|
220 | - case 'scale': |
|
221 | - $scale = (string)$child; |
|
222 | - $options['scale'] = $scale; |
|
223 | - break; |
|
224 | - default: |
|
225 | - throw new \DomainException('Unknown element: ' . $child->getName()); |
|
155 | + /** |
|
156 | + * @param \Doctrine\DBAL\Schema\Table $table |
|
157 | + * @param \SimpleXMLElement $xml |
|
158 | + * @throws \DomainException |
|
159 | + */ |
|
160 | + private function loadField($table, $xml) { |
|
161 | + $options = array( 'notnull' => false ); |
|
162 | + foreach ($xml->children() as $child) { |
|
163 | + /** |
|
164 | + * @var \SimpleXMLElement $child |
|
165 | + */ |
|
166 | + switch ($child->getName()) { |
|
167 | + case 'name': |
|
168 | + $name = (string)$child; |
|
169 | + $name = $this->platform->quoteIdentifier($name); |
|
170 | + break; |
|
171 | + case 'type': |
|
172 | + $type = (string)$child; |
|
173 | + switch ($type) { |
|
174 | + case 'text': |
|
175 | + $type = 'string'; |
|
176 | + break; |
|
177 | + case 'clob': |
|
178 | + $type = 'text'; |
|
179 | + break; |
|
180 | + case 'timestamp': |
|
181 | + $type = 'datetime'; |
|
182 | + break; |
|
183 | + case 'numeric': |
|
184 | + $type = 'decimal'; |
|
185 | + break; |
|
186 | + } |
|
187 | + break; |
|
188 | + case 'length': |
|
189 | + $length = (string)$child; |
|
190 | + $options['length'] = $length; |
|
191 | + break; |
|
192 | + case 'unsigned': |
|
193 | + $unsigned = $this->asBool($child); |
|
194 | + $options['unsigned'] = $unsigned; |
|
195 | + break; |
|
196 | + case 'notnull': |
|
197 | + $notnull = $this->asBool($child); |
|
198 | + $options['notnull'] = $notnull; |
|
199 | + break; |
|
200 | + case 'autoincrement': |
|
201 | + $autoincrement = $this->asBool($child); |
|
202 | + $options['autoincrement'] = $autoincrement; |
|
203 | + break; |
|
204 | + case 'default': |
|
205 | + $default = (string)$child; |
|
206 | + $options['default'] = $default; |
|
207 | + break; |
|
208 | + case 'comments': |
|
209 | + $comment = (string)$child; |
|
210 | + $options['comment'] = $comment; |
|
211 | + break; |
|
212 | + case 'primary': |
|
213 | + $primary = $this->asBool($child); |
|
214 | + $options['primary'] = $primary; |
|
215 | + break; |
|
216 | + case 'precision': |
|
217 | + $precision = (string)$child; |
|
218 | + $options['precision'] = $precision; |
|
219 | + break; |
|
220 | + case 'scale': |
|
221 | + $scale = (string)$child; |
|
222 | + $options['scale'] = $scale; |
|
223 | + break; |
|
224 | + default: |
|
225 | + throw new \DomainException('Unknown element: ' . $child->getName()); |
|
226 | 226 | |
227 | - } |
|
228 | - } |
|
229 | - if (isset($name) && isset($type)) { |
|
230 | - if (isset($options['default']) && empty($options['default'])) { |
|
231 | - if (empty($options['notnull']) || !$options['notnull']) { |
|
232 | - unset($options['default']); |
|
233 | - $options['notnull'] = false; |
|
234 | - } else { |
|
235 | - $options['default'] = ''; |
|
236 | - } |
|
237 | - if ($type == 'integer' || $type == 'decimal') { |
|
238 | - $options['default'] = 0; |
|
239 | - } elseif ($type == 'boolean') { |
|
240 | - $options['default'] = false; |
|
241 | - } |
|
242 | - if (!empty($options['autoincrement']) && $options['autoincrement']) { |
|
243 | - unset($options['default']); |
|
244 | - } |
|
245 | - } |
|
246 | - if ($type === 'integer' && isset($options['default'])) { |
|
247 | - $options['default'] = (int)$options['default']; |
|
248 | - } |
|
249 | - if ($type === 'integer' && isset($options['length'])) { |
|
250 | - $length = $options['length']; |
|
251 | - if ($length < 4) { |
|
252 | - $type = 'smallint'; |
|
253 | - } else if ($length > 4) { |
|
254 | - $type = 'bigint'; |
|
255 | - } |
|
256 | - } |
|
257 | - if ($type === 'boolean' && isset($options['default'])) { |
|
258 | - $options['default'] = $this->asBool($options['default']); |
|
259 | - } |
|
260 | - if (!empty($options['autoincrement']) |
|
261 | - && !empty($options['notnull']) |
|
262 | - ) { |
|
263 | - $options['primary'] = true; |
|
264 | - } |
|
227 | + } |
|
228 | + } |
|
229 | + if (isset($name) && isset($type)) { |
|
230 | + if (isset($options['default']) && empty($options['default'])) { |
|
231 | + if (empty($options['notnull']) || !$options['notnull']) { |
|
232 | + unset($options['default']); |
|
233 | + $options['notnull'] = false; |
|
234 | + } else { |
|
235 | + $options['default'] = ''; |
|
236 | + } |
|
237 | + if ($type == 'integer' || $type == 'decimal') { |
|
238 | + $options['default'] = 0; |
|
239 | + } elseif ($type == 'boolean') { |
|
240 | + $options['default'] = false; |
|
241 | + } |
|
242 | + if (!empty($options['autoincrement']) && $options['autoincrement']) { |
|
243 | + unset($options['default']); |
|
244 | + } |
|
245 | + } |
|
246 | + if ($type === 'integer' && isset($options['default'])) { |
|
247 | + $options['default'] = (int)$options['default']; |
|
248 | + } |
|
249 | + if ($type === 'integer' && isset($options['length'])) { |
|
250 | + $length = $options['length']; |
|
251 | + if ($length < 4) { |
|
252 | + $type = 'smallint'; |
|
253 | + } else if ($length > 4) { |
|
254 | + $type = 'bigint'; |
|
255 | + } |
|
256 | + } |
|
257 | + if ($type === 'boolean' && isset($options['default'])) { |
|
258 | + $options['default'] = $this->asBool($options['default']); |
|
259 | + } |
|
260 | + if (!empty($options['autoincrement']) |
|
261 | + && !empty($options['notnull']) |
|
262 | + ) { |
|
263 | + $options['primary'] = true; |
|
264 | + } |
|
265 | 265 | |
266 | - $table->addColumn($name, $type, $options); |
|
267 | - if (!empty($options['primary']) && $options['primary']) { |
|
268 | - $table->setPrimaryKey(array($name)); |
|
269 | - } |
|
270 | - } |
|
271 | - } |
|
266 | + $table->addColumn($name, $type, $options); |
|
267 | + if (!empty($options['primary']) && $options['primary']) { |
|
268 | + $table->setPrimaryKey(array($name)); |
|
269 | + } |
|
270 | + } |
|
271 | + } |
|
272 | 272 | |
273 | - /** |
|
274 | - * @param \Doctrine\DBAL\Schema\Table $table |
|
275 | - * @param \SimpleXMLElement $xml |
|
276 | - * @throws \DomainException |
|
277 | - */ |
|
278 | - private function loadIndex($table, $xml) { |
|
279 | - $name = null; |
|
280 | - $fields = array(); |
|
281 | - foreach ($xml->children() as $child) { |
|
282 | - /** |
|
283 | - * @var \SimpleXMLElement $child |
|
284 | - */ |
|
285 | - switch ($child->getName()) { |
|
286 | - case 'name': |
|
287 | - $name = (string)$child; |
|
288 | - break; |
|
289 | - case 'primary': |
|
290 | - $primary = $this->asBool($child); |
|
291 | - break; |
|
292 | - case 'unique': |
|
293 | - $unique = $this->asBool($child); |
|
294 | - break; |
|
295 | - case 'field': |
|
296 | - foreach ($child->children() as $field) { |
|
297 | - /** |
|
298 | - * @var \SimpleXMLElement $field |
|
299 | - */ |
|
300 | - switch ($field->getName()) { |
|
301 | - case 'name': |
|
302 | - $field_name = (string)$field; |
|
303 | - $field_name = $this->platform->quoteIdentifier($field_name); |
|
304 | - $fields[] = $field_name; |
|
305 | - break; |
|
306 | - case 'sorting': |
|
307 | - break; |
|
308 | - default: |
|
309 | - throw new \DomainException('Unknown element: ' . $field->getName()); |
|
273 | + /** |
|
274 | + * @param \Doctrine\DBAL\Schema\Table $table |
|
275 | + * @param \SimpleXMLElement $xml |
|
276 | + * @throws \DomainException |
|
277 | + */ |
|
278 | + private function loadIndex($table, $xml) { |
|
279 | + $name = null; |
|
280 | + $fields = array(); |
|
281 | + foreach ($xml->children() as $child) { |
|
282 | + /** |
|
283 | + * @var \SimpleXMLElement $child |
|
284 | + */ |
|
285 | + switch ($child->getName()) { |
|
286 | + case 'name': |
|
287 | + $name = (string)$child; |
|
288 | + break; |
|
289 | + case 'primary': |
|
290 | + $primary = $this->asBool($child); |
|
291 | + break; |
|
292 | + case 'unique': |
|
293 | + $unique = $this->asBool($child); |
|
294 | + break; |
|
295 | + case 'field': |
|
296 | + foreach ($child->children() as $field) { |
|
297 | + /** |
|
298 | + * @var \SimpleXMLElement $field |
|
299 | + */ |
|
300 | + switch ($field->getName()) { |
|
301 | + case 'name': |
|
302 | + $field_name = (string)$field; |
|
303 | + $field_name = $this->platform->quoteIdentifier($field_name); |
|
304 | + $fields[] = $field_name; |
|
305 | + break; |
|
306 | + case 'sorting': |
|
307 | + break; |
|
308 | + default: |
|
309 | + throw new \DomainException('Unknown element: ' . $field->getName()); |
|
310 | 310 | |
311 | - } |
|
312 | - } |
|
313 | - break; |
|
314 | - default: |
|
315 | - throw new \DomainException('Unknown element: ' . $child->getName()); |
|
311 | + } |
|
312 | + } |
|
313 | + break; |
|
314 | + default: |
|
315 | + throw new \DomainException('Unknown element: ' . $child->getName()); |
|
316 | 316 | |
317 | - } |
|
318 | - } |
|
319 | - if (!empty($fields)) { |
|
320 | - if (isset($primary) && $primary) { |
|
321 | - if ($table->hasPrimaryKey()) { |
|
322 | - return; |
|
323 | - } |
|
324 | - $table->setPrimaryKey($fields, $name); |
|
325 | - } else { |
|
326 | - if (isset($unique) && $unique) { |
|
327 | - $table->addUniqueIndex($fields, $name); |
|
328 | - } else { |
|
329 | - $table->addIndex($fields, $name); |
|
330 | - } |
|
331 | - } |
|
332 | - } else { |
|
333 | - throw new \DomainException('Empty index definition: ' . $name . ' options:' . print_r($fields, true)); |
|
334 | - } |
|
335 | - } |
|
317 | + } |
|
318 | + } |
|
319 | + if (!empty($fields)) { |
|
320 | + if (isset($primary) && $primary) { |
|
321 | + if ($table->hasPrimaryKey()) { |
|
322 | + return; |
|
323 | + } |
|
324 | + $table->setPrimaryKey($fields, $name); |
|
325 | + } else { |
|
326 | + if (isset($unique) && $unique) { |
|
327 | + $table->addUniqueIndex($fields, $name); |
|
328 | + } else { |
|
329 | + $table->addIndex($fields, $name); |
|
330 | + } |
|
331 | + } |
|
332 | + } else { |
|
333 | + throw new \DomainException('Empty index definition: ' . $name . ' options:' . print_r($fields, true)); |
|
334 | + } |
|
335 | + } |
|
336 | 336 | |
337 | - /** |
|
338 | - * @param \SimpleXMLElement|string $xml |
|
339 | - * @return bool |
|
340 | - */ |
|
341 | - private function asBool($xml) { |
|
342 | - $result = (string)$xml; |
|
343 | - if ($result == 'true') { |
|
344 | - $result = true; |
|
345 | - } elseif ($result == 'false') { |
|
346 | - $result = false; |
|
347 | - } |
|
348 | - return (bool)$result; |
|
349 | - } |
|
337 | + /** |
|
338 | + * @param \SimpleXMLElement|string $xml |
|
339 | + * @return bool |
|
340 | + */ |
|
341 | + private function asBool($xml) { |
|
342 | + $result = (string)$xml; |
|
343 | + if ($result == 'true') { |
|
344 | + $result = true; |
|
345 | + } elseif ($result == 'false') { |
|
346 | + $result = false; |
|
347 | + } |
|
348 | + return (bool)$result; |
|
349 | + } |
|
350 | 350 | |
351 | 351 | } |
@@ -47,58 +47,58 @@ |
||
47 | 47 | */ |
48 | 48 | interface IContainer { |
49 | 49 | |
50 | - /** |
|
51 | - * If a parameter is not registered in the container try to instantiate it |
|
52 | - * by using reflection to find out how to build the class |
|
53 | - * @param string $name the class name to resolve |
|
54 | - * @return \stdClass |
|
55 | - * @since 8.2.0 |
|
56 | - * @throws QueryException if the class could not be found or instantiated |
|
57 | - */ |
|
58 | - public function resolve($name); |
|
50 | + /** |
|
51 | + * If a parameter is not registered in the container try to instantiate it |
|
52 | + * by using reflection to find out how to build the class |
|
53 | + * @param string $name the class name to resolve |
|
54 | + * @return \stdClass |
|
55 | + * @since 8.2.0 |
|
56 | + * @throws QueryException if the class could not be found or instantiated |
|
57 | + */ |
|
58 | + public function resolve($name); |
|
59 | 59 | |
60 | - /** |
|
61 | - * Look up a service for a given name in the container. |
|
62 | - * |
|
63 | - * @param string $name |
|
64 | - * @return mixed |
|
65 | - * @throws QueryException if the query could not be resolved |
|
66 | - * @since 6.0.0 |
|
67 | - */ |
|
68 | - public function query($name); |
|
60 | + /** |
|
61 | + * Look up a service for a given name in the container. |
|
62 | + * |
|
63 | + * @param string $name |
|
64 | + * @return mixed |
|
65 | + * @throws QueryException if the query could not be resolved |
|
66 | + * @since 6.0.0 |
|
67 | + */ |
|
68 | + public function query($name); |
|
69 | 69 | |
70 | - /** |
|
71 | - * A value is stored in the container with it's corresponding name |
|
72 | - * |
|
73 | - * @param string $name |
|
74 | - * @param mixed $value |
|
75 | - * @return void |
|
76 | - * @since 6.0.0 |
|
77 | - */ |
|
78 | - public function registerParameter($name, $value); |
|
70 | + /** |
|
71 | + * A value is stored in the container with it's corresponding name |
|
72 | + * |
|
73 | + * @param string $name |
|
74 | + * @param mixed $value |
|
75 | + * @return void |
|
76 | + * @since 6.0.0 |
|
77 | + */ |
|
78 | + public function registerParameter($name, $value); |
|
79 | 79 | |
80 | - /** |
|
81 | - * A service is registered in the container where a closure is passed in which will actually |
|
82 | - * create the service on demand. |
|
83 | - * In case the parameter $shared is set to true (the default usage) the once created service will remain in |
|
84 | - * memory and be reused on subsequent calls. |
|
85 | - * In case the parameter is false the service will be recreated on every call. |
|
86 | - * |
|
87 | - * @param string $name |
|
88 | - * @param \Closure $closure |
|
89 | - * @param bool $shared |
|
90 | - * @return void |
|
91 | - * @since 6.0.0 |
|
92 | - */ |
|
93 | - public function registerService($name, Closure $closure, $shared = true); |
|
80 | + /** |
|
81 | + * A service is registered in the container where a closure is passed in which will actually |
|
82 | + * create the service on demand. |
|
83 | + * In case the parameter $shared is set to true (the default usage) the once created service will remain in |
|
84 | + * memory and be reused on subsequent calls. |
|
85 | + * In case the parameter is false the service will be recreated on every call. |
|
86 | + * |
|
87 | + * @param string $name |
|
88 | + * @param \Closure $closure |
|
89 | + * @param bool $shared |
|
90 | + * @return void |
|
91 | + * @since 6.0.0 |
|
92 | + */ |
|
93 | + public function registerService($name, Closure $closure, $shared = true); |
|
94 | 94 | |
95 | - /** |
|
96 | - * Shortcut for returning a service from a service under a different key, |
|
97 | - * e.g. to tell the container to return a class when queried for an |
|
98 | - * interface |
|
99 | - * @param string $alias the alias that should be registered |
|
100 | - * @param string $target the target that should be resolved instead |
|
101 | - * @since 8.2.0 |
|
102 | - */ |
|
103 | - public function registerAlias($alias, $target); |
|
95 | + /** |
|
96 | + * Shortcut for returning a service from a service under a different key, |
|
97 | + * e.g. to tell the container to return a class when queried for an |
|
98 | + * interface |
|
99 | + * @param string $alias the alias that should be registered |
|
100 | + * @param string $target the target that should be resolved instead |
|
101 | + * @since 8.2.0 |
|
102 | + */ |
|
103 | + public function registerAlias($alias, $target); |
|
104 | 104 | } |
@@ -29,159 +29,159 @@ |
||
29 | 29 | |
30 | 30 | class FileMimeType extends AbstractStringCheck { |
31 | 31 | |
32 | - /** @var array */ |
|
33 | - protected $mimeType; |
|
34 | - |
|
35 | - /** @var IRequest */ |
|
36 | - protected $request; |
|
37 | - |
|
38 | - /** @var IMimeTypeDetector */ |
|
39 | - protected $mimeTypeDetector; |
|
40 | - |
|
41 | - /** @var IStorage */ |
|
42 | - protected $storage; |
|
43 | - |
|
44 | - /** @var string */ |
|
45 | - protected $path; |
|
46 | - |
|
47 | - /** |
|
48 | - * @param IL10N $l |
|
49 | - * @param IRequest $request |
|
50 | - * @param IMimeTypeDetector $mimeTypeDetector |
|
51 | - */ |
|
52 | - public function __construct(IL10N $l, IRequest $request, IMimeTypeDetector $mimeTypeDetector) { |
|
53 | - parent::__construct($l); |
|
54 | - $this->request = $request; |
|
55 | - $this->mimeTypeDetector = $mimeTypeDetector; |
|
56 | - } |
|
57 | - |
|
58 | - /** |
|
59 | - * @param IStorage $storage |
|
60 | - * @param string $path |
|
61 | - */ |
|
62 | - public function setFileInfo(IStorage $storage, $path) { |
|
63 | - $this->storage = $storage; |
|
64 | - $this->path = $path; |
|
65 | - if (!isset($this->mimeType[$this->storage->getId()][$this->path]) |
|
66 | - || $this->mimeType[$this->storage->getId()][$this->path] === '') { |
|
67 | - $this->mimeType[$this->storage->getId()][$this->path] = null; |
|
68 | - } |
|
69 | - } |
|
70 | - |
|
71 | - /** |
|
72 | - * @return string |
|
73 | - */ |
|
74 | - protected function getActualValue() { |
|
75 | - if ($this->mimeType[$this->storage->getId()][$this->path] !== null) { |
|
76 | - return $this->mimeType[$this->storage->getId()][$this->path]; |
|
77 | - } |
|
78 | - |
|
79 | - if ($this->isWebDAVRequest()) { |
|
80 | - // Creating a folder |
|
81 | - if ($this->request->getMethod() === 'MKCOL') { |
|
82 | - $this->mimeType[$this->storage->getId()][$this->path] = 'httpd/unix-directory'; |
|
83 | - return $this->mimeType[$this->storage->getId()][$this->path]; |
|
84 | - } |
|
85 | - |
|
86 | - if ($this->request->getMethod() === 'PUT') { |
|
87 | - $path = $this->request->getPathInfo(); |
|
88 | - $this->mimeType[$this->storage->getId()][$this->path] = $this->mimeTypeDetector->detectPath($path); |
|
89 | - return $this->mimeType[$this->storage->getId()][$this->path]; |
|
90 | - } |
|
91 | - } else if ($this->isPublicWebDAVRequest()) { |
|
92 | - if ($this->request->getMethod() === 'PUT') { |
|
93 | - $path = $this->request->getPathInfo(); |
|
94 | - if (strpos($path, '/webdav/') === 0) { |
|
95 | - $path = substr($path, strlen('/webdav')); |
|
96 | - } |
|
97 | - $path = $this->path . $path; |
|
98 | - $this->mimeType[$this->storage->getId()][$path] = $this->mimeTypeDetector->detectPath($path); |
|
99 | - return $this->mimeType[$this->storage->getId()][$path]; |
|
100 | - } |
|
101 | - } |
|
102 | - |
|
103 | - if (in_array($this->request->getMethod(), ['POST', 'PUT'])) { |
|
104 | - $files = $this->request->getUploadedFile('files'); |
|
105 | - if (isset($files['type'][0])) { |
|
106 | - $mimeType = $files['type'][0]; |
|
107 | - if ($this->mimeType === 'application/octet-stream') { |
|
108 | - // Maybe not... |
|
109 | - $mimeTypeTest = $this->mimeTypeDetector->detectPath($files['name'][0]); |
|
110 | - if ($mimeTypeTest !== 'application/octet-stream' && $mimeTypeTest !== false) { |
|
111 | - $mimeType = $mimeTypeTest; |
|
112 | - } else { |
|
113 | - $mimeTypeTest = $this->mimeTypeDetector->detect($files['tmp_name'][0]); |
|
114 | - if ($mimeTypeTest !== 'application/octet-stream' && $mimeTypeTest !== false) { |
|
115 | - $mimeType = $mimeTypeTest; |
|
116 | - } |
|
117 | - } |
|
118 | - } |
|
119 | - $this->mimeType[$this->storage->getId()][$this->path] = $mimeType; |
|
120 | - return $mimeType; |
|
121 | - } |
|
122 | - } |
|
123 | - |
|
124 | - $this->mimeType[$this->storage->getId()][$this->path] = $this->storage->getMimeType($this->path); |
|
125 | - if ($this->mimeType[$this->storage->getId()][$this->path] === 'application/octet-stream') { |
|
126 | - $this->mimeType[$this->storage->getId()][$this->path] = $this->detectMimetypeFromPath(); |
|
127 | - } |
|
128 | - |
|
129 | - return $this->mimeType[$this->storage->getId()][$this->path]; |
|
130 | - } |
|
131 | - |
|
132 | - /** |
|
133 | - * @return string |
|
134 | - */ |
|
135 | - protected function detectMimetypeFromPath() { |
|
136 | - $mimeType = $this->mimeTypeDetector->detectPath($this->path); |
|
137 | - if ($mimeType !== 'application/octet-stream' && $mimeType !== false) { |
|
138 | - return $mimeType; |
|
139 | - } |
|
140 | - |
|
141 | - if ($this->storage->instanceOfStorage('\OC\Files\Storage\Local') |
|
142 | - || $this->storage->instanceOfStorage('\OC\Files\Storage\Home') |
|
143 | - || $this->storage->instanceOfStorage('\OC\Files\ObjectStore\HomeObjectStoreStorage')) { |
|
144 | - $localFile = $this->storage->getLocalFile($this->path); |
|
145 | - if ($localFile !== false) { |
|
146 | - $mimeType = $this->mimeTypeDetector->detect($localFile); |
|
147 | - if ($mimeType !== false) { |
|
148 | - return $mimeType; |
|
149 | - } |
|
150 | - } |
|
151 | - |
|
152 | - return 'application/octet-stream'; |
|
153 | - } else { |
|
154 | - $handle = $this->storage->fopen($this->path, 'r'); |
|
155 | - $data = fread($handle, 8024); |
|
156 | - fclose($handle); |
|
157 | - $mimeType = $this->mimeTypeDetector->detectString($data); |
|
158 | - if ($mimeType !== false) { |
|
159 | - return $mimeType; |
|
160 | - } |
|
161 | - |
|
162 | - return 'application/octet-stream'; |
|
163 | - } |
|
164 | - } |
|
165 | - |
|
166 | - /** |
|
167 | - * @return bool |
|
168 | - */ |
|
169 | - protected function isWebDAVRequest() { |
|
170 | - return substr($this->request->getScriptName(), 0 - strlen('/remote.php')) === '/remote.php' && ( |
|
171 | - $this->request->getPathInfo() === '/webdav' || |
|
172 | - strpos($this->request->getPathInfo(), '/webdav/') === 0 || |
|
173 | - $this->request->getPathInfo() === '/dav/files' || |
|
174 | - strpos($this->request->getPathInfo(), '/dav/files/') === 0 |
|
175 | - ); |
|
176 | - } |
|
177 | - |
|
178 | - /** |
|
179 | - * @return bool |
|
180 | - */ |
|
181 | - protected function isPublicWebDAVRequest() { |
|
182 | - return substr($this->request->getScriptName(), 0 - strlen('/public.php')) === '/public.php' && ( |
|
183 | - $this->request->getPathInfo() === '/webdav' || |
|
184 | - strpos($this->request->getPathInfo(), '/webdav/') === 0 |
|
185 | - ); |
|
186 | - } |
|
32 | + /** @var array */ |
|
33 | + protected $mimeType; |
|
34 | + |
|
35 | + /** @var IRequest */ |
|
36 | + protected $request; |
|
37 | + |
|
38 | + /** @var IMimeTypeDetector */ |
|
39 | + protected $mimeTypeDetector; |
|
40 | + |
|
41 | + /** @var IStorage */ |
|
42 | + protected $storage; |
|
43 | + |
|
44 | + /** @var string */ |
|
45 | + protected $path; |
|
46 | + |
|
47 | + /** |
|
48 | + * @param IL10N $l |
|
49 | + * @param IRequest $request |
|
50 | + * @param IMimeTypeDetector $mimeTypeDetector |
|
51 | + */ |
|
52 | + public function __construct(IL10N $l, IRequest $request, IMimeTypeDetector $mimeTypeDetector) { |
|
53 | + parent::__construct($l); |
|
54 | + $this->request = $request; |
|
55 | + $this->mimeTypeDetector = $mimeTypeDetector; |
|
56 | + } |
|
57 | + |
|
58 | + /** |
|
59 | + * @param IStorage $storage |
|
60 | + * @param string $path |
|
61 | + */ |
|
62 | + public function setFileInfo(IStorage $storage, $path) { |
|
63 | + $this->storage = $storage; |
|
64 | + $this->path = $path; |
|
65 | + if (!isset($this->mimeType[$this->storage->getId()][$this->path]) |
|
66 | + || $this->mimeType[$this->storage->getId()][$this->path] === '') { |
|
67 | + $this->mimeType[$this->storage->getId()][$this->path] = null; |
|
68 | + } |
|
69 | + } |
|
70 | + |
|
71 | + /** |
|
72 | + * @return string |
|
73 | + */ |
|
74 | + protected function getActualValue() { |
|
75 | + if ($this->mimeType[$this->storage->getId()][$this->path] !== null) { |
|
76 | + return $this->mimeType[$this->storage->getId()][$this->path]; |
|
77 | + } |
|
78 | + |
|
79 | + if ($this->isWebDAVRequest()) { |
|
80 | + // Creating a folder |
|
81 | + if ($this->request->getMethod() === 'MKCOL') { |
|
82 | + $this->mimeType[$this->storage->getId()][$this->path] = 'httpd/unix-directory'; |
|
83 | + return $this->mimeType[$this->storage->getId()][$this->path]; |
|
84 | + } |
|
85 | + |
|
86 | + if ($this->request->getMethod() === 'PUT') { |
|
87 | + $path = $this->request->getPathInfo(); |
|
88 | + $this->mimeType[$this->storage->getId()][$this->path] = $this->mimeTypeDetector->detectPath($path); |
|
89 | + return $this->mimeType[$this->storage->getId()][$this->path]; |
|
90 | + } |
|
91 | + } else if ($this->isPublicWebDAVRequest()) { |
|
92 | + if ($this->request->getMethod() === 'PUT') { |
|
93 | + $path = $this->request->getPathInfo(); |
|
94 | + if (strpos($path, '/webdav/') === 0) { |
|
95 | + $path = substr($path, strlen('/webdav')); |
|
96 | + } |
|
97 | + $path = $this->path . $path; |
|
98 | + $this->mimeType[$this->storage->getId()][$path] = $this->mimeTypeDetector->detectPath($path); |
|
99 | + return $this->mimeType[$this->storage->getId()][$path]; |
|
100 | + } |
|
101 | + } |
|
102 | + |
|
103 | + if (in_array($this->request->getMethod(), ['POST', 'PUT'])) { |
|
104 | + $files = $this->request->getUploadedFile('files'); |
|
105 | + if (isset($files['type'][0])) { |
|
106 | + $mimeType = $files['type'][0]; |
|
107 | + if ($this->mimeType === 'application/octet-stream') { |
|
108 | + // Maybe not... |
|
109 | + $mimeTypeTest = $this->mimeTypeDetector->detectPath($files['name'][0]); |
|
110 | + if ($mimeTypeTest !== 'application/octet-stream' && $mimeTypeTest !== false) { |
|
111 | + $mimeType = $mimeTypeTest; |
|
112 | + } else { |
|
113 | + $mimeTypeTest = $this->mimeTypeDetector->detect($files['tmp_name'][0]); |
|
114 | + if ($mimeTypeTest !== 'application/octet-stream' && $mimeTypeTest !== false) { |
|
115 | + $mimeType = $mimeTypeTest; |
|
116 | + } |
|
117 | + } |
|
118 | + } |
|
119 | + $this->mimeType[$this->storage->getId()][$this->path] = $mimeType; |
|
120 | + return $mimeType; |
|
121 | + } |
|
122 | + } |
|
123 | + |
|
124 | + $this->mimeType[$this->storage->getId()][$this->path] = $this->storage->getMimeType($this->path); |
|
125 | + if ($this->mimeType[$this->storage->getId()][$this->path] === 'application/octet-stream') { |
|
126 | + $this->mimeType[$this->storage->getId()][$this->path] = $this->detectMimetypeFromPath(); |
|
127 | + } |
|
128 | + |
|
129 | + return $this->mimeType[$this->storage->getId()][$this->path]; |
|
130 | + } |
|
131 | + |
|
132 | + /** |
|
133 | + * @return string |
|
134 | + */ |
|
135 | + protected function detectMimetypeFromPath() { |
|
136 | + $mimeType = $this->mimeTypeDetector->detectPath($this->path); |
|
137 | + if ($mimeType !== 'application/octet-stream' && $mimeType !== false) { |
|
138 | + return $mimeType; |
|
139 | + } |
|
140 | + |
|
141 | + if ($this->storage->instanceOfStorage('\OC\Files\Storage\Local') |
|
142 | + || $this->storage->instanceOfStorage('\OC\Files\Storage\Home') |
|
143 | + || $this->storage->instanceOfStorage('\OC\Files\ObjectStore\HomeObjectStoreStorage')) { |
|
144 | + $localFile = $this->storage->getLocalFile($this->path); |
|
145 | + if ($localFile !== false) { |
|
146 | + $mimeType = $this->mimeTypeDetector->detect($localFile); |
|
147 | + if ($mimeType !== false) { |
|
148 | + return $mimeType; |
|
149 | + } |
|
150 | + } |
|
151 | + |
|
152 | + return 'application/octet-stream'; |
|
153 | + } else { |
|
154 | + $handle = $this->storage->fopen($this->path, 'r'); |
|
155 | + $data = fread($handle, 8024); |
|
156 | + fclose($handle); |
|
157 | + $mimeType = $this->mimeTypeDetector->detectString($data); |
|
158 | + if ($mimeType !== false) { |
|
159 | + return $mimeType; |
|
160 | + } |
|
161 | + |
|
162 | + return 'application/octet-stream'; |
|
163 | + } |
|
164 | + } |
|
165 | + |
|
166 | + /** |
|
167 | + * @return bool |
|
168 | + */ |
|
169 | + protected function isWebDAVRequest() { |
|
170 | + return substr($this->request->getScriptName(), 0 - strlen('/remote.php')) === '/remote.php' && ( |
|
171 | + $this->request->getPathInfo() === '/webdav' || |
|
172 | + strpos($this->request->getPathInfo(), '/webdav/') === 0 || |
|
173 | + $this->request->getPathInfo() === '/dav/files' || |
|
174 | + strpos($this->request->getPathInfo(), '/dav/files/') === 0 |
|
175 | + ); |
|
176 | + } |
|
177 | + |
|
178 | + /** |
|
179 | + * @return bool |
|
180 | + */ |
|
181 | + protected function isPublicWebDAVRequest() { |
|
182 | + return substr($this->request->getScriptName(), 0 - strlen('/public.php')) === '/public.php' && ( |
|
183 | + $this->request->getPathInfo() === '/webdav' || |
|
184 | + strpos($this->request->getPathInfo(), '/webdav/') === 0 |
|
185 | + ); |
|
186 | + } |
|
187 | 187 | } |