Example #1
0
 public static function convertContent($content, $headers, $failOnNoEncode = FALSE)
 {
     $data = NULL;
     if ($headers->has('content-transfer-encoding')) {
         $encoding = $headers->get('content-transfer-encoding')->getFieldValue();
         if ($encoding === 'base64') {
             $data = preg_replace('~[^a-zA-Z0-9+=/]+~s', '', $content);
             $data = base64_decode($data);
         } elseif ($encoding === '7bit') {
             $data = File::decode7bit($content);
         } elseif ($encoding === '8bit') {
             $data = $content;
         } elseif ($encoding === 'quoted-printable') {
             $data = quoted_printable_decode($content);
         }
     }
     if (is_null($data)) {
         if ($failOnNoEncode === TRUE) {
             throw new RuntimeException("Missing Content-Transfer-Encoding header. " . "Unsure about how to decode.");
         }
         // Depending on file extension, we may need to base64_decode
         // the data.
         $decoded = base64_decode($content, TRUE);
         $data = $decoded ?: $content;
     }
     return $data;
 }