Switch the 'modify' code of admin/field.php to use templates
[bugdar.git] / attachment.php
index 76fff065bca66ff9c00109a7c1df5bce7b80ed39..89a6240a7d4fc36b570ec802bcc402a3902d7eac 100755 (executable)
@@ -1,13 +1,22 @@
 <?php
 /*=====================================================================*\
-|| ################################################################### ||
-|| # Bugdar [#]version[#]
-|| # --------------------------------------------------------------- # ||
-|| # Copyright ©2002-[#]year[#] by Iris Studios, Inc. All Rights Reserved. # ||
-|| # This file may not be reproduced in any way without permission.  # ||
-|| # --------------------------------------------------------------- # ||
-|| # User License Agreement at http://www.iris-studios.com/license/  # ||
-|| ################################################################### ||
+|| ###################################################################
+|| # Bugdar
+|| # Copyright (c)2004-2009 Blue Static
+|| #
+|| # This program is free software; you can redistribute it and/or modify
+|| # it under the terms of the GNU General Public License as published by
+|| # the Free Software Foundation; version 2 of the License.
+|| #
+|| # This program is distributed in the hope that it will be useful, but
+|| # WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+|| # or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
+|| # more details.
+|| #
+|| # You should have received a copy of the GNU General Public License along
+|| # with this program; if not, write to the Free Software Foundation, Inc.,
+|| # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
+|| ###################################################################
 \*=====================================================================*/
 
 $fetchtemplates = array(
@@ -15,196 +24,236 @@ $fetchtemplates = array(
        'editattach'
 );
 
-define('SVN', '$Id$');
 
 $focus['showreport'] = 'focus';
 
 require_once('./global.php');
+require_once('./includes/class_notification.php');
+require_once('./includes/api_attachment.php');
+require_once('./includes/api_comment.php');
 
-if (isset($bugsys->in['attachmentid']))
+if (isset($input->in['attachmentid']))
 {
-       $attachment = $db->query_first("SELECT * FROM " . TABLE_PREFIX . "attachment WHERE attachmentid = " . intval($bugsys->in['attachmentid']));
+       $attachment = $db->queryFirst("SELECT * FROM " . TABLE_PREFIX . "attachment WHERE attachmentid = " . $input->inputClean('attachmentid', TYPE_UINT));
        if (!$attachment)
        {
-               $message->error($lang->getlex('error_invalid_id'));
+               $message->error(L_INVALID_ID);
        }
 }
 
-$bug = $db->query_first("SELECT * FROM " . TABLE_PREFIX . "bug WHERE bugid = " . (($attachment['attachmentid']) ? $attachment['bugid'] : intval($bugsys->in['bugid'])));
+$bug = $db->queryFirst("SELECT * FROM " . TABLE_PREFIX . "bug WHERE bugid = " . (($attachment['attachmentid']) ? $attachment['bugid'] : $input->inputClean('bugid', TYPE_UINT)));
 if (!$bug)
 {
-       $message->error($lang->getlex('error_invalid_id'));
+       $message->error(L_INVALID_ID);
+}
+
+if (!check_bug_permissions($bug))
+{
+       $message->errorPermission();
 }
 
-// setup logging
-require_once('./includes/class_history.php');
-$log = new History();
-$log->bugid = $bug['bugid'];
+require_once('./includes/class_logging.php');
+
+$notif = new NotificationCenter();
+$notif->setBugData($bug);
 
 // ###################################################################
 
 if ($_POST['do'] == 'insert')
 {
-       if (!can_perform('canputattach', $bug['productid']))
+       $attachapi = new AttachmentAPI();
+       $attachapi->set('bugid',        $input->in['bugid']);
+       
+       if (!can_perform('canputattach', $bug['product']))
        {
-               $message->error_permission();
+               $message->errorPermission();
        }
        
+       // max packet size
+       $var = $db->queryFirst("SHOW VARIABLES LIKE 'max_allowed_packet'");
+       BSApp::debug("max_allowed_packet = $var[Value]");
+       
        // create alias
-       $FILE =$_FILES['attachment'];
+       $FILE = &$_FILES['attachment'];
        
        // PHP errors
        switch ($FILE['error'])
        {
                case 0: break;
-               case 1: $message->error($lang->string('PHP said the file you uploaded was too big.')); break;
-               case 2: $message->error($lang->string('The file exceeds the allowed upload size.')); break;
-               case 3: $message->error($lang->string('The file was only partially uploaded.')); break;
-               case 4: $message->error($lang->string('The file was not uploaded at all.')); break;
-               case 6: $message->error($lang->string('PHP could not find the /tmp directory.')); break;
+               case 1: $message->addError(T('PHP said the file you uploaded was too big.')); break;
+               case 2: $message->addError(T('The file exceeds the allowed upload size.')); break;
+               case 3: $message->addError(T('The file was only partially uploaded.')); break;
+               case 4: $message->addError(T('The file was not uploaded at all.')); break;
+               case 6: $message->addError(T('PHP could not find the /tmp directory.')); break;
        }
        
        // did it upload?
        if (!is_uploaded_file($FILE['tmp_name']))
        {
-               $message->error($lang->string('The file you specified did not upload.'));
+               $message->addError(T('The file you specified did not upload.'));
        }
        
-       // #*# put some MIME-type validation here
+       // TODO - put some MIME-type validation here
        
-       if (!$bugsys->in['description'])
+       if (filesize($FILE['tmp_name']) > $var['Value'])
        {
-               $message->error($lang->string('You need to specify a file description.'));
+               $message->addError(T('The file you specified exceeds MySQL\'s maximum allowed packet.'));
        }
        
-       $filedata = $bugsys->escape(file_get_contents($FILE['tmp_name']), true, true);
-       $time = TIMENOW;
-       
+       $attachapi->set('attachment',   file_get_contents($FILE['tmp_name']));
+       $attachapi->set('filename',             $FILE['name']);
+       $attachapi->set('mimetype',             $FILE['type']);
+       $attachapi->set('filesize',             $FILE['size']);
+       $attachapi->set('description',  $input->in['description']);
+       $attachapi->set('userid',               bugdar::$userinfo['userid']);
+               
        // insert an attachment
-       $db->query("
-               INSERT INTO attachment
-                       (bugid, filename, mimetype, filesize,
-                       attachment, description, dateline, userid)
-               VALUES
-                       ($bug[bugid], '" . $bugsys->escape($FILE['name']) . "',
-                       '" . $bugsys->escape($FILE['type']) . "', " . intval($FILE['size']) . ",
-                       '$filedata', '" . $bugsys->in['description'] . "', $time,
-                       " . $bugsys->userinfo['userid'] . "
-               )"
-       );
-       
-       // mark obsoletes
-       $obsoletes = $_POST['obsoletes'];
-       if (count($obsoletes) > 0)
+       if (!$message->hasErrors())
        {
-               array_walk($obsoletes, 'intval');
-               $db->query("UPDATE " . TABLE_PREFIX . "attachment SET obsolete = 1 WHERE attachmentid IN (" . implode(',', $obsoletes) . ") AND !obsolete AND bugid = $bug[bugid]");
+               $attachapi->insert();
                
-               foreach ($obsoletes AS $attachmentid)
+               $obsoletes = $input->inputClean('obsoletes', TYPE_UINT);
+               
+               $notif->sendNewAttachmentNotice($attachapi->values, $obsoletes, $attachapi->insertid);
+               
+               // mark obsoletes
+               if (is_array($obsoletes) AND sizeof($obsoletes) > 0)
                {
-                       $log->attachmentid = $attachmentid;
-                       $log->log($log->diff('obsolete', 0, 1));
+                       $db->query("UPDATE " . TABLE_PREFIX . "attachment SET obsolete = 1 WHERE attachmentid IN (" . implode(',', $obsoletes) . ") AND !obsolete AND bugid = $bug[bugid]");
+                       
+                       foreach ($obsoletes as $attachmentid)
+                       {
+                               $log = new Logging;
+                               $log->setBugId($bug['bugid']);
+                               $log->setAttachmentId($attachmentid);
+                               $log->addData(true, array('obsolete' => 0), array('obsolete'), false, 'attachment');
+                               $log->addData(false, array('obsolete' => 1), array('obsolete'), false, 'attachment'); 
+                               $log->updateHistory();
+                       }
                }
-       }
-       
-       // handle comment stuff
-       if (can_perform('canpostcomments', $bug['productid']) AND trim($bugsys->in['comment']))
-       {
-               $bugsys->in['comment_parsed'] = $bugsys->in['comment'];
                
-               if (!$bugsys->options['allowhtml'])
+               // handle comment stuff
+               if (can_perform('canpostcomments', $bug['product']) AND trim($input->in['comment']))
                {
-                       $bugsys->in['comment_parsed'] = $bugsys->sanitize($bugsys->in['comment_parsed']);
+                       $comment = new CommentAPI();
+                       $comment->set('bugid',          $input->in['bugid']);
+                       $comment->set('userid',         bugdar::$userinfo['userid']);
+                       $comment->set('comment',        $input->in['comment']);
+                       $comment->set('dateline',       $attachapi->values['dateline']);
+                       $comment->insert();
+                       
+                       $notif->sendNewCommentNotice($comment->values);
                }
                
-               $db->query("
-                       INSERT INTO " . TABLE_PREFIX . "comment
-                               (bugid, userid, dateline, comment, comment_parsed)
-                       VALUES
-                               ($bug[bugid], " . $bugsys->userinfo['userid'] . ",
-                               $time, '" . $bugsys->in['comment'] . "',
-                               '" . nl2br($bugsys->in['comment_parsed']) . "'
-                       )"
-               );
+               // update the last post data
+               $db->query("UPDATE " . TABLE_PREFIX . "bug SET lastposttime = " . $attachapi->values['dateline'] . ", hiddenlastposttime = " . $attachapi->values['dateline'] . ", lastpostby = " . bugdar::$userinfo['userid'] . ", hiddenlastpostby = " . bugdar::$userinfo['userid'] . " WHERE bugid = $bug[bugid]");
+               
+               $notif->finalize();
+               
+               $message->redirect(T('The attachment has been added to the bug.'), "showreport.php?bugid=$bug[bugid]");
+       }
+       else
+       {
+               $show['errors'] = true;
+               $_REQUEST['do'] = 'add';
        }
-       
-       // update the last post data
-       $db->query("UPDATE " . TABLE_PREFIX . "bug SET lastposttime = $time, lastpostby = " . $bugsys->userinfo['userid'] . " WHERE bugid = $bug[bugid]");
-       
-       $message->redirect($lang->string('The attachment has been added to the bug.'), "showreport.php?bugid=$bug[bugid]");
 }
 
 // ###################################################################
 
 if ($_REQUEST['do'] == 'add')
 {
-       if (!can_perform('canputattach', $bug['productid']))
+       if (!can_perform('canputattach', $bug['product']))
        {
-               $message->error_permission();
+               $message->errorPermission();
        }
        
-       $MAXFILESIZE = $funct->fetch_max_attachment_size();
+       $MAXFILESIZE = BSFunctions::fetch_max_php_file_size();
        
-       $show['addcomment'] = ((can_perform('canpostcomments', $bug['productid'])) ? true : false);
+       $show['addcomment'] = ((can_perform('canpostcomments', $bug['product'])) ? true : false);
        $show['obsoletes'] = false;
        
        $obsoletes_fetch = $db->query("SELECT * FROM " . TABLE_PREFIX . "attachment WHERE bugid = $bug[bugid] AND !obsolete");
        $obsoletes = '';
-       while ($obsolete = $db->fetch_array($obsoletes_fetch))
+       foreach ($obsoletes_fetch as $obsolete)
        {
                $show['obsoletes'] = true;
-               $obsoletes .= "<div><input name=\"obsoletes[]\" type=\"checkbox\" value=\"$obsolete[attachmentid]\" /> $obsolete[filename] [$obsolete[description]]</div>\n";
+               $obsoletes .= "<div><input name=\"obsoletes[]\" type=\"checkbox\" value=\"$obsolete[attachmentid]\"" . (is_array($input->in['obsoletes']) AND in_array($obsolete['attachmentid'], $input->in['obsoletes']) ? ' checked="checked"' : '') . " /> $obsolete[filename]" . ($obsolete['description'] ? " [$obsolete[description]]" : '') . "</div>\n";
        }
        
-       eval('$template->flush("' . $template->fetch('newattach') . '");');
+       $tpl = new BSTemplate('newattach');
+       $tpl->vars = array(
+               'bug'                   => $bug,
+               'message'               => $message,
+               'MAXFILESIZE'   => $MAXFILESIZE,
+               'obsoletes'             => $obsoletes,
+               'input'                 => $input
+       );
+       $tpl->evaluate()->flush();
 }
 
 // ###################################################################
 
 if ($_POST['do'] == 'update')
 {
-       if (!(can_perform('caneditattach', $bug['productid']) OR ($attachment['userid'] == $bugsys->userinfo['userid'] AND can_perform('canputattach', $bug['productid']))))
+       if (!(can_perform('caneditattach', $bug['product']) OR ($attachment['userid'] == bugdar::$userinfo['userid'] AND can_perform('canputattach', $bug['product']))))
        {
-               $message->error_permission();
+               $message->errorPermission();
        }
        
-       $db->query("
-               UPDATE " . TABLE_PREFIX . "attachment
-               SET description = '" . $bugsys->in['description'] . "',
-                       obsolete = " . intval($bugsys->in['obsolete']) . "
-               WHERE attachmentid = " . intval($bugsys->in['attachmentid'])
-       );
-       
-       $hist[1] = $db->query_first("SELECT * FROM " . TABLE_PREFIX . "attachment WHERE attachmentid = $attachment[attachmentid]");
-       
-       $diff[0] = array_diff_assoc($attachment, $hist[1]);
-       $diff[1] = array_diff_assoc($hist[1], $attachment);
+       $attachapi = new AttachmentAPI();
+       $attachapi->set('attachmentid', $input->in['attachmentid']);
        
-       $log->attachmentid = $attachment['attachmentid'];
-       $log->log($log->diff('description', $diff[0]['description'], $diff[1]['description']));
-       $log->log($log->diff('obsolete', $diff[0]['obsolete'], $diff[1]['obsolete']));
-       
-       $message->redirect($lang->string('The attachment was successfully modified.'), "showreport.php?bugid=$bug[bugid]");
+       if ($input->in['__delete__'] != '')
+       {
+               if (!(can_perform('caneditattach', $bug['product']) AND can_perform('candeletedata', $bug['productid'])))
+               {
+                       $message->errorPermission();
+               }
+               
+               $attachapi->remove();
+               
+               $message->redirect(T('The attachment was successfully deleted.'), "showreport.php?bugid=$bug[bugid]");
+       }
+       else
+       {
+               $log = new Logging();
+               $log->setBugId($bug['bugid']);
+               $log->setAttachmentId($input->in['attachmentid']);
+               
+               $attachapi->fetch();
+               
+               $log->addData(true, $attachapi->record, array('attachment'), true, 'attachment');
+               
+               $attachapi->set('description',  $input->in['description']);
+               $attachapi->set('obsolete',             $input->in['obsolete']);
+               $attachapi->update();
+               
+               $log->addData(false, $attachapi->values, array('attachment'), true, 'attachment');
+               
+               $log->updateHistory();
+               
+               $message->redirect(T('The attachment was successfully modified.'), "showreport.php?bugid=$bug[bugid]");
+       }
 }
 
 // ###################################################################
 
 if ($_REQUEST['do'] == 'edit')
 {
-       if (!(can_perform('caneditattach', $bug['productid']) OR ($attachment['userid'] == $bugsys->userinfo['userid'] AND can_perform('canputattach', $bug['productid']))))
+       if (!(can_perform('caneditattach', $bug['product']) OR ($attachment['userid'] == bugdar::$userinfo['userid'] AND can_perform('canputattach', $bug['product']))))
        {
-               $message->error_permission();
+               $message->errorPermission();
        }
        
-       $show['delete'] = ((can_perform('caneditattach', $bug['productid'])) ? true : false);
+       $show['delete'] = (can_perform('caneditattach', $bug['product']) AND can_perform('candeletedata', $bug['productid']));
        
-       eval('$template->flush("' . $template->fetch('editattach') . '");');
+       $tpl = new BSTemplate('editattach');
+       $tpl->vars = array(
+               'attachment'    => $attachment,
+               'bug'                   => $bug
+       );
+       $tpl->evaluate()->flush();
 }
 
-/*=====================================================================*\
-|| ###################################################################
-|| # $HeadURL$
-|| # $Id$
-|| ###################################################################
-\*=====================================================================*/
 ?>
\ No newline at end of file