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

class/file.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

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');
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
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
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)
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)
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']);
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