[SPIP][PLUGINS] v3.0-->v3.2
[lhc/web/www.git] / www / ecrire / action / editer_logo.php
1 <?php
2
3 /***************************************************************************\
4 * SPIP, Systeme de publication pour l'internet *
5 * *
6 * Copyright (c) 2001-2017 *
7 * Arnaud Martin, Antoine Pitrou, Philippe Riviere, Emmanuel Saint-James *
8 * *
9 * Ce programme est un logiciel libre distribue sous licence GNU/GPL. *
10 * Pour plus de details voir le fichier COPYING.txt ou l'aide en ligne. *
11 \***************************************************************************/
12
13 /**
14 * Gestion de l'API de modification/suppression des logos
15 *
16 * @package SPIP\Core\Logo\Edition
17 */
18
19 if (!defined('_ECRIRE_INC_VERSION')) {
20 return;
21 }
22
23
24 /**
25 * Supprimer le logo d'un objet
26 *
27 * @param string $objet
28 * @param int $id_objet
29 * @param string $etat
30 * `on` ou `off`
31 */
32 function logo_supprimer($objet, $id_objet, $etat) {
33 $chercher_logo = charger_fonction('chercher_logo', 'inc');
34 $objet = objet_type($objet);
35 $primary = id_table_objet($objet);
36 include_spip('inc/chercher_logo');
37
38 // existe-t-il deja un logo ?
39 $logo = $chercher_logo($id_objet, $primary, $etat);
40 if ($logo) {
41 spip_unlink($logo[0]);
42 }
43 }
44
45 /**
46 * Modifier le logo d'un objet
47 *
48 * @param string $objet
49 * @param int $id_objet
50 * @param string $etat
51 * `on` ou `off`
52 * @param string|array $source
53 * - array : sous tableau de `$_FILE` issu de l'upload
54 * - string : fichier source (chemin complet ou chemin relatif a `tmp/upload`)
55 * @return string
56 * Erreur, sinon ''
57 */
58 function logo_modifier($objet, $id_objet, $etat, $source) {
59 $chercher_logo = charger_fonction('chercher_logo', 'inc');
60 $objet = objet_type($objet);
61 $primary = id_table_objet($objet);
62 include_spip('inc/chercher_logo');
63 $type = type_du_logo($primary);
64
65 // nom du logo
66 $nom = $type . $etat . $id_objet;
67
68 // supprimer le logo eventueel existant
69 logo_supprimer($objet, $id_objet, $etat);
70
71 include_spip('inc/documents');
72 $erreur = '';
73
74 if (!$source) {
75 spip_log('spip_image_ajouter : source inconnue');
76 $erreur = 'source inconnue';
77
78 return $erreur;
79 }
80
81 $file_tmp = _DIR_LOGOS . $nom . '.tmp';
82
83 $ok = false;
84 // fichier dans upload/
85 if (is_string($source)) {
86 if (file_exists($source)) {
87 $ok = @copy($source, $file_tmp);
88 } elseif (file_exists($f = determine_upload() . $source)) {
89 $ok = @copy($f, $file_tmp);
90 }
91 } elseif (!$erreur = check_upload_error($source['error'], '', true)) {
92 // Intercepter une erreur a l'envoi
93 // analyse le type de l'image (on ne fait pas confiance au nom de
94 // fichier envoye par le browser : pour les Macs c'est plus sur)
95 $ok = deplacer_fichier_upload($source['tmp_name'], $file_tmp);
96 }
97
98 if ($erreur) {
99 return $erreur;
100 }
101 if (!$ok or !file_exists($file_tmp)) {
102 spip_log($erreur = "probleme de copie pour $file_tmp ");
103
104 return $erreur;
105 }
106
107 $size = @getimagesize($file_tmp);
108 $extension = !$size ? '' : ($size[2] > 3 ? '' : $GLOBALS['formats_logos'][$size[2] - 1]);
109 if ($extension) {
110 @rename($file_tmp, $file_tmp . ".$extension");
111 $file_tmp = $file_tmp . ".$extension";
112 $poids = filesize($file_tmp);
113
114 if (defined('_LOGO_MAX_WIDTH') or defined('_LOGO_MAX_HEIGHT')) {
115 if ((defined('_LOGO_MAX_WIDTH') and _LOGO_MAX_WIDTH and $size[0] > _LOGO_MAX_WIDTH)
116 or (defined('_LOGO_MAX_HEIGHT') and _LOGO_MAX_HEIGHT and $size[1] > _LOGO_MAX_HEIGHT)
117 ) {
118 $max_width = (defined('_LOGO_MAX_WIDTH') and _LOGO_MAX_WIDTH) ? _LOGO_MAX_WIDTH : '*';
119 $max_height = (defined('_LOGO_MAX_HEIGHT') and _LOGO_MAX_HEIGHT) ? _LOGO_MAX_HEIGHT : '*';
120
121 // pas la peine d'embeter le redacteur avec ca si on a active le calcul des miniatures
122 // on met directement a la taille maxi a la volee
123 if (isset($GLOBALS['meta']['creer_preview']) and $GLOBALS['meta']['creer_preview'] == 'oui') {
124 include_spip('inc/filtres');
125 $img = filtrer('image_reduire', $file_tmp, $max_width, $max_height);
126 $img = extraire_attribut($img, 'src');
127 $img = supprimer_timestamp($img);
128 if (@file_exists($img) and $img !== $file_tmp) {
129 spip_unlink($file_tmp);
130 @rename($img, $file_tmp);
131 $size = @getimagesize($file_tmp);
132 }
133 }
134 // verifier au cas ou image_reduire a echoue
135 if ((defined('_LOGO_MAX_WIDTH') and _LOGO_MAX_WIDTH and $size[0] > _LOGO_MAX_WIDTH)
136 or (defined('_LOGO_MAX_HEIGHT') and _LOGO_MAX_HEIGHT and $size[1] > _LOGO_MAX_HEIGHT)
137 ) {
138 spip_unlink($file_tmp);
139 $erreur = _T(
140 'info_logo_max_poids',
141 array(
142 'maxi' =>
143 _T(
144 'info_largeur_vignette',
145 array(
146 'largeur_vignette' => $max_width,
147 'hauteur_vignette' => $max_height
148 )
149 ),
150 'actuel' =>
151 _T(
152 'info_largeur_vignette',
153 array(
154 'largeur_vignette' => $size[0],
155 'hauteur_vignette' => $size[1]
156 )
157 )
158 )
159 );
160 }
161 }
162 }
163
164 if (!$erreur and defined('_LOGO_MAX_SIZE') and _LOGO_MAX_SIZE and $poids > _LOGO_MAX_SIZE * 1024) {
165 spip_unlink($file_tmp);
166 $erreur = _T(
167 'info_logo_max_poids',
168 array(
169 'maxi' => taille_en_octets(_LOGO_MAX_SIZE * 1024),
170 'actuel' => taille_en_octets($poids)
171 )
172 );
173 }
174
175 if (!$erreur) {
176 @rename($file_tmp, _DIR_LOGOS . $nom . ".$extension");
177 }
178 } else {
179 spip_unlink($file_tmp);
180 $erreur = _T(
181 'info_logo_format_interdit',
182 array('formats' => join(', ', $GLOBALS['formats_logos']))
183 );
184 }
185
186 return $erreur;
187 }