<?php
/**
* @file FbFileType.class.php
* Definition of FbFileType class.
* @author Kamil Dudka <xdudka00@gmail.com>
* @ingroup FileBrowser
*/
ClassFactory::importClass('CharsetDetector');
// Import GeSHi
require_once(
Config::instance()->documentRoot().
Config::instance()->geshiDir().
'/geshi.php');
/**
* File name analyzator.
* @ingroup FileBrowser
*/
class FbFileType {
private $fileName_;
private $fileExtension_;
private $geshiType_;
private $isTextFile_;
private $geshiTypesMap_ = Array(
'asm' => 'asm',
'bat' => 'dos',
'c' => 'c',
'cc' => 'c++',
'config' => 'xml',
'cpp' => 'c++',
'cs' => 'csharp',
'csproj' => 'xml',
'css' => 'css',
'cxx' => 'c++',
'dtd' => 'html4strict',
'h' => 'c++',
'htc' => 'javascript',
'html' => 'html4strict',
'js' => 'javascript',
'kdevelop' => 'xml',
'lsp' => 'lisp',
'php' => 'php',
'qrc' => 'xml',
'resx' => 'xml',
'sh' => 'bash',
'sql' => 'sql',
'tex' => 'latex',
'tpl' => 'smarty',
'ui' => 'xml',
'vcproj' => 'xml',
'webprj' => 'xml',
'xmi' => 'xml',
'xml' => 'xml',
'xsd' => 'xml',
'xsl' => 'xml',
);
private $textFileExts_ = Array(
'asc',
'conf',
'inc',
'pl',
'pro',
'rc',
'sln',
'txt',
);
private $imgFilePatterns_ = Array(
'\.png$',
'\.jpe?g$',
'\.gif$'
);
/**
* @param fileName Full path to file to analyze.
*/
public function __construct($fileName) {
$this->fileName_ = $fileName;
$baseName = basename($this->fileName_);
// Guess file type using file extension
$tmp = Array();
if (ereg('\.[^.]+$', $baseName, $tmp)) {
$this->fileExtension_ = ereg_replace('^\.', '', $tmp[0]);
$this->geshiType_ = @$this->geshiTypesMap_[$this->fileExtension_];
}
// Other file types...
if (ereg('\/\.htaccess$', $fileName))
$this->geshiType_ = 'apache';
// Is file text-file?
$this->isTextFile_ =
isset($this->geshiType_) ||
(false!==array_search($this->fileExtension_, $this->textFileExts_)) ||
eregi('^readme$', $baseName) ||
eregi('^makefile$', $baseName) ||
eregi('^todo$', $baseName) ||
eregi('^install$', $baseName) ||
eregi('^head$', $baseName);
if ($this->isTextFile_) {
$this->charsetDetector_ = new CharsetDetector;
$fd = fopen($this->fileName_, "r");
$head = fread($fd, CharsetDetector::DEFAULT_MAX_ANALYZED_LENGTH);
fclose($fd);
$this->charsetDetector_->analyze($head);
}
}
/**
* Check if syntax-highlihting is available for file.
* @return Return true, if syntax-highlighting is available.
*/
public function canHighLight() {
return isset($this->geshiType_);
}
/**
* Check if file is human readable.
* @return Return true, if file seems as human readable.
*/
public function isTextFile() {
return $this->isTextFile_;
}
/**
* Check if download is default action for this type of file.
* @return Return true if download is default action.
*/
public function isDownloadDefault() {
switch ($this->fileExtension_) {
default:
return false;
}
}
/**
* Check if file type is PDF.
* @return Return true if file type is PDF.
*/
public function isPdf() {
return
'pdf' == $this->fileExtension_;
}
public function getText() {
if (!$this->isTextFile_)
return false;
else
return $this->charsetDetector_->convertIfRelevant(file_get_contents($this->fileName_));
}
/**
* @note getText() method always returns text in UTF-8.
* @return Return document's original charset.
*/
public function getDetectedCharset() {
if (!$this->isTextFile_)
return false;
$analyzer = $this->charsetDetector_->getCharsetStreamAnalyzer();
$weightMap = $analyzer->getCharsetWeightMap();
$weightMapIterator = $weightMap->createSortedIterator();
$top = $weightMapIterator->next();
if (null==$top || $top->getRelWeight() < CharsetDetector::DEFAULT_MIN_RELEVANCE)
return false;
else
return $top->getCharset();
}
/**
* @b Experimental! Syntax higlight file using GeSHi.
* @return Return syntax-highlighted file's source.
*/
public function highlight() {
if (!$this->canHighLight())
return false;
$file = $this->getText();
$geshi = new GeSHI($file, $this->geshiType_);
return $geshi->parse_code();
}
/**
* Check if file type is image.
* @return Return true if file type is image.
*/
public function isImage() {
foreach ($this->imgFilePatterns_ as $pattern)
if (eregi($pattern, $this->fileName_))
return true;
return false;
}
};
?>