Эх сурвалжийг харах

Added user system, added mail-bridge

Moritz Schmidt 10 жил өмнө
parent
commit
519165a134

+ 4 - 0
cron.php

@@ -7,6 +7,8 @@ include('includes/database.inc.php');
 include('includes/functions.inc.php');
 include('includes/document.inc.php');
 include('includes/label.inc.php');
+include('includes/imap.inc.php');
+include('includes/mailboxfolder.inc.php');
 
 $db = new Database($CONFIG['dbHost'], $CONFIG['dbUser'], $CONFIG['dbPassword'], $CONFIG['dbDatabase']);
 
@@ -19,4 +21,6 @@ foreach($dirs as $dir) {
     searchNewFiles($dir);
 }
 
+searchMails();
+
 ?>

+ 0 - 1
includes/config.inc.php

@@ -6,7 +6,6 @@ $CONFIG = array(
 	"dbPassword"	=>		"root", // Database Password
 	"dbDatabase"	=>		"atoffice", // Database name
 	"documentPath"	=> 		"/home/moritz/atOffice/documents/" // Full path to documents folder
-
 );
 
 ?>

+ 61 - 43
includes/controller.inc.php

@@ -21,11 +21,20 @@ class Controller {
 		$this->request = $request;
 		$this->template = !empty($request['action']) ? $request['action'] : 'default';
 
-		if(isset($request['action']) && $request['action'] === "login" && isset($request['username']))
+		if(isset($request['action']) && $request['action'] === "login" && isset($request['username'])) {
+			echo "jo";
 			$user->login($request['username'], $request['password']);
+		}
 
-		if(isset($request['action']) && $request['action'] === "logout")
+		if(isset($request['action']) && $request['action'] === "logout") {
 			$user->logout();
+		}
+
+		if(!isset($_SESSION['loggedIn']) || (isset($_SESSION['loggedIn']) && !$_SESSION['loggedIn'])) {
+			$user->setLoggedIn(false);
+		} else {
+			$user->loginByUsername($_SESSION['username']);
+		}
 	}
 
 	/**
@@ -35,48 +44,57 @@ class Controller {
 	*/
 	public function display() {
 		global $user;
-		$innerView = new View();
-		switch($this->template){
-			case 'login':
-				$innerView->setTemplate('login');
-				$entryid = $this->request['id'];
-				$entry = Model::getEntry($entryid);
-				$innerView->assign('title', $entry['title']);
-				$innerView->assign('content', $entry['content']);
-				break;
-			case 'profile':
-				$innerView->setTemplate('profile');
-				$openvpn = new OpenVPNStatus("/home/moritz/www/openvpn-status.log");
-				$clientName = $user->getClients();
-				$hostname = searchForHostname($clientName, $openvpn->logfile);
-				$innerView->assign('openvpn', $openvpn->logfile[$hostname]);
-				break;
-			case 'download':
-				$innerView->setTemplate('download');
-				break;
-			case 'label':
-				$innerView->setTemplate('label');
-				$innerView->assign('label', getLabelById($this->request['labelId']));
-				$documentView = new View();
-				$documentView->setTemplate('documentlist');
-				$innerView->assign('documentlist', $documentView->loadTemplate());
-				break;
-			case 'default':
-			default:
-				$entries = Model::getEntries();
-				$innerView->setTemplate('default');
-		}
 
-		$this->headerView->setTemplate('header');
-		$this->footerView->setTemplate('footer');
-		$this->view->setTemplate('matrix');
-		$this->headerView->assign('title', 'MMNxVPN');
-		$this->headerView->assign('request', $this->request);
-		$this->headerView->assign('labels', getLabels());
-		$this->view->assign('blog_title', $this->headerView->loadTemplate());
-		$this->view->assign('blog_footer', $this->footerView->loadTemplate());
-		$this->view->assign('blog_content', $innerView->loadTemplate());
-		return $this->view->loadTemplate();
+		if($user->getLoggedIn()) {
+			$innerView = new View();
+
+			switch($this->template){
+				case 'login':
+					$innerView->setTemplate('login');
+					$entryid = $this->request['id'];
+					$entry = Model::getEntry($entryid);
+					$innerView->assign('title', $entry['title']);
+					$innerView->assign('content', $entry['content']);
+					break;
+				case 'profile':
+					$innerView->setTemplate('profile');
+					$openvpn = new OpenVPNStatus("/home/moritz/www/openvpn-status.log");
+					$clientName = $user->getClients();
+					$hostname = searchForHostname($clientName, $openvpn->logfile);
+					$innerView->assign('openvpn', $openvpn->logfile[$hostname]);
+					break;
+				case 'download':
+					$innerView->setTemplate('download');
+					break;
+				case 'label':
+					$innerView->setTemplate('label');
+					$innerView->assign('label', getLabelById($this->request['labelId']));
+					$documentView = new View();
+					$documentView->setTemplate('documentlist');
+					$documentView->assign('labelId', $this->request['labelId']);
+					$innerView->assign('documentlist', $documentView->loadTemplate());
+					break;
+				case 'default':
+				default:
+					$entries = Model::getEntries();
+					$innerView->setTemplate('default');
+			}
+
+			$this->headerView->setTemplate('header');
+			$this->footerView->setTemplate('footer');
+			$this->view->setTemplate('matrix');
+			$this->headerView->assign('title', 'atOfficeWeb');
+			$this->headerView->assign('request', $this->request);
+			$this->headerView->assign('labels', getLabels());
+			$this->view->assign('blog_title', $this->headerView->loadTemplate());
+			$this->view->assign('blog_footer', $this->footerView->loadTemplate());
+			$this->view->assign('blog_content', $innerView->loadTemplate());
+			return $this->view->loadTemplate();
+		} else {
+			$this->view->setTemplate('login');
+			//$this->view->assign('blog_content', $innerView->loadTemplate());
+			return $this->view->loadTemplate();
+		}
 	}
 }
 

