Passed
Push — master ( a2dd4f...271415 )
by Julito
09:03
created

Compilatio::getTransportMode()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 3
rs 10
1
<?php
2
/* For licensing terms, see /license.txt */
3
4
/**
5
 * Build the comunication with the SOAP server Compilatio.net
6
 * call severals methods for the file management in Compilatio.net.
7
 *
8
 * @version: 2.0
9
 */
10
class Compilatio
11
{
12
    /** Identification key for the Compilatio account*/
13
    public $key;
14
    /** Webservice connection*/
15
    public $soapcli;
16
    private $transportMode;
17
    private $maxFileSize;
18
    private $wgetUri;
19
    private $wgetLogin;
20
    private $wgetPassword;
21
    private $proxyHost;
22
    private $proxyPort;
23
24
    /**
25
     * Compilatio constructor.
26
     */
27
    public function __construct()
28
    {
29
        if (empty(api_get_configuration_value('allow_compilatio_tool')) ||
30
            empty(api_get_configuration_value('compilatio_tool'))
31
        ) {
32
            throw new Exception('Compilatio not available');
33
        }
34
35
        $settings = api_get_configuration_value('compilatio_tool');
36
37
        if (isset($settings['settings'])) {
38
            $settings = $settings['settings'];
39
        } else {
40
            throw new Exception('Compilatio config available');
41
        }
42
43
        $key = $this->key = $settings['key'];
44
        $urlsoap = $settings['soap_url'];
45
        $proxyHost = $this->proxyHost = $settings['proxy_host'];
46
        $proxyPort = $this->proxyPort = $settings['proxy_port'];
47
        $this->transportMode = $settings['transport_mode'];
48
        $this->maxFileSize = $settings['max_filesize'];
49
        $this->wgetUri = $settings['wget_uri'];
50
        $this->wgetLogin = $settings['wget_login'];
51
        $this->wgetPassword = $settings['wget_password'];
52
        $soapVersion = 2;
53
54
        try {
55
            if (!empty($key)) {
56
                $this->key = $key;
57
                if (!empty($urlsoap)) {
58
                    if (!empty($proxyHost)) {
59
                        $param = [
60
                            'trace' => false,
61
                            'soap_version' => $soapVersion,
62
                            'exceptions' => true,
63
                            'proxy_host' => '"'.$proxyHost.'"',
64
                            'proxy_port' => $proxyPort,
65
                        ];
66
                    } else {
67
                        $param = [
68
                            'trace' => false,
69
                            'soap_version' => $soapVersion,
70
                            'exceptions' => true,
71
                        ];
72
                    }
73
                    $this->soapcli = new SoapClient($urlsoap, $param);
74
                } else {
75
                    throw new Exception('WS urlsoap not available');
76
                }
77
            } else {
78
                throw new Exception('API key not available');
79
            }
80
        } catch (SoapFault $fault) {
81
            $this->soapcli = "Error constructor compilatio $fault->faultcode $fault->faultstring ";
82
        } catch (Exception $e) {
83
            $this->soapcli = "Error constructor compilatio with urlsoap $urlsoap ".$e->getMessage();
84
        }
85
    }
86
87
    /**
88
     * @return string
89
     */
90
    public function getKey()
91
    {
92
        return $this->key;
93
    }
94
95
    /**
96
     * @param mixed $key
97
     *
98
     * @return Compilatio
99
     */
100
    public function setKey($key)
101
    {
102
        $this->key = $key;
103
104
        return $this;
105
    }
106
107
    /**
108
     * @return mixed
109
     */
110
    public function getTransportMode()
111
    {
112
        return $this->transportMode;
113
    }
114
115
    /**
116
     * @param mixed $transportMode
117
     *
118
     * @return Compilatio
119
     */
120
    public function setTransportMode($transportMode)
121
    {
122
        $this->transportMode = $transportMode;
123
124
        return $this;
125
    }
126
127
    /**
128
     * @return mixed
129
     */
130
    public function getMaxFileSize()
131
    {
132
        return $this->maxFileSize;
133
    }
134
135
    /**
136
     * @param mixed $maxFileSize
137
     *
138
     * @return Compilatio
139
     */
140
    public function setMaxFileSize($maxFileSize)
141
    {
142
        $this->maxFileSize = $maxFileSize;
143
144
        return $this;
145
    }
146
147
    /**
148
     * @return mixed
149
     */
150
    public function getWgetUri()
151
    {
152
        return $this->wgetUri;
153
    }
154
155
    /**
156
     * @param mixed $wgetUri
157
     *
158
     * @return Compilatio
159
     */
160
    public function setWgetUri($wgetUri)
161
    {
162
        $this->wgetUri = $wgetUri;
163
164
        return $this;
165
    }
166
167
    /**
168
     * @return mixed
169
     */
170
    public function getWgetLogin()
171
    {
172
        return $this->wgetLogin;
173
    }
174
175
    /**
176
     * @param mixed $wgetLogin
177
     *
178
     * @return Compilatio
179
     */
180
    public function setWgetLogin($wgetLogin)
181
    {
182
        $this->wgetLogin = $wgetLogin;
183
184
        return $this;
185
    }
186
187
    /**
188
     * @return mixed
189
     */
190
    public function getWgetPassword()
191
    {
192
        return $this->wgetPassword;
193
    }
194
195
    /**
196
     * @param mixed $wgetPassword
197
     *
198
     * @return Compilatio
199
     */
200
    public function setWgetPassword($wgetPassword)
201
    {
202
        $this->wgetPassword = $wgetPassword;
203
204
        return $this;
205
    }
206
207
    /**
208
     * @return mixed
209
     */
210
    public function getProxyHost()
211
    {
212
        return $this->proxyHost;
213
    }
214
215
    /**
216
     * @param mixed $proxyHost
217
     *
218
     * @return Compilatio
219
     */
220
    public function setProxyHost($proxyHost)
221
    {
222
        $this->proxyHost = $proxyHost;
223
224
        return $this;
225
    }
226
227
    /**
228
     * @return mixed
229
     */
230
    public function getProxyPort()
231
    {
232
        return $this->proxyPort;
233
    }
234
235
    /**
236
     * @param mixed $proxyPort
237
     *
238
     * @return Compilatio
239
     */
240
    public function setProxyPort($proxyPort)
241
    {
242
        $this->proxyPort = $proxyPort;
243
244
        return $this;
245
    }
246
247
    /**
248
     * Method for the file load.
249
     *
250
     * @param $title
251
     * @param $description
252
     * @param $filename
253
     * @param $mimeType
254
     * @param $content
255
     *
256
     * @return string
257
     */
258
    public function sendDoc($title, $description, $filename, $mimeType, $content)
259
    {
260
        try {
261
            if (!is_object($this->soapcli)) {
262
                return "Error in constructor compilatio() $this->soapcli";
263
            }
264
            $idDocument = $this->soapcli->__call(
265
                'addDocumentBase64',
266
                [
267
                    $this->key,
268
                    utf8_encode(urlencode($title)),
269
                    utf8_encode(urlencode($description)),
270
                    utf8_encode(urlencode($filename)),
271
                    utf8_encode($mimeType),
272
                    base64_encode($content),
273
                ]
274
            );
275
276
            return $idDocument;
277
        } catch (SoapFault $fault) {
278
            return "Erreur sendDoc()".$fault->faultcode." ".$fault->faultstring;
279
        }
280
    }
281
282
    /**
283
     * Method for recover a document's information.
284
     *
285
     * @param $compiHash
286
     *
287
     * @return string
288
     */
289
    public function getDoc($compiHash)
290
    {
291
        try {
292
            if (!is_object($this->soapcli)) {
293
                return "Error in constructor compilatio() ".$this->soapcli;
294
            }
295
            $param = [$this->key, $compiHash];
296
            $idDocument = $this->soapcli->__call('getDocument', $param);
297
298
            return $idDocument;
299
        } catch (SoapFault $fault) {
300
            return "Erreur getDoc()".$fault->faultcode." ".$fault->faultstring;
301
        }
302
    }
303
304
    /**
305
     * method for recover an url document's report.
306
     *
307
     * @param $compiHash
308
     *
309
     * @return string
310
     */
311
    public function getReportUrl($compiHash)
312
    {
313
        try {
314
            if (!is_object($this->soapcli)) {
315
                return "Error in constructor compilatio() ".$this->soapcli;
316
            }
317
            $param = [$this->key, $compiHash];
318
            $idDocument = $this->soapcli->__call('getDocumentReportUrl', $param);
319
320
            return $idDocument;
321
        } catch (SoapFault $fault) {
322
            return "Erreur  getReportUrl()".$fault->faultcode." ".$fault->faultstring;
323
        }
324
    }
325
326
    /**
327
     *  Method for deleting a Compialtio's account document.
328
     *
329
     * @param $compiHash
330
     *
331
     * @return string
332
     */
333
    public function deldoc($compiHash)
334
    {
335
        try {
336
            if (!is_object($this->soapcli)) {
337
                return "Error in constructor compilatio() ".$this->soapcli;
338
            }
339
            $param = [$this->key, $compiHash];
340
            $this->soapcli->__call('deleteDocument', $param);
341
        } catch (SoapFault $fault) {
342
            return "Erreur  deldoc()".$fault->faultcode." ".$fault->faultstring;
343
        }
344
    }
345
346
    /**
347
     * Method for start the analysis for a document.
348
     *
349
     * @param $compiHash
350
     *
351
     * @return string
352
     */
353
    public function startAnalyse($compiHash)
354
    {
355
        try {
356
            if (!is_object($this->soapcli)) {
357
                return "Error in constructor compilatio() ".$this->soapcli;
358
            }
359
            $param = [$this->key, $compiHash];
360
            $this->soapcli->__call('startDocumentAnalyse', $param);
361
        } catch (SoapFault $fault) {
362
            return "Erreur  startAnalyse()".$fault->faultcode." ".$fault->faultstring;
363
        }
364
    }
365
366
    /**
367
     * Method for recover the account's quota.
368
     *
369
     * @return string
370
     */
371
    public function getQuotas()
372
    {
373
        try {
374
            if (!is_object($this->soapcli)) {
375
                return "Error in constructor compilatio() ".$this->soapcli;
376
            }
377
            $param = [$this->key];
378
            $resultat = $this->soapcli->__call('getAccountQuotas', $param);
379
380
            return $resultat;
381
        } catch (SoapFault $fault) {
382
            return "Erreur  getQuotas()".$fault->faultcode." ".$fault->faultstring;
383
        }
384
    }
385
386
    /**
387
     * Method for identify a file extension and the possibility that the document can be managed by Compilatio.
388
     *
389
     * @param $filename
390
     *
391
     * @return bool
392
     */
393
    public static function verifiFileType($filename)
394
    {
395
        $types = ['doc', 'docx', 'rtf', 'xls', 'xlsx', 'ppt', 'pptx', 'odt', 'pdf', 'txt', 'htm', 'html'];
396
        $extension = substr($filename, strrpos($filename, '.') + 1);
397
        $extension = strtolower($extension);
398
399
        return in_array($extension, $types);
400
    }
401
402
    /**
403
     * Fonction affichage de la barre de progression d'analyse version 3.1.
404
     *
405
     * @param string $status From the document
406
     * @param int    $pour
407
     * @param array  $text   Array includes the extract from the text
408
     *
409
     * @return string
410
     */
411
    public static function getProgressionAnalyseDocv31($status, $pour = 0, $text = [])
412
    {
413
        $loading = Display::returnFontAwesomeIcon('spinner', null, true, 'fa-spin');
414
        $loading .= '&nbsp;';
415
        //$refreshReturn = Display::url('javascript:window.location.reload(false);', $loading);
416
417
        switch ($status) {
418
            case 'ANALYSE_IN_QUEUE':
419
                $content = $loading.$text['analysisinqueue'];
420
                break;
421
            case 'ANALYSE_PROCESSING':
422
                $content = $loading.$text['analysisinfinalization'];
423
                break;
424
            default:
425
                $content = Display::bar_progress($pour, true);
426
                break;
427
        }
428
429
        return $content;
430
    }
431
432
    /**
433
     * Method for display the PomprseuilmankBar (% de plagiat).
434
     *
435
     * @param $index
436
     * @param $weakThreshold
437
     * @param $highThreshold
438
     *
439
     * @return string
440
     */
441
    public static function getPomprankBarv31(
442
        $index,
443
        $weakThreshold,
444
        $highThreshold
445
    ) {
446
        $index = round($index);
447
        $pour = round((50 * $index) / 100);
448
        $return = '';
449
        $class = 'error';
450
        if ($index < $weakThreshold) {
451
            $class = 'success';
452
        } else {
453
            if ($index >= $weakThreshold && $index < $highThreshold) {
454
                $class = 'warning';
455
            }
456
        }
457
458
        $return .= Display::bar_progress($index, true, null, $class);
459
460
        return $return;
461
    }
462
463
    /**
464
     * Method for validation of hash.
465
     *
466
     * @param string $hash
467
     *
468
     * @return bool
469
     */
470
    public static function isMd5($hash)
471
    {
472
        return preg_match('`^[a-f0-9]{32}$`', $hash);
0 ignored issues
show
Bug Best Practice introduced by
The expression return preg_match('`^[a-f0-9]{32}$`', $hash) returns the type integer which is incompatible with the documented return type boolean.
Loading history...
473
    }
474
475
    /**
476
     * function for delete a document of the compilatio table if plagiarismTool is Compilatio.
477
     *
478
     * @param int $courseId
479
     * @param int $itemId
480
     *
481
     * @return bool
482
     */
483
    public static function plagiarismDeleteDoc($courseId, $itemId)
484
    {
485
        if (api_get_configuration_value('allow_compilatio_tool')) {
486
            return false;
487
        }
488
489
        $table = Database:: get_course_table(TABLE_PLAGIARISM);
490
        $params = [$courseId, $itemId];
491
        Database::delete($table, ['c_id = ? AND document_id = ?' => $params]);
492
493
        return true;
494
    }
495
496
    /**
497
     * @param int $courseId
498
     * @param int $documentId
499
     * @param int $compilatioId
500
     */
501
    public function saveDocument($courseId, $documentId, $compilatioId)
502
    {
503
        $documentId = (int) $documentId;
504
        $courseId = (int) $courseId;
505
506
        $table = Database::get_course_table(TABLE_PLAGIARISM);
507
        $params = [
508
            'c_id' => $courseId,
509
            'document_id' => $documentId,
510
            'compilatio_id' => $compilatioId,
511
        ];
512
        Database::insert($table, $params);
513
    }
514
515
    /**
516
     * @param int $documentId
517
     * @param int $courseId
518
     *
519
     * @return string md5 value
520
     */
521
    public function getCompilatioId($documentId, $courseId)
522
    {
523
        $documentId = (int) $documentId;
524
        $courseId = (int) $courseId;
525
526
        $table = Database::get_course_table(TABLE_PLAGIARISM);
527
        $sql = "SELECT compilatio_id FROM $table 
528
                WHERE document_id = $documentId AND c_id= $courseId";
529
        $result = Database::query($sql);
530
        $result = Database::fetch_object($result);
531
532
        if ($result) {
533
            return (string) $result->compilatio_id;
534
        }
535
536
        return 0;
537
    }
538
539
    /**
540
     * @param int $workId
541
     *
542
     * @return string
543
     */
544
    public function giveWorkIdState($workId)
545
    {
546
        $compilatioImgFolder = api_get_path(WEB_CODE_PATH).'plagiarism/compilatio/img/';
547
        $courseId = api_get_course_int_id();
548
        $compilatioId = $this->getCompilatioId($workId, $courseId);
549
550
        $actionCompilatio = '';
551
        $status = '';
552
        if (!empty($compilatioId)) {
553
            if (self::isMd5($compilatioId)) {
554
                // if compilatio_id is a hash md5, we call the function of the compilatio's
555
                // webservice who return the document's status
556
                $soapRes = $this->getDoc($compilatioId);
557
                if (isset($soapRes->documentStatus)) {
0 ignored issues
show
Bug introduced by
The property documentStatus does not exist on string.
Loading history...
558
                    $status = $soapRes->documentStatus->status;
559
                }
560
            } else {
561
                // if the compilatio's hash is not a valide hash md5,
562
                // we return à specific status (cf : IsInCompilatio() )
563
                $status = 'NOT_IN_COMPILATIO';
564
                $actionCompilatio = get_lang('CompilatioDocumentTextNotImage').'<br/>'.
565
                    get_lang('CompilatioDocumentNotCorrupt');
566
            }
567
568
            switch ($status) {
569
                case 'ANALYSE_COMPLETE':
570
                    $urlRapport = $this->getReportUrl($compilatioId);
571
                    $actionCompilatio .= self::getPomprankBarv31(
572
                            $soapRes->documentStatus->indice,
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $soapRes does not seem to be defined for all execution paths leading up to this point.
Loading history...
573
                            10,
574
                            35
575
                        )
576
                        .Display::url(
577
                            get_lang('CompilatioAnalysis'),
578
                            $urlRapport,
579
                            ['class' => 'btn btn-primary btn-xs', 'target' => '_blank']
580
                        );
581
                    break;
582
                case 'ANALYSE_PROCESSING':
583
                    $actionCompilatio .= "<div style='font-weight:bold;text-align:left'>"
584
                        .get_lang('CompilatioAnalysisInProgress')
585
                        ."</div>";
586
                    $actionCompilatio .= "<div style='font-size:80%;font-style:italic;margin-bottom:5px;'>"
587
                        .get_lang('CompilatioAnalysisPercentage')
588
                        ."</div>";
589
                    $text = [];
590
                    $text['analysisinqueue'] = get_lang('CompilatioWaitingAnalysis');
591
                    $text['analysisinfinalization'] = get_lang('CompilatioAnalysisEnding');
592
                    $text['refresh'] = get_lang('Refresh');
593
                    $actionCompilatio .= self::getProgressionAnalyseDocv31(
594
                        $status,
595
                        $soapRes->documentStatus->progression,
596
                        $text
597
                    );
598
                    break;
599
                case 'ANALYSE_IN_QUEUE':
600
                    $loading = Display::returnFontAwesomeIcon('spinner', null, true, 'fa-spin');
601
                    $actionCompilatio .= $loading.'&nbsp;'.get_lang('CompilatioAwaitingAnalysis');
602
                    break;
603
                case 'BAD_FILETYPE':
604
                    $actionCompilatio .= get_lang('CompilatioFileIsNotSupported')
605
                        .'<br/>'
606
                        .get_lang('CompilatioProtectedPdfVerification');
607
                    break;
608
                case 'BAD_FILESIZE':
609
                    $actionCompilatio .= get_lang('CompilatioFileIsTooBig');
610
                    break;
611
            }
612
        }
613
614
        $result = $workId.'|'.$actionCompilatio.'|'.$status.'|';
615
616
        return $result;
617
    }
618
}
619