From 97a6bab4a5a2aa11339ac8ef3a0d27d9fd4f43cc Mon Sep 17 00:00:00 2001 From: Angel Fernando Quiroz Campos <1697880+AngelFQC@users.noreply.github.com> Date: Sat, 12 Oct 2024 23:59:28 -0500 Subject: [PATCH 1/3] Compilatio: Refactoring to use RESTful api - refs BT#22064 --- main/inc/lib/Compilatio.php | 597 ++++++------------ main/plagiarism/compilatio/compiladmin.php | 34 +- .../plagiarism/compilatio/compilatio_ajax.php | 18 +- main/plagiarism/compilatio/upload.php | 106 +--- main/work/work.lib.php | 33 +- 5 files changed, 273 insertions(+), 515 deletions(-) diff --git a/main/inc/lib/Compilatio.php b/main/inc/lib/Compilatio.php index 42bb007fa1f..6e3b780ad1f 100644 --- a/main/inc/lib/Compilatio.php +++ b/main/inc/lib/Compilatio.php @@ -2,6 +2,9 @@ /* For licensing terms, see /license.txt */ +use GuzzleHttp\Client; +use GuzzleHttp\Psr7\Utils; + /** * Build the communication with the SOAP server Compilatio.net * call several methods for the file management in Compilatio.net. @@ -12,6 +15,10 @@ class Compilatio { /** Identification key for the Compilatio account*/ public $key; + /** + * @var string + */ + protected $baseUrl; /** Webservice connection*/ public $soapcli; private $transportMode; @@ -22,165 +29,92 @@ class Compilatio private $proxyHost; private $proxyPort; + /** + * @var Client + */ + public $client; + /** * Compilatio constructor. */ public function __construct() { - if (empty(api_get_configuration_value('allow_compilatio_tool')) || - empty(api_get_configuration_value('compilatio_tool')) - ) { - throw new Exception('Compilatio not available'); - } + $settings = $this->getSettings(); - $settings = api_get_configuration_value('compilatio_tool'); - - if (isset($settings['settings'])) { - $settings = $settings['settings']; - } else { - throw new Exception('Compilatio config available'); - } - - $key = $this->key = $settings['key']; - $urlsoap = $settings['soap_url']; - $proxyHost = $this->proxyHost = $settings['proxy_host']; - $proxyPort = $this->proxyPort = $settings['proxy_port']; $this->transportMode = $settings['transport_mode']; $this->maxFileSize = $settings['max_filesize']; $this->wgetUri = $settings['wget_uri']; $this->wgetLogin = $settings['wget_login']; $this->wgetPassword = $settings['wget_password']; - $soapVersion = 2; + $this->key = $settings['key']; + $this->baseUrl = $settings['soap_url']; - try { - if (!empty($key)) { - $this->key = $key; - if (!empty($urlsoap)) { - if (!empty($proxyHost)) { - $param = [ - 'trace' => false, - 'soap_version' => $soapVersion, - 'exceptions' => true, - 'proxy_host' => '"'.$proxyHost.'"', - 'proxy_port' => $proxyPort, - ]; - } else { - $param = [ - 'trace' => false, - 'soap_version' => $soapVersion, - 'exceptions' => true, - ]; - } - $this->soapcli = new SoapClient($urlsoap, $param); - } else { - throw new Exception('WS urlsoap not available'); - } - } else { - throw new Exception('API key not available'); - } - } catch (SoapFault $fault) { - $this->soapcli = "Error constructor compilatio $fault->faultcode $fault->faultstring "; - } catch (Exception $e) { - $this->soapcli = "Error constructor compilatio with urlsoap $urlsoap ".$e->getMessage(); + if (!empty($settings['proxy_host'])) { + $this->proxyHost = $settings['proxy_host']; + $this->proxyPort = $settings['proxy_port']; } - } - - /** - * @return string - */ - public function getKey() - { - return $this->key; - } - /** - * @param mixed $key - * - * @return Compilatio - */ - public function setKey($key) - { - $this->key = $key; + $clientConfig = [ + 'base_uri' => api_remove_trailing_slash($this->baseUrl).'/', + 'headers' => [ + 'X-Auth-Token' => $this->key, + 'Accept' => 'application/json', + ], + ]; - return $this; - } + if ($this->proxyPort) { + $clientConfig['proxy'] = $this->proxyHost.':'.$this->proxyPort; + } - /** - * @return mixed - */ - public function getTransportMode() - { - return $this->transportMode; + $this->client = new Client($clientConfig); } /** - * @param mixed $transportMode - * - * @return Compilatio + * @throws Exception */ - public function setTransportMode($transportMode) + protected function getSettings(): array { - $this->transportMode = $transportMode; - - return $this; - } + if (empty(api_get_configuration_value('allow_compilatio_tool')) || + empty(api_get_configuration_value('compilatio_tool')) + ) { + throw new Exception('Compilatio not available'); + } - /** - * @return mixed - */ - public function getMaxFileSize() - { - return $this->maxFileSize; - } + $compilatioTool = api_get_configuration_value('compilatio_tool'); - /** - * @param mixed $maxFileSize - * - * @return Compilatio - */ - public function setMaxFileSize($maxFileSize) - { - $this->maxFileSize = $maxFileSize; + if (!isset($compilatioTool['settings'])) { + throw new Exception('Compilatio config available'); + } - return $this; - } + $settings = $compilatioTool['settings']; - /** - * @return mixed - */ - public function getWgetUri() - { - return $this->wgetUri; - } + if (empty($settings['key'])) { + throw new Exception('API key not available'); + } - /** - * @param mixed $wgetUri - * - * @return Compilatio - */ - public function setWgetUri($wgetUri) - { - $this->wgetUri = $wgetUri; + if (empty($settings['soap_url'])) { + throw new Exception('WS urlsoap not available'); + } - return $this; + return $settings; } /** - * @return mixed + * @return string */ - public function getWgetLogin() + public function getKey() { - return $this->wgetLogin; + return $this->key; } /** - * @param mixed $wgetLogin + * @param mixed $key * * @return Compilatio */ - public function setWgetLogin($wgetLogin) + public function setKey($key) { - $this->wgetLogin = $wgetLogin; + $this->key = $key; return $this; } @@ -188,21 +122,9 @@ public function setWgetLogin($wgetLogin) /** * @return mixed */ - public function getWgetPassword() - { - return $this->wgetPassword; - } - - /** - * @param mixed $wgetPassword - * - * @return Compilatio - */ - public function setWgetPassword($wgetPassword) + public function getMaxFileSize() { - $this->wgetPassword = $wgetPassword; - - return $this; + return $this->maxFileSize; } /** @@ -213,18 +135,6 @@ public function getProxyHost() return $this->proxyHost; } - /** - * @param mixed $proxyHost - * - * @return Compilatio - */ - public function setProxyHost($proxyHost) - { - $this->proxyHost = $proxyHost; - - return $this; - } - /** * @return mixed */ @@ -233,161 +143,144 @@ public function getProxyPort() return $this->proxyPort; } - /** - * @param mixed $proxyPort - * - * @return Compilatio - */ - public function setProxyPort($proxyPort) - { - $this->proxyPort = $proxyPort; - - return $this; - } - /** * Method for the file load. - * - * @param $title - * @param $description - * @param $filename - * @param $mimeType - * @param $content - * - * @return string */ - public function sendDoc($title, $description, $filename, $mimeType, $content) - { - try { - if (!is_object($this->soapcli)) { - return "Error in constructor compilatio() $this->soapcli"; - } - - return $this->soapcli->__call( - 'addDocumentBase64', + public function sendDoc( + string $title, + string $description, + string $filename, + string $filepath + ) { + $user = api_get_user_entity(api_get_user_id()); + + $postData = [ + 'folder_id' => '', + 'title' => $title, + 'filename' => basename($filename), + 'indexed' => 'true', + 'user_notes' => [ + 'description' => $description + ], + 'authors' => [ [ - $this->key, - utf8_encode(urlencode($title)), - utf8_encode(urlencode($description)), - utf8_encode(urlencode($filename)), - utf8_encode($mimeType), - base64_encode($content), + 'firstname' => $user->getFirstname(), + 'lastname' => $user->getlastname(), + 'email_address' => $user->getEmail(), ] - ); - } catch (SoapFault $fault) { - return "Erreur sendDoc()".$fault->faultcode." ".$fault->faultstring; + ], + 'depositor' => [ + 'firstname' => $user->getFirstname(), + 'lastname' => $user->getlastname(), + 'email_address' => $user->getEmail(), + ] + ]; + + try { + $responseBody = $this->client + ->post( + 'private/documents', + [ + 'multipart' => [ + [ + 'name' => 'postData', + 'contents' => json_encode($postData) + ], + [ + 'name' => 'file', + 'contents' => Utils::tryFopen($filepath, 'r'), + ] + ] + ] + ) + ->getBody() + ; + } catch (Exception $e) { + throw new Exception($e->getMessage()); } + + $body = json_decode((string) $responseBody, true); + + return $body['data']['document']['id']; } /** * Method for recover a document's information. * - * @param $compiHash - * - * @return string + * @throws Exception */ - public function getDoc($compiHash) + public function getDoc(string $documentId): array { try { - if (!is_object($this->soapcli)) { - return "Error in constructor compilatio() ".$this->soapcli; - } - $param = [$this->key, $compiHash]; - - return $this->soapcli->__call('getDocument', $param); - } catch (SoapFault $fault) { - return "Erreur getDoc()".$fault->faultcode." ".$fault->faultstring; + $responseBody = $this->client + ->get( + "private/documents/$documentId" + ) + ->getBody() + ; + } catch (Exception $e) { + throw new Exception($e->getMessage()); } - } - /** - * method for recover an url document's report. - * - * @param $compiHash - * - * @return string - */ - public function getReportUrl($compiHash) - { - try { - if (!is_object($this->soapcli)) { - return "Error in constructor compilatio() ".$this->soapcli; - } - $param = [$this->key, $compiHash]; + $responseJson = json_decode((string) $responseBody, true); + $dataDocument = $responseJson['data']['document']; - return $this->soapcli->__call('getDocumentReportUrl', $param); - } catch (SoapFault $fault) { - return "Erreur getReportUrl()".$fault->faultcode." ".$fault->faultstring; + $documentInfo = [ + 'report_url' => $dataDocument['report_url'], + ]; + + if (isset($dataDocument['analyses']['anasim']['state'])) { + $documentInfo['analysis_status'] = $dataDocument['analyses']['anasim']['state']; } + + if (isset($dataDocument['light_reports']['anasim']['scores']['global_score_percent'])) { + $documentInfo['report_percent'] = $dataDocument['light_reports']['anasim']['scores']['global_score_percent']; + } + + return $documentInfo; } /** * Method for deleting a Compialtio's account document. - * - * @param $compiHash - * - * @return string */ - public function deldoc($compiHash) + public function deldoc(string $documentId) { - try { - if (!is_object($this->soapcli)) { - return "Error in constructor compilatio() ".$this->soapcli; - } - $param = [$this->key, $compiHash]; - $this->soapcli->__call('deleteDocument', $param); - } catch (SoapFault $fault) { - return "Erreur deldoc()".$fault->faultcode." ".$fault->faultstring; - } + } /** * Method for start the analysis for a document. * - * @param $compiHash - * - * @return string + * @throws Exception */ - public function startAnalyse($compiHash) + public function startAnalyse(string $compilatioId): string { try { - if (!is_object($this->soapcli)) { - return "Error in constructor compilatio() ".$this->soapcli; - } - $param = [$this->key, $compiHash]; - $this->soapcli->__call('startDocumentAnalyse', $param); - } catch (SoapFault $fault) { - return "Erreur startAnalyse()".$fault->faultcode." ".$fault->faultstring; + $responseBody = $this->client + ->post( + 'private/analyses', + [ + 'json' => [ + 'doc_id' => $compilatioId, + 'recipe_name' => 'anasim' + ], + ] + ) + ->getBody() + ; + } catch (Exception $e) { + throw new Exception($e->getMessage()); } - } - /** - * Method for recover the account's quota. - * - * @return string - */ - public function getQuotas() - { - try { - if (!is_object($this->soapcli)) { - return "Error in constructor compilatio() ".$this->soapcli; - } - $param = [$this->key]; + $body = json_decode((string) $responseBody, true); - return $this->soapcli->__call('getAccountQuotas', $param); - } catch (SoapFault $fault) { - return "Erreur getQuotas()".$fault->faultcode." ".$fault->faultstring; - } + return $body['data']['analysis']['state']; } /** * Method for identify a file extension and the possibility that the document can be managed by Compilatio. - * - * @param $filename - * - * @return bool */ - public static function verifiFileType($filename) + public static function verifiFileType(string $filename): bool { $types = ['doc', 'docx', 'rtf', 'xls', 'xlsx', 'ppt', 'pptx', 'odt', 'pdf', 'txt', 'htm', 'html']; $extension = substr($filename, strrpos($filename, '.') + 1); @@ -396,105 +289,39 @@ public static function verifiFileType($filename) return in_array($extension, $types); } - /** - * Fonction affichage de la barre de progression d'analyse version 3.1. - * - * @param string $status From the document - * @param int $pour - * @param array $text Array includes the extract from the text - * - * @return string - */ - public static function getProgressionAnalyseDocv31($status, $pour = 0, $text = []) - { - $loading = Display::returnFontAwesomeIcon('spinner', null, true, 'fa-spin'); - $loading .= ' '; - - switch ($status) { - case 'ANALYSE_IN_QUEUE': - $content = $loading.$text['analysisinqueue']; - break; - case 'ANALYSE_PROCESSING': - $content = $loading.$text['analysisinfinalization']; - break; - default: - $content = Display::bar_progress($pour, true); - break; - } - - return $content; - } - /** * Method for display the PomprseuilmankBar (% de plagiat). - * - * @param $index - * @param $weakThreshold - * @param $highThreshold - * - * @return string */ public static function getPomprankBarv31( - $index, - $weakThreshold, - $highThreshold - ) { + int $index, + int $weakThreshold, + int $highThreshold + ): string { $index = round($index); - $class = 'error'; + $class = 'danger'; if ($index < $weakThreshold) { $class = 'success'; - } else { - if ($index >= $weakThreshold && $index < $highThreshold) { - $class = 'warning'; - } + } elseif ($index < $highThreshold) { + $class = 'warning'; } return Display::bar_progress($index, true, null, $class); } /** - * Method for validation of hash. - * - * @param string $hash - * - * @return bool + * Function for delete a document of the compilatio table if plagiarismTool is Compilatio. */ - public static function isMd5($hash) + public static function plagiarismDeleteDoc(int $courseId, int $itemId) { - return preg_match('`^[a-f0-9]{32}$`', $hash) || preg_match('`^[a-f0-9]{40}$`', $hash); - } - - /** - * function for delete a document of the compilatio table if plagiarismTool is Compilatio. - * - * @param int $courseId - * @param int $itemId - * - * @return bool - */ - public static function plagiarismDeleteDoc($courseId, $itemId) - { - if (api_get_configuration_value('allow_compilatio_tool') === false) { - return false; + if (api_get_configuration_value('allow_compilatio_tool') !== false) { + $table = Database::get_course_table(TABLE_PLAGIARISM); + $params = [$courseId, $itemId]; + Database::delete($table, ['c_id = ? AND document_id = ?' => $params]); } - - $table = Database::get_course_table(TABLE_PLAGIARISM); - $params = [$courseId, $itemId]; - Database::delete($table, ['c_id = ? AND document_id = ?' => $params]); - - return true; } - /** - * @param int $courseId - * @param int $documentId - * @param int $compilatioId - */ - public function saveDocument($courseId, $documentId, $compilatioId) + public function saveDocument(int $courseId, int $documentId, string $compilatioId) { - $documentId = (int) $documentId; - $courseId = (int) $courseId; - $table = Database::get_course_table(TABLE_PLAGIARISM); $params = [ 'c_id' => $courseId, @@ -504,100 +331,62 @@ public function saveDocument($courseId, $documentId, $compilatioId) Database::insert($table, $params); } - /** - * @param int $documentId - * @param int $courseId - * - * @return string md5 value - */ - public function getCompilatioId($documentId, $courseId) + public function getCompilatioId(int $documentId, int $courseId): ?string { - $documentId = (int) $documentId; - $courseId = (int) $courseId; - $table = Database::get_course_table(TABLE_PLAGIARISM); $sql = "SELECT compilatio_id FROM $table WHERE document_id = $documentId AND c_id= $courseId"; $result = Database::query($sql); $result = Database::fetch_object($result); - if ($result) { - return (string) $result->compilatio_id; - } - - return 0; + return $result ? (string)$result->compilatio_id : null; } - /** - * @param int $workId - * - * @return string - */ - public function giveWorkIdState($workId) + public function giveWorkIdState(int $workId): string { $courseId = api_get_course_int_id(); $compilatioId = $this->getCompilatioId($workId, $courseId); - $actionCompilatio = ''; + // if the compilatio's hash is not a valide hash md5, + // we return à specific status (cf : IsInCompilatio() ) + $actionCompilatio = get_lang('CompilatioDocumentTextNotImage').'
'. + get_lang('CompilatioDocumentNotCorrupt'); $status = ''; if (!empty($compilatioId)) { - if (self::isMd5($compilatioId)) { - // if compilatio_id is a hash md5, we call the function of the compilatio's - // webservice who return the document's status - $soapRes = $this->getDoc($compilatioId); - if (isset($soapRes->documentStatus)) { - $status = $soapRes->documentStatus->status; - } - } else { - // if the compilatio's hash is not a valide hash md5, - // we return à specific status (cf : IsInCompilatio() ) - $status = 'NOT_IN_COMPILATIO'; - $actionCompilatio = get_lang('CompilatioDocumentTextNotImage').'
'. - get_lang('CompilatioDocumentNotCorrupt'); - } + // if compilatio_id is a hash md5, we call the function of the compilatio's + // webservice who return the document's status + $soapRes = $this->getDoc($compilatioId); + $status = $soapRes['analysis_status'] ?? ''; + + $spinnerIcon = Display::returnFontAwesomeIcon('spinner', null, true, 'fa-spin'); switch ($status) { - case 'ANALYSE_COMPLETE': - $urlRapport = $this->getReportUrl($compilatioId); - $actionCompilatio .= self::getPomprankBarv31( - $soapRes->documentStatus->indice, - 10, - 35 - ) + case 'finished': + $actionCompilatio .= self::getPomprankBarv31($soapRes['report_percent'], 10, 35) + .PHP_EOL .Display::url( get_lang('CompilatioAnalysis'), - $urlRapport, + $soapRes['report_url'], ['class' => 'btn btn-primary btn-xs', 'target' => '_blank'] ); break; - case 'ANALYSE_PROCESSING': + case 'running': $actionCompilatio .= "
" .get_lang('CompilatioAnalysisInProgress') ."
"; $actionCompilatio .= "
" .get_lang('CompilatioAnalysisPercentage') ."
"; - $text = []; - $text['analysisinqueue'] = get_lang('CompilatioWaitingAnalysis'); - $text['analysisinfinalization'] = get_lang('CompilatioAnalysisEnding'); - $text['refresh'] = get_lang('Refresh'); - $actionCompilatio .= self::getProgressionAnalyseDocv31( - $status, - $soapRes->documentStatus->progression, - $text - ); + $actionCompilatio .= $spinnerIcon.PHP_EOL .get_lang('CompilatioAnalysisEnding'); break; - case 'ANALYSE_IN_QUEUE': - $loading = Display::returnFontAwesomeIcon('spinner', null, true, 'fa-spin'); - $actionCompilatio .= $loading.' '.get_lang('CompilatioAwaitingAnalysis'); + case 'waiting': + $actionCompilatio .= $spinnerIcon.PHP_EOL.get_lang('CompilatioWaitingAnalysis'); break; - case 'BAD_FILETYPE': - $actionCompilatio .= get_lang('FileFormatNotSupported') - .'
' - .get_lang('CompilatioProtectedPdfVerification'); + case 'canceled': + $actionCompilatio .= get_lang('Cancelled'); break; - case 'BAD_FILESIZE': - $actionCompilatio .= get_lang('UplFileTooBig'); + case 'scheduled': + $actionCompilatio .= $spinnerIcon.PHP_EOL.get_lang('CompilatioAwaitingAnalysis'); break; } } diff --git a/main/plagiarism/compilatio/compiladmin.php b/main/plagiarism/compilatio/compiladmin.php index b0a2c18db64..cd75942caec 100644 --- a/main/plagiarism/compilatio/compiladmin.php +++ b/main/plagiarism/compilatio/compiladmin.php @@ -3,12 +3,8 @@ exit; -ini_set('soap.wsdl_cache_enabled', 0); -ini_set('default_socket_timeout', '1000'); - require_once '../../inc/global.inc.php'; -$compilatio = new Compilatio(); $use_space = number_format($quotas->usedSpace / 1000000, 2); $total_space = $quotas->space / 1000000; @@ -33,27 +29,31 @@ } else { echo get_lang('CompilatioConnectionTestSoap')."
"; echo "1) ".get_lang('CompilatioServerConnection')."
"; - $compilatio = new Compilatio(); + + try { + $compilatio = new Compilatio(); + } catch (Exception $e) { + $compilatio = null; + echo get_lang('CompilatioNoConnection')."
"; + echo get_lang('CompilatioParamVerification')."
"; + } + if ($compilatio) { echo get_lang('CompilatioConnectionSuccessful')."
"; echo "2) ".get_lang('CompilatioSendTextToServer')."
"; $text = sprintf(get_lang('CompilatioTextSendingTestKeyX'), $compilatio->getKey()); - $id_compi = $compilatio->SendDoc( - 'Doc de test', - 'test', - 'test', - 'text/plain', - $text - ); - if (Compilatio::isMd5($id_compi)) { + try { + $id_compi = $compilatio->sendDoc( + 'Doc de test', + 'test', + 'test', + $text + ); echo get_lang('CompilatioSuccessfulTransfer')."
"; - } else { + } catch (Exception $e) { echo get_lang('CompilatioFailedTransfer')."
"; echo get_lang('CompilatioParamVerification')."
"; } - } else { - echo get_lang('CompilatioNoConnection')."
"; - echo get_lang('CompilatioParamVerification')."
"; } } ?> diff --git a/main/plagiarism/compilatio/compilatio_ajax.php b/main/plagiarism/compilatio/compilatio_ajax.php index b7433b806a4..da88f2d0270 100644 --- a/main/plagiarism/compilatio/compilatio_ajax.php +++ b/main/plagiarism/compilatio/compilatio_ajax.php @@ -8,12 +8,18 @@ if (isset($_GET['workid'])) { $workIdList = $_GET['workid']; // list of workid separate by the : $workList = explode('a', $workIdList); - $compilatio = new Compilatio(); - $result = ''; - foreach ($workList as $workId) { - if (!empty($workId)) { - $result .= $compilatio->giveWorkIdState($workId); + $workList = array_map('intval', $workList); + $workList = array_filter($workList); + try { + $compilatio = new Compilatio(); + $result = ''; + foreach ($workList as $workId) { + if (!empty($workId)) { + $result .= $compilatio->giveWorkIdState($workId); + } } + echo $result; + } catch (Exception $e) { + echo $e->getMessage(); } - echo $result; } diff --git a/main/plagiarism/compilatio/upload.php b/main/plagiarism/compilatio/upload.php index a84cdd768df..e356474fdb5 100644 --- a/main/plagiarism/compilatio/upload.php +++ b/main/plagiarism/compilatio/upload.php @@ -14,7 +14,14 @@ $courseId = api_get_course_int_id(); $courseInfo = api_get_course_info(); -$compilatio = new Compilatio(); + +try { + $compilatio = new Compilatio(); +} catch (Exception $e) { + $message = Display::return_message($e->getMessage(), 'error'); + + exit($message); +} /* if we have to upload severals documents*/ if (isset($_REQUEST['type']) && 'multi' === $_REQUEST['type']) { @@ -39,47 +46,22 @@ $sqlResult = Database::query($query); $doc = Database::fetch_object($sqlResult); if ($doc) { - /*We load the document in compilatio through the webservice */ - $currentCourseRepositoryWeb = api_get_path(WEB_COURSE_PATH).$courseInfo['path'].'/'; - $WrkUrl = $currentCourseRepositoryWeb.$doc->url; - $LocalWrkUrl = $courseInfo['course_sys_path'].$doc->url; - $mime = DocumentManager::file_get_mime_type($doc->title); - if ('wget' === $compilatio->getTransportMode()) { - /*Compilatio's server recover tjre file throught wget like this: - username:password@http://somedomain.com/reg/remotefilename.tar.gz */ - if (strlen($compilatio->getWgetUri()) > 2) { - $filename = preg_replace( - '/$', - '', - $compilatio->getWgetUri() - ).'/'.$courseInfo['path'].'/'.$doc->url; - } else { - $filename = $WrkUrl; - } - if (strlen($compilatio->getWgetLogin()) > 2) { - $filename = $compilatio->getWgetLogin().':'.$compilatio->getWgetPassword().'@'.$filename; - } - $mime = 'text/plain'; - $compilatioId = $compilatio->sendDoc($doc->title, '', $filename, $mime, 'get_url'); - } else { - /* we use strictly the SOAP for the data trasmission */ - $pieces = explode('/', $doc->url); - $nbPieces = count($pieces); - $filename = $pieces[$nbPieces - 1]; + try { $compilatioId = $compilatio->sendDoc( $doc->title, - '', - $filename, - $mime, - file_get_contents($LocalWrkUrl) + $doc->description, + $doc->url, + $courseInfo['course_sys_path'].$doc->url ); - } - /*we associate in the database the document chamilo to the document compilatio*/ - /*we verify that the docmuent's id is an hash_md5*/ - if (Compilatio::isMd5($compilatioId)) { + + /*we associate in the database the document chamilo to the document compilatio*/ $compilatio->saveDocument($courseId, $doc->id, $compilatioId); sleep(10); - $soapRes = $compilatio->startAnalyse($compilatioId); + $compilatio->startAnalyse($compilatioId); + } catch (Exception $e) { + $message = Display::return_message($e->getMessage(), 'error'); + + exit($message); } } } @@ -92,14 +74,14 @@ function sendDocument($documentId, $courseInfo) { if (empty($courseInfo)) { - return false; + return; } $courseId = $courseInfo['real_id'] ?? 0; $documentId = (int) $documentId; if (empty($courseId) || empty($documentId)) { - return false; + return; } compilatioUpdateWorkDocument($documentId, $courseId); @@ -108,37 +90,25 @@ function sendDocument($documentId, $courseInfo) WHERE id = $documentId AND c_id= $courseId"; $sqlResult = Database::query($query); $doc = Database::fetch_object($sqlResult); - $currentCourseRepositoryWeb = api_get_path(WEB_COURSE_PATH).$courseInfo['path'].'/'; - $documentUrl = $currentCourseRepositoryWeb.$doc->url; $filePath = $courseInfo['course_sys_path'].$doc->url; - $mime = DocumentManager::file_get_mime_type($doc->title); - $compilatio = new Compilatio(); - if ('wget' === $compilatio->getTransportMode()) { - if (strlen($compilatio->getWgetUri()) > 2) { - $filename = preg_replace('/$', '', $compilatio->getWgetUri()).'/'.$courseInfo['path'].'/'.$doc->title; - } else { - $filename = $documentUrl; - } - if (strlen($compilatio->getWgetLogin()) > 2) { - $filename = $compilatio->getWgetLogin().':'.$compilatio->getWgetPassword().'@'.$filename; - } - $compilatioId = $compilatio->sendDoc($doc->title, '', $filename, 'text/plain', 'get_url'); - } else { - $pieces = explode('/', $doc->url); - $nbPieces = count($pieces); - $filename = $pieces[$nbPieces - 1]; - $compilatioId = $compilatio->sendDoc($doc->title, '', $filename, $mime, file_get_contents($filePath)); - } + try { + $compilatio = new Compilatio(); + + $compilatioId = $compilatio->sendDoc( + $doc->title, + $doc->description, + $doc->url, + $filePath + ); - if (Compilatio::isMd5($compilatioId)) { $compilatio->saveDocument($courseId, $doc->id, $compilatioId); sleep(10); $compilatio->startAnalyse($compilatioId); - echo Display::return_message(get_lang('Uploaded')); - } else { - echo Display::return_message(get_lang('Error'), 'error'); + echo Display::return_message(get_lang('Uploaded'), 'success'); + } catch (Exception $e) { + echo Display::return_message($e->getMessage(), 'error'); } } @@ -208,16 +178,6 @@ function getWorkFolder($txt) return $res; } -function getShortFilename($txt) -{ - $res = $txt; - if (strlen($txt) > 10) { - $res = substr($txt, 0, 10); - } - - return $res; -} - function compilatioUpdateWorkDocument($docId, $courseId) { $_course = api_get_course_info(); diff --git a/main/work/work.lib.php b/main/work/work.lib.php index 20bea3dcae5..8357a32e0ac 100755 --- a/main/work/work.lib.php +++ b/main/work/work.lib.php @@ -2101,16 +2101,17 @@ function get_work_user_list( $group_id = api_get_group_id(); $course_info = api_get_course_info(); $course_info = empty($course_info) ? api_get_course_info_by_id($courseId) : $course_info; - $course_id = isset($course_info['real_id']) ? $course_info['real_id'] : $courseId; + $course_id = (int) ($course_info['real_id'] ?? $courseId); $work_id = (int) $work_id; $start = (int) $start; $limit = (int) $limit; $column = !empty($column) ? Database::escape_string($column) : 'sent_date'; - $compilation = null; - if (api_get_configuration_value('allow_compilatio_tool')) { - $compilation = new Compilatio(); + try { + $compilatio = new Compilatio(); + } catch (Exception $e) { + $compilatio = false; } if (!in_array($direction, ['asc', 'desc'])) { @@ -2281,7 +2282,7 @@ function get_work_user_list( } while ($work = Database::fetch_array($result, 'ASSOC')) { - $item_id = $work['id']; + $item_id = (int) $work['id']; $dbTitle = $work['title']; // Get the author ID for that document from the item_property table $is_author = false; @@ -2557,8 +2558,8 @@ class="work_correction_file_upload file_upload_small fileinput-button" $work['actions'] = '
'.$linkToDownload.$action.'
'; $work['correction'] = $correction; - if (!empty($compilation) && $is_allowed_to_edit) { - $compilationId = $compilation->getCompilatioId($item_id, $course_id); + if ($compilatio && $is_allowed_to_edit) { + $compilationId = $compilatio->getCompilatioId($item_id, $course_id); if ($compilationId) { $actionCompilatio = "
".$loading.' '.get_lang('CompilatioConnectionWithServer').'
'; @@ -2566,7 +2567,7 @@ class="work_correction_file_upload file_upload_small fileinput-button" $workDirectory = api_get_path(SYS_COURSE_PATH).$course_info['directory']; if (!Compilatio::verifiFileType($dbTitle)) { $actionCompilatio = get_lang('FileFormatNotSupported'); - } elseif (filesize($workDirectory.'/'.$work['url']) > $compilation->getMaxFileSize()) { + } elseif (filesize($workDirectory.'/'.$work['url']) > $compilatio->getMaxFileSize()) { $sizeFile = round(filesize($workDirectory.'/'.$work['url']) / 1000000); $actionCompilatio = get_lang('UplFileTooBig').': '.format_file_size($sizeFile).'
'; } else { @@ -2688,10 +2689,12 @@ function getAllWork( } $courseQueryToString = implode(' OR ', $courseQuery); - $compilation = null; - /*if (api_get_configuration_value('allow_compilatio_tool')) { - $compilation = new Compilatio(); - }*/ + + //try { + $compilatio = new Compilatio(); + //} catch (Exception $e) { + // $compilatio = null; + //} if ($getCount) { if (empty($courseQuery)) { @@ -3094,8 +3097,8 @@ class="work_correction_file_upload file_upload_small fileinput-button" $work['actions'] = '
'.$linkToDownload.$action.'
'; $work['correction'] = $correction; - if (!empty($compilation)) { - $compilationId = $compilation->getCompilatioId($item_id, $courseId); + if (!empty($compilatio)) { + $compilationId = $compilatio->getCompilatioId($item_id, $courseId); if ($compilationId) { $actionCompilatio = "
".$loading.' '.get_lang('CompilatioConnectionWithServer').'
'; @@ -3103,7 +3106,7 @@ class="work_correction_file_upload file_upload_small fileinput-button" $workDirectory = api_get_path(SYS_COURSE_PATH).$courseInfo['directory']; if (!Compilatio::verifiFileType($dbTitle)) { $actionCompilatio = get_lang('FileFormatNotSupported'); - } elseif (filesize($workDirectory.'/'.$work['url']) > $compilation->getMaxFileSize()) { + } elseif (filesize($workDirectory.'/'.$work['url']) > $compilatio->getMaxFileSize()) { $sizeFile = round(filesize($workDirectory.'/'.$work['url']) / 1000000); $actionCompilatio = get_lang('UplFileTooBig').': '.format_file_size($sizeFile).'
'; } else { From 95a845cea9b136a3ef2a134c9011572567bef1c9 Mon Sep 17 00:00:00 2001 From: Angel Fernando Quiroz Campos <1697880+AngelFQC@users.noreply.github.com> Date: Sun, 13 Oct 2024 00:00:17 -0500 Subject: [PATCH 2/3] Compilatio: Change text length for c_plagiarism_compilatio_docs.compilatio_id column - refs BT#22064 --- main/install/configuration.dist.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/main/install/configuration.dist.php b/main/install/configuration.dist.php index a2dd68ef016..7cb659f9560 100644 --- a/main/install/configuration.dist.php +++ b/main/install/configuration.dist.php @@ -1560,7 +1560,7 @@ id INT AUTO_INCREMENT NOT NULL, c_id int(11) NOT NULL, document_id int(11) NOT NULL, - compilatio_id varchar(32) CHARACTER SET utf8 NOT NULL, + compilatio_id varchar(40) CHARACTER SET utf8 NOT NULL, PRIMARY KEY (id) ) DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci ENGINE = InnoDB; From a6898b1711c3c0e672c3bac4a56177319b859caa Mon Sep 17 00:00:00 2001 From: Angel Fernando Quiroz Campos <1697880+AngelFQC@users.noreply.github.com> Date: Mon, 14 Oct 2024 16:00:11 -0500 Subject: [PATCH 3/3] Compilatio: Clean and update settings to work - refs BT#22064 --- main/inc/lib/Compilatio.php | 17 +++++------------ main/install/configuration.dist.php | 6 +----- 2 files changed, 6 insertions(+), 17 deletions(-) diff --git a/main/inc/lib/Compilatio.php b/main/inc/lib/Compilatio.php index 6e3b780ad1f..c252c6c229a 100644 --- a/main/inc/lib/Compilatio.php +++ b/main/inc/lib/Compilatio.php @@ -20,12 +20,7 @@ class Compilatio */ protected $baseUrl; /** Webservice connection*/ - public $soapcli; - private $transportMode; private $maxFileSize; - private $wgetUri; - private $wgetLogin; - private $wgetPassword; private $proxyHost; private $proxyPort; @@ -36,18 +31,16 @@ class Compilatio /** * Compilatio constructor. + * + * @throws Exception */ public function __construct() { $settings = $this->getSettings(); - $this->transportMode = $settings['transport_mode']; $this->maxFileSize = $settings['max_filesize']; - $this->wgetUri = $settings['wget_uri']; - $this->wgetLogin = $settings['wget_login']; - $this->wgetPassword = $settings['wget_password']; $this->key = $settings['key']; - $this->baseUrl = $settings['soap_url']; + $this->baseUrl = $settings['api_url']; if (!empty($settings['proxy_host'])) { $this->proxyHost = $settings['proxy_host']; @@ -92,8 +85,8 @@ protected function getSettings(): array throw new Exception('API key not available'); } - if (empty($settings['soap_url'])) { - throw new Exception('WS urlsoap not available'); + if (empty($settings['api_url'])) { + throw new Exception('Api URL not available'); } return $settings; diff --git a/main/install/configuration.dist.php b/main/install/configuration.dist.php index 7cb659f9560..80a5eca844e 100644 --- a/main/install/configuration.dist.php +++ b/main/install/configuration.dist.php @@ -1576,14 +1576,10 @@ /*$_configuration['compilatio_tool'] = [ 'settings' => [ 'key' => '', - 'soap_url' => '', + 'api_url' => 'https://app.compilatio.net/api', 'proxy_host' => '', 'proxy_port' => '', 'max_filesize' => '', - 'transport_mode' => '', - 'wget_uri' => '', - 'wget_login' => '', - 'wget_password' => '', ] ];*/