Beispiel #1
0
/**
 * @param string $objet
 * @param int $id_objet
 * @param string $etat
 *   on ou off
 * @param string|array $source
 *   array : sous tableau de $_FILE issu de l'upload
 *   string : fichier source (chemin complet ou chemin relatif a tmp/upload)
 * @return string
 */
function logo_modifier($objet, $id_objet, $etat, $source)
{
    $chercher_logo = charger_fonction('chercher_logo', 'inc');
    $objet = objet_type($objet);
    $primary = id_table_objet($objet);
    include_spip('inc/chercher_logo');
    $type = type_du_logo($primary);
    // nom du logo
    $nom = $type . $etat . $id_objet;
    // supprimer le logo eventueel existant
    logo_supprimer($objet, $id_objet, $etat);
    include_spip('inc/documents');
    $erreur = "";
    if (!$source) {
        spip_log("spip_image_ajouter : source inconnue");
        $erreur = "source inconnue";
        return $erreur;
    }
    $file_tmp = _DIR_LOGOS . $nom . '.tmp';
    $ok = false;
    // fichier dans upload/
    if (is_string($source)) {
        if (file_exists($source)) {
            $ok = @copy($source, $file_tmp);
        } elseif (file_exists($f = determine_upload() . $source)) {
            $ok = @copy($f, $file_tmp);
        }
    } elseif (!($erreur = check_upload_error($source['error'], "", true))) {
        // analyse le type de l'image (on ne fait pas confiance au nom de
        // fichier envoye par le browser : pour les Macs c'est plus sur)
        $ok = deplacer_fichier_upload($source['tmp_name'], $file_tmp);
    }
    if ($erreur) {
        return $erreur;
    }
    if (!$ok or !file_exists($file_tmp)) {
        spip_log($erreur = "probleme de copie pour {$file_tmp} ");
        return $erreur;
    }
    $size = @getimagesize($file_tmp);
    $type = !$size ? '' : ($size[2] > 3 ? '' : $GLOBALS['formats_logos'][$size[2] - 1]);
    if ($type) {
        @rename($file_tmp, $file_tmp . ".{$type}");
        $file_tmp = $file_tmp . ".{$type}";
        $poids = filesize($file_tmp);
        if (defined('_LOGO_MAX_WIDTH') or defined('_LOGO_MAX_HEIGHT')) {
            if (defined('_LOGO_MAX_WIDTH') and _LOGO_MAX_WIDTH and $size[0] > _LOGO_MAX_WIDTH or defined('_LOGO_MAX_HEIGHT') and _LOGO_MAX_HEIGHT and $size[1] > _LOGO_MAX_HEIGHT) {
                $max_width = (defined('_LOGO_MAX_WIDTH') and _LOGO_MAX_WIDTH) ? _LOGO_MAX_WIDTH : '*';
                $max_height = (defined('_LOGO_MAX_HEIGHT') and _LOGO_MAX_HEIGHT) ? _LOGO_MAX_HEIGHT : '*';
                // pas la peine d'embeter le redacteur avec ca si on a active le calcul des miniatures
                // on met directement a la taille maxi a la volee
                if (isset($GLOBALS['meta']['creer_preview']) and $GLOBALS['meta']['creer_preview'] == 'oui') {
                    include_spip('inc/filtres');
                    $img = filtrer('image_reduire', $file_tmp, $max_width, $max_height);
                    $img = extraire_attribut($img, 'src');
                    $img = supprimer_timestamp($img);
                    if (@file_exists($img) and $img !== $file_tmp) {
                        spip_unlink($file_tmp);
                        @rename($img, $file_tmp);
                        $size = @getimagesize($file_tmp);
                    }
                }
                // verifier au cas ou image_reduire a echoue
                if (defined('_LOGO_MAX_WIDTH') and _LOGO_MAX_WIDTH and $size[0] > _LOGO_MAX_WIDTH or defined('_LOGO_MAX_HEIGHT') and _LOGO_MAX_HEIGHT and $size[1] > _LOGO_MAX_HEIGHT) {
                    spip_unlink($file_tmp);
                    $erreur = _T('info_logo_max_poids', array('maxi' => _T('info_largeur_vignette', array('largeur_vignette' => $max_width, 'hauteur_vignette' => $max_height)), 'actuel' => _T('info_largeur_vignette', array('largeur_vignette' => $size[0], 'hauteur_vignette' => $size[1]))));
                }
            }
        }
        if (!$erreur and defined('_LOGO_MAX_SIZE') and _LOGO_MAX_SIZE and $poids > _LOGO_MAX_SIZE * 1024) {
            spip_unlink($file_tmp);
            $erreur = _T('info_logo_max_poids', array('maxi' => taille_en_octets(_LOGO_MAX_SIZE * 1024), 'actuel' => taille_en_octets($poids)));
        }
        if (!$erreur) {
            @rename($file_tmp, _DIR_LOGOS . $nom . ".{$type}");
        }
    } else {
        spip_unlink($file_tmp);
        $erreur = _T('info_logo_format_interdit', array('formats' => join(', ', $GLOBALS['formats_logos'])));
    }
    return $erreur;
}
/**
 * Traitement de l'upload d'un logo
 *
 * Il est affecte au site si la balise n'est pas dans une boucle,
 * sinon a l'objet concerne par la boucle ou indiquee par les parametres d'appel
 *
 * @param string $objet Objet SPIP auquel sera lie le document (ex. article)
 * @param int $id_objet Identifiant de l'objet
 * @param string $retour Url de redirection apres traitement
 * @param array $options Tableau d'option (exemple : image_reduire => 50)
 * @return array               Retour des traitements
 */
