Completed
Push — spip-3.0 ( 5d8b58 )
by cam
53:01 queued 42:30
created

iconifier.php ➔ action_spip_image_ajouter_dist()   F

Complexity

Conditions 20
Paths 1785

Size

Total Lines 77
Code Lines 56

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 20
eloc 56
c 0
b 0
f 0
nc 1785
nop 4
dl 0
loc 77
rs 2.2913

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
/***************************************************************************\
4
 *  SPIP, Systeme de publication pour l'internet                           *
5
 *                                                                         *
6
 *  Copyright (c) 2001-2016                                                *
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
if (!defined('_ECRIRE_INC_VERSION')) return;
14
15
/**
16
 * L'entree par l'action ne sert plus qu'a une retro compat eventuelle
17
 * le #FORMULAIRE_EDITER_LOGO utilise action_spip_image_ajouter_dist
18
 */
19
function action_iconifier_dist()
20
{
21
	include_spip('inc/actions');
22
	$securiser_action = charger_fonction('securiser_action', 'inc');
23
	$arg = $securiser_action();
24
	$iframe_redirect = _request('iframe_redirect');
25
26
	$arg = rawurldecode($arg);
27
28
	if (!preg_match(',^-?\d*(\D)(.*)$,',$arg, $r))
29
		spip_log("action iconifier: $arg pas compris");
30
	elseif ($r[1] == '+')
31
		action_spip_image_ajouter_dist($r[2], _request('sousaction2'), _request('source'));
32
	else	action_spip_image_effacer_dist($r[2]);
33
	
34
	if(_request("iframe") == 'iframe') {
35
		$redirect = urldecode($iframe_redirect)."&iframe=iframe";
36
		redirige_par_entete(urldecode($redirect));
37
	}
38
}
39
40
// http://doc.spip.org/@action_spip_image_effacer_dist
41
function action_spip_image_effacer_dist($arg) {
42
43
	if (!strstr($arg, ".."))
44
		spip_unlink(_DIR_LOGOS . $arg);
45
}
46
47
//
48
// Ajouter un logo
49
//
50
51
// $source = $_FILES[0]
52
// $dest = arton12.xxx
53
// http://doc.spip.org/@action_spip_image_ajouter_dist
54
function action_spip_image_ajouter_dist($arg,$sousaction2,$source,$return=false) {
55
	global $formats_logos;
56
57
	include_spip('inc/documents');
58
	if (!$sousaction2) {
59
		if (!$_FILES) $_FILES = $GLOBALS['HTTP_POST_FILES'];
60
		$source = (is_array($_FILES) ? array_pop($_FILES) : "");
61
	}
62
	$erreur = "";
63
	if (!$source)
64
		spip_log("spip_image_ajouter : source inconnue");
65
	else {
66
		$f =_DIR_LOGOS . $arg . '.tmp';
67
68
		if (!is_array($source)) 
69
		// fichier dans upload/
70
	  		$source = @copy(determine_upload() . $source, $f);
71
		else {
72
		// Intercepter une erreur a l'envoi
73
			if ($erreur = check_upload_error($source['error'],"",$return))
74
				$source ="";
75
			else
76
		// analyse le type de l'image (on ne fait pas confiance au nom de
77
		// fichier envoye par le browser : pour les Macs c'est plus sur)
78
79
				$source = deplacer_fichier_upload($source['tmp_name'], $f);
80
		}
81
		if (!$source)
82
			spip_log("pb de copie pour $f");
83
	}
84
	if ($source AND $f) {
85
		$size = @getimagesize($f);
0 ignored issues
show
Bug introduced by
The variable $f does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
86
		$type = !$size ? '': ($size[2] > 3 ? '' : $formats_logos[$size[2]-1]);
87
		if ($type) {
88
			$poids = filesize($f);
89
90
			if (_LOGO_MAX_SIZE > 0
91
			AND $poids > _LOGO_MAX_SIZE*1024) {
92
				spip_unlink ($f);
93
				$erreur = _T('info_logo_max_poids',
94
									array('maxi' => taille_en_octets(_LOGO_MAX_SIZE*1024),
95
									'actuel' => taille_en_octets($poids)));
96
			}
97
98
			elseif (_LOGO_MAX_WIDTH * _LOGO_MAX_HEIGHT
99
			AND ($size[0] > _LOGO_MAX_WIDTH
100
			OR $size[1] > _LOGO_MAX_HEIGHT)) {
101
				spip_unlink ($f);
102
				$erreur = _T('info_logo_max_poids',
103
									array(
104
									'maxi' =>
105
										_T('info_largeur_vignette',
106
											array('largeur_vignette' => _LOGO_MAX_WIDTH,
107
											'hauteur_vignette' => _LOGO_MAX_HEIGHT)),
108
									'actuel' =>
109
										_T('info_largeur_vignette',
110
											array('largeur_vignette' => $size[0],
111
											'hauteur_vignette' => $size[1]))
112
									));
113
			}
114
			else
115
				@rename ($f, _DIR_LOGOS . $arg . ".$type");
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
116
		}
117
		else {
118
			spip_unlink ($f);
119
			$erreur = _T('info_logo_format_interdit',
120
									array('formats' => join(', ', $formats_logos)));
121
		}
122
	
123
	}
124
	if ($erreur){
125
		if ($return)
126
			return $erreur;
127
		else
128
			check_upload_error(6,$erreur);
129
	}
130
}
131
?>
0 ignored issues
show
Best Practice introduced by
It is not recommended to use PHP's closing tag ?> in files other than templates.

Using a closing tag in PHP files that only contain PHP code is not recommended as you might accidentally add whitespace after the closing tag which would then be output by PHP. This can cause severe problems, for example headers cannot be sent anymore.

A simple precaution is to leave off the closing tag as it is not required, and it also has no negative effects whatsoever.

Loading history...
132