Advertisement
Guest User

Untitled

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