IspDb::getUrls()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 7
ccs 5
cts 5
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 5
nc 1
nop 0
crap 1
1
<?php
2
3
/**
4
 * @author Bernhard Scheirle <[email protected]>
5
 * @author Christoph Wurst <[email protected]>
6
 * @author Scheirle <[email protected]>
7
 *
8
 * Mail
9
 *
10
 * This code is free software: you can redistribute it and/or modify
11
 * it under the terms of the GNU Affero General Public License, version 3,
12
 * as published by the Free Software Foundation.
13
 *
14
 * This program is distributed in the hope that it will be useful,
15
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17
 * GNU Affero General Public License for more details.
18
 *
19
 * You should have received a copy of the GNU Affero General Public License, version 3,
20
 * along with this program.  If not, see <http://www.gnu.org/licenses/>
21
 *
22
 */
23
24
namespace OCA\Mail\Service\AutoConfig;
25
26
use Exception;
27
use OCA\Mail\Service\Logger;
28
29
class IspDb {
30
31
	/** @var Logger */
32
	private $logger;
33
34
	/** @var string[] */
35 2
	public function getUrls() {
36
		return [
37 2
			'https://autoconfig.{DOMAIN}/mail/config-v1.1.xml',
38 2
			'https://{DOMAIN}/.well-known/autoconfig/mail/config-v1.1.xml',
39 2
			'https://autoconfig.thunderbird.net/v1.1/{DOMAIN}',
40 2
		];
41
	}
42
43
	/**
44
	 * @param Logger $logger
45
	 * @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...
46
	 */
47 3
	public function __construct(Logger $logger) {
48 3
		$this->logger = $logger;
49 3
	}
50
51 3
	private function queryUrl($url) {
52
		try {
53 3
			$content = @file_get_contents($url, false, stream_context_create([
54
				'http' => [
55
					'timeout' => 7
56 3
				]
57 3
			]));
58 3
			if ($content !== false) {
59 3
				$xml = @simplexml_load_string($content);
60 3
			} else {
61 2
				$this->logger->debug("IsbDb: <$url> request timed out");
62 2
				return [];
63
			}
64
65 3
			if (libxml_get_last_error() !== false || !is_object($xml) || !$xml->emailProvider) {
66
				libxml_clear_errors();
67
				return [];
68
			}
69
			$provider = [
70 3
				'displayName' => (string) $xml->emailProvider->displayName,
71 3
			];
72 3
			foreach ($xml->emailProvider->children() as $tag => $server) {
73 3
				if (!in_array($tag, ['incomingServer', 'outgoingServer'])) {
74 3
					continue;
75
				}
76 3
				foreach ($server->attributes() as $name => $value) {
77 3
					if ($name == 'type') {
78 3
						$type = (string) $value;
79 3
					}
80 3
				}
81 3
				$data = [];
82 3
				foreach ($server as $name => $value) {
83 3
					foreach ($value->children() as $tag => $val) {
84 2
						$data[$name][$tag] = (string) $val;
85 3
					}
86 3
					if (!isset($data[$name])) {
87 3
						$data[$name] = (string) $value;
88 3
					}
89 3
				}
90 3
				$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...
91 3
			}
92 3
		} catch (Exception $e) {
93
			// ignore own not-found exception or xml parsing exceptions
94
			unset($e);
95
			$provider = [];
96
		}
97 3
		return $provider;
98
	}
99
100
	/**
101
	 * @param string $domain
102
	 * @return array
103
	 */
104 3
	public function query($domain, $tryMx = true) {
105 3
		$this->logger->debug("IsbDb: querying <$domain>");
106 3
		if (strpos($domain, '@') !== false) {
107
			// TODO: use horde mail address parsing instead
108
			list(, $domain) = explode('@', $domain);
109
		}
110
111 3
		$provider = [];
112 3
		foreach ($this->getUrls() as $url) {
113 3
			$url = str_replace("{DOMAIN}", $domain, $url);
114 3
			$this->logger->debug("IsbDb: querying <$domain> via <$url>");
115
116 3
			$provider = $this->queryUrl($url);
117 3
			if (!empty($provider)) {
118 3
				return $provider;
119
			}
120 2
		}
121
122
		if ($tryMx && ($dns = dns_get_record($domain, DNS_MX))) {
123
			$domain = $dns[0]['target'];
124
			if (!($provider = $this->query($domain, false))) {
125
				list(, $domain) = explode('.', $domain, 2);
126
				$provider = $this->query($domain, false);
127
			}
128
		}
129
		return $provider;
130
	}
131
132
}
133