ILIAS  release_5-1 Revision 5.0.0-5477-g43f3e3fab5f
class.ilWACToken.php
Go to the documentation of this file.
1 <?php
2 require_once('class.ilWACSignedPath.php');
3 
10 class ilWACToken {
11 
12  const DEBUG = false;
18  protected static $SALT = '';
22  protected $session_id = '';
26  protected $timestamp = 0;
30  protected $ip = '';
34  protected $token = '';
38  protected $path = '';
42  protected $id = '';
46  protected $client = '';
50  protected $ttl = 0;
51 
52 
60  public function __construct($path, $client, $timestamp = null, $ttl = null) {
61  $this->setClient($client);
62  $parts = parse_url($path);
63  $this->setPath($parts['path']);
64  $session_id = session_id();
65  $this->setSessionId($session_id ? $session_id : '-');
66  if (isset($_SERVER['REMOTE_ADDR'])) {
67  $this->setIp($_SERVER['REMOTE_ADDR']);
68  }
69  $this->setTimestamp($timestamp ? $timestamp : time());
71  $this->setTTL($ttl); // since we do not know the type at this poit we choose the shorter duration for security reasons
72  $this->generateToken();
73  self::isDEBUG() ? $this->setId($this->getPath()) : $this->setId(md5($this->getPath()));
74  }
75 
76 
80  protected static function isDEBUG() {
82  }
83 
84 
85  public function generateToken() {
86  $this->initSalt();
87  $token = implode('-', array( self::getSALT(), $this->getIp(), $this->getClient(), $this->getTimestamp(), $this->getTTL() ));
88  $token = self::isDEBUG() ? $token : sha1($token);
89  $this->setToken($token);
90  }
91 
92 
96  protected function getSaltFilePath() {
97  $salt_file = './data/wacsalt.php';
98 
99  return $salt_file;
100  }
101 
102 
103  protected function initSalt() {
104  if (self::getSALT()) {
105  return true;
106  }
107  $salt = null;
108  if (is_file($this->getSaltFilePath())) {
109  include($this->getSaltFilePath());
110  }
111  self::setSALT($salt);
112  if (!$salt) {
113  $this->generateSaltFile();
114  }
115  }
116 
117 
118  protected function generateSaltFile() {
119  if (is_file($this->getSaltFilePath())) {
120  unlink($this->getSaltFilePath());
121  }
122  $template = file_get_contents('./Services/WebAccessChecker/wacsalt.php.template');
123  $salt = md5(time() * rand(1000, 9999) . $this->getSaltFilePath());
124  self::setSALT($salt);
125  $template = str_replace('INSERT_SALT', $salt, $template);
126  if (is_writable(dirname($this->getSaltFilePath()))) {
127  file_put_contents($this->getSaltFilePath(), $template);
128  } else {
130  }
131  }
132 
133 
137  public function getSessionId() {
138  return $this->session_id;
139  }
140 
141 
145  public function setSessionId($session_id) {
146  $this->session_id = $session_id;
147  }
148 
149 
153  public function getTimestamp() {
154  return $this->timestamp;
155  }
156 
157 
161  public function setTimestamp($timestamp) {
162  $this->timestamp = $timestamp;
163  }
164 
165 
169  public function getIp() {
170  return $this->ip;
171  }
172 
173 
177  public function setIp($ip) {
178  $this->ip = $ip;
179  }
180 
181 
185  public function getToken() {
186  return $this->token;
187  }
188 
189 
193  public function setToken($token) {
194  $this->token = $token;
195  }
196 
197 
201  public function getPath() {
202  return $this->path;
203  }
204 
205 
209  public function setPath($path) {
210  $this->path = $path;
211  }
212 
213 
217  public function getId() {
218  return $this->id;
219  }
220 
221 
225  public function setId($id) {
226  $this->id = $id;
227  }
228 
229 
233  public static function getSALT() {
234  return self::$SALT;
235  }
236 
237 
241  public static function setSALT($SALT) {
242  self::$SALT = $SALT;
243  }
244 
245 
249  public function getClient() {
250  return $this->client;
251  }
252 
253 
257  public function setClient($client) {
258  $this->client = $client;
259  }
260 
261 
265  public function getTTL() {
266  return $this->ttl;
267  }
268 
269 
273  public function setTTL($ttl) {
274  $this->ttl = $ttl;
275  }
276 }
static setSALT($SALT)
if((!isset($_SERVER['DOCUMENT_ROOT'])) OR(empty($_SERVER['DOCUMENT_ROOT']))) $_SERVER['DOCUMENT_ROOT']
Class ilWACException.
__construct($path, $client, $timestamp=null, $ttl=null)
ilWACToken constructor.
setTimestamp($timestamp)
const DEBUG
static getSALT()
Class ilWACToken.
static isDEBUG()
setSessionId($session_id)
static getTokenMaxLifetimeInSeconds()
setClient($client)