-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathsaprfc_test.php
746 lines (696 loc) · 32.7 KB
/
saprfc_test.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
<?
// SAPRFC_TEST.PHP - $Id: saprfc_test.php,v 1.7 2004/07/15 05:42:52 koucky Exp $
// Test script for SAPRFC extension module for PHP 4 and PHP 5
//
// Features:
// - similar function as "Single Test" in SE37 transaction (Function Builder)
// - support login to application server or to logon group (using load balancing)
// - login data are saved in cookie on client side
// - select of function module from list of available RFC functions in target R/3 (* convention is supported)
// - display R/3 system information (system id, version R/3, ....) after succesfull login
// - possibility to set import parameters and fill internal tables
// - generate fully functional PHP script calling of the function module
// - call selected RFC function and show results
function print_header ($title)
{
echo "
<html>
<head>
<title>$title</title>
<style type=\"text/css\">
body { background-color: #FFFFFF; font-family : Verdana, Geneva, Arial, Helvetica, sans-serif; font-size : 10pt; }
td {font-family : Verdana, Geneva, Arial, Helvetica, sans-serif; font-size : 10pt; }
.text1 { color: #0000A0; font-size : 14pt;}
.text2 { color: Red; font-size : 9pt;}
.text3 { font-size : 8pt;}
.text4 { color: Red; }
.td1 {font-size : 12pt; background-color : #0000a0; color: #ffffff;}
.td2 {font-size: 10pt; background-color : #FFFFFF;}
.td3 {font-size : 9pt; background-color : #9FC1EA; color:#0000a0;}
.td4 {font-size : 12pt; background-color : red; color:#ffffff;}
.td5 {font-size: 10pt; background-color : #ffffff;}
.td6 {font-size : 8pt; background-color : #ffa0a0; color:#000000;}
.table1 {background-color : #000000; }
.table2 {background-color : Red; }
</style>
</head>
<body>
<span class=\"text1\">
<b><a href=\"http://saprfc.sourceforge.net\">SAPRFC</a> test script</b><br>
</span>
<span class=\"text2\">
<b>extension module for PHP 4 and PHP 5</b><br><br><br>
</span>
";
}
function print_footer ()
{
global $RFC_SYSTEM_INFO;
if ($RFC_SYSTEM_INFO != "")
echo ("<p align='center' class=\"text3\">$RFC_SYSTEM_INFO\n");
echo "</body></html>";
}
function print_window ($title,$content,$bottom)
{
echo ("<table class='table1' align='center' border=0 cellpadding=0 cellspacing=0 width=60%>\n");
echo ("<tr><td><table border=0 cellpadding=3 cellspacing=1 width=100%>\n");
echo ("<tr><td class=\"td1\">\n");
echo ("<span align=center><b>$title</b></td></tr>\n");
echo ("<tr><td class=\"td2\">\n");
echo ("$content</td></tr>\n");
if ($bottom)
{
echo ("<tr><td class=\"td3\">\n");
echo ("$bottom</td></tr>\n");
}
echo ("</table></td></tr></table>\n");
}
function print_login_form ($ASHOST, $SYSNR, $CLIENT, $USER, $PASSWD, $GWHOST,
$GWSERV, $MSHOST, $R3NAME, $GROUP,
$LANG, $TRACE, $LCHECK, $CODEPAGE)
{
$form = "<form name=login method=post action=saprfc_test.php>\n";
$form .= "<input type=hidden name=p value=login>\n";
$form .= "<table>\n";
$form .= "<tr><td><b>Application server:</b></td><td><input type=text size=30 name=ASHOST value=\"$ASHOST\"></td></tr>\n";
$form .= "<tr><td><b>System number:</b></td><td><input type=text size=2 name=SYSNR value=\"$SYSNR\"></td></tr>\n";
$form .= "<tr><td><b>Client:</b></td><td><input type=text size=3 name=CLIENT value=\"$CLIENT\"></td></tr>\n";
$form .= "<tr><td><b>User:</b></td><td><input type=text size=12 name=USER value=\"$USER\"></td></tr>\n";
$form .= "<tr><td><b>Password:</b></td><td><input type=password size=12 name=PASSWD value=\"$PASSWD\"></td></tr>\n";
$form .= "<tr><td><b>Message server:</b></td><td><input type=text size=30 name=MSHOST value=\"$MSHOST\"></td></tr>\n";
$form .= "<tr><td><b>R/3 system name:</b></td><td><input type=text size=3 name=R3NAME value=\"$R3NAME\"></td></tr>\n";
$form .= "<tr><td><b>Logon group:</b></td><td><input type=text size=10 name=GROUP value=\"$GROUP\"></td></tr>\n";
$form .= "<tr><td><b>Language:</b></td><td><input type=text size=2 name=LANG value=\"$LANG\"></td></tr>\n";
$checked = ($TRACE) ? "checked" : "";
$form .= "<tr><td colspan=2><input type=checkbox name=TRACE value=1 $checked> trace (RFC debug info to syslog)</td></tr>\n";
$checked = ($LCHECK) ? "checked" : "";
$form .= "<tr><td colspan=2><input type=checkbox name=LCHECK value=1 $checked> authenticate user login data (if not set, the RFC connection is opened only)</td></tr>\n";
$form .= "<tr><td><b>SAP Codepage:</b></td><td><input type=text size=4 name=CODEPAGE value=\"$CODEPAGE\"></td></tr>\n";
$form .= "<tr><td colspan=2 align=center><input type=submit value=\"Login\"> <input type=reset value=\"Reset\"></td></tr>\n";
$form .= "</table></form>\n";
$form .= "<p align=center><span class=\"text4\"><b>Warning: The password is send as the clear text !!! <br>Don't use this script in the Internet environment. <br>Don't login to the productive system. </b></span>\n";
print_header ("Login to SAP R/3");
print_window ("Login to SAP R/3",$form,"");
print_footer ();
}
function print_select_form ($function, $last_entries, $functions_list)
{
$form = "<form name=select method=post action=saprfc_test.php>\n";
$form .= "<input type=hidden name=p value=select_do>\n";
$form .= "<table>\n";
$form .= "<tr><td><b>Function module:</b></td><td><input type=text size=30 name=function value=\"$function\"></td></tr>\n";
if (is_array ($last_entries))
{
$form .= "<tr><td colspan=2>or\n";
$form .= "<tr><td><b>Last entries:</b></td><td><select size=1 name=entry>\n";
$form .= "<option value = \"\"></option>\n";
for ($i=0;$i<count($last_entries);$i++)
$form .= "<option value = \"".$last_entries[$i]."\">".$last_entries[$i]."</option>\n";
$form .= "</select></td></tr>\n";
}
if (is_array ($functions_list))
{
$form .= "<tr><td colspan=2>or\n";
$form .= "<tr><td><b>List of functions:</b></td><td><select size=1 name=list>\n";
$form .= "<option value = \"\"></option>\n";
for ($i=0;$i<count($functions_list);$i++)
$form .= "<option value = \"".$functions_list[$i]."\">".$functions_list[$i]."</option>\n";
$form .= "</select></td></tr>\n";
}
$form .= "<tr><td colspan=2 align=center><input type=submit value=\"Select function module\"> <input type=reset value=\"Reset\"></td></tr>\n";
$form .= "</table></form>\n";
print_header ("Selection of function module");
print_window ("Selection of function module",$form,"<a href=\"saprfc_test.php?p=newlogin\">New login</a>");
print_footer ();
}
function print_error_message ($title, $message, $debug)
{
print_header ($title);
echo ("<table align='center' class='table2' border=0 cellpadding=0 cellspacing=0 width=60%>\n");
echo ("<tr><td><table border=0 cellpadding=3 cellspacing=1 width=100%>\n");
echo ("<tr><td class=\"td4\">\n");
echo ("<span align='center'><b>$title</b></td></tr>\n");
echo ("<tr><td class=\"td5\">\n");
echo ("<b>Message: </b>$message</td></tr>\n");
echo ("<tr><td class=\"td6\">\n");
echo ("$debug</td></tr>");
echo ("</table></td></tr></table>");
print_footer ();
}
function show_parameter ($name,$optional, $def, $value)
{
if ( $def[0]["name"] == "" )
{
$form = sprintf ("<tr><td colspan=2><b>%s</b> <input name=\"RFC_%s\" type=text size=%d value=\"%s\">",$name,$name,$def[0]["len"],$value);
$form .= sprintf (" %s(%d)</td></tr>\n",$def[0]["abap"],$def[0]["len"]);
}
else
{
$form = "<tr><td valign=top><b>$name</b></td><td>\n<table colspacing=0 colpadding=0>\n<tr>";
for ($i=0;$i<count($def);$i++)
$form .= sprintf ("<td align=center>%s<br>%s(%d)</td>\n",$def[$i]["name"],$def[$i]["abap"],$def[$i]["len"]);
$form .= "</tr>\n<tr>\n";
for ($i=0;$i<count($def);$i++)
{
$size = $def[$i]["len"] < 80 ? $def[$i]["len"] : 80;
$form .= sprintf ("<td align=center><input name=\"RFC_%s[%s]\" type=text size=%d value=\"%s\"></td>\n",$name,$def[$i]["name"],$size,$value[$i]);
}
$form .= "</tr>\n</table>\n</td></tr>\n";
}
return ($form);
}
function show_parameter_out ($name, $def, $value)
{
if ( $def[0]["name"] == "" )
{
$form = sprintf ("<tr><td><b>%s</b></td><td>%s</td></tr>\n",$name,$value);
}
else
{
$form = "<tr><td valign=top><b>$name</b></td>\n<td>\n<table colspacing=0 colpadding=0>\n<tr>\n";
for ($i=0;$i<count($def);$i++)
$form .= sprintf ("<td align=center>%s</td>\n",$def[$i]["name"]);
$form .= "</tr>\n<tr>\n";
for ($i=0;$i<count($def);$i++)
$form .= sprintf ("<td>%s</td>\n",$value[$def[$i]["name"]]);
$form .= "</tr>\n</table>\n</td></tr>\n";
}
return ($form);
}
function show_table ($name, $def, $value)
{
$form = "<tr><td colspan=2>\n<table colspacing=0 colpadding=0>\n<tr>\n";
for ($i=0;$i<count($def);$i++)
$form .= sprintf ("<td align=center>%s<br>%s(%d)</td>\n",$def[$i]["name"],$def[$i]["abap"],$def[$i]["len"]);
$form .= "</tr>\n";
$form .= sprintf ("<input type=hidden name=%s value=\"%s\">\n","RFC_TABLE_".$name,urlencode(serialize($value)));
for ($j=0;$j<count($value);$j++)
{
$form .= "<tr>\n";
for ($i=0;$i<count($def);$i++)
$form .= sprintf ("<td>%s</td>\n",$value[$j][$def[$i]["name"]]);
$form .= "</tr>\n";
}
for ($i=0;$i<count($def);$i++)
{
$size = $def[$i]["len"] < 80 ? $def[$i]["len"] : 80;
$form .= sprintf ("<td align=center><input name=\"RFC_%s[%s]\" type=text size=%d></td>\n",$name,$def[$i]["name"],$size);
}
$form .= "</tr></table></td></tr>\n";
$form .= "<tr><td><input type=submit name=action value=\"Append $name\"></td></tr>\n";
return ($form);
}
function show_table_out ($name, $def, $value)
{
$form = "<tr><td colspan=2>\n<table colspacing=0 colpadding=0>\n<tr>";
for ($i=0;$i<count($def);$i++)
$form .= sprintf ("<td colspan=2>%s</td>\n",$def[$i]["name"]);
$form .= "</tr>\n";
for ($j=0;$j<count($value);$j++)
{
$form .= "<tr>\n";
for ($i=0;$i<count($def);$i++)
$form .= sprintf ("<td colspan=2>%s</td>\n",$value[$j][$def[$i]["name"]]);
$form .= "</tr>\n";
}
$form .= "</table>\n</td></tr>\n";
return ($form);
}
//-----------------------------------------------------------------------------
// MAIN
//-----------------------------------------------------------------------------
if ( ini_get("register_globals") == false )
{
if ( isset ($_COOKIE) ) {
foreach ($_COOKIE as $var=>$val) {
$$var = $val;
}
} elseif ( isset ($HTTP_COOKIE_VARS) ) {
foreach ($HTTP_COOKIE_VARS as $var=>$val) {
$$var = $val;
}
}
if ( isset ($_POST) ) {
foreach ($_POST as $var=>$val) {
$$var = $val;
}
} elseif ( isset ($HTTP_POST_VARS) ) {
foreach ($HTTP_POST_VARS as $var=>$val) {
$$var = $val;
}
}
if ( isset ($_GET) ) {
foreach ($_GET as $var=>$val) {
$$var = $val;
}
} elseif ( isset ($HTTP_GET_VARS) ) {
foreach ($HTTP_GET_VARS as $var=>$val) {
$$var = $val;
}
}
}
if (! extension_loaded ("saprfc"))
{
print_error_message ( "SAPRFC extension not loaded",
"This script use SAPRFC extension module for PHP and the extension isn't loaded. You can download ".
"it with installation instructions from <a href=\"saprfc.sourceforge.net\">".
"http://saprfc.sourceforge.net</a>. If you have already the extension compiled and installed, check your ".
"php.ini configuration ",
""
);
exit;
}
if (isset($SAPRFC_LOGON)) // if cookie with login data, decode it
{
$SAPRFC_LOGON = urldecode($SAPRFC_LOGON);
$l = unserialize ($SAPRFC_LOGON); // l = array with login data (user,client, host....)
}
if (isset($SAPRFC_ENTRIES)) {
$SAPRFC_ENTRIES = urldecode($SAPRFC_ENTRIES); // cookie with last entries
$e = unserialize ($SAPRFC_ENTRIES); // e = array with last entries (selected funcions)
}
if (!isset ($p) ) $p="";
// if no cookie with login data exist or if newlogin request, show login page
if ( ! isset($SAPRFC_LOGON) && $p != "login" || $p == "newlogin" )
{
if (! isset($l["ASHOST"])) $l["ASHOST"] = "";
if (! isset($l["SYSNR"])) $l["SYSNR"] = "";
if (! isset($l["CLIENT"])) $l["CLIENT"] = "";
if (! isset($l["USER"])) $l["USER"] = "";
if (! isset($l["PASSWD"])) $l["PASSWD"] = "";
if (! isset($l["GWHOST"])) $l["GWHOST"] = "";
if (! isset($l["GWSERV"])) $l["GWSERV"] = "";
if (! isset($l["MSHOST"])) $l["MSHOST"] = "";
if (! isset($l["R3NAME"])) $l["R3NAME"] = "";
if (! isset($l["GROUP"])) $l["GROUP"] = "";
if (! isset($l["LANG"])) $l["LANG"] = "";
if (! isset($l["TRACE"])) $l["TRACE"] = "";
if (! isset($l["LCHECK"])) $l["LCHECK"] = "";
if (! isset($l["CODEPAGE"])) $l["CODEPAGE"] = "";
print_login_form ($l["ASHOST"], $l["SYSNR"], $l["CLIENT"], $l["USER"], $l["PASSWD"], $l["GWHOST"],
$l["GWSERV"], $l["MSHOST"], $l["R3NAME"], $l["GROUP"],
$l["LANG"], $l["TRACE"], $l["LCHECK"], $l["CODEPAGE"]);
exit;
}
else
{
if ( $p == "login" ) // process data from login page
{
if (isset($ASHOST)) $l["ASHOST"] = $ASHOST;
if (isset($SYSNR)) $l["SYSNR"] = $SYSNR;
if (isset($CLIENT)) $l["CLIENT"] = $CLIENT;
if (isset($USER)) $l["USER"] = $USER;
if (isset($PASSWD)) $l["PASSWD"] = $PASSWD;
if (isset($GWHOST)) $l["GWHOST"] = $GWHOST;
if (isset($GWSERV)) $l["GWSERV"] = $GWSERV;
if (isset($MSHOST)) $l["MSHOST"] = $MSHOST;
if (isset($R3NAME)) $l["R3NAME"] = $R3NAME;
if (isset($GROUP)) $l["GROUP"] = $GROUP;
if (isset($LANG)) $l["LANG"] = $LANG;
if (isset($TRACE)) $l["TRACE"] = $TRACE;
if (isset($LCHECK)) $l["LCHECK"] = $LCHECK;
if (isset($CODEPAGE)) $l["CODEPAGE"] = $CODEPAGE;
$SAPRFC_LOGON = serialize ($l); // set cookie variable
$p = "select"; // switch to selection of function module
}
}
// login data avaible, can open connection to SAP R/3
$rfc = @saprfc_open ($l);
if (! $rfc ) // if login failed, show error message
{
if (isset ($l["use_load_balancing"]))
print_error_message ( "Login error",
"Can't login to client ".$l["CLIENT"]." of the system ".$l["R3NAME"]." (Host: ".$l["MSHOST"].", Group: ".$l["GROUP"].") as user ".$l["USER"].". <a href=\"saprfc_test.php?p=newlogin\">New login</a>",
saprfc_error()
);
else
print_error_message ( "Login error",
"Can't login to client ".$l["CLIENT"]." and host ".$l["ASHOST"]." (System number: ".$l["SYSNR"].") as user ".$l["USER"].". <a href=\"saprfc_test.php?p=newlogin\">New login</a>",
saprfc_error()
);
exit;
}
// if SAP codepage set, set codepage for opened rfc connection
//if ($l[codepage]!="")
// saprfc_set_code_page ($rfc,$l[codepage]);
// save login data as cookie at the client side
setcookie ("SAPRFC_LOGON", urlencode($SAPRFC_LOGON),time()+7200);
// retrieve a system information from connected R/3
// use function module RFC_SYSTEM_INFO
// discover interface of function module and set resource $sysinfo_fce
$sysinfo_fce = @saprfc_function_discover ($rfc,"RFC_SYSTEM_INFO");
if ($sysinfo_fce)
{
// do RFC call to connected R/3 system
$retval = @saprfc_call_and_receive ($sysinfo_fce);
if ($retval == SAPRFC_OK)
{
// retrieve export (output) parametr RFCSI_EXPORT
$sysinfo = saprfc_export ($sysinfo_fce,"RFCSI_EXPORT");
$RFC_SYSTEM_INFO = sprintf ("system id: %s (%s), client=%03d, user=%s, application server=%s (%s,%s,%s), database=%s (%s)",
$sysinfo["RFCSYSID"],$sysinfo["RFCSAPRL"],$l["CLIENT"],$l["USER"],$sysinfo["RFCHOST"], $sysinfo["RFCOPSYS"],
$sysinfo["RFCIPADDR"],$sysinfo["RFCKERNRL"], $sysinfo["RFCDBHOST"], $sysinfo["RFCDBSYS"] );
}
// free allocated resources
@saprfc_function_free ($sysinfo_fce);
}
if ( $p == "select_do" ) // action for selection of function module page
{
if (isset ($list) && $list != "") // if function from function list selected, get it
{
$function = $list;
$p = "input";
}
elseif (isset ($entry) && $entry != "") // if function from last entries selected, get it
{
$function = $entry;
$p = "input";
}
else
{ // other, use function module RFC_FUNCTION_SEARCH to
// get a list of RFC functions of target R/3
$search_fce = saprfc_function_discover ($rfc,"RFC_FUNCTION_SEARCH");
if ($search_fce)
{
saprfc_import ($search_fce,"FUNCNAME",$function); // you can use asterix *, e.g. RFC_*
saprfc_optional ($search_fce,"GROUPNAME",1); // set optional parameters, the default values will be used
saprfc_optional ($search_fce,"LANGUAGE",1);
saprfc_table_init ($search_fce,"FUNCTIONS"); // init internal table named FUNCTIONS, equivalent of abap
// commands: refresh functions; clear functions;
$retval = @saprfc_call_and_receive ($search_fce); // rfc call
$numrows = saprfc_table_rows ($search_fce,"FUNCTIONS"); // get number of rows internal table
if ($numrows == 0) // no function found, show selection page again
$p = "select";
elseif ($numrows == 1)
{ // exactly one function found, show input page
$row = saprfc_table_read ($search_fce,"FUNCTIONS",1);
$function = $row["FUNCNAME"];
$p = "input";
}
else
{ // retrive list of founded function to array
for ($i=1;$i<=$numrows;$i++) // note: lines of internal tables are indexed from 1, (e.g 1,2,3....)
{
$row = saprfc_table_read ($search_fce,"FUNCTIONS",$i);
$functions_list[$i-1] = $row["FUNCNAME"];
}
$p = "select"; // show selection page again
}
saprfc_function_free ($search_fce);
}
else // error, can't discover interface of RFC_FUNCTION_SEARCH
$p = "select";
}
}
if ( $p == "select" || $p == "") // selection of the function module page
{ // show also if have login data from cookie (p=="")
if (! isset ($function) ) $function="";
if (! isset ($e) ) $e="";
if (! isset ($functions_list) ) $functions_list="";
print_select_form ($function, $e, $functions_list);
}
if ( $p == "input" ) // input values for import parameters or internal tables
{ // of function module $function
if (!isset ($action) ) $action = "";
if ($action == "") // if enter from selection page, set last entries cookie
{
if (! isset ($e) || ! is_array ($e)) $e = array();
$t_e = $e;
unset ($e);
$e[0] = strtoupper($function);
foreach ($t_e as $t_func) {
if (strtoupper($function) != $t_func) $e[] = $t_func;
}
unset ($t_e);
$SAPRFC_ENTRIES=serialize($e);
setcookie ("SAPRFC_ENTRIES", urlencode($SAPRFC_ENTRIES),time()+3600);
}
$fce = @saprfc_function_discover ($rfc,$function); // discover interface of function module $function
if (! $fce )
{
print_error_message ( "Discovering function module error",
"Can't discover interface of function module <b>$function</b>. Please check if the module is Remote-enable (transaction SE37, Atrributes).<a href=\"saprfc_test.php?p=select\">Other function module</a>",
saprfc_error()
);
exit;
}
$def = @saprfc_function_interface($fce); // retrieve definition of interface in array $def
// $def has following format:
/* $def = array (
array ("name"=>"PARAMETER", // name of import, export parameter or internal table
"type"=>"IMPORT", // type: IMPORT, EXPORT, TABLE
"optional"=>0 // 1 - optional parameter
"def"=> typedef // type definition (elementary SAP data type or structure)
),
............
)
typedef = array (
array ("name"=>"item1" "abap"=>"C", "len"=>30, "dec"=>0),
array ("name"=>"item2" "abap"=>"I", "len"=>4, "dec"=>0),
.....
)
*/
if ($action == "Call $function") // switch to call/result page if Call button pressed
$p = "call";
elseif ($action == "Generate PHP")
{
Header ("Content-type: application/x-httpd-php-source\n");
Header ("Content-Disposition: inline; filename=${function}.php\n");
echo ("<?\n//RFC Call for $function\n");
echo ("//Generated by saprfc_test.php (http://saprfc.sourceforge.net)\n\n");
echo ("//Login to SAP R/3\n");
echo ("\$login = array (");
$first = true;
foreach ($l as $key => $value)
{
if ($value == "") continue;
if ($first)
{$first = false; echo ("\n");}
else
echo (",\n");
echo ("\t\t\t\"$key\"=>\"$value\"");
}
echo (");\n");
echo ("\$rfc = saprfc_open (\$login );\n");
echo ("if (! \$rfc ) { echo \"RFC connection failed\"; exit; }\n");
echo ("//Discover interface for function module $function\n");
echo ("\$fce = saprfc_function_discover(\$rfc,\"$function\");\n");
echo ("if (! \$fce ) { echo \"Discovering interface of function module failed\"; exit; }\n");
echo ("//It's possible to define interface manually. If you would like do it, uncomment following lines:\n");
echo ("/*\$def = array (\n");
for ($i=0;$i<count($def);$i++)
{
$interface = $def[$i];
echo (" \t\t\t array (\n");
echo (" \t\t\t\t \"name\"=>\"".$interface["name"]."\",\n");
echo (" \t\t\t\t \"type\"=>\"".$interface["type"]."\",\n");
echo (" \t\t\t\t \"optional\"=>\"".$interface["optional"]."\",\n");
echo (" \t\t\t\t \"def\"=> array (\n");
$typedef = $interface["def"];
for ($j=0; $j<count($typedef); $j++)
{
echo (" \t\t\t\t\t array (\"name\"=>\"".$typedef[$j]["name"]."\",\"abap\"=>\"".$typedef[$j]["abap"]."\",\"len\"=>".$typedef[$j]["len"].",\"dec\"=>".$typedef[$j]["dec"].",\"offset\"=>".$typedef[$j]["offset"].")");
if ($j < (count($typedef)-1) )
echo (",\n");
else
echo ("\n");
}
echo (" \t\t\t\t\t)\n");
echo (" \t\t\t)");
if ($i < (count($def)-1) )
echo (",\n");
else
echo ("\n");
}
echo (" \t\t); \n");
echo (" \$fce = saprfc_function_define(\$rfc,\"$function\",\$def);\n");
echo ("*/\n");
echo ("//Set import parameters. You can use function saprfc_optional() to mark parameter as optional.\n");
for ($i=0;$i<count($def);$i++)
{
$interface = $def[$i];
if ($interface["type"] == "IMPORT")
{
$var="RFC_".$interface["name"];
if (!isset ($$var) ) $$var="";
if (! is_array ($$var))
echo ("saprfc_import (\$fce,\"".$interface["name"]."\",\"".$$var."\");\n");
else
{
echo ("saprfc_import (\$fce,\"".$interface["name"]."\", array (");
$first = true;
foreach ($$var as $key => $value)
{
if ($first)
{$first = false;}
else
echo (",");
echo ("\"$key\"=>\"$value\"");
}
echo ("));\n");
}
}
}
echo ("//Fill internal tables\n");
for ($i=0;$i<count($def);$i++)
{
$interface = $def[$i];
if ($interface["type"] == "TABLE")
{
echo ("saprfc_table_init (\$fce,\"".$interface["name"]."\");\n");
$var="RFC_TABLE_".$interface["name"];
if (!isset ($$var) ) $$var="";
$vararray = unserialize (urldecode($$var));
if (is_array($vararray))
{
for ($j=0;$j<count($vararray);$j++)
{
echo ("saprfc_table_append (\$fce,\"".$interface["name"]."\", array (");
$first = true;
foreach ($vararray[$j] as $key => $value)
{
if ($first)
{$first = false;}
else
echo (",");
echo ("\"$key\"=>\"$value\"");
}
echo ("));\n");
}
}
}
}
echo ("//Do RFC call of function $function, for handling exceptions use saprfc_exception()\n");
echo ("\$rfc_rc = saprfc_call_and_receive (\$fce);\n");
echo ("if (\$rfc_rc != SAPRFC_OK) { if (\$rfc == SAPRFC_EXCEPTION ) echo (\"Exception raised: \".saprfc_exception(\$fce)); else echo (saprfc_error(\$fce)); exit; }\n");
echo ("//Retrieve export parameters\n");
for ($i=0;$i<count($def);$i++)
{
$interface = $def[$i];
if ($interface["type"] == "EXPORT")
{
$var="RFC_".$interface["name"];
if (!isset ($$var)) $$var = "";
echo ("\$".$interface["name"]." = saprfc_export (\$fce,\"".$interface["name"]."\");\n");
}
}
for ($i=0;$i<count($def);$i++)
{
$interface = $def[$i];
if ($interface["type"] == "TABLE")
{
echo ("\$rows = saprfc_table_rows (\$fce,\"".$interface[name]."\");\n");
echo ("for (\$i=1;\$i<=\$rows;\$i++)\n");
echo ("\t\$".$interface[name]."[] = saprfc_table_read (\$fce,\"".$interface[name]."\",\$i);\n");
}
}
echo ("//Debug info\n");
echo ("saprfc_function_debug_info(\$fce);\n");
echo ("saprfc_function_free(\$fce);\n");
echo ("saprfc_close(\$rfc);\n");
}
else
{
print_header ("Test of function module $function");
$form = "<form name=test method=post action=\"saprfc_test.php\">\n";
$form .= "<input name=p type=hidden value=input>\n";
$form .= "<input name=function type=hidden value=\"$function\">\n";
$form .= "<table colspacing=0 colpadding=0>\n";
$form .= "<tr bgcolor=#D0D0D0><td colspan=2><b>IMPORT PARAMETERS</b></td></tr>\n";
for ($i=0;$i<count($def);$i++)
{
$interface = $def[$i];
if ($interface["type"] == "IMPORT") // if import parameter
{
$var="RFC_".$interface["name"]; // prefix RFC_
if (!isset ($$var) ) $$var="";
$form .= show_parameter ($interface["name"],$interface["optional"],$interface["def"],$$var);
}
}
for ($i=0;$i<count($def);$i++)
{
$interface = $def[$i];
if ($interface["type"] == "TABLE") // if internal table
{
$form .= "<tr bgcolor=#D0D0D0><td colspan=2><b>TABLE ".$interface["name"]."</b></td></tr>\n";
$var="RFC_TABLE_".$interface["name"];
if ( !isset ($$var) ) $$var="";
$vararray = unserialize (urldecode($$var)); // get saved array of values from RFC_TABLE_*
$var="RFC_".$interface["name"];
if ($action=="Append ".$interface["name"]) // if Append button pressed, add new line to internal table (array)
$vararray[]=$$var;
$form .= show_table ($interface["name"],$interface["def"],$vararray);
}
}
$form .= "<tr><td colspan=2><input type=submit name=action value=\"Call $function\">\n";
$form .="<input type=submit name=action value=\"Generate PHP\"></td></tr>\n";
$form .= "</table></form>\n";
print_window ("Function module $function",$form,"<a href=\"javascript:history.back()\">Back</a> <a href=\"saprfc_test.php?p=newlogin\">New login</a> <a href=\"saprfc_test.php?p=select\">Select other function</a>");
print_footer();
}
}
if ( $p == "call" ) // set input parameters and tables, call function module, show results
{
for ($i=0;$i<count($def);$i++)
{
$interface = $def[$i];
if ($interface["type"] == "IMPORT")
{
$var="RFC_".$interface["name"];
saprfc_import ($fce,$interface["name"],$$var); // set import parameters
}
if ($interface["type"] == "TABLE")
{
saprfc_table_init ($fce,$interface["name"]);
$var="RFC_TABLE_".$interface["name"];
$vararray = unserialize (urldecode($$var));
if (is_array($vararray))
{
for ($j=0;$j<count($vararray);$j++)
saprfc_table_append ($fce,$interface["name"],$vararray[$j]);
}
}
}
// rfc call function in connected R/3
$retval = @saprfc_call_and_receive ($fce);
if ( $retval != SAPRFC_OK )
{
print_error_message ( "Call error",
"Calling of function module $function failed. See the error message below.<a href=\"saprfc_test.php?p=select\">Other function module</a>",
saprfc_error()
);
exit;
}
print_header ("Results of function module $function");
$form = "<table colspacing=0 colpadding=0>\n";
$form .= "<tr bgcolor=#D0D0D0><td colspan=2><b>EXPORT PARAMETERS</b></td></tr>\n";
for ($i=0;$i<count($def);$i++)
{
$interface = $def[$i];
if ($interface["type"] == "EXPORT") // show export parameters
{
$var = saprfc_export ($fce,$interface["name"]);
$form .= show_parameter_out ($interface["name"],$interface["def"],$var);
}
}
for ($i=0;$i<count($def);$i++)
{
$interface = $def[$i];
if ($interface["type"] == "TABLE") // show content of internal tables
{
$form .= "<tr bgcolor=#D0D0D0><td colspan=2><b>TABLE ".$interface["name"]."</b></td></tr>\n";
unset ($vararray);
$rows = saprfc_table_rows ($fce,$interface["name"]);
for ($j=1;$j<=$rows;$j++)
$vararray[] = saprfc_table_read($fce,$interface["name"],$j);
$form .= show_table_out ($interface["name"],$interface["def"],$vararray);
}
}
$form .= "</table>";
print_window ("Results of function module $function",$form,"<a href=\"javascript:history.back()\">Back</a> <a href=\"saprfc_test.php?p=newlogin\">New login</a> <a href=\"saprfc_test.php?p=select\">Select other function</a>");
print_footer();
}
// free resources and close rfc connection
@saprfc_function_free($fce);
@saprfc_close($rfc);
?>