| Total Complexity | 40 |
| Total Lines | 448 |
| Duplicated Lines | 0 % |
| Changes | 2 | ||
| Bugs | 1 | Features | 0 |
Complex classes like Pref_Users 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.
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 Pref_Users, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 2 | class Pref_Users extends Handler_Protected { |
||
| 3 | public function before($method) { |
||
| 4 | if (parent::before($method)) { |
||
| 5 | if ($_SESSION["access_level"] < 10) { |
||
| 6 | print __("Your access level is insufficient to open this tab."); |
||
| 7 | return false; |
||
| 8 | } |
||
| 9 | return true; |
||
| 10 | } |
||
| 11 | return false; |
||
| 12 | } |
||
| 13 | |||
| 14 | public function csrf_ignore($method) { |
||
| 18 | } |
||
| 19 | |||
| 20 | public function edit() { |
||
| 21 | global $access_level_names; |
||
| 22 | |||
| 23 | print "<form id='user_edit_form' onsubmit='return false' dojoType='dijit.form.Form'>"; |
||
| 24 | |||
| 25 | print '<div dojoType="dijit.layout.TabContainer" style="height : 400px"> |
||
| 26 | <div dojoType="dijit.layout.ContentPane" title="'.__('Edit user').'">'; |
||
| 27 | |||
| 28 | //print "<form id=\"user_edit_form\" onsubmit='return false' dojoType=\"dijit.form.Form\">"; |
||
| 29 | |||
| 30 | $id = (int) clean($_REQUEST["id"]); |
||
| 31 | |||
| 32 | print_hidden("id", "$id"); |
||
| 33 | print_hidden("op", "pref-users"); |
||
| 34 | print_hidden("method", "editSave"); |
||
| 35 | |||
| 36 | $sth = $this->pdo->prepare("SELECT * FROM ttrss_users WHERE id = ?"); |
||
| 37 | $sth->execute([$id]); |
||
| 38 | |||
| 39 | if ($row = $sth->fetch()) { |
||
| 40 | |||
| 41 | $login = $row["login"]; |
||
| 42 | $access_level = $row["access_level"]; |
||
| 43 | $email = $row["email"]; |
||
| 44 | |||
| 45 | $sel_disabled = ($id == $_SESSION["uid"] || $login == "admin") ? "disabled" : ""; |
||
| 46 | |||
| 47 | print "<header>".__("User")."</header>"; |
||
| 48 | print "<section>"; |
||
| 49 | |||
| 50 | if ($sel_disabled) { |
||
| 51 | print_hidden("login", "$login"); |
||
| 52 | } |
||
| 53 | |||
| 54 | print "<fieldset>"; |
||
| 55 | print "<label>".__("Login:")."</label>"; |
||
| 56 | print "<input style='font-size : 16px' |
||
| 57 | dojoType='dijit.form.ValidationTextBox' required='1' |
||
| 58 | $sel_disabled name='login' value=\"$login\">"; |
||
| 59 | print "</fieldset>"; |
||
| 60 | |||
| 61 | print "</section>"; |
||
| 62 | |||
| 63 | print "<header>".__("Authentication")."</header>"; |
||
| 64 | print "<section>"; |
||
| 65 | |||
| 66 | print "<fieldset>"; |
||
| 67 | |||
| 68 | print "<label>".__('Access level: ')."</label> "; |
||
| 69 | |||
| 70 | if (!$sel_disabled) { |
||
| 71 | print_select_hash("access_level", $access_level, $access_level_names, |
||
| 72 | "dojoType=\"fox.form.Select\" $sel_disabled"); |
||
| 73 | } else { |
||
| 74 | print_select_hash("", $access_level, $access_level_names, |
||
| 75 | "dojoType=\"fox.form.Select\" $sel_disabled"); |
||
| 76 | print_hidden("access_level", "$access_level"); |
||
| 77 | } |
||
| 78 | |||
| 79 | print "</fieldset>"; |
||
| 80 | print "<fieldset>"; |
||
| 81 | |||
| 82 | print "<label>".__("New password:")."</label> "; |
||
| 83 | print "<input dojoType='dijit.form.TextBox' type='password' size='20' placeholder='Change password' |
||
| 84 | name='password'>"; |
||
| 85 | |||
| 86 | print "</fieldset>"; |
||
| 87 | |||
| 88 | print "</section>"; |
||
| 89 | |||
| 90 | print "<header>".__("Options")."</header>"; |
||
| 91 | print "<section>"; |
||
| 92 | |||
| 93 | print "<fieldset>"; |
||
| 94 | print "<label>".__("E-mail:")."</label> "; |
||
| 95 | print "<input dojoType='dijit.form.TextBox' size='30' name='email' |
||
| 96 | value=\"$email\">"; |
||
| 97 | print "</fieldset>"; |
||
| 98 | |||
| 99 | print "</section>"; |
||
| 100 | |||
| 101 | print "</table>"; |
||
| 102 | |||
| 103 | } |
||
| 104 | |||
| 105 | print '</div>'; #tab |
||
| 106 | print "<div href=\"backend.php?op=pref-users&method=userdetails&id=$id\" |
||
| 107 | dojoType=\"dijit.layout.ContentPane\" title=\"".__('User details')."\">"; |
||
| 108 | |||
| 109 | print '</div>'; |
||
| 110 | print '</div>'; |
||
| 111 | |||
| 112 | print "<footer> |
||
| 113 | <button dojoType='dijit.form.Button' class='alt-primary' type='submit' onclick=\"dijit.byId('userEditDlg').execute()\">". |
||
| 114 | __('Save')."</button> |
||
| 115 | <button dojoType='dijit.form.Button' onclick=\"dijit.byId('userEditDlg').hide()\">". |
||
| 116 | __('Cancel')."</button> |
||
| 117 | </footer>"; |
||
| 118 | |||
| 119 | print "</form>"; |
||
| 120 | |||
| 121 | return; |
||
| 122 | } |
||
| 123 | |||
| 124 | public function userdetails() { |
||
| 125 | $id = (int) clean($_REQUEST["id"]); |
||
| 126 | |||
| 127 | $sth = $this->pdo->prepare("SELECT login, |
||
| 128 | ".SUBSTRING_FOR_DATE."(last_login,1,16) AS last_login, |
||
| 129 | access_level, |
||
| 130 | (SELECT COUNT(int_id) FROM ttrss_user_entries |
||
| 131 | WHERE owner_uid = id) AS stored_articles, |
||
| 132 | ".SUBSTRING_FOR_DATE."(created,1,16) AS created |
||
| 133 | FROM ttrss_users |
||
| 134 | WHERE id = ?"); |
||
| 135 | $sth->execute([$id]); |
||
| 136 | |||
| 137 | if ($row = $sth->fetch()) { |
||
| 138 | print "<table width='100%'>"; |
||
| 139 | |||
| 140 | $last_login = make_local_datetime( |
||
| 141 | $row["last_login"], true); |
||
| 142 | |||
| 143 | $created = make_local_datetime( |
||
| 144 | $row["created"], true); |
||
| 145 | |||
| 146 | $stored_articles = $row["stored_articles"]; |
||
| 147 | |||
| 148 | print "<tr><td>".__('Registered')."</td><td>$created</td></tr>"; |
||
| 149 | print "<tr><td>".__('Last logged in')."</td><td>$last_login</td></tr>"; |
||
| 150 | |||
| 151 | $sth = $this->pdo->prepare("SELECT COUNT(id) as num_feeds FROM ttrss_feeds |
||
| 152 | WHERE owner_uid = ?"); |
||
| 153 | $sth->execute([$id]); |
||
| 154 | $row = $sth->fetch(); |
||
| 155 | $num_feeds = $row["num_feeds"]; |
||
| 156 | |||
| 157 | print "<tr><td>".__('Subscribed feeds count')."</td><td>$num_feeds</td></tr>"; |
||
| 158 | print "<tr><td>".__('Stored articles')."</td><td>$stored_articles</td></tr>"; |
||
| 159 | |||
| 160 | print "</table>"; |
||
| 161 | |||
| 162 | print "<h1>".__('Subscribed feeds')."</h1>"; |
||
| 163 | |||
| 164 | $sth = $this->pdo->prepare("SELECT id,title,site_url FROM ttrss_feeds |
||
| 165 | WHERE owner_uid = ? ORDER BY title"); |
||
| 166 | $sth->execute([$id]); |
||
| 167 | |||
| 168 | print "<ul class=\"panel panel-scrollable list list-unstyled\">"; |
||
| 169 | |||
| 170 | while ($line = $sth->fetch()) { |
||
| 171 | |||
| 172 | $icon_file = ICONS_URL."/".$line["id"].".ico"; |
||
|
|
|||
| 173 | |||
| 174 | if (file_exists($icon_file) && filesize($icon_file) > 0) { |
||
| 175 | $feed_icon = "<img class=\"icon\" src=\"$icon_file\">"; |
||
| 176 | } else { |
||
| 177 | $feed_icon = "<img class=\"icon\" src=\"images/blank_icon.gif\">"; |
||
| 178 | } |
||
| 179 | |||
| 180 | print "<li>$feed_icon <a href=\"".$line["site_url"]."\">".$line["title"]."</a></li>"; |
||
| 181 | |||
| 182 | } |
||
| 183 | |||
| 184 | print "</ul>"; |
||
| 185 | |||
| 186 | |||
| 187 | } else { |
||
| 188 | print "<h1>".__('User not found')."</h1>"; |
||
| 189 | } |
||
| 190 | |||
| 191 | } |
||
| 192 | |||
| 193 | public function editSave() { |
||
| 212 | |||
| 213 | } |
||
| 214 | |||
| 215 | public function remove() { |
||
| 228 | } |
||
| 229 | } |
||
| 230 | } |
||
| 231 | |||
| 232 | public function add() { |
||
| 233 | $login = trim(clean($_REQUEST["login"])); |
||
| 234 | $tmp_user_pwd = make_password(); |
||
| 235 | $salt = substr(bin2hex(get_random_bytes(125)), 0, 250); |
||
| 236 | $pwd_hash = encrypt_password($tmp_user_pwd, $salt, true); |
||
| 237 | |||
| 238 | if (!$login) { |
||
| 239 | return; |
||
| 240 | } |
||
| 241 | // no blank usernames |
||
| 242 | |||
| 243 | $sth = $this->pdo->prepare("SELECT id FROM ttrss_users WHERE |
||
| 244 | login = ?"); |
||
| 245 | $sth->execute([$login]); |
||
| 246 | |||
| 247 | if (!$sth->fetch()) { |
||
| 248 | |||
| 249 | $sth = $this->pdo->prepare("INSERT INTO ttrss_users |
||
| 250 | (login,pwd_hash,access_level,last_login,created, salt) |
||
| 251 | VALUES (?, ?, 0, null, NOW(), ?)"); |
||
| 252 | $sth->execute([$login, $pwd_hash, $salt]); |
||
| 253 | |||
| 254 | $sth = $this->pdo->prepare("SELECT id FROM ttrss_users WHERE |
||
| 255 | login = ? AND pwd_hash = ?"); |
||
| 256 | $sth->execute([$login, $pwd_hash]); |
||
| 257 | |||
| 258 | if ($row = $sth->fetch()) { |
||
| 259 | |||
| 260 | $new_uid = $row['id']; |
||
| 261 | |||
| 262 | print T_sprintf("Added user %s with password %s", |
||
| 263 | $login, $tmp_user_pwd); |
||
| 264 | |||
| 265 | initialize_user($new_uid); |
||
| 266 | |||
| 267 | } else { |
||
| 268 | |||
| 269 | print T_sprintf("Could not create user %s", $login); |
||
| 270 | |||
| 271 | } |
||
| 272 | } else { |
||
| 273 | print T_sprintf("User %s already exists.", $login); |
||
| 274 | } |
||
| 275 | } |
||
| 276 | |||
| 277 | public static function resetUserPassword($uid, $format_output = false) { |
||
| 304 | } |
||
| 305 | |||
| 306 | } |
||
| 307 | } |
||
| 308 | |||
| 309 | public function resetPass() { |
||
| 310 | $uid = clean($_REQUEST["id"]); |
||
| 311 | Pref_Users::resetUserPassword($uid); |
||
| 312 | } |
||
| 313 | |||
| 314 | public function index() { |
||
| 442 | |||
| 443 | } |
||
| 444 | |||
| 445 | public function validate_field($string, $allowed, $default = "") { |
||
| 454 |