
<?php
$QueryString = preg_replace("/&/", "?", $_SERVER['QUERY_STRING'], 1);
$File = fopen("http://localhost:8888/$QueryString", "r");
if($File <> false) {
while (!feof($File)) {
$Line = fread($File, 4096);
echo $Line;
}
fclose($File);
}
?>
#-Begin-----------------------------------------------------------------
#-Global variables----------------------------------------------------
$url = "http://localhost:8888/" #Listening URL
$CrLf = "`r`n"
#-Here-Strings--------------------------------------------------------
$error_htm = @"
<!DOCTYPE HTML>
<html>
<head>
<title>Error site</title>
</head>
<body>
An error occured
</body>
</html>
"@
$index_htm = @"
<!DOCTYPE HTML>
<html>
<head>
<title>Index site</title>
</head>
<body>
WebServer example
</body>
</html>
"@
#-Sub Load-NCo--------------------------------------------------------
Function Load-NCo() {
$ScriptDir = $PSScriptRoot
If ([Environment]::Is64BitProcess) {
$Path = $ScriptDir + "\x64\"
}
Else {
$Path = $ScriptDir + "\x86\"
}
$File = $Path + "sapnco.dll"; Add-Type -Path $File
$File = $Path + "sapnco_utils.dll"; Add-Type -Path $File
}
#-Function Get-Destination--------------------------------------------
Function Get-Destination() {
#-Connection parameters-------------------------------------------
$cfgParams = New-Object SAP.Middleware.Connector.RfcConfigParameters
$cfgParams.Add("NAME", "TEST")
$cfgParams.Add("ASHOST", "ABAP702")
$cfgParams.Add("SYSNR", "00")
$cfgParams.Add("CLIENT", "001")
$cfgParams.Add("USER", "BCUSER")
$cfgParams.Add("PASSWD", "minisap")
Return [SAP.Middleware.Connector.RfcDestinationManager]::GetDestination($cfgParams)
}
#-Function Get-SystemInfo---------------------------------------------
Function Get-SystemInfo() {
$destination = Get-Destination
#-Metadata--------------------------------------------------------
$rfcFunction = `
$destination.Repository.CreateFunction("RFC_SYSTEM_INFO")
#-Call function module--------------------------------------------
Try {
$rfcFunction.Invoke($destination)
$Export = $rfcFunction.GetStructure("RFCSI_EXPORT")
#-Get information---------------------------------------------
$strRet = $CrLf + "Host: " + `
$Export.GetValue("RFCHOST") + "<br />"
$strRet = $strRet + $CrLf + "SysID: " + `
$Export.GetValue("RFCSYSID") + "<br />"
$strRet = $strRet + $CrLf + "DBHost: " + `
$Export.GetValue("RFCDBHOST") + "<br />"
$strRet = $strRet + $CrLf + "DBSys: " + `
$Export.GetValue("RFCDBSYS") + "<br />"
$strRet = $strRet + $CrLf
}
Catch {
Write-Host "Exception occured:`r`n" $_.Exception.Message
Return "<strong>Exception occured</strong><br />" + `
$_.Exception.Message
}
Return $strRet
}
#-Function Read-Table-------------------------------------------------
Function Read-Table([String]$TableName, [Int]$RowCount) {
$destination = Get-Destination
#-Metadata--------------------------------------------------------
$rfcFunction = `
$destination.Repository.CreateFunction("RFC_READ_TABLE")
#-Call function module--------------------------------------------
Try {
$rfcFunction.SetValue("QUERY_TABLE", $TableName)
$rfcFunction.SetValue("DELIMITER", "~")
$rfcFunction.SetValue("ROWCOUNT", $RowCount)
$rfcFunction.Invoke($destination)
$Header = New-Object System.Collections.ArrayList
#-Get field names for the CSV header--------------------------
[SAP.Middleware.Connector.IRfcTable]$Fields = `
$rfcFunction.GetTable("FIELDS")
ForEach ($Field In $Fields) {
[Void]$Header.Add($Field.GetValue("FIELDNAME"))
}
#-Get table data line by line for CSV-------------------------
[SAP.Middleware.Connector.IRfcTable]$Table = `
$rfcFunction.GetTable("DATA")
ForEach ($Line In $Table) {
$CSV = $CSV + $Line.GetValue("WA") + "`r`n"
}
#-Convert CSV to JSON-----------------------------------------
$JSON = $CSV | ConvertFrom-Csv -Delimiter "~" `
-Header $Header | ConvertTo-Json
Return $JSON
}
Catch {
Write-Host "Exception occured:`r`n" $_.Exception.Message
Return "<strong>Exception occured</strong><br />" + `
$_.Exception.Message
}
}
#-Function Head-------------------------------------------------------
Function Head([String] $Title) {
Return "<!DOCTYPE HTML>$($CrLf)<html>$($CrLf) <head>$($CrLf)" + `
" <title>" + $Title + "</title>$($CrLf) </head>$($CrLf)" + `
" <body>"
}
#-Function Tail-------------------------------------------------------
Function Tail() {
Return " </body>$($CrLf)</html>"
}
#-Sub Main------------------------------------------------------------
Function Start-WebServer() {
$listener = new-object system.net.httplistener
$listener.prefixes.add($url)
$listener.start()
if ($Host.Name -eq "ConsoleHost") {
[console]::TreatControlCAsInput = $true
write-host "Press any key to end after the next incoming request"
}
$Continue = $True
while ($Continue) {
if ($Host.Name -eq "ConsoleHost") {
if ([console]::KeyAvailable) {
$Continue = $False
}
}
$context = $listener.getcontext()
$request = $context.request
$response = $context.response
switch ($request.httpmethod.toUpper()) {
#-Create------------------------------------------------------
"POST" {
switch ($request.url.localpath) {
Default {
$content = $error_htm
$response.StatusCode = 404
}
}
}
#-Read--------------------------------------------------------
"GET" {
switch ($request.url.localpath) {
#---------------------------------------------------------
#-
#- Call
#- http://localhost/001sap.php?systeminfo
#- or
#- http://localhost:8888/systeminfo
#-
#---------------------------------------------------------
"/systeminfo" {
$content = $(Head("System Information")) + `
$(Get-SystemInfo) + $(Tail)
$response.StatusCode = 200
}
#---------------------------------------------------------
#-
#- Call
#- http://localhost/001sap.php?readtable&query_table=USR01&rowcount=3
#- or
#- http://localhost:8888/readtable?query_table=usr05&rowcount=2
#- The first parameter is the table name and the second
#- parameter is the number of rowcount
#-
#---------------------------------------------------------
"/readtable" {
$QueryTable = $request.querystring["query_table"].ToUpper()
$RowCount = $request.querystring["rowcount"]
if ($RowCount -notmatch "^[\d\.]+$") {
$RowCount = 0
}
$content = $(Read-Table $QueryTable $RowCount)
$response.StatusCode = 200
}
Default {
$content = $index_htm
$response.StatusCode = 200
}
}
}
#-Update (Replace/Modify)-------------------------------------
{"PUT", "PATCH" -Contains $_} {
switch ($request.url.localpath) {
Default {
$content = $error_htm
$response.StatusCode = 404
}
}
}
#-Delete------------------------------------------------------
"DELETE" {
switch ($request.url.localpath) {
Default {
$content = $error_htm
$response.StatusCode = 404
}
}
}
#-Other methods are not supported-----------------------------
Default {
$content = $error_htm
$response.StatusCode = 404
}
}
$buffer = [System.Text.Encoding]::UTF8.GetBytes($content)
$response.contentlength64 = $buffer.length
$response.outputstream.write($buffer, 0, $buffer.length)
$response.close()
}
$listener.stop()
}
#-Sub Main------------------------------------------------------------
Function Main() {
If ($PSVersionTable.PSVersion.Major -ge 5) {
Load-NCo
Start-WebServer
}
}
#-Main----------------------------------------------------------------
Main
#-End-------------------------------------------------------------------
<!DOCTYPE html>
<html>
<head>
<title>
SAP.php
</title>
</head>
<body>
<?php
$Result = "";
if (isset($_POST["ashost"])) { $ashost = $_POST["ashost"]; }
if (isset($_POST["sysnr"])) { $sysnr = $_POST["sysnr"]; }
if (isset($_POST["client"])) { $client = $_POST["client"]; }
if (isset($_POST["user"])) { $user = $_POST["user"]; }
if (isset($_POST["passwd"])) { $passwd = $_POST["passwd"]; }
if (isset($ashost) && isset($sysnr) && isset($client) &&
isset($user) && isset($passwd) ) {
if($_POST["operation"] == "systeminfo") {
$QueryString = "systeminfo";
} elseif ($_POST["operation"] == "readtable") {
$QueryString = "readtable";
}
$QueryString = $QueryString . "?ASHOST=" . $ashost .
"&SYSNR=" . $sysnr . "&CLIENT=" . $client .
"&USER=" . $user . "&PASSWD=" . $passwd;
if ($_POST["operation"] == "readtable"){
$QueryString = $QueryString . "&QUERY_TABLE=" .
$_POST["tablename"] . "&ROWCOUNT=" . $_POST["rowcount"];
}
$File = fopen("http://localhost:8888/$QueryString", "r");
if($File <> false) {
while (!feof($File)) {
$Line = fread($File, 4096);
$Result = $Result . $Line;
}
fclose($File);
}
}
?>
<form method="post" action="002sap.php">
ASHOST <input type="text" name="ashost" value="ABAP702" /><br />
SYSNR <input type="text" name="sysnr" value="00" /><br />
CLIENT <input type="text" name="client" value="001" /><br />
USER <input type="text" name="user" value="BCUSER" /><br />
PASSWD <input type="password" name="passwd" value="minisap" /><br />
<br />
<input type="radio" name="operation" value="systeminfo" checked />systeminfo<br />
<input type="radio" name="operation" value="readtable" />readtable<br />
TableName <input type="text" name="tablename" value="SFLIGHT" /><br />
RowCount <input type="number" name="rowcount" value="3" /><br />
<br />
<input type="submit" name="submit" value="Execute" />
</form>
<br />
<textarea name="result" cols="80" rows="25" wrap="off"
style="overflow:scroll;"> <?php echo $Result; ?> </textarea>
</body>
</html>
#-Begin-----------------------------------------------------------------
#-Global variables----------------------------------------------------
$url = "http://localhost:8888/" #Listening URL
$CrLf = "`r`n"
#-Here-Strings--------------------------------------------------------
$error_htm = @"
<!DOCTYPE HTML>
<html>
<head>
<title>Error site</title>
</head>
<body>
An error occured
</body>
</html>
"@
$index_htm = @"
<!DOCTYPE HTML>
<html>
<head>
<title>Index site</title>
</head>
<body>
WebServer example
</body>
</html>
"@
#-Sub Load-NCo--------------------------------------------------------
Function Load-NCo() {
$ScriptDir = $PSScriptRoot
If ([Environment]::Is64BitProcess) {
$Path = $ScriptDir + "\x64\"
}
Else {
$Path = $ScriptDir + "\x86\"
}
$File = $Path + "sapnco.dll"; Add-Type -Path $File
$File = $Path + "sapnco_utils.dll"; Add-Type -Path $File
}
#-Function Get-Destination--------------------------------------------
Function Get-Destination([String]$ASHOST, [String]$SYSNR,
[String]$CLIENT, [String]$USER, [String]$PASSWD) {
#-Connection parameters-------------------------------------------
$cfgParams = New-Object SAP.Middleware.Connector.RfcConfigParameters
$cfgParams.Add("NAME", "TEST")
$cfgParams.Add("ASHOST", $ASHOST)
$cfgParams.Add("SYSNR", $SYSNR)
$cfgParams.Add("CLIENT", $CLIENT)
$cfgParams.Add("USER", $USER)
$cfgParams.Add("PASSWD", $PASSWD)
Return [SAP.Middleware.Connector.RfcDestinationManager]::GetDestination($cfgParams)
}
#-Function Get-SystemInfo---------------------------------------------
Function Get-SystemInfo([String]$ASHOST, [String]$SYSNR,
[String]$CLIENT, [String]$USER, [String]$PASSWD) {
$destination = Get-Destination $ASHOST $SYSNR $CLIENT $USER $PASSWD
#-Metadata--------------------------------------------------------
$rfcFunction = `
$destination.Repository.CreateFunction("RFC_SYSTEM_INFO")
#-Call function module--------------------------------------------
Try {
$rfcFunction.Invoke($destination)
$Export = $rfcFunction.GetStructure("RFCSI_EXPORT")
#-Get information---------------------------------------------
$strRet = $CrLf + "Host: " + `
$Export.GetValue("RFCHOST")
$strRet = $strRet + $CrLf + "SysID: " + `
$Export.GetValue("RFCSYSID")
$strRet = $strRet + $CrLf + "DBHost: " + `
$Export.GetValue("RFCDBHOST")
$strRet = $strRet + $CrLf + "DBSys: " + `
$Export.GetValue("RFCDBSYS")
$strRet = $strRet + $CrLf
}
Catch {
Write-Host "Exception occured:`r`n" $_.Exception.Message
Return "<strong>Exception occured</strong><br />" + `
$_.Exception.Message
}
Return $strRet
}
#-Function Read-Table-------------------------------------------------
Function Read-Table([String]$ASHOST, [String]$SYSNR, [String]$CLIENT,
[String]$USER, [String]$PASSWD, [String]$TableName, [Int]$RowCount) {
$destination = Get-Destination $ASHOST $SYSNR $CLIENT $USER $PASSWD
#-Metadata--------------------------------------------------------
$rfcFunction = `
$destination.Repository.CreateFunction("RFC_READ_TABLE")
#-Call function module--------------------------------------------
Try {
$rfcFunction.SetValue("QUERY_TABLE", $TableName)
$rfcFunction.SetValue("DELIMITER", "~")
$rfcFunction.SetValue("ROWCOUNT", $RowCount)
$rfcFunction.Invoke($destination)
$Header = New-Object System.Collections.ArrayList
#-Get field names for the CSV header--------------------------
[SAP.Middleware.Connector.IRfcTable]$Fields = `
$rfcFunction.GetTable("FIELDS")
ForEach ($Field In $Fields) {
[Void]$Header.Add($Field.GetValue("FIELDNAME"))
}
#-Get table data line by line for CSV-------------------------
[SAP.Middleware.Connector.IRfcTable]$Table = `
$rfcFunction.GetTable("DATA")
ForEach ($Line In $Table) {
$CSV = $CSV + $Line.GetValue("WA") + "`r`n"
}
#-Convert CSV to JSON-----------------------------------------
$JSON = $CSV | ConvertFrom-Csv -Delimiter "~" `
-Header $Header | ConvertTo-Json
Return $JSON
}
Catch {
Write-Host "Exception occured:`r`n" $_.Exception.Message
Return "<strong>Exception occured</strong><br />" + `
$_.Exception.Message
}
}
#-Sub Main------------------------------------------------------------
Function Start-WebServer() {
$listener = new-object system.net.httplistener
$listener.prefixes.add($url)
$listener.start()
if ($Host.Name -eq "ConsoleHost") {
[console]::TreatControlCAsInput = $true
write-host "Press any key to end after the next incoming request"
}
$Continue = $True
while ($Continue) {
if ($Host.Name -eq "ConsoleHost") {
if ([console]::KeyAvailable) {
$Continue = $False
}
}
$context = $listener.getcontext()
$request = $context.request
$response = $context.response
$ASHOST = $request.querystring["ASHOST"]
$SYSNR = $request.querystring["SYSNR"]
$CLIENT = $request.querystring["CLIENT"]
$USER = $request.querystring["USER"]
$PASSWD = $request.querystring["PASSWD"]
switch ($request.httpmethod.toUpper()) {
#-Create------------------------------------------------------
"POST" {
switch ($request.url.localpath) {
Default {
$content = $error_htm
$response.StatusCode = 404
}
}
}
#-Read--------------------------------------------------------
"GET" {
switch ($request.url.localpath) {
"/systeminfo" {
$content = $(Get-SystemInfo $ASHOST $SYSNR $CLIENT `
$USER $PASSWD)
$response.StatusCode = 200
}
"/readtable" {
$QueryTable = $request.querystring["QUERY_TABLE"].ToUpper()
$RowCount = $request.querystring["ROWCOUNT"]
if ($RowCount -notmatch "^[\d\.]+$") {
$RowCount = 0
}
$content = $(Read-Table $ASHOST $SYSNR $CLIENT $USER `
$PASSWD $QueryTable $RowCount)
$response.StatusCode = 200
}
Default {
$content = $index_htm
$response.StatusCode = 200
}
}
}
#-Update (Replace/Modify)-------------------------------------
{"PUT", "PATCH" -Contains $_} {
switch ($request.url.localpath) {
Default {
$content = $error_htm
$response.StatusCode = 404
}
}
}
#-Delete------------------------------------------------------
"DELETE" {
switch ($request.url.localpath) {
Default {
$content = $error_htm
$response.StatusCode = 404
}
}
}
#-Other methods are not supported-----------------------------
Default {
$content = $error_htm
$response.StatusCode = 404
}
}
$buffer = [System.Text.Encoding]::UTF8.GetBytes($content)
$response.contentlength64 = $buffer.length
$response.outputstream.write($buffer, 0, $buffer.length)
$response.close()
}
$listener.stop()
}
#-Sub Main------------------------------------------------------------
Function Main() {
If ($PSVersionTable.PSVersion.Major -ge 5) {
Load-NCo
Start-WebServer
}
}
#-Main----------------------------------------------------------------
Main
#-End-------------------------------------------------------------------
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
User | Count |
---|---|
2 | |
2 | |
2 | |
1 | |
1 | |
1 | |
1 | |
1 | |
1 |