Location: PHPKode > scripts > SAPConnect > sapconnect/SAPConnect.php
<?


define("AS_ERROR",1);
define("AS_WARNING",2);
define("AS_OK",3);

class SAPConnect
{
  var $Rfc;
  var $Login;
  var $CallFuncName;
  var $FCall;
  var $ImportCount;
  var $Import;
  var $RetVal;
  var $InterFace;
  var $ExportCount;
  var $Export;
  var $TabCount;
  var $TabNames;
  var $TabData;
  var $TabRows;
  var $TabCols;
  var $ConnectInfo;
  function SAPConnect($connect_include)
  {
    if (file_exists($connect_include))
    {
      include_once $connect_include;
    }
    else
    {
      $this->DrawMessage("Connect to SAP failed. File $connect_include not found");
      return;
    }
    global $ASHOST,$SYSNR,$CLIENT,$USER,$PASSWD,$GWHOST,$GWSERV,$MSHOST,$R3NAME,$GROUP,$LANG,$TRACE,$LCEHCK,$CODEPAGE;

    $this->Login["ASHOST"]                   = $ASHOST;
    $this->Login["SYSNR"]                    = $SYSNR;
    $this->Login["CLIENT"]                   = $CLIENT;
    $this->Login["USER"]                     = $USER;
    $this->Login["PASSWD"]                   = $PASSWD;
    $this->Login["GWHOST"]                   = $GWHOST;
    $this->Login["GWSERV"]                   = $GWSERV;
    $this->Login["MSHOST"]                   = $MSHOST;
    $this->Login["R3NAME"]                   = $R3NAME;
    $this->Login["GROUP"]                    = $GROUP;
    $this->Login["LANGUAGE"]                 = $LANG;
    $this->Login["TRACE"]                    = $TRACE;
    $this->Login["LCHECK"]                   = $LCHECK;
    $this->Login["CODEPAGE"]                 = $CODEPAGE;

    $this->Rfc = @saprfc_open ($this->Login);

    if (! $this->Rfc )                    // if login failed, show error message
    {
     if ($this->Login["use_load_balancing"])
          $this->DrawMessage( "Login error:".
                            "Can't login to client ".$this->Login[CLIENT]." of the system ".$this->Login[R3NAME]." (Host: ".$this->Login[MSHOST].", Group: ".$this->Login[GROUP].") as user ".$this->Login[USER].
                            saprfc_error());
          else
          $this->DrawMessage("<P>Login error:<BR>".
                                "Can't login to client ".$this->Login["CLIENT"]." and host ".$this->Login["ASHOST"]." (System number: ".$this->Login["SYSNR"].") as user ".$this->Login["USER"]."<BR>".
                                saprfc_error()."</P>");
       return;
   }
   $this->ConnectInfo = saprfc_attributes($this->Rfc);
   }

  function DrawMessage($message,$type = AS_ERROR)
  {
    switch($type)
    {
      case(AS_ERROR)   : echo "<B><FONT COLOR=DARKRED>Error : ".$message."</FONT><BR>"; break;
      case(AS_WARNING) : echo "<B><FONT COLOR=NAVY>Warning : ".$message."</FONT><BR>"; break;
      case(AS_OK)      : echo "<B><FONT COLOR=DARKGREEN>".$message."</FONT><BR>"; break;
    }
  }

  function OpenFunction($funcname,$check=0)
  {
    $this->CallFuncName = strtoupper($funcname);
  }

  function AddImport($parameter,$value)
  {
      $this->ImportCount ++;
      $this->Import[$this->ImportCount]["PARAMETER"] = strtoupper($parameter);
      $this->Import[$this->ImportCount]["VALUE"]     = $value;
  }

  function Run()
  {
    if (!$this->Rfc) return;
    if ($this->CallFuncName)
    {
       $this->FCall = @saprfc_function_discover ($this->Rfc,$this->CallFuncName);
       for ($i=1;$i<=$this->ImportCount;$i++)
           saprfc_import ($this->FCall,$this->Import[$i]["PARAMETER"],$this->Import[$i]["VALUE"]);
       $this->RetVal = @saprfc_call_and_receive ($this->FCall);
       $this->InterFace = @saprfc_function_interface($this->FCall);
       $this->FillExport();
    }
  }

  function FillExport()
  {
      for ($i=0;$i<count($this->InterFace);$i++)
      {
         $interface = $this->InterFace[$i];
         if ($interface[type] == "EXPORT")
         {
            $this->ExportCount++;
            $this->Export[$this->ExportCount]["TYPE"] = "EXPORT";
            $this->Export[$this->ExportCount]["NAME"]  = $interface["name"];
            $var = saprfc_export ($this->FCall,$interface["name"]);
            $this->Export[$this->ExportCount]["VALUE"] = $var;
         }
         if ($interface[type] == "TABLE")
         {
            $this->TabCount++;
            $this->TabName[$this->TabCount] =  $interface[name];
            $this->TabRows[$this->TabCount] = saprfc_table_rows ($this->FCall,$interface["name"]);
            for ($j=1;$j<=$this->TabRows[$this->TabCount];$j++)
                $this->TabData[$this->TabCount][] = saprfc_table_read($this->FCall,$interface[name],$j);
           for ($i=0;$i<count($interface[def]);$i++)
                $this->TabCols[$this->TabCount][$i+1] = $interface[def][$i][name];
         }

      }
  }


  function GetTableColumns($tabname="")
  {
        for ($i=1;$i<=$this->TabCount;$i++)
        if ($this->TabName[$i]==$tabname || $this->TabCount==1)
          for ($j=0;$j<count($this->TabCols[$i]);$j++) $S = $S.($S?",,":"").$this->TabCols[$i][$j];
        return $S;
  }


  function GetValue($name)
  {
    for ($i=1;$i<=$this->ExportCount;$i++)
       if ($this->Export[$i]["NAME"] == strtoupper($name)) return $this->Export[$i]["VALUE"];
  }

  function GetTabValue($tab,$col,$row)
  {

      $NAME = $this->TabCols[$tab][$row+1];
      return $this->TabData[$tab][$col][$NAME];
  }

  function GetTabValue1($tab,$col,$NAME)
  {
      return $this->TabData[$tab][$col][$NAME];
  }


  function Attr($attr)
  {
    echo "<TR><TD>".str_replace("_"," ",strtoupper($attr))."</TD><TD>".$this->ConnectInfo[$attr]."</TD></TR>";
  }

  function PrintAttributes()
  {
    echo "<TABLE BORDER=1>";
    $this->Attr("own_host");
    $this->Attr("partner_host");
    $this->Attr("sysid");
    $this->Attr("user");
    $this->Attr("language");
    $this->Attr("trace");
    $this->Attr("own_codepage");
    $this->Attr("partner_codepage");
    $this->Attr("rfc_role");
    $this->Attr("own_type");
    $this->Attr("own_rel");
    $this->Attr("partner_type");
    $this->Attr("partner_rel");
    $this->Attr("kernel_rel");
    $this->Attr("CPIC_convid");
    echo "</TABLE>";
  }

  function Close()
  {
    @saprfc_function_free($this->FCall);
    @saprfc_close($this->Rfc);

  }

}

?>
Return current item: SAPConnect