Added check for <text> fields that contain markup (an error!)
[moodle-linuxchix.git] / enrol / authorize / enrol_form.php
blob062fa4f041de8f3e7f15fce070b09db30bc80c2a
1 <?php // $Id$
3 require_once($CFG->libdir.'/formslib.php');
5 class enrol_authorize_form extends moodleform
7 function definition()
9 global $CFG, $USER;
11 $paymentmethodsenabled = get_list_of_payment_methods();
12 $paymentmethod = optional_param('paymentmethod', $paymentmethodsenabled[0], PARAM_ALPHA);
13 if (!in_array($paymentmethod, $paymentmethodsenabled)) {
14 error("Invalid payment method: $paymentmethod");
17 $mform =& $this->_form;
18 $course = $this->_customdata['course'];
20 $mform->addElement('header', 'general', get_string('paymentrequired'));
21 $othermethodstr = $this->other_method_available($paymentmethod);
22 if ($othermethodstr) {
23 $mform->addElement('static', '', '<div align="right">' . $othermethodstr . '</div>', '');
26 $mform->addElement('hidden', 'id', $course->id);
27 $mform->setType('id', PARAM_INT);
29 $mform->addElement('hidden', 'paymentmethod', $paymentmethod);
30 $mform->setType('paymentmethod', PARAM_ALPHA);
32 $firstlastnamestr = (AN_METHOD_CC == $paymentmethod) ? get_string('nameoncard', 'enrol_authorize') : get_string('echeckfirslasttname', 'enrol_authorize');
33 $firstlastnamegrp = array();
34 $firstlastnamegrp[] = &$mform->createElement('text', 'firstname', '', 'size="16"');
35 $firstlastnamegrp[] = &$mform->createElement('text', 'lastname', '', 'size="16"');
36 $mform->addGroup($firstlastnamegrp, 'firstlastgrp', $firstlastnamestr, '&nbsp;', false);
37 $firstlastnamegrprules = array();
38 $firstlastnamegrprules['firstname'][] = array(get_string('missingfirstname'), 'required', null, 'client');
39 $firstlastnamegrprules['lastname'][] = array(get_string('missinglastname'), 'required', null, 'client');
40 $mform->addGroupRule('firstlastgrp', $firstlastnamegrprules);
41 $mform->setType('firstname', PARAM_ALPHANUM);
42 $mform->setType('lastname', PARAM_ALPHANUM);
43 $mform->setDefault('firstname', $USER->firstname);
44 $mform->setDefault('lastname', $USER->lastname);
46 if (AN_METHOD_CC == $paymentmethod)
48 $mform->addElement('passwordunmask', 'cc', get_string('ccno', 'enrol_authorize'), 'size="20"');
49 $mform->setType('cc', PARAM_ALPHANUM);
50 $mform->setDefault('cc', '');
51 $mform->addRule('cc', get_string('missingcc', 'enrol_authorize'), 'required', null, 'client');
52 $mform->addRule('cc', get_string('ccinvalid', 'enrol_authorize'), 'numeric', null, 'client');
54 $monthsmenu = array('' => get_string('choose'));
55 for ($i = 1; $i <= 12; $i++) {
56 $monthsmenu[$i] = userdate(gmmktime(12, 0, 0, $i, 15, 2000), "%B");
58 $nowdate = getdate();
59 $startyear = $nowdate["year"] - 1;
60 $endyear = $startyear + 20;
61 $yearsmenu = array('' => get_string('choose'));
62 for ($i = $startyear; $i < $endyear; $i++) {
63 $yearsmenu[$i] = $i;
65 $ccexpiregrp = array();
66 $ccexpiregrp[] = &$mform->createElement('select', 'ccexpiremm', '', $monthsmenu);
67 $ccexpiregrp[] = &$mform->createElement('select', 'ccexpireyyyy', '', $yearsmenu);
68 $mform->addGroup($ccexpiregrp, 'ccexpiregrp', get_string('ccexpire', 'enrol_authorize'), '&nbsp;', false);
69 $ccexpiregrprules = array();
70 $ccexpiregrprules['ccexpiremm'][] = array(get_string('missingccexpire', 'enrol_authorize'), 'required', null, 'client');
71 $ccexpiregrprules['ccexpireyyyy'][] = array(get_string('missingccexpire', 'enrol_authorize'), 'required', null, 'client');
72 $mform->addGroupRule('ccexpiregrp', $ccexpiregrprules);
73 $mform->setType('ccexpiremm', PARAM_INT);
74 $mform->setType('ccexpireyyyy', PARAM_INT);
75 $mform->setDefault('ccexpiremm', '');
76 $mform->setDefault('ccexpireyyyy', '');
78 $creditcardsmenu = array('' => get_string('choose')) + get_list_of_creditcards();
79 $mform->addElement('select', 'cctype', get_string('cctype', 'enrol_authorize'), $creditcardsmenu);
80 $mform->setType('cctype', PARAM_ALPHA);
81 $mform->addRule('cctype', get_string('missingcctype', 'enrol_authorize'), 'required', null, 'client');
82 $mform->setDefault('cctype', '');
84 $mform->addElement('text', 'cvv', get_string('ccvv', 'enrol_authorize'), 'size="4"');
85 $mform->setHelpButton('cvv', array('cvv',get_string('ccvv', 'enrol_authorize'),'enrol/authorize'), true);
86 $mform->setType('cvv', PARAM_ALPHANUM);
87 $mform->setDefault('cvv', '');
88 $mform->addRule('cvv', get_string('missingcvv', 'enrol_authorize'), 'required', null, 'client');
89 $mform->addRule('cvv', get_string('missingcvv', 'enrol_authorize'), 'numeric', null, 'client');
91 if (!empty($CFG->an_authcode)) {
92 $ccauthgrp = array();
93 $ccauthgrp[] = &$mform->createElement('checkbox', 'haveauth', null, get_string('haveauthcode', 'enrol_authorize'));
94 $ccauthgrp[] = &$mform->createElement('static', 'nextline', null, '<br />');
95 $ccauthgrp[] = &$mform->createElement('text', 'ccauthcode', '', 'size="8"');
96 $mform->addGroup($ccauthgrp, 'ccauthgrp', get_string('authcode', 'enrol_authorize'), '&nbsp;', false);
97 $mform->setHelpButton('ccauthgrp', array('authcode',get_string('authcode', 'enrol_authorize'),'enrol/authorize'), true);
99 $ccauthgrprules = array();
100 $ccauthgrprules['ccauthcode'][] = array(get_string('missingccauthcode', 'enrol_authorize'), 'numeric', null, 'client');
101 $mform->addGroupRule('ccauthgrp', $ccauthgrprules);
102 $mform->setDefault('haveauth', '');
103 $mform->setDefault('ccauthcode', '');
106 if (!empty($CFG->an_avs)) {
107 $mform->addElement('header', '', '&nbsp;&nbsp;' . get_string('address'), '');
109 $mform->addElement('text', 'ccaddress', get_string('address'), 'size="30"');
110 $mform->setType('ccaddress', PARAM_ALPHANUM);
111 $mform->setDefault('ccaddress', $USER->address);
112 $mform->addRule('ccaddress', get_string('missingaddress', 'enrol_authorize'), 'required', null, 'client');
114 $citystategrp = array();
115 $citystategrp[] = &$mform->createElement('text', 'cccity', '', 'size="14"');
116 $citystategrp[] = &$mform->createElement('static', 'sep', null, ' - ');
117 $citystategrp[] = &$mform->createElement('text', 'ccstate', '', 'size="8"');
118 $mform->addGroup($citystategrp, 'citystategrp', get_string('city') . ' - ' . get_string('state'), '&nbsp;', false);
119 $citystategrprules = array();
120 $citystategrprules['cccity'][] = array(get_string('missingcity'), 'required', null, 'client');
121 $mform->addGroupRule('citystategrp', $citystategrprules);
122 $mform->setType('cccity', PARAM_ALPHANUM);
123 $mform->setType('ccstate', PARAM_ALPHANUM);
124 $mform->setDefault('cccity', $USER->city);
125 $mform->setDefault('ccstate', '');
127 $mform->addElement('select', 'cccountry', get_string('country'), get_list_of_countries());
128 $mform->addRule('cccountry', get_string('missingcountry'), 'required', null, 'client');
129 $mform->setType('cccountry', PARAM_ALPHA);
130 $mform->setDefault('cccountry', $USER->country);
132 else {
133 $mform->addElement('hidden', 'ccstate', '');
134 $mform->addElement('hidden', 'ccaddress', $USER->address);
135 $mform->addElement('hidden', 'cccity', $USER->city);
136 $mform->addElement('hidden', 'cccountry', $USER->country);
139 elseif (AN_METHOD_ECHECK == $paymentmethod)
141 $mform->addElement('text', 'abacode', get_string('echeckabacode', 'enrol_authorize'), 'size="9" maxlength="9"');
142 $mform->setHelpButton('abacode', array('aba',get_string('echeckabacode', 'enrol_authorize'),'enrol/authorize'), true);
143 $mform->setType('abacode', PARAM_ALPHANUM);
144 $mform->setDefault('abacode', '');
145 $mform->addRule('abacode', get_string('missingaba', 'enrol_authorize'), 'required', null, 'client');
146 $mform->addRule('abacode', get_string('missingaba', 'enrol_authorize'), 'numeric', null, 'client');
148 $mform->addElement('text', 'accnum', get_string('echeckaccnum', 'enrol_authorize'), 'size="20" maxlength="20"');
149 $mform->setType('accnum', PARAM_ALPHANUM);
150 $mform->setDefault('accnum', '');
151 $mform->addRule('accnum', get_string('invalidaccnum', 'enrol_authorize'), 'required', null, 'client');
152 $mform->addRule('accnum', get_string('invalidaccnum', 'enrol_authorize'), 'numeric', null, 'client');
154 $acctypes = array();
155 $acctypesenabled = get_list_of_bank_account_types();
156 foreach ($acctypesenabled as $key) {
157 $acctypes[$key] = get_string("echeck".strtolower($key), "enrol_authorize");
159 $acctypes = array('' => get_string('choose')) + $acctypes;
160 $mform->addElement('select', 'acctype', get_string('echeckacctype', 'enrol_authorize'), $acctypes);
161 $mform->setType('acctype', PARAM_ALPHA);
162 $mform->addRule('acctype', get_string('invalidacctype', 'enrol_authorize'), 'required', null, 'client');
163 $mform->setDefault('acctype', '');
165 $mform->addElement('text', 'bankname', get_string('echeckbankname', 'enrol_authorize'), 'size="20" maxlength="50"');
166 $mform->setType('bankname', PARAM_ALPHANUM);
167 $mform->setDefault('bankname', '');
168 $mform->addRule('bankname', get_string('missingbankname', 'enrol_authorize'), 'required', null, 'client');
171 $mform->addElement('text', 'cczip', get_string('zipcode', 'enrol_authorize'), 'size="5"');
172 $mform->setType('cczip', PARAM_ALPHANUM);
173 $mform->setDefault('cczip', '');
174 $mform->addRule('cczip', get_string('missingzip', 'enrol_authorize'), 'required', null, 'client');
176 $this->add_action_buttons(false, get_string('sendpaymentbutton', 'enrol_authorize'));
179 function validation($data, $files)
181 global $CFG;
182 $errors = parent::validation($data, $files);
184 if (AN_METHOD_CC == $data['paymentmethod'])
186 if (!in_array($data['cctype'], array_keys(get_list_of_creditcards()))) {
187 $errors['cctype'] = get_string('missingcctype', 'enrol_authorize');
190 $expdate = sprintf("%02d", intval($data['ccexpiremm'])) . $data['ccexpireyyyy'];
191 $validcc = $this->validate_cc($data['cc'], $data['cctype'], $expdate);
192 if (!$validcc) {
193 if ($validcc === 0) {
194 $errors['ccexpiregrp'] = get_string('ccexpired', 'enrol_authorize');
196 else {
197 $errors['cc'] = get_string('ccinvalid', 'enrol_authorize');
201 if (!empty($CFG->an_authcode) && !empty($data['haveauth']) && empty($data['ccauthcode'])) {
202 $errors['ccauthgrp'] = get_string('missingccauthcode', 'enrol_authorize');
205 elseif (AN_METHOD_ECHECK == $data['paymentmethod'])
207 if (!$this->validate_aba($data['abacode'])) {
208 $errors['abacode'] = get_string('invalidaba', 'enrol_authorize');
211 if (!in_array($data['acctype'], get_list_of_bank_account_types())) {
212 $errors['acctype'] = get_string('invalidacctype', 'enrol_authorize');
216 return $errors;
219 function other_method_available($currentmethod)
221 $course = $this->_customdata['course'];
223 if ($currentmethod == AN_METHOD_CC) {
224 $otheravailable = in_array(AN_METHOD_ECHECK, get_list_of_payment_methods());
225 $url = 'enrol.php?id='.$course->id.'&amp;paymentmethod='.AN_METHOD_ECHECK;
226 $stringtofetch = 'usingecheckmethod';
228 else {
229 $otheravailable = in_array(AN_METHOD_CC, get_list_of_payment_methods());
230 $url = 'enrol.php?id='.$course->id.'&amp;paymentmethod='.AN_METHOD_CC;
231 $stringtofetch = 'usingccmethod';
233 if ($otheravailable) {
234 $a = new stdClass;
235 $a->url = $url;
236 return get_string($stringtofetch, "enrol_authorize", $a);
238 else {
239 return '';
243 function validate_aba($aba)
245 if (ereg("^[0-9]{9}$", $aba)) {
246 $n = 0;
247 for($i = 0; $i < 9; $i += 3) {
248 $n += (substr($aba, $i, 1) * 3) + (substr($aba, $i + 1, 1) * 7) + (substr($aba, $i + 2, 1));
250 if ($n != 0 and $n % 10 == 0) {
251 return true;
254 return false;
257 function validate_cc($Num, $Name = "n/a", $Exp = "")
259 // Check the expiration date first
260 if (strlen($Exp))
262 $Month = substr($Exp, 0, 2);
263 $Year = substr($Exp, -2);
264 $WorkDate = "$Month/01/$Year";
265 $WorkDate = strtotime($WorkDate);
266 $LastDay = date("t", $WorkDate);
267 $Expires = strtotime("$Month/$LastDay/$Year 11:59:59");
268 if ($Expires < time()) return 0;
271 // Innocent until proven guilty
272 $GoodCard = true;
274 // Get rid of any non-digits
275 $Num = ereg_replace("[^0-9]", "", $Num);
277 // Perform card-specific checks, if applicable
278 switch ($Name)
280 case "mcd" :
281 $GoodCard = ereg("^5[1-5].{14}$", $Num);
282 break;
284 case "vis" :
285 $GoodCard = ereg("^4.{15}$|^4.{12}$", $Num);
286 break;
288 case "amx" :
289 $GoodCard = ereg("^3[47].{13}$", $Num);
290 break;
292 case "dsc" :
293 $GoodCard = ereg("^6011.{12}$", $Num);
294 break;
296 case "dnc" :
297 $GoodCard = ereg("^30[0-5].{11}$|^3[68].{12}$", $Num);
298 break;
300 case "jcb" :
301 $GoodCard = ereg("^3.{15}$|^2131|1800.{11}$", $Num);
302 break;
304 case "dlt" :
305 $GoodCard = ereg("^4.{15}$", $Num);
306 break;
308 case "swi" :
309 $GoodCard = ereg("^[456].{15}$|^[456].{17,18}$", $Num);
310 break;
312 case "enr" :
313 $GoodCard = ereg("^2014.{11}$|^2149.{11}$", $Num);
314 break;
317 // The Luhn formula works right to left, so reverse the number.
318 $Num = strrev($Num);
319 $Total = 0;
321 for ($x=0; $x < strlen($Num); $x++)
323 $digit = substr($Num, $x, 1);
325 // If it's an odd digit, double it
326 if ($x/2 != floor($x/2)) {
327 $digit *= 2;
329 // If the result is two digits, add them
330 if (strlen($digit) == 2)
331 $digit = substr($digit, 0, 1) + substr($digit, 1, 1);
333 // Add the current digit, doubled and added if applicable, to the Total
334 $Total += $digit;
337 // If it passed (or bypassed) the card-specific check and the Total is
338 // evenly divisible by 10, it's cool!
339 return ($GoodCard && $Total % 10 == 0);