123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136 |
- <?php
- /* For licensing terms, see /license.txt */
- use Chamilo\UserBundle\Entity\User;
- use FFMpeg\FFMpeg;
- use FFMpeg\Format\Audio\Wav;
- $cidReset = true;
- require_once __DIR__.'/../../../main/inc/global.inc.php';
- $action = isset($_POST['action']) ? $_POST['action'] : 'enrollment';
- $isEnrollment = 'enrollment' === $action;
- $isAuthentify = 'authentify' === $action;
- $isAllowed = true;
- if ($isEnrollment) {
- api_block_anonymous_users(false);
- $isAllowed = !empty($_FILES['audio']);
- } elseif ($isAuthentify) {
- $isAllowed = !empty($_POST['username']) && !empty($_FILES['audio']);
- }
- if (!$isAllowed) {
- echo Display::return_message(get_lang('NotAllowed'), 'error');
- exit;
- }
- $plugin = WhispeakAuthPlugin::create();
- $plugin->protectTool(false);
- if ($isAuthentify) {
- $em = Database::getManager();
- /** @var User|null $user */
- $user = $em->getRepository('ChamiloUserBundle:User')->findOneBy(['username' => $_POST['username']]);
- } else {
- /** @var User $user */
- $user = api_get_user_entity(api_get_user_id());
- }
- if (empty($user)) {
- echo Display::return_message(get_lang('NoUser'), 'error');
- exit;
- }
- $path = api_upload_file('whispeakauth', $_FILES['audio'], $user->getId());
- if (false === $path) {
- echo Display::return_message(get_lang('UploadError'), 'error');
- exit;
- }
- $newFullPath = $originFullPath = api_get_path(SYS_UPLOAD_PATH).'whispeakauth'.$path['path_to_save'];
- $fileType = mime_content_type($originFullPath);
- if ('wav' !== substr($fileType, -3)) {
- $directory = dirname($originFullPath);
- $newFullPath = $directory.'/audio.wav';
- try {
- $ffmpeg = FFMpeg::create();
- $audio = $ffmpeg->open($originFullPath);
- $audio->save(new Wav(), $newFullPath);
- } catch (Exception $exception) {
- echo Display::return_message($exception->getMessage(), 'error');
- exit;
- }
- }
- if ($isEnrollment) {
- $result = $plugin->requestEnrollment($user, $newFullPath);
- if (empty($result)) {
- echo Display::return_message($plugin->get_lang('EnrollmentFailed'));
- exit;
- }
- $reliability = (int) $result['reliability'];
- if ($reliability <= 0) {
- echo Display::return_message($plugin->get_lang('EnrollmentSignature0'), 'error');
- exit;
- }
- $plugin->saveEnrollment($user, $result['uid']);
- $message = '<strong>'.$plugin->get_lang('EnrollmentSuccess').'</strong>';
- $message .= PHP_EOL;
- $message .= $plugin->get_lang("EnrollmentSignature$reliability");
- echo Display::return_message($message, 'success', false);
- exit;
- }
- if ($isAuthentify) {
- $result = $plugin->requestAuthentify($user, $newFullPath);
- if (empty($result)) {
- echo Display::return_message($plugin->get_lang('AuthentifyFailed'), 'error');
- exit;
- }
- $success = (bool) $result['audio'][0]['result'];
- if (!$success) {
- echo Display::return_message($plugin->get_lang('TryAgain'), 'warning');
- exit;
- }
- $loggedUser = [
- 'user_id' => $user->getId(),
- 'status' => $user->getStatus(),
- 'uidReset' => true,
- ];
- ChamiloSession::write('_user', $loggedUser);
- Login::init_user($user->getId(), true);
- echo Display::return_message($plugin->get_lang('AuthentifySuccess'), 'success');
- echo '<script>window.location.href = "'.api_get_path(WEB_PATH).'";</script>';
- exit;
- }
|