/**
  * Set a curl option for the request.
  *
  * See: http://php.net/manual/en/function.curl-setopt.php
  *
  * @param int $key The option to set.
  * @param mixed $value The value of the option.
  */
 private function setOption($key, $value)
 {
     switch ($key) {
         // Cases that we support.
         case CURLOPT_FOLLOWLOCATION:
             $this->request->setFollowRedirects($value);
             break;
         case CURLOPT_HTTPGET:
             $this->request->setMethod(RequestMethod::GET);
             break;
         case CURLOPT_NOBODY:
             $this->request->setMethod(RequestMethod::HEAD);
             break;
         case CURLOPT_POST:
             $this->request->setMethod(RequestMethod::POST);
             break;
         case CURLOPT_PUT:
             $this->request->setMethod(RequestMethod::PUT);
             break;
         case CURLOPT_SSL_VERIFYPEER:
             $this->request->setMustValidateServerCertificate($value);
             break;
         case CURLOPT_TIMEOUT:
             $this->request->setDeadline($value);
             break;
         case CURLOPT_TIMEOUT_MS:
             $this->request->setDeadline($value * 1000);
             break;
         case CURLOPT_CUSTOMREQUEST:
             if (!in_array($value, array_keys(static::$custom_request_map))) {
                 throw new CurlLiteOptionNotSupportedException('Custom request ' . $value . ' not supported by this curl ' . 'implementation.');
             }
             $this->request->setMethod(static::$custom_request_map[$value]);
             break;
         case CURLOPT_RANGE:
             $this->headers['Range'] = $value;
             break;
         case CURLOPT_REFERER:
             $this->headers['Referer'] = $value;
             $break;
         case CURLOPT_URL:
             $this->setRequestUrl($value);
             break;
         case CURLOPT_USERAGENT:
             $this->headers['User-Agent'] = $value;
             break;
         case CURLOPT_COOKIE:
             $this->headers['Cookie'] = $value;
             break;
         case CURLOPT_HTTPHEADER:
             $this->headers = ArrayUtil::arrayMergeIgnoreCase($this->headers, $this->parseHttpHeaders($value));
             break;
             // Cases that we don't support, that could cause a semantic change in the
             // application by not supporting.
         // Cases that we don't support, that could cause a semantic change in the
         // application by not supporting.
         case CURLOPT_COOKIESESSION:
         case CURLOPT_CERTINFO:
         case CURLOPT_CONNECT_ONLY:
         case CURLOPT_FTP_USE_EPRT:
         case CURLOPT_FTP_USE_EPSV:
         case CURLOPT_FTP_CREATE_MISSING_DIRS:
         case CURLOPT_FTPAPPEND:
         case CURLOPT_FTPLISTONLY:
         case CURLOPT_HTTPPROXYTUNNEL:
         case CURLOPT_NETRC:
         case CURLOPT_NOSIGNAL:
         case CURLOPT_SAFE_UPLOAD:
         case CURLOPT_TRANSFERTEXT:
         case CURLOPT_FTPSSLAUTH:
         case CURLOPT_TIMEVALUE:
         case CURLOPT_CAINFO:
         case CURLOPT_COOKIEJAR:
         case CURLOPT_FTPPORT:
         case CURLOPT_KEYPASSWD:
         case CURLOPT_KRB4LEVEL:
         case CURLOPT_SSH_HOST_PUBLIC_KEY_MD5:
         case CURLOPT_SSH_PUBLIC_KEYFILE:
         case CURLOPT_SSH_PRIVATE_KEYFILE:
         case CURLOPT_SSLCERT:
         case CURLOPT_SSLCERTPASSWD:
         case CURLOPT_SSLCERTTYPE:
         case CURLOPT_SSLENGINE:
         case CURLOPT_SSLENGINE_DEFAULT:
         case CURLOPT_SSLKEY:
         case CURLOPT_SSLKEYPASSWD:
         case CURLOPT_SSLKEYTYPE:
         case CURLOPT_POSTQUOTE:
         case CURLOPT_QUOTE:
         case CURLOPT_PROGRESSFUNCTION:
         case CURLOPT_SHARE:
             throw new CurlLiteOptionNotSupportedException('Option ' . $key . ' is not supported by this curl implementation.');
             // Everything else is a no-op, or will be configured at request time.
         // Everything else is a no-op, or will be configured at request time.
         default:
     }
     $this->options[$key] = $value;
     return true;
 }
