<?php
session_start();
error_reporting(0);
if (!isset($_SESSION["mikhmon"])) {
  header("Location:../admin.php?id=login");
} else {
  $session = $_GET['session'];
  $url = $_SERVER['REQUEST_URI'];
  include('./include/config.php');
  include('./include/readcfg.php');
  include('./include/lang.php');
  include('./lang/'.$langid.'.php');

  if (isset($_POST['save'])) {
    $surl = trim($_POST['genieacs_url']);
    $suser = trim($_POST['genieacs_user']);
    $spass = trim($_POST['genieacs_pass']);
    $file = file('./include/config.php');
    foreach ($file as $k => $line) {
      $name = explode("'", $line)[1];
      if ($name === $session) {
        $clean = preg_replace("/'".preg_quote($session, '/')."~[^']*'/", "", $line);
        $clean = preg_replace("/'".preg_quote($session, '/')."u\\|u[^']*'/", "", $clean);
        $clean = preg_replace("/'".preg_quote($session, '/')."p\\|p[^']*'/", "", $clean);
        $clean = preg_replace("/,\\s*\\)/", ")", $clean);
        $append = [];
        if ($surl !== '') $append[] = "'".$session."~".$surl."'";
        if ($suser !== '') $append[] = "'".$session."u|u".$suser."'";
        if ($spass !== '') $append[] = "'".$session."p|p".encrypt($spass)."'";
        if (!empty($append)) {
          $clean = preg_replace("/\\);\\s*$/", ",".implode(",", $append).");", $clean);
        }
        $file[$k] = $clean;
        break;
      }
    }
    file_put_contents('./include/config.php', implode("", $file));
    $genieacs_url = $surl !== '' ? $surl : $genieacs_url;
    $genieacs_user = $suser !== '' ? $suser : $genieacs_user;
    $genieacs_pass = $spass !== '' ? $spass : $genieacs_pass;
  }
}
?>
<div class="row">
  <div class="col-12">
    <div class="card">
      <div class="card-header">
        <h3 class="card-title"><i class="fa fa-plug"></i> GenieACS Settings</h3>
      </div>
      <div class="card-body">
        <form method="post" action="" autocomplete="off">
          <table class="table table-sm">
            <tr>
              <td class="align-middle">GenieACS URL</td>
              <td><input class="form-control" type="text" name="genieacs_url" value="<?= htmlspecialchars($genieacs_url); ?>" placeholder="http://localhost:7557"/></td>
            </tr>
            <tr>
              <td class="align-middle">GenieACS User</td>
              <td><input class="form-control" type="text" name="genieacs_user" value="<?= htmlspecialchars($genieacs_user); ?>" placeholder="admin"/></td>
            </tr>
            <tr>
              <td class="align-middle">GenieACS Password</td>
              <td>
                <div class="input-group">
                  <div class="input-group-11 col-box-10">
                    <input class="group-item group-item-l" id="genieacs_pass" type="password" name="genieacs_pass" value="<?= htmlspecialchars($genieacs_pass); ?>"/>
                  </div>
                  <div class="input-group-1 col-box-2">
                    <div class="group-item group-item-r pd-2p5 text-center align-middle">
                      <input type="checkbox" title="Show/Hide Password" onclick="(function(){var x=document.getElementById('genieacs_pass');x.type=x.type==='password'?'text':'password'})()">
                    </div>
                  </div>
                </div>
              </td>
            </tr>
            <tr>
              <td></td>
              <td class="text-right">
                <div class="input-group-4">
                  <input class="group-item group-item-l" type="submit" name="save" value="<?= $_save ?>"/>
                </div>
                <div class="input-group-2">
                  <div class="group-item group-item-r pd-2p5 text-center" onclick="location.reload();" title="Reload Data"><i class="fa fa-refresh"></i></div>
                </div>
              </td>
            </tr>
          </table>
        </form>
      </div>
    </div>
  </div>
  <div class="col-12">
    <div class="card">
      <div class="card-header">
        <h3 class="card-title"><i class="fa fa-dashboard"></i> Preview</h3>
      </div>
      <div class="card-body">
        <div>URL: <?= htmlspecialchars($genieacs_url); ?></div>
        <div>User: <?= htmlspecialchars($genieacs_user); ?></div>
      </div>
    </div>
  </div>
 </div>
