ILIAS  eassessment Revision 61809
 All Data Structures Namespaces Files Functions Variables Groups Pages
Magic.php
Go to the documentation of this file.
1 <?
2 
3 /*
4 
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
9 
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14 
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 */
19 
20 require "MagicConfig.php";
21 
22 class Magic
23 {
24  function Magic()
25  {
26  $this->config = new MagicConfig();
27  }
28 
29  function load_file($filename)
30  {
31  if (!($filehandle = @fopen($filename, "rb"))) return undef;
32  set_magic_quotes_runtime(0);
33  $text = fread($filehandle, filesize($filename));
34  set_magic_quotes_runtime(get_magic_quotes_gpc());
35  fclose($filehandle);
36  return $text;
37  }
38 
39  function _is_ascii($file)
40  {
41  $arr = unpack("C*", $file);
42  for($i=1; $i<sizeof($arr); $i++)
43  {
44  if ($arr[$i]<32 && $arr[$i]!=13 && $arr[$i]!=10) return false;
45  }
46  return true;
47  }
48 
50  {
51  // First attempt a match with the filename extension.
52  $extensionspl = explode($this->config->separator, $filename);
53  $extension = $extensionspl[sizeof($extensionspl)-1];
54  $langstr = $this->config->extensions[$extension];
55  // We may have a choice of languages, so check.
56  $langarr = explode("|", $langstr);
57 
58  // Got one language? Good!
59  if (sizeof($langarr)==1 && $langarr[0]!="") return $langstr;
60 
61  // Now the real magic starts :o) We may have a narrowed-down set of options from
62  // the tests above, which will speed things up a little.
63 
64  // Get one /big/ string.
65  $this->config->file = $this->load_file($filename);
66  if ($this->config->file == undef) return "illegal";
67  $this->is_ascii = $this->_is_ascii($this->config->file);
68 
69  if (isset($langstr))
70  {
71  // We don't know if all of the types are in our magic file, so filter out those that
72  // aren't.
73 
74  $outarray = array();
75  foreach($langarr as $lang)
76  {
77  if ($this->is_ascii && in_array($lang, $this->config->langfunctions))
78  array_push($outarray, $lang);
79  else if(in_array($lang, $this->config->binfunctions))
80  array_push($outarray, $lang);
81  }
82  $testarray = $outarray;
83  }
84  else
85  {
86  if ($this->is_ascii) $testarray = $this->config->langfunctions;
87  else $testarray = $this->config->binfunctions;
88  }
89  foreach($testarray as $test)
90  {
91  $func = "detect_".$test;
92  if ($this->config->$func()) return $test;
93  }
94  }
95 }
96 
97 ?>