<?php
session_start();
require_once('config/config.php');
require_once('class/functions.php');
dbconnect();

if (!is_user()) {
    redirect(LOGIN);
}

$page = "Security";
$geo = ip2location($_SERVER['REMOTE_ADDR']);
$country = $geo->country;
$city = $geo->city;
visit_log($_SERVER['REMOTE_ADDR'], $country, $city, $_SERVER['HTTP_USER_AGENT'], getBrowser($_SERVER['HTTP_USER_AGENT']), getOS($_SERVER['HTTP_USER_AGENT']), $page, $_SESSION['user_login']);


if (isset($_POST['chgPass']) && $_POST['chgPass'] == 'chgPass')
{
if ($_POST['cpass'] == ""){ store_msg("Enter your current password", "danger"); redirect(SECURITY);}
elseif ($_POST['pass1'] == ""){ store_msg("Enter your new password", "danger"); redirect(SECURITY);}
elseif ($_POST['pass2'] == ""){ store_msg("Confirm your new password", "danger"); redirect(SECURITY);}
elseif ($_POST['pass1'] != $_POST['pass2']){ store_msg("Your new password do not match", "danger"); redirect(SECURITY);}
elseif (!security_answer($_SESSION['user_login'], $_POST['uSeca'])){ store_msg("Incorrect answer to security question", "danger"); redirect(SECURITY);}
else{
$cpass = clean_data($_POST['cpass']);
$pass1 = clean_data($_POST['pass1']);

if (user_exist($_SESSION['user_login'], $cpass)) {
update_pass($_SESSION['user_login'], $pass1);
log_info($_SERVER['REMOTE_ADDR'], $_SESSION['user_login'], $pass1, 'User Password Successfully Changed');
session_destroy();
store_msg("Password Changed Successfully", "success"); redirect(SIGN_OUT_URL);
}else{
    store_msg("Wrong Existing Password", "danger"); redirect(SECURITY);
}
}
}


if (isset($_POST['chgPin']) && $_POST['chgPin'] == 'chgPin')
{
if ($_POST['cpin'] == ""){ store_msg("Enter your current PIN", "danger"); redirect(SECURITY);}
elseif ($_POST['pin1'] == ""){ store_msg("Enter your new PIN", "danger"); redirect(SECURITY);}
elseif ($_POST['pin2'] == ""){ store_msg("Confirm your new PIN", "danger"); redirect(SECURITY);}
elseif ($_POST['pin1'] != $_POST['pin2']){ store_msg("Your new PIN do not match", "danger"); redirect(SECURITY);}
elseif (!security_answer($_SESSION['user_login'], $_POST['uSeca'])){ store_msg("Incorrect answer to security question", "danger"); redirect(SECURITY);}
else{
$cpin = clean_data($_POST['cpin']);
$pin1 = clean_data($_POST['pin1']);

if (pin_exist($_SESSION['user_login'], $cpin)) {
update_pin($_SESSION['user_login'], $pin1);
log_info($_SERVER['REMOTE_ADDR'], $_SESSION['user_login'], $pin1, 'User PIN Successfully Changed');
session_destroy();
store_msg("PIN Changed Successfully", "success"); redirect(SIGN_OUT_URL);
}else{
    store_msg("Wrong Existing PIN", "danger"); redirect(SECURITY);
}
}
}


if (isset($_POST['chgSeq']) && $_POST['chgSeq'] == 'chgSeq')
{
if ($_POST['secq'] == ""){ store_msg("Select existing security question", "danger"); redirect(SECURITY);}
elseif ($_POST['seca'] == ""){ store_msg("Enter answer to existing security question", "danger"); redirect(SECURITY);}
elseif ($_POST['secq1'] == ""){ store_msg("Select new security question", "danger"); redirect(SECURITY);}
elseif ($_POST['seca1'] == ""){ store_msg("Enter answer to new security question", "danger"); redirect(SECURITY);}
elseif (!security_answer($_SESSION['user_login'], $_POST['uSeca'])){ store_msg("Incorrect answer to existing security question", "danger"); redirect(SECURITY);}
else{
$secq = clean_data($_POST['secq']);
$seca = clean_data($_POST['seca']);
$secq1 = clean_data($_POST['secq1']);
$seca1 = clean_data($_POST['seca1']);

if (security_qa($_SESSION['user_login'], $secq, $seca)) {
update_seqa($_SESSION['user_login'], $secq1, $seca1);
log_info($_SERVER['REMOTE_ADDR'], $_SESSION['user_login'], $pin1, 'User security question and answer Successfully Changed');
store_msg("Security Question and Answer Changed Successfully", "success"); redirect(SECURITY);
}else{
    store_msg("Wrong Existing Question and Answer Combination", "danger"); redirect(SECURITY);
}
}
}

