Completed
Push — master ( 565410...3576c2 )
by cam
04:33
created

editer_logo.php ➔ logo_supprimer()   B

Complexity

Conditions 7
Paths 5

Size

Total Lines 30

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 7
nc 5
nop 3
dl 0
loc 30
rs 8.5066
c 0
b 0
f 0
1
<?php
2
3
/***************************************************************************\
4
 *  SPIP, Systeme de publication pour l'internet                           *
5
 *                                                                         *
6
 *  Copyright (c) 2001-2019                                                *
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
		# TODO : deprecated, a supprimer -> anciens logos IMG/artonxx.png pas en base
42
		if (count($logo) < 6) {
43
			spip_unlink($logo[0]);
44
		}
45
		elseif ($doc = $logo[5]
46
			and isset($doc['id_document'])
47
		  and $id_document = $doc['id_document']) {
48
49
			include_spip('action/editer_liens');
50
			// supprimer le lien dans la base
51
			objet_dissocier(array('document' => $id_document), array($objet => $id_objet), array('role' => '*'));
52
53
			// verifier si il reste des liens avec d'autres objets et sinon supprimer
54
			$liens = objet_trouver_liens(array('document' => $id_document), '*');
55
			if (!count($liens)) {
56
				$supprimer_document = charger_fonction('supprimer_document', 'action');
57
				$supprimer_document($doc['id_document']);
58
			}
59
		}
60
	}
61
}
62
63
/**
64
 * Modifier le logo d'un objet
65
 *
66
 * @param string $objet
67
 * @param int $id_objet
68
 * @param string $etat
69
 *     `on` ou `off`
70
 * @param string|array $source
71
 *     - array : sous tableau de `$_FILE` issu de l'upload
72
 *     - string : fichier source (chemin complet ou chemin relatif a `tmp/upload`)
73
 * @return string
0 ignored issues
show
Documentation introduced by
Should the return type not be string|boolean|object|array? Also, consider making the array more specific, something like array<String>, or String[].

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

If the return type contains the type array, this check recommends the use of a more specific type like String[] or array<String>.

Loading history...
74
 *     Erreur, sinon ''
75
 */
76
function logo_modifier($objet, $id_objet, $etat, $source) {
77
	$chercher_logo = charger_fonction('chercher_logo', 'inc');
0 ignored issues
show
Unused Code introduced by
$chercher_logo is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
78
	$objet = objet_type($objet);
79
	$primary = id_table_objet($objet);
0 ignored issues
show
Unused Code introduced by
$primary is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
80
	include_spip('inc/chercher_logo');
81
82
	$mode = preg_replace(",\W,", '', $etat);
83
	if (!$mode){
84
		spip_log("logo_modifier : etat $etat invalide", 'logo');
85
		$erreur = 'etat invalide';
86
87
		return $erreur;
88
	}
89
	// chercher dans la base
90
	$mode_document = 'logo' . $mode;
91
92
	// supprimer le logo eventueel existant
93
	// TODO : si un logo existe, le modifier plutot que supprimer + reinserer (mais il faut gerer le cas ou il est utilise par plusieurs objets, donc pas si simple)
94
	// mais de toute facon l'interface actuelle oblige a supprimer + reinserer
95
	logo_supprimer($objet, $id_objet, $etat);
96
97
98
	include_spip('inc/documents');
99
	$erreur = '';
0 ignored issues
show
Unused Code introduced by
$erreur is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
100
101
	if (!$source) {
102
		spip_log('spip_image_ajouter : source inconnue', 'logo');
103
		$erreur = 'source inconnue';
104
105
		return $erreur;
106
	}
107
108
	// fichier dans upload/
109
	if (is_string($source)) {
110
		$tmp_name = false;
111
		if (file_exists($source)) {
112
			$tmp_name = $source;
113
		} elseif (file_exists($f = determine_upload() . $source)) {
114
			$tmp_name = $f;
115
		}
116
		if (!$tmp_name) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $tmp_name of type string|false is loosely compared to false; this is ambiguous if the string can be empty. You might want to explicitly use === false instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
117
			spip_log('spip_image_ajouter : source inconnue', 'logo');
118
			$erreur = 'source inconnue';
119
120
			return $erreur;
121
		}
122
		$source = array(
123
			'tmp_name' => $tmp_name,
124
			'name' => basename($tmp_name),
125
		);
126
	} elseif ($erreur = check_upload_error($source['error'], '', true)) {
127
		return $erreur;
128
	}
129
130
	$source['mode'] = $mode_document;
131
	$ajouter_documents = charger_fonction('ajouter_documents', 'action');
132
	$ajoutes = $ajouter_documents('new', [$source], $objet, $id_objet, $mode_document);
133
134
	$id_document = reset($ajoutes);
135
136
	if (!is_numeric($id_document)) {
137
		$erreur = ($id_document ? $id_document : 'Erreur inconnue');
138
		spip_log("Erreur ajout logo : $erreur pour source=".json_encode($source), 'logo');
139
		return $erreur;
140
	}
141
142
	return ''; // tout est bon, pas d'erreur
143
144
}
145
146
147
/**
148
 * Convertit le type numerique retourne par getimagesize() en extension fichier
149
 * doublon de la fonction presente dans metadata/image du plugin medias
150
 * a fusionner avec les logos en documents
151
 *
152
 * @param int $type
153
 * @param bool $strict
154
 * @return string
155
 */
156
// https://code.spip.net/@decoder_type_image
157
function logo_decoder_type_image($type, $strict = false) {
158
	switch ($type) {
159
		case IMAGETYPE_GIF:
160
			return 'gif';
161
		case IMAGETYPE_JPEG:
162
			return 'jpg';
163
		case IMAGETYPE_PNG:
164
			return 'png';
165
		case IMAGETYPE_SWF:
166
			return $strict ? '' : 'swf';
167
		case IMAGETYPE_PSD:
168
			return 'psd';
169
		case IMAGETYPE_BMP:
170
			return 'bmp';
171
		case IMAGETYPE_TIFF_II:
172
		case IMAGETYPE_TIFF_MM:
173
			return 'tif';
174
		case IMAGETYPE_WEBP:
175
			return 'webp';
176
		case IMAGETYPE_SVG:
177
			return $strict ? '' : 'svg';
178
		default:
179
			return '';
180
	}
181
}
182