From ee6fe2f9efe4cd999245224a61ec6eb40543c28e Mon Sep 17 00:00:00 2001 From: Nikolai R Kristiansen Date: Sun, 2 Aug 2015 23:05:49 +0200 Subject: [PATCH] Activation of card users should now work --- sms/register.php | 2 +- snapporder/activate.php | 5 -- snapporder/lib/functions.php | 110 +++++++++++++++++++++++++++-------- 3 files changed, 86 insertions(+), 31 deletions(-) diff --git a/sms/register.php b/sms/register.php index 5a503c2..c0382fa 100644 --- a/sms/register.php +++ b/sms/register.php @@ -100,7 +100,7 @@ if( isset($_POST['submit']) ) {
- +
diff --git a/snapporder/activate.php b/snapporder/activate.php index 009373f..406b2fc 100644 --- a/snapporder/activate.php +++ b/snapporder/activate.php @@ -75,11 +75,6 @@ if( isset($_POST['submit']) ) { // persist save_activation_form($data); - // Push the user to LDAP - $migrated = ldap_add_user($data['username'], $data['firstname'], $data['lastname'], $data['email'], $data['password'], array('dns-alle')); - _log($migrated); - set_migrated($data['userid']); // FIXME assumes this works every time - // send warm and fuzzy welcome email send_confirmation_email($data, $user); diff --git a/snapporder/lib/functions.php b/snapporder/lib/functions.php index 4dd71ad..b0f30e6 100644 --- a/snapporder/lib/functions.php +++ b/snapporder/lib/functions.php @@ -21,6 +21,15 @@ if( !function_exists('hash_ldap_password') ) { return '{SSHA}' . base64_encode(sha1( $raw_password.$salt, TRUE ). $salt); } } +function update_card_with_user($phone, $activation_code, $user_id) { + global $conn; + + $sql = "UPDATE din_card SET user_id=$user_id,owner_phone_number=NULL WHERE owner_phone_number=$phone AND card_number=$activation_code"; + $res = $conn->query($sql); + if( DB::isError($res) ) { + throw new InsideDatabaseException($res->getMessage().". DEBUG: ".$res->getDebugInfo()); + } +} function add_user($data, $source) { global $conn; @@ -28,6 +37,8 @@ function add_user($data, $source) { /* User table */ $phone = $data['phone']; // used later unset($data['phone']); + $activation_code = isset($data['activation_code']) ? $data['activation_code'] : null; + unset($data['activation_code']); unset($data['user_id']); /* Add our own initial values */ @@ -35,7 +46,6 @@ function add_user($data, $source) { $data['source'] = $source; $data['registration_status'] = "partial"; - /* Membership expiry */ /* One year from today (default) */ if( !isset($data['purchased']) ) { @@ -67,13 +77,20 @@ function add_user($data, $source) { $user_id = get_user_id_by_username($data['username']); /* Phonenumber table */ - $phone_number_validated = $source === 'snapporder' ? 1 : 0; + $validated_sources = array('snapporder', 'sms'); + $phone_number_validated = in_array($source, $validated_sources) ? 1 : 0; $cols = array('user_id', 'number', 'validated'); $values = array($user_id, $phone, $phone_number_validated); $sth = $conn->autoPrepare("din_userphonenumber", $cols, DB_AUTOQUERY_INSERT); $res = $conn->execute($sth, $values); + /* Card table */ + if($source == 'card') { + update_card_with_user($phone, $activation_code, $user_id); + log_userupdate($user_id, "Kortnummer $activation_code knyttet til bruker."); + } + if( DB::isError($res) ) { throw new InsideDatabaseException($res->getMessage().". DEBUG: ".$res->getDebugInfo()); } @@ -545,16 +562,18 @@ function validate_sms_form($data) { if( getUseridFromPhone($phone) !== false ) { throw new ValidationException("Telefonnummeret ".$data['phone']." er allerede registrert i bruk."); } + $data['phone'] = $phone; // lookup code and phonenumber tuple to validate code and get purchase date - $purchased = get_purchase_date($data['phone'], $data['activation_code']); + list($purchased, $source) = get_purchase_date_and_source($data['phone'], $data['activation_code']); if( $purchased === false ) { - throw new ValidationException("Ugyldig aktiveringskode ".$data['activation_code']); + throw new ValidationException("Ugyldig kortnummer/kode ".$data['activation_code']); } $data['purchased'] = clean_timestamp($purchased); + $data['source'] = $source; - /* validate firstname and lastname */ + /* firstname and lastname */ if( strlen($data['firstname']) < 2 ) { throw new ValidationException("Fornavn må være 2 tegn eller lengre: ".$data['firstname']); } @@ -569,14 +588,13 @@ function validate_sms_form($data) { throw new ValidationException("Epostadressen ".$data['email']." er allerede registrert i bruk"); } - // remove fields for later db insertion - unset($data['activation_code']); + // remove field for later db insertion unset($data['submit']); return $data; } function save_sms_form($data) { - $user_id = add_user($data, "sms"); + $user_id = add_user($data, $data['source']); log_userupdate($user_id, "Membership activated."); return $user_id; @@ -598,7 +616,38 @@ function save_activation_form($data) { return true; } -function get_purchase_date($phone, $activation_code) { +function get_purchase_date_legacy($phone, $activation_code) { + global $conn; + + $gsm = substr($phone, 3); // strip off country code + + $sql = "SELECT date FROM din_sms_sent WHERE activation_code=".$conn->quoteSmart($activation_code)." AND receiver=".$conn->quoteSmart($gsm); + $res = $conn->getOne($sql); + + if( DB::isError($res) ) { + throw new InsideDatabaseException($res->getMessage().". DEBUG: ".$res->getDebugInfo()); + } + + return $res !== null ? $res : false; +} + +function get_purchase_date_card($phone, $activation_code) { + global $conn; + + $phone = $conn->quoteSmart($phone); + $activation_code = $conn->quoteSmart($activation_code); + + $sql = "SELECT registered FROM din_card WHERE card_number=$activation_code AND owner_phone_number=$phone AND is_active=1"; + $res = $conn->getOne($sql); + + if( DB::isError($res) ) { + throw new InsideDatabaseException($res->getMessage().". DEBUG: ".$res->getDebugInfo()); + } + + return $res !== null ? $res : false; +} + +function get_purchase_date_tekstmelding($phone, $activation_code) { $url = "https://tekstmelding.neuf.no/inside-code-purchase-date"; $params = array( @@ -612,34 +661,44 @@ function get_purchase_date($phone, $activation_code) { curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); $result = curl_exec($ch); + $status_code = curl_getinfo($ch, CURLINFO_HTTP_CODE); curl_close($ch); - $timestamp = clean_timestamp($result); - - if ($timestamp === false) { - return get_purchase_date_legacy($phone, $activation_code); + if($status_code !== 200) { + return false; } - return $timestamp; + return $result; } -function get_purchase_date_legacy($phone, $activation_code) { - global $conn; - $gsm = substr($phone, 3); // strip off country code +function get_purchase_date_and_source($phone, $activation_code) { + /* Try card number lookup */ + $source = 'card'; + $timestamp = get_purchase_date_card($phone, $activation_code); - $sql = "SELECT date FROM din_sms_sent AS s WHERE s.activation_code=".$conn->quoteSmart($activation_code)." AND s.receiver=".$conn->quoteSmart($gsm); - $res = $conn->getOne($sql); - - if( DB::isError($res) ) { - throw new InsideDatabaseException($res->getMessage().". DEBUG: ".$res->getDebugInfo()); + /* Try SMS */ + if ($timestamp === false) { + $timestamp = get_purchase_date_tekstmelding($phone, $activation_code); + $source = 'sms'; } - return $res !== null ? $res : false; + /* Try legacy SMS-table */ + // FIXME: Remove if date later than 2016-02-01 + if ($timestamp === false) { + $timestamp = get_purchase_date_legacy($phone, $activation_code); + $source = 'sms'; + } + + if($timestamp === false) { + $source = NULL; + } + + return array($timestamp, $source); } /* The purpose of this email is: * - to store the registration_url in the users inbox - * - give some sort of positive confirmation outside of the SnappOrder-app + * - give some sort of positive confirmation :-) */ function send_activation_email($data, $user) { $from_email = "medlemskap@studentersamfundet.no"; @@ -681,11 +740,12 @@ function send_confirmation_email($data, $user) { $message = ''; $message .= '

Gratulerer med ditt nye medlemskap i Det Norske Studentersamfund!

'; - $message .= '

Medlemskapsbeviset ditt finner du i appen SnappOrder (i App Store eller Google Play) under valget "Chateau Neuf".

'; $message .= '

Brukernavnet ditt er: '.$data['username'].'

'; $message .= '

Om du i fremtiden skulle glemme passordet ditt, så kan du få nytt her.

'; $message .= '

Trykk på lenken under for å logge inn og se medlemskapet ditt.

'; $message .= '

Logg inn

'; + $message .= '

Medlemskortet ditt, om du ikke har fått det enda, får du i baren på Chateau Neuf.

'; + $message .= '

Medlemskapsbevis finner du også i appen SnappOrder (i App Store eller Google Play) under valget "Chateau Neuf".

'; $message .= "

Med vennlig hilsen
Det Norske Studentersamfund

"; $message .= "";