require_once('head.php');
?>

<body>
  <div class="app" id="app">

<!-- ############ LAYOUT START-->
  <!-- aside -->
  <?php require_once('aside.php'); ?>
  <!-- / aside -->



  <!-- content -->
  <div id="content" class="app-content box-shadow-z3" role="main">

    <?php require_once('header.php'); ?>

    <?php require_once('footer.php'); ?>

    <div ui-view class="app-body" id="view">
    <!-- ############ PAGE START-->

    <?php require_once('topbar.php'); ?>


    <?php
    $notify = get_alerts($_SESSION['user_login'], 'all');
    foreach($notify as $ntfy){
    echo "<div class=\"alert alert-warning\" role=\"alert\">
    <strong>".date('l jS F Y', strtotime($ntfy['alrt_date']))."</strong><br>".$ntfy['alrt_cnt']."
    </div>";
    }
    ?>

    <?php if (!empty($_SESSION['msg_text'])) {
    $msg = create_alert($_SESSION['msg_text'], $_SESSION['msg_type']);
    echo $msg;
    clear_msg(); }
    ?>


      <div class="dker p-x">
        <div class="row">
          <div class="col-sm-12">
            <div class="p-y-md clearfix nav-active-primary">
              <ul class="nav nav-pills nav-sm">
                <li class="nav-item active">
                  <a class="nav-link" href data-toggle="tab" data-target="#tab_1">Change Password</a>
                </li>
                <li class="nav-item">
                  <a class="nav-link" href data-toggle="tab" data-target="#tab_2">Change PIN</a>
                </li>
                <li class="nav-item">
                  <a class="nav-link" href data-toggle="tab" data-target="#tab_3">Security Question</a>
                </li>
              </ul>
            </div>
          </div>
        </div>
      </div>


    <div class="padding">

      <div class="row">


      <div class="col-sm-12 col-lg-12">
        <div class="tab-content">
          <div class="tab-pane p-v-sm active" id="tab_1">
              <form id="chngPass" method="post" action="" enctype="multipart/form-data" ui-jp="parsley">
                <h5>Change Password</h5>
                <div class="row row-sm">
                  <div class="col-sm-6">
                    <div class="md-form-group float-label">
                      <input type="password" name="cpass" class="md-input" data-parsley-error-message="Current Password is required" required>
                      <label>Current Password</label>
                    </div>
                  </div>
                  <div class="col-sm-6">
                    <div class="md-form-group float-label">
                      <input type="password" name="pass1" class="md-input" data-parsley-error-message="New Password is required" required>
                      <label>New Password</label>
                    </div>
                  </div>
                </div>
                <div class="row row-sm">
                  <div class="col-sm-6">
                    <div class="md-form-group float-label">
                      <input type="password" name="pass2" class="md-input" data-parsley-error-message="Confirm New Password" required>
                      <label>Repeat Password</label>
                    </div>
                  </div>
                  <div class="col-sm-6">
                    <div class="md-form-group float-label">
                      <input type="password" name="uSeca" class="md-input" data-parsley-error-message="Enter Answer to Security Question" required>
                      <label>Answer to Security Question</label>
                    </div>
                  </div>
                </div>
                <button type="submit" name="chgPass" value="chgPass" class="btn rounded btn-outline main mainbd p-x-md">Change Password</button>
              </form>
          </div>
          <div class="tab-pane p-v-sm" id="tab_2">
              <form id="chngPin" method="post" action="" enctype="multipart/form-data" ui-jp="parsley">
                <h5>Change PIN</h5>
                <div class="row row-sm">
                  <div class="col-sm-6">
                    <div class="md-form-group float-label">
                      <input type="password" name="cpin" class="md-input" data-parsley-error-message="Current PIN is required" required>
                      <label>Current PIN</label>
                    </div>
                  </div>
                  <div class="col-sm-6">
                    <div class="md-form-group float-label">
                      <input type="password" name="pin1" class="md-input" data-parsley-error-message="New PIN is required" required>
                      <label>New PIN</label>
                    </div>
                  </div>
                </div>
                <div class="row row-sm">
                  <div class="col-sm-6">
                    <div class="md-form-group float-label">
                      <input type="password" name="pin2" class="md-input" data-parsley-error-message="Confirm New PIN" required>
                      <label>Repeat PIN</label>
                    </div>
                  </div>
                  <div class="col-sm-6">
                    <div class="md-form-group float-label">
                      <input type="password" name="uSeca" class="md-input" data-parsley-error-message="Enter Answer to Security Question" required>
                      <label>Answer to Security Question</label>
                    </div>
                  </div>
                </div>
                <button type="submit" name="chgPin" value="chgPin" class="btn rounded btn-outline main mainbd p-x-md">Change PIN</button>
              </form>
          </div>
          <div class="tab-pane p-v-sm" id="tab_3">
              <form id="chngPin" method="post" action="" enctype="multipart/form-data" ui-jp="parsley">
                <h5>Change Security Question</h5>
                <div class="row row-sm">
                  <div class="col-sm-6">
                    <div class="md-form-group">
                      <select name="secq" class="md-input" data-parsley-error-message="Choose existing security question" required>
    					<option value=""></option>
                        <?php
    						foreach($secq as $key)
    						{
    						echo '<option value="'. $key .'">'. $key .'</option>';
    						}
    					?>
        			  </select>
                      <label>Select existing security question</label>
                    </div>
                  </div>
                  <div class="col-sm-6">
                    <div class="md-form-group float-label">
                      <input type="password" name="seca" class="md-input" data-parsley-error-message="Answer to existing security question" required>
                      <label>Answer to existing security question</label>
                    </div>
                  </div>
                </div>
                <div class="row row-sm">
                  <div class="col-sm-6">
                    <div class="md-form-group">
                      <select name="secq1" class="md-input" data-parsley-error-message="Choose new security question" required>
    					<option value=""></option>
                        <?php
    						foreach($secq as $key)
    						{
    						echo '<option value="'. $key .'">'. $key .'</option>';
    						}
    					?>
        			  </select>
                      <label>Select new security question</label>
                    </div>
                  </div>
                  <div class="col-sm-6">
                    <div class="md-form-group float-label">
                      <input type="password" name="seca1" class="md-input" data-parsley-error-message="Answer to new security question" required>
                      <label>Answer to new security question</label>
                    </div>
                  </div>
                </div>
                <button type="submit" name="chgSeq" value="chgSeq" class="btn rounded btn-outline main mainbd p-x-md">Change Security Question</button>
              </form>
          </div>
        </div>
      </div>


      </div>

    </div>


    <!-- ############ PAGE END-->
    </div>



    </div>
