/** * Gets a single temporary file for rendering and removes it from filesystem. * * @param ServiceBase $api The service base * @param array $args Arguments array built by the service base */ public function getTempImage($api, $args) { // Get the field if (empty($args['field'])) { // @TODO Localize this exception message throw new SugarApiExceptionMissingParameter('Field name is missing'); } $field = $args['field']; // Get the bean $bean = BeanFactory::newBean($args['module']); // Handle ACL $this->verifyFieldAccess($bean, $field); $filepath = UploadStream::path("upload://tmp/") . $args['temp_id']; if (is_file($filepath)) { $filedata = getimagesize($filepath); $info = array('content-type' => $filedata['mime'], 'path' => $filepath); require_once "include/download_file.php"; $dl = new DownloadFileApi($api); $dl->outputFile(false, $info); if (!empty($args['keep'])) { return; } register_shutdown_function(function () use($filepath) { if (is_file($filepath)) { unlink($filepath); } }); } else { throw new SugarApiExceptionInvalidParameter('File not found'); } }
/** * Constructor * * @param string $filename * @param string $delimiter * @param string $enclosure * @param bool $deleteFile * @param bool $checkUploadPath * @param int $rowsCount */ public function __construct($filename, $delimiter = ',', $enclosure = '', $deleteFile = true, $checkUploadPath = true, $rowsCount = 0) { if (!is_file($filename) || !is_readable($filename)) { return false; } if ($checkUploadPath && UploadStream::path($filename) == null) { $GLOBALS['log']->fatal("ImportFile detected attempt to access to the following file not within the sugar upload dir: {$filename}"); return null; } // turn on auto-detection of line endings to fix bug #10770 ini_set('auto_detect_line_endings', '1'); $this->_fp = sugar_fopen($filename, 'r'); $this->_sourcename = $filename; $this->_deleteFile = $deleteFile; $this->_delimiter = empty($delimiter) ? ',' : $delimiter; if ($this->_delimiter == '\\t') { $this->_delimiter = "\t"; } $this->_enclosure = empty($enclosure) ? '' : trim($enclosure); // Autodetect does setFpAfterBOM() $this->_encoding = $this->autoDetectCharacterSet(); $this->_rowsCount = $rowsCount; }
/** * Return real FS path of the file * @param string $path */ public static function realpath($path) { if (substr($path, 0, 9) == "upload://") { $path = UploadStream::path($path); } $ret = realpath($path); return $ret ? $ret : $path; }
/** * Moves temporary files associated with the bean from the temporary folder * to the upload folder. * * @param array $args The request arguments. * @param SugarBean $bean The bean associated with the file. * @throws SugarApiExceptionInvalidParameter If the file mime types differ * from $imageFileMimeTypes. */ protected function moveTemporaryFiles($args, SugarBean $bean) { require_once 'include/upload_file.php'; require_once 'include/SugarFields/SugarFieldHandler.php'; $fileFields = $bean->getFieldDefinitions('type', array('file', 'image')); $sfh = new SugarFieldHandler(); // FIXME This path should be changed with BR-1955. $basepath = UploadStream::path('upload://tmp/'); $configDir = SugarConfig::getInstance()->get('upload_dir', 'upload'); foreach ($fileFields as $fieldName => $def) { if (empty($args[$fieldName . '_guid'])) { continue; } $this->verifyFieldAccess($bean, $fieldName); $filepath = $basepath . $args[$fieldName . '_guid']; if (!is_file($filepath)) { continue; } if ($def['type'] === 'image') { $filename = $args[$fieldName . '_guid']; $bean->{$fieldName} = $filename; } else { // FIXME Image verification and mime type updating // should not be duplicated from SugarFieldFile. // SC-3338 is tracking this. require_once 'include/utils/file_utils.php'; $filename = $bean->id; $mimeType = get_file_mime_type($filepath, 'application/octet-stream'); $sf = $sfh->getSugarField($def['type']); $extension = pathinfo($fieldName, PATHINFO_EXTENSION); if (in_array($mimeType, $sf::$imageFileMimeTypes) && !verify_image_file($filepath)) { throw new SugarApiExceptionInvalidParameter(string_format($GLOBALS['app_strings']['LBL_UPLOAD_IMAGE_FILE_NOT_SUPPORTED'], array($extension))); } $bean->file_mime_type = $mimeType; $bean->file_ext = $extension; } $destination = rtrim($configDir, '/\\') . '/' . $filename; // FIXME BR-1956 will address having multiple files // associated with a record. rename($filepath, $destination); } }