<?php
ob_start();
session_start();
?>
<?php include('til-config.php'); ?>
<?php include('includes/til-version.php'); ?>


<?php $bg_number = rand(1,5); ?>

<style>

    .bg_opacity{
        background: url('<?php echo _site_url; ?>/content/themes/default/img/login/bg-<?php echo $bg_number; ?>.jpg') no-repeat center;
        background-size: 100%;
    }
    .bg_opacity {
        height: 100%;
        width: 100%;
    }
</style>
<?php

$login_form_error = false;
if(isset($_POST['username'])) {

    // Bağlatıyı Kuralım.
    $db = new mysqli(_serverName, _userName, _userPassword);
    if($db->connect_errno) {
        echo "Bağlantı Hatası:".$db->connect_errno;
        exit;
    }

    // Veritabanımızı Seçelim.
    $db->select_db(_dbName);
    $db->query("SET NAMES 'utf8'");

    function db() {
        global $db;
        return $db;
    }


    function dbname($val) {
        global $til;
        return _prefix.$val;
    }

    /**
     * title: get_template_url()
     * desc: secili temanın klasör adresini url olarak döndürür
     */
    function get_template_url($val='')
    {
        return get_site_url().'/content/themes/default/'.$val;
    }
    /**
     * title: template_url()
     * func: get_template_url()
     */
    function template_url($val='')
    {
        echo get_template_url($val);
    }


    /**
     * title: get_site_url()
     * desc: site adresini dondurur
     */
    function get_site_url($val='', $val_2=false)
    {
        if(_helper_site_url($val)) {
            $val = _helper_site_url($val);

            if(is_numeric($val_2)) {
                $val = $val.'?id='.$val_2;
            } else {
                $val = $val.'?'.$val_2;
            }
        }

        if(substr($val, 0,1) == '/') {
            return _site_url.''.$val;
        } else {
            return _site_url.'/'.$val;
        }
    }


    /**
     * title: site_url()
     * desc: site adresini gosterir
     * func: get_site_url()
     */
    function site_url($val='', $val_2=false)
    {
        echo get_site_url($val, $val_2);
    }


    /*
     * _helper_site_url()
     *  get_site_url() fonksiyonu icin kisaltmalari olusturur
     */
    function _helper_site_url($val) {
        if($val == 'form') {
            return 'admin/form/detail.php';
        } else if($val == 'account') {
            return 'admin/account/detail.php';
        } else if($val == 'payment') {
            return 'admin/payment/detail.php';
        } else if($val == 'item') {
            return 'admin/item/detail.php';
        } else if($val == 'message') {
            return 'admin/user/message/detail.php';
        } else if($val == 'task') {
            return 'admin/user/task/detail.php';
        } else {
            return false;
        }
    }

    include ('includes/input.php');
    include ('includes/helper.php');
    include ('includes/db.php');
    include ('includes/user.php');
    include ('includes/notification.php');

    $username = trim(addslashes($_POST['username']));
    $password = trim(addslashes($_POST['password']));

    if ( $q_login = db()->query("SELECT * FROM ".dbname('users')." WHERE username='$username' AND password='$password'") ) {
        if( $q_login->num_rows > 0 ) {
            $login = $q_login->fetch_assoc();
            $_SESSION['login_id'] = $login['id'];

            if ( $q_select = db()->query("SELECT * FROM ". dbname('users') ." WHERE (role='1' OR role='2') AND id='". $_SESSION['login_id'] ."' ") ) {
                if ( $q_select->num_rows ) {
                    if ( $query = @file_get_contents('http://api.tilpark.org/version.php?version='.til()->version.'&user_ip='.$_SERVER['SERVER_ADDR']) ) {
                        if ( is_json($query) ) {
                            $query = json_decode($query);

                            if ( $query->version > til()->version ) {
                                if ( $q_select = db()->query("SELECT * FROM ". dbname('messages') ." WHERE choice='". $query->version ."' AND type='notification' AND rec_u_id='". $_SESSION['login_id'] ."' ") ) {
                                    if ( !$q_select->num_rows ) {
                                        if ( add_notification(array('rec_u_id' => $_SESSION['login_id'], 'title' => 'Ortaklar Muhasebe v1.0 '. $query->version .' sürümü hazır.', 'message' => $query->url, 'writing' => 'fa fa-download', 'choice' => $query->version)) ) {

                                            header("Location:"._site_url);
                                        } else { header("Location:"._site_url); }
                                    } else { header("Location:"._site_url); }
                                } else { header("Location:"._site_url); }
                            } else { header("Location:"._site_url); }
                        } else { header("Location:"._site_url); }
                    } else { header("Location:"._site_url); }
                } else { header("Location:"._site_url); }
            } else { header("Location:"._site_url); }
        } else {
            $login_form_error = true;
        }
    }
}
?>



