"User Remote Service" plugin - refs BT#16297

pull/3353/head
Sébastien Ducoulombier 6 years ago
parent 94cf83af56
commit dcb97c98aa
  1. 129
      plugin/userremoteservice/Entity/UserRemoteService.php
  2. 4
      plugin/userremoteservice/README.md
  3. 18
      plugin/userremoteservice/admin.php
  4. 4
      plugin/userremoteservice/config.php
  5. 12
      plugin/userremoteservice/iframe.php
  6. 8
      plugin/userremoteservice/index.php
  7. 6
      plugin/userremoteservice/install.php
  8. 15
      plugin/userremoteservice/lang/english.php
  9. 18
      plugin/userremoteservice/lang/french.php
  10. 6
      plugin/userremoteservice/plugin.php
  11. 267
      plugin/userremoteservice/src/user_remote_service_plugin.class.php
  12. 6
      plugin/userremoteservice/uninstall.php

@ -0,0 +1,129 @@
<?php
/* For licensing terms, see /license.txt */
namespace Chamilo\PluginBundle\UserRemoteService;
use Doctrine\ORM\Mapping as ORM;
use Exception;
/**
* UserRemoteService.
*
* @ORM\Table(name="user_remote_service")
* @ORM\Entity
*/
class UserRemoteService
{
/**
* @var int
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue
*/
protected $id;
/**
* @var string
*
* @ORM\Column(name="title", type="string", length=255, nullable=false)
*/
protected $title;
/**
* @var string
*
* @ORM\Column(name="url", type="string", length=255, nullable=false)
*/
protected $url;
/**
* @return int
*/
public function getId()
{
return $this->id;
}
/**
* @param int $id
*
* @return $this
*/
public function setId($id)
{
$this->id = $id;
return $this;
}
/**
* @return string
*/
public function getTitle()
{
return $this->title;
}
/**
* @param string $title
*
* @return UserRemoteService
*/
public function setTitle($title)
{
$this->title = $title;
return $this;
}
/**
* @return string
*/
public function getURL()
{
return $this->url;
}
/**
* @param string $url
*
* @return UserRemoteService
*/
public function setURL($url)
{
$this->url = $url;
return $this;
}
/**
* Returns a user-specific URL, with two extra query string parameters : 'username' and 'hash'.
* 'hash' is the return value of function call crypt($username, $salt).
*
* @param string $username the URL query parameter 'username'
* @param string $salt the salt to be passed to crypt() in order to generate the 'hash' query parameter
*
* @throws Exception on crypt() failure
*
* @return string the custom user URL
*/
public function getCustomUserURL($username, $salt)
{
$hash = crypt($username, $salt);
if (is_null($hash)) {
throw new Exception('crypt() failed');
}
return sprintf(
'%s%s%s',
$this->url,
false === strpos($this->url, '?') ? '?' : '&',
http_build_query(
[
'username' => $username,
'hash' => $hash,
]
)
);
}
}

@ -0,0 +1,4 @@
User Remote Service Plugin
==========================
Appends site-specific iframe-targetted user-identifying links to the menu bar.

@ -0,0 +1,18 @@
<?php
/* For licensing terms, see /license.txt */
require_once __DIR__.'/config.php';
api_protect_admin_script(true);
$plugin = UserRemoteServicePlugin::create();
Display::display_header($plugin->get_title());
echo $plugin->getCreationForm()->returnForm();
echo $plugin->getDeletionForm()->returnForm();
echo $plugin->getServiceHTMLTable();
Display::display_footer();

@ -0,0 +1,4 @@
<?php
/* For licensing terms, see /license.txt */
require_once __DIR__.'/../../main/inc/global.inc.php';

@ -0,0 +1,12 @@
<?php
/* For licensing terms, see /license.txt */
require_once __DIR__.'/config.php';
$plugin = UserRemoteServicePlugin::create();
Display::display_header($plugin->get_title());
echo $plugin->getIFrame($_REQUEST['serviceId']);
Display::display_footer();

