Skip to content

Instantly share code, notes, and snippets.

@jalbertbowden
Last active June 5, 2019 15:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jalbertbowden/b12a9da0d99285841cb647ec24f7296b to your computer and use it in GitHub Desktop.
Save jalbertbowden/b12a9da0d99285841cb647ec24f7296b to your computer and use it in GitHub Desktop.
renew
<?php
require 'vendor/autoload.php';
include $_SERVER["DOCUMENT_ROOT"] . '/admin/AlmaAPIClient.php';
include 'DMVDatabaseClient.php';
include 'ErrorList.php';
use Rakit\Validation\Validator;
/**
* Provides basic CSRF protection.
*
* Make sure session_start() is the first thing that gets run on the page.
*
* @return Bool
*/
function validateCSRFToken()
{
session_start();
if (empty($_SESSION['token']) || empty($_SESSION['token-expire'])) {
$_SESSION['token'] = bin2hex(random_bytes(32));
$_SESSION['token-expire'] = time() + 3600; // 1 hour
}
if (time() >= $_SESSION['token-expire']) {
echo "Your session has expired, please refresh the page.";
$_SESSION['token-expire'] = null;
exit;
}
$token = $_SESSION['token'];
if (!empty($_POST['token'])) {
if (hash_equals($_SESSION['token'], $_POST['token'])) {
return true;
}
}
}
/**
* Validates the form fields.
* Sets $errorList to error array if there are errors.
* Returns true if there are no errors.
*
* @param ErrorList $errorList A object which stores an array of errors.
* @return Bool
*/
function validateForm($errorList)
{
$validator = new Validator;
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$validation = $validator->validate($_POST, [
'first_name' => 'required|max:255',
'middle_name' => 'max:255',
'last_name' => 'required|max:255',
'email' => 'required|email|max:255',
'email_confirmation' => 'required|same:email',
'password' => 'required|min:8|max:255',
'password_confirmation' => 'required|same:password',
'previous_identifier' => 'required|max:255',
], [
'required' => ':attribute is required.',
'email' => 'You must enter a valid email address.',
'same' => ':field and :attribute must match.',
'min' => ':attribute must be at least :min characters long.',
'max' => ':attribute must be less than 256 characters.',
]);
storeOldFieldValues($_POST);
if ($validation->fails()) {
$errorList->set($validation->errors()->all(':message'));
return false;
} else {
return true;
}
}
}
/**
* Constructs a JSON string with the values from the form.
*
* @return String
*/
function generateJSON(array $data)
{
$finalJSON = '{
"password": "' . $data['password'] . '",
"status": {
"value": "ACTIVE"
},
"record_type": {
"value": "PUBLIC"
},
"primary_id": "' . $data['email'] . '",
"first_name": "' . $data['first_name'] . '",
"middle_name": "' . $data['middle_name'] . '",
"last_name": "' . $data['last_name'] . '",
"user_group": {
"value": "01"
},
"campus_code": {
"value": "Main"
},
"preferred_language": {
"value": "en"
},
"birth_date": "1979-01-11Z",
"expiry_date": "2030-01-16Z",
"purge_date": "2021-01-20Z",
"account_type": {
"value": "INTERNAL"
},
"contact_info": {
"address": [
{
"preferred": true,
"line1": "' . $data['street_address'] . '",
"line2": "' . $data['street_address2'] . '",
"city": "' . $data['city'] . '",
"state_province": "' . $data['state'] . '",
"postal_code": "' . $data['zip'] . '",
"address_note": "",
"start_date": "2013-12-26Z",
"address_type": [
{
"value": "home"
}
]
}
],
"email": [
{
"description": null,
"preferred": true,
"email_address": "' . $data['email'] . '",
"email_type": [
{
"value": "personal"
}
]
}
]
},
"user_block": [
{
"block_type": {
"value": "GENERAL"
},
"block_description": {
"value": "02-GLOBAL"
},
"block_status": "ACTIVE"
}
]';
if (isset($data['mailingList'])) {
$finalJSON = $finalJSON . ',
"user_statistic": [
{
"statistic_category": {
"value": "NLS",
"desc": "Newsletter Subscriber"
}
}
]';
}
$finalJSON = $finalJSON . '}';
return $finalJSON;
}
/**
* Creates an AlmaAPIClient and attempts to create a user from the form data.
* Sets $errorList to error array if there are errors.
*
* @param Array $data An array of $_POST form values that will be sent to the Alma API.
* @param ErrorList $errorList A object which stores an array of errors.
*/
function callAlmaAPI($data, $errorList)
{
$config = parse_ini_file($_SERVER["DOCUMENT_ROOT"] . "/admin/alma_config.ini");
$alma_api = new AlmaAPIClient($config["api_key"]);
$api_result = $alma_api->get_user($data['previous_identifier']);
if ($api_result != null ) {
$api_result = addEmail($api_result, $data['email']);
$api_result = addPassword($api_result, $data['password']);
$api_result = updateExpiryDate($api_result, 1);
$api_result = $alma_api->update_user(json_encode($api_result), $data['previous_identifier']);
}
else {
$api_result = array("You're not currently a library patron! Please <a href='http://www.lva.virginia.gov/register/'>create a new library account!</a>");
}
if(correctFirstLast($api_result, $data['first_name'], $data['last_name'])){
if(check_expired($api_result)){
$api_result = addEmail($api_result, $data['email']);
$api_result = addPassword($api_result, $data['password']);
$api_result = updateExpiryDate($api_result, 1);
$api_result = $alma_api->update_user(json_encode($api_result), $data['previous_identifier']);
}
else
{
$api_result = array('Your account is not expired yet!');
}
}
else
{
$api_result = array('The first or last name entered does not match our records. ');
}
// echo '<pre>';
// print_r(json_encode($api_result));
// echo '</pre>';
if ($api_result == AlmaAPIClient::SUCCESS) {
return true;
} else {
$errorList->set($api_result);
return false;
}
}
function correctFirstLast($api_result, $first_name, $last_name)
{
$first_name_api = explode(' ', $api_result['first_name'])[0];
return ($first_name_api == strtoupper($first_name)) && ($api_result['last_name'] == strtoupper($last_name));
}
function addEmail($api_result, $email)
{
array_push($api_result['contact_info']['email'], array(
"email_address" => $email,
"description" => "",
"preferred" => "true",
"segment_type" => "Internal",
"email_type" => array(array(
"value" => "personal",
"desc" => "Personal"
))
));
$newIdentifier = array(
'value' => $email,
'id_type' => array(
'value' => '02',
'desc' => 'Additional'
),
'note' => null,
'status' => 'ACTIVE',
'segment_type' => 'Internal',
);
array_push($api_result['user_identifier'], $newIdentifier);
return $api_result;
}
function addPassword($api_result, $password)
{
$api_result['password'] = $password;
return $api_result;
};
function check_expired($patronJSON) {
return strtotime($patronJSON['expiry_date']) < time();
}
function updateExpiryDate($api_result, $years){
$new_expiry_year = ((int)substr($api_result['expiry_date'], 0, 4)) + $years;
$expiry_date_length = strlen($api_result['expiry_date']);
$old_expiry_vars = substr($api_result['expiry_date'], 4, $expiry_date_length);
$api_result['expiry_date'] = date('Y-m-d', strtotime('+1 year')) . 'Z';
// $api_result['expiry_date'] = $new_expiry_year . $old_expiry_vars;
return $api_result;
}
/**
* Verifies the client's reCaptcha request.
*
* @param Bool
*/
function verifyRecaptcha()
{
$postdata = http_build_query(["secret" => "6LfHhg4UAAAAAIZWwuoDu2Es4WLakCp_M3EwQ3t6", "response" => $_POST['g-recaptcha-response'], "remoteip" => $_SERVER['REMOTE_ADDR']]);
$options = [
'http' =>
[
'method' => 'POST',
'header' => 'Content-type: application/x-www-form-urlencoded',
'content' => $postdata
]
];
$context = stream_context_create($options);
$googresp = file_get_contents('https://www.google.com/recaptcha/api/siteverify', false, $context);
$decgoogresp = json_decode($googresp);
return $decgoogresp->success;
}
/**
* Stores an array of values into the $_SESSION
*
* @param Array $values
*/
function storeOldFieldValues($values)
{
unset($_SESSION['old']);
foreach ($values as $field => $value) {
$_SESSION['old'][$field] = $value;
}
// We don't need to store the CSRF token or the reCaptcha token in the session
unset($_SESSION['old']['token']);
unset($_SESSION['old']['g-recaptcha-response']);
}
/**
* Get the previously submitted value to repopulate the field.
*
* @param String
*/
function old($field)
{
if (isset($_SESSION['old']) && isset($_SESSION['old'][$field]) && $_SERVER['REQUEST_METHOD'] === 'POST') {
return htmlentities($_SESSION['old'][$field]);
} else {
if (isset($_SESSION['old']) && isset($_SESSION['old'][$field]))
unset($_SESSION['old'][$field]); // If it's a GET request, get rid of the session variables
return '';
}
}
$errorList = new ErrorList;
if(validateCSRFToken() && verifyRecaptcha()) {
//if(validateCSRFToken()) {
if (validateForm($errorList)) {
if (callAlmaAPI($_POST, $errorList)) {
$dmv_db = new DMVDatabaseClient();
if($dmv_db->db_create_patron($_POST)) {
header('Location: https://' . $_SERVER['HTTP_HOST'] . '/register/renew_success.php');
}
}
}
}
?>
<html lang="en">
<head>
<meta name="robots" content="noindex">
<meta name="googlebot" content="noindex">
<title>Library of Virginia Account Renewal</title>
<link rel="stylesheet" type="text/css" href="style.css">
<script src="https://www.google.com/recaptcha/api.js" async defer></script>
</head>
<body>
<div class="content">
<section class="section section--quarter">
<div class="section__background section__background--building"></div>
<div class="logo-wrapper">
<a href="/"><img class="logo" src="/img/logo.svg"></a>
</div>
<div class="infoBox">
<p class="infoBox__text">
Virginia Residents may renew their Library of Virginia accounts online. In order to do so, we require a valid Virginia Department of Motor Vehicles (DMV) issued driver's license or identification card. You must also provide a valid email address and password.
</p>
<p class="infoBox__text">
Non–Virginia residents who have recently received a card at the Library may send an email request with their full name and library card barcode number to <a href="mailto:refdesk@lva.virginia.gov" style="color:yellow">refdesk@lva.virginia.gov</a>. Within two to three business days, we will extend your card to expire on June 4, 2020. Otherwise, you can renew in–person at your next visit. We do not offer online renewal for non-Virginia residents.
</p>
</div>
</section>
<section class="section section--threequarter">
<div class="header">
<h2 class="header__title">Previous Patron Renewal</h2>
</div>
<?php
if (count($errorList->get())) {
?>
<div class="errorList-wrapper" role="alert">
<ul class="errorList">
<?php
foreach ($errorList->get() as $error) {
?>
<li class="error"><?= ucfirst($error) ?></li>
<?php
}
?>
</ul>
</div>
<?php
}
?>
<form method="POST" action="">
<input type="hidden" name="token" value="<?= $_SESSION['token'] ?>" />
<section class="form__section form__section--orange">
<div class="form__fieldset">
<legend class="fieldset__legend">
<h3 class="legend__heading">
Patron Information
</h3>
</legend>
<section class="fieldset__content">
<div class="form__group form__group--singleLine">
<div class="form__group form__group--third">
<label class="form__label" for="firstName_field">
First Name
</label>
<input id="firstName_field" class='form__input' name="first_name" type='text' value="<?= old('first_name') ?>" required />
</div>
<div class="form__group form__group--third">
<label class="form__label" for="middleName_field">
Middle Name <span class='form__optional'>(Optional)</span>
</label>
<input id="middleName_field" class='form__input' name="middle_name" type='text' value="<?= old('middle_name') ?>" />
</div>
<div class="form__group form__group--third">
<label class="form__label" for="lastName_field">
Last Name
</label>
<input id="lastName_field" class="form__input" name="last_name" type="text" value="<?= old('last_name') ?>" required />
</div>
</div>
<div class="form__group form__group--singleLine">
<div class="form__group">
<label class="form__label" for="previous_identifier_field">
DMV Customer ID or Library Card Number
</label>
<input id="previous_identifier_field" class='form__input' name="previous_identifier" type="text" value="<?= old('previous_identifier') ?>" required />
</div>
</div>
</section>
</div>
</section>
<section class="form__section form__section--red">
<div class="form__fieldset">
<legend class="fieldset__legend">
<h3 class="legend__heading">
Add an Email and Password
</h3>
<p class="legend__text">
This email and password will be the new way to sign in to the online catalog and subscription databases.
</p>
</legend>
<section class="fieldset__content">
<div class="form__group">
<div class="form__group form__group--threequarter">
<label class="form__label" for="email_field">
Email
</label>
<input id="email_field" class='form__input' name="email" type="text" value="<?= old('email') ?>" required />
</div>
<div class="form__group form__group--threequarter">
<label class="form__label" for="emailConfirmation_field">
Email Confirmation
</label>
<input id="emailConfirmation_field" class='form__input' name="email_confirmation" type="text" value="<?= old('email_confirmation') ?>" required />
</div>
</div>
<div class="form__group form__group--singleLine">
<div class="form__group form__group--half">
<label class="form__label" for="password_field">
Password (minimum of 8 characters)
</label>
<input id="password_field" class='form__input' name="password" type="password" required />
</div>
<div class="form__group form__group--half">
<label class="form__label" for="passwordConfirmation_field">
Password Confirmation
</label>
<input id="passwordConfirmation_field" class='form__input' name="password_confirmation" type="password" required />
</div>
</div>
</section>
</div>
</section>
<section class="form__section form__section--blue">
<div class="form__fieldset">
<legend class="fieldset__legend">
<h3 class="legend__heading">
One last thing...
</h3>
</legend>
<section class="fieldset__content">
<div class="checkbox__item-wrapper">
<div class="g-recaptcha" data-sitekey="6LfHhg4UAAAAAF1erABlIyHvL0LChHg3pTg_jlGk" data-theme="dark"></div>
</div>
<div class="button-wrapper">
<button class="button" type="submit">Renew Your Account</button>
</div>
</section>
</div>
</section>
</form>
</section>
</div>
</body>
</html>
<?php
require 'vendor/autoload.php';
include $_SERVER["DOCUMENT_ROOT"] . '/admin/AlmaAPIClient.php';
include 'DMVDatabaseClient.php';
include 'ErrorList.php';
use Rakit\Validation\Validator;
/**
* Provides basic CSRF protection.
*
* Make sure session_start() is the first thing that gets run on the page.
*
* @return Bool
*/
function validateCSRFToken()
{
session_start();
if (empty($_SESSION['token']) || empty($_SESSION['token-expire'])) {
$_SESSION['token'] = bin2hex(random_bytes(32));
$_SESSION['token-expire'] = time() + 3600; // 1 hour
}
if (time() >= $_SESSION['token-expire']) {
echo "Your session has expired, please refresh the page.";
$_SESSION['token-expire'] = null;
exit;
}
$token = $_SESSION['token'];
if (!empty($_POST['token'])) {
if (hash_equals($_SESSION['token'], $_POST['token'])) {
return true;
}
}
}
/**
* Validates the form fields.
* Sets $errorList to error array if there are errors.
* Returns true if there are no errors.
*
* @param ErrorList $errorList A object which stores an array of errors.
* @return Bool
*/
function validateForm($errorList)
{
$validator = new Validator;
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$validation = $validator->validate($_POST, [
'first_name' => 'required|max:255',
'middle_name' => 'max:255',
'last_name' => 'required|max:255',
'email' => 'required|email|max:255',
'email_confirmation' => 'required|same:email',
'password' => 'required|min:8|max:255',
'password_confirmation' => 'required|same:password',
'previous_identifier' => 'required|max:255',
], [
'required' => ':attribute is required.',
'email' => 'You must enter a valid email address.',
'same' => ':field and :attribute must match.',
'min' => ':attribute must be at least :min characters long.',
'max' => ':attribute must be less than 256 characters.',
]);
storeOldFieldValues($_POST);
if ($validation->fails()) {
$errorList->set($validation->errors()->all(':message'));
return false;
} else {
return true;
}
}
}
/**
* Constructs a JSON string with the values from the form.
*
* @return String
*/
function generateJSON(array $data)
{
$finalJSON = '{
"password": "' . $data['password'] . '",
"status": {
"value": "ACTIVE"
},
"record_type": {
"value": "PUBLIC"
},
"primary_id": "' . $data['email'] . '",
"first_name": "' . $data['first_name'] . '",
"middle_name": "' . $data['middle_name'] . '",
"last_name": "' . $data['last_name'] . '",
"user_group": {
"value": "01"
},
"campus_code": {
"value": "Main"
},
"preferred_language": {
"value": "en"
},
"birth_date": "1979-01-11Z",
"expiry_date": "2030-01-16Z",
"purge_date": "2021-01-20Z",
"account_type": {
"value": "INTERNAL"
},
"contact_info": {
"address": [
{
"preferred": true,
"line1": "' . $data['street_address'] . '",
"line2": "' . $data['street_address2'] . '",
"city": "' . $data['city'] . '",
"state_province": "' . $data['state'] . '",
"postal_code": "' . $data['zip'] . '",
"address_note": "",
"start_date": "2013-12-26Z",
"address_type": [
{
"value": "home"
}
]
}
],
"email": [
{
"description": null,
"preferred": true,
"email_address": "' . $data['email'] . '",
"email_type": [
{
"value": "personal"
}
]
}
]
},
"user_block": [
{
"block_type": {
"value": "GENERAL"
},
"block_description": {
"value": "02-GLOBAL"
},
"block_status": "ACTIVE"
}
]';
if (isset($data['mailingList'])) {
$finalJSON = $finalJSON . ',
"user_statistic": [
{
"statistic_category": {
"value": "NLS",
"desc": "Newsletter Subscriber"
}
}
]';
}
$finalJSON = $finalJSON . '}';
return $finalJSON;
}
/**
* Creates an AlmaAPIClient and attempts to create a user from the form data.
* Sets $errorList to error array if there are errors.
*
* @param Array $data An array of $_POST form values that will be sent to the Alma API.
* @param ErrorList $errorList A object which stores an array of errors.
*/
function callAlmaAPI($data, $errorList)
{
$config = parse_ini_file($_SERVER["DOCUMENT_ROOT"] . "/admin/alma_config.ini");
$alma_api = new AlmaAPIClient($config["api_key"]);
$api_result = $alma_api->get_user($data['previous_identifier']);
$api_result = addEmail($api_result, $data['email']);
$api_result = addPassword($api_result, $data['password']);
$api_result = updateExpiryDate($api_result, 1);
$api_result = $alma_api->update_user(json_encode($api_result), $data['previous_identifier']);
// echo '<pre>';
// print_r(json_encode($api_result));
// echo '</pre>';
if ($api_result == AlmaAPIClient::SUCCESS) {
return true;
} else {
$errorList->set($api_result);
return false;
}
}
function addEmail($api_result, $email)
{
array_push($api_result['contact_info']['email'], array(
"email_address" => $email,
"description" => "",
"preferred" => "true",
"segment_type" => "Internal",
"email_type" => array(array(
"value" => "personal",
"desc" => "Personal"
))
));
$newIdentifier = array(
'value' => $email,
'id_type' => array(
'value' => '02',
'desc' => 'Additional'
),
'note' => null,
'status' => 'ACTIVE',
'segment_type' => 'Internal',
);
array_push($api_result['user_identifier'], $newIdentifier);
return $api_result;
}
function addPassword($api_result, $password)
{
$api_result['password'] = $password;
return $api_result;
};
function updateExpiryDate($api_result, $years){
$new_expiry_year = ((int)substr($api_result['expiry_date'], 0, 4)) + $years;
$expiry_date_length = strlen($api_result['expiry_date']);
$old_expiry_vars = substr($api_result['expiry_date'], 4, $expiry_date_length);
$api_result['expiry_date'] = $new_expiry_year . $old_expiry_vars;
return $api_result;
}
/**
* Verifies the client's reCaptcha request.
*
* @param Bool
*/
function verifyRecaptcha()
{
$postdata = http_build_query(["secret" => "6LfHhg4UAAAAAIZWwuoDu2Es4WLakCp_M3EwQ3t6", "response" => $_POST['g-recaptcha-response'], "remoteip" => $_SERVER['REMOTE_ADDR']]);
$options = [
'http' =>
[
'method' => 'POST',
'header' => 'Content-type: application/x-www-form-urlencoded',
'content' => $postdata
]
];
$context = stream_context_create($options);
$googresp = file_get_contents('https://www.google.com/recaptcha/api/siteverify', false, $context);
$decgoogresp = json_decode($googresp);
return $decgoogresp->success;
}
/**
* Stores an array of values into the $_SESSION
*
* @param Array $values
*/
function storeOldFieldValues($values)
{
if (isset($_SESSION['old'])) {
unset($_SESSION['old']);
}
foreach ($values as $field => $value) {
$_SESSION['old'][$field] = $value;
}
// We don't need to store the CSRF token or the reCaptcha token in the session
unset($_SESSION['old']['token']);
unset($_SESSION['old']['g-recaptcha-response']);
}
/**
* Get the previously submitted value to repopulate the field.
*
* @param String
*/
function old($field)
{
if (isset($_SESSION['old']) && isset($_SESSION['old'][$field]) && $_SERVER['REQUEST_METHOD'] === 'POST') {
return htmlentities($_SESSION['old'][$field]);
} else {
if (isset($_SESSION['old']) && isset($_SESSION['old'][$field]))
unset($_SESSION['old'][$field]); // If it's a GET request, get rid of the session variables
return '';
}
}
$errorList = new ErrorList;
// if(validateCSRFToken() && verifyRecaptcha()) {
if (validateForm($errorList)) {
if (callAlmaAPI($_POST, $errorList)) {
// $dmv_db = new DMVDatabaseClient();
// if($dmv_db->db_create_patron($_POST)) {
// header('Location: https://' . $_SERVER['HTTP_HOST'] . '/register/success.php');
// }
}
}
// }
?>
<html lang="en">
<head>
<meta name="robots" content="noindex">
<meta name="googlebot" content="noindex">
<title>Library of Virginia Account Renewal</title>
<link rel="stylesheet" type="text/css" href="style.css">
<script src="https://www.google.com/recaptcha/api.js" async defer></script>
</head>
<body>
<div class="content">
<section class="section section--quarter">
<div class="section__background section__background--building"></div>
<div class="logo-wrapper">
<a href="/"><img class="logo" src="/img/logo.svg"></a>
</div>
<div class="infoBox">
<p class="infoBox__text">
Virginia Residents may renew their Library of Virginia accounts online. In order to do so, we require a valid Virginia Department of Motor Vehicles (DMV) issued driver's license or identification card. You must also provide a valid email address and password.
</p>
<p class="infoBox__text">
Non–Virginia residents who have recently received a card at the Library may send an email request with their full name and library card barcode number to <a href="mailto:refdesk@lva.virginia.gov" style="color:yellow">refdesk@lva.virginia.gov</a>. Within two to three business days, we will extend your card to expire on June 4, 2020. Otherwise, you can renew in–person at your next visit. We do not offer online renewal for non-Virginia residents.
</p>
</div>
</section>
<section class="section section--threequarter">
<div class="header">
<h2 class="header__title">Virginia Resident Account Renewal</h2>
</div>
<?php
if (count($errorList->get())) {
?>
<div class="errorList-wrapper" role="alert">
<ul class="errorList">
<?php
foreach ($errorList->get() as $error) {
?>
<li class="error"><?= ucfirst($error) ?></li>
<?php
}
?>
</ul>
</div>
<?php
}
?>
<form method="POST" action="">
<input type="hidden" name="token" value="<?= $_SESSION['token'] ?>" />
<section class="form__section form__section--orange">
<div class="form__fieldset">
<legend class="fieldset__legend">
<h3 class="legend__heading">
Patron Information
</h3>
</legend>
<section class="fieldset__content">
<div class="form__group form__group--singleLine">
<div class="form__group form__group--third">
<label class="form__label" for="firstName_field">
First Name
</label>
<input id="firstName_field" class='form__input' name="first_name" type='text' value="<?= old('first_name') ?>" required />
</div>
<div class="form__group form__group--third">
<label class="form__label" for="middleName_field">
Middle Name <span class='form__optional'>(Optional)</span>
</label>
<input id="middleName_field" class='form__input' name="middle_name" type='text' value="<?= old('middle_name') ?>" />
</div>
<div class="form__group form__group--third">
<label class="form__label" for="lastName_field">
Last Name
</label>
<input id="lastName_field" class="form__input" name="last_name" type="text" value="<?= old('last_name') ?>" required />
</div>
</div>
<div class="form__group form__group--singleLine">
<div class="form__group">
<label class="form__label" for="previous_identifier_field">
DMV Customer ID or Library Card Number
</label>
<input id="previous_identifier_field" class='form__input' name="previous_identifier" type="text" value="<?= old('previous_identifier') ?>" required />
</div>
</div>
</section>
</div>
</section>
<section class="form__section form__section--red">
<div class="form__fieldset">
<legend class="fieldset__legend">
<h3 class="legend__heading">
Add an Email and Password
</h3>
<p class="legend__text">
This email and password will be the new way to sign in to the online catalog and subscription databases.
</p>
</legend>
<section class="fieldset__content">
<div class="form__group">
<div class="form__group form__group--threequarter">
<label class="form__label" for="email_field">
Email
</label>
<input id="email_field" class='form__input' name="email" type="text" value="<?= old('email') ?>" required />
</div>
<div class="form__group form__group--threequarter">
<label class="form__label" for="emailConfirmation_field">
Email Confirmation
</label>
<input id="emailConfirmation_field" class='form__input' name="email_confirmation" type="text" value="<?= old('email_confirmation') ?>" required />
</div>
</div>
<div class="form__group form__group--singleLine">
<div class="form__group form__group--half">
<label class="form__label" for="password_field">
Password (minimum of 8 characters)
</label>
<input id="password_field" class='form__input' name="password" type="password" required />
</div>
<div class="form__group form__group--half">
<label class="form__label" for="passwordConfirmation_field">
Password Confirmation
</label>
<input id="passwordConfirmation_field" class='form__input' name="password_confirmation" type="password" required />
</div>
</div>
</section>
</div>
</section>
<section class="form__section form__section--blue">
<div class="form__fieldset">
<legend class="fieldset__legend">
<h3 class="legend__heading">
One last thing...
</h3>
</legend>
<section class="fieldset__content">
<div class="checkbox__item-wrapper">
<div class="g-recaptcha" data-sitekey="6LfHhg4UAAAAAF1erABlIyHvL0LChHg3pTg_jlGk" data-theme="dark"></div>
</div>
<div class="button-wrapper">
<button class="button" type="submit">Renew Your Account</button>
</div>
</section>
</div>
</section>
</form>
</section>
</div>
</body>
</html>
@AustinCarr
Copy link

So on line 191, you should create an if statement that checks if $api_result == null.

if ($api_result != null ) {
  $api_result = addEmail($api_result, $data['email']);
  $api_result = addPassword($api_result, $data['password']);
  $api_result = updateExpiryDate($api_result, 1);
  $api_result = $alma_api->update_user(json_encode($api_result), $data['previous_identifier']);
}
else {
  $api_result = array("You're not currently a library patron! Please <a href='http://www.lva.virginia.gov/register/'>create a new library account!</a>");
}

We'll assume that if the request is returning null, then the user doesn't exist.

@AustinCarr
Copy link

I also don't know if the error codes will let you put in a link, but they should?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment