ProfesseurCraft

processing.php

Oct 9th, 2013
146
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 5.05 KB | None | 0 0
  1. <?php session_start(); // on démarre une session pour ne pas avoir à appeller plusieurs fois la requête lors des refreshs.
  2.  
  3. // On inclue les différentes class dont nous aurons besoin
  4. require_once('class/Infos.php'); require_once('class/Rtk.php'); require_once('class/Sftp.php');
  5.  
  6. // On inclue les paramètres de connexion à la base de données
  7. require_once ('bdd/config_bdd.php');
  8. try {
  9.   // Options de connection
  10.   $options = array(
  11.     PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8",
  12.     PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION
  13.   );
  14.   $connexion_bdd = new PDO( $dns, $login, $pass, $options );
  15.  
  16. } catch ( Exception $e ) {
  17.     echo "Connection à MySQL impossible : ", $e->getMessage();
  18.   die();
  19. }
  20.  
  21. // On crée une fonction pour
  22. function message($status,$msg){
  23.     $content_message = array('status'=>''.$status.'','msg'=>''.$msg.'');
  24.     echo json_encode($content_message);
  25. }
  26. function prepareDatas($datas,$msg) {
  27.     $arrayAllDatas = array(
  28.         'datas' => $datas,
  29.         'msg' => $msg
  30.         );
  31.     echo json_encode($arrayAllDatas);
  32. }
  33.  
  34. // on vérifie le type d'action à effectuer
  35. if(isset($_GET['action'])){
  36.  
  37.  
  38.     $action = $_GET['action'];
  39.  
  40.     switch ($action) {
  41.  
  42.         case 'select':
  43.  
  44.                 // On récupère l'id pour sélectionner le bon serveur et tout ses champs
  45.                 $id = $_GET['id'];         
  46.                 $select = $connexion_bdd->query('select * from servers WHERE id="'.$id.'"');
  47.                 $select->setFetchMode(PDO::FETCH_OBJ);
  48.                 $select = $select->fetch();
  49.  
  50.                 // Paramètres pour la connexion au serveur de jeu afin de récupérer ses informations via la class Infos
  51.                 $_SESSION['server_name'] = $select->server_name;
  52.                 $_SESSION['server_ip'] = $select->server_ip;
  53.                 $_SESSION['server_port'] = $select->server_port;
  54.  
  55.                 // Paramètres pour la connection à rtk pour controller le serveur via la class Rtk
  56.                 $_SESSION['rtk_port'] = $select->rtk_port;
  57.                 $_SESSION['rtk_login'] = $select->rtk_login;
  58.                 $_SESSION['rtk_pass'] = $select->rtk_pass;
  59.  
  60.                 // Paramètres pour la connection au sftp pour lire les logs via la class Sftp (port 22)
  61.                 $_SESSION['sftp_login'] = $select->sftp_login;
  62.                 $_SESSION['sftp_pass'] = $select->sftp_pass;
  63.                 $_SESSION['sftp_path'] = $select->sftp_path;
  64.  
  65.                     function infosServer(){
  66.                         // On affecte les sessions dont on a besoin dans des variables
  67.                             $server_name = $_SESSION['server_name'];
  68.                             $server_ip = $_SESSION['server_ip'];
  69.                             $server_port = $_SESSION['server_port'];
  70.  
  71.                             $sftp_login = $_SESSION['sftp_login'];
  72.                             $sftp_pass = $_SESSION['sftp_pass'];
  73.                             $sftp_path = $_SESSION['sftp_path'];
  74.  
  75.                         // On instancie les class Infos et Sftp (infos serveur et logs)
  76.                             $Infos = new Infos();
  77.                             $Sftp = new Sftp();
  78.  
  79.                             $server = ''.$server_ip.':'.$server_port.'';
  80.  
  81.                             $logs = $Sftp->Read($server_ip,$sftp_login,$sftp_pass,$sftp_path);
  82.                            
  83.                             if($Infos->Connect($server))
  84.                                 {          
  85.                                     $players_online = $Infos->Players_online();
  86.                                     $slots = $Infos->Slots();
  87.                                    
  88.                                         $array_infos = array(
  89.  
  90.                                             'status' => true,
  91.                                             'name' => ''.$server_name.'',
  92.                                             'ip' => $server_ip.':'.$server_port,
  93.                                             'players_online' => $players_online,
  94.                                             'slots' => $slots,
  95.                                             'logs' => $logs
  96.                                         );
  97.                                         $array_msg = array(
  98.                                             'status' => 'success',
  99.                                             'msg' => 'La récupération des informations du serveur s’est bien déroulée'
  100.                                         );
  101.  
  102.                                         prepareDatas($array_infos,$array_msg);
  103.                                 }
  104.                             else {
  105.                                         $array_infos = array(
  106.  
  107.                                             'status' => false,
  108.                                             'name' => ''.$server_name.'',
  109.                                             'ip' => $server_ip.':'.$server_port,
  110.                                             'players_online' => $players_online,
  111.                                             'slots' => $slots,
  112.                                             'logs' => $logs
  113.                                         );
  114.  
  115.                                             $array_msg = array(
  116.                                             'status' => 'error',
  117.                                             'msg' => 'La récupération des informations du serveur est impossible'
  118.                                         );
  119.  
  120.                                         prepareDatas($array_infos,$array_msg);
  121.                             }
  122.                     }
  123.                     infosServer();
  124.                
  125.             break;
  126.  
  127.         case 'rtk':
  128.                
  129.                 $ip = $_SESSION['server_ip'];
  130.                 $rtk_port = $_SESSION['rtk_port'];
  131.  
  132.                 $command = $_GET['rtk']; // On récupère la commande (start, stop ou restart) via le paramètre rtk
  133.  
  134.                 $rtk_login = $_SESSION['rtk_login'];
  135.                 $rtk_pass = $_SESSION['rtk_pass'];
  136.  
  137.                 // On instancie la class Rtk pour envoyer notre commande au serveur
  138.                 $Rtk = new Rtk();
  139.  
  140.                 $string_rtk = $Rtk->Send($ip,$rtk_port,$command,$rtk_login,$rtk_pass);
  141.                 if($string_rtk){
  142.                     $datas = null;
  143.                     $array_msg = array(
  144.                         'status' => 'success',
  145.                         'msg' => 'La commande '.$command.' à bien été envoyé'
  146.                     );
  147.  
  148.                     prepareDatas($datas,$array_msg);
  149.  
  150.                 } else {
  151.                     $datas = null;
  152.                     $array_msg = array(
  153.                         'status' => 'error',
  154.                         'msg' => 'La commande '.$command.' à bien été envoyé'
  155.                     );
  156.  
  157.                     prepareDatas($datas,$array_msg);
  158.  
  159.                 }
  160.  
  161.  
  162.             break;
  163.  
  164.         case 'refresh':
  165.             global infosServer(); // On se contente de rappeller la fonction infosServer()
  166.             break;
  167.        
  168.     }
  169.  
  170. }
  171.  
  172.  
  173.  
  174. ?>
Advertisement
Add Comment
Please, Sign In to add comment