function formulaires_editer_logo_traiter_dist($objet, $id_objet, $retour = '', $options = array())
{
    $res = array('editable' => ' ');
    // pas dans une boucle ? formulaire pour le logo du site
    // dans ce cas, il faut chercher un 'siteon0.ext'
    if (!$objet) {
        $objet = 'site';
    }
    include_spip("action/editer_logo");
    // effectuer la suppression si demandee d'un logo
    $on = _request('supprimer_logo_on');
    if ($on or _request('supprimer_logo_off')) {
        logo_supprimer($objet, $id_objet, $on ? 'on' : 'off');
        $res['message_ok'] = '';
        // pas besoin de message : la validation est visuelle
        set_request('logo_up', ' ');
    } else {
        $sources = formulaire_editer_logo_get_sources();
        foreach ($sources as $etat => $file) {
            if ($file and $file['error'] == 0) {
                if ($err = logo_modifier($objet, $id_objet, $etat, $file)) {
                    $res['message_erreur'] = $err;
                } else {
                    $res['message_ok'] = '';
                }
                // pas besoin de message : la validation est visuelle
                set_request('logo_up', ' ');
            }
        }
    }
    // Invalider les caches de l'objet
    include_spip('inc/invalideur');
    suivre_invalideur("id='{$objet}/{$id_objet}'");
    if ($retour) {
        $res['redirect'] = $retour;
    }
    return $res;
}
Beispiel #3
0
function logo_revision($id, $file, $type, $ref)
{
    $chercher_logo = charger_fonction('chercher_logo', 'inc');
    $_id_objet = id_table_objet($type);
    // Chargement d'un nouveau logo ?
    if ($file['logo']) {
        define('FILE_UPLOAD', true);
        // message pour crayons_json_export :(
        if (include_spip("action/editer_logo") and function_exists("logo_modifier")) {
            logo_modifier($type, $id, "on", $file['logo']);
        } else {
            // supprimer l'ancien logo
            $on = $chercher_logo($id, $_id_objet, 'on');
            if ($on) {
                @unlink($on[0]);
            }
            // ajouter le nouveau
            include_spip('action/iconifier');
            action_spip_image_ajouter_dist(type_du_logo($_id_objet) . 'on' . $id, false, false);
            // beurk
        }
    } else {
        // Suppression du logo ?
        if ($wid = array_pop($ref) and $_POST['content_' . $wid . '_logo_supprimer'] == 'on') {
            if (include_spip("action/editer_logo") and function_exists("logo_supprimer")) {
                logo_supprimer($type, $id, "on");
            } else {
                if ($on = $chercher_logo($id, $_id_objet, 'on')) {
                    @unlink($on[0]);
                }
            }
        }
    }
    // Reduire le logo ?
    if (is_array($cfg = @unserialize($GLOBALS['meta']['crayons'])) and $max = intval($cfg['reduire_logo'])) {
        $on = $chercher_logo($id, $_id_objet, 'on');
        include_spip('inc/filtres');
        @copy($on[0], $temp = _DIR_VAR . 'tmp' . rand(0, 999) . '.' . $on[3]);
        $img1 = filtrer('image_reduire', $temp, $max);
        $img2 = preg_replace(',[?].*,', '', extraire_attribut($img1, 'src'));
        if (@file_exists($img2) and $img2 != $temp) {
            if (include_spip("action/editer_logo") and function_exists("logo_modifier")) {
                logo_modifier($type, $id, "on", $img2);
            } else {
                @unlink($on[0]);
                $dest = $on[1] . $on[2] . '.' . preg_replace(',^.*\\.(gif|jpg|png)$,', '\\1', $img2);
                @rename($img2, $dest);
            }
        }
        @unlink($temp);
    }
    return true;
}