<?php
error_reporting(E_ERROR | E_PARSE);

if (!class_exists('Database')) require_once("common_cadline_libraries/crm_database.class.php");

session_start();
include('config.php');

if (isset($_POST['submit'])) {
  if ($_POST['modId']) {
    $id = $_POST['modId'];
    $text = $_POST['modText'];

    $updateUserQuery = "UPDATE `www_archline_hu`.`crash` SET komment = ? WHERE id = ? LIMIT 1;";
    Database::getInstance()->query($updateUserQuery, array('si',  $text, $id));
  }
  if ($_POST['listaid'] && isset($_POST['CbBoxValue'])) {
    $listaid = substr($_POST['listaid'], 5);

    $updateUserQuery = "UPDATE `www_archline_hu`.`crash` SET eredmeny = ? WHERE id = ? LIMIT 1;";
    Database::getInstance()->query($updateUserQuery, array('ii',  $_POST['CbBoxValue'], $listaid));
  }
}


function downloadFile($filename)
{
  if ($f = fopen($filename, "rb")) {
    $content_len = (int) filesize($filename);
    //@ini_set('zlib.output_compression', 'Off');
    header('Pragma: public');
    //header('Last-Modified: '.gmdate('D, d M Y H(idea)(worry)') . ' GMT');
    header('Cache-Control: no-store, no-cache, must-revalidate'); // HTTP/1.1
    header('Cache-Control: pre-check=0, post-check=0, max-age=0'); // HTTP/1.1
    header('Content-Transfer-Encoding: none');
    header('Content-Type: plain/text');
    header('Content-Disposition: inline; filename="' . $filename . '"');
    header("Content-length: $content_len");

    while (!feof($f)) {
      echo fread($f, 2 << 20);
    }
    fclose($f);
  } else {
    print "error opening file";
  }
  exit();
}