Beispiel #2
0
 /**
  * Serve a Google Cloud Storage file as the response.
  *
  * @param string $gs_filename The name of the Google Cloud Storage object to
  * serve.
  * @param mixed[] $options Array of additional options for serving the object.
  * <ul>
  *   <li>'content_type': string Content-Type to override when known.
  *   <li>'save_as': boolean If True then send the file as an attachment.
  *   <li>'start': int Start index of content-range to send.
  *   <li>'end': int End index of content-range to send. End index is
  *   inclusive.
  *   <li>'use_range': boolean Use provided content range from the request's
  *   Range header. Mutually exclusive with start and end.
  * </ul>
  *
  * @throws \InvalidArgumentException If invalid options are supplied.
  */
 public static function serve($gs_filename, $options = [])
 {
     $extra_options = array_diff(array_keys($options), self::$serve_options);
     if (!empty($extra_options)) {
         throw new \InvalidArgumentException('Invalid options supplied: ' . htmlspecialchars(implode(',', $extra_options)));
     }
     // Determine the range to send
     $start = ArrayUtil::findByKeyOrNull($options, "start");
     $end = ArrayUtil::findByKeyOrNull($options, "end");
     $use_range = ArrayUtil::findByKeyOrNull($options, "use_range");
     $request_range_header = ArrayUtil::findByKeyOrNull($_SERVER, "HTTP_RANGE");
     $range_header = self::checkRanges($start, $end, $use_range, $request_range_header);
     $save_as = ArrayUtil::findByKeyOrNull($options, "save_as");
     if (isset($save_as) && !is_string($save_as)) {
         throw new \InvalidArgumentException("Unexpected value for save_as.");
     }
     $blob_key = self::createGsKey($gs_filename);
     self::sendHeader(self::BLOB_KEY_HEADER, $blob_key);
     if (isset($range_header)) {
         self::sendHeader(self::BLOB_RANGE_HEADER, $range_header);
     }
     $content_type = ArrayUtil::findByKeyOrNull($options, "content_type");
     if (isset($content_type)) {
         self::sendHeader("Content-Type", $content_type);
     }
     if (isset($save_as)) {
         self::sendHeader("Content-Disposition", sprintf("attachment; filename=%s", $save_as));
     }
 }
 /**
  * Construct an object of CloudStorageClient.
  *
  * @param string $bucket The name of the bucket.
  * @param string $object The name of the object, or null if there is no
  * object.
  * @param resource $context The stream context to use.
  */
 public function __construct($bucket, $object = null, $context = null)
 {
     $this->bucket_name = $bucket;
     $this->object_name = $object;
     if (!isset($context)) {
         $context = stream_context_get_default();
     }
     $context_array = stream_context_get_options($context);
     if (array_key_exists("gs", $context_array)) {
         $this->context_options = array_merge(self::$default_gs_context_options, $context_array["gs"]);
     } else {
         $this->context_options = self::$default_gs_context_options;
     }
     $this->anonymous = ArrayUtil::findByKeyOrNull($this->context_options, "anonymous");
     $this->url = $this->createObjectUrl($bucket, $object);
 }
Beispiel #4
0
 /**
  * Parse a MIME part and set the Message object accordingly.
  *
  * @param resource $part A MIME part, returned from mailparse_msg_get_part,
  *    to be parse.
  * @param string $raw_mail The string holding the raw content of the email
  *    $part is extracted from.
  * @param Message& $email The Message object to be set.
  */
 private static function parseMimePart($part, $raw_mail, &$email)
 {
     $data = mailparse_msg_get_part_data($part);
     $type = ArrayUtil::findByKeyOrDefault($data, 'content-type', 'text/plain');
     $start = $data['starting-pos-body'];
     $end = $data['ending-pos-body'];
     $encoding = ArrayUtil::findByKeyOrDefault($data, 'transfer-encoding', '');
     $content = self::decodeContent(substr($raw_mail, $start, $end - $start), $encoding);
     if (isset($data['content-disposition'])) {
         $filename = ArrayUtil::findByKeyOrDefault($data, 'disposition-filename', uniqid());
         $content_id = ArrayUtil::findByKeyOrNull($data, 'content-id');
         if ($content_id != null) {
             $content_id = "<{$content_id}>";
         }
         $email->addAttachment($filename, $content, $content_id);
     } else {
         if ($type == 'text/html') {
             $email->setHtmlBody($content);
         } else {
             if ($type == 'text/plain') {
                 $email->setTextBody($content);
             } else {
                 if (!StringUtil::startsWith($type, 'multipart/')) {
                     trigger_error("Ignore MIME part with unknown Content-Type {$type}. " . "Did you forget to specifcy Content-Disposition header?", E_USER_WARNING);
                 }
             }
         }
     }
 }