Passed
Push — master ( 4761c2...696f77 )
by
unknown
05:50 queued 19s
created

class/AudioHandler.php (3 issues)

1
<?php
2
3
declare(strict_types=1);
4
5
namespace XoopsModules\Suico;
6
7
/*
8
 You may not change or alter any portion of this comment or credits
9
 of supporting developers from this source code or any supporting source code
10
 which is considered copyrighted (c) material of the original comment or credit authors.
11
12
 This program is distributed in the hope that it will be useful,
13
 but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
15
*/
16
17
/**
18
 * @category        Module
19
 * @package         suico
20
 * @copyright       {@link https://xoops.org/ XOOPS Project}
21
 * @license         GNU GPL 2 or later (https://www.gnu.org/licenses/gpl-2.0.html)
22
 * @author          Bruno Barthez, Marcello Brandão aka  Suico, Mamba, LioMJ  <https://xoops.org>
23
 */
24
25
use CriteriaCompo;
26
use CriteriaElement;
27
use Xmf\Request;
28
use XoopsDatabase;
29
use XoopsMediaUploader;
30
use XoopsObject;
31
use XoopsPersistableObjectHandler;
32
33
require_once XOOPS_ROOT_PATH . '/kernel/object.php';
34
require_once XOOPS_ROOT_PATH . '/class/uploader.php';
35
36
/**
37
 * AudioHandler class.
38
 * This class provides simple mechanism for suico_audio object
39
 */
40
class AudioHandler extends XoopsPersistableObjectHandler
41
{
42
    public $isAdmin;
43
    public $helper;
44
45
    /**
46
     * Constructor
47
     * @param \XoopsDatabase|null $xoopsDatabase
48
     * @param Helper|null         $helper
49
     */
50
    public function __construct(
51
        ?XoopsDatabase $xoopsDatabase = null,
52
        $helper = null
53
    ) {
54
        /** @var Helper $this ->helper */
55
        if (null === $helper) {
56
            $this->helper = Helper::getInstance();
57
        } else {
58
            $this->helper = $helper;
59
        }
60
        $isAdmin = $this->helper->isUserAdmin();
0 ignored issues
show
The assignment to $isAdmin is dead and can be removed.
Loading history...
61
        parent::__construct($xoopsDatabase, 'suico_audios', Audio::class, 'audio_id', 'title');
62
    }
63
64
    /**
65
     * @param bool $isNew
66
     *
67
     * @return XoopsObject
68
     */
69
    public function create($isNew = true)
70
    {
71
        $obj = parent::create($isNew);
72
        if ($isNew) {
73
            $obj->setNew();
74
        } else {
75
            $obj->unsetNew();
76
        }
77
        $obj->helper = $this->helper;
78
        return $obj;
79
    }
80
81
    /**
82
     * retrieve a suico_audio
83
     *
84
     * @param int $id of the suico_audio
85
     * @return mixed reference to the {@link suico_audio} object, FALSE if failed
86
     */
87
    public function get2(
88
        $id = null
89
        //        $fields = null
90
    )
91
    {
92
        $sql = 'SELECT * FROM ' . $this->db->prefix('suico_audios') . ' WHERE audio_id=' . $id;
93
        if (!$result = $this->db->query($sql)) {
94
            return false;
95
        }
96
        $numrows = $this->db->getRowsNum($result);
97
        if (1 === $numrows) {
98
            $suicoAudio = new Audio();
99
            $suicoAudio->assignVars($this->db->fetchArray($result));
100
            return $suicoAudio;
101
        }
102
        return false;
103
    }
104
105
    /**
106
     * insert a new Audio in the database
107
     *
108
     * @param XoopsObject $xoopsObject reference to the {@link suico_audio}
109
     *                                 object
110
     * @param bool        $force
111
     * @return bool FALSE if failed, TRUE if already present and unchanged or successful
112
     */
113
    public function insert2(
114
        XoopsObject $xoopsObject,
115
        $force = false
116
    ) {
117
        global $xoopsConfig;
118
        if (Audio::class !== \get_class($xoopsObject)) {
119
            return false;
120
        }
121
        if (!$xoopsObject->isDirty()) {
122
            return true;
123
        }
124
        if (!$xoopsObject->cleanVars()) {
125
            return false;
126
        }
127
        foreach ($xoopsObject->cleanVars as $k => $v) {
128
            ${$k} = $v;
129
        }
130
        $audio_id     = Request::getString('audio_id', '', 'POST');
131
        $uid_owner    = Request::getInt('audio_id', 0, 'POST');
132
        $title        = Request::getString('title', '', 'POST');
133
        $author       = Request::getString('author', '', 'POST');
134
        $description  = Request::getText('description', '', 'POST');
135
        $filename     = Request::getString('filename', '', 'POST');
136
        $date_created = Request::getString('date_created', \time(), 'POST');
137
        $date_updated = Request::getString('date_updated', \time(), 'POST');
138
        //        $now = 'date_add(now(), interval ' . $xoopsConfig['server_TZ'] . ' hour)';
139
        if ($xoopsObject->isNew()) {
140
            // adding / modifying a suico_audio
141
            $xoopsObject = new Audio();
142
            $format      = 'INSERT INTO %s (audio_id, title, author, description, filename, uid_owner, date_created, date_updated)';
143
            $format      .= ' VALUES (%u, %s, %s, %s, %s, %u, %s, %s)';
144
            $sql         = \sprintf(
145
                $format,
146
                $this->db->prefix('suico_audios'),
147
                $audio_id,
148
                $this->db->quoteString($author),
149
                $this->db->quoteString($title),
150
                $this->db->quoteString($description),
151
                $this->db->quoteString($filename),
152
                $uid_owner,
153
                $date_created,
154
                $date_updated
155
            );
156
            $force       = true;
157
        } else {
158
            $format = 'UPDATE %s SET ';
159
            $format .= 'audio_id=%u, title=%s, author=%s, filename=%s, description=%s,uid_owner=%u, date_created=%s, date_updated=%s';
160
            $format .= ' WHERE audio_id = %u';
161
            $sql    = \sprintf(
162
                $format,
163
                $this->db->prefix('suico_audios'),
164
                $audio_id,
165
                $this->db->quoteString($title),
166
                $this->db->quoteString($author),
167
                $this->db->quoteString($filename),
168
                $uid_owner,
169
                $date_created,
170
                $date_updated,
171
                $audio_id
172
            );
173
        }
174
        if ($force) {
175
            $result = $this->db->queryF($sql);
176
        } else {
177
            $result = $this->db->query($sql);
178
        }
179
        if (!$result) {
180
            return false;
181
        }
182
        if (empty($audio_id)) {
183
            $audio_id = $this->db->getInsertId();
184
        }
185
        $xoopsObject->assignVar('audio_id', $audio_id);
186
        return true;
187
    }
188
189
    /**
190
     * delete a suico_audio from the database
191
     *
192
     * @param XoopsObject $xoopsObject reference to the suico_audio to delete
193
     * @param bool        $force
194
     * @return bool FALSE if failed.
195
     */
196
    public function delete(
197
        XoopsObject $xoopsObject,
198
        $force = false
199
    ) {
200
        if ('suico_audio' !== \get_class($xoopsObject)) {
201
            return false;
202
        }
203
        $sql = \sprintf(
204
            'DELETE FROM %s WHERE audio_id = %u',
205
            $this->db->prefix('suico_audios'),
206
            $xoopsObject->getVar('audio_id')
207
        );
208
        if ($force) {
209
            $result = $this->db->queryF($sql);
210
        } else {
211
            $result = $this->db->query($sql);
212
        }
213
        if (!$result) {
214
            return false;
215
        }
216
        return true;
217
    }
218
219
    /**
220
     * retrieve suico_audios from the database
221
     *
222
     * @param CriteriaElement|CriteriaCompo|null $criteriaElement {@link \CriteriaElement} conditions to be met
223
     * @param bool                               $id_as_key       use the UID as key for the array?
224
     * @param bool                               $as_object
225
     * @return array array of {@link suico_audio} objects
226
     */
227
    public function &getObjects(
228
        ?CriteriaElement $criteriaElement = null,
229
        $id_as_key = false,
230
        $as_object = true
231
    ) {
232
        $ret   = [];
233
        $limit = $start = 0;
234
        $sql   = 'SELECT * FROM ' . $this->db->prefix('suico_audios');
235
        if (isset($criteriaElement) && $criteriaElement instanceof CriteriaElement) {
236
            $sql .= ' ' . $criteriaElement->renderWhere();
237
            if ('' !== $criteriaElement->getSort()) {
238
                $sql .= ' ORDER BY ' . $criteriaElement->getSort() . ' ' . $criteriaElement->getOrder();
239
            }
240
            $limit = $criteriaElement->getLimit();
241
            $start = $criteriaElement->getStart();
242
        }
243
        $result = $this->db->query($sql, $limit, $start);
244
        if (!$result) {
245
            return $ret;
246
        }
247
        while (false !== ($myrow = $this->db->fetchArray($result))) {
248
            $suicoAudio = new Audio();
249
            $suicoAudio->assignVars($myrow);
250
            if (!$id_as_key) {
251
                $ret[] = &$suicoAudio;
252
            } else {
253
                $ret[$myrow['audio_id']] = &$suicoAudio;
254
            }
255
            unset($suicoAudio);
256
        }
257
        return $ret;
258
    }
259
260
    /**
261
     * count suico_audios matching a condition
262
     *
263
     * @param CriteriaElement|CriteriaCompo|null $criteriaElement {@link \CriteriaElement} to match
264
     * @return int count of suico_audios
265
     */
266
    public function getCount(
267
        ?CriteriaElement $criteriaElement = null
268
    ) {
269
        $sql = 'SELECT COUNT(*) FROM ' . $this->db->prefix('suico_audios');
270
        if (isset($criteriaElement) && $criteriaElement instanceof CriteriaElement) {
271
            $sql .= ' ' . $criteriaElement->renderWhere();
272
        }
273
        $result = $this->db->query($sql);
274
        if (!$result) {
275
            return 0;
276
        }
277
        [$count] = $this->db->fetchRow($result);
278
        return (int)$count;
279
    }
280
281
    /**
282
     * delete suico_audios matching a set of conditions
283
     *
284
     * @param CriteriaElement|CriteriaCompo|null $criteriaElement {@link \CriteriaElement}
285
     * @param bool                               $force
286
     * @param bool                               $asObject
287
     * @return bool FALSE if deletion failed
288
     */
289
    public function deleteAll(
290
        ?CriteriaElement $criteriaElement = null,
291
        $force = true,
292
        $asObject = false
293
    ) {
294
        $sql = 'DELETE FROM ' . $this->db->prefix('suico_audios');
295
        if (isset($criteriaElement) && $criteriaElement instanceof CriteriaElement) {
296
            $sql .= ' ' . $criteriaElement->renderWhere();
297
        }
298
        if (!$result = $this->db->query($sql)) {
0 ignored issues
show
The assignment to $result is dead and can be removed.
Loading history...
299
            return false;
300
        }
301
        return true;
302
    }
303
304
    /**
305
     * Upload the file and Save into database
306
     *
307
     * @param string $title       A litle description of the file
308
     * @param string $path_upload The path to where the file should be uploaded
309
     * @param string $author      the author of the music or audio file
310
     * @param        $maxfilebytes
311
     * @return bool FALSE if upload fails or database fails
312
     */
313
    public function receiveAudio(
314
        $title,
315
        $path_upload,
316
        $author,
317
        $maxfilebytes,
318
        $description
319
    ) {
320
        global $xoopsUser, $xoopsDB, $_POST, $_FILES;
321
        //busca id do user logado
322
        $uid = $xoopsUser->getVar('uid');
323
        //create a hash so it does not erase another file
324
        //$hash1 = date();
325
        //$hash = substr($hash1,0,4);
326
        // mimetypes and settings put this in admin part later
327
        $allowed_mimetypes = [
328
            'audio/mp3',
329
            'audio/x-mp3',
330
            'audio/mpeg',
331
        ];
332
        $maxfilesize       = $maxfilebytes;
333
        $uploadDir         = $path_upload;
334
        // create the object to upload
335
        $uploader = new XoopsMediaUploader(
336
            $uploadDir, $allowed_mimetypes, $maxfilesize
337
        );
338
        // fetch the media
339
        if ($uploader->fetchMedia((Request::getArray('xoops_upload_file', '', 'POST')[0]))) {
340
            //lets create a name for it
341
            $uploader->setPrefix('aud_' . $uid . '_');
342
            //now let s upload the file
343
            if (!$uploader->upload()) {
344
                // if there are errors lets return them
345
                echo '<div style="color:#FF0000; background-color:#FFEAF4; border-color:#FF0000; border-width:thick; border-style:solid; text-align:center"><p>' . $uploader->getErrors() . '</p></div>';
346
                return false;
347
            }
348
            // now let s create a new object audio and set its variables
349
            //echo "passei aqui";
350
            $audio = $this->create();
351
            $audio->setVar('uid_owner', $xoopsUser->getVar('uid'));
352
            $audio->setVar('title', $title);
353
            $audio->setVar('author', $author);
354
            $audio->setVar('description', $description);
355
            $audio->setVar(
356
                'filename',
357
                $uploader->getSavedFileName()
358
            );
359
            $audio->setVar('date_created', \time());
360
            $audio->setVar('date_updated', \time());
361
            $this->insert($audio);
362
            $saved_destination = $uploader->getSavedDestination();
0 ignored issues
show
The assignment to $saved_destination is dead and can be removed.
Loading history...
363
            //print_r($_FILES);
364
        } else {
365
            echo '<div style="color:#FF0000; background-color:#FFEAF4; border-color:#FF0000; border-width:thick; border-style:solid; text-align:center"><p>' . $uploader->getErrors() . '</p></div>';
366
            return false;
367
        }
368
        return true;
369
    }
370
}
371