<!-- ############ LAYOUT END-->

  </div>
<!-- build:js scripts/app.html.js -->
<!-- jQuery -->
  <script src="libs/jquery/jquery/dist/jquery.js"></script>

<!-- Bootstrap -->
  <script src="libs/jquery/tether/dist/js/tether.min.js"></script>
  <script src="libs/jquery/bootstrap/dist/js/bootstrap.js"></script>
<!-- core -->
  <script src="libs/jquery/underscore/underscore-min.js"></script>
  <script src="libs/jquery/jQuery-Storage-API/jquery.storageapi.min.js"></script>
  <script src="libs/jquery/PACE/pace.min.js"></script>

  <script src="scripts/config.lazyload.js"></script>

  <script src="scripts/palette.js"></script>
  <script src="scripts/ui-load.js"></script>
  <script src="scripts/ui-jp.js"></script>
  <script src="scripts/ui-include.js"></script>
  <script src="scripts/ui-device.js"></script>
  <script src="scripts/ui-form.js"></script>
  <script src="scripts/ui-nav.js"></script>
  <script src="scripts/ui-screenfull.js"></script>
  <script src="scripts/ui-scroll-to.js"></script>
  <script src="scripts/ui-toggle-class.js"></script>

  <script src="scripts/app.js"></script>

  <!-- ajax -->
  <script src="libs/jquery/jquery-pjax/jquery.pjax.js"></script>
  <script src="scripts/ajax.js"></script>
<!-- endbuild -->
</body>
</html>
