| Conditions | 14 |
| Paths | 168 |
| Total Lines | 104 |
| Code Lines | 66 |
| Lines | 20 |
| Ratio | 19.23 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 66 | public function setupDatabase($username) { |
||
| 67 | $e_host = addslashes($this->dbHost); |
||
| 68 | // casting to int to avoid malicious input |
||
| 69 | $e_port = (int)$this->dbPort; |
||
| 70 | $e_dbname = addslashes($this->dbName); |
||
| 71 | //check if the database user has admin right |
||
| 72 | View Code Duplication | if ($e_host == '') { |
|
| 73 | $easy_connect_string = $e_dbname; // use dbname as easy connect name |
||
| 74 | } else { |
||
| 75 | $easy_connect_string = '//'.$e_host.(!empty($e_port) ? ":{$e_port}" : "").'/'.$e_dbname; |
||
| 76 | } |
||
| 77 | $this->logger->debug('connect string: ' . $easy_connect_string, ['app' => 'setup.oci']); |
||
| 78 | $connection = @oci_connect($this->dbUser, $this->dbPassword, $easy_connect_string); |
||
| 79 | if(!$connection) { |
||
| 80 | $errorMessage = $this->getLastError(); |
||
| 81 | if ($errorMessage) { |
||
| 82 | throw new \OC\DatabaseSetupException($this->trans->t('Oracle connection could not be established'), |
||
| 83 | $errorMessage.' Check environment: ORACLE_HOME='.getenv('ORACLE_HOME') |
||
| 84 | .' ORACLE_SID='.getenv('ORACLE_SID') |
||
| 85 | .' LD_LIBRARY_PATH='.getenv('LD_LIBRARY_PATH') |
||
| 86 | .' NLS_LANG='.getenv('NLS_LANG') |
||
| 87 | .' tnsnames.ora is '.(is_readable(getenv('ORACLE_HOME').'/network/admin/tnsnames.ora')?'':'not ').'readable'); |
||
| 88 | } |
||
| 89 | throw new \OC\DatabaseSetupException($this->trans->t('Oracle username and/or password not valid'), |
||
| 90 | 'Check environment: ORACLE_HOME='.getenv('ORACLE_HOME') |
||
| 91 | .' ORACLE_SID='.getenv('ORACLE_SID') |
||
| 92 | .' LD_LIBRARY_PATH='.getenv('LD_LIBRARY_PATH') |
||
| 93 | .' NLS_LANG='.getenv('NLS_LANG') |
||
| 94 | .' tnsnames.ora is '.(is_readable(getenv('ORACLE_HOME').'/network/admin/tnsnames.ora')?'':'not ').'readable'); |
||
| 95 | } |
||
| 96 | //check for roles creation rights in oracle |
||
| 97 | |||
| 98 | $query='SELECT count(*) FROM user_role_privs, role_sys_privs' |
||
| 99 | ." WHERE user_role_privs.granted_role = role_sys_privs.role AND privilege = 'CREATE ROLE'"; |
||
| 100 | $stmt = oci_parse($connection, $query); |
||
| 101 | View Code Duplication | if (!$stmt) { |
|
| 102 | $entry = $this->trans->t('DB Error: "%s"', array($this->getLastError($connection))) . '<br />'; |
||
| 103 | $entry .= $this->trans->t('Offending command was: "%s"', array($query)) . '<br />'; |
||
| 104 | $this->logger->warning($entry, ['app' => 'setup.oci']); |
||
| 105 | } |
||
| 106 | $result = oci_execute($stmt); |
||
| 107 | if($result) { |
||
| 108 | $row = oci_fetch_row($stmt); |
||
| 109 | |||
| 110 | if ($row[0] > 0) { |
||
| 111 | //use the admin login data for the new database user |
||
| 112 | |||
| 113 | //add prefix to the oracle user name to prevent collisions |
||
| 114 | $this->dbUser='oc_'.$username; |
||
| 115 | //create a new password so we don't need to store the admin config in the config file |
||
| 116 | $this->dbPassword = \OC::$server->getSecureRandom()->generate(30, \OCP\Security\ISecureRandom::CHAR_LOWER.\OCP\Security\ISecureRandom::CHAR_DIGITS); |
||
| 117 | |||
| 118 | //oracle passwords are treated as identifiers: |
||
| 119 | // must start with alphanumeric char |
||
| 120 | // needs to be shortened to 30 bytes, as the two " needed to escape the identifier count towards the identifier length. |
||
| 121 | $this->dbPassword=substr($this->dbPassword, 0, 30); |
||
| 122 | |||
| 123 | $this->createDBUser($connection); |
||
| 124 | } |
||
| 125 | } |
||
| 126 | |||
| 127 | $this->config->setValues([ |
||
| 128 | 'dbuser' => $this->dbUser, |
||
| 129 | 'dbname' => $this->dbName, |
||
| 130 | 'dbpassword' => $this->dbPassword, |
||
| 131 | ]); |
||
| 132 | |||
| 133 | //create the database not necessary, oracle implies user = schema |
||
| 134 | //$this->createDatabase($this->dbname, $this->dbuser, $connection); |
||
|
|
|||
| 135 | |||
| 136 | //FIXME check tablespace exists: select * from user_tablespaces |
||
| 137 | |||
| 138 | // the connection to dbname=oracle is not needed anymore |
||
| 139 | oci_close($connection); |
||
| 140 | |||
| 141 | // connect to the oracle database (schema=$this->dbuser) an check if the schema needs to be filled |
||
| 142 | $this->dbUser = $this->config->getValue('dbuser'); |
||
| 143 | //$this->dbname = \OC_Config::getValue('dbname'); |
||
| 144 | $this->dbPassword = $this->config->getValue('dbpassword'); |
||
| 145 | |||
| 146 | $e_host = addslashes($this->dbHost); |
||
| 147 | $e_dbname = addslashes($this->dbName); |
||
| 148 | |||
| 149 | View Code Duplication | if ($e_host == '') { |
|
| 150 | $easy_connect_string = $e_dbname; // use dbname as easy connect name |
||
| 151 | } else { |
||
| 152 | $easy_connect_string = '//' . $e_host . (!empty($e_port) ? ":{$e_port}" : "") . '/' . $e_dbname; |
||
| 153 | } |
||
| 154 | $connection = @oci_connect($this->dbUser, $this->dbPassword, $easy_connect_string); |
||
| 155 | if(!$connection) { |
||
| 156 | throw new \OC\DatabaseSetupException($this->trans->t('Oracle username and/or password not valid'), |
||
| 157 | $this->trans->t('You need to enter details of an existing account.')); |
||
| 158 | } |
||
| 159 | $query = "SELECT count(*) FROM user_tables WHERE table_name = :un"; |
||
| 160 | $stmt = oci_parse($connection, $query); |
||
| 161 | $un = $this->tablePrefix.'users'; |
||
| 162 | oci_bind_by_name($stmt, ':un', $un); |
||
| 163 | View Code Duplication | if (!$stmt) { |
|
| 164 | $entry = $this->trans->t('DB Error: "%s"', array($this->getLastError($connection))) . '<br />'; |
||
| 165 | $entry .= $this->trans->t('Offending command was: "%s"', array($query)) . '<br />'; |
||
| 166 | $this->logger->warning( $entry, ['app' => 'setup.oci']); |
||
| 167 | } |
||
| 168 | oci_execute($stmt); |
||
| 169 | } |
||
| 170 | |||
| 264 |
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.