Test Setup Failed
Push — master ( 4e700f...c7183e )
by Julito
63:12
created

DropBoxDriver::_dirname()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 2
nc 4
nop 1
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
/* For licensing terms, see /license.txt */
3
4
namespace Chamilo\CoreBundle\Component\Editor\Driver;
5
6
use Chamilo\CoreBundle\Entity\CDropboxFile;
7
use Chamilo\CoreBundle\Component\Editor\Connector;
8
9
/**
10
 * Class DropBoxDriver
11
 * @todo finish implementation
12
 * @package Chamilo\CoreBundle\Component\Editor\Driver
13
 */
14
class DropBoxDriver extends \elFinderVolumeMySQL implements DriverInterface
0 ignored issues
show
Bug introduced by
There is at least one abstract method in this class. Maybe declare it as abstract, or implement the remaining methods: allow, setup
Loading history...
15
{
16
    /** @var string */
17
    public $name = 'DropBoxDriver';
18
19
    /** @var Connector */
20
    public $connector;
21
22
    /**
23
     * DropBoxDriver constructor.
24
     */
25
    public function __construct()
26
    {
27
        parent::__construct();
28
    }
29
30
    /**
31
     * Gets driver name.
32
     * @return string
33
     */
34
    public function getName()
35
    {
36
        return $this->name;
37
    }
38
39
    /**
40
     * Gets driver name.
41
     * @param string
42
     */
43
    public function setName($name)
44
    {
45
        $this->name = $name;
46
    }
47
48
    /**
49
     * Set connector
50
     * @param Connector $connector
51
     */
52
    public function setConnector(Connector $connector)
53
    {
54
        $this->connector = $connector;
55
    }
56
57
    /**
58
     * @return array
59
     */
60
    public function getAppPluginOptions()
61
    {
62
        return $this->getOptionsPlugin('chamilo');
63
    }
64
65
    /**
66
     * @return Connector
67
     */
68
    public function setConnectorFromPlugin()
69
    {
70
        $options = $this->getAppPluginOptions();
71
        $this->setConnector($options['connector']);
72
    }
73
74
    /**
75
     * {@inheritdoc}
76
     */
77
    public function getConfiguration()
78
    {
79
        if ($this->connector->security->isGranted('IS_AUTHENTICATED_FULLY')) {
80
            /** @var \Chamilo\CoreBundle\Entity\Repository\UserRepository $repository */
81
            /*$repository = $this->connector->entityManager->getRepository('Chamilo\UserBundle\Entity\User');
82
            $courses = $repository->getCourses($this->connector->user);*/
83
84
            //if (!empty($courses)) {
85
            $userId = $this->connector->user->getUserId();
0 ignored issues
show
Bug introduced by
The method getUserId cannot be called on $this->connector->user (of type array).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
86
            if (!empty($userId)) {
87
                return array(
88
                    'driver'     => 'DropBoxDriver',
89
                    'path'       => '1',
90
                    'alias' => 'dropbox',
91
                    'tmpPath' => $this->connector->paths['path.temp'],
92
                    //'alias' => $courseInfo['code'].' personal documents',
93
                    //'URL' => $this->getCourseDocumentRelativeWebPath().$path,
94
                    'accessControl' => 'access'
95
                );
96
            }
97
            //}
98
        }
99
    }
100
101
    protected function init()
102
    {
103
        $this->updateCache($this->options['path'], $this->_stat($this->options['path']));
0 ignored issues
show
Security Bug introduced by
It seems like $this->_stat($this->options['path']) targeting Chamilo\CoreBundle\Compo...\DropBoxDriver::_stat() can also be of type false; however, elFinderVolumeDriver::updateCache() does only seem to accept array, did you maybe forget to handle an error condition?
Loading history...
104
        return true;
105
    }
106
107
    /**
108
     * Set tmp path
109
     *
110
     * @return void
111
     * @author Dmitry (dio) Levashov
112
     **/
113
    protected function configure()
114
    {
115
        parent::configure();
116
117
        if (($tmp = $this->options['tmpPath'])) {
118
            if (!file_exists($tmp)) {
119
                if (@mkdir($tmp)) {
120
                    @chmod($tmp, $this->options['tmbPathMode']);
121
                }
122
            }
123
124
            $this->tmpPath = is_dir($tmp) && is_writable($tmp) ? $tmp : false;
125
        }
126
127
        if (!$this->tmpPath && $this->tmbPath && $this->tmbPathWritable) {
128
            $this->tmpPath = $this->tmbPath;
129
        }
130
131
        $this->mimeDetect = 'internal';
132
    }
133
134
135
136
    /**
137
     * Close connection
138
     *
139
     * @return void
140
     * @author Dmitry (dio) Levashov
141
     **/
142
    public function umount()
143
    {
144
        return true;
145
    }
146
147
    /* FS API */
148
    /**
149
     * Cache dir contents
150
     *
151
     * @param  string  $path  dir path
152
     * @return void
153
     * @author Dmitry Levashov
154
     **/
155
    protected function cacheDir($path)
156
    {
157
        $this->setConnectorFromPlugin();
158
        $posts = $this->connector->user->getDropBoxReceivedFiles();
0 ignored issues
show
Bug introduced by
The method getDropBoxReceivedFiles cannot be called on $this->connector->user (of type array).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
159
        $this->dirsCache[$path] = array();
160
161
        if (!empty($posts)) {
162
            foreach ($posts as $post) {
163
                /** @var CDropboxFile $file */
164
                $file = $post->getFile();
165
166
                $data = $this->transformFileInStat($file);
167
                $id = $data['id'];
168
                if (($stat = $this->updateCache($id, $data)) && empty($stat['hidden'])) {
169
                    $this->dirsCache[$path][] = $id;
170
                }
171
            }
172
            return $this->dirsCache[$path];
173
        }
174
        return $this->dirsCache[$path];
175
    }
176
177
    /***************** file stat ********************/
178
    /**
179
     * Return stat for given path.
180
     * Stat contains following fields:
181
     * - (int)    size    file size in b. required
182
     * - (int)    ts      file modification time in unix time. required
183
     * - (string) mime    mimetype. required for folders, others - optionally
184
     * - (bool)   read    read permissions. required
185
     * - (bool)   write   write permissions. required
186
     * - (bool)   locked  is object locked. optionally
187
     * - (bool)   hidden  is object hidden. optionally
188
     * - (string) alias   for symlinks - link target path relative to root path. optionally
189
     * - (string) target  for symlinks - link target path. optionally
190
     *
191
     * If file does not exists - returns empty array or false.
192
     *
193
     * @param  string  $path    file path
194
     * @return array|false
195
     * @author Dmitry (dio) Levashov
196
     **/
197
    protected function _stat($path)
198
    {
199
        $this->setConnectorFromPlugin();
200
201
        $userId = $this->connector->user->getUserId();
0 ignored issues
show
Bug introduced by
The method getUserId cannot be called on $this->connector->user (of type array).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
202
        $criteria = array();
203
        $criteria['uploaderId'] = $userId;
204
205
        if ($path != 1) {
206
            $criteria['filename'] = $path;
207
            $criteria = array('filename' => $path);
208
        } else {
209
            return $this->returnDirectory();
210
        }
211
212
        $file = $this->connector->entityManager->getRepository('Chamilo\CoreBundle\Entity\CDropboxFile')->findOneBy($criteria);
213
214
        if ($file) {
215
            $stat = $this->transformFileInStat($file);
216
            return $stat;
217
        }
218
        return array();
219
    }
220
221
    /**
222
     * @return array
223
     */
224
    private function returnDirectory()
225
    {
226
        return array(
227
            //'id' => $file->getId().$file->getCId(),
228
            'name' => 'Dropbox',
229
            //'ts' => $file->getUploadDate(),
230
            'mime' => 'directory',
231
            'read' => true,
232
            'write' => true,
233
            'locked' => false,
234
            'hidden' => false,
235
            'dirs' => 0
236
        );
237
    }
238
239
    /**
240
     * @param CDropboxFile $file
241
     * @return array
242
     */
243
    private function transformFileInStat(CDropboxFile $file)
244
    {
245
        $stat = array(
246
            'id' => $file->getId().$file->getCId(),
247
            'name' => $file->getFilename(),
248
            'ts' => $file->getUploadDate(),
249
            'mime' => 'directory',
250
            'read' => true,
251
            'write' => false,
252
            'locked' => false,
253
            'hidden' => false,
254
            'width' => 100,
255
            'height' => 100,
256
            'dirs' => 0
257
        );
258
        return $stat;
259
260
        /*
261
        if ($stat['parent_id']) {
262
            $stat['phash'] = $this->encode($stat['parent_id']);
263
        }
264
        if ($stat['mime'] == 'directory') {
265
            unset($stat['width']);
266
            unset($stat['height']);
267
        } else {
268
            unset($stat['dirs']);
269
        }
270
        unset($stat['id']);
271
        unset($stat['parent_id']);
272
        */
273
    }
274
275
    /**
276
     * Return array of parents paths (ids)
277
     *
278
     * @param  int   $path  file path (id)
279
     * @return array
280
     * @author Dmitry (dio) Levashov
281
     **/
282
    protected function getParents($path)
283
    {
284
        $parents = array();
285
        while ($path) {
286
            if ($file = $this->stat($path)) {
287
                array_unshift($parents, $path);
288
                $path = isset($file['phash']) ? $this->decode($file['phash']) : false;
289
            }
290
        }
291
292
        if (count($parents)) {
293
            array_pop($parents);
294
        }
295
        return $parents;
296
    }
297
298
    /**
299
     * Return correct file path for LOAD_FILE method
300
     *
301
     * @param  string $path  file path (id)
302
     * @return string
303
     * @author Troex Nevelin
304
     **/
305
    protected function loadFilePath($path)
306
    {
307
        $realPath = realpath($path);
308
        if (DIRECTORY_SEPARATOR == '\\') { // windows
309
            $realPath = str_replace('\\', '\\\\', $realPath);
310
        }
311
        return $this->db->real_escape_string($realPath);
312
    }
313
314
    /**
315
     * Recursive files search
316
     *
317
     * @param  string  $path   dir path
318
     * @param  string  $q      search string
319
     * @param  array   $mimes
320
     * @return array
321
     * @author Dmitry (dio) Levashov
322
     **/
323
    protected function doSearch($path, $q, $mimes)
324
    {
325
        return array();
326
    }
327
328
    /*********************** paths/urls *************************/
329
330
    /**
331
     * Return parent directory path
332
     *
333
     * @param  string  $path  file path
334
     * @return string
335
     * @author Dmitry (dio) Levashov
336
     **/
337
    protected function _dirname($path)
338
    {
339
        return ($stat = $this->stat($path)) ? ($stat['phash'] ? $this->decode($stat['phash']) : $this->root) : false;
340
    }
341
342
    /**
343
     * Return file name
344
     *
345
     * @param  string  $path  file path
346
     * @return string
347
     * @author Dmitry (dio) Levashov
348
     **/
349
    protected function _basename($path)
350
    {
351
        return ($stat = $this->stat($path)) ? $stat['name'] : false;
352
    }
353
354
    /**
355
     * Return normalized path, this works the same as os.path.normpath() in Python
356
     *
357
     * @param  string $path path
358
     * @return string
359
     * @author Troex Nevelin
360
     **/
361
    protected function _normpath($path)
362
    {
363
        return $path;
364
    }
365
366
    /**
367
     * Return file path related to root dir
368
     *
369
     * @param  string $path file path
370
     * @return string
371
     * @author Dmitry (dio) Levashov
372
     **/
373
    protected function _relpath($path)
374
    {
375
        return $path;
376
    }
377
378
    /**
379
     * Convert path related to root dir into real path
380
     *
381
     * @param  string  $path  file path
382
     * @return string
383
     * @author Dmitry (dio) Levashov
384
     **/
385
    protected function _abspath($path)
386
    {
387
        return $path;
388
    }
389
390
    /**
391
     * Return fake path started from root dir
392
     *
393
     * @param  string  $path  file path
394
     * @return string
395
     * @author Dmitry (dio) Levashov
396
     **/
397
    protected function _path($path)
398
    {
399
        if (($file = $this->stat($path)) == false) {
400
            return '';
401
        }
402
403
        $parentsIds = $this->getParents($path);
404
        $path = '';
405
        foreach ($parentsIds as $id) {
406
            $dir = $this->stat($id);
407
            $path .= $dir['name'].$this->separator;
408
        }
409
        return $path.$file['name'];
410
    }
411
412
    /**
413
     * Return true if $path is children of $parent
414
     *
415
     * @param  string  $path    path to check
416
     * @param  string  $parent  parent path
417
     * @return bool
418
     * @author Dmitry (dio) Levashov
419
     **/
420
    protected function _inpath($path, $parent)
421
    {
422
        return $path == $parent
423
            ? true
424
            : in_array($parent, $this->getParents($path));
425
    }
426
427
    /**
428
     * Return true if path is dir and has at least one childs directory
429
     *
430
     * @param  string  $path  dir path
431
     * @return bool
432
     * @author Dmitry (dio) Levashov
433
     **/
434
    protected function _subdirs($path)
435
    {
436
        return ($stat = $this->stat($path)) && isset($stat['dirs']) ? $stat['dirs'] : false;
437
    }
438
439
    /**
440
     * Return object width and height
441
     * Usualy used for images, but can be realize for video etc...
442
     *
443
     * @param  string  $path  file path
444
     * @param  string  $mime  file mime type
445
     * @return string
446
     * @author Dmitry (dio) Levashov
447
     **/
448
    protected function _dimensions($path, $mime)
449
    {
450
        return ($stat = $this->stat($path)) && isset($stat['width']) && isset($stat['height']) ? $stat['width'].'x'.$stat['height'] : '';
451
    }
452
453
    /******************** file/dir content *********************/
454
455
    /**
456
     * Return files list in directory.
457
     *
458
     * @param  string  $path  dir path
459
     * @return array
460
     * @author Dmitry (dio) Levashov
461
     **/
462
    protected function _scandir($path)
463
    {
464
        return isset($this->dirsCache[$path])
465
            ? $this->dirsCache[$path]
466
            : $this->cacheDir($path);
467
    }
468
469
    /**
470
     * Open file and return file pointer
471
     *
472
     * @param  string  $path  file path
473
     * @param  string  $mode  open file mode (ignored in this driver)
474
     * @return resource|false
475
     * @author Dmitry (dio) Levashov
476
     **/
477
    protected function _fopen($path, $mode = 'rb')
478
    {
479
        $fp = $this->tmbPath
480
            ? @fopen($this->tmpname($path), 'w+')
481
            : @tmpfile();
482
483
        if ($fp) {
484
            if (($res = $this->query('SELECT content FROM '.$this->tbf.' WHERE id="'.$path.'"'))
485
                && ($r = $res->fetch_assoc())) {
486
                fwrite($fp, $r['content']);
487
                rewind($fp);
488
                return $fp;
489
            } else {
490
                $this->_fclose($fp, $path);
491
            }
492
        }
493
494
        return false;
495
    }
496
497
    /**
498
     * Close opened file
499
     *
500
     * @param  resource  $fp  file pointer
501
     * @return bool
502
     * @author Dmitry (dio) Levashov
503
     **/
504
    protected function _fclose($fp, $path = '')
505
    {
506
        @fclose($fp);
507
        if ($path) {
508
            @unlink($this->tmpname($path));
509
        }
510
    }
511
512
    /********************  file/dir manipulations *************************/
513
514
    /**
515
     * Create dir and return created dir path or false on failed
516
     *
517
     * @param  string  $path  parent dir path
518
     * @param string  $name  new directory name
519
     * @return string|bool
520
     * @author Dmitry (dio) Levashov
521
     **/
522
    protected function _mkdir($path, $name)
523
    {
524
        return $this->make($path, $name, 'directory') ? $this->_joinPath($path, $name) : false;
525
    }
526
527
    /**
528
     * {@inheritdoc}
529
     */
530
    protected function _mkfile($path, $name)
531
    {
532
        return false;
533
    }
534
535
    /**
536
     * {@inheritdoc}
537
     */
538
    protected function _symlink($target, $path, $name)
539
    {
540
        return false;
541
    }
542
543
    /**
544
     * {@inheritdoc}
545
     */
546
    protected function _copy($source, $targetDir, $name)
547
    {
548
        return false;
549
    }
550
551
    /**
552
     * {@inheritdoc}
553
     */
554
    protected function _move($source, $targetDir, $name)
555
    {
556
        return false;
557
    }
558
559
    /**
560
     * Remove file
561
     *
562
     * @param  string  $path  file path
563
     * @return bool
564
     * @author Dmitry (dio) Levashov
565
     **/
566
    protected function _unlink($path)
567
    {
568
        return false;
569
        return $this->query(sprintf('DELETE FROM %s WHERE id=%d AND mime!="directory" LIMIT 1', $this->tbf, $path)) && $this->db->affected_rows;
0 ignored issues
show
Unused Code introduced by
return $this->query(spri...his->db->affected_rows; does not seem to be reachable.

This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.

Unreachable code is most often the result of return, die or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.

Loading history...
570
    }
571
572
    /**
573
     * Remove dir
574
     *
575
     * @param  string  $path  dir path
576
     * @return bool
577
     * @author Dmitry (dio) Levashov
578
     **/
579
    protected function _rmdir($path)
580
    {
581
        return false;
582
        return $this->query(sprintf('DELETE FROM %s WHERE id=%d AND mime="directory" LIMIT 1', $this->tbf, $path)) && $this->db->affected_rows;
0 ignored issues
show
Unused Code introduced by
return $this->query(spri...his->db->affected_rows; does not seem to be reachable.

This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.

Unreachable code is most often the result of return, die or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.

Loading history...
583
    }
584
585
    /**
586
     * undocumented function
587
     *
588
     * @return void
589
     * @author Dmitry Levashov
590
     **/
591
    protected function _setContent($path, $fp)
592
    {
593
        rewind($fp);
594
        $fstat = fstat($fp);
595
        $size = $fstat['size'];
596
    }
597
598
    /**
599
     * {@inheritdoc}
600
     */
601
    protected function _save($fp, $dir, $name, $stat)
602
    {
603
        return false;
604
    }
605
606
    /**
607
     * {@inheritdoc}
608
     */
609
    protected function _getContents($path)
610
    {
611
        return false;
612
        //return ($res = $this->query(sprintf('SELECT content FROM %s WHERE id=%d', $this->tbf, $path))) && ($r = $res->fetch_assoc()) ? $r['content'] : false;
613
    }
614
615
    /**
616
     * Write a string to a file
617
     *
618
     * @param  string  $path     file path
619
     * @param  string  $content  new file content
620
     * @return bool
621
     * @author Dmitry (dio) Levashov
622
     **/
623
    protected function _filePutContents($path, $content)
624
    {
625
        return false;
626
        //return $this->query(sprintf('UPDATE %s SET content="%s", size=%d, mtime=%d WHERE id=%d LIMIT 1', $this->tbf, $this->db->real_escape_string($content), strlen($content), time(), $path));
627
    }
628
629
    /**
630
     * {@inheritdoc}
631
     */
632
    protected function _checkArchivers()
633
    {
634
        return;
635
    }
636
637
    /**
638
     * {@inheritdoc}
639
     */
640
    protected function _unpack($path, $arc)
641
    {
642
        return;
643
    }
644
645
    /**
646
     * {@inheritdoc}
647
     */
648
    protected function _findSymlinks($path)
649
    {
650
        return false;
651
    }
652
653
    /**
654
     * {@inheritdoc}
655
     */
656
    protected function _extract($path, $arc)
657
    {
658
        return false;
659
    }
660
661
    /**
662
     * {@inheritdoc}
663
     */
664
    protected function _archive($dir, $files, $name, $arc)
665
    {
666
        return false;
667
    }
668
}
669