Moritz Schmidt преди 10 години
родител
ревизия
b65633c7ba
променени са 3 файла, в които са добавени 119 реда и са изтрити 13 реда
  1. 6 10
      cron.php
  2. 26 2
      includes/document.inc.php
  3. 87 1
      includes/functions.inc.php

+ 6 - 10
cron.php

@@ -12,18 +12,14 @@ include('includes/mailboxfolder.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;
-    }
+$files = recursiveScanDir($CONFIG['documentPath']);
+$allDocuments = getObjectsAsArray(Document::getAllDocuments(), array("id", "fileName", "path", "labelId", "draft", "created", "lastChange", "type", "mailUid"));
 
-    if(is_dir($CONFIG['documentPath'] . $dir)) {
-        searchNewFiles($dir);
-    }
-}
 
-searchMails();
+handleFile($files, $allDocuments);
+
+//searchMails();
+
 
 ?>

+ 26 - 2
includes/document.inc.php

@@ -35,6 +35,10 @@ class Document {
         return $this->path;
     }
 
+    public function getLabelId() {
+        return $this->labelId;
+    }
+
     public function getDraft() {
         return $this->draft;
     }
@@ -55,6 +59,26 @@ class Document {
         return $this->mailUid;
     }
 
+    /**
+     * Get all Documents
+     *
+     * @return Array(Document)  Array with all Documents
+     *
+     */
+
+     public static function getAllDocuments() {
+         global $db;
+
+         $return = array();
+         $documents = $db->selectQuery("SELECT * FROM `documents`;");
+
+         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;
+     }
+
     /**
      * Get Documents by Label ID
      *
@@ -88,7 +112,7 @@ class Document {
      *
      */
 
-    public static function getDocumentsByPath($path) {
+    public static function getDocumentsByLabelPath($path) {
         global $db;
 
     	$return = array();
@@ -147,7 +171,7 @@ class Document {
     public static function addDocument($type, $fileName, $path, $labelId, $draft, $created, $lastChange, $mailUid = 0, $mailAcc = -1) {
         global $db;
 
-        $query = "INSERT INTO `documents`(`type`, `filename`, `path`, `label_id`, `draft`, `created`, `last_change`, `mail_uid`, `mail_acc`) VALUES ('" . $type . "', '" . $fileName . "', '" . $path . "', " . $labelId . ", '" . $draft . "', " . $created . ", " . $lastChange . ", " . $mailUid . ", " . $mailAcc . ");";
+        $query = "INSERT INTO `documents`(`type`, `filename`, `path`, `label_id`, `draft`, `created`, `last_change`, `mail_uid`, `mail_acc`) VALUES ('" . $type . "', '" . $fileName . "', '" . $path . "', " . $labelId . ", '" . $draft . "', '" . $created . "', '" . $lastChange . "', " . $mailUid . ", " . $mailAcc . ");";
         $db->insertQuery($query);
     }
 

+ 87 - 1
includes/functions.inc.php

@@ -34,7 +34,7 @@ function searchNewFiles($scanDir) {
 	global $db;
 	global $CONFIG;
 
-	$oldDocuments = Document::getDocumentsByPath($scanDir);
+	$oldDocuments = Document::getDocumentsByLabelPath($scanDir);
 	$files = scandir($CONFIG['documentPath'] . $scanDir);
 
 	foreach($files as $file) {
@@ -60,6 +60,49 @@ function searchNewFiles($scanDir) {
 
 }
 
+function handleFile($filename, &$allDocuments) {
+	global $db;
+	global $CONFIG;
+
+	if(is_array($filename)) {
+		foreach($filename as $dir) {
+			handleFile($dir, $allDocuments);
+		}
+	} else {
+		$found = false;
+		foreach($allDocuments as $key => $document) {
+			if($document['fileName'] == explode('/', $filename)[sizeof(explode('/', $filename)) - 1]) {
+				$labelPath = Label::getLabelById($document['labelId'])->getPath();
+
+
+				if(str_replace($CONFIG['documentPath'], "", $filename) == $labelPath . "/" . $document['path'] . $document['fileName']) {
+					// found
+					// update stuff
+					unset($allDocuments[$key]); // remove from array
+					$found = true;
+					break;
+				}
+
+				// same filename, path unknown
+			}
+		}
+
+		if(!$found) {
+			$fileTime = date("Y-m-d H:i:s", filemtime($filename));
+
+			$path = explode('/', str_replace($CONFIG['documentPath'], "", $filename));
+			$labelPath = $path[0];
+			unset($path[0]);
+			unset($path[sizeof($path)]);
+			$path = implode('/', $path) . '/';
+
+			Document::addDocument('file', explode('/', $filename)[sizeof(explode('/', $filename)) - 1], $path, Label::getLabelByPath($labelPath)->getId(), '', $fileTime, $fileTime);
+		}
+	}
+
+
+}
+
 function searchMails() {
 	global $user;
 	$mailboxes = Mailbox::getAllMailBoxes();
@@ -132,4 +175,47 @@ function getExcerptFromString($string, $count = 50) {
 	return $string;
 }
 
+function startsWith($haystack, $needle)
+{
+     $length = strlen($needle);
+     return (substr($haystack, 0, $length) === $needle);
+}
+
+function endsWith($haystack, $needle)
+{
+    $length = strlen($needle);
+    if ($length == 0) {
+        return true;
+    }
+
+    return (substr($haystack, -$length) === $needle);
+}
+
+function recursiveScanDir($dir, &$results = array()){
+    $files = scandir($dir);
+
+    foreach($files as $key => $value){
+        $path = realpath($dir . DIRECTORY_SEPARATOR . $value);
+        if(!is_dir($path)) {
+            $results[] = $path;
+        } else if(is_dir($path) && $value != "." && $value != "..") {
+			$results[$path] = array();
+			recursiveScanDir($path, $results[$path]);
+        }
+    }
+
+    return $results;
+}
+
+function in_array_r($needle, &$haystack, $strict = false) {
+    foreach ($haystack as $item) {
+        if (($strict ? $item === $needle : $item == $needle) || (is_array($item) && in_array_r($needle, $item, $strict))) {
+			echo $item;
+            return true;
+        }
+    }
+
+    return false;
+}
+
 ?>