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 USER 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 USER, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 57 | class USER { |
||
| 58 | |||
| 59 | public $user_id = "0"; // So we have an ID for non-logged in users reporting comments etc. |
||
| 60 | public $firstname = "Guest"; // So we have something to print for non-logged in users. |
||
| 61 | public $lastname = ""; |
||
| 62 | public $password = ""; // This will be a hashed version of a plaintext pw. |
||
| 63 | public $email = ""; |
||
| 64 | public $emailpublic = ""; // boolean - can other users see this user's email? |
||
| 65 | public $postcode = ""; |
||
| 66 | public $url = ""; |
||
| 67 | public $lastvisit = ""; // Last time the logged-in user loaded a page (GMT). |
||
| 68 | public $registrationtime = ""; // When they registered (GMT). |
||
| 69 | public $registrationip = ""; // Where they registered from. |
||
| 70 | public $optin = ""; // boolean - Do they want emails from us? |
||
| 71 | public $deleted = ""; // User can't log in or have their info displayed. |
||
| 72 | public $confirmed = ''; // boolean - Has the user confirmed via email? |
||
| 73 | public $facebook_id = ''; // Facebook ID for users who login with FB |
||
| 74 | public $facebook_token = ''; // Facebook token for users who login with FB |
||
| 75 | // Don't use the status to check access privileges - use the is_able_to() function. |
||
| 76 | public $status = "Viewer"; |
||
| 77 | |||
| 78 | // If you add more user variables above you should also: |
||
| 79 | // Add the approrprate code to $this->add() |
||
| 80 | // Add the appropriate code to $this->_update() |
||
| 81 | // Add accessor functions way down below... |
||
| 82 | // Alter THEUSER->update_self() to update with the new vars, if appropriate. |
||
| 83 | // Change things in the add/edit/view user page. |
||
| 84 | |||
| 85 | 1 | public function __construct() { |
|
| 86 | 1 | $this->db = new ParlDB; |
|
| 87 | 1 | } |
|
| 88 | |||
| 89 | 4 | public function init($user_id) { |
|
| 90 | // Pass it a user id and it will fetch the user's data from the db |
||
| 91 | // and put it all in the appropriate variables. |
||
| 92 | // Returns true if we've found user_id in the DB, false otherwise. |
||
| 93 | |||
| 94 | // Look for this user_id's details. |
||
| 95 | 4 | $q = $this->db->query("SELECT firstname, |
|
| 96 | lastname, |
||
| 97 | password, |
||
| 98 | email, |
||
| 99 | emailpublic, |
||
| 100 | postcode, |
||
| 101 | url, |
||
| 102 | lastvisit, |
||
| 103 | registrationtime, |
||
| 104 | registrationtoken, |
||
| 105 | registrationip, |
||
| 106 | optin, |
||
| 107 | status, |
||
| 108 | deleted, |
||
| 109 | confirmed, |
||
| 110 | facebook_id, |
||
| 111 | facebook_token |
||
| 112 | FROM users |
||
| 113 | 4 | WHERE user_id = :user_id", |
|
| 114 | 4 | array(':user_id' => $user_id)); |
|
| 115 | |||
| 116 | |||
| 117 | 4 | if ($q->rows() == 1) { |
|
| 118 | // We've got a user, so set them up. |
||
| 119 | |||
| 120 | 4 | $this->user_id = $user_id; |
|
| 121 | 4 | $this->firstname = $q->field(0,"firstname"); |
|
| 122 | 4 | $this->lastname = $q->field(0,"lastname"); |
|
| 123 | 4 | $this->password = $q->field(0,"password"); |
|
| 124 | 4 | $this->email = $q->field(0,"email"); |
|
| 125 | 4 | $this->emailpublic = $q->field(0,"emailpublic") == 1 ? true : false; |
|
| 126 | 4 | $this->postcode = $q->field(0,"postcode"); |
|
| 127 | 4 | $this->facebook_id = $q->field(0,"facebook_id"); |
|
| 128 | 4 | $this->facebook_token = $q->field(0,"facebook_token"); |
|
| 129 | 4 | $this->url = $q->field(0,"url"); |
|
| 130 | 4 | $this->lastvisit = $q->field(0,"lastvisit"); |
|
| 131 | 4 | $this->registrationtoken = $q->field(0, 'registrationtoken'); |
|
| 132 | 4 | $this->registrationtime = $q->field(0,"registrationtime"); |
|
| 133 | 4 | $this->registrationip = $q->field(0,"registrationip"); |
|
| 134 | 4 | $this->optin = $q->field(0,"optin") == 1 ? true : false; |
|
| 135 | 4 | $this->status = $q->field(0,"status"); |
|
| 136 | 4 | $this->deleted = $q->field(0,"deleted") == 1 ? true : false; |
|
| 137 | 4 | $this->confirmed = $q->field(0,"confirmed") == 1 ? true : false; |
|
| 138 | |||
| 139 | 4 | return true; |
|
| 140 | |||
| 141 | } elseif ($q->rows() > 1) { |
||
| 142 | // And, yes, if we've ended up with more than one row returned |
||
| 143 | // we're going to show an error too, just in case. |
||
| 144 | // *Should* never happen... |
||
| 145 | return false; |
||
| 146 | twfy_debug("USER", "There is more than one user with an id of '" . _htmlentities($user_id) . "'"); |
||
| 147 | |||
| 148 | } else { |
||
| 149 | return false; |
||
| 150 | twfy_debug("USER", "There is no user with an id of '" . _htmlentities($user_id) . "'"); |
||
| 151 | } |
||
| 152 | |||
| 153 | } |
||
| 154 | |||
| 155 | 1 | public function add($details, $confirmation_required=true) { |
|
| 156 | // Adds a new user's info into the db. |
||
| 157 | // Then optionally (and usually) calls another function to |
||
| 158 | // send them a confirmation email. |
||
| 159 | |||
| 160 | // $details is an associative array of all the user's details, of the form: |
||
| 161 | // array ( |
||
| 162 | // "firstname" => "Fred", |
||
| 163 | // "lastname" => "Bloggs", |
||
| 164 | // etc... using the same keys as the object variable names. |
||
| 165 | // ) |
||
| 166 | // The BOOL variables (eg, optin) will be true or false and will need to be |
||
| 167 | // converted to 1/0 for MySQL. |
||
| 168 | 1 | global $REMOTE_ADDR; |
|
| 169 | |||
| 170 | 1 | $registrationtime = gmdate("YmdHis"); |
|
| 171 | |||
| 172 | 1 | $passwordforDB = password_hash($details["password"], PASSWORD_BCRYPT); |
|
| 173 | |||
| 174 | 1 | if (!isset($details["status"])) { |
|
| 175 | $details["status"] = "User"; |
||
| 176 | } |
||
| 177 | |||
| 178 | 1 | if (!isset($details["facebook_id"])) { |
|
| 179 | 1 | $details["facebook_id"] = ""; |
|
| 180 | 1 | } |
|
| 181 | |||
| 182 | 1 | $optin = $details["optin"] == true ? 1 : 0; |
|
| 183 | |||
| 184 | 1 | $emailpublic = $details["emailpublic"] == true ? 1 : 0; |
|
| 185 | |||
| 186 | 1 | $q = $this->db->query("INSERT INTO users ( |
|
| 187 | firstname, |
||
| 188 | lastname, |
||
| 189 | email, |
||
| 190 | emailpublic, |
||
| 191 | postcode, |
||
| 192 | url, |
||
| 193 | password, |
||
| 194 | optin, |
||
| 195 | status, |
||
| 196 | registrationtime, |
||
| 197 | registrationip, |
||
| 198 | facebook_id, |
||
| 199 | deleted |
||
| 200 | ) VALUES ( |
||
| 201 | :firstname, |
||
| 202 | :lastname, |
||
| 203 | :email, |
||
| 204 | :emailpublic, |
||
| 205 | :postcode, |
||
| 206 | :url, |
||
| 207 | :password, |
||
| 208 | :optin, |
||
| 209 | :status, |
||
| 210 | :registrationtime, |
||
| 211 | :registrationip, |
||
| 212 | :facebook_id, |
||
| 213 | '0' |
||
| 214 | ) |
||
| 215 | 1 | ", array( |
|
| 216 | 1 | ':firstname' => $details["firstname"], |
|
| 217 | 1 | ':lastname' => $details["lastname"], |
|
| 218 | 1 | ':email' => $details["email"], |
|
| 219 | 1 | ':emailpublic' => $emailpublic, |
|
| 220 | 1 | ':postcode' => $details["postcode"], |
|
| 221 | 1 | ':url' => $details["url"], |
|
| 222 | 1 | ':password' => $passwordforDB, |
|
| 223 | 1 | ':optin' => $optin, |
|
| 224 | 1 | ':status' => $details["status"], |
|
| 225 | 1 | ':registrationtime' => $registrationtime, |
|
| 226 | 1 | ':facebook_id' => $details["facebook_id"], |
|
| 227 | ':registrationip' => $REMOTE_ADDR |
||
| 228 | 1 | )); |
|
| 229 | |||
| 230 | 1 | if ($q->success()) { |
|
| 231 | // Set these so we can log in. |
||
| 232 | // Except we no longer automatically log new users in, we |
||
| 233 | // send them an email. So this may not be required. |
||
| 234 | 1 | $this->user_id = $q->insert_id(); |
|
| 235 | 1 | $this->password = $passwordforDB; |
|
| 236 | 1 | $this->facebook_id = $details["facebook_id"]; |
|
| 237 | |||
| 238 | // We have to set the user's registration token. |
||
| 239 | // This will be sent to them via email, so we can confirm they exist. |
||
| 240 | // The token will be the first 16 characters of a hash. |
||
| 241 | |||
| 242 | 1 | $token = substr( password_hash($details["email"] . microtime(), PASSWORD_BCRYPT), 29, 16 ); |
|
| 243 | |||
| 244 | // Full stops don't work well at the end of URLs in emails, |
||
| 245 | // so replace them. We won't be doing anything clever with the hash |
||
| 246 | // stuff, just need to match this token. |
||
| 247 | |||
| 248 | 1 | $this->registrationtoken = strtr($token, '.', 'X'); |
|
| 249 | |||
| 250 | // Add that to the DB. |
||
| 251 | 1 | $r = $this->db->query("UPDATE users |
|
| 252 | SET registrationtoken = :registrationtoken |
||
| 253 | WHERE user_id = :user_id |
||
| 254 | 1 | ", array ( |
|
| 255 | 1 | ':registrationtoken' => $this->registrationtoken, |
|
| 256 | 1 | ':user_id' => $this->user_id |
|
| 257 | 1 | )); |
|
| 258 | |||
| 259 | 1 | if ($r->success()) { |
|
| 260 | // Updated DB OK. |
||
| 261 | |||
| 262 | 1 | if ($details['mp_alert'] && $details['postcode']) { |
|
| 263 | $MEMBER = new MEMBER(array('postcode'=>$details['postcode'], 'house'=>HOUSE_TYPE_COMMONS)); |
||
| 264 | $pid = $MEMBER->person_id(); |
||
| 265 | # No confirmation email, but don't automatically confirm |
||
| 266 | $ALERT = new ALERT; |
||
| 267 | $ALERT->add(array( |
||
| 268 | 'email' => $details['email'], |
||
| 269 | 'pid' => $pid, |
||
| 270 | 'pc' => $details['postcode'], |
||
| 271 | ), false, false); |
||
| 272 | } |
||
| 273 | |||
| 274 | 1 | if ($confirmation_required) { |
|
| 275 | // Right, send the email... |
||
| 276 | $success = $this->send_confirmation_email($details); |
||
| 277 | |||
| 278 | if ($success) { |
||
| 279 | // All is good in the world! |
||
| 280 | return true; |
||
| 281 | } else { |
||
| 282 | // Couldn't send the email. |
||
| 283 | return false; |
||
| 284 | } |
||
| 285 | } else { |
||
| 286 | // No confirmation email needed. |
||
| 287 | 1 | return true; |
|
| 288 | } |
||
| 289 | } else { |
||
| 290 | // Couldn't add the registration token to the DB. |
||
| 291 | return false; |
||
| 292 | } |
||
| 293 | |||
| 294 | } else { |
||
| 295 | // Couldn't add the user's data to the DB. |
||
| 296 | return false; |
||
| 297 | } |
||
| 298 | } |
||
| 299 | |||
| 300 | public function add_facebook_id($facebook_id) { |
||
| 301 | $q = $this->db->query ("UPDATE users SET facebook_id = :facebook_id WHERE email = :email", |
||
| 302 | array( |
||
| 303 | ':facebook_id' => $facebook_id, |
||
| 304 | ':email' => $this->email |
||
| 305 | )); |
||
| 306 | |||
| 307 | if ($q->success()) { |
||
| 308 | $this->facebook_id = $facebook_id; |
||
| 309 | |||
| 310 | return $facebook_id; |
||
| 311 | } else { |
||
| 312 | return false; |
||
| 313 | } |
||
| 314 | } |
||
| 315 | |||
| 316 | public function send_email_confirmation_email($details) { |
||
| 317 | // A brief check of the facts... |
||
| 318 | if (!is_numeric($this->user_id) || |
||
| 319 | !isset($details['email']) || |
||
| 320 | $details['email'] == '' || |
||
| 321 | !isset($details['token']) || |
||
| 322 | $details['token'] == '' ) { |
||
| 323 | return false; |
||
| 324 | } |
||
| 325 | |||
| 326 | // We prefix the registration token with the user's id and '-'. |
||
| 327 | // Not for any particularly good reason, but we do. |
||
| 328 | |||
| 329 | $urltoken = $this->user_id . '-' . $details['token']; |
||
| 330 | |||
| 331 | $confirmurl = 'https://' . DOMAIN . '/E/' . $urltoken; |
||
|
1 ignored issue
–
show
|
|||
| 332 | |||
| 333 | // Arrays we need to send a templated email. |
||
| 334 | $data = array ( |
||
| 335 | 'to' => $details['email'], |
||
| 336 | 'template' => 'email_confirmation' |
||
| 337 | ); |
||
| 338 | |||
| 339 | $merge = array ( |
||
| 340 | 'FIRSTNAME' => $details['firstname'], |
||
| 341 | 'LASTNAME' => $details['lastname'], |
||
| 342 | 'CONFIRMURL' => $confirmurl |
||
| 343 | ); |
||
| 344 | |||
| 345 | $success = send_template_email($data, $merge); |
||
| 346 | |||
| 347 | if ($success) { |
||
| 348 | return true; |
||
| 349 | } else { |
||
| 350 | return false; |
||
| 351 | } |
||
| 352 | } |
||
| 353 | |||
| 354 | public function send_confirmation_email($details) { |
||
| 355 | // After we've add()ed a user we'll probably be sending them |
||
| 356 | // a confirmation email with a link to confirm their address. |
||
| 357 | |||
| 358 | // $details is the array we just sent to add(), and which it's |
||
| 359 | // passed on to us here. |
||
| 360 | |||
| 361 | // A brief check of the facts... |
||
| 362 | if (!is_numeric($this->user_id) || |
||
| 363 | !isset($details['email']) || |
||
| 364 | $details['email'] == '') { |
||
| 365 | return false; |
||
| 366 | } |
||
| 367 | |||
| 368 | // We prefix the registration token with the user's id and '-'. |
||
| 369 | // Not for any particularly good reason, but we do. |
||
| 370 | |||
| 371 | $urltoken = $this->user_id . '-' . $this->registrationtoken; |
||
| 372 | |||
| 373 | $confirmurl = 'https://' . DOMAIN . '/U/' . $urltoken; |
||
|
1 ignored issue
–
show
|
|||
| 374 | |||
| 375 | // Arrays we need to send a templated email. |
||
| 376 | $data = array ( |
||
| 377 | 'to' => $details['email'], |
||
| 378 | 'template' => 'join_confirmation' |
||
| 379 | ); |
||
| 380 | |||
| 381 | $merge = array ( |
||
| 382 | 'FIRSTNAME' => $details['firstname'], |
||
| 383 | 'LASTNAME' => $details['lastname'], |
||
| 384 | 'CONFIRMURL' => $confirmurl |
||
| 385 | ); |
||
| 386 | |||
| 387 | $success = send_template_email($data, $merge); |
||
| 388 | |||
| 389 | if ($success) { |
||
| 390 | return true; |
||
| 391 | } else { |
||
| 392 | return false; |
||
| 393 | } |
||
| 394 | } |
||
| 395 | |||
| 396 | |||
| 397 | public function update_other_user($details) { |
||
| 398 | // If someone (like an admin) is updating another user, call this |
||
| 399 | // function. It checks their privileges before letting them. |
||
| 400 | |||
| 401 | // $details is an array like that in $this->add(). |
||
| 402 | // It must include a 'user_id' element! |
||
| 403 | |||
| 404 | global $THEUSER; |
||
| 405 | |||
| 406 | if (!isset($details["user_id"])) { |
||
| 407 | return false; |
||
| 408 | |||
| 409 | } elseif ($THEUSER->is_able_to("edituser")) { |
||
| 410 | |||
| 411 | // If the user doing the updating has appropriate privileges... |
||
| 412 | |||
| 413 | $newdetails = $this->_update($details); |
||
| 414 | |||
| 415 | // $newdetails will be an array of details if all went well, |
||
| 416 | // false otherwise. |
||
| 417 | if ($newdetails) { |
||
| 418 | return true; |
||
| 419 | } else { |
||
| 420 | return false; |
||
| 421 | } |
||
| 422 | |||
| 423 | } else { |
||
| 424 | return false; |
||
| 425 | |||
| 426 | } |
||
| 427 | } |
||
| 428 | |||
| 429 | |||
| 430 | |||
| 431 | public function change_password($email) { |
||
| 490 | } |
||
| 491 | |||
| 492 | } |
||
| 493 | |||
| 494 | public function send_password_reminder() { |
||
| 495 | global $PAGE; |
||
| 496 | |||
| 497 | // You'll probably have just called $this->change_password(). |
||
| 498 | |||
| 499 | if ($this->email() == '') { |
||
| 500 | $PAGE->error_message("No email set for this user, so can't send a password reminder."); |
||
| 501 | |||
| 502 | return false; |
||
| 503 | } |
||
| 504 | |||
| 505 | $data = array ( |
||
| 506 | 'to' => $this->email(), |
||
| 507 | 'template' => 'new_password' |
||
| 508 | ); |
||
| 509 | |||
| 510 | $URL = new \MySociety\TheyWorkForYou\Url("userlogin"); |
||
| 511 | |||
| 512 | $merge = array ( |
||
| 513 | 'EMAIL' => $this->email(), |
||
| 514 | 'LOGINURL' => "https://" . DOMAIN . $URL->generate(), |
||
|
1 ignored issue
–
show
|
|||
| 515 | 'PASSWORD' => $this->password() |
||
| 516 | ); |
||
| 517 | |||
| 518 | // send_template_email in utility.php. |
||
| 519 | $success = send_template_email($data, $merge); |
||
| 520 | |||
| 521 | return $success; |
||
| 522 | |||
| 523 | } |
||
| 524 | |||
| 525 | |||
| 526 | |||
| 527 | |||
| 528 | public function id_exists($user_id) { |
||
| 529 | // Returns true if there's a user with this user_id. |
||
| 530 | |||
| 531 | if (is_numeric($user_id)) { |
||
| 532 | $q = $this->db->query("SELECT user_id FROM users WHERE user_id = :user_id", |
||
| 533 | array(':user_id' => $user_id)); |
||
| 534 | if ($q->rows() > 0) { |
||
| 535 | return true; |
||
| 536 | } else { |
||
| 537 | return false; |
||
| 538 | } |
||
| 539 | } else { |
||
| 540 | return false; |
||
| 541 | } |
||
| 542 | |||
| 543 | } |
||
| 544 | |||
| 545 | |||
| 546 | 1 | public function email_exists($email, $return_id = false) { |
|
| 547 | // Returns true if there's a user with this email address. |
||
| 548 | |||
| 549 | 1 | if ($email != "") { |
|
| 550 | 1 | $q = $this->db->query("SELECT user_id FROM users WHERE email = :email", array(':email' => $email)); |
|
| 551 | 1 | if ($q->rows() > 0) { |
|
| 552 | if ($return_id) { |
||
| 553 | $row = $q->row(0); |
||
| 554 | |||
| 555 | return $row['user_id']; |
||
| 556 | } |
||
| 557 | |||
| 558 | return true; |
||
| 559 | } else { |
||
| 560 | 1 | return false; |
|
| 561 | } |
||
| 562 | } else { |
||
| 563 | return false; |
||
| 564 | } |
||
| 565 | |||
| 566 | } |
||
| 567 | |||
| 568 | public function facebook_id_exists($id, $return_id = false) { |
||
| 569 | // Returns true if there's a user with this facebook id. |
||
| 570 | |||
| 571 | if ($id!= "") { |
||
| 572 | $q = $this->db->query("SELECT user_id FROM users WHERE facebook_id = :id", array(':id' => $id)); |
||
| 573 | if ($q->rows() > 0) { |
||
| 574 | if ($return_id) { |
||
| 575 | $row = $q->row(0); |
||
| 576 | |||
| 577 | return $row['user_id']; |
||
| 578 | } |
||
| 579 | |||
| 580 | return true; |
||
| 581 | } else { |
||
| 582 | return false; |
||
| 583 | } |
||
| 584 | } else { |
||
| 585 | return false; |
||
| 586 | } |
||
| 587 | |||
| 588 | } |
||
| 589 | |||
| 590 | public function is_able_to($action) { |
||
| 591 | // Call this function to find out if a user is allowed to do something. |
||
| 592 | // It uses the user's status to return true or false. |
||
| 593 | // Possible actions: |
||
| 594 | // "addcomment" |
||
| 595 | // "reportcomment" |
||
| 596 | // "edituser" |
||
| 597 | global $PAGE; |
||
| 598 | |||
| 599 | $status = $this->status(); |
||
| 600 | |||
| 601 | switch ($action) { |
||
| 602 | |||
| 603 | // You can add more below as they're needed... |
||
| 604 | // But keep them in alphabetical order! |
||
| 605 | |||
| 606 | case "deletecomment": // Delete comments. |
||
| 607 | |||
| 608 | switch ($status) { |
||
| 609 | case "User": return false; |
||
| 610 | case "Moderator": return true; |
||
| 611 | case "Administrator": return true; |
||
| 612 | case "Superuser": return true; |
||
| 613 | default: /* Viewer */ return false; |
||
| 614 | } |
||
| 615 | |||
| 616 | case "edituser": |
||
| 617 | |||
| 618 | switch ($status) { |
||
| 619 | case "User": return false; |
||
| 620 | case "Moderator": return false; |
||
| 621 | case "Administrator": return false; |
||
| 622 | case "Superuser": return true; |
||
| 623 | default: /* Viewer */ return false; |
||
| 624 | } |
||
| 625 | |||
| 626 | case "reportcomment": // Report a comment for moderation. |
||
| 627 | |||
| 628 | switch ($status) { |
||
| 629 | case "User": return true; |
||
| 630 | case "Moderator": return true; |
||
| 631 | case "Administrator": return true; |
||
| 632 | case "Superuser": return true; |
||
| 633 | default: /* Viewer */ return true; |
||
| 634 | } |
||
| 635 | |||
| 636 | case "viewadminsection": // Access pages in the Admin section. |
||
| 637 | |||
| 638 | switch ($status) { |
||
| 639 | case "User": return false; |
||
| 640 | case "Moderator": return false; |
||
| 641 | case "Administrator": return true; |
||
| 642 | case "Superuser": return true; |
||
| 643 | default: /* Viewer */ return false; |
||
| 644 | } |
||
| 645 | |||
| 646 | case "voteonhansard": // Rate hansard things interesting/not. |
||
| 647 | /* Everyone */ return true; |
||
| 648 | |||
| 649 | default: |
||
| 650 | $PAGE->error_message ("You need to set permissions for '$action'!"); |
||
| 651 | |||
| 652 | return false; |
||
| 653 | |||
| 654 | |||
| 655 | } |
||
| 656 | |||
| 657 | |||
| 658 | |||
| 659 | } |
||
| 660 | |||
| 661 | // Same for every user... |
||
| 662 | // Just returns an array of the possible statuses a user could have. |
||
| 663 | // Handy for forms where you edit/view users etc. |
||
| 664 | public function possible_statuses() { |
||
| 665 | // Maybe there's a way of fetching these from the DB, |
||
| 666 | // so we don't duplicate them here...? |
||
| 667 | |||
| 668 | $statuses = array ("Viewer", "User", "Moderator", "Administrator", "Superuser"); |
||
| 669 | |||
| 670 | return $statuses; |
||
| 671 | |||
| 672 | } |
||
| 673 | |||
| 674 | |||
| 675 | |||
| 676 | // Functions for accessing the user's variables. |
||
| 677 | |||
| 678 | public function user_id() { return $this->user_id; } |
||
| 679 | public function firstname() { return $this->firstname; } |
||
| 680 | public function lastname() { return $this->lastname; } |
||
| 681 | public function password() { return $this->password; } |
||
| 682 | public function email() { return $this->email; } |
||
| 683 | public function emailpublic() { return $this->emailpublic; } |
||
| 684 | public function postcode() { return $this->postcode; } |
||
| 685 | public function url() { return $this->url; } |
||
| 686 | public function lastvisit() { return $this->lastvisit; } |
||
| 687 | public function facebook_id() { return $this->facebook_id; } |
||
| 688 | public function facebook_token() { return $this->facebook_token; } |
||
| 689 | public function facebook_user() { return $this->facebook_user; } |
||
| 690 | |||
| 691 | public function registrationtime() { return $this->registrationtime; } |
||
| 692 | public function registrationip() { return $this->registrationip; } |
||
| 693 | public function optin() { return $this->optin; } |
||
| 694 | // Don't use the status to check access privileges - use the is_able_to() function. |
||
| 695 | // But you might use status() to return text to display, describing a user. |
||
| 696 | // We can then change what status() does in the future if our permissions system |
||
| 697 | // changes. |
||
| 698 | public function status() { return $this->status; } |
||
| 699 | public function deleted() { return $this->deleted; } |
||
| 700 | public function confirmed() { return $this->confirmed; } |
||
| 701 | |||
| 702 | |||
| 703 | 19 | public function postcode_is_set() { |
|
| 704 | // So we can tell if the, er, postcode is set or not. |
||
| 710 | } |
||
| 711 | } |
||
| 712 | |||
| 713 | |||
| 714 | /////////// PRIVATE FUNCTIONS BELOW... //////////////// |
||
| 715 | |||
| 716 | 2 | public function _update($details) { |
|
| 717 | // Update a user's info. |
||
| 718 | // DO NOT call this function direct. |
||
| 719 | // Call either $this->update_other_user() or $this->update_self(). |
||
| 720 | |||
| 721 | // $details is an array like that in $this->add(). |
||
| 722 | 2 | global $PAGE; |
|
| 723 | |||
| 724 | // Update email alerts if email address changed |
||
| 725 | 2 | if (isset($details['email']) && $this->email != $details['email']) { |
|
| 726 | 1 | $this->db->query('UPDATE alerts SET email = :details_email WHERE email = :email', |
|
| 727 | array( |
||
| 728 | 1 | ':details_email' => $details['email'], |
|
| 729 | 1 | ':email' => $this->email |
|
| 730 | 1 | )); |
|
| 731 | 1 | } |
|
| 830 | } |
||
| 831 | |||
| 832 | |||
| 833 | } |
||
| 834 | |||
| 835 | |||
| 836 | |||
| 837 | |||
| 838 | |||
| 839 | } // End USER class |
||
| 840 | |||
| 1534 |
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.