| Total Complexity | 98 |
| Total Lines | 680 |
| Duplicated Lines | 0 % |
| Coverage | 30.7% |
| Changes | 3 | ||
| Bugs | 0 | Features | 1 |
Complex classes like THEUSER 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 THEUSER, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 846 | class THEUSER extends USER { |
||
| 847 | |||
| 848 | // Handles all the login/out functionality and checking for the user |
||
| 849 | // who is using the site right NOW. Yes, him, over there. |
||
| 850 | |||
| 851 | // This will become true if all goes well... |
||
| 852 | public $loggedin = false; |
||
| 853 | public $facebook_user = false; |
||
| 854 | |||
| 855 | |||
| 856 | 3 | public function __construct() { |
|
| 857 | // This function is run automatically when a THEUSER |
||
| 858 | // object is instantiated. |
||
| 859 | |||
| 860 | 3 | $this->db = new ParlDB; |
|
| 861 | |||
| 862 | // We look at the user's cookie and see if it's valid. |
||
| 863 | // If so, we're going to log them in. |
||
| 864 | |||
| 865 | // A user's cookie is of the form: |
||
| 866 | // 123.blahblahblah |
||
| 867 | // Where '123' is a user id, and 'blahblahblah' is an md5 hash of the |
||
| 868 | // encrypted password we've stored in the db. |
||
| 869 | // (Maybe we could just put the encrypted pw in the cookie and md5ing |
||
| 870 | // it is overkill? Whatever, it works.) |
||
| 871 | |||
| 872 | 3 | $cookie = get_cookie_var("epuser_id"); // In includes/utility.php. |
|
| 873 | |||
| 874 | 3 | if ($cookie == '') { |
|
| 875 | $cookie = get_cookie_var("facebook_id"); |
||
| 876 | if ($cookie != '') { |
||
| 877 | $this->facebook_user = True; |
||
| 878 | twfy_debug("THEUSER", "is facebook login"); |
||
| 879 | } |
||
| 880 | } |
||
| 881 | |||
| 882 | 3 | if ($cookie == '') { |
|
| 883 | twfy_debug("THEUSER init FAILED", "No cookie set"); |
||
| 884 | $this->loggedin = false; |
||
| 885 | |||
| 886 | 3 | } elseif (preg_match("/([[:alnum:]]*)\.([[:alnum:]]*)/", $cookie, $matches)) { |
|
| 887 | |||
| 888 | 3 | if (is_numeric($matches[1])) { |
|
| 889 | |||
| 890 | 3 | $success = $this->init($matches[1]); |
|
| 891 | |||
| 892 | 3 | if ($success) { |
|
| 893 | // We got all the user's data from the DB. |
||
| 894 | |||
| 895 | // But we need to check the password before we log them in. |
||
| 896 | // And make sure the user hasn't been "deleted". |
||
| 897 | |||
| 898 | 3 | if ($this->facebook_user) { |
|
| 899 | if (md5($this->facebook_token()) == $matches[2] && $this->deleted() == false) { |
||
| 900 | twfy_debug ("THEUSER", "init SUCCESS: setting as logged in"); |
||
| 901 | $this->loggedin = true; |
||
| 902 | } elseif (md5 ($this->facebook_token()) != $matches[2]) { |
||
| 903 | twfy_debug ("THEUSER", "init FAILED: Facebook token doesn't match cookie"); |
||
| 904 | $this->loggedin = false; |
||
| 905 | } else { |
||
| 906 | twfy_debug ("THEUSER", "init FAILED: User is deleted"); |
||
| 907 | $this->loggedin = false; |
||
| 908 | } |
||
| 909 | } else { |
||
| 910 | 3 | if (md5($this->password()) == $matches[2] && $this->deleted() == false) { |
|
| 911 | // The correct password is in the cookie, |
||
| 912 | // and the user isn't deleted, so set the user to be logged in. |
||
| 913 | |||
| 914 | // This would be an appropriate place to call other functions |
||
| 915 | // that might set user info that only a logged-in user is going |
||
| 916 | // to need. Their preferences and saved things or something. |
||
| 917 | |||
| 918 | |||
| 919 | twfy_debug ("THEUSER init SUCCEEDED", "setting as logged in"); |
||
| 920 | $this->loggedin = true; |
||
| 921 | |||
| 922 | 3 | } elseif (md5 ($this->password()) != $matches[2]) { |
|
| 923 | 3 | twfy_debug ("THEUSER init FAILED", "Password doesn't match cookie"); |
|
| 924 | 3 | $this->loggedin = false; |
|
| 925 | 3 | } else { |
|
| 926 | twfy_debug ("THEUSER init FAILED", "User is deleted"); |
||
| 927 | $this->loggedin = false; |
||
| 928 | } |
||
| 929 | } |
||
| 930 | |||
| 931 | 3 | } else { |
|
| 932 | twfy_debug ("THEUSER init FAILED", "didn't get 1 row from db"); |
||
| 933 | $this->loggedin = false; |
||
| 934 | } |
||
| 935 | |||
| 936 | 3 | } else { |
|
| 937 | twfy_debug ("THEUSER init FAILED", "cookie's user_id is not numeric"); |
||
| 938 | $this->loggedin = false; |
||
| 939 | } |
||
| 940 | |||
| 941 | 3 | } else { |
|
| 942 | twfy_debug ("THEUSER init FAILED", "cookie is not of the correct form"); |
||
| 943 | $this->loggedin = false; |
||
| 944 | } |
||
| 945 | |||
| 946 | // If a user is logged in they *might* have set their own postcode. |
||
| 947 | // If they aren't logged in, or they haven't set one, then we may |
||
| 948 | // have set a postcode for them when they searched for their MP. |
||
| 949 | // If so, we'll use that as $this->postcode. |
||
| 950 | 3 | if ($this->postcode == '') { |
|
| 951 | 3 | if (get_cookie_var(POSTCODE_COOKIE) != '') { |
|
| 952 | $pc = get_cookie_var(POSTCODE_COOKIE); |
||
| 953 | |||
| 954 | $this->set_postcode_cookie($pc); |
||
| 955 | } |
||
| 956 | 3 | } |
|
| 957 | |||
| 958 | 3 | $this->update_lastvisit(); |
|
| 959 | |||
| 960 | 3 | } // End THEUSER() |
|
| 961 | |||
| 962 | 3 | public function update_lastvisit() { |
|
| 963 | |||
| 964 | 3 | if ($this->isloggedin()) { |
|
| 965 | // Set last_visit to now. |
||
| 966 | $date_now = gmdate("Y-m-d H:i:s"); |
||
| 967 | $q = $this->db->query("UPDATE users |
||
| 968 | SET lastvisit = '$date_now' |
||
| 969 | WHERE user_id = '" . $this->user_id() . "'"); |
||
| 970 | |||
| 971 | $this->lastvisit = $date_now; |
||
| 972 | } |
||
| 973 | 3 | } |
|
| 974 | |||
| 975 | // For completeness, but it's better to call $this->isloggedin() |
||
| 976 | // if you want to check the log in status. |
||
| 977 | public function loggedin() { return $this->loggedin; } |
||
| 978 | |||
| 979 | |||
| 980 | |||
| 981 | 7 | public function isloggedin() { |
|
| 982 | // Call this function to check if the user is successfully logged in. |
||
| 983 | |||
| 984 | 7 | if ($this->loggedin()) { |
|
| 985 | 2 | twfy_debug("THEUSER", "isloggedin: true"); |
|
| 986 | |||
| 987 | 2 | return true; |
|
| 988 | } else { |
||
| 989 | 7 | twfy_debug("THEUSER", "isloggedin: false"); |
|
| 990 | |||
| 991 | 7 | return false; |
|
| 992 | } |
||
| 993 | } |
||
| 994 | |||
| 995 | |||
| 996 | public function isvalid($email, $userenteredpassword) { |
||
| 997 | // Returns true if this email and plaintext password match a user in the db. |
||
| 998 | // If false returns an array of form error messages. |
||
| 999 | |||
| 1000 | // We use this on the log in page to check if the details the user entered |
||
| 1001 | // are correct. We can then continue with logging the user in (taking into |
||
| 1002 | // account their cookie remembering settings etc) with $this->login(). |
||
| 1003 | |||
| 1004 | // This error string is shared between both email and password errors to |
||
| 1005 | // prevent leaking of account existence. |
||
| 1006 | |||
| 1007 | $error_string = 'There is no user registered with an email of ' . _htmlentities($email) . ', or the given password is incorrect. If you are subscribed to email alerts, you are not necessarily registered on the website. If you register, you will be able to manage your email alerts, as well as leave annotations.'; |
||
| 1008 | |||
| 1009 | $q = $this->db->query("SELECT user_id, password, deleted, confirmed FROM users WHERE email = :email", array(':email' => $email)); |
||
| 1010 | |||
| 1011 | if ($q->rows() == 1) { |
||
| 1012 | // OK. |
||
| 1013 | $dbpassword = $q->field(0,"password"); |
||
| 1014 | if (password_verify($userenteredpassword, $dbpassword)) { |
||
| 1015 | $this->user_id = $q->field(0,"user_id"); |
||
| 1016 | $this->password = $dbpassword; |
||
| 1017 | // We'll need these when we're going to log in. |
||
| 1018 | $this->deleted = $q->field(0,"deleted") == 1 ? true : false; |
||
| 1019 | $this->confirmed = $q->field(0,"confirmed") == 1 ? true : false; |
||
| 1020 | |||
| 1021 | return true; |
||
| 1022 | |||
| 1023 | } else { |
||
| 1024 | // Failed. |
||
| 1025 | return array ("invalidemail" => $error_string); |
||
| 1026 | |||
| 1027 | } |
||
| 1028 | |||
| 1029 | } else { |
||
| 1030 | // Failed. |
||
| 1031 | return array ("invalidemail" => $error_string); |
||
| 1032 | } |
||
| 1033 | |||
| 1034 | } |
||
| 1035 | |||
| 1036 | 4 | public function has_postcode() { |
|
| 1037 | 4 | $has_postcode = false; |
|
| 1038 | 4 | if ( $this->isloggedin() && $this->postcode() != '' || $this->postcode_is_set() ) { |
|
| 1039 | $has_postcode = true; |
||
| 1040 | } |
||
| 1041 | 4 | return $has_postcode; |
|
| 1042 | } |
||
| 1043 | |||
| 1044 | |||
| 1045 | public function facebook_login($returl="", $expire, $accessToken) { |
||
| 1046 | global $PAGE; |
||
| 1047 | |||
| 1048 | twfy_debug("THEUSER", "Faceook login, user_id " . $this->user_id); |
||
| 1049 | twfy_debug("THEUSER", "Faceook login, facebook_id " . $this->facebook_id); |
||
| 1050 | twfy_debug("THEUSER", "Faceook login, email" . $this->email); |
||
| 1051 | if ($this->facebook_id() == "") { |
||
| 1052 | $PAGE->error_message ("We don't have a facebook id for this user.", true); |
||
| 1053 | |||
| 1054 | return; |
||
| 1055 | } |
||
| 1056 | |||
| 1057 | twfy_debug("THEUSER", "Faceook login, facebook_token: " . $accessToken); |
||
| 1058 | |||
| 1059 | $q = $this->db->query ("UPDATE users SET facebook_token = :token WHERE email = :email", |
||
| 1060 | array( |
||
| 1061 | ':token' => $accessToken, |
||
| 1062 | ':email' => $this->email |
||
| 1063 | )); |
||
| 1064 | |||
| 1065 | if (!$q->success()) { |
||
| 1066 | $PAGE->error_message ("There was a problem logging you in", true); |
||
| 1067 | twfy_debug("THEUSER", "Faceook login, failed to set accessToken"); |
||
| 1068 | |||
| 1069 | return false; |
||
| 1070 | } |
||
| 1071 | |||
| 1072 | // facebook login users probably don't have a password |
||
| 1073 | $cookie = $this->user_id() . "." . md5 ($accessToken); |
||
| 1074 | twfy_debug("THEUSER", "Faceook login, cookie: " . $cookie); |
||
| 1075 | |||
| 1076 | twfy_debug("USER", "logging in user from facebook " . $this->user_id); |
||
| 1077 | |||
| 1078 | $this->loggedin = True; |
||
| 1079 | $this->_login($returl, $expire, $cookie, 'facebook_id'); |
||
| 1080 | return true; |
||
| 1081 | } |
||
| 1082 | |||
| 1083 | public function login($returl="", $expire) { |
||
| 1084 | |||
| 1085 | // This is used to log the user in. Duh. |
||
| 1086 | // You should already have checked the user's email and password using |
||
| 1087 | // $this->isvalid() |
||
| 1088 | // That will have set $this->user_id and $this->password, allowing the |
||
| 1089 | // login to proceed... |
||
| 1090 | |||
| 1091 | // $expire is either 'session' or 'never' - for the cookie. |
||
| 1092 | |||
| 1093 | // $returl is the URL to redirect the user to after log in, generally the |
||
| 1094 | // page they were on before. But if it doesn't exist, they'll just go to |
||
| 1095 | // the front page. |
||
| 1096 | global $PAGE; |
||
| 1097 | |||
| 1098 | if ($returl == "") { |
||
| 1099 | $URL = new \MySociety\TheyWorkForYou\Url("home"); |
||
| 1100 | $returl = $URL->generate(); |
||
| 1101 | } |
||
| 1102 | |||
| 1103 | // Various checks about the user - if they fail, we exit. |
||
| 1104 | if ($this->user_id() == "" || $this->password == "") { |
||
| 1105 | $PAGE->error_message ("We don't have the user_id or password to make the cookie.", true); |
||
| 1106 | |||
| 1107 | return; |
||
| 1108 | } elseif ($this->deleted) { |
||
| 1109 | $PAGE->error_message ("This user has been deleted.", true); |
||
| 1110 | |||
| 1111 | return; |
||
| 1112 | } elseif (!$this->confirmed) { |
||
| 1113 | $PAGE->error_message ("You have not yet confirmed your account by clicking the link in the confirmation email we sent to you. If you don't have the email, you can <a href='/user/login/?resend=" . $this->user_id() . "'>have it resent</a>. If it still doesn't arrive, get in touch.", true); |
||
| 1114 | |||
| 1115 | return; |
||
| 1116 | } |
||
| 1117 | |||
| 1118 | // Reminder: $this->password is actually a hashed version of the plaintext pw. |
||
| 1119 | $cookie = $this->user_id() . "." . md5 ($this->password()); |
||
| 1120 | |||
| 1121 | $this->_login($returl, $expire, $cookie); |
||
| 1122 | } |
||
| 1123 | |||
| 1124 | private function _login($returl, $expire, $cookie, $cookie_name = 'epuser_id') { |
||
| 1125 | // Unset any existing postcode cookie. |
||
| 1126 | // This will be the postcode the user set for themselves as a non-logged-in |
||
| 1127 | // user. We don't want it hanging around as it causes confusion. |
||
| 1128 | $this->unset_postcode_cookie(); |
||
| 1129 | |||
| 1130 | twfy_debug("THEUSER", "expire is " . $expire); |
||
| 1131 | |||
| 1132 | $cookie_expires = 0; |
||
| 1133 | if ($expire == 'never') { |
||
| 1134 | twfy_debug("THEUSER", "cookie never expires"); |
||
| 1135 | $cookie_expires = time()+86400*365*20; |
||
| 1136 | } elseif (is_int($expire) && $expire > time()) { |
||
| 1137 | twfy_debug("THEUSER", "cookie expires at " . $expire); |
||
| 1138 | $cookie_expires = $expire; |
||
| 1139 | } else { |
||
| 1140 | twfy_debug("THEUSER", "cookie expires with session"); |
||
| 1141 | } |
||
| 1142 | |||
| 1143 | header("Location: $returl"); |
||
| 1144 | setcookie($cookie_name, $cookie, $cookie_expires, '/', COOKIEDOMAIN); |
||
|
1 ignored issue
–
show
|
|||
| 1145 | } |
||
| 1146 | |||
| 1147 | |||
| 1148 | public function logout($returl) { |
||
| 1149 | |||
| 1150 | // $returl is the URL to redirect the user to after log in, generally the |
||
| 1151 | // page they were on before. But if it doesn't exist, they'll just go to |
||
| 1152 | // the front page. |
||
| 1153 | |||
| 1154 | if ($returl == '') { |
||
| 1155 | $URL = new \MySociety\TheyWorkForYou\Url("home"); |
||
| 1156 | $returl = $URL->generate(); |
||
| 1157 | } |
||
| 1158 | |||
| 1159 | // get_cookie_var() is in includes/utility.php |
||
| 1160 | if (get_cookie_var("epuser_id") != "") { |
||
| 1161 | // They're logged in, so set the cookie to empty. |
||
| 1162 | header("Location: $returl"); |
||
| 1163 | setcookie('epuser_id', '', time() - 86400, '/', COOKIEDOMAIN); |
||
|
1 ignored issue
–
show
|
|||
| 1164 | } |
||
| 1165 | |||
| 1166 | if (get_cookie_var("facebook_id") != "") { |
||
| 1167 | // They're logged in, so set the cookie to empty. |
||
| 1168 | header("Location: $returl"); |
||
| 1169 | setcookie('facebook_id', '', time() - 86400, '/', COOKIEDOMAIN); |
||
| 1170 | } |
||
| 1171 | } |
||
| 1172 | |||
| 1173 | 2 | public function confirm_email($token, $redirect=true) { |
|
| 1174 | 2 | $arg = ''; |
|
| 1175 | 2 | if (strstr($token, '::')) $arg = '::'; |
|
| 1176 | 2 | if (strstr($token, '-')) $arg = '-'; |
|
| 1177 | 2 | list($user_id, $registrationtoken) = explode($arg, $token); |
|
| 1178 | |||
| 1179 | 2 | if (!is_numeric($user_id) || $registrationtoken == '') { |
|
| 1180 | return false; |
||
| 1181 | } |
||
| 1182 | 2 | $q = $this->db->query("SELECT expires, data |
|
| 1183 | FROM tokens |
||
| 1184 | WHERE token = :token |
||
| 1185 | AND type = 'E' |
||
| 1186 | 2 | ", array (':token' => $registrationtoken)); |
|
| 1187 | |||
| 1188 | 2 | if ($q->rows() == 1) { |
|
| 1189 | 2 | $expires = $q->field(0, 'expires'); |
|
| 1190 | 2 | $expire_time = strtotime($expires); |
|
| 1191 | 2 | if ( $expire_time < time() ) { |
|
| 1192 | 1 | global $PAGE; |
|
| 1193 | 1 | if ($PAGE && $redirect) { |
|
| 1194 | $PAGE->error_message ("Sorry, that token seems to have expired"); |
||
| 1195 | } |
||
| 1196 | |||
| 1197 | 1 | return false; |
|
| 1198 | } |
||
| 1199 | |||
| 1200 | 1 | list( $user_id, $email ) = explode('::', $q->field(0, 'data')); |
|
| 1201 | |||
| 1202 | // if we are logged in as someone else don't change the email |
||
| 1203 | 1 | if ( $this->user_id() != 0 && $this->user_id() != $user_id ) { |
|
| 1204 | return false; |
||
| 1205 | } |
||
| 1206 | |||
| 1207 | // if the user isn't logged in then try and load the |
||
| 1208 | // details |
||
| 1209 | 1 | if ($this->user_id() == 0 && !$this->init($user_id)) { |
|
| 1210 | return false; |
||
| 1211 | } |
||
| 1212 | |||
| 1213 | $details = array( |
||
| 1214 | 1 | 'email' => $email, |
|
| 1215 | 1 | 'firstname' => $this->firstname(), |
|
| 1216 | 1 | 'lastname' => $this->lastname(), |
|
| 1217 | 1 | 'postcode' => $this->postcode(), |
|
| 1218 | 1 | 'url' => $this->url(), |
|
| 1219 | 1 | 'optin' => $this->optin(), |
|
| 1220 | 1 | 'user_id' => $user_id, |
|
| 1221 | 1 | 'emailpublic' => $this->emailpublic() |
|
| 1222 | 1 | ); |
|
| 1223 | 1 | $ret = $this->_update($details); |
|
| 1224 | |||
| 1225 | 1 | if ($ret) { |
|
| 1226 | // and remove the token to be tidy |
||
| 1227 | 1 | $q = $this->db->query("DELETE |
|
| 1228 | FROM tokens |
||
| 1229 | WHERE token = :token |
||
| 1230 | AND type = 'E' |
||
| 1231 | 1 | ", array(':token' => $registrationtoken)); |
|
| 1232 | |||
| 1233 | 1 | $this->email = $email; |
|
| 1234 | 1 | $URL = new \MySociety\TheyWorkForYou\Url('userconfirmed'); |
|
| 1235 | 1 | $URL->insert(array('email'=>'t')); |
|
| 1236 | 1 | $redirecturl = $URL->generate(); |
|
| 1237 | 1 | if ($redirect) { |
|
| 1238 | $this->login($redirecturl, 'session'); |
||
| 1239 | } else { |
||
| 1240 | 1 | return true; |
|
| 1241 | } |
||
| 1242 | } else { |
||
| 1243 | return false; |
||
| 1244 | } |
||
| 1245 | } else { |
||
| 1246 | return false; |
||
| 1247 | } |
||
| 1248 | |||
| 1249 | } |
||
| 1250 | |||
| 1251 | public function confirm($token) { |
||
| 1252 | // The user has clicked the link in their confirmation email |
||
| 1253 | // and the confirm page has passed the token from the URL to here. |
||
| 1254 | // If all goes well they'll be confirmed and then logged in. |
||
| 1255 | |||
| 1256 | // Split the token into its parts. |
||
| 1257 | $arg = ''; |
||
| 1258 | if (strstr($token, '::')) $arg = '::'; |
||
| 1259 | if (strstr($token, '-')) $arg = '-'; |
||
| 1260 | list($user_id, $registrationtoken) = explode($arg, $token); |
||
| 1261 | |||
| 1262 | if (!is_numeric($user_id) || $registrationtoken == '') { |
||
| 1263 | return false; |
||
| 1264 | } |
||
| 1265 | |||
| 1266 | $q = $this->db->query("SELECT email, password, postcode |
||
| 1267 | FROM users |
||
| 1268 | WHERE user_id = :user_id |
||
| 1269 | AND registrationtoken = :token |
||
| 1270 | ", array( |
||
| 1271 | ':user_id' => $user_id, |
||
| 1272 | ':token' => $registrationtoken |
||
| 1273 | )); |
||
| 1274 | |||
| 1275 | if ($q->rows() == 1) { |
||
| 1276 | |||
| 1277 | // We'll need these to be set before logging the user in. |
||
| 1278 | $this->user_id = $user_id; |
||
| 1279 | $this->email = $q->field(0, 'email'); |
||
| 1280 | $this->password = $q->field(0, 'password'); |
||
| 1281 | |||
| 1282 | // Set that they're confirmed in the DB. |
||
| 1283 | $r = $this->db->query("UPDATE users |
||
| 1284 | SET confirmed = '1' |
||
| 1285 | WHERE user_id = :user_id |
||
| 1286 | ", array(':user_id' => $user_id)); |
||
| 1287 | |||
| 1288 | if ($q->field(0, 'postcode')) { |
||
| 1289 | try { |
||
| 1290 | $MEMBER = new MEMBER(array('postcode'=>$q->field(0, 'postcode'), 'house'=>HOUSE_TYPE_COMMONS)); |
||
| 1291 | $pid = $MEMBER->person_id(); |
||
| 1292 | # This should probably be in the ALERT class |
||
| 1293 | $this->db->query('update alerts set confirmed=1 where email = :email and criteria = :criteria', array( |
||
| 1294 | ':email' => $this->email, |
||
| 1295 | ':criteria' => 'speaker:' . $pid |
||
| 1296 | )); |
||
| 1297 | } catch (MySociety\TheyWorkForYou\MemberException $e) { |
||
| 1298 | } |
||
| 1299 | } |
||
| 1300 | |||
| 1301 | if ($r->success()) { |
||
| 1302 | |||
| 1303 | $this->confirmed = true; |
||
| 1304 | |||
| 1305 | // Log the user in, redirecting them to the confirm page |
||
| 1306 | // where they should get a nice welcome message. |
||
| 1307 | $URL = new \MySociety\TheyWorkForYou\Url('userconfirmed'); |
||
| 1308 | $URL->insert(array('welcome'=>'t')); |
||
| 1309 | $redirecturl = $URL->generate(); |
||
| 1310 | |||
| 1311 | $this->login($redirecturl, 'session'); |
||
| 1312 | |||
| 1313 | } else { |
||
| 1314 | // Couldn't set them as confirmed in the DB. |
||
| 1315 | return false; |
||
| 1316 | } |
||
| 1317 | |||
| 1318 | } else { |
||
| 1319 | // Couldn't find this user in the DB. Maybe the token was |
||
| 1320 | // wrong or incomplete? |
||
| 1321 | return false; |
||
| 1322 | } |
||
| 1323 | } |
||
| 1324 | |||
| 1325 | public function confirm_without_token() { |
||
| 1326 | // If we want to confirm login without a token, e.g. during |
||
| 1327 | // Facebook registration |
||
| 1328 | // |
||
| 1329 | // Note that this doesn't login or redirect the user. |
||
| 1330 | |||
| 1331 | twfy_debug("THEUSER", "Confirming user without token: " . $this->user_id()); |
||
| 1332 | $q = $this->db->query("SELECT email, password, postcode |
||
| 1333 | FROM users |
||
| 1334 | WHERE user_id = :user_id |
||
| 1335 | ", array( |
||
| 1336 | ':user_id' => $this->user_id, |
||
| 1337 | )); |
||
| 1338 | |||
| 1339 | if ($q->rows() == 1) { |
||
| 1340 | |||
| 1341 | twfy_debug("THEUSER", "User with ID found to confirm: " . $this->user_id()); |
||
| 1342 | // We'll need these to be set before logging the user in. |
||
| 1343 | $this->email = $q->field(0, 'email'); |
||
| 1344 | |||
| 1345 | // Set that they're confirmed in the DB. |
||
| 1346 | $r = $this->db->query("UPDATE users |
||
| 1347 | SET confirmed = '1' |
||
| 1348 | WHERE user_id = :user_id |
||
| 1349 | ", array(':user_id' => $this->user_id)); |
||
| 1350 | |||
| 1351 | if ($q->field(0, 'postcode')) { |
||
| 1352 | try { |
||
| 1353 | $MEMBER = new MEMBER(array('postcode'=>$q->field(0, 'postcode'), 'house'=>HOUSE_TYPE_COMMONS)); |
||
| 1354 | $pid = $MEMBER->person_id(); |
||
| 1355 | # This should probably be in the ALERT class |
||
| 1356 | $this->db->query('update alerts set confirmed=1 where email = :email and criteria = :criteria', array( |
||
| 1357 | ':email' => $this->email, |
||
| 1358 | ':criteria' => 'speaker:' . $pid |
||
| 1359 | )); |
||
| 1360 | } catch (MySociety\TheyWorkForYou\MemberException $e) { |
||
| 1361 | } |
||
| 1362 | } |
||
| 1363 | |||
| 1364 | if ($r->success()) { |
||
| 1365 | twfy_debug("THEUSER", "User with ID confirmed: " . $this->user_id()); |
||
| 1366 | $this->confirmed = true; |
||
| 1367 | return true; |
||
| 1368 | } else { |
||
| 1369 | twfy_debug("THEUSER", "User with ID not confirmed: " . $this->user_id()); |
||
| 1370 | // Couldn't set them as confirmed in the DB. |
||
| 1371 | return false; |
||
| 1372 | } |
||
| 1373 | |||
| 1374 | } else { |
||
| 1375 | // Couldn't find this user in the DB. Maybe the token was |
||
| 1376 | // wrong or incomplete? |
||
| 1377 | twfy_debug("THEUSER", "User with ID not found to confirm: " . $this->user_id()); |
||
| 1378 | return false; |
||
| 1379 | } |
||
| 1380 | } |
||
| 1381 | |||
| 1382 | |||
| 1383 | public function set_postcode_cookie($pc) { |
||
| 1393 | } |
||
| 1394 | |||
| 1395 | public function unset_postcode_cookie() { |
||
| 1396 | if (!headers_sent()) // if in debug mode |
||
| 1397 | setcookie (POSTCODE_COOKIE, '', time() - 3600, '/', COOKIEDOMAIN); |
||
|
1 ignored issue
–
show
|
|||
| 1398 | } |
||
| 1399 | |||
| 1400 | // mostly here for updating from facebook where we do not need |
||
| 1401 | // to confirm the email address |
||
| 1402 | public function update_self_no_confirm($details) { |
||
| 1442 | } |
||
| 1443 | |||
| 1444 | } |
||
| 1445 | |||
| 1446 | 2 | public function update_self($details, $confirm_email = true) { |
|
| 1526 | } |
||
| 1527 | |||
| 1528 | } |
||
| 1529 | |||
| 1530 | } |
||
| 1531 | |||
| 1532 | // Yes, we instantiate a new global $THEUSER object when every page loads. |
||
| 1533 | $THEUSER = new THEUSER; |
||
| 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.