<?php
/*
 * webJAM - A modern JAM MessageBase Editor
 * Copyright (C) 2017  Philipp Giebel (stimpy@kuehlbox.wtf)
 * 
 * This program is free software: you can redistribute it and/or modify it under the terms of the 
 * GNU General Public License as published by the Free Software Foundation, either version 3 of 
 * the License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; 
 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 
 * See the GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License along with this program. 
 * If not, see (http://www.gnu.org/licenses/).
 * 
 *   * filename: index.php
 *   * description: Main site. webJAM starts here.
 * 
 */
  require_once 'assets/include/init.inc.php';
?>
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <meta name="description" content="JAM msgbase reaser">
    <meta name="author" content="Philipp Giebel">
    <link rel="apple-touch-icon" sizes="180x180" href="<?= $cfg['urlbase']; ?>apple-touch-icon.png">
    <link rel="icon" type="image/png" sizes="32x32" href="<?= $cfg['urlbase']; ?>favicon-32x32.png">
    <link rel="icon" type="image/png" sizes="16x16" href="<?= $cfg['urlbase']; ?>favicon-16x16.png">
    <link rel="manifest" href="<?= $cfg['urlbase']; ?>manifest.json">
    <link rel="mask-icon" href="<?= $cfg['urlbase']; ?>safari-pinned-tab.svg" color="#5bbad5">    <meta name="theme-color" content="#ffffff">

    <title>webJAM</title>
    
    <link rel="stylesheet" href="assets/css/complete.min.css">
    
    <!-- HTML5 shim and Respond.js for IE8 support of HTML5 elements and media queries -->
    <!--[if lt IE 9]>
      <script src="assets/js/iecompat.min.js"></script>
    <![endif]-->
    <!--[if lt IE 11]>
      <script src="assets/js/ie10-viewport-bug-workaround.min.js"></script>
    <![endif]-->
  </head>

  <body class="dos <?= $cfg['theme']; ?>">
    <nav class="nav navbar navbar-fixed-top navbar-inverse">
      <div class="container-fluid">
        <div class="navbar-header">
          <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#sidebar" aria-expanded="false" aria-controls="navbar">
            <span class="sr-only">Toggle navigation</span>
            <span class="icon-bar"></span>
            <span class="icon-bar"></span>
            <span class="icon-bar"></span>
          </button>
          <div class="navbar-right pull-right">
<?php
            if ( $user == false ) {
?>
              <a href="#" data-toggle="modal" data-target="#login-modal" class="btn btn-success"><span class="glyphicon glyphicon-log-in"></span></a>
<?php
            } else {
?>
              <a href="index.php?action=logout" class="btn btn-danger" title="Logout"><span class="glyphicon glyphicon-log-out"></span></a>
<?php
              if ( $user['readonly'] === false ) {
?>
                <a href="?echo=<?= $echo; ?>&action=new" class="btn btn-primary btn-newmail" title="New Mail"><span class="glyphicon glyphicon-pencil"></span></a>
<?php
              }
            }
?>
          </div>
          <a class="navbar-brand" href="index.php">webJAM v<?= VERSION; ?></a>
          <a href="#" class="btn-toggle btn-theme" title="Toggle theme"><span class="glyphicon glyphicon-certificate"></span></a>
          <a href="#" class="btn-toggle btn-font" title="Switch font"><span class="glyphicon glyphicon-font"></span></a>
        </div>
      </div>
    </nav>
  <div class="modal fade" id="login-modal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true" style="display: none;">
    <div class="modal-dialog">
      <div class="loginmodal-container">
        <h1>Login to Your Account</h1><br>
        <form action="index.php" method="post" id="loginform">
          <input type="text" name="username" id="username" placeholder="Username">
          <input type="password" name="password" id="password" placeholder="Password">
          <input type="submit" name="login" class="login loginmodal-submit" value="Login">
          <div class="login-help">
            <input type="checkbox" name="stay" id="stay" value="1" /> <label for="stay">Stay logged in</label>
          </div>
        </form>
      </div>
      </div>
    </div>
    <div id="disabled">
      <div class="spinner">
        <div class="rect1"></div>
        <div class="rect2"></div>
        <div class="rect3"></div>
        <div class="rect4"></div>
        <div class="rect5"></div>
      </div>
    </div>
    
    <div class="container-fluid">
      <div class="row">
        <div class="col-sm-3 col-md-2 sidebar scrollbars" id="sidebar">
<?php
          if ( $user != false ) {
            foreach ( array_keys( $user['groups'] ) as $group ) {
              if ( $group != 'netmail' ) {
?>
                <a role="button" data-toggle="collapse" href="#group<?= $group; ?>" aria-expanded="false" aria-controls="group<?= $group; ?>" class="btn-collapse<?= $echo == '' ? '' : $group == $echos[$echo]['group'] ? ' active"' : ''; ?>"><?= $group; ?></a>
                <div class="collapse" id="group<?= $group; ?>">
<?php
              }
?>
                  <ul class="nav nav-sidebar echos">
<?php
                    foreach ( array_keys( $echos ) as $e ) {
                      if ( $group == $echos[$e]['group'] ) {
?>
                        <li<?= $e == $echo ? ' class="active"' : ''; ?>><a href="?echo=<?= urlencode( $e ); ?>#echo<?= urlencode( $e ); ?>" id="echo<?= urlencode( $e ); ?>"><?= $e; ?></a></li>
<?php
                      }
                    }
?>
                  </ul>
<?php
              if ( $group != 'netmail' ) {
?>
                </div>
<?php
              }
            }
          }
