<?php
    include_once '../account/session.php';
    include_once '../../scripts/bd.php';
    
    if(isset($_GET['action'])){
        $action=$_GET['action'];
        if($action=='add')add();
        if($action=='show')show();
        if($action=='get')get();
        if($action=='set')set();
        if($action=='set_active_on')set_active_on();
        if($action=='set_active_off')set_active_off();
        if($action=='show_ip_blocked')show_ip_blocked();
        if($action=='delistedSpam')delistedSpam();
   } 

   function show_ip_blocked()
   {
      include_once('OvhApi.php');
      $myapi= new OvhApi();
      $servers = bd::query("SELECT id FROM server WHERE provider like '%ovh%'");
      $count = 0;
      while($server = mysql_fetch_array($servers)) {
        $req = bd::query("SELECT address_ip FROM server_vmta WHERE id_server = '".$server['id']."'");
        while($req_data = mysql_fetch_array($req)) {
          $res = $myapi->get('/ip/'.$req_data['address_ip'].'/spam/'.$req_data['address_ip']);
          if($res['state'] == "blockedForSpam") {
            $count++;
          } 
        } 
      }
      if($count > 0 AND !isset($_SESSION['in'])) {
        echo '<script> function delistedSpam() {
          var avant = $("#text_ovh").html();
          $("#text_ovh").html("In progress...");
            $.get( "/app/manager/server/scripts.php?action=delistedSpam", function( data ) {
              alert(data);
              $("#text_ovh").html(avant);
            });

         } </script>
        <li style="    margin-top: 21px;
    margin-left: 272px;
    position: absolute;"><div style="     padding: 10px;
    background-color: #d04d4d;
    color: white;
    border-radius: 3px;
    position: absolute;
    height: 20px;
    margin-top: -9px;
    width: 195px;"><span style="margin-left: 5px;" id="text_ovh">Ip\'s blocked (OVH): </span> <h3 id="count_ip_blocked" style="    margin-top: -29px;
    margin-left: 148px;
    color: white;"><a onclick="delistedSpam()" style="cursor:pointer;color:white;text-decoration: none;">'.$count.'</a></h3></div></li>';
      }
      

   }

   function delistedSpam()
   {
      include_once('OvhApi.php');
      $myapi= new OvhApi();
      $result = array();
      $servers = bd::query("SELECT id FROM server WHERE provider like '%ovh%'");
      while($server = mysql_fetch_array($servers)) {
        $req = bd::query("SELECT address_ip FROM server_vmta WHERE id_server = '".$server['id']."'");
        while($req_data = mysql_fetch_array($req)) {
          $res = $myapi->get('/ip/'.$req_data['address_ip'].'/spam/'.$req_data['address_ip']);
          if($res['state'] == "blockedForSpam") {
            $ex = explode('.',$req_data['address_ip']);
             echo $req_data['address_ip']."\n";
             $result[] = $myapi->post('/ip/'.$req_data['address_ip'].'/spam/'.$req_data['address_ip'].'/unblock');
          } 
        } 
      }
      $unique = array();
 
      for ($i = 0; $i < count($result); $i++) {
          if (!in_array($result[$i]['message'], $unique)){
              array_push($unique, $result[$i]['message']);
          }
      }
      $text = "";
      foreach($unique as $u) {
        $text .= $u."\n";
      }
      echo $text;

   }
   
   function verify_name($id,$name){
       $query = bd::query("SELECT * FROM server where name like '$name' && id!='$id'");
       if(mysql_num_rows($query)>0){
           return true;
       }
       return false;
   }

   function add(){ 
        $name=trim($_POST["name"]);
        $provider=$_POST["provider"];
        $main_ip=$_POST["main_ip"];
        $support=$_POST["support"];
        $login=$_POST["login"];
        $password=$_POST["password"];
        $active=0;
        $date_add=date('Y-m-d h:i:s');
        $date_set=date('Y-m-d h:i:s');
        
        $ok=0;
        if(!verify_name('0',$name)){
            $rep=bd::query("INSERT INTO server VALUES (NULL, '$name', '$provider', '$main_ip', '$support', '$login', '$password', '$active', '$date_add', '$date_set');");
            if(!$rep){
               $ok=2; 
            }
        }else{
            $ok=1; 
        }
        echo $ok;
   }
   
   function show(){
      $list=array();
      $data= bd::query("SELECT id,name,provider,main_ip,support,active FROM server");
      while ($row = mysql_fetch_object($data)) {
          $list[]=$row;
      }
      echo json_encode($list);
   }
   
   function get_data_tables(){
      $data=array();
      $query = bd::query("show tables like 'list_%'");
      while ($row = mysql_fetch_row($query)) {
              $data[]=$row;
      }
      echo json_encode($data);
   }
   
   function get(){ 
      $data = bd::query("SELECT * FROM server WHERE id='{$_GET["id_server"]}'");
      $row = mysql_fetch_array($data);
      return $row;
   }
   
    function set(){ 
       $name=trim($_POST["name"]);
       $provider=$_POST["provider"];
       $main_ip=$_POST["main_ip"];
       $support=$_POST["support"];
       $login=$_POST["login"];
       $password=$_POST["password"];
       $date_set=date('Y-m-d h:i:s');
       
       $ok=0;
        if(!verify_name($_POST["id"],$name)){
            $rep=bd::query("UPDATE server SET name='$name', provider='$provider', main_ip='$main_ip', support='$support', login='$login', password='$password', date_set='$date_set' WHERE id='{$_POST["id"]}'");
            if(!$rep){
               $ok=2; 
            }
        }else{
            $ok=1; 
        }
        echo $ok;
   }
   
   function set_active_on(){ 
       bd::query("UPDATE server SET active= '1' WHERE id='{$_GET["id"]}'");
   }
   
   function set_active_off(){ 
       bd::query("UPDATE server SET active= '0' WHERE id='{$_GET["id"]}'");
   }
   