Completed
Branch master (55a138)
by Michael
03:21
created

FileHandler::getFile()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php namespace XoopsModules\Extcal;
0 ignored issues
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 28 and the first side effect is on line 23.

The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.

The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.

To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.

Loading history...
2
/*
3
 * You may not change or alter any portion of this comment or credits
4
 * of supporting developers from this source code or any supporting source code
5
 * which is considered copyrighted (c) material of the original comment or credit authors.
6
 *
7
 * This program is distributed in the hope that it will be useful,
8
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
10
 */
11
12
/**
13
 * @copyright    {@link https://xoops.org/ XOOPS Project}
14
 * @license      {@link http://www.gnu.org/licenses/gpl-2.0.html GNU GPL 2 or later}
15
 * @package      extcal
16
 * @since
17
 * @author       XOOPS Development Team,
18
 */
19
20
// defined('XOOPS_ROOT_PATH') || exit('Restricted access.');
0 ignored issues
show
Unused Code Comprehensibility introduced by
70% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
21
22
// // require_once __DIR__ . '/ExtcalPersistableObjectHandler.php';
23
require_once XOOPS_ROOT_PATH . '/class/uploader.php';
24
25
/**
26
 * Class FileHandler.
27
 */
28
class FileHandler extends ExtcalPersistableObjectHandler
29
{
30
    /**
31
     * @param $db
32
     */
33
    public function __construct(\XoopsDatabase $db)
34
    {
35
        parent::__construct($db, 'extcal_file', File::class, 'file_id');
36
    }
37
38
    /**
39
     * @param $eventId
40
     *
41
     * @return bool
42
     */
43
    public function createFile($eventId)
0 ignored issues
show
Coding Style introduced by
createFile uses the super-global variable $GLOBALS which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
Coding Style introduced by
createFile uses the super-global variable $_FILES which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
44
    {
45
        $userId = $GLOBALS['xoopsUser'] ? $GLOBALS['xoopsUser']->getVar('uid') : 0;
46
47
        $allowedMimeType = [];
48
        $mimeType        = include XOOPS_ROOT_PATH . '/include/mimetypes.inc.php';
49
        foreach ($GLOBALS['xoopsModuleConfig']['allowed_file_extention'] as $fileExt) {
50
            $allowedMimeType[] = $mimeType[$fileExt];
51
        }
52
53
        $uploader = new \XoopsMediaUploader(XOOPS_ROOT_PATH . '/uploads/extcal', $allowedMimeType, 3145728);
54
        $uploader->setPrefix($userId . '-' . $eventId . '_');
55
        if ($uploader->fetchMedia('event_file')) {
56
            if (!$uploader->upload()) {
57
                return false;
58
            }
59
        } else {
60
            return false;
61
        }
62
63
        $data = [
64
            'file_name'     => $uploader->getSavedFileName(),
65
            'file_nicename' => $uploader->getMediaName(),
66
            'file_mimetype' => $uploader->getMediaType(),
67
            'file_size'     => $_FILES['event_file']['size'],
68
            'file_date'     => time(),
69
            'file_approved' => 1,
70
            'event_id'      => $eventId,
71
            'uid'           => $userId,
72
        ];
73
74
        $file = $this->create();
75
        $file->setVars($data);
76
77
        return $this->insert($file);
78
    }
79
80
    /**
81
     * @param $file
82
     */
83
    public function deleteFile(&$file)
84
    {
85
        $this->_deleteFile($file);
86
        $this->deleteById($file->getVar('file_id'));
87
    }
88
89
    /**
90
     * @param $eventId
91
     *
92
     * @return array
93
     */
94
    public function getEventFiles($eventId)
95
    {
96
        $criteria =  new \CriteriaCompo();
97
        $criteria->add( new \Criteria('file_approved', 1));
98
        $criteria->add( new \Criteria('event_id', $eventId));
99
100
        return $this->getObjects($criteria);
101
    }
102
103
    /**
104
     * @param $eventId
105
     */
106
    public function updateEventFile($eventId)
0 ignored issues
show
Coding Style introduced by
updateEventFile uses the super-global variable $_POST which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
107
    {
108
        $criteria =  new \CriteriaCompo();
109
        $criteria->add( new \Criteria('file_approved', 1));
110
        $criteria->add( new \Criteria('event_id', $eventId));
111
112
        if (isset($_POST['filetokeep'])) {
113
            if (is_array($_POST['filetokeep'])) {
114
                $count = count($_POST['filetokeep']);
0 ignored issues
show
Unused Code introduced by
$count is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
115
                $in    = '(' . $_POST['filetokeep'][0];
116
                array_shift($_POST['filetokeep']);
117
                foreach ($_POST['filetokeep'] as $elmt) {
118
                    $in .= ',' . $elmt;
119
                }
120
                $in .= ')';
121
            } else {
122
                $in = '(' . $_POST['filetokeep'] . ')';
123
            }
124
            $criteria->add( new \Criteria('file_id', $in, 'NOT IN'));
125
        }
126
127
        $files = $this->getObjects($criteria);
128
        foreach ($files as $file) {
129
            $this->deleteFile($file);
130
        }
131
    }
132
133
    /**
134
     * @param $fileId
135
     *
136
     * @return mixed
137
     */
138
    public function getFile($fileId)
139
    {
140
        return $this->get($fileId);
141
    }
142
143
    /**
144
     * @param $files
145
     */
146
    public function formatFilesSize(&$files)
147
    {
148
        for ($i = 0, $iMax = count($files); $i < $iMax; ++$i) {
149
            $this->formatFileSize($files[$i]);
150
        }
151
    }
152
153
    /**
154
     * @param $file
155
     */
156
    public function formatFileSize(&$file)
157
    {
158
        if ($file['file_size'] > 1000) {
159
            $file['formated_file_size'] = round($file['file_size'] / 1000) . 'kb';
160
        } else {
161
            $file['formated_file_size'] = '1kb';
162
        }
163
    }
164
165
    /**
166
     * @param $file
167
     */
168
    public function _deleteFile(&$file)
169
    {
170
        if (file_exists(XOOPS_ROOT_PATH . '/uploads/extcal/' . $file->getVar('file_name'))) {
171
            unlink(XOOPS_ROOT_PATH . '/uploads/extcal/' . $file->getVar('file_name'));
172
        }
173
    }
174
}
175