From 3a43b2f5db0d6501aea79f9ed5e0b1f97d63600a Mon Sep 17 00:00:00 2001 From: Ulrich Block Date: Sun, 26 Jan 2014 13:04:36 +0100 Subject: [PATCH] #17 + #79: Refactor XML API repsonse --- web/startupdates.php | 4 +- web/stuff/api_gserver.php | 688 ++++++++++++++++++++++++++------------ 2 files changed, 467 insertions(+), 225 deletions(-) diff --git a/web/startupdates.php b/web/startupdates.php index 53c2e525..d8211255 100644 --- a/web/startupdates.php +++ b/web/startupdates.php @@ -57,11 +57,11 @@ include(EASYWIDIR . '/stuff/keyphrasefile.php'); if (!isset($ip) or $ui->escaped('SERVER_ADDR', 'server') == $ip or in_array($ip, ipstoarray($rSA['cronjob_ips']))) { - echo "Start Syncs and Updates loading...\r\n"; - $currentHour = date('G'); $currentMinute = (int) date('i'); + echo "Start Syncs and Updates. Hour is ${currentHour} and minute is ${currentMinute}\r\n"; + $query = $sql->prepare("SELECT `lastUpdateRun` FROM `settings` WHERE `resellerid`=0 LIMIT 1"); $query->execute(); $lastUpdateRun= (int) $query->fetchColumn(); diff --git a/web/stuff/api_gserver.php b/web/stuff/api_gserver.php index 54460bed..5e0fed86 100644 --- a/web/stuff/api_gserver.php +++ b/web/stuff/api_gserver.php @@ -1,4 +1,5 @@ $licenceDetails['lG']) { + +if (!isset($success['false']) and array_value_exists('action', 'add', $data) and 1 > $licenceDetails['lG']) { $success['false'][] = 'licence limit reached'; -} else if (!isset($success['false']) and array_value_exists('action','add',$data) and $licenceDetails['lG']>0) { - if (dataExist('identify_user_by',$data) and isid($data['slots'],11)) { + +} else if (!isset($success['false']) and array_value_exists('action', 'add', $data) and $licenceDetails['lG'] > 0) { + + if (dataExist('identify_user_by', $data) and isid($data['slots'], 11)) { + if (is_array($data['shorten']) or is_object($data['shorten'])) { $shorten = $data['shorten']; } else { - $shorten=array($data['shorten']); + $shorten = array($data['shorten']); } - if (count($shorten)==0) { + + if (count($shorten) == 0) { + $success['false'][] = 'No gameshorten(s) has been send'; + } else { - $active=active_check($data['active']); - $private=active_check($data['private']); - $slots=isid($data['slots'],11); - $identifyUserBy = $data['identify_user_by']; - $localUserID=isid($data['user_localid'],21); - $externalUserID = $data['user_externalid']; - $username = $data['username']; - $identifyServerBy = $data['identify_server_by']; - $localServerID=isid($data['server_local_id'],19); - $externalServerID = $data['server_external_id']; - $from=array('user_localid' => 'id','username' => 'cname','user_externalid' => 'externalID','email' => 'mail'); - $query = $sql->prepare("SELECT `id`,`cname` FROM `userdata` WHERE `".$from[$data['identify_user_by']]."`=? AND `resellerid`=?"); - $query->execute(array($data[$data['identify_user_by']],$resellerID)); - foreach ($query->fetchAll(PDO::FETCH_ASSOC) as $row) { - $localUserLookupID = $row['id']; - } - if (!isset($localUserLookupID)) { - $success['false'][] = 'user does not exist'; - } - $query = $sql->prepare("SELECT * FROM `servertypes` WHERE `shorten`=? AND `resellerid`=? LIMIT 1"); + $typeIDs = array(); $typeIDList = array(); $shortenToID = array(); + + $from = array('user_localid' => 'id', 'username' => 'cname', 'user_externalid' => 'externalID', 'email' => 'mail'); + + $active = active_check($data['active']); + $private = active_check($data['private']); + $slots = isid($data['slots'], 11); + $identifyUserBy = $data['identify_user_by']; + $localUserID = isid($data['user_localid'], 21); + $externalUserID = $data['user_externalid']; + $username = $data['username']; + $identifyServerBy = $data['identify_server_by']; + $localServerID = isid($data['server_local_id'], 19); + $externalServerID = $data['server_external_id']; + + $query = $sql->prepare("SELECT `id`,`cname` FROM `userdata` WHERE `" . $from[$data['identify_user_by']] . "`=? AND `resellerid`=? LIMIT 1"); + $query->execute(array($data[$data['identify_user_by']], $resellerID)); + $localUserLookupID = $query->fetchColumn(); + + if (!isid($localUserLookupID, 11)) { + $success['false'][] = 'user does not exist'; + } + + $query = $sql->prepare("SELECT * FROM `servertypes` WHERE `shorten`=? AND `resellerid`=? LIMIT 1"); + foreach ($shorten as $singleShorten) { - $query->execute(array($singleShorten,$resellerID)); + + $query->execute(array($singleShorten, $resellerID)); foreach ($query->fetchAll(PDO::FETCH_ASSOC) as $row) { - if (!isset($portMax) or $row['portMax']>$portMax or (isset($data['primary']) and gamestring($data['primary']) and $row['portMax']<=$portMax and $singleShorten==$data['primary'])) { + + if (!isset($portMax) or $row['portMax'] > $portMax or (isset($data['primary']) and gamestring($data['primary']) and $row['portMax'] <= $portMax and $singleShorten == $data['primary'])) { $portStep = $row['portStep']; $portMax = $row['portMax']; $port = $row['portOne']; @@ -129,346 +148,447 @@ if (!isset($success['false']) and array_value_exists('action','add',$data) and 1 $port4 = $row['portFour']; $port5 = $row['portFive']; } + $typeIDList[] = $row['id']; $shortenToID[$row['id']] = $singleShorten; $typeIDs[$singleShorten] = array('id' => $row['id'], 'map' => $row['map'], 'mapGroup' => $row['mapGroup'], 'tic' => $row['tic'], 'cmd' => $row['cmd'], 'gamemod' => $row['gamemod'], 'gamemod2' => $row['gamemod2']); } + if (!isset($typeIDs[$singleShorten])) { $success['false'][] = 'image with the shorten '.$singleShorten.' does not exists'; } + } - if (!isset($success['false']) and !in_array($externalServerID,$bad)) { + + if (!isset($success['false']) and !in_array($externalServerID, $bad)) { $query = $sql->prepare("SELECT COUNT(`id`) AS `amount` FROM `gsswitch` WHERE `externalID`=? LIMIT 1"); $query->execute(array($externalServerID)); - if ($query->fetchColumn()>0) { + + if ($query->fetchColumn() > 0) { $success['false'][] = 'server with external ID already exists'; } } + if (!isset($success['false'])) { - $masterServerCount=count($typeIDList); - if ($masterServerCount==1) { - $implodedQuery='m.`servertypeid`='.$typeIDList[0]; + + $masterServerCount = count($typeIDList); + + if ($masterServerCount == 1) { + $implodedQuery = 'm.`servertypeid`=' . $typeIDList[0]; } else { - $implodedQuery='(m.`servertypeid`='.implode(' OR m.`servertypeid`=',$typeIDList).')'; + $implodedQuery = '(m.`servertypeid`=' . implode(' OR m.`servertypeid`=', $typeIDList) . ')'; } - if (isset($data['master_server_id']) and isid($data['master_server_id'],19)) { + + if (isset($data['master_server_id']) and isid($data['master_server_id'], 19)) { + $query = $sql->prepare("SELECT r.`id`,r.`externalID`,r.`ip`,r.`altips`,r.`maxslots`,r.`maxserver`,r.`active` AS `hostactive`,r.`resellerid` AS `resellerid`,(r.`maxserver`-(SELECT COUNT(`id`) FROM gsswitch g WHERE g.`rootID`=r.`id` )) AS `freeserver`,(r.`maxslots`-(SELECT SUM(g.`slots`) FROM gsswitch g WHERE g.`rootID`=r.`id`)) AS `leftslots`,(SELECT COUNT(m.`id`) FROM `rservermasterg`m WHERE m.`serverid`=r.`id` AND $implodedQuery) `mastercount` FROM `rserverdata` r GROUP BY r.`id` HAVING (r.`id`=? AND `hostactive`='Y' AND r.`resellerid`=? AND (`freeserver`>0 OR `freeserver` IS NULL) AND (`leftslots`>? OR `leftslots` IS NULL) AND `mastercount`=?) ORDER BY `freeserver` DESC LIMIT 1"); - $query->execute(array($data['master_server_id'],$resellerID,$slots,$masterServerCount)); - } else if (isset($data['master_server_external_id']) and wpreg_check($data['master_server_external_id'],255)) { + $query->execute(array($data['master_server_id'], $resellerID, $slots, $masterServerCount)); + + } else if (isset($data['master_server_external_id']) and wpreg_check($data['master_server_external_id'], 255)) { + $query = $sql->prepare("SELECT r.`id`,r.`externalID`,r.`ip`,r.`altips`,r.`maxslots`,r.`maxserver`,r.`active` AS `hostactive`,r.`resellerid` AS `resellerid`,(r.`maxserver`-(SELECT COUNT(`id`) FROM gsswitch g WHERE g.`rootID`=r.`id` )) AS `freeserver`,(r.`maxslots`-(SELECT SUM(g.`slots`) FROM gsswitch g WHERE g.`rootID`=r.`id`)) AS `leftslots`,(SELECT COUNT(m.`id`) FROM `rservermasterg`m WHERE m.`serverid`=r.`id` AND $implodedQuery) `mastercount` FROM `rserverdata` r GROUP BY r.`id` HAVING (r.`externalID`=? AND `hostactive`='Y' AND r.`resellerid`=? AND (`freeserver`>0 OR `freeserver` IS NULL) AND (`leftslots`>? OR `leftslots` IS NULL) AND `mastercount`=?) ORDER BY `freeserver` DESC LIMIT 1"); - $query->execute(array($data['master_server_external_id'],$resellerID,$slots,$masterServerCount)); + $query->execute(array($data['master_server_external_id'], $resellerID, $slots, $masterServerCount)); + } else { + $query = $sql->prepare("SELECT r.`id`,r.`externalID`,r.`ip`,r.`altips`,r.`maxslots`,r.`maxserver`,r.`active` AS `hostactive`,r.`resellerid` AS `resellerid`,(r.`maxserver`-(SELECT COUNT(`id`) FROM gsswitch g WHERE g.`rootID`=r.`id` )) AS `freeserver`,(r.`maxslots`-(SELECT SUM(g.`slots`) FROM gsswitch g WHERE g.`rootID`=r.`id`)) AS `leftslots`,(SELECT COUNT(m.`id`) FROM `rservermasterg`m WHERE m.`serverid`=r.`id` AND $implodedQuery) `mastercount` FROM `rserverdata` r GROUP BY r.`id` HAVING (`hostactive`='Y' AND r.`resellerid`=? AND (`freeserver`>0 OR `freeserver` IS NULL) AND (`leftslots`>? OR `leftslots` IS NULL) AND `mastercount`=?) ORDER BY `freeserver` DESC LIMIT 1"); - $query->execute(array($resellerID,$slots,$masterServerCount)); + $query->execute(array($resellerID, $slots, $masterServerCount)); + } + foreach ($query->fetchAll(PDO::FETCH_ASSOC) as $row) { $hostID = $row['id']; $hostExternalID = $row['externalID']; $ips[] = $row['ip']; - foreach (preg_split('/\r\n/', $row['altips'],-1,PREG_SPLIT_NO_EMPTY) as $ip) { + + foreach (preg_split('/\r\n/', -1, PREG_SPLIT_NO_EMPTY) as $ip) { $ips[] = $ip; } } + if (isset($ips)) { - $used=usedPorts($ips); + + $used = usedPorts($ips); $ip = $used['ip']; $ports = $used['ports']; - } else if (isset($data['master_server_id']) and isid($data['master_server_id'],19)) { + + } else if (isset($data['master_server_id']) and isid($data['master_server_id'], 19)) { + $missing = array(); + $query = $sql->prepare("SELECT r.`id` FROM `rserverdata` r LEFT JOIN `rservermasterg` m ON m.`serverid`=r.`id` WHERE r.`id`=? AND r.`active`='Y' AND r.`resellerid`=? AND m.`servertypeid`=? LIMIT 1"); foreach ($typeIDList as $ID) { - $query->execute(array($data['master_server_id'],$resellerID,$ID)); - if ($query->rowCount()==0) { + $query->execute(array($data['master_server_id'], $resellerID, $ID)); + + if ($query->rowCount() == 0) { $missing[] = $shortenToID[$ID]; } } - } else if (isset($data['master_server_external_id']) and wpreg_check($data['master_server_external_id'],255)) { + + } else if (isset($data['master_server_external_id']) and wpreg_check($data['master_server_external_id'], 255)) { + $missing = array(); + $query = $sql->prepare("SELECT r.`id` FROM `rserverdata` r LEFT JOIN `rservermasterg` m ON m.`serverid`=r.`id` WHERE r.`externalID`=? AND r.`active`='Y' AND r.`resellerid`=? AND m.`servertypeid`=? LIMIT 1"); foreach ($typeIDList as $ID) { - $query->execute(array($data['master_server_external_id'],$resellerID,$ID)); - if ($query->rowCount()==0) { + $query->execute(array($data['master_server_external_id'], $resellerID, $ID)); + if ($query->rowCount() == 0) { $missing[] = $shortenToID[$ID]; } } } else { $missing = $shorten; } + if (isset($missing) and count($missing)>0) { - $success['false'][] = 'No free host with shorten(s): '.implode(', ',$missing); + $success['false'][] = 'No free host with shorten(s): '.implode(', ', $missing); } } - if (!isset($success['false']) and isip($ip,'ip4')) { + + if (!isset($success['false']) and isip($ip, 'ip4')) { + if ($portMax==1) { - if (isset($data['port']) and checkPorts(array($data['port']),$ports) === true) { + + if (isset($data['port']) and checkPorts(array($data['port']), $ports) === true) { $port = $data['port']; } - while (in_array($port,$ports)) { - $port+=$portStep; + + while (in_array($port, $ports)) { + $port += $portStep; } + $port2 = ''; $port3 = ''; $port4 = ''; $port5 = ''; - } else if ($portMax==2) { - if (isset($data['port'],$data['port2']) and checkPorts(array($data['port'],$data['port2']),$ports) === true) { + + } else if ($portMax == 2) { + + if (isset($data['port'], $data['port2']) and checkPorts(array($data['port'], $data['port2']), $ports) === true) { $port = $data['port']; $port2 = $data['port2']; } - while (in_array($port,$ports) or in_array($port2,$ports)) { - $port+=$portStep; - $port2+=$portStep; + + while (in_array($port, $ports) or in_array($port2, $ports)) { + $port += $portStep; + $port2 += $portStep; } + $port3 = ''; $port4 = ''; $port5 = ''; - } else if ($portMax==3) { - if (isset($data['port'],$data['port2'],$data['port3']) and checkPorts(array($data['port'],$data['port2'],$data['port3']),$ports) === true) { + + } else if ($portMax == 3) { + + if (isset($data['port'], $data['port2'], $data['port3']) and checkPorts(array($data['port'], $data['port2'], $data['port3']), $ports) === true) { $port = $data['port']; $port2 = $data['port2']; $port3 = $data['port3']; } - while (in_array($port,$ports) or in_array($port2,$ports) or in_array($port3,$ports)) { - $port+=$portStep; - $port2+=$portStep; - $port3+=$portStep; + + while (in_array($port, $ports) or in_array($port2, $ports) or in_array($port3, $ports)) { + $port += $portStep; + $port2 += $portStep; + $port3 += $portStep; } + $port4 = ''; $port5 = ''; + } else if ($portMax==4) { - if (isset($data['port'],$data['port2'],$data['port3'],$data['port4']) and checkPorts(array($data['port'],$data['port2'],$data['port3'],$data['port4']),$ports) === true) { + + if (isset($data['port'], $data['port2'], $data['port3'], $data['port4']) and checkPorts(array($data['port'], $data['port2'], $data['port3'], $data['port4']), $ports) === true) { $port = $data['port']; $port2 = $data['port2']; $port3 = $data['port3']; $port4 = $data['port4']; } - while (in_array($port,$ports) or in_array($port2,$ports) or in_array($port3,$ports) or in_array($port4,$ports)) { - $port+=$portStep; - $port2+=$portStep; - $port3+=$portStep; - $port4+=$portStep; + + while (in_array($port, $ports) or in_array($port2, $ports) or in_array($port3, $ports) or in_array($port4, $ports)) { + $port += $portStep; + $port2 += $portStep; + $port3 += $portStep; + $port4 += $portStep; } + $port5 = ''; + } else { - if (isset($data['port'],$data['port2'],$data['port3'],$data['port4'],$data['port5']) and checkPorts(array($data['port'],$data['port2'],$data['port3'],$data['port4'],$data['port5']),$ports) === true) { + + if (isset($data['port'], $data['port2'], $data['port3'], $data['port4'], $data['port5']) and checkPorts(array($data['port'], $data['port2'], $data['port3'], $data['port4'], $data['port5']), $ports) === true) { $port = $data['port']; $port2 = $data['port2']; $port3 = $data['port3']; $port4 = $data['port4']; $port5 = $data['port5']; } - while (in_array($port,$ports) or in_array($port2,$ports) or in_array($port3,$ports) or in_array($port4,$ports) or in_array($port5,$ports)) { - $port+=$portStep; - $port2+=$portStep; - $port3+=$portStep; - $port4+=$portStep; - $port5+=$portStep; + + while (in_array($port, $ports) or in_array($port2, $ports) or in_array($port3, $ports) or in_array($port4, $ports) or in_array($port5, $ports)) { + $port += $portStep; + $port2 += $portStep; + $port3 += $portStep; + $port4 += $portStep; + $port5 += $portStep; } + } - $initialpassword=passwordgenerate(10); - $taskset=(isset($data['taskset']) and active_check($data['taskset'])) ? $data['taskset'] : 'N'; - $eacallowed=(isset($data['eacallowed']) and active_check($data['eacallowed'])) ? $data['eacallowed'] : 'N'; - $brandname=(isset($data['brandname']) and active_check($data['brandname'])) ? $data['brandname'] : 'N'; - $tvenable=(isset($data['tvenable']) and active_check($data['tvenable'])) ? $data['tvenable'] : 'N'; - $pallowed=(isset($data['pallowed']) and active_check($data['pallowed'])) ? $data['pallowed'] : 'N'; - $autoRestart=(isset($data['autoRestart']) and active_check($data['autoRestart'])) ? $data['autoRestart'] : 'Y'; - $minram=(isset($data['minram']) and isid($data['minram'],10)) ? $data['minram'] : ''; - $maxram=(isset($data['maxram']) and isid($data['maxram'],10)) ? $data['maxram'] : ''; - $cores=(isset($data['cores']) and cores($data['cores'])) ? $data['cores'] : ''; - if (isset($data['installGames']) and wpreg_check($data['installGames'],1)) { + + $initialpassword = passwordgenerate(10); + $taskset = (isset($data['taskset']) and active_check($data['taskset'])) ? $data['taskset'] : 'N'; + $eacallowed = (isset($data['eacallowed']) and active_check($data['eacallowed'])) ? $data['eacallowed'] : 'N'; + $brandname = (isset($data['brandname']) and active_check($data['brandname'])) ? $data['brandname'] : 'N'; + $tvenable = (isset($data['tvenable']) and active_check($data['tvenable'])) ? $data['tvenable'] : 'N'; + $pallowed = (isset($data['pallowed']) and active_check($data['pallowed'])) ? $data['pallowed'] : 'N'; + $autoRestart = (isset($data['autoRestart']) and active_check($data['autoRestart'])) ? $data['autoRestart'] : 'Y'; + $minram = (isset($data['minram']) and isid($data['minram'], 10)) ? $data['minram'] : ''; + $maxram = (isset($data['maxram']) and isid($data['maxram'], 10)) ? $data['maxram'] : ''; + $cores = (isset($data['cores']) and cores($data['cores'])) ? $data['cores'] : ''; + + if (isset($data['installGames']) and wpreg_check($data['installGames'], 1)) { $installGames = $data['installGames']; } - $json=json_encode(array('installGames' => $installGames)); + + $json = json_encode(array('installGames' => $installGames)); + $query = $sql->prepare("INSERT INTO `gsswitch` (`active`,`taskset`,`cores`,`userid`,`pallowed`,`eacallowed`,`serverip`,`rootID`,`tvenable`,`port`,`port2`,`port3`,`port4`,`port5`,`minram`,`maxram`,`slots`,`war`,`brandname`,`autoRestart`,`ftppassword`,`resellerid`,`externalID`,`serverid`,`stopped`) VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,AES_ENCRYPT(?,?),?,?,1,'Y')"); - $query->execute(array($active,$taskset,$cores,$localUserLookupID,$pallowed,$eacallowed,$ip,$hostID,$tvenable,$port,$port2,$port3,$port4,$port5,$minram,$maxram,$slots,$private,$brandname,$autoRestart,$initialpassword,$aeskey,$resellerID,$externalServerID)); + $query->execute(array($active, $taskset, $cores, $localUserLookupID, $pallowed, $eacallowed, $ip, $hostID, $tvenable, $port, $port2, $port3, $port4, $port5, $minram, $maxram, $slots, $private, $brandname, $autoRestart, $initialpassword, $aeskey, $resellerID, $externalServerID)); + $localServerID = $sql->lastInsertId(); - customColumns('G',$localServerID,'save',$data); $customID = $localServerID; - if (isid($localServerID,19)) { + + customColumns('G', $localServerID,'save', $data); + + if (isid($localServerID, 19)) { + $query = $sql->prepare("INSERT INTO `serverlist` (`servertype`,`switchID`,`map`,`mapGroup`,`cmd`,`tic`,`gamemod`,`gamemod2`,`resellerid`) VALUES (?,?,?,?,?,?,?,?,?)"); foreach ($typeIDs as $shorten=>$array) { - $query->execute(array($array['id'],$localServerID,$array['map'],$array['mapGroup'],$array['cmd'],$array['tic'],$array['gamemod'],$array['gamemod2'],$resellerID)); - if (!isset($lastServerID) or (isset($data['primary']) and gamestring($data['primary']) and $shorten==$data['primary'])) { + $query->execute(array($array['id'], $localServerID, $array['map'], $array['mapGroup'], $array['cmd'], $array['tic'], $array['gamemod'], $array['gamemod2'], $resellerID)); + + if (!isset($lastServerID) or (isset($data['primary']) and gamestring($data['primary']) and $shorten == $data['primary'])) { $lastServerID = $sql->lastInsertId(); } } - if (!isset($lastServerID) or !isid($lastServerID,19) ) { + + if (!isset($lastServerID) or !isid($lastServerID, 19) ) { $query = $sql->prepare("SELECT `id` FROM `serverlist` WHERE `switchID`=? AND `resellerid`=? ORDER BY `id` DESC LIMIT 1"); - $query->execute(array($localServerID,$resellerID)); + $query->execute(array($localServerID, $resellerID)); $lastServerID = $query->fetchColumn(); } + $query = $sql->prepare("UPDATE `gsswitch` SET `serverid`=? WHERE `id`=? AND `resellerid`=? LIMIT 1"); - $query->execute(array($lastServerID,$localServerID,$resellerID)); + $query->execute(array($lastServerID, $localServerID, $resellerID)); + $query = $sql->prepare("UPDATE `jobs` SET `status`='2' WHERE `type`='gs' AND (`status` IS NULL OR `status`='1') AND `affectedID`=? and `resellerID`=?"); - $query->execute(array($localServerID,$resellerID)); + $query->execute(array($localServerID, $resellerID)); + $query = $sql->prepare("INSERT INTO `jobs` (`api`,`type`,`hostID`,`invoicedByID`,`affectedID`,`userID`,`name`,`status`,`date`,`action`,`extraData`,`resellerid`) VALUES ('A','gs',?,?,?,?,?,NULL,NOW(),'ad',?,?)"); - $query->execute(array($hostID,$resellerID,$localServerID,$localUserLookupID,$ip . ':' . $port,$json,$resellerID)); + $query->execute(array($hostID, $resellerID, $localServerID, $localUserLookupID, $ip . ':' . $port, $json, $resellerID)); + } else { $success['false'][] = 'Could not write game server to database'; } } } + } else if (!isset($success['false'])) { - $active=active_check($data['active']); - $private=active_check($data['private']); + + $active = active_check($data['active']); + $private = active_check($data['private']); $shorten = $data['shorten']; - $slots=isid($data['slots'],11); + $slots = isid($data['slots'], 11); $identifyUserBy = $data['identify_user_by']; - $localUserID=isid($data['user_localid'],21); + $localUserID = isid($data['user_localid'], 21); $externalUserID = $data['user_externalid']; $username = $data['username']; $identifyServerBy = $data['identify_server_by']; - $localServerID=isid($data['server_local_id'],21); + $localServerID = isid($data['server_local_id'], 21); $externalServerID = $data['server_external_id']; - if (!dataExist('identify_user_by',$data)) { - $success['false'][] = 'Can not identify user or bad email'; - } else { - $success['false'][] = 'Slot amount needs to be specified'; - } + + $success['false'][] = (!dataExist('identify_user_by', $data)) ? 'Can not identify user or bad email' : 'Slot amount needs to be specified'; } -} else if (!isset($success['false']) and array_value_exists('action','mod',$data)) { + +} else if (!isset($success['false']) and array_value_exists('action', 'mod', $data)) { + $identifyUserBy = $data['identify_user_by']; - $localUserID=isid($data['user_localid'],21); + $localUserID = isid($data['user_localid'], 21); $externalUserID = $data['user_externalid']; $username = $data['username']; $identifyServerBy = $data['identify_server_by']; - $localServerID=isid($data['server_local_id'],21); + $localServerID = isid($data['server_local_id'], 21); $externalServerID = $data['server_external_id']; $shorten = $data['shorten']; - $from=array('server_local_id' => 'id','server_external_id' => 'externalID'); + $from = array('server_local_id' => 'id', 'server_external_id' => 'externalID'); $initialpassword = ''; - if (dataExist('identify_server_by',$data)) { + + if (dataExist('identify_server_by', $data)) { + $query = $sql->prepare("SELECT r.`externalID`,g.`id`,g.`serverip`,g.`port`,g.`userid`,g.`active`,g.`slots`,g.`rootID`,g.`war` FROM `gsswitch` g LEFT JOIN `rserverdata` r ON g.`rootID`=r.`id` WHERE g.`".$from[$data['identify_server_by']]."`=? AND g.`resellerid`=? LIMIT 1"); - $query->execute(array($data[$data['identify_server_by']],$resellerID)); + $query->execute(array($data[$data['identify_server_by']], $resellerID)); foreach ($query->fetchAll(PDO::FETCH_ASSOC) as $row) { + $localID = $row['id']; $userID = $row['userid']; $hostID = $row['rootID']; $hostExternalID = $row['externalID']; $oldSlots = $row['slots']; $name = $row['serverip'] . ':' . $row['port']; - $usedPorts=usedPorts(array($row['serverip'])); $oldActive = $row['active']; $oldPort = $row['port']; + $usedPorts = usedPorts(array($row['serverip'])); + $query = $sql->prepare("SELECT COUNT(`jobID`) AS `amount` FROM `jobs` WHERE `affectedID`=? AND `resellerID`=? AND `action`='dl' AND (`status` IS NULL OR `status`='1') LIMIT 1"); - $query->execute(array($localID,$resellerID)); - foreach ($query->fetchAll(PDO::FETCH_ASSOC) as $row) { - if ($row['amount']>0) $success['false'][] = 'Server is marked for deletion'; + $query->execute(array($localID, $resellerID)); + if ($query->fetchColumn() > 0) { + $success['false'][] = 'Server is marked for deletion'; } + $updateArray = array(); $eventualUpdate = ''; + if (isset($data['private']) and active_check($data['private'])) { $updateArray[] = $data['private']; $eventualUpdate .= ',`war`=?'; $private = $data['private']; } - if (isset($data['slots']) and isid($data['slots'],11)) { + + if (isset($data['slots']) and isid($data['slots'], 11)) { $updateArray[] = $data['slots']; $eventualUpdate .= ',`slots`=?'; $slots = $data['slots']; } + if (isset($data['taskset']) and active_check($data['taskset'])) { $updateArray[] = $data['taskset']; $eventualUpdate .= ',`taskset`=?'; $taskset = $data['taskset']; } + if (isset($data['eacallowed']) and active_check($data['eacallowed'])) { $updateArray[] = $data['eacallowed']; $eventualUpdate .= ',`eacallowed`=?'; $eacallowed = $data['eacallowed']; } + if (isset($data['brandname']) and active_check($data['brandname'])) { $updateArray[] = $data['brandname']; $eventualUpdate .= ',`brandname`=?'; $brandname = $data['brandname']; } + if (isset($data['tvenable']) and active_check($data['tvenable'])) { $updateArray[] = $data['tvenable']; $eventualUpdate .= ',`tvenable`=?'; $tvenable = $data['tvenable']; } + if (isset($data['pallowed']) and active_check($data['pallowed'])) { $updateArray[] = $data['pallowed']; $eventualUpdate .= ',`pallowed`=?'; $pallowed = $data['pallowed']; } + if (isset($data['autoRestart']) and active_check($data['autoRestart'])) { $updateArray[] = $data['autoRestart']; $eventualUpdate .= ',`autoRestart`=?'; $autoRestart = $data['autoRestart']; } - if (isset($data['minram']) and isid($data['minram'],10)) { + + if (isset($data['minram']) and isid($data['minram'], 10)) { $updateArray[] = $data['minram']; $eventualUpdate .= ',`minram`=?'; $minram = $data['minram']; } - if (isset($data['maxram']) and isid($data['maxram'],10)) { + + if (isset($data['maxram']) and isid($data['maxram'], 10)) { $updateArray[] = $data['maxram']; $eventualUpdate .= ',`maxram`=?'; $maxram = $data['maxram']; } + if (isset($data['cores']) and cores($data['cores'])) { $updateArray[] = $data['cores']; $eventualUpdate .= ',`cores`=?'; $cores = $data['cores']; } - if (isset($data['port']) and port($data['port']) and !in_array($data['port'],$usedPorts)) { + + if (isset($data['port']) and port($data['port']) and !in_array($data['port'], $usedPorts)) { $updateArray[] = $data['port']; $eventualUpdate .= ',`port`=?'; $port = $data['port']; } - if (isset($data['port2']) and port($data['port2']) and !in_array($data['port'],$usedPorts)) { + + if (isset($data['port2']) and port($data['port2']) and !in_array($data['port'], $usedPorts)) { $updateArray[] = $data['port2']; $eventualUpdate .= ',`port2`=?'; $port2 = $data['port2']; } - if (isset($data['port3']) and port($data['port3']) and !in_array($data['port'],$usedPorts)) { + + if (isset($data['port3']) and port($data['port3']) and !in_array($data['port'], $usedPorts)) { $updateArray[] = $data['port3']; $eventualUpdate .= ',`port3`=?'; $port3 = $data['port3']; } - if (isset($data['port4']) and port($data['port4']) and !in_array($data['port'],$usedPorts)) { + + if (isset($data['port4']) and port($data['port4']) and !in_array($data['port'], $usedPorts)) { $updateArray[] = $data['port4']; $eventualUpdate .= ',`port4`=?'; $port4 = $data['port4']; } - if (isset($data['port5']) and port($data['port5']) and !in_array($data['port'],$usedPorts)) { + + if (isset($data['port5']) and port($data['port5']) and !in_array($data['port'], $usedPorts)) { $updateArray[] = $data['port5']; $eventualUpdate .= ',`port5`=?'; $port5 = $data['port5']; } - if (isset($data['active']) and active_check($data['active'])) $active = $data['active']; + + if (isset($data['active']) and active_check($data['active'])) { + $active = $data['active']; + } + if (count($updateArray)>0) { $eventualUpdate = trim($eventualUpdate,','); $eventualUpdate .= ','; } + $updateArray[] = $localID; $updateArray[] = $resellerID; + $query = $sql->prepare("UPDATE `gsswitch` SET $eventualUpdate`jobPending`='Y' WHERE `id`=? AND `resellerid`=? LIMIT 1"); $query->execute($updateArray); - customColumns('G',$localID,'save',$data); + + customColumns('G', $localID,'save', $data); + $customID = $localID; + if ((isset($active) and ($active == 'Y' or $active == 'N') and $active != $oldActive) or $slots != $oldSlots or (isset($port) and $port != $oldPort)) { - $update = $sql->prepare("UPDATE `jobs` SET `status`='2' WHERE `type`='gs' AND (`status` IS NULL OR `status`='1') AND `action`!='ad' AND `affectedID`=? and `resellerID`=?"); - $update->execute(array($localID,$resellerID)); - $insert = $sql->prepare("INSERT INTO `jobs` (`api`,`type`,`hostID`,`invoicedByID`,`affectedID`,`userID`,`name`,`status`,`date`,`action`,`extraData`,`resellerID`) VALUES ('A','gs',?,?,?,?,?,NULL,NOW(),'md',?,?)"); - $insert->execute(array($hostID,$resellerID,$localID,$userID,$name,json_encode(array('newActive' => $active,'newPort' => $port)),$resellerID)); + + $query = $sql->prepare("UPDATE `jobs` SET `status`='2' WHERE `type`='gs' AND (`status` IS NULL OR `status`='1') AND `action`!='ad' AND `affectedID`=? and `resellerID`=?"); + $query->execute(array($localID, $resellerID)); + + $query = $sql->prepare("INSERT INTO `jobs` (`api`,`type`,`hostID`,`invoicedByID`,`affectedID`,`userID`,`name`,`status`,`date`,`action`,`extraData`,`resellerID`) VALUES ('A','gs',?,?,?,?,?,NULL,NOW(),'md',?,?)"); + $query->execute(array($hostID, $resellerID, $localID, $userID, $name,json_encode(array('newActive' => $active,'newPort' => $port)), $resellerID)); } } + if (!isset($oldSlots)) { $success['false'][] = 'No server can be found to edit'; } } else { - $success['false'][] = 'No data for this method: '.$data['action']; + $success['false'][] = 'No data for this method: ' . $data['action']; } -} else if (!isset($success['false']) and array_value_exists('action','del',$data)) { + +} else if (!isset($success['false']) and array_value_exists('action', 'del', $data)) { + $identifyServerBy = $data['identify_server_by']; - $localServerID=isid($data['server_local_id'],21); + $localServerID = isid($data['server_local_id'], 21); $externalServerID = $data['server_external_id']; - $from=array('server_local_id' => 'id','server_external_id' => 'externalID'); - if (dataExist('identify_server_by',$data)) { + + $from = array('server_local_id' => 'id', 'server_external_id' => 'externalID'); + + if (dataExist('identify_server_by', $data)) { + $query = $sql->prepare("SELECT r.`externalID`,g.`id`,g.`serverip`,g.`port`,g.`userid`,g.`rootID` FROM `gsswitch` g LEFT JOIN `rserverdata` r ON g.`rootID`=r.`id` WHERE g.`".$from[$data['identify_server_by']]."`=? AND g.`resellerid`=?"); - $query->execute(array($data[$data['identify_server_by']],$resellerID)); + $query->execute(array($data[$data['identify_server_by']], $resellerID)); foreach ($query->fetchAll(PDO::FETCH_ASSOC) as $row) { $localID = $row['id']; $userID = $row['userid']; @@ -476,147 +596,269 @@ if (!isset($success['false']) and array_value_exists('action','add',$data) and 1 $hostID = $row['rootID']; $hostExternalID = $row['rootID']; } + if (isset($localID) and isset($name)) { + $query = $sql->prepare("UPDATE `gsswitch` SET `jobPending`='Y' WHERE `id`=? AND `resellerid`=? LIMIT 1"); - $query->execute(array($localID,$resellerID)); + $query->execute(array($localID, $resellerID)); + $query = $sql->prepare("UPDATE `jobs` SET `status`='2' WHERE `type`='gs' AND (`status` IS NULL OR `status`='1') AND `affectedID`=? and `resellerID`=?"); - $query->execute(array($localID,$resellerID)); + $query->execute(array($localID, $resellerID)); + $query = $sql->prepare("INSERT INTO `jobs` (`api`,`type`,`hostID`,`invoicedByID`,`affectedID`,`userID`,`name`,`status`,`date`,`action`,`resellerid`) VALUES ('A','gs',?,?,?,?,?,NULL,NOW(),'dl',?)"); - $query->execute(array($hostID,$resellerID,$localID,$userID,$name,$resellerID)); + $query->execute(array($hostID, $resellerID, $localID, $userID, $name, $resellerID)); + } else { $success['false'][] = 'No server can be found to delete'; } + } else { $success['false'][] = 'No data for this method: '.$data['action']; } -} else if (array_value_exists('action','ls',$data)) { + +} else if (array_value_exists('action', 'ls', $data)) { + + $list = true; + $query = $sql->prepare("SELECT r.`id`,r.`ip`,r.`altips`,r.`maxslots`,r.`maxserver`,r.`maxserver`-COUNT(g.`id`) AS `freeserver`,COUNT(g.`id`) AS `installedserver`,r.`active` AS `hostactive`,r.`resellerid` AS `resellerid`,(r.`maxslots`-SUM(g.`slots`)) AS `leftslots`,SUM(g.`slots`) AS `installedslots` FROM `rserverdata` r LEFT JOIN `gsswitch` g ON g.`rootID`=r.`id` GROUP BY r.`id` HAVING ((`freeserver` > 0 OR `freeserver` IS NULL) AND (`leftslots`>0 OR `leftslots` IS NULL) AND `hostactive`='Y' AND `resellerid`=?) ORDER BY `freeserver` DESC"); $query->execute(array($resellerID)); - $list = true; + if ($apiType == 'xml') { - $reply=" - -"; - foreach ($query->fetchAll(PDO::FETCH_ASSOC) as $row) { - $reply .= ' - '.$row['id'].' - '.$row['ip'].' - '.$row['altips'].' - '.$row['maxslots'].' - '.$row['maxserver'].' - '.$row['freeserver'].' - '.$row['installedserver'].' - '.$row['leftslots'].' - '.$row['installedslots'].' - '; - } - $reply .= ''; + header("Content-type: text/xml; charset=UTF-8"); - echo $reply; + + $responsexml = new DOMDocument('1.0','utf-8'); + $element = $responsexml->createElement('gserver'); + + foreach ($query->fetchAll(PDO::FETCH_ASSOC) as $row) { + + $key = $responsexml->createElement('server'); + + $listServerXML = $responsexml->createElement('id', $row['id']); + $key->appendChild($listServerXML); + + $listServerXML = $responsexml->createElement('ip', $row['ip']); + $key->appendChild($listServerXML); + + $listServerXML = $responsexml->createElement('altips', $row['altips']); + $key->appendChild($listServerXML); + + $listServerXML = $responsexml->createElement('maxslots', $row['maxslots']); + $key->appendChild($listServerXML); + + $listServerXML = $responsexml->createElement('maxserver', $row['maxserver']); + $key->appendChild($listServerXML); + + $listServerXML = $responsexml->createElement('freeserver', $row['freeserver']); + $key->appendChild($listServerXML); + + $listServerXML = $responsexml->createElement('installedserver', $row['installedserver']); + $key->appendChild($listServerXML); + + $listServerXML = $responsexml->createElement('leftslots', $row['leftslots']); + $key->appendChild($listServerXML); + + $listServerXML = $responsexml->createElement('installedslots', $row['installedslots']); + $key->appendChild($listServerXML); + + $element->appendChild($key); + } + + $responsexml->appendChild($element); + + $responsexml->formatOutput = true; + + echo $responsexml->saveXML(); + } else if ($apiType == 'json') { + header("Content-type: application/json; charset=UTF-8"); + echo json_encode($query->fetchAll(PDO::FETCH_ASSOC)); + } else { header('HTTP/1.1 403 Forbidden'); die('403 Forbidden'); } -} else if (!isset($success['false']) and array_value_exists('action','gs',$data)) { + +} else if (!isset($success['false']) and array_value_exists('action', 'gs', $data)) { + $identifyServerBy = $data['identify_server_by']; - $localServerID=isid($data['server_local_id'],21); + $localServerID = isid($data['server_local_id'], 21); $externalServerID = $data['server_external_id']; + if (isset($data['restart']) and ($data['restart'] == 're' or $data['restart'] == 'st')) { + + $from = array('server_local_id' => 'id', 'server_external_id' => 'externalID'); $gsRestart = $data['restart']; - $from=array('server_local_id' => 'id','server_external_id' => 'externalID'); - if (dataExist('identify_server_by',$data)) { + + if (dataExist('identify_server_by', $data)) { + $query = $sql->prepare("SELECT `id`,`userid`,`rootID`,`serverip`,`port` FROM `gsswitch` WHERE `".$from[$data['identify_server_by']]."`=? AND `resellerid`=? LIMIT 1"); - $query->execute(array($data[$data['identify_server_by']],$resellerID)); + $query->execute(array($data[$data['identify_server_by']], $resellerID)); foreach ($query->fetchAll(PDO::FETCH_ASSOC) as $row) { $hostID = $row['rootID']; $userID = $row['userid']; $localID = $row['id']; $name = $row['serverip'] . ':' . $row['port']; } + if (isset($localID) and isset($userID)) { + $query = $sql->prepare("UPDATE `gsswitch` SET `jobPending`='Y' WHERE `id`=? AND `resellerid`=? LIMIT 1"); - $query->execute(array($localID,$resellerID)); + $query->execute(array($localID, $resellerID)); + $query = $sql->prepare("UPDATE `jobs` SET `status`='2' WHERE `type`='gs' AND (`status` IS NULL OR `status`='1') AND (`action`='re' OR `action`='st') AND `affectedID`=? and `resellerID`=?"); - $query->execute(array($localID,$resellerID)); + $query->execute(array($localID, $resellerID)); + $query = $sql->prepare("INSERT INTO `jobs` (`api`,`type`,`hostID`,`invoicedByID`,`affectedID`,`userID`,`name`,`status`,`date`,`action`,`resellerid`) VALUES ('A','gs',?,?,?,?,?,NULL,NOW(),?,?)"); - $query->execute(array($hostID,$resellerID,$localID,$userID,$name,$gsRestart,$resellerID)); + $query->execute(array($hostID, $resellerID, $localID, $userID, $name, $gsRestart, $resellerID)); + } else { $success['false'][] = 'No server can be found to edit'; } + } else { $success['false'][] = 'Server cannot be identified'; } + } else { $success['false'][] = '(Re)start or Stop not defined'; } + } else { $success['false'][] = 'Not supported method or incomplete data'; } if ($apiType == 'xml' and !isset($list)) { - header("Content-type: text/xml; charset=UTF-8"); + if (isset($success['false'])) { - $errors=implode(', ',$success['false']); - $action='fail'; + $errors = implode(', ', $success['false']); + $action = 'fail'; } else { $errors = ''; - $action='success'; + $action = 'success'; } - $reply=<< - - - $action - $private - $active - $identifyServerBy - $slots - $externalServerID - $localServerID - $identifyUserBy - $localUserID - $externalUserID - $username - $taskset - $cores - $eacallowed - $brandname - $tvenable - $pallowed - $port - $port2 - $port3 - $port4 - $port5 - $minram - $maxram - $hostID - $hostExternalID - $initialpassword - $installGames - $autoRestart - $errors -XML; + + header("Content-type: text/xml; charset=UTF-8"); + + $responsexml = new DOMDocument('1.0','utf-8'); + $element = $responsexml->createElement('gserver'); + + $key = $responsexml->createElement('action', $action); + $element->appendChild($key); + + $key = $responsexml->createElement('private', $private); + $element->appendChild($key); + + $key = $responsexml->createElement('active', $active); + $element->appendChild($key); + + $key = $responsexml->createElement('identify_server_by', $identifyServerBy); + $element->appendChild($key); + + $key = $responsexml->createElement('slots', $slots); + $element->appendChild($key); + + $key = $responsexml->createElement('server_external_id', $externalServerID); + $element->appendChild($key); + + $key = $responsexml->createElement('server_local_id', $localServerID); + $element->appendChild($key); + + $key = $responsexml->createElement('identify_user_by', $identifyUserBy); + $element->appendChild($key); + + $key = $responsexml->createElement('user_localid', $localUserID); + $element->appendChild($key); + + $key = $responsexml->createElement('user_externalid', $externalUserID); + $element->appendChild($key); + + $key = $responsexml->createElement('username', $username); + $element->appendChild($key); + + $key = $responsexml->createElement('taskset', $taskset); + $element->appendChild($key); + + $key = $responsexml->createElement('cores', $cores); + $element->appendChild($key); + + $key = $responsexml->createElement('eacallowed', $eacallowed); + $element->appendChild($key); + + $key = $responsexml->createElement('brandname', $brandname); + $element->appendChild($key); + + $key = $responsexml->createElement('tvenable', $tvenable); + $element->appendChild($key); + + $key = $responsexml->createElement('pallowed', $pallowed); + $element->appendChild($key); + + $key = $responsexml->createElement('port', $port); + $element->appendChild($key); + + $key = $responsexml->createElement('port2', $port2); + $element->appendChild($key); + + $key = $responsexml->createElement('port3', $port3); + $element->appendChild($key); + + $key = $responsexml->createElement('port4', $port4); + $element->appendChild($key); + + $key = $responsexml->createElement('port5', $port5); + $element->appendChild($key); + + $key = $responsexml->createElement('minram', $minram); + $element->appendChild($key); + + $key = $responsexml->createElement('maxram', $maxram); + $element->appendChild($key); + + $key = $responsexml->createElement('master_server_id', $hostID); + $element->appendChild($key); + + $key = $responsexml->createElement('master_server_external_id', $hostExternalID); + $element->appendChild($key); + + $key = $responsexml->createElement('initialpassword', $initialpassword); + $element->appendChild($key); + + $key = $responsexml->createElement('installGames', $installGames); + $element->appendChild($key); + + $key = $responsexml->createElement('autoRestart', $autoRestart); + $element->appendChild($key); + + $key = $responsexml->createElement('errors', $errors); + $element->appendChild($key); + if (isset ($shorten) and is_array($shorten)) { foreach ($shorten as $short) { - $reply .= ' -'.$short.''; + $key = $responsexml->createElement('shorten', $short); + $element->appendChild($key); } } - foreach(customColumns('G',$customID) as $row) { - $reply .=" - <${row['name']}>${row['value']}"; + foreach(customColumns('G', $customID) as $row) { + $key = $responsexml->createElement($row['name'], $row['value']); + $element->appendChild($key); } - $reply .= ' -'; - print $reply; + + $responsexml->appendChild($element); + + $responsexml->formatOutput = true; + + echo $responsexml->saveXML(); + } else if ($apiType == 'json' and !isset($list)) { + header("Content-type: application/json; charset=UTF-8"); + echo json_encode(array('action' => $action,'private' => $private,'active' => $active,'identify_server_by' => $identifyServerBy,'shorten' => $shorten,'slots' => $slots,'server_external_id' => $externalServerID,'server_local_id' => $localServerID,'identify_user_by' => $identifyUserBy,'user_localid' => $localUserID,'user_externalid' => $externalUserID,'username' => $username,'errors' => $errors)); + } else if (!isset($list)) { header('HTTP/1.1 403 Forbidden'); die('403 Forbidden');