+ 13 - 1
includes/document.inc.php

@@ -8,8 +8,10 @@ class Document {
     private $draft      =   NULL;
     private $created    =   NULL;
     private $lastChange =   NULL;
+    private $type       =   NULL;
+    private $mailUid    =   NULL;
 
-    public function __construct($id, $fileName, $path, $labelId, $draft, $created, $lastChange) {
+    public function __construct($id, $fileName, $path, $labelId, $draft, $created, $lastChange, $type, $mailUid) {
         $this->id           = $id;
         $this->fileName     = $fileName;
         $this->path         = $path;
@@ -17,6 +19,8 @@ class Document {
         $this->draft        = $draft;
         $this->created      = $created;
         $this->lastChange   = $lastChange;
+        $this->type         = $type;
+        $this->mailUid      = $mailUid;
     }
 
     public function getId() {
@@ -43,6 +47,14 @@ class Document {
         return $this->lastChange;
     }
 
+    public function getType() {
+        return $this->type;
+    }
+
+    public function getMailUid() {
+        return $this->mailUid;
+    }
+
 }
 
 ?>

+ 119 - 5
includes/functions.inc.php

@@ -8,6 +8,10 @@ function pa($array) {
 	echo '</pre>';
 }
 
+function error($message) {
+	echo $message;
+}
+
 function getLabels() {
 	global $db;
 
@@ -54,7 +58,7 @@ function getDocumentsByLabelId($labelId) {
 	$documents = $db->selectQuery("SELECT * FROM `documents` WHERE `label_id` = " . $labelId . ";");
 
 	foreach($documents as $document) {
-		$return[] = new Document($document->id, $document->filename, $document->path, $document->label_id, $document->draft, $document->created, $document->last_change);
+		$return[] = new Document($document->id, $document->filename, $document->path, $document->label_id, $document->draft, $document->created, $document->last_change, $document->type, $document->mail_uid);
 	}
 
 	return $return;
@@ -67,15 +71,92 @@ function getDocumentsByPath($path) {
 	$documents = $db->selectQuery("SELECT documents.* FROM `documents` JOIN `labels` ON labels.id = documents.label_id WHERE labels.path = '" . $path . "';");
 
 	foreach($documents as $document) {
-		$return[] = new Document($document->id, $document->filename, $document->path, $document->label_id, $document->draft, $document->created, $document->last_change);
+		$return[] = new Document($document->id, $document->filename, $document->path, $document->label_id, $document->draft, $document->created, $document->last_change, $document->type, $document->mail_uid);
+	}
+
+	return $return;
+}
+
+function getDocumentsByMailUidLabelId($mailUid, $labelId) {
+	global $db;
+
+	$return = array();
+	$documents = $db->selectQuery("SELECT * FROM `documents` WHERE `mail_uid` = " . $mailUid . " AND `label_id` = " . $labelId . ";");
+
+	foreach($documents as $document) {
+		$return[] = new Document($document->id, $document->filename, $document->path, $document->label_id, $document->draft, $document->created, $document->last_change, $document->type, $document->mail_uid);
 	}
 
 	return $return;
 }
 
-function addDocument($type, $fileName, $path, $labelId, $draft, $created, $lastChange) {
+function getMailboxes() {
 	global $db;
-	$query = "INSERT INTO `documents`(`type`, `filename`, `path`, `label_id`, `draft`, `created`, `last_change`) VALUES ('" . $type . "', '" . $fileName . "', '" . $path . "', " . $labelId . ", '" . $draft . "', " . $created . ", " . $lastChange . ");";
+
+	$return = array();
+	$mailboxes = $db->selectQuery("SELECT * FROM `mailboxes`;");
+
+	foreach($mailboxes as $mailbox) {
+		$useSsl = '';
+		$noValidCert = '';
+
+		if($mailbox->use_ssl) {
+			$useSsl = '/ssl';
+		}
+
+		if(!$mailbox->valid_ssl) {
+			$noValidCert = '/novalidate-cert';
+		}
+		$return[] = new Imap('{' . $mailbox->server . ':' . $mailbox->port . '/' . $mailbox->protocol . $useSsl . $noValidCert . '}', $mailbox->username, $mailbox->password); // TODO: Encrypt password
+	}
+
+	return $return;
+}
+
+function getMailboxesByUserId($userId) {
+	global $db;
+
+	$return = array();
+	$mailboxes = $db->selectQuery("SELECT * FROM `mailboxes` WHERE `user_id` = " . $userId . ";");
+
+	foreach($mailboxes as $mailbox) {
+		$useSsl = '';
+		$noValidCert = '';
+
+		if($mailbox->use_ssl) {
+			$useSsl = '/ssl';
+		}
+
+		if(!$mailbox->valid_ssl) {
+			$noValidCert = '/novalidate-cert';
+		}
+		$return[] = new Imap('{' . $mailbox->server . ':' . $mailbox->port . '/' . $mailbox->protocol . $useSsl . $noValidCert . '}', $mailbox->username, $mailbox->password); // TODO: Encrypt password
+	}
+
+	return $return;
+}
+
+function getMailboxFolderByName($folderName) {
+	global $db;
+
+	$return = array();
+	$folders = $db->selectQuery("SELECT * FROM `mailbox-folders` WHERE `folder_name` = '" . $folderName . "';");
+
+	if(!$folders) {
+		return false;
+	}
+
+	foreach($folders as $folder) {
+		$return[] = new MailboxFolder($folder->folder_name, $folder->mailbox_id, $folder->label_id);
+	}
+
+	return $return;
+
+}
+
+function addDocument($type, $fileName, $path, $labelId, $draft, $created, $lastChange, $mailUid) {
+	global $db;
+	$query = "INSERT INTO `documents`(`type`, `filename`, `path`, `label_id`, `draft`, `created`, `last_change`, `mail_uid`) VALUES ('" . $type . "', '" . $fileName . "', '" . $path . "', " . $labelId . ", '" . $draft . "', " . $created . ", " . $lastChange . ", " . $mailUid . ");";
 	$db->insertQuery($query);
 }
 
@@ -103,10 +184,43 @@ function searchNewFiles($scanDir) {
 			continue;
 		}
 
-		addDocument('file', $file, '/', getLabelByPath($scanDir)[0]->getId(), '', 'NOW()', 'NOW()'); // TODO: get dates by filesystem
+		addDocument('file', $file, '/', getLabelByPath($scanDir)[0]->getId(), '', 'NOW()', 'NOW()', 0); // TODO: get dates by filesystem
 
 	}
 
 }
 
+function searchMails() {
+	global $user;
+	//$imap = new Imap('{mail.mmnx.de:993/imap/ssl/novalidate-cert}', 'mobi@mmnx.de', 'msmoro');
+	//$imap->listFolders();
+	$mailboxes = getMailBoxes();
+	//pa($mailboxes);
+
+	foreach($mailboxes as $mailbox) {
+		$mailbox->listFolders();
+	}
+
+	foreach($mailboxes as $mailbox) {
+		foreach($mailbox->getFolders() as $folder) {
+			$mbFolder = getMailboxFolderByName($folder);
+			if($mbFolder[0] != false) {
+				$mailbox->changeFolder($mbFolder[0]->getFolderName());
+
+				$messageCount = imap_num_msg($mailbox->getMailbox());
+
+				for($i = 1; $i <= $messageCount; ++$i) {
+				    $headers = imap_header($mailbox->getMailbox(), $i);
+					$uid = imap_uid($mailbox->getMailbox(), $i);
+
+					$documents = getDocumentsByMailUidLabelId($uid, $mbFolder[0]->getLabelId());
+
+					if(sizeof($documents) < 1) {
+						addDocument('mail', $headers->subject, $headers->from[0]->mailbox . '@' . $headers->from[0]->host, $mbFolder[0]->getLabelId(), '', '\'' . $headers->date . '\'', '\'' . $headers->date . '\'', $uid);
+					}
+				}
+			}
+		}
+	}
+}
 ?>

+ 46 - 0
includes/imap.inc.php

@@ -0,0 +1,46 @@
+<?php
+
+class Imap {
+    private $server     = NULL;
+    private $username   = NULL;
+    private $password   = NULL;
+    private $mailbox    = NULL;
+    private $folders    = NULL;
+
+    public function __construct($server, $username, $password) {
+        $this->server   = $server;
+        $this->username = $username;
+        $this->password = $password;
+
+        $this->mailbox = imap_open($this->server . 'INBOX', $this->username, $this->password) or error('Failed to connect to IMAP');
+    }
+
+    public function changeFolder($folder) {
+        imap_reopen($this->mailbox, $this->server . $folder);
+    }
+
+    public function listFolders() {
+        $newFolders = array();
+        $folders = imap_list($this->mailbox, $this->server, "*");
+
+        foreach($folders as $folder) {
+            $newFolders[] = preg_replace('/(\{(.*)\})(.*)/', '${3}', $folder);
+        }
+
+        $this->folders = $newFolders;
+    }
+
+    public function getFolders() {
+        return $this->folders;
+    }
+
+    public function getServer() {
+        return $this->server;
+    }
+
+    public function getMailbox() {
+        return $this->mailbox;
+    }
+
+
+}

+ 26 - 0
includes/mailboxfolder.inc.php

@@ -0,0 +1,26 @@
+<?php
+
+class MailboxFolder {
+    private $folderName = NULL;
+    private $mailboxId  = NULL;
+    private $labelId    = NULL;
+
+    public function __construct($folderName, $mailboxId, $labelId) {
+        $this->folderName = $folderName;
+        $this->mailboxId = $mailboxId;
+        $this->labelId = $labelId;
+
+    }
+
+    public function getFolderName() {
+        return $this->folderName;
+    }
+
+    public function getMailboxId() {
+        return $this->mailboxId;
+    }
+
+    public function getLabelId() {
+        return $this->labelId;
+    }
+}

+ 50 - 4
includes/user.inc.php

@@ -3,8 +3,10 @@
 Class User {
 
 	private $username 	= "";
+	private $userId		= NULL;
 	private $password 	= "";
 	private $email 		= "";
+	private $loggedIn	= false;
 
 	public function __construct($username) {
 		if($username != NULL) {
@@ -13,32 +15,76 @@ Class User {
 
 	}
 
+	public function setLoggedIn($loggedIn) {
+		$this->loggedIn = $loggedIn;
+	}
+
+	public function getLoggedIn() {
+		return $this->loggedIn;
+	}
+
+	public function setUserId($userId) {
+		$this->userId = $userId;
+	}
+
+	public function getUserId() {
+		return $this->userId;
+	}
+
 	public function login($username, $password) {
 		global $db;
 		$this->username = $username;
 		$this->password = $password;
 
-		$user = $db->query("SELECT * FROM `users` WHERE `username` = '" . $username . "'");
+		$user = $db->selectQuery("SELECT * FROM `users` WHERE `username` = '" . $username . "'");
 
-		if($user->password === md5($password)) {
+		if(sizeof($user) > 1) {
+			die('Something really went wrong.');
+		}
+
+		if(sizeof($user) < 1) {
+			die('Something really went wrong.');
+		}
+
+		if($user[0]->password === md5($password)) {
 			$_SESSION['loggedIn'] = true;
 			$_SESSION['username'] = $this->username;
-			header("Location: http://vpn.mmnx.de/index.php?action=profile");
+			$this->loggedIn = true;
+			header("Location: http://atoffice");
 		} else {
 			return false;
 		}
 
 	}
 
+	public function loginByUsername($username) {
+		global $db;
+
+		$user = $db->selectQuery("SELECT * FROM `users` WHERE `username` = '" . $username . "'");
+
+		if(sizeof($user) > 1) {
+			die('Something really went wrong.');
+		}
+
+		if(sizeof($user) < 1) {
+			die('Something really went wrong.');
+		}
+
+		$_SESSION['loggedIn'] = true;
+		$this->userId = $user[0]->id;
+		$this->loggedIn = true;
+	}
+
 	public function logout() {
 		$this->username = "";
 		$this->password = "";
 		$this->email = "";
 
 		$_SESSION['loggedIn'] = false;
+		$this->loggedIn	= false;
 		unset($_SESSION['username']);
 		session_destroy();
-		header("Location: http://vpn.mmnx.de/index.php");
+		header("Location: http://atoffice");
 	}
 
 	public function getClients() {

+ 3 - 1
index.php

@@ -10,9 +10,11 @@ include('includes/view.inc.php');
 include('includes/functions.inc.php');
 include('includes/label.inc.php');
 include('includes/document.inc.php');
+include('includes/imap.inc.php');
+include('includes/mailboxfolder.inc.php');
 
 $db = new Database($CONFIG['dbHost'], $CONFIG['dbUser'], $CONFIG['dbPassword'], $CONFIG['dbDatabase']);
-//$user = new User($_SESSION['username']);
+$user = new User(isset($_SESSION['username']) ? $_SESSION['username'] : NULL);
 
 // $_GET und $_POST zusammenfasen
 $request = $_REQUEST;

+ 40 - 0
styles/signin.css

@@ -0,0 +1,40 @@
+body {
+  padding-top: 40px;
+  padding-bottom: 40px;
+  background-color: #eee;
+}
+
+.form-signin {
+  max-width: 330px;
+  padding: 15px;
+  margin: 0 auto;
+}
+.form-signin .form-signin-heading,
+.form-signin .checkbox {
+  margin-bottom: 10px;
+}
+.form-signin .checkbox {
+  font-weight: normal;
+}
+.form-signin .form-control {
+  position: relative;
+  height: auto;
+  -webkit-box-sizing: border-box;
+     -moz-box-sizing: border-box;
+          box-sizing: border-box;
+  padding: 10px;
+  font-size: 16px;
+}
+.form-signin .form-control:focus {
+  z-index: 2;
+}
+.form-signin input[type="email"] {
+  margin-bottom: -1px;
+  border-bottom-right-radius: 0;
+  border-bottom-left-radius: 0;
+}
+.form-signin input[type="password"] {
+  margin-bottom: 10px;
+  border-top-left-radius: 0;
+  border-top-right-radius: 0;
+}

+ 3 - 1
templates/documentlist.php

@@ -1,6 +1,6 @@
 <?php
 // TODO: Make this MVC-style
-$documents = getDocumentsByLabelId(2); // TODO: get documents via labelID
+$documents = getDocumentsByLabelId($this->_['labelId']);
 
 //print_r($documents);
 ?>
@@ -10,6 +10,7 @@ $documents = getDocumentsByLabelId(2); // TODO: get documents via labelID
         <tr>
             <th>Name</th>
             <th>Vorlage</th>
+            <th>Typ</th>
             <th>Erstellt am</th>
             <th>Letzte Änderung</th>
         </tr>
@@ -19,6 +20,7 @@ foreach($documents as $document) {
     echo '<tr>';
     echo '<td>' . $document->getFileName() . '</td>';
     echo '<td>' . $document->getDraft() . '</td>';
+    echo '<td>' . $document->getType() . '</td>';
     echo '<td>' . $document->getCreated() . '</td>';
     echo '<td>' . $document->getLastChange() . '</td>';
     echo '</tr>';

+ 1 - 0
templates/header.php

@@ -44,6 +44,7 @@
 			<li><a href="">Einstellungen</a></li>
 			<li><a href="">Profil</a></li>
 			<li><a href="">Hilfe</a></li>
+			<li><a href="?action=logout">Logout</a></li>
 		</ul>
 		<form class="navbar-form navbar-right">
 			<input type="text" class="form-control" placeholder="Search...">

+ 51 - 19
templates/login.php

@@ -1,19 +1,51 @@
-<div class="row">
-	<div class="col-md-12">
-		<div class="center-block text-center signinbox">
-			<form action="?action=login" method="POST" class="form-signin">
-				<h2 class="form-signin-heading">Sign in</h2>
-				<label for="inputUsername" class="sr-only">Username</label>
-				<input type="text" id="inputUsername" name="username" class="form-control" placeholder="Username" required autofocus>
-				<label for="inputPassword" class="sr-only">Password</label>
-				<input type="password" id="inputPassword" name="password" class="form-control" placeholder="Password" required>
-				<!--<div class="checkbox">
-					<label>
-						<input type="checkbox" value="remember-me"> Remember me
-					</label>
-				</div>-->
-				<button class="btn btn-lg btn-primary btn-block" type="submit">Sign in</button>
-			</form>
-		</div>
-	</div>
-</div>
+
+<!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="">
+    <meta name="author" content="">
+    <link rel="icon" href="../../favicon.ico">
+
+    <title>Signin Template for Bootstrap</title>
+
+    <!-- Bootstrap core CSS -->
+    <link href="styles/bootstrap.min.css" rel="stylesheet">
+
+    <!-- Custom styles for this template -->
+    <link href="styles/signin.css" rel="stylesheet">
+
+    <!-- HTML5 shim and Respond.js for IE8 support of HTML5 elements and media queries -->
+    <!--[if lt IE 9]>
+      <script src="https://oss.maxcdn.com/html5shiv/3.7.2/html5shiv.min.js"></script>
+      <script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
+    <![endif]-->
+  </head>
+
+  <body>
+
+    <div class="container">
+
+      <form action="?action=login" method="POST" class="form-signin">
+        <h2 class="form-signin-heading">Bitte einloggen</h2>
+        <label for="inputUsername" class="sr-only">Username</label>
+        <input type="text" name="username" id="inputUsername" class="form-control" placeholder="Username" required autofocus>
+        <label for="inputPassword" class="sr-only">Password</label>
+        <input type="password" name="password" id="inputPassword" class="form-control" placeholder="Password" required>
+        <!--<div class="checkbox">
+          <label>
+            <input type="checkbox" value="remember-me"> Remember me
+          </label>
+		</div>-->
+        <button class="btn btn-lg btn-primary btn-block" type="submit">Login</button>
+      </form>
+
+    </div> <!-- /container -->
+
+
+    <!-- IE10 viewport hack for Surface/desktop Windows 8 bug -->
+    <!--<script src="../../assets/js/ie10-viewport-bug-workaround.js"></script>-->
+  </body>
+</html>