Passed
Push — master ( 15378b...a2dd4f )
by Julito
09:24
created

getWorkFolder()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

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