示例#1
0
 /**
  * Parse RFC 2397 formatted data strings. Format according to the RFC
  * should look something like:
  *
  * data:[type/subtype][;charset=utf-8][;base64],data[#filename]
  *
  * Parameters:
  * $data - (string) RFC2397 formatted data string
  * $output_encoding - (string:optional) Character set the input data
  *      should be encoded to.
  * $always_convert - (bool|default:true) If the input data string does
  *      not specify an input encding, assume iso-8859-1. If this flag is
  *      set, the output will always be transcoded to the declared
  *      output_encoding, if set.
  *
  * Returs:
  * array (data=>parsed and transcoded data string, type=>MIME type
  * declared in the data string or text/plain otherwise)
  *
  * References:
  * http://www.ietf.org/rfc/rfc2397.txt
  */
 static function parse($data, $output_encoding = false, $always_convert = true)
 {
     if (substr($data, 0, 5) != "data:") {
         return array('data' => $data, 'type' => 'text/plain');
     }
     $data = substr($data, 5);
     list($meta, $contents) = explode(",", $data, 2);
     if ($meta) {
         list($type, $extra) = explode(";", $meta, 2);
     } else {
         $extra = '';
     }
     if (!isset($type) || !$type) {
         $type = 'text/plain';
     }
     $parameters = explode(";", $extra);
     # Handle 'charset' hint in $extra, such as
     # data:text/plain;charset=iso-8859-1,Blah
     # Convert to utf-8 since it's the encoding scheme for the database.
     $charset = $always_convert ? 'iso-8859-1' : false;
     foreach ($parameters as $p) {
         list($param, $value) = explode('=', $extra);
         if ($param == 'charset') {
             $charset = $value;
         } elseif ($param == 'base64') {
             $contents = base64_decode($contents);
         }
     }
     if ($output_encoding && $charset) {
         $contents = Format::encode($contents, $charset, $output_encoding);
     }
     return array('data' => $contents, 'type' => $type);
 }
示例#2
0
 function translate($string)
 {
     if ($this->short_circuit) {
         return $string;
     }
     // Caching enabled, get translated string from cache
     if (isset($this->cache_translations[$string])) {
         $string = $this->cache_translations[$string];
     }
     if (!$this->encode) {
         return $string;
     }
     return Format::encode($string, 'utf-8', $this->charset);
 }
示例#3
0
 function mimedecode($text, $encoding = 'UTF-8')
 {
     if (function_exists('imap_mime_header_decode') && ($parts = imap_mime_header_decode($text))) {
         $str = '';
         foreach ($parts as $part) {
             $str .= Format::encode($part->text, $part->charset, $encoding);
         }
         $text = $str;
     } elseif (function_exists('iconv_mime_decode')) {
         $text = iconv_mime_decode($text, 0, $encoding);
     } elseif (!strcasecmp($encoding, 'utf-8') && function_exists('imap_utf8')) {
         $text = imap_utf8($text);
     }
     return $text;
 }
示例#4
0
文件: Mail.php 项目: iHunt101/phlite
 function decode($what, $errors = false)
 {
     if (function_exists('imap_mime_header_decode') && ($parts = imap_mime_header_decode($text))) {
         $str = '';
         foreach ($parts as $part) {
             $str .= Format::encode($part->text, $part->charset, $encoding);
         }
         return $str;
     } elseif ($text[0] == '=' && function_exists('iconv_mime_decode')) {
         return iconv_mime_decode($text, 0, $encoding);
         // TODO: Use a pure-PHP version to perform the decoding
     } elseif (!strcasecmp($encoding, 'utf-8') && function_exists('imap_utf8')) {
         return imap_utf8($text);
     }
     return $text;
 }
