Exemplo n.º 1
0
 /**
  * @example
  * $content = array(
  *            'foo' => 'bar',
  *            'file' => array(
  *                  'content' => 'file content is simple text', // or path, mantatory
  *                  'name' => 'filename.txt', // optional
  *                  'type' => 'text/plain' // optional
  *            ));
  *
  * @param string $url
  * @param array $content
  * @return CUrl - call ->exec()
  */
 static function postUploadFile($url, array $content)
 {
     $eol = "\r\n";
     $boundary = md5(microtime(TRUE));
     $curl = new CUrl($url);
     $curl->setOptions(array(CURLOPT_SSL_VERIFYPEER => false, CURLOPT_SSL_VERIFYHOST => 1, CURLOPT_RETURNTRANSFER => 1, CURLOPT_HEADER => 0, CURLOPT_POST => 1, CURLOPT_VERBOSE => 1, CURLOPT_HTTPHEADER => array('Content-Type: multipart/form-data; charset=utf-8; boundary="' . $boundary . '"')));
     $body = '';
     foreach ($content as $name => $value) {
         $body .= '--' . $boundary . $eol;
         $body .= 'Content-Disposition: form-data;name="' . $name . '"';
         if (is_array($value)) {
             if (file_exists($value['content'])) {
                 $type = MimeTypeDetector::fromFile($value['content']);
                 $content = file_get_contents($value['content']);
             } else {
                 $type = MimeTypeDetector::fromString($content = $value['content']);
             }
             // is file
             $body .= '; filename="' . (isset($value['name']) ? $value['name'] : date('YmdHis')) . '"' . $eol;
             $body .= 'Content-Type: ' . (isset($value['type']) ? $value['type'] : $type) . $eol;
             if (preg_match('~base64~i', $content)) {
                 $body .= 'Content-Transfer-Encoding: base64' . $eol;
                 $content = preg_replace('~^base64~i', '', $content);
             }
             $body .= $eol;
             // $body .= chunk_split(base64_encode($content)); // RFC 2045
             $body .= trim($content) . $eol;
         } else {
             $body .= $eol . $eol . $value . $eol;
         }
     }
     $body .= "--{$boundary}--" . $eol . $eol;
     $curl->setopt(CURLOPT_POSTFIELDS, $body);
     return $curl;
 }
Exemplo n.º 2
0
 /**
  * Returns the MIME content type of an uploaded file.
  * @return string
  */
 public function getContentType()
 {
     if ($this->isOk() && $this->type === NULL) {
         $this->type = MimeTypeDetector::fromFile($this->tmpName);
     }
     return $this->type;
 }
Exemplo n.º 3
0
 /**
  * The data: URI generator.
  * @param  string
  * @param  string
  * @return string
  */
 public static function dataStream($data, $type = NULL)
 {
     if ($type === NULL) {
         $type = MimeTypeDetector::fromString($data);
     }
     return 'data:' . ($type ? "{$type};" : '') . 'base64,' . base64_encode($data);
 }
Exemplo n.º 4
0
 /**
  * Get format from the image stream in the string.
  * @param  string
  * @return mixed  detected image format
  */
 public static function getFormatFromString($s)
 {
     $types = array('image/jpeg' => self::JPEG, 'image/gif' => self::GIF, 'image/png' => self::PNG);
     $type = MimeTypeDetector::fromString($s);
     return isset($types[$type]) ? $types[$type] : NULL;
 }
Exemplo n.º 5
0
 /**
  * Creates file MIME part.
  * @return MailMimePart
  */
 private function createAttachment($file, $content, $contentType, $disposition)
 {
     $part = new MailMimePart();
     if ($content === NULL) {
         $content = file_get_contents($file);
         if ($content === FALSE) {
             throw new FileNotFoundException("Unable to read file '{$file}'.");
         }
     } else {
         $content = (string) $content;
     }
     $part->setBody($content);
     $part->setContentType($contentType ? $contentType : MimeTypeDetector::fromString($content));
     $part->setEncoding(preg_match('#(multipart|message)/#A', $contentType) ? self::ENCODING_8BIT : self::ENCODING_BASE64);
     $part->setHeader('Content-Disposition', $disposition . '; filename="' . Strings::fixEncoding(basename($file)) . '"');
     return $part;
 }