<!DOCTYPE html>
  <!--[if IE 8]> <html lang="en" class="ie8"> <![endif]-->
      <!--[if !IE]><!-->
  <html lang="en">
      <!--<![endif]-->
      <head>
      <meta charset="utf-8" />
      <title>Ortaklar Muhasebe v1.0 Beta</title>
  <meta content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no" name="viewport" />
      <meta content="" name="description" />
      <meta content="" name="author" />

      <!-- ================== BEGIN BASE CSS STYLE ================== -->
      <link href="http://fonts.googleapis.com/css?family=Open+Sans:300,400,600,700" rel="stylesheet" />
      <link href="<?php echo _site_url; ?>/content/themes/default/new_login/jquery-ui.min.css" rel="stylesheet" />
      <link href="<?php echo _site_url; ?>/content/themes/default/new_login/bootstrap.min.css" rel="stylesheet" />
      <link href="<?php echo _site_url; ?>/content/themes/default/new_login/fontawesome-all.min.css" rel="stylesheet" />
      <link href="<?php echo _site_url; ?>/content/themes/default/new_login/animate.min.css" rel="stylesheet" />
      <link href="<?php echo _site_url; ?>/content/themes/default/new_login/style.min.css" rel="stylesheet" />
      <link href="<?php echo _site_url; ?>/content/themes/default/new_login/style-responsive.min.css" rel="stylesheet" />
      <link href="<?php echo _site_url; ?>/content/themes/default/new_login/default.css" rel="stylesheet" id="theme" />
      <!-- ================== END BASE CSS STYLE ================== -->

      <!-- ================== BEGIN BASE JS ================== -->
      <script src="<?php echo _site_url; ?>/content/themes/default/new_login/pace.min.js"></script>
  <!-- ================== END BASE JS ================== -->
  </head>
  <body class="pace-top bg-white">
  <!-- begin #page-loader -->
  <div id="page-loader" class="fade show"><span class="spinner"></span></div>
  <!-- end #page-loader -->

  <!-- begin #page-container -->
  <div id="page-container" class="fade">
      <!-- begin login -->
      <div class="login login-with-news-feed">
          <!-- begin news-feed -->
          <div class="news-feed">
              <div class="bg_opacity"></div>
              <div class="news-caption">
                  <h4 class="caption-title"><b>Ortaklar Özel Servis</b> Muhasebe v1.0 Beta</h4>
                  <p>
                  </p>
              </div>
          </div>
          <!-- end news-feed -->
          <!-- begin right-content -->
          <div class="right-content">
              <!-- begin login-header -->
              <div class="login-header">
                  <div class="brand">
                      <span><img src="<?php echo _site_url; ?>/content/themes/default/img/logo_header_login.png" class="img-responsive" style="width: 300px;">
</span>
                      <small>Ortaklar Özel Servis Muhasebe Modülü</small>
                  </div>
                  <div class="icon">
                      <i class="fa fa-sign-in"></i>
                  </div>
              </div>

              <?php if($login_form_error): ?>
                  <div class="alert alert-danger alert-loginError" role="alert"> <strong><i class="fa fa-exclamation-triangle"></i> Giriş başarısız!</strong> Kullanıcı adı veya şifre hatalı. </div>
                  <style type="text/css">
                      .alert-loginError{
                          animation: alertloginError 200ms infinite;
                          animation-iteration-count: 5;
                      }

                      @keyframes alertloginError{
                          0%{ border-color: #a94442; }
                          100%{ border-color: #ebccd1; }
                      }
                  </style>

              <?php endif; ?>

              <!-- end login-header -->
              <!-- begin login-content -->

              <div class="login-content">
                  <form name="form_login" id="form_login" action="" method="POST" class="margin-bottom-0" class="validation">
                      <div class="form-group m-b-15">
                          <input type="text"  name="username" id="username" class="form-control form-control-lg" placeholder="E-posta" required value="<?php echo @$username;?>" />
                      </div>
                      <div class="form-group m-b-15">
                          <input type="password" name="password" id="password" class="form-control form-control-lg" placeholder="Şifre" required value="<?php echo @$password; ?>" />
                      </div>
                      <div class="checkbox checkbox-css m-b-30">
                          <input type="checkbox" id="remember_me_checkbox" value="" />
                          <label for="remember_me_checkbox">
                              Beni hatırla
                          </label>
                      </div>
                      <div class="login-buttons">
                          <button  class="btn btn-success btn-block btn-lg">Giriş Yap</button>
                      </div>

                      <hr />
                      <p class="text-center text-grey-darker">
                          &copy; 2019 Ortaklar Muhasebe v1.0 Beta Tüm Hakları Saklıdır</p>
                  </form>
              </div>


              </div>
              <!-- end login-content -->
          </div>
          <!-- end right-container -->
      </div>

  <!-- ================== BEGIN BASE JS ================== -->
  <script src="<?php echo _site_url; ?>/content/themes/default/new_login/jquery-3.2.1.min.js"></script>
  <script src="<?php echo _site_url; ?>/content/themes/default/new_login/jquery-ui.min.js"></script>
  <script src="<?php echo _site_url; ?>/content/themes/default/new_login/bootstrap.bundle.min.js"></script>
  <!--[if lt IE 9]>
  <script src="<?php echo _site_url; ?>/content/themes/default/new_login/html5shiv.js"></script>
  <script src="<?php echo _site_url; ?>/content/themes/default/new_login/respond.min.js"></script>
  <script src="<?php echo _site_url; ?>/content/themes/default/new_login/excanvas.min.js"></script>
  <![endif]-->
  <script src="<?php echo _site_url; ?>/content/themes/default/new_login/jquery.slimscroll.min.js"></script>
  <script src="<?php echo _site_url; ?>/content/themes/default/new_login/js.cookie.js"></script>
  <script src="<?php echo _site_url; ?>/content/themes/default/new_login/default.min.js"></script>
  <script src="<?php echo _site_url; ?>/content/themes/default/new_login/apps.min.js"></script>
  <!-- ================== END BASE JS ================== -->

  <script>
      $(document).ready(function() {
          App.init();
      });
  </script>
  </body>
</html>