00001 <?php
00009 ClassFactory::importClass('CharsetDetector');
00010
00011
00012 require_once(
00013 Config::instance()->documentRoot().
00014 Config::instance()->geshiDir().
00015 '/geshi.php');
00016
00021 class FbFileType {
00022 private $fileName_;
00023 private $fileExtension_;
00024 private $geshiType_;
00025 private $isTextFile_;
00026 private $geshiTypesMap_ = Array(
00027 'asm' => 'asm',
00028 'bat' => 'dos',
00029 'c' => 'c',
00030 'cc' => 'c++',
00031 'config' => 'xml',
00032 'cpp' => 'c++',
00033 'cs' => 'csharp',
00034 'csproj' => 'xml',
00035 'css' => 'css',
00036 'cxx' => 'c++',
00037 'dtd' => 'html4strict',
00038 'h' => 'c++',
00039 'htc' => 'javascript',
00040 'html' => 'html4strict',
00041 'js' => 'javascript',
00042 'kdevelop' => 'xml',
00043 'lsp' => 'lisp',
00044 'php' => 'php',
00045 'qrc' => 'xml',
00046 'resx' => 'xml',
00047 'sh' => 'bash',
00048 'sql' => 'sql',
00049 'tex' => 'latex',
00050 'tpl' => 'smarty',
00051 'ui' => 'xml',
00052 'vcproj' => 'xml',
00053 'webprj' => 'xml',
00054 'xmi' => 'xml',
00055 'xml' => 'xml',
00056 'xsd' => 'xml',
00057 'xsl' => 'xml',
00058 );
00059 private $textFileExts_ = Array(
00060 'asc',
00061 'conf',
00062 'inc',
00063 'pl',
00064 'pro',
00065 'rc',
00066 'sln',
00067 'txt',
00068 );
00069 private $imgFilePatterns_ = Array(
00070 '\.png$',
00071 '\.jpe?g$',
00072 '\.gif$'
00073 );
00077 public function __construct($fileName) {
00078 $this->fileName_ = $fileName;
00079 $baseName = basename($this->fileName_);
00080
00081
00082 $tmp = Array();
00083 if (ereg('\.[^.]+$', $baseName, $tmp)) {
00084 $this->fileExtension_ = ereg_replace('^\.', '', $tmp[0]);
00085 $this->geshiType_ = @$this->geshiTypesMap_[$this->fileExtension_];
00086 }
00087
00088
00089 if (ereg('\/\.htaccess$', $fileName))
00090 $this->geshiType_ = 'apache';
00091
00092
00093 $this->isTextFile_ =
00094 isset($this->geshiType_) ||
00095 (false!==array_search($this->fileExtension_, $this->textFileExts_)) ||
00096 eregi('^readme$', $baseName) ||
00097 eregi('^makefile$', $baseName) ||
00098 eregi('^todo$', $baseName) ||
00099 eregi('^install$', $baseName) ||
00100 eregi('^head$', $baseName);
00101
00102 if ($this->isTextFile_) {
00103 $this->charsetDetector_ = new CharsetDetector;
00104 $fd = fopen($this->fileName_, "r");
00105 $head = fread($fd, CharsetDetector::DEFAULT_MAX_ANALYZED_LENGTH);
00106 fclose($fd);
00107 $this->charsetDetector_->analyze($head);
00108 }
00109 }
00114 public function canHighLight() {
00115 return isset($this->geshiType_);
00116 }
00121 public function isTextFile() {
00122 return $this->isTextFile_;
00123 }
00128 public function isDownloadDefault() {
00129 switch ($this->fileExtension_) {
00130 default:
00131 return false;
00132 }
00133 }
00138 public function isPdf() {
00139 return
00140 'pdf' == $this->fileExtension_;
00141 }
00142 public function getText() {
00143 if (!$this->isTextFile_)
00144 return false;
00145 else
00146 return $this->charsetDetector_->convertIfRelevant(file_get_contents($this->fileName_));
00147 }
00152 public function getDetectedCharset() {
00153 if (!$this->isTextFile_)
00154 return false;
00155 $analyzer = $this->charsetDetector_->getCharsetStreamAnalyzer();
00156 $weightMap = $analyzer->getCharsetWeightMap();
00157 $weightMapIterator = $weightMap->createSortedIterator();
00158 $top = $weightMapIterator->next();
00159 if (null==$top || $top->getRelWeight() < CharsetDetector::DEFAULT_MIN_RELEVANCE)
00160 return false;
00161 else
00162 return $top->getCharset();
00163 }
00168 public function highlight() {
00169 if (!$this->canHighLight())
00170 return false;
00171 $file = $this->getText();
00172 $geshi = new GeSHI($file, $this->geshiType_);
00173 return $geshi->parse_code();
00174 }
00175
00180 public function isImage() {
00181 foreach ($this->imgFilePatterns_ as $pattern)
00182 if (eregi($pattern, $this->fileName_))
00183 return true;
00184 return false;
00185 }
00186 };
00187 ?>