Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like OC often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use OC, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
62 | class OC { |
||
63 | /** |
||
64 | * Associative array for autoloading. classname => filename |
||
65 | */ |
||
66 | public static $CLASSPATH = array(); |
||
67 | /** |
||
68 | * The installation path for owncloud on the server (e.g. /srv/http/owncloud) |
||
69 | */ |
||
70 | public static $SERVERROOT = ''; |
||
71 | /** |
||
72 | * the current request path relative to the owncloud root (e.g. files/index.php) |
||
73 | */ |
||
74 | private static $SUBURI = ''; |
||
75 | /** |
||
76 | * the owncloud root path for http requests (e.g. owncloud/) |
||
77 | */ |
||
78 | public static $WEBROOT = ''; |
||
79 | /** |
||
80 | * The installation path of the 3rdparty folder on the server (e.g. /srv/http/owncloud/3rdparty) |
||
81 | */ |
||
82 | public static $THIRDPARTYROOT = ''; |
||
83 | /** |
||
84 | * the root path of the 3rdparty folder for http requests (e.g. owncloud/3rdparty) |
||
85 | */ |
||
86 | public static $THIRDPARTYWEBROOT = ''; |
||
87 | /** |
||
88 | * The installation path array of the apps folder on the server (e.g. /srv/http/owncloud) 'path' and |
||
89 | * web path in 'url' |
||
90 | */ |
||
91 | public static $APPSROOTS = array(); |
||
92 | |||
93 | public static $configDir; |
||
94 | |||
95 | /** |
||
96 | * requested app |
||
97 | */ |
||
98 | public static $REQUESTEDAPP = ''; |
||
99 | |||
100 | /** |
||
101 | * check if ownCloud runs in cli mode |
||
102 | */ |
||
103 | public static $CLI = false; |
||
104 | |||
105 | /** |
||
106 | * @var \OC\Autoloader $loader |
||
107 | */ |
||
108 | public static $loader = null; |
||
109 | |||
110 | /** |
||
111 | * @var \OC\Server |
||
112 | */ |
||
113 | public static $server = null; |
||
114 | |||
115 | /** |
||
116 | * @throws \RuntimeException when the 3rdparty directory is missing or |
||
117 | * the app path list is empty or contains an invalid path |
||
118 | */ |
||
119 | public static function initPaths() { |
||
120 | // ensure we can find OC_Config |
||
121 | set_include_path( |
||
122 | OC::$SERVERROOT . '/lib' . PATH_SEPARATOR . |
||
123 | get_include_path() |
||
124 | ); |
||
125 | |||
126 | if(defined('PHPUNIT_CONFIG_DIR')) { |
||
127 | self::$configDir = OC::$SERVERROOT . '/' . PHPUNIT_CONFIG_DIR . '/'; |
||
128 | } elseif(defined('PHPUNIT_RUN') and PHPUNIT_RUN and is_dir(OC::$SERVERROOT . '/tests/config/')) { |
||
129 | self::$configDir = OC::$SERVERROOT . '/tests/config/'; |
||
130 | } else { |
||
131 | self::$configDir = OC::$SERVERROOT . '/config/'; |
||
132 | } |
||
133 | OC_Config::$object = new \OC\Config(self::$configDir); |
||
134 | |||
135 | OC::$SUBURI = str_replace("\\", "/", substr(realpath($_SERVER["SCRIPT_FILENAME"]), strlen(OC::$SERVERROOT))); |
||
136 | /** |
||
137 | * FIXME: The following lines are required because we can't yet instantiiate |
||
138 | * \OC::$server->getRequest() since \OC::$server does not yet exist. |
||
139 | */ |
||
140 | $params = [ |
||
141 | 'server' => [ |
||
142 | 'SCRIPT_NAME' => $_SERVER['SCRIPT_NAME'], |
||
143 | 'SCRIPT_FILENAME' => $_SERVER['SCRIPT_FILENAME'], |
||
144 | ], |
||
145 | ]; |
||
146 | $fakeRequest = new \OC\AppFramework\Http\Request($params, null, new \OC\AllConfig(new \OC\SystemConfig())); |
||
147 | $scriptName = $fakeRequest->getScriptName(); |
||
148 | if (substr($scriptName, -1) == '/') { |
||
149 | $scriptName .= 'index.php'; |
||
150 | //make sure suburi follows the same rules as scriptName |
||
151 | if (substr(OC::$SUBURI, -9) != 'index.php') { |
||
152 | if (substr(OC::$SUBURI, -1) != '/') { |
||
153 | OC::$SUBURI = OC::$SUBURI . '/'; |
||
154 | } |
||
155 | OC::$SUBURI = OC::$SUBURI . 'index.php'; |
||
156 | } |
||
157 | } |
||
158 | |||
159 | |||
160 | if (OC::$CLI) { |
||
161 | OC::$WEBROOT = OC_Config::getValue('overwritewebroot', ''); |
||
162 | } else { |
||
163 | if (substr($scriptName, 0 - strlen(OC::$SUBURI)) === OC::$SUBURI) { |
||
164 | OC::$WEBROOT = substr($scriptName, 0, 0 - strlen(OC::$SUBURI)); |
||
165 | |||
166 | if (OC::$WEBROOT != '' && OC::$WEBROOT[0] !== '/') { |
||
167 | OC::$WEBROOT = '/' . OC::$WEBROOT; |
||
168 | } |
||
169 | } else { |
||
170 | // The scriptName is not ending with OC::$SUBURI |
||
171 | // This most likely means that we are calling from CLI. |
||
172 | // However some cron jobs still need to generate |
||
173 | // a web URL, so we use overwritewebroot as a fallback. |
||
174 | OC::$WEBROOT = OC_Config::getValue('overwritewebroot', ''); |
||
175 | } |
||
176 | } |
||
177 | |||
178 | // search the 3rdparty folder |
||
179 | OC::$THIRDPARTYROOT = OC_Config::getValue('3rdpartyroot', null); |
||
180 | OC::$THIRDPARTYWEBROOT = OC_Config::getValue('3rdpartyurl', null); |
||
181 | |||
182 | if (empty(OC::$THIRDPARTYROOT) && empty(OC::$THIRDPARTYWEBROOT)) { |
||
183 | if (file_exists(OC::$SERVERROOT . '/3rdparty')) { |
||
184 | OC::$THIRDPARTYROOT = OC::$SERVERROOT; |
||
185 | OC::$THIRDPARTYWEBROOT = OC::$WEBROOT; |
||
186 | } elseif (file_exists(OC::$SERVERROOT . '/../3rdparty')) { |
||
187 | OC::$THIRDPARTYWEBROOT = rtrim(dirname(OC::$WEBROOT), '/'); |
||
188 | OC::$THIRDPARTYROOT = rtrim(dirname(OC::$SERVERROOT), '/'); |
||
189 | } |
||
190 | } |
||
191 | if (empty(OC::$THIRDPARTYROOT) || !file_exists(OC::$THIRDPARTYROOT)) { |
||
192 | throw new \RuntimeException('3rdparty directory not found! Please put the ownCloud 3rdparty' |
||
193 | . ' folder in the ownCloud folder or the folder above.' |
||
194 | . ' You can also configure the location in the config.php file.'); |
||
195 | } |
||
196 | |||
197 | // search the apps folder |
||
198 | $config_paths = OC_Config::getValue('apps_paths', array()); |
||
199 | if (!empty($config_paths)) { |
||
200 | foreach ($config_paths as $paths) { |
||
201 | if (isset($paths['url']) && isset($paths['path'])) { |
||
202 | $paths['url'] = rtrim($paths['url'], '/'); |
||
203 | $paths['path'] = rtrim($paths['path'], '/'); |
||
204 | OC::$APPSROOTS[] = $paths; |
||
205 | } |
||
206 | } |
||
207 | View Code Duplication | } elseif (file_exists(OC::$SERVERROOT . '/apps')) { |
|
|
|||
208 | OC::$APPSROOTS[] = array('path' => OC::$SERVERROOT . '/apps', 'url' => '/apps', 'writable' => true); |
||
209 | } elseif (file_exists(OC::$SERVERROOT . '/../apps')) { |
||
210 | OC::$APPSROOTS[] = array( |
||
211 | 'path' => rtrim(dirname(OC::$SERVERROOT), '/') . '/apps', |
||
212 | 'url' => '/apps', |
||
213 | 'writable' => true |
||
214 | ); |
||
215 | } |
||
216 | |||
217 | if (empty(OC::$APPSROOTS)) { |
||
218 | throw new \RuntimeException('apps directory not found! Please put the ownCloud apps folder in the ownCloud folder' |
||
219 | . ' or the folder above. You can also configure the location in the config.php file.'); |
||
220 | } |
||
221 | $paths = array(); |
||
222 | foreach (OC::$APPSROOTS as $path) { |
||
223 | $paths[] = $path['path']; |
||
224 | if (!is_dir($path['path'])) { |
||
225 | throw new \RuntimeException(sprintf('App directory "%s" not found! Please put the ownCloud apps folder in the' |
||
226 | . ' ownCloud folder or the folder above. You can also configure the location in the' |
||
227 | . ' config.php file.', $path['path'])); |
||
228 | } |
||
229 | } |
||
230 | |||
231 | // set the right include path |
||
232 | set_include_path( |
||
233 | OC::$SERVERROOT . '/lib/private' . PATH_SEPARATOR . |
||
234 | OC::$SERVERROOT . '/config' . PATH_SEPARATOR . |
||
235 | OC::$THIRDPARTYROOT . '/3rdparty' . PATH_SEPARATOR . |
||
236 | implode(PATH_SEPARATOR, $paths) . PATH_SEPARATOR . |
||
237 | get_include_path() . PATH_SEPARATOR . |
||
238 | OC::$SERVERROOT |
||
239 | ); |
||
240 | } |
||
241 | |||
242 | public static function checkConfig() { |
||
243 | $l = \OC::$server->getL10N('lib'); |
||
244 | |||
245 | // Create config if it does not already exist |
||
246 | $configFilePath = self::$configDir .'/config.php'; |
||
247 | if(!file_exists($configFilePath)) { |
||
248 | @touch($configFilePath); |
||
249 | } |
||
250 | |||
251 | // Check if config is writable |
||
252 | $configFileWritable = is_writable($configFilePath); |
||
253 | if (!$configFileWritable && !OC_Helper::isReadOnlyConfigEnabled() |
||
254 | || !$configFileWritable && self::checkUpgrade(false)) { |
||
255 | if (self::$CLI) { |
||
256 | echo $l->t('Cannot write into "config" directory!')."\n"; |
||
257 | echo $l->t('This can usually be fixed by giving the webserver write access to the config directory')."\n"; |
||
258 | echo "\n"; |
||
259 | echo $l->t('See %s', array(\OC_Helper::linkToDocs('admin-dir_permissions')))."\n"; |
||
260 | exit; |
||
261 | } else { |
||
262 | OC_Template::printErrorPage( |
||
263 | $l->t('Cannot write into "config" directory!'), |
||
264 | $l->t('This can usually be fixed by ' |
||
265 | . '%sgiving the webserver write access to the config directory%s.', |
||
266 | array('<a href="'.\OC_Helper::linkToDocs('admin-dir_permissions').'" target="_blank">', '</a>')) |
||
267 | ); |
||
268 | } |
||
269 | } |
||
270 | } |
||
271 | |||
272 | public static function checkInstalled() { |
||
273 | if (defined('OC_CONSOLE')) { |
||
274 | return; |
||
275 | } |
||
276 | // Redirect to installer if not installed |
||
277 | if (!\OC::$server->getSystemConfig()->getValue('installed', false) && OC::$SUBURI != '/index.php') { |
||
278 | if (OC::$CLI) { |
||
279 | throw new Exception('Not installed'); |
||
280 | } else { |
||
281 | $url = 'http://' . $_SERVER['SERVER_NAME'] . OC::$WEBROOT . '/index.php'; |
||
282 | header('Location: ' . $url); |
||
283 | } |
||
284 | exit(); |
||
285 | } |
||
286 | } |
||
287 | |||
288 | public static function checkMaintenanceMode() { |
||
289 | // Allow ajax update script to execute without being stopped |
||
290 | if (\OC::$server->getSystemConfig()->getValue('maintenance', false) && OC::$SUBURI != '/core/ajax/update.php') { |
||
291 | // send http status 503 |
||
292 | header('HTTP/1.1 503 Service Temporarily Unavailable'); |
||
293 | header('Status: 503 Service Temporarily Unavailable'); |
||
294 | header('Retry-After: 120'); |
||
295 | |||
296 | // render error page |
||
297 | $template = new OC_Template('', 'update.user', 'guest'); |
||
298 | OC_Util::addscript('maintenance-check'); |
||
299 | $template->printPage(); |
||
300 | die(); |
||
301 | } |
||
302 | } |
||
303 | |||
304 | public static function checkSingleUserMode($lockIfNoUserLoggedIn = false) { |
||
305 | if (!\OC::$server->getSystemConfig()->getValue('singleuser', false)) { |
||
306 | return; |
||
307 | } |
||
308 | $user = OC_User::getUserSession()->getUser(); |
||
309 | if ($user) { |
||
310 | $group = \OC::$server->getGroupManager()->get('admin'); |
||
311 | if ($group->inGroup($user)) { |
||
312 | return; |
||
313 | } |
||
314 | } else { |
||
315 | if(!$lockIfNoUserLoggedIn) { |
||
316 | return; |
||
317 | } |
||
318 | } |
||
319 | // send http status 503 |
||
320 | header('HTTP/1.1 503 Service Temporarily Unavailable'); |
||
321 | header('Status: 503 Service Temporarily Unavailable'); |
||
322 | header('Retry-After: 120'); |
||
323 | |||
324 | // render error page |
||
325 | $template = new OC_Template('', 'singleuser.user', 'guest'); |
||
326 | $template->printPage(); |
||
327 | die(); |
||
328 | } |
||
329 | |||
330 | /** |
||
331 | * check if the instance needs to perform an upgrade |
||
332 | * |
||
333 | * @return bool |
||
334 | * @deprecated use \OCP\Util::needUpgrade() instead |
||
335 | */ |
||
336 | public static function needUpgrade() { |
||
337 | return \OCP\Util::needUpgrade(); |
||
338 | } |
||
339 | |||
340 | /** |
||
341 | * Checks if the version requires an update and shows |
||
342 | * @param bool $showTemplate Whether an update screen should get shown |
||
343 | * @return bool|void |
||
344 | */ |
||
345 | 14 | public static function checkUpgrade($showTemplate = true) { |
|
346 | 14 | if (\OCP\Util::needUpgrade()) { |
|
347 | $systemConfig = \OC::$server->getSystemConfig(); |
||
348 | if ($showTemplate && !$systemConfig->getValue('maintenance', false)) { |
||
349 | self::printUpgradePage(); |
||
350 | exit(); |
||
351 | } else { |
||
352 | return true; |
||
353 | } |
||
354 | } |
||
355 | 14 | return false; |
|
356 | } |
||
357 | |||
358 | /** |
||
359 | * Prints the upgrade page |
||
360 | */ |
||
361 | private static function printUpgradePage() { |
||
362 | $systemConfig = \OC::$server->getSystemConfig(); |
||
363 | $oldTheme = $systemConfig->getValue('theme'); |
||
364 | $systemConfig->setValue('theme', ''); |
||
365 | \OCP\Util::addScript('config'); // needed for web root |
||
366 | \OCP\Util::addScript('update'); |
||
367 | |||
368 | // check whether this is a core update or apps update |
||
369 | $installedVersion = $systemConfig->getValue('version', '0.0.0'); |
||
370 | $currentVersion = implode('.', OC_Util::getVersion()); |
||
371 | |||
372 | $appManager = \OC::$server->getAppManager(); |
||
373 | |||
374 | $tmpl = new OC_Template('', 'update.admin', 'guest'); |
||
375 | $tmpl->assign('version', OC_Util::getVersionString()); |
||
376 | |||
377 | // if not a core upgrade, then it's apps upgrade |
||
378 | if (version_compare($currentVersion, $installedVersion, '=')) { |
||
379 | $tmpl->assign('isAppsOnlyUpgrade', true); |
||
380 | } else { |
||
381 | $tmpl->assign('isAppsOnlyUpgrade', false); |
||
382 | } |
||
383 | |||
384 | // get third party apps |
||
385 | $ocVersion = OC_Util::getVersion(); |
||
386 | $tmpl->assign('appsToUpgrade', $appManager->getAppsNeedingUpgrade($ocVersion)); |
||
387 | $tmpl->assign('incompatibleAppsList', $appManager->getIncompatibleApps($ocVersion)); |
||
388 | $tmpl->assign('productName', 'ownCloud'); // for now |
||
389 | $tmpl->assign('oldTheme', $oldTheme); |
||
390 | $tmpl->printPage(); |
||
391 | } |
||
392 | |||
393 | public static function initSession() { |
||
394 | // prevents javascript from accessing php session cookies |
||
395 | ini_set('session.cookie_httponly', true); |
||
396 | |||
397 | // set the cookie path to the ownCloud directory |
||
398 | $cookie_path = OC::$WEBROOT ? : '/'; |
||
399 | ini_set('session.cookie_path', $cookie_path); |
||
400 | |||
401 | // Let the session name be changed in the initSession Hook |
||
402 | $sessionName = OC_Util::getInstanceId(); |
||
403 | |||
404 | try { |
||
405 | // Allow session apps to create a custom session object |
||
406 | $useCustomSession = false; |
||
407 | $session = self::$server->getSession(); |
||
408 | OC_Hook::emit('OC', 'initSession', array('session' => &$session, 'sessionName' => &$sessionName, 'useCustomSession' => &$useCustomSession)); |
||
409 | if (!$useCustomSession) { |
||
410 | // set the session name to the instance id - which is unique |
||
411 | $session = new \OC\Session\Internal($sessionName); |
||
412 | } |
||
413 | |||
414 | $cryptoWrapper = \OC::$server->getSessionCryptoWrapper(); |
||
415 | $session = $cryptoWrapper->wrapSession($session); |
||
416 | self::$server->setSession($session); |
||
417 | |||
418 | // if session cant be started break with http 500 error |
||
419 | } catch (Exception $e) { |
||
420 | \OCP\Util::logException('base', $e); |
||
421 | //show the user a detailed error page |
||
422 | OC_Response::setStatus(OC_Response::STATUS_INTERNAL_SERVER_ERROR); |
||
423 | OC_Template::printExceptionErrorPage($e); |
||
424 | } |
||
425 | |||
426 | $sessionLifeTime = self::getSessionLifeTime(); |
||
427 | // regenerate session id periodically to avoid session fixation |
||
428 | /** |
||
429 | * @var \OCP\ISession $session |
||
430 | */ |
||
431 | $session = self::$server->getSession(); |
||
432 | if (!$session->exists('SID_CREATED')) { |
||
433 | $session->set('SID_CREATED', time()); |
||
434 | } else if (time() - $session->get('SID_CREATED') > $sessionLifeTime / 2) { |
||
435 | session_regenerate_id(true); |
||
436 | $session->set('SID_CREATED', time()); |
||
437 | } |
||
438 | |||
439 | // session timeout |
||
440 | if ($session->exists('LAST_ACTIVITY') && (time() - $session->get('LAST_ACTIVITY') > $sessionLifeTime)) { |
||
441 | if (isset($_COOKIE[session_name()])) { |
||
442 | setcookie(session_name(), '', time() - 42000, $cookie_path); |
||
443 | } |
||
444 | $session->clear(); |
||
445 | } |
||
446 | |||
447 | $session->set('LAST_ACTIVITY', time()); |
||
448 | } |
||
449 | |||
450 | /** |
||
451 | * @return string |
||
452 | */ |
||
453 | private static function getSessionLifeTime() { |
||
456 | |||
457 | public static function loadAppClassPaths() { |
||
458 | View Code Duplication | foreach (OC_APP::getEnabledApps() as $app) { |
|
459 | $file = OC_App::getAppPath($app) . '/appinfo/classpath.php'; |
||
460 | if (file_exists($file)) { |
||
461 | require_once $file; |
||
462 | } |
||
463 | } |
||
464 | } |
||
465 | |||
466 | /** |
||
467 | * Try to set some values to the required ownCloud default |
||
468 | */ |
||
469 | public static function setRequiredIniValues() { |
||
472 | |||
473 | public static function init() { |
||
474 | // calculate the root directories |
||
475 | OC::$SERVERROOT = str_replace("\\", '/', substr(__DIR__, 0, -4)); |
||
476 | |||
477 | // register autoloader |
||
478 | $loaderStart = microtime(true); |
||
479 | require_once __DIR__ . '/autoloader.php'; |
||
480 | self::$loader = new \OC\Autoloader([ |
||
481 | OC::$SERVERROOT . '/lib', |
||
482 | OC::$SERVERROOT . '/core', |
||
687 | |||
688 | private static function registerLocalAddressBook() { |
||
695 | |||
696 | /** |
||
697 | * register hooks for the cache |
||
698 | */ |
||
699 | 263 | public static function registerCacheHooks() { |
|
720 | |||
721 | private static function registerEncryptionWrapper() { |
||
725 | |||
726 | private static function registerEncryptionHooks() { |
||
735 | |||
736 | /** |
||
737 | * register hooks for the cache |
||
738 | */ |
||
739 | public static function registerLogRotate() { |
||
747 | |||
748 | /** |
||
749 | * register hooks for the filesystem |
||
750 | */ |
||
751 | public static function registerFilesystemHooks() { |
||
756 | |||
757 | /** |
||
758 | * register hooks for previews |
||
759 | */ |
||
760 | public static function registerPreviewHooks() { |
||
770 | 78 | ||
771 | 78 | /** |
|
772 | 78 | * register hooks for sharing |
|
773 | 78 | */ |
|
774 | 78 | public static function registerShareHooks() { |
|
783 | |||
784 | protected static function registerAutoloaderCache() { |
||
800 | |||
801 | /** |
||
802 | * Handle the request |
||
803 | */ |
||
804 | public static function handleRequest() { |
||
914 | |||
915 | protected static function handleAuthHeaders() { |
||
935 | |||
936 | protected static function handleLogin() { |
||
962 | |||
963 | /** |
||
964 | * Remove outdated and therefore invalid tokens for a user |
||
965 | * @param string $user |
||
966 | */ |
||
967 | protected static function cleanupLoginTokens($user) { |
||
978 | |||
979 | /** |
||
980 | * Try to login a user via HTTP authentication |
||
981 | * @return bool|void |
||
982 | */ |
||
983 | protected static function tryApacheAuth() { |
||
996 | |||
997 | /** |
||
998 | * Try to login a user using the remember me cookie. |
||
999 | * @return bool Whether the provided cookie was valid |
||
1000 | */ |
||
1001 | protected static function tryRememberLogin() { |
||
1035 | |||
1036 | /** |
||
1037 | * Tries to login a user using the form based authentication |
||
1038 | * @return bool|void |
||
1039 | */ |
||
1040 | protected static function tryFormLogin() { |
||
1079 | |||
1080 | } |
||
1081 | |||
1084 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.