Completed
Push — master ( a9decc...a21b67 )
by Michael
02:51
created

ExtcalFileHandler::createFile()   B

Complexity

Conditions 5
Paths 12

Size

Total Lines 36
Code Lines 25

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 25
nc 12
nop 1
dl 0
loc 36
rs 8.439
c 0
b 0
f 0
1
<?php
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 22.

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('XOOPS root path not defined');
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 ExtcalFile.
27
 */
28
class ExtcalFile extends XoopsObject
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
29
{
30
    /**
31
     * ExtcalFile constructor.
32
     */
33
    public function __construct()
34
    {
35
        $this->initVar('file_id', XOBJ_DTYPE_INT, null, false);
36
        $this->initVar('file_name', XOBJ_DTYPE_TXTBOX, null, false, 255);
37
        $this->initVar('file_nicename', XOBJ_DTYPE_TXTBOX, null, false, 255);
38
        $this->initVar('file_mimetype', XOBJ_DTYPE_TXTBOX, null, false, 255);
39
        $this->initVar('file_size', XOBJ_DTYPE_INT, null, false);
40
        $this->initVar('file_download', XOBJ_DTYPE_INT, null, false);
41
        $this->initVar('file_date', XOBJ_DTYPE_INT, null, false);
42
        $this->initVar('file_approved', XOBJ_DTYPE_INT, null, false);
43
        $this->initVar('event_id', XOBJ_DTYPE_INT, null, false);
44
        $this->initVar('uid', XOBJ_DTYPE_INT, null, false);
45
    }
46
}
47
48
/**
49
 * Class ExtcalFileHandler.
50
 */
51
class ExtcalFileHandler extends ExtcalPersistableObjectHandler
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class should be in its own file to aid autoloaders.

Having each class in a dedicated file usually plays nice with PSR autoloaders and is therefore a well established practice. If you use other autoloaders, you might not want to follow this rule.

Loading history...
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
52
{
53
    /**
54
     * @param $db
55
     */
56
    public function __construct(XoopsDatabase $db)
57
    {
58
        parent::__construct($db, 'extcal_file', _EXTCAL_CLN_FILE, 'file_id');
59
    }
60
61
    /**
62
     * @param $eventId
63
     *
64
     * @return bool
65
     */
66
    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...
67
    {
68
        $userId = $GLOBALS['xoopsUser'] ? $GLOBALS['xoopsUser']->getVar('uid') : 0;
69
70
        $allowedMimeType = array();
71
        $mimeType        = include XOOPS_ROOT_PATH . '/include/mimetypes.inc.php';
72
        foreach ($GLOBALS['xoopsModuleConfig']['allowed_file_extention'] as $fileExt) {
73
            $allowedMimeType[] = $mimeType[$fileExt];
74
        }
75
76
        $uploader = new XoopsMediaUploader(XOOPS_ROOT_PATH . '/uploads/extcal', $allowedMimeType, 3145728);
77
        $uploader->setPrefix($userId . '-' . $eventId . '_');
78
        if ($uploader->fetchMedia('event_file')) {
79
            if (!$uploader->upload()) {
80
                return false;
81
            }
82
        } else {
83
            return false;
84
        }
85
86
        $data = array(
87
            'file_name'     => $uploader->getSavedFileName(),
88
            'file_nicename' => $uploader->getMediaName(),
89
            'file_mimetype' => $uploader->getMediaType(),
90
            'file_size'     => $_FILES['event_file']['size'],
91
            'file_date'     => time(),
92
            'file_approved' => 1,
93
            'event_id'      => $eventId,
94
            'uid'           => $userId,
95
        );
96
97
        $file = $this->create();
98
        $file->setVars($data);
99
100
        return $this->insert($file);
101
    }
102
103
    /**
104
     * @param $file
105
     */
106
    public function deleteFile(&$file)
107
    {
108
        $this->_deleteFile($file);
109
        $this->deleteById($file->getVar('file_id'));
110
    }
111
112
    /**
113
     * @param $eventId
114
     *
115
     * @return array
116
     */
117
    public function getEventFiles($eventId)
118
    {
119
        $criteria = new CriteriaCompo();
120
        $criteria->add(new Criteria('file_approved', 1));
121
        $criteria->add(new Criteria('event_id', $eventId));
122
123
        return $this->getObjects($criteria);
124
    }
125
126
    /**
127
     * @param $eventId
128
     */
129
    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...
130
    {
131
        $criteria = new CriteriaCompo();
132
        $criteria->add(new Criteria('file_approved', 1));
133
        $criteria->add(new Criteria('event_id', $eventId));
134
135
        if (isset($_POST['filetokeep'])) {
136
            if (is_array($_POST['filetokeep'])) {
137
                $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...
138
                $in    = '(' . $_POST['filetokeep'][0];
139
                array_shift($_POST['filetokeep']);
140
                foreach ($_POST['filetokeep'] as $elmt) {
141
                    $in .= ',' . $elmt;
142
                }
143
                $in .= ')';
144
            } else {
145
                $in = '(' . $_POST['filetokeep'] . ')';
146
            }
147
            $criteria->add(new Criteria('file_id', $in, 'NOT IN'));
148
        }
149
150
        $files = $this->getObjects($criteria);
151
        foreach ($files as $file) {
152
            $this->deleteFile($file);
153
        }
154
    }
155
156
    /**
157
     * @param $fileId
158
     *
159
     * @return mixed
160
     */
161
    public function getFile($fileId)
162
    {
163
        return $this->get($fileId);
164
    }
165
166
    /**
167
     * @param $files
168
     */
169
    public function formatFilesSize(&$files)
170
    {
171
        for ($i = 0, $iMax = count($files); $i < $iMax; ++$i) {
172
            $this->formatFileSize($files[$i]);
173
        }
174
    }
175
176
    /**
177
     * @param $file
178
     */
179
    public function formatFileSize(&$file)
180
    {
181
        if ($file['file_size'] > 1000) {
182
            $file['formated_file_size'] = round($file['file_size'] / 1000) . 'kb';
183
        } else {
184
            $file['formated_file_size'] = '1kb';
185
        }
186
    }
187
188
    /**
189
     * @param $file
190
     */
191
    public function _deleteFile(&$file)
192
    {
193
        if (file_exists(XOOPS_ROOT_PATH . '/uploads/extcal/' . $file->getVar('file_name'))) {
194
            unlink(XOOPS_ROOT_PATH . '/uploads/extcal/' . $file->getVar('file_name'));
195
        }
196
    }
197
}
198