//:: http://10.10.2.6/loan.php?name=Eva&amount=10000&rate=3.5&term=1 ::
//:: no query parameters were provided, redirect client to "loan.html" ::
//:: **MISSING ::
//:: if we have query parameters, we process a GET/POST form ::
$name = $_GET['name'];
if($name!='') :
$monthList = array("January", "February", "March", "April", "May",
"June", "July", "August", "September", "October",
"November", "December", "");
$start = microtime(true);
$amount = floatval(trim($_GET['amount']));
if($amount < 1) :
$amount = 1;
endif;
$rate = floatval(trim($_GET['rate']));
if($rate > 19) :
$rate = 20;
endif;
if($rate > 1) :
$rate = $rate / 100.;
else :
if($rate < 1) :
$rate = 1. / 100.;
endif;
endif;
$term = floatval(trim($_GET['term']));
if($term < 0.1) :
$term = 1. / 12.;
endif;
$months = ($term>32)?$term:$term*12;
$name = addslashes($_GET['name']);
$year = 1;
$lastpayment = 1;
//:: all litteral strings provided by a client must be escaped this way ::
//:: if you inject them into an HTML page ::
htmlspecialchars($name);
//:: filter input data to avoid all the useless/nasty cases ::
//:: **MISSING ::
//:: calculate the monthly payment amount ::
$payment = $amount*$rate/12*pow(1+$rate/12, $term*12)
/ (pow(1+$rate/12, $term*12)-1);
$cost = ($term*12*$payment)-$amount;
//:: build the top of our HTML page ::
$result = "
Loan Calculator " .
" " .
" " .
"
Loan Calculator " .
"Dear $name, your loan goes as follows: ";
$result .= sprintf("" .
"loan details " .
"Amount %s " .
"Rate %.2f%% " .
"Term %u %s(s) " .
"Cost %.2f (%.2f%%) " .
"
", number_format($amount, 2), $rate*100,
$term,
//:: **MISSING uceil(12*term), ::
($term<36)?"year":"month",
$cost, 100/($amount/$cost));
$result .= sprintf(" " .
"" .
"month payment interest " .
"principle balance ",
$year);
for($month=1; $payment>0; $month++) : //:: output monthly payments ::
$interest = ($amount*$rate)/12;
if($amount>$payment) :
$amount = ($amount-$payment)+$interest;
$principle = $payment-$interest;
else : //:: calculate last payment ::
if($lastpayment) :
$lastpayment = 0;
$payment = $amount;
$principle = $amount-$interest;
$amount = 0;
else : //:: all payments are done, just padd the table ::
$amount = 0;
$payment = 0;
$interest = 0;
$principle = 0;
endif;
endif;
$result .= sprintf("%s %s " .
"%s %s %s ",
$month%2, $monthList[$month-1],
number_format($payment, 2),
number_format($interest, 2),
number_format($principle, 2),
number_format($amount, 2));
if($month==12) :
if($amount) :
$month=0; $year++;
$result .= sprintf("
" .
"month payment interest " .
"principle balance ",
$year);
endif;
endif;
endfor;
//:: time the process and close the HTML page ::
$result .= sprintf("
This page was generated in %s ms." .
" (on a 3GHz CPU 1 ms = 3,000,000 cycles)" .
" ",
number_format((microtime(true)-$start)*1000), 2);
echo $result;
exit;
endif;
?>