@ -0,0 +1,8 @@
<?php
/* For licensing terms, see /license.txt */
require_once __DIR__.'/config.php';
foreach (UserRemoteServicePlugin::create()->getNavigationMenu() as $menu) {
$template->params['menu'][] = $menu;
}

@ -0,0 +1,6 @@
<?php
/* For licensing terms, see /license.txt */
require_once __DIR__.'/config.php';
UserRemoteServicePlugin::create()->install();

@ -0,0 +1,15 @@
<?php
/* For licensing terms, see /license.txt */
$strings['plugin_title'] = 'User Remote Services';
$strings['plugin_comment'] = 'Appends site-specific iframe-targetted user-identifying links to the menu bar.';
$strings['salt'] = 'Salt';
$strings['salt_help'] = '"hash" URL query string parameter crypt() salt';
// Please keep alphabetically sorted
$strings['CreateService'] = 'Add service to menu bar';
$strings['DeleteServices'] = 'Remove services from menu bar';
$strings['ServicesToDelete'] = 'Services to remove from menu bar';
$strings['ServiceTitle'] = 'Service title';
$strings['ServiceURL'] = 'Service web site location (URL)';

@ -0,0 +1,18 @@
<?php
/* For licensing terms, see /license.txt */
$strings['plugin_title'] = "Services distants pour l'utilisateur";
$strings['plugin_comment'] =
"Ajoute à la barre de menu des liens spécifiques qui identifient l'utilisateur et qui s'ouvrent dans une iframe.";
/* Strings for settings */
$strings['salt'] = "Sel";
$strings['salt_help'] =
"Sel ('salt') à passer à la fonction crypt() pour générer le paramètre d'URL 'hash' des liens générés.";
// Please keep alphabetically sorted
$strings['CreateService'] = "Ajouter le service au menu";
$strings['DeleteServices'] = "Retirer les services du menu";
$strings['ServicesToDelete'] = "Services à retirer du menu";
$strings['ServiceTitle'] = "Titre du service";
$strings['ServiceURL'] = "Adresse web du service (URL)";

@ -0,0 +1,6 @@
<?php
/* For licensing terms, see /license.txt */
require_once __DIR__.'/config.php';
$plugin_info = UserRemoteServicePlugin::create()->get_info();

