Jump to content

[SCRIPT] phplib-mangosoap: Library/Class for MaNGOS SOAP interface


Guest fdb_

Recommended Posts

Hi everyone,

Here is my PHP interface for the MaNGOS SOAP connector.

First is a Python script to generate the commands list from the mangos.sql file.

Here is the source code:

#!/usr/bin/python

import re

cmdList = dict()

fd = open('mangos.sql', 'r')
flag_command_found = False
pattern = re.compile("^\\('(.*)',(.|'|)([0-9])(.|'|),
(.|)'(.*)'\\)[,;]$")
for line in fd:
   if flag_command_found == False:
       if re.search('^INSERT INTO `command` VALUES$', line):
           flag_command_found = True
       continue
   #print line
   tmp = re.search(pattern, line)
   if tmp == None or line == "":
       break
   info = tmp.group(6).replace('\\\\r', ' ')
   info = info.replace('\\\\n', '')
   info = info.replace('\\\\', '')
   info = info.replace('$', '\\$')
   info = info.replace('"', '\\\\"')
   cmdList[tmp.group(1)] = [tmp.group(3), info]

fd = open('phplib-mangosoap-commands.php', 'w')
fd.write("<?\\n");
fd.write("\\tunset($commands);\\n")
for cmd, [level, info] in cmdList.iteritems():
   fd.write("\\t$commands['" + str(cmd) + "'] = array(" + str(level) + ', "' + str(info) + '");\\n')
fd.write("?>\\n")
fd.close()

Now the main PHP class:

<?

class MaNGOSOAPClient
{
   private $username=null;
   private $password=null;
   private $url=null;
   private $commands = array();
   private $session = null;
   private $persistent = true;
   private $maxcmdsize = 4;
   private $error_msg = null;

   // Input: Command string
   // Return: Numeric means something wrong happened
   //         1 => Already connected
   //         2 => Missing config settings
   //         3 => Connection error
   //         4 => Command not found
   //         5 => Command failed
   //         String with the command result
   public function SendCommand($p_command)
   {
       // Check if command exists in our commands list
       $cmdArray = explode(' ', $p_command, $this->maxcmdsize);
       $cmd = array_shift($cmdArray);
       $count = 0;
       for ($i=0; $i<$this->maxcmdsize; $i++)
       {
           if(!$this->GetCommandInfo($cmd))
               $count++;
           $cmd .= ' '.array_shift($cmdArray);
       }

       // Command not found, return an error
       if ($count >= $this->maxcmdsize)
       {
           $this->error_msg = 'Command not found';
           return 4;
       }

       // Hoho somebody forget to connect before sending commands
       // lets connect
       if ($this->session == null && $this->persistent)
           if ($ret = $this->Connect())
               return $ret;

       if ($this->session != null && !$this->persistent)
           if ($ret = $this->Connect())
               return $ret;

       $result = null;
       try
       {
           $result = $this->session->executeCommand(new SoapParam($p_command, "command"));
       }
       catch (Exception $e)
       {
           $this->error_msg = "Command failed! Reason: ".$e->getMessage();
           return 5;
       }

       if ($this->session != null && $this->persistent)
           $this->Disconnect();

       return $result;
   }

   // Input: Nothing
   // Output: 0 => Connection OK
   //         1 => Already connected
   //         2 => Missing config settings
   //         3 => Connection error
   private function Connect()
   {
       if ($this->session != null)
       {
           $this->error_msg = 'Already connected';
           return 1;
       }
       if ($this->username == null || $this->password == null || $this->url == null)
       {
           $this->error_msg = 'Missing config settings';
           return 2;
       }
       $this->session = new SoapClient(null, array( 'location' => $this->url,
                                              'uri' => "urn:MaNGOS",
                                              'style' => SOAP_RPC,
                                              'login' => $this->username,
                                              'password' => $this->password
                                             )
                                );
   }

   // Input: Nothing
   private function Disconnect()
   {
       if ($this->session != null)
           $this->session = null;
   }

   // Input: Array of commands
   //        Sets the class commands list.
   public function SetCommandsList($p_cmdList)
   {
       $this->commands = $p_cmdList;
   }

   // Input: Username
   //        Sets the username.
   public function SetUsername($p_username)
   {
       $this->username = $p_username;
   }

   // Input: Password
   //        Sets the password.
   public function SetPassword($p_pwd)
   {
       $this->password = $p_pwd;
   }

   // Input: Url
   //        Sets the soap url.
   public function SetUrl($p_url)
   {
       $this->url = $p_url;
   }

   // Input: Nothing
   //        Return full commands list.
   public function GetCommandsList()
   {
       return $this->commands;
   }

   // Input: Command
   //        Return an array(command level, command help).
   public function GetCommandInfo($p_command)
   {
       $rett = null;
       if (array_key_exists($p_command, $this->commands))
           $rett = $this->commands[$p_command];
       return $rett;
   }

   // Input: Nothing
   //        Return the current username.
   public function GetUsername()
   {
       return $this->username;
   }

   // Input: Nothing
   //        Return the current password.
   public function GetPassword()
   {
       return $this->password;
   }

   // Input: Nothing
   //        Return the server URL.
   public function GetUrl()
   {
       return $this->url;
   }

   // Input: Nothing
   //        Return the last error message.
   public function GetErrorMsg()
   {
       return $this->error_msg;
   }

   // Input: true or false
   //        Sets the session persistence
   //  true: keep session up until the object is destroyed
   //  false: each send command will send a reconnect
   public function SetSessionPersistence($p_bool=true)
   {
       $this->persistent = $p_bool;
   }

}

?>

Finally the simple example:

<?

require_once('phplib-mangosoap-commands.php');
require_once('phplib-mangosoap.php');

// The 2 following lines are needed,
// first to create the object,
// the other to populate the commands inside the object
$msoap = new MaNGOSOAPClient();
$msoap->SetCommandsList($commands);

// Some examples there
print_r($msoap->GetCommandsList());
print_r($msoap->GetCommandInfo('learn'));

// SendCommand needs user/pwd and server url
$msoap->SetUsername('user');
$msoap->SetPassword('pwd');
$msoap->SetUrl('http://127.0.0.1:7878/');

// Send learn....
$result = $msoap->SendCommand('server info');
print_r($result);

// Send false command leard....
$result = $msoap->SendCommand('leard hello');
// Error check
if (is_numeric($result))
{
   // Print error code
   print_r($result);
   // Print error msg
   print_r($msoap->GetErrorMsg());
}

?>

Here are a link to download the full package including the generated commands list.

http://www.filedropper.com/phplib-mangosoaptar

I hope you enjoy it ;)

Link to comment
Share on other sites

  • 1 month later...
×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue. Privacy Policy Terms of Use