Completed
Push — master ( 36e791...c454b4 )
by cam
42:37 queued 38:13
created

email_valide.php ➔ inc_email_valide_dist()   A

Complexity

Conditions 5
Paths 5

Size

Total Lines 25

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
nc 5
nop 1
dl 0
loc 25
rs 9.2088
c 0
b 0
f 0
1
<?php
2
3
/***************************************************************************\
4
 *  SPIP, Système de publication pour l'internet                           *
5
 *                                                                         *
6
 *  Copyright © avec tendresse depuis 2001                                 *
7
 *  Arnaud Martin, Antoine Pitrou, Philippe Rivière, Emmanuel Saint-James  *
8
 *                                                                         *
9
 *  Ce programme est un logiciel libre distribué sous licence GNU/GPL.     *
10
 *  Pour plus de détails voir le fichier COPYING.txt ou l'aide en ligne.   *
11
\***************************************************************************/
12
13
/**
14
 * Gestion des emails et de leur envoi
15
 *
16
 * @package SPIP\Core\Mail
17
 **/
18
if (!defined('_ECRIRE_INC_VERSION')) {
19
	return;
20
}
21
22
23
24
/**
25
 * Vérifier la conformité d'une ou plusieurs adresses email (suivant RFC 822)
26
 *
27
 * @param string $adresses
28
 *      Adresse ou liste d'adresse (separees pas des virgules)
29
 * @return bool|string
0 ignored issues
show
Documentation introduced by
Consider making the return type a bit more specific; maybe use false|string.

This check looks for the generic type array as a return type and suggests a more specific type. This type is inferred from the actual code.

Loading history...
30
 *      - false si une des adresses n'est pas conforme,
31
 *      - la normalisation de la dernière adresse donnée sinon
32
 **/
33
function inc_email_valide_dist($adresses) {
34
	// eviter d'injecter n'importe quoi dans preg_match
35
	if (!is_string($adresses)) {
36
		return false;
37
	}
38
39
	// Si c'est un spammeur autant arreter tout de suite
40
	if (preg_match(",[\n\r].*(MIME|multipart|Content-),i", $adresses)) {
41
		spip_log("Tentative d'injection de mail : $adresses");
42
43
		return false;
44
	}
45
46
	foreach (explode(',', $adresses) as $v) {
47
		// nettoyer certains formats
48
		// "Marie Toto <[email protected]>"
49
		$adresse = trim(preg_replace(",^[^<>\"]*<([^<>\"]+)>$,i", "\\1", $v));
50
		// RFC 822
51
		if (!preg_match('#^[^()<>@,;:\\"/[:space:]]+(@([-_0-9a-z]+\.)*[-_0-9a-z]+)$#i', $adresse)) {
52
			return false;
53
		}
54
	}
55
56
	return $adresse;
0 ignored issues
show
Bug introduced by
The variable $adresse 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...
57
}