| Conditions | 29 |
| Paths | 1483 |
| Total Lines | 126 |
| Code Lines | 69 |
| 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 |
||
| 30 | public function getContent() : string { |
||
| 31 | $show_form = !User::current()->isLoggedIn(); |
||
| 32 | |||
| 33 | $template = new Template("pages/login", Registry::singleton()); |
||
| 34 | |||
| 35 | if (isset($_REQUEST['action']) && $_REQUEST['action'] === "login") { |
||
| 36 | //try to login |
||
| 37 | |||
| 38 | $username_set = false; |
||
| 39 | $mail_set = false; |
||
| 40 | $password_set = false; |
||
| 41 | |||
| 42 | if (isset($_POST['username']) && !empty($_POST['username'])) { |
||
| 43 | $username_set = true; |
||
| 44 | } |
||
| 45 | |||
| 46 | if (isset($_POST['mail']) && !empty($_POST['mail'])) { |
||
| 47 | $mail_set = true; |
||
| 48 | } |
||
| 49 | |||
| 50 | if (isset($_POST['password']) && !empty($_POST['password'])) { |
||
| 51 | $password_set = true; |
||
| 52 | } |
||
| 53 | |||
| 54 | if (!$username_set && !$mail_set && !$password_set) { |
||
| 55 | //form was not submitted |
||
| 56 | } else { |
||
| 57 | if (!$username_set && !$mail_set) { |
||
| 58 | $template->parse("main.no_username"); |
||
| 59 | $template->parse("main.no_mail"); |
||
| 60 | } |
||
| 61 | |||
| 62 | if (!$password_set) { |
||
| 63 | $template->parse("main.no_password"); |
||
| 64 | } |
||
| 65 | } |
||
| 66 | |||
| 67 | if (($username_set || $mail_set) && $password_set) { |
||
| 68 | //check CSRF token |
||
| 69 | if (Security::checkCSRFToken()) { |
||
| 70 | //check, if user is already logged in |
||
| 71 | if (User::current()->isLoggedIn()) { |
||
| 72 | $template->assign("ERROR_TEXT", "User is already logged in!"); |
||
| 73 | $template->parse("main.error_msg"); |
||
| 74 | |||
| 75 | //dont show form, because user is already logged in |
||
| 76 | $show_form = false; |
||
| 77 | } else { |
||
| 78 | //try to login |
||
| 79 | $user = User::current(); |
||
| 80 | |||
| 81 | if ($username_set) { |
||
| 82 | $res = $user->loginByUsername($_REQUEST['username'], $_REQUEST['password']); |
||
| 83 | } else { |
||
| 84 | $res = $user->loginByMail($_REQUEST['mail'], $_REQUEST['password']); |
||
| 85 | } |
||
| 86 | |||
| 87 | if ($res['success'] === true) { |
||
| 88 | //login successful, show redirect |
||
| 89 | |||
| 90 | if (isset($_REQUEST['redirect_url']) && !empty($_REQUEST['redirect_url'])) { |
||
| 91 | //TODO: check for security issues, maybe we should check if redirect_url is a known domain |
||
| 92 | |||
| 93 | header("Location: " . urldecode($_REQUEST['redirect_url'])); |
||
| 94 | |||
| 95 | //flush gzip buffer |
||
| 96 | ob_end_flush(); |
||
| 97 | |||
| 98 | exit; |
||
| 99 | } else { |
||
| 100 | //redirect to index page |
||
| 101 | |||
| 102 | //get domain |
||
| 103 | $domain = Registry::singleton()->getObject("domain"); |
||
| 104 | |||
| 105 | //generate index url |
||
| 106 | $index_url = DomainUtils::generateURL($domain->getHomePage()); |
||
| 107 | |||
| 108 | header("Location: " . $index_url); |
||
| 109 | |||
| 110 | //flush gzip buffer |
||
| 111 | ob_end_flush(); |
||
| 112 | |||
| 113 | exit; |
||
| 114 | } |
||
| 115 | |||
| 116 | $template->parse("login_successful"); |
||
| 117 | |||
| 118 | Events::throwEvent("page_login_successful"); |
||
| 119 | |||
| 120 | $show_form = false; |
||
| 121 | } else { |
||
| 122 | if ($res['error'] === "user_not_exists") { |
||
| 123 | $template->assign("ERROR_TEXT", /*"Username doesnt exists!"*/"Wrong credentials!"); |
||
| 124 | $template->parse("main.error_msg"); |
||
| 125 | } else if ($res['error'] === "wrong_password") { |
||
| 126 | $template->assign("ERROR_TEXT", /*"Wrong password!"*/"Wrong credentials!"); |
||
| 127 | $template->parse("main.error_msg"); |
||
| 128 | } else if ($res['error'] === "mail_not_valide") { |
||
| 129 | $template->assign("ERROR_TEXT", /*"Mail is not valide!"*/"Wrong credentials!"); |
||
| 130 | $template->parse("main.error_msg"); |
||
| 131 | } else { |
||
| 132 | $template->assign("ERROR_TEXT", "Unknown error message: " . $res['error']); |
||
| 133 | $template->parse("main.error_msg"); |
||
| 134 | } |
||
| 135 | } |
||
| 136 | } |
||
| 137 | } else { |
||
| 138 | $template->assign("ERROR_TEXT", "Wrong CSRF token! Please try to login again!"); |
||
| 139 | $template->parse("main.error_msg"); |
||
| 140 | } |
||
| 141 | } |
||
| 142 | } |
||
| 143 | |||
| 144 | if ($show_form) {//show form |
||
| 145 | $template->parse("main.form"); |
||
| 146 | } else if (User::current()->isLoggedIn()) { |
||
| 147 | $template->assign("USERID", User::current()->getID()); |
||
| 148 | $template->assign("USERNAME", User::current()->getUsername()); |
||
| 149 | |||
| 150 | $template->parse("main.already_logged_in"); |
||
| 151 | } |
||
| 152 | |||
| 153 | //get HTML code |
||
| 154 | $template->parse(); |
||
| 155 | return $template->getCode(); |
||
| 156 | } |
||
| 161 |
In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.