POST operation adds an object to a specified bucket using HTML forms. POST is an alternate * form of PUT that enables browser-based uploads as a way of putting objects in buckets. * Parameters that are passed to PUT via HTTP headers are instead passed as form fields to * POST in the multipart/form-data encoded message body. You must have * WRITE access on a bucket to add an object to it. Amazon S3 never stores partial objects: if * you receive a successful response, you can be confident the entire object was stored. * * @param string $bucket (Required) The name of the bucket to use. * @param string|integer $expires (Optional) The point in time when the upload form field should expire. The default value is +1 hour. * @param array $opt (Optional) An associative array of parameters that can have the following keys: * @return array An array of fields that can be converted into markup. * @link http://docs.amazonwebservices.com/AmazonS3/latest/API/RESTObjectPOST.html POST Object */ public function generate_upload_parameters($bucket, $expires = '+1 hour', $opt = null) { if (!$opt) $opt = array(); // Policy document $policy = array( 'conditions' => array( array('bucket' => $bucket), ) ); // Basic form $form = array(); $form['form'] = array( 'action' => $bucket . '.s3.amazonaws.com', 'method' => 'POST', 'enctype' => 'multipart/form-data' ); // Inputs $form['inputs'] = array( 'AWSAccessKeyId' => $this->key ); // Expires if ($expires) { if (is_numeric($expires)) { $expires = gmdate('j M Y, g:i a Z', (integer) $expires); } $expires = $this->util->convert_date_to_iso8601($expires); $policy['expiration'] = (string) $expires; } // Default values if (!isset($opt['key'])) { $opt['key'] = '${filename}'; } // Success Action Status if (isset($opt['success_action_status']) && !empty($opt['success_action_status'])) { $form['inputs']['success_action_status'] = (string) $opt['success_action_status']; $policy['conditions'][] = array( 'success_action_status' => (string) $opt['success_action_status'] ); unset($opt['success_action_status']); } // Other parameters foreach ($opt as $param_key => $param_value) { if ($param_value[0] === '^') { $form['inputs'][$param_key] = substr((string) $param_value, 1); $param_value = preg_replace('/\$\{(\w*)\}/', '', (string) $param_value); $policy['conditions'][] = array('starts-with', '$' . $param_key, (substr((string) $param_value, 1) ? substr((string) $param_value, 1) : '')); } else { $form['inputs'][$param_key] = (string) $param_value; $policy['conditions'][] = array( $param_key => (string) $param_value ); } } // Add policy $json_policy = json_encode($policy); $json_policy_b64 = base64_encode($json_policy); $form['inputs']['policy'] = $json_policy_b64; $form['metadata']['json_policy'] = $json_policy; // Add signature $form['inputs']['signature'] = base64_encode(hash_hmac('sha1', $json_policy_b64, $this->secret_key, true)); return $form; } /*%******************************************************************************************%*/ // HELPERS /** * Returns the protocol of the web page that this script is currently running on. This method only works * correctly when run from a publicly-accessible web page. */ public static function protocol() { return (isset($_SERVER['HTTPS']) && strtolower($_SERVER['HTTPS']) === 'on') ? 'https://' : 'http://'; } /** * Returns the domain (and port) of the web page that this script is currently running on. This method * only works correctly when run from a publicly-accessible web page. */ public static function domain() { if (isset($_SERVER['SERVER_NAME']) && isset($_SERVER['SERVER_PORT'])) { return $_SERVER['SERVER_NAME'] . ((integer) $_SERVER['SERVER_PORT'] === 80 ? '' : ':' . $_SERVER['SERVER_PORT']); } return null; } /** * Returns the URI of the web page that this script is currently running on. This method only works * correctly when run from a publicly-accessible web page. */ public static function current_uri() { if (isset($_SERVER['REQUEST_URI'])) { $uri = self::protocol(); $uri .= self::domain(); $uri .= $_SERVER['REQUEST_URI']; return $uri; } return null; } }