-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathclass.EphorusApi.php
More file actions
627 lines (557 loc) · 19 KB
/
Copy pathclass.EphorusApi.php
File metadata and controls
627 lines (557 loc) · 19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
<?php
/**
* class.EphorusApi.php - The file to receive data from Ephorus
*
* @package ephorus plagiarism plugin
* @subpackage ephoruscomms
* @author Guido Bonnet
* @copyright 2012 Guido Bonnet http://ephorus.com
*/
include_once(dirname(dirname(__FILE__)).'/class.DLEApi.php');
/**
* EphorusService Class
*
* The class to get and set data for the different Ephorus services.
*
*/
class EphorusService {
const VISIBLE = 1;
const INVISIBLE = 2;
/**
* Constructor
*
* @param bool $logging - overrule function for logging
*/
function __construct($logging = true) {
DLEApi::initialize();
if($logging) {
$this->initLogging();
}
}
function cronAuth() {
DLEApi::cronAuth();
}
function initSoapClient($address, $service = 'visibility') {
$proxy_host = DLEApi::getProxySetting('host');
$proxy_port = DLEApi::getProxySetting('port');
$proxy_username = DLEApi::getProxySetting('username');
$proxy_password = DLEApi::getProxySetting('password');
// Create the client instance.
try {
$soap_client = @new SoapClient($address, array(
'proxy_host' => ($proxy_host) ? $proxy_host : null,
'proxy_port' => ($proxy_port) ? $proxy_port : null,
'proxy_login' => ($proxy_username) ? $proxy_username : null,
'proxy_password' => ($proxy_password) ? $proxy_password : null,
));
if($service != 'visibility') {
$this->trace('... Created soap client');
}
return $soap_client;
} catch (Exception $e) {
$this->log($e->getMessage());
if($service != 'visibility') {
$this->trace($e->getMessage());
}
return false;
}
}
/**
* Function to send documents to Ephorus
*/
function handInService() {
$handin_code = DLEApi::getSetting('handin_code');
if($handin_code == '') {
$this->log('Handin code not found');
$this->trace('... No hand-in code found. Sending of documents terminated.');
return;
}
$this->trace('... Hand-in code: '.$handin_code);
$handin_address = DLEApi::getSetting('handin_address');
if($handin_address == '') {
$this->log('Handin address not found');
$this->trace('... No hand-in address found. Sending of documents terminated.');
return;
}
$this->trace('... Hand-in address: '.$handin_address);
$unsent_documents = DLEApi::getUnsentDocuments();
if(count($unsent_documents) == 0) {
$this->trace('... No documents to be send.');
return;
}
$this->trace('... '.count($unsent_documents).' Documents found. Start sending documents');
$soap_client = $this->initSoapClient($handin_address, 'handin');
if($soap_client == false) {
$this->trace('... Connection problem.');
return;
}
foreach($unsent_documents as $document) {
// Make sure the document has enough time to be sent to Ephorus.
set_time_limit(600);
$this->log('Started hand-in document - '.$document->filename);
if(!$this->isSupportedFiletype($document->filename)) {
$this->log('Cancelled hand-in document - Wrong filetype.');
$this->trace('... Error with document: '.$document->filename.' ('.$document->id.')');
DLEApi::setHandinErrorToDocument($document->id, 'unsupported_file_type');
continue;
}
$handin_parameters = DLEApi::getHandinParameters($document);
if(!$handin_parameters) {
$this->log('Cancelled hand-in document - Wrong parameters.');
continue;
}
$this->trace('... POST DOCUMENT ('.$document->id.'): '.$document->filename.
' with processtype '.$document->processtype);
try {
$result = $soap_client->UploadDocument($handin_parameters);
if($result == false) {
$this->log('Something went wrong while sending document');
$this->trace('... Something went wrong while sending');
} else {
$guid = $result->UploadDocumentResult;
$this->log('Document ('.$document->id.') sent successfully - Guid: '.$guid);
$this->trace('- Succes! GUID: '.$guid);
// Update the documents table with the GUID.
DLEApi::setGUIDtoDocument($document->id, $guid);
}
} catch (SoapFault $result) {
$this->log('Document ('.$document->id.') sent. error received - Error: '.$result->getMessage());
$this->trace('- Error. '.$result->getMessage());
if (strpos($result->getMessage(), 'Wrong file format.') !== false) {
DLEApi::setHandinErrorToDocument($document->id, 'unsupported_file_type');
} else if (strpos($result->getMessage(), 'Hand In code not found') === false) {
DLEApi::setHandinErrorToDocument($document->id, 'unknown_file_error');
}
}
$this->log('Finished hand-in document ('.$document->id.') - '.$document->filename);
unset($document);
unset($result);
}
$this->trace('... All documents have been sent.');
}
/**
* Function to change the visibility of a document
*
* @param string $document_guid - the guid of the document to change visibility
* @param int $visibility - the visibility that the document has to become
* @return bool - $the result of the change
*/
function visibilityService($document_guid, $visibility) {
$index_address = DLEApi::getSetting('index_address');
if($index_address == '') {
$this->log('Index address not found');
return false;
}
$soap_client = $this->initSoapClient($index_address);
if($soap_client == false) {
$this->log('... Connection problem.');
return false;
}
// Call the SOAP method...
try {
$soap_client->IndexDocument(array('documentGuid' => $document_guid, 'indexType' => $visibility));
DLEApi::changeVisibility($document_guid, $visibility);
return true;
} catch (SoapFault $e) {
$this->log('Document ('.$document_guid.') index change. error received - Error: '.$e->getMessage());
return false;
}
}
/**
* Function for initiating the soap server
*
* @return void
*/
function initReportingService() {
/* Instantiate soapserver */
try {
$server = @new SoapServer(dirname(__FILE__).'/ReportingService.wsdl', array('encoding' => 'UTF-8', 'features' => SOAP_SINGLE_ELEMENT_ARRAYS));
return $server;
} catch (SoapFault $e) {
$this->log('Error creating reporting soap server. Error: '.$e->getMessage());
header($_SERVER['SERVER_PROTOCOL'] . ' 500 Internal Server Error', true, 500);
exit;
}
}
/**
* Function for receiving Ephorus reports
*
* @param object $report - the object containing the report
* @return void
*/
function reportingService($report) {
$this->log('Received report for document ('.$report->document_guid.')');
if(DLEApi::checkDocumentExists($report->document_guid) == false) {
$this->log('Report has unknown guid ('.$report->document_guid.')');
$this->log('Report finished processing');
return new SoapFault('SOAP-ENV:Client', 'Unknown guid received');
}
// Create Eph_document representation.
$document = new stdClass();
$document->guid = $report->document_guid;
$document->status = $report->status;
$document->student_name = $report->student_name;
$document->student_number = substr($report->student_number, 0, 25);
$document->date_created = $report->document_date;
$document->percentage = 0;
$document->summary = NULL;
$document->duplicate_guid = NULL;
$document->duplicate_student_name = NULL;
$document->duplicate_student_number = NULL;
switch ($report->status) {
case 1:
$document->percentage = $report->document_percentage;
// Get summary...
$document->summary = $report->summary->any;
// When only one result, $report->results->result is not an array (as expected) but an object
$result_list = (is_array($report->results->result)) ? $report->results->result : $report->results;
$results = array();
foreach($result_list as $result_item) {
$result = new stdClass();
$result->guid = $result_item->result_guid;
$result->document_guid = $report->document_guid;
$result->percentage = $result_item->percent;
$result->type = $result_item->type;
$result->url = $result_item->url;
$result->original_guid = ($result_item->type == 'local') ? $result_item->original_guid : NULL;
$result->student_name = ($result_item->type == 'local') ? $result_item->student_name : NULL;
$result->student_number = ($result_item->type == 'local') ? substr($result_item->student_number, 0, 25) : NULL;
$result->comparison = $result_item->diff->any;
$results[] = $result;
}
unset($result);
break;
case 2: // Duplicate document
$this->log('Report returned a duplicate');
$document->duplicate_guid = $report->duplicate_original_guid;
$document->duplicate_student_name = $report->duplicate_student_name;
$document->duplicate_student_number = substr($report->duplicate_student_number, 0, 25);
break;
default: // Nothing extra needs to happen if the status is 3 - 6.
break;
}
DLEApi::saveReport($document, isset($results) ? $results : array());
$this->log('Report finished processing');
}
/**
* Initializes logging
* Checks if the setting is set. If so, enable all logging
*
* @return void
*/
function initLogging() {
if(DLEApi::getSetting('ephorus_logging') == 1) {
error_reporting(-1);
ini_set("error_reporting", E_ALL);
ini_set("log_errors", 1);
}
}
/**
* Creates a line of logging
* Checks if the setting is set. If so, write the rule
*
* @param string $message - the message to be written
* @return void
*/
function log($message) {
if(DLEApi::getSetting('ephorus_logging') == 1) {
error_log('['.date('r').'] Ephorus logging: '.$message, 0);
}
}
/**
* Creates a line of logging
* Checks if the setting is set. If so, write the rule
*
* @param string $message - the message to be written
* @param string $eol - the character at the end of the line
*/
static function trace($message, $eol="\n") {
if (defined('STDOUT')) {
fwrite(STDOUT, $message.$eol);
} else {
echo $message.$eol;
}
flush();
}
/**
* Method to check if Ephorus supports a file type
*
* @param string $filename - the name of the file that has to be checked
* @return bool
*/
function isSupportedFiletype($filename) {
$supported_types = array('doc', 'docx', 'wpd', 'pdf', 'txt', 'rtf', 'odt', 'sxw', 'html', 'htm');
$ext = pathinfo($filename, PATHINFO_EXTENSION);
return in_array(strtolower($ext), $supported_types);
}
}
/**
* EphorusReport Class
*
* The class to get and set data for the Ephorus plagiarism report.
*
*/
class EphorusReport {
var $document_guid;
var $mode;
/**
* Constructor
*/
public function __construct($document_guid, $mode = 'summary') {
$this->document_guid = $document_guid;
$this->mode = $mode;
}
/**
* Gets the header of the report
*
* @param array|string $selected_sources
* @return array
*/
public function getHeader($selected_sources) {
$result_list = array();
$results = DLEApi::getResults($this->document_guid);
foreach($results as $result) {
$result_list[$result->guid] = array(
'percentage' => $result->percentage,
);
if($this->mode == 'detailed') {
$result_list[$result->guid]['input'] = array(
'type' => 'radio',
'checked' => ($result->guid == $selected_sources),
'value' => $result->guid,
'name' => 'diff'
);
} else {
$result_list[$result->guid]['input'] = array(
'type' => 'checkbox',
'checked' => in_array($result->guid, $selected_sources),
'value' => $result->guid,
'name' => 'guids_use[]'
);
}
$result_list[$result->guid]['source'] = array(
'title' => ($result->type == 'local') ?
sprintf(DLEApi::getText('document_written_by'), $result->student_name, $result->student_number) :
$result->url,
'link' => ($result->type == 'local') ?
($document_id = DLEApi::checkDocumentExists($result->original_guid)) ?
DLEApi::getURL($document_id) :
false :
$result->url,
);
}
return $result_list;
}
/**
* Gets the report content
*
* @param array $selected_sources
* @param string $comparison
* @return string
*/
public function getReport($selected_sources = array(), $comparison = '') {
$document = DLEApi::getDocument($this->document_guid);
switch($document->status) {
case 0:
$report = DLEApi::getText('wait_for_sending_msg');
break;
case 1:
if($document->summary == NULL) {
$report = DLEApi::getText('no_results_found');
} else if($this->mode == 'detailed') {
$xml = new DOMDocument();
$xml->loadXML($comparison);
$xml->saveXML();
$xsl = new DOMDocument();
$xsl->load(dirname(__FILE__).'/detailed.xslt');
$xslt = new XSLTProcessor();
$xslt->setParameter('', 'original', DLEApi::getText('original_text'));
$xslt->setParameter('', 'found', DLEApi::getText('found_by_ephorus'));
$xslt->importStyleSheet($xsl);
$report = $xslt->transformToXML($xml);
} else {
$xml = new DOMDocument();
$xml->loadXML($document->summary);
$xml->saveXML();
$xsl = new DOMDocument();
$xsl->load(dirname(__FILE__).'/summary.xslt');
$xslt = new XSLTProcessor();
$xslt->setParameter('', 'guids', implode(', ', $selected_sources));
$xslt->importStyleSheet($xsl);
$report = $xslt->transformToXML($xml);
}
break;
case 2:
$report = DLEApi::getText('duplicate_document_msg')
.'<br />';
$original_document = DLEApi::getDocument($document->duplicate_guid);
if($original_document) {
$report .= sprintf(DLEApi::getText('original_document_by'),
$document->duplicate_student_name,
$document->duplicate_student_number,
DLEApi::formatDate($original_document->date_created))."<br />";
$report .= DLEApi::getText('duplicate_document_download').': '
.DLEApi::getLink($original_document->id)
.'<br />';
$report .= DLEApi::getText('original_report').': '
.DLEApi::getReportLink($original_document->guid, DLEApi::getText('link_original_report'));
}
else
{
$report .= sprintf(DLEApi::getText('original_document_by_no_date'),
$document->duplicate_student_name, $document->duplicate_student_number)
.'<br />';
}
break;
case 3:
$report = DLEApi::getText('document_protected_msg');
break;
case 4:
$report = DLEApi::getText('not_enough_text_msg');
break;
case 5:
$report = DLEApi::getText('no_text_msg');
break;
case 6:
$report = DLEApi::getText('unknown_error_msg');
break;
case 99:
$error = !empty($document->error) ? $document->error : 'unknown_file_error';
$report = DLEApi::getText($error.'_msg');
break;
default:
$report = DLEApi::getText('unknown_error_msg');
break;
}
return $report;
}
/**
* Function to export the report as pdf
*
* @param array|string$selected_sources
*/
public function exportReport($selected_sources) {
}
}
/**
* EphorusStatus Class
*
* The class to get and set data the current status of the Ephorus module and ephorus comms
*
*/
class EphorusStatus {
/**
* Constructor
*/
public function __construct() {
DLEApi::initialize();
}
public function versions()
{
$data = DLEApi::getDLEData();
EphorusService::trace('Version control version: 0.3');
EphorusService::trace('Digital Learning Environment: '.$data['dle']);
EphorusService::trace('Digital Learning Environment version: '.$data['dle_version']);
EphorusService::trace('Digital Learning Environment release: '.$data['dle_release']);
EphorusService::trace('Module version: '.$data['module_version']);
EphorusService::trace('Ephorus Comms version: 1.5 (2013-08-05)');
EphorusService::trace('PHP version: '.PHP_VERSION);
EphorusService::trace('PHP memory limit: '.ini_get('memory_limit'));
EphorusService::trace('XSL: '.((extension_loaded('xsl') == true)?'enabled':'disabled'));
EphorusService::trace('SOAP: '.((extension_loaded('soap') == true)?'enabled':'disabled'));
EphorusService::trace('Soap Client: '.((class_exists("SOAPClient") == true)?'enabled':'disabled'));
EphorusService::trace('Soap Server: '.((class_exists("SOAPServer") == true)?'enabled':'disabled'));
EphorusService::trace('Hand-in address: '.$data['handin_address']);
EphorusService::trace('Index document address: '.$data['index_address']);
}
/**
* Function to test if all connections are open
*
* @return array
*/
public function connectivityTest()
{
return array(
'handin' => $this->testHandIn(),
'index' => $this->testIndex()
);
}
public function testHandIn()
{
/* Initiate the service class */
$service = new EphorusService(false);
/* Get the hand-in code */
$handin_code = DLEApi::getSetting('handin_code');
if($handin_code == '') {
return false;
}
/* Get the hand-in address */
$handin_address = DLEApi::getSetting('handin_address');
if($handin_address == '') {
return false;
}
$curl = curl_init($handin_address);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_FAILONERROR, true);
if(DLEApi::getProxySetting('host')) {
curl_setopt($curl, CURLOPT_PROXY, DLEApi::getProxySetting('host').':'.DLEApi::getProxySetting('port'));
}
if(!curl_exec($curl) || !strpos($handin_address, 'wsdl')) {
return false;
}
/* Initiate soap */
$soap_client = @$service->initSoapClient($handin_address, 'visibility');
set_time_limit(610);
/* Open the test document */
$test_document = dirname(__FILE__).'/connection_test.txt';
$file = fopen($test_document, 'r') or die('Cannot open file: '.$test_document);
$file = fread($file, filesize($test_document));
$handin_parameters = array(
'code' => $handin_code,
'firstName' => 'firstname',
'middleName' => 'middlename',
'lastName' => 'lastname',
'studentEmail' => 'student_email@school.com',
'studentNumber' => '1',
'comment' => 'Document for testing the connectivity',
'fileName' => 'connection_test.txt',
'file' => base64_encode($file),
'processType' => 2,
);
/* Try to send file */
try {
@$soap_client->UploadDocument($handin_parameters);
return true;
} catch(SoapFault $e) {
return true;
}
}
public function testIndex()
{
/* Initiate the service class */
$service = new EphorusService(false);
/* Get the index address */
$index_address = DLEApi::getSetting('index_address');
if($index_address == '') {
return false;
}
$curl = curl_init($index_address);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, false);
curl_setopt($curl, CURLOPT_FAILONERROR, true);
if(DLEApi::getProxySetting('host')) {
curl_setopt($curl, CURLOPT_PROXY, DLEApi::getProxySetting('host').':'.DLEApi::getProxySetting('port'));
}
if(!curl_exec($curl) || !strpos($index_address, 'wsdl')) {
return false;
}
/* Initiate soap */
$soap_client = $service->initSoapClient($index_address, 'visibility');
/* Try to change file's visibility index */
try {
@$soap_client->IndexDocument(array('documentGuid' => '123456-123456-123456-123456', 'indexType' => 2));
return true;
} catch(SoapFault $e) {
return true;
}
}
}