ILIAS  release_5-3 Revision v5.3.23-19-g915713cf615
Magic.php
Go to the documentation of this file.
1 <?php
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  public function Magic()
25  {
26  $this->config = new MagicConfig();
27  }
28 
29  public function load_file($filename)
30  {
31  if (!($filehandle = @fopen($filename, "rb"))) {
32  return undef;
33  }
34  set_magic_quotes_runtime(0);
35  $text = fread($filehandle, filesize($filename));
36  set_magic_quotes_runtime(get_magic_quotes_gpc());
37  fclose($filehandle);
38  return $text;
39  }
40 
41  public function _is_ascii($file)
42  {
43  $arr = unpack("C*", $file);
44  for ($i=1; $i<sizeof($arr); $i++) {
45  if ($arr[$i]<32 && $arr[$i]!=13 && $arr[$i]!=10) {
46  return false;
47  }
48  }
49  return true;
50  }
51 
52  public function get_language($filename)
53  {
54  // First attempt a match with the filename extension.
55  $extensionspl = explode($this->config->separator, $filename);
56  $extension = $extensionspl[sizeof($extensionspl)-1];
57  $langstr = $this->config->extensions[$extension];
58  // We may have a choice of languages, so check.
59  $langarr = explode("|", $langstr);
60 
61  // Got one language? Good!
62  if (sizeof($langarr)==1 && $langarr[0]!="") {
63  return $langstr;
64  }
65 
66  // Now the real magic starts :o) We may have a narrowed-down set of options from
67  // the tests above, which will speed things up a little.
68 
69  // Get one /big/ string.
70  $this->config->file = $this->load_file($filename);
71  if ($this->config->file == undef) {
72  return "illegal";
73  }
74  $this->is_ascii = $this->_is_ascii($this->config->file);
75 
76  if (isset($langstr)) {
77  // We don't know if all of the types are in our magic file, so filter out those that
78  // aren't.
79 
80  $outarray = array();
81  foreach ($langarr as $lang) {
82  if ($this->is_ascii && in_array($lang, $this->config->langfunctions)) {
83  array_push($outarray, $lang);
84  } elseif (in_array($lang, $this->config->binfunctions)) {
85  array_push($outarray, $lang);
86  }
87  }
88  $testarray = $outarray;
89  } else {
90  if ($this->is_ascii) {
91  $testarray = $this->config->langfunctions;
92  } else {
93  $testarray = $this->config->binfunctions;
94  }
95  }
96  foreach ($testarray as $test) {
97  $func = "detect_" . $test;
98  if ($this->config->$func()) {
99  return $test;
100  }
101  }
102  }
103 }
get_language($filename)
Definition: Magic.php:52
load_file($filename)
Definition: Magic.php:29
$text
Definition: errorreport.php:18
Definition: Magic.php:22
Create styles array
The data for the language used.
Magic()
Definition: Magic.php:24
$i
Definition: disco.tpl.php:19
if(!file_exists("$old.txt")) if($old===$new) if(file_exists("$new.txt")) $file
_is_ascii($file)
Definition: Magic.php:41
$test
Definition: Utf8Test.php:84