public function testget_mime_content_type_from_filename()
 {
     //execute the method and test if it returns expected values
     $this->assertSame('', get_mime_content_type_from_filename(''));
     $this->assertSame('application/octet-stream', get_mime_content_type_from_filename('file.tmp'));
     $this->assertSame('text/plain', get_mime_content_type_from_filename('file.txt'));
     $this->assertSame('application/x-shockwave-flash', get_mime_content_type_from_filename('file.swf'));
     $this->assertSame('video/x-flv', get_mime_content_type_from_filename('file.flv'));
 }
Exemple #2
0
 public function save(&$bean, $params, $field, $vardef, $prefix = '')
 {
     $fakeDisplayParams = array();
     $this->fillInOptions($vardef, $fakeDisplayParams);
     require_once 'include/upload_file.php';
     $upload_file = new UploadFile($prefix . $field . '_file');
     //remove file
     if (isset($_REQUEST['remove_file_' . $field]) && $params['remove_file_' . $field] == 1) {
         $upload_file->unlink_file($bean->{$field});
         $bean->{$field} = "";
     }
     $move = false;
     if (isset($_FILES[$prefix . $field . '_file']) && $upload_file->confirm_upload()) {
         if ($this->verify_image($upload_file)) {
             $bean->{$field} = $upload_file->get_stored_file_name();
             $move = true;
         } else {
             //not valid image.
             $GLOBALS['log']->fatal("Image Field : Not a Valid Image.");
             $temp = $vardef['vname'];
             $temp = translate($temp, $bean->module_name);
             SugarApplication::appendErrorMessage($temp . " Field :  Not a valid image format.");
         }
     }
     if (empty($bean->id)) {
         $bean->id = create_guid();
         $bean->new_with_id = true;
     }
     if ($move) {
         $upload_file->final_move($bean->id . '_' . $field);
         //BEAN ID IS THE FILE NAME IN THE INSTANCE.
         $upload_file->upload_doc($bean, $bean->id, $params[$prefix . $vardef['docType']], $bean->{$field}, $upload_file->mime_type);
     } else {
         if (!empty($old_id)) {
             // It's a duplicate, I think
             if (empty($params[$prefix . $vardef['docUrl']])) {
                 $upload_file->duplicate_file($old_id, $bean->id, $bean->{$field});
             } else {
                 $docType = $vardef['docType'];
                 $bean->{$docType} = $params[$prefix . $field . '_old_doctype'];
             }
         } else {
             if (!empty($params[$prefix . $field . '_remoteName'])) {
                 // We aren't moving, we might need to do some remote linking
                 $displayParams = array();
                 $this->fillInOptions($vardef, $displayParams);
                 if (isset($params[$prefix . $vardef['docId']]) && !empty($params[$prefix . $vardef['docId']]) && isset($params[$prefix . $vardef['docType']]) && !empty($params[$prefix . $vardef['docType']])) {
                     $bean->{$field} = $params[$prefix . $field . '_remoteName'];
                     require_once 'include/utils/file_utils.php';
                     $extension = get_file_extension($bean->{$field});
                     if (!empty($extension)) {
                         $bean->file_ext = $extension;
                         $bean->file_mime_type = get_mime_content_type_from_filename($bean->{$field});
                     }
                 }
             }
         }
     }
 }
 public function save(&$bean, $params, $field, $vardef, $prefix = '')
 {
     $fakeDisplayParams = array();
     $this->fillInOptions($vardef, $fakeDisplayParams);
     require_once 'include/upload_file.php';
     $upload_file = new UploadFile($prefix . $field . '_file');
     //remove file
     if (isset($_REQUEST['remove_file_' . $field]) && $params['remove_file_' . $field] == 1) {
         $upload_file->unlink_file($bean->{$field});
         $bean->{$field} = "";
     }
     $move = false;
     if (isset($_FILES[$prefix . $field . '_file']) && $upload_file->confirm_upload()) {
         $bean->{$field} = $upload_file->get_stored_file_name();
         $bean->file_mime_type = $upload_file->mime_type;
         $bean->file_ext = $upload_file->file_ext;
         $move = true;
     }
     if (!empty($params['isDuplicate']) && $params['isDuplicate'] == 'true') {
         // This way of detecting duplicates is used in Notes
         $old_id = $params['relate_id'];
     }
     if (!empty($params['duplicateSave']) && !empty($params['duplicateId'])) {
         // It's a duplicate
         $old_id = $params['duplicateId'];
     }
     // Backwards compatibility for fields that still use customCode to handle the file uploads
     if (!$move && empty($old_id) && isset($_FILES['uploadfile'])) {
         $upload_file = new UploadFile('uploadfile');
         if ($upload_file->confirm_upload()) {
             $bean->{$field} = $upload_file->get_stored_file_name();
             $bean->file_mime_type = $upload_file->mime_type;
             $bean->file_ext = $upload_file->file_ext;
             $move = true;
         }
     } else {
         if (!$move && !empty($old_id) && isset($_REQUEST['uploadfile']) && !isset($_REQUEST[$prefix . $field . '_file'])) {
             // I think we are duplicating a backwards compatibility module.
             $upload_file = new UploadFile('uploadfile');
         }
     }
     if (empty($bean->id)) {
         $bean->id = create_guid();
         $bean->new_with_id = true;
     }
     if ($move) {
         $upload_file->final_move($bean->id);
         $upload_file->upload_doc($bean, $bean->id, $params[$prefix . $vardef['docType']], $bean->{$field}, $upload_file->mime_type);
     } else {
         if (!empty($old_id)) {
             // It's a duplicate, I think
             if (empty($params[$prefix . $vardef['docUrl']])) {
                 $upload_file->duplicate_file($old_id, $bean->id, $bean->{$field});
             } else {
                 $docType = $vardef['docType'];
                 $bean->{$docType} = $params[$prefix . $field . '_old_doctype'];
             }
         } else {
             if (!empty($params[$prefix . $field . '_remoteName'])) {
                 // We aren't moving, we might need to do some remote linking
                 $displayParams = array();
                 $this->fillInOptions($vardef, $displayParams);
                 if (isset($params[$prefix . $vardef['docId']]) && !empty($params[$prefix . $vardef['docId']]) && isset($params[$prefix . $vardef['docType']]) && !empty($params[$prefix . $vardef['docType']])) {
                     $bean->{$field} = $params[$prefix . $field . '_remoteName'];
                     require_once 'include/utils/file_utils.php';
                     $extension = get_file_extension($bean->{$field});
                     if (!empty($extension)) {
                         $bean->file_ext = $extension;
                         $bean->file_mime_type = get_mime_content_type_from_filename($bean->{$field});
                     }
                 }
             }
         }
     }
     if ($vardef['allowEapm'] == true && empty($bean->{$field})) {
         $GLOBALS['log']->info("The {$field} is empty, clearing out the lot");
         // Looks like we are emptying this out
         $clearFields = array('docId', 'docType', 'docUrl', 'docDirectUrl');
         foreach ($clearFields as $clearMe) {
             if (!isset($vardef[$clearMe])) {
                 continue;
             }
             $clearField = $vardef[$clearMe];
             $bean->{$clearField} = '';
         }
     }
 }
 public function save($bean, $params, $field, $vardef, $prefix = '')
 {
     $fakeDisplayParams = array();
     $this->fillInOptions($vardef, $fakeDisplayParams);
     require_once 'include/upload_file.php';
     $upload_file = new UploadFile($prefix . $field . '_file');
     //remove file
     if (isset($_REQUEST['remove_file_' . $field]) && $params['remove_file_' . $field] == 1) {
         $upload_file->unlink_file($bean->{$field});
         $bean->{$field} = "";
     }
     $move = false;
     // In case of failure midway, we need to reset the values of the bean
     $originalvals = array('value' => $bean->{$field});
     // Bug 57400 - Some beans with a filename field type do NOT have file_mime_type
     // or file_ext. In the case of Documents, for example, this happens to be
     // the case, since the DocumentRevisions bean is where these fields are found.
     if (isset($bean->file_mime_type)) {
         $originalvals['mime'] = $bean->file_mime_type;
     }
     if (isset($bean->file_ext)) {
         $originalvals['ext'] = $bean->file_ext;
     }
     if (isset($_FILES[$prefix . $field . '_file']) && $upload_file->confirm_upload()) {
         // in order to avoid any discrepancies of MIME type with the download code,
         // call the same MIME function instead of using the uploaded file's mime type property.
         $mimeType = get_file_mime_type($upload_file->get_temp_file_location(), 'application/octet-stream');
         //verify the image
         if (in_array($mimeType, self::$imageFileMimeTypes) && !verify_image_file($upload_file->get_temp_file_location())) {
             $this->error = string_format($GLOBALS['app_strings']['LBL_UPLOAD_IMAGE_FILE_NOT_SUPPORTED'], array($upload_file->file_ext));
             return;
         }
         $bean->{$field} = $upload_file->get_stored_file_name();
         $bean->file_mime_type = $upload_file->mime_type;
         $bean->file_ext = $upload_file->file_ext;
         $move = true;
     } else {
         $this->error = $upload_file->getErrorMessage();
     }
     if (!empty($params['isDuplicate']) && $params['isDuplicate'] == 'true') {
         // This way of detecting duplicates is used in Notes
         $old_id = $params['relate_id'];
     }
     if (!empty($params['duplicateSave']) && !empty($params['duplicateId'])) {
         // It's a duplicate
         $old_id = $params['duplicateId'];
     }
     // case when we should copy one file to another using merge-duplicate view
     // $params[$field . '_duplicateBeanId'] contains id of bean from
     // which we should copy file.
     if (!empty($params[$field . '_duplicateBeanId'])) {
         $duplicateModuleId = $params[$field . '_duplicateBeanId'];
     }
     // Backwards compatibility for fields that still use customCode to handle the file uploads
     if (!$move && empty($old_id) && isset($_FILES['uploadfile'])) {
         $upload_file = new UploadFile('uploadfile');
         if ($upload_file->confirm_upload()) {
             $bean->{$field} = $upload_file->get_stored_file_name();
             $bean->file_mime_type = $upload_file->mime_type;
             $bean->file_ext = $upload_file->file_ext;
             $move = true;
         } else {
             $this->error = $upload_file->getErrorMessage();
         }
     } elseif (!$move && !empty($old_id) && isset($_REQUEST['uploadfile']) && !isset($_REQUEST[$prefix . $field . '_file'])) {
         // I think we are duplicating a backwards compatibility module.
         $upload_file = new UploadFile('uploadfile');
     }
     if (empty($bean->id)) {
         $bean->id = create_guid();
         $bean->new_with_id = true;
     }
     if ($move) {
         $temp = !empty($params['temp']);
         // Added checking of final move to capture errors that might occur
         if ($upload_file->final_move($bean->id, $temp)) {
             if (!$temp) {
                 // This fixes an undefined index warning being thrown
                 $docType = isset($vardef['docType']) && isset($params[$prefix . $vardef['docType']]) ? $params[$prefix . $vardef['docType']] : null;
                 $upload_file->upload_doc($bean, $bean->id, $docType, $bean->{$field}, $upload_file->mime_type);
             }
         } else {
             // Reset the bean back to original, but only if we had set them.
             $bean->{$field} = $originalvals['value'];
             // See comments for these properties above in regards to Bug 57400
             if (isset($originalvals['mime'])) {
                 $bean->file_mime_type = $originalvals['mime'];
             }
             if (isset($originalvals['ext'])) {
                 $bean->file_ext = $originalvals['ext'];
             }
             // Report the error
             $this->error = $upload_file->getErrorMessage();
         }
     } elseif (!empty($old_id)) {
         // It's a duplicate, I think
         if (empty($vardef['docUrl']) || empty($params[$prefix . $vardef['docUrl']])) {
             $upload_file->duplicate_file($old_id, $bean->id, $bean->{$field});
         } else {
             $docType = $vardef['docType'];
             $bean->{$docType} = $params[$prefix . $field . '_old_doctype'];
         }
     } elseif (!empty($params[$prefix . $field . '_remoteName'])) {
         // We aren't moving, we might need to do some remote linking
         $displayParams = array();
         $this->fillInOptions($vardef, $displayParams);
         if (isset($params[$prefix . $vardef['docId']]) && !empty($params[$prefix . $vardef['docId']]) && isset($params[$prefix . $vardef['docType']]) && !empty($params[$prefix . $vardef['docType']])) {
             $bean->{$field} = $params[$prefix . $field . '_remoteName'];
             require_once 'include/utils/file_utils.php';
             $extension = get_file_extension($bean->{$field});
             if (!empty($extension)) {
                 $bean->file_ext = $extension;
                 $bean->file_mime_type = get_mime_content_type_from_filename($bean->{$field});
             }
         }
     } elseif (!empty($duplicateModuleId)) {
         $upload_file->duplicate_file($duplicateModuleId, $bean->id, $bean->{$field});
         $bean->{$field} = $params[$field];
         require_once 'include/utils/file_utils.php';
         $extension = get_file_extension($bean->{$field});
         if (!empty($extension)) {
             $bean->file_ext = $extension;
             $bean->file_mime_type = get_mime_content_type_from_filename($bean->{$field});
         }
     }
     if ($vardef['allowEapm'] == true && empty($bean->{$field})) {
         $GLOBALS['log']->info("The {$field} is empty, clearing out the lot");
         // Looks like we are emptying this out
         $clearFields = array('docId', 'docType', 'docUrl', 'docDirectUrl');
         foreach ($clearFields as $clearMe) {
             if (!isset($vardef[$clearMe])) {
                 continue;
             }
             $clearField = $vardef[$clearMe];
             $bean->{$clearField} = '';
         }
     }
 }