示例#5
0
 function getBody($type = 'text/html', $encoding = false)
 {
     // First select the body
     switch ($type) {
         case 'text/html':
             $body = $this->BodyHtml;
             break;
         default:
             return false;
     }
     // Figure out the source encoding (§5.1.2)
     $charset = false;
     if (@$this->OemCodepage) {
         $charset = 'cp' . $this->OemCodepage;
     } elseif (@$this->InternetCodepage) {
         $charset = 'cp' . $this->InternetCodepage;
     }
     // Transcode it
     if ($encoding && $charset) {
         $body = Format::encode($body, $charset, $encoding);
     }
     return $body;
 }
 function mime_decode($text, $encoding = 'utf-8')
 {
     if (function_exists('mb_detect_encoding')) {
         if (($src_enc = mb_detect_encoding($text)) && strcasecmp($src_enc, 'ASCII') !== 0) {
             return Format::encode($text, $src_enc, $encoding);
         }
     }
     $str = '';
     $parts = imap_mime_header_decode($text);
     foreach ($parts as $part) {
         $str .= $this->mime_encode($part->text, $part->charset, $encoding);
     }
     return $str ? $str : imap_utf8($text);
 }
示例#7
0
文件: file.php 项目: gizur/osticket
 function run($args, $options)
 {
     Bootstrap::connect();
     osTicket::start();
     switch ($args['action']) {
         case 'backends':
             // List configured backends
             foreach (FileStorageBackend::allRegistered() as $char => $bk) {
                 print "{$char} -- {$bk::$desc} ({$bk})\n";
             }
             break;
         case 'list':
             // List files matching criteria
             // ORM would be nice!
             $files = FileModel::objects();
             $this->_applyCriteria($options, $files);
             foreach ($files as $f) {
                 printf("% 5d %s % 8d %s % 16s %s\n", $f->id, $f->bk, $f->size, $f->created, $f->type, $f->name);
                 if ($f->attrs) {
                     printf("        %s\n", $f->attrs);
                 }
             }
             break;
         case 'dump':
             $files = FileModel::objects();
             $this->_applyCriteria($options, $files);
             if ($files->count() != 1) {
                 $this->fail('Criteria must select exactly 1 file');
             }
             if (($f = AttachmentFile::lookup($files[0]->id)) && ($bk = $f->open())) {
                 $bk->passthru();
             }
             break;
         case 'load':
             // Load file content from STDIN
             $files = FileModel::objects();
             $this->_applyCriteria($options, $files);
             if ($files->count() != 1) {
                 $this->fail('Criteria must select exactly 1 file');
             }
             $f = AttachmentFile::lookup($files[0]->id);
             try {
                 if ($bk = $f->open()) {
                     $bk->unlink();
                 }
             } catch (Exception $e) {
             }
             if ($options['to']) {
                 $bk = FileStorageBackend::lookup($options['to'], $f);
             } else {
                 // Use the system default
                 $bk = AttachmentFile::getBackendForFile($f);
             }
             $type = false;
             $signature = '';
             $finfo = new finfo(FILEINFO_MIME_TYPE);
             if ($options['file'] && $options['file'] != '-') {
                 if (!file_exists($options['file'])) {
                     $this->fail($options['file'] . ': Cannot open file');
                 }
                 if (!$bk->upload($options['file'])) {
                     $this->fail('Unable to upload file contents to backend');
                 }
                 $type = $finfo->file($options['file']);
                 list(, $signature) = AttachmentFile::_getKeyAndHash($options['file'], true);
             } else {
                 $stream = fopen('php://stdin', 'rb');
                 while ($block = fread($stream, $bk->getBlockSize())) {
                     if (!$bk->write($block)) {
                         $this->fail('Unable to send file contents to backend');
                     }
                     if (!$type) {
                         $type = $finfo->buffer($block);
                     }
                 }
                 if (!$bk->flush()) {
                     $this->fail('Unable to commit file contents to backend');
                 }
             }
             // TODO: Update file metadata
             $sql = 'UPDATE ' . FILE_TABLE . ' SET bk=' . db_input($bk->getBkChar()) . ', created=CURRENT_TIMESTAMP' . ', type=' . db_input($type) . ', signature=' . db_input($signature) . ' WHERE id=' . db_input($f->getId());
             if (!db_query($sql) || db_affected_rows() != 1) {
                 $this->fail('Unable to update file metadata');
             }
             $this->stdout->write("Successfully saved contents\n");
             break;
         case 'migrate':
             if (!$options['to']) {
                 $this->fail('Please specify a target backend for migration');
             }
             if (!FileStorageBackend::isRegistered($options['to'])) {
                 $this->fail('Target backend is not installed. See `backends` action');
             }
             $files = FileModel::objects();
             $this->_applyCriteria($options, $files);
             $count = 0;
             foreach ($files as $m) {
                 $f = AttachmentFile::lookup($m->id);
                 if ($f->getBackend() == $options['to']) {
                     continue;
                 }
                 if ($options['verbose']) {
                     $this->stdout->write('Migrating ' . $m->name . "\n");
                 }
                 try {
                     if (!$f->migrate($options['to'])) {
                         $this->stderr->write('Unable to migrate ' . $m->name . "\n");
                     } else {
                         $count++;
                     }
                 } catch (IOException $e) {
                     $this->stderr->write('IOError: ' . $e->getMessage());
                 }
             }
             $this->stdout->write("Migrated {$count} files\n");
             break;
             /**
              * export
              *
              * Export file contents to a stream file. The format of the stream
              * will be a continuous stream of file information in the following
              * format:
              *
              * AFIL<meta-length><data-length><meta><data>EOF\x1c
              *
              * Where
              *   A              is the version code of the export
              *   "FIL"          is the literal text 'FIL'
              *   meta-length    is 'V' packed header length (bytes)
              *   data-length    is 'V' packed data length (bytes)
              *   meta           is the %file record, php serialized
              *   data           is the raw content of the file
              *   "EOF"          is the literal text 'EOF'
              *   \x1c           is an ASCII 0x1c byte (file separator)
              *
              * Options:
              * --file       File to which to direct the stream output, default
              *              is stdout
              */
         /**
          * export
          *
          * Export file contents to a stream file. The format of the stream
          * will be a continuous stream of file information in the following
          * format:
          *
          * AFIL<meta-length><data-length><meta><data>EOF\x1c
          *
          * Where
          *   A              is the version code of the export
          *   "FIL"          is the literal text 'FIL'
          *   meta-length    is 'V' packed header length (bytes)
          *   data-length    is 'V' packed data length (bytes)
          *   meta           is the %file record, php serialized
          *   data           is the raw content of the file
          *   "EOF"          is the literal text 'EOF'
          *   \x1c           is an ASCII 0x1c byte (file separator)
          *
          * Options:
          * --file       File to which to direct the stream output, default
          *              is stdout
          */
         case 'export':
             $files = FileModel::objects();
             $this->_applyCriteria($options, $files);
             if (!$options['file'] || $options['file'] == '-') {
                 $options['file'] = 'php://stdout';
             }
             if (!($stream = fopen($options['file'], 'wb'))) {
                 $this->fail($options['file'] . ': Unable to open file for export stream');
             }
             foreach ($files as $m) {
                 $f = AttachmentFile::lookup($m->id);
                 if ($options['verbose']) {
                     $this->stderr->write($m->name . "\n");
                 }
                 // TODO: Log %attachment and %ticket_attachment entries
                 $info = array('file' => $f->getInfo());
                 $header = serialize($info);
                 fwrite($stream, 'AFIL' . pack('VV', strlen($header), $f->getSize()));
                 fwrite($stream, $header);
                 $FS = $f->open();
                 while ($block = $FS->read()) {
                     fwrite($stream, $block);
                 }
                 fwrite($stream, "EOF");
             }
             fclose($stream);
             break;
             /**
              * import
              *
              * Import a collection of file contents exported by the `export`.
              * See the export function above for details about the stream
              * format.
              *
              * Options:
              * --file       File from which to read the export stream, default
              *              is stdin
              * --to         Backend to receive the contents (@see `backends`)
              * --verbose    Show file names while importing
              */
         /**
          * import
          *
          * Import a collection of file contents exported by the `export`.
          * See the export function above for details about the stream
          * format.
          *
          * Options:
          * --file       File from which to read the export stream, default
          *              is stdin
          * --to         Backend to receive the contents (@see `backends`)
          * --verbose    Show file names while importing
          */
         case 'import':
             if (!$options['file'] || $options['file'] == '-') {
                 $options['file'] = 'php://stdin';
             }
             if (!($stream = fopen($options['file'], 'rb'))) {
                 $this->fail($options['file'] . ': Unable to open import stream');
             }
             while (true) {
                 // Read the file header
                 // struct file_data_header {
                 //   char[4] marker; // Four chars, 'AFIL'
                 //   int     lenMeta;
                 //   int     lenData;
                 // };
                 if (!($header = fread($stream, 12))) {
                     break;
                 }
                 // EOF
                 list(, $mark, $hlen, $dlen) = unpack('V3', $header);
                 // AFIL written as little-endian 4-byte int is 0x4c4946xx (LIFA),
                 // where 'A' is the version code of the export
                 $version = $mark & 0xff;
                 if ($mark >> 8 != 0x4c4946) {
                     $this->fail('Bad file record');
                 }
                 // Read the header
                 $header = fread($stream, $hlen);
                 if (strlen($header) != $hlen) {
                     $this->fail('Short read getting header info');
                 }
                 $header = unserialize($header);
                 if (!$header) {
                     $this->fail('Unable to decipher file header');
                 }
                 // Find or create the file record
                 $finfo = $header['file'];
                 // TODO: Consider the $version code
                 $f = AttachmentFile::lookup($finfo['id']);
                 if ($f) {
                     // Verify file information
                     if ($f->getSize() != $finfo['size'] || $f->getSignature() != $finfo['signature']) {
                         $this->fail(sprintf('%s: File data does not match existing file record', $finfo['name']));
                     }
                     // Drop existing file contents, if any
                     try {
                         if ($bk = $f->open()) {
                             $bk->unlink();
                         }
                     } catch (Exception $e) {
                     }
                 } else {
                     $fm = FileModel::create($finfo);
                     if (!$fm->save() || !($f = AttachmentFile::lookup($fm->id))) {
                         $this->fail(sprintf('%s: Unable to create new file record', $finfo['name']));
                     }
                 }
                 // Determine the backend to recieve the file contents
                 if ($options['to']) {
                     $bk = FileStorageBackend::lookup($options['to'], $f);
                 } else {
                     $bk = AttachmentFile::getBackendForFile($f);
                 }
                 if ($options['verbose']) {
                     $this->stdout->write('Importing ' . $f->getName() . "\n");
                 }
                 // Write file contents to the backend
                 $md5 = hash_init('md5');
                 $sha1 = hash_init('sha1');
                 $written = 0;
                 // Handle exceptions by dropping imported file contents and
                 // then returning the error to the error output stream.
                 try {
                     while ($dlen > 0) {
                         $read_size = min($dlen, $bk->getBlockSize());
                         $contents = '';
                         // reading from the stream will likely return an amount of
                         // data different from the backend requested block size. Loop
                         // until $read_size bytes are recieved.
                         while ($read_size > 0 && ($block = fread($stream, $read_size))) {
                             $contents .= $block;
                             $read_size -= strlen($block);
                         }
                         if ($read_size != 0) {
                             // short read
                             throw new Exception(sprintf('%s: Some contents are missing from the stream', $f->getName()));
                         }
                         // Calculate MD5 and SHA1 hashes of the file to verify
                         // contents after successfully written to backend
                         if (!$bk->write($contents)) {
                             throw new Exception('Unable to send file contents to backend');
                         }
                         hash_update($md5, $contents);
                         hash_update($sha1, $contents);
                         $dlen -= strlen($contents);
                         $written += strlen($contents);
                     }
                     // Some backends cannot handle flush() without a
                     // corresponding write() call.
                     if ($written && !$bk->flush()) {
                         throw new Exception('Unable to commit file contents to backend');
                     }
                     // Check the signature hash
                     if ($finfo['signature']) {
                         $md5 = base64_encode(hash_final($md5, true));
                         $sha1 = base64_encode(hash_final($sha1, true));
                         $sig = str_replace(array('=', '+', '/'), array('', '-', '_'), substr($sha1, 0, 16) . substr($md5, 0, 16));
                         if ($sig != $finfo['signature']) {
                             throw new Exception(sprintf('%s: Signature verification failed', $f->getName()));
                         }
                     }
                     // Update file to record current backend
                     $sql = 'UPDATE ' . FILE_TABLE . ' SET bk=' . db_input($bk->getBkChar()) . ' WHERE id=' . db_input($f->getId());
                     if (!db_query($sql) || db_affected_rows() != 1) {
                         return false;
                     }
                 } catch (Exception $ex) {
                     if ($bk) {
                         $bk->unlink();
                     }
                     $this->fail($ex->getMessage());
                 }
                 // Read file record footer
                 $footer = fread($stream, 4);
                 if (strlen($footer) != 4) {
                     $this->fail('Unable to read file EOF marker');
                 }
                 list(, $footer) = unpack('N', $footer);
                 // Footer should be EOF\x1c as an int
                 if ($footer != 0x454f461c) {
                     $this->fail('Incorrect file EOF marker');
                 }
             }
             break;
         case 'zip':
             // Create a temporary ZIP file
             $files = FileModel::objects();
             $this->_applyCriteria($options, $files);
             if (!$options['file']) {
                 $this->fail('Please specify zip file with `-f`');
             }
             $zip = new ZipArchive();
             if (true !== ($reason = $zip->open($options['file'], ZipArchive::CREATE))) {
                 $this->fail($reason . ': Unable to create zip file');
             }
             foreach ($files as $m) {
                 $f = AttachmentFile::lookup($m->id);
                 if ($options['verbose']) {
                     $this->stderr->write($m->name . "\n");
                 }
                 $name = Format::encode(sprintf('%d-%s', $f->getId(), $f->getName()), 'utf-8', 'cp437');
                 $zip->addFromString($name, $f->getData());
             }
             $zip->close();
             break;
         case 'expunge':
             $files = FileModel::objects();
             $this->_applyCriteria($options, $files);
             foreach ($files as $m) {
                 // Drop associated attachment links
                 $m->tickets->expunge();
                 $f = AttachmentFile::lookup($m->id);
                 // Drop file contents
                 if ($bk = $f->open()) {
                     $bk->unlink();
                 }
                 // Drop file record
                 $f->delete();
             }
     }
 }
示例#8
0
 /**
  * Mime header value decoder. Usually unicode characters are encoded
  * according to RFC-2047. This function will decode RFC-2047 encoded
  * header values as well as detect headers which are not encoded.
  *
  * Caveats:
  * If headers contain non-ascii characters the result of this function
  * is completely undefined. If osTicket is corrupting your email
  * headers, your mail software is not encoding the header text
  * correctly.
  *
  * Returns:
  * Header value, transocded to UTF-8
  */
 function mime_decode($text, $encoding = 'utf-8')
 {
     // Handle poorly or completely un-encoded header values (
     if (function_exists('mb_detect_encoding')) {
         if (($src_enc = mb_detect_encoding($text)) && strcasecmp($src_enc, 'ASCII') !== 0) {
             return Format::encode($text, $src_enc, $encoding);
         }
     }
     // Handle ASCII text and RFC-2047 encoding
     $str = '';
     $parts = imap_mime_header_decode($text);
     foreach ($parts as $part) {
         $str .= $this->mime_encode($part->text, $part->charset, $encoding);
     }
     return $str ? $str : imap_utf8($text);
 }
 function mime_encode($text, $charset = null, $encoding = 'utf-8')
 {
     return Format::encode($text, $charset, $encoding);
 }