]> git.mxchange.org Git - quix0rs-gnu-social.git/blobdiff - actions/newnotice.php
register checks Profile bio length correctly
[quix0rs-gnu-social.git] / actions / newnotice.php
index 72ccd8c3254ff4197b235c5dec7b7ffad657572d..e254eac4999852389b8623b36db62d10826f5bb3 100644 (file)
@@ -116,6 +116,9 @@ class NewnoticeAction extends Action
     function getUploadedFileType() {
         require_once 'MIME/Type.php';
 
+        $cmd = &PEAR::getStaticProperty('MIME_Type', 'fileCmd');
+        $cmd = common_config('attachments', 'filecommand');
+
         $filetype = MIME_Type::autoDetect($_FILES['attach']['tmp_name']);
         if (in_array($filetype, common_config('attachments', 'supported'))) {
             return $filetype;
@@ -132,7 +135,7 @@ class NewnoticeAction extends Action
 
     function isRespectsQuota($user) {
         $file = new File;
-        $ret = $file->isRespectsQuota($user);
+        $ret = $file->isRespectsQuota($user,$_FILES['attach']['size']);
         if (true === $ret) return true;
         $this->clientError($ret);
     }
@@ -221,16 +224,46 @@ class NewnoticeAction extends Action
             }
         }
 
+        if (isset($mimetype)) {
+            $filename = $this->saveFile($mimetype);
+            if (empty($filename)) {
+                $this->clientError(_('Couldn\'t save file.'));
+            }
+
+            $fileRecord = $this->storeFile($filename, $mimetype);
+
+            $fileurl = common_local_url('attachment',
+                array('attachment' => $fileRecord->id));
+
+            // not sure this is necessary -- Zach
+            $this->maybeAddRedir($fileRecord->id, $fileurl);
+
+            $short_fileurl = common_shorten_url($fileurl);
+            $content_shortened .= ' ' . $short_fileurl;
+
+            if (mb_strlen($content_shortened) > 140) {
+                $this->deleteFile($filename);
+                $this->clientError(_('Max notice size is 140 chars, including attachment URL.'));
+            }
+
+            // Also, not sure this is necessary -- Zach
+            $this->maybeAddRedir($fileRecord->id, $short_fileurl);
+        }
+
         $notice = Notice::saveNew($user->id, $content_shortened, 'web', 1,
                                   ($replyto == 'false') ? null : $replyto);
 
         if (is_string($notice)) {
+            if (isset($filename)) {
+                $this->deleteFile($filename);
+            }
             $this->clientError($notice);
         }
 
         if (isset($mimetype)) {
-            $this->storeFile($notice, $mimetype);
+            $this->attachFile($notice, $fileRecord);
         }
+
         common_broadcast_notice($notice);
 
         if ($this->boolean('ajax')) {
@@ -256,33 +289,87 @@ class NewnoticeAction extends Action
         }
     }
 
-    function storeFile($notice, $mimetype) {
-        $filename = basename($_FILES['attach']['name']);
-        $destination = "file/{$notice->id}-$filename";
-        if (move_uploaded_file($_FILES['attach']['tmp_name'], INSTALLDIR . "/$destination")) {
-            $file = new File;
-            $file->url = common_local_url('file', array('notice' => $notice->id));
-            $file->size = filesize(INSTALLDIR . "/$destination");
-            $file->date = time();
-            $file->mimetype = $mimetype;
-            if ($file_id = $file->insert()) {
-                $file_redir = new File_redirection;
-                $file_redir->url = common_path($destination);
-                $file_redir->file_id = $file_id;
-                $file_redir->insert();
-
-                $f2p = new File_to_post;
-                $f2p->file_id = $file_id; 
-                $f2p->post_id = $notice->id; 
-                $f2p->insert();
-            } else {
-                $this->clientError(_('There was a database error while saving your file. Please try again.'));
-            }
+    function saveFile($mimetype) {
+
+        $cur = common_current_user();
+
+        if (empty($cur)) {
+            $this->serverError(_('Somehow lost the login in saveFile'));
+        }
+
+        $basename = basename($_FILES['attach']['name']);
+
+        $filename = File::filename($cur->getProfile(), $basename, $mimetype);
+
+        $filepath = File::path($filename);
+
+        if (move_uploaded_file($_FILES['attach']['tmp_name'], $filepath)) {
+            return $filename;
         } else {
             $this->clientError(_('File could not be moved to destination directory.'));
         }
     }
 
+    function deleteFile($filename)
+    {
+        $filepath = File::path($filename);
+        @unlink($filepath);
+    }
+
+    function storeFile($filename, $mimetype) {
+
+        $file = new File;
+        $file->filename = $filename;
+
+        $file->url = File::url($filename);
+
+        $filepath = File::path($filename);
+
+        $file->size = filesize($filepath);
+        $file->date = time();
+        $file->mimetype = $mimetype;
+
+        $file_id = $file->insert();
+
+        if (!$file_id) {
+            common_log_db_error($file, "INSERT", __FILE__);
+            $this->clientError(_('There was a database error while saving your file. Please try again.'));
+        }
+
+        return $file;
+    }
+
+    function rememberFile($file, $short)
+    {
+        $this->maybeAddRedir($file->id, $short);
+    }
+
+    function maybeAddRedir($file_id, $url)
+    {
+        $file_redir = File_redirection::staticGet('url', $url);
+
+        if (empty($file_redir)) {
+            $file_redir = new File_redirection;
+            $file_redir->url = $url;
+            $file_redir->file_id = $file_id;
+
+            $result = $file_redir->insert();
+
+            if (!$result) {
+                common_log_db_error($file_redir, "INSERT", __FILE__);
+                $this->clientError(_('There was a database error while saving your file. Please try again.'));
+            }
+        }
+    }
+
+    function attachFile($notice, $filerec)
+    {
+        File_to_post::processNew($filerec->id, $notice->id);
+
+        $this->maybeAddRedir($filerec->id,
+            common_local_url('file', array('notice' => $notice->id)));
+    }
+
     /**
      * Show an Ajax-y error message
      *