Completed
Pull Request — 1.10.x (#1244)
by
unknown
42:26
created

upload.php ➔ workDocExtension()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 4
c 1
b 0
f 0
nc 1
nop 2
dl 0
loc 5
rs 9.4285
1
<?php
2
3
/* Dokeos and compilatio's includes */
4
require ('../../inc/global.inc.php');
5
require_once ("../../work/work.lib.php");
6
require_once('../../inc/lib/document.lib.php');
7
require_once('../../inc/lib/fileDisplay.lib.php');
8
require_once ('compilatio.class.php');
9
require_once ('config.php');
10
11
/*parameters SOAP use*/
12
ini_set('soap.wsdl_cache_enabled', 0);
13
ini_set('default_socket_timeout', '1000');
14
$coursId = api_get_course_int_id();
15
16
/* Variables */ 
17
$typeMessage = 0;
18
$errorCodeNotValid = get_lang('compilatioError') . get_lang('documentCodeNotValid');
19
$errorLoadError = get_lang('compilatioLoadError') . get_lang('compilatioContactAdmin');
20
21
/*message to the user for be patient*/
22
$msgWait = get_lang('PleaseWaitThisCouldTakeAWhile');
23
24
/* if we have to upload severals documents*/
25
if(isset($_REQUEST['type']) && $_REQUEST['type']=="multi"){
26
    $docs = preg_split("/a/", $_REQUEST['doc']);
27
    for ($k=0 ; $k < sizeof($docs)-1 ; $k++)	{
28
        /* We have to modify the timeout server for send the heavy files */
29
        set_time_limit(600);
30
        $documentId = 0;
31
        if (intval($docs[$k])) {
32
            $documentId = $docs[$k];
33
        }
34
        /**
35
         * File problem in the url field that no longer have the file extension,
36
         * Compilatio's server refuse the files
37
         * we renames in the FS and the database with the file extension that is found in the title field
38
         */
39
        compilatioUpdateWorkDocument($documentId, $coursId);
40
41
        $compTable = Database::get_course_table("plagiarism_compilatio_docs");
42
        $compilatioQuery = "SELECT compilatio_id FROM "
43
            . $compTable
44
            . " WHERE id_doc="
45
            . $documentId
46
            . " AND c_id="
47
            . $coursId;
48
        $compiSqlResult = Database::query($compilatioQuery);
49
        $compi = Database::fetch_object($compiSqlResult);
50
        if (isset($compi->compilatio_id)) {
0 ignored issues
show
Unused Code introduced by
This if statement is empty and can be removed.

This check looks for the bodies of if statements that have no statements or where all statements have been commented out. This may be the result of changes for debugging or the code may simply be obsolete.

These if bodies can be removed. If you have an empty if but statements in the else branch, consider inverting the condition.

if (rand(1, 6) > 3) {
//print "Check failed";
} else {
    print "Check succeeded";
}

could be turned into

if (rand(1, 6) <= 3) {
    print "Check succeeded";
}

This is much more concise to read.

Loading history...
51
            /*The document is already in Compilatio, we do nothing*/
52
53
        } else {
54
            $workTable = Database::get_course_table(TABLE_STUDENT_PUBLICATION);
55
            $query = "SELECT * FROM "
56
                . $workTable
57
                . " WHERE id='"
58
                . $documentId
59
                . "' AND c_id="
60
                . $coursId;
61
            $sqlResult = Database::query($query);
62
            $doc = Database::fetch_object($sqlResult);
63
            if ($doc) {
64
                /*We load the document in compilatio through the webservice */
65
                $currentCourseRepositoryWeb = api_get_path(WEB_COURSE_PATH) . $_course["path"] . "/";
66
                $WrkUrl = $currentCourseRepositoryWeb . $doc->url;
67
                $LocalWrkUrl = api_get_path(SYS_COURSE_PATH) . $_course["path"] . "/" . $doc->url;
68
                $LocalWrkTitle = $doc->title;
69
                $mime = typeMime($LocalWrkTitle);
70
                $compilatio = new compilatio(
71
                    $compilatioParameter['key'],
72
                    $compilatioParameter['$urlsoap'],
73
                    $compilatioParameter['proxy_host'],
74
                    $compilatioParameter['proxy_port']);
75
76 View Code Duplication
                if ($compilatioParameter['mode_transport'] == 'wget') {
77
                    /*Compilatio's server recover tjre file throught wget like this:
78
                    username:password@http://somedomain.com/reg/remotefilename.tar.gz */
79
                    if (strlen($compilatioParameter['wget_uri']) > 2)  {
80
                        $filename = ereg_replace("/$",
81
                                "",
82
                                $compilatioParameter['wget_uri'])
83
                            . "/"
84
                            . $_course["path"]
85
                            . "/"
86
                            . $doc->url;
87
                    } else {
88
                        $filename = $WrkUrl;
89
                    }
90
                    if (strlen($compilatioParameter['wget_login']) > 2) {
91
                        $filename = $compilatioParameter['wget_login']
92
                            . ":"
93
                            . $compilatioParameter['wget_password']
94
                            . "@"
95
                            . $filename;
96
                    }
97
                    $mime = "text/plain";
98
                    $compilatioId = $compilatio->sendDoc($doc->title, '', $filename, $mime, 'get_url');
99
                } else {
100
                    /* we use strictly the SOAP for the data trasmission */
101
                    $pieces = explode("/", $doc->url);
102
                    $nbPieces = count($pieces);
103
                    $filename = $pieces[$nbPieces - 1];
104
                    $compilatioId = $compilatio->sendDoc($doc->title,
105
                        '',
106
                        $filename,
107
                        $mime,
108
                        file_get_contents($LocalWrkUrl)
109
                    );
110
                }
111
                /*we associate in the database the document chamilo to the document compilatio*/
112
                $compTable = Database::get_course_table("plagiarism_compilatio_docs");
113
                $sql4 = "INSERT INTO "
114
                    . $compTable
115
                    . " (c_id, id_doc, compilatio_id) VALUES ("
116
                    . $coursId
117
                    . ", '"
118
                    . $doc->id
119
                    . "','"
120
                    . $compilatioId
121
                    . "')";
122
                /*we verify that the docmuent's id is an hash_md5*/
123
124
                if (isMd5($compilatioId)) {
125
                    /*We insert the document into the DB*/
126
                    Database::query($sql4);
127
                } else {
128
                    $typeMessage = 1;
129
                }
130
                if (isMd5($compilatioId)) {
131
                    /*the uploading is good, we return an compilatio's id_doc*/
132
                    $soapRes = $compilatio->startAnalyse($compilatioId);
133
                } else {
134
                    $typeMessage = 2;
135
                }
136
            }
137
        }
138
    }
139
140 View Code Duplication
    if ($typeMessage == 1) {
141
        $message = $errorCodeNotValid;
142
        Display::display_error_message($message);
143
    }elseif($typeMessage == 2){
144
        $message = $errorLoadError;
145
        Display::display_error_message($message);
146
    }else{
147
        $message = $msgWait;
148
        Display::display_confirmation_message($message);
149
    }
150
151
} else {
152
    //non multiple
153
154
	$documentId = $_GET['doc'];
155
	compilatioUpdateWorkDocument($documentId, $coursId);
156
	$workTable = Database::get_course_table(TABLE_STUDENT_PUBLICATION);
157
	$query = "SELECT * FROM "
158
        . $workTable
159
        . " WHERE id='"
160
        . $documentId
161
        . "' AND c_id="
162
        . $coursId;
163
	$sqlResult = Database::query($query);
164
	$doc = Database::fetch_object($sqlResult);
165
	$currentCourseRepositoryWeb = api_get_path(WEB_COURSE_PATH) . $_course["path"] . "/";
166
	$WrkUrl = $currentCourseRepositoryWeb . $doc->url;
167
	$WrkTitle = $doc->title;
168
	$LocalWrkUrl = api_get_path(SYS_COURSE_PATH).$_course["path"] . "/" . $doc->url;
169
	$mime = typeMime($WrkTitle);
170
	$compilatio = new compilatio(
171
        $compilatioParameter['key'],
172
        $compilatioParameter['$urlsoap'],
173
        $compilatioParameter['proxy_host'],
174
        $compilatioParameter['proxy_port']);
175 View Code Duplication
	if ($compilatioParameter['mode_transport'] == 'wget') {
176
177
        if (strlen($compilatioParameter['wget_uri']) > 2) {
178
            $filename = ereg_replace("/$",
179
                    "",
180
                    $compilatioParameter['wget_uri'])
181
                    . "/"
182
                    . $_course["path"]
183
                    . "/"
184
                    . $doc->title;
185
        } else {
186
            $filename = $WrkUrl;
187
        }
188
        if (strlen($compilatioParameter['wget_login']) > 2) {
189
            $filename = $compilatioParameter['wget_login']
190
                . ":"
191
                . $compilatioParameter['wget_password']
192
                . "@"
193
                . $filename;
194
        }
195
        $mime = "text/plain";
196
        $compilatioId = $compilatio->sendDoc($doc->title, '', $filename, $mime, 'get_url');
197
    } else {
198
		$pieces = explode("/", $doc->url);
199
		$nbPieces = count($pieces);
200
		$filename = $pieces[$nbPieces-1];
201
        $compilatioId = $compilatio->sendDoc($doc->title, '', $filename, $mime, file_get_contents($LocalWrkUrl));
202
    }
203
	$compTable = Database::get_course_table("plagiarism_compilatio_docs");
204
205
	$sql4 = "INSERT INTO "
206
        . $compTable
207
        . "(c_id, id_doc, compilatio_id) VALUES ("
208
        . $coursId
209
        . ", '"
210
        . $doc->id
211
        . "','"
212
        . $compilatioId
213
        . "')";
214
	if (isMd5($compilatioId)) {
215
        Database::query($sql4);
216
	} else {
217
		$sql5 = "INSERT INTO "
218
            . $compTable
219
            . " (c_id, id_doc,compilatio_id) VALUES ("
220
            . $coursId
221
            . ", '"
222
            . $doc->id
223
            ."','error')";
224
        Database::query($sql5);
225
226
	}
227
	if (isMd5($compilatioId)) {
228
        $soapRes = $compilatio->startAnalyse($compilatioId);
229
    } else {
230
        $typeMessage = 2;
231
    }
232
233 View Code Duplication
    if ($typeMessage == 1) {
234
        $message = $errorCodeNotValid;
235
        Display::display_error_message($message);
236
    }elseif($typeMessage == 2){
237
        $message = $errorLoadError;
238
        Display::display_error_message($message);
239
    }else{
240
        $message = $msgWait;
241
        Display::display_confirmation_message($message);
242
	}
243
244
}
245
246
/**
247
 * function for show and recovery the extension from a file
248
 *
249
 * @param $docId
250
 * @param $coursId
251
 * @return string
252
 */
253
function workDocExtension($docId, $coursId) {
254
    $dbTitle = getWorkTitle($docId, $coursId);
255
    $res = getFileExtension($dbTitle);
256
    return $res;
257
}
258
259
function getFileExtension($filename) {
260
    $res = "";
261
    preg_match("/.*\.([^.]+)/", $filename, $dbTitle);
262
    if (count($dbTitle) > 1) {
263
        $res = $dbTitle[1];
264
    }
265
    return $res;
266
}
267
268
function getWorkTitle($docId, $coursId) {
269
    $res = "";
270
    $workTable = Database::get_course_table(TABLE_STUDENT_PUBLICATION);    
271
    $sql = "SELECT title FROM " . $workTable . " WHERE c_id=" . $coursId . " AND id=" . $docId;
272
    $res = Database::query($sql);
273
    if (Database::num_rows($res) > 0) {
274
        $data = Database::fetch_array($res);
275
        $res = $data['title'];
276
    }
277
    return $res;
278
}
279
280
281 View Code Duplication
function getFilename($txt) {
282
    $res = $txt;
283
    preg_match("|.*/([^/]+)|", $txt, $urlList);
284
    if (count($urlList) > 0) {
285
        $res = $urlList[1];
286
    }
287
    return $res;
288
}
289
290 View Code Duplication
function getWorkFolder($txt) {
291
    $res = "";
292
    preg_match("|(.*/)[^/]+|", $txt, $urlList);
293
    if (count($urlList) > 0) {
294
        $res = $urlList[1];
295
    }
296
    return $res;
297
}
298
299
function getShortFilename($txt) {
300
    $res = $txt;
301
    if (strlen($txt) > 10) {
302
        $res = substr($txt, 0, 10);
303
    }
304
    return $res;
305
}
306
307
function compilatioUpdateWorkDocument($docId, $coursId) {
308
    global $_course;
309
    $workTable = Database::get_course_table(TABLE_STUDENT_PUBLICATION);
310
    $extensionFile = workDocExtension($docId, $coursId);
311
    $urlFile = get_work_path($docId);
312
    $filename = getFilename($urlFile);
313
    $work_folder = getWorkFolder($urlFile);
314
    $urlFile_ext = getFileExtension($urlFile);
315
    $coursePath = api_get_path(SYS_COURSE_PATH) . $_course["path"] . "/";
316
    $workTitle = getWorkTitle($docId, $coursId);
317
318
    if ($extensionFile != "" && $urlFile_ext == "") {
319
        /*rename the files in the FS whit the extension*/
320
        $shortFilename = $filename;                                        
321
        $cleanWorkTitle = api_replace_dangerous_char($workTitle); 
322
        $newestFilename = $shortFilename . "_" . $cleanWorkTitle;
323
        rename($coursePath . $urlFile, $coursePath . $work_folder . $newestFilename);
324
        /*rename the db's input with the extension*/
325
        $sql = "UPDATE "
326
            . $workTable
327
            . " SET url='"
328
            . $work_folder
329
            . $newestFilename
330
            . "' WHERE c_id="
331
            . $coursId
332
            . " AND id="
333
            . $docId;
334
        $res = Database::query($sql);
335
    }
336
}
337
?>
0 ignored issues
show
Best Practice introduced by
It is not recommended to use PHP's closing tag ?> in files other than templates.

Using a closing tag in PHP files that only contain PHP code is not recommended as you might accidentally add whitespace after the closing tag which would then be output by PHP. This can cause severe problems, for example headers cannot be sent anymore.

A simple precaution is to leave off the closing tag as it is not required, and it also has no negative effects whatsoever.

Loading history...