/**
  * Processes the next item on the work queue. Emits a JSON messages to
  * indicate current progress.
  *
  * Returns:
  * TRUE/NULL if the migration was successful
  */
 function next()
 {
     # Fetch next item -- use the last item so the array indices don't
     # need to be recalculated for every shift() operation.
     $info = array_pop($this->queue);
     # Attach file to the ticket
     if (!($info['data'] = @file_get_contents($info['path']))) {
         # Continue with next file
         return $this->error(sprintf('%s: Cannot read file contents', $info['path']));
     }
     # Get the mime/type of each file
     # XXX: Use finfo_buffer for PHP 5.3+
     $info['type'] = mime_content_type($info['path']);
     if (!($fileId = AttachmentFile::save($info))) {
         return $this->error(sprintf('%s: Unable to migrate attachment', $info['path']));
     }
     # Update the ATTACHMENT_TABLE record to set file_id
     db_query('update ' . TICKET_ATTACHMENT_TABLE . ' set file_id=' . db_input($fileId) . ' where attach_id=' . db_input($info['attachId']));
     # Remove disk image of the file. If this fails, the migration for
     # this file would not be retried, because the file_id in the
     # TICKET_ATTACHMENT_TABLE has a nonzero value now
     if (!@unlink($info['path'])) {
         $this->error(sprintf('%s: Unable to remove file from disk', $info['path']));
     }
     # TODO: Log an internal note to the ticket?
     return true;
 }
     $attachment->licenseID = $_POST['licenseID'];
     try {
         $attachment->save();
         echo $attachment->primaryKey;
     } catch (Exception $e) {
         echo $e->getMessage();
     }
     break;
     //adding the attachment file to the db - saves the URL to it only
 //adding the attachment file to the db - saves the URL to it only
 case 'addAttachmentFile':
     $attachmentFile = new AttachmentFile();
     $attachmentFile->attachmentID = $_GET['attachmentID'];
     $attachmentFile->attachmentURL = $_GET['attachmentURL'];
     try {
         $attachmentFile->save();
         echo $attachmentFile->primaryKey;
     } catch (Exception $e) {
         echo $e->getMessage();
     }
     break;
 case 'deleteAttachment':
     $attachment = new Attachment(new NamedArguments(array('primaryKey' => $_GET['attachmentID'])));
     //first delete attachments
     foreach ($attachment->getAttachmentFiles() as $attachmentFile) {
         $attachmentFile->delete();
     }
     try {
         $attachment->delete();
         echo "Attachment successfully deleted";
     } catch (Exception $e) {
 /**
  * Processes the next item on the work queue. Emits a JSON messages to
  * indicate current progress.
  *
  * Returns:
  * TRUE/NULL if the migration was successful
  */
 function next()
 {
     # Fetch next item -- use the last item so the array indices don't
     # need to be recalculated for every shift() operation.
     $info = array_pop($this->queue);
     # Attach file to the ticket
     if (!($info['data'] = @file_get_contents($info['path']))) {
         # Continue with next file
         return $this->skip($info['attachId'], sprintf('%s: Cannot read file contents', $info['path']));
     }
     # Get the mime/type of each file
     # XXX: Use finfo_buffer for PHP 5.3+
     if (function_exists('mime_content_type')) {
         //XXX: function depreciated in newer versions of PHP!!!!!
         $info['type'] = mime_content_type($info['path']);
     } elseif (function_exists('finfo_file')) {
         // PHP 5.3.0+
         $finfo = finfo_open(FILEINFO_MIME_TYPE);
         $info['type'] = finfo_file($finfo, $info['path']);
     }
     # TODO: Add extension-based mime-type lookup
     if (!($fileId = AttachmentFile::save($info))) {
         return $this->skip($info['attachId'], sprintf('%s: Unable to migrate attachment', $info['path']));
     }
     # Update the ATTACHMENT_TABLE record to set file_id
     db_query('update ' . TICKET_ATTACHMENT_TABLE . ' set file_id=' . db_input($fileId) . ' where attach_id=' . db_input($info['attachId']));
     # Remove disk image of the file. If this fails, the migration for
     # this file would not be retried, because the file_id in the
     # TICKET_ATTACHMENT_TABLE has a nonzero value now
     if (!@unlink($info['path'])) {
         //XXX: what should we do on failure?
         $this->error(sprintf('%s: Unable to remove file from disk', $info['path']));
     }
     # TODO: Log an internal note to the ticket?
     return true;
 }
Example #4
0
 /**
  * Called from API and email routines and such to handle attachments
  * sent other than via web upload
  */
 function uploadAttachment(&$file)
 {
     if (!$this->isValidFileType($file['name'], $file['type'])) {
         throw new FileUploadError(__('File type is not allowed'));
     }
     if (is_callable($file['data'])) {
         $file['data'] = $file['data']();
     }
     if (!isset($file['size'])) {
         // bootstrap.php include a compat version of mb_strlen
         if (extension_loaded('mbstring')) {
             $file['size'] = mb_strlen($file['data'], '8bit');
         } else {
             $file['size'] = strlen($file['data']);
         }
     }
     $config = $this->getConfiguration();
     if ($file['size'] > $config['size']) {
         throw new FileUploadError(__('File size is too large'));
     }
     if (!($id = AttachmentFile::save($file))) {
         throw new FileUploadError(__('Unable to save file'));
     }
     return $id;
 }
Example #5
0
 function upload($file, $ft = 'T', $deduplicate = true)
 {
     if (!$file['name'] || $file['error'] || !is_uploaded_file($file['tmp_name'])) {
         return false;
     }
     list($key, $sig) = self::_getKeyAndHash($file['tmp_name'], true);
     $info = array('type' => $file['type'], 'filetype' => $ft, 'size' => $file['size'], 'name' => $file['name'], 'key' => $key, 'signature' => $sig, 'tmp_name' => $file['tmp_name']);
     return AttachmentFile::save($info, $ft, $deduplicate);
 }
Example #6
0
 function save($info, $inline = true)
 {
     if (!($fileId = AttachmentFile::save($info))) {
         return false;
     }
     $sql = 'INSERT INTO ' . ATTACHMENT_TABLE . ' SET `type`=' . db_input($this->getType()) . ',object_id=' . db_input($this->getId()) . ',file_id=' . db_input($fileId) . ',inline=' . db_input($inline ? 1 : 0);
     if (!db_query($sql) || !db_affected_rows()) {
         return false;
     }
     return $fileId;
 }
 function saveAttachment($file, $refid, $type)
 {
     if (!$refid || !$type || !($fileId = is_numeric($file) ? $file : AttachmentFile::save($file))) {
         return 0;
     }
     $sql = 'INSERT INTO ' . TICKET_ATTACHMENT_TABLE . ' SET created=NOW() ' . ' ,ticket_id=' . db_input($this->getId()) . ' ,file_id=' . db_input($fileId) . ' ,ref_id=' . db_input($refid) . ' ,ref_type=' . db_input($type);
     return db_query($sql) && ($id = db_insert_id()) ? $id : 0;
 }
Example #8
0
 function upload($file, $ft = 'T')
 {
     if (!$file['name'] || $file['error'] || !is_uploaded_file($file['tmp_name'])) {
         return false;
     }
     $info = array('type' => $file['type'], 'filetype' => $ft, 'size' => $file['size'], 'name' => $file['name'], 'hash' => MD5(MD5_FILE($file['tmp_name']) . time()), 'data' => file_get_contents($file['tmp_name']));
     return AttachmentFile::save($info);
 }
 function saveAttachment(&$file)
 {
     if (is_numeric($file)) {
         $fileId = $file;
     } elseif (is_array($file) && isset($file['id'])) {
         $fileId = $file['id'];
     } elseif (!($fileId = AttachmentFile::save($file))) {
         return 0;
     }
     $inline = is_array($file) && @$file['inline'];
     // TODO: Add a unique index to TICKET_ATTACHMENT_TABLE (file_id,
     // ref_id), and remove this block
     if ($id = db_result(db_query('SELECT attach_id FROM ' . TICKET_ATTACHMENT_TABLE . ' WHERE file_id=' . db_input($fileId) . ' AND ref_id=' . db_input($this->getId())))) {
         return $id;
     }
     $sql = 'INSERT IGNORE INTO ' . TICKET_ATTACHMENT_TABLE . ' SET created=NOW() ' . ' ,file_id=' . db_input($fileId) . ' ,ticket_id=' . db_input($this->getTicketId()) . ' ,inline=' . db_input($inline ? 1 : 0) . ' ,ref_id=' . db_input($this->getId());
     return db_query($sql) && ($id = db_insert_id()) ? $id : 0;
 }
Example #10
0
 function save($file, $inline = true)
 {
     if (is_numeric($file)) {
         $fileId = $file;
     } elseif (is_array($file) && isset($file['id'])) {
         $fileId = $file['id'];
     } elseif (!($fileId = AttachmentFile::save($file))) {
         return false;
     }
     $sql = 'INSERT INTO ' . ATTACHMENT_TABLE . ' SET `type`=' . db_input($this->getType()) . ',object_id=' . db_input($this->getId()) . ',file_id=' . db_input($fileId) . ',inline=' . db_input($inline ? 1 : 0);
     if (!db_query($sql) || !db_affected_rows()) {
         return false;
     }
     return $fileId;
 }