|
|
|
@ -3,8 +3,7 @@ |
|
|
|
|
|
|
|
|
|
|
|
use Chamilo\PluginBundle\Entity\LtiProvider\PlatformKey; |
|
|
|
use Chamilo\PluginBundle\Entity\LtiProvider\PlatformKey; |
|
|
|
use Doctrine\DBAL\DBALException; |
|
|
|
use Doctrine\DBAL\DBALException; |
|
|
|
use Doctrine\DBAL\Schema\Schema; |
|
|
|
use Doctrine\ORM\OptimisticLockException; |
|
|
|
use Doctrine\DBAL\Types\Type; |
|
|
|
|
|
|
|
use Symfony\Component\Filesystem\Filesystem; |
|
|
|
use Symfony\Component\Filesystem\Filesystem; |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
@ -32,7 +31,8 @@ class LtiProviderPlugin extends Plugin |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
$pkHtml = '<div class="form-group "> |
|
|
|
$pkHtml = '<div class="form-group "> |
|
|
|
<label for="lti_provider_public_key" class="col-sm-2 control-label">'.$this->get_lang('PublicKey').'</label> |
|
|
|
<label for="lti_provider_public_key" class="col-sm-2 control-label">' |
|
|
|
|
|
|
|
.$this->get_lang('PublicKey').'</label> |
|
|
|
<div class="col-sm-8"> |
|
|
|
<div class="col-sm-8"> |
|
|
|
<pre>'.$publicKey.'</pre> |
|
|
|
<pre>'.$publicKey.'</pre> |
|
|
|
<p class="help-block"></p> |
|
|
|
<p class="help-block"></p> |
|
|
|
@ -47,28 +47,53 @@ class LtiProviderPlugin extends Plugin |
|
|
|
'login_url' => 'text', |
|
|
|
'login_url' => 'text', |
|
|
|
'redirect_url' => 'text', |
|
|
|
'redirect_url' => 'text', |
|
|
|
$pkHtml => 'html', |
|
|
|
$pkHtml => 'html', |
|
|
|
'enabled' => 'boolean' |
|
|
|
'enabled' => 'boolean', |
|
|
|
]; |
|
|
|
]; |
|
|
|
parent::__construct($version, $author, $settings); |
|
|
|
parent::__construct($version, $author, $settings); |
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
|
|
* Get the public key |
|
|
|
|
|
|
|
* |
|
|
|
|
|
|
|
* @return string |
|
|
|
|
|
|
|
*/ |
|
|
|
|
|
|
|
public function getPublicKey(): string |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
$publicKey = ''; |
|
|
|
|
|
|
|
$platformKey = Database::getManager() |
|
|
|
|
|
|
|
->getRepository('ChamiloPluginBundle:LtiProvider\PlatformKey') |
|
|
|
|
|
|
|
->findOneBy([]); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if ($platformKey) { |
|
|
|
|
|
|
|
$publicKey = $platformKey->getPublicKey(); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return $publicKey; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* Get the class instance |
|
|
|
* Get the class instance |
|
|
|
|
|
|
|
* |
|
|
|
* @staticvar LtiProviderPlugin $result |
|
|
|
* @staticvar LtiProviderPlugin $result |
|
|
|
* @return LtiProviderPlugin |
|
|
|
* @return LtiProviderPlugin |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
public static function create() |
|
|
|
public static function create(): LtiProviderPlugin |
|
|
|
{ |
|
|
|
{ |
|
|
|
static $result = null; |
|
|
|
static $result = null; |
|
|
|
|
|
|
|
|
|
|
|
return $result ?: $result = new self(); |
|
|
|
return $result ?: $result = new self(); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public static function isInstructor() |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
api_is_allowed_to_edit(false, true); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* Get the plugin directory name |
|
|
|
* Get the plugin directory name |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
public function get_name() |
|
|
|
public function get_name(): string |
|
|
|
{ |
|
|
|
{ |
|
|
|
return 'lti_provider'; |
|
|
|
return 'lti_provider'; |
|
|
|
} |
|
|
|
} |
|
|
|
@ -85,7 +110,7 @@ class LtiProviderPlugin extends Plugin |
|
|
|
$message = get_lang('ErrorCreatingDir').': '.$pluginEntityPath; |
|
|
|
$message = get_lang('ErrorCreatingDir').': '.$pluginEntityPath; |
|
|
|
Display::addFlash(Display::return_message($message, 'error')); |
|
|
|
Display::addFlash(Display::return_message($message, 'error')); |
|
|
|
|
|
|
|
|
|
|
|
return false; |
|
|
|
return; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
mkdir($pluginEntityPath, api_get_permissions_for_new_directories()); |
|
|
|
mkdir($pluginEntityPath, api_get_permissions_for_new_directories()); |
|
|
|
@ -98,78 +123,25 @@ class LtiProviderPlugin extends Plugin |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* Save configuration for plugin. |
|
|
|
* @return string |
|
|
|
* |
|
|
|
|
|
|
|
* Generate a new key pair for platform when enabling plugin. |
|
|
|
|
|
|
|
* |
|
|
|
|
|
|
|
* @throws \Doctrine\ORM\OptimisticLockException |
|
|
|
|
|
|
|
* |
|
|
|
|
|
|
|
* @return $this|Plugin |
|
|
|
|
|
|
|
*/ |
|
|
|
|
|
|
|
public function performActionsAfterConfigure() |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
$em = Database::getManager(); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** @var PlatformKey $platformKey */ |
|
|
|
|
|
|
|
$platformKey = $em |
|
|
|
|
|
|
|
->getRepository('ChamiloPluginBundle:LtiProvider\PlatformKey') |
|
|
|
|
|
|
|
->findOneBy([]); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if ($this->get('enabled') === 'true') { |
|
|
|
|
|
|
|
if (!$platformKey) { |
|
|
|
|
|
|
|
$platformKey = new PlatformKey(); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
$keyPair = self::generatePlatformKeys(); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
$platformKey->setKid($keyPair['kid']); |
|
|
|
|
|
|
|
$platformKey->publicKey = $keyPair['public']; |
|
|
|
|
|
|
|
$platformKey->setPrivateKey($keyPair['private']); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
$em->persist($platformKey); |
|
|
|
|
|
|
|
} else { |
|
|
|
|
|
|
|
if ($platformKey) { |
|
|
|
|
|
|
|
$em->remove($platformKey); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
$em->flush(); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return $this; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
|
|
* Unistall plugin. Clear the database |
|
|
|
|
|
|
|
*/ |
|
|
|
*/ |
|
|
|
public function uninstall() |
|
|
|
public function getEntityPath(): string |
|
|
|
{ |
|
|
|
{ |
|
|
|
$pluginEntityPath = $this->getEntityPath(); |
|
|
|
return api_get_path(SYS_PATH).'src/Chamilo/PluginBundle/Entity/'.$this->getCamelCaseName(); |
|
|
|
$fs = new Filesystem(); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if ($fs->exists($pluginEntityPath)) { |
|
|
|
|
|
|
|
$fs->remove($pluginEntityPath); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
try { |
|
|
|
|
|
|
|
$this->dropPluginTables(); |
|
|
|
|
|
|
|
} catch (DBALException $e) { |
|
|
|
|
|
|
|
error_log('Error while uninstalling IMS/LTI plugin: '.$e->getMessage()); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* Creates the plugin tables on database |
|
|
|
* Creates the plugin tables on database |
|
|
|
* |
|
|
|
* |
|
|
|
* @return boolean |
|
|
|
* @return void |
|
|
|
* @throws DBALException |
|
|
|
|
|
|
|
*/ |
|
|
|
*/ |
|
|
|
private function createPluginTables() |
|
|
|
private function createPluginTables(): void |
|
|
|
{ |
|
|
|
{ |
|
|
|
$entityManager = Database::getManager(); |
|
|
|
$entityManager = Database::getManager(); |
|
|
|
$connection = $entityManager->getConnection(); |
|
|
|
$connection = $entityManager->getConnection(); |
|
|
|
|
|
|
|
|
|
|
|
if ($connection->getSchemaManager()->tablesExist(self::TABLE_PLATFORM)) { |
|
|
|
if ($connection->getSchemaManager()->tablesExist(self::TABLE_PLATFORM)) { |
|
|
|
return true; |
|
|
|
return; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
$queries = [ |
|
|
|
$queries = [ |
|
|
|
@ -190,64 +162,55 @@ class LtiProviderPlugin extends Plugin |
|
|
|
public_key LONGTEXT NOT NULL, |
|
|
|
public_key LONGTEXT NOT NULL, |
|
|
|
private_key LONGTEXT NOT NULL, |
|
|
|
private_key LONGTEXT NOT NULL, |
|
|
|
PRIMARY KEY(id) |
|
|
|
PRIMARY KEY(id) |
|
|
|
) DEFAULT CHARACTER SET utf8 COLLATE `utf8_unicode_ci` ENGINE = InnoDB" |
|
|
|
) DEFAULT CHARACTER SET utf8 COLLATE `utf8_unicode_ci` ENGINE = InnoDB", |
|
|
|
]; |
|
|
|
]; |
|
|
|
|
|
|
|
|
|
|
|
foreach ($queries as $query) { |
|
|
|
foreach ($queries as $query) { |
|
|
|
Database::query($query); |
|
|
|
Database::query($query); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
return true; |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* Drops the plugin tables on database |
|
|
|
* Save configuration for plugin. |
|
|
|
* |
|
|
|
* |
|
|
|
* @return boolean |
|
|
|
* Generate a new key pair for platform when enabling plugin. |
|
|
|
|
|
|
|
* |
|
|
|
|
|
|
|
* @throws OptimisticLockException |
|
|
|
|
|
|
|
* @throws \Doctrine\ORM\ORMException |
|
|
|
|
|
|
|
* |
|
|
|
|
|
|
|
* @return $this|Plugin |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
private function dropPluginTables() |
|
|
|
public function performActionsAfterConfigure() |
|
|
|
{ |
|
|
|
{ |
|
|
|
Database::query("DROP TABLE IF EXISTS plugin_lti_provider_platform"); |
|
|
|
$em = Database::getManager(); |
|
|
|
Database::query("DROP TABLE IF EXISTS plugin_lti_provider_platform_key"); |
|
|
|
|
|
|
|
return true; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** @var PlatformKey $platformKey */ |
|
|
|
* @return string |
|
|
|
$platformKey = $em |
|
|
|
*/ |
|
|
|
->getRepository('ChamiloPluginBundle:LtiProvider\PlatformKey') |
|
|
|
public function getEntityPath() |
|
|
|
->findOneBy([]); |
|
|
|
{ |
|
|
|
|
|
|
|
return api_get_path(SYS_PATH).'src/Chamilo/PluginBundle/Entity/'.$this->getCamelCaseName(); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public static function isInstructor() |
|
|
|
if ($this->get('enabled') === 'true') { |
|
|
|
{ |
|
|
|
if (!$platformKey) { |
|
|
|
api_is_allowed_to_edit(false, true); |
|
|
|
$platformKey = new PlatformKey(); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
$keyPair = self::generatePlatformKeys(); |
|
|
|
* @return null|SimpleXMLElement |
|
|
|
|
|
|
|
*/ |
|
|
|
|
|
|
|
private function getRequestXmlElement() |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
$request = file_get_contents("php://input"); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (empty($request)) { |
|
|
|
$platformKey->setKid($keyPair['kid']); |
|
|
|
return null; |
|
|
|
$platformKey->publicKey = $keyPair['public']; |
|
|
|
} |
|
|
|
$platformKey->setPrivateKey($keyPair['private']); |
|
|
|
|
|
|
|
|
|
|
|
return new SimpleXMLElement($request); |
|
|
|
$em->persist($platformKey); |
|
|
|
|
|
|
|
} else { |
|
|
|
|
|
|
|
if ($platformKey) { |
|
|
|
|
|
|
|
$em->remove($platformKey); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
|
|
* @param array $params |
|
|
|
|
|
|
|
*/ |
|
|
|
|
|
|
|
public function trimParams(array &$params) |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
foreach ($params as $key => $value) { |
|
|
|
|
|
|
|
$newValue = preg_replace('/\s+/', ' ', $value); |
|
|
|
|
|
|
|
$params[$key] = trim($newValue); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
$em->flush(); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return $this; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
@ -257,7 +220,7 @@ class LtiProviderPlugin extends Plugin |
|
|
|
* |
|
|
|
* |
|
|
|
* @return array |
|
|
|
* @return array |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
private static function generatePlatformKeys() |
|
|
|
private static function generatePlatformKeys(): array |
|
|
|
{ |
|
|
|
{ |
|
|
|
// Create the private and public key |
|
|
|
// Create the private and public key |
|
|
|
$res = openssl_pkey_new( |
|
|
|
$res = openssl_pkey_new( |
|
|
|
@ -283,19 +246,62 @@ class LtiProviderPlugin extends Plugin |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* Get the public key |
|
|
|
* Unistall plugin. Clear the database |
|
|
|
|
|
|
|
*/ |
|
|
|
|
|
|
|
public function uninstall() |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
$pluginEntityPath = $this->getEntityPath(); |
|
|
|
|
|
|
|
$fs = new Filesystem(); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if ($fs->exists($pluginEntityPath)) { |
|
|
|
|
|
|
|
$fs->remove($pluginEntityPath); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
try { |
|
|
|
|
|
|
|
$this->dropPluginTables(); |
|
|
|
|
|
|
|
} catch (DBALException $e) { |
|
|
|
|
|
|
|
error_log('Error while uninstalling IMS/LTI plugin: '.$e->getMessage()); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
|
|
* Drops the plugin tables on database |
|
|
|
* |
|
|
|
* |
|
|
|
* @return string |
|
|
|
* @return boolean |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
public function getPublicKey() { |
|
|
|
private function dropPluginTables(): bool |
|
|
|
$publicKey = ''; |
|
|
|
{ |
|
|
|
$platformKey = Database::getManager() |
|
|
|
Database::query("DROP TABLE IF EXISTS plugin_lti_provider_platform"); |
|
|
|
->getRepository('ChamiloPluginBundle:LtiProvider\PlatformKey') |
|
|
|
Database::query("DROP TABLE IF EXISTS plugin_lti_provider_platform_key"); |
|
|
|
->findOneBy([]); |
|
|
|
|
|
|
|
if ($platformKey) { |
|
|
|
return true; |
|
|
|
$publicKey = $platformKey->getPublicKey(); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
return $publicKey; |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
|
|
* @param array $params |
|
|
|
|
|
|
|
*/ |
|
|
|
|
|
|
|
public function trimParams(array &$params) |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
foreach ($params as $key => $value) { |
|
|
|
|
|
|
|
$newValue = preg_replace('/\s+/', ' ', $value); |
|
|
|
|
|
|
|
$params[$key] = trim($newValue); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
|
|
* @throws Exception |
|
|
|
|
|
|
|
* |
|
|
|
|
|
|
|
* @return null|SimpleXMLElement |
|
|
|
|
|
|
|
*/ |
|
|
|
|
|
|
|
private function getRequestXmlElement(): ?SimpleXMLElement |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
$request = file_get_contents("php://input"); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (empty($request)) { |
|
|
|
|
|
|
|
return null; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return new SimpleXMLElement($request); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|