@ -0,0 +1,267 @@
<?php
/* For licensing terms, see /license.txt */
use Chamilo\PluginBundle\UserRemoteService\UserRemoteService;
use Doctrine\ORM\OptimisticLockException;
class UserRemoteServicePlugin extends Plugin
{
const TABLE = 'user_remote_service';
/**
* UserRemoteServicePlugin constructor.
*/
protected function __construct()
{
parent::__construct(
'1.0',
'Sébastien Ducoulombier',
[
'salt' => 'text',
]
);
$this->isAdminPlugin = true;
}
/**
* Caches and returns a single instance.
*
* @return UserRemoteServicePlugin
*/
public static function create()
{
static $result = null;
return $result ? $result : $result = new self();
}
/**
* Creates the plugin table.
*/
public function install()
{
Database::query(
sprintf(
<<<OEQ
create table if not exists %s (
id int unsigned not null auto_increment primary key,
title varchar(255) not null,
url varchar(255) not null
)
OEQ,
Database::get_main_table(self::TABLE)
)
);
}
/**
* Drops the plugin table.
*/
public function uninstall()
{
Database::query('drop table if exists '.Database::get_main_table(self::TABLE));
}
/**
* @return string the salt setting
*/
public function salt()
{
return $this->get('salt');
}
/**
* Retrieves the list of all services.
*
* @return UserRemoteService[]
*/
public function getServices()
{
return Database::getManager()->getRepository(
'Chamilo\PluginBundle\UserRemoteService\UserRemoteService'
)->findAll();
}
/**
* Adds a new service.
*
* @param string $title
* @param string $url
*
* @throws OptimisticLockException
*/
public function addService($title, $url)
{
$service = new UserRemoteService;
$service->setTitle($title);
$service->setURL($url);
Database::getManager()->persist($service);
Database::getManager()->flush();
}
/**
* Removes a service.
*
* @param UserRemoteService $service
*
* @throws OptimisticLockException
*/
public function removeService($service)
{
Database::getManager()->remove($service);
Database::getManager()->flush();
}
/**
* Updates a service.
*
* @param UserRemoteService $service
*
* @throws OptimisticLockException
*/
public function updateService($service)
{
Database::getManager()->persist($service);
Database::getManager()->flush();
}
/**
* Generates the menu items to be appended to the navigation array
*
* @see \return_navigation_array
*
* @throws Exception on crypt() failure
*
* @return array menu items
*/
public function getNavigationMenu()
{
$menu = [];
foreach ($this->getServices() as $service) {
$menu['service_'.$service->getId()] = [
'url' => sprintf(
'%s%s/iframe.php?serviceId=%d',
api_get_path(WEB_PLUGIN_PATH),
$this->get_name(),
$service->getId()
),
'title' => $service->getTitle(),
];
}
return $menu;
}
/**
* Generates and handles submission of the creation form.
*
* @throws OptimisticLockException
*
* @return FormValidator
*/
public function getCreationForm()
{
$form = new FormValidator('creationForm');
$titleText = $form->addText('title', get_lang('ServiceTitle'));
$urlText = $form->addText('url', get_lang('ServiceURL'));
$form->addButtonCreate(get_lang('CreateService'));
if ($form->validate()) {
$this->addService($titleText->getValue(), $urlText->getValue());
}
return $form;
}
/**
* Generates and handles submission of the service deletion form.
*
* @throws OptimisticLockException
*
* @return FormValidator
*/
public function getDeletionForm()
{
$form = new FormValidator('deletionForm');
$services = $this->getServices();
$options = [];
foreach ($services as $service) {
$options[$service->getId()] = $service->getTitle();
}
$serviceIdSelect = $form->addSelect('serviceId', get_lang('ServicesToDelete'), $options);
$serviceIdSelect->setMultiple(true);
$form->addButtonDelete(get_lang('DeleteServices'));
if ($form->validate()) {
foreach ($serviceIdSelect->getValue() as $serviceId) {
foreach ($services as $service) {
if ($service->getId() == $serviceId) {
$this->removeService($service);
}
}
}
}
return $form;
}
/**
* Generates the service HTML table.
*
* @return string
*/
public function getServiceHTMLTable()
{
$html = '';
$services = $this->getServices();
if (!empty($services)) {
$table = new HTML_Table('class="table"');
$table->addRow(
[
get_lang('ServiceTitle'),
get_lang('ServiceURL'),
],
null,
'th'
);
foreach ($services as $service) {
$table->addRow([
$service->getTitle(),
$service->getURL(),
]);
}
$html = $table->toHtml();
}
return $html;
}
/**
* Retrieves one service.
*
* @param int $id the service identifier
*
* @return UserRemoteService the service
*/
public function getService($id)
{
return Database::getManager()->getRepository(
'Chamilo\PluginBundle\UserRemoteService\UserRemoteService'
)->find($id);
}
/**
* Generates the iframe HTML element to load a service URL
*
* @param int $serviceId the service identifier
*
* @throws Exception on crypt() failure
*
* @return string the iframe HTML element
*/
public function getIFrame($serviceId)
{
return sprintf(
<<<HTML
<div class="embed-responsive embed-responsive-16by9">
<iframe class="embed-responsive-item" src="%s"></iframe>
</div>
HTML,
$this->getService($serviceId)->getCustomUserURL(api_get_user_info()['username'], $this->salt())
);
}
}

@ -0,0 +1,6 @@
<?php
/* For licensing terms, see /license.txt */
require_once __DIR__.'/config.php';
UserRemoteServicePlugin::create()->uninstall();
Loading…
Cancel
Save