Completed
Push — 1.10.x ( a26f23...c6c321 )
by Julito
59:04
created

CourseDriver::upload()   B

Complexity

Conditions 4
Paths 5

Size

Total Lines 41
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
cc 4
eloc 20
nc 5
nop 4
dl 0
loc 41
rs 8.5806
c 1
b 1
f 0
1
<?php
2
/* For licensing terms, see /license.txt */
3
4
namespace Chamilo\CoreBundle\Component\Editor\Driver;
5
6
/**
7
 * Class CourseDriver
8
 *
9
 * @package Chamilo\CoreBundle\Component\Editor\Driver
10
 *
11
 */
12
class CourseDriver extends Driver implements DriverInterface
13
{
14
    public $name = 'CourseDriver';
15
    public $visibleFiles = [];
16
    private $coursePath;
17
18
    /**
19
     * Setups the folder
20
     */
21
    public function setup()
22
    {
23
        $userId = api_get_user_id();
24
        $userInfo = api_get_user_info();
25
        $sessionId = api_get_session_id();
26
        $courseInfo = $this->connector->course;
27
28
        if (!empty($courseInfo)) {
29
            $coursePath = api_get_path(SYS_COURSE_PATH);
30
            $courseDir = $courseInfo['directory'] . '/document';
31
            $baseDir = $coursePath . $courseDir;
32
33
            $this->coursePath = $baseDir;
34
35
            // Creates shared folder
36
37
            if (!file_exists($baseDir . '/shared_folder')) {
38
                $title = get_lang('UserFolders');
39
                $folderName = '/shared_folder';
40
                //$groupId = 0;
41
                $visibility = 0;
42
                create_unexisting_directory(
43
                    $courseInfo,
44
                    $userId,
45
                    $sessionId,
46
                    0,
47
                    null,
48
                    $baseDir,
49
                    $folderName,
50
                    $title,
51
                    $visibility
52
                );
53
            }
54
55
            // Creates user-course folder
56 View Code Duplication
            if (!file_exists($baseDir . '/shared_folder/sf_user_' . $userId)) {
57
                $title = $userInfo['complete_name'];
58
                $folderName = '/shared_folder/sf_user_' . $userId;
59
                $visibility = 1;
60
                create_unexisting_directory(
61
                    $courseInfo,
62
                    $userId,
63
                    $sessionId,
64
                    0,
65
                    null,
66
                    $baseDir,
67
                    $folderName,
68
                    $title,
69
                    $visibility
70
                );
71
            }
72
        }
73
    }
74
75
    /**
76
     * {@inheritdoc}
77
     */
78
    public function getConfiguration()
79
    {
80
        if ($this->allow()) {
81
            //$translator = $this->connector->translator;
82
            //$code = $this->connector->course->getCode();
83
            $courseCode = $this->connector->course['code'];
84
            $alias = $courseCode.' '.get_lang('Documents');
85
            $userId = api_get_user_id();
86
87
            $config = array(
88
                'driver' => 'CourseDriver',
89
                'path' => $this->getCourseDocumentSysPath(),
90
                'URL' => $this->getCourseDocumentRelativeWebPath(),
91
                'accessControl' => array($this, 'access'),
92
                'alias' => $alias,
93
                'attributes' => array(
94
                    // Hide shared_folder
95
                    array(
96
                        'pattern' => '/shared_folder/',
97
                        'read' => false,
98
                        'write' => false,
99
                        'hidden' => true,
100
                        'locked' => false
101
                    )
102
                )
103
            );
104
105
            $folders = \DocumentManager::get_all_document_folders($this->connector->course, null, false, true);
106
            if (!empty($folders)) {
107
                foreach ($folders as $folder) {
108
                    //$folder = str_replace('-', "", $folder);
109
                    //\/
110
                    $config['attributes'][] = [
111
                        'pattern' => '!'.$folder.'!',
112
                        'read' => false,
113
                        'write' => false,
114
                        'hidden' => true,
115
                        'locked' => false
116
                    ];
117
                }
118
            }
119
120
            // Blocking all groups
121
122
            // hide all groups folders
123
            $config['attributes'][] = [
124
                'pattern' => '!_groupdocs_!',
125
                'read' => false,
126
                'write' => false,
127
                'hidden' => true,
128
                'locked' => false
129
            ];
130
131
132
            // Allow only the groups I have access
133
            $allGroups = \GroupManager::getAllGroupPerUserSubscription($userId);
134
            if (!empty($allGroups)) {
135
                foreach ($allGroups as $groupInfo) {
136
                    $groupId = $groupInfo['iid'];
137
                    if (\GroupManager::user_has_access($userId, $groupId, \GroupManager::GROUP_TOOL_DOCUMENTS)) {
138
                        $config['attributes'][] = [
139
                            'pattern' => '!'.$groupInfo['secret_directory'].'!',
140
                            'read' => true,
141
                            'write' => false,
142
                            'hidden' => false,
143
                            'locked' => false
144
                        ];
145
                    }
146
                }
147
            }
148
149
            return $config;
150
        }
151
152
        return array();
153
    }
154
155
    /**
156
     * This is the absolute document course path like
157
     * /var/www/portal/data/courses/XXX/document/
158
     * @return string
159
     */
160
    public function getCourseDocumentSysPath()
161
    {
162
        $url = null;
163
        if ($this->allow()) {
164
            $directory = $this->getCourseDirectory();
165
            $coursePath = $this->connector->paths['sys_course_path'];
166
            $url = $coursePath.$directory.'/document/';
167
        }
168
169
        return $url;
170
    }
171
172
    /**
173
     * @return string
174
     */
175 View Code Duplication
    public function getCourseDocumentRelativeWebPath()
176
    {
177
        $url = null;
178
        if ($this->allow()) {
179
            $directory = $this->getCourseDirectory();
180
            $url = api_get_path(REL_COURSE_PATH).$directory.'/document/';
181
        }
182
183
        return $url;
184
    }
185
186
187
    /**
188
     * @return string
189
     */
190 View Code Duplication
    public function getCourseDocumentWebPath()
191
    {
192
        $url = null;
193
        if ($this->allow()) {
194
            $directory = $this->getCourseDirectory();
195
            $url = api_get_path(WEB_COURSE_PATH).$directory.'/document/';
196
        }
197
198
        return $url;
199
    }
200
201
    /**
202
     *
203
     * @return string
204
     */
205
    public function getCourseDirectory()
206
    {
207
        return $this->connector->course['directory'];
208
    }
209
210
    /**
211
     * {@inheritdoc}
212
     */
213
    public function upload($fp, $dst, $name, $tmpname)
214
    {
215
        $this->setConnectorFromPlugin();
216
217
        if ($this->allowToEdit()) {
218
219
            // upload file by elfinder.
220
            $result = parent::upload($fp, $dst, $name, $tmpname);
221
            $name = $result['name'];
222
            $filtered = \URLify::filter($result['name'], 80, '', true);
223
224
            if (strcmp($name, $filtered) != 0) {
225
                $result = $this->customRename($result['hash'], $filtered);
226
            }
227
228
            $realPath = $this->realpath($result['hash']);
229
            if (!empty($realPath)) {
230
                // Getting file info
231
                //$info = $elFinder->exec('file', array('target' => $file['hash']));
232
                /** @var elFinderVolumeLocalFileSystem $volume */
233
                //$volume = $info['volume'];
234
                //$root = $volume->root();
235
                //var/www/chamilogits/data/courses/NEWONE/document
236
                $realPathRoot = $this->getCourseDocumentSysPath();
237
238
                // Removing course path
239
                $realPath = str_replace($realPathRoot, '/', $realPath);
240
                add_document(
241
                    $this->connector->course,
242
                    $realPath,
243
                    'file',
244
                    intval($result['size']),
245
                    $result['name']
246
                );
247
            }
248
249
            return $result;
250
        }
251
252
        return false;
253
    }
254
255
    /**
256
     * {@inheritdoc}
257
     */
258
    public function rm($hash)
259
    {
260
        // elfinder does not delete the file
261
        //parent::rm($hash);
262
        $this->setConnectorFromPlugin();
263
264
        if ($this->allowToEdit()) {
265
266
            $path = $this->decode($hash);
267
            $stat = $this->stat($path);
268
            $stat['realpath'] = $path;
269
            $this->removed[] = $stat;
270
271
            $realFilePath = $path;
272
            $coursePath = $this->getCourseDocumentSysPath();
273
            $filePath = str_replace($coursePath, '/', $realFilePath);
274
275
            \DocumentManager::delete_document(
276
                $this->connector->course,
277
                $filePath,
278
                $coursePath
279
            );
280
281
            return true;
282
        }
283
284
        return false;
285
    }
286
287
    /**
288
     * @return bool
289
     */
290
    public function allow()
291
    {
292
        //if ($this->connector->security->isGranted('ROLE_ADMIN')) {
293
294
        if (api_is_anonymous()) {
295
296
            return false;
297
        }
298
299
        if (isset($this->connector->course) && !empty($this->connector->course)) {
300
301
            return true;
302
        }
303
304
        return false;
305
    }
306
307
    /**
308
     * Allow to upload/delete folder or files
309
     *
310
     * @return bool
311
     */
312
    public function allowToEdit()
313
    {
314
        $allow = $this->allow();
315
316
        return $allow && api_is_allowed_to_edit(null, true);
317
    }
318
319
    /**
320
     * @param string $attr
321
     * @param string $path
322
     * @param $data
323
     * @param CourseDriver $volume
324
     */
325
    /*public function access($attr, $path, $data, $volume)
326
    {
327
        if ($path == $this->coursePath) {
328
329
            return true;
330
        }
331
332
        $allowToEdit = $this->allowToEdit();
333
        if ($allowToEdit) {
334
            return true;
335
        }
336
337
        $path = str_replace($this->coursePath, '', $path);
338
        $documentId = \DocumentManager::get_document_id($this->connector->course, $path);
339
340
        if ($documentId) {
341
342
            $result = \DocumentManager::is_visible_by_id(
343
                $documentId,
344
                $this->connector->course,
345
                api_get_session_id(),
346
                api_get_user_id()
347
            );
348
            return false;
349
        }
350
351
        return false;
352
353
    }*/
354
}
355