Guest User

Untitled

a guest
May 17th, 2018
204
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 29.45 KB | None | 0 0
  1. <?php
  2.   ini_set('mysql.connect_timeout', 25);
  3.   error_reporting(E_ALL);
  4.   set_time_limit(0);
  5.  
  6.   include "crumbs.php";
  7.   include_once "config.php";
  8.   $init = false;
  9.   $count = 0;
  10.   class penguinelite {
  11.       public $ip;
  12.       public $count;
  13.       public $port;
  14.       public $users = array();
  15.       public $mode;
  16.       public $config;
  17.       public $mysql;
  18.       public $bot;
  19.       private $socket;
  20.       public function __construct($config = "config.xml") {
  21.           global $init, $count;
  22.           $count++;
  23.           $this->count = $count;
  24.           if ($init == false)
  25.               $this->createHeader();
  26.           else
  27.               $this->writeOutput("Starting next server...", "INFO");
  28.           $init = true;
  29.           $this->readConfig($config);
  30.       }
  31.       public function readConfig($file) {
  32.           if (!file_exists($file))
  33.             $this->shutDown("Could not find $file. Does it exist?");
  34.             $this->config = simplexml_load_file($file) or $this->shutDown("$file has errors!");
  35.             $this->writeOutput("This is " . $this->config->type . " server", "INFO");
  36.             $this->writeOutput("Configs -> Done");
  37.       }
  38.       public function init() {
  39.           $this->mysql = new mysql();
  40.           $err = false;
  41.           $this->writeOutput("Connecting to MySQL database...", "INFO");
  42.           $this->mysql->connect($this->config->mysql->host, $this->config->mysql->username, $this->config->mysql->password) or $err = true;
  43.           if ($err == true)
  44.               $this->shutDown("Could not connect to MySQL: " . $this->mysql->getError());
  45.           $this->mysql->selectDB($this->config->mysql->dbname);
  46.           if ($err == true)
  47.               $this->shutDown("Could not select the database: " . $this->mysql->getError());
  48.           $this->bind((integer)$this->config->port, (string)$this->config->ip);
  49.           $this->writeOutput("MySQL Connection -> Done\n", "FINEST");
  50.       }
  51.       public function bind($port, $ip = "0") {
  52.           $this->socket = socket_create(AF_INET, SOCK_STREAM, 0) or $this->shutDown("Could not create socket. Please check php.ini to see if sockets are enabled!");
  53.           socket_set_option($this->socket, SOL_SOCKET, SO_REUSEADDR, 1);
  54.           socket_bind($this->socket, $ip, $port) or $this->shutDown("Could not bind to port. Make sure the port is over 1024 if you are using linux");
  55.           socket_listen($this->socket);
  56.       }
  57.       public function loopFunction() {
  58.           socket_set_block($this->socket);
  59.           $read = array();
  60.           $read[0] = $this->socket;
  61.           foreach ($this->users as $i=>&$user) {
  62.               if (!empty($user))
  63.                   $read[] = &$user->sock;
  64.               if ($user->selfDestruct == true)
  65.                   unset($this->users[$i]);
  66.           }
  67.           $ready = socket_select($read, $w = null, $e = null, $t = 0);
  68.           if (in_array($this->socket, $read)) {
  69.               /*if (count($this->users) <= 0) {
  70.                   //$this->users[] = new CPUser(socket_accept($this->socket), $this);
  71.                   $iplggt=$_SERVER['REMOTE_ADDR'];
  72.                   $this->writeOutput("New Client Connected - $iplggt", "FINE");
  73.               } else */if(count($this->users) >= 0)
  74.                   $this->writeOutput("Server is full", "INFO");
  75.                   sleep(100);
  76.           }
  77.           if ($ready-- <= 0)
  78.               return;
  79.           else {
  80.               foreach ($this->users as $index=>&$user) {
  81.                   if (in_array($user->sock, $read)) {
  82.                       $input = socket_read($user->sock, 65536);
  83.                       if ($input == null) {
  84.                           unset($this->users[$index]);
  85.                           continue;
  86.                       }
  87.                       $x = explode(chr(0), $input);
  88.                       array_pop($x);
  89.                       foreach ($x as $input2){
  90.                           $this->handleRawPacket($input2, $user);
  91.                       }
  92.                   }
  93.               }
  94.           }
  95.       }
  96.       public function doLogin(&$user, $packet) {
  97.           $username = $this->mysql->escape($this->stribet($packet, "<nick><![CDATA[", "]]"));
  98.           $password = $this->stribet($packet, "<pword><![CDATA[", "]]");
  99.           if ($this->mysql->getRows("SELECT * FROM {$this->config->mysql->userTableName} WHERE username='" . $username . "';") > 0) {
  100.               $dbv = $this->mysql->returnArray("SELECT * FROM {$this->config->mysql->userTableName} WHERE username='" . $this->mysql->escape($username) . "';");
  101.           if($this->config->type == "login"){
  102.                $hash = strtoupper($dbv[0]["password"]);
  103.                $hash = $this->encryptPassword($hash, $user->key);
  104.           } else {
  105.                $hash = $this->swapMD5(md5($dbv[0]["lkey"] . $user->key)) . $dbv[0]["lkey"];
  106.           }
  107.           if ($password == $hash) {
  108.                   if ($dbv[0]["active"] != "0") {
  109.                       if ($dbv[0]["ubdate"] != "PERMABANNED") {
  110.                           if ($dbv[0]["ubdate"] < strtotime("NOW MDT")) {
  111.                               if ($this->config->type == "login") {
  112.                                   $this->writeSocket($user, "%xt%gs%-1%$server1IP:$server1Port:$server1Name:3|$server2IP:$server2Port:$server2Name:2% 3;");
  113.                                   $this->writeSocket($user, "%xt%l%-1%" . $dbv[0]["id"] . "%" . md5(strrev($user->key)) . "%0%");
  114.                                   $this->mysql->query("UPDATE {$this->config->mysql->userTableName} SET lkey='" . md5(strrev($user->key)) . "' WHERE id='" . $dbv[0]["id"] . "';");
  115.                               } else {
  116.                                   socket_getsockname($user->sock, $ip);
  117.                                   $user->id = $dbv[0]["id"];
  118.                                   $this->mysql->query("UPDATE {$this->config->mysql->userTableName} SET ips=ips + '\n" . $this->mysql->escape($ip) . "' WHERE id='" . $user->getID() . "';");
  119.                                   $user->resetDetails();
  120.                                   $user->sendPacket("%xt%l%-1%");
  121.                               }
  122.                           } else
  123.                               $this->writeSocket($user, "%xt%e%-1%601%24%");
  124.                       } else
  125.                           $this->writeSocket($user, "%xt%e%-1%603%");
  126.                   } else
  127.                       $this->writeSocket($user, "%xt%e%-1%900%");
  128.               } else
  129.                   $this->writeSocket($user, "%xt%e%-1%101%");
  130.           } else
  131.               $this->writeSocket($user, "%xt%e%-1%100%");
  132.       }
  133.       public function encryptPassword($password, $key) {
  134.           return $this->swapMD5(md5($this->swapMD5($password) . $key . 'Y(02.>\'H}t":E1'));
  135.       }
  136.       public function swapMD5($func_md5) {
  137.           return substr($func_md5, 16, 16) . substr($func_md5, 0, 16);
  138.       }
  139.       public function handleRawPacket($packet, &$user) {
  140.           if (substr($packet, 0, 1) == "<")
  141.               $this->handleSysPacket($packet, $user);
  142.           elseif (substr($packet, 0, 1) == "%")
  143.               $this->handleXtPacket($packet, $user);
  144.       }
  145.       public function handleSysPacket($packet, &$user) {
  146.           if (stristr($packet, "<policy-file-request/>") > -1)
  147.               $this->writeSocket($user, "<cross-domain-policy><allow-access-from domain='*' to-ports='*' /></cross-domain-policy>");
  148.           if (stristr($packet, "<msg t='sys'><body action='verChk'") > -1)
  149.               $this->writeSocket($user, "<msg t='sys'><body action='apiOK' r='0'></body></msg>");
  150.           if (stristr($packet, "<msg t='sys'><body action='rndK' r='-1'></body></msg>") > -1){
  151.               $user->key = $this->generateRandomKey();
  152.               $this->writeSocket($user, "<msg t='sys'><body action='rndK' r='-1'><k>" . $user->key . "</k></body></msg>");
  153.           }
  154.           if (stristr($packet, "<msg t='sys'><body action='login' r='0'>") > -1)
  155.               $this->doLogin($user, $packet);
  156.       }
  157.       public function handleXtPacket($packet, &$user) {
  158.           $raw = explode("%", $packet);
  159.           $handler = $raw[2];
  160.           if ($handler == "s")
  161.               $this->handleStandardPacket($packet, $user);
  162.           if ($handler == "z")
  163.               $this->handleGamePacket($packet, $user);
  164.                   if ($handler == "iCP") $this->handleCustomPacket($packet, $user);
  165.       }
  166.           public function handleCustomPacket($packet, &$user){
  167.             $raw = explode("%", $packet);
  168.             $cmd = $raw[3];
  169.             if($cmd == "umo")
  170.      $this->setMood($raw[4]);
  171.          }
  172.       public function getDefaultRoom(){
  173.           $rooms = array("100", "110", "111", "120", "121", "130", "200", "210", "220", "221", "230", "300", "310", "320", "330", "340", "400", "410", "411", "800", "801", "802", "804", "805", "806", "807", "808", "809");
  174.           return $rooms[array_rand($rooms)];
  175.       }
  176.       public function handleStandardPacket($packet, &$user) {
  177.           $raw = explode("%", $packet);
  178.           $cmd = $raw[3];
  179.           if ($cmd == "j#js") {
  180.               $lkey = $raw[6];
  181.               $res = $this->mysql->returnArray("SELECT * FROM {$this->config->mysql->userTableName} WHERE id='" . $user->getID() . "'");
  182.               if (count($res) > 0)
  183.                   $user->sendPacket("%xt%js%-1%0%1%" . $res[0]["ismoderator"] . "%0%");
  184.               $this->mysql->query("UPDATE {$this->config->mysql->userTableName} SET lkey='' WHERE id='" . $user->getID() . "';");
  185.           }
  186.           if ($cmd == "j#jp"){
  187.               $user->sendPacket("%xt%jp%" . $raw[4] . "%" . $raw[5] . "%");
  188.               $user->joinRoom($raw[5], $raw[6], $raw[7]);
  189.           }
  190.           if ($cmd == "p#pg"){
  191.               $user->sendPacket("%xt%pg%" . $raw[4] . "%");
  192.           }
  193.           if ($cmd == "i#gi"){
  194.               $user->sendPacket("%xt%gps%-1%" . $user->getID() . "%9|10|11|14|20|183%");
  195.               $user->sendPacket("%xt%glr%-1%3555%");
  196.               $user->sendPacket("%xt%lp%-1%" . implode("|", $user->getDetails()) . "%" . $user->getCoins() . "%0%1440%" . rand(1200000000000, 1500000000000) . "%" . $user->getAge() . "%4%" . $user->getAge() . "% %7%");
  197.               $user->joinRoom($this->getDefaultRoom());
  198.               $user->sendPacket("%xt%gi%-1%" . implode("%", $user->getInventory()) . "%");
  199.           }
  200.           if ($cmd == "i#ai")
  201.               $user->addItem($raw[5]);
  202.           if ($cmd == "n#gn")
  203.               $user->sendPacket("%xt%gn%-1%");
  204.           if ($cmd == "l#mst")
  205.               $user->sendPacket("%xt%mst%-1%0%1");
  206.           if ($cmd == "l#mg")
  207.               $user->sendPacket("%xt%mg%-1%Safari|0|12|penguinelite|0|63%");
  208.           if ($cmd == "j#jr")
  209.               $user->joinRoom($raw[5], $raw[6], $raw[7]);
  210.           if ($cmd == "m#sm")
  211.               $user->speak($raw[6]);
  212.           if ($cmd == "o#k"){
  213.               foreach($this->users as &$suser){
  214.                   if($suser->getID() == $raw[5]){
  215.                        $suser->kick();
  216.                   }
  217.               }
  218.           }
  219.          $h = explode("#", $cmd);
  220.           $h = $h[0];
  221.           if ($h == "s")
  222.               $this->handleUserSettingPacket($packet, $user);
  223.           if ($h == "u")
  224.               $this->handleUserSettingPacket($packet, $user);
  225.           if ($h == "f")
  226.               $this->handleEPFPacket($packet, $user);
  227.           if ($h == "b")
  228.               $this->handleBuddyPacket($packet, $user);
  229.           if ($h == "g")
  230.               $this->handleIglooPacket($packet, $user);
  231.       }
  232.       public function handleBuddyPacket($packet, &$user){
  233.           $raw = explode("%", $packet);
  234.           $cmd = $raw[3];
  235.           if ($cmd == "b#gb")
  236.               $user->sendPacket("%xt%gb%-1%" . $user->getBuddyStr());
  237.           if($cmd == "b#br")
  238.               $user->requestBuddy($raw[5]);
  239.           if($cmd == "b#ba")
  240.               $user->acceptBuddy($raw[5]);
  241.           if($cmd == "b#rb")
  242.               $user->removeBuddy($raw[5]);
  243.           if($cmd == "b#bf")
  244.               $user->findBuddy($raw[5]);
  245.       }
  246.       public function handleIglooPacket($packet, &$user){
  247.           $raw = explode("%", $packet);
  248.           $cmd = $raw[3];
  249.           if($cmd == "g#gm")
  250.               $user->sendPacket("%xt%gm%-1%" . $raw[5] . "%1%0%0%%");
  251.       }
  252.       public function handleUserSettingPacket($packet, &$user) {
  253.           $raw = explode("%", $packet);
  254.           $cmd = $raw[3];
  255.           if ($cmd == "u#sp")
  256.               $user->setXY($raw[5], $raw[6]);
  257.           if ($cmd == "u#gp"){
  258.               $playerInfo = $this->mysql->returnArray("SELECT id, nickname, '1', colour, curhead, curface, curneck, curbody, curhands, curfeet, curflag, curphoto, rank * 146 FROM {$this->config->mysql->userTableName} WHERE id='" . $this->mysql->escape($raw[5]) . "';");
  259.               $playerInfo = $playerInfo[0];
  260.               $user->sendPacket("%xt%gp%-1%" . $raw[5] . "%" . implode("|", $playerInfo) . "%");
  261.           }
  262.           if ($cmd == "s#upc")
  263.               $user->setColour($raw[5]);
  264.           if ($cmd == "s#uph")
  265.               $user->setHead($raw[5]);
  266.           if ($cmd == "s#upf")
  267.               $user->setFace($raw[5]);
  268.           if ($cmd == "s#upn")
  269.               $user->setNeck($raw[5]);
  270.           if ($cmd == "s#upb")
  271.               $user->setBody($raw[5]);
  272.           if ($cmd == "s#upa")
  273.               $user->setHands($raw[5]);
  274.           if ($cmd == "s#upe")
  275.               $user->setFeet($raw[5]);
  276.           if ($cmd == "s#upp")
  277.               $user->setPhoto($raw[5]);
  278.           if ($cmd == "s#upl")
  279.               $user->setPin($raw[5]);
  280.           if ($cmd == "u#h")
  281.               $user->sendPacket("%xt%h%" . $raw[4] . "%");
  282.           if ($cmd == "u#sf")
  283.               $user->setFrame($raw[5]);
  284.           if($cmd == "u#sb")
  285.               $user->sendRoom("%xt%sb%-1%" . $user->getID() . "%" . $raw[5] . "%" . $raw[6] . "%");
  286.           if($cmd == "u#se")
  287.               $user->sendRoom("%xt%se%-1%" . $user->getID() . "%" . $raw[5] . "%");
  288.           if($cmd == "u#sa")
  289.               $user->setAction($raw[5]);
  290.           if($cmd == "u#ss")
  291.               $user->sendRoom("%xt%ss%-1%" . $user->getID() . "%" . $raw[5] . "%");
  292.           if($cmd == "u#sl")
  293.               $user->sendRoom("%xt%sl%-1%" . $user->getID() . "%" . $raw[5] . "%");
  294.           if($cmd == "u#sq")
  295.               $user->sendRoom("%xt%sq%-1%" . $user->getID() . "%" . $raw[5] . "%");
  296.           if($cmd == "u#sg")
  297.               $user->sendRoom("%xt%sg%-1%" . $user->getID() . "%" . $raw[5] . "%");
  298.           if($cmd == "u#sj")
  299.               $user->sendRoom("%xt%sj%-1%" . $user->getID() . "%" . $raw[5] . "%");
  300.           if($cmd == "u#sma")
  301.               $user->sendRoom("%xt%sma%-1%" . $user->getID() . "%" . $raw[5] . "%");
  302.       }
  303.       public function handleEPFPacket($packet, &$user) {
  304.           $raw = explode("%", $packet);
  305.           $cmd = $raw[3];
  306.           if ($cmd == "f#epfga")
  307.               $user->sendPacket("%xt%epfga%-1%1%");
  308.           if ($cmd == "f#epfgr")
  309.               $user->sendPacket("%xt%epfgr%-1%0%0%");
  310.           if ($cmd == "f#epfgf")
  311.               $user->sendPacket("%xt%epfgf%-1%0%");
  312.       }
  313.       public function handleGamePacket($packet, &$user) {
  314.           //No games yet
  315.       }
  316.       public function writeSocket(&$user, $packet) {
  317.           if (@stristr($packet, strlen($packet) - 1, 1) != chr(0))
  318.               $packet = $packet . chr(0);
  319.           socket_write($user->sock, $packet, strlen($packet));
  320.       }
  321.       public function stribet($input, $left, $right) {
  322.           $pl = stripos($input, $left) + strlen($left);
  323.           $pr = stripos($input, $right, $pl);
  324.           return substr($input, $pl, $pr - $pl);
  325.       }
  326.       public function generateRandomKey($amount = 9) {
  327.           return "abc12345";
  328.           $keyset = "abcdefghijklmABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!\"Ј$%^&*()_+-=[]{}:@~;'#<>?|\\,./";
  329.           $randkey = "";
  330.           for ($i = 0; $i < $amount; $i++)
  331.               $randkey .= substr($keyset, rand(0, strlen($keyset) - 1), 1);
  332.           return $randkey;
  333.       }
  334.       public function __destruct() {
  335.           @socket_shutdown($this->socket);
  336.       }
  337.       public function shutDown($error) {
  338.           $this->writeOutput("System error. Terminating server", "CRITICAL");
  339.           $this->writeOutput($error, "CRITICAL");
  340.           $this->writeOutput("Server terminated.", "CRITICAL");
  341.           if ($this->socket != null)
  342.               $this->writeOutput("Closing ports", "INFO");
  343.           die();
  344.       }
  345.       private function createHeader() {
  346.           echo 'Welcome to Operation Reborn sourcecode!'."\n";
  347.       }
  348.       private function writeOutput($msg, $type = "INFO") {
  349.           echo date("H\:i\:s") . " - [$type] [" . $this->count . "]  > $msg\n";
  350.       }
  351.       public function handleCommand(&$user, $msg) {
  352.           if (function_exists("handleCommand") && substr($msg, 0, 1) == "!"){
  353.               handleCommand($user, $msg, $this);
  354.           }
  355.       }
  356.       public function sendPacket($packet) {
  357.           foreach ($this->users as $user)
  358.               $user->sendPacket($packet);
  359.       }
  360.   }
  361.   class CPUser {
  362.       public $mood;
  363.       public $isMember;
  364.       public $selfDestruct;
  365.       public $sock;
  366.       public $parent;
  367.       public $inventory;
  368.       public $coins;
  369.       public $username;
  370.       public $email;
  371.       public $room;
  372.       public $lkey;
  373.       public $colour;
  374.       public $id;
  375.       public $head;
  376.       public $face;
  377.       public $neck;
  378.       public $body;
  379.       public $hands;
  380.       public $feet;
  381.       public $pin;
  382.       public $photo;
  383.       public $loggedin;
  384.       public $x;
  385.       public $y;
  386.       public $key;
  387.       public $rank;
  388.       public $frame;
  389.       public $buddies;
  390.       public $buddyRequests = array();
  391.       public $isModerator = false;
  392.       public function __construct($socket, &$parent) {
  393.           $this->sock = $socket;
  394.           $this->parent = $parent;
  395.       }
  396.      
  397.           public function getMood(){ return $this->mood; }
  398.     public function setMood($mood){
  399.             $mood = $this->parent->mysql->escape($mood);
  400.             $this->parent->mysql->query("UPDATE {$this->parent->config->mysql->userTableName} SET mood='$mood' WHERE id='" . $this->getID() . "';");
  401.             $this->sendRoom("%xt%umo%". $this->getID() . "%". $mood ."%");
  402.             $this->mood = $mood;
  403.          }
  404.          
  405.          
  406.          
  407.           public function getMember(){return (bool)$this->isMember; }
  408.               public function setMember($xMember){
  409.        $xMember=(bool)$xMember;
  410.        $this->parent->mysql->query("UPDATE {$this->parent->config->mysql->userTableName} SET isMember='$xMember' WHERE id='" . $this->getID() . "';");
  411.        $this->isMember = $xMember;
  412.     }
  413.    
  414.       public function alertBot($msg)
  415.       {
  416.         $this->sendPacket("%xt%sm%-1%0%$msg%");
  417.       }
  418.       public function __destruct() {
  419.           $this->sendRoom("%xt%rp%-1%" . $this->getID() . "%");
  420.       }
  421.       public function getName() {
  422.           return $this->username;
  423.       }
  424.       public function getEmail() {
  425.           return $this->email;
  426.       }
  427.       public function getID() {
  428.           return $this->id;
  429.       }
  430.       public function getHead() {
  431.           return $this->head;
  432.       }
  433.       public function getFace() {
  434.           return $this->face;
  435.       }
  436.       public function getNeck() {
  437.           return $this->neck;
  438.       }
  439.       public function getBody() {
  440.           return $this->body;
  441.       }
  442.       public function getHands() {
  443.           return $this->hands;
  444.       }
  445.       public function getFeet() {
  446.           return $this->feet;
  447.       }
  448.       public function getPin() {
  449.           return $this->pin;
  450.       }
  451.       public function getPhoto() {
  452.           return $this->photo;
  453.       }
  454.       public function getColour() {
  455.           return $this->colour;
  456.       }
  457.       public function getAge() {
  458.           return $this->age;
  459.       }
  460.       public function getCoins() {
  461.           return $this->coins;
  462.       }
  463.       public function getX() {
  464.           return $this->x;
  465.       }
  466.       public function getY() {
  467.           return $this->y;
  468.       }
  469.       public function getInventory() {
  470.           return $this->inventory;
  471.       }
  472.       public function getFrame() {
  473.           return $this->frame;
  474.       }
  475.       public function setHead($id) {
  476.           $id = $this->parent->mysql->escape($id);
  477.           $this->parent->mysql->query("UPDATE {$this->parent->config->mysql->userTableName} SET curhead='$id' WHERE id='" . $this->getID() . "';");
  478.           $this->sendRoom("%xt%uph%-1%{$this->getID()}%" . $id . "%");
  479.           $this->head = $id;
  480.       }
  481.       public function setFace($id) {
  482.           $id = $this->parent->mysql->escape($id);
  483.           $this->parent->mysql->query("UPDATE {$this->parent->config->mysql->userTableName} SET curface='$id' WHERE id='" . $this->getID() . "';");
  484.           $this->sendRoom("%xt%upf%-1%{$this->getID()}%" . $id . "%");
  485.           $this->face = $id;
  486.       }
  487.       public function setNeck($id) {
  488.           $id = $this->parent->mysql->escape($id);
  489.           $this->parent->mysql->query("UPDATE {$this->parent->config->mysql->userTableName} SET curneck='$id' WHERE id='" . $this->getID() . "';");
  490.           $this->sendRoom("%xt%upn%-1%{$this->getID()}%" . $id . "%");
  491.           $this->neck = $id;
  492.       }
  493.       public function setBody($id) {
  494.           $id = $this->parent->mysql->escape($id);
  495.           $this->parent->mysql->query("UPDATE {$this->parent->config->mysql->userTableName} SET curbody='$id' WHERE id='" . $this->getID() . "';");
  496.           $this->sendRoom("%xt%upb%-1%{$this->getID()}%" . $id . "%");
  497.           $this->body = $id;
  498.       }
  499.       public function setHands($id) {
  500.           $id = $this->parent->mysql->escape($id);
  501.           $this->parent->mysql->query("UPDATE {$this->parent->config->mysql->userTableName} SET curhands='$id' WHERE id='" . $this->getID() . "';");
  502.           $this->sendRoom("%xt%upa%-1%{$this->getID()}%" . $id . "%");
  503.           $this->hands = $id;
  504.       }
  505.       public function setFeet($id) {
  506.           $id = $this->parent->mysql->escape($id);
  507.           $this->parent->mysql->query("UPDATE {$this->parent->config->mysql->userTableName} SET curfeet='$id' WHERE id='" . $this->getID() . "';");
  508.           $this->sendRoom("%xt%upe%-1%{$this->getID()}%" . $id . "%");
  509.           $this->feet = $id;
  510.       }
  511.       public function setPin($id) {
  512.           $id = $this->parent->mysql->escape($id);
  513.           $this->parent->mysql->query("UPDATE {$this->parent->config->mysql->userTableName} SET curflag='$id' WHERE id='" . $this->getID() . "';");
  514.           $this->sendRoom("%xt%upl%-1%{$this->getID()}%" . $id . "%");
  515.           $this->pin = $id;
  516.       }
  517.       public function setPhoto($id) {
  518.           $id = $this->parent->mysql->escape($id);
  519.           $this->parent->mysql->query("UPDATE {$this->parent->config->mysql->userTableName} SET curphoto='$id' WHERE id='" . $this->getID() . "';");
  520.           $this->sendRoom("%xt%upp%-1%{$this->getID()}%" . $id . "%");
  521.           $this->photo = $id;
  522.       }
  523.       public function setColour($id) {
  524.           $id = $this->parent->mysql->escape($id);
  525.           $this->parent->mysql->query("UPDATE {$this->parent->config->mysql->userTableName} SET colour='$id' WHERE id='" . $this->getID() . "';");
  526.           $this->sendRoom("%xt%upc%-1%{$this->getID()}%" . $id . "%");
  527.           $this->colour = $id;
  528.       }
  529.       public function setCoins($coins) {
  530.           $coins = $this->parent->mysql->escape($coins);
  531.           $this->parent->mysql->query("UPDATE {$this->parent->config->mysql->userTableName} SET coins='$coins' WHERE id='" . $this->getID() . "';");
  532.           $this->sendPacket("%xt%zo%-1%" . $coins . "%Open%CP%");
  533.       }
  534.       public function setXY($x, $y) {
  535.           $this->x = $x;
  536.           $this->y = $y;
  537.           $this->sendRoom("%xt%sp%-1%" . $this->getID() . "%$x%$y%");
  538.       }
  539.       public function setFrame($frame) {
  540.           $this->frame = $frame;
  541.           $this->sendRoom("%xt%sf%-1%" . $this->getID() . "%" . $frame . "%");
  542.       }
  543.       public function setAction($action) {
  544.           $this->frame = 1;
  545.           $this->sendRoom("%xt%sf%-1%" . $this->getID() . "%" . $action . "%");
  546.       }
  547.       public function speak($msg = "I need friends") {
  548.           $this->sendRoom("%xt%sm%-1%" . $this->getID() . "%" . htmlspecialchars($msg) . "%");
  549.           $this->parent->handleCommand($this, $msg);
  550.       }
  551.       public function resetDetails() {
  552.           $res = $this->parent->mysql->returnArray("SELECT * FROM {$this->parent->config->mysql->userTableName} WHERE id='" . $this->getID() . "'");
  553.          
  554.           $res = $res[0];
  555.           $this->username = $res["nickname"];
  556.               $this->isMember = $res["isMember"];
  557.           $this->head = $res["curhead"];
  558.           $this->mood = $res["mood"];
  559.           $this->face = $res["curface"];
  560.           $this->neck = $res["curneck"];
  561.           $this->body = $res["curbody"];
  562.           $this->hands = $res["curhands"];
  563.           $this->feet = $res["curfeet"];
  564.           $this->pin = $res["curflag"];
  565.           $this->photo = $res["curphoto"];
  566.           $this->colour = $res["colour"];
  567.           $this->age = round((strtotime("NOW") - strtotime($res['joindate'])) / (60 * 60 * 24));
  568.           $this->coins = $res["coins"];
  569.           $this->isModerator = $res["ismoderator"];
  570.           $this->inventory = explode(",", $res["items"]);
  571.           if($this->inventory[0] == "0")
  572.               array_shift($this->inventory);
  573.           $this->buddies = explode(",", $res["buddies"]);
  574.           $this->rank = $res["rank"];
  575.       }
  576.       public function getBuddyStr(){
  577.       $buddyStr = "";
  578.           foreach($this->buddies as $buddyID){
  579.               $buddyInfo = $this->parent->mysql->returnArray("SELECT * FROM {$this->parent->config->mysql->userTableName} WHERE id='" . $this->parent->mysql->escape($buddyID) . "';");
  580.               $buddyName = $buddyInfo[0]["nickname"];
  581.               $isOnline = false;
  582.               foreach($this->parent->users as &$user){
  583.                   if($user->getID() == $buddyID){
  584.                       $isOnline = true;
  585.                       break;
  586.                   }
  587.               }
  588.               $buddyStr .= "$buddyID|" . $buddyName . "|" . $isOnline . "%";
  589.           }
  590.           if($buddyStr == "")
  591.               $buddyStr = "%";
  592.           return $buddyStr;
  593.       }
  594.       public function requestBuddy($id){
  595.           $isOnline = false;
  596.           foreach($this->parent->users as &$user){
  597.               if($user->getID() == $id){
  598.                   $isOnline = true;
  599.                   break;
  600.               }
  601.           }
  602.           if($isOnline){
  603.               $user->buddyRequests[$this->getID()] = true;
  604.               $user->sendPacket("%xt%br%-1%" . $this->parent->mysql->escape($this->getID()) . "%" . $this->parent->mysql->escape($this->getName()) . "%");
  605.           }
  606.       }
  607.       public function acceptBuddy($id){
  608.           $isOnline = false;
  609.           foreach($this->parent->users as &$user){
  610.               if($user->getID() == $id){
  611.                   $isOnline = true;
  612.                   break;
  613.               }
  614.           }
  615.           if($isOnline == false){ return $this->kick(); }
  616.           if($this->buddyRequests[$id] != true){ return $this->kick(); }
  617.           unset($user->buddyRequests[$this->getID()]);
  618.           $this->buddies[$id] = $id;
  619.           $user->buddies[$this->getID()] = $this->getID();
  620.           $this->parent->mysql->query("UPDATE {$this->parent->config->mysql->userTableName} SET buddies='" . $this->parent->mysql->escape(implode(",", $this->buddies)) . "' WHERE id='" . $this->getID() . "';");
  621.           $this->parent->mysql->query("UPDATE {$this->parent->config->mysql->userTableName} SET buddies='" . $this->parent->mysql->escape(implode(",", $user->buddies)) . "' WHERE id='" . $user->getID() . "';");
  622.           $user->sendPacket("%xt%ba%-1%" . $this->getID() . "%" . $this->getName() . "%");
  623.       }
  624.       public function removeBuddy($id){
  625.           foreach($this->parent->users as &$user){
  626.               if($user->getID() == $id){
  627.                   break;
  628.               }
  629.           }
  630.           unset($this->buddies[$id]);
  631.           unset($user->buddies[$id]);
  632.           $this->parent->mysql->query("UPDATE {$this->parent->config->mysql->userTableName} SET buddies='" . $this->parent->mysql->escape(implode(",", $this->buddies)) . "' WHERE id='" . $this->getID() . "';");
  633.           $this->parent->mysql->query("UPDATE {$this->parent->config->mysql->userTableName} SET buddies='" . $this->parent->mysql->escape(implode(",", $user->buddies)) . "' WHERE id='" . $user->getID() . "';");
  634.           $user->sendPacket("%xt%rb%-1%" . $this->getID() . "%" . $this->getName() . "%");
  635.       }
  636.       public function findBuddy($id){
  637.           foreach($this->parent->users as &$user){
  638.               if($user->getID() == $id){
  639.                   break;
  640.               }
  641.           }
  642.           $this->sendPacket("%xt%bf%-1%" . $user->room . "%");
  643.       }
  644.       public function getRoomCount() {
  645.           $i = 0;
  646.           foreach ($this->parent->users as $user) {
  647.               if ($user->room == $this->room)
  648.                   $i++;
  649.           }
  650.           return $i;
  651.       }
  652.     public function joinRoom($id = 100, $x = 330, $y = 300) {
  653.             $this->resetDetails();
  654.             if ($this->getRoomCount() > 50)
  655.                $this->sendPacket("%xt%e%-1%210%");
  656.             else {
  657.                $this->sendRoom("%xt%rp%-1%" . $this->getID() . "%");
  658.                $this->x = $x;
  659.                $this->room = $id;
  660.                $this->y = $y;
  661.                $s = "%xt%jr%-1%$id%" . $this->getString() . "%";
  662.                $s .= "0|$BotName|1|14|413|442|0|0|0|0|0|0|380|300|0|1|999|0|0|Bot%";
  663.                foreach ($this->getUserList() as $user) $s .= $user->getString() . "%";
  664.                $this->sendPacket($s);
  665.                $this->sendRoom("%xt%ap%-1%" . $this->getString() . "%");
  666.             }
  667.             sleep(3);
  668.          }
  669.       public function sendRoom($packet) {
  670.           foreach ($this->parent->users as $user) {
  671.               if ($user->room == $this->room)
  672.                   $user->sendPacket($packet);
  673.           }
  674.       }
  675.       public function getUserList() {
  676.           $users = array();
  677.           foreach ($this->parent->users as &$user) {
  678.               if ($user->room == $this->room)
  679.                   $users[] = $user;
  680.           }
  681.           return $users;
  682.       }
  683.       public function sendPacket($packet) {
  684.           if (@stristr($packet, strlen($packet) - 1, 1) != chr(0))
  685.               $packet = $packet . chr(0);
  686.           if(!socket_write($this->sock, $packet, strlen($packet))){
  687.               $this->selfDestruct = true;
  688.           }
  689.       }
  690.       public function getDetails() {
  691.           return array($this->getID(), $this->getName(), "1", $this->getColour(), $this->getHead(), $this->getFace(), $this->getNeck(), $this->getBody(), $this->getHands(), $this->getFeet(), $this->getPin(), $this->getPhoto(), $this->getX(), $this->getY(), $this->getFrame(), (int)$this->getMember(), $this->getRank() * 146,0,0,$this->getMood);
  692.       }
  693.       public function getRank(){
  694.           return $this->rank;
  695.       }
  696.       public function getString() {
  697.           return implode("|", $this->getDetails());
  698.       }
  699.       public function addItem($id) {
  700.           global $crumbs;
  701.           if ($crumbs[$id] == null)
  702.               $this->sendPacket("%xt%e%-1%402%");
  703.           elseif (in_array($id, $this->inventory))
  704.               $this->sendPacket("%xt%e%-1%400%");
  705.           elseif ($this->coins < $crumbs[$id]["cost"])
  706.               $this->sendPacket("%xt%e%-1%401%");
  707.           else {
  708.               $this->inventory[] = $id;
  709.               $this->coins = $this->coins - $crumbs[$id]["cost"];
  710.               $this->parent->mysql->query("UPDATE {$this->parent->config->mysql->userTableName} SET items='" . implode(",", $this->inventory) . "', coins='" . $this->getCoins() . "' WHERE id='" . $this->getID() . "';");
  711.               $this->sendPacket("%xt%ai%-1%" . $id . "%" . $this->getCoins() . "%");
  712.           }
  713.       }
  714.       public function timerKick($minutes, $from){
  715.           $this->sendPacket("%xt%tk%-1%$minutes%$from%");
  716.       }
  717.       public function kick(){
  718.           $this->sendPacket("%xt%e%-1%5%");
  719.       }
  720.   }
  721.   class MySQL {
  722.       public $host;
  723.       public $username;
  724.       public $password;
  725.       private $ref;
  726.       public function mysql() {
  727.       }
  728.       public function connect($host, $username, $password) {
  729.           $this->ref = @mysql_connect($host, $username, $password);
  730.           $this->host = $host;
  731.           $this->username = $username;
  732.           $this->password = $password;
  733.           if ($this->ref == false)
  734.               return false;
  735.           else
  736.               return true;
  737.       }
  738.       public function escape($string) {
  739.           $this->checkConnection();
  740.           return @mysql_real_escape_string($string, $this->ref);
  741.       }
  742.       public function getError() {
  743.           $this->checkConnection();
  744.           return mysql_error($this->ref);
  745.       }
  746.       public function selectDB($db) {
  747.           $this->checkConnection();
  748.           $newRes = @mysql_select_db($db, $this->ref);
  749.           if ($newRes == true)
  750.               return true;
  751.           else
  752.               return false;
  753.       }
  754.       public function query($query) {
  755.           $this->checkConnection();
  756.           return @mysql_query($query, $this->ref);
  757.       }
  758.       public function getRows($query) {
  759.           $this->checkConnection();
  760.           $result = $this->query($query);
  761.           return @mysql_num_rows($result);
  762.       }
  763.       public function returnArray($query) {
  764.           $this->checkConnection();
  765.           $result = $this->query($query);
  766.          
  767.           if (@mysql_num_rows($result) != 0) {
  768.               $arr = array();
  769.               while ($row = @mysql_fetch_assoc($result))
  770.                   $arr[] = $row;
  771.               return $arr;
  772.           } else
  773.               return array();
  774.       }
  775.       public function checkConnection() {
  776.           @$this->connect($this->host, $this->username, $this->password);
  777.       }
  778.       public function disconnect(){
  779.           return @mysql_close($this->ref);
  780.       }
  781.       public function __destruct(){
  782.           $this->disconnect();
  783.           sleep(4);
  784.       }
  785.   }
  786. ?>
Add Comment
Please, Sign In to add comment