public function processRawFieldData($data, &$status, $simulate = false, $entry_id = NULL)
 {
     if (is_array($data) and isset($data['name'])) {
         $data['name'] = $this->getUniqueFilename($data['name']);
     }
     return parent::processRawFieldData($data, $status, $simulate, $entry_id);
 }
 function commit()
 {
     if (!parent::commit()) {
         return false;
     }
     $id = $this->get('id');
     if ($id === false) {
         return false;
     }
     $fields = array();
     $fields['field_id'] = $id;
     $fields['destination'] = $this->get('destination');
     $fields['validator'] = $fields['validator'] == 'custom' ? NULL : $this->get('validator');
     $fields['max_width'] = $this->get('max_width');
     $fields['max_height'] = $this->get('max_height');
     Symphony::Database()->query("DELETE FROM `tbl_fields_" . $this->handle() . "` WHERE `field_id` = '{$id}' LIMIT 1");
     return Symphony::Database()->insert($fields, 'tbl_fields_' . $this->handle());
 }
 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'])));
 }
 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'])));
 }
 public function checkPostFieldData($data, &$message, $entry_id = NULL)
 {
     if (is_array($data) and isset($data['name'])) {
         $data['name'] = $this->getUniqueFilename($data['name']);
     }
     return parent::checkPostFieldData($data, $message, $entry_id);
 }
 public function prepareTableValue($data, XMLElement $link = NULL, $entry_id = null)
 {
     //var_dump($data);
     if (!($file = $data['file'])) {
         if ($link) {
             return parent::prepareTableValue(null, $link);
         } else {
             return parent::prepareTableValue(null);
         }
     }
     if ($link) {
         $link->setValue(basename($file));
         return $link->generate();
     } else {
         $link = Widget::Anchor(basename($file), URL . '/workspace' . $file);
         return $link->generate();
     }
 }
 public function prepareTableValue($data, XMLElement $link = NULL, $entry_id = null)
 {
     $data = $this->buildFileItems($data);
     $files = array();
     foreach ($data as $file_item) {
         $result = parent::prepareTableValue($file_item, $link, $entry_id);
         if (is_string($result)) {
             $files[] = $result;
         } else {
             $files[] = $result->generate();
         }
     }
     return implode(', ', $files);
 }