ILIAS  Release_4_1_x_branch Revision 61804
 All Data Structures Namespaces Files Functions Variables Groups Pages
class.ilChatServerConfig.php
Go to the documentation of this file.
1 <?php
2 /*
3  +-----------------------------------------------------------------------------+
4  | ILIAS open source |
5  +-----------------------------------------------------------------------------+
6  | Copyright (c) 1998-2009 ILIAS open source, University of Cologne |
7  | |
8  | This program is free software; you can redistribute it and/or |
9  | modify it under the terms of the GNU General Public License |
10  | as published by the Free Software Foundation; either version 2 |
11  | of the License, or (at your option) any later version. |
12  | |
13  | This program is distributed in the hope that it will be useful, |
14  | but WITHOUT ANY WARRANTY; without even the implied warranty of |
15  | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
16  | GNU General Public License for more details. |
17  | |
18  | You should have received a copy of the GNU General Public License |
19  | along with this program; if not, write to the Free Software |
20  | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
21  +-----------------------------------------------------------------------------+
22 */
23 
33 {
34  var $ilias;
35  var $lng;
36 
39  var $port;
41  var $ssl_port;
43  var $logfile;
44  var $loglevel;
45  var $hosts;
46  var $active;
47  var $nic;
48 
50 
51 
58  function __construct()
59  {
60  global $ilias,$lng;
61 
62  $this->ilias =& $ilias;
63  $this->lng =& $lng;
64  $this->lng->loadLanguageModule("chat");
65 
66  define(TIMEOUT,2);
67 
68  $this->read();
69  }
70 
71 
72  static function _isActive()
73  {
74  global $ilias;
75 
76  return (bool) $ilias->getSetting("chat_active");
77  }
78 
79 
80  // SET/GET
81  public function setInternalIp($ip)
82  {
83  $this->internal_ip = $ip;
84  }
85 
86  public function getInternalIp()
87  {
88  return $this->internal_ip;
89  }
90 
91  public function setExternalIp($ip)
92  {
93  $this->external_ip = $ip;
94  }
95 
96  public function getExternalIp()
97  {
98  return $this->external_ip ? $this->external_ip : $this->internal_ip;
99  }
100 
101  public function setPort($port)
102  {
103  $this->port = $port;
104  }
105 
106  public function getPort()
107  {
108  return $this->port;
109  }
110 
111  public function setSSLStatus($ssl_status = 0)
112  {
113  $this->ssl_status = $ssl_status;
114  }
115 
116  public function getSSLStatus()
117  {
118  return $this->ssl_status;
119  }
120 
121  public function setSSLPort($ssl_port)
122  {
123  $this->ssl_port = $ssl_port;
124  }
125 
126  public function getSSLPort()
127  {
128  return $this->ssl_port;
129  }
130 
131  public function setModeratorPassword($a_passwd)
132  {
133  $this->moderator = $a_passwd;
134  }
135 
136  public function getModeratorPassword()
137  {
138  return $this->moderator;
139  }
140 
141  public function setLogfile($logfile)
142  {
143  $this->logfile = $logfile;
144  }
145 
146  public function getLogfile()
147  {
148  return $this->logfile;
149  }
150 
151  public function setLogLevel($loglevel)
152  {
153  $this->loglevel = $loglevel;
154  }
155 
156  public function getLogLevel()
157  {
158  return $this->loglevel;
159  }
160 
161  public function setAllowedHosts($hosts)
162  {
163  $this->hosts = $hosts;
164  }
165 
166  public function getAllowedHosts()
167  {
168  return $this->hosts;
169  }
170 
171  public function getErrorMessage()
172  {
173  return $this->error_message;
174  }
175 
176  public function setActiveStatus($status)
177  {
178  $this->active = $status;
179  }
180 
181  public function getActiveStatus()
182  {
183  return $this->active;
184  }
185 
186  public function getNic()
187  {
188  return substr($this->nic,0,6);
189  }
190 
191  public function validate()
192  {
193  $this->error_message = "";
194 
195  if(!$this->getInternalIp())
196  {
197  $this->error_message .= $this->lng->txt("chat_add_internal_ip");
198  }
199  if(!$this->getExternalIp())
200  {
201  if($this->error_message)
202  {
203  $this->error_message .= "<br />";
204  }
205  $this->error_message .= $this->lng->txt("chat_add_external_ip");
206  }
207  if(!$this->getPort())
208  {
209  if($this->error_message)
210  {
211  $this->error_message .= "<br />";
212  }
213  $this->error_message .= $this->lng->txt("chat_add_port");
214  }
215  if($this->getSSLStatus() && !$this->getSSLPort())
216  {
217  if($this->error_message)
218  {
219  $this->error_message .= "<br />";
220  }
221  $this->error_message .= $this->lng->txt("chat_add_ssl_port");
222  }
223  if(!$this->getModeratorPassword())
224  {
225  if($this->error_message)
226  {
227  $this->error_message .= "<br />";
228  }
229  $this->error_message .= $this->lng->txt("chat_add_moderator_password");
230  }
231  if(!$this->getAllowedHosts())
232  {
233  if($this->error_message)
234  {
235  $this->error_message .= "<br />";
236  }
237  $this->error_message .= $this->lng->txt("chat_add_allowed_hosts");
238  }
239 
240  if($this->getAllowedHosts())
241  {
242  $this->parseAllowedHosts();
243  }
244 
245  return $this->error_message ? false : true;
246  }
247 
248  public function update()
249  {
250  $this->ilias->setSetting("chat_internal_ip",$this->getInternalIp());
251  $this->ilias->setSetting("chat_external_ip",$this->getExternalIp());
252  $this->ilias->setSetting("chat_port",$this->getPort());
253  $this->ilias->setSetting("chat_ssl_status",$this->getSSLStatus());
254  $this->ilias->setSetting("chat_ssl_port",$this->getSSLPort());
255  $this->ilias->setSetting("chat_logfile",$this->getLogfile());
256  $this->ilias->setSetting("chat_loglevel",$this->getLogLevel());
257  $this->ilias->setSetting("chat_hosts",$this->getAllowedHosts());
258  $this->ilias->setSetting("chat_moderator_password",$this->getModeratorPassword());
259 
260  return $this->writeConfigFile();
261  }
262 
263  public function updateStatus()
264  {
265  $this->ilias->setSetting("chat_active",$this->getActiveStatus());
266  }
267 
268 
269  public function read()
270  {
271  $this->internal_ip = $this->ilias->getSetting("chat_internal_ip");
272  $this->external_ip = $this->ilias->getSetting("chat_external_ip");
273  $this->port = $this->ilias->getSetting("chat_port");
274  $this->ssl_status = $this->ilias->getSetting("chat_ssl_status");
275  $this->ssl_port = $this->ilias->getSetting("chat_ssl_port");
276  $this->moderator = $this->ilias->getSetting("chat_moderator_password");
277  $this->loglevel = $this->ilias->getSetting("chat_loglevel");
278  $this->logfile = $this->ilias->getSetting("chat_logfile");
279  $this->hosts = $this->ilias->getSetting("chat_hosts");
280  $this->active = $this->ilias->getSetting("chat_active");
281  $this->nic = $this->ilias->getSetting("nic_key");
282  }
283 
284  public function isAlive()
285  {
286  if($this->getInternalIp() and $this->getPort())
287  {
288  if( $sp = @fsockopen($this->getInternalIp(),$this->getPort(), $errno, $errstr, TIMEOUT))
289  {
290  fclose($sp);
291  return true;
292  }
293  return false;
294  }
295  return false;
296  }
297 
298  private function parseAllowedHosts()
299  {
300  $hosts_arr2 = array();
301  $hosts_arr = explode(',',$this->getAllowedHosts());
302 
303  for($i = 0;$i < count($hosts_arr); ++$i)
304  {
305  if(trim($hosts_arr[$i]))
306  {
307  $hosts_arr2[] = trim($hosts_arr[$i]);
308  }
309  }
310  $this->setAllowedHosts(implode(',',$hosts_arr2));
311 
312  return true;
313  }
314 
315  private function writeConfigFile()
316  {
317  if(!@is_dir(ilUtil::getDataDir().'/chat'))
318  {
320  }
321  if(!($fp = @fopen(ilUtil::getDataDir().'/chat/server.ini',"w")))
322  {
323  $this->error_message = ilUtil::getDataDir().'/chat/server.ini ' .$this->lng->txt("chat_no_write_perm");
324  return false;
325  }
326  $content = "LogLevel = ".$this->getLogLevel()."\n";
327  if($this->getLogfile())
328  {
329  $content .= "LogFile = ".$this->getLogfile()."\n";
330  }
331  $content .= "IpAddress = ".$this->getInternalIp()."\n";
332  $content .= "ExternalIpAddress = ".$this->getExternalIp()."\n";
333  $content .= "Port = ".$this->getPort()."\n";
334  #$content .= "SSLStatus = ".($this->getSSLStatus() ? $this->getSSLStatus() : 0)."\n";
335  #$content .= "SSLPort = ".$this->getSSLPort()."\n";
336  $content .= "ModeratorPassword = ".$this->getModeratorPassword()."\n";
337  $content .= "HeaderFileName = ".ILIAS_ABSOLUTE_PATH."/Modules/Chat/templates/default/header.html\n";
338  $content .= "FooterFileName = ".ILIAS_ABSOLUTE_PATH."/Modules/Chat/templates/default/footer.html\n";
339  $content .= "Authentication = 1\n";
340  $content .= "ConnectionsFrom = ".$this->getAllowedHosts()."\n";
341 
342  if(!@fwrite($fp,$content))
343  {
344  $this->error_message = ilUtil::getDataDir().'/chat/server.ini '.$this->lng->txt("chat_no_write_perm");
345  fclose($fp);
346 
347  return false;
348 
349  }
350  fclose($fp);
351  return true;
352  }
353 
354 } // END class.ilObjChatServer
355 ?>