| Conditions | 8 |
| Paths | 6 |
| Total Lines | 55 |
| Code Lines | 25 |
| 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 |
||
| 32 | public static function headerEvent () { |
||
| 33 | //get preferences first |
||
| 34 | $prefs = new Preferences("plugin_httpauth"); |
||
| 35 | |||
| 36 | $activated = $prefs->get("activated", true); |
||
| 37 | |||
| 38 | if (!$activated) { |
||
| 39 | return; |
||
| 40 | } |
||
| 41 | |||
| 42 | //check, if user is logged in |
||
| 43 | if (User::current()->isLoggedIn()) { |
||
| 44 | //http auth is not required, because user is already logged in |
||
| 45 | return; |
||
| 46 | } |
||
| 47 | |||
| 48 | //check, if credentials was already send |
||
| 49 | if (!isset($_SERVER['PHP_AUTH_USER']) || !isset($_SERVER['PHP_AUTH_PW'])) { |
||
| 50 | self::sendHeader($prefs); |
||
| 51 | } else { |
||
| 52 | $username = $_SERVER['PHP_AUTH_USER']; |
||
| 53 | $password = $_SERVER['PHP_AUTH_PW']; |
||
| 54 | |||
| 55 | //try to login |
||
| 56 | $res = User::current()->loginByUsername($username, $password); |
||
| 57 | |||
| 58 | if ($res['success'] !== true) { |
||
| 59 | //send http header again |
||
| 60 | self::sendHeader($prefs); |
||
| 61 | } else { |
||
| 62 | //login successful, show redirect |
||
| 63 | if (isset($_REQUEST['redirect_url']) && !empty($_REQUEST['redirect_url'])) { |
||
| 64 | //TODO: check for security issues, maybe we should check if redirect_url is a known domain |
||
| 65 | |||
| 66 | header("Location: " . urldecode($_REQUEST['redirect_url'])); |
||
| 67 | |||
| 68 | //flush gzip buffer |
||
| 69 | ob_end_flush(); |
||
| 70 | |||
| 71 | exit; |
||
|
|
|||
| 72 | } else { |
||
| 73 | //redirect to index page |
||
| 74 | |||
| 75 | //get domain |
||
| 76 | $domain = Registry::singleton()->getObject("domain"); |
||
| 77 | |||
| 78 | //generate index url |
||
| 79 | $index_url = DomainUtils::generateURL($domain->getHomePage()); |
||
| 80 | |||
| 81 | header("Location: " . $index_url); |
||
| 82 | |||
| 83 | //flush gzip buffer |
||
| 84 | ob_end_flush(); |
||
| 85 | |||
| 86 | exit; |
||
| 87 | } |
||
| 118 |
In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.