Completed
Push — master ( 4d4ad3...59ae2c )
by Christoph
19:02
created

IspDb   A

Complexity

Total Complexity 21

Size/Duplication

Total Lines 93
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 77.78%

Importance

Changes 4
Bugs 1 Features 0
Metric Value
wmc 21
c 4
b 1
f 0
lcom 1
cbo 1
dl 0
loc 93
ccs 42
cts 54
cp 0.7778
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getUrls() 0 7 1
A __construct() 0 3 1
C queryUrl() 0 37 12
C query() 0 27 7
1
<?php
2
3
/**
4
 * @author Christoph Wurst <[email protected]>
5
 *
6
 * ownCloud - Mail
7
 *
8
 * This code is free software: you can redistribute it and/or modify
9
 * it under the terms of the GNU Affero General Public License, version 3,
10
 * as published by the Free Software Foundation.
11
 *
12
 * This program is distributed in the hope that it will be useful,
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
 * GNU Affero General Public License for more details.
16
 *
17
 * You should have received a copy of the GNU Affero General Public License, version 3,
18
 * along with this program.  If not, see <http://www.gnu.org/licenses/>
19
 *
20
 */
21
22
namespace OCA\Mail\Service\AutoConfig;
23
24
use Exception;
25
use OCA\Mail\Service\Logger;
26
27
class IspDb {
28
29
	/** @var Logger */
30
	private $logger;
31
32
	/** @var string[] */
33 3
	public function getUrls() {
34 3
		return [
35 3
			'https://autoconfig.{DOMAIN}/mail/config-v1.1.xml',
36
			'https://{DOMAIN}/.well-known/autoconfig/mail/config-v1.1.xml',
37 3
			'https://autoconfig.thunderbird.net/v1.1/{DOMAIN}',
38
		];
39 3
	}
40 3
41 3
	/**
42 3
	 * @param Logger $logger
43
	 * @param string[] $ispUrls
0 ignored issues
show
Bug introduced by
There is no parameter named $ispUrls. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
44
	 */
45 3
	public function __construct(Logger $logger) {
46 3
		$this->logger = $logger;
47 3
	}
48 3
49 3
	private function queryUrl($url) {
50
		try {
51 3
			$xml = @simplexml_load_file($url);
52 3
			if (libxml_get_last_error() !== False || !is_object($xml) || !$xml->emailProvider) {
53 3
				libxml_clear_errors();
54 3
				return [];
55 3
			}
56 3
			$provider = [
57 3
				'displayName' => (string) $xml->emailProvider->displayName,
58 3
			];
59 2
			foreach ($xml->emailProvider->children() as $tag => $server) {
60 3
				if (!in_array($tag, ['incomingServer', 'outgoingServer'])) {
61 3
					continue;
62 3
				}
63 3
				foreach ($server->attributes() as $name => $value) {
64 3
					if ($name == 'type') {
65 3
						$type = (string) $value;
66 3
					}
67 3
				}
68
				$data = [];
69
				foreach ($server as $name => $value) {
70
					foreach ($value->children() as $tag => $val) {
71
						$data[$name][$tag] = (string) $val;
72 3
					}
73
					if (!isset($data[$name])) {
74
						$data[$name] = (string) $value;
75
					}
76
				}
77
				$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...
78
			}
79 3
		} catch (Exception $e) {
80 3
			// ignore own not-found exception or xml parsing exceptions
81 3
			unset($e);
82
			$provider = [];
83
		}
84
		return $provider;
85
	}
86 3
87 3
	/**
88 3
	 * @param string $domain
89 3
	 * @return array
90
	 */
91 3
	public function query($domain, $tryMx = true) {
92 3
		$this->logger->debug("IsbDb: querying <$domain>");
93 3
		if (strpos($domain, '@') !== false) {
94
			// TODO: use horde mail address parsing instead
95 3
			list(, $domain) = explode('@', $domain);
96
		}
97
98
		$provider = [];
99
		foreach ($this->getUrls() as $url) {
100
			$url = str_replace("{DOMAIN}", $domain, $url);
101
			$this->logger->debug("IsbDb: querying <$domain> via <$url>");
102
103
			$provider = $this->queryUrl($url);
104
			if (!empty($provider)) {
105
				return $provider;
106
			}
107
		}
108
109
		if ($tryMx && ($dns = dns_get_record($domain, DNS_MX))) {
110
			$domain = $dns[0]['target'];
111
			if (!($provider = $this->query($domain, false))) {
112
				list(, $domain) = explode('.', $domain, 2);
113
				$provider = $this->query($domain, false);
114
			}
115
		}
116
		return $provider;
117
	}
118
119
}
120