ILIAS  release_4-3 Revision
 All Data Structures Namespaces Files Functions Variables Groups Pages
class.ilChatroomViewTask.php
Go to the documentation of this file.
1 <?php
2 /* Copyright (c) 1998-2013 ILIAS open source, Extended GPL, see docs/LICENSE */
3 
4 require_once 'Modules/Chatroom/classes/class.ilChatroom.php';
5 
13 {
17  private $gui;
18 
22  private $commonSettings;
23 
29  public function __construct(ilDBayObjectGUI $gui)
30  {
31  $this->gui = $gui;
32  $this->commonSettings = new ilSetting('common');
33  }
34 
38  private function showSoapWarningIfNeeded()
39  {
43  global $lng;
44 
45  if(!$this->commonSettings->get('soap_user_administration'))
46  {
47  ilUtil::sendInfo($lng->txt('soap_must_be_enabled'));
48  }
49  }
50 
54  public function saveSettings()
55  {
60  global $ilCtrl, $lng;
61 
62  require_once 'Modules/Chatroom/classes/class.ilChatroomFormFactory.php';
63  $factory = new ilChatroomFormFactory();
64  $form = $factory->getGeneralSettingsForm();
65 
66  if(!$form->checkInput())
67  {
68  $form->setValuesByPost();
69  $this->serversettings($form);
70  return;
71  }
72 
73  if(!$this->checkPrivHosts($form->getInput('priv_hosts')))
74  {
75  $form->setValuesByPost();
76  ilUtil::sendFailure($lng->txt('invalid_priv_hosts'));
77  $this->serversettings($form);
78  return;
79  }
80 
81  $settings = array(
82  'protocol' => $form->getInput('protocol'),
83  'instance' => $form->getInput('instance'),
84  'port' => $form->getInput('port'),
85  'address' => $form->getInput('address'),
86  'priv_hosts' => $form->getInput('priv_hosts'),
87  'keystore' => $form->getInput('keystore'),
88  'keypass' => $form->getInput('keypass'),
89  'storepass' => $form->getInput('storepass')
90  );
91 
92  require_once 'Modules/Chatroom/classes/class.ilChatroomAdmin.php';
93  $adminSettings = new ilChatroomAdmin($this->gui->object->getId());
94  $adminSettings->saveGeneralSettings((object)$settings);
95 
96  $this->writeDataToFile($settings);
97 
98  ilUtil::sendSuccess($lng->txt('settings_has_been_saved'), true);
99  $ilCtrl->redirect($this->gui, 'view-serversettings');
100  }
101 
105  public function saveClientSettings()
106  {
111  global $ilCtrl, $lng;
112 
113  require_once 'Modules/Chatroom/classes/class.ilChatroomFormFactory.php';
114  $factory = new ilChatroomFormFactory();
115  $form = $factory->getClientSettingsForm();
116 
117  if(!$form->checkInput())
118  {
119  $form->setValuesByPost();
120  $this->clientsettings($form);
121  return;
122  }
123 
124  $settings = array(
125  'hash' => $form->getInput('name'),
126  'name' => $form->getInput('name'),
127  'url' => $form->getInput('url'),
128  'user' => $form->getInput('user'),
129  'password' => $form->getInput('password'),
130  'client' => CLIENT_ID,
131  'enable_osd' => (boolean)$form->getInput('enable_osd'),
132  'osd_intervall' => (int)$form->getInput('osd_intervall'),
133  'chat_enabled' => ((boolean)$form->getInput('chat_enabled')) && ((boolean)$this->commonSettings->get('soap_user_administration')),
134  'enable_smilies' => (boolean)$form->getInput('enable_smilies'),
135  'play_invitation_sound' => (boolean)$form->getInput('play_invitation_sound')
136  );
137 
138  $notificationSettings = new ilSetting('notifications');
139  $notificationSettings->set('osd_polling_intervall', (int)$form->getInput('osd_intervall'));
140  $notificationSettings->set('enable_osd', (boolean)$form->getInput('enable_osd'));
141 
142  $chatSettings = new ilSetting('chatroom');
143  $chatSettings->set('chat_enabled', $settings['chat_enabled']);
144  $chatSettings->set('play_invitation_sound', (boolean)$form->getInput('play_invitation_sound'));
145 
146  require_once 'Modules/Chatroom/classes/class.ilChatroomAdmin.php';
147  $adminSettings = new ilChatroomAdmin($this->gui->object->getId());
148  $adminSettings->saveClientSettings((object)$settings);
149 
150  $this->writeClientSettingsToFile($settings);
151 
152  ilUtil::sendSuccess($lng->txt('settings_has_been_saved'), true);
153 
154  $ilCtrl->redirect($this->gui, 'view-clientsettings');
155  }
156 
162  protected function writeClientSettingsToFile(array $settings)
163  {
164  if($srv_prp_path = $this->checkDirectory())
165  {
166  $handle = fopen($srv_prp_path . 'client.properties', 'w');
167 
168  if(!fwrite($handle, $this->getClientFileContent($settings)))
169  {
170  throw new Exception('Cannot write to file');
171  }
172 
173  fclose($handle);
174  }
175  }
176 
182  protected function getClientFileContent(array $settings)
183  {
184  $linebreak = "\n";
185 
186  $content = 'hash = ' . $settings['hash'] . $linebreak;
187  $content .= 'name = ' . $settings['name'] . $linebreak;
188  $content .= 'url = ' . $settings['url'] . $linebreak;
189  $content .= 'user = ' . $settings['user'] . $linebreak;
190  $content .= 'password = ' . $settings['password'] . $linebreak;
191  $content .= 'client = ' . $settings['client'];
192 
193  return $content;
194  }
195 
196 
201  protected function writeDataToFile(array $settings)
202  {
203  if($srv_prp_path = $this->checkDirectory())
204  {
205  $handle = fopen($srv_prp_path . 'server.properties', 'w');
206 
207  if(!fwrite($handle, $this->getFileContent($settings)))
208  {
209  throw new Exception('Cannot write to file');
210  }
211 
212  fclose($handle);
213  }
214  }
215 
221  protected function getFileContent(array $settings)
222  {
223  $linebreak = "\n";
224 
225  $content = 'host = ' . $settings['address'] . $linebreak;
226  $content .= 'port = ' . $settings['port'] . $linebreak;
227  $content .= 'privileged_hosts = ' . $settings['priv_hosts'] . $linebreak;
228 
229  $settings['protocol'] == 'https' ? $https = 1 : $https = 0;
230 
231  $content .= 'https = ' . $https . $linebreak;
232  $content .= 'keystore = ' . $settings['keystore'] . $linebreak;
233  $content .= 'keypass = ' . $settings['keypass'] . $linebreak;
234  $content .= 'storepass = ' . $settings['storepass'];
235 
236  return $content;
237  }
238 
244  protected function checkDirectory()
245  {
246  $srv_prp_path = ilUtil::getDataDir() . '/chatroom/';
247 
248  if(!file_exists($srv_prp_path))
249  {
250  if(!ilUtil::makeDir($srv_prp_path))
251  {
252  throw new Exception('Directory cannot be created');
253  }
254  }
255 
256  return $srv_prp_path;
257  }
258 
259 
266  protected function checkPrivHosts($ipnumbers)
267  {
268  $ipnumbers = preg_replace("/[^0-9.,]+/", "", $ipnumbers);
269  $ips = explode(',', $ipnumbers);
270 
271  foreach($ips as $ip)
272  {
273  $ip_parts = explode('.', $ip);
274 
275  if(!($ip_parts[0] <= 255 && $ip_parts[1] <= 255 && $ip_parts[2] <= 255 && $ip_parts[3] <= 255 &&
276  preg_match("!^([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})$!", $ip))
277  )
278  {
279  return false;
280  }
281  }
282 
283  return true;
284  }
285 
286 
291  public function serversettings(ilPropertyFormGUI $form = null)
292  {
298  global $tpl, $ilCtrl, $lng;
299 
300  include_once 'Modules/Chatroom/classes/class.ilChatroom.php';
301 
302  ilChatroom::checkUserPermissions('read', $this->gui->ref_id);
303 
304  $this->defaultActions();
305  $this->gui->switchToVisibleMode();
306  $this->showSoapWarningIfNeeded();
307 
308  require_once 'Modules/Chatroom/classes/class.ilChatroomAdmin.php';
309  $adminSettings = new ilChatroomAdmin($this->gui->object->getId());
310  $serverSettings = (array)$adminSettings->loadGeneralSettings();
311  if($form === null)
312  {
313  require_once 'Modules/Chatroom/classes/class.ilChatroomFormFactory.php';
314  $factory = new ilChatroomFormFactory();
315  $form = $factory->getGeneralSettingsForm();
316 
317  if(!$serverSettings['protocol'])
318  {
319  $serverSettings['protocol'] = 'http';
320  }
321 
322  $form->setValuesByArray($serverSettings);
323  }
324 
325  require_once 'Modules/Chatroom/classes/class.ilChatroomServerConnector.php';
326 
327  if($serverSettings['port'] && $serverSettings['address'] && !(boolean)@ilChatroomServerConnector::checkServerConnection())
328  {
329  ilUtil::sendInfo($lng->txt('chat_cannot_connect_to_server'));
330  }
331 
332  $form->setTitle($lng->txt('chatserver_settings_title'));
333  $form->addCommandButton('view-saveSettings', $lng->txt('save'));
334  $form->setFormAction($ilCtrl->getFormAction($this->gui, 'view-saveSettings'));
335  $serverTpl = new ilTemplate('tpl.chatroom_serversettings.html', true, true, 'Modules/Chatroom');
336 
337  $serverTpl->setVariable('VAL_SERVERSETTINGS_FORM', $form->getHTML());
338  $serverTpl->setVariable('LBL_SERVERSETTINGS_FURTHER_INFORMATION', sprintf($lng->txt('server_further_information'), ilUtil::_getHttpPath() . '/Modules/Chatroom/server/README.txt'));
339 
340  $tpl->setVariable('ADM_CONTENT', $serverTpl->get());
341  }
342 
347  public function executeDefault($method)
348  {
352  global $ilCtrl;
353 
354  $ilCtrl->redirect($this->gui, 'view-clientsettings');
355  }
356 
360  public function clientsettings(ilPropertyFormGUI $form = null)
361  {
367  global $tpl, $ilCtrl, $lng;
368 
369  ilChatroom::checkUserPermissions('read', $this->gui->ref_id);
370 
371  $this->defaultActions();
372 
373  $this->gui->switchToVisibleMode();
374 
375  $this->showSoapWarningIfNeeded();
376 
377  require_once 'Modules/Chatroom/classes/class.ilChatroomAdmin.php';
378  $adminSettings = new ilChatroomAdmin($this->gui->object->getId());
379 
380  if($form === null)
381  {
382  require_once 'Modules/Chatroom/classes/class.ilChatroomFormFactory.php';
383  $factory = new ilChatroomFormFactory();
384  $form = $factory->getClientSettingsForm();
385 
386  if(!$this->commonSettings->get('soap_user_administration'))
387  {
388  $form->getItemByPostVar('chat_enabled')->setDisabled(!(boolean)$this->commonSettings->get('soap_user_administration'));
389  $form->getItemByPostVar('chat_enabled')->setChecked(0);
390  }
391 
392  $data = (array)$adminSettings->loadClientSettings();
393 
394  if(!$data['osd_intervall'])
395  {
396  $data['osd_intervall'] = 60;
397  }
398 
399  if(!$data)
400  {
401  $data = array();
402  }
403 
404  if(!$data['url'])
405  {
406  $data['url'] = ilUtil::_getHttpPath();
407  }
408 
409  if(!$data['client'])
410  {
411  $data['client'] = CLIENT_ID;
412  }
413 
414  $data['password_retype'] = $data['password'];
415  $form->setValuesByArray($data);
416  }
417 
418  require_once 'Modules/Chatroom/classes/class.ilChatroomServerConnector.php';
419 
420  $serverSettings = (array)$adminSettings->loadGeneralSettings();
421  if($serverSettings['port'] && $serverSettings['address'] && !(boolean)@ilChatroomServerConnector::checkServerConnection())
422  {
423  ilUtil::sendInfo($lng->txt('chat_cannot_connect_to_server'));
424  }
425 
426  $form->setTitle($lng->txt('general_settings_title'));
427  $form->addCommandButton('view-saveClientSettings', $lng->txt('save'));
428  $form->setFormAction($ilCtrl->getFormAction($this->gui, 'view-saveClientSettings'));
429 
430  $settingsTpl = new ilTemplate('tpl.chatroom_serversettings.html', true, true, 'Modules/Chatroom');
431 
432  $settingsTpl->setVariable('VAL_SERVERSETTINGS_FORM', $form->getHTML());
433  $settingsTpl->setVariable('LBL_SERVERSETTINGS_FURTHER_INFORMATION', sprintf($lng->txt('server_further_information'), ilUtil::_getHttpPath() . '/Modules/Chatroom/server/README.txt'));
434 
435  $tpl->setVariable('ADM_CONTENT', $settingsTpl->get());
436  }
437 
441  private function defaultActions()
442  {
443  $chatSettings = new ilSetting('chatroom');
444  if($chatSettings->get('chat_enabled', false))
445  {
446  $this->forcePublicRoom();
447  }
448  }
449 
453  public function forcePublicRoom()
454  {
456  if(!$ref_id)
457  {
458  $this->createPublicRoom();
459  return;
460  }
461 
462  $instance = ilObjectFactory::getInstanceByRefId($ref_id, false);
463  if(!$instance)
464  {
465  $this->createPublicRoom();
466  return;
467  }
468 
469  $obj_id = ilObject::_lookupObjId($ref_id);
470  if(!$obj_id)
471  {
472  $this->createPublicRoom();
473  return;
474  }
475 
477  {
478  $this->createPublicRoom();
479  return;
480  }
481  }
482 
486  public function createPublicRoom()
487  {
491  global $lng;
492 
493  require_once 'Modules/Chatroom/classes/class.ilChatroomInstaller.php';
494  ilChatroomInstaller::createDefaultPublicRoom(true);
495  ilUtil::sendSuccess($lng->txt('public_chat_created'), true);
496  }
497 }