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