Passed
Pull Request — 1.11.x (#5852)
by Angel Fernando Quiroz
13:18
created

getShortFilename()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 1
dl 0
loc 8
rs 10
c 0
b 0
f 0
1
<?php
2
3
/* For licensing terms, see /license.txt */
4
5
require_once '../../inc/global.inc.php';
6
require_once '../../work/work.lib.php';
7
8
ini_set('soap.wsdl_cache_enabled', 0);
9
ini_set('default_socket_timeout', '1000');
10
11
api_set_more_memory_and_time_limits();
12
13
api_protect_course_script();
14
15
$courseId = api_get_course_int_id();
16
$courseInfo = api_get_course_info();
17
18
try {
19
    $compilatio = new Compilatio();
20
} catch (Exception $e) {
21
    $message = Display::return_message($e->getMessage(), 'error');
22
23
    exit($message);
24
}
25
26
/* if we have to upload severals documents*/
27
if (isset($_REQUEST['type']) && 'multi' === $_REQUEST['type']) {
28
    $docs = explode('a', $_REQUEST['doc']);
29
    for ($k = 0; $k < count($docs) - 1; $k++) {
30
        $documentId = 0;
31
        if (isset($docs[$k])) {
32
            $documentId = (int) $docs[$k];
33
        }
34
35
        /**
36
         * File problem in the url field that no longer have the file extension,
37
         * Compilatio's server refuse the files
38
         * we renames in the FS and the database with the file extension that is found in the title field.
39
         */
40
        compilatioUpdateWorkDocument($documentId, $courseId);
41
42
        $compilatioId = $compilatio->getCompilatioId($documentId, $courseId);
43
        if (empty($compilatioId)) {
44
            $workTable = Database::get_course_table(TABLE_STUDENT_PUBLICATION);
45
            $query = "SELECT * FROM $workTable WHERE id= $documentId AND c_id= $courseId";
46
            $sqlResult = Database::query($query);
47
            $doc = Database::fetch_object($sqlResult);
48
            if ($doc) {
49
                try {
50
                    $compilatioId = $compilatio->sendDoc(
51
                        $doc->title,
52
                        $doc->description,
53
                        $doc->url,
54
                        $courseInfo['course_sys_path'].$doc->url
55
                    );
56
57
                    /*we associate in the database the document chamilo to the document compilatio*/
58
                    $compilatio->saveDocument($courseId, $doc->id, $compilatioId);
59
                    sleep(10);
60
                    $compilatio->startAnalyse($compilatioId);
61
                } catch (Exception $e) {
62
                    $message = Display::return_message($e->getMessage(), 'error');
63
64
                    exit($message);
65
                }
66
            }
67
        }
68
    }
69
} else {
70
    $documentId = isset($_GET['doc']) ? $_GET['doc'] : 0;
71
    sendDocument($documentId, $courseInfo);
72
}
73
74
function sendDocument($documentId, $courseInfo)
75
{
76
    if (empty($courseInfo)) {
77
        return;
78
    }
79
80
    $courseId = $courseInfo['real_id'] ?? 0;
81
    $documentId = (int) $documentId;
82
83
    if (empty($courseId) || empty($documentId)) {
84
        return;
85
    }
86
87
    compilatioUpdateWorkDocument($documentId, $courseId);
88
    $workTable = Database::get_course_table(TABLE_STUDENT_PUBLICATION);
89
    $query = "SELECT * FROM $workTable
90
              WHERE id = $documentId AND c_id= $courseId";
91
    $sqlResult = Database::query($query);
92
    $doc = Database::fetch_object($sqlResult);
93
94
    $filePath = $courseInfo['course_sys_path'].$doc->url;
95
96
    try {
97
        $compilatio = new Compilatio();
98
99
        $compilatioId = $compilatio->sendDoc(
100
            $doc->title,
101
            $doc->description,
102
            $doc->url,
103
            $filePath
104
        );
105
106
        $compilatio->saveDocument($courseId, $doc->id, $compilatioId);
107
        sleep(10);
108
        $compilatio->startAnalyse($compilatioId);
109
        echo Display::return_message(get_lang('Uploaded'), 'success');
110
    } catch (Exception $e) {
111
        echo Display::return_message($e->getMessage(), 'error');
112
    }
113
}
114
115
/**
116
 * function for show and recovery the extension from a file.
117
 *
118
 * @param $docId
119
 * @param $courseId
120
 *
121
 * @return string
122
 */
123
function workDocExtension($docId, $courseId)
124
{
125
    $dbTitle = getWorkTitle($docId, $courseId);
126
    $res = getFileExtension($dbTitle);
127
128
    return $res;
129
}
130
131
function getFileExtension($filename)
132
{
133
    $res = '';
134
    preg_match("/.*\.([^.]+)/", $filename, $dbTitle);
135
    if (count($dbTitle) > 1) {
136
        $res = $dbTitle[1];
137
    }
138
139
    return $res;
140
}
141
142
function getWorkTitle($docId, $courseId)
143
{
144
    $docId = (int) $docId;
145
    $courseId = (int) $courseId;
146
147
    $workTable = Database::get_course_table(TABLE_STUDENT_PUBLICATION);
148
    $sql = "SELECT title FROM $workTable
149
            WHERE c_id= $courseId AND id = $docId";
150
    $res = Database::query($sql);
151
    if (Database::num_rows($res) > 0) {
152
        $data = Database::fetch_array($res);
153
        $res = $data['title'];
154
    }
155
156
    return $res;
157
}
158
159
function getFilename($txt)
160
{
161
    $res = $txt;
162
    preg_match('|.*/([^/]+)|', $txt, $urlList);
163
    if (count($urlList) > 0) {
164
        $res = $urlList[1];
165
    }
166
167
    return $res;
168
}
169
170
function getWorkFolder($txt)
171
{
172
    $res = '';
173
    preg_match('|(.*/)[^/]+|', $txt, $urlList);
174
    if (count($urlList) > 0) {
175
        $res = $urlList[1];
176
    }
177
178
    return $res;
179
}
180
181
function compilatioUpdateWorkDocument($docId, $courseId)
182
{
183
    $_course = api_get_course_info();
184
185
    $docId = (int) $docId;
186
    $courseId = (int) $courseId;
187
188
    $workTable = Database::get_course_table(TABLE_STUDENT_PUBLICATION);
189
    $extensionFile = workDocExtension($docId, $courseId);
190
    $urlFile = get_work_path($docId);
191
    $filename = getFilename($urlFile);
192
    $work_folder = getWorkFolder($urlFile);
193
    $urlFile_ext = getFileExtension($urlFile);
194
    $coursePath = $_course['course_sys_path'];
195
    $workTitle = getWorkTitle($docId, $courseId);
196
197
    if ($extensionFile != '' && $urlFile_ext == '') {
198
        /* Rename the files in the FS with the extension */
199
        $shortFilename = $filename;
200
        $cleanWorkTitle = api_replace_dangerous_char($workTitle);
201
        $newestFilename = $shortFilename.'_'.$cleanWorkTitle;
202
        rename($coursePath.$urlFile, $coursePath.$work_folder.$newestFilename);
203
        /*rename the db's input with the extension*/
204
        $sql = "UPDATE $workTable SET url='".$work_folder.$newestFilename."'
205
                WHERE c_id=$courseId AND id=$docId";
206
        Database::query($sql);
207
    }
208
}
209