roundcubemail-plugins-kolab/plugins/kolab_chat/kolab_chat.php

222 lines
6.8 KiB
PHP
Raw Normal View History

2018-07-03 10:44:27 +00:00
<?php
/**
* Kolab Chat
*
* @author Aleksander Machniak <machniak@kolabsys.com>
*
* Copyright (C) 2014-2018, Kolab Systems AG <contact@kolabsys.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
class kolab_chat extends rcube_plugin
{
public $task = '^(?!login|logout).*$';
2018-07-03 10:44:27 +00:00
private $rc;
2018-07-03 10:44:27 +00:00
private $driver;
public function init()
{
$this->rc = rcube::get_instance();
2024-01-24 11:24:41 +01:00
$this->add_hook('startup', [$this, 'startup']);
2018-07-03 10:44:27 +00:00
}
/**
* Startup hook handler, initializes/enables Chat
*/
public function startup($args)
{
// the files module can be enabled/disabled by the kolab_auth plugin
if ($this->rc->config->get('kolab_chat_disabled') || !$this->rc->config->get('kolab_chat_enabled', true)) {
return;
}
$this->load_config();
$extwin = $this->rc->config->get('kolab_chat_extwin');
$driver = $this->rc->config->get('kolab_chat_driver', 'mattermost');
if (!$driver || !file_exists(__DIR__ . "/drivers/$driver.php")) {
return;
}
// Load the driver
require_once __DIR__ . "/drivers/$driver.php";
$class_name = "kolab_chat_$driver";
$this->driver = new $class_name($this);
$this->add_texts('localization/');
2018-07-03 10:44:27 +00:00
// Register UI end-points
$this->register_task('kolab-chat');
2024-01-24 11:24:41 +01:00
$this->register_action('index', [$this, 'ui']);
$this->register_action('action', [$this, 'action']);
2018-07-03 10:44:27 +00:00
if ($this->rc->output->type == 'html' && !$this->rc->output->get_env('framed')) {
2018-07-09 09:07:34 +00:00
$this->include_stylesheet($this->local_skin_path() . '/kolab_chat.css');
2018-07-03 10:44:27 +00:00
$this->rc->output->set_env('kolab_chat_extwin', (bool) $extwin);
$this->rc->output->add_script(
2024-01-24 11:24:41 +01:00
"rcmail.addEventListener('beforeswitch-task', function(p) {
2018-07-03 10:44:27 +00:00
if (p == 'kolab-chat' && rcmail.env.kolab_chat_extwin) {
rcmail.open_window('?_task=kolab-chat&redirect=1', false, false, true);
return false;
}
});",
'foot'
);
2024-01-24 11:24:41 +01:00
$this->add_button([
2018-07-03 10:44:27 +00:00
'command' => 'kolab-chat',
'class' => 'button-chat',
'classsel' => 'button-chat button-selected',
'innerclass' => 'button-inner',
'label' => 'kolab_chat.chat',
'type' => 'link',
2024-01-24 11:24:41 +01:00
], 'taskbar');
$this->driver->ui();
2018-07-03 10:44:27 +00:00
}
if ($this->rc->output->type == 'html' && $args['task'] == 'settings') {
2018-07-03 10:44:27 +00:00
// add hooks for Chat settings
2024-01-24 11:24:41 +01:00
$this->add_hook('preferences_sections_list', [$this, 'preferences_sections_list']);
$this->add_hook('preferences_list', [$this, 'preferences_list']);
$this->add_hook('preferences_save', [$this, 'preferences_save']);
2018-07-03 10:44:27 +00:00
}
}
/**
* Display chat iframe wrapped by Roundcube interface elements (taskmenu)
* or a dummy page with redirect to the chat app.
*/
2024-01-24 11:24:41 +01:00
public function ui()
2018-07-03 10:44:27 +00:00
{
$this->driver->ui();
2018-07-03 10:44:27 +00:00
$url = rcube::JQ($this->driver->url());
2018-07-03 10:44:27 +00:00
if (!empty($_GET['redirect'])) {
echo '<!DOCTYPE html><html><head>'
. '<meta http-equiv="refresh" content="0; url=' . $url . '">'
. '</head><body></body></html>';
exit;
2024-01-24 11:24:41 +01:00
} else {
$this->rc->output->add_script(
"rcmail.addEventListener('init', function() {"
. "rcmail.location_href('$url', rcmail.get_frame_window(rcmail.env.contentframe));"
. "});",
'foot'
);
$this->rc->output->send('kolab_chat.chat');
2018-07-03 10:44:27 +00:00
}
}
/**
* Handler for driver specific actions
*/
2024-01-24 11:24:41 +01:00
public function action()
{
$this->driver->action();
}
2018-07-03 10:44:27 +00:00
/**
* Handler for preferences_sections_list hook.
* Adds Chat settings section into preferences sections list.
*
* @param array Original parameters
*
* @return array Modified parameters
*/
2024-01-24 11:24:41 +01:00
public function preferences_sections_list($p)
2018-07-03 10:44:27 +00:00
{
2024-01-24 11:24:41 +01:00
$p['list']['kolab-chat'] = [
'id' => 'kolab-chat', 'section' => $this->gettext('chat'), 'class' => 'chat',
];
2018-07-03 10:44:27 +00:00
return $p;
}
/**
* Handler for preferences_list hook.
* Adds options blocks into Chat settings sections in Preferences.
*
* @param array Original parameters
*
* @return array Modified parameters
*/
2024-01-24 11:24:41 +01:00
public function preferences_list($p)
2018-07-03 10:44:27 +00:00
{
if ($p['section'] != 'kolab-chat') {
return $p;
}
$no_override = array_flip((array) $this->rc->config->get('dont_override'));
$p['blocks']['main']['name'] = $this->gettext('mainoptions');
if (!isset($no_override['kolab_chat_extwin'])) {
if (!$p['current']) {
$p['blocks']['main']['content'] = true;
return $p;
}
$field_id = 'rcmfd_kolab_chat_extwin';
2024-01-24 11:24:41 +01:00
$input = new html_checkbox(['name' => '_kolab_chat_extwin', 'id' => $field_id, 'value' => 1]);
2018-07-03 10:44:27 +00:00
2024-01-24 11:24:41 +01:00
$p['blocks']['main']['options']['kolab_chat_extwin'] = [
2018-07-03 10:44:27 +00:00
'title' => html::label($field_id, rcube::Q($this->gettext('showinextwin'))),
'content' => $input->show($this->rc->config->get('kolab_chat_extwin') ? 1 : 0),
2024-01-24 11:24:41 +01:00
];
2018-07-03 10:44:27 +00:00
}
if ($p['current']) {
// update env flag in the parent window
2024-01-24 11:24:41 +01:00
$this->rc->output->command('parent.set_env', ['kolab_chat_extwin' => (bool) $this->rc->config->get('kolab_chat_extwin')]);
// Add driver-specific options
$this->driver->preferences_list($p);
2018-07-03 10:44:27 +00:00
}
return $p;
}
/**
* Handler for preferences_save hook.
* Executed on Chat settings form submit.
*
* @param array Original parameters
*
* @return array Modified parameters
*/
2024-01-24 11:24:41 +01:00
public function preferences_save($p)
2018-07-03 10:44:27 +00:00
{
if ($p['section'] == 'kolab-chat') {
2024-01-24 11:24:41 +01:00
$p['prefs'] = [
2018-07-03 10:44:27 +00:00
'kolab_chat_extwin' => isset($_POST['_kolab_chat_extwin']),
2024-01-24 11:24:41 +01:00
];
// Add driver-specific options
$this->driver->preferences_save($p);
2018-07-03 10:44:27 +00:00
}
return $p;
}
}