@@ -29,20 +29,20 @@ |
||
| 29 | 29 | * @deprecated 8.1.0 |
| 30 | 30 | */ |
| 31 | 31 | class AppHelper implements \OCP\IHelper { |
| 32 | - /** |
|
| 33 | - * Gets the content of an URL by using CURL or a fallback if it is not |
|
| 34 | - * installed |
|
| 35 | - * @param string $url the url that should be fetched |
|
| 36 | - * @return string the content of the webpage |
|
| 37 | - * @deprecated 8.1.0 Use \OCP\IServerContainer::getHTTPClientService |
|
| 38 | - */ |
|
| 39 | - public function getUrlContent($url) { |
|
| 40 | - try { |
|
| 41 | - $client = \OC::$server->getHTTPClientService()->newClient(); |
|
| 42 | - $response = $client->get($url); |
|
| 43 | - return $response->getBody(); |
|
| 44 | - } catch (\Exception $e) { |
|
| 45 | - return false; |
|
| 46 | - } |
|
| 47 | - } |
|
| 32 | + /** |
|
| 33 | + * Gets the content of an URL by using CURL or a fallback if it is not |
|
| 34 | + * installed |
|
| 35 | + * @param string $url the url that should be fetched |
|
| 36 | + * @return string the content of the webpage |
|
| 37 | + * @deprecated 8.1.0 Use \OCP\IServerContainer::getHTTPClientService |
|
| 38 | + */ |
|
| 39 | + public function getUrlContent($url) { |
|
| 40 | + try { |
|
| 41 | + $client = \OC::$server->getHTTPClientService()->newClient(); |
|
| 42 | + $response = $client->get($url); |
|
| 43 | + return $response->getBody(); |
|
| 44 | + } catch (\Exception $e) { |
|
| 45 | + return false; |
|
| 46 | + } |
|
| 47 | + } |
|
| 48 | 48 | } |
@@ -60,9 +60,9 @@ discard block |
||
| 60 | 60 | //use the admin login data for the new database user |
| 61 | 61 | |
| 62 | 62 | //add prefix to the postgresql user name to prevent collisions |
| 63 | - $this->dbUser = 'oc_' . strtolower($username); |
|
| 63 | + $this->dbUser = 'oc_'.strtolower($username); |
|
| 64 | 64 | //create a new password so we don't need to store the admin config in the config file |
| 65 | - $this->dbPassword = \OC::$server->getSecureRandom()->generate(30, \OCP\Security\ISecureRandom::CHAR_LOWER . \OCP\Security\ISecureRandom::CHAR_DIGITS); |
|
| 65 | + $this->dbPassword = \OC::$server->getSecureRandom()->generate(30, \OCP\Security\ISecureRandom::CHAR_LOWER.\OCP\Security\ISecureRandom::CHAR_DIGITS); |
|
| 66 | 66 | |
| 67 | 67 | $this->createDBUser($connection); |
| 68 | 68 | } |
@@ -75,7 +75,7 @@ discard block |
||
| 75 | 75 | //create the database |
| 76 | 76 | $this->createDatabase($connection); |
| 77 | 77 | $query = $connection->prepare("select count(*) FROM pg_class WHERE relname=? limit 1"); |
| 78 | - $query->execute([$this->tablePrefix . "users"]); |
|
| 78 | + $query->execute([$this->tablePrefix."users"]); |
|
| 79 | 79 | $tablesSetup = $query->fetchColumn() > 0; |
| 80 | 80 | |
| 81 | 81 | // the connection to dbname=postgres is not needed anymore |
@@ -111,7 +111,7 @@ discard block |
||
| 111 | 111 | private function createDatabase(IDBConnection $connection) { |
| 112 | 112 | if (!$this->databaseExists($connection)) { |
| 113 | 113 | //The database does not exists... let's create it |
| 114 | - $query = $connection->prepare("CREATE DATABASE " . addslashes($this->dbName) . " OWNER " . addslashes($this->dbUser)); |
|
| 114 | + $query = $connection->prepare("CREATE DATABASE ".addslashes($this->dbName)." OWNER ".addslashes($this->dbUser)); |
|
| 115 | 115 | try { |
| 116 | 116 | $query->execute(); |
| 117 | 117 | } catch (DatabaseException $e) { |
@@ -119,7 +119,7 @@ discard block |
||
| 119 | 119 | $this->logger->logException($e); |
| 120 | 120 | } |
| 121 | 121 | } else { |
| 122 | - $query = $connection->prepare("REVOKE ALL PRIVILEGES ON DATABASE " . addslashes($this->dbName) . " FROM PUBLIC"); |
|
| 122 | + $query = $connection->prepare("REVOKE ALL PRIVILEGES ON DATABASE ".addslashes($this->dbName)." FROM PUBLIC"); |
|
| 123 | 123 | try { |
| 124 | 124 | $query->execute(); |
| 125 | 125 | } catch (DatabaseException $e) { |
@@ -155,11 +155,11 @@ discard block |
||
| 155 | 155 | $i = 1; |
| 156 | 156 | while ($this->userExists($connection)) { |
| 157 | 157 | $i++; |
| 158 | - $this->dbUser = $dbUser . $i; |
|
| 158 | + $this->dbUser = $dbUser.$i; |
|
| 159 | 159 | }; |
| 160 | 160 | |
| 161 | 161 | // create the user |
| 162 | - $query = $connection->prepare("CREATE USER " . addslashes($this->dbUser) . " CREATEDB PASSWORD '" . addslashes($this->dbPassword) . "'"); |
|
| 162 | + $query = $connection->prepare("CREATE USER ".addslashes($this->dbUser)." CREATEDB PASSWORD '".addslashes($this->dbPassword)."'"); |
|
| 163 | 163 | $query->execute(); |
| 164 | 164 | } catch (DatabaseException $e) { |
| 165 | 165 | $this->logger->error('Error while trying to create database user'); |
@@ -32,137 +32,137 @@ |
||
| 32 | 32 | use OCP\IDBConnection; |
| 33 | 33 | |
| 34 | 34 | class PostgreSQL extends AbstractDatabase { |
| 35 | - public $dbprettyname = 'PostgreSQL'; |
|
| 36 | - |
|
| 37 | - /** |
|
| 38 | - * @param string $username |
|
| 39 | - * @throws \OC\DatabaseSetupException |
|
| 40 | - * @suppress SqlInjectionChecker |
|
| 41 | - */ |
|
| 42 | - public function setupDatabase($username) { |
|
| 43 | - try { |
|
| 44 | - $connection = $this->connect([ |
|
| 45 | - 'dbname' => 'postgres' |
|
| 46 | - ]); |
|
| 47 | - //check for roles creation rights in postgresql |
|
| 48 | - $builder = $connection->getQueryBuilder(); |
|
| 49 | - $builder->automaticTablePrefix(false); |
|
| 50 | - $query = $builder |
|
| 51 | - ->select('rolname') |
|
| 52 | - ->from('pg_roles') |
|
| 53 | - ->where($builder->expr()->eq('rolcreaterole', new Literal('TRUE'))) |
|
| 54 | - ->andWhere($builder->expr()->eq('rolname', $builder->createNamedParameter($this->dbUser))); |
|
| 55 | - |
|
| 56 | - try { |
|
| 57 | - $result = $query->execute(); |
|
| 58 | - $canCreateRoles = $result->rowCount() > 0; |
|
| 59 | - } catch (DatabaseException $e) { |
|
| 60 | - $canCreateRoles = false; |
|
| 61 | - } |
|
| 62 | - |
|
| 63 | - if ($canCreateRoles) { |
|
| 64 | - //use the admin login data for the new database user |
|
| 65 | - |
|
| 66 | - //add prefix to the postgresql user name to prevent collisions |
|
| 67 | - $this->dbUser = 'oc_' . strtolower($username); |
|
| 68 | - //create a new password so we don't need to store the admin config in the config file |
|
| 69 | - $this->dbPassword = \OC::$server->getSecureRandom()->generate(30, \OCP\Security\ISecureRandom::CHAR_LOWER . \OCP\Security\ISecureRandom::CHAR_DIGITS); |
|
| 70 | - |
|
| 71 | - $this->createDBUser($connection); |
|
| 72 | - } |
|
| 73 | - |
|
| 74 | - $this->config->setValues([ |
|
| 75 | - 'dbuser' => $this->dbUser, |
|
| 76 | - 'dbpassword' => $this->dbPassword, |
|
| 77 | - ]); |
|
| 78 | - |
|
| 79 | - //create the database |
|
| 80 | - $this->createDatabase($connection); |
|
| 81 | - $query = $connection->prepare("select count(*) FROM pg_class WHERE relname=? limit 1"); |
|
| 82 | - $query->execute([$this->tablePrefix . "users"]); |
|
| 83 | - $tablesSetup = $query->fetchColumn() > 0; |
|
| 84 | - |
|
| 85 | - // the connection to dbname=postgres is not needed anymore |
|
| 86 | - $connection->close(); |
|
| 87 | - } catch (\Exception $e) { |
|
| 88 | - $this->logger->logException($e); |
|
| 89 | - $this->logger->warning('Error trying to connect as "postgres", assuming database is setup and tables need to be created'); |
|
| 90 | - $tablesSetup = false; |
|
| 91 | - $this->config->setValues([ |
|
| 92 | - 'dbuser' => $this->dbUser, |
|
| 93 | - 'dbpassword' => $this->dbPassword, |
|
| 94 | - ]); |
|
| 95 | - } |
|
| 96 | - |
|
| 97 | - // connect to the ownCloud database (dbname=$this->dbname) and check if it needs to be filled |
|
| 98 | - $this->dbUser = $this->config->getValue('dbuser'); |
|
| 99 | - $this->dbPassword = $this->config->getValue('dbpassword'); |
|
| 100 | - $connection = $this->connect(); |
|
| 101 | - try { |
|
| 102 | - $connection->connect(); |
|
| 103 | - } catch (\Exception $e) { |
|
| 104 | - $this->logger->logException($e); |
|
| 105 | - throw new \OC\DatabaseSetupException($this->trans->t('PostgreSQL username and/or password not valid'), |
|
| 106 | - $this->trans->t('You need to enter details of an existing account.')); |
|
| 107 | - } |
|
| 108 | - } |
|
| 109 | - |
|
| 110 | - private function createDatabase(IDBConnection $connection) { |
|
| 111 | - if (!$this->databaseExists($connection)) { |
|
| 112 | - //The database does not exists... let's create it |
|
| 113 | - $query = $connection->prepare("CREATE DATABASE " . addslashes($this->dbName) . " OWNER " . addslashes($this->dbUser)); |
|
| 114 | - try { |
|
| 115 | - $query->execute(); |
|
| 116 | - } catch (DatabaseException $e) { |
|
| 117 | - $this->logger->error('Error while trying to create database'); |
|
| 118 | - $this->logger->logException($e); |
|
| 119 | - } |
|
| 120 | - } else { |
|
| 121 | - $query = $connection->prepare("REVOKE ALL PRIVILEGES ON DATABASE " . addslashes($this->dbName) . " FROM PUBLIC"); |
|
| 122 | - try { |
|
| 123 | - $query->execute(); |
|
| 124 | - } catch (DatabaseException $e) { |
|
| 125 | - $this->logger->error('Error while trying to restrict database permissions'); |
|
| 126 | - $this->logger->logException($e); |
|
| 127 | - } |
|
| 128 | - } |
|
| 129 | - } |
|
| 130 | - |
|
| 131 | - private function userExists(IDBConnection $connection) { |
|
| 132 | - $builder = $connection->getQueryBuilder(); |
|
| 133 | - $builder->automaticTablePrefix(false); |
|
| 134 | - $query = $builder->select('*') |
|
| 135 | - ->from('pg_roles') |
|
| 136 | - ->where($builder->expr()->eq('rolname', $builder->createNamedParameter($this->dbUser))); |
|
| 137 | - $result = $query->execute(); |
|
| 138 | - return $result->rowCount() > 0; |
|
| 139 | - } |
|
| 140 | - |
|
| 141 | - private function databaseExists(IDBConnection $connection) { |
|
| 142 | - $builder = $connection->getQueryBuilder(); |
|
| 143 | - $builder->automaticTablePrefix(false); |
|
| 144 | - $query = $builder->select('datname') |
|
| 145 | - ->from('pg_database') |
|
| 146 | - ->where($builder->expr()->eq('datname', $builder->createNamedParameter($this->dbName))); |
|
| 147 | - $result = $query->execute(); |
|
| 148 | - return $result->rowCount() > 0; |
|
| 149 | - } |
|
| 150 | - |
|
| 151 | - private function createDBUser(IDBConnection $connection) { |
|
| 152 | - $dbUser = $this->dbUser; |
|
| 153 | - try { |
|
| 154 | - $i = 1; |
|
| 155 | - while ($this->userExists($connection)) { |
|
| 156 | - $i++; |
|
| 157 | - $this->dbUser = $dbUser . $i; |
|
| 158 | - }; |
|
| 159 | - |
|
| 160 | - // create the user |
|
| 161 | - $query = $connection->prepare("CREATE USER " . addslashes($this->dbUser) . " CREATEDB PASSWORD '" . addslashes($this->dbPassword) . "'"); |
|
| 162 | - $query->execute(); |
|
| 163 | - } catch (DatabaseException $e) { |
|
| 164 | - $this->logger->error('Error while trying to create database user'); |
|
| 165 | - $this->logger->logException($e); |
|
| 166 | - } |
|
| 167 | - } |
|
| 35 | + public $dbprettyname = 'PostgreSQL'; |
|
| 36 | + |
|
| 37 | + /** |
|
| 38 | + * @param string $username |
|
| 39 | + * @throws \OC\DatabaseSetupException |
|
| 40 | + * @suppress SqlInjectionChecker |
|
| 41 | + */ |
|
| 42 | + public function setupDatabase($username) { |
|
| 43 | + try { |
|
| 44 | + $connection = $this->connect([ |
|
| 45 | + 'dbname' => 'postgres' |
|
| 46 | + ]); |
|
| 47 | + //check for roles creation rights in postgresql |
|
| 48 | + $builder = $connection->getQueryBuilder(); |
|
| 49 | + $builder->automaticTablePrefix(false); |
|
| 50 | + $query = $builder |
|
| 51 | + ->select('rolname') |
|
| 52 | + ->from('pg_roles') |
|
| 53 | + ->where($builder->expr()->eq('rolcreaterole', new Literal('TRUE'))) |
|
| 54 | + ->andWhere($builder->expr()->eq('rolname', $builder->createNamedParameter($this->dbUser))); |
|
| 55 | + |
|
| 56 | + try { |
|
| 57 | + $result = $query->execute(); |
|
| 58 | + $canCreateRoles = $result->rowCount() > 0; |
|
| 59 | + } catch (DatabaseException $e) { |
|
| 60 | + $canCreateRoles = false; |
|
| 61 | + } |
|
| 62 | + |
|
| 63 | + if ($canCreateRoles) { |
|
| 64 | + //use the admin login data for the new database user |
|
| 65 | + |
|
| 66 | + //add prefix to the postgresql user name to prevent collisions |
|
| 67 | + $this->dbUser = 'oc_' . strtolower($username); |
|
| 68 | + //create a new password so we don't need to store the admin config in the config file |
|
| 69 | + $this->dbPassword = \OC::$server->getSecureRandom()->generate(30, \OCP\Security\ISecureRandom::CHAR_LOWER . \OCP\Security\ISecureRandom::CHAR_DIGITS); |
|
| 70 | + |
|
| 71 | + $this->createDBUser($connection); |
|
| 72 | + } |
|
| 73 | + |
|
| 74 | + $this->config->setValues([ |
|
| 75 | + 'dbuser' => $this->dbUser, |
|
| 76 | + 'dbpassword' => $this->dbPassword, |
|
| 77 | + ]); |
|
| 78 | + |
|
| 79 | + //create the database |
|
| 80 | + $this->createDatabase($connection); |
|
| 81 | + $query = $connection->prepare("select count(*) FROM pg_class WHERE relname=? limit 1"); |
|
| 82 | + $query->execute([$this->tablePrefix . "users"]); |
|
| 83 | + $tablesSetup = $query->fetchColumn() > 0; |
|
| 84 | + |
|
| 85 | + // the connection to dbname=postgres is not needed anymore |
|
| 86 | + $connection->close(); |
|
| 87 | + } catch (\Exception $e) { |
|
| 88 | + $this->logger->logException($e); |
|
| 89 | + $this->logger->warning('Error trying to connect as "postgres", assuming database is setup and tables need to be created'); |
|
| 90 | + $tablesSetup = false; |
|
| 91 | + $this->config->setValues([ |
|
| 92 | + 'dbuser' => $this->dbUser, |
|
| 93 | + 'dbpassword' => $this->dbPassword, |
|
| 94 | + ]); |
|
| 95 | + } |
|
| 96 | + |
|
| 97 | + // connect to the ownCloud database (dbname=$this->dbname) and check if it needs to be filled |
|
| 98 | + $this->dbUser = $this->config->getValue('dbuser'); |
|
| 99 | + $this->dbPassword = $this->config->getValue('dbpassword'); |
|
| 100 | + $connection = $this->connect(); |
|
| 101 | + try { |
|
| 102 | + $connection->connect(); |
|
| 103 | + } catch (\Exception $e) { |
|
| 104 | + $this->logger->logException($e); |
|
| 105 | + throw new \OC\DatabaseSetupException($this->trans->t('PostgreSQL username and/or password not valid'), |
|
| 106 | + $this->trans->t('You need to enter details of an existing account.')); |
|
| 107 | + } |
|
| 108 | + } |
|
| 109 | + |
|
| 110 | + private function createDatabase(IDBConnection $connection) { |
|
| 111 | + if (!$this->databaseExists($connection)) { |
|
| 112 | + //The database does not exists... let's create it |
|
| 113 | + $query = $connection->prepare("CREATE DATABASE " . addslashes($this->dbName) . " OWNER " . addslashes($this->dbUser)); |
|
| 114 | + try { |
|
| 115 | + $query->execute(); |
|
| 116 | + } catch (DatabaseException $e) { |
|
| 117 | + $this->logger->error('Error while trying to create database'); |
|
| 118 | + $this->logger->logException($e); |
|
| 119 | + } |
|
| 120 | + } else { |
|
| 121 | + $query = $connection->prepare("REVOKE ALL PRIVILEGES ON DATABASE " . addslashes($this->dbName) . " FROM PUBLIC"); |
|
| 122 | + try { |
|
| 123 | + $query->execute(); |
|
| 124 | + } catch (DatabaseException $e) { |
|
| 125 | + $this->logger->error('Error while trying to restrict database permissions'); |
|
| 126 | + $this->logger->logException($e); |
|
| 127 | + } |
|
| 128 | + } |
|
| 129 | + } |
|
| 130 | + |
|
| 131 | + private function userExists(IDBConnection $connection) { |
|
| 132 | + $builder = $connection->getQueryBuilder(); |
|
| 133 | + $builder->automaticTablePrefix(false); |
|
| 134 | + $query = $builder->select('*') |
|
| 135 | + ->from('pg_roles') |
|
| 136 | + ->where($builder->expr()->eq('rolname', $builder->createNamedParameter($this->dbUser))); |
|
| 137 | + $result = $query->execute(); |
|
| 138 | + return $result->rowCount() > 0; |
|
| 139 | + } |
|
| 140 | + |
|
| 141 | + private function databaseExists(IDBConnection $connection) { |
|
| 142 | + $builder = $connection->getQueryBuilder(); |
|
| 143 | + $builder->automaticTablePrefix(false); |
|
| 144 | + $query = $builder->select('datname') |
|
| 145 | + ->from('pg_database') |
|
| 146 | + ->where($builder->expr()->eq('datname', $builder->createNamedParameter($this->dbName))); |
|
| 147 | + $result = $query->execute(); |
|
| 148 | + return $result->rowCount() > 0; |
|
| 149 | + } |
|
| 150 | + |
|
| 151 | + private function createDBUser(IDBConnection $connection) { |
|
| 152 | + $dbUser = $this->dbUser; |
|
| 153 | + try { |
|
| 154 | + $i = 1; |
|
| 155 | + while ($this->userExists($connection)) { |
|
| 156 | + $i++; |
|
| 157 | + $this->dbUser = $dbUser . $i; |
|
| 158 | + }; |
|
| 159 | + |
|
| 160 | + // create the user |
|
| 161 | + $query = $connection->prepare("CREATE USER " . addslashes($this->dbUser) . " CREATEDB PASSWORD '" . addslashes($this->dbPassword) . "'"); |
|
| 162 | + $query->execute(); |
|
| 163 | + } catch (DatabaseException $e) { |
|
| 164 | + $this->logger->error('Error while trying to create database user'); |
|
| 165 | + $this->logger->logException($e); |
|
| 166 | + } |
|
| 167 | + } |
|
| 168 | 168 | } |
@@ -68,14 +68,14 @@ |
||
| 68 | 68 | |
| 69 | 69 | public function validate($config) { |
| 70 | 70 | $errors = array(); |
| 71 | - if(empty($config['dbuser']) && empty($config['dbname'])) { |
|
| 71 | + if (empty($config['dbuser']) && empty($config['dbname'])) { |
|
| 72 | 72 | $errors[] = $this->trans->t("%s enter the database username and name.", array($this->dbprettyname)); |
| 73 | - } else if(empty($config['dbuser'])) { |
|
| 73 | + } else if (empty($config['dbuser'])) { |
|
| 74 | 74 | $errors[] = $this->trans->t("%s enter the database username.", array($this->dbprettyname)); |
| 75 | - } else if(empty($config['dbname'])) { |
|
| 75 | + } else if (empty($config['dbname'])) { |
|
| 76 | 76 | $errors[] = $this->trans->t("%s enter the database name.", array($this->dbprettyname)); |
| 77 | 77 | } |
| 78 | - if(substr_count($config['dbname'], '.') >= 1) { |
|
| 78 | + if (substr_count($config['dbname'], '.') >= 1) { |
|
| 79 | 79 | $errors[] = $this->trans->t("%s you may not use dots in the database name", array($this->dbprettyname)); |
| 80 | 80 | } |
| 81 | 81 | return $errors; |
@@ -35,118 +35,118 @@ |
||
| 35 | 35 | |
| 36 | 36 | abstract class AbstractDatabase { |
| 37 | 37 | |
| 38 | - /** @var IL10N */ |
|
| 39 | - protected $trans; |
|
| 40 | - /** @var string */ |
|
| 41 | - protected $dbUser; |
|
| 42 | - /** @var string */ |
|
| 43 | - protected $dbPassword; |
|
| 44 | - /** @var string */ |
|
| 45 | - protected $dbName; |
|
| 46 | - /** @var string */ |
|
| 47 | - protected $dbHost; |
|
| 48 | - /** @var string */ |
|
| 49 | - protected $dbPort; |
|
| 50 | - /** @var string */ |
|
| 51 | - protected $tablePrefix; |
|
| 52 | - /** @var SystemConfig */ |
|
| 53 | - protected $config; |
|
| 54 | - /** @var ILogger */ |
|
| 55 | - protected $logger; |
|
| 56 | - /** @var ISecureRandom */ |
|
| 57 | - protected $random; |
|
| 38 | + /** @var IL10N */ |
|
| 39 | + protected $trans; |
|
| 40 | + /** @var string */ |
|
| 41 | + protected $dbUser; |
|
| 42 | + /** @var string */ |
|
| 43 | + protected $dbPassword; |
|
| 44 | + /** @var string */ |
|
| 45 | + protected $dbName; |
|
| 46 | + /** @var string */ |
|
| 47 | + protected $dbHost; |
|
| 48 | + /** @var string */ |
|
| 49 | + protected $dbPort; |
|
| 50 | + /** @var string */ |
|
| 51 | + protected $tablePrefix; |
|
| 52 | + /** @var SystemConfig */ |
|
| 53 | + protected $config; |
|
| 54 | + /** @var ILogger */ |
|
| 55 | + protected $logger; |
|
| 56 | + /** @var ISecureRandom */ |
|
| 57 | + protected $random; |
|
| 58 | 58 | |
| 59 | - public function __construct(IL10N $trans, SystemConfig $config, ILogger $logger, ISecureRandom $random) { |
|
| 60 | - $this->trans = $trans; |
|
| 61 | - $this->config = $config; |
|
| 62 | - $this->logger = $logger; |
|
| 63 | - $this->random = $random; |
|
| 64 | - } |
|
| 59 | + public function __construct(IL10N $trans, SystemConfig $config, ILogger $logger, ISecureRandom $random) { |
|
| 60 | + $this->trans = $trans; |
|
| 61 | + $this->config = $config; |
|
| 62 | + $this->logger = $logger; |
|
| 63 | + $this->random = $random; |
|
| 64 | + } |
|
| 65 | 65 | |
| 66 | - public function validate($config) { |
|
| 67 | - $errors = array(); |
|
| 68 | - if(empty($config['dbuser']) && empty($config['dbname'])) { |
|
| 69 | - $errors[] = $this->trans->t("%s enter the database username and name.", array($this->dbprettyname)); |
|
| 70 | - } else if(empty($config['dbuser'])) { |
|
| 71 | - $errors[] = $this->trans->t("%s enter the database username.", array($this->dbprettyname)); |
|
| 72 | - } else if(empty($config['dbname'])) { |
|
| 73 | - $errors[] = $this->trans->t("%s enter the database name.", array($this->dbprettyname)); |
|
| 74 | - } |
|
| 75 | - if(substr_count($config['dbname'], '.') >= 1) { |
|
| 76 | - $errors[] = $this->trans->t("%s you may not use dots in the database name", array($this->dbprettyname)); |
|
| 77 | - } |
|
| 78 | - return $errors; |
|
| 79 | - } |
|
| 66 | + public function validate($config) { |
|
| 67 | + $errors = array(); |
|
| 68 | + if(empty($config['dbuser']) && empty($config['dbname'])) { |
|
| 69 | + $errors[] = $this->trans->t("%s enter the database username and name.", array($this->dbprettyname)); |
|
| 70 | + } else if(empty($config['dbuser'])) { |
|
| 71 | + $errors[] = $this->trans->t("%s enter the database username.", array($this->dbprettyname)); |
|
| 72 | + } else if(empty($config['dbname'])) { |
|
| 73 | + $errors[] = $this->trans->t("%s enter the database name.", array($this->dbprettyname)); |
|
| 74 | + } |
|
| 75 | + if(substr_count($config['dbname'], '.') >= 1) { |
|
| 76 | + $errors[] = $this->trans->t("%s you may not use dots in the database name", array($this->dbprettyname)); |
|
| 77 | + } |
|
| 78 | + return $errors; |
|
| 79 | + } |
|
| 80 | 80 | |
| 81 | - public function initialize($config) { |
|
| 82 | - $dbUser = $config['dbuser']; |
|
| 83 | - $dbPass = $config['dbpass']; |
|
| 84 | - $dbName = $config['dbname']; |
|
| 85 | - $dbHost = !empty($config['dbhost']) ? $config['dbhost'] : 'localhost'; |
|
| 86 | - $dbPort = !empty($config['dbport']) ? $config['dbport'] : ''; |
|
| 87 | - $dbTablePrefix = isset($config['dbtableprefix']) ? $config['dbtableprefix'] : 'oc_'; |
|
| 81 | + public function initialize($config) { |
|
| 82 | + $dbUser = $config['dbuser']; |
|
| 83 | + $dbPass = $config['dbpass']; |
|
| 84 | + $dbName = $config['dbname']; |
|
| 85 | + $dbHost = !empty($config['dbhost']) ? $config['dbhost'] : 'localhost'; |
|
| 86 | + $dbPort = !empty($config['dbport']) ? $config['dbport'] : ''; |
|
| 87 | + $dbTablePrefix = isset($config['dbtableprefix']) ? $config['dbtableprefix'] : 'oc_'; |
|
| 88 | 88 | |
| 89 | - $this->config->setValues([ |
|
| 90 | - 'dbname' => $dbName, |
|
| 91 | - 'dbhost' => $dbHost, |
|
| 92 | - 'dbport' => $dbPort, |
|
| 93 | - 'dbtableprefix' => $dbTablePrefix, |
|
| 94 | - ]); |
|
| 89 | + $this->config->setValues([ |
|
| 90 | + 'dbname' => $dbName, |
|
| 91 | + 'dbhost' => $dbHost, |
|
| 92 | + 'dbport' => $dbPort, |
|
| 93 | + 'dbtableprefix' => $dbTablePrefix, |
|
| 94 | + ]); |
|
| 95 | 95 | |
| 96 | - $this->dbUser = $dbUser; |
|
| 97 | - $this->dbPassword = $dbPass; |
|
| 98 | - $this->dbName = $dbName; |
|
| 99 | - $this->dbHost = $dbHost; |
|
| 100 | - $this->dbPort = $dbPort; |
|
| 101 | - $this->tablePrefix = $dbTablePrefix; |
|
| 102 | - } |
|
| 96 | + $this->dbUser = $dbUser; |
|
| 97 | + $this->dbPassword = $dbPass; |
|
| 98 | + $this->dbName = $dbName; |
|
| 99 | + $this->dbHost = $dbHost; |
|
| 100 | + $this->dbPort = $dbPort; |
|
| 101 | + $this->tablePrefix = $dbTablePrefix; |
|
| 102 | + } |
|
| 103 | 103 | |
| 104 | - /** |
|
| 105 | - * @param array $configOverwrite |
|
| 106 | - * @return \OC\DB\Connection |
|
| 107 | - */ |
|
| 108 | - protected function connect(array $configOverwrite = []) { |
|
| 109 | - $connectionParams = array( |
|
| 110 | - 'host' => $this->dbHost, |
|
| 111 | - 'user' => $this->dbUser, |
|
| 112 | - 'password' => $this->dbPassword, |
|
| 113 | - 'tablePrefix' => $this->tablePrefix, |
|
| 114 | - 'dbname' => $this->dbName |
|
| 115 | - ); |
|
| 104 | + /** |
|
| 105 | + * @param array $configOverwrite |
|
| 106 | + * @return \OC\DB\Connection |
|
| 107 | + */ |
|
| 108 | + protected function connect(array $configOverwrite = []) { |
|
| 109 | + $connectionParams = array( |
|
| 110 | + 'host' => $this->dbHost, |
|
| 111 | + 'user' => $this->dbUser, |
|
| 112 | + 'password' => $this->dbPassword, |
|
| 113 | + 'tablePrefix' => $this->tablePrefix, |
|
| 114 | + 'dbname' => $this->dbName |
|
| 115 | + ); |
|
| 116 | 116 | |
| 117 | - // adding port support through installer |
|
| 118 | - if (!empty($this->dbPort)) { |
|
| 119 | - if (ctype_digit($this->dbPort)) { |
|
| 120 | - $connectionParams['port'] = $this->dbPort; |
|
| 121 | - } else { |
|
| 122 | - $connectionParams['unix_socket'] = $this->dbPort; |
|
| 123 | - } |
|
| 124 | - } else if (strpos($this->dbHost, ':')) { |
|
| 125 | - // Host variable may carry a port or socket. |
|
| 126 | - list($host, $portOrSocket) = explode(':', $this->dbHost, 2); |
|
| 127 | - if (ctype_digit($portOrSocket)) { |
|
| 128 | - $connectionParams['port'] = $portOrSocket; |
|
| 129 | - } else { |
|
| 130 | - $connectionParams['unix_socket'] = $portOrSocket; |
|
| 131 | - } |
|
| 132 | - $connectionParams['host'] = $host; |
|
| 133 | - } |
|
| 117 | + // adding port support through installer |
|
| 118 | + if (!empty($this->dbPort)) { |
|
| 119 | + if (ctype_digit($this->dbPort)) { |
|
| 120 | + $connectionParams['port'] = $this->dbPort; |
|
| 121 | + } else { |
|
| 122 | + $connectionParams['unix_socket'] = $this->dbPort; |
|
| 123 | + } |
|
| 124 | + } else if (strpos($this->dbHost, ':')) { |
|
| 125 | + // Host variable may carry a port or socket. |
|
| 126 | + list($host, $portOrSocket) = explode(':', $this->dbHost, 2); |
|
| 127 | + if (ctype_digit($portOrSocket)) { |
|
| 128 | + $connectionParams['port'] = $portOrSocket; |
|
| 129 | + } else { |
|
| 130 | + $connectionParams['unix_socket'] = $portOrSocket; |
|
| 131 | + } |
|
| 132 | + $connectionParams['host'] = $host; |
|
| 133 | + } |
|
| 134 | 134 | |
| 135 | - $connectionParams = array_merge($connectionParams, $configOverwrite); |
|
| 136 | - $cf = new ConnectionFactory($this->config); |
|
| 137 | - return $cf->getConnection($this->config->getValue('dbtype', 'sqlite'), $connectionParams); |
|
| 138 | - } |
|
| 135 | + $connectionParams = array_merge($connectionParams, $configOverwrite); |
|
| 136 | + $cf = new ConnectionFactory($this->config); |
|
| 137 | + return $cf->getConnection($this->config->getValue('dbtype', 'sqlite'), $connectionParams); |
|
| 138 | + } |
|
| 139 | 139 | |
| 140 | - /** |
|
| 141 | - * @param string $userName |
|
| 142 | - */ |
|
| 143 | - abstract public function setupDatabase($userName); |
|
| 140 | + /** |
|
| 141 | + * @param string $userName |
|
| 142 | + */ |
|
| 143 | + abstract public function setupDatabase($userName); |
|
| 144 | 144 | |
| 145 | - public function runMigrations() { |
|
| 146 | - if (!is_dir(\OC::$SERVERROOT."/core/Migrations")) { |
|
| 147 | - return; |
|
| 148 | - } |
|
| 149 | - $ms = new MigrationService('core', \OC::$server->getDatabaseConnection()); |
|
| 150 | - $ms->migrate(); |
|
| 151 | - } |
|
| 145 | + public function runMigrations() { |
|
| 146 | + if (!is_dir(\OC::$SERVERROOT."/core/Migrations")) { |
|
| 147 | + return; |
|
| 148 | + } |
|
| 149 | + $ms = new MigrationService('core', \OC::$server->getDatabaseConnection()); |
|
| 150 | + $ms->migrate(); |
|
| 151 | + } |
|
| 152 | 152 | } |
@@ -96,8 +96,7 @@ |
||
| 96 | 96 | $connection->executeUpdate($query); |
| 97 | 97 | $query = "CREATE USER '$name'@'%' IDENTIFIED BY '$password'"; |
| 98 | 98 | $connection->executeUpdate($query); |
| 99 | - } |
|
| 100 | - catch (\Exception $ex){ |
|
| 99 | + } catch (\Exception $ex){ |
|
| 101 | 100 | $this->logger->error('Database User creation failed: {error}', [ |
| 102 | 101 | 'app' => 'mysql.setup', |
| 103 | 102 | 'error' => $ex->getMessage() |
@@ -31,139 +31,139 @@ |
||
| 31 | 31 | use OCP\IDBConnection; |
| 32 | 32 | |
| 33 | 33 | class MySQL extends AbstractDatabase { |
| 34 | - public $dbprettyname = 'MySQL/MariaDB'; |
|
| 35 | - |
|
| 36 | - public function setupDatabase($username) { |
|
| 37 | - //check if the database user has admin right |
|
| 38 | - $connection = $this->connect(['dbname' => null]); |
|
| 39 | - |
|
| 40 | - // detect mb4 |
|
| 41 | - $tools = new MySqlTools(); |
|
| 42 | - if ($tools->supports4ByteCharset($connection)) { |
|
| 43 | - $this->config->setValue('mysql.utf8mb4', true); |
|
| 44 | - $connection = $this->connect(['dbname' => null]); |
|
| 45 | - } |
|
| 46 | - |
|
| 47 | - $this->createSpecificUser($username, $connection); |
|
| 48 | - |
|
| 49 | - //create the database |
|
| 50 | - $this->createDatabase($connection); |
|
| 51 | - |
|
| 52 | - //fill the database if needed |
|
| 53 | - $query='select count(*) from information_schema.tables where table_schema=? AND table_name = ?'; |
|
| 54 | - $connection->executeQuery($query, [$this->dbName, $this->tablePrefix.'users']); |
|
| 55 | - } |
|
| 56 | - |
|
| 57 | - /** |
|
| 58 | - * @param \OC\DB\Connection $connection |
|
| 59 | - */ |
|
| 60 | - private function createDatabase($connection) { |
|
| 61 | - try{ |
|
| 62 | - $name = $this->dbName; |
|
| 63 | - $user = $this->dbUser; |
|
| 64 | - //we can't use OC_DB functions here because we need to connect as the administrative user. |
|
| 65 | - $characterSet = $this->config->getValue('mysql.utf8mb4', false) ? 'utf8mb4' : 'utf8'; |
|
| 66 | - $query = "CREATE DATABASE IF NOT EXISTS `$name` CHARACTER SET $characterSet COLLATE ${characterSet}_bin;"; |
|
| 67 | - $connection->executeUpdate($query); |
|
| 68 | - } catch (\Exception $ex) { |
|
| 69 | - $this->logger->error('Database creation failed: {error}', [ |
|
| 70 | - 'app' => 'mysql.setup', |
|
| 71 | - 'error' => $ex->getMessage() |
|
| 72 | - ]); |
|
| 73 | - return; |
|
| 74 | - } |
|
| 75 | - |
|
| 76 | - try { |
|
| 77 | - //this query will fail if there aren't the right permissions, ignore the error |
|
| 78 | - $query="GRANT ALL PRIVILEGES ON `$name` . * TO '$user'"; |
|
| 79 | - $connection->executeUpdate($query); |
|
| 80 | - } catch (\Exception $ex) { |
|
| 81 | - $this->logger->debug('Could not automatically grant privileges, this can be ignored if database user already had privileges: {error}', [ |
|
| 82 | - 'app' => 'mysql.setup', |
|
| 83 | - 'error' => $ex->getMessage() |
|
| 84 | - ]); |
|
| 85 | - } |
|
| 86 | - } |
|
| 87 | - |
|
| 88 | - /** |
|
| 89 | - * @param IDBConnection $connection |
|
| 90 | - * @throws \OC\DatabaseSetupException |
|
| 91 | - */ |
|
| 92 | - private function createDBUser($connection) { |
|
| 93 | - try{ |
|
| 94 | - $name = $this->dbUser; |
|
| 95 | - $password = $this->dbPassword; |
|
| 96 | - // we need to create 2 accounts, one for global use and one for local user. if we don't specify the local one, |
|
| 97 | - // the anonymous user would take precedence when there is one. |
|
| 98 | - $query = "CREATE USER '$name'@'localhost' IDENTIFIED BY '$password'"; |
|
| 99 | - $connection->executeUpdate($query); |
|
| 100 | - $query = "CREATE USER '$name'@'%' IDENTIFIED BY '$password'"; |
|
| 101 | - $connection->executeUpdate($query); |
|
| 102 | - } |
|
| 103 | - catch (\Exception $ex){ |
|
| 104 | - $this->logger->error('Database User creation failed: {error}', [ |
|
| 34 | + public $dbprettyname = 'MySQL/MariaDB'; |
|
| 35 | + |
|
| 36 | + public function setupDatabase($username) { |
|
| 37 | + //check if the database user has admin right |
|
| 38 | + $connection = $this->connect(['dbname' => null]); |
|
| 39 | + |
|
| 40 | + // detect mb4 |
|
| 41 | + $tools = new MySqlTools(); |
|
| 42 | + if ($tools->supports4ByteCharset($connection)) { |
|
| 43 | + $this->config->setValue('mysql.utf8mb4', true); |
|
| 44 | + $connection = $this->connect(['dbname' => null]); |
|
| 45 | + } |
|
| 46 | + |
|
| 47 | + $this->createSpecificUser($username, $connection); |
|
| 48 | + |
|
| 49 | + //create the database |
|
| 50 | + $this->createDatabase($connection); |
|
| 51 | + |
|
| 52 | + //fill the database if needed |
|
| 53 | + $query='select count(*) from information_schema.tables where table_schema=? AND table_name = ?'; |
|
| 54 | + $connection->executeQuery($query, [$this->dbName, $this->tablePrefix.'users']); |
|
| 55 | + } |
|
| 56 | + |
|
| 57 | + /** |
|
| 58 | + * @param \OC\DB\Connection $connection |
|
| 59 | + */ |
|
| 60 | + private function createDatabase($connection) { |
|
| 61 | + try{ |
|
| 62 | + $name = $this->dbName; |
|
| 63 | + $user = $this->dbUser; |
|
| 64 | + //we can't use OC_DB functions here because we need to connect as the administrative user. |
|
| 65 | + $characterSet = $this->config->getValue('mysql.utf8mb4', false) ? 'utf8mb4' : 'utf8'; |
|
| 66 | + $query = "CREATE DATABASE IF NOT EXISTS `$name` CHARACTER SET $characterSet COLLATE ${characterSet}_bin;"; |
|
| 67 | + $connection->executeUpdate($query); |
|
| 68 | + } catch (\Exception $ex) { |
|
| 69 | + $this->logger->error('Database creation failed: {error}', [ |
|
| 70 | + 'app' => 'mysql.setup', |
|
| 71 | + 'error' => $ex->getMessage() |
|
| 72 | + ]); |
|
| 73 | + return; |
|
| 74 | + } |
|
| 75 | + |
|
| 76 | + try { |
|
| 77 | + //this query will fail if there aren't the right permissions, ignore the error |
|
| 78 | + $query="GRANT ALL PRIVILEGES ON `$name` . * TO '$user'"; |
|
| 79 | + $connection->executeUpdate($query); |
|
| 80 | + } catch (\Exception $ex) { |
|
| 81 | + $this->logger->debug('Could not automatically grant privileges, this can be ignored if database user already had privileges: {error}', [ |
|
| 82 | + 'app' => 'mysql.setup', |
|
| 83 | + 'error' => $ex->getMessage() |
|
| 84 | + ]); |
|
| 85 | + } |
|
| 86 | + } |
|
| 87 | + |
|
| 88 | + /** |
|
| 89 | + * @param IDBConnection $connection |
|
| 90 | + * @throws \OC\DatabaseSetupException |
|
| 91 | + */ |
|
| 92 | + private function createDBUser($connection) { |
|
| 93 | + try{ |
|
| 94 | + $name = $this->dbUser; |
|
| 95 | + $password = $this->dbPassword; |
|
| 96 | + // we need to create 2 accounts, one for global use and one for local user. if we don't specify the local one, |
|
| 97 | + // the anonymous user would take precedence when there is one. |
|
| 98 | + $query = "CREATE USER '$name'@'localhost' IDENTIFIED BY '$password'"; |
|
| 99 | + $connection->executeUpdate($query); |
|
| 100 | + $query = "CREATE USER '$name'@'%' IDENTIFIED BY '$password'"; |
|
| 101 | + $connection->executeUpdate($query); |
|
| 102 | + } |
|
| 103 | + catch (\Exception $ex){ |
|
| 104 | + $this->logger->error('Database User creation failed: {error}', [ |
|
| 105 | 105 | 'app' => 'mysql.setup', |
| 106 | 106 | 'error' => $ex->getMessage() |
| 107 | 107 | ]); |
| 108 | - } |
|
| 109 | - } |
|
| 110 | - |
|
| 111 | - /** |
|
| 112 | - * @param $username |
|
| 113 | - * @param IDBConnection $connection |
|
| 114 | - * @return array |
|
| 115 | - */ |
|
| 116 | - private function createSpecificUser($username, $connection) { |
|
| 117 | - try { |
|
| 118 | - //user already specified in config |
|
| 119 | - $oldUser = $this->config->getValue('dbuser', false); |
|
| 120 | - |
|
| 121 | - //we don't have a dbuser specified in config |
|
| 122 | - if ($this->dbUser !== $oldUser) { |
|
| 123 | - //add prefix to the admin username to prevent collisions |
|
| 124 | - $adminUser = substr('oc_' . $username, 0, 16); |
|
| 125 | - |
|
| 126 | - $i = 1; |
|
| 127 | - while (true) { |
|
| 128 | - //this should be enough to check for admin rights in mysql |
|
| 129 | - $query = 'SELECT user FROM mysql.user WHERE user=?'; |
|
| 130 | - $result = $connection->executeQuery($query, [$adminUser]); |
|
| 131 | - |
|
| 132 | - //current dbuser has admin rights |
|
| 133 | - if ($result) { |
|
| 134 | - $data = $result->fetchAll(); |
|
| 135 | - //new dbuser does not exist |
|
| 136 | - if (count($data) === 0) { |
|
| 137 | - //use the admin login data for the new database user |
|
| 138 | - $this->dbUser = $adminUser; |
|
| 139 | - |
|
| 140 | - //create a random password so we don't need to store the admin password in the config file |
|
| 141 | - $this->dbPassword = $this->random->generate(30); |
|
| 142 | - |
|
| 143 | - $this->createDBUser($connection); |
|
| 144 | - |
|
| 145 | - break; |
|
| 146 | - } else { |
|
| 147 | - //repeat with different username |
|
| 148 | - $length = strlen((string)$i); |
|
| 149 | - $adminUser = substr('oc_' . $username, 0, 16 - $length) . $i; |
|
| 150 | - $i++; |
|
| 151 | - } |
|
| 152 | - } else { |
|
| 153 | - break; |
|
| 154 | - } |
|
| 155 | - }; |
|
| 156 | - } |
|
| 157 | - } catch (\Exception $ex) { |
|
| 158 | - $this->logger->info('Can not create a new MySQL user, will continue with the provided user: {error}', [ |
|
| 159 | - 'app' => 'mysql.setup', |
|
| 160 | - 'error' => $ex->getMessage() |
|
| 161 | - ]); |
|
| 162 | - } |
|
| 163 | - |
|
| 164 | - $this->config->setValues([ |
|
| 165 | - 'dbuser' => $this->dbUser, |
|
| 166 | - 'dbpassword' => $this->dbPassword, |
|
| 167 | - ]); |
|
| 168 | - } |
|
| 108 | + } |
|
| 109 | + } |
|
| 110 | + |
|
| 111 | + /** |
|
| 112 | + * @param $username |
|
| 113 | + * @param IDBConnection $connection |
|
| 114 | + * @return array |
|
| 115 | + */ |
|
| 116 | + private function createSpecificUser($username, $connection) { |
|
| 117 | + try { |
|
| 118 | + //user already specified in config |
|
| 119 | + $oldUser = $this->config->getValue('dbuser', false); |
|
| 120 | + |
|
| 121 | + //we don't have a dbuser specified in config |
|
| 122 | + if ($this->dbUser !== $oldUser) { |
|
| 123 | + //add prefix to the admin username to prevent collisions |
|
| 124 | + $adminUser = substr('oc_' . $username, 0, 16); |
|
| 125 | + |
|
| 126 | + $i = 1; |
|
| 127 | + while (true) { |
|
| 128 | + //this should be enough to check for admin rights in mysql |
|
| 129 | + $query = 'SELECT user FROM mysql.user WHERE user=?'; |
|
| 130 | + $result = $connection->executeQuery($query, [$adminUser]); |
|
| 131 | + |
|
| 132 | + //current dbuser has admin rights |
|
| 133 | + if ($result) { |
|
| 134 | + $data = $result->fetchAll(); |
|
| 135 | + //new dbuser does not exist |
|
| 136 | + if (count($data) === 0) { |
|
| 137 | + //use the admin login data for the new database user |
|
| 138 | + $this->dbUser = $adminUser; |
|
| 139 | + |
|
| 140 | + //create a random password so we don't need to store the admin password in the config file |
|
| 141 | + $this->dbPassword = $this->random->generate(30); |
|
| 142 | + |
|
| 143 | + $this->createDBUser($connection); |
|
| 144 | + |
|
| 145 | + break; |
|
| 146 | + } else { |
|
| 147 | + //repeat with different username |
|
| 148 | + $length = strlen((string)$i); |
|
| 149 | + $adminUser = substr('oc_' . $username, 0, 16 - $length) . $i; |
|
| 150 | + $i++; |
|
| 151 | + } |
|
| 152 | + } else { |
|
| 153 | + break; |
|
| 154 | + } |
|
| 155 | + }; |
|
| 156 | + } |
|
| 157 | + } catch (\Exception $ex) { |
|
| 158 | + $this->logger->info('Can not create a new MySQL user, will continue with the provided user: {error}', [ |
|
| 159 | + 'app' => 'mysql.setup', |
|
| 160 | + 'error' => $ex->getMessage() |
|
| 161 | + ]); |
|
| 162 | + } |
|
| 163 | + |
|
| 164 | + $this->config->setValues([ |
|
| 165 | + 'dbuser' => $this->dbUser, |
|
| 166 | + 'dbpassword' => $this->dbPassword, |
|
| 167 | + ]); |
|
| 168 | + } |
|
| 169 | 169 | } |
@@ -50,7 +50,7 @@ discard block |
||
| 50 | 50 | $this->createDatabase($connection); |
| 51 | 51 | |
| 52 | 52 | //fill the database if needed |
| 53 | - $query='select count(*) from information_schema.tables where table_schema=? AND table_name = ?'; |
|
| 53 | + $query = 'select count(*) from information_schema.tables where table_schema=? AND table_name = ?'; |
|
| 54 | 54 | $connection->executeQuery($query, [$this->dbName, $this->tablePrefix.'users']); |
| 55 | 55 | } |
| 56 | 56 | |
@@ -58,7 +58,7 @@ discard block |
||
| 58 | 58 | * @param \OC\DB\Connection $connection |
| 59 | 59 | */ |
| 60 | 60 | private function createDatabase($connection) { |
| 61 | - try{ |
|
| 61 | + try { |
|
| 62 | 62 | $name = $this->dbName; |
| 63 | 63 | $user = $this->dbUser; |
| 64 | 64 | //we can't use OC_DB functions here because we need to connect as the administrative user. |
@@ -75,7 +75,7 @@ discard block |
||
| 75 | 75 | |
| 76 | 76 | try { |
| 77 | 77 | //this query will fail if there aren't the right permissions, ignore the error |
| 78 | - $query="GRANT ALL PRIVILEGES ON `$name` . * TO '$user'"; |
|
| 78 | + $query = "GRANT ALL PRIVILEGES ON `$name` . * TO '$user'"; |
|
| 79 | 79 | $connection->executeUpdate($query); |
| 80 | 80 | } catch (\Exception $ex) { |
| 81 | 81 | $this->logger->debug('Could not automatically grant privileges, this can be ignored if database user already had privileges: {error}', [ |
@@ -90,7 +90,7 @@ discard block |
||
| 90 | 90 | * @throws \OC\DatabaseSetupException |
| 91 | 91 | */ |
| 92 | 92 | private function createDBUser($connection) { |
| 93 | - try{ |
|
| 93 | + try { |
|
| 94 | 94 | $name = $this->dbUser; |
| 95 | 95 | $password = $this->dbPassword; |
| 96 | 96 | // we need to create 2 accounts, one for global use and one for local user. if we don't specify the local one, |
@@ -100,7 +100,7 @@ discard block |
||
| 100 | 100 | $query = "CREATE USER '$name'@'%' IDENTIFIED BY '$password'"; |
| 101 | 101 | $connection->executeUpdate($query); |
| 102 | 102 | } |
| 103 | - catch (\Exception $ex){ |
|
| 103 | + catch (\Exception $ex) { |
|
| 104 | 104 | $this->logger->error('Database User creation failed: {error}', [ |
| 105 | 105 | 'app' => 'mysql.setup', |
| 106 | 106 | 'error' => $ex->getMessage() |
@@ -121,7 +121,7 @@ discard block |
||
| 121 | 121 | //we don't have a dbuser specified in config |
| 122 | 122 | if ($this->dbUser !== $oldUser) { |
| 123 | 123 | //add prefix to the admin username to prevent collisions |
| 124 | - $adminUser = substr('oc_' . $username, 0, 16); |
|
| 124 | + $adminUser = substr('oc_'.$username, 0, 16); |
|
| 125 | 125 | |
| 126 | 126 | $i = 1; |
| 127 | 127 | while (true) { |
@@ -138,15 +138,15 @@ discard block |
||
| 138 | 138 | $this->dbUser = $adminUser; |
| 139 | 139 | |
| 140 | 140 | //create a random password so we don't need to store the admin password in the config file |
| 141 | - $this->dbPassword = $this->random->generate(30); |
|
| 141 | + $this->dbPassword = $this->random->generate(30); |
|
| 142 | 142 | |
| 143 | 143 | $this->createDBUser($connection); |
| 144 | 144 | |
| 145 | 145 | break; |
| 146 | 146 | } else { |
| 147 | 147 | //repeat with different username |
| 148 | - $length = strlen((string)$i); |
|
| 149 | - $adminUser = substr('oc_' . $username, 0, 16 - $length) . $i; |
|
| 148 | + $length = strlen((string) $i); |
|
| 149 | + $adminUser = substr('oc_'.$username, 0, 16 - $length).$i; |
|
| 150 | 150 | $i++; |
| 151 | 151 | } |
| 152 | 152 | } else { |
@@ -244,7 +244,7 @@ discard block |
||
| 244 | 244 | } |
| 245 | 245 | |
| 246 | 246 | $mount = $file->getMountPoint(); |
| 247 | - if ($mount and !$mount->getOption('previews', true)){ |
|
| 247 | + if ($mount and !$mount->getOption('previews', true)) { |
|
| 248 | 248 | return false; |
| 249 | 249 | } |
| 250 | 250 | |
@@ -330,7 +330,7 @@ discard block |
||
| 330 | 330 | */ |
| 331 | 331 | protected function registerCoreProvider($class, $mimeType, $options = []) { |
| 332 | 332 | if (in_array(trim($class, '\\'), $this->getEnabledDefaultProvider())) { |
| 333 | - $this->registerProvider($mimeType, function () use ($class, $options) { |
|
| 333 | + $this->registerProvider($mimeType, function() use ($class, $options) { |
|
| 334 | 334 | return new $class($options); |
| 335 | 335 | }); |
| 336 | 336 | } |
@@ -38,385 +38,385 @@ |
||
| 38 | 38 | use Symfony\Component\EventDispatcher\EventDispatcherInterface; |
| 39 | 39 | |
| 40 | 40 | class PreviewManager implements IPreview { |
| 41 | - /** @var IConfig */ |
|
| 42 | - protected $config; |
|
| 43 | - |
|
| 44 | - /** @var IRootFolder */ |
|
| 45 | - protected $rootFolder; |
|
| 46 | - |
|
| 47 | - /** @var IAppData */ |
|
| 48 | - protected $appData; |
|
| 49 | - |
|
| 50 | - /** @var EventDispatcherInterface */ |
|
| 51 | - protected $eventDispatcher; |
|
| 52 | - |
|
| 53 | - /** @var Generator */ |
|
| 54 | - private $generator; |
|
| 55 | - |
|
| 56 | - /** @var bool */ |
|
| 57 | - protected $providerListDirty = false; |
|
| 58 | - |
|
| 59 | - /** @var bool */ |
|
| 60 | - protected $registeredCoreProviders = false; |
|
| 61 | - |
|
| 62 | - /** @var array */ |
|
| 63 | - protected $providers = []; |
|
| 64 | - |
|
| 65 | - /** @var array mime type => support status */ |
|
| 66 | - protected $mimeTypeSupportMap = []; |
|
| 67 | - |
|
| 68 | - /** @var array */ |
|
| 69 | - protected $defaultProviders; |
|
| 70 | - |
|
| 71 | - /** @var string */ |
|
| 72 | - protected $userId; |
|
| 73 | - |
|
| 74 | - /** |
|
| 75 | - * PreviewManager constructor. |
|
| 76 | - * |
|
| 77 | - * @param IConfig $config |
|
| 78 | - * @param IRootFolder $rootFolder |
|
| 79 | - * @param IAppData $appData |
|
| 80 | - * @param EventDispatcherInterface $eventDispatcher |
|
| 81 | - * @param string $userId |
|
| 82 | - */ |
|
| 83 | - public function __construct(IConfig $config, |
|
| 84 | - IRootFolder $rootFolder, |
|
| 85 | - IAppData $appData, |
|
| 86 | - EventDispatcherInterface $eventDispatcher, |
|
| 87 | - $userId) { |
|
| 88 | - $this->config = $config; |
|
| 89 | - $this->rootFolder = $rootFolder; |
|
| 90 | - $this->appData = $appData; |
|
| 91 | - $this->eventDispatcher = $eventDispatcher; |
|
| 92 | - $this->userId = $userId; |
|
| 93 | - } |
|
| 94 | - |
|
| 95 | - /** |
|
| 96 | - * In order to improve lazy loading a closure can be registered which will be |
|
| 97 | - * called in case preview providers are actually requested |
|
| 98 | - * |
|
| 99 | - * $callable has to return an instance of \OCP\Preview\IProvider |
|
| 100 | - * |
|
| 101 | - * @param string $mimeTypeRegex Regex with the mime types that are supported by this provider |
|
| 102 | - * @param \Closure $callable |
|
| 103 | - * @return void |
|
| 104 | - */ |
|
| 105 | - public function registerProvider($mimeTypeRegex, \Closure $callable) { |
|
| 106 | - if (!$this->config->getSystemValue('enable_previews', true)) { |
|
| 107 | - return; |
|
| 108 | - } |
|
| 109 | - |
|
| 110 | - if (!isset($this->providers[$mimeTypeRegex])) { |
|
| 111 | - $this->providers[$mimeTypeRegex] = []; |
|
| 112 | - } |
|
| 113 | - $this->providers[$mimeTypeRegex][] = $callable; |
|
| 114 | - $this->providerListDirty = true; |
|
| 115 | - } |
|
| 116 | - |
|
| 117 | - /** |
|
| 118 | - * Get all providers |
|
| 119 | - * @return array |
|
| 120 | - */ |
|
| 121 | - public function getProviders() { |
|
| 122 | - if (!$this->config->getSystemValue('enable_previews', true)) { |
|
| 123 | - return []; |
|
| 124 | - } |
|
| 125 | - |
|
| 126 | - $this->registerCoreProviders(); |
|
| 127 | - if ($this->providerListDirty) { |
|
| 128 | - $keys = array_map('strlen', array_keys($this->providers)); |
|
| 129 | - array_multisort($keys, SORT_DESC, $this->providers); |
|
| 130 | - $this->providerListDirty = false; |
|
| 131 | - } |
|
| 132 | - |
|
| 133 | - return $this->providers; |
|
| 134 | - } |
|
| 135 | - |
|
| 136 | - /** |
|
| 137 | - * Does the manager have any providers |
|
| 138 | - * @return bool |
|
| 139 | - */ |
|
| 140 | - public function hasProviders() { |
|
| 141 | - $this->registerCoreProviders(); |
|
| 142 | - return !empty($this->providers); |
|
| 143 | - } |
|
| 144 | - |
|
| 145 | - /** |
|
| 146 | - * return a preview of a file |
|
| 147 | - * |
|
| 148 | - * @param string $file The path to the file where you want a thumbnail from |
|
| 149 | - * @param int $maxX The maximum X size of the thumbnail. It can be smaller depending on the shape of the image |
|
| 150 | - * @param int $maxY The maximum Y size of the thumbnail. It can be smaller depending on the shape of the image |
|
| 151 | - * @param boolean $scaleUp Scale smaller images up to the thumbnail size or not. Might look ugly |
|
| 152 | - * @return \OCP\IImage |
|
| 153 | - * @deprecated 11 Use getPreview |
|
| 154 | - */ |
|
| 155 | - public function createPreview($file, $maxX = 100, $maxY = 75, $scaleUp = false) { |
|
| 156 | - try { |
|
| 157 | - $userRoot = $this->rootFolder->getUserFolder($this->userId)->getParent(); |
|
| 158 | - $node = $userRoot->get($file); |
|
| 159 | - if (!($file instanceof File)) { |
|
| 160 | - throw new NotFoundException(); |
|
| 161 | - } |
|
| 162 | - |
|
| 163 | - $preview = $this->getPreview($node, $maxX, $maxY); |
|
| 164 | - } catch (\Exception $e) { |
|
| 165 | - return new \OC_Image(); |
|
| 166 | - } |
|
| 167 | - |
|
| 168 | - return new \OC_Image($preview->getContent()); |
|
| 169 | - } |
|
| 170 | - |
|
| 171 | - /** |
|
| 172 | - * Returns a preview of a file |
|
| 173 | - * |
|
| 174 | - * The cache is searched first and if nothing usable was found then a preview is |
|
| 175 | - * generated by one of the providers |
|
| 176 | - * |
|
| 177 | - * @param File $file |
|
| 178 | - * @param int $width |
|
| 179 | - * @param int $height |
|
| 180 | - * @param bool $crop |
|
| 181 | - * @param string $mode |
|
| 182 | - * @param string $mimeType |
|
| 183 | - * @return ISimpleFile |
|
| 184 | - * @throws NotFoundException |
|
| 185 | - * @throws \InvalidArgumentException if the preview would be invalid (in case the original image is invalid) |
|
| 186 | - * @since 11.0.0 - \InvalidArgumentException was added in 12.0.0 |
|
| 187 | - */ |
|
| 188 | - public function getPreview(File $file, $width = -1, $height = -1, $crop = false, $mode = IPreview::MODE_FILL, $mimeType = null) { |
|
| 189 | - if ($this->generator === null) { |
|
| 190 | - $this->generator = new Generator( |
|
| 191 | - $this->config, |
|
| 192 | - $this, |
|
| 193 | - $this->appData, |
|
| 194 | - new GeneratorHelper( |
|
| 195 | - $this->rootFolder |
|
| 196 | - ), |
|
| 197 | - $this->eventDispatcher |
|
| 198 | - ); |
|
| 199 | - } |
|
| 200 | - |
|
| 201 | - return $this->generator->getPreview($file, $width, $height, $crop, $mode, $mimeType); |
|
| 202 | - } |
|
| 203 | - |
|
| 204 | - /** |
|
| 205 | - * returns true if the passed mime type is supported |
|
| 206 | - * |
|
| 207 | - * @param string $mimeType |
|
| 208 | - * @return boolean |
|
| 209 | - */ |
|
| 210 | - public function isMimeSupported($mimeType = '*') { |
|
| 211 | - if (!$this->config->getSystemValue('enable_previews', true)) { |
|
| 212 | - return false; |
|
| 213 | - } |
|
| 214 | - |
|
| 215 | - if (isset($this->mimeTypeSupportMap[$mimeType])) { |
|
| 216 | - return $this->mimeTypeSupportMap[$mimeType]; |
|
| 217 | - } |
|
| 218 | - |
|
| 219 | - $this->registerCoreProviders(); |
|
| 220 | - $providerMimeTypes = array_keys($this->providers); |
|
| 221 | - foreach ($providerMimeTypes as $supportedMimeType) { |
|
| 222 | - if (preg_match($supportedMimeType, $mimeType)) { |
|
| 223 | - $this->mimeTypeSupportMap[$mimeType] = true; |
|
| 224 | - return true; |
|
| 225 | - } |
|
| 226 | - } |
|
| 227 | - $this->mimeTypeSupportMap[$mimeType] = false; |
|
| 228 | - return false; |
|
| 229 | - } |
|
| 230 | - |
|
| 231 | - /** |
|
| 232 | - * Check if a preview can be generated for a file |
|
| 233 | - * |
|
| 234 | - * @param \OCP\Files\FileInfo $file |
|
| 235 | - * @return bool |
|
| 236 | - */ |
|
| 237 | - public function isAvailable(\OCP\Files\FileInfo $file) { |
|
| 238 | - if (!$this->config->getSystemValue('enable_previews', true)) { |
|
| 239 | - return false; |
|
| 240 | - } |
|
| 241 | - |
|
| 242 | - $this->registerCoreProviders(); |
|
| 243 | - if (!$this->isMimeSupported($file->getMimetype())) { |
|
| 244 | - return false; |
|
| 245 | - } |
|
| 246 | - |
|
| 247 | - $mount = $file->getMountPoint(); |
|
| 248 | - if ($mount and !$mount->getOption('previews', true)){ |
|
| 249 | - return false; |
|
| 250 | - } |
|
| 251 | - |
|
| 252 | - foreach ($this->providers as $supportedMimeType => $providers) { |
|
| 253 | - if (preg_match($supportedMimeType, $file->getMimetype())) { |
|
| 254 | - foreach ($providers as $closure) { |
|
| 255 | - $provider = $closure(); |
|
| 256 | - if (!($provider instanceof IProvider)) { |
|
| 257 | - continue; |
|
| 258 | - } |
|
| 259 | - |
|
| 260 | - /** @var $provider IProvider */ |
|
| 261 | - if ($provider->isAvailable($file)) { |
|
| 262 | - return true; |
|
| 263 | - } |
|
| 264 | - } |
|
| 265 | - } |
|
| 266 | - } |
|
| 267 | - return false; |
|
| 268 | - } |
|
| 269 | - |
|
| 270 | - /** |
|
| 271 | - * List of enabled default providers |
|
| 272 | - * |
|
| 273 | - * The following providers are enabled by default: |
|
| 274 | - * - OC\Preview\PNG |
|
| 275 | - * - OC\Preview\JPEG |
|
| 276 | - * - OC\Preview\GIF |
|
| 277 | - * - OC\Preview\BMP |
|
| 278 | - * - OC\Preview\XBitmap |
|
| 279 | - * - OC\Preview\MarkDown |
|
| 280 | - * - OC\Preview\MP3 |
|
| 281 | - * - OC\Preview\TXT |
|
| 282 | - * |
|
| 283 | - * The following providers are disabled by default due to performance or privacy concerns: |
|
| 284 | - * - OC\Preview\Font |
|
| 285 | - * - OC\Preview\Illustrator |
|
| 286 | - * - OC\Preview\Movie |
|
| 287 | - * - OC\Preview\MSOfficeDoc |
|
| 288 | - * - OC\Preview\MSOffice2003 |
|
| 289 | - * - OC\Preview\MSOffice2007 |
|
| 290 | - * - OC\Preview\OpenDocument |
|
| 291 | - * - OC\Preview\PDF |
|
| 292 | - * - OC\Preview\Photoshop |
|
| 293 | - * - OC\Preview\Postscript |
|
| 294 | - * - OC\Preview\StarOffice |
|
| 295 | - * - OC\Preview\SVG |
|
| 296 | - * - OC\Preview\TIFF |
|
| 297 | - * |
|
| 298 | - * @return array |
|
| 299 | - */ |
|
| 300 | - protected function getEnabledDefaultProvider() { |
|
| 301 | - if ($this->defaultProviders !== null) { |
|
| 302 | - return $this->defaultProviders; |
|
| 303 | - } |
|
| 304 | - |
|
| 305 | - $imageProviders = [ |
|
| 306 | - 'OC\Preview\PNG', |
|
| 307 | - 'OC\Preview\JPEG', |
|
| 308 | - 'OC\Preview\GIF', |
|
| 309 | - 'OC\Preview\BMP', |
|
| 310 | - 'OC\Preview\XBitmap' |
|
| 311 | - ]; |
|
| 312 | - |
|
| 313 | - $this->defaultProviders = $this->config->getSystemValue('enabledPreviewProviders', array_merge([ |
|
| 314 | - 'OC\Preview\MarkDown', |
|
| 315 | - 'OC\Preview\MP3', |
|
| 316 | - 'OC\Preview\TXT', |
|
| 317 | - ], $imageProviders)); |
|
| 318 | - |
|
| 319 | - if (in_array('OC\Preview\Image', $this->defaultProviders)) { |
|
| 320 | - $this->defaultProviders = array_merge($this->defaultProviders, $imageProviders); |
|
| 321 | - } |
|
| 322 | - $this->defaultProviders = array_unique($this->defaultProviders); |
|
| 323 | - return $this->defaultProviders; |
|
| 324 | - } |
|
| 325 | - |
|
| 326 | - /** |
|
| 327 | - * Register the default providers (if enabled) |
|
| 328 | - * |
|
| 329 | - * @param string $class |
|
| 330 | - * @param string $mimeType |
|
| 331 | - */ |
|
| 332 | - protected function registerCoreProvider($class, $mimeType, $options = []) { |
|
| 333 | - if (in_array(trim($class, '\\'), $this->getEnabledDefaultProvider())) { |
|
| 334 | - $this->registerProvider($mimeType, function () use ($class, $options) { |
|
| 335 | - return new $class($options); |
|
| 336 | - }); |
|
| 337 | - } |
|
| 338 | - } |
|
| 339 | - |
|
| 340 | - /** |
|
| 341 | - * Register the default providers (if enabled) |
|
| 342 | - */ |
|
| 343 | - protected function registerCoreProviders() { |
|
| 344 | - if ($this->registeredCoreProviders) { |
|
| 345 | - return; |
|
| 346 | - } |
|
| 347 | - $this->registeredCoreProviders = true; |
|
| 348 | - |
|
| 349 | - $this->registerCoreProvider('OC\Preview\TXT', '/text\/plain/'); |
|
| 350 | - $this->registerCoreProvider('OC\Preview\MarkDown', '/text\/(x-)?markdown/'); |
|
| 351 | - $this->registerCoreProvider('OC\Preview\PNG', '/image\/png/'); |
|
| 352 | - $this->registerCoreProvider('OC\Preview\JPEG', '/image\/jpeg/'); |
|
| 353 | - $this->registerCoreProvider('OC\Preview\GIF', '/image\/gif/'); |
|
| 354 | - $this->registerCoreProvider('OC\Preview\BMP', '/image\/bmp/'); |
|
| 355 | - $this->registerCoreProvider('OC\Preview\XBitmap', '/image\/x-xbitmap/'); |
|
| 356 | - $this->registerCoreProvider('OC\Preview\MP3', '/audio\/mpeg/'); |
|
| 357 | - |
|
| 358 | - // SVG, Office and Bitmap require imagick |
|
| 359 | - if (extension_loaded('imagick')) { |
|
| 360 | - $checkImagick = new \Imagick(); |
|
| 361 | - |
|
| 362 | - $imagickProviders = [ |
|
| 363 | - 'SVG' => ['mimetype' => '/image\/svg\+xml/', 'class' => '\OC\Preview\SVG'], |
|
| 364 | - 'TIFF' => ['mimetype' => '/image\/tiff/', 'class' => '\OC\Preview\TIFF'], |
|
| 365 | - 'PDF' => ['mimetype' => '/application\/pdf/', 'class' => '\OC\Preview\PDF'], |
|
| 366 | - 'AI' => ['mimetype' => '/application\/illustrator/', 'class' => '\OC\Preview\Illustrator'], |
|
| 367 | - 'PSD' => ['mimetype' => '/application\/x-photoshop/', 'class' => '\OC\Preview\Photoshop'], |
|
| 368 | - 'EPS' => ['mimetype' => '/application\/postscript/', 'class' => '\OC\Preview\Postscript'], |
|
| 369 | - 'TTF' => ['mimetype' => '/application\/(?:font-sfnt|x-font$)/', 'class' => '\OC\Preview\Font'], |
|
| 370 | - ]; |
|
| 371 | - |
|
| 372 | - foreach ($imagickProviders as $queryFormat => $provider) { |
|
| 373 | - $class = $provider['class']; |
|
| 374 | - if (!in_array(trim($class, '\\'), $this->getEnabledDefaultProvider())) { |
|
| 375 | - continue; |
|
| 376 | - } |
|
| 377 | - |
|
| 378 | - if (count($checkImagick->queryFormats($queryFormat)) === 1) { |
|
| 379 | - $this->registerCoreProvider($class, $provider['mimetype']); |
|
| 380 | - } |
|
| 381 | - } |
|
| 382 | - |
|
| 383 | - if (count($checkImagick->queryFormats('PDF')) === 1) { |
|
| 384 | - if (\OC_Helper::is_function_enabled('shell_exec')) { |
|
| 385 | - $officeFound = is_string($this->config->getSystemValue('preview_libreoffice_path', null)); |
|
| 386 | - |
|
| 387 | - if (!$officeFound) { |
|
| 388 | - //let's see if there is libreoffice or openoffice on this machine |
|
| 389 | - $whichLibreOffice = shell_exec('command -v libreoffice'); |
|
| 390 | - $officeFound = !empty($whichLibreOffice); |
|
| 391 | - if (!$officeFound) { |
|
| 392 | - $whichOpenOffice = shell_exec('command -v openoffice'); |
|
| 393 | - $officeFound = !empty($whichOpenOffice); |
|
| 394 | - } |
|
| 395 | - } |
|
| 396 | - |
|
| 397 | - if ($officeFound) { |
|
| 398 | - $this->registerCoreProvider('\OC\Preview\MSOfficeDoc', '/application\/msword/'); |
|
| 399 | - $this->registerCoreProvider('\OC\Preview\MSOffice2003', '/application\/vnd.ms-.*/'); |
|
| 400 | - $this->registerCoreProvider('\OC\Preview\MSOffice2007', '/application\/vnd.openxmlformats-officedocument.*/'); |
|
| 401 | - $this->registerCoreProvider('\OC\Preview\OpenDocument', '/application\/vnd.oasis.opendocument.*/'); |
|
| 402 | - $this->registerCoreProvider('\OC\Preview\StarOffice', '/application\/vnd.sun.xml.*/'); |
|
| 403 | - } |
|
| 404 | - } |
|
| 405 | - } |
|
| 406 | - } |
|
| 407 | - |
|
| 408 | - // Video requires avconv or ffmpeg |
|
| 409 | - if (in_array('OC\Preview\Movie', $this->getEnabledDefaultProvider())) { |
|
| 410 | - $avconvBinary = \OC_Helper::findBinaryPath('avconv'); |
|
| 411 | - $ffmpegBinary = ($avconvBinary) ? null : \OC_Helper::findBinaryPath('ffmpeg'); |
|
| 412 | - |
|
| 413 | - if ($avconvBinary || $ffmpegBinary) { |
|
| 414 | - // FIXME // a bit hacky but didn't want to use subclasses |
|
| 415 | - \OC\Preview\Movie::$avconvBinary = $avconvBinary; |
|
| 416 | - \OC\Preview\Movie::$ffmpegBinary = $ffmpegBinary; |
|
| 417 | - |
|
| 418 | - $this->registerCoreProvider('\OC\Preview\Movie', '/video\/.*/'); |
|
| 419 | - } |
|
| 420 | - } |
|
| 421 | - } |
|
| 41 | + /** @var IConfig */ |
|
| 42 | + protected $config; |
|
| 43 | + |
|
| 44 | + /** @var IRootFolder */ |
|
| 45 | + protected $rootFolder; |
|
| 46 | + |
|
| 47 | + /** @var IAppData */ |
|
| 48 | + protected $appData; |
|
| 49 | + |
|
| 50 | + /** @var EventDispatcherInterface */ |
|
| 51 | + protected $eventDispatcher; |
|
| 52 | + |
|
| 53 | + /** @var Generator */ |
|
| 54 | + private $generator; |
|
| 55 | + |
|
| 56 | + /** @var bool */ |
|
| 57 | + protected $providerListDirty = false; |
|
| 58 | + |
|
| 59 | + /** @var bool */ |
|
| 60 | + protected $registeredCoreProviders = false; |
|
| 61 | + |
|
| 62 | + /** @var array */ |
|
| 63 | + protected $providers = []; |
|
| 64 | + |
|
| 65 | + /** @var array mime type => support status */ |
|
| 66 | + protected $mimeTypeSupportMap = []; |
|
| 67 | + |
|
| 68 | + /** @var array */ |
|
| 69 | + protected $defaultProviders; |
|
| 70 | + |
|
| 71 | + /** @var string */ |
|
| 72 | + protected $userId; |
|
| 73 | + |
|
| 74 | + /** |
|
| 75 | + * PreviewManager constructor. |
|
| 76 | + * |
|
| 77 | + * @param IConfig $config |
|
| 78 | + * @param IRootFolder $rootFolder |
|
| 79 | + * @param IAppData $appData |
|
| 80 | + * @param EventDispatcherInterface $eventDispatcher |
|
| 81 | + * @param string $userId |
|
| 82 | + */ |
|
| 83 | + public function __construct(IConfig $config, |
|
| 84 | + IRootFolder $rootFolder, |
|
| 85 | + IAppData $appData, |
|
| 86 | + EventDispatcherInterface $eventDispatcher, |
|
| 87 | + $userId) { |
|
| 88 | + $this->config = $config; |
|
| 89 | + $this->rootFolder = $rootFolder; |
|
| 90 | + $this->appData = $appData; |
|
| 91 | + $this->eventDispatcher = $eventDispatcher; |
|
| 92 | + $this->userId = $userId; |
|
| 93 | + } |
|
| 94 | + |
|
| 95 | + /** |
|
| 96 | + * In order to improve lazy loading a closure can be registered which will be |
|
| 97 | + * called in case preview providers are actually requested |
|
| 98 | + * |
|
| 99 | + * $callable has to return an instance of \OCP\Preview\IProvider |
|
| 100 | + * |
|
| 101 | + * @param string $mimeTypeRegex Regex with the mime types that are supported by this provider |
|
| 102 | + * @param \Closure $callable |
|
| 103 | + * @return void |
|
| 104 | + */ |
|
| 105 | + public function registerProvider($mimeTypeRegex, \Closure $callable) { |
|
| 106 | + if (!$this->config->getSystemValue('enable_previews', true)) { |
|
| 107 | + return; |
|
| 108 | + } |
|
| 109 | + |
|
| 110 | + if (!isset($this->providers[$mimeTypeRegex])) { |
|
| 111 | + $this->providers[$mimeTypeRegex] = []; |
|
| 112 | + } |
|
| 113 | + $this->providers[$mimeTypeRegex][] = $callable; |
|
| 114 | + $this->providerListDirty = true; |
|
| 115 | + } |
|
| 116 | + |
|
| 117 | + /** |
|
| 118 | + * Get all providers |
|
| 119 | + * @return array |
|
| 120 | + */ |
|
| 121 | + public function getProviders() { |
|
| 122 | + if (!$this->config->getSystemValue('enable_previews', true)) { |
|
| 123 | + return []; |
|
| 124 | + } |
|
| 125 | + |
|
| 126 | + $this->registerCoreProviders(); |
|
| 127 | + if ($this->providerListDirty) { |
|
| 128 | + $keys = array_map('strlen', array_keys($this->providers)); |
|
| 129 | + array_multisort($keys, SORT_DESC, $this->providers); |
|
| 130 | + $this->providerListDirty = false; |
|
| 131 | + } |
|
| 132 | + |
|
| 133 | + return $this->providers; |
|
| 134 | + } |
|
| 135 | + |
|
| 136 | + /** |
|
| 137 | + * Does the manager have any providers |
|
| 138 | + * @return bool |
|
| 139 | + */ |
|
| 140 | + public function hasProviders() { |
|
| 141 | + $this->registerCoreProviders(); |
|
| 142 | + return !empty($this->providers); |
|
| 143 | + } |
|
| 144 | + |
|
| 145 | + /** |
|
| 146 | + * return a preview of a file |
|
| 147 | + * |
|
| 148 | + * @param string $file The path to the file where you want a thumbnail from |
|
| 149 | + * @param int $maxX The maximum X size of the thumbnail. It can be smaller depending on the shape of the image |
|
| 150 | + * @param int $maxY The maximum Y size of the thumbnail. It can be smaller depending on the shape of the image |
|
| 151 | + * @param boolean $scaleUp Scale smaller images up to the thumbnail size or not. Might look ugly |
|
| 152 | + * @return \OCP\IImage |
|
| 153 | + * @deprecated 11 Use getPreview |
|
| 154 | + */ |
|
| 155 | + public function createPreview($file, $maxX = 100, $maxY = 75, $scaleUp = false) { |
|
| 156 | + try { |
|
| 157 | + $userRoot = $this->rootFolder->getUserFolder($this->userId)->getParent(); |
|
| 158 | + $node = $userRoot->get($file); |
|
| 159 | + if (!($file instanceof File)) { |
|
| 160 | + throw new NotFoundException(); |
|
| 161 | + } |
|
| 162 | + |
|
| 163 | + $preview = $this->getPreview($node, $maxX, $maxY); |
|
| 164 | + } catch (\Exception $e) { |
|
| 165 | + return new \OC_Image(); |
|
| 166 | + } |
|
| 167 | + |
|
| 168 | + return new \OC_Image($preview->getContent()); |
|
| 169 | + } |
|
| 170 | + |
|
| 171 | + /** |
|
| 172 | + * Returns a preview of a file |
|
| 173 | + * |
|
| 174 | + * The cache is searched first and if nothing usable was found then a preview is |
|
| 175 | + * generated by one of the providers |
|
| 176 | + * |
|
| 177 | + * @param File $file |
|
| 178 | + * @param int $width |
|
| 179 | + * @param int $height |
|
| 180 | + * @param bool $crop |
|
| 181 | + * @param string $mode |
|
| 182 | + * @param string $mimeType |
|
| 183 | + * @return ISimpleFile |
|
| 184 | + * @throws NotFoundException |
|
| 185 | + * @throws \InvalidArgumentException if the preview would be invalid (in case the original image is invalid) |
|
| 186 | + * @since 11.0.0 - \InvalidArgumentException was added in 12.0.0 |
|
| 187 | + */ |
|
| 188 | + public function getPreview(File $file, $width = -1, $height = -1, $crop = false, $mode = IPreview::MODE_FILL, $mimeType = null) { |
|
| 189 | + if ($this->generator === null) { |
|
| 190 | + $this->generator = new Generator( |
|
| 191 | + $this->config, |
|
| 192 | + $this, |
|
| 193 | + $this->appData, |
|
| 194 | + new GeneratorHelper( |
|
| 195 | + $this->rootFolder |
|
| 196 | + ), |
|
| 197 | + $this->eventDispatcher |
|
| 198 | + ); |
|
| 199 | + } |
|
| 200 | + |
|
| 201 | + return $this->generator->getPreview($file, $width, $height, $crop, $mode, $mimeType); |
|
| 202 | + } |
|
| 203 | + |
|
| 204 | + /** |
|
| 205 | + * returns true if the passed mime type is supported |
|
| 206 | + * |
|
| 207 | + * @param string $mimeType |
|
| 208 | + * @return boolean |
|
| 209 | + */ |
|
| 210 | + public function isMimeSupported($mimeType = '*') { |
|
| 211 | + if (!$this->config->getSystemValue('enable_previews', true)) { |
|
| 212 | + return false; |
|
| 213 | + } |
|
| 214 | + |
|
| 215 | + if (isset($this->mimeTypeSupportMap[$mimeType])) { |
|
| 216 | + return $this->mimeTypeSupportMap[$mimeType]; |
|
| 217 | + } |
|
| 218 | + |
|
| 219 | + $this->registerCoreProviders(); |
|
| 220 | + $providerMimeTypes = array_keys($this->providers); |
|
| 221 | + foreach ($providerMimeTypes as $supportedMimeType) { |
|
| 222 | + if (preg_match($supportedMimeType, $mimeType)) { |
|
| 223 | + $this->mimeTypeSupportMap[$mimeType] = true; |
|
| 224 | + return true; |
|
| 225 | + } |
|
| 226 | + } |
|
| 227 | + $this->mimeTypeSupportMap[$mimeType] = false; |
|
| 228 | + return false; |
|
| 229 | + } |
|
| 230 | + |
|
| 231 | + /** |
|
| 232 | + * Check if a preview can be generated for a file |
|
| 233 | + * |
|
| 234 | + * @param \OCP\Files\FileInfo $file |
|
| 235 | + * @return bool |
|
| 236 | + */ |
|
| 237 | + public function isAvailable(\OCP\Files\FileInfo $file) { |
|
| 238 | + if (!$this->config->getSystemValue('enable_previews', true)) { |
|
| 239 | + return false; |
|
| 240 | + } |
|
| 241 | + |
|
| 242 | + $this->registerCoreProviders(); |
|
| 243 | + if (!$this->isMimeSupported($file->getMimetype())) { |
|
| 244 | + return false; |
|
| 245 | + } |
|
| 246 | + |
|
| 247 | + $mount = $file->getMountPoint(); |
|
| 248 | + if ($mount and !$mount->getOption('previews', true)){ |
|
| 249 | + return false; |
|
| 250 | + } |
|
| 251 | + |
|
| 252 | + foreach ($this->providers as $supportedMimeType => $providers) { |
|
| 253 | + if (preg_match($supportedMimeType, $file->getMimetype())) { |
|
| 254 | + foreach ($providers as $closure) { |
|
| 255 | + $provider = $closure(); |
|
| 256 | + if (!($provider instanceof IProvider)) { |
|
| 257 | + continue; |
|
| 258 | + } |
|
| 259 | + |
|
| 260 | + /** @var $provider IProvider */ |
|
| 261 | + if ($provider->isAvailable($file)) { |
|
| 262 | + return true; |
|
| 263 | + } |
|
| 264 | + } |
|
| 265 | + } |
|
| 266 | + } |
|
| 267 | + return false; |
|
| 268 | + } |
|
| 269 | + |
|
| 270 | + /** |
|
| 271 | + * List of enabled default providers |
|
| 272 | + * |
|
| 273 | + * The following providers are enabled by default: |
|
| 274 | + * - OC\Preview\PNG |
|
| 275 | + * - OC\Preview\JPEG |
|
| 276 | + * - OC\Preview\GIF |
|
| 277 | + * - OC\Preview\BMP |
|
| 278 | + * - OC\Preview\XBitmap |
|
| 279 | + * - OC\Preview\MarkDown |
|
| 280 | + * - OC\Preview\MP3 |
|
| 281 | + * - OC\Preview\TXT |
|
| 282 | + * |
|
| 283 | + * The following providers are disabled by default due to performance or privacy concerns: |
|
| 284 | + * - OC\Preview\Font |
|
| 285 | + * - OC\Preview\Illustrator |
|
| 286 | + * - OC\Preview\Movie |
|
| 287 | + * - OC\Preview\MSOfficeDoc |
|
| 288 | + * - OC\Preview\MSOffice2003 |
|
| 289 | + * - OC\Preview\MSOffice2007 |
|
| 290 | + * - OC\Preview\OpenDocument |
|
| 291 | + * - OC\Preview\PDF |
|
| 292 | + * - OC\Preview\Photoshop |
|
| 293 | + * - OC\Preview\Postscript |
|
| 294 | + * - OC\Preview\StarOffice |
|
| 295 | + * - OC\Preview\SVG |
|
| 296 | + * - OC\Preview\TIFF |
|
| 297 | + * |
|
| 298 | + * @return array |
|
| 299 | + */ |
|
| 300 | + protected function getEnabledDefaultProvider() { |
|
| 301 | + if ($this->defaultProviders !== null) { |
|
| 302 | + return $this->defaultProviders; |
|
| 303 | + } |
|
| 304 | + |
|
| 305 | + $imageProviders = [ |
|
| 306 | + 'OC\Preview\PNG', |
|
| 307 | + 'OC\Preview\JPEG', |
|
| 308 | + 'OC\Preview\GIF', |
|
| 309 | + 'OC\Preview\BMP', |
|
| 310 | + 'OC\Preview\XBitmap' |
|
| 311 | + ]; |
|
| 312 | + |
|
| 313 | + $this->defaultProviders = $this->config->getSystemValue('enabledPreviewProviders', array_merge([ |
|
| 314 | + 'OC\Preview\MarkDown', |
|
| 315 | + 'OC\Preview\MP3', |
|
| 316 | + 'OC\Preview\TXT', |
|
| 317 | + ], $imageProviders)); |
|
| 318 | + |
|
| 319 | + if (in_array('OC\Preview\Image', $this->defaultProviders)) { |
|
| 320 | + $this->defaultProviders = array_merge($this->defaultProviders, $imageProviders); |
|
| 321 | + } |
|
| 322 | + $this->defaultProviders = array_unique($this->defaultProviders); |
|
| 323 | + return $this->defaultProviders; |
|
| 324 | + } |
|
| 325 | + |
|
| 326 | + /** |
|
| 327 | + * Register the default providers (if enabled) |
|
| 328 | + * |
|
| 329 | + * @param string $class |
|
| 330 | + * @param string $mimeType |
|
| 331 | + */ |
|
| 332 | + protected function registerCoreProvider($class, $mimeType, $options = []) { |
|
| 333 | + if (in_array(trim($class, '\\'), $this->getEnabledDefaultProvider())) { |
|
| 334 | + $this->registerProvider($mimeType, function () use ($class, $options) { |
|
| 335 | + return new $class($options); |
|
| 336 | + }); |
|
| 337 | + } |
|
| 338 | + } |
|
| 339 | + |
|
| 340 | + /** |
|
| 341 | + * Register the default providers (if enabled) |
|
| 342 | + */ |
|
| 343 | + protected function registerCoreProviders() { |
|
| 344 | + if ($this->registeredCoreProviders) { |
|
| 345 | + return; |
|
| 346 | + } |
|
| 347 | + $this->registeredCoreProviders = true; |
|
| 348 | + |
|
| 349 | + $this->registerCoreProvider('OC\Preview\TXT', '/text\/plain/'); |
|
| 350 | + $this->registerCoreProvider('OC\Preview\MarkDown', '/text\/(x-)?markdown/'); |
|
| 351 | + $this->registerCoreProvider('OC\Preview\PNG', '/image\/png/'); |
|
| 352 | + $this->registerCoreProvider('OC\Preview\JPEG', '/image\/jpeg/'); |
|
| 353 | + $this->registerCoreProvider('OC\Preview\GIF', '/image\/gif/'); |
|
| 354 | + $this->registerCoreProvider('OC\Preview\BMP', '/image\/bmp/'); |
|
| 355 | + $this->registerCoreProvider('OC\Preview\XBitmap', '/image\/x-xbitmap/'); |
|
| 356 | + $this->registerCoreProvider('OC\Preview\MP3', '/audio\/mpeg/'); |
|
| 357 | + |
|
| 358 | + // SVG, Office and Bitmap require imagick |
|
| 359 | + if (extension_loaded('imagick')) { |
|
| 360 | + $checkImagick = new \Imagick(); |
|
| 361 | + |
|
| 362 | + $imagickProviders = [ |
|
| 363 | + 'SVG' => ['mimetype' => '/image\/svg\+xml/', 'class' => '\OC\Preview\SVG'], |
|
| 364 | + 'TIFF' => ['mimetype' => '/image\/tiff/', 'class' => '\OC\Preview\TIFF'], |
|
| 365 | + 'PDF' => ['mimetype' => '/application\/pdf/', 'class' => '\OC\Preview\PDF'], |
|
| 366 | + 'AI' => ['mimetype' => '/application\/illustrator/', 'class' => '\OC\Preview\Illustrator'], |
|
| 367 | + 'PSD' => ['mimetype' => '/application\/x-photoshop/', 'class' => '\OC\Preview\Photoshop'], |
|
| 368 | + 'EPS' => ['mimetype' => '/application\/postscript/', 'class' => '\OC\Preview\Postscript'], |
|
| 369 | + 'TTF' => ['mimetype' => '/application\/(?:font-sfnt|x-font$)/', 'class' => '\OC\Preview\Font'], |
|
| 370 | + ]; |
|
| 371 | + |
|
| 372 | + foreach ($imagickProviders as $queryFormat => $provider) { |
|
| 373 | + $class = $provider['class']; |
|
| 374 | + if (!in_array(trim($class, '\\'), $this->getEnabledDefaultProvider())) { |
|
| 375 | + continue; |
|
| 376 | + } |
|
| 377 | + |
|
| 378 | + if (count($checkImagick->queryFormats($queryFormat)) === 1) { |
|
| 379 | + $this->registerCoreProvider($class, $provider['mimetype']); |
|
| 380 | + } |
|
| 381 | + } |
|
| 382 | + |
|
| 383 | + if (count($checkImagick->queryFormats('PDF')) === 1) { |
|
| 384 | + if (\OC_Helper::is_function_enabled('shell_exec')) { |
|
| 385 | + $officeFound = is_string($this->config->getSystemValue('preview_libreoffice_path', null)); |
|
| 386 | + |
|
| 387 | + if (!$officeFound) { |
|
| 388 | + //let's see if there is libreoffice or openoffice on this machine |
|
| 389 | + $whichLibreOffice = shell_exec('command -v libreoffice'); |
|
| 390 | + $officeFound = !empty($whichLibreOffice); |
|
| 391 | + if (!$officeFound) { |
|
| 392 | + $whichOpenOffice = shell_exec('command -v openoffice'); |
|
| 393 | + $officeFound = !empty($whichOpenOffice); |
|
| 394 | + } |
|
| 395 | + } |
|
| 396 | + |
|
| 397 | + if ($officeFound) { |
|
| 398 | + $this->registerCoreProvider('\OC\Preview\MSOfficeDoc', '/application\/msword/'); |
|
| 399 | + $this->registerCoreProvider('\OC\Preview\MSOffice2003', '/application\/vnd.ms-.*/'); |
|
| 400 | + $this->registerCoreProvider('\OC\Preview\MSOffice2007', '/application\/vnd.openxmlformats-officedocument.*/'); |
|
| 401 | + $this->registerCoreProvider('\OC\Preview\OpenDocument', '/application\/vnd.oasis.opendocument.*/'); |
|
| 402 | + $this->registerCoreProvider('\OC\Preview\StarOffice', '/application\/vnd.sun.xml.*/'); |
|
| 403 | + } |
|
| 404 | + } |
|
| 405 | + } |
|
| 406 | + } |
|
| 407 | + |
|
| 408 | + // Video requires avconv or ffmpeg |
|
| 409 | + if (in_array('OC\Preview\Movie', $this->getEnabledDefaultProvider())) { |
|
| 410 | + $avconvBinary = \OC_Helper::findBinaryPath('avconv'); |
|
| 411 | + $ffmpegBinary = ($avconvBinary) ? null : \OC_Helper::findBinaryPath('ffmpeg'); |
|
| 412 | + |
|
| 413 | + if ($avconvBinary || $ffmpegBinary) { |
|
| 414 | + // FIXME // a bit hacky but didn't want to use subclasses |
|
| 415 | + \OC\Preview\Movie::$avconvBinary = $avconvBinary; |
|
| 416 | + \OC\Preview\Movie::$ffmpegBinary = $ffmpegBinary; |
|
| 417 | + |
|
| 418 | + $this->registerCoreProvider('\OC\Preview\Movie', '/video\/.*/'); |
|
| 419 | + } |
|
| 420 | + } |
|
| 421 | + } |
|
| 422 | 422 | } |
@@ -87,7 +87,7 @@ discard block |
||
| 87 | 87 | if ($params['itemType'] === 'file' || $params['itemType'] === 'folder') { |
| 88 | 88 | $path = Filesystem::getPath($params['fileSource']); |
| 89 | 89 | list($owner, $ownerPath) = $this->getOwnerPath($path); |
| 90 | - $absPath = '/' . $owner . '/files/' . $ownerPath; |
|
| 90 | + $absPath = '/'.$owner.'/files/'.$ownerPath; |
|
| 91 | 91 | $this->update($absPath); |
| 92 | 92 | } |
| 93 | 93 | } |
@@ -103,7 +103,7 @@ discard block |
||
| 103 | 103 | if ($params['itemType'] === 'file' || $params['itemType'] === 'folder') { |
| 104 | 104 | $path = Filesystem::getPath($params['fileSource']); |
| 105 | 105 | list($owner, $ownerPath) = $this->getOwnerPath($path); |
| 106 | - $absPath = '/' . $owner . '/files/' . $ownerPath; |
|
| 106 | + $absPath = '/'.$owner.'/files/'.$ownerPath; |
|
| 107 | 107 | $this->update($absPath); |
| 108 | 108 | } |
| 109 | 109 | } |
@@ -117,7 +117,7 @@ discard block |
||
| 117 | 117 | */ |
| 118 | 118 | public function postRestore($params) { |
| 119 | 119 | if ($this->encryptionManager->isEnabled()) { |
| 120 | - $path = Filesystem::normalizePath('/' . $this->uid . '/files/' . $params['filePath']); |
|
| 120 | + $path = Filesystem::normalizePath('/'.$this->uid.'/files/'.$params['filePath']); |
|
| 121 | 121 | $this->update($path); |
| 122 | 122 | } |
| 123 | 123 | } |
@@ -131,12 +131,12 @@ discard block |
||
| 131 | 131 | public function postRename($params) { |
| 132 | 132 | $source = $params['oldpath']; |
| 133 | 133 | $target = $params['newpath']; |
| 134 | - if( |
|
| 134 | + if ( |
|
| 135 | 135 | $this->encryptionManager->isEnabled() && |
| 136 | 136 | dirname($source) !== dirname($target) |
| 137 | 137 | ) { |
| 138 | 138 | list($owner, $ownerPath) = $this->getOwnerPath($target); |
| 139 | - $absPath = '/' . $owner . '/files/' . $ownerPath; |
|
| 139 | + $absPath = '/'.$owner.'/files/'.$ownerPath; |
|
| 140 | 140 | $this->update($absPath); |
| 141 | 141 | } |
| 142 | 142 | } |
@@ -151,10 +151,10 @@ discard block |
||
| 151 | 151 | protected function getOwnerPath($path) { |
| 152 | 152 | $info = Filesystem::getFileInfo($path); |
| 153 | 153 | $owner = Filesystem::getOwner($path); |
| 154 | - $view = new View('/' . $owner . '/files'); |
|
| 154 | + $view = new View('/'.$owner.'/files'); |
|
| 155 | 155 | $path = $view->getPath($info->getId()); |
| 156 | 156 | if ($path === null) { |
| 157 | - throw new \InvalidArgumentException('No file found for ' . $info->getId()); |
|
| 157 | + throw new \InvalidArgumentException('No file found for '.$info->getId()); |
|
| 158 | 158 | } |
| 159 | 159 | |
| 160 | 160 | return array($owner, $path); |
@@ -33,162 +33,162 @@ |
||
| 33 | 33 | */ |
| 34 | 34 | class Update { |
| 35 | 35 | |
| 36 | - /** @var \OC\Files\View */ |
|
| 37 | - protected $view; |
|
| 38 | - |
|
| 39 | - /** @var \OC\Encryption\Util */ |
|
| 40 | - protected $util; |
|
| 41 | - |
|
| 42 | - /** @var \OC\Files\Mount\Manager */ |
|
| 43 | - protected $mountManager; |
|
| 44 | - |
|
| 45 | - /** @var \OC\Encryption\Manager */ |
|
| 46 | - protected $encryptionManager; |
|
| 47 | - |
|
| 48 | - /** @var string */ |
|
| 49 | - protected $uid; |
|
| 50 | - |
|
| 51 | - /** @var \OC\Encryption\File */ |
|
| 52 | - protected $file; |
|
| 53 | - |
|
| 54 | - /** |
|
| 55 | - * |
|
| 56 | - * @param \OC\Files\View $view |
|
| 57 | - * @param \OC\Encryption\Util $util |
|
| 58 | - * @param \OC\Files\Mount\Manager $mountManager |
|
| 59 | - * @param \OC\Encryption\Manager $encryptionManager |
|
| 60 | - * @param \OC\Encryption\File $file |
|
| 61 | - * @param string $uid |
|
| 62 | - */ |
|
| 63 | - public function __construct( |
|
| 64 | - View $view, |
|
| 65 | - Util $util, |
|
| 66 | - Mount\Manager $mountManager, |
|
| 67 | - Manager $encryptionManager, |
|
| 68 | - File $file, |
|
| 69 | - $uid |
|
| 70 | - ) { |
|
| 71 | - |
|
| 72 | - $this->view = $view; |
|
| 73 | - $this->util = $util; |
|
| 74 | - $this->mountManager = $mountManager; |
|
| 75 | - $this->encryptionManager = $encryptionManager; |
|
| 76 | - $this->file = $file; |
|
| 77 | - $this->uid = $uid; |
|
| 78 | - } |
|
| 79 | - |
|
| 80 | - /** |
|
| 81 | - * hook after file was shared |
|
| 82 | - * |
|
| 83 | - * @param array $params |
|
| 84 | - */ |
|
| 85 | - public function postShared($params) { |
|
| 86 | - if ($this->encryptionManager->isEnabled()) { |
|
| 87 | - if ($params['itemType'] === 'file' || $params['itemType'] === 'folder') { |
|
| 88 | - $path = Filesystem::getPath($params['fileSource']); |
|
| 89 | - list($owner, $ownerPath) = $this->getOwnerPath($path); |
|
| 90 | - $absPath = '/' . $owner . '/files/' . $ownerPath; |
|
| 91 | - $this->update($absPath); |
|
| 92 | - } |
|
| 93 | - } |
|
| 94 | - } |
|
| 95 | - |
|
| 96 | - /** |
|
| 97 | - * hook after file was unshared |
|
| 98 | - * |
|
| 99 | - * @param array $params |
|
| 100 | - */ |
|
| 101 | - public function postUnshared($params) { |
|
| 102 | - if ($this->encryptionManager->isEnabled()) { |
|
| 103 | - if ($params['itemType'] === 'file' || $params['itemType'] === 'folder') { |
|
| 104 | - $path = Filesystem::getPath($params['fileSource']); |
|
| 105 | - list($owner, $ownerPath) = $this->getOwnerPath($path); |
|
| 106 | - $absPath = '/' . $owner . '/files/' . $ownerPath; |
|
| 107 | - $this->update($absPath); |
|
| 108 | - } |
|
| 109 | - } |
|
| 110 | - } |
|
| 111 | - |
|
| 112 | - /** |
|
| 113 | - * inform encryption module that a file was restored from the trash bin, |
|
| 114 | - * e.g. to update the encryption keys |
|
| 115 | - * |
|
| 116 | - * @param array $params |
|
| 117 | - */ |
|
| 118 | - public function postRestore($params) { |
|
| 119 | - if ($this->encryptionManager->isEnabled()) { |
|
| 120 | - $path = Filesystem::normalizePath('/' . $this->uid . '/files/' . $params['filePath']); |
|
| 121 | - $this->update($path); |
|
| 122 | - } |
|
| 123 | - } |
|
| 124 | - |
|
| 125 | - /** |
|
| 126 | - * inform encryption module that a file was renamed, |
|
| 127 | - * e.g. to update the encryption keys |
|
| 128 | - * |
|
| 129 | - * @param array $params |
|
| 130 | - */ |
|
| 131 | - public function postRename($params) { |
|
| 132 | - $source = $params['oldpath']; |
|
| 133 | - $target = $params['newpath']; |
|
| 134 | - if( |
|
| 135 | - $this->encryptionManager->isEnabled() && |
|
| 136 | - dirname($source) !== dirname($target) |
|
| 137 | - ) { |
|
| 138 | - list($owner, $ownerPath) = $this->getOwnerPath($target); |
|
| 139 | - $absPath = '/' . $owner . '/files/' . $ownerPath; |
|
| 140 | - $this->update($absPath); |
|
| 141 | - } |
|
| 142 | - } |
|
| 143 | - |
|
| 144 | - /** |
|
| 145 | - * get owner and path relative to data/<owner>/files |
|
| 146 | - * |
|
| 147 | - * @param string $path path to file for current user |
|
| 148 | - * @return array ['owner' => $owner, 'path' => $path] |
|
| 149 | - * @throw \InvalidArgumentException |
|
| 150 | - */ |
|
| 151 | - protected function getOwnerPath($path) { |
|
| 152 | - $info = Filesystem::getFileInfo($path); |
|
| 153 | - $owner = Filesystem::getOwner($path); |
|
| 154 | - $view = new View('/' . $owner . '/files'); |
|
| 155 | - $path = $view->getPath($info->getId()); |
|
| 156 | - if ($path === null) { |
|
| 157 | - throw new \InvalidArgumentException('No file found for ' . $info->getId()); |
|
| 158 | - } |
|
| 159 | - |
|
| 160 | - return array($owner, $path); |
|
| 161 | - } |
|
| 162 | - |
|
| 163 | - /** |
|
| 164 | - * notify encryption module about added/removed users from a file/folder |
|
| 165 | - * |
|
| 166 | - * @param string $path relative to data/ |
|
| 167 | - * @throws Exceptions\ModuleDoesNotExistsException |
|
| 168 | - */ |
|
| 169 | - public function update($path) { |
|
| 170 | - |
|
| 171 | - $encryptionModule = $this->encryptionManager->getEncryptionModule(); |
|
| 172 | - |
|
| 173 | - // if the encryption module doesn't encrypt the files on a per-user basis |
|
| 174 | - // we have nothing to do here. |
|
| 175 | - if ($encryptionModule->needDetailedAccessList() === false) { |
|
| 176 | - return; |
|
| 177 | - } |
|
| 178 | - |
|
| 179 | - // if a folder was shared, get a list of all (sub-)folders |
|
| 180 | - if ($this->view->is_dir($path)) { |
|
| 181 | - $allFiles = $this->util->getAllFiles($path); |
|
| 182 | - } else { |
|
| 183 | - $allFiles = array($path); |
|
| 184 | - } |
|
| 185 | - |
|
| 186 | - |
|
| 187 | - |
|
| 188 | - foreach ($allFiles as $file) { |
|
| 189 | - $usersSharing = $this->file->getAccessList($file); |
|
| 190 | - $encryptionModule->update($file, $this->uid, $usersSharing); |
|
| 191 | - } |
|
| 192 | - } |
|
| 36 | + /** @var \OC\Files\View */ |
|
| 37 | + protected $view; |
|
| 38 | + |
|
| 39 | + /** @var \OC\Encryption\Util */ |
|
| 40 | + protected $util; |
|
| 41 | + |
|
| 42 | + /** @var \OC\Files\Mount\Manager */ |
|
| 43 | + protected $mountManager; |
|
| 44 | + |
|
| 45 | + /** @var \OC\Encryption\Manager */ |
|
| 46 | + protected $encryptionManager; |
|
| 47 | + |
|
| 48 | + /** @var string */ |
|
| 49 | + protected $uid; |
|
| 50 | + |
|
| 51 | + /** @var \OC\Encryption\File */ |
|
| 52 | + protected $file; |
|
| 53 | + |
|
| 54 | + /** |
|
| 55 | + * |
|
| 56 | + * @param \OC\Files\View $view |
|
| 57 | + * @param \OC\Encryption\Util $util |
|
| 58 | + * @param \OC\Files\Mount\Manager $mountManager |
|
| 59 | + * @param \OC\Encryption\Manager $encryptionManager |
|
| 60 | + * @param \OC\Encryption\File $file |
|
| 61 | + * @param string $uid |
|
| 62 | + */ |
|
| 63 | + public function __construct( |
|
| 64 | + View $view, |
|
| 65 | + Util $util, |
|
| 66 | + Mount\Manager $mountManager, |
|
| 67 | + Manager $encryptionManager, |
|
| 68 | + File $file, |
|
| 69 | + $uid |
|
| 70 | + ) { |
|
| 71 | + |
|
| 72 | + $this->view = $view; |
|
| 73 | + $this->util = $util; |
|
| 74 | + $this->mountManager = $mountManager; |
|
| 75 | + $this->encryptionManager = $encryptionManager; |
|
| 76 | + $this->file = $file; |
|
| 77 | + $this->uid = $uid; |
|
| 78 | + } |
|
| 79 | + |
|
| 80 | + /** |
|
| 81 | + * hook after file was shared |
|
| 82 | + * |
|
| 83 | + * @param array $params |
|
| 84 | + */ |
|
| 85 | + public function postShared($params) { |
|
| 86 | + if ($this->encryptionManager->isEnabled()) { |
|
| 87 | + if ($params['itemType'] === 'file' || $params['itemType'] === 'folder') { |
|
| 88 | + $path = Filesystem::getPath($params['fileSource']); |
|
| 89 | + list($owner, $ownerPath) = $this->getOwnerPath($path); |
|
| 90 | + $absPath = '/' . $owner . '/files/' . $ownerPath; |
|
| 91 | + $this->update($absPath); |
|
| 92 | + } |
|
| 93 | + } |
|
| 94 | + } |
|
| 95 | + |
|
| 96 | + /** |
|
| 97 | + * hook after file was unshared |
|
| 98 | + * |
|
| 99 | + * @param array $params |
|
| 100 | + */ |
|
| 101 | + public function postUnshared($params) { |
|
| 102 | + if ($this->encryptionManager->isEnabled()) { |
|
| 103 | + if ($params['itemType'] === 'file' || $params['itemType'] === 'folder') { |
|
| 104 | + $path = Filesystem::getPath($params['fileSource']); |
|
| 105 | + list($owner, $ownerPath) = $this->getOwnerPath($path); |
|
| 106 | + $absPath = '/' . $owner . '/files/' . $ownerPath; |
|
| 107 | + $this->update($absPath); |
|
| 108 | + } |
|
| 109 | + } |
|
| 110 | + } |
|
| 111 | + |
|
| 112 | + /** |
|
| 113 | + * inform encryption module that a file was restored from the trash bin, |
|
| 114 | + * e.g. to update the encryption keys |
|
| 115 | + * |
|
| 116 | + * @param array $params |
|
| 117 | + */ |
|
| 118 | + public function postRestore($params) { |
|
| 119 | + if ($this->encryptionManager->isEnabled()) { |
|
| 120 | + $path = Filesystem::normalizePath('/' . $this->uid . '/files/' . $params['filePath']); |
|
| 121 | + $this->update($path); |
|
| 122 | + } |
|
| 123 | + } |
|
| 124 | + |
|
| 125 | + /** |
|
| 126 | + * inform encryption module that a file was renamed, |
|
| 127 | + * e.g. to update the encryption keys |
|
| 128 | + * |
|
| 129 | + * @param array $params |
|
| 130 | + */ |
|
| 131 | + public function postRename($params) { |
|
| 132 | + $source = $params['oldpath']; |
|
| 133 | + $target = $params['newpath']; |
|
| 134 | + if( |
|
| 135 | + $this->encryptionManager->isEnabled() && |
|
| 136 | + dirname($source) !== dirname($target) |
|
| 137 | + ) { |
|
| 138 | + list($owner, $ownerPath) = $this->getOwnerPath($target); |
|
| 139 | + $absPath = '/' . $owner . '/files/' . $ownerPath; |
|
| 140 | + $this->update($absPath); |
|
| 141 | + } |
|
| 142 | + } |
|
| 143 | + |
|
| 144 | + /** |
|
| 145 | + * get owner and path relative to data/<owner>/files |
|
| 146 | + * |
|
| 147 | + * @param string $path path to file for current user |
|
| 148 | + * @return array ['owner' => $owner, 'path' => $path] |
|
| 149 | + * @throw \InvalidArgumentException |
|
| 150 | + */ |
|
| 151 | + protected function getOwnerPath($path) { |
|
| 152 | + $info = Filesystem::getFileInfo($path); |
|
| 153 | + $owner = Filesystem::getOwner($path); |
|
| 154 | + $view = new View('/' . $owner . '/files'); |
|
| 155 | + $path = $view->getPath($info->getId()); |
|
| 156 | + if ($path === null) { |
|
| 157 | + throw new \InvalidArgumentException('No file found for ' . $info->getId()); |
|
| 158 | + } |
|
| 159 | + |
|
| 160 | + return array($owner, $path); |
|
| 161 | + } |
|
| 162 | + |
|
| 163 | + /** |
|
| 164 | + * notify encryption module about added/removed users from a file/folder |
|
| 165 | + * |
|
| 166 | + * @param string $path relative to data/ |
|
| 167 | + * @throws Exceptions\ModuleDoesNotExistsException |
|
| 168 | + */ |
|
| 169 | + public function update($path) { |
|
| 170 | + |
|
| 171 | + $encryptionModule = $this->encryptionManager->getEncryptionModule(); |
|
| 172 | + |
|
| 173 | + // if the encryption module doesn't encrypt the files on a per-user basis |
|
| 174 | + // we have nothing to do here. |
|
| 175 | + if ($encryptionModule->needDetailedAccessList() === false) { |
|
| 176 | + return; |
|
| 177 | + } |
|
| 178 | + |
|
| 179 | + // if a folder was shared, get a list of all (sub-)folders |
|
| 180 | + if ($this->view->is_dir($path)) { |
|
| 181 | + $allFiles = $this->util->getAllFiles($path); |
|
| 182 | + } else { |
|
| 183 | + $allFiles = array($path); |
|
| 184 | + } |
|
| 185 | + |
|
| 186 | + |
|
| 187 | + |
|
| 188 | + foreach ($allFiles as $file) { |
|
| 189 | + $usersSharing = $this->file->getAccessList($file); |
|
| 190 | + $encryptionModule->update($file, $this->uid, $usersSharing); |
|
| 191 | + } |
|
| 192 | + } |
|
| 193 | 193 | |
| 194 | 194 | } |
@@ -27,12 +27,12 @@ |
||
| 27 | 27 | |
| 28 | 28 | class ModuleAlreadyExistsException extends GenericEncryptionException { |
| 29 | 29 | |
| 30 | - /** |
|
| 31 | - * @param string $id |
|
| 32 | - * @param string $name |
|
| 33 | - */ |
|
| 34 | - public function __construct($id, $name) { |
|
| 35 | - parent::__construct('Id "' . $id . '" already used by encryption module "' . $name . '"'); |
|
| 36 | - } |
|
| 30 | + /** |
|
| 31 | + * @param string $id |
|
| 32 | + * @param string $name |
|
| 33 | + */ |
|
| 34 | + public function __construct($id, $name) { |
|
| 35 | + parent::__construct('Id "' . $id . '" already used by encryption module "' . $name . '"'); |
|
| 36 | + } |
|
| 37 | 37 | |
| 38 | 38 | } |
@@ -32,7 +32,7 @@ |
||
| 32 | 32 | * @param string $name |
| 33 | 33 | */ |
| 34 | 34 | public function __construct($id, $name) { |
| 35 | - parent::__construct('Id "' . $id . '" already used by encryption module "' . $name . '"'); |
|
| 35 | + parent::__construct('Id "'.$id.'" already used by encryption module "'.$name.'"'); |
|
| 36 | 36 | } |
| 37 | 37 | |
| 38 | 38 | } |
@@ -28,8 +28,8 @@ |
||
| 28 | 28 | |
| 29 | 29 | class EncryptionHeaderToLargeException extends GenericEncryptionException { |
| 30 | 30 | |
| 31 | - public function __construct() { |
|
| 32 | - parent::__construct('max header size exceeded'); |
|
| 33 | - } |
|
| 31 | + public function __construct() { |
|
| 32 | + parent::__construct('max header size exceeded'); |
|
| 33 | + } |
|
| 34 | 34 | |
| 35 | 35 | } |
@@ -27,10 +27,10 @@ |
||
| 27 | 27 | |
| 28 | 28 | class EncryptionHeaderKeyExistsException extends GenericEncryptionException { |
| 29 | 29 | |
| 30 | - /** |
|
| 31 | - * @param string $key |
|
| 32 | - */ |
|
| 33 | - public function __construct($key) { |
|
| 34 | - parent::__construct('header key "'. $key . '" already reserved by ownCloud'); |
|
| 35 | - } |
|
| 30 | + /** |
|
| 31 | + * @param string $key |
|
| 32 | + */ |
|
| 33 | + public function __construct($key) { |
|
| 34 | + parent::__construct('header key "'. $key . '" already reserved by ownCloud'); |
|
| 35 | + } |
|
| 36 | 36 | } |
@@ -31,6 +31,6 @@ |
||
| 31 | 31 | * @param string $key |
| 32 | 32 | */ |
| 33 | 33 | public function __construct($key) { |
| 34 | - parent::__construct('header key "'. $key . '" already reserved by ownCloud'); |
|
| 34 | + parent::__construct('header key "'.$key.'" already reserved by ownCloud'); |
|
| 35 | 35 | } |
| 36 | 36 | } |