?>
        </div>
        <div class="col-sm-9 col-sm-offset-3 col-md-10 col-md-offset-2 main">
          <div class="container-fluid">
            <div class="row msgheaders scrollbars">
              <div class="col-sm-12">
                  <table class="table table-condensed table-hover table-responsive">
                    <thead>
                      <tr>
                        <th width="40%">Subject</th>
                        <th width="20%">Sender</th>
                        <th width="20%">Receiver</th>
                        <th width="10%">Date written</th>
                        <th width="10%">Date received</th>
                      </tr>
                    </thead>
                    <tbody id="headersbody">
<?php
                      if ( $message_headers ) {
                          for ( $i = 0; $i < count( $message_headers ); $i++ ) {
?>
                            <tr id="msg<?= $i; ?>"<?= $i == $msgid ? ' class="active"' : ''; ?>>
                              <td><a href="?echo=<?= $echo; ?>&msgid=<?= $i; ?>#msg<?= $i; ?>"><?= $message_headers[$i]['SUBJECT']; ?></a></td>
                              <td><a href="?echo=<?= $echo; ?>&msgid=<?= $i; ?>#msg<?= $i; ?>"><?= $message_headers[$i]['SENDERNAME']; ?></a></td>
                              <td><a href="?echo=<?= $echo; ?>&msgid=<?= $i; ?>#msg<?= $i; ?>"><?= $message_headers[$i]['RECEIVERNAME']; ?></a></td>
                              <td><a href="?echo=<?= $echo; ?>&msgid=<?= $i; ?>#msg<?= $i; ?>"><?= date( 'm/d H:i', $message_headers[$i]['datewritten'] ); ?></a></td>
                              <td><a href="?echo=<?= $echo; ?>&msgid=<?= $i; ?>#msg<?= $i; ?>"><?= date( 'm/d H:i', $message_headers[$i]['datereceived'] ); ?></a></td>
                            </tr>
<?php
                          }
                      }
?>
                    </tbody>
                  </table>
              </div>
            </div>
            <div class="row msgtext">
              <form action="?echo=<?= $echo; ?>&action=postmail" method="post">
                <div class="col-md-12">
                  <div class="container-fluid msgheader">
                    <div class="row">
                      <div class="col-xs-7">
                        <div class="col-md-1 title">
                          From:
                        </div>
                        <div class="col-md-11 from">
                          <?= $msgid ? $message_headers[$msgid]['SENDERNAME'] : '&nbsp;'; ?>
                        </div>
                        <div class="col-md-1 title">
                          To:
                        </div>
                        <div class="col-md-11 to">
                          <?= $msgid ? $message_headers[$msgid]['RECEIVERNAME'] : '&nbsp;'; ?>
                        </div>
                      </div>
                      <div class="col-xs-5">
                        <div class="col-md-3 title">
                          Written:
                        </div>
                        <div class="col-md-9 datewritten">
                          <?= $msgid ? date( 'Y-m-d H:i', $message_headers[$msgid]['datewritten'] ) : '&nbsp;'; ?>
                        </div>
                        <div class="col-md-3 title akatitle">
                          Processed:
                        </div>
                        <div class="col-md-9 dateprocessed">
                          <?= $msgid ? date( 'Y-m-d H:i', $message_headers[$msgid]['dateprocessed'] ) : '&nbsp;'; ?>
                        </div>
                      </div>
                    </div>
                    <div class="row">
                      <div class="col-md-12">
                        <span class="title">
                         Subject:
                        </span>
                        <span class="subject">
                          <?= $msgid ? $message_headers[$msgid]['SUBJECT'] : '&nbsp;'; ?>
                        </span>
                      </div>
                    </div>
                  </div>
                  <div class="msgbody scrollbars">
<?=
                    $msgid ? utf8_encode( getMessage( $echos[$echo]['filename'] .'.jdt', $message_headers[$msgid]['offset'], $message_headers[$msgid]['txtlen'] ) ) : '';
?>
                  </div>
                </div>
              </form>
            </div>
          </div>
      </div>
    </div>
    <script src="assets/js/complete.min.js"></script>
    <!--[if lt IE 9]>
        <script>
            Object.defineProperty = function () { /* Do Nothing */ };
        </script>
    <![endif]-->
<?php
    if ( $user != false ) {
?>
      <script>
        $user = [];
        $cfg = [];
        $user['ajaxtoken'] = "<?= hash( 'sha256', $user['password'] ); ?>";
        $user['username'] = "<?= $user['username']; ?>";
        $user['realname'] = "<?= $user['realname']; ?>";
        $user['aliases'] = <?=  json_encode($user['aliases']); ?>;
        $user['origin'] = "<?= $user['origin']; ?>";
        $user['readonly'] = <?= $user['readonly'] ? 'true' : 'false'; ?>;
        $echos = <?= json_encode( $echos ); ?>;
        VERSION = "<?= VERSION; ?>";
      </script>
<?php
    }
?>
  </body>
</html>