Conditions | 20 |
Paths | 2592 |
Total Lines | 126 |
Code Lines | 74 |
Lines | 0 |
Ratio | 0 % |
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 |
||
119 | public static function install($options) { |
||
120 | $l = self::getTrans(); |
||
121 | |||
122 | $error = array(); |
||
123 | $dbType = $options['dbtype']; |
||
124 | |||
125 | if(empty($options['adminlogin'])) { |
||
126 | $error[] = $l->t('Set an admin username.'); |
||
127 | } |
||
128 | if(empty($options['adminpass'])) { |
||
129 | $error[] = $l->t('Set an admin password.'); |
||
130 | } |
||
131 | if(empty($options['directory'])) { |
||
132 | $options['directory'] = OC::$SERVERROOT."/data"; |
||
133 | } |
||
134 | |||
135 | if (!isset(self::$dbSetupClasses[$dbType])) { |
||
136 | $dbType = 'sqlite'; |
||
137 | } |
||
138 | |||
139 | $username = htmlspecialchars_decode($options['adminlogin']); |
||
140 | $password = htmlspecialchars_decode($options['adminpass']); |
||
141 | $dataDir = htmlspecialchars_decode($options['directory']); |
||
142 | |||
143 | $class = self::$dbSetupClasses[$dbType]; |
||
144 | /** @var \OC\Setup\AbstractDatabase $dbSetup */ |
||
145 | $dbSetup = new $class(self::getTrans(), 'db_structure.xml'); |
||
146 | $error = array_merge($error, $dbSetup->validate($options)); |
||
147 | |||
148 | // validate the data directory |
||
149 | if ( |
||
150 | (!is_dir($dataDir) and !mkdir($dataDir)) or |
||
151 | !is_writable($dataDir) |
||
152 | ) { |
||
153 | $error[] = $l->t("Can't create or write into the data directory %s", array($dataDir)); |
||
154 | } |
||
155 | |||
156 | if(count($error) != 0) { |
||
157 | return $error; |
||
158 | } |
||
159 | |||
160 | //no errors, good |
||
161 | if(isset($options['trusted_domains']) |
||
162 | && is_array($options['trusted_domains'])) { |
||
163 | $trustedDomains = $options['trusted_domains']; |
||
164 | } else { |
||
165 | $trustedDomains = array(\OC_Request::getDomainWithoutPort(\OC_Request::serverHost())); |
||
166 | } |
||
167 | |||
168 | if (OC_Util::runningOnWindows()) { |
||
169 | $dataDir = rtrim(realpath($dataDir), '\\'); |
||
170 | } |
||
171 | |||
172 | //use sqlite3 when available, otherwise sqlite2 will be used. |
||
173 | if($dbType=='sqlite' and class_exists('SQLite3')) { |
||
174 | $dbType='sqlite3'; |
||
175 | } |
||
176 | |||
177 | //generate a random salt that is used to salt the local user passwords |
||
178 | $salt = \OC::$server->getSecureRandom()->getLowStrengthGenerator()->generate(30); |
||
179 | // generate a secret |
||
180 | $secret = \OC::$server->getSecureRandom()->getMediumStrengthGenerator()->generate(48); |
||
181 | |||
182 | //write the config file |
||
183 | \OC::$server->getConfig()->setSystemValues([ |
||
184 | 'passwordsalt' => $salt, |
||
185 | 'secret' => $secret, |
||
186 | 'trusted_domains' => $trustedDomains, |
||
187 | 'datadirectory' => $dataDir, |
||
188 | 'overwrite.cli.url' => \OC_Request::serverProtocol() . '://' . \OC_Request::serverHost() . OC::$WEBROOT, |
||
189 | 'dbtype' => $dbType, |
||
190 | 'version' => implode('.', OC_Util::getVersion()), |
||
191 | ]); |
||
192 | |||
193 | try { |
||
194 | $dbSetup->initialize($options); |
||
195 | $dbSetup->setupDatabase($username); |
||
196 | } catch (\OC\DatabaseSetupException $e) { |
||
197 | $error[] = array( |
||
198 | 'error' => $e->getMessage(), |
||
199 | 'hint' => $e->getHint() |
||
200 | ); |
||
201 | return($error); |
||
202 | } catch (Exception $e) { |
||
203 | $error[] = array( |
||
204 | 'error' => 'Error while trying to create admin user: ' . $e->getMessage(), |
||
205 | 'hint' => '' |
||
206 | ); |
||
207 | return($error); |
||
208 | } |
||
209 | |||
210 | //create the user and group |
||
211 | try { |
||
212 | OC_User::createUser($username, $password); |
||
213 | } catch(Exception $exception) { |
||
214 | $error[] = $exception->getMessage(); |
||
215 | } |
||
216 | |||
217 | if(count($error) == 0) { |
||
218 | $appConfig = \OC::$server->getAppConfig(); |
||
219 | $appConfig->setValue('core', 'installedat', microtime(true)); |
||
220 | $appConfig->setValue('core', 'lastupdatedat', microtime(true)); |
||
221 | |||
222 | OC_Group::createGroup('admin'); |
||
223 | OC_Group::addToGroup($username, 'admin'); |
||
224 | OC_User::login($username, $password); |
||
225 | |||
226 | //guess what this does |
||
227 | OC_Installer::installShippedApps(); |
||
228 | |||
229 | // create empty file in data dir, so we can later find |
||
230 | // out that this is indeed an ownCloud data directory |
||
231 | file_put_contents(OC_Config::getValue('datadirectory', OC::$SERVERROOT.'/data').'/.ocdata', ''); |
||
232 | |||
233 | // Update htaccess files for apache hosts |
||
234 | if (isset($_SERVER['SERVER_SOFTWARE']) && strstr($_SERVER['SERVER_SOFTWARE'], 'Apache')) { |
||
235 | self::updateHtaccess(); |
||
236 | self::protectDataDirectory(); |
||
237 | } |
||
238 | |||
239 | //and we are done |
||
240 | OC_Config::setValue('installed', true); |
||
241 | } |
||
242 | |||
243 | return $error; |
||
244 | } |
||
245 | |||
303 |
If you suppress an error, we recommend checking for the error condition explicitly: