| only for RuBoard - do not distribute or recompile |
When a user first starts using the Warm Mail system, he will need to set up some email accounts. If the user clicks on the Account Setup button, this will set the $action variable to account-setup and recall the index.php script. The user will then see the output shown in Figure 27.4.
Look back at the script in Listing 27.2. This time around because of the value of $action, we get different behavior.
We get a slightly different header, as follows:
do_html_header($auth_user, "Warm Mail - ".
format_action($action), $selected_account);
More importantly, we get a different body, as follows:
case 'store-settings' :
case 'account-setup' :
case 'delete-account' :
{
display_account_setup($auth_user);
break;
}
This is the typical pattern: Each command calls a function. In this case, we call the display_account_setup() function. The code for this function is shown in Listing 27.3.
function display_account_setup($auth_user)
{
//display empty 'new account'form
display_account_form($auth_user);
$list = get_accounts($auth_user);
// display each stored account
foreach($list as $key => $account)
{
// display form for each accounts details.
// note that we are going to send the password for all accounts in the HTML
// this is not really a very good idea
display_account_form($auth_user, $account['accountid'], $account['server'],
$account['remoteuser'],
$account['remotepassword'], $account['type'],
$account['port']);
}
}
When we call this function, it displays a blank form to add a new account, followed by editable forms containing each of the user's current email accounts. The display_account_form() function will display the form that we can see in Figure 27.4. You can see that we use it in two different ways here: We use it with no parameters to display an empty form, and we use it with a full set of parameters to display an existing record. This function is in the output_fns.php library; it simply outputs HTML so we will not go through it here.
The function that retrieves any existing accounts is get_accounts(), from the mail_fns.php library. This function is shown in Listing 27.4.
function get_accounts($auth_user)
{
$list = array();
if(db_connect())
{
$query = "select * from accounts where username = '$auth_user'";
$result = mysql_query($query);
if($result)
{
while($settings = mysql_fetch_array($result))
array_push( $list, $settings);
}
else
return false;
}
return $list;
}
As you can see, this function connects to the database, retrieves all the accounts for a particular user, and returns them as an array.
If a user fills out the account form and clicks the Save Changes button, the store-settings action will be activated. Let's look at the event handling code for this from index.php. In the preprocessing stage, we execute the following code:
case 'store-settings' :
{
store_account_settings($auth_user, $HTTP_POST_VARS);
break;
}
The store_account_settings() function writes the new account details into the database. The code for this function is shown in Listing 27.5.
function store_account_settings($auth_user, $settings)
{
if(!filled_out($settings))
{
echo "All fields must be filled in. Try again.<br><br>";
return false;
}
else
{
if($settings['account']>0)
$query = "update accounts set server = '$settings[server]',
port = $settings[port], type = '$settings[type]',
remoteuser = '$settings[remoteuser]',
remotepassword = '$settings[remotepassword]'
where accountid = $settings[account]
and username = '$auth_user'";
else
$query = "insert into accounts values ('$auth_user',
'$settings[server]', $settings[port],
'$settings[type]', '$settings[remoteuser]',
'$settings[remotepassword]', NULL)";
if(db_connect() && mysql_query($query))
{
return true;
}
else
{
echo "could not store changes.<br><br><br><br><br><br>";
return false;
}
}
}
As you can see, two choices within this function correspond to inserting a new account or updating an existing account. The function executes the appropriate query to save the account details.
After storing the account details, we go back to index.php, to the main body stage:
case 'store-settings' :
case 'account-setup' :
case 'delete-account' :
{
display_account_setup($auth_user);
break;
}
As you can see, we then execute the display_account_setup()function as before to list the user's account details. The newly added account will now be included.
The process for modifying an existing account is very similar. The user can change the account details and click the Save Changes button. Again this will trigger the store-settings action, but this time it will update the account details instead of inserting them.
To delete an account, the user can click the Delete Account button that is shown under each account listing. This activates the delete-account action.
In the preprocessing section of the index.php script, we will execute the following code:
case 'delete-account' :
{
delete_account($auth_user, $account);
break;
}
This code calls the delete_account() function. The code for this function is shown in Listing 27.6. Deleting accounts needs to be handled before the header because a choice of which account to use is inside the header. The account list needs to be updated before this can be correctly drawn.
function delete_account($auth_user, $accountid)
{
//delete one of this user's accounts from the DB
$query = "delete from accounts where
accountid='$accountid'and
username = '$auth_user'";
if(db_connect())
{
$result = mysql_query($query);
}
return $result;
}
After execution returns to index.php, the body stage will run the following code:
case 'store-settings' :
case 'account-setup' :
case 'delete-account' :
{
display_account_setup($auth_user);
break;
}
You will recognize this as the same code we ran before—it just displays the list of the user's accounts.
| only for RuBoard - do not distribute or recompile |