Ejemplo n.º 1
0
 public function processRawFieldData($data, &$status, &$message = null, $simulate = false, $entry_id = NULL)
 {
     $status = self::__OK__;
     //fixes bug where files are deleted, but their database entries are not.
     if ($data === NULL) {
         return array('file' => NULL, 'mimetype' => NULL, 'size' => NULL, 'meta' => NULL);
     }
     ## Its not an array, so just retain the current data and return (the case where we're not uploading a new file)
     if (!is_array($data)) {
         $status = self::__OK__;
         $result = array('file' => $data, 'mimetype' => NULL, 'size' => NULL, 'meta' => NULL);
         // Grab the existing entry data to preserve the MIME type and size information
         if (isset($entry_id) && !is_null($entry_id)) {
             $row = Symphony::Database()->fetchRow(0, sprintf("SELECT `file`, `mimetype`, `size`, `meta` FROM `tbl_entries_data_%d` WHERE `entry_id` = %d", $this->get('id'), $entry_id));
             if (!empty($row)) {
                 $result = $row;
             }
         }
         return $result;
     }
     if ($this->get('unique_filename') == true && isset($data['name'])) {
         $this->getUniqueFilename($data['name']);
     }
     // Editing an entry: Where we're uploading a new file and getting rid of the old one
     if ($entry_id) {
         $row = Symphony::Database()->fetchRow(0, "SELECT * FROM `tbl_entries_data_" . $this->get('id') . "` WHERE `entry_id` = '{$entry_id}' LIMIT 1");
         $existing_file = $row['file'];
         if (!is_null($existing_file) && strtolower($existing_file) != strtolower($data['file']) || $data['error'] == UPLOAD_ERR_NO_FILE && !is_null($existing_file)) {
             $this->S3->deleteObject($this->get('bucket'), basename($existing_file));
         }
     }
     if ($data['error'] == UPLOAD_ERR_NO_FILE || $data['error'] != UPLOAD_ERR_OK) {
         return;
     }
     // Sanitize the filename
     $data['name'] = Lang::createFilename($data['name']);
     ## Upload the new file
     $headers = array('Content-Type' => $data['type']);
     if ($this->_driver->getCacheControl() != false) {
         $headers['Cache-Control'] = "max-age=" . $this->_driver->getCacheControl();
     }
     try {
         $this->S3->putObject($this->S3->inputResource(fopen($data['tmp_name'], 'rb'), filesize($data['tmp_name'])), $this->get('bucket'), $data['name'], 'public-read', array(), $headers);
     } catch (Exception $e) {
         $status = self::__ERROR_CUSTOM__;
         return array('file' => NULL, 'mimetype' => NULL, 'size' => NULL, 'meta' => NULL);
     }
     $status = self::__OK__;
     ## If browser doesn't send MIME type (e.g. .flv in Safari)
     if (strlen(trim($data['type'])) == 0) {
         $data['type'] = 'unknown';
     }
     // all we need is the path and name, the domain is abstracted depending on whether or not it has a cname
     return array('file' => $data['name'], 'size' => $data['size'], 'mimetype' => $data['type'], 'meta' => serialize(parent::getMetaInfo($data['tmp_name'], $data['type'])));
 }
Ejemplo n.º 2
0
 public function processRawFieldData($data, &$status, &$message = null, $simulate = false, $entry_id = NULL)
 {
     $status = self::__OK__;
     //fixes bug where files are deleted, but their database entries are not.
     if ($data === NULL) {
         return array('file' => NULL, 'mimetype' => NULL, 'size' => NULL, 'meta' => NULL);
     }
     ## Its not an array, so just retain the current data and return (the case where we're not uploading a new file)
     if (!is_array($data)) {
         $result = array('file' => $data, 'mimetype' => NULL, 'size' => NULL, 'meta' => NULL);
         // Grab the existing entry data to preserve the MIME type and size information
         if (isset($entry_id) && !is_null($entry_id)) {
             $row = Symphony::Database()->fetchRow(0, sprintf("SELECT `file`, `mimetype`, `size`, `meta` FROM `tbl_entries_data_%d` WHERE `entry_id` = %d", $this->get('id'), $entry_id));
             if (!empty($row)) {
                 $result = $row;
             }
         }
         return $result;
     }
     if ($this->get('unique_filename') == true && isset($data['name'])) {
         $this->getUniqueFilename($data['name']);
     }
     // Editing an entry: Where we're uploading a new file and getting rid of the old one
     if (is_null($entry_id) === false) {
         $row = Symphony::Database()->fetchRow(0, sprintf("\r\n                SELECT * FROM `tbl_entries_data_%d` WHERE `entry_id` = %d LIMIT 1\r\n            ", $this->get('id'), $entry_id));
         $existing_file = $row['file'];
         if (!is_null($existing_file) && strtolower($existing_file) != strtolower($data['file']) || $data['error'] == UPLOAD_ERR_NO_FILE && !is_null($existing_file)) {
             $this->s3->deleteObject($this->get('bucket'), basename($existing_file));
         }
     }
     if ($data['error'] == UPLOAD_ERR_NO_FILE || $data['error'] != UPLOAD_ERR_OK) {
         return false;
     }
     // Sanitize the filename
     $data['name'] = Lang::createFilename($data['name']);
     ## Upload the new file
     $options = array('ACL' => 'public-read', 'ContentType' => $data['type']);
     if ($this->_driver->getCacheControl() != false) {
         $options['CacheControl'] = "max-age=" . $this->_driver->getCacheControl();
     }
     try {
         $this->s3->putObject($this->get('bucket'), $data['name'], $data['tmp_name'], $options);
     } catch (Exception $e) {
         $status = self::__ERROR_CUSTOM__;
         $message = __(__('There was an error while trying to upload the file %s to the bucket %s.'), array('<code>' . $data['name'] . '</code>', '<code>' . $this->get('bucket') . '</code>'));
         return array('file' => NULL, 'mimetype' => NULL, 'size' => NULL, 'meta' => NULL);
     }
     $status = self::__OK__;
     // Get the mimetype, don't trust the browser. RE: #1609
     $data['type'] = General::getMimeType($data['tmp_name']);
     // all we need is the path and name, the domain is abstracted depending on whether or not it has a cname
     return array('file' => $data['name'], 'size' => $data['size'], 'mimetype' => $data['type'], 'meta' => serialize(parent::getMetaInfo($data['tmp_name'], $data['type'])));
 }