if (isset($_GET) && !empty($_GET)) {
  $filepath = "/home/vendeg/crash/";
  $filepath .= $_GET["guid"];

  if (!file_exists($filepath))
    $filepath = "../webdav/crash/" . $_GET['guid'];
  downloadFile($filepath);
} else {
?>
  <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">

  <head>
    <meta http-equiv="Content-type" content="text/html;charset=UTF-8" />
    <style>
      #submit {
        position: fixed;
        right: 1%;
        top: 1%;
      }

      .tr0 {
        background-color: white;
      }

      .tr1 {
        background-color: #33cc33;
      }

      .tr2,
      .tr5,
      .tr6 {
        background-color: #ff5050;
      }

      .tr3 {
        background-color: #cccccc;
      }

      .tr4 {
        background-color: #99ff33;
      }

      .tr7 {
        background-color: #808080;
      }

      .ta0 {
        background-color: white;
      }

      .ta1 {
        background-color: #33cc33;
      }

      .ta2,
      .ta5,
      .ta6 {
        background-color: #ff5050;
      }

      .ta3 {
        background-color: #cccccc;
      }

      .ta4 {
        background-color: #99ff33;
      }

      .ta7 {
        background-color: #808080;
      }
    </style>
    <script>
      var textId;
      var CbBox;

      function valtoztat(id) {
        textId = id;
        document.getElementById("modId").value = id;
      }

      function textChange() {
        var x = document.getElementById(textId).value;
        document.getElementById("modText").value = x;

        var cbV = document.getElementById(CbBox).selectedIndex;
        document.getElementById("CbBoxValue").value = cbV;
      }

      function cbboxid(CbId) {
        var cbV = document.getElementById(CbId).selectedIndex;
        document.getElementById("CbBoxValue").value = cbV;
        document.getElementById("listaid").value = CbId;
        CbBox = CbId;
      }
    </script>
  </head>

  <body>
    <?php
    $V0 = "Nem feldolgozott";
    $V1 = "Javítva";
    $V2 = "Nem debuggolható";
    $V3 = "Nem értelmezhető";
    $V4 = "Előzőleg javított";
    $V5 = "Nem reprodukálható";
    $V6 = "Nem javítható";
    $V7 = "Nem feldolgozandó";
    $V8 = "Mind";

    if (isset($_POST['datemin'])) {
      $_SESSION["mindate"]  = $_POST['datemin'];
      $_SESSION["maxdate"]  = $_POST['datemax'];
      $_SESSION["lista"]    = $_POST['lista'];
      $_SESSION['product']  = $_POST['product'];
      $_SESSION['prod']     = $_POST['prod'];
    }

    $maxdate = date("Y-m-d");
    $mindate = date("Y-m-d", strtotime("-5 days"));

    echo '<form method="post" action="">';
    if (isset($_SESSION["maxdate"]) || isset($_SESSION["mindate"]) || isset($_SESSION["lista"])) {
      echo 'Dátum szűrés<br /> <input type="date" name="datemin" id="datemin" value="' . $_SESSION["mindate"] . '">-tól &nbsp;&nbsp;&nbsp; <input type="date" name="datemax" id="datemax" value="' . $_SESSION["maxdate"] . '">-ig';
    } else {
      echo 'Dátum szűrés<br /> <input type="date" name="datemin" id="datemin" value="' . $mindate . '">-tól &nbsp;&nbsp;&nbsp; <input type="date" name="datemax" id="datemax" value="' . $maxdate . '">-ig';
    }
    ?>
    <br /><br />
    Eredmény szűrő:<br />
    <select name="lista">
      <option value="0" <?php if (isset($_SESSION['lista']) && $_SESSION['lista'] == "0") echo ' selected'; ?>><?php echo $V0; ?></option>
      <option value="1" <?php if (isset($_SESSION['lista']) && $_SESSION['lista'] == "1") echo ' selected'; ?>><?php echo $V1; ?></option>
      <option value="2" <?php if (isset($_SESSION['lista']) && $_SESSION['lista'] == "2") echo ' selected'; ?>><?php echo $V2; ?></option>
      <option value="3" <?php if (isset($_SESSION['lista']) && $_SESSION['lista'] == "3") echo ' selected'; ?>><?php echo $V3; ?></option>
      <option value="4" <?php if (isset($_SESSION['lista']) && $_SESSION['lista'] == "4") echo ' selected'; ?>><?php echo $V4; ?></option>
      <option value="5" <?php if (isset($_SESSION['lista']) && $_SESSION['lista'] == "5") echo ' selected'; ?>><?php echo $V5; ?></option>
      <option value="6" <?php if (isset($_SESSION['lista']) && $_SESSION['lista'] == "6") echo ' selected'; ?>><?php echo $V6; ?></option>
      <option value="7" <?php if (isset($_SESSION['lista']) && $_SESSION['lista'] == "7") echo ' selected'; ?>><?php echo $V7; ?></option>
      <option value="%" <?php if (isset($_SESSION['lista']) && $_SESSION['lista'] == "%") echo ' selected'; ?>><?php echo $V8; ?></option>
    </select>

    <select name="product">
      <option value="0" <?php if (isset($_SESSION['product']) && $_SESSION['product'] == "0") echo ' selected'; ?>>Mind</option>
      <option value="1" <?php if (isset($_SESSION['product']) && $_SESSION['product'] == "1") echo ' selected'; ?>>ARCHLine.XP</option>
      <option value="2" <?php if (isset($_SESSION['product']) && $_SESSION['product'] == "2") echo ' selected'; ?>>LIVE</option>
    </select>

    <select name="prod">
      <option value="" <?php if (isset($_SESSION['prod']) && $_SESSION['prod'] == "") echo ' selected'; ?>>Mind</option>
      <option value="AL" <?php if (isset($_SESSION['prod']) && $_SESSION['prod'] == "AL") echo ' selected'; ?>>ARCHLine.XP</option>
      <option value="Render" <?php if (isset($_SESSION['prod']) && $_SESSION['prod'] == "Render") echo ' selected'; ?>>Render</option>
    </select>

    <br /><br />
    <input type="submit" value="Szűrés">
    <br /><br />
    </form>

    <?php

    $bind = '';

    $query = "SELECT * FROM `www_archline_hu`.`crash` WHERE 1 ";
    $feltetel = array();

    if (isset($_POST['datemin'])) {
      array_push($feltetel, $_POST['datemin']);
      $query .= "AND left(datum, 10) >= ? ";
      $bind .= 's';
    }

    if (isset($_POST['datemax'])) {
      array_push($feltetel, $_POST['datemax']);
      $query .= "AND left(datum, 10) <= ? ";
      $bind .= 's';
    }

    if (isset($_POST['lista']) && $_POST['lista'] != "%") {
      array_push($feltetel, $_POST['lista']);
      $query .= "AND eredmeny = ? ";
      $bind .= 'i';
    }

    if (isset($_POST['prod']) && $_POST['prod'] != "") {
      array_push($feltetel, $_POST['prod']);
      $query .= "AND prod = ? ";
      $bind .= 's';
    }

    $query .= "ORDER BY id DESC LIMIT 500;";

    array_unshift($feltetel, $bind);

    Database::getInstance()->query($query, $feltetel);
    $results = Database::getInstance()->fetchAssoc();

    print "<form method='post' action='' onsubmit='textChange()'>";
    print "<table border = 1>";
    print "<tr>";
    print "<th>Email</th>";
    print "<th>Üzenet</th>";
    print "<th>Dátum</th>";
    print "<th>Dmp fájl</th>";
    print "<th>Info fájl</th>";
    print "<th>Crash fájl</th>";
    print "<th>Megjegyzés</th>";
    print "<th>Eredmény</th>";
    print "</tr>";

    foreach ($results as $result) {
      $r = (object)$result;

      $filepathFtp  = "/home/vendeg/crash/";
      $filepathFtp .= $r->guid;
      $filepathFtp .= ".DMP";

      $filepathWebdav  = "../webdav/crash/";
      $filepathWebdav .= $r->guid;
      $filepathWebdav .= ".DMP";

      $crashFile =  "../webdav/crash/" . $r->guid . ".crh";

      if (!file_exists($filepathFtp) && !file_exists($filepathWebdav) && !file_exists($crashFile))
        continue;

      print "<tr class='tr" . $r->eredmeny . "'>";
      print "<td>";
      print $r->email;
      print "</td>";

      print "<td style='width:300px'>";
      print $r->info;
      print "</td>";

      print "<td>";
      //print substr($r->datum,0,10);
      print $r->datum;
      print "</td>";

      print "<td>";

      if (file_exists($filepathFtp) || file_exists($filepathWebdav)) {
        echo '<a href="https://archline.hu/crashadmin/' . $r->guid . '.DMP' . '">' . $r->guid . '</a>';
      }
      print "</td>";

      print "<td>";

      $infoPath = '/home/vendeg/crash/' . $r->guid . '.TXT';
      $infoPathWebdav = '../webdav/crash/' . $r->guid . '.TXT';
      $logPath = '/home/vendeg/crash/' . $r->guid . '.LOG';
      $logPathWebdav = '../webdav/crash/' . $r->guid . '.LOG';
      $crashPath = '../webdav/crash/' . $r->guid . '.crh';
      $logExtension = '.LOG';
      $infoExtension = '.TXT';
      $crashExtension = '.crh';
      if (!file_exists($infoPath) && !file_exists($infoPathWebdav)) {
        $infoPath = '/home/vendeg/crash/' . $r->guid . '.XML';
        $infoExtension = '.XML';
      }

      if (file_exists($infoPath) || file_exists($infoPathWebdav)) {
        echo '<a href="https://archline.hu/crashadmin/' . $r->guid . $infoExtension . '">Info</a>';
      }

      if (file_exists($logPath) || file_exists($logPathWebdav)) {
        echo ' <a href="https://archline.hu/crashadmin/' . $r->guid . $logExtension . '">Log</a>';
      }

      if (file_exists($infoPath)) {
        $file = '/home/vendeg/crash/' . $r->guid . '.TXT';
        if (!file_exists($file))
          $file = $infoPathWebdav;

        $searchfor = 'developer';
        $contents = file_get_contents($file);
        $pattern = preg_quote($searchfor, '/');
        $pattern = "/^.*$pattern.*\$/m";
        if (preg_match_all($pattern, $contents, $matches))
          echo "developer";

        $searchfor = 'Product: render';
        $contents = file_get_contents($file);
        $pattern = preg_quote($searchfor, '/');
        $pattern = "/^.*$pattern.*\$/m";
        if (preg_match_all($pattern, $contents, $matches)) {
          echo "render";
        }
      }
      print "</td>";

      print "<td>";
      if (file_exists($crashPath)) {
        echo ' <a href="https://partners.staging.cadline.hu/softwarestatistic/crashReport/' . $r->guid . '" target="_blank">Crash</a>';
      }
      print "</td>";

      print "<td>";
      echo "<textarea rows='2' cols='60' class='ta" . $r->eredmeny . "' id='" . $r->id . "' onClick='valtoztat(this.id)'>" . $r->komment . "</textarea>";
      print "</td>";

      print "<td>";
      switch ($r->eredmeny) {
        case '0':
          echo '
      <select id="lista' . $r->id . '" onchange="cbboxid(this.id)">
        <option value="0" selected>' . $V0 . '</option>
        <option value="1">' . $V1 . '</option>
        <option value="2">' . $V2 . '</option>
        <option value="3">' . $V3 . '</option>
        <option value="4">' . $V4 . '</option>
        <option value="5">' . $V5 . '</option>
        <option value="6">' . $V6 . '</option>
        <option value="7">' . $V7 . '</option>
      </select>
      ';
          break;

        case '1':
          echo '
      <select id="lista' . $r->id . '" onchange="cbboxid(this.id)">
        <option value="0">' . $V0 . '</option>
        <option value="1" selected>' . $V1 . '</option>
        <option value="2">' . $V2 . '</option>
        <option value="3">' . $V3 . '</option>
        <option value="4">' . $V4 . '</option>
        <option value="5">' . $V5 . '</option>
        <option value="6">' . $V6 . '</option>
        <option value="7">' . $V7 . '</option>
      </select>
      ';
          break;

        case '2':
          echo '
      <select id="lista' . $r->id . '" onchange="cbboxid(this.id)">
        <option value="0">' . $V0 . '</option>
        <option value="1">' . $V1 . '</option>
        <option value="2" selected>' . $V2 . '</option>
        <option value="3">' . $V3 . '</option>
        <option value="4">' . $V4 . '</option>
        <option value="5">' . $V5 . '</option>
        <option value="6">' . $V6 . '</option>
        <option value="7">' . $V7 . '</option>
      </select>
      ';
          break;

        case '3':
          echo '
      <select id="lista' . $r->id . '" onchange="cbboxid(this.id)">
        <option value="0">' . $V0 . '</option>
        <option value="1">' . $V1 . '</option>
        <option value="2">' . $V2 . '</option>
        <option value="3" selected>' . $V3 . '</option>
        <option value="4">' . $V4 . '</option>
        <option value="5">' . $V5 . '</option>
        <option value="6">' . $V6 . '</option>
        <option value="7">' . $V7 . '</option>
      </select>
      ';
          break;
        case '4':
          echo '
      <select id="lista' . $r->id . '" onchange="cbboxid(this.id)">
        <option value="0">' . $V0 . '</option>
        <option value="1">' . $V1 . '</option>
        <option value="2">' . $V2 . '</option>
        <option value="3">' . $V3 . '</option>
        <option value="4" selected>' . $V4 . '</option>
        <option value="5">' . $V5 . '</option>
        <option value="6">' . $V6 . '</option>
        <option value="7">' . $V7 . '</option>
      </select>
      ';
          break;
        case '5':
          echo '
      <select id="lista' . $r->id . '" onchange="cbboxid(this.id)">
        <option value="0">' . $V0 . '</option>
        <option value="1">' . $V1 . '</option>
        <option value="2">' . $V2 . '</option>
        <option value="3">' . $V3 . '</option>
        <option value="4">' . $V4 . '</option>
        <option value="5" selected>' . $V5 . '</option>
        <option value="6">' . $V6 . '</option>
        <option value="7">' . $V7 . '</option>
      </select>
      ';
          break;
        case '6':
          echo '
      <select id="lista' . $r->id . '" onchange="cbboxid(this.id)">
        <option value="0">' . $V0 . '</option>
        <option value="1">' . $V1 . '</option>
        <option value="2">' . $V2 . '</option>
        <option value="3">' . $V3 . '</option>
        <option value="4">' . $V4 . '</option>
        <option value="5">' . $V5 . '</option>
        <option value="6" selected>' . $V6 . '</option>
        <option value="7">' . $V7 . '</option>
      </select>
      ';
          break;
        case '7':
          echo '
      <select id="lista' . $r->id . '" onchange="cbboxid(this.id)">
        <option value="0">' . $V0 . '</option>
        <option value="1">' . $V1 . '</option>
        <option value="2">' . $V2 . '</option>
        <option value="3">' . $V3 . '</option>
        <option value="4">' . $V4 . '</option>
        <option value="5">' . $V5 . '</option>
        <option value="6">' . $V6 . '</option>
        <option value="7" selected>' . $V7 . '</option>
      </select>
      ';
          break;
      }
      print "</td>";

      print "</tr>";
    }
    print '<input type="hidden" id="listaid" name="listaid" value="">';
    print '<input type="hidden" id="CbBoxValue" name="CbBoxValue" value="">';
    print '<input type="hidden" id="modId" name="modId" value="">';
    print '<input type="hidden" id="modText" name="modText" value="">';

    print '<input type="submit" value="Mentés" id="submit" name="submit">';
    print "</table>";
    ?>
  </body>

  </html>
<?php
}
?>