Просмотр исходного кода

cron.php inserting new files into DB now

Moritz Schmidt 10 лет назад
Родитель
Сommit
560413fb91
5 измененных файлов с 103 добавлено и 12 удалено
  1. 11 0
      cron.php
  2. 1 1
      includes/config.inc.php
  3. 20 7
      includes/database.inc.php
  4. 71 3
      includes/functions.inc.php
  5. 0 1
      index.php

+ 11 - 0
cron.php

@@ -5,7 +5,18 @@
 include('includes/config.inc.php');
 include('includes/database.inc.php');
 include('includes/functions.inc.php');
+include('includes/document.inc.php');
+include('includes/label.inc.php');
 
+$db = new Database($CONFIG['dbHost'], $CONFIG['dbUser'], $CONFIG['dbPassword'], $CONFIG['dbDatabase']);
 
+$dirs = scandir($CONFIG['documentPath']);
+foreach($dirs as $dir) {
+    if($dir === '.' || $dir === '..') {
+        continue;
+    }
+
+    searchNewFiles($dir);
+}
 
 ?>

+ 1 - 1
includes/config.inc.php

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

+ 20 - 7
includes/database.inc.php

@@ -12,15 +12,20 @@ class Database {
 		$this->handle->Close();
 	}
 
-	public function query($query) {
+	public function SelectQuery($query) {
 		$stmt = $this->handle->query($query);
-		if($stmt->num_rows == 0) {
-			$result = NULL;
-		} else {
-			$result = array();
-			while($res = $stmt->fetch_object()) {
-				$result[] = $res;
+
+		if($stmt) {
+			if($stmt->num_rows == 0) {
+				$result = NULL;
+			} else {
+				$result = array();
+				while($res = $stmt->fetch_object()) {
+					$result[] = $res;
+				}
 			}
+		} else {
+			return null;
 		}
 
 		/*
@@ -28,8 +33,16 @@ class Database {
 			$result = $stmt->fetch_object();
 		}
 		*/
+
+		$stmt->close();
 		return $result;
 	}
+
+	public function insertQuery($query) {
+		$stmt = $this->handle->prepare($query);
+		$stmt->execute();
+		$stmt->close();
+	}
 }
 
 ?>

+ 71 - 3
includes/functions.inc.php

@@ -2,11 +2,17 @@
 
 // Label functions
 
+function pa($array) {
+	echo '<pre>';
+	print_r($array);
+	echo '</pre>';
+}
+
 function getLabels() {
 	global $db;
 
 	$return = array();
-	$labels = $db->query("SELECT * FROM `labels`;");
+	$labels = $db->selectQuery("SELECT * FROM `labels`;");
 
 	foreach($labels as $label) {
 		$return[] = new Label($label->id, $label->name);
@@ -19,7 +25,20 @@ function getLabelById($labelId) {
 	global $db;
 
 	$return = array();
-	$labels = $db->query("SELECT * FROM `labels` WHERE `id` = " . $labelId . ";");
+	$labels = $db->selectQuery("SELECT * FROM `labels` WHERE `id` = " . $labelId . ";");
+
+	foreach($labels as $label) {
+		$return[] = new Label($label->id, $label->name);
+	}
+
+	return $return;
+}
+
+function getLabelByPath($path) {
+	global $db;
+
+	$return = array();
+	$labels = $db->selectQuery("SELECT * FROM `labels` WHERE `path` = '" . $path . "';");
 
 	foreach($labels as $label) {
 		$return[] = new Label($label->id, $label->name);
@@ -32,7 +51,20 @@ function getDocumentsByLabelId($labelId) {
 	global $db;
 
 	$return = array();
-	$documents = $db->query("SELECT * FROM `documents` WHERE `label_id` = " . $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 $return;
+}
+
+function getDocumentsByPath($path) {
+	global $db;
+
+	$return = array();
+	$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);
@@ -41,4 +73,40 @@ function getDocumentsByLabelId($labelId) {
 	return $return;
 }
 
+function addDocument($type, $fileName, $path, $labelId, $draft, $created, $lastChange) {
+	global $db;
+	$query = "INSERT INTO `documents`(`type`, `filename`, `path`, `label_id`, `draft`, `created`, `last_change`) VALUES ('" . $type . "', '" . $fileName . "', '" . $path . "', " . $labelId . ", '" . $draft . "', " . $created . ", " . $lastChange . ");";
+	$db->insertQuery($query);
+}
+
+function searchNewFiles($scanDir) {
+	global $db;
+	global $CONFIG;
+
+	$oldDocuments = getDocumentsByPath($scanDir);
+	$files = scandir($CONFIG['documentPath'] . $scanDir);
+
+	foreach($files as $file) {
+		$existed = false;
+		if($file === '.' || $file === '..') {
+			continue;
+		}
+
+		foreach($oldDocuments as $oldDocument) {
+			if($oldDocument->getFileName() === $file) { // TODO: Check update-date, maybe removed files
+				$existed = true;
+				break;
+			}
+		}
+
+		if($existed) {
+			continue;
+		}
+
+		addDocument('file', $file, '/', getLabelByPath($scanDir)[0]->getId(), '', 'NOW()', 'NOW()'); // TODO: get dates by filesystem
+
+	}
+
+}
+
 ?>

+ 0 - 1
index.php

@@ -7,7 +7,6 @@ include('includes/user.inc.php');
 include('includes/controller.inc.php');
 include('includes/model.inc.php');
 include('includes/view.inc.php');
-include('includes/openvpn.inc.php');
 include('includes/functions.inc.php');
 include('includes/label.inc.php');
 include('includes/document.inc.php');