ILIAS  Release_4_0_x_branch Revision 61816
 All Data Structures Namespaces Files Functions Variables Groups Pages
Serializer.php
Go to the documentation of this file.
1 <?php
2 
5 {
6 
7  public function add($def, $config) {
8  if (!$this->checkDefType($def)) return;
10  if (file_exists($file)) return false;
11  if (!$this->_prepareDir($config)) return false;
12  return $this->_write($file, serialize($def));
13  }
14 
15  public function set($def, $config) {
16  if (!$this->checkDefType($def)) return;
17  $file = $this->generateFilePath($config);
18  if (!$this->_prepareDir($config)) return false;
19  return $this->_write($file, serialize($def));
20  }
21 
22  public function replace($def, $config) {
23  if (!$this->checkDefType($def)) return;
24  $file = $this->generateFilePath($config);
25  if (!file_exists($file)) return false;
26  if (!$this->_prepareDir($config)) return false;
27  return $this->_write($file, serialize($def));
28  }
29 
30  public function get($config) {
31  $file = $this->generateFilePath($config);
32  if (!file_exists($file)) return false;
33  return unserialize(file_get_contents($file));
34  }
35 
36  public function remove($config) {
37  $file = $this->generateFilePath($config);
38  if (!file_exists($file)) return false;
39  return unlink($file);
40  }
41 
42  public function flush($config) {
43  if (!$this->_prepareDir($config)) return false;
45  $dh = opendir($dir);
46  while (false !== ($filename = readdir($dh))) {
47  if (empty($filename)) continue;
48  if ($filename[0] === '.') continue;
49  unlink($dir . '/' . $filename);
50  }
51  }
52 
53  public function cleanup($config) {
54  if (!$this->_prepareDir($config)) return false;
56  $dh = opendir($dir);
57  while (false !== ($filename = readdir($dh))) {
58  if (empty($filename)) continue;
59  if ($filename[0] === '.') continue;
60  $key = substr($filename, 0, strlen($filename) - 4);
61  if ($this->isOld($key, $config)) unlink($dir . '/' . $filename);
62  }
63  }
64 
70  public function generateFilePath($config) {
71  $key = $this->generateKey($config);
72  return $this->generateDirectoryPath($config) . '/' . $key . '.ser';
73  }
74 
80  public function generateDirectoryPath($config) {
81  $base = $this->generateBaseDirectoryPath($config);
82  return $base . '/' . $this->type;
83  }
84 
91  $base = $config->get('Cache.SerializerPath');
92  $base = is_null($base) ? HTMLPURIFIER_PREFIX . '/HTMLPurifier/DefinitionCache/Serializer' : $base;
93  return $base;
94  }
95 
102  private function _write($file, $data) {
103  return file_put_contents($file, $data);
104  }
105 
110  private function _prepareDir($config) {
111  $directory = $this->generateDirectoryPath($config);
112  if (!is_dir($directory)) {
113  $base = $this->generateBaseDirectoryPath($config);
114  if (!is_dir($base)) {
115  trigger_error('Base directory '.$base.' does not exist,
116  please create or change using %Cache.SerializerPath',
117  E_USER_WARNING);
118  return false;
119  } elseif (!$this->_testPermissions($base)) {
120  return false;
121  }
122  $old = umask(0022); // disable group and world writes
123  mkdir($directory);
124  umask($old);
125  } elseif (!$this->_testPermissions($directory)) {
126  return false;
127  }
128  return true;
129  }
130 
135  private function _testPermissions($dir) {
136  // early abort, if it is writable, everything is hunky-dory
137  if (is_writable($dir)) return true;
138  if (!is_dir($dir)) {
139  // generally, you'll want to handle this beforehand
140  // so a more specific error message can be given
141  trigger_error('Directory '.$dir.' does not exist',
142  E_USER_WARNING);
143  return false;
144  }
145  if (function_exists('posix_getuid')) {
146  // POSIX system, we can give more specific advice
147  if (fileowner($dir) === posix_getuid()) {
148  // we can chmod it ourselves
149  chmod($dir, 0755);
150  return true;
151  } elseif (filegroup($dir) === posix_getgid()) {
152  $chmod = '775';
153  } else {
154  // PHP's probably running as nobody, so we'll
155  // need to give global permissions
156  $chmod = '777';
157  }
158  trigger_error('Directory '.$dir.' not writable, '.
159  'please chmod to ' . $chmod,
160  E_USER_WARNING);
161  } else {
162  // generic error message
163  trigger_error('Directory '.$dir.' not writable, '.
164  'please alter file permissions',
165  E_USER_WARNING);
166  }
167  return false;
168  }
169 
170 }
171 
172 // vim: et sw=4 sts=4