示例#1
0
/**
* @desc almacena la información de la imagen
**/
function saveImages($edit = 0)
{
    global $mc, $xoopsSecurity, $db, $xoopsUser, $db;
    foreach ($_POST as $k => $v) {
        ${$k} = $v;
    }
    $ruta = "page={$page}&limit={$limit}&search={$search}&owner={$owner}&sort={$sort}&mode={$mode}";
    if (!$xoopsSecurity->check()) {
        redirectMsg('./images.php?' . $ruta, __('Session token expired!', 'galleries'), 1);
        die;
    }
    if ($edit) {
        //Verificamos si la imagen es válida
        if ($id <= 0) {
            redirectMsg('./images.php?op=edit&id=' . $id . '&' . $ruta, __('Image ID not valid!', 'galleries'), 1);
            die;
        }
        //Verificamos si la imagen existe
        $img = new GSImage($id);
        if ($img->isNew()) {
            redirectMsg('./images.php?op=edit&id=' . $id . '&' . $ruta, __('Specified image does not exists!', 'galleries'), 1);
            die;
        }
    } else {
        $img = new GSImage();
    }
    $img->setTitle($title);
    $img->setDesc($desc);
    $img->isNew() ? $img->setCreated(time()) : $img->setModified(time());
    if (!$edit) {
        $img->setOwner($uid);
    }
    $img->setPublic($public);
    //Insertamos las etiquetas
    $tgs = array();
    $tags = explode(",", $tags);
    foreach ($tags as $k) {
        $k = trim($k);
        if (!$k) {
            continue;
        }
        if (strlen($k) < $mc['min_tag'] || strlen($k) > $mc['max_tag']) {
            continue;
        }
        $tag = new GSTag($k);
        if (!$tag->isNew()) {
            $tgs[] = $tag->id();
            continue;
        }
        $tag->setTag(strtolower($k));
        if ($tag->save()) {
            $tgs[] = $tag->id();
        }
    }
    if (!$edit) {
        if ($xoopsUser->uid() == $uid) {
            $xu =& $xoopsUser;
        } else {
            $xu = new XoopsUser($uid);
        }
        //Verificamos si el usuario se encuentra registrado
        $user = new GSUser($xu->uname());
        if ($user->isNew()) {
            //Insertamos información del usuario
            $user->setUid($uid);
            $user->setUname($xu->uname());
            $user->setQuota($mc['quota'] * 1024 * 1024);
            $user->setDate(time());
            if (!$user->save()) {
                redirectMsg('./images.php?op=' . ($edit ? 'edit&id=' . $id : 'new') . '&' . $ruta, __('New user owner could not be saved!', 'galleries'), 1);
                die;
            } else {
                mkdir($mc['storedir'] . "/" . $user->uname());
                mkdir($mc['storedir'] . "/" . $user->uname() . "/ths");
                mkdir($mc['storedir'] . "/" . $user->uname() . "/formats");
            }
        }
    } else {
        $user = new GSUser($img->owner(), 1);
    }
    //Imagen
    include_once RMCPATH . '/class/uploader.php';
    $folder = $mc['storedir'] . "/" . $user->uname();
    $folderths = $mc['storedir'] . "/" . $user->uname() . "/ths";
    $up = new RMFileUploader($folder, $mc['size_image'] * 1024, array('jpg', 'png', 'gif'));
    if ($edit) {
        $filename = $img->image();
    } else {
        $filename = '';
    }
    if ($up->fetchMedia('image')) {
        if (!$up->upload()) {
            redirectMsg('./images.php?op=' . ($edit ? 'edit&id=' . $id : 'new') . '&' . $ruta, $up->getErrors(), 1);
            die;
        }
        if ($edit && $img->image() != '') {
            @unlink($mc['storedir'] . "/" . $user->uname() . "/" . $img->image());
            @unlink($mc['storedir'] . "/" . $user->uname() . "/ths/" . $img->image());
            @unlink($mc['storedir'] . "/originals/" . $img->image());
        }
        $filename = $up->getSavedFileName();
        $fullpath = $up->getSavedDestination();
        $thSize = $mc['image_ths'];
        $imgSize = $mc['image'];
        if ($thSize[0] <= 0) {
            $thSize[0] = 100;
        }
        if (!isset($thSize[1]) || $thSize[1] <= 0) {
            $thSize[1] = $thSize[0];
        }
        if ($imgSize[0] <= 0) {
            $imgSize[0] = 500;
        }
        if (!isset($imgSize[1]) || $imgSize[1] <= 0) {
            $imgSize[1] = $imgSize[0];
        }
        // Almacenamos la imágen original
        if ($mc['saveoriginal']) {
            copy($fullpath, $mc['storedir'] . '/originals/' . $filename);
        }
        // Redimensionamos la imagen
        $redim = new RMImageResizer($fullpath, $fullpath);
        switch ($mc['redim_image']) {
            case 0:
                //Recortar miniatura
                $redim->resizeWidth($imgSize[0]);
                $redim->setTargetFile($folderths . "/{$filename}");
                $redim->resizeAndCrop($thSize[0], $thSize[1]);
                break;
            case 1:
                //Recortar imagen grande
                $redim->resizeWidthOrHeight($imgSize[0], $imgSize[1]);
                $redim->setTargetFile($folderths . "/{$filename}");
                $redim->resizeWidth($thSize[0]);
                break;
            case 2:
                //Recortar ambas
                $redim->resizeWidthOrHeight($imgSize[0], $imgSize[1]);
                $redim->setTargetFile($folderths . "/{$filename}");
                $redim->resizeAndCrop($thSize[0], $thSize[1]);
                break;
            case 3:
                //Redimensionar
                $redim->resizeWidth($imgSize[0]);
                $redim->setTargetFile($folderths . "/{$filename}");
                $redim->resizeWidth($thSize[0]);
                break;
        }
    } else {
        echo $up->getErrors();
    }
    //Fin de Imagen
    $img->setImage($filename);
    $db = Database::getInstance();
    $new = $img->isNew();
    if (!$img->save()) {
        redirectMsg('./images.php?op=' . ($edit ? 'edit&id=' . $id : 'new') . '&' . $ruta, __('Errors ocurred while trying to save image.', 'galleries') . $img->errors(), 1);
        die;
    } else {
        $user->addPic();
        $img->setTags($tgs);
        $sets = '';
        $tbl1 = $db->prefix("gs_sets");
        $tbl2 = $db->prefix("gs_setsimages");
        foreach ($albums as $k => $v) {
            // Si el album existe no incrementamos el numero de imagenes
            $sets .= $sets == '' ? "{$tbl2}.id_set<>'{$v}'" : " AND {$tbl2}.id_set<>'{$v}'";
            if ($img->inSet($v)) {
                continue;
            }
            $album = new GSSet($v);
            $album->addPic($img->id());
        }
        // Actualizamos los valores de los ignorados
        $sql = "UPDATE {$tbl1}, {$tbl2} SET {$tbl1}.pics={$tbl1}.pics-1 WHERE ({$tbl2}.id_image='" . $img->id() . "'" . ($sets != '' ? ' AND ' . $sets : '') . ") AND {$tbl1}.id_set={$tbl2}.id_set";
        $db->queryF($sql);
        $sets = str_replace($tbl2 . '.', '', $sets);
        $sql = "DELETE FROM " . $db->prefix("gs_setsimages") . " WHERE id_image='" . $img->id() . "' " . ($sets != '' ? " AND ({$sets})" : '');
        $db->queryF($sql);
        redirectMsg('./images.php?' . $ruta, __('Database updated successfully!', 'galleries'), 0);
        die;
    }
}
示例#2
0
/**
* @desc Almacena la asignación de albumes a las imágenes
**/
function saveSets()
{
    global $db, $xoopsUser, $xoopsConfig;
    foreach ($_POST as $k => $v) {
        ${$k} = $v;
    }
    $errors = '';
    foreach ($ids as $k) {
        //Verificamos si la imagen es válida
        if ($k <= 0) {
            $errors .= sprintf(__('Picture with id %u is not valid!', 'galleries'), $k);
            continue;
        }
        //Verificamos si la imagen existe
        $img = new GSImage($k);
        if ($img->isNew()) {
            $errors .= sprintf(__('Picture with id %u does not exists!', 'galleries'), $k);
            continue;
        }
        if ($img->owner() != $xoopsUser->uid()) {
            $errors = sprintf(__('You don\'t have authorization!', 'galleries'), $k);
            continue;
        }
        $db = XoopsDatabaseFactory::getDatabaseConnection();
        $sets = '';
        $tbl1 = $db->prefix("gs_sets");
        $tbl2 = $db->prefix("gs_setsimages");
        foreach ($albums as $k => $v) {
            // Si el album existe no incrementamos el numero de imagenes
            if ($img->inSet($v)) {
                continue;
            }
            $album = new GSSet($v);
            $album->addPic($img->id());
        }
        // Actualizamos los valores de los ignorados
        $sql = "UPDATE {$tbl1}, {$tbl2} SET {$tbl1}.pics={$tbl1}.pics-1 WHERE ({$tbl2}.id_image='" . $img->id() . "'" . ($sets != '' ? ' AND ' . $sets : '') . ") AND {$tbl1}.id_set={$tbl2}.id_set";
        $db->queryF($sql);
    }
    if (!$referer) {
        $referer = GSFunctions::get_url();
    }
    if ($errors != '') {
        redirect_header($referer, 2, __('Errors ocurred while trying to update picture!', 'galleries') . $errors);
        die;
    } else {
        redirect_header($referer, 2, __('Pictures updated successfully!', 'galleries'));
        die;
    }
}