-
Notifications
You must be signed in to change notification settings - Fork 0
/
pnuser.php
888 lines (772 loc) · 29.7 KB
/
pnuser.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
<?php
/**
* FEproc - Mail template backend module for FormExpress for
* Zikula Content Management System
*
* @copyrightt (C) 2002 by Jason Judge, 2011 Chris Candreva
* @Version $Id: $
* @license GNU/GPL - http://www.gnu.org/copyleft/gpl.html
* @package FEproc
*
*
* LICENSE
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License (GPL)
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WIthOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* To read the license please visit http://www.gnu.org/copyleft/gpl.html
* ----------------------------------------------------------------------
* Original Author of file: Jason Judge.
* Based on template by Jim MacDonald.
* Current Maintainer of file: Chris Candreva <[email protected]>
* ----------------------------------------------------------------------
*
*/
/*
* Handler for FormExpress
* The submit action catches the form content, and the success and
* failue actions switch to the next stage.
* @param $args['action'] - either 'submit', 'success' or 'failure'
* @param $args['setid'] - the set id
*/
function feproc_user_formexpress($args)
{
// General check for access to this module.
if (!SecurityUtil::checkPermission ('FEproc::', 'FEproc::', ACCESS_READ)) {
return LogUtil::registerPermissionError();
}
// Get the arguments
extract($args);
if (isset($setid) && !is_numeric($setid))
{
pnSessionSetVar('errormsg', __("Invalid Parameters"));
return false;
}
if (!isset($action))
{
pnSessionSetVar('errormsg', __("Invalid Parameters"));
return false;
} else {
$action = strtolower($action);
}
// Ignore the form submission or validation. Catch the form again
// at the one of the final stages.
if ($action != 'success' && $action != 'failure')
{
return true;
}
// Get form result status.
if ($action == 'success')
{
$actionsuccess = true;
} else {
$actionsuccess = false;
}
// Get the feproc session object.
if (!pnModAPILoad('feproc', 'session'))
{
pnSessionSetVar('errormsg', _FXMODLOADFAILED);
return pnRedirect(pnModURL('feproc', 'user', 'view'));
}
// Get the FormExpress session object and extract the submitted form id.
$fxSession = new FXSession();
$form_id = $fxSession->getSubmittedFormID();
$sessiondata = new feprocSession();
// Get set id if one is running.
$currentsetid = $sessiondata->getSetID();
$currentstage = false;
if ($currentsetid)
{
// A set is running. Ensure this form is valid for the set.
// If the set has timed out, then restart it.
// TODO: wait until these two sections (FE and Proc) are merged. It should be done
// centrally in getNextStage().
/*
if ($currentsetid)
{
$sessiontimeout = pnModGetVar('FEproc', 'sessiontimeout');
if ($sessiontimeout > 0 && time() > (60*$sessiontimeout + $sessiondata->getSessionStart()))
{
// TODO: tell the user the session has timed out.
// Set the user back to the start of the set...
$setid = $currentsetid;
$stageid = false;
// ...and close the current set.
$currentsetid = false;
}
}
*/
if ($currentsetid)
{
// Get the last registered stage for this set.
$stageid = $sessiondata->getStageID();
// Try with the last stage id first (in case the same form is
// used more than once in the set.
$currentstage = pnModAPIFunc('feproc', 'user', 'getstage',
array(
'type' => 'formexpress',
'stageid' => $stageid,
'handlerid' => $form_id,
'setid' => $currentsetid
)
);
}
if (!$currentstage)
{
// Try again without the stage.
$currentstage = pnModAPIFunc('feproc', 'user', 'getstage',
array(
'type' => 'formexpress',
'handlerid' => $form_id,
'setid' => $currentsetid
)
);
}
// If we did not get a stage, then close the set: we may have
// moved on to a new set.
if ($currentstage)
{
$stageid = $currentstage['stageid'];
} else {
$sessiondata->closeSession();
$currentsetid = false;
$stageid = false;
}
}
if (! $currentsetid)
{
// Set is not in process. See if we can start one.
$currentstage = pnModAPIFunc('feproc', 'user', 'getstage',
array(
'start' => true,
'type' => 'formexpress',
'handlerid' => $form_id
)
);
if ($currentstage)
{
// Yes - this is a starting stage form.
$currentsetid = $currentstage['setid'];
$stageid = $currentstage['id'];
if (SecurityUtil::checkPermission ('FEproc::Set', "::$setid", ACCESS_READ))
{
$sessiondata->startSet($currentsetid, $stageid);
} else {
return LogUtil::registerPermissionError();
}
} else {
// TODO: error if no starting stage.
pnSessionSetVar('errormsg', "Not a starting stage (this form can only be run as part of a set)");
return pnRedirect(pnModURL('feproc', 'user', 'view'));
}
}
// Get the form data if the form submission action was a success.
if ($actionsuccess)
{
// Get the form data.
// The form cache provides the definition of the form so
// empty fields can still be included, like unchecked checkboxes.
$fxCache = new FXCache();
$formcache = $fxCache->getForm($form_id);
// Get the submitted form data.
$formdata = $fxSession->getForm($form_id);
$newformdata = Array();
foreach($formcache['items'] as $item)
{
$itemName = $item['item_name'];
// Remove any substitution variables the user may have entered.
$itemValue = preg_replace('/\${[^:]+:[^}]+}/', '*REMOVED*', $formdata[$itemName]);
// Ignore some special FormExpress items.
if ($itemName <> 'BoilerPlate' && $itemName <> 'GroupStart' && $itemName <> 'GroupEnd')
{
$newformdata[$itemName] = $itemValue;
}
}
if (!empty($newformdata))
{
$sessiondata->addFormData($newformdata);
}
}
if ($actionsuccess)
{
$nextstageid = $currentstage['successid'];
} else {
$nextstageid = $currentstage['failureid'];
}
// Pass in: the next stage id.
$nextstage = pnModAPIFunc(
'feproc', 'user', 'nextstage',
array(
'stageid' => $nextstageid
)
);
// Return control to the user: either a redirect or display an error.
if ($nextstage['action'] == 'redirect' || $nextstage['action'] == 'form'
|| $nextstage['action'] == 'display' || $nextstage['action'] == 'formexpress')
{
// There is a URL to go to. This could be external to feproc
// or it could be the next stage in the set.
return pnRedirect($nextstage['url']);
}
if ($nextstage['action'] == 'error')
{
// Display the text. This is a templated output.
$render = pnRender::getInstance('feproc');
$render->assign('text', $nextstage['text']);
return $render->fetch('feproc_user_error.tpl');
}
// Don't know what the next stage is, so redirect to an error page.
// TODO: handle better as an error.
return false;
}
/*
* Handler for any stage - except for FE forms (TODO: yet).
* Used when a display stage link needs to jump to a stage
* that does not have a user display element to it (not a form,
* display or error stage.
* failue actions switch to the next stage.
* @param $args['stageid'] - the stage id
* @param $args['setid'] - the set id (to start a set)
* @param $args['resetid'] - set to reset the set and start again
* @param $args['stagename'] - identify a stage by name rather than ID
*/
function feproc_user_process($args)
{
// General check for access to this module.
if (!SecurityUtil::checkPermission ('FEproc::', '::', ACCESS_READ)) {
return LogUtil::registerPermissionError();
}
list($stageid, $setid, /*$resetid,*/ $reset) = pnVarCleanFromInput('stageid', 'setid', /*'resetid',*/ 'reset');
// TODO: not sure at all about this logic. Split up parameter checks into
// a number of layers: a) at least one is set; b) those that are set are
// the correct types.
if ( (!isset($stageid) || $stageid == 0 || !is_numeric($stageid))
&& (!isset($setid) || $setid == 0 || !is_numeric($setid))
&& (!isset($resetid) || $resetid == 0 || !is_numeric($resetid)) )
{
pnSessionSetVar('errormsg', "Invalid or missing parameters");
return pnRedirect(pnModURL('feproc', 'user', 'view'));
}
// Get the feproc session object.
if (!pnModAPILoad('feproc', 'session'))
{
pnSessionSetVar('errormsg', _FXMODLOADFAILED);
return pnRedirect(pnModURL('feproc', 'user', 'view'));
}
$sessiondata = new feprocSession();
$currentsetid = $sessiondata->getSetID();
$laststageid = $sessiondata->getStageID();
// A set is already running.
if ($currentsetid)
{
// A set is already running. Make sure this stage is in that set.
// We don't know what type of stage it is yet.
// Logic is:
// setid set : stageid set : action
// Y : N : setid must be same as current setid; get set starting stage; clear done session (start fresh)
// Y : Y : setid must be same as current setid; get stage from within set
// N : Y : get stage from within current set
// Any failures to get the stage from this logic will result in the current
// set being closed.
$stage = false;
// If 'reset' is passed in, then close the current set.
if ($reset)
{
$currentsetid = false;
}
/*
// If resetid is sent in, then close everything and restart the set.
if ($currentsetid && $resetid)
{
// Set the user to the start of the set (which may or may not be the
// same as the current set)...
$setid = $resetid;
$stageid = false; // TODO: check this. We need a reset with multiple starting points.
// ...and close the current set.
$currentsetid = false;
}
*/
// If setid is sent in it must be the same as the running one.
if ($currentsetid && $setid && $setid <> $currentsetid)
{
$currentsetid = false;
}
// If this session has timed out, then close it now. The form set
// session is distinct from the main user session in that it has
// its own timeout period, which could be shorter than the user's
// timeout period.
// TODO: timeout functionality to be done in FE processing too.
// If the set has timed out, then restart it.
if ($currentsetid)
{
$sessiontimeout = pnModGetVar('FEproc', 'sessiontimeout');
if ($sessiontimeout > 0 && time() > (60*$sessiontimeout + $sessiondata->getSessionStart()))
{
// TODO: tell the user the session has timed out.
// Set the user back to the start of the set...
$setid = $currentsetid;
$stageid = false; // TODO: get default timeout stage from the set attributes.
// ...and close the current set.
$currentsetid = false;
}
}
if ($currentsetid && $stageid)
{
// A stageid has been supplied - get that stage from the set.
$stage = pnModAPIFunc('feproc', 'user', 'getstage',
array(
'stageid' => $stageid,
'setid' => $currentsetid
)
);
if (!$stage)
{
$currentsetid = false;
}
}
if ($currentsetid && !$stageid)
{
// A stageid has not been supplied - get the starting stage from the set.
$stage = pnModAPIFunc('feproc', 'user', 'getstage',
array(
'start' => true,
'setid' => $currentsetid
)
);
if ($stage)
{
$stageid = $stage['stageid'];
}
// Since we have only been provided with the setid, reset the form session and
// start from afresh.
//$currentsetid = false;
}
// If this stage is not in the set, then close the current set.
if (!$currentsetid)
{
$sessiondata->closeSession();
$currentsetid = false; // Redundant
}
} /*else {
// No set already running, but do some manipulation of the
// parameters.
if ($resetid)
{
// Set the user to the start of the set (which may or may not be the
// same as the current set)...
$setid = $resetid;
$stageid = false; // TODO: check this. We need a reset with multiple starting points.
}
}*/
// No set is already running (or one has just been aborted).
if (! $currentsetid)
{
// No set is currently running.
// This display stage should be at the start of a set.
// Get the stage details of the stage that starts a set.
if (!$stage && $stageid)
{
// Only fetch it if we don't already have it.
$stage = pnModAPIFunc(
'feproc', 'user', 'getstage',
array(
'start' => true,
'stageid' => $stageid
)
);
}
if (!$stage && $setid)
{
// Only fetch it if we don't already have it.
$stage = pnModAPIFunc(
'feproc', 'user', 'getstage',
array(
'start' => true,
'setid' => $setid
)
);
}
if (!$stage)
{
// TODO: error if not a starting stage (redirect).
pnSessionSetVar('errormsg', "Not a starting stage (this display page can only be run as part of a set)");
return pnRedirect(pnModURL('feproc', 'user', 'view'));
} else {
$setid = $stage['setid'];
$stageid = $stage['id'];
// Check we are allowed to run this set before instantiating it.
if (pnSecAuthAction(0, 'FEproc::Set', "::$setid", ACCESS_READ))
{
$sessiondata->startSet($setid, $stageid);
} else {
pnSessionSetVar('errormsg', _BADAUTHKEY);
pnRedirect(pnModURL('feproc', 'user', 'view'));
return true;
}
}
}
// If a form user-stage then redirect there now as another page is needed to display it.
if ($stage['type'] == 'form' || $stage['type'] == 'formexpress')
{
if (pnModGetVar('FEproc', 'tracestack'))
{
// Log a stack message.
$sessiondata->setMessages(Array('stack' => "$stageid: $stage[action] $stage[handlerid]"), true);
}
// If required, set form items from previously-captured data.
if (pnModGetVar('FEproc', 'shareformitems') && false) // Not yet supported.
{
// Copy session form values to the form about to be launched
// where the item names are the same, but values differ.
// The form may not have been launched yet, so initialise it
// if required.
// Load the FE module.
if (!pnModLoad('FormExpress', 'user'))
{
pnSessionSetVar('errormsg', _FXMODLOADFAILED);
pnRedirect(pnModURL('feproc', 'user', 'view'));
return true;
}
// Has form been loaded already? Initialise if not.
$fxSession = new FXSession();
// Get submitted form data - there may not be any.
$formdata = $fxSession->getForm($stage['handlerid']);
if (!is_array($formdata))
{
// The form has not been submitted yet - initialise it.
// Get the form cache (the definition of the form items).
$fxCache = new FXCache();
$formcache = $fxCache->getForm($stage['handlerid']);
// Create the blank form.
if (is_array($formcache))
{
// TODO: a little more thought needs to go into this...
// FE can take default values from a previous form anyway
// using the function defined here - feproc_user_DefItemValue()
// but if revisiting a form, we need a way to refresh the
// item value from the cached value as it may have changed.
}
}
}
$url = pnModAPIFunc(
'feproc', 'user', 'stageurl',
array('stageid' => $stageid)
);
return pnRedirect($url);
}
// Execute any backend handler stages then get the next user stage.
$nextstage = pnModAPIFunc(
'feproc', 'user', 'nextstage',
array('stageid' => $stageid)
);
if ($nextstage['stageid'] <> $stageid)
{
// We have moved on to a new stage. Refresh the details
// then redirect to the stage so the URL is set correctly.
// This would happen if several internal stages were
// executed before the user stage.
$stage = $nextstage['stage'];
$stageid = $nextstage['stageid'];
$url = pnModAPIFunc(
'feproc', 'user', 'stageurl',
array('stageid' => $stageid)
);
return pnRedirect($url);
}
// Display handler: display the page now.
if ($nextstage['action'] == 'display' || $nextstage['action'] == 'redirect')
{
// Get the template-substituted handler data.
$handlerdata = pnModAPIFunc('feproc', 'user', 'handlerdata',
array('stageid' => $stageid)
);
// Get the handler details.
$handler = pnModAPIFunc('feproc', 'handleruser', 'gethandler',
array('hid' => $stage['handlerid'])
);
// The handler may not exist.
if (!$handler)
{
$nextstage = Array(
'action' => 'error',
'url' => false,
'text' => "Handler does not exist (hid=$stage[handlerid]).",
'complete' => true
);
break;
}
// Load the handler API
if (!pnModAPILoad($handler['modulename'], $handler['apiname']))
{
// TODO: proper error message.
$nextstage = Array(
'action' => 'error',
'url' => false,
'text' => "Failed to load handler API ($handler[modulename], $handler[apiname]).",
'complete' => true
);
break;
}
// Call the handler processing function.
$handlerResult = pnModAPIFunc(
$handler['modulename'], $handler['apiname'], $handler['apifunc'],
array('action' => 'execute', 'info' => $handlerdata)
);
// TODO: combine the display and redirect as they are very similar?
// TODO: most of this will go into the 'nextstage' handler anyway.
if ($nextstage['action'] == 'display')
{
// Before returning, if this is the last stage then close the session.
if (($handlerResult['result'] && !$stage['successid'])
|| (!$handlerResult['result'] && !$stage['failureid']))
{
$sessiondata->closeSession();
}
if ($handlerResult['result'])
{
if (pnModGetVar('FEproc', 'tracestack'))
{
// Log a stack message.
$sessiondata->setMessages(Array('stack' => "$stageid: $nextstage[action] success"), true);
}
// Handler returned success and some text to display.
return $handlerResult['text'];
} else {
// Handler returned failure - redirect to the error stage.
if ($stage['failureid'])
{
if (pnModGetVar('FEproc', 'tracestack'))
{
// Log a stack message.
$sessiondata->setMessages(Array('stack' => "$stageid: $nextstage[action] failure to $stage[failureid]"), true);
}
$url = pnModAPIFunc(
'feproc', 'user', 'stageurl',
array('stageid' => $stage['failureid'])
);
} else {
if (pnModGetVar('FEproc', 'tracestack'))
{
// Log a stack message.
$sessiondata->setMessages(Array('stack' => "$stageid: $nextstage[action] failure no failureid"), true);
}
pnSessionSetVar('errormsg', "Handler returned error but there is no error stage defined.");
$url = pnModURL('feproc', 'user', 'view');
// TODO: close session?
}
pnRedirect($url);
return true;
}
}
if ($nextstage['action'] == 'redirect')
{
// If there is no next stage then close the session
if (($handlerResult['result'] && !$stage['successid'])
|| (!$handlerResult['result'] && !$stage['failureid']))
{
$sessiondata->closeSession();
}
if ($handlerResult['result'])
{
// Handler return success with a url.
$url = $handlerResult['url'];
} else {
// Handler returned failure - redirect to the error stage.
if ($stage['failureid'])
{
$url = pnModAPIFunc(
'feproc', 'user', 'stageurl',
array('stageid' => $stage['failureid'])
);
} else {
pnSessionSetVar('errormsg', "Handler returned error but there is no error stage defined.");
$url = pnModURL('feproc', 'user', 'view');
// TODO: close session?
}
}
return pnRedirect($url);
}
}
// Return control to the user: either a redirect or display an error.
// TODO: make all returns a redirect - even for errors.
if ($nextstage['action'] == 'form' || $nextstage['action'] == 'formexpress')
{
// There is a URL to go to. This could be external to feproc
// or it could be the next stage in the set.
// TODO: need to jump back to the top really. It may be necessary to
// pre-load the form with shared values before redirecting to FormExpress.
// This redirect to FormExpress is being done in two places here - it should
// be done in one place only. Perhaps just:
// return feproc_user_process(array('stageid' =>$stageid));
// would do the trick?
pnRedirect($nextstage['url']);
return true;
}
if ($nextstage['action'] == 'error')
{
// Display the text. This is a templated output.
// TODO: make this a redirect too.
return "<h3>Error</h3>" . $nextstage['text'];
}
// Don't know what the next stage is, so redirect to an error page.
// TODO: handle better as an error.
return false;
}
/*
* Default user view.
*/
function feproc_user_view($args)
{
// TODO: Need to handle this a bit better.
// At present there is no generic 'view' of a set.
// Perhaps there should be for administrators?
$message = pnSessionGetVar('errormsg');
return "An unexpected error has occurred: $message";
}
/*
* Handy validation functions for FE.
* These should be moved to another module as there is no point loading
* them if they are not used in the majority of cases. If FE will support
* calling functions from module types other than 'user' then perhaps
* these can be moved to another type, such as 'validation'.
*/
/*
* Validate credit card numbers.
* (Rules derived from http://www.beachnet.com/~hstiles/cardtype.html).
* Parameters are:
* args['fx_value']: value of item being validated.
* args['message']: message to display if card number is invalid.
* Called as an FE validation string-
* {FEproc:ValCreditCard&message='Failure over-ride message'}
* Or-
* {FEproc:ValCreditCard&dummy=dummy}
*/
function feproc_user_ValCreditCard($args)
{
// Validate a credit card number
extract($args);
if (!isset($message))
{
$message = 'Invalid credit card number';
}
if (!isset($fx_value) || strlen($fx_value) == 0)
{
// Value is null: nothing to validate.
return false;
}
// Remove spaces and dashes.
$fx_value = str_replace(' ', '', $fx_value);
$fx_value = str_replace('-', '', $fx_value);
// Check characters. Only numeric is allowed.
if (!ereg('^[0-9]+$', $fx_value))
{
return "$message (invalid characters)";
}
// Check issuers. Start with a list of card types and issuers.
$cardtypes = array(
array('issuer'=>'VISA', 'alg'=>'mod10', 'format'=>'^4(.{12}|.{15})$'),
array('issuer'=>'MASTERCARD', 'alg'=>'mod10', 'format'=>'^5[1-5].{14}$'),
array('issuer'=>'AMEX', 'alg'=>'mod10', 'format'=>'^3[47].{13}$'),
array('issuer'=>'Diners Club/Carte Blanche',
'alg'=>'mod10', 'format'=>'^3(0[0-5].{11}|[68].{12})$'),
array('issuer'=>'Discover', 'alg'=>'mod10', 'format'=>'^6011.{12}$'),
array('issuer'=>'JCB', 'alg'=>'mod10', 'format'=>'^(3.{15}|(2131|1800).{11})$'),
array('issuer'=>'enRoute', 'alg'=>'mod10', 'format'=>'^2(014|149).{11}$'),
array('issuer'=>'SWITCH', 'alg'=>'mod10', 'format'=>'^([0-9]{19}|[0-9]{18}|[0-9]{16})$')
);
/* UK Switch cards (need to confirm this is the full list before imlementing it)
* I am suspicious that some Switch card numbers are already identified as Visa
* cards.
* 19 digits:- 4936 633301 675901 675905 675918 675950-675962 675998
* 18 digits:- 675938-675940 490302-490309 490335-490340 491174-491182
* 16 digits:- 564182 633110 490525-490529 491100-491102 633300 633302-633399
675900 675902-675904 675906-675917 675919-675937 675941-675949
675963-675997 675999
*/
unset($issuer);
foreach($cardtypes as $cardtype)
{
if (ereg($cardtype['format'], $fx_value))
{
$issuer = $cardtype['issuer'];
$algorithm = $cardtype['alg'];
break;
}
}
if (!isset($issuer))
{
return "$message (card type not recognised)";
}
// Now validate the check digit.
if ($algorithm == 'mod10')
{
// Do mod10 check.
// Reverse the number to make processing a little easier
// since the RH-most digit is declared 'odd' regardless of
// the total number of digits in the card number.
$cardrev = strrev ($fx_value);
// Loop through the card number one digit at a time.
// Double the value of every second digit (starting from the RHS
// of the original card number).
// Concatenate the new values with the unaffected digits
$newdigits = '';
for ($i = 0; $i < strlen($cardrev); ++$i)
{
$newdigits .= ($i % 2) ? $cardrev[$i] * 2 : $cardrev[$i];
}
// Sum the resulting digits.
$digitsum = 0;
for ($i = 0; $i < strlen($newdigits); ++$i)
{
$digitsum += $newdigits[$i];
}
// Valid card numbers will have a digit sum divisible by 10.
if (($digitsum % 10) <> 0)
{
return "$message (the $issuer card number is not correct)";
}
}
// A false return means 'no validation failure'.
return false;
}
/*
* Function to return a value from the form value cache.
* This would generally be used to supply a default value
* to a form.
* A form value would be obtained using the following syntax
* in FormExpress-
* {FEproc:DefItemValue&name='item-name'}
*/
function feproc_user_DefItemValue($args)
{
extract ($args);
if (!isset($name))
{
// No name supplied.
return false;
}
// Get the feproc session object.
if (!pnModAPILoad('feproc', 'session'))
{
//pnSessionSetVar('errormsg', _FXMODLOADFAILED);
return false;
}
// Get the FEproc session information.
$sessiondata = new feprocSession();
// Get the form data array (form items collected so far).
$formdata = $sessiondata->getFormData();
if (isset($formdata[$name]))
{
return $formdata[$name];
} else {
return false;
}
}
?>