@@ -36,117 +36,117 @@ |
||
36 | 36 | use OCP\ILogger; |
37 | 37 | |
38 | 38 | class SetupController { |
39 | - /** @var Setup */ |
|
40 | - protected $setupHelper; |
|
41 | - /** @var string */ |
|
42 | - private $autoConfigFile; |
|
43 | - |
|
44 | - /** |
|
45 | - * @param Setup $setupHelper |
|
46 | - */ |
|
47 | - function __construct(Setup $setupHelper) { |
|
48 | - $this->autoConfigFile = \OC::$configDir.'autoconfig.php'; |
|
49 | - $this->setupHelper = $setupHelper; |
|
50 | - } |
|
51 | - |
|
52 | - /** |
|
53 | - * @param $post |
|
54 | - */ |
|
55 | - public function run($post) { |
|
56 | - // Check for autosetup: |
|
57 | - $post = $this->loadAutoConfig($post); |
|
58 | - $opts = $this->setupHelper->getSystemInfo(); |
|
59 | - |
|
60 | - // convert 'abcpassword' to 'abcpass' |
|
61 | - if (isset($post['adminpassword'])) { |
|
62 | - $post['adminpass'] = $post['adminpassword']; |
|
63 | - } |
|
64 | - if (isset($post['dbpassword'])) { |
|
65 | - $post['dbpass'] = $post['dbpassword']; |
|
66 | - } |
|
67 | - |
|
68 | - if (!is_file(\OC::$configDir.'/CAN_INSTALL')) { |
|
69 | - $this->displaySetupForbidden(); |
|
70 | - return; |
|
71 | - } |
|
72 | - |
|
73 | - if(isset($post['install']) and $post['install']=='true') { |
|
74 | - // We have to launch the installation process : |
|
75 | - $e = $this->setupHelper->install($post); |
|
76 | - $errors = ['errors' => $e]; |
|
77 | - |
|
78 | - if(count($e) > 0) { |
|
79 | - $options = array_merge($opts, $post, $errors); |
|
80 | - $this->display($options); |
|
81 | - } else { |
|
82 | - $this->finishSetup(isset($post['install-recommended-apps'])); |
|
83 | - } |
|
84 | - } else { |
|
85 | - $options = array_merge($opts, $post); |
|
86 | - $this->display($options); |
|
87 | - } |
|
88 | - } |
|
89 | - |
|
90 | - private function displaySetupForbidden() { |
|
91 | - \OC_Template::printGuestPage('', 'installation_forbidden'); |
|
92 | - } |
|
93 | - |
|
94 | - public function display($post) { |
|
95 | - $defaults = [ |
|
96 | - 'adminlogin' => '', |
|
97 | - 'adminpass' => '', |
|
98 | - 'dbuser' => '', |
|
99 | - 'dbpass' => '', |
|
100 | - 'dbname' => '', |
|
101 | - 'dbtablespace' => '', |
|
102 | - 'dbhost' => 'localhost', |
|
103 | - 'dbtype' => '', |
|
104 | - ]; |
|
105 | - $parameters = array_merge($defaults, $post); |
|
106 | - |
|
107 | - \OC_Util::addScript('setup'); |
|
108 | - \OC_Template::printGuestPage('', 'installation', $parameters); |
|
109 | - } |
|
110 | - |
|
111 | - private function finishSetup(bool $installRecommended) { |
|
112 | - if( file_exists( $this->autoConfigFile )) { |
|
113 | - unlink($this->autoConfigFile); |
|
114 | - } |
|
115 | - \OC::$server->getIntegrityCodeChecker()->runInstanceVerification(); |
|
116 | - |
|
117 | - if (\OC_Util::getChannel() !== 'git' && is_file(\OC::$configDir.'/CAN_INSTALL')) { |
|
118 | - if (!unlink(\OC::$configDir.'/CAN_INSTALL')) { |
|
119 | - \OC_Template::printGuestPage('', 'installation_incomplete'); |
|
120 | - } |
|
121 | - } |
|
122 | - |
|
123 | - if ($installRecommended) { |
|
124 | - $urlGenerator = \OC::$server->getURLGenerator(); |
|
125 | - $location = $urlGenerator->getAbsoluteURL('index.php/core/apps/recommended'); |
|
126 | - header('Location: ' . $location); |
|
127 | - exit(); |
|
128 | - } |
|
129 | - \OC_Util::redirectToDefaultPage(); |
|
130 | - } |
|
131 | - |
|
132 | - public function loadAutoConfig($post) { |
|
133 | - if( file_exists($this->autoConfigFile)) { |
|
134 | - \OCP\Util::writeLog('core', 'Autoconfig file found, setting up Nextcloud…', ILogger::INFO); |
|
135 | - $AUTOCONFIG = []; |
|
136 | - include $this->autoConfigFile; |
|
137 | - $post = array_merge ($post, $AUTOCONFIG); |
|
138 | - } |
|
139 | - |
|
140 | - $dbIsSet = isset($post['dbtype']); |
|
141 | - $directoryIsSet = isset($post['directory']); |
|
142 | - $adminAccountIsSet = isset($post['adminlogin']); |
|
143 | - |
|
144 | - if ($dbIsSet and $directoryIsSet and $adminAccountIsSet) { |
|
145 | - $post['install'] = 'true'; |
|
146 | - } |
|
147 | - $post['dbIsSet'] = $dbIsSet; |
|
148 | - $post['directoryIsSet'] = $directoryIsSet; |
|
149 | - |
|
150 | - return $post; |
|
151 | - } |
|
39 | + /** @var Setup */ |
|
40 | + protected $setupHelper; |
|
41 | + /** @var string */ |
|
42 | + private $autoConfigFile; |
|
43 | + |
|
44 | + /** |
|
45 | + * @param Setup $setupHelper |
|
46 | + */ |
|
47 | + function __construct(Setup $setupHelper) { |
|
48 | + $this->autoConfigFile = \OC::$configDir.'autoconfig.php'; |
|
49 | + $this->setupHelper = $setupHelper; |
|
50 | + } |
|
51 | + |
|
52 | + /** |
|
53 | + * @param $post |
|
54 | + */ |
|
55 | + public function run($post) { |
|
56 | + // Check for autosetup: |
|
57 | + $post = $this->loadAutoConfig($post); |
|
58 | + $opts = $this->setupHelper->getSystemInfo(); |
|
59 | + |
|
60 | + // convert 'abcpassword' to 'abcpass' |
|
61 | + if (isset($post['adminpassword'])) { |
|
62 | + $post['adminpass'] = $post['adminpassword']; |
|
63 | + } |
|
64 | + if (isset($post['dbpassword'])) { |
|
65 | + $post['dbpass'] = $post['dbpassword']; |
|
66 | + } |
|
67 | + |
|
68 | + if (!is_file(\OC::$configDir.'/CAN_INSTALL')) { |
|
69 | + $this->displaySetupForbidden(); |
|
70 | + return; |
|
71 | + } |
|
72 | + |
|
73 | + if(isset($post['install']) and $post['install']=='true') { |
|
74 | + // We have to launch the installation process : |
|
75 | + $e = $this->setupHelper->install($post); |
|
76 | + $errors = ['errors' => $e]; |
|
77 | + |
|
78 | + if(count($e) > 0) { |
|
79 | + $options = array_merge($opts, $post, $errors); |
|
80 | + $this->display($options); |
|
81 | + } else { |
|
82 | + $this->finishSetup(isset($post['install-recommended-apps'])); |
|
83 | + } |
|
84 | + } else { |
|
85 | + $options = array_merge($opts, $post); |
|
86 | + $this->display($options); |
|
87 | + } |
|
88 | + } |
|
89 | + |
|
90 | + private function displaySetupForbidden() { |
|
91 | + \OC_Template::printGuestPage('', 'installation_forbidden'); |
|
92 | + } |
|
93 | + |
|
94 | + public function display($post) { |
|
95 | + $defaults = [ |
|
96 | + 'adminlogin' => '', |
|
97 | + 'adminpass' => '', |
|
98 | + 'dbuser' => '', |
|
99 | + 'dbpass' => '', |
|
100 | + 'dbname' => '', |
|
101 | + 'dbtablespace' => '', |
|
102 | + 'dbhost' => 'localhost', |
|
103 | + 'dbtype' => '', |
|
104 | + ]; |
|
105 | + $parameters = array_merge($defaults, $post); |
|
106 | + |
|
107 | + \OC_Util::addScript('setup'); |
|
108 | + \OC_Template::printGuestPage('', 'installation', $parameters); |
|
109 | + } |
|
110 | + |
|
111 | + private function finishSetup(bool $installRecommended) { |
|
112 | + if( file_exists( $this->autoConfigFile )) { |
|
113 | + unlink($this->autoConfigFile); |
|
114 | + } |
|
115 | + \OC::$server->getIntegrityCodeChecker()->runInstanceVerification(); |
|
116 | + |
|
117 | + if (\OC_Util::getChannel() !== 'git' && is_file(\OC::$configDir.'/CAN_INSTALL')) { |
|
118 | + if (!unlink(\OC::$configDir.'/CAN_INSTALL')) { |
|
119 | + \OC_Template::printGuestPage('', 'installation_incomplete'); |
|
120 | + } |
|
121 | + } |
|
122 | + |
|
123 | + if ($installRecommended) { |
|
124 | + $urlGenerator = \OC::$server->getURLGenerator(); |
|
125 | + $location = $urlGenerator->getAbsoluteURL('index.php/core/apps/recommended'); |
|
126 | + header('Location: ' . $location); |
|
127 | + exit(); |
|
128 | + } |
|
129 | + \OC_Util::redirectToDefaultPage(); |
|
130 | + } |
|
131 | + |
|
132 | + public function loadAutoConfig($post) { |
|
133 | + if( file_exists($this->autoConfigFile)) { |
|
134 | + \OCP\Util::writeLog('core', 'Autoconfig file found, setting up Nextcloud…', ILogger::INFO); |
|
135 | + $AUTOCONFIG = []; |
|
136 | + include $this->autoConfigFile; |
|
137 | + $post = array_merge ($post, $AUTOCONFIG); |
|
138 | + } |
|
139 | + |
|
140 | + $dbIsSet = isset($post['dbtype']); |
|
141 | + $directoryIsSet = isset($post['directory']); |
|
142 | + $adminAccountIsSet = isset($post['adminlogin']); |
|
143 | + |
|
144 | + if ($dbIsSet and $directoryIsSet and $adminAccountIsSet) { |
|
145 | + $post['install'] = 'true'; |
|
146 | + } |
|
147 | + $post['dbIsSet'] = $dbIsSet; |
|
148 | + $post['directoryIsSet'] = $directoryIsSet; |
|
149 | + |
|
150 | + return $post; |
|
151 | + } |
|
152 | 152 | } |
@@ -70,12 +70,12 @@ discard block |
||
70 | 70 | return; |
71 | 71 | } |
72 | 72 | |
73 | - if(isset($post['install']) and $post['install']=='true') { |
|
73 | + if (isset($post['install']) and $post['install'] == 'true') { |
|
74 | 74 | // We have to launch the installation process : |
75 | 75 | $e = $this->setupHelper->install($post); |
76 | 76 | $errors = ['errors' => $e]; |
77 | 77 | |
78 | - if(count($e) > 0) { |
|
78 | + if (count($e) > 0) { |
|
79 | 79 | $options = array_merge($opts, $post, $errors); |
80 | 80 | $this->display($options); |
81 | 81 | } else { |
@@ -109,7 +109,7 @@ discard block |
||
109 | 109 | } |
110 | 110 | |
111 | 111 | private function finishSetup(bool $installRecommended) { |
112 | - if( file_exists( $this->autoConfigFile )) { |
|
112 | + if (file_exists($this->autoConfigFile)) { |
|
113 | 113 | unlink($this->autoConfigFile); |
114 | 114 | } |
115 | 115 | \OC::$server->getIntegrityCodeChecker()->runInstanceVerification(); |
@@ -123,18 +123,18 @@ discard block |
||
123 | 123 | if ($installRecommended) { |
124 | 124 | $urlGenerator = \OC::$server->getURLGenerator(); |
125 | 125 | $location = $urlGenerator->getAbsoluteURL('index.php/core/apps/recommended'); |
126 | - header('Location: ' . $location); |
|
126 | + header('Location: '.$location); |
|
127 | 127 | exit(); |
128 | 128 | } |
129 | 129 | \OC_Util::redirectToDefaultPage(); |
130 | 130 | } |
131 | 131 | |
132 | 132 | public function loadAutoConfig($post) { |
133 | - if( file_exists($this->autoConfigFile)) { |
|
133 | + if (file_exists($this->autoConfigFile)) { |
|
134 | 134 | \OCP\Util::writeLog('core', 'Autoconfig file found, setting up Nextcloud…', ILogger::INFO); |
135 | 135 | $AUTOCONFIG = []; |
136 | 136 | include $this->autoConfigFile; |
137 | - $post = array_merge ($post, $AUTOCONFIG); |
|
137 | + $post = array_merge($post, $AUTOCONFIG); |
|
138 | 138 | } |
139 | 139 | |
140 | 140 | $dbIsSet = isset($post['dbtype']); |
@@ -1,6 +1,6 @@ discard block |
||
1 | 1 | <?php |
2 | 2 | script('core', [ |
3 | - 'dist/install' |
|
3 | + 'dist/install' |
|
4 | 4 | ]); |
5 | 5 | ?> |
6 | 6 | <input type='hidden' id='hasMySQL' value='<?php p($_['hasMySQL']) ?>'> |
@@ -29,9 +29,9 @@ discard block |
||
29 | 29 | <legend><strong><?php p($l->t('Security warning'));?></strong></legend> |
30 | 30 | <p><?php p($l->t('Your data directory and files are probably accessible from the internet because the .htaccess file does not work.'));?><br> |
31 | 31 | <?php print_unescaped($l->t( |
32 | - 'For information how to properly configure your server, please see the <a href="%s" target="_blank" rel="noreferrer noopener">documentation</a>.', |
|
33 | - [link_to_docs('admin-install')] |
|
34 | - )); ?></p> |
|
32 | + 'For information how to properly configure your server, please see the <a href="%s" target="_blank" rel="noreferrer noopener">documentation</a>.', |
|
33 | + [link_to_docs('admin-install')] |
|
34 | + )); ?></p> |
|
35 | 35 | </fieldset> |
36 | 36 | <?php endif; ?> |
37 | 37 | <fieldset id="adminaccount"> |
@@ -75,7 +75,7 @@ discard block |
||
75 | 75 | <?php if(!$_['dbIsSet'] or count($_['errors']) > 0): ?> |
76 | 76 | <fieldset id='databaseBackend'> |
77 | 77 | <?php if($_['hasMySQL'] or $_['hasPostgreSQL'] or $_['hasOracle']) |
78 | - $hasOtherDB = true; else $hasOtherDB =false; //other than SQLite ?> |
|
78 | + $hasOtherDB = true; else $hasOtherDB =false; //other than SQLite ?> |
|
79 | 79 | <legend><?php p($l->t( 'Configure the database' )); ?></legend> |
80 | 80 | <div id="selectDbType"> |
81 | 81 | <?php foreach($_['databases'] as $type => $label): ?> |
@@ -9,12 +9,12 @@ discard block |
||
9 | 9 | <input type='hidden' id='hasOracle' value='<?php p($_['hasOracle']) ?>'> |
10 | 10 | <form action="index.php" method="post"> |
11 | 11 | <input type="hidden" name="install" value="true"> |
12 | - <?php if(count($_['errors']) > 0): ?> |
|
12 | + <?php if (count($_['errors']) > 0): ?> |
|
13 | 13 | <fieldset class="warning"> |
14 | - <legend><strong><?php p($l->t('Error'));?></strong></legend> |
|
15 | - <?php foreach($_['errors'] as $err): ?> |
|
14 | + <legend><strong><?php p($l->t('Error')); ?></strong></legend> |
|
15 | + <?php foreach ($_['errors'] as $err): ?> |
|
16 | 16 | <p> |
17 | - <?php if(is_array($err)):?> |
|
17 | + <?php if (is_array($err)):?> |
|
18 | 18 | <?php p($err['error']); ?> |
19 | 19 | <span class='hint'><?php p($err['hint']); ?></span> |
20 | 20 | <?php else: ?> |
@@ -24,10 +24,10 @@ discard block |
||
24 | 24 | <?php endforeach; ?> |
25 | 25 | </fieldset> |
26 | 26 | <?php endif; ?> |
27 | - <?php if(!$_['htaccessWorking']): ?> |
|
27 | + <?php if (!$_['htaccessWorking']): ?> |
|
28 | 28 | <fieldset class="warning"> |
29 | - <legend><strong><?php p($l->t('Security warning'));?></strong></legend> |
|
30 | - <p><?php p($l->t('Your data directory and files are probably accessible from the internet because the .htaccess file does not work.'));?><br> |
|
29 | + <legend><strong><?php p($l->t('Security warning')); ?></strong></legend> |
|
30 | + <p><?php p($l->t('Your data directory and files are probably accessible from the internet because the .htaccess file does not work.')); ?><br> |
|
31 | 31 | <?php print_unescaped($l->t( |
32 | 32 | 'For information how to properly configure your server, please see the <a href="%s" target="_blank" rel="noreferrer noopener">documentation</a>.', |
33 | 33 | [link_to_docs('admin-install')] |
@@ -35,35 +35,35 @@ discard block |
||
35 | 35 | </fieldset> |
36 | 36 | <?php endif; ?> |
37 | 37 | <fieldset id="adminaccount"> |
38 | - <legend><?php print_unescaped($l->t( 'Create an <strong>admin account</strong>' )); ?></legend> |
|
38 | + <legend><?php print_unescaped($l->t('Create an <strong>admin account</strong>')); ?></legend> |
|
39 | 39 | <p class="grouptop"> |
40 | 40 | <input type="text" name="adminlogin" id="adminlogin" |
41 | - placeholder="<?php p($l->t( 'Username' )); ?>" |
|
41 | + placeholder="<?php p($l->t('Username')); ?>" |
|
42 | 42 | value="<?php p($_['adminlogin']); ?>" |
43 | 43 | autocomplete="off" autocapitalize="none" autocorrect="off" autofocus required> |
44 | - <label for="adminlogin" class="infield"><?php p($l->t( 'Username' )); ?></label> |
|
44 | + <label for="adminlogin" class="infield"><?php p($l->t('Username')); ?></label> |
|
45 | 45 | </p> |
46 | 46 | <p class="groupbottom"> |
47 | 47 | <input type="password" name="adminpass" data-typetoggle="#show" id="adminpass" |
48 | - placeholder="<?php p($l->t( 'Password' )); ?>" |
|
48 | + placeholder="<?php p($l->t('Password')); ?>" |
|
49 | 49 | value="<?php p($_['adminpass']); ?>" |
50 | 50 | autocomplete="off" autocapitalize="none" autocorrect="off" required> |
51 | - <label for="adminpass" class="infield"><?php p($l->t( 'Password' )); ?></label> |
|
51 | + <label for="adminpass" class="infield"><?php p($l->t('Password')); ?></label> |
|
52 | 52 | <input type="checkbox" id="show" class="hidden-visually" name="show"> |
53 | - <label for="show" class="hidden-visually"><?php p($l->t( 'Show password')); ?></label> |
|
53 | + <label for="show" class="hidden-visually"><?php p($l->t('Show password')); ?></label> |
|
54 | 54 | </p> |
55 | 55 | </fieldset> |
56 | 56 | |
57 | - <?php if(!$_['directoryIsSet'] or !$_['dbIsSet'] or count($_['errors']) > 0): ?> |
|
57 | + <?php if (!$_['directoryIsSet'] or !$_['dbIsSet'] or count($_['errors']) > 0): ?> |
|
58 | 58 | <fieldset id="advancedHeader"> |
59 | - <legend><a id="showAdvanced" tabindex="0" href="#"><?php p($l->t( 'Storage & database' )); ?><img src="<?php print_unescaped(image_path('', 'actions/caret-white.svg')); ?>" /></a></legend> |
|
59 | + <legend><a id="showAdvanced" tabindex="0" href="#"><?php p($l->t('Storage & database')); ?><img src="<?php print_unescaped(image_path('', 'actions/caret-white.svg')); ?>" /></a></legend> |
|
60 | 60 | </fieldset> |
61 | 61 | <?php endif; ?> |
62 | 62 | |
63 | - <?php if(!$_['directoryIsSet'] or count($_['errors']) > 0): ?> |
|
63 | + <?php if (!$_['directoryIsSet'] or count($_['errors']) > 0): ?> |
|
64 | 64 | <fieldset id="datadirField"> |
65 | 65 | <div id="datadirContent"> |
66 | - <label for="directory"><?php p($l->t( 'Data folder' )); ?></label> |
|
66 | + <label for="directory"><?php p($l->t('Data folder')); ?></label> |
|
67 | 67 | <input type="text" name="directory" id="directory" |
68 | 68 | placeholder="<?php p(OC::$SERVERROOT.'/data'); ?>" |
69 | 69 | value="<?php p($_['directory']); ?>" |
@@ -72,19 +72,19 @@ discard block |
||
72 | 72 | </fieldset> |
73 | 73 | <?php endif; ?> |
74 | 74 | |
75 | - <?php if(!$_['dbIsSet'] or count($_['errors']) > 0): ?> |
|
75 | + <?php if (!$_['dbIsSet'] or count($_['errors']) > 0): ?> |
|
76 | 76 | <fieldset id='databaseBackend'> |
77 | - <?php if($_['hasMySQL'] or $_['hasPostgreSQL'] or $_['hasOracle']) |
|
78 | - $hasOtherDB = true; else $hasOtherDB =false; //other than SQLite ?> |
|
79 | - <legend><?php p($l->t( 'Configure the database' )); ?></legend> |
|
77 | + <?php if ($_['hasMySQL'] or $_['hasPostgreSQL'] or $_['hasOracle']) |
|
78 | + $hasOtherDB = true; else $hasOtherDB = false; //other than SQLite ?> |
|
79 | + <legend><?php p($l->t('Configure the database')); ?></legend> |
|
80 | 80 | <div id="selectDbType"> |
81 | - <?php foreach($_['databases'] as $type => $label): ?> |
|
82 | - <?php if(count($_['databases']) === 1): ?> |
|
81 | + <?php foreach ($_['databases'] as $type => $label): ?> |
|
82 | + <?php if (count($_['databases']) === 1): ?> |
|
83 | 83 | <p class="info"> |
84 | - <?php p($l->t( 'Only %s is available.', [$label] )); ?> |
|
85 | - <?php p($l->t( 'Install and activate additional PHP modules to choose other database types.' )); ?><br> |
|
84 | + <?php p($l->t('Only %s is available.', [$label])); ?> |
|
85 | + <?php p($l->t('Install and activate additional PHP modules to choose other database types.')); ?><br> |
|
86 | 86 | <a href="<?php print_unescaped(link_to_docs('admin-source_install')); ?>" target="_blank" rel="noreferrer noopener"> |
87 | - <?php p($l->t( 'For more details check out the documentation.' )); ?> ↗</a> |
|
87 | + <?php p($l->t('For more details check out the documentation.')); ?> ↗</a> |
|
88 | 88 | </p> |
89 | 89 | <input type="hidden" id="dbtype" name="dbtype" value="<?php p($type) ?>"> |
90 | 90 | <?php else: ?> |
@@ -96,64 +96,64 @@ discard block |
||
96 | 96 | </div> |
97 | 97 | </fieldset> |
98 | 98 | |
99 | - <?php if($hasOtherDB): ?> |
|
99 | + <?php if ($hasOtherDB): ?> |
|
100 | 100 | <fieldset id='databaseField'> |
101 | 101 | <div id="use_other_db"> |
102 | 102 | <p class="grouptop"> |
103 | - <label for="dbuser" class="infield"><?php p($l->t( 'Database user' )); ?></label> |
|
103 | + <label for="dbuser" class="infield"><?php p($l->t('Database user')); ?></label> |
|
104 | 104 | <input type="text" name="dbuser" id="dbuser" |
105 | - placeholder="<?php p($l->t( 'Database user' )); ?>" |
|
105 | + placeholder="<?php p($l->t('Database user')); ?>" |
|
106 | 106 | value="<?php p($_['dbuser']); ?>" |
107 | 107 | autocomplete="off" autocapitalize="none" autocorrect="off"> |
108 | 108 | </p> |
109 | 109 | <p class="groupmiddle"> |
110 | 110 | <input type="password" name="dbpass" id="dbpass" data-typetoggle="#dbpassword-toggle" |
111 | - placeholder="<?php p($l->t( 'Database password' )); ?>" |
|
111 | + placeholder="<?php p($l->t('Database password')); ?>" |
|
112 | 112 | value="<?php p($_['dbpass']); ?>" |
113 | 113 | autocomplete="off" autocapitalize="none" autocorrect="off"> |
114 | - <label for="dbpass" class="infield"><?php p($l->t( 'Database password' )); ?></label> |
|
114 | + <label for="dbpass" class="infield"><?php p($l->t('Database password')); ?></label> |
|
115 | 115 | <input type="checkbox" id="dbpassword-toggle" class="hidden-visually" name="dbpassword-toggle"> |
116 | 116 | <label for="dbpassword-toggle"></label> |
117 | 117 | </p> |
118 | 118 | <p class="groupmiddle"> |
119 | - <label for="dbname" class="infield"><?php p($l->t( 'Database name' )); ?></label> |
|
119 | + <label for="dbname" class="infield"><?php p($l->t('Database name')); ?></label> |
|
120 | 120 | <input type="text" name="dbname" id="dbname" |
121 | - placeholder="<?php p($l->t( 'Database name' )); ?>" |
|
121 | + placeholder="<?php p($l->t('Database name')); ?>" |
|
122 | 122 | value="<?php p($_['dbname']); ?>" |
123 | 123 | autocomplete="off" autocapitalize="none" autocorrect="off" |
124 | 124 | pattern="[0-9a-zA-Z$_-]+"> |
125 | 125 | </p> |
126 | - <?php if($_['hasOracle']): ?> |
|
126 | + <?php if ($_['hasOracle']): ?> |
|
127 | 127 | <div id="use_oracle_db"> |
128 | 128 | <p class="groupmiddle"> |
129 | - <label for="dbtablespace" class="infield"><?php p($l->t( 'Database tablespace' )); ?></label> |
|
129 | + <label for="dbtablespace" class="infield"><?php p($l->t('Database tablespace')); ?></label> |
|
130 | 130 | <input type="text" name="dbtablespace" id="dbtablespace" |
131 | - placeholder="<?php p($l->t( 'Database tablespace' )); ?>" |
|
131 | + placeholder="<?php p($l->t('Database tablespace')); ?>" |
|
132 | 132 | value="<?php p($_['dbtablespace']); ?>" |
133 | 133 | autocomplete="off" autocapitalize="none" autocorrect="off"> |
134 | 134 | </p> |
135 | 135 | </div> |
136 | 136 | <?php endif; ?> |
137 | 137 | <p class="groupbottom"> |
138 | - <label for="dbhost" class="infield"><?php p($l->t( 'Database host' )); ?></label> |
|
138 | + <label for="dbhost" class="infield"><?php p($l->t('Database host')); ?></label> |
|
139 | 139 | <input type="text" name="dbhost" id="dbhost" |
140 | - placeholder="<?php p($l->t( 'Database host' )); ?>" |
|
140 | + placeholder="<?php p($l->t('Database host')); ?>" |
|
141 | 141 | value="<?php p($_['dbhost']); ?>" |
142 | 142 | autocomplete="off" autocapitalize="none" autocorrect="off"> |
143 | 143 | </p> |
144 | 144 | <p class="info"> |
145 | - <?php p($l->t( 'Please specify the port number along with the host name (e.g., localhost:5432).' )); ?> |
|
145 | + <?php p($l->t('Please specify the port number along with the host name (e.g., localhost:5432).')); ?> |
|
146 | 146 | </p> |
147 | 147 | </div> |
148 | 148 | </fieldset> |
149 | 149 | <?php endif; ?> |
150 | 150 | <?php endif; ?> |
151 | 151 | |
152 | - <?php if(!$_['dbIsSet'] or count($_['errors']) > 0): ?> |
|
152 | + <?php if (!$_['dbIsSet'] or count($_['errors']) > 0): ?> |
|
153 | 153 | <fieldset id="sqliteInformation" class="warning"> |
154 | - <legend><?php p($l->t('Performance warning'));?></legend> |
|
155 | - <p><?php p($l->t('You chose SQLite as database.'));?></p> |
|
156 | - <p><?php p($l->t('SQLite should only be used for minimal and development instances. For production we recommend a different database backend.'));?></p> |
|
154 | + <legend><?php p($l->t('Performance warning')); ?></legend> |
|
155 | + <p><?php p($l->t('You chose SQLite as database.')); ?></p> |
|
156 | + <p><?php p($l->t('SQLite should only be used for minimal and development instances. For production we recommend a different database backend.')); ?></p> |
|
157 | 157 | <p><?php p($l->t('If you use clients for file syncing, the use of SQLite is highly discouraged.')); ?></p> |
158 | 158 | </fieldset> |
159 | 159 | <?php endif ?> |
@@ -162,19 +162,19 @@ discard block |
||
162 | 162 | <p class="info"> |
163 | 163 | <input type="checkbox" id="install-recommended-apps" name="install-recommended-apps" class="checkbox checkbox--white" checked> |
164 | 164 | <label for="install-recommended-apps"> |
165 | - <?php p($l->t( 'Install recommended apps' )); ?> |
|
166 | - <span><?php p($l->t( 'Calendar, Contacts, Talk, Mail & OnlyOffice' )); ?></span> |
|
165 | + <?php p($l->t('Install recommended apps')); ?> |
|
166 | + <span><?php p($l->t('Calendar, Contacts, Talk, Mail & OnlyOffice')); ?></span> |
|
167 | 167 | </label> |
168 | 168 | </p> |
169 | 169 | </fieldset> |
170 | 170 | |
171 | 171 | <div class="icon-loading-dark float-spinner"> </div> |
172 | 172 | |
173 | - <div class="buttons"><input type="submit" class="primary" value="<?php p($l->t( 'Finish setup' )); ?>" data-finishing="<?php p($l->t( 'Finishing …' )); ?>"></div> |
|
173 | + <div class="buttons"><input type="submit" class="primary" value="<?php p($l->t('Finish setup')); ?>" data-finishing="<?php p($l->t('Finishing …')); ?>"></div> |
|
174 | 174 | |
175 | 175 | <p class="info"> |
176 | 176 | <span class="icon-info-white"></span> |
177 | - <?php p($l->t('Need help?'));?> |
|
178 | - <a target="_blank" rel="noreferrer noopener" href="<?php p(link_to_docs('admin-install')); ?>"><?php p($l->t('See the documentation'));?> ↗</a> |
|
177 | + <?php p($l->t('Need help?')); ?> |
|
178 | + <a target="_blank" rel="noreferrer noopener" href="<?php p(link_to_docs('admin-install')); ?>"><?php p($l->t('See the documentation')); ?> ↗</a> |
|
179 | 179 | </p> |
180 | 180 | </form> |
@@ -17,8 +17,11 @@ discard block |
||
17 | 17 | <?php if(is_array($err)):?> |
18 | 18 | <?php p($err['error']); ?> |
19 | 19 | <span class='hint'><?php p($err['hint']); ?></span> |
20 | - <?php else: ?> |
|
21 | - <?php p($err); ?> |
|
20 | + <?php else { |
|
21 | + : ?> |
|
22 | + <?php p($err); |
|
23 | +} |
|
24 | +?> |
|
22 | 25 | <?php endif; ?> |
23 | 26 | </p> |
24 | 27 | <?php endforeach; ?> |
@@ -74,8 +77,12 @@ discard block |
||
74 | 77 | |
75 | 78 | <?php if(!$_['dbIsSet'] or count($_['errors']) > 0): ?> |
76 | 79 | <fieldset id='databaseBackend'> |
77 | - <?php if($_['hasMySQL'] or $_['hasPostgreSQL'] or $_['hasOracle']) |
|
78 | - $hasOtherDB = true; else $hasOtherDB =false; //other than SQLite ?> |
|
80 | + <?php if($_['hasMySQL'] or $_['hasPostgreSQL'] or $_['hasOracle']) { |
|
81 | + $hasOtherDB = true; |
|
82 | +} else { |
|
83 | + $hasOtherDB =false; |
|
84 | + } |
|
85 | + //other than SQLite ?> |
|
79 | 86 | <legend><?php p($l->t( 'Configure the database' )); ?></legend> |
80 | 87 | <div id="selectDbType"> |
81 | 88 | <?php foreach($_['databases'] as $type => $label): ?> |
@@ -87,11 +94,14 @@ discard block |
||
87 | 94 | <?php p($l->t( 'For more details check out the documentation.' )); ?> ↗</a> |
88 | 95 | </p> |
89 | 96 | <input type="hidden" id="dbtype" name="dbtype" value="<?php p($type) ?>"> |
90 | - <?php else: ?> |
|
97 | + <?php else { |
|
98 | + : ?> |
|
91 | 99 | <input type="radio" name="dbtype" value="<?php p($type) ?>" id="<?php p($type) ?>" |
92 | 100 | <?php print_unescaped($_['dbtype'] === $type ? 'checked="checked" ' : '') ?>/> |
93 | 101 | <label class="<?php p($type) ?>" for="<?php p($type) ?>"><?php p($label) ?></label> |
94 | - <?php endif; ?> |
|
102 | + <?php endif; |
|
103 | +} |
|
104 | +?> |
|
95 | 105 | <?php endforeach; ?> |
96 | 106 | </div> |
97 | 107 | </fieldset> |
@@ -31,293 +31,293 @@ |
||
31 | 31 | * @package OCA\User_LDAP\Mapping |
32 | 32 | */ |
33 | 33 | abstract class AbstractMapping { |
34 | - /** |
|
35 | - * @var \OCP\IDBConnection $dbc |
|
36 | - */ |
|
37 | - protected $dbc; |
|
34 | + /** |
|
35 | + * @var \OCP\IDBConnection $dbc |
|
36 | + */ |
|
37 | + protected $dbc; |
|
38 | 38 | |
39 | - /** |
|
40 | - * returns the DB table name which holds the mappings |
|
41 | - * @return string |
|
42 | - */ |
|
43 | - abstract protected function getTableName(); |
|
39 | + /** |
|
40 | + * returns the DB table name which holds the mappings |
|
41 | + * @return string |
|
42 | + */ |
|
43 | + abstract protected function getTableName(); |
|
44 | 44 | |
45 | - /** |
|
46 | - * @param \OCP\IDBConnection $dbc |
|
47 | - */ |
|
48 | - public function __construct(\OCP\IDBConnection $dbc) { |
|
49 | - $this->dbc = $dbc; |
|
50 | - } |
|
45 | + /** |
|
46 | + * @param \OCP\IDBConnection $dbc |
|
47 | + */ |
|
48 | + public function __construct(\OCP\IDBConnection $dbc) { |
|
49 | + $this->dbc = $dbc; |
|
50 | + } |
|
51 | 51 | |
52 | - /** |
|
53 | - * checks whether a provided string represents an existing table col |
|
54 | - * @param string $col |
|
55 | - * @return bool |
|
56 | - */ |
|
57 | - public function isColNameValid($col) { |
|
58 | - switch($col) { |
|
59 | - case 'ldap_dn': |
|
60 | - case 'owncloud_name': |
|
61 | - case 'directory_uuid': |
|
62 | - return true; |
|
63 | - default: |
|
64 | - return false; |
|
65 | - } |
|
66 | - } |
|
52 | + /** |
|
53 | + * checks whether a provided string represents an existing table col |
|
54 | + * @param string $col |
|
55 | + * @return bool |
|
56 | + */ |
|
57 | + public function isColNameValid($col) { |
|
58 | + switch($col) { |
|
59 | + case 'ldap_dn': |
|
60 | + case 'owncloud_name': |
|
61 | + case 'directory_uuid': |
|
62 | + return true; |
|
63 | + default: |
|
64 | + return false; |
|
65 | + } |
|
66 | + } |
|
67 | 67 | |
68 | - /** |
|
69 | - * Gets the value of one column based on a provided value of another column |
|
70 | - * @param string $fetchCol |
|
71 | - * @param string $compareCol |
|
72 | - * @param string $search |
|
73 | - * @throws \Exception |
|
74 | - * @return string|false |
|
75 | - */ |
|
76 | - protected function getXbyY($fetchCol, $compareCol, $search) { |
|
77 | - if(!$this->isColNameValid($fetchCol)) { |
|
78 | - //this is used internally only, but we don't want to risk |
|
79 | - //having SQL injection at all. |
|
80 | - throw new \Exception('Invalid Column Name'); |
|
81 | - } |
|
82 | - $query = $this->dbc->prepare(' |
|
68 | + /** |
|
69 | + * Gets the value of one column based on a provided value of another column |
|
70 | + * @param string $fetchCol |
|
71 | + * @param string $compareCol |
|
72 | + * @param string $search |
|
73 | + * @throws \Exception |
|
74 | + * @return string|false |
|
75 | + */ |
|
76 | + protected function getXbyY($fetchCol, $compareCol, $search) { |
|
77 | + if(!$this->isColNameValid($fetchCol)) { |
|
78 | + //this is used internally only, but we don't want to risk |
|
79 | + //having SQL injection at all. |
|
80 | + throw new \Exception('Invalid Column Name'); |
|
81 | + } |
|
82 | + $query = $this->dbc->prepare(' |
|
83 | 83 | SELECT `' . $fetchCol . '` |
84 | 84 | FROM `'. $this->getTableName() .'` |
85 | 85 | WHERE `' . $compareCol . '` = ? |
86 | 86 | '); |
87 | 87 | |
88 | - $res = $query->execute([$search]); |
|
89 | - if($res !== false) { |
|
90 | - return $query->fetchColumn(); |
|
91 | - } |
|
88 | + $res = $query->execute([$search]); |
|
89 | + if($res !== false) { |
|
90 | + return $query->fetchColumn(); |
|
91 | + } |
|
92 | 92 | |
93 | - return false; |
|
94 | - } |
|
93 | + return false; |
|
94 | + } |
|
95 | 95 | |
96 | - /** |
|
97 | - * Performs a DELETE or UPDATE query to the database. |
|
98 | - * @param \Doctrine\DBAL\Driver\Statement $query |
|
99 | - * @param array $parameters |
|
100 | - * @return bool true if at least one row was modified, false otherwise |
|
101 | - */ |
|
102 | - protected function modify($query, $parameters) { |
|
103 | - $result = $query->execute($parameters); |
|
104 | - return ($result === true && $query->rowCount() > 0); |
|
105 | - } |
|
96 | + /** |
|
97 | + * Performs a DELETE or UPDATE query to the database. |
|
98 | + * @param \Doctrine\DBAL\Driver\Statement $query |
|
99 | + * @param array $parameters |
|
100 | + * @return bool true if at least one row was modified, false otherwise |
|
101 | + */ |
|
102 | + protected function modify($query, $parameters) { |
|
103 | + $result = $query->execute($parameters); |
|
104 | + return ($result === true && $query->rowCount() > 0); |
|
105 | + } |
|
106 | 106 | |
107 | - /** |
|
108 | - * Gets the LDAP DN based on the provided name. |
|
109 | - * Replaces Access::ocname2dn |
|
110 | - * @param string $name |
|
111 | - * @return string|false |
|
112 | - */ |
|
113 | - public function getDNByName($name) { |
|
114 | - return $this->getXbyY('ldap_dn', 'owncloud_name', $name); |
|
115 | - } |
|
107 | + /** |
|
108 | + * Gets the LDAP DN based on the provided name. |
|
109 | + * Replaces Access::ocname2dn |
|
110 | + * @param string $name |
|
111 | + * @return string|false |
|
112 | + */ |
|
113 | + public function getDNByName($name) { |
|
114 | + return $this->getXbyY('ldap_dn', 'owncloud_name', $name); |
|
115 | + } |
|
116 | 116 | |
117 | - /** |
|
118 | - * Updates the DN based on the given UUID |
|
119 | - * @param string $fdn |
|
120 | - * @param string $uuid |
|
121 | - * @return bool |
|
122 | - */ |
|
123 | - public function setDNbyUUID($fdn, $uuid) { |
|
124 | - $query = $this->dbc->prepare(' |
|
117 | + /** |
|
118 | + * Updates the DN based on the given UUID |
|
119 | + * @param string $fdn |
|
120 | + * @param string $uuid |
|
121 | + * @return bool |
|
122 | + */ |
|
123 | + public function setDNbyUUID($fdn, $uuid) { |
|
124 | + $query = $this->dbc->prepare(' |
|
125 | 125 | UPDATE `' . $this->getTableName() . '` |
126 | 126 | SET `ldap_dn` = ? |
127 | 127 | WHERE `directory_uuid` = ? |
128 | 128 | '); |
129 | 129 | |
130 | - return $this->modify($query, [$fdn, $uuid]); |
|
131 | - } |
|
130 | + return $this->modify($query, [$fdn, $uuid]); |
|
131 | + } |
|
132 | 132 | |
133 | - /** |
|
134 | - * Updates the UUID based on the given DN |
|
135 | - * |
|
136 | - * required by Migration/UUIDFix |
|
137 | - * |
|
138 | - * @param $uuid |
|
139 | - * @param $fdn |
|
140 | - * @return bool |
|
141 | - */ |
|
142 | - public function setUUIDbyDN($uuid, $fdn) { |
|
143 | - $query = $this->dbc->prepare(' |
|
133 | + /** |
|
134 | + * Updates the UUID based on the given DN |
|
135 | + * |
|
136 | + * required by Migration/UUIDFix |
|
137 | + * |
|
138 | + * @param $uuid |
|
139 | + * @param $fdn |
|
140 | + * @return bool |
|
141 | + */ |
|
142 | + public function setUUIDbyDN($uuid, $fdn) { |
|
143 | + $query = $this->dbc->prepare(' |
|
144 | 144 | UPDATE `' . $this->getTableName() . '` |
145 | 145 | SET `directory_uuid` = ? |
146 | 146 | WHERE `ldap_dn` = ? |
147 | 147 | '); |
148 | 148 | |
149 | - return $this->modify($query, [$uuid, $fdn]); |
|
150 | - } |
|
149 | + return $this->modify($query, [$uuid, $fdn]); |
|
150 | + } |
|
151 | 151 | |
152 | - /** |
|
153 | - * Gets the name based on the provided LDAP DN. |
|
154 | - * @param string $fdn |
|
155 | - * @return string|false |
|
156 | - */ |
|
157 | - public function getNameByDN($fdn) { |
|
158 | - return $this->getXbyY('owncloud_name', 'ldap_dn', $fdn); |
|
159 | - } |
|
152 | + /** |
|
153 | + * Gets the name based on the provided LDAP DN. |
|
154 | + * @param string $fdn |
|
155 | + * @return string|false |
|
156 | + */ |
|
157 | + public function getNameByDN($fdn) { |
|
158 | + return $this->getXbyY('owncloud_name', 'ldap_dn', $fdn); |
|
159 | + } |
|
160 | 160 | |
161 | - /** |
|
162 | - * Searches mapped names by the giving string in the name column |
|
163 | - * @param string $search |
|
164 | - * @param string $prefixMatch |
|
165 | - * @param string $postfixMatch |
|
166 | - * @return string[] |
|
167 | - */ |
|
168 | - public function getNamesBySearch($search, $prefixMatch = "", $postfixMatch = "") { |
|
169 | - $query = $this->dbc->prepare(' |
|
161 | + /** |
|
162 | + * Searches mapped names by the giving string in the name column |
|
163 | + * @param string $search |
|
164 | + * @param string $prefixMatch |
|
165 | + * @param string $postfixMatch |
|
166 | + * @return string[] |
|
167 | + */ |
|
168 | + public function getNamesBySearch($search, $prefixMatch = "", $postfixMatch = "") { |
|
169 | + $query = $this->dbc->prepare(' |
|
170 | 170 | SELECT `owncloud_name` |
171 | 171 | FROM `'. $this->getTableName() .'` |
172 | 172 | WHERE `owncloud_name` LIKE ? |
173 | 173 | '); |
174 | 174 | |
175 | - $res = $query->execute([$prefixMatch.$this->dbc->escapeLikeParameter($search).$postfixMatch]); |
|
176 | - $names = []; |
|
177 | - if($res !== false) { |
|
178 | - while($row = $query->fetch()) { |
|
179 | - $names[] = $row['owncloud_name']; |
|
180 | - } |
|
181 | - } |
|
182 | - return $names; |
|
183 | - } |
|
175 | + $res = $query->execute([$prefixMatch.$this->dbc->escapeLikeParameter($search).$postfixMatch]); |
|
176 | + $names = []; |
|
177 | + if($res !== false) { |
|
178 | + while($row = $query->fetch()) { |
|
179 | + $names[] = $row['owncloud_name']; |
|
180 | + } |
|
181 | + } |
|
182 | + return $names; |
|
183 | + } |
|
184 | 184 | |
185 | - /** |
|
186 | - * Gets the name based on the provided LDAP UUID. |
|
187 | - * @param string $uuid |
|
188 | - * @return string|false |
|
189 | - */ |
|
190 | - public function getNameByUUID($uuid) { |
|
191 | - return $this->getXbyY('owncloud_name', 'directory_uuid', $uuid); |
|
192 | - } |
|
185 | + /** |
|
186 | + * Gets the name based on the provided LDAP UUID. |
|
187 | + * @param string $uuid |
|
188 | + * @return string|false |
|
189 | + */ |
|
190 | + public function getNameByUUID($uuid) { |
|
191 | + return $this->getXbyY('owncloud_name', 'directory_uuid', $uuid); |
|
192 | + } |
|
193 | 193 | |
194 | - /** |
|
195 | - * Gets the UUID based on the provided LDAP DN |
|
196 | - * @param string $dn |
|
197 | - * @return false|string |
|
198 | - * @throws \Exception |
|
199 | - */ |
|
200 | - public function getUUIDByDN($dn) { |
|
201 | - return $this->getXbyY('directory_uuid', 'ldap_dn', $dn); |
|
202 | - } |
|
194 | + /** |
|
195 | + * Gets the UUID based on the provided LDAP DN |
|
196 | + * @param string $dn |
|
197 | + * @return false|string |
|
198 | + * @throws \Exception |
|
199 | + */ |
|
200 | + public function getUUIDByDN($dn) { |
|
201 | + return $this->getXbyY('directory_uuid', 'ldap_dn', $dn); |
|
202 | + } |
|
203 | 203 | |
204 | - /** |
|
205 | - * gets a piece of the mapping list |
|
206 | - * @param int $offset |
|
207 | - * @param int $limit |
|
208 | - * @return array |
|
209 | - */ |
|
210 | - public function getList($offset = null, $limit = null) { |
|
211 | - $query = $this->dbc->prepare(' |
|
204 | + /** |
|
205 | + * gets a piece of the mapping list |
|
206 | + * @param int $offset |
|
207 | + * @param int $limit |
|
208 | + * @return array |
|
209 | + */ |
|
210 | + public function getList($offset = null, $limit = null) { |
|
211 | + $query = $this->dbc->prepare(' |
|
212 | 212 | SELECT |
213 | 213 | `ldap_dn` AS `dn`, |
214 | 214 | `owncloud_name` AS `name`, |
215 | 215 | `directory_uuid` AS `uuid` |
216 | 216 | FROM `' . $this->getTableName() . '`', |
217 | - $limit, |
|
218 | - $offset |
|
219 | - ); |
|
217 | + $limit, |
|
218 | + $offset |
|
219 | + ); |
|
220 | 220 | |
221 | - $query->execute(); |
|
222 | - return $query->fetchAll(); |
|
223 | - } |
|
221 | + $query->execute(); |
|
222 | + return $query->fetchAll(); |
|
223 | + } |
|
224 | 224 | |
225 | - /** |
|
226 | - * attempts to map the given entry |
|
227 | - * @param string $fdn fully distinguished name (from LDAP) |
|
228 | - * @param string $name |
|
229 | - * @param string $uuid a unique identifier as used in LDAP |
|
230 | - * @return bool |
|
231 | - */ |
|
232 | - public function map($fdn, $name, $uuid) { |
|
233 | - if(mb_strlen($fdn) > 255) { |
|
234 | - \OC::$server->getLogger()->error( |
|
235 | - 'Cannot map, because the DN exceeds 255 characters: {dn}', |
|
236 | - [ |
|
237 | - 'app' => 'user_ldap', |
|
238 | - 'dn' => $fdn, |
|
239 | - ] |
|
240 | - ); |
|
241 | - return false; |
|
242 | - } |
|
225 | + /** |
|
226 | + * attempts to map the given entry |
|
227 | + * @param string $fdn fully distinguished name (from LDAP) |
|
228 | + * @param string $name |
|
229 | + * @param string $uuid a unique identifier as used in LDAP |
|
230 | + * @return bool |
|
231 | + */ |
|
232 | + public function map($fdn, $name, $uuid) { |
|
233 | + if(mb_strlen($fdn) > 255) { |
|
234 | + \OC::$server->getLogger()->error( |
|
235 | + 'Cannot map, because the DN exceeds 255 characters: {dn}', |
|
236 | + [ |
|
237 | + 'app' => 'user_ldap', |
|
238 | + 'dn' => $fdn, |
|
239 | + ] |
|
240 | + ); |
|
241 | + return false; |
|
242 | + } |
|
243 | 243 | |
244 | - $row = [ |
|
245 | - 'ldap_dn' => $fdn, |
|
246 | - 'owncloud_name' => $name, |
|
247 | - 'directory_uuid' => $uuid |
|
248 | - ]; |
|
244 | + $row = [ |
|
245 | + 'ldap_dn' => $fdn, |
|
246 | + 'owncloud_name' => $name, |
|
247 | + 'directory_uuid' => $uuid |
|
248 | + ]; |
|
249 | 249 | |
250 | - try { |
|
251 | - $result = $this->dbc->insertIfNotExist($this->getTableName(), $row); |
|
252 | - // insertIfNotExist returns values as int |
|
253 | - return (bool)$result; |
|
254 | - } catch (\Exception $e) { |
|
255 | - return false; |
|
256 | - } |
|
257 | - } |
|
250 | + try { |
|
251 | + $result = $this->dbc->insertIfNotExist($this->getTableName(), $row); |
|
252 | + // insertIfNotExist returns values as int |
|
253 | + return (bool)$result; |
|
254 | + } catch (\Exception $e) { |
|
255 | + return false; |
|
256 | + } |
|
257 | + } |
|
258 | 258 | |
259 | - /** |
|
260 | - * removes a mapping based on the owncloud_name of the entry |
|
261 | - * @param string $name |
|
262 | - * @return bool |
|
263 | - */ |
|
264 | - public function unmap($name) { |
|
265 | - $query = $this->dbc->prepare(' |
|
259 | + /** |
|
260 | + * removes a mapping based on the owncloud_name of the entry |
|
261 | + * @param string $name |
|
262 | + * @return bool |
|
263 | + */ |
|
264 | + public function unmap($name) { |
|
265 | + $query = $this->dbc->prepare(' |
|
266 | 266 | DELETE FROM `'. $this->getTableName() .'` |
267 | 267 | WHERE `owncloud_name` = ?'); |
268 | 268 | |
269 | - return $this->modify($query, [$name]); |
|
270 | - } |
|
269 | + return $this->modify($query, [$name]); |
|
270 | + } |
|
271 | 271 | |
272 | - /** |
|
273 | - * Truncate's the mapping table |
|
274 | - * @return bool |
|
275 | - */ |
|
276 | - public function clear() { |
|
277 | - $sql = $this->dbc |
|
278 | - ->getDatabasePlatform() |
|
279 | - ->getTruncateTableSQL('`' . $this->getTableName() . '`'); |
|
280 | - return $this->dbc->prepare($sql)->execute(); |
|
281 | - } |
|
272 | + /** |
|
273 | + * Truncate's the mapping table |
|
274 | + * @return bool |
|
275 | + */ |
|
276 | + public function clear() { |
|
277 | + $sql = $this->dbc |
|
278 | + ->getDatabasePlatform() |
|
279 | + ->getTruncateTableSQL('`' . $this->getTableName() . '`'); |
|
280 | + return $this->dbc->prepare($sql)->execute(); |
|
281 | + } |
|
282 | 282 | |
283 | - /** |
|
284 | - * clears the mapping table one by one and executing a callback with |
|
285 | - * each row's id (=owncloud_name col) |
|
286 | - * |
|
287 | - * @param callable $preCallback |
|
288 | - * @param callable $postCallback |
|
289 | - * @return bool true on success, false when at least one row was not |
|
290 | - * deleted |
|
291 | - */ |
|
292 | - public function clearCb(callable $preCallback, callable $postCallback): bool { |
|
293 | - $picker = $this->dbc->getQueryBuilder(); |
|
294 | - $picker->select('owncloud_name') |
|
295 | - ->from($this->getTableName()); |
|
296 | - $cursor = $picker->execute(); |
|
297 | - $result = true; |
|
298 | - while($id = $cursor->fetchColumn(0)) { |
|
299 | - $preCallback($id); |
|
300 | - if($isUnmapped = $this->unmap($id)) { |
|
301 | - $postCallback($id); |
|
302 | - } |
|
303 | - $result &= $isUnmapped; |
|
304 | - } |
|
305 | - $cursor->closeCursor(); |
|
306 | - return $result; |
|
307 | - } |
|
283 | + /** |
|
284 | + * clears the mapping table one by one and executing a callback with |
|
285 | + * each row's id (=owncloud_name col) |
|
286 | + * |
|
287 | + * @param callable $preCallback |
|
288 | + * @param callable $postCallback |
|
289 | + * @return bool true on success, false when at least one row was not |
|
290 | + * deleted |
|
291 | + */ |
|
292 | + public function clearCb(callable $preCallback, callable $postCallback): bool { |
|
293 | + $picker = $this->dbc->getQueryBuilder(); |
|
294 | + $picker->select('owncloud_name') |
|
295 | + ->from($this->getTableName()); |
|
296 | + $cursor = $picker->execute(); |
|
297 | + $result = true; |
|
298 | + while($id = $cursor->fetchColumn(0)) { |
|
299 | + $preCallback($id); |
|
300 | + if($isUnmapped = $this->unmap($id)) { |
|
301 | + $postCallback($id); |
|
302 | + } |
|
303 | + $result &= $isUnmapped; |
|
304 | + } |
|
305 | + $cursor->closeCursor(); |
|
306 | + return $result; |
|
307 | + } |
|
308 | 308 | |
309 | - /** |
|
310 | - * returns the number of entries in the mappings table |
|
311 | - * |
|
312 | - * @return int |
|
313 | - */ |
|
314 | - public function count() { |
|
315 | - $qb = $this->dbc->getQueryBuilder(); |
|
316 | - $query = $qb->select($qb->func()->count('ldap_dn')) |
|
317 | - ->from($this->getTableName()); |
|
318 | - $res = $query->execute(); |
|
319 | - $count = $res->fetchColumn(); |
|
320 | - $res->closeCursor(); |
|
321 | - return (int)$count; |
|
322 | - } |
|
309 | + /** |
|
310 | + * returns the number of entries in the mappings table |
|
311 | + * |
|
312 | + * @return int |
|
313 | + */ |
|
314 | + public function count() { |
|
315 | + $qb = $this->dbc->getQueryBuilder(); |
|
316 | + $query = $qb->select($qb->func()->count('ldap_dn')) |
|
317 | + ->from($this->getTableName()); |
|
318 | + $res = $query->execute(); |
|
319 | + $count = $res->fetchColumn(); |
|
320 | + $res->closeCursor(); |
|
321 | + return (int)$count; |
|
322 | + } |
|
323 | 323 | } |
@@ -44,185 +44,185 @@ |
||
44 | 44 | * User storages controller |
45 | 45 | */ |
46 | 46 | class UserStoragesController extends StoragesController { |
47 | - /** |
|
48 | - * @var IUserSession |
|
49 | - */ |
|
50 | - private $userSession; |
|
51 | - |
|
52 | - /** |
|
53 | - * Creates a new user storages controller. |
|
54 | - * |
|
55 | - * @param string $AppName application name |
|
56 | - * @param IRequest $request request object |
|
57 | - * @param IL10N $l10n l10n service |
|
58 | - * @param UserStoragesService $userStoragesService storage service |
|
59 | - * @param IUserSession $userSession |
|
60 | - * @param ILogger $logger |
|
61 | - */ |
|
62 | - public function __construct( |
|
63 | - $AppName, |
|
64 | - IRequest $request, |
|
65 | - IL10N $l10n, |
|
66 | - UserStoragesService $userStoragesService, |
|
67 | - IUserSession $userSession, |
|
68 | - ILogger $logger |
|
69 | - ) { |
|
70 | - parent::__construct( |
|
71 | - $AppName, |
|
72 | - $request, |
|
73 | - $l10n, |
|
74 | - $userStoragesService, |
|
75 | - $logger |
|
76 | - ); |
|
77 | - $this->userSession = $userSession; |
|
78 | - } |
|
79 | - |
|
80 | - protected function manipulateStorageConfig(StorageConfig $storage) { |
|
81 | - /** @var AuthMechanism */ |
|
82 | - $authMechanism = $storage->getAuthMechanism(); |
|
83 | - $authMechanism->manipulateStorageConfig($storage, $this->userSession->getUser()); |
|
84 | - /** @var Backend */ |
|
85 | - $backend = $storage->getBackend(); |
|
86 | - $backend->manipulateStorageConfig($storage, $this->userSession->getUser()); |
|
87 | - } |
|
88 | - |
|
89 | - /** |
|
90 | - * Get all storage entries |
|
91 | - * |
|
92 | - * @NoAdminRequired |
|
93 | - * |
|
94 | - * @return DataResponse |
|
95 | - */ |
|
96 | - public function index() { |
|
97 | - return parent::index(); |
|
98 | - } |
|
99 | - |
|
100 | - /** |
|
101 | - * Return storage |
|
102 | - * |
|
103 | - * @NoAdminRequired |
|
104 | - * |
|
105 | - * {@inheritdoc} |
|
106 | - */ |
|
107 | - public function show($id, $testOnly = true) { |
|
108 | - return parent::show($id, $testOnly); |
|
109 | - } |
|
110 | - |
|
111 | - /** |
|
112 | - * Create an external storage entry. |
|
113 | - * |
|
114 | - * @param string $mountPoint storage mount point |
|
115 | - * @param string $backend backend identifier |
|
116 | - * @param string $authMechanism authentication mechanism identifier |
|
117 | - * @param array $backendOptions backend-specific options |
|
118 | - * @param array $mountOptions backend-specific mount options |
|
119 | - * |
|
120 | - * @return DataResponse |
|
121 | - * |
|
122 | - * @NoAdminRequired |
|
123 | - */ |
|
124 | - public function create( |
|
125 | - $mountPoint, |
|
126 | - $backend, |
|
127 | - $authMechanism, |
|
128 | - $backendOptions, |
|
129 | - $mountOptions |
|
130 | - ) { |
|
131 | - $newStorage = $this->createStorage( |
|
132 | - $mountPoint, |
|
133 | - $backend, |
|
134 | - $authMechanism, |
|
135 | - $backendOptions, |
|
136 | - $mountOptions |
|
137 | - ); |
|
138 | - if ($newStorage instanceof DataResponse) { |
|
139 | - return $newStorage; |
|
140 | - } |
|
141 | - |
|
142 | - $response = $this->validate($newStorage); |
|
143 | - if (!empty($response)) { |
|
144 | - return $response; |
|
145 | - } |
|
146 | - |
|
147 | - $newStorage = $this->service->addStorage($newStorage); |
|
148 | - $this->updateStorageStatus($newStorage); |
|
149 | - |
|
150 | - return new DataResponse( |
|
151 | - $this->formatStorageForUI($newStorage), |
|
152 | - Http::STATUS_CREATED |
|
153 | - ); |
|
154 | - } |
|
155 | - |
|
156 | - /** |
|
157 | - * Update an external storage entry. |
|
158 | - * |
|
159 | - * @param int $id storage id |
|
160 | - * @param string $mountPoint storage mount point |
|
161 | - * @param string $backend backend identifier |
|
162 | - * @param string $authMechanism authentication mechanism identifier |
|
163 | - * @param array $backendOptions backend-specific options |
|
164 | - * @param array $mountOptions backend-specific mount options |
|
165 | - * @param bool $testOnly whether to storage should only test the connection or do more things |
|
166 | - * |
|
167 | - * @return DataResponse |
|
168 | - * |
|
169 | - * @NoAdminRequired |
|
170 | - */ |
|
171 | - public function update( |
|
172 | - $id, |
|
173 | - $mountPoint, |
|
174 | - $backend, |
|
175 | - $authMechanism, |
|
176 | - $backendOptions, |
|
177 | - $mountOptions, |
|
178 | - $testOnly = true |
|
179 | - ) { |
|
180 | - $storage = $this->createStorage( |
|
181 | - $mountPoint, |
|
182 | - $backend, |
|
183 | - $authMechanism, |
|
184 | - $backendOptions, |
|
185 | - $mountOptions |
|
186 | - ); |
|
187 | - if ($storage instanceof DataResponse) { |
|
188 | - return $storage; |
|
189 | - } |
|
190 | - $storage->setId($id); |
|
191 | - |
|
192 | - $response = $this->validate($storage); |
|
193 | - if (!empty($response)) { |
|
194 | - return $response; |
|
195 | - } |
|
196 | - |
|
197 | - try { |
|
198 | - $storage = $this->service->updateStorage($storage); |
|
199 | - } catch (NotFoundException $e) { |
|
200 | - return new DataResponse( |
|
201 | - [ |
|
202 | - 'message' => (string)$this->l10n->t('Storage with ID "%d" not found', [$id]) |
|
203 | - ], |
|
204 | - Http::STATUS_NOT_FOUND |
|
205 | - ); |
|
206 | - } |
|
207 | - |
|
208 | - $this->updateStorageStatus($storage, $testOnly); |
|
209 | - |
|
210 | - return new DataResponse( |
|
211 | - $this->formatStorageForUI($storage), |
|
212 | - Http::STATUS_OK |
|
213 | - ); |
|
214 | - |
|
215 | - } |
|
216 | - |
|
217 | - /** |
|
218 | - * Delete storage |
|
219 | - * |
|
220 | - * @NoAdminRequired |
|
221 | - * |
|
222 | - * {@inheritdoc} |
|
223 | - */ |
|
224 | - public function destroy($id) { |
|
225 | - return parent::destroy($id); |
|
226 | - } |
|
47 | + /** |
|
48 | + * @var IUserSession |
|
49 | + */ |
|
50 | + private $userSession; |
|
51 | + |
|
52 | + /** |
|
53 | + * Creates a new user storages controller. |
|
54 | + * |
|
55 | + * @param string $AppName application name |
|
56 | + * @param IRequest $request request object |
|
57 | + * @param IL10N $l10n l10n service |
|
58 | + * @param UserStoragesService $userStoragesService storage service |
|
59 | + * @param IUserSession $userSession |
|
60 | + * @param ILogger $logger |
|
61 | + */ |
|
62 | + public function __construct( |
|
63 | + $AppName, |
|
64 | + IRequest $request, |
|
65 | + IL10N $l10n, |
|
66 | + UserStoragesService $userStoragesService, |
|
67 | + IUserSession $userSession, |
|
68 | + ILogger $logger |
|
69 | + ) { |
|
70 | + parent::__construct( |
|
71 | + $AppName, |
|
72 | + $request, |
|
73 | + $l10n, |
|
74 | + $userStoragesService, |
|
75 | + $logger |
|
76 | + ); |
|
77 | + $this->userSession = $userSession; |
|
78 | + } |
|
79 | + |
|
80 | + protected function manipulateStorageConfig(StorageConfig $storage) { |
|
81 | + /** @var AuthMechanism */ |
|
82 | + $authMechanism = $storage->getAuthMechanism(); |
|
83 | + $authMechanism->manipulateStorageConfig($storage, $this->userSession->getUser()); |
|
84 | + /** @var Backend */ |
|
85 | + $backend = $storage->getBackend(); |
|
86 | + $backend->manipulateStorageConfig($storage, $this->userSession->getUser()); |
|
87 | + } |
|
88 | + |
|
89 | + /** |
|
90 | + * Get all storage entries |
|
91 | + * |
|
92 | + * @NoAdminRequired |
|
93 | + * |
|
94 | + * @return DataResponse |
|
95 | + */ |
|
96 | + public function index() { |
|
97 | + return parent::index(); |
|
98 | + } |
|
99 | + |
|
100 | + /** |
|
101 | + * Return storage |
|
102 | + * |
|
103 | + * @NoAdminRequired |
|
104 | + * |
|
105 | + * {@inheritdoc} |
|
106 | + */ |
|
107 | + public function show($id, $testOnly = true) { |
|
108 | + return parent::show($id, $testOnly); |
|
109 | + } |
|
110 | + |
|
111 | + /** |
|
112 | + * Create an external storage entry. |
|
113 | + * |
|
114 | + * @param string $mountPoint storage mount point |
|
115 | + * @param string $backend backend identifier |
|
116 | + * @param string $authMechanism authentication mechanism identifier |
|
117 | + * @param array $backendOptions backend-specific options |
|
118 | + * @param array $mountOptions backend-specific mount options |
|
119 | + * |
|
120 | + * @return DataResponse |
|
121 | + * |
|
122 | + * @NoAdminRequired |
|
123 | + */ |
|
124 | + public function create( |
|
125 | + $mountPoint, |
|
126 | + $backend, |
|
127 | + $authMechanism, |
|
128 | + $backendOptions, |
|
129 | + $mountOptions |
|
130 | + ) { |
|
131 | + $newStorage = $this->createStorage( |
|
132 | + $mountPoint, |
|
133 | + $backend, |
|
134 | + $authMechanism, |
|
135 | + $backendOptions, |
|
136 | + $mountOptions |
|
137 | + ); |
|
138 | + if ($newStorage instanceof DataResponse) { |
|
139 | + return $newStorage; |
|
140 | + } |
|
141 | + |
|
142 | + $response = $this->validate($newStorage); |
|
143 | + if (!empty($response)) { |
|
144 | + return $response; |
|
145 | + } |
|
146 | + |
|
147 | + $newStorage = $this->service->addStorage($newStorage); |
|
148 | + $this->updateStorageStatus($newStorage); |
|
149 | + |
|
150 | + return new DataResponse( |
|
151 | + $this->formatStorageForUI($newStorage), |
|
152 | + Http::STATUS_CREATED |
|
153 | + ); |
|
154 | + } |
|
155 | + |
|
156 | + /** |
|
157 | + * Update an external storage entry. |
|
158 | + * |
|
159 | + * @param int $id storage id |
|
160 | + * @param string $mountPoint storage mount point |
|
161 | + * @param string $backend backend identifier |
|
162 | + * @param string $authMechanism authentication mechanism identifier |
|
163 | + * @param array $backendOptions backend-specific options |
|
164 | + * @param array $mountOptions backend-specific mount options |
|
165 | + * @param bool $testOnly whether to storage should only test the connection or do more things |
|
166 | + * |
|
167 | + * @return DataResponse |
|
168 | + * |
|
169 | + * @NoAdminRequired |
|
170 | + */ |
|
171 | + public function update( |
|
172 | + $id, |
|
173 | + $mountPoint, |
|
174 | + $backend, |
|
175 | + $authMechanism, |
|
176 | + $backendOptions, |
|
177 | + $mountOptions, |
|
178 | + $testOnly = true |
|
179 | + ) { |
|
180 | + $storage = $this->createStorage( |
|
181 | + $mountPoint, |
|
182 | + $backend, |
|
183 | + $authMechanism, |
|
184 | + $backendOptions, |
|
185 | + $mountOptions |
|
186 | + ); |
|
187 | + if ($storage instanceof DataResponse) { |
|
188 | + return $storage; |
|
189 | + } |
|
190 | + $storage->setId($id); |
|
191 | + |
|
192 | + $response = $this->validate($storage); |
|
193 | + if (!empty($response)) { |
|
194 | + return $response; |
|
195 | + } |
|
196 | + |
|
197 | + try { |
|
198 | + $storage = $this->service->updateStorage($storage); |
|
199 | + } catch (NotFoundException $e) { |
|
200 | + return new DataResponse( |
|
201 | + [ |
|
202 | + 'message' => (string)$this->l10n->t('Storage with ID "%d" not found', [$id]) |
|
203 | + ], |
|
204 | + Http::STATUS_NOT_FOUND |
|
205 | + ); |
|
206 | + } |
|
207 | + |
|
208 | + $this->updateStorageStatus($storage, $testOnly); |
|
209 | + |
|
210 | + return new DataResponse( |
|
211 | + $this->formatStorageForUI($storage), |
|
212 | + Http::STATUS_OK |
|
213 | + ); |
|
214 | + |
|
215 | + } |
|
216 | + |
|
217 | + /** |
|
218 | + * Delete storage |
|
219 | + * |
|
220 | + * @NoAdminRequired |
|
221 | + * |
|
222 | + * {@inheritdoc} |
|
223 | + */ |
|
224 | + public function destroy($id) { |
|
225 | + return parent::destroy($id); |
|
226 | + } |
|
227 | 227 | |
228 | 228 | } |
@@ -48,170 +48,170 @@ |
||
48 | 48 | |
49 | 49 | class DeletedShareAPIController extends OCSController { |
50 | 50 | |
51 | - /** @var ShareManager */ |
|
52 | - private $shareManager; |
|
53 | - |
|
54 | - /** @var string */ |
|
55 | - private $userId; |
|
56 | - |
|
57 | - /** @var IUserManager */ |
|
58 | - private $userManager; |
|
59 | - |
|
60 | - /** @var IGroupManager */ |
|
61 | - private $groupManager; |
|
62 | - |
|
63 | - /** @var IRootFolder */ |
|
64 | - private $rootFolder; |
|
65 | - |
|
66 | - /** @var IAppManager */ |
|
67 | - private $appManager; |
|
68 | - |
|
69 | - /** @var IServerContainer */ |
|
70 | - private $serverContainer; |
|
71 | - |
|
72 | - public function __construct(string $appName, |
|
73 | - IRequest $request, |
|
74 | - ShareManager $shareManager, |
|
75 | - string $UserId, |
|
76 | - IUserManager $userManager, |
|
77 | - IGroupManager $groupManager, |
|
78 | - IRootFolder $rootFolder, |
|
79 | - IAppManager $appManager, |
|
80 | - IServerContainer $serverContainer) { |
|
81 | - parent::__construct($appName, $request); |
|
82 | - |
|
83 | - $this->shareManager = $shareManager; |
|
84 | - $this->userId = $UserId; |
|
85 | - $this->userManager = $userManager; |
|
86 | - $this->groupManager = $groupManager; |
|
87 | - $this->rootFolder = $rootFolder; |
|
88 | - $this->appManager = $appManager; |
|
89 | - $this->serverContainer = $serverContainer; |
|
90 | - } |
|
91 | - |
|
92 | - /** |
|
93 | - * @suppress PhanUndeclaredClassMethod |
|
94 | - */ |
|
95 | - private function formatShare(IShare $share): array { |
|
96 | - |
|
97 | - $result = [ |
|
98 | - 'id' => $share->getFullId(), |
|
99 | - 'share_type' => $share->getShareType(), |
|
100 | - 'uid_owner' => $share->getSharedBy(), |
|
101 | - 'displayname_owner' => $this->userManager->get($share->getSharedBy())->getDisplayName(), |
|
102 | - 'permissions' => 0, |
|
103 | - 'stime' => $share->getShareTime()->getTimestamp(), |
|
104 | - 'parent' => null, |
|
105 | - 'expiration' => null, |
|
106 | - 'token' => null, |
|
107 | - 'uid_file_owner' => $share->getShareOwner(), |
|
108 | - 'displayname_file_owner' => $this->userManager->get($share->getShareOwner())->getDisplayName(), |
|
109 | - 'path' => $share->getTarget(), |
|
110 | - ]; |
|
111 | - $userFolder = $this->rootFolder->getUserFolder($share->getSharedBy()); |
|
112 | - $nodes = $userFolder->getById($share->getNodeId()); |
|
113 | - if (empty($nodes)) { |
|
114 | - // fallback to guessing the path |
|
115 | - $node = $userFolder->get($share->getTarget()); |
|
116 | - if ($node === null || $share->getTarget() === '') { |
|
117 | - throw new NotFoundException(); |
|
118 | - } |
|
119 | - } else { |
|
120 | - $node = $nodes[0]; |
|
121 | - } |
|
122 | - |
|
123 | - $result['path'] = $userFolder->getRelativePath($node->getPath()); |
|
124 | - if ($node instanceof \OCP\Files\Folder) { |
|
125 | - $result['item_type'] = 'folder'; |
|
126 | - } else { |
|
127 | - $result['item_type'] = 'file'; |
|
128 | - } |
|
129 | - $result['mimetype'] = $node->getMimetype(); |
|
130 | - $result['storage_id'] = $node->getStorage()->getId(); |
|
131 | - $result['storage'] = $node->getStorage()->getCache()->getNumericStorageId(); |
|
132 | - $result['item_source'] = $node->getId(); |
|
133 | - $result['file_source'] = $node->getId(); |
|
134 | - $result['file_parent'] = $node->getParent()->getId(); |
|
135 | - $result['file_target'] = $share->getTarget(); |
|
136 | - |
|
137 | - $expiration = $share->getExpirationDate(); |
|
138 | - if ($expiration !== null) { |
|
139 | - $result['expiration'] = $expiration->format('Y-m-d 00:00:00'); |
|
140 | - } |
|
141 | - |
|
142 | - if ($share->getShareType() === \OCP\Share::SHARE_TYPE_GROUP) { |
|
143 | - $group = $this->groupManager->get($share->getSharedWith()); |
|
144 | - $result['share_with'] = $share->getSharedWith(); |
|
145 | - $result['share_with_displayname'] = $group !== null ? $group->getDisplayName() : $share->getSharedWith(); |
|
146 | - } else if ($share->getShareType() === \OCP\Share::SHARE_TYPE_ROOM) { |
|
147 | - $result['share_with'] = $share->getSharedWith(); |
|
148 | - $result['share_with_displayname'] = ''; |
|
149 | - |
|
150 | - try { |
|
151 | - $result = array_merge($result, $this->getRoomShareHelper()->formatShare($share)); |
|
152 | - } catch (QueryException $e) { |
|
153 | - } |
|
154 | - } |
|
155 | - |
|
156 | - return $result; |
|
157 | - |
|
158 | - } |
|
159 | - |
|
160 | - /** |
|
161 | - * @NoAdminRequired |
|
162 | - */ |
|
163 | - public function index(): DataResponse { |
|
164 | - $groupShares = $this->shareManager->getDeletedSharedWith($this->userId, \OCP\Share::SHARE_TYPE_GROUP, null, -1, 0); |
|
165 | - $roomShares = $this->shareManager->getDeletedSharedWith($this->userId, \OCP\Share::SHARE_TYPE_ROOM, null, -1, 0); |
|
166 | - |
|
167 | - $shares = array_merge($groupShares, $roomShares); |
|
168 | - |
|
169 | - $shares = array_map(function (IShare $share) { |
|
170 | - return $this->formatShare($share); |
|
171 | - }, $shares); |
|
172 | - |
|
173 | - return new DataResponse($shares); |
|
174 | - } |
|
175 | - |
|
176 | - /** |
|
177 | - * @NoAdminRequired |
|
178 | - * |
|
179 | - * @throws OCSException |
|
180 | - */ |
|
181 | - public function undelete(string $id): DataResponse { |
|
182 | - try { |
|
183 | - $share = $this->shareManager->getShareById($id, $this->userId); |
|
184 | - } catch (ShareNotFound $e) { |
|
185 | - throw new OCSNotFoundException('Share not found'); |
|
186 | - } |
|
187 | - |
|
188 | - if ($share->getPermissions() !== 0) { |
|
189 | - throw new OCSNotFoundException('No deleted share found'); |
|
190 | - } |
|
191 | - |
|
192 | - try { |
|
193 | - $this->shareManager->restoreShare($share, $this->userId); |
|
194 | - } catch (GenericShareException $e) { |
|
195 | - throw new OCSException('Something went wrong'); |
|
196 | - } |
|
197 | - |
|
198 | - return new DataResponse([]); |
|
199 | - } |
|
200 | - |
|
201 | - /** |
|
202 | - * Returns the helper of DeletedShareAPIController for room shares. |
|
203 | - * |
|
204 | - * If the Talk application is not enabled or the helper is not available |
|
205 | - * a QueryException is thrown instead. |
|
206 | - * |
|
207 | - * @return \OCA\Talk\Share\Helper\DeletedShareAPIController |
|
208 | - * @throws QueryException |
|
209 | - */ |
|
210 | - private function getRoomShareHelper() { |
|
211 | - if (!$this->appManager->isEnabledForUser('spreed')) { |
|
212 | - throw new QueryException(); |
|
213 | - } |
|
214 | - |
|
215 | - return $this->serverContainer->query('\OCA\Talk\Share\Helper\DeletedShareAPIController'); |
|
216 | - } |
|
51 | + /** @var ShareManager */ |
|
52 | + private $shareManager; |
|
53 | + |
|
54 | + /** @var string */ |
|
55 | + private $userId; |
|
56 | + |
|
57 | + /** @var IUserManager */ |
|
58 | + private $userManager; |
|
59 | + |
|
60 | + /** @var IGroupManager */ |
|
61 | + private $groupManager; |
|
62 | + |
|
63 | + /** @var IRootFolder */ |
|
64 | + private $rootFolder; |
|
65 | + |
|
66 | + /** @var IAppManager */ |
|
67 | + private $appManager; |
|
68 | + |
|
69 | + /** @var IServerContainer */ |
|
70 | + private $serverContainer; |
|
71 | + |
|
72 | + public function __construct(string $appName, |
|
73 | + IRequest $request, |
|
74 | + ShareManager $shareManager, |
|
75 | + string $UserId, |
|
76 | + IUserManager $userManager, |
|
77 | + IGroupManager $groupManager, |
|
78 | + IRootFolder $rootFolder, |
|
79 | + IAppManager $appManager, |
|
80 | + IServerContainer $serverContainer) { |
|
81 | + parent::__construct($appName, $request); |
|
82 | + |
|
83 | + $this->shareManager = $shareManager; |
|
84 | + $this->userId = $UserId; |
|
85 | + $this->userManager = $userManager; |
|
86 | + $this->groupManager = $groupManager; |
|
87 | + $this->rootFolder = $rootFolder; |
|
88 | + $this->appManager = $appManager; |
|
89 | + $this->serverContainer = $serverContainer; |
|
90 | + } |
|
91 | + |
|
92 | + /** |
|
93 | + * @suppress PhanUndeclaredClassMethod |
|
94 | + */ |
|
95 | + private function formatShare(IShare $share): array { |
|
96 | + |
|
97 | + $result = [ |
|
98 | + 'id' => $share->getFullId(), |
|
99 | + 'share_type' => $share->getShareType(), |
|
100 | + 'uid_owner' => $share->getSharedBy(), |
|
101 | + 'displayname_owner' => $this->userManager->get($share->getSharedBy())->getDisplayName(), |
|
102 | + 'permissions' => 0, |
|
103 | + 'stime' => $share->getShareTime()->getTimestamp(), |
|
104 | + 'parent' => null, |
|
105 | + 'expiration' => null, |
|
106 | + 'token' => null, |
|
107 | + 'uid_file_owner' => $share->getShareOwner(), |
|
108 | + 'displayname_file_owner' => $this->userManager->get($share->getShareOwner())->getDisplayName(), |
|
109 | + 'path' => $share->getTarget(), |
|
110 | + ]; |
|
111 | + $userFolder = $this->rootFolder->getUserFolder($share->getSharedBy()); |
|
112 | + $nodes = $userFolder->getById($share->getNodeId()); |
|
113 | + if (empty($nodes)) { |
|
114 | + // fallback to guessing the path |
|
115 | + $node = $userFolder->get($share->getTarget()); |
|
116 | + if ($node === null || $share->getTarget() === '') { |
|
117 | + throw new NotFoundException(); |
|
118 | + } |
|
119 | + } else { |
|
120 | + $node = $nodes[0]; |
|
121 | + } |
|
122 | + |
|
123 | + $result['path'] = $userFolder->getRelativePath($node->getPath()); |
|
124 | + if ($node instanceof \OCP\Files\Folder) { |
|
125 | + $result['item_type'] = 'folder'; |
|
126 | + } else { |
|
127 | + $result['item_type'] = 'file'; |
|
128 | + } |
|
129 | + $result['mimetype'] = $node->getMimetype(); |
|
130 | + $result['storage_id'] = $node->getStorage()->getId(); |
|
131 | + $result['storage'] = $node->getStorage()->getCache()->getNumericStorageId(); |
|
132 | + $result['item_source'] = $node->getId(); |
|
133 | + $result['file_source'] = $node->getId(); |
|
134 | + $result['file_parent'] = $node->getParent()->getId(); |
|
135 | + $result['file_target'] = $share->getTarget(); |
|
136 | + |
|
137 | + $expiration = $share->getExpirationDate(); |
|
138 | + if ($expiration !== null) { |
|
139 | + $result['expiration'] = $expiration->format('Y-m-d 00:00:00'); |
|
140 | + } |
|
141 | + |
|
142 | + if ($share->getShareType() === \OCP\Share::SHARE_TYPE_GROUP) { |
|
143 | + $group = $this->groupManager->get($share->getSharedWith()); |
|
144 | + $result['share_with'] = $share->getSharedWith(); |
|
145 | + $result['share_with_displayname'] = $group !== null ? $group->getDisplayName() : $share->getSharedWith(); |
|
146 | + } else if ($share->getShareType() === \OCP\Share::SHARE_TYPE_ROOM) { |
|
147 | + $result['share_with'] = $share->getSharedWith(); |
|
148 | + $result['share_with_displayname'] = ''; |
|
149 | + |
|
150 | + try { |
|
151 | + $result = array_merge($result, $this->getRoomShareHelper()->formatShare($share)); |
|
152 | + } catch (QueryException $e) { |
|
153 | + } |
|
154 | + } |
|
155 | + |
|
156 | + return $result; |
|
157 | + |
|
158 | + } |
|
159 | + |
|
160 | + /** |
|
161 | + * @NoAdminRequired |
|
162 | + */ |
|
163 | + public function index(): DataResponse { |
|
164 | + $groupShares = $this->shareManager->getDeletedSharedWith($this->userId, \OCP\Share::SHARE_TYPE_GROUP, null, -1, 0); |
|
165 | + $roomShares = $this->shareManager->getDeletedSharedWith($this->userId, \OCP\Share::SHARE_TYPE_ROOM, null, -1, 0); |
|
166 | + |
|
167 | + $shares = array_merge($groupShares, $roomShares); |
|
168 | + |
|
169 | + $shares = array_map(function (IShare $share) { |
|
170 | + return $this->formatShare($share); |
|
171 | + }, $shares); |
|
172 | + |
|
173 | + return new DataResponse($shares); |
|
174 | + } |
|
175 | + |
|
176 | + /** |
|
177 | + * @NoAdminRequired |
|
178 | + * |
|
179 | + * @throws OCSException |
|
180 | + */ |
|
181 | + public function undelete(string $id): DataResponse { |
|
182 | + try { |
|
183 | + $share = $this->shareManager->getShareById($id, $this->userId); |
|
184 | + } catch (ShareNotFound $e) { |
|
185 | + throw new OCSNotFoundException('Share not found'); |
|
186 | + } |
|
187 | + |
|
188 | + if ($share->getPermissions() !== 0) { |
|
189 | + throw new OCSNotFoundException('No deleted share found'); |
|
190 | + } |
|
191 | + |
|
192 | + try { |
|
193 | + $this->shareManager->restoreShare($share, $this->userId); |
|
194 | + } catch (GenericShareException $e) { |
|
195 | + throw new OCSException('Something went wrong'); |
|
196 | + } |
|
197 | + |
|
198 | + return new DataResponse([]); |
|
199 | + } |
|
200 | + |
|
201 | + /** |
|
202 | + * Returns the helper of DeletedShareAPIController for room shares. |
|
203 | + * |
|
204 | + * If the Talk application is not enabled or the helper is not available |
|
205 | + * a QueryException is thrown instead. |
|
206 | + * |
|
207 | + * @return \OCA\Talk\Share\Helper\DeletedShareAPIController |
|
208 | + * @throws QueryException |
|
209 | + */ |
|
210 | + private function getRoomShareHelper() { |
|
211 | + if (!$this->appManager->isEnabledForUser('spreed')) { |
|
212 | + throw new QueryException(); |
|
213 | + } |
|
214 | + |
|
215 | + return $this->serverContainer->query('\OCA\Talk\Share\Helper\DeletedShareAPIController'); |
|
216 | + } |
|
217 | 217 | } |
@@ -31,136 +31,136 @@ |
||
31 | 31 | * capabilities. |
32 | 32 | */ |
33 | 33 | abstract class Backend implements UserInterface { |
34 | - /** |
|
35 | - * error code for functions not provided by the user backend |
|
36 | - */ |
|
37 | - const NOT_IMPLEMENTED = -501; |
|
34 | + /** |
|
35 | + * error code for functions not provided by the user backend |
|
36 | + */ |
|
37 | + const NOT_IMPLEMENTED = -501; |
|
38 | 38 | |
39 | - /** |
|
40 | - * actions that user backends can define |
|
41 | - */ |
|
42 | - const CREATE_USER = 1; // 1 << 0 |
|
43 | - const SET_PASSWORD = 16; // 1 << 4 |
|
44 | - const CHECK_PASSWORD = 256; // 1 << 8 |
|
45 | - const GET_HOME = 4096; // 1 << 12 |
|
46 | - const GET_DISPLAYNAME = 65536; // 1 << 16 |
|
47 | - const SET_DISPLAYNAME = 1048576; // 1 << 20 |
|
48 | - const PROVIDE_AVATAR = 16777216; // 1 << 24 |
|
49 | - const COUNT_USERS = 268435456; // 1 << 28 |
|
39 | + /** |
|
40 | + * actions that user backends can define |
|
41 | + */ |
|
42 | + const CREATE_USER = 1; // 1 << 0 |
|
43 | + const SET_PASSWORD = 16; // 1 << 4 |
|
44 | + const CHECK_PASSWORD = 256; // 1 << 8 |
|
45 | + const GET_HOME = 4096; // 1 << 12 |
|
46 | + const GET_DISPLAYNAME = 65536; // 1 << 16 |
|
47 | + const SET_DISPLAYNAME = 1048576; // 1 << 20 |
|
48 | + const PROVIDE_AVATAR = 16777216; // 1 << 24 |
|
49 | + const COUNT_USERS = 268435456; // 1 << 28 |
|
50 | 50 | |
51 | - protected $possibleActions = [ |
|
52 | - self::CREATE_USER => 'createUser', |
|
53 | - self::SET_PASSWORD => 'setPassword', |
|
54 | - self::CHECK_PASSWORD => 'checkPassword', |
|
55 | - self::GET_HOME => 'getHome', |
|
56 | - self::GET_DISPLAYNAME => 'getDisplayName', |
|
57 | - self::SET_DISPLAYNAME => 'setDisplayName', |
|
58 | - self::PROVIDE_AVATAR => 'canChangeAvatar', |
|
59 | - self::COUNT_USERS => 'countUsers', |
|
60 | - ]; |
|
51 | + protected $possibleActions = [ |
|
52 | + self::CREATE_USER => 'createUser', |
|
53 | + self::SET_PASSWORD => 'setPassword', |
|
54 | + self::CHECK_PASSWORD => 'checkPassword', |
|
55 | + self::GET_HOME => 'getHome', |
|
56 | + self::GET_DISPLAYNAME => 'getDisplayName', |
|
57 | + self::SET_DISPLAYNAME => 'setDisplayName', |
|
58 | + self::PROVIDE_AVATAR => 'canChangeAvatar', |
|
59 | + self::COUNT_USERS => 'countUsers', |
|
60 | + ]; |
|
61 | 61 | |
62 | - /** |
|
63 | - * Get all supported actions |
|
64 | - * @return int bitwise-or'ed actions |
|
65 | - * |
|
66 | - * Returns the supported actions as int to be |
|
67 | - * compared with self::CREATE_USER etc. |
|
68 | - */ |
|
69 | - public function getSupportedActions() { |
|
70 | - $actions = 0; |
|
71 | - foreach($this->possibleActions as $action => $methodName) { |
|
72 | - if(method_exists($this, $methodName)) { |
|
73 | - $actions |= $action; |
|
74 | - } |
|
75 | - } |
|
62 | + /** |
|
63 | + * Get all supported actions |
|
64 | + * @return int bitwise-or'ed actions |
|
65 | + * |
|
66 | + * Returns the supported actions as int to be |
|
67 | + * compared with self::CREATE_USER etc. |
|
68 | + */ |
|
69 | + public function getSupportedActions() { |
|
70 | + $actions = 0; |
|
71 | + foreach($this->possibleActions as $action => $methodName) { |
|
72 | + if(method_exists($this, $methodName)) { |
|
73 | + $actions |= $action; |
|
74 | + } |
|
75 | + } |
|
76 | 76 | |
77 | - return $actions; |
|
78 | - } |
|
77 | + return $actions; |
|
78 | + } |
|
79 | 79 | |
80 | - /** |
|
81 | - * Check if backend implements actions |
|
82 | - * @param int $actions bitwise-or'ed actions |
|
83 | - * @return boolean |
|
84 | - * |
|
85 | - * Returns the supported actions as int to be |
|
86 | - * compared with self::CREATE_USER etc. |
|
87 | - */ |
|
88 | - public function implementsActions($actions) { |
|
89 | - return (bool)($this->getSupportedActions() & $actions); |
|
90 | - } |
|
80 | + /** |
|
81 | + * Check if backend implements actions |
|
82 | + * @param int $actions bitwise-or'ed actions |
|
83 | + * @return boolean |
|
84 | + * |
|
85 | + * Returns the supported actions as int to be |
|
86 | + * compared with self::CREATE_USER etc. |
|
87 | + */ |
|
88 | + public function implementsActions($actions) { |
|
89 | + return (bool)($this->getSupportedActions() & $actions); |
|
90 | + } |
|
91 | 91 | |
92 | - /** |
|
93 | - * delete a user |
|
94 | - * @param string $uid The username of the user to delete |
|
95 | - * @return bool |
|
96 | - * |
|
97 | - * Deletes a user |
|
98 | - */ |
|
99 | - public function deleteUser($uid) { |
|
100 | - return false; |
|
101 | - } |
|
92 | + /** |
|
93 | + * delete a user |
|
94 | + * @param string $uid The username of the user to delete |
|
95 | + * @return bool |
|
96 | + * |
|
97 | + * Deletes a user |
|
98 | + */ |
|
99 | + public function deleteUser($uid) { |
|
100 | + return false; |
|
101 | + } |
|
102 | 102 | |
103 | - /** |
|
104 | - * Get a list of all users |
|
105 | - * |
|
106 | - * @param string $search |
|
107 | - * @param null|int $limit |
|
108 | - * @param null|int $offset |
|
109 | - * @return string[] an array of all uids |
|
110 | - */ |
|
111 | - public function getUsers($search = '', $limit = null, $offset = null) { |
|
112 | - return []; |
|
113 | - } |
|
103 | + /** |
|
104 | + * Get a list of all users |
|
105 | + * |
|
106 | + * @param string $search |
|
107 | + * @param null|int $limit |
|
108 | + * @param null|int $offset |
|
109 | + * @return string[] an array of all uids |
|
110 | + */ |
|
111 | + public function getUsers($search = '', $limit = null, $offset = null) { |
|
112 | + return []; |
|
113 | + } |
|
114 | 114 | |
115 | - /** |
|
116 | - * check if a user exists |
|
117 | - * @param string $uid the username |
|
118 | - * @return boolean |
|
119 | - */ |
|
120 | - public function userExists($uid) { |
|
121 | - return false; |
|
122 | - } |
|
115 | + /** |
|
116 | + * check if a user exists |
|
117 | + * @param string $uid the username |
|
118 | + * @return boolean |
|
119 | + */ |
|
120 | + public function userExists($uid) { |
|
121 | + return false; |
|
122 | + } |
|
123 | 123 | |
124 | - /** |
|
125 | - * get the user's home directory |
|
126 | - * @param string $uid the username |
|
127 | - * @return boolean |
|
128 | - */ |
|
129 | - public function getHome($uid) { |
|
130 | - return false; |
|
131 | - } |
|
124 | + /** |
|
125 | + * get the user's home directory |
|
126 | + * @param string $uid the username |
|
127 | + * @return boolean |
|
128 | + */ |
|
129 | + public function getHome($uid) { |
|
130 | + return false; |
|
131 | + } |
|
132 | 132 | |
133 | - /** |
|
134 | - * get display name of the user |
|
135 | - * @param string $uid user ID of the user |
|
136 | - * @return string display name |
|
137 | - */ |
|
138 | - public function getDisplayName($uid) { |
|
139 | - return $uid; |
|
140 | - } |
|
133 | + /** |
|
134 | + * get display name of the user |
|
135 | + * @param string $uid user ID of the user |
|
136 | + * @return string display name |
|
137 | + */ |
|
138 | + public function getDisplayName($uid) { |
|
139 | + return $uid; |
|
140 | + } |
|
141 | 141 | |
142 | - /** |
|
143 | - * Get a list of all display names and user ids. |
|
144 | - * |
|
145 | - * @param string $search |
|
146 | - * @param string|null $limit |
|
147 | - * @param string|null $offset |
|
148 | - * @return array an array of all displayNames (value) and the corresponding uids (key) |
|
149 | - */ |
|
150 | - public function getDisplayNames($search = '', $limit = null, $offset = null) { |
|
151 | - $displayNames = []; |
|
152 | - $users = $this->getUsers($search, $limit, $offset); |
|
153 | - foreach ( $users as $user) { |
|
154 | - $displayNames[$user] = $user; |
|
155 | - } |
|
156 | - return $displayNames; |
|
157 | - } |
|
142 | + /** |
|
143 | + * Get a list of all display names and user ids. |
|
144 | + * |
|
145 | + * @param string $search |
|
146 | + * @param string|null $limit |
|
147 | + * @param string|null $offset |
|
148 | + * @return array an array of all displayNames (value) and the corresponding uids (key) |
|
149 | + */ |
|
150 | + public function getDisplayNames($search = '', $limit = null, $offset = null) { |
|
151 | + $displayNames = []; |
|
152 | + $users = $this->getUsers($search, $limit, $offset); |
|
153 | + foreach ( $users as $user) { |
|
154 | + $displayNames[$user] = $user; |
|
155 | + } |
|
156 | + return $displayNames; |
|
157 | + } |
|
158 | 158 | |
159 | - /** |
|
160 | - * Check if a user list is available or not |
|
161 | - * @return boolean if users can be listed or not |
|
162 | - */ |
|
163 | - public function hasUserListings() { |
|
164 | - return false; |
|
165 | - } |
|
159 | + /** |
|
160 | + * Check if a user list is available or not |
|
161 | + * @return boolean if users can be listed or not |
|
162 | + */ |
|
163 | + public function hasUserListings() { |
|
164 | + return false; |
|
165 | + } |
|
166 | 166 | } |
@@ -39,14 +39,14 @@ discard block |
||
39 | 39 | /** |
40 | 40 | * actions that user backends can define |
41 | 41 | */ |
42 | - const CREATE_USER = 1; // 1 << 0 |
|
43 | - const SET_PASSWORD = 16; // 1 << 4 |
|
44 | - const CHECK_PASSWORD = 256; // 1 << 8 |
|
45 | - const GET_HOME = 4096; // 1 << 12 |
|
46 | - const GET_DISPLAYNAME = 65536; // 1 << 16 |
|
47 | - const SET_DISPLAYNAME = 1048576; // 1 << 20 |
|
48 | - const PROVIDE_AVATAR = 16777216; // 1 << 24 |
|
49 | - const COUNT_USERS = 268435456; // 1 << 28 |
|
42 | + const CREATE_USER = 1; // 1 << 0 |
|
43 | + const SET_PASSWORD = 16; // 1 << 4 |
|
44 | + const CHECK_PASSWORD = 256; // 1 << 8 |
|
45 | + const GET_HOME = 4096; // 1 << 12 |
|
46 | + const GET_DISPLAYNAME = 65536; // 1 << 16 |
|
47 | + const SET_DISPLAYNAME = 1048576; // 1 << 20 |
|
48 | + const PROVIDE_AVATAR = 16777216; // 1 << 24 |
|
49 | + const COUNT_USERS = 268435456; // 1 << 28 |
|
50 | 50 | |
51 | 51 | protected $possibleActions = [ |
52 | 52 | self::CREATE_USER => 'createUser', |
@@ -68,8 +68,8 @@ discard block |
||
68 | 68 | */ |
69 | 69 | public function getSupportedActions() { |
70 | 70 | $actions = 0; |
71 | - foreach($this->possibleActions as $action => $methodName) { |
|
72 | - if(method_exists($this, $methodName)) { |
|
71 | + foreach ($this->possibleActions as $action => $methodName) { |
|
72 | + if (method_exists($this, $methodName)) { |
|
73 | 73 | $actions |= $action; |
74 | 74 | } |
75 | 75 | } |
@@ -86,7 +86,7 @@ discard block |
||
86 | 86 | * compared with self::CREATE_USER etc. |
87 | 87 | */ |
88 | 88 | public function implementsActions($actions) { |
89 | - return (bool)($this->getSupportedActions() & $actions); |
|
89 | + return (bool) ($this->getSupportedActions() & $actions); |
|
90 | 90 | } |
91 | 91 | |
92 | 92 | /** |
@@ -150,7 +150,7 @@ discard block |
||
150 | 150 | public function getDisplayNames($search = '', $limit = null, $offset = null) { |
151 | 151 | $displayNames = []; |
152 | 152 | $users = $this->getUsers($search, $limit, $offset); |
153 | - foreach ( $users as $user) { |
|
153 | + foreach ($users as $user) { |
|
154 | 154 | $displayNames[$user] = $user; |
155 | 155 | } |
156 | 156 | return $displayNames; |
@@ -40,177 +40,177 @@ |
||
40 | 40 | * @package OC\Session |
41 | 41 | */ |
42 | 42 | class CryptoSessionData implements \ArrayAccess, ISession { |
43 | - /** @var ISession */ |
|
44 | - protected $session; |
|
45 | - /** @var \OCP\Security\ICrypto */ |
|
46 | - protected $crypto; |
|
47 | - /** @var string */ |
|
48 | - protected $passphrase; |
|
49 | - /** @var array */ |
|
50 | - protected $sessionValues; |
|
51 | - /** @var bool */ |
|
52 | - protected $isModified = false; |
|
53 | - const encryptedSessionName = 'encrypted_session_data'; |
|
54 | - |
|
55 | - /** |
|
56 | - * @param ISession $session |
|
57 | - * @param ICrypto $crypto |
|
58 | - * @param string $passphrase |
|
59 | - */ |
|
60 | - public function __construct(ISession $session, |
|
61 | - ICrypto $crypto, |
|
62 | - string $passphrase) { |
|
63 | - $this->crypto = $crypto; |
|
64 | - $this->session = $session; |
|
65 | - $this->passphrase = $passphrase; |
|
66 | - $this->initializeSession(); |
|
67 | - } |
|
68 | - |
|
69 | - /** |
|
70 | - * Close session if class gets destructed |
|
71 | - */ |
|
72 | - public function __destruct() { |
|
73 | - try { |
|
74 | - $this->close(); |
|
75 | - } catch (SessionNotAvailableException $e){ |
|
76 | - // This exception can occur if session is already closed |
|
77 | - // So it is safe to ignore it and let the garbage collector to proceed |
|
78 | - } |
|
79 | - } |
|
80 | - |
|
81 | - protected function initializeSession() { |
|
82 | - $encryptedSessionData = $this->session->get(self::encryptedSessionName) ?: ''; |
|
83 | - try { |
|
84 | - $this->sessionValues = json_decode( |
|
85 | - $this->crypto->decrypt($encryptedSessionData, $this->passphrase), |
|
86 | - true |
|
87 | - ); |
|
88 | - } catch (\Exception $e) { |
|
89 | - $this->sessionValues = []; |
|
90 | - } |
|
91 | - } |
|
92 | - |
|
93 | - /** |
|
94 | - * Set a value in the session |
|
95 | - * |
|
96 | - * @param string $key |
|
97 | - * @param mixed $value |
|
98 | - */ |
|
99 | - public function set(string $key, $value) { |
|
100 | - $this->sessionValues[$key] = $value; |
|
101 | - $this->isModified = true; |
|
102 | - } |
|
103 | - |
|
104 | - /** |
|
105 | - * Get a value from the session |
|
106 | - * |
|
107 | - * @param string $key |
|
108 | - * @return string|null Either the value or null |
|
109 | - */ |
|
110 | - public function get(string $key) { |
|
111 | - if(isset($this->sessionValues[$key])) { |
|
112 | - return $this->sessionValues[$key]; |
|
113 | - } |
|
114 | - |
|
115 | - return null; |
|
116 | - } |
|
117 | - |
|
118 | - /** |
|
119 | - * Check if a named key exists in the session |
|
120 | - * |
|
121 | - * @param string $key |
|
122 | - * @return bool |
|
123 | - */ |
|
124 | - public function exists(string $key): bool { |
|
125 | - return isset($this->sessionValues[$key]); |
|
126 | - } |
|
127 | - |
|
128 | - /** |
|
129 | - * Remove a $key/$value pair from the session |
|
130 | - * |
|
131 | - * @param string $key |
|
132 | - */ |
|
133 | - public function remove(string $key) { |
|
134 | - $this->isModified = true; |
|
135 | - unset($this->sessionValues[$key]); |
|
136 | - $this->session->remove(self::encryptedSessionName); |
|
137 | - } |
|
138 | - |
|
139 | - /** |
|
140 | - * Reset and recreate the session |
|
141 | - */ |
|
142 | - public function clear() { |
|
143 | - $requesttoken = $this->get('requesttoken'); |
|
144 | - $this->sessionValues = []; |
|
145 | - if ($requesttoken !== null) { |
|
146 | - $this->set('requesttoken', $requesttoken); |
|
147 | - } |
|
148 | - $this->isModified = true; |
|
149 | - $this->session->clear(); |
|
150 | - } |
|
151 | - |
|
152 | - /** |
|
153 | - * Wrapper around session_regenerate_id |
|
154 | - * |
|
155 | - * @param bool $deleteOldSession Whether to delete the old associated session file or not. |
|
156 | - * @param bool $updateToken Wheater to update the associated auth token |
|
157 | - * @return void |
|
158 | - */ |
|
159 | - public function regenerateId(bool $deleteOldSession = true, bool $updateToken = false) { |
|
160 | - $this->session->regenerateId($deleteOldSession, $updateToken); |
|
161 | - } |
|
162 | - |
|
163 | - /** |
|
164 | - * Wrapper around session_id |
|
165 | - * |
|
166 | - * @return string |
|
167 | - * @throws SessionNotAvailableException |
|
168 | - * @since 9.1.0 |
|
169 | - */ |
|
170 | - public function getId(): string { |
|
171 | - return $this->session->getId(); |
|
172 | - } |
|
173 | - |
|
174 | - /** |
|
175 | - * Close the session and release the lock, also writes all changed data in batch |
|
176 | - */ |
|
177 | - public function close() { |
|
178 | - if($this->isModified) { |
|
179 | - $encryptedValue = $this->crypto->encrypt(json_encode($this->sessionValues), $this->passphrase); |
|
180 | - $this->session->set(self::encryptedSessionName, $encryptedValue); |
|
181 | - $this->isModified = false; |
|
182 | - } |
|
183 | - $this->session->close(); |
|
184 | - } |
|
185 | - |
|
186 | - /** |
|
187 | - * @param mixed $offset |
|
188 | - * @return bool |
|
189 | - */ |
|
190 | - public function offsetExists($offset): bool { |
|
191 | - return $this->exists($offset); |
|
192 | - } |
|
193 | - |
|
194 | - /** |
|
195 | - * @param mixed $offset |
|
196 | - * @return mixed |
|
197 | - */ |
|
198 | - public function offsetGet($offset) { |
|
199 | - return $this->get($offset); |
|
200 | - } |
|
201 | - |
|
202 | - /** |
|
203 | - * @param mixed $offset |
|
204 | - * @param mixed $value |
|
205 | - */ |
|
206 | - public function offsetSet($offset, $value) { |
|
207 | - $this->set($offset, $value); |
|
208 | - } |
|
209 | - |
|
210 | - /** |
|
211 | - * @param mixed $offset |
|
212 | - */ |
|
213 | - public function offsetUnset($offset) { |
|
214 | - $this->remove($offset); |
|
215 | - } |
|
43 | + /** @var ISession */ |
|
44 | + protected $session; |
|
45 | + /** @var \OCP\Security\ICrypto */ |
|
46 | + protected $crypto; |
|
47 | + /** @var string */ |
|
48 | + protected $passphrase; |
|
49 | + /** @var array */ |
|
50 | + protected $sessionValues; |
|
51 | + /** @var bool */ |
|
52 | + protected $isModified = false; |
|
53 | + const encryptedSessionName = 'encrypted_session_data'; |
|
54 | + |
|
55 | + /** |
|
56 | + * @param ISession $session |
|
57 | + * @param ICrypto $crypto |
|
58 | + * @param string $passphrase |
|
59 | + */ |
|
60 | + public function __construct(ISession $session, |
|
61 | + ICrypto $crypto, |
|
62 | + string $passphrase) { |
|
63 | + $this->crypto = $crypto; |
|
64 | + $this->session = $session; |
|
65 | + $this->passphrase = $passphrase; |
|
66 | + $this->initializeSession(); |
|
67 | + } |
|
68 | + |
|
69 | + /** |
|
70 | + * Close session if class gets destructed |
|
71 | + */ |
|
72 | + public function __destruct() { |
|
73 | + try { |
|
74 | + $this->close(); |
|
75 | + } catch (SessionNotAvailableException $e){ |
|
76 | + // This exception can occur if session is already closed |
|
77 | + // So it is safe to ignore it and let the garbage collector to proceed |
|
78 | + } |
|
79 | + } |
|
80 | + |
|
81 | + protected function initializeSession() { |
|
82 | + $encryptedSessionData = $this->session->get(self::encryptedSessionName) ?: ''; |
|
83 | + try { |
|
84 | + $this->sessionValues = json_decode( |
|
85 | + $this->crypto->decrypt($encryptedSessionData, $this->passphrase), |
|
86 | + true |
|
87 | + ); |
|
88 | + } catch (\Exception $e) { |
|
89 | + $this->sessionValues = []; |
|
90 | + } |
|
91 | + } |
|
92 | + |
|
93 | + /** |
|
94 | + * Set a value in the session |
|
95 | + * |
|
96 | + * @param string $key |
|
97 | + * @param mixed $value |
|
98 | + */ |
|
99 | + public function set(string $key, $value) { |
|
100 | + $this->sessionValues[$key] = $value; |
|
101 | + $this->isModified = true; |
|
102 | + } |
|
103 | + |
|
104 | + /** |
|
105 | + * Get a value from the session |
|
106 | + * |
|
107 | + * @param string $key |
|
108 | + * @return string|null Either the value or null |
|
109 | + */ |
|
110 | + public function get(string $key) { |
|
111 | + if(isset($this->sessionValues[$key])) { |
|
112 | + return $this->sessionValues[$key]; |
|
113 | + } |
|
114 | + |
|
115 | + return null; |
|
116 | + } |
|
117 | + |
|
118 | + /** |
|
119 | + * Check if a named key exists in the session |
|
120 | + * |
|
121 | + * @param string $key |
|
122 | + * @return bool |
|
123 | + */ |
|
124 | + public function exists(string $key): bool { |
|
125 | + return isset($this->sessionValues[$key]); |
|
126 | + } |
|
127 | + |
|
128 | + /** |
|
129 | + * Remove a $key/$value pair from the session |
|
130 | + * |
|
131 | + * @param string $key |
|
132 | + */ |
|
133 | + public function remove(string $key) { |
|
134 | + $this->isModified = true; |
|
135 | + unset($this->sessionValues[$key]); |
|
136 | + $this->session->remove(self::encryptedSessionName); |
|
137 | + } |
|
138 | + |
|
139 | + /** |
|
140 | + * Reset and recreate the session |
|
141 | + */ |
|
142 | + public function clear() { |
|
143 | + $requesttoken = $this->get('requesttoken'); |
|
144 | + $this->sessionValues = []; |
|
145 | + if ($requesttoken !== null) { |
|
146 | + $this->set('requesttoken', $requesttoken); |
|
147 | + } |
|
148 | + $this->isModified = true; |
|
149 | + $this->session->clear(); |
|
150 | + } |
|
151 | + |
|
152 | + /** |
|
153 | + * Wrapper around session_regenerate_id |
|
154 | + * |
|
155 | + * @param bool $deleteOldSession Whether to delete the old associated session file or not. |
|
156 | + * @param bool $updateToken Wheater to update the associated auth token |
|
157 | + * @return void |
|
158 | + */ |
|
159 | + public function regenerateId(bool $deleteOldSession = true, bool $updateToken = false) { |
|
160 | + $this->session->regenerateId($deleteOldSession, $updateToken); |
|
161 | + } |
|
162 | + |
|
163 | + /** |
|
164 | + * Wrapper around session_id |
|
165 | + * |
|
166 | + * @return string |
|
167 | + * @throws SessionNotAvailableException |
|
168 | + * @since 9.1.0 |
|
169 | + */ |
|
170 | + public function getId(): string { |
|
171 | + return $this->session->getId(); |
|
172 | + } |
|
173 | + |
|
174 | + /** |
|
175 | + * Close the session and release the lock, also writes all changed data in batch |
|
176 | + */ |
|
177 | + public function close() { |
|
178 | + if($this->isModified) { |
|
179 | + $encryptedValue = $this->crypto->encrypt(json_encode($this->sessionValues), $this->passphrase); |
|
180 | + $this->session->set(self::encryptedSessionName, $encryptedValue); |
|
181 | + $this->isModified = false; |
|
182 | + } |
|
183 | + $this->session->close(); |
|
184 | + } |
|
185 | + |
|
186 | + /** |
|
187 | + * @param mixed $offset |
|
188 | + * @return bool |
|
189 | + */ |
|
190 | + public function offsetExists($offset): bool { |
|
191 | + return $this->exists($offset); |
|
192 | + } |
|
193 | + |
|
194 | + /** |
|
195 | + * @param mixed $offset |
|
196 | + * @return mixed |
|
197 | + */ |
|
198 | + public function offsetGet($offset) { |
|
199 | + return $this->get($offset); |
|
200 | + } |
|
201 | + |
|
202 | + /** |
|
203 | + * @param mixed $offset |
|
204 | + * @param mixed $value |
|
205 | + */ |
|
206 | + public function offsetSet($offset, $value) { |
|
207 | + $this->set($offset, $value); |
|
208 | + } |
|
209 | + |
|
210 | + /** |
|
211 | + * @param mixed $offset |
|
212 | + */ |
|
213 | + public function offsetUnset($offset) { |
|
214 | + $this->remove($offset); |
|
215 | + } |
|
216 | 216 | } |
@@ -50,551 +50,551 @@ |
||
50 | 50 | * Collection of useful functions |
51 | 51 | */ |
52 | 52 | class OC_Helper { |
53 | - private static $templateManager; |
|
54 | - |
|
55 | - /** |
|
56 | - * Make a human file size |
|
57 | - * @param int $bytes file size in bytes |
|
58 | - * @return string a human readable file size |
|
59 | - * |
|
60 | - * Makes 2048 to 2 kB. |
|
61 | - */ |
|
62 | - public static function humanFileSize($bytes) { |
|
63 | - if ($bytes < 0) { |
|
64 | - return "?"; |
|
65 | - } |
|
66 | - if ($bytes < 1024) { |
|
67 | - return "$bytes B"; |
|
68 | - } |
|
69 | - $bytes = round($bytes / 1024, 0); |
|
70 | - if ($bytes < 1024) { |
|
71 | - return "$bytes KB"; |
|
72 | - } |
|
73 | - $bytes = round($bytes / 1024, 1); |
|
74 | - if ($bytes < 1024) { |
|
75 | - return "$bytes MB"; |
|
76 | - } |
|
77 | - $bytes = round($bytes / 1024, 1); |
|
78 | - if ($bytes < 1024) { |
|
79 | - return "$bytes GB"; |
|
80 | - } |
|
81 | - $bytes = round($bytes / 1024, 1); |
|
82 | - if ($bytes < 1024) { |
|
83 | - return "$bytes TB"; |
|
84 | - } |
|
85 | - |
|
86 | - $bytes = round($bytes / 1024, 1); |
|
87 | - return "$bytes PB"; |
|
88 | - } |
|
89 | - |
|
90 | - /** |
|
91 | - * Make a computer file size |
|
92 | - * @param string $str file size in human readable format |
|
93 | - * @return float|bool a file size in bytes |
|
94 | - * |
|
95 | - * Makes 2kB to 2048. |
|
96 | - * |
|
97 | - * Inspired by: http://www.php.net/manual/en/function.filesize.php#92418 |
|
98 | - */ |
|
99 | - public static function computerFileSize($str) { |
|
100 | - $str = strtolower($str); |
|
101 | - if (is_numeric($str)) { |
|
102 | - return (float)$str; |
|
103 | - } |
|
104 | - |
|
105 | - $bytes_array = [ |
|
106 | - 'b' => 1, |
|
107 | - 'k' => 1024, |
|
108 | - 'kb' => 1024, |
|
109 | - 'mb' => 1024 * 1024, |
|
110 | - 'm' => 1024 * 1024, |
|
111 | - 'gb' => 1024 * 1024 * 1024, |
|
112 | - 'g' => 1024 * 1024 * 1024, |
|
113 | - 'tb' => 1024 * 1024 * 1024 * 1024, |
|
114 | - 't' => 1024 * 1024 * 1024 * 1024, |
|
115 | - 'pb' => 1024 * 1024 * 1024 * 1024 * 1024, |
|
116 | - 'p' => 1024 * 1024 * 1024 * 1024 * 1024, |
|
117 | - ]; |
|
118 | - |
|
119 | - $bytes = (float)$str; |
|
120 | - |
|
121 | - if (preg_match('#([kmgtp]?b?)$#si', $str, $matches) && !empty($bytes_array[$matches[1]])) { |
|
122 | - $bytes *= $bytes_array[$matches[1]]; |
|
123 | - } else { |
|
124 | - return false; |
|
125 | - } |
|
126 | - |
|
127 | - $bytes = round($bytes); |
|
128 | - |
|
129 | - return $bytes; |
|
130 | - } |
|
131 | - |
|
132 | - /** |
|
133 | - * Recursive copying of folders |
|
134 | - * @param string $src source folder |
|
135 | - * @param string $dest target folder |
|
136 | - * |
|
137 | - */ |
|
138 | - static function copyr($src, $dest) { |
|
139 | - if (is_dir($src)) { |
|
140 | - if (!is_dir($dest)) { |
|
141 | - mkdir($dest); |
|
142 | - } |
|
143 | - $files = scandir($src); |
|
144 | - foreach ($files as $file) { |
|
145 | - if ($file != "." && $file != "..") { |
|
146 | - self::copyr("$src/$file", "$dest/$file"); |
|
147 | - } |
|
148 | - } |
|
149 | - } elseif (file_exists($src) && !\OC\Files\Filesystem::isFileBlacklisted($src)) { |
|
150 | - copy($src, $dest); |
|
151 | - } |
|
152 | - } |
|
153 | - |
|
154 | - /** |
|
155 | - * Recursive deletion of folders |
|
156 | - * @param string $dir path to the folder |
|
157 | - * @param bool $deleteSelf if set to false only the content of the folder will be deleted |
|
158 | - * @return bool |
|
159 | - */ |
|
160 | - static function rmdirr($dir, $deleteSelf = true) { |
|
161 | - if (is_dir($dir)) { |
|
162 | - $files = new RecursiveIteratorIterator( |
|
163 | - new RecursiveDirectoryIterator($dir, RecursiveDirectoryIterator::SKIP_DOTS), |
|
164 | - RecursiveIteratorIterator::CHILD_FIRST |
|
165 | - ); |
|
166 | - |
|
167 | - foreach ($files as $fileInfo) { |
|
168 | - /** @var SplFileInfo $fileInfo */ |
|
169 | - if ($fileInfo->isLink()) { |
|
170 | - unlink($fileInfo->getPathname()); |
|
171 | - } else if ($fileInfo->isDir()) { |
|
172 | - rmdir($fileInfo->getRealPath()); |
|
173 | - } else { |
|
174 | - unlink($fileInfo->getRealPath()); |
|
175 | - } |
|
176 | - } |
|
177 | - if ($deleteSelf) { |
|
178 | - rmdir($dir); |
|
179 | - } |
|
180 | - } elseif (file_exists($dir)) { |
|
181 | - if ($deleteSelf) { |
|
182 | - unlink($dir); |
|
183 | - } |
|
184 | - } |
|
185 | - if (!$deleteSelf) { |
|
186 | - return true; |
|
187 | - } |
|
188 | - |
|
189 | - return !file_exists($dir); |
|
190 | - } |
|
191 | - |
|
192 | - /** |
|
193 | - * @deprecated 18.0.0 |
|
194 | - * @return \OC\Files\Type\TemplateManager |
|
195 | - */ |
|
196 | - static public function getFileTemplateManager() { |
|
197 | - if (!self::$templateManager) { |
|
198 | - self::$templateManager = new \OC\Files\Type\TemplateManager(); |
|
199 | - } |
|
200 | - return self::$templateManager; |
|
201 | - } |
|
202 | - |
|
203 | - /** |
|
204 | - * detect if a given program is found in the search PATH |
|
205 | - * |
|
206 | - * @param string $name |
|
207 | - * @param bool $path |
|
208 | - * @internal param string $program name |
|
209 | - * @internal param string $optional search path, defaults to $PATH |
|
210 | - * @return bool true if executable program found in path |
|
211 | - */ |
|
212 | - public static function canExecute($name, $path = false) { |
|
213 | - // path defaults to PATH from environment if not set |
|
214 | - if ($path === false) { |
|
215 | - $path = getenv("PATH"); |
|
216 | - } |
|
217 | - // we look for an executable file of that name |
|
218 | - $exts = [""]; |
|
219 | - $check_fn = "is_executable"; |
|
220 | - // Default check will be done with $path directories : |
|
221 | - $dirs = explode(PATH_SEPARATOR, $path); |
|
222 | - // WARNING : We have to check if open_basedir is enabled : |
|
223 | - $obd = OC::$server->getIniWrapper()->getString('open_basedir'); |
|
224 | - if ($obd != "none") { |
|
225 | - $obd_values = explode(PATH_SEPARATOR, $obd); |
|
226 | - if (count($obd_values) > 0 and $obd_values[0]) { |
|
227 | - // open_basedir is in effect ! |
|
228 | - // We need to check if the program is in one of these dirs : |
|
229 | - $dirs = $obd_values; |
|
230 | - } |
|
231 | - } |
|
232 | - foreach ($dirs as $dir) { |
|
233 | - foreach ($exts as $ext) { |
|
234 | - if ($check_fn("$dir/$name" . $ext)) |
|
235 | - return true; |
|
236 | - } |
|
237 | - } |
|
238 | - return false; |
|
239 | - } |
|
240 | - |
|
241 | - /** |
|
242 | - * copy the contents of one stream to another |
|
243 | - * |
|
244 | - * @param resource $source |
|
245 | - * @param resource $target |
|
246 | - * @return array the number of bytes copied and result |
|
247 | - */ |
|
248 | - public static function streamCopy($source, $target) { |
|
249 | - if (!$source or !$target) { |
|
250 | - return [0, false]; |
|
251 | - } |
|
252 | - $bufSize = 8192; |
|
253 | - $result = true; |
|
254 | - $count = 0; |
|
255 | - while (!feof($source)) { |
|
256 | - $buf = fread($source, $bufSize); |
|
257 | - $bytesWritten = fwrite($target, $buf); |
|
258 | - if ($bytesWritten !== false) { |
|
259 | - $count += $bytesWritten; |
|
260 | - } |
|
261 | - // note: strlen is expensive so only use it when necessary, |
|
262 | - // on the last block |
|
263 | - if ($bytesWritten === false |
|
264 | - || ($bytesWritten < $bufSize && $bytesWritten < strlen($buf)) |
|
265 | - ) { |
|
266 | - // write error, could be disk full ? |
|
267 | - $result = false; |
|
268 | - break; |
|
269 | - } |
|
270 | - } |
|
271 | - return [$count, $result]; |
|
272 | - } |
|
273 | - |
|
274 | - /** |
|
275 | - * Adds a suffix to the name in case the file exists |
|
276 | - * |
|
277 | - * @param string $path |
|
278 | - * @param string $filename |
|
279 | - * @return string |
|
280 | - */ |
|
281 | - public static function buildNotExistingFileName($path, $filename) { |
|
282 | - $view = \OC\Files\Filesystem::getView(); |
|
283 | - return self::buildNotExistingFileNameForView($path, $filename, $view); |
|
284 | - } |
|
285 | - |
|
286 | - /** |
|
287 | - * Adds a suffix to the name in case the file exists |
|
288 | - * |
|
289 | - * @param string $path |
|
290 | - * @param string $filename |
|
291 | - * @return string |
|
292 | - */ |
|
293 | - public static function buildNotExistingFileNameForView($path, $filename, \OC\Files\View $view) { |
|
294 | - if ($path === '/') { |
|
295 | - $path = ''; |
|
296 | - } |
|
297 | - if ($pos = strrpos($filename, '.')) { |
|
298 | - $name = substr($filename, 0, $pos); |
|
299 | - $ext = substr($filename, $pos); |
|
300 | - } else { |
|
301 | - $name = $filename; |
|
302 | - $ext = ''; |
|
303 | - } |
|
304 | - |
|
305 | - $newpath = $path . '/' . $filename; |
|
306 | - if ($view->file_exists($newpath)) { |
|
307 | - if (preg_match_all('/\((\d+)\)/', $name, $matches, PREG_OFFSET_CAPTURE)) { |
|
308 | - //Replace the last "(number)" with "(number+1)" |
|
309 | - $last_match = count($matches[0]) - 1; |
|
310 | - $counter = $matches[1][$last_match][0] + 1; |
|
311 | - $offset = $matches[0][$last_match][1]; |
|
312 | - $match_length = strlen($matches[0][$last_match][0]); |
|
313 | - } else { |
|
314 | - $counter = 2; |
|
315 | - $match_length = 0; |
|
316 | - $offset = false; |
|
317 | - } |
|
318 | - do { |
|
319 | - if ($offset) { |
|
320 | - //Replace the last "(number)" with "(number+1)" |
|
321 | - $newname = substr_replace($name, '(' . $counter . ')', $offset, $match_length); |
|
322 | - } else { |
|
323 | - $newname = $name . ' (' . $counter . ')'; |
|
324 | - } |
|
325 | - $newpath = $path . '/' . $newname . $ext; |
|
326 | - $counter++; |
|
327 | - } while ($view->file_exists($newpath)); |
|
328 | - } |
|
329 | - |
|
330 | - return $newpath; |
|
331 | - } |
|
332 | - |
|
333 | - /** |
|
334 | - * Returns an array with all keys from input lowercased or uppercased. Numbered indices are left as is. |
|
335 | - * |
|
336 | - * @param array $input The array to work on |
|
337 | - * @param int $case Either MB_CASE_UPPER or MB_CASE_LOWER (default) |
|
338 | - * @param string $encoding The encoding parameter is the character encoding. Defaults to UTF-8 |
|
339 | - * @return array |
|
340 | - * |
|
341 | - * Returns an array with all keys from input lowercased or uppercased. Numbered indices are left as is. |
|
342 | - * based on http://www.php.net/manual/en/function.array-change-key-case.php#107715 |
|
343 | - * |
|
344 | - */ |
|
345 | - public static function mb_array_change_key_case($input, $case = MB_CASE_LOWER, $encoding = 'UTF-8') { |
|
346 | - $case = ($case != MB_CASE_UPPER) ? MB_CASE_LOWER : MB_CASE_UPPER; |
|
347 | - $ret = []; |
|
348 | - foreach ($input as $k => $v) { |
|
349 | - $ret[mb_convert_case($k, $case, $encoding)] = $v; |
|
350 | - } |
|
351 | - return $ret; |
|
352 | - } |
|
353 | - |
|
354 | - /** |
|
355 | - * performs a search in a nested array |
|
356 | - * @param array $haystack the array to be searched |
|
357 | - * @param string $needle the search string |
|
358 | - * @param mixed $index optional, only search this key name |
|
359 | - * @return mixed the key of the matching field, otherwise false |
|
360 | - * |
|
361 | - * performs a search in a nested array |
|
362 | - * |
|
363 | - * taken from http://www.php.net/manual/en/function.array-search.php#97645 |
|
364 | - */ |
|
365 | - public static function recursiveArraySearch($haystack, $needle, $index = null) { |
|
366 | - $aIt = new RecursiveArrayIterator($haystack); |
|
367 | - $it = new RecursiveIteratorIterator($aIt); |
|
368 | - |
|
369 | - while ($it->valid()) { |
|
370 | - if (((isset($index) and ($it->key() == $index)) or !isset($index)) and ($it->current() == $needle)) { |
|
371 | - return $aIt->key(); |
|
372 | - } |
|
373 | - |
|
374 | - $it->next(); |
|
375 | - } |
|
376 | - |
|
377 | - return false; |
|
378 | - } |
|
379 | - |
|
380 | - /** |
|
381 | - * calculates the maximum upload size respecting system settings, free space and user quota |
|
382 | - * |
|
383 | - * @param string $dir the current folder where the user currently operates |
|
384 | - * @param int $freeSpace the number of bytes free on the storage holding $dir, if not set this will be received from the storage directly |
|
385 | - * @return int number of bytes representing |
|
386 | - */ |
|
387 | - public static function maxUploadFilesize($dir, $freeSpace = null) { |
|
388 | - if (is_null($freeSpace) || $freeSpace < 0){ |
|
389 | - $freeSpace = self::freeSpace($dir); |
|
390 | - } |
|
391 | - return min($freeSpace, self::uploadLimit()); |
|
392 | - } |
|
393 | - |
|
394 | - /** |
|
395 | - * Calculate free space left within user quota |
|
396 | - * |
|
397 | - * @param string $dir the current folder where the user currently operates |
|
398 | - * @return int number of bytes representing |
|
399 | - */ |
|
400 | - public static function freeSpace($dir) { |
|
401 | - $freeSpace = \OC\Files\Filesystem::free_space($dir); |
|
402 | - if ($freeSpace < \OCP\Files\FileInfo::SPACE_UNLIMITED) { |
|
403 | - $freeSpace = max($freeSpace, 0); |
|
404 | - return $freeSpace; |
|
405 | - } else { |
|
406 | - return (INF > 0)? INF: PHP_INT_MAX; // work around https://bugs.php.net/bug.php?id=69188 |
|
407 | - } |
|
408 | - } |
|
409 | - |
|
410 | - /** |
|
411 | - * Calculate PHP upload limit |
|
412 | - * |
|
413 | - * @return int PHP upload file size limit |
|
414 | - */ |
|
415 | - public static function uploadLimit() { |
|
416 | - $ini = \OC::$server->getIniWrapper(); |
|
417 | - $upload_max_filesize = OCP\Util::computerFileSize($ini->get('upload_max_filesize')); |
|
418 | - $post_max_size = OCP\Util::computerFileSize($ini->get('post_max_size')); |
|
419 | - if ((int)$upload_max_filesize === 0 and (int)$post_max_size === 0) { |
|
420 | - return INF; |
|
421 | - } elseif ((int)$upload_max_filesize === 0 or (int)$post_max_size === 0) { |
|
422 | - return max($upload_max_filesize, $post_max_size); //only the non 0 value counts |
|
423 | - } else { |
|
424 | - return min($upload_max_filesize, $post_max_size); |
|
425 | - } |
|
426 | - } |
|
427 | - |
|
428 | - /** |
|
429 | - * Checks if a function is available |
|
430 | - * |
|
431 | - * @param string $function_name |
|
432 | - * @return bool |
|
433 | - */ |
|
434 | - public static function is_function_enabled($function_name) { |
|
435 | - if (!function_exists($function_name)) { |
|
436 | - return false; |
|
437 | - } |
|
438 | - $ini = \OC::$server->getIniWrapper(); |
|
439 | - $disabled = explode(',', $ini->get('disable_functions') ?: ''); |
|
440 | - $disabled = array_map('trim', $disabled); |
|
441 | - if (in_array($function_name, $disabled)) { |
|
442 | - return false; |
|
443 | - } |
|
444 | - $disabled = explode(',', $ini->get('suhosin.executor.func.blacklist') ?: ''); |
|
445 | - $disabled = array_map('trim', $disabled); |
|
446 | - if (in_array($function_name, $disabled)) { |
|
447 | - return false; |
|
448 | - } |
|
449 | - return true; |
|
450 | - } |
|
451 | - |
|
452 | - /** |
|
453 | - * Try to find a program |
|
454 | - * |
|
455 | - * @param string $program |
|
456 | - * @return null|string |
|
457 | - */ |
|
458 | - public static function findBinaryPath($program) { |
|
459 | - $memcache = \OC::$server->getMemCacheFactory()->createDistributed('findBinaryPath'); |
|
460 | - if ($memcache->hasKey($program)) { |
|
461 | - return $memcache->get($program); |
|
462 | - } |
|
463 | - $result = null; |
|
464 | - if (self::is_function_enabled('exec')) { |
|
465 | - $exeSniffer = new ExecutableFinder(); |
|
466 | - // Returns null if nothing is found |
|
467 | - $result = $exeSniffer->find($program, null, ['/usr/local/sbin', '/usr/local/bin', '/usr/sbin', '/usr/bin', '/sbin', '/bin', '/opt/bin']); |
|
468 | - } |
|
469 | - // store the value for 5 minutes |
|
470 | - $memcache->set($program, $result, 300); |
|
471 | - return $result; |
|
472 | - } |
|
473 | - |
|
474 | - /** |
|
475 | - * Calculate the disc space for the given path |
|
476 | - * |
|
477 | - * @param string $path |
|
478 | - * @param \OCP\Files\FileInfo $rootInfo (optional) |
|
479 | - * @return array |
|
480 | - * @throws \OCP\Files\NotFoundException |
|
481 | - */ |
|
482 | - public static function getStorageInfo($path, $rootInfo = null) { |
|
483 | - // return storage info without adding mount points |
|
484 | - $includeExtStorage = \OC::$server->getSystemConfig()->getValue('quota_include_external_storage', false); |
|
485 | - |
|
486 | - if (!$rootInfo) { |
|
487 | - $rootInfo = \OC\Files\Filesystem::getFileInfo($path, $includeExtStorage ? 'ext' : false); |
|
488 | - } |
|
489 | - if (!$rootInfo instanceof \OCP\Files\FileInfo) { |
|
490 | - throw new \OCP\Files\NotFoundException(); |
|
491 | - } |
|
492 | - $used = $rootInfo->getSize(); |
|
493 | - if ($used < 0) { |
|
494 | - $used = 0; |
|
495 | - } |
|
496 | - $quota = \OCP\Files\FileInfo::SPACE_UNLIMITED; |
|
497 | - $storage = $rootInfo->getStorage(); |
|
498 | - $sourceStorage = $storage; |
|
499 | - if ($storage->instanceOfStorage('\OCA\Files_Sharing\SharedStorage')) { |
|
500 | - $includeExtStorage = false; |
|
501 | - $sourceStorage = $storage->getSourceStorage(); |
|
502 | - } |
|
503 | - if ($includeExtStorage) { |
|
504 | - if ($storage->instanceOfStorage('\OC\Files\Storage\Home') |
|
505 | - || $storage->instanceOfStorage('\OC\Files\ObjectStore\HomeObjectStoreStorage') |
|
506 | - ) { |
|
507 | - /** @var \OC\Files\Storage\Home $storage */ |
|
508 | - $user = $storage->getUser(); |
|
509 | - } else { |
|
510 | - $user = \OC::$server->getUserSession()->getUser(); |
|
511 | - } |
|
512 | - $quota = OC_Util::getUserQuota($user); |
|
513 | - if ($quota !== \OCP\Files\FileInfo::SPACE_UNLIMITED) { |
|
514 | - // always get free space / total space from root + mount points |
|
515 | - return self::getGlobalStorageInfo($quota); |
|
516 | - } |
|
517 | - } |
|
518 | - |
|
519 | - // TODO: need a better way to get total space from storage |
|
520 | - if ($sourceStorage->instanceOfStorage('\OC\Files\Storage\Wrapper\Quota')) { |
|
521 | - /** @var \OC\Files\Storage\Wrapper\Quota $storage */ |
|
522 | - $quota = $sourceStorage->getQuota(); |
|
523 | - } |
|
524 | - $free = $sourceStorage->free_space($rootInfo->getInternalPath()); |
|
525 | - if ($free >= 0) { |
|
526 | - $total = $free + $used; |
|
527 | - } else { |
|
528 | - $total = $free; //either unknown or unlimited |
|
529 | - } |
|
530 | - if ($total > 0) { |
|
531 | - if ($quota > 0 && $total > $quota) { |
|
532 | - $total = $quota; |
|
533 | - } |
|
534 | - // prevent division by zero or error codes (negative values) |
|
535 | - $relative = round(($used / $total) * 10000) / 100; |
|
536 | - } else { |
|
537 | - $relative = 0; |
|
538 | - } |
|
539 | - |
|
540 | - $ownerId = $storage->getOwner($path); |
|
541 | - $ownerDisplayName = ''; |
|
542 | - $owner = \OC::$server->getUserManager()->get($ownerId); |
|
543 | - if($owner) { |
|
544 | - $ownerDisplayName = $owner->getDisplayName(); |
|
545 | - } |
|
546 | - |
|
547 | - return [ |
|
548 | - 'free' => $free, |
|
549 | - 'used' => $used, |
|
550 | - 'quota' => $quota, |
|
551 | - 'total' => $total, |
|
552 | - 'relative' => $relative, |
|
553 | - 'owner' => $ownerId, |
|
554 | - 'ownerDisplayName' => $ownerDisplayName, |
|
555 | - ]; |
|
556 | - } |
|
557 | - |
|
558 | - /** |
|
559 | - * Get storage info including all mount points and quota |
|
560 | - * |
|
561 | - * @param int $quota |
|
562 | - * @return array |
|
563 | - */ |
|
564 | - private static function getGlobalStorageInfo($quota) { |
|
565 | - $rootInfo = \OC\Files\Filesystem::getFileInfo('', 'ext'); |
|
566 | - $used = $rootInfo['size']; |
|
567 | - if ($used < 0) { |
|
568 | - $used = 0; |
|
569 | - } |
|
570 | - |
|
571 | - $total = $quota; |
|
572 | - $free = $quota - $used; |
|
573 | - |
|
574 | - if ($total > 0) { |
|
575 | - if ($quota > 0 && $total > $quota) { |
|
576 | - $total = $quota; |
|
577 | - } |
|
578 | - // prevent division by zero or error codes (negative values) |
|
579 | - $relative = round(($used / $total) * 10000) / 100; |
|
580 | - } else { |
|
581 | - $relative = 0; |
|
582 | - } |
|
583 | - |
|
584 | - return [ |
|
585 | - 'free' => $free, |
|
586 | - 'used' => $used, |
|
587 | - 'total' => $total, |
|
588 | - 'relative' => $relative, |
|
589 | - 'quota' => $quota |
|
590 | - ]; |
|
591 | - } |
|
592 | - |
|
593 | - /** |
|
594 | - * Returns whether the config file is set manually to read-only |
|
595 | - * @return bool |
|
596 | - */ |
|
597 | - public static function isReadOnlyConfigEnabled() { |
|
598 | - return \OC::$server->getConfig()->getSystemValue('config_is_read_only', false); |
|
599 | - } |
|
53 | + private static $templateManager; |
|
54 | + |
|
55 | + /** |
|
56 | + * Make a human file size |
|
57 | + * @param int $bytes file size in bytes |
|
58 | + * @return string a human readable file size |
|
59 | + * |
|
60 | + * Makes 2048 to 2 kB. |
|
61 | + */ |
|
62 | + public static function humanFileSize($bytes) { |
|
63 | + if ($bytes < 0) { |
|
64 | + return "?"; |
|
65 | + } |
|
66 | + if ($bytes < 1024) { |
|
67 | + return "$bytes B"; |
|
68 | + } |
|
69 | + $bytes = round($bytes / 1024, 0); |
|
70 | + if ($bytes < 1024) { |
|
71 | + return "$bytes KB"; |
|
72 | + } |
|
73 | + $bytes = round($bytes / 1024, 1); |
|
74 | + if ($bytes < 1024) { |
|
75 | + return "$bytes MB"; |
|
76 | + } |
|
77 | + $bytes = round($bytes / 1024, 1); |
|
78 | + if ($bytes < 1024) { |
|
79 | + return "$bytes GB"; |
|
80 | + } |
|
81 | + $bytes = round($bytes / 1024, 1); |
|
82 | + if ($bytes < 1024) { |
|
83 | + return "$bytes TB"; |
|
84 | + } |
|
85 | + |
|
86 | + $bytes = round($bytes / 1024, 1); |
|
87 | + return "$bytes PB"; |
|
88 | + } |
|
89 | + |
|
90 | + /** |
|
91 | + * Make a computer file size |
|
92 | + * @param string $str file size in human readable format |
|
93 | + * @return float|bool a file size in bytes |
|
94 | + * |
|
95 | + * Makes 2kB to 2048. |
|
96 | + * |
|
97 | + * Inspired by: http://www.php.net/manual/en/function.filesize.php#92418 |
|
98 | + */ |
|
99 | + public static function computerFileSize($str) { |
|
100 | + $str = strtolower($str); |
|
101 | + if (is_numeric($str)) { |
|
102 | + return (float)$str; |
|
103 | + } |
|
104 | + |
|
105 | + $bytes_array = [ |
|
106 | + 'b' => 1, |
|
107 | + 'k' => 1024, |
|
108 | + 'kb' => 1024, |
|
109 | + 'mb' => 1024 * 1024, |
|
110 | + 'm' => 1024 * 1024, |
|
111 | + 'gb' => 1024 * 1024 * 1024, |
|
112 | + 'g' => 1024 * 1024 * 1024, |
|
113 | + 'tb' => 1024 * 1024 * 1024 * 1024, |
|
114 | + 't' => 1024 * 1024 * 1024 * 1024, |
|
115 | + 'pb' => 1024 * 1024 * 1024 * 1024 * 1024, |
|
116 | + 'p' => 1024 * 1024 * 1024 * 1024 * 1024, |
|
117 | + ]; |
|
118 | + |
|
119 | + $bytes = (float)$str; |
|
120 | + |
|
121 | + if (preg_match('#([kmgtp]?b?)$#si', $str, $matches) && !empty($bytes_array[$matches[1]])) { |
|
122 | + $bytes *= $bytes_array[$matches[1]]; |
|
123 | + } else { |
|
124 | + return false; |
|
125 | + } |
|
126 | + |
|
127 | + $bytes = round($bytes); |
|
128 | + |
|
129 | + return $bytes; |
|
130 | + } |
|
131 | + |
|
132 | + /** |
|
133 | + * Recursive copying of folders |
|
134 | + * @param string $src source folder |
|
135 | + * @param string $dest target folder |
|
136 | + * |
|
137 | + */ |
|
138 | + static function copyr($src, $dest) { |
|
139 | + if (is_dir($src)) { |
|
140 | + if (!is_dir($dest)) { |
|
141 | + mkdir($dest); |
|
142 | + } |
|
143 | + $files = scandir($src); |
|
144 | + foreach ($files as $file) { |
|
145 | + if ($file != "." && $file != "..") { |
|
146 | + self::copyr("$src/$file", "$dest/$file"); |
|
147 | + } |
|
148 | + } |
|
149 | + } elseif (file_exists($src) && !\OC\Files\Filesystem::isFileBlacklisted($src)) { |
|
150 | + copy($src, $dest); |
|
151 | + } |
|
152 | + } |
|
153 | + |
|
154 | + /** |
|
155 | + * Recursive deletion of folders |
|
156 | + * @param string $dir path to the folder |
|
157 | + * @param bool $deleteSelf if set to false only the content of the folder will be deleted |
|
158 | + * @return bool |
|
159 | + */ |
|
160 | + static function rmdirr($dir, $deleteSelf = true) { |
|
161 | + if (is_dir($dir)) { |
|
162 | + $files = new RecursiveIteratorIterator( |
|
163 | + new RecursiveDirectoryIterator($dir, RecursiveDirectoryIterator::SKIP_DOTS), |
|
164 | + RecursiveIteratorIterator::CHILD_FIRST |
|
165 | + ); |
|
166 | + |
|
167 | + foreach ($files as $fileInfo) { |
|
168 | + /** @var SplFileInfo $fileInfo */ |
|
169 | + if ($fileInfo->isLink()) { |
|
170 | + unlink($fileInfo->getPathname()); |
|
171 | + } else if ($fileInfo->isDir()) { |
|
172 | + rmdir($fileInfo->getRealPath()); |
|
173 | + } else { |
|
174 | + unlink($fileInfo->getRealPath()); |
|
175 | + } |
|
176 | + } |
|
177 | + if ($deleteSelf) { |
|
178 | + rmdir($dir); |
|
179 | + } |
|
180 | + } elseif (file_exists($dir)) { |
|
181 | + if ($deleteSelf) { |
|
182 | + unlink($dir); |
|
183 | + } |
|
184 | + } |
|
185 | + if (!$deleteSelf) { |
|
186 | + return true; |
|
187 | + } |
|
188 | + |
|
189 | + return !file_exists($dir); |
|
190 | + } |
|
191 | + |
|
192 | + /** |
|
193 | + * @deprecated 18.0.0 |
|
194 | + * @return \OC\Files\Type\TemplateManager |
|
195 | + */ |
|
196 | + static public function getFileTemplateManager() { |
|
197 | + if (!self::$templateManager) { |
|
198 | + self::$templateManager = new \OC\Files\Type\TemplateManager(); |
|
199 | + } |
|
200 | + return self::$templateManager; |
|
201 | + } |
|
202 | + |
|
203 | + /** |
|
204 | + * detect if a given program is found in the search PATH |
|
205 | + * |
|
206 | + * @param string $name |
|
207 | + * @param bool $path |
|
208 | + * @internal param string $program name |
|
209 | + * @internal param string $optional search path, defaults to $PATH |
|
210 | + * @return bool true if executable program found in path |
|
211 | + */ |
|
212 | + public static function canExecute($name, $path = false) { |
|
213 | + // path defaults to PATH from environment if not set |
|
214 | + if ($path === false) { |
|
215 | + $path = getenv("PATH"); |
|
216 | + } |
|
217 | + // we look for an executable file of that name |
|
218 | + $exts = [""]; |
|
219 | + $check_fn = "is_executable"; |
|
220 | + // Default check will be done with $path directories : |
|
221 | + $dirs = explode(PATH_SEPARATOR, $path); |
|
222 | + // WARNING : We have to check if open_basedir is enabled : |
|
223 | + $obd = OC::$server->getIniWrapper()->getString('open_basedir'); |
|
224 | + if ($obd != "none") { |
|
225 | + $obd_values = explode(PATH_SEPARATOR, $obd); |
|
226 | + if (count($obd_values) > 0 and $obd_values[0]) { |
|
227 | + // open_basedir is in effect ! |
|
228 | + // We need to check if the program is in one of these dirs : |
|
229 | + $dirs = $obd_values; |
|
230 | + } |
|
231 | + } |
|
232 | + foreach ($dirs as $dir) { |
|
233 | + foreach ($exts as $ext) { |
|
234 | + if ($check_fn("$dir/$name" . $ext)) |
|
235 | + return true; |
|
236 | + } |
|
237 | + } |
|
238 | + return false; |
|
239 | + } |
|
240 | + |
|
241 | + /** |
|
242 | + * copy the contents of one stream to another |
|
243 | + * |
|
244 | + * @param resource $source |
|
245 | + * @param resource $target |
|
246 | + * @return array the number of bytes copied and result |
|
247 | + */ |
|
248 | + public static function streamCopy($source, $target) { |
|
249 | + if (!$source or !$target) { |
|
250 | + return [0, false]; |
|
251 | + } |
|
252 | + $bufSize = 8192; |
|
253 | + $result = true; |
|
254 | + $count = 0; |
|
255 | + while (!feof($source)) { |
|
256 | + $buf = fread($source, $bufSize); |
|
257 | + $bytesWritten = fwrite($target, $buf); |
|
258 | + if ($bytesWritten !== false) { |
|
259 | + $count += $bytesWritten; |
|
260 | + } |
|
261 | + // note: strlen is expensive so only use it when necessary, |
|
262 | + // on the last block |
|
263 | + if ($bytesWritten === false |
|
264 | + || ($bytesWritten < $bufSize && $bytesWritten < strlen($buf)) |
|
265 | + ) { |
|
266 | + // write error, could be disk full ? |
|
267 | + $result = false; |
|
268 | + break; |
|
269 | + } |
|
270 | + } |
|
271 | + return [$count, $result]; |
|
272 | + } |
|
273 | + |
|
274 | + /** |
|
275 | + * Adds a suffix to the name in case the file exists |
|
276 | + * |
|
277 | + * @param string $path |
|
278 | + * @param string $filename |
|
279 | + * @return string |
|
280 | + */ |
|
281 | + public static function buildNotExistingFileName($path, $filename) { |
|
282 | + $view = \OC\Files\Filesystem::getView(); |
|
283 | + return self::buildNotExistingFileNameForView($path, $filename, $view); |
|
284 | + } |
|
285 | + |
|
286 | + /** |
|
287 | + * Adds a suffix to the name in case the file exists |
|
288 | + * |
|
289 | + * @param string $path |
|
290 | + * @param string $filename |
|
291 | + * @return string |
|
292 | + */ |
|
293 | + public static function buildNotExistingFileNameForView($path, $filename, \OC\Files\View $view) { |
|
294 | + if ($path === '/') { |
|
295 | + $path = ''; |
|
296 | + } |
|
297 | + if ($pos = strrpos($filename, '.')) { |
|
298 | + $name = substr($filename, 0, $pos); |
|
299 | + $ext = substr($filename, $pos); |
|
300 | + } else { |
|
301 | + $name = $filename; |
|
302 | + $ext = ''; |
|
303 | + } |
|
304 | + |
|
305 | + $newpath = $path . '/' . $filename; |
|
306 | + if ($view->file_exists($newpath)) { |
|
307 | + if (preg_match_all('/\((\d+)\)/', $name, $matches, PREG_OFFSET_CAPTURE)) { |
|
308 | + //Replace the last "(number)" with "(number+1)" |
|
309 | + $last_match = count($matches[0]) - 1; |
|
310 | + $counter = $matches[1][$last_match][0] + 1; |
|
311 | + $offset = $matches[0][$last_match][1]; |
|
312 | + $match_length = strlen($matches[0][$last_match][0]); |
|
313 | + } else { |
|
314 | + $counter = 2; |
|
315 | + $match_length = 0; |
|
316 | + $offset = false; |
|
317 | + } |
|
318 | + do { |
|
319 | + if ($offset) { |
|
320 | + //Replace the last "(number)" with "(number+1)" |
|
321 | + $newname = substr_replace($name, '(' . $counter . ')', $offset, $match_length); |
|
322 | + } else { |
|
323 | + $newname = $name . ' (' . $counter . ')'; |
|
324 | + } |
|
325 | + $newpath = $path . '/' . $newname . $ext; |
|
326 | + $counter++; |
|
327 | + } while ($view->file_exists($newpath)); |
|
328 | + } |
|
329 | + |
|
330 | + return $newpath; |
|
331 | + } |
|
332 | + |
|
333 | + /** |
|
334 | + * Returns an array with all keys from input lowercased or uppercased. Numbered indices are left as is. |
|
335 | + * |
|
336 | + * @param array $input The array to work on |
|
337 | + * @param int $case Either MB_CASE_UPPER or MB_CASE_LOWER (default) |
|
338 | + * @param string $encoding The encoding parameter is the character encoding. Defaults to UTF-8 |
|
339 | + * @return array |
|
340 | + * |
|
341 | + * Returns an array with all keys from input lowercased or uppercased. Numbered indices are left as is. |
|
342 | + * based on http://www.php.net/manual/en/function.array-change-key-case.php#107715 |
|
343 | + * |
|
344 | + */ |
|
345 | + public static function mb_array_change_key_case($input, $case = MB_CASE_LOWER, $encoding = 'UTF-8') { |
|
346 | + $case = ($case != MB_CASE_UPPER) ? MB_CASE_LOWER : MB_CASE_UPPER; |
|
347 | + $ret = []; |
|
348 | + foreach ($input as $k => $v) { |
|
349 | + $ret[mb_convert_case($k, $case, $encoding)] = $v; |
|
350 | + } |
|
351 | + return $ret; |
|
352 | + } |
|
353 | + |
|
354 | + /** |
|
355 | + * performs a search in a nested array |
|
356 | + * @param array $haystack the array to be searched |
|
357 | + * @param string $needle the search string |
|
358 | + * @param mixed $index optional, only search this key name |
|
359 | + * @return mixed the key of the matching field, otherwise false |
|
360 | + * |
|
361 | + * performs a search in a nested array |
|
362 | + * |
|
363 | + * taken from http://www.php.net/manual/en/function.array-search.php#97645 |
|
364 | + */ |
|
365 | + public static function recursiveArraySearch($haystack, $needle, $index = null) { |
|
366 | + $aIt = new RecursiveArrayIterator($haystack); |
|
367 | + $it = new RecursiveIteratorIterator($aIt); |
|
368 | + |
|
369 | + while ($it->valid()) { |
|
370 | + if (((isset($index) and ($it->key() == $index)) or !isset($index)) and ($it->current() == $needle)) { |
|
371 | + return $aIt->key(); |
|
372 | + } |
|
373 | + |
|
374 | + $it->next(); |
|
375 | + } |
|
376 | + |
|
377 | + return false; |
|
378 | + } |
|
379 | + |
|
380 | + /** |
|
381 | + * calculates the maximum upload size respecting system settings, free space and user quota |
|
382 | + * |
|
383 | + * @param string $dir the current folder where the user currently operates |
|
384 | + * @param int $freeSpace the number of bytes free on the storage holding $dir, if not set this will be received from the storage directly |
|
385 | + * @return int number of bytes representing |
|
386 | + */ |
|
387 | + public static function maxUploadFilesize($dir, $freeSpace = null) { |
|
388 | + if (is_null($freeSpace) || $freeSpace < 0){ |
|
389 | + $freeSpace = self::freeSpace($dir); |
|
390 | + } |
|
391 | + return min($freeSpace, self::uploadLimit()); |
|
392 | + } |
|
393 | + |
|
394 | + /** |
|
395 | + * Calculate free space left within user quota |
|
396 | + * |
|
397 | + * @param string $dir the current folder where the user currently operates |
|
398 | + * @return int number of bytes representing |
|
399 | + */ |
|
400 | + public static function freeSpace($dir) { |
|
401 | + $freeSpace = \OC\Files\Filesystem::free_space($dir); |
|
402 | + if ($freeSpace < \OCP\Files\FileInfo::SPACE_UNLIMITED) { |
|
403 | + $freeSpace = max($freeSpace, 0); |
|
404 | + return $freeSpace; |
|
405 | + } else { |
|
406 | + return (INF > 0)? INF: PHP_INT_MAX; // work around https://bugs.php.net/bug.php?id=69188 |
|
407 | + } |
|
408 | + } |
|
409 | + |
|
410 | + /** |
|
411 | + * Calculate PHP upload limit |
|
412 | + * |
|
413 | + * @return int PHP upload file size limit |
|
414 | + */ |
|
415 | + public static function uploadLimit() { |
|
416 | + $ini = \OC::$server->getIniWrapper(); |
|
417 | + $upload_max_filesize = OCP\Util::computerFileSize($ini->get('upload_max_filesize')); |
|
418 | + $post_max_size = OCP\Util::computerFileSize($ini->get('post_max_size')); |
|
419 | + if ((int)$upload_max_filesize === 0 and (int)$post_max_size === 0) { |
|
420 | + return INF; |
|
421 | + } elseif ((int)$upload_max_filesize === 0 or (int)$post_max_size === 0) { |
|
422 | + return max($upload_max_filesize, $post_max_size); //only the non 0 value counts |
|
423 | + } else { |
|
424 | + return min($upload_max_filesize, $post_max_size); |
|
425 | + } |
|
426 | + } |
|
427 | + |
|
428 | + /** |
|
429 | + * Checks if a function is available |
|
430 | + * |
|
431 | + * @param string $function_name |
|
432 | + * @return bool |
|
433 | + */ |
|
434 | + public static function is_function_enabled($function_name) { |
|
435 | + if (!function_exists($function_name)) { |
|
436 | + return false; |
|
437 | + } |
|
438 | + $ini = \OC::$server->getIniWrapper(); |
|
439 | + $disabled = explode(',', $ini->get('disable_functions') ?: ''); |
|
440 | + $disabled = array_map('trim', $disabled); |
|
441 | + if (in_array($function_name, $disabled)) { |
|
442 | + return false; |
|
443 | + } |
|
444 | + $disabled = explode(',', $ini->get('suhosin.executor.func.blacklist') ?: ''); |
|
445 | + $disabled = array_map('trim', $disabled); |
|
446 | + if (in_array($function_name, $disabled)) { |
|
447 | + return false; |
|
448 | + } |
|
449 | + return true; |
|
450 | + } |
|
451 | + |
|
452 | + /** |
|
453 | + * Try to find a program |
|
454 | + * |
|
455 | + * @param string $program |
|
456 | + * @return null|string |
|
457 | + */ |
|
458 | + public static function findBinaryPath($program) { |
|
459 | + $memcache = \OC::$server->getMemCacheFactory()->createDistributed('findBinaryPath'); |
|
460 | + if ($memcache->hasKey($program)) { |
|
461 | + return $memcache->get($program); |
|
462 | + } |
|
463 | + $result = null; |
|
464 | + if (self::is_function_enabled('exec')) { |
|
465 | + $exeSniffer = new ExecutableFinder(); |
|
466 | + // Returns null if nothing is found |
|
467 | + $result = $exeSniffer->find($program, null, ['/usr/local/sbin', '/usr/local/bin', '/usr/sbin', '/usr/bin', '/sbin', '/bin', '/opt/bin']); |
|
468 | + } |
|
469 | + // store the value for 5 minutes |
|
470 | + $memcache->set($program, $result, 300); |
|
471 | + return $result; |
|
472 | + } |
|
473 | + |
|
474 | + /** |
|
475 | + * Calculate the disc space for the given path |
|
476 | + * |
|
477 | + * @param string $path |
|
478 | + * @param \OCP\Files\FileInfo $rootInfo (optional) |
|
479 | + * @return array |
|
480 | + * @throws \OCP\Files\NotFoundException |
|
481 | + */ |
|
482 | + public static function getStorageInfo($path, $rootInfo = null) { |
|
483 | + // return storage info without adding mount points |
|
484 | + $includeExtStorage = \OC::$server->getSystemConfig()->getValue('quota_include_external_storage', false); |
|
485 | + |
|
486 | + if (!$rootInfo) { |
|
487 | + $rootInfo = \OC\Files\Filesystem::getFileInfo($path, $includeExtStorage ? 'ext' : false); |
|
488 | + } |
|
489 | + if (!$rootInfo instanceof \OCP\Files\FileInfo) { |
|
490 | + throw new \OCP\Files\NotFoundException(); |
|
491 | + } |
|
492 | + $used = $rootInfo->getSize(); |
|
493 | + if ($used < 0) { |
|
494 | + $used = 0; |
|
495 | + } |
|
496 | + $quota = \OCP\Files\FileInfo::SPACE_UNLIMITED; |
|
497 | + $storage = $rootInfo->getStorage(); |
|
498 | + $sourceStorage = $storage; |
|
499 | + if ($storage->instanceOfStorage('\OCA\Files_Sharing\SharedStorage')) { |
|
500 | + $includeExtStorage = false; |
|
501 | + $sourceStorage = $storage->getSourceStorage(); |
|
502 | + } |
|
503 | + if ($includeExtStorage) { |
|
504 | + if ($storage->instanceOfStorage('\OC\Files\Storage\Home') |
|
505 | + || $storage->instanceOfStorage('\OC\Files\ObjectStore\HomeObjectStoreStorage') |
|
506 | + ) { |
|
507 | + /** @var \OC\Files\Storage\Home $storage */ |
|
508 | + $user = $storage->getUser(); |
|
509 | + } else { |
|
510 | + $user = \OC::$server->getUserSession()->getUser(); |
|
511 | + } |
|
512 | + $quota = OC_Util::getUserQuota($user); |
|
513 | + if ($quota !== \OCP\Files\FileInfo::SPACE_UNLIMITED) { |
|
514 | + // always get free space / total space from root + mount points |
|
515 | + return self::getGlobalStorageInfo($quota); |
|
516 | + } |
|
517 | + } |
|
518 | + |
|
519 | + // TODO: need a better way to get total space from storage |
|
520 | + if ($sourceStorage->instanceOfStorage('\OC\Files\Storage\Wrapper\Quota')) { |
|
521 | + /** @var \OC\Files\Storage\Wrapper\Quota $storage */ |
|
522 | + $quota = $sourceStorage->getQuota(); |
|
523 | + } |
|
524 | + $free = $sourceStorage->free_space($rootInfo->getInternalPath()); |
|
525 | + if ($free >= 0) { |
|
526 | + $total = $free + $used; |
|
527 | + } else { |
|
528 | + $total = $free; //either unknown or unlimited |
|
529 | + } |
|
530 | + if ($total > 0) { |
|
531 | + if ($quota > 0 && $total > $quota) { |
|
532 | + $total = $quota; |
|
533 | + } |
|
534 | + // prevent division by zero or error codes (negative values) |
|
535 | + $relative = round(($used / $total) * 10000) / 100; |
|
536 | + } else { |
|
537 | + $relative = 0; |
|
538 | + } |
|
539 | + |
|
540 | + $ownerId = $storage->getOwner($path); |
|
541 | + $ownerDisplayName = ''; |
|
542 | + $owner = \OC::$server->getUserManager()->get($ownerId); |
|
543 | + if($owner) { |
|
544 | + $ownerDisplayName = $owner->getDisplayName(); |
|
545 | + } |
|
546 | + |
|
547 | + return [ |
|
548 | + 'free' => $free, |
|
549 | + 'used' => $used, |
|
550 | + 'quota' => $quota, |
|
551 | + 'total' => $total, |
|
552 | + 'relative' => $relative, |
|
553 | + 'owner' => $ownerId, |
|
554 | + 'ownerDisplayName' => $ownerDisplayName, |
|
555 | + ]; |
|
556 | + } |
|
557 | + |
|
558 | + /** |
|
559 | + * Get storage info including all mount points and quota |
|
560 | + * |
|
561 | + * @param int $quota |
|
562 | + * @return array |
|
563 | + */ |
|
564 | + private static function getGlobalStorageInfo($quota) { |
|
565 | + $rootInfo = \OC\Files\Filesystem::getFileInfo('', 'ext'); |
|
566 | + $used = $rootInfo['size']; |
|
567 | + if ($used < 0) { |
|
568 | + $used = 0; |
|
569 | + } |
|
570 | + |
|
571 | + $total = $quota; |
|
572 | + $free = $quota - $used; |
|
573 | + |
|
574 | + if ($total > 0) { |
|
575 | + if ($quota > 0 && $total > $quota) { |
|
576 | + $total = $quota; |
|
577 | + } |
|
578 | + // prevent division by zero or error codes (negative values) |
|
579 | + $relative = round(($used / $total) * 10000) / 100; |
|
580 | + } else { |
|
581 | + $relative = 0; |
|
582 | + } |
|
583 | + |
|
584 | + return [ |
|
585 | + 'free' => $free, |
|
586 | + 'used' => $used, |
|
587 | + 'total' => $total, |
|
588 | + 'relative' => $relative, |
|
589 | + 'quota' => $quota |
|
590 | + ]; |
|
591 | + } |
|
592 | + |
|
593 | + /** |
|
594 | + * Returns whether the config file is set manually to read-only |
|
595 | + * @return bool |
|
596 | + */ |
|
597 | + public static function isReadOnlyConfigEnabled() { |
|
598 | + return \OC::$server->getConfig()->getSystemValue('config_is_read_only', false); |
|
599 | + } |
|
600 | 600 | } |
@@ -58,354 +58,354 @@ |
||
58 | 58 | */ |
59 | 59 | class OC_User { |
60 | 60 | |
61 | - private static $_usedBackends = []; |
|
62 | - |
|
63 | - private static $_setupedBackends = []; |
|
64 | - |
|
65 | - // bool, stores if a user want to access a resource anonymously, e.g if they open a public link |
|
66 | - private static $incognitoMode = false; |
|
67 | - |
|
68 | - /** |
|
69 | - * Adds the backend to the list of used backends |
|
70 | - * |
|
71 | - * @param string|\OCP\UserInterface $backend default: database The backend to use for user management |
|
72 | - * @return bool |
|
73 | - * |
|
74 | - * Set the User Authentication Module |
|
75 | - * @suppress PhanDeprecatedFunction |
|
76 | - */ |
|
77 | - public static function useBackend($backend = 'database') { |
|
78 | - if ($backend instanceof \OCP\UserInterface) { |
|
79 | - self::$_usedBackends[get_class($backend)] = $backend; |
|
80 | - \OC::$server->getUserManager()->registerBackend($backend); |
|
81 | - } else { |
|
82 | - // You'll never know what happens |
|
83 | - if (null === $backend or !is_string($backend)) { |
|
84 | - $backend = 'database'; |
|
85 | - } |
|
86 | - |
|
87 | - // Load backend |
|
88 | - switch ($backend) { |
|
89 | - case 'database': |
|
90 | - case 'mysql': |
|
91 | - case 'sqlite': |
|
92 | - \OCP\Util::writeLog('core', 'Adding user backend ' . $backend . '.', ILogger::DEBUG); |
|
93 | - self::$_usedBackends[$backend] = new \OC\User\Database(); |
|
94 | - \OC::$server->getUserManager()->registerBackend(self::$_usedBackends[$backend]); |
|
95 | - break; |
|
96 | - case 'dummy': |
|
97 | - self::$_usedBackends[$backend] = new \Test\Util\User\Dummy(); |
|
98 | - \OC::$server->getUserManager()->registerBackend(self::$_usedBackends[$backend]); |
|
99 | - break; |
|
100 | - default: |
|
101 | - \OCP\Util::writeLog('core', 'Adding default user backend ' . $backend . '.', ILogger::DEBUG); |
|
102 | - $className = 'OC_USER_' . strtoupper($backend); |
|
103 | - self::$_usedBackends[$backend] = new $className(); |
|
104 | - \OC::$server->getUserManager()->registerBackend(self::$_usedBackends[$backend]); |
|
105 | - break; |
|
106 | - } |
|
107 | - } |
|
108 | - return true; |
|
109 | - } |
|
110 | - |
|
111 | - /** |
|
112 | - * remove all used backends |
|
113 | - */ |
|
114 | - public static function clearBackends() { |
|
115 | - self::$_usedBackends = []; |
|
116 | - \OC::$server->getUserManager()->clearBackends(); |
|
117 | - } |
|
118 | - |
|
119 | - /** |
|
120 | - * setup the configured backends in config.php |
|
121 | - * @suppress PhanDeprecatedFunction |
|
122 | - */ |
|
123 | - public static function setupBackends() { |
|
124 | - OC_App::loadApps(['prelogin']); |
|
125 | - $backends = \OC::$server->getSystemConfig()->getValue('user_backends', []); |
|
126 | - if (isset($backends['default']) && !$backends['default']) { |
|
127 | - // clear default backends |
|
128 | - self::clearBackends(); |
|
129 | - } |
|
130 | - foreach ($backends as $i => $config) { |
|
131 | - if (!is_array($config)) { |
|
132 | - continue; |
|
133 | - } |
|
134 | - $class = $config['class']; |
|
135 | - $arguments = $config['arguments']; |
|
136 | - if (class_exists($class)) { |
|
137 | - if (array_search($i, self::$_setupedBackends) === false) { |
|
138 | - // make a reflection object |
|
139 | - $reflectionObj = new ReflectionClass($class); |
|
140 | - |
|
141 | - // use Reflection to create a new instance, using the $args |
|
142 | - $backend = $reflectionObj->newInstanceArgs($arguments); |
|
143 | - self::useBackend($backend); |
|
144 | - self::$_setupedBackends[] = $i; |
|
145 | - } else { |
|
146 | - \OCP\Util::writeLog('core', 'User backend ' . $class . ' already initialized.', ILogger::DEBUG); |
|
147 | - } |
|
148 | - } else { |
|
149 | - \OCP\Util::writeLog('core', 'User backend ' . $class . ' not found.', ILogger::ERROR); |
|
150 | - } |
|
151 | - } |
|
152 | - } |
|
153 | - |
|
154 | - /** |
|
155 | - * Try to login a user, assuming authentication |
|
156 | - * has already happened (e.g. via Single Sign On). |
|
157 | - * |
|
158 | - * Log in a user and regenerate a new session. |
|
159 | - * |
|
160 | - * @param \OCP\Authentication\IApacheBackend $backend |
|
161 | - * @return bool |
|
162 | - */ |
|
163 | - public static function loginWithApache(\OCP\Authentication\IApacheBackend $backend) { |
|
164 | - |
|
165 | - $uid = $backend->getCurrentUserId(); |
|
166 | - $run = true; |
|
167 | - OC_Hook::emit("OC_User", "pre_login", ["run" => &$run, "uid" => $uid, 'backend' => $backend]); |
|
168 | - |
|
169 | - if ($uid) { |
|
170 | - if (self::getUser() !== $uid) { |
|
171 | - self::setUserId($uid); |
|
172 | - $userSession = \OC::$server->getUserSession(); |
|
173 | - $userSession->setLoginName($uid); |
|
174 | - $request = OC::$server->getRequest(); |
|
175 | - $userSession->createSessionToken($request, $uid, $uid); |
|
176 | - // setup the filesystem |
|
177 | - OC_Util::setupFS($uid); |
|
178 | - // first call the post_login hooks, the login-process needs to be |
|
179 | - // completed before we can safely create the users folder. |
|
180 | - // For example encryption needs to initialize the users keys first |
|
181 | - // before we can create the user folder with the skeleton files |
|
182 | - OC_Hook::emit( |
|
183 | - 'OC_User', |
|
184 | - 'post_login', |
|
185 | - [ |
|
186 | - 'uid' => $uid, |
|
187 | - 'password' => '', |
|
188 | - 'isTokenLogin' => false, |
|
189 | - ] |
|
190 | - ); |
|
191 | - //trigger creation of user home and /files folder |
|
192 | - \OC::$server->getUserFolder($uid); |
|
193 | - } |
|
194 | - return true; |
|
195 | - } |
|
196 | - return false; |
|
197 | - } |
|
198 | - |
|
199 | - /** |
|
200 | - * Verify with Apache whether user is authenticated. |
|
201 | - * |
|
202 | - * @return boolean|null |
|
203 | - * true: authenticated |
|
204 | - * false: not authenticated |
|
205 | - * null: not handled / no backend available |
|
206 | - */ |
|
207 | - public static function handleApacheAuth() { |
|
208 | - $backend = self::findFirstActiveUsedBackend(); |
|
209 | - if ($backend) { |
|
210 | - OC_App::loadApps(); |
|
211 | - |
|
212 | - //setup extra user backends |
|
213 | - self::setupBackends(); |
|
214 | - \OC::$server->getUserSession()->unsetMagicInCookie(); |
|
215 | - |
|
216 | - return self::loginWithApache($backend); |
|
217 | - } |
|
218 | - |
|
219 | - return null; |
|
220 | - } |
|
221 | - |
|
222 | - |
|
223 | - /** |
|
224 | - * Sets user id for session and triggers emit |
|
225 | - * |
|
226 | - * @param string $uid |
|
227 | - */ |
|
228 | - public static function setUserId($uid) { |
|
229 | - $userSession = \OC::$server->getUserSession(); |
|
230 | - $userManager = \OC::$server->getUserManager(); |
|
231 | - if ($user = $userManager->get($uid)) { |
|
232 | - $userSession->setUser($user); |
|
233 | - } else { |
|
234 | - \OC::$server->getSession()->set('user_id', $uid); |
|
235 | - } |
|
236 | - } |
|
237 | - |
|
238 | - /** |
|
239 | - * Check if the user is logged in, considers also the HTTP basic credentials |
|
240 | - * |
|
241 | - * @deprecated use \OC::$server->getUserSession()->isLoggedIn() |
|
242 | - * @return bool |
|
243 | - */ |
|
244 | - public static function isLoggedIn() { |
|
245 | - return \OC::$server->getUserSession()->isLoggedIn(); |
|
246 | - } |
|
247 | - |
|
248 | - /** |
|
249 | - * set incognito mode, e.g. if a user wants to open a public link |
|
250 | - * |
|
251 | - * @param bool $status |
|
252 | - */ |
|
253 | - public static function setIncognitoMode($status) { |
|
254 | - self::$incognitoMode = $status; |
|
255 | - } |
|
256 | - |
|
257 | - /** |
|
258 | - * get incognito mode status |
|
259 | - * |
|
260 | - * @return bool |
|
261 | - */ |
|
262 | - public static function isIncognitoMode() { |
|
263 | - return self::$incognitoMode; |
|
264 | - } |
|
265 | - |
|
266 | - /** |
|
267 | - * Returns the current logout URL valid for the currently logged-in user |
|
268 | - * |
|
269 | - * @param \OCP\IURLGenerator $urlGenerator |
|
270 | - * @return string |
|
271 | - */ |
|
272 | - public static function getLogoutUrl(\OCP\IURLGenerator $urlGenerator) { |
|
273 | - $backend = self::findFirstActiveUsedBackend(); |
|
274 | - if ($backend) { |
|
275 | - return $backend->getLogoutUrl(); |
|
276 | - } |
|
277 | - |
|
278 | - $logoutUrl = $urlGenerator->linkToRoute('core.login.logout'); |
|
279 | - $logoutUrl .= '?requesttoken=' . urlencode(\OCP\Util::callRegister()); |
|
280 | - |
|
281 | - return $logoutUrl; |
|
282 | - } |
|
283 | - |
|
284 | - /** |
|
285 | - * Check if the user is an admin user |
|
286 | - * |
|
287 | - * @param string $uid uid of the admin |
|
288 | - * @return bool |
|
289 | - */ |
|
290 | - public static function isAdminUser($uid) { |
|
291 | - $group = \OC::$server->getGroupManager()->get('admin'); |
|
292 | - $user = \OC::$server->getUserManager()->get($uid); |
|
293 | - if ($group && $user && $group->inGroup($user) && self::$incognitoMode === false) { |
|
294 | - return true; |
|
295 | - } |
|
296 | - return false; |
|
297 | - } |
|
298 | - |
|
299 | - |
|
300 | - /** |
|
301 | - * get the user id of the user currently logged in. |
|
302 | - * |
|
303 | - * @return string|bool uid or false |
|
304 | - */ |
|
305 | - public static function getUser() { |
|
306 | - $uid = \OC::$server->getSession() ? \OC::$server->getSession()->get('user_id') : null; |
|
307 | - if (!is_null($uid) && self::$incognitoMode === false) { |
|
308 | - return $uid; |
|
309 | - } else { |
|
310 | - return false; |
|
311 | - } |
|
312 | - } |
|
313 | - |
|
314 | - /** |
|
315 | - * get the display name of the user currently logged in. |
|
316 | - * |
|
317 | - * @param string $uid |
|
318 | - * @return string|bool uid or false |
|
319 | - * @deprecated 8.1.0 fetch \OCP\IUser (has getDisplayName()) by using method |
|
320 | - * get() of \OCP\IUserManager - \OC::$server->getUserManager() |
|
321 | - */ |
|
322 | - public static function getDisplayName($uid = null) { |
|
323 | - if ($uid) { |
|
324 | - $user = \OC::$server->getUserManager()->get($uid); |
|
325 | - if ($user) { |
|
326 | - return $user->getDisplayName(); |
|
327 | - } else { |
|
328 | - return $uid; |
|
329 | - } |
|
330 | - } else { |
|
331 | - $user = \OC::$server->getUserSession()->getUser(); |
|
332 | - if ($user) { |
|
333 | - return $user->getDisplayName(); |
|
334 | - } else { |
|
335 | - return false; |
|
336 | - } |
|
337 | - } |
|
338 | - } |
|
339 | - |
|
340 | - /** |
|
341 | - * Set password |
|
342 | - * |
|
343 | - * @param string $uid The username |
|
344 | - * @param string $password The new password |
|
345 | - * @param string $recoveryPassword for the encryption app to reset encryption keys |
|
346 | - * @return bool |
|
347 | - * |
|
348 | - * Change the password of a user |
|
349 | - */ |
|
350 | - public static function setPassword($uid, $password, $recoveryPassword = null) { |
|
351 | - $user = \OC::$server->getUserManager()->get($uid); |
|
352 | - if ($user) { |
|
353 | - return $user->setPassword($password, $recoveryPassword); |
|
354 | - } else { |
|
355 | - return false; |
|
356 | - } |
|
357 | - } |
|
358 | - |
|
359 | - /** |
|
360 | - * @param string $uid The username |
|
361 | - * @return string |
|
362 | - * |
|
363 | - * returns the path to the users home directory |
|
364 | - * @deprecated Use \OC::$server->getUserManager->getHome() |
|
365 | - */ |
|
366 | - public static function getHome($uid) { |
|
367 | - $user = \OC::$server->getUserManager()->get($uid); |
|
368 | - if ($user) { |
|
369 | - return $user->getHome(); |
|
370 | - } else { |
|
371 | - return \OC::$server->getSystemConfig()->getValue('datadirectory', OC::$SERVERROOT . '/data') . '/' . $uid; |
|
372 | - } |
|
373 | - } |
|
374 | - |
|
375 | - /** |
|
376 | - * Get a list of all users display name |
|
377 | - * |
|
378 | - * @param string $search |
|
379 | - * @param int $limit |
|
380 | - * @param int $offset |
|
381 | - * @return array associative array with all display names (value) and corresponding uids (key) |
|
382 | - * |
|
383 | - * Get a list of all display names and user ids. |
|
384 | - * @deprecated Use \OC::$server->getUserManager->searchDisplayName($search, $limit, $offset) instead. |
|
385 | - */ |
|
386 | - public static function getDisplayNames($search = '', $limit = null, $offset = null) { |
|
387 | - $displayNames = []; |
|
388 | - $users = \OC::$server->getUserManager()->searchDisplayName($search, $limit, $offset); |
|
389 | - foreach ($users as $user) { |
|
390 | - $displayNames[$user->getUID()] = $user->getDisplayName(); |
|
391 | - } |
|
392 | - return $displayNames; |
|
393 | - } |
|
394 | - |
|
395 | - /** |
|
396 | - * Returns the first active backend from self::$_usedBackends. |
|
397 | - * |
|
398 | - * @return OCP\Authentication\IApacheBackend|null if no backend active, otherwise OCP\Authentication\IApacheBackend |
|
399 | - */ |
|
400 | - private static function findFirstActiveUsedBackend() { |
|
401 | - foreach (self::$_usedBackends as $backend) { |
|
402 | - if ($backend instanceof OCP\Authentication\IApacheBackend) { |
|
403 | - if ($backend->isSessionActive()) { |
|
404 | - return $backend; |
|
405 | - } |
|
406 | - } |
|
407 | - } |
|
408 | - |
|
409 | - return null; |
|
410 | - } |
|
61 | + private static $_usedBackends = []; |
|
62 | + |
|
63 | + private static $_setupedBackends = []; |
|
64 | + |
|
65 | + // bool, stores if a user want to access a resource anonymously, e.g if they open a public link |
|
66 | + private static $incognitoMode = false; |
|
67 | + |
|
68 | + /** |
|
69 | + * Adds the backend to the list of used backends |
|
70 | + * |
|
71 | + * @param string|\OCP\UserInterface $backend default: database The backend to use for user management |
|
72 | + * @return bool |
|
73 | + * |
|
74 | + * Set the User Authentication Module |
|
75 | + * @suppress PhanDeprecatedFunction |
|
76 | + */ |
|
77 | + public static function useBackend($backend = 'database') { |
|
78 | + if ($backend instanceof \OCP\UserInterface) { |
|
79 | + self::$_usedBackends[get_class($backend)] = $backend; |
|
80 | + \OC::$server->getUserManager()->registerBackend($backend); |
|
81 | + } else { |
|
82 | + // You'll never know what happens |
|
83 | + if (null === $backend or !is_string($backend)) { |
|
84 | + $backend = 'database'; |
|
85 | + } |
|
86 | + |
|
87 | + // Load backend |
|
88 | + switch ($backend) { |
|
89 | + case 'database': |
|
90 | + case 'mysql': |
|
91 | + case 'sqlite': |
|
92 | + \OCP\Util::writeLog('core', 'Adding user backend ' . $backend . '.', ILogger::DEBUG); |
|
93 | + self::$_usedBackends[$backend] = new \OC\User\Database(); |
|
94 | + \OC::$server->getUserManager()->registerBackend(self::$_usedBackends[$backend]); |
|
95 | + break; |
|
96 | + case 'dummy': |
|
97 | + self::$_usedBackends[$backend] = new \Test\Util\User\Dummy(); |
|
98 | + \OC::$server->getUserManager()->registerBackend(self::$_usedBackends[$backend]); |
|
99 | + break; |
|
100 | + default: |
|
101 | + \OCP\Util::writeLog('core', 'Adding default user backend ' . $backend . '.', ILogger::DEBUG); |
|
102 | + $className = 'OC_USER_' . strtoupper($backend); |
|
103 | + self::$_usedBackends[$backend] = new $className(); |
|
104 | + \OC::$server->getUserManager()->registerBackend(self::$_usedBackends[$backend]); |
|
105 | + break; |
|
106 | + } |
|
107 | + } |
|
108 | + return true; |
|
109 | + } |
|
110 | + |
|
111 | + /** |
|
112 | + * remove all used backends |
|
113 | + */ |
|
114 | + public static function clearBackends() { |
|
115 | + self::$_usedBackends = []; |
|
116 | + \OC::$server->getUserManager()->clearBackends(); |
|
117 | + } |
|
118 | + |
|
119 | + /** |
|
120 | + * setup the configured backends in config.php |
|
121 | + * @suppress PhanDeprecatedFunction |
|
122 | + */ |
|
123 | + public static function setupBackends() { |
|
124 | + OC_App::loadApps(['prelogin']); |
|
125 | + $backends = \OC::$server->getSystemConfig()->getValue('user_backends', []); |
|
126 | + if (isset($backends['default']) && !$backends['default']) { |
|
127 | + // clear default backends |
|
128 | + self::clearBackends(); |
|
129 | + } |
|
130 | + foreach ($backends as $i => $config) { |
|
131 | + if (!is_array($config)) { |
|
132 | + continue; |
|
133 | + } |
|
134 | + $class = $config['class']; |
|
135 | + $arguments = $config['arguments']; |
|
136 | + if (class_exists($class)) { |
|
137 | + if (array_search($i, self::$_setupedBackends) === false) { |
|
138 | + // make a reflection object |
|
139 | + $reflectionObj = new ReflectionClass($class); |
|
140 | + |
|
141 | + // use Reflection to create a new instance, using the $args |
|
142 | + $backend = $reflectionObj->newInstanceArgs($arguments); |
|
143 | + self::useBackend($backend); |
|
144 | + self::$_setupedBackends[] = $i; |
|
145 | + } else { |
|
146 | + \OCP\Util::writeLog('core', 'User backend ' . $class . ' already initialized.', ILogger::DEBUG); |
|
147 | + } |
|
148 | + } else { |
|
149 | + \OCP\Util::writeLog('core', 'User backend ' . $class . ' not found.', ILogger::ERROR); |
|
150 | + } |
|
151 | + } |
|
152 | + } |
|
153 | + |
|
154 | + /** |
|
155 | + * Try to login a user, assuming authentication |
|
156 | + * has already happened (e.g. via Single Sign On). |
|
157 | + * |
|
158 | + * Log in a user and regenerate a new session. |
|
159 | + * |
|
160 | + * @param \OCP\Authentication\IApacheBackend $backend |
|
161 | + * @return bool |
|
162 | + */ |
|
163 | + public static function loginWithApache(\OCP\Authentication\IApacheBackend $backend) { |
|
164 | + |
|
165 | + $uid = $backend->getCurrentUserId(); |
|
166 | + $run = true; |
|
167 | + OC_Hook::emit("OC_User", "pre_login", ["run" => &$run, "uid" => $uid, 'backend' => $backend]); |
|
168 | + |
|
169 | + if ($uid) { |
|
170 | + if (self::getUser() !== $uid) { |
|
171 | + self::setUserId($uid); |
|
172 | + $userSession = \OC::$server->getUserSession(); |
|
173 | + $userSession->setLoginName($uid); |
|
174 | + $request = OC::$server->getRequest(); |
|
175 | + $userSession->createSessionToken($request, $uid, $uid); |
|
176 | + // setup the filesystem |
|
177 | + OC_Util::setupFS($uid); |
|
178 | + // first call the post_login hooks, the login-process needs to be |
|
179 | + // completed before we can safely create the users folder. |
|
180 | + // For example encryption needs to initialize the users keys first |
|
181 | + // before we can create the user folder with the skeleton files |
|
182 | + OC_Hook::emit( |
|
183 | + 'OC_User', |
|
184 | + 'post_login', |
|
185 | + [ |
|
186 | + 'uid' => $uid, |
|
187 | + 'password' => '', |
|
188 | + 'isTokenLogin' => false, |
|
189 | + ] |
|
190 | + ); |
|
191 | + //trigger creation of user home and /files folder |
|
192 | + \OC::$server->getUserFolder($uid); |
|
193 | + } |
|
194 | + return true; |
|
195 | + } |
|
196 | + return false; |
|
197 | + } |
|
198 | + |
|
199 | + /** |
|
200 | + * Verify with Apache whether user is authenticated. |
|
201 | + * |
|
202 | + * @return boolean|null |
|
203 | + * true: authenticated |
|
204 | + * false: not authenticated |
|
205 | + * null: not handled / no backend available |
|
206 | + */ |
|
207 | + public static function handleApacheAuth() { |
|
208 | + $backend = self::findFirstActiveUsedBackend(); |
|
209 | + if ($backend) { |
|
210 | + OC_App::loadApps(); |
|
211 | + |
|
212 | + //setup extra user backends |
|
213 | + self::setupBackends(); |
|
214 | + \OC::$server->getUserSession()->unsetMagicInCookie(); |
|
215 | + |
|
216 | + return self::loginWithApache($backend); |
|
217 | + } |
|
218 | + |
|
219 | + return null; |
|
220 | + } |
|
221 | + |
|
222 | + |
|
223 | + /** |
|
224 | + * Sets user id for session and triggers emit |
|
225 | + * |
|
226 | + * @param string $uid |
|
227 | + */ |
|
228 | + public static function setUserId($uid) { |
|
229 | + $userSession = \OC::$server->getUserSession(); |
|
230 | + $userManager = \OC::$server->getUserManager(); |
|
231 | + if ($user = $userManager->get($uid)) { |
|
232 | + $userSession->setUser($user); |
|
233 | + } else { |
|
234 | + \OC::$server->getSession()->set('user_id', $uid); |
|
235 | + } |
|
236 | + } |
|
237 | + |
|
238 | + /** |
|
239 | + * Check if the user is logged in, considers also the HTTP basic credentials |
|
240 | + * |
|
241 | + * @deprecated use \OC::$server->getUserSession()->isLoggedIn() |
|
242 | + * @return bool |
|
243 | + */ |
|
244 | + public static function isLoggedIn() { |
|
245 | + return \OC::$server->getUserSession()->isLoggedIn(); |
|
246 | + } |
|
247 | + |
|
248 | + /** |
|
249 | + * set incognito mode, e.g. if a user wants to open a public link |
|
250 | + * |
|
251 | + * @param bool $status |
|
252 | + */ |
|
253 | + public static function setIncognitoMode($status) { |
|
254 | + self::$incognitoMode = $status; |
|
255 | + } |
|
256 | + |
|
257 | + /** |
|
258 | + * get incognito mode status |
|
259 | + * |
|
260 | + * @return bool |
|
261 | + */ |
|
262 | + public static function isIncognitoMode() { |
|
263 | + return self::$incognitoMode; |
|
264 | + } |
|
265 | + |
|
266 | + /** |
|
267 | + * Returns the current logout URL valid for the currently logged-in user |
|
268 | + * |
|
269 | + * @param \OCP\IURLGenerator $urlGenerator |
|
270 | + * @return string |
|
271 | + */ |
|
272 | + public static function getLogoutUrl(\OCP\IURLGenerator $urlGenerator) { |
|
273 | + $backend = self::findFirstActiveUsedBackend(); |
|
274 | + if ($backend) { |
|
275 | + return $backend->getLogoutUrl(); |
|
276 | + } |
|
277 | + |
|
278 | + $logoutUrl = $urlGenerator->linkToRoute('core.login.logout'); |
|
279 | + $logoutUrl .= '?requesttoken=' . urlencode(\OCP\Util::callRegister()); |
|
280 | + |
|
281 | + return $logoutUrl; |
|
282 | + } |
|
283 | + |
|
284 | + /** |
|
285 | + * Check if the user is an admin user |
|
286 | + * |
|
287 | + * @param string $uid uid of the admin |
|
288 | + * @return bool |
|
289 | + */ |
|
290 | + public static function isAdminUser($uid) { |
|
291 | + $group = \OC::$server->getGroupManager()->get('admin'); |
|
292 | + $user = \OC::$server->getUserManager()->get($uid); |
|
293 | + if ($group && $user && $group->inGroup($user) && self::$incognitoMode === false) { |
|
294 | + return true; |
|
295 | + } |
|
296 | + return false; |
|
297 | + } |
|
298 | + |
|
299 | + |
|
300 | + /** |
|
301 | + * get the user id of the user currently logged in. |
|
302 | + * |
|
303 | + * @return string|bool uid or false |
|
304 | + */ |
|
305 | + public static function getUser() { |
|
306 | + $uid = \OC::$server->getSession() ? \OC::$server->getSession()->get('user_id') : null; |
|
307 | + if (!is_null($uid) && self::$incognitoMode === false) { |
|
308 | + return $uid; |
|
309 | + } else { |
|
310 | + return false; |
|
311 | + } |
|
312 | + } |
|
313 | + |
|
314 | + /** |
|
315 | + * get the display name of the user currently logged in. |
|
316 | + * |
|
317 | + * @param string $uid |
|
318 | + * @return string|bool uid or false |
|
319 | + * @deprecated 8.1.0 fetch \OCP\IUser (has getDisplayName()) by using method |
|
320 | + * get() of \OCP\IUserManager - \OC::$server->getUserManager() |
|
321 | + */ |
|
322 | + public static function getDisplayName($uid = null) { |
|
323 | + if ($uid) { |
|
324 | + $user = \OC::$server->getUserManager()->get($uid); |
|
325 | + if ($user) { |
|
326 | + return $user->getDisplayName(); |
|
327 | + } else { |
|
328 | + return $uid; |
|
329 | + } |
|
330 | + } else { |
|
331 | + $user = \OC::$server->getUserSession()->getUser(); |
|
332 | + if ($user) { |
|
333 | + return $user->getDisplayName(); |
|
334 | + } else { |
|
335 | + return false; |
|
336 | + } |
|
337 | + } |
|
338 | + } |
|
339 | + |
|
340 | + /** |
|
341 | + * Set password |
|
342 | + * |
|
343 | + * @param string $uid The username |
|
344 | + * @param string $password The new password |
|
345 | + * @param string $recoveryPassword for the encryption app to reset encryption keys |
|
346 | + * @return bool |
|
347 | + * |
|
348 | + * Change the password of a user |
|
349 | + */ |
|
350 | + public static function setPassword($uid, $password, $recoveryPassword = null) { |
|
351 | + $user = \OC::$server->getUserManager()->get($uid); |
|
352 | + if ($user) { |
|
353 | + return $user->setPassword($password, $recoveryPassword); |
|
354 | + } else { |
|
355 | + return false; |
|
356 | + } |
|
357 | + } |
|
358 | + |
|
359 | + /** |
|
360 | + * @param string $uid The username |
|
361 | + * @return string |
|
362 | + * |
|
363 | + * returns the path to the users home directory |
|
364 | + * @deprecated Use \OC::$server->getUserManager->getHome() |
|
365 | + */ |
|
366 | + public static function getHome($uid) { |
|
367 | + $user = \OC::$server->getUserManager()->get($uid); |
|
368 | + if ($user) { |
|
369 | + return $user->getHome(); |
|
370 | + } else { |
|
371 | + return \OC::$server->getSystemConfig()->getValue('datadirectory', OC::$SERVERROOT . '/data') . '/' . $uid; |
|
372 | + } |
|
373 | + } |
|
374 | + |
|
375 | + /** |
|
376 | + * Get a list of all users display name |
|
377 | + * |
|
378 | + * @param string $search |
|
379 | + * @param int $limit |
|
380 | + * @param int $offset |
|
381 | + * @return array associative array with all display names (value) and corresponding uids (key) |
|
382 | + * |
|
383 | + * Get a list of all display names and user ids. |
|
384 | + * @deprecated Use \OC::$server->getUserManager->searchDisplayName($search, $limit, $offset) instead. |
|
385 | + */ |
|
386 | + public static function getDisplayNames($search = '', $limit = null, $offset = null) { |
|
387 | + $displayNames = []; |
|
388 | + $users = \OC::$server->getUserManager()->searchDisplayName($search, $limit, $offset); |
|
389 | + foreach ($users as $user) { |
|
390 | + $displayNames[$user->getUID()] = $user->getDisplayName(); |
|
391 | + } |
|
392 | + return $displayNames; |
|
393 | + } |
|
394 | + |
|
395 | + /** |
|
396 | + * Returns the first active backend from self::$_usedBackends. |
|
397 | + * |
|
398 | + * @return OCP\Authentication\IApacheBackend|null if no backend active, otherwise OCP\Authentication\IApacheBackend |
|
399 | + */ |
|
400 | + private static function findFirstActiveUsedBackend() { |
|
401 | + foreach (self::$_usedBackends as $backend) { |
|
402 | + if ($backend instanceof OCP\Authentication\IApacheBackend) { |
|
403 | + if ($backend->isSessionActive()) { |
|
404 | + return $backend; |
|
405 | + } |
|
406 | + } |
|
407 | + } |
|
408 | + |
|
409 | + return null; |
|
410 | + } |
|
411 | 411 | } |