]> git.mxchange.org Git - quix0rs-gnu-social.git/blobdiff - actions/newnotice.php
Merge branch '0.8.x' into 0.9.x
[quix0rs-gnu-social.git] / actions / newnotice.php
index 29b748dd1ac509cd2e9ca84cbc340217196653a0..049d7c322982aadfa3cff3f8d51eeb16d78b1e5c 100644 (file)
@@ -91,8 +91,8 @@ class NewnoticeAction extends Action
             // is losts when size is exceeded
             if (empty($_POST) && $_SERVER['CONTENT_LENGTH']) {
                 $this->clientError(sprintf(_('The server was unable to handle ' .
-                    'that much POST data (%s bytes) due to its current configuration.'),
-                    $_SERVER['CONTENT_LENGTH']));
+                                             'that much POST data (%s bytes) due to its current configuration.'),
+                                           $_SERVER['CONTENT_LENGTH']));
             }
             parent::handle($args);
 
@@ -113,12 +113,15 @@ class NewnoticeAction extends Action
         }
     }
 
-    function isSupportedFileType() {
+    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 true;
+            return $filetype;
         }
         $media = MIME_Type::getMedia($filetype);
         if ('application' !== $media) {
@@ -127,41 +130,14 @@ class NewnoticeAction extends Action
             $hint = '';
         }
         $this->clientError(sprintf(
-            _('%s is not a supported filetype on this server.'), $filetype) . $hint);
+                                   _('%s is not a supported filetype on this server.'), $filetype) . $hint);
     }
 
     function isRespectsQuota($user) {
-        if ($_FILES['attach']['size'] > common_config('attachments', 'file_quota')) {
-            $this->clientError(sprintf(_('No file may be larger than %d bytes ' .
-                'and the file you sent was %d bytes. Try to upload a smaller version.'),
-                common_config('attachments', 'file_quota'), $_FILES['attach']['size']));
-        }
-
-        $query = "select sum(size) as total from file join file_to_post on file_to_post.file_id = file.id join notice on file_to_post.post_id = notice.id where profile_id = {$user->id} and file.url like '%/notice/%/file'";
         $file = new File;
-        $file->query($query);
-        $file->fetch();
-        $total = $file->total + $_FILES['attach']['size'];
-        if ($total > common_config('attachments', 'user_quota')) {
-            $this->clientError(sprintf(_('A file this large would exceed your user quota of %d bytes.'), common_config('attachments', 'user_quota')));
-        }
-
-        $query .= ' month(modified) = month(now()) and year(modified) = year(now())';
-        $file2 = new File;
-        $file2->query($query);
-        $file2->fetch();
-        $total2 = $file2->total + $_FILES['attach']['size'];
-        if ($total2 > common_config('attachments', 'monthly_quota')) {
-            $this->clientError(sprintf(_('A file this large would exceed your monthly quota of %d bytes.'), common_config('attachments', 'monthly_quota')));
-        }
-        return true;
-    }
-
-    function isValidFileAttached($user) {
-        return isset($_FILES['attach']['error'])
-            && ($_FILES['attach']['error'] === UPLOAD_ERR_OK)
-            && $this->isSupportedFileType()
-            && $this->isRespectsQuota($user);
+        $ret = $file->isRespectsQuota($user,$_FILES['attach']['size']);
+        if (true === $ret) return true;
+        $this->clientError($ret);
     }
 
     /**
@@ -186,9 +162,10 @@ class NewnoticeAction extends Action
             $this->clientError(_('No content!'));
         } else {
             $content_shortened = common_shorten_links($content);
-            if (mb_strlen($content_shortened) > 140) {
-                $this->clientError(_('That\'s too long. '.
-                                     'Max notice size is 140 chars.'));
+            if (Notice::contentTooLong($content_shortened)) {
+                $this->clientError(sprintf(_('That\'s too long. '.
+                                             'Max notice size is %d chars.'),
+                                           Notice::maxContent()));
             }
         }
 
@@ -212,54 +189,83 @@ class NewnoticeAction extends Action
             $replyto = 'false';
         }
 
-        switch ($_FILES['attach']['error']) {
-            case UPLOAD_ERR_NO_FILE:
-                // no file uploaded
-                // nothing to do
+        if (isset($_FILES['attach']['error'])) {
+            switch ($_FILES['attach']['error']) {
+             case UPLOAD_ERR_NO_FILE:
+                // no file uploaded, nothing to do
                 break;
 
              case UPLOAD_ERR_OK:
-                // file was uploaded alright
-                // lets check if we really support its format
-                // and it doesn't go over quotas
-
-
-                if (!$this->isValidFileAttached($user)) {
+                $mimetype = $this->getUploadedFileType();
+                if (!$this->isRespectsQuota($user)) {
                     die('clientError() should trigger an exception before reaching here.');
                 }
                 break;
 
-            case UPLOAD_ERR_INI_SIZE:
+             case UPLOAD_ERR_INI_SIZE:
                 $this->clientError(_('The uploaded file exceeds the upload_max_filesize directive in php.ini.'));
 
-            case UPLOAD_ERR_FORM_SIZE:
+             case UPLOAD_ERR_FORM_SIZE:
                 $this->clientError(_('The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form.'));
 
-            case UPLOAD_ERR_PARTIAL:
+             case UPLOAD_ERR_PARTIAL:
                 $this->clientError(_('The uploaded file was only partially uploaded.'));
 
-            case  UPLOAD_ERR_NO_TMP_DIR:
+             case  UPLOAD_ERR_NO_TMP_DIR:
                 $this->clientError(_('Missing a temporary folder.'));
 
-            case UPLOAD_ERR_CANT_WRITE:
+             case UPLOAD_ERR_CANT_WRITE:
                 $this->clientError(_('Failed to write file to disk.'));
 
-            case UPLOAD_ERR_EXTENSION:
+             case UPLOAD_ERR_EXTENSION:
                 $this->clientError(_('File upload stopped by extension.'));
 
-            default:
+             default:
                 die('Should never reach here.');
+            }
+        }
+
+        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 (Notice::contentTooLong($content_shortened)) {
+                $this->deleteFile($filename);
+                $this->clientError(sprintf(_('Max notice size is %d chars, including attachment URL.'),
+                                           Notice::maxContent()));
+            }
+
+            // 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);
         }
 
-        $this->storeFile($notice);
-        $this->saveUrls($notice);
+        if (isset($mimetype)) {
+            $this->attachFile($notice, $fileRecord);
+        }
+
         common_broadcast_notice($notice);
 
         if ($this->boolean('ajax')) {
@@ -285,49 +291,85 @@ class NewnoticeAction extends Action
         }
     }
 
-    function storeFile($notice) {
-        if (UPLOAD_ERR_NO_FILE === $_FILES['attach']['error']) return;
-        $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 = $_FILES['attach']['type'];
-            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);
+    }
 
-    /** save all urls in the notice to the db
-     *
-     * follow redirects and save all available file information
-     * (mimetype, date, size, oembed, etc.)
-     *
-     * @param class $notice Notice to pull URLs from
-     *
-     * @return void
-     */
-    function saveUrls($notice, $uploaded = null) {
-        common_replace_urls_callback($notice->content, array($this, 'saveUrl'), $notice->id);
+    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 saveUrl($data) {
-        list($url, $notice_id) = $data;
-        $zzz = File::processNew($url, $notice_id);
+    function attachFile($notice, $filerec)
+    {
+        File_to_post::processNew($filerec->id, $notice->id);
+
+        $this->maybeAddRedir($filerec->id,
+                             common_local_url('file', array('notice' => $notice->id)));
     }
 
     /**
@@ -434,3 +476,4 @@ class NewnoticeAction extends Action
         $nli->show();
     }
 }
+