Passed
Push — ofaj ( 0f9380...ade756 )
by
unknown
12:01 queued 12s
created

sendDocument()   B

Complexity

Conditions 8
Paths 12

Size

Total Lines 49
Code Lines 36

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 8
eloc 36
c 1
b 0
f 0
nc 12
nop 2
dl 0
loc 49
rs 8.0995
1
<?php
2
/* For licensing terms, see /license.txt */
3
4
require_once '../../inc/global.inc.php';
5
require_once '../../work/work.lib.php';
6
7
ini_set('soap.wsdl_cache_enabled', 0);
8
ini_set('default_socket_timeout', '1000');
9
10
api_set_more_memory_and_time_limits();
11
12
api_protect_course_script();
13
$courseId = api_get_course_int_id();
14
$courseInfo = api_get_course_info();
15
$compilatio = new Compilatio();
16
17
/* if we have to upload severals documents*/
18
if (isset($_REQUEST['type']) && 'multi' === $_REQUEST['type']) {
19
    $docs = explode('a', $_REQUEST['doc']);
20
    for ($k = 0; $k < count($docs) - 1; $k++) {
21
        $documentId = 0;
22
        if (!isset($docs[$k])) {
23
            $documentId = (int) $docs[$k];
24
        }
25
26
        /**
27
         * File problem in the url field that no longer have the file extension,
28
         * Compilatio's server refuse the files
29
         * we renames in the FS and the database with the file extension that is found in the title field.
30
         */
31
        compilatioUpdateWorkDocument($documentId, $courseId);
32
33
        $compilatioId = $compilatio->getCompilatioId($documentId, $courseId);
34
        if (empty($compilatioId)) {
35
            $workTable = Database::get_course_table(TABLE_STUDENT_PUBLICATION);
36
            $query = "SELECT * FROM $workTable WHERE id= $documentId AND c_id= $courseId";
37
            $sqlResult = Database::query($query);
38
            $doc = Database::fetch_object($sqlResult);
39
            if ($doc) {
40
                /*We load the document in compilatio through the webservice */
41
                $currentCourseRepositoryWeb = api_get_path(WEB_COURSE_PATH).$courseInfo['path'].'/';
42
                $WrkUrl = $currentCourseRepositoryWeb.$doc->url;
43
                $LocalWrkUrl = $courseInfo['course_sys_path'].$doc->url;
44
                $mime = DocumentManager::file_get_mime_type($doc->title);
45
                if ('wget' === $compilatio->getTransportMode()) {
46
                    /*Compilatio's server recover tjre file throught wget like this:
47
                    username:password@http://somedomain.com/reg/remotefilename.tar.gz */
48
                    if (strlen($compilatio->getWgetUri()) > 2) {
49
                        $filename = preg_replace(
50
                                '/$',
51
                                '',
52
                                $compilatio->getWgetUri()
53
                            ).'/'.$courseInfo['path'].'/'.$doc->url;
54
                    } else {
55
                        $filename = $WrkUrl;
56
                    }
57
                    if (strlen($compilatio->getWgetLogin()) > 2) {
58
                        $filename = $compilatio->getWgetLogin().':'.$compilatio->getWgetPassword().'@'.$filename;
59
                    }
60
                    $mime = 'text/plain';
61
                    $compilatioId = $compilatio->sendDoc($doc->title, '', $filename, $mime, 'get_url');
62
                } else {
63
                    /* we use strictly the SOAP for the data trasmission */
64
                    $pieces = explode('/', $doc->url);
65
                    $nbPieces = count($pieces);
66
                    $filename = $pieces[$nbPieces - 1];
67
                    $compilatioId = $compilatio->sendDoc(
68
                        $doc->title,
69
                        '',
70
                        $filename,
71
                        $mime,
72
                        file_get_contents($LocalWrkUrl)
73
                    );
74
                }
75
                /*we associate in the database the document chamilo to the document compilatio*/
76
                /*we verify that the docmuent's id is an hash_md5*/
77
                if (Compilatio::isMd5($compilatioId)) {
78
                    $compilatio->saveDocument($courseId, $doc->id, $compilatioId);
79
                    $soapRes = $compilatio->startAnalyse($compilatioId);
80
                }
81
            }
82
        }
83
    }
84
} else {
85
    $documentId = isset($_GET['doc']) ? $_GET['doc'] : 0;
86
    sendDocument($documentId, $courseInfo);
87
}
88
89
function sendDocument($documentId, $courseInfo)
90
{
91
    if (empty($courseInfo)) {
92
        return false;
93
    }
94
95
    $courseId = $courseInfo['real_id'] ?? 0;
96
    $documentId = (int) $documentId;
97
98
    if (empty($courseId) || empty($documentId)) {
99
        return false;
100
    }
101
102
    compilatioUpdateWorkDocument($documentId, $courseId);
103
    $workTable = Database::get_course_table(TABLE_STUDENT_PUBLICATION);
104
    $query = "SELECT * FROM $workTable 
105
              WHERE id = $documentId AND c_id= $courseId";
106
    $sqlResult = Database::query($query);
107
    $doc = Database::fetch_object($sqlResult);
108
    $currentCourseRepositoryWeb = api_get_path(WEB_COURSE_PATH).$courseInfo['path'].'/';
109
    $documentUrl = $currentCourseRepositoryWeb.$doc->url;
110
111
    $filePath = $courseInfo['course_sys_path'].$doc->url;
112
    $mime = DocumentManager::file_get_mime_type($doc->title);
113
114
    $compilatio = new Compilatio();
115
    if ('wget' === $compilatio->getTransportMode()) {
116
        if (strlen($compilatio->getWgetUri()) > 2) {
117
            $filename = preg_replace('/$', '', $compilatio->getWgetUri()).'/'.$courseInfo['path'].'/'.$doc->title;
118
        } else {
119
            $filename = $documentUrl;
120
        }
121
        if (strlen($compilatio->getWgetLogin()) > 2) {
122
            $filename = $compilatio->getWgetLogin().':'.$compilatio->getWgetPassword().'@'.$filename;
123
        }
124
        $compilatioId = $compilatio->sendDoc($doc->title, '', $filename, 'text/plain', 'get_url');
125
    } else {
126
        $pieces = explode('/', $doc->url);
127
        $nbPieces = count($pieces);
128
        $filename = $pieces[$nbPieces - 1];
129
        $compilatioId = $compilatio->sendDoc($doc->title, '', $filename, $mime, file_get_contents($filePath));
130
    }
131
132
    if (Compilatio::isMd5($compilatioId)) {
133
        $compilatio->saveDocument($courseId, $doc->id, $compilatioId);
134
        $compilatio->startAnalyse($compilatioId);
135
        echo Display::return_message(get_lang('Uploaded'));
136
    } else {
137
        echo Display::return_message(get_lang('Error'), 'error');
138
    }
139
}
140
141
/**
142
 * function for show and recovery the extension from a file.
143
 *
144
 * @param $docId
145
 * @param $courseId
146
 *
147
 * @return string
148
 */
149
function workDocExtension($docId, $courseId)
150
{
151
    $dbTitle = getWorkTitle($docId, $courseId);
152
    $res = getFileExtension($dbTitle);
153
154
    return $res;
155
}
156
157
function getFileExtension($filename)
158
{
159
    $res = '';
160
    preg_match("/.*\.([^.]+)/", $filename, $dbTitle);
161
    if (count($dbTitle) > 1) {
162
        $res = $dbTitle[1];
163
    }
164
165
    return $res;
166
}
167
168
function getWorkTitle($docId, $courseId)
169
{
170
    $docId = (int) $docId;
171
    $courseId = (int) $courseId;
172
173
    $workTable = Database::get_course_table(TABLE_STUDENT_PUBLICATION);
174
    $sql = "SELECT title FROM $workTable 
175
            WHERE c_id= $courseId AND id = $docId";
176
    $res = Database::query($sql);
177
    if (Database::num_rows($res) > 0) {
178
        $data = Database::fetch_array($res);
179
        $res = $data['title'];
180
    }
181
182
    return $res;
183
}
184
185
function getFilename($txt)
186
{
187
    $res = $txt;
188
    preg_match('|.*/([^/]+)|', $txt, $urlList);
189
    if (count($urlList) > 0) {
190
        $res = $urlList[1];
191
    }
192
193
    return $res;
194
}
195
196
function getWorkFolder($txt)
197
{
198
    $res = '';
199
    preg_match('|(.*/)[^/]+|', $txt, $urlList);
200
    if (count($urlList) > 0) {
201
        $res = $urlList[1];
202
    }
203
204
    return $res;
205
}
206
207
function getShortFilename($txt)
208
{
209
    $res = $txt;
210
    if (strlen($txt) > 10) {
211
        $res = substr($txt, 0, 10);
212
    }
213
214
    return $res;
215
}
216
217
function compilatioUpdateWorkDocument($docId, $courseId)
218
{
219
    $_course = api_get_course_info();
220
221
    $docId = (int) $docId;
222
    $courseId = (int) $courseId;
223
224
    $workTable = Database::get_course_table(TABLE_STUDENT_PUBLICATION);
225
    $extensionFile = workDocExtension($docId, $courseId);
226
    $urlFile = get_work_path($docId);
227
    $filename = getFilename($urlFile);
228
    $work_folder = getWorkFolder($urlFile);
229
    $urlFile_ext = getFileExtension($urlFile);
230
    $coursePath = $_course['course_sys_path'];
231
    $workTitle = getWorkTitle($docId, $courseId);
232
233
    if ($extensionFile != '' && $urlFile_ext == '') {
234
        /* Rename the files in the FS whit the extension*/
235
        $shortFilename = $filename;
236
        $cleanWorkTitle = api_replace_dangerous_char($workTitle);
237
        $newestFilename = $shortFilename.'_'.$cleanWorkTitle;
238
        rename($coursePath.$urlFile, $coursePath.$work_folder.$newestFilename);
239
        /*rename the db's input with the extension*/
240
        $sql = "UPDATE $workTable SET url='".$work_folder.$newestFilename."' 
241
                WHERE c_id=$courseId AND id=$docId";
242
        Database::query($sql);
243
    }
244
}
245