Completed
Pull Request — master (#1407)
by
unknown
17:24
created

MozillaIspDb::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 3
rs 10
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
/**
3
 * @author Christoph Wurst <[email protected]>
4
 *
5
 * ownCloud - Mail
6
 *
7
 * This code is free software: you can redistribute it and/or modify
8
 * it under the terms of the GNU Affero General Public License, version 3,
9
 * as published by the Free Software Foundation.
10
 *
11
 * This program is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
 * GNU Affero General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU Affero General Public License, version 3,
17
 * along with this program.  If not, see <http://www.gnu.org/licenses/>
18
 *
19
 */
20
namespace OCA\Mail\Service\AutoConfig;
21
22
use OCA\Mail\Service\Logger;
23
24
class MozillaIspDb {
25
26
	private $logger;
27
	private $urls = array(
28
		'https://autoconfig.{DOMAIN}/mail/config-v1.1.xml',
29
		'https://{DOMAIN}/.well-known/autoconfig/mail/config-v1.1.xml',
30
		'https://autoconfig.thunderbird.net/v1.1/{DOMAIN}',
31
		// fallback for self-signed certificates (needed?)
32
		'http://autoconfig.{DOMAIN}/mail/config-v1.1.xml',
33
		'http://{DOMAIN}/.well-known/autoconfig/mail/config-v1.1.xml',
34
	);
35
36
	public function __construct(Logger $logger) {
37
		$this->logger = $logger;
38
	}
39
40
	private function queryUrl($url) {
41
		$provider = [];
0 ignored issues
show
Unused Code introduced by
$provider 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...
42
		try {
43
			$xml = @simplexml_load_file($url);
44
			if (!is_object($xml) || !$xml->emailProvider) {
45
				return [];
46
			}
47
			$provider = [
48
				'displayName' => (string) $xml->emailProvider->displayName,
49
			];
50
			foreach ($xml->emailProvider->children() as $tag => $server) {
51
				if (!in_array($tag, ['incomingServer', 'outgoingServer'])) {
52
					continue;
53
				}
54
				foreach ($server->attributes() as $name => $value) {
55
					if ($name == 'type') {
56
						$type = (string) $value;
57
					}
58
				}
59
				$data = [];
60
				foreach ($server as $name => $value) {
61
					foreach ($value->children() as $tag => $val) {
62
						$data[$name][$tag] = (string) $val;
63
					}
64
					if (!isset($data[$name])) {
65
						$data[$name] = (string) $value;
66
					}
67
				}
68
				$provider[$type][] = $data;
0 ignored issues
show
Bug introduced by
The variable $type 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...
69
			}
70
		} catch (Exception $e) {
0 ignored issues
show
Bug introduced by
The class OCA\Mail\Service\AutoConfig\Exception does not exist. Did you forget a USE statement, or did you not list all dependencies?

Scrutinizer analyzes your composer.json/composer.lock file if available to determine the classes, and functions that are defined by your dependencies.

It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.

Loading history...
71
			// ignore own not-found exception or xml parsing exceptions
72
			unset($e);
73
			$provider = [];
74
		}
75
		return $provider;
76
	}
77
78
	/**
79
	 * @param string $domain
80
	 * @return array
81
	 */
82
	public function query($domain, $tryMx = true) {
83
		$this->logger->debug("MozillaIsbDb: querying <$domain>");
84
		if (strpos($domain, '@') !== false) {
85
			// TODO: use horde mail address parsing instead
86
			list(, $domain) = explode('@', $domain);
87
		}
88
89
		foreach($this->urls as $url) {
90
			$url = str_replace("{DOMAIN}", $domain, $url);
91
			$this->logger->debug("MozillaIsbDb: querying <$domain> via <$url>");
92
93
			$provider = $this->queryUrl($url);
94
			if (!empty($provider)) {
95
				return $provider;
96
			}
97
		}
98
99
		if ($tryMx && ($dns = dns_get_record($domain, DNS_MX))) {
100
			$domain = $dns[0]['target'];
101
			if (!($provider = $this->query($domain, false))) {
102
				list(, $domain) = explode('.', $domain, 2);
103
				$provider = $this->query($domain, false);
104
			}
105
		}
106
		return $provider;
0 ignored issues
show
Bug introduced by
The variable $provider 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...
107
	}
108
109
}
110