PHP Protocol API

Results 1 to 7 of 7
  1. #1
    Black Magic Development das7002 is offline
    MemberRank
    Apr 2010 Join Date
    EarthLocation
    2,188Posts

    PHP Protocol API

    I am considering writing a PHP API that uses the Protocol java classes.

    It would work like so,

    PHP Function is called in the API script
    Reformatting of the data to work properly in a URL string
    Gets pushed via curl (or fgets) to localhost:jsp server port
    JSP then takes over addressing the protocol classes and returns 1 for success or 0 for failure
    PHP takes response, if it receives a 0 notify user of failure, if 1 send success message

    Would that seem appealing to anyone?

    I was originally considering it as I prefer to code in PHP then in JSP and it would also include a similar function system in JSP as it would just be running though a case switch, but my primary goal is to easily allow PHP to function with it for better integration with other PHP webapps such as forums and the like.


  2. #2
    Account Upgraded | Title Enabled! ronny1982 is offline
    MemberRank
    Jan 2010 Join Date
    744Posts

    Re: PHP Protocol API

    i've started a php api, but dropped it... to much work, it's simpler to stick with jsp directly
    i haven't my api files anymore, so i can't share them...

    following information is from my memory

    API Interface
    • each php class is a rebuild of java class
    • data from php to java will be submitted via GET
    • data from java to php will be replied by the generated page
    • the reply from java first line is always the status
    • if the reply status is OK then each following line represent a single value (i.e. "role_count=12")
    • the values are read from the java response stream into the corresponding (instanciated) php class


    Security
    • to prevent injection of the GET interface we submit every time an additional session_key to java
    • the session_key is registered on both sides (php and java) by a login function wich requires the java_password
    • the session_key is transmitted on every request from php to jsp and each jsp script must check this key first
    • the key is deleted from both sides (php and java) by a logout function
    Last edited by ronny1982; 04-10-10 at 09:30 PM.

  3. #3
    Black Magic Development das7002 is offline
    MemberRank
    Apr 2010 Join Date
    EarthLocation
    2,188Posts

    Re: PHP Protocol API

    Quote Originally Posted by ronny1982 View Post
    i've started a php api, but dropped it... to much work, it's simpler to stick with jsp directly
    i haven't my api files anymore, so i can't share them...

    following information is from my memory

    API Interface
    • each php class is a rebuild of java class
    • data from php to java will be submitted via GET
    • data from java to php will be replied by the generated page
    • the reply from java first line is always the status
    • if the reply status is OK then each following line represent a single value (i.e. "role_count=12")
    • the values are read from the java response stream into the corresponding (instanciated) php class


    Security
    • to prevent injection of the GET interface we submit every time a session_key
    • the session_key is registered on both sides (php and java) by a login function wich requires the java_password
    • the session_key is transmitted on every request from php to jsp and each jsp script must check this key first
    • the key is deleted from both sides (php and java) by a logout function
    I wouldn't be rebuilding any of the Java classes into PHP, it just sends requests to the JSP server and then gets data off of it, and since the PHP part won't respond at all to request directly (it would just be a file with classes and functions) I don't think security needs to be laid on that heavily (if you keep the JSP server limited to localhost access only)

  4. #4
    Account Upgraded | Title Enabled! ronny1982 is offline
    MemberRank
    Jan 2010 Join Date
    744Posts

    Re: PHP Protocol API

    without rebuilding the java classes you have a large amount of traffic and power consumption

    i.e. rolebean class

    you need to rebuild this class in php, so that java fetch a rolebean and return a rolebean in a single step, otherwise you have to call a function everytime you need a member of this class (and the RoleBean have a lot members)...

    Pseudocode pwapi.php
    Code:
    session_start();
    
    class PW
    {
    	public function Login($java_password)
    	{
    		// GET request to login.jsp api file
    		// java check for $java_password
    		// if password check success, java (register and) returns a session_key
    		... // get the session_key here by scanning through jsp result
    		// php registers the $session_key
    		$_SESSION['key'] = $session_key;
    	}
    
    	public function Logout()
    	{
    		// GET request to logout.jsp api file
    		... // clear session key in java
    		// clear the session_key in php
    		unset($_SESSION['key']);
    	}
    
    	public function GetRoleBean($char_id, $session_key)
    	{
    		$role_bean = new RoleBean();
    		// GET request to get_rolebean.jsp api file
    		... // fill the RoleBean here by scanning through jsp result
    		// return the fetched RoleBean object
    		return($role_bean);
    	}
    
    	public function SetRoleBean($char_id, $role_bean, $session_key)
    	{
    		$status = "ERROR";
    		// GET request $role_bean to set_rolebean.jsp api file
    		... // get the status by scanning through jsp result
    		// return "OK" if saving role_bean successed
    		return($status);
    	}
    }
    
    class RoleBean
    {
    	public $Level;
    	public $Name;
    	public $Spouse;
    	public $HP;
    	public $Mana;
    	...
    }
    
    
    
    
    // somewhere later in our code we can acquire a role bean...
    
    // user login in
    PW::Login("password");
    
    $role_bean = PW::GetRoleBean(32, $_SESSION['key']);
    
    ... // do some more stuff
    
    // user ends session by logout
    PW::Logout();
    other required classes are roleinventory, roleitem, ...
    Last edited by ronny1982; 05-10-10 at 12:24 PM.

  5. #5
    Black Magic Development das7002 is offline
    MemberRank
    Apr 2010 Join Date
    EarthLocation
    2,188Posts

    Re: PHP Protocol API

    Quote Originally Posted by ronny1982 View Post
    without rebuilding the java classes you have a large amount of traffic and power consumption

    i.e. rolebean class

    you need to rebuild this class in php, so that java fetch a rolebean and return a rolebean in a single step, otherwise you have to call a function everytime you need a member of this class (and the RoleBean have a lot members)...

    Pseudocode pwapi.php
    Code:
    session_start();
    
    class PW
    {
    	public function Login($java_password)
    	{
    		// GET request to login.jsp api file
    		// java check for $java_password
    		// if password check success, java (register and) returns a session_key
    		... // get the session_key here by scanning through jsp result
    		// php registers the $session_key
    		$_SESSION['key'] = $session_key;
    	}
    
    	public function Logout()
    	{
    		// GET request to logout.jsp api file
    		... // clear session key in java
    		// clear the session_key in php
    		unset($_SESSION['key']);
    	}
    
    	public function GetRoleBean($char_id, $session_key)
    	{
    		$role_bean = new RoleBean();
    		// GET request to get_rolebean.jsp api file
    		... // fill the RoleBean here by scanning through jsp result
    		// return the fetched RoleBean object
    		return($role_bean);
    	}
    
    	public function SetRoleBean($char_id, $role_bean, $session_key)
    	{
    		$status = "ERROR";
    		// GET request $role_bean to set_rolebean.jsp api file
    		... // get the status by scanning through jsp result
    		// return "OK" if saving role_bean successed
    		return($status);
    	}
    }
    
    class RoleBean
    {
    	public $Level;
    	public $Name;
    	public $Spouse;
    	public $HP;
    	public $Mana;
    	...
    }
    
    
    
    
    // somewhere later in our code we can acquire a role bean...
    
    // user login in
    PW::Login("password");
    
    $role_bean = PW::GetRoleBean(32, $_SESSION['key']);
    
    ... // do some more stuff
    
    // user ends session by logout
    PW::Logout();
    other required classes are roleinventory, roleitem, ...
    I was considering either having all the gamedbd character related stuff get exported to mysql everyday

    or if I wanted live checking I could just store all that information in the session itself so I am pretty sure I don't need to rebuild it perse...

  6. #6
    Apprentice gouranga is offline
    MemberRank
    Dec 2008 Join Date
    RussiaLocation
    15Posts

    Re: PHP Protocol API

    why do you want to use old java api? you can implement everything on php, with marshaling. somthing like follows:

    PHP Code:
    <?php
    require "marshalling.php";

    function 
    hexdump($data$htmloutput true$uppercase false$return false)
    {
            
    $hexi   '';
            
    $ascii  '';
            
    $dump   = ($htmloutput === true) ? '<pre>' '';
            
    $offset 0;
            
    $len    strlen($data);
            
    $x = ($uppercase === false) ? 'x' 'X';

            for (
    $i $j 0$i $len$i++)
            {
                    
    $hexi .= sprintf("%02$x "ord($data[$i]));     // Convert to hexidecimal
                    
    if (ord($data[$i]) >= 32)                       // Replace non-viewable bytes with '.'
                            
    $ascii .= ($htmloutput === true) ? htmlentities($data[$i]) : $data[$i];
                    else
                            
    $ascii .= '.';

                    if (++
    $j === 16 || $i === $len 1)
                    {
                            
    $dump .= sprintf("%04$x  %-49s  %s"$offset$hexi$ascii); // Join the hexi / ascii output

                            
    $hexi   $ascii '';
                            
    $offset += 16;
                            
    $j      0;

                            if (
    $i !== $len 1)
                                    
    $dump .= "\n";
                    }
            }

            
    $dump .= $htmloutput === true '</pre>' '';
            
    $dump .= "\n";

            if (
    $return === false) {
                    echo 
    $dump;
            } else {
                    return 
    $dump;
            }
    }


    // interaction logic
    $sock socket_create(AF_INETSOCK_STREAMSOL_TCP); // http://ru.php.net/manual/en/function.socket-create.php
    if(!$sock)
            die(
    socket_strerror(socket_last_error()));

    // 127.0.0.1:29100 - Deliveryd
    // 127.0.0.1:29400 - GameDbd
    // 127.0.0.1:29401 - UniqueNamed
    // 127.0.0.1:29500 - Factiond
    if(socket_connect($sock"127.0.0.1""29400")) // http://ru.php.net/manual/en/function.socket-connect.php
    {
            
    $debug true;
            
    socket_set_block($sock); // blocking script

    // GetUserRoles --// GameDbd
            //$data = mUInt32(3032) . mByte(8) . mShort(32768) . mShort(0x2076) . mInt(64);

    // GetUserInfo --// GameDbd
            
    $data mUInt32(3002) . mByte(8) . mShort(32768) . mShort(0x2076) . mInt(64);

    // GetUserIdByName --// GameDbd
            //$msg = "gouranga";
            //$data2 = mShort(32768) . mShort(0x2076) . mString($msg);
            //$data = mUInt32(3033) . mUInt32(strlen($data2)) . $data2;

    // WorldChat --// Deliveryd
            //$msg = "Some red message.";
            //$data2 = mByte(9) . mByte(0) . mInt(-1) . mInt(0) . mString($msg);
            //$data = mUInt32(79) . mUInt32(strlen($data2)) . $data2;

            
    if ($debug && false !== ($sbytes socket_send($sock$data81920))) {
                    echo 
    "<p>Sended $sbytes bytes from socket_send():</p>";
                    
    hexdump($data);
            } else {
                    die(
    "socket_send() failed; reason: " socket_strerror(socket_last_error($socket)));
            }

            if (
    $debug && false !== ($rbytes socket_recv($sock$buf81920))) {
                    echo 
    "<p>Readed $rbytes bytes from socket_recv():</p>";
                    
    hexdump($buf);
            } else {
                    die(
    "socket_recv() failed; reason: " socket_strerror(socket_last_error($socket)));
            }

            
    socket_set_nonblock($sock);
            
    socket_close($sock);
    }
    else
    {
            die(
    socket_strerror(socket_last_error()));
    }
    unmarshaler and everything else create by yourself)
    Attached Files Attached Files

  7. #7
    Лучезарный loko9988 is offline
    MemberRank
    Mar 2010 Join Date
    RussiaLocation
    335Posts

    Re: PHP Protocol API

    tnx Sergey



Advertisement