Share Button

setClient($client); $this->setBucket($bucket); parent::__construct($options); } /** * Analyzes the provided data and turns it into useful data that can be * consumed and used to build an upload form * * @return PostObject */ public function prepareData() { // Validate required options $options = Collection::fromConfig($this->data, array( 'ttd' => '+1 hour', 'key' => '^${filename}', )); // Format ttd option $ttd = $options['ttd']; $ttd = is_numeric($ttd) ? (int) $ttd : strtotime($ttd); unset($options['ttd']); // If a policy or policy callback were provided, extract those from the options $rawJsonPolicy = $options['policy']; $policyCallback = $options['policy_callback']; unset($options['policy'], $options['policy_callback']); // Setup policy document $policy = array( 'expiration' => gmdate(DateFormat::ISO8601_S3, $ttd), 'conditions' => array(array('bucket' => $this->bucket)) ); // Configure the endpoint/action $url = Url::factory($this->client->getBaseUrl()); if ($url->getScheme() === 'https' && strpos($this->bucket, '.') !== false) { // Use path-style URLs $url->setPath($this->bucket); } else { // Use virtual-style URLs $url->setHost($this->bucket . '.' . $url->getHost()); } // Setup basic form $this->formAttributes = array( 'action' => (string) $url, 'method' => 'POST', 'enctype' => 'multipart/form-data' ); $this->formInputs = array( 'AWSAccessKeyId' => $this->client->getCredentials()->getAccessKeyId() ); // Add success action status $status = (int) $options->get('success_action_status'); if ($status && in_array($status, array(200, 201, 204))) { $this->formInputs['success_action_status'] = (string) $status; $policy['conditions'][] = array( 'success_action_status' => (string) $status ); unset($options['success_action_status']); } // Add other options foreach ($options as $key => $value) { $value = (string) $value; if ($value[0] === '^') { $value = substr($value, 1); $this->formInputs[$key] = $value; $value = preg_replace('/\$\{(\w*)\}/', '', $value); $policy['conditions'][] = array('starts-with', '$' . $key, $value); } else { $this->formInputs[$key] = $value; $policy['conditions'][] = array($key => $value); } } // Handle the policy $policy = is_callable($policyCallback) ? $policyCallback($policy, $this) : $policy; $this->jsonPolicy = $rawJsonPolicy ?: json_encode($policy); $this->applyPolicy(); return $this; } /** * Sets the S3 client * * @param S3Client $client * * @return PostObject */ public function setClient(S3Client $client) { $this->client = $client; return $this; } /** * Gets the S3 client * * @return S3Client */ public function getClient() { return $this->client; } /** * Sets the bucket and makes sure it is a valid bucket name * * @param string $bucket * * @return PostObject */ public function setBucket($bucket) { $this->bucket = $bucket; return $this; } /** * Gets the bucket name * * @return string */ public function getBucket() { return $this->bucket; } /** * Gets the form attributes as an array * * @return array */ public function getFormAttributes() { return $this->formAttributes; } /** * Gets the form inputs as an array * * @return array */ public function getFormInputs() { return $this->formInputs; } /** * Gets the raw JSON policy * * @return string */ public function getJsonPolicy() { return $this->jsonPolicy; } /** * Handles the encoding, singing, and injecting of the policy */ protected function applyPolicy() { $jsonPolicy64 = base64_encode($this->jsonPolicy); $this->formInputs['policy'] = $jsonPolicy64; $this->formInputs['signature'] = base64_encode(hash_hmac( 'sha1', $jsonPolicy64, $this->client->getCredentials()->getSecretKey(), true )); } } w Date();a=s.createElement(o), m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m) })(window,document,'script','//www.google-analytics.com/analytics.js','ga'); ga('create', 'UA-7486148-1', 'auto'); ga('send', 'pageview');