Advertisement
Guest User

Untitled

a guest
Aug 3rd, 2016
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 16.90 KB | None | 0 0
  1. <?php
  2. class PacketParser {
  3.     public $DB, $ClientData, $Room, $Items;
  4.  
  5.     public function __construct() {
  6.         try {
  7.             $this->DB = new DB(Config::get('User'), Config::get('Pass'), Config::get('DB'));
  8.         } catch (PDOException $e) {
  9.             echo $e->getMessage();
  10.         }
  11.  
  12.         $this->ClientData = new Map();
  13.         $this->Room = new Room();
  14.         $this->Items = new Map();
  15.  
  16.         $this->CacheAll();
  17.     }
  18.  
  19.     public function CacheAll() {
  20.         $items = $this->DB->query('SELECT * FROM items')->resultset();
  21.  
  22.         foreach($items as $item) {
  23.             $this->Items->set($item->id, $item);
  24.         }
  25.  
  26.         $this->write("Cached {$this->Items->size()} items", "CACHE");
  27.         unset($items);
  28.     }
  29.  
  30.     public function CheckPacket($client, $packet) {
  31.         if (startsWith($packet, "<")) {
  32.             $this->HandleXML($client, $packet);
  33.         } else {
  34.             $this->HandlePercentage($client, $packet);
  35.         }
  36.     }
  37.  
  38.     public function HandleXML($client, $xml) {
  39.         if(contains('policy', $xml)) {
  40.             $this->send($client, "<cross-domain-policy><allow-access-from domain='*' to-ports='5588'/></cross-domain-policy>");
  41.         } elseif(contains("verChk", $xml)) {
  42.             $this->send($client, "<msg t='sys'><body action='apiOK' r='0'></body></msg>");
  43.  
  44.         } elseif(contains("login", $xml)){
  45.             $this->LoginParse($client, $xml);
  46.         } else {
  47.             $this->write('Unknown packet: ' . $xml, 'CORE');
  48.         }
  49.     }
  50.  
  51.     public function moveToCell($client) {
  52.         $packet = json_decode('{"t":"xt","b":{"r":-1,"o":{"cmd":"uotls","o":{"strFrame":"Enter","intMP":100.0,"intLevel":1,"entID":2,"strPad":"Spawn","intMPMax":400.0,"intHP":400.0,"afk":false,"intHPMax":400.0,"ty":0,"tx":0,"intState":1,"entType":"p","showCloak":true,"showHelm":true,"strUsername":"lol","uoName":"lol"},"unm":"lol"}}}', true);
  53.         $user = $this->GetClientData($client);
  54.        
  55.         $packet['b']['o']['o']['entID'] = (int) $user->id;
  56.         $packet['b']['o']['o']['strUsername'] = $user->Username;
  57.         $packet['b']['o']['o']['uoName'] = $user->Username;
  58.         $packet['b']['o']['unm'] = $user->Username;
  59.  
  60.         $this->SendRoom_Except($client, stripslashes(json_encode($packet)));
  61.     }
  62.  
  63.     public function RetriveUserData($client, $id) {
  64.         $user = $this->GetData_ID($id);
  65.        
  66.         $packet = json_decode('{"t":"xt","b":{"r":-1,"o":{"uid":30426,"cmd":"initUserData","data":{"intColorTrim":"7890487","intColorAccessory":"4674410","iCP":0,"intLevel":52,"intColorSkin":"14333328","intAccessLevel":"0","intColorBase":"7681060","iUpgDays":"-1744","strHairFilename":"hair/M/DFSpiky.swf","intColorHair":"16777215","intColorEye":"16737792","strHairName":"DFSpiky","strGender":"M","strUsername":"ChaoticShaper","strClassName":"undefined","eqp":{}}}}}', true);
  67.        
  68.         $packet['b']['o']['uid'] = (int) $user->id;
  69.        
  70.         $packet['b']['o']['data']['intColorTrim'] = $user->ColorTrim;
  71.         $packet['b']['o']['data']['intColorAccessory'] = $user->ColorAccessory;
  72.         $packet['b']['o']['data']['iCP'] = 0;
  73.         $packet['b']['o']['data']['intLevel'] = (int) $user->Level;
  74.         $packet['b']['o']['data']['intColorSkin'] = $user->ColorSkin;
  75.         $packet['b']['o']['data']['intAccessLevel'] = $user->Access;
  76.         $packet['b']['o']['data']['intColorBase'] = $user->ColorBase;
  77.         $packet['b']['o']['data']['iUpgDays'] = $user->UpgradeDays;
  78.         $packet['b']['o']['data']['strHairFilename'] = $user->HairFile;
  79.         $packet['b']['o']['data']['intColorHair'] = $user->ColorHair;
  80.         $packet['b']['o']['data']['intColorEye'] = $user->ColorEye;
  81.         $packet['b']['o']['data']['strHairName'] = $user->HairName;
  82.         $packet['b']['o']['data']['strGender'] = $user->Gender;
  83.         $packet['b']['o']['data']['strUsername'] = $user->Username;
  84.         $packet['b']['o']['data']['strClassName'] = 'Warrior';     
  85.         $packet['b']['o']['data']['eqp'] = '';
  86.  
  87.         $UserItems = $this->DB->query('SELECT equipment, itemid FROM users_items WHERE userid = ? AND equipped = 1')->bind(1, $user->id)->resultset();
  88.  
  89.         foreach($UserItems as $UserItem) {
  90.             $Item = $this->Items->get($UserItem->itemid);
  91.  
  92.             $packet['b']['o']['data']['eqp'][$UserItem->equipment] = [];
  93.             $packet['b']['o']['data']['eqp'][$UserItem->equipment]['ItemID'] = (int) $Item->id;
  94.             $packet['b']['o']['data']['eqp'][$UserItem->equipment]['sFile'] = $Item->File;
  95.             $packet['b']['o']['data']['eqp'][$UserItem->equipment]['sLink'] = $Item->Link;  
  96.         }
  97.        
  98.  
  99.  
  100.         $this->send($client, stripslashes(json_encode($packet)));
  101.         //$this->equipItem($client);
  102.  
  103.     }
  104.  
  105.     public function HandlePercentage($client, $perc) {
  106.         if(contains('firstJoin', $perc)) {
  107.             $this->SendCVU($client);
  108.             $this->UserBody($client);
  109.  
  110.             $this->SendRoomData($client, 'battleon', 1);
  111.  
  112.         } elseif(contains('retrieveUserDatas', $perc)) {
  113.             $this->RetrieveUserDatas($client, $perc);
  114.         } elseif(contains('moveToCell', $perc)) {
  115.             $this->moveToCell($client);
  116.         } elseif(contains('%xt%zm%retrieveUserData%', $perc)) {
  117.             $splitted = explode("%", $perc);
  118.  
  119.             array_pop($splitted);
  120.             for($i = 5; $i < count($splitted); $i++) {
  121.                 $this->RetriveUserData($client, $splitted[$i]);
  122.             }
  123.         } elseif(contains('mv', $perc)) {
  124.             $splitted = explode("%", $perc);
  125.             array_pop($splitted);
  126.  
  127.             $this->SendRoom("%xt%zm%mv%2%{$splitted[5]}%{$splitted[6]}%{$splitted[7]}%");
  128.         } else {
  129.             $this->write('Unknown packet: ' . $perc);
  130.         }
  131.     }
  132.  
  133.     public function RetrieveUserDatas($client, $perc) {
  134.         $a = [];
  135.  
  136.         $splitted = explode("%", $perc);
  137.  
  138.         for($i = 5; $i < count($splitted); $i++) {
  139.             //array_pop($splitted);
  140.  
  141.             if($i != 0 && $i != null && $splitted[$i] != 0 && $splitted[$i] != null) {
  142.                 array_push($a, $this->RetrieveUserInfo($splitted[$i]));
  143.             }
  144.         }
  145.  
  146.         $packet = [
  147.             "t" => "xt",
  148.             "b" => [
  149.                 "r" => -1,
  150.                 "o" => [
  151.                     "cmd" => "initUserDatas",
  152.                     "a" => $a
  153.                 ]
  154.             ]
  155.         ];
  156.  
  157.         $this->send($client, stripslashes(json_encode($packet)));
  158.     }
  159.  
  160.  
  161.     public function RetrieveUserInfo($id) {
  162.         $user = $this->GetData_ID($id);
  163.         $this->write("USER ID: " . $user->id);
  164.         $userArr = [];
  165.  
  166.         $userArr['uid'] = $user->id;
  167.        
  168.         $userArr['data'] = [];
  169.         $userArr['data']['intColorTrim'] = $user->ColorTrim;
  170.         $userArr['data']['intColorAccessory'] = $user->ColorAccessory;
  171.         $userArr['data']['iCP'] = 0;
  172.         $userArr['data']['intLevel'] = (int) $user->Level;
  173.         $userArr['data']['intColorSkin'] = $user->ColorSkin;
  174.         $userArr['data']['intAccessLevel'] = $user->Access;
  175.         $userArr['data']['intColorBase'] = $user->ColorBase;
  176.         $userArr['data']['iUpgDays'] = $user->UpgradeDays;
  177.         $userArr['data']['strHairFilename'] = $user->HairFile;
  178.         $userArr['data']['intColorHair'] = $user->ColorHair;
  179.         $userArr['data']['intColorEye'] = $user->ColorEye;
  180.         $userArr['data']['strHairName'] = $user->HairName;
  181.         $userArr['data']['strGender'] = $user->Gender;
  182.         $userArr['data']['strUsername'] = $user->Username;
  183.         $userArr['data']['strClassName'] = 'Warrior';
  184.         $userArr['data']['eqp'] = '';
  185.  
  186.         $UserItems = $this->DB->query('SELECT equipment, itemid FROM users_items WHERE userid = ? AND equipped = 1')->bind(1, $user->id)->resultset();
  187.        
  188.  
  189.         foreach($UserItems as $UserItem) {
  190.             $Item = $this->Items->get($UserItem->itemid);
  191.  
  192.             $userArr['data']['eqp'][$UserItem->equipment] = [];
  193.             $userArr['data']['eqp'][$UserItem->equipment]['ItemID'] = (int) $Item->id;
  194.             $userArr['data']['eqp'][$UserItem->equipment]['sFile'] = $Item->File;
  195.             $userArr['data']['eqp'][$UserItem->equipment]['sLink'] = $Item->Link;  
  196.         }
  197.    
  198.  
  199.         return $userArr;
  200.     }
  201.  
  202.     public function SendRoomData($client, $battleon, $rid) {
  203.         $this->JoinOK($client);
  204.  
  205.         $packet = json_decode('{"t":"xt","b":{"r":-1,"o":{"cmd":"moveToArea","areaName":"battleon-2","uoBranch":[{"strFrame":"Enter2","intMP":100,"intLevel":61,"entID":26752,"strPad":"Spawn","intMPMax":100,"intHP":2593,"afk":false,"intHPMax":2593,"ty":387,"tx":170,"intState":1,"entType":"p","showHelm":true,"showCloak":true,"strUsername":"havokpwndu","uoName":"havokpwndu"},{"strFrame":"Enter","intMP":100,"intLevel":51,"entID":26799,"strPad":"Spawn","intMPMax":100,"intHP":2175,"afk":true,"intHPMax":2175,"ty":0,"tx":0,"intState":1,"entType":"p","showHelm":true,"showCloak":true,"strUsername":"ghostxzz","uoName":"ghostxzz"},{"strFrame":"Enter","intMP":100,"intLevel":46,"entID":26815,"strPad":"Spawn","intMPMax":100,"intHP":1815,"afk":false,"intHPMax":1815,"ty":0,"tx":0,"intState":1,"entType":"p","showHelm":true,"showCloak":false,"strUsername":"pes9632","uoName":"pes9632"},{"strFrame":"Enter","intMP":100,"intLevel":29,"entID":26817,"strPad":"Spawn","intMPMax":100,"intHP":1688,"afk":false,"intHPMax":1688,"ty":0,"tx":0,"intState":1,"entType":"p","showHelm":true,"showCloak":true,"strUsername":"maki_okami","uoName":"maki_okami"},{"strFrame":"Enter","intMP":100,"intLevel":65,"entID":26821,"strPad":"Spawn","intMPMax":100,"intHP":2655,"afk":false,"intHPMax":2655,"ty":429,"tx":694,"intState":1,"entType":"p","showHelm":true,"showCloak":true,"strUsername":"mazenzeka2015","uoName":"mazenzeka2015"},{"strFrame":"Enter","intMP":100,"intLevel":43,"entID":26822,"strPad":"Spawn","intMPMax":100,"intHP":1851,"afk":false,"intHPMax":1851,"ty":0,"tx":0,"intState":1,"entType":"p","showHelm":true,"showCloak":true,"strUsername":"Mario Mario Mario4","uoName":"mario mario mario4"},{"strFrame":"Enter","intMP":100,"intLevel":52,"entID":26825,"strPad":"Spawn","intMPMax":100,"intHP":100,"afk":false,"intHPMax":100,"ty":0,"tx":0,"intState":1,"entType":"p","showHelm":true,"showCloak":true,"strUsername":"ChaoticShaper","uoName":"chaoticshaper"}],"strMapFileName":"Battleon/town-battleon-30Jul16-QUIB.swf","mondef":[{"intHPMax":310,"sRace":"Chaos","strElement":"Fire","MonID":"2566","intMP":"100","intLevel":1,"strLinkage":"Chicken","strMonFileName":"Monster-Chicken.swf","strMonName":"Chicken","intMPMax":100,"intHP":310,"strBehave":"walk"},{"intHPMax":310,"sRace":"Dragonkin","strElement":"Fire","MonID":"2568","intMP":"100","intLevel":1,"strLinkage":"Chicken2","strMonFileName":"Monster-Chicken2.swf","strMonName":"Sabertooth Chicken","intMPMax":100,"intHP":310,"strBehave":"walk"}],"intType":"2","monBranch":[{"intHPMax":310,"iLvl":1,"MonMapID":5,"MonID":"2566","intMP":100,"wDPS":6,"intState":1,"intMPMax":100,"bRed":"0","intHP":310},{"intHPMax":310,"iLvl":1,"MonMapID":3,"MonID":"2566","intMP":100,"wDPS":6,"intState":1,"intMPMax":100,"bRed":"0","intHP":310},{"intHPMax":310,"iLvl":1,"MonMapID":4,"MonID":"2568","intMP":100,"wDPS":6,"intState":1,"intMPMax":100,"bRed":"0","intHP":310}],"sExtra":"","monmap":[{"MonMapID":"3","strFrame":"Enter2","intRSS":"-1","MonID":"2566","bRed":"0"},{"MonMapID":"4","strFrame":"Enter2","intRSS":"-1","MonID":"2568","bRed":"0"},{"MonMapID":"5","strFrame":"Enter2","intRSS":"-1","MonID":"2566","bRed":"0"}],"areaId":66516,"strMapName":"battleon"}}}');
  206.         $packet->b->o->areaName = "battleon-2";
  207.         $packet->b->o->uoBranch = $this->GetUOBranch();
  208.         $packet->b->o->strMapFileName = "Battleon/town-battleon-20may11r1.swf";
  209.         $packet->b->o->mondef = [];
  210.         $packet->b->o->intType = "2";
  211.         $packet->b->o->monBranch = [];
  212.         $packet->b->o->sExtra = "";
  213.         $packet->b->o->monmap = [];
  214.         $packet->b->o->areaId = 2;
  215.         $packet->b->o->strMapName = "battleon";
  216.  
  217.         $this->send($client, stripslashes(json_encode($packet)));
  218.         $this->SendMessage($client, 'server', 'You joined "battleon-2"!');
  219.     }
  220.  
  221.     public function GetUOBranch() {
  222.         $usersArr = [];
  223.  
  224.         foreach($this->Room->UserList as $users) {
  225.             $user = $this->GetClientData($users);
  226.  
  227.             array_push($usersArr, [
  228.                 "strFrame" => "Enter",
  229.                 "intMP" => 100,
  230.                 "intLevel" => $user->Level,
  231.                 "entID" => $user->id,
  232.                 "strPad" => "Spawn",
  233.                 "intMPMax" => 100,
  234.                 "intHP" => 1000,
  235.                 "afk" => false,
  236.                 "intHPMax" => 1000,
  237.                 "ty" => 0,
  238.                 "tx" => 0,
  239.                 "intState" => 1,
  240.                 "entType" => "p",
  241.                 "showHelm" => true,
  242.                 "showCloak" => true,
  243.                 "strUsername" => $user->Username,
  244.                 "uoName" => $user->Username
  245.             ]);
  246.         }
  247.  
  248.         return $usersArr;
  249.     }
  250.  
  251.     public function SendMessage($client, $type, $msg) {
  252.         $this->send($client, '%xt%'.$type.'%-1%'.$msg.'%');
  253.     }
  254.  
  255.     public function JoinOK($client) {
  256.         $data = "<msg t='sys'><body action='joinOK' r='2'><pid id='1'/><vars /><uLs r='2'>";
  257.  
  258.         foreach($this->Room->UserList as $users) {
  259.             $user = $this->GetClientData($users);
  260.             $data .= "<u i='{$user->id}' m='0' s='0' p='1'><n><![CDATA[{$user->Username}]]></n><vars></vars></u>";
  261.         }
  262.  
  263.         $data .= "</uLs></body></msg>";
  264.  
  265.         $this->send($client, $data);
  266.     }
  267.  
  268.     public function UserBody($client) {
  269.         $data = "<msg t='sys'><body action='uER' r='2'>";
  270.  
  271.         foreach($this->Room->UserList as $users) {
  272.             $user = $this->GetClientData($users);
  273.  
  274.             $data .= "<u i ='" . $user->id . "' m='0' s='0' p='1'><n><![CDATA[" . $user->Username . "]]></n><vars></vars></u>";
  275.         }
  276.  
  277.         $data .= "</body></msg>";
  278.  
  279.         $this->SendRoom_Except($client, stripslashes($data));
  280.     }
  281.  
  282.     public function SendRoom_Except($client, $packet) {
  283.         foreach ($this->Room->UserList as $user) {
  284.             if ($client != $user) {
  285.                 $this->send($user, $packet);
  286.             }
  287.         }
  288.     }
  289.  
  290.     public function SendRoom($packet) {
  291.         foreach ($this->Room->UserList as $user) {
  292.             $this->send($user, $packet);
  293.         }
  294.     }
  295.  
  296.     public function equipItem($client) {
  297.         $packet = null;
  298.         $user = $this->GetClientData($client);
  299.  
  300.         $useritems = $this->DB->query('SELECT * FROM users_items WHERE userid = ? AND equipped=1')->bind(1, $user->id);
  301.        
  302.         if($useritems->num_rows() > 0) {
  303.             $UItems = $useritems->resultset();
  304.  
  305.             foreach($UItems as $Item) {
  306.                 $packet = json_decode('{"t":"xt","b":{"r":-1,"o":{"uid":1,"ItemID":1,"strES":"Weapon","cmd":"equipItem","sFile":"items/swords/sword01.swf","sLink":"","sType":"Sword","sMeta":""}}}');
  307.  
  308.                 $items = $this->Items->get($Item->itemid);
  309.  
  310.                 $packet->b->o->uid = (int) $user->id;
  311.                 $packet->b->o->ItemID = (int) $items->id;
  312.                 $packet->b->o->strES = $Item->equipment;
  313.                 $packet->b->o->sFile = $items->File;
  314.                 $packet->b->o->sLink = $items->Link;
  315.                 $packet->b->o->sType = $items->Type;
  316.  
  317.                 $this->send($client, stripslashes(json_encode($packet)));
  318.             }
  319.         }
  320.  
  321.         $this->send($client, '{"t":"xt","b":{"r":-1,"o":{"uid":'.$user->id.',"sStats":"Warriors favor Strength, Endurance, and Dexterity","iCP":568,"cmd":"updateClass","sDesc":"Warriors are quintessential melee fighters.  Simple, direct, and effective, they are masters of armed combat, and quite durable.","sClassCat":"M1","aMRM":["Warriors gain mana when they Strike an enemy in combat (more effective on crits) Are struck by an enemy in combat"],"sClassName":"Warrior"}}}');
  322.        
  323.         unset($packet);
  324.         unset($user);
  325.     }
  326.  
  327.     public function SendCVU($client) {
  328.         $this->send($client, '{"t":"xt","b":{"r":-1,"o":{"cmd":"cvu","o":{"PCstRatio":7.47,"PChpDelta":1640,"PChpBase1":360,"baseHit":0,"intSPtoDPS":10,"resistRating":17,"curveExponent":0.66,"baseCritValue":1.5,"PChpGoal100":4000,"intLevelCap":100,"baseMiss":0.1,"baseParry":0.03,"GstBase":12,"modRating":3,"baseResistValue":0.7,"baseBlockValue":0.7,"intHPperEND":5,"baseHaste":0,"baseBlock":0,"statsExponent":1,"PChpBase100":2000,"intAPtoDPS":10,"PCstBase":15,"baseCrit":0.05,"baseEventValue":0.05,"GstGoal":572,"PChpGoal1":400,"GstRatio":5.6,"intLevelMax":100,"bigNumberBase":8,"PCstGoal":762,"baseDodge":0.04,"PCDPSMod":0.85}}}}'); 
  329.     }
  330.  
  331.     public function LoginParse($client, $xml) {
  332.         $XMLDom = new SimpleXMLElement($xml);
  333.         $unmArray = $XMLDom->xpath('/msg/body/login/nick');
  334.         $pwdArray = $XMLDom->xpath('/msg/body/login/pword');
  335.        
  336.         $username = explode("~",$unmArray[0])[1];
  337.         $password = $pwdArray[0];
  338.  
  339.         unset($unmArray);
  340.         unset($pwdArray);
  341.         $XMLDom = null;    
  342.  
  343.         $this->write("Login attempt by: " . $username . ", " . $password, "XML");
  344.  
  345.         $query = $this->DB->query('SELECT * FROM users WHERE Username = ? AND Password =?')->bind(1, $username)->bind(2, $password);
  346.  
  347.         if($query->num_rows() > 0) {
  348.             $this->Room->AddToRoom($client);
  349.            
  350.             $resultset = $query->single();
  351.             $resultset->Username = strtolower($resultset->Username);
  352.  
  353.             $this->AddClientData($client, $resultset);
  354.  
  355.             $settings = $this->GetsettingsObject();
  356.  
  357.             $this->send($client, "%xt%loginResponse%-1%true%{$resultset->id}%{$resultset->Username}%{$settings->MOTD}%2016-07-18T01:01:05%sNews={$settings->News},sMap={$settings->Map},sBook={$settings->Book},sFBC={$settings->FBC},sAssets={$settings->Assets},sWTSandbox={$settings->WTSandbox},gMenu={$settings->GameMenu},sVideo={$settings->Video}%");
  358.  
  359.             unset($settings);
  360.             unset($resultset);
  361.         }
  362.  
  363.         unset($query);
  364.     }
  365.  
  366.     public function write($msg, $type = 'INFO') {
  367.         printf('%s%s %s  <%s> %s  %s', "\n", @date('m/d/y'), @date('g:i A'), $type, $msg, chr(10));
  368.     }
  369.  
  370.     public function AddClientData($socket, $data) {
  371.         $this->ClientData->set((int)$socket, $data);
  372.     }
  373.    
  374.     public function GetClientData($client) {
  375.         if ($this->ClientData->containsKey((int)$client) && !$this->ClientData->isEmpty()) {
  376.             return $this->ClientData->get((int)$client);
  377.         }
  378.         return null;
  379.     }
  380.    
  381.     public function GetData_ID($id) {
  382.         return $this->DB->query('SELECT * FROM users WHERE id = ?')->bind(1, $id)->single();
  383.     }
  384.    
  385.     public function GetsettingsObject() {
  386.         return $this->DB->query('SELECT * FROM settings_server WHERE id = 1')->single();
  387.     }
  388.  
  389.     public function send($client, $packet) {
  390.         $packet .= "\0";
  391.         socket_write($client, $packet);
  392.         $this->write($packet, "SENDPACKET");
  393.     }
  394. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement