mkdir($updir); chmod($updir, octdec('0777')); } if (!file_exists($updir . '/sizes')) { mkdir($updir . '/sizes'); chmod($updir . '/sizes', octdec('0777')); } include RMCPATH . '/class/uploader.php'; $uploader = new RMFileUploader($updir, $cat->max_file_size(), array('gif', 'jpg', 'jpeg', 'png')); $err = array(); if (!$uploader->fetchMedia('Filedata')) { error($uploader->getErrors()); } if (!$uploader->upload()) { error($uploader->getErrors()); } // Insertamos el archivo en la base de datos $image = new RMImage(); $image->setVar('title', $uploader->savedFileName); $image->setVar('date', time()); $image->setVar('file', $uploader->savedFileName); $image->setVar('cat', $cat->id()); $image->setVar('uid', $xoopsUser->uid()); if (!$image->save()) { unlink($uploader->savedDestination); error(__('File could not be inserted to database!', 'rmcommon')); } $ret['message'] = '1'; $ret['id'] = $image->id(); echo json_encode($ret); die;
/** * Update image data */ function update_image() { global $xoopsUser, $xoopsSecurity; set_time_limit(0); $title = rmc_server_var($_POST, 'title', ''); $category = rmc_server_var($_POST, 'cat', ''); $desc = rmc_server_var($_POST, 'desc', ''); $page = rmc_server_var($_POST, 'page', 1); $id = rmc_server_var($_POST, 'id', 0); if (!$xoopsSecurity->check()) { redirectMsg('images.php', __('Operation not allowed!', 'rmcommon'), 1); die; } if ($id <= 0) { redirectMsg("images.php?category={$cat}&page={$page}", __('Image ID not provided!', 'rmcommon'), 1); die; } if (trim($title) == '') { redirectMsg("images.php?action=edit&id={$id}&page={$page}", __('You must provide a title for this image', 'rmcommon'), 1); die; } $image = new RMImage($id); if ($image->isNew()) { redirectMsg("images.php?category={$cat}&page={$page}", __('Image not exists!', 'rmcommon'), 1); die; } $cat = new RMImageCategory($category); if ($cat->isNew()) { redirectMsg("images.php", __('Category not exist!', 'rmcommon'), 1); die; } if ($cat->id() != $image->getVar('cat')) { $pcat = new RMImageCategory($image->getVar('cat')); } $image->setVar('title', $title); $image->setVar('desc', $desc); if (isset($pcat)) { $image->setVar('cat', $cat->id()); } if (!$image->save()) { redirectMsg("images.php?action=edit&id={$id}&page={$page}", __('the image could not be updated!', 'rmcommon') . '<br />' . $image->errors(), 1); die; } // Modify image dimensions if category has changed if (!isset($pcat)) { redirectMsg("images.php?category=" . $cat->id() . "&page={$page}", __('Image updated succesfully!', 'rmcommon'), 0); die; } $fd = pathinfo($image->getVar('file')); $updir = XOOPS_UPLOAD_PATH . '/' . date('Y', $image->getVar('date')) . '/' . date('m', time()); // Delete current image files foreach ($pcat->getVar('sizes') as $size) { if ($size['width'] <= 0) { continue; } $file = $updir . '/sizes/' . $fd['filename'] . '_' . $size['width'] . 'x' . $size['height'] . '.' . $fd['extension']; @unlink($file); } // Create new image files foreach ($cat->getVar('sizes') as $size) { if ($size['width'] <= 0 && $size['height'] <= 0) { continue; } $name = $updir . '/sizes/' . $fd['filename'] . '_' . $size['width'] . 'x' . $size['height'] . '.' . $fd['extension']; $sizer = new RMImageResizer($updir . '/' . $image->getVar('file'), $name); switch ($size['type']) { case 'crop': $sizer->resizeAndCrop($size['width'], $size['height']); break; default: if ($size['width'] <= 0 || $size['height'] <= 0) { $sizer->resizeWidth($size['width']); } else { $sizer->resizeWidthOrHeight($size['width'], $size['height']); } break; } $width = $width == 0 ? $size['width'] : $width; if ($width < $size['width']) { $with = $size['width']; $tfile = str_replace(XOOPS_UPLOAD_PATH, XOOPS_UPLOAD_URL, $name); } } redirectMsg('images.php?category=' . $cat->id(), __('Image updated successfully!', 'rmcommon'), 0); }