ILIAS  Release_4_0_x_branch Revision 61816
 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  $this->read();
67  }
68 
69 
70  static function _isActive()
71  {
72  global $ilias;
73 
74  return (bool) $ilias->getSetting("chat_active");
75  }
76 
77 
78  // SET/GET
79  public function setInternalIp($ip)
80  {
81  $this->internal_ip = $ip;
82  }
83 
84  public function getInternalIp()
85  {
86  return $this->internal_ip;
87  }
88 
89  public function setExternalIp($ip)
90  {
91  $this->external_ip = $ip;
92  }
93 
94  public function getExternalIp()
95  {
96  return $this->external_ip ? $this->external_ip : $this->internal_ip;
97  }
98 
99  public function setPort($port)
100  {
101  $this->port = $port;
102  }
103 
104  public function getPort()
105  {
106  return $this->port;
107  }
108 
109  public function setSSLStatus($ssl_status = 0)
110  {
111  $this->ssl_status = $ssl_status;
112  }
113 
114  public function getSSLStatus()
115  {
116  return $this->ssl_status;
117  }
118 
119  public function setSSLPort($ssl_port)
120  {
121  $this->ssl_port = $ssl_port;
122  }
123 
124  public function getSSLPort()
125  {
126  return $this->ssl_port;
127  }
128 
129  public function setModeratorPassword($a_passwd)
130  {
131  $this->moderator = $a_passwd;
132  }
133 
134  public function getModeratorPassword()
135  {
136  return $this->moderator;
137  }
138 
139  public function setLogfile($logfile)
140  {
141  $this->logfile = $logfile;
142  }
143 
144  public function getLogfile()
145  {
146  return $this->logfile;
147  }
148 
149  public function setLogLevel($loglevel)
150  {
151  $this->loglevel = $loglevel;
152  }
153 
154  public function getLogLevel()
155  {
156  return $this->loglevel;
157  }
158 
159  public function setAllowedHosts($hosts)
160  {
161  $this->hosts = $hosts;
162  }
163 
164  public function getAllowedHosts()
165  {
166  return $this->hosts;
167  }
168 
169  public function getErrorMessage()
170  {
171  return $this->error_message;
172  }
173 
174  public function setActiveStatus($status)
175  {
176  $this->active = $status;
177  }
178 
179  public function getActiveStatus()
180  {
181  return $this->active;
182  }
183 
184  public function getNic()
185  {
186  return substr($this->nic,0,6);
187  }
188 
189  public function validate()
190  {
191  $this->error_message = "";
192 
193  if(!$this->getInternalIp())
194  {
195  $this->error_message .= $this->lng->txt("chat_add_internal_ip");
196  }
197  if(!$this->getExternalIp())
198  {
199  if($this->error_message)
200  {
201  $this->error_message .= "<br />";
202  }
203  $this->error_message .= $this->lng->txt("chat_add_external_ip");
204  }
205  if(!$this->getPort())
206  {
207  if($this->error_message)
208  {
209  $this->error_message .= "<br />";
210  }
211  $this->error_message .= $this->lng->txt("chat_add_port");
212  }
213  if($this->getSSLStatus() && !$this->getSSLPort())
214  {
215  if($this->error_message)
216  {
217  $this->error_message .= "<br />";
218  }
219  $this->error_message .= $this->lng->txt("chat_add_ssl_port");
220  }
221  if(!$this->getModeratorPassword())
222  {
223  if($this->error_message)
224  {
225  $this->error_message .= "<br />";
226  }
227  $this->error_message .= $this->lng->txt("chat_add_moderator_password");
228  }
229  if(!$this->getAllowedHosts())
230  {
231  if($this->error_message)
232  {
233  $this->error_message .= "<br />";
234  }
235  $this->error_message .= $this->lng->txt("chat_add_allowed_hosts");
236  }
237 
238  if($this->getAllowedHosts())
239  {
240  $this->parseAllowedHosts();
241  }
242 
243  return $this->error_message ? false : true;
244  }
245 
246  public function update()
247  {
248  $this->ilias->setSetting("chat_internal_ip",$this->getInternalIp());
249  $this->ilias->setSetting("chat_external_ip",$this->getExternalIp());
250  $this->ilias->setSetting("chat_port",$this->getPort());
251  $this->ilias->setSetting("chat_ssl_status",$this->getSSLStatus());
252  $this->ilias->setSetting("chat_ssl_port",$this->getSSLPort());
253  $this->ilias->setSetting("chat_logfile",$this->getLogfile());
254  $this->ilias->setSetting("chat_loglevel",$this->getLogLevel());
255  $this->ilias->setSetting("chat_hosts",$this->getAllowedHosts());
256  $this->ilias->setSetting("chat_moderator_password",$this->getModeratorPassword());
257 
258  return $this->writeConfigFile();
259  }
260 
261  public function updateStatus()
262  {
263  $this->ilias->setSetting("chat_active",$this->getActiveStatus());
264  }
265 
266 
267  public function read()
268  {
269  $this->internal_ip = $this->ilias->getSetting("chat_internal_ip");
270  $this->external_ip = $this->ilias->getSetting("chat_external_ip");
271  $this->port = $this->ilias->getSetting("chat_port");
272  $this->ssl_status = $this->ilias->getSetting("chat_ssl_status");
273  $this->ssl_port = $this->ilias->getSetting("chat_ssl_port");
274  $this->moderator = $this->ilias->getSetting("chat_moderator_password");
275  $this->loglevel = $this->ilias->getSetting("chat_loglevel");
276  $this->logfile = $this->ilias->getSetting("chat_logfile");
277  $this->hosts = $this->ilias->getSetting("chat_hosts");
278  $this->active = $this->ilias->getSetting("chat_active");
279  $this->nic = $this->ilias->getSetting("nic_key");
280  }
281 
282  public function isAlive()
283  {
284  if($this->getInternalIp() and $this->getPort())
285  {
286  if( $sp = @fsockopen($this->getInternalIp(),$this->getPort(), $errno, $errstr, 100))
287  {
288  fclose($sp);
289  return true;
290  }
291  return false;
292  }
293  return false;
294  }
295 
296  private function parseAllowedHosts()
297  {
298  $hosts_arr2 = array();
299  $hosts_arr = explode(',',$this->getAllowedHosts());
300 
301  for($i = 0;$i < count($hosts_arr); ++$i)
302  {
303  if(trim($hosts_arr[$i]))
304  {
305  $hosts_arr2[] = trim($hosts_arr[$i]);
306  }
307  }
308  $this->setAllowedHosts(implode(',',$hosts_arr2));
309 
310  return true;
311  }
312 
313  private function writeConfigFile()
314  {
315  if(!@is_dir(ilUtil::getDataDir().'/chat'))
316  {
318  }
319  if(!($fp = @fopen(ilUtil::getDataDir().'/chat/server.ini',"w")))
320  {
321  $this->error_message = ilUtil::getDataDir().'/chat/server.ini ' .$this->lng->txt("chat_no_write_perm");
322  return false;
323  }
324  $content = "LogLevel = ".$this->getLogLevel()."\n";
325  if($this->getLogfile())
326  {
327  $content .= "LogFile = ".$this->getLogfile()."\n";
328  }
329  $content .= "IpAddress = ".$this->getInternalIp()."\n";
330  $content .= "ExternalIpAddress = ".$this->getExternalIp()."\n";
331  $content .= "Port = ".$this->getPort()."\n";
332  #$content .= "SSLStatus = ".($this->getSSLStatus() ? $this->getSSLStatus() : 0)."\n";
333  #$content .= "SSLPort = ".$this->getSSLPort()."\n";
334  $content .= "ModeratorPassword = ".$this->getModeratorPassword()."\n";
335  $content .= "HeaderFileName = ".ILIAS_ABSOLUTE_PATH."/Modules/Chat/templates/default/header.html\n";
336  $content .= "FooterFileName = ".ILIAS_ABSOLUTE_PATH."/Modules/Chat/templates/default/footer.html\n";
337  $content .= "Authentication = 1\n";
338  $content .= "ConnectionsFrom = ".$this->getAllowedHosts()."\n";
339 
340  if(!@fwrite($fp,$content))
341  {
342  $this->error_message = ilUtil::getDataDir().'/chat/server.ini '.$this->lng->txt("chat_no_write_perm");
343  fclose($fp);
344 
345  return false;
346 
347  }
348  fclose($fp);
349  return true;
350  }
351 
352 } // END class.ilObjChatServer
353 ?>