• Main Page
  • Related Pages
  • Namespaces
  • Data Structures
  • Files
  • File List
  • Globals

syntax_highlight/php/Beautifier/Magic.php

Go to the documentation of this file.
00001 <?
00002 
00003 /*
00004 
00005 This program is free software; you can redistribute it and/or modify
00006 it under the terms of the GNU General Public License as published by
00007 the Free Software Foundation; either version 2 of the License, or
00008 (at your option) any later version.
00009 
00010 This program is distributed in the hope that it will be useful,
00011 but WITHOUT ANY WARRANTY; without even the implied warranty of
00012 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013 GNU General Public License for more details.
00014 
00015 You should have received a copy of the GNU General Public License
00016 along with this program; if not, write to the Free Software
00017 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00018 */
00019 
00020 require "MagicConfig.php";
00021 
00022 class Magic
00023 {
00024         function Magic()
00025         {
00026                 $this->config = new MagicConfig();
00027         }
00028         
00029         function load_file($filename)
00030         {
00031                 if (!($filehandle = @fopen($filename, "rb"))) return undef;
00032                 set_magic_quotes_runtime(0); 
00033                 $text = fread($filehandle, filesize($filename));
00034                 set_magic_quotes_runtime(get_magic_quotes_gpc());
00035                 fclose($filehandle);
00036                 return $text;
00037         }   
00038 
00039         function _is_ascii($file)
00040         {
00041                 $arr = unpack("C*", $file);
00042                 for($i=1; $i<sizeof($arr); $i++)
00043                 {       
00044                         if ($arr[$i]<32 && $arr[$i]!=13 && $arr[$i]!=10) return false;
00045                 }
00046                 return true;
00047         }
00048         
00049         function get_language($filename)
00050         {
00051                 // First attempt a match with the filename extension.
00052                 $extensionspl = explode($this->config->separator, $filename);
00053                 $extension = $extensionspl[sizeof($extensionspl)-1];
00054                 $langstr = $this->config->extensions[$extension];
00055                 // We may have a choice of languages, so check.
00056                 $langarr = explode("|", $langstr);
00057                 
00058                 // Got one language? Good!
00059                 if (sizeof($langarr)==1 && $langarr[0]!="") return $langstr;
00060                 
00061                 // Now the real magic starts :o) We may have a narrowed-down set of options from 
00062                 // the tests above, which will speed things up a little.
00063                 
00064                 // Get one /big/ string.
00065                 $this->config->file = $this->load_file($filename);
00066                 if ($this->config->file == undef) return "illegal";
00067                 $this->is_ascii = $this->_is_ascii($this->config->file);
00068 
00069                 if (isset($langstr)) 
00070                 {
00071                         // We don't know if all of the types are in our magic file, so filter out those that
00072                         // aren't.
00073                         
00074                         $outarray = array();
00075                         foreach($langarr as $lang)
00076                         {
00077                                 if ($this->is_ascii && in_array($lang, $this->config->langfunctions))
00078                                         array_push($outarray, $lang);
00079                                 else if(in_array($lang, $this->config->binfunctions))
00080                                         array_push($outarray, $lang);
00081                         }
00082                         $testarray = $outarray;
00083                 }
00084                 else 
00085                 {
00086                         if ($this->is_ascii) $testarray = $this->config->langfunctions;
00087                                 else $testarray = $this->config->binfunctions;
00088                 }
00089                 foreach($testarray as $test)
00090                 {
00091                         $func = "detect_".$test;
00092                         if ($this->config->$func()) return $test;
00093                 }
00094         }
00095 }
00096 
00097 ?>

Generated on Fri Dec 13 2013 09:06:39 for ILIAS Release_3_4_x_branch .rev 46804 by  doxygen 1.7.1