fix bug #10: error when nothing submitted
[phorkie.git] / src / phorkie / Repository / Post.php
index fa356ee3cb0fcfa5ac266fc2898fa69e52f8d462..fe2372f67a1f80a50c60fa2ae12484966024c29d 100644 (file)
@@ -20,15 +20,24 @@ class Repository_Post
         if (!isset($postData['files'])) {
             return false;
         }
+        if (!$this->hasContent($postData)) {
+            return false;
+        }
 
         if (!$this->repo) {
             $this->repo = $this->createRepo();
         }
 
         $vc = $this->repo->getVc();
-        $this->repo->setDescription($postData['description']);
+
 
         $bChanged = false;
+        $bCommit  = false;
+        if ($postData['description'] != $this->repo->getDescription()) {
+            $this->repo->setDescription($postData['description']);
+            $bChanged = true;
+        }
+
         foreach ($postData['files'] as $num => $arFile) {
             $bUpload = false;
             if ($_FILES['files']['error'][$num]['upload'] == 0) {
@@ -42,6 +51,11 @@ class Repository_Post
             $orignalName = Tools::sanitizeFilename($arFile['original_name']);
             $name        = Tools::sanitizeFilename($arFile['name']);
 
+            if ($arFile['type'] == '_auto_') {
+                //FIXME: upload
+                $arFile['type'] = $this->getType($arFile['content']);
+            }
+
             if ($name == '') {
                 if ($bUpload) {
                     $name = Tools::sanitizeFilename($_FILES['files']['name'][$num]['upload']);
@@ -74,7 +88,7 @@ class Repository_Post
                         ->addArgument($orignalName)
                         ->addArgument($name)
                         ->execute();
-                    $bChanged = true;
+                    $bCommit = true;
                 } else {
                     $name = $orignalName;
                 }
@@ -85,7 +99,7 @@ class Repository_Post
                 $command = $vc->getCommand('rm')
                     ->addArgument($file->getFilename())
                     ->execute();
-                $bChanged = true;
+                $bCommit = true;
             } else if ($bUpload) {
                 move_uploaded_file(
                     $_FILES['files']['tmp_name'][$num]['upload'], $file->getFullPath()
@@ -93,27 +107,52 @@ class Repository_Post
                 $command = $vc->getCommand('add')
                     ->addArgument($file->getFilename())
                     ->execute();
-                $bChanged = true;
+                $bCommit = true;
             } else if ($bNew || (isset($arFile['content']) && $file->getContent() != $arFile['content'])) {
                 file_put_contents($file->getFullPath(), $arFile['content']);
                 $command = $vc->getCommand('add')
                     ->addArgument($file->getFilename())
                     ->execute();
-                $bChanged = true;
+                $bCommit = true;
             }
         }
 
-        if ($bChanged) {
+        if ($bCommit) {
             $vc->getCommand('commit')
                 ->setOption('message', '')
                 ->setOption('allow-empty-message')
                 ->setOption('author', 'Anonymous <anonymous@phorkie>')
                 ->execute();
+            $bChanged = true;
+        }
+
+        if ($bChanged) {
+            //FIXME: index changed files only
+            //also handle file deletions
+            $db = new Database();
+            if ($bNew) {
+                $db->getIndexer()->addRepo($this->repo);
+            } else {
+                $db->getIndexer()->updateRepo($this->repo);
+            }
         }
 
         return true;
     }
 
+    protected function hasContent($postData)
+    {
+        foreach ($postData['files'] as $num => $arFile) {
+            if ($_FILES['files']['error'][$num]['upload'] == 0) {
+                return true;
+            }
+            if ($arFile['content'] != '') {
+                return true;
+            }
+        }
+        return false;
+    }
+
     public function createRepo()
     {
         $rs = new Repositories();
@@ -145,6 +184,28 @@ class Repository_Post
 
         return $prefix . $num;
     }
+
+    protected function getType($content)
+    {
+        $tmp = tempnam(sys_get_temp_dir(), 'phorkie-autodetect-');
+        file_put_contents($tmp, $content);
+        $type = \MIME_Type_PlainDetect::autoDetect($tmp);
+        unlink($tmp);
+
+        return $this->findExtForType($type);
+    }
+
+    protected function findExtForType($type)
+    {
+        $ext = 'text/plain';
+        foreach ($GLOBALS['phorkie']['languages'] as $lext => $arLang) {
+            if ($arLang['mime'] == $type) {
+                $ext = $lext;
+                break;
+            }
+        }
+        return $ext;
+    }
 }
 
 ?>