parent
01e85e5964
commit
d20cbf56cb
@ -0,0 +1,95 @@ |
|||||||
|
<?php |
||||||
|
/* For licensing terms, see /license.txt */ |
||||||
|
|
||||||
|
namespace Chamilo\PluginBundle\WhispeakAuth\Controller; |
||||||
|
|
||||||
|
use Chamilo\UserBundle\Entity\User; |
||||||
|
use FFMpeg\FFMpeg; |
||||||
|
use FFMpeg\Format\Audio\Wav; |
||||||
|
|
||||||
|
/** |
||||||
|
* Class BaseController. |
||||||
|
* |
||||||
|
* @package Chamilo\PluginBundle\WhispeakAuth\Controller |
||||||
|
*/ |
||||||
|
abstract class BaseController |
||||||
|
{ |
||||||
|
/** |
||||||
|
* @var \WhispeakAuthPlugin |
||||||
|
*/ |
||||||
|
protected $plugin; |
||||||
|
|
||||||
|
/** |
||||||
|
* BaseController constructor. |
||||||
|
*/ |
||||||
|
public function __construct() |
||||||
|
{ |
||||||
|
$this->plugin = \WhispeakAuthPlugin::create(); |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* @param array $variables |
||||||
|
*/ |
||||||
|
protected function displayPage(array $variables) |
||||||
|
{ |
||||||
|
global $htmlHeadXtra; |
||||||
|
|
||||||
|
$htmlHeadXtra[] = api_get_js('rtc/RecordRTC.js'); |
||||||
|
$htmlHeadXtra[] = api_get_js_simple(api_get_path(WEB_PLUGIN_PATH).'whispeakauth/assets/js/RecordAudio.js'); |
||||||
|
|
||||||
|
$pageTitle = $this->plugin->get_title(); |
||||||
|
|
||||||
|
$template = new \Template($pageTitle); |
||||||
|
|
||||||
|
foreach ($variables as $key => $value) { |
||||||
|
$template->assign($key, $value); |
||||||
|
} |
||||||
|
|
||||||
|
$pageContent = $template->fetch('whispeakauth/view/record_audio.html.twig'); |
||||||
|
|
||||||
|
$template->assign('header', $pageTitle); |
||||||
|
$template->assign('content', $pageContent); |
||||||
|
$template->display_one_col_template(); |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* @param string $message |
||||||
|
* @param string $type |
||||||
|
*/ |
||||||
|
protected function displayMessage($message, $type) |
||||||
|
{ |
||||||
|
echo \Display::return_message($message, $type); |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* @param \Chamilo\UserBundle\Entity\User $user |
||||||
|
* |
||||||
|
* @throws \Exception |
||||||
|
* @return string |
||||||
|
*/ |
||||||
|
protected function uploadAudioFile(User $user) |
||||||
|
{ |
||||||
|
$pluginName = $this->plugin->get_name(); |
||||||
|
|
||||||
|
$path = api_upload_file($pluginName, $_FILES['audio'], $user->getId()); |
||||||
|
|
||||||
|
if (false === $path) { |
||||||
|
throw new \Exception(get_lang('UploadError')); |
||||||
|
} |
||||||
|
|
||||||
|
$fullPath = api_get_path(SYS_UPLOAD_PATH).$pluginName.$path['path_to_save']; |
||||||
|
$mimeType = mime_content_type($fullPath); |
||||||
|
|
||||||
|
if ('wav' !== substr($mimeType, -3)) { |
||||||
|
$ffmpeg = FFMpeg::create(); |
||||||
|
|
||||||
|
$audioFile = $ffmpeg->open($fullPath); |
||||||
|
|
||||||
|
$fullPath = dirname($fullPath).'/audio.wav'; |
||||||
|
|
||||||
|
$audioFile->save(new Wav(), $fullPath); |
||||||
|
} |
||||||
|
|
||||||
|
return $fullPath; |
||||||
|
} |
||||||
|
} |
@ -1,126 +0,0 @@ |
|||||||
<?php |
|
||||||
/* For licensing terms, see /license.txt */ |
|
||||||
|
|
||||||
namespace Chamilo\PluginBundle\WhispeakAuth\Controller; |
|
||||||
|
|
||||||
use GuzzleHttp\Exception\RequestException; |
|
||||||
|
|
||||||
/** |
|
||||||
* Class CreateEnrollmentRequestController. |
|
||||||
* |
|
||||||
* @package Chamilo\PluginBundle\WhispeakAuth\Controller |
|
||||||
*/ |
|
||||||
class CreateEnrollmentRequestController extends BaseRequestController |
|
||||||
{ |
|
||||||
protected function setUser() |
|
||||||
{ |
|
||||||
$this->user = api_get_user_entity(api_get_user_id()); |
|
||||||
} |
|
||||||
|
|
||||||
/** |
|
||||||
* @return bool |
|
||||||
*/ |
|
||||||
protected function userIsAllowed() |
|
||||||
{ |
|
||||||
return !empty($_FILES['audio']); |
|
||||||
} |
|
||||||
|
|
||||||
/** |
|
||||||
* @throws \Exception |
|
||||||
*/ |
|
||||||
protected function protect() |
|
||||||
{ |
|
||||||
api_block_anonymous_users(false); |
|
||||||
|
|
||||||
parent::protect(); |
|
||||||
} |
|
||||||
|
|
||||||
/** |
|
||||||
* @throws \Doctrine\ORM\OptimisticLockException |
|
||||||
* @throws \Exception |
|
||||||
* |
|
||||||
* @return string |
|
||||||
*/ |
|
||||||
protected function doApiRequest() |
|
||||||
{ |
|
||||||
$token = $this->createSessionToken(); |
|
||||||
$speaker = $this->createEnrollment($token); |
|
||||||
|
|
||||||
$this->plugin->saveEnrollment($this->user, $speaker); |
|
||||||
|
|
||||||
$message = '<strong>'.$this->plugin->get_lang('EnrollmentSuccess').'</strong>'.PHP_EOL; |
|
||||||
|
|
||||||
return \Display::return_message($message, 'success', false); |
|
||||||
} |
|
||||||
|
|
||||||
/** |
|
||||||
* Create a session token to perform an enrollment. |
|
||||||
* |
|
||||||
* @throws \Exception |
|
||||||
* |
|
||||||
* @return string |
|
||||||
*/ |
|
||||||
private function createSessionToken() |
|
||||||
{ |
|
||||||
try { |
|
||||||
$response = $this->httpClient->get( |
|
||||||
'enroll', |
|
||||||
[ |
|
||||||
'headers' => [ |
|
||||||
'Authorization' => "Bearer {$this->apiKey}", |
|
||||||
], |
|
||||||
'json' => [], |
|
||||||
'query' => [ |
|
||||||
'lang' => api_get_language_isocode($this->user->getLanguage()), |
|
||||||
], |
|
||||||
] |
|
||||||
); |
|
||||||
$json = json_decode((string) $response->getBody(), true); |
|
||||||
|
|
||||||
return $json['token']; |
|
||||||
} catch (RequestException $requestException) { |
|
||||||
$this->throwRequestException( |
|
||||||
$requestException, |
|
||||||
$this->plugin->get_lang('EnrollmentFailed') |
|
||||||
); |
|
||||||
} |
|
||||||
} |
|
||||||
|
|
||||||
/** |
|
||||||
* Create a signature associated to a configuration. |
|
||||||
* |
|
||||||
* @param string $token |
|
||||||
* |
|
||||||
* @throws \Exception |
|
||||||
* |
|
||||||
* @return string |
|
||||||
*/ |
|
||||||
private function createEnrollment($token) |
|
||||||
{ |
|
||||||
try { |
|
||||||
$response = $this->httpClient->post( |
|
||||||
'enroll', |
|
||||||
[ |
|
||||||
'headers' => [ |
|
||||||
'Authorization' => "Bearer $token", |
|
||||||
], |
|
||||||
'multipart' => [ |
|
||||||
[ |
|
||||||
'name' => 'file', |
|
||||||
'contents' => fopen($this->audioFilePath, 'r'), |
|
||||||
'filename' => basename($this->audioFilePath), |
|
||||||
], |
|
||||||
], |
|
||||||
] |
|
||||||
); |
|
||||||
$json = json_decode((string) $response->getBody(), true); |
|
||||||
|
|
||||||
return $json['speaker']; |
|
||||||
} catch (RequestException $requestException) { |
|
||||||
$this->throwRequestException( |
|
||||||
$requestException, |
|
||||||
$this->plugin->get_lang('EnrollmentFailed') |
|
||||||
); |
|
||||||
} |
|
||||||
} |
|
||||||
} |
|
@ -0,0 +1,73 @@ |
|||||||
|
<?php |
||||||
|
/* For licensing terms, see /license.txt */ |
||||||
|
|
||||||
|
namespace Chamilo\PluginBundle\WhispeakAuth\Controller; |
||||||
|
|
||||||
|
use Chamilo\PluginBundle\WhispeakAuth\Request\ApiRequest; |
||||||
|
|
||||||
|
/** |
||||||
|
* Class EnrollmentController. |
||||||
|
* |
||||||
|
* @package Chamilo\PluginBundle\WhispeakAuth\Controller |
||||||
|
*/ |
||||||
|
class EnrollmentController extends BaseController |
||||||
|
{ |
||||||
|
/** |
||||||
|
* @throws \Exception |
||||||
|
*/ |
||||||
|
public function index() |
||||||
|
{ |
||||||
|
if (!$this->plugin->toolIsEnabled()) { |
||||||
|
throw new \Exception(get_lang('NotAllowed')); |
||||||
|
} |
||||||
|
|
||||||
|
$user = api_get_user_entity(api_get_user_id()); |
||||||
|
|
||||||
|
$userIsEnrolled = \WhispeakAuthPlugin::checkUserIsEnrolled($user->getId()); |
||||||
|
|
||||||
|
if ($userIsEnrolled) { |
||||||
|
throw new \Exception($this->plugin->get_lang('SpeechAuthAlreadyEnrolled')); |
||||||
|
} |
||||||
|
|
||||||
|
$request = new ApiRequest(); |
||||||
|
$response = $request->createEnrollmentSessionToken($user); |
||||||
|
|
||||||
|
\ChamiloSession::write(\WhispeakAuthPlugin::SESSION_SENTENCE_TEXT, $response['token']); |
||||||
|
|
||||||
|
$this->displayPage( |
||||||
|
[ |
||||||
|
'action' => 'enrollment', |
||||||
|
'sample_text' => $response['text'], |
||||||
|
] |
||||||
|
); |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* @throws \Exception |
||||||
|
*/ |
||||||
|
public function ajax() |
||||||
|
{ |
||||||
|
if (!$this->plugin->toolIsEnabled() || empty($_FILES['audio'])) { |
||||||
|
throw new \Exception(get_lang('NotAllowed')); |
||||||
|
} |
||||||
|
|
||||||
|
$user = api_get_user_entity(api_get_user_id()); |
||||||
|
|
||||||
|
$audioFilePath = $this->uploadAudioFile($user); |
||||||
|
|
||||||
|
$token = \ChamiloSession::read(\WhispeakAuthPlugin::SESSION_SENTENCE_TEXT); |
||||||
|
|
||||||
|
if (empty($token)) { |
||||||
|
throw new \Exception($this->plugin->get_lang('EnrollmentFailed')); |
||||||
|
} |
||||||
|
|
||||||
|
$request = new ApiRequest(); |
||||||
|
$response = $request->createEnrollment($token, $audioFilePath); |
||||||
|
|
||||||
|
\ChamiloSession::erase(\WhispeakAuthPlugin::SESSION_SENTENCE_TEXT); |
||||||
|
|
||||||
|
$this->plugin->saveEnrollment($user, $response['speaker']); |
||||||
|
|
||||||
|
$this->displayMessage($this->plugin->get_lang('EnrollmentSuccess'), 'success'); |
||||||
|
} |
||||||
|
} |
@ -0,0 +1,127 @@ |
|||||||
|
<?php |
||||||
|
/* For licensing terms, see /license.txt */ |
||||||
|
|
||||||
|
namespace Chamilo\PluginBundle\WhispeakAuth\Request; |
||||||
|
|
||||||
|
use Chamilo\UserBundle\Entity\User; |
||||||
|
use GuzzleHttp\Client; |
||||||
|
use GuzzleHttp\Exception\RequestException; |
||||||
|
|
||||||
|
/** |
||||||
|
* Class ApiRequest. |
||||||
|
* |
||||||
|
* @package Chamilo\PluginBundle\WhispeakAuth\Request |
||||||
|
*/ |
||||||
|
class ApiRequest |
||||||
|
{ |
||||||
|
/** |
||||||
|
* @var \WhispeakAuthPlugin |
||||||
|
*/ |
||||||
|
protected $plugin; |
||||||
|
/** |
||||||
|
* @var string |
||||||
|
*/ |
||||||
|
protected $apiKey; |
||||||
|
|
||||||
|
/** |
||||||
|
* BaseController constructor. |
||||||
|
*/ |
||||||
|
public function __construct() |
||||||
|
{ |
||||||
|
$this->plugin = \WhispeakAuthPlugin::create(); |
||||||
|
$this->apiKey = $this->plugin->get(\WhispeakAuthPlugin::SETTING_TOKEN); |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* Create a session token to perform an enrollment. |
||||||
|
* |
||||||
|
* @param \Chamilo\UserBundle\Entity\User $user |
||||||
|
* |
||||||
|
* @throws \Exception |
||||||
|
* |
||||||
|
* @return array |
||||||
|
*/ |
||||||
|
public function createEnrollmentSessionToken(User $user) |
||||||
|
{ |
||||||
|
$apiKey = $this->plugin->get(\WhispeakAuthPlugin::SETTING_TOKEN); |
||||||
|
$langIso = api_get_language_isocode($user->getLanguage()); |
||||||
|
|
||||||
|
return $this->sendRequest( |
||||||
|
'get', |
||||||
|
'enroll', |
||||||
|
$apiKey, |
||||||
|
['lang' => $langIso] |
||||||
|
); |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* @param string $token |
||||||
|
* @param string $audioFilePath |
||||||
|
* |
||||||
|
* @throws \Exception |
||||||
|
* |
||||||
|
* @return array |
||||||
|
*/ |
||||||
|
public function createEnrollment($token, $audioFilePath) |
||||||
|
{ |
||||||
|
return $this->sendRequest( |
||||||
|
'post', |
||||||
|
'enroll', |
||||||
|
$token, |
||||||
|
[], |
||||||
|
[ |
||||||
|
[ |
||||||
|
'name' => 'file', |
||||||
|
'contents' => fopen($audioFilePath, 'r'), |
||||||
|
'filename' => basename($audioFilePath), |
||||||
|
], |
||||||
|
] |
||||||
|
); |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* @param string $method |
||||||
|
* @param string $uri |
||||||
|
* @param string $authBearer |
||||||
|
* @param array $query |
||||||
|
* @param array $multipart |
||||||
|
* |
||||||
|
* @throws \Exception |
||||||
|
* |
||||||
|
* @return array |
||||||
|
*/ |
||||||
|
private function sendRequest($method, $uri, $authBearer, array $query = [], array $multipart = []) |
||||||
|
{ |
||||||
|
$httpClient = new Client(['base_uri' => $this->plugin->getApiUrl()]); |
||||||
|
|
||||||
|
try { |
||||||
|
$responseBody = $httpClient |
||||||
|
->request( |
||||||
|
$method, |
||||||
|
$uri, |
||||||
|
[ |
||||||
|
'headers' => ['Authorization' => "Bearer $authBearer"], |
||||||
|
'query' => $query, |
||||||
|
'multipart' => $multipart, |
||||||
|
] |
||||||
|
) |
||||||
|
->getBody() |
||||||
|
->getContents(); |
||||||
|
|
||||||
|
return json_decode($responseBody, true); |
||||||
|
} catch (RequestException $requestException) { |
||||||
|
if (!$requestException->hasResponse()) { |
||||||
|
throw new \Exception($requestException->getMessage()); |
||||||
|
} |
||||||
|
|
||||||
|
$responseBody = $requestException->getResponse()->getBody()->getContents(); |
||||||
|
$json = json_decode($responseBody, true); |
||||||
|
|
||||||
|
$message = is_array($json['message']) ? implode(PHP_EOL, $json['message']) : $json['message']; |
||||||
|
|
||||||
|
throw new \Exception($message); |
||||||
|
} catch (Exception $exception) { |
||||||
|
throw new \Exception($exception->getMessage()); |
||||||
|
} |
||||||
|
} |
||||||
|
} |
Loading…
Reference in new issue