/** * Returns an abstract of the file content. * * @return string */ public function getContentPreview() { if ($this->contentPreview === null) { $this->contentPreview = ''; if (ATTACHMENT_ENABLE_CONTENT_PREVIEW && !$this->isBinary && $this->attachmentSize != 0) { try { $file = new File(WCF_DIR . 'attachments/attachment-' . $this->attachmentID, 'rb'); $this->contentPreview = $file->read(2003); $file->close(); if (CHARSET == 'UTF-8') { if (!StringUtil::isASCII($this->contentPreview) && !StringUtil::isUTF8($this->contentPreview)) { $this->contentPreview = StringUtil::convertEncoding('ISO-8859-1', CHARSET, $this->contentPreview); } $this->contentPreview = StringUtil::substring($this->contentPreview, 0, 500); if (strlen($this->contentPreview) < $file->filesize()) { $this->contentPreview .= '...'; } } else { if (StringUtil::isUTF8($this->contentPreview)) { $this->contentPreview = ''; } else { $this->contentPreview = StringUtil::substring($this->contentPreview, 0, 500); if ($file->filesize() > 500) { $this->contentPreview .= '...'; } } } } catch (Exception $e) { } // ignore errors } } return $this->contentPreview; }
/** * Returns an abstract of the file content. * * @param array $data * @return string */ protected static function getContentPreview($data) { if (!ATTACHMENT_ENABLE_CONTENT_PREVIEW || $data['isBinary'] || $data['attachmentSize'] == 0) { return ''; } $content = ''; try { $file = new File(WCF_DIR . 'attachments/attachment-' . $data['attachmentID'], 'rb'); $content = $file->read(2003); $file->close(); if (CHARSET == 'UTF-8') { if (!StringUtil::isASCII($content) && !StringUtil::isUTF8($content)) { $content = StringUtil::convertEncoding('ISO-8859-1', CHARSET, $content); } $content = StringUtil::substring($content, 0, 500); if (strlen($content) < $file->filesize()) { $content .= '...'; } } else { if (StringUtil::isUTF8($content)) { return ''; } $content = StringUtil::substring($content, 0, 500); if ($file->filesize() > 500) { $content .= '...'; } } } catch (Exception $e) { } // ignore errors return $content; }
/** * Determines whether a file is text or binary by checking the first few bytes in the file. * The exact number of bytes is system dependent, but it is typically several thousand. * If every byte in that part of the file is non-null, considers the file to be text; * otherwise it considers the file to be binary. * * @param string $file * @return boolean */ public static function isBinary($file) { // open file $file = new File($file, 'rb'); // get block size $stat = $file->stat(); $blockSize = $stat['blksize']; if ($blockSize < 0) { $blockSize = 1024; } if ($blockSize > $file->filesize()) { $blockSize = $file->filesize(); } if ($blockSize <= 0) { return false; } // get bytes $block = $file->read($blockSize); return strlen($block) == 0 || preg_match_all('/\\x00/', $block, $match) > 0; }