Completed
Pull Request — master (#1416)
by Christoph
06:57
created

IspDb::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 3
Bugs 0 Features 0
Metric Value
c 3
b 0
f 0
dl 0
loc 4
ccs 4
cts 4
cp 1
rs 10
cc 1
eloc 3
nc 1
nop 2
crap 1
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
use OCP\Http\Client\IClientService;
27
28
class IspDb {
29
30
	/** @var Logger */
31
	private $logger;
32
33
	/** @var IClientService */
34
	private $clientService;
35
36
	/** @var string[] */
37
	private $urls = [
38
		'https://autoconfig.{DOMAIN}/mail/config-v1.1.xml',
39
		'https://{DOMAIN}/.well-known/autoconfig/mail/config-v1.1.xml',
40
		'https://autoconfig.thunderbird.net/v1.1/{DOMAIN}',
41
	];
42
43
	/**
44
	 * @param Logger $logger
45
	 * @param string[] $clientService
46
	 */
47 2
	public function __construct(Logger $logger, IClientService $clientService) {
48 2
		$this->logger = $logger;
49 2
		$this->clientService = $clientService;
50 2
	}
51
52 1
	private function readProviderData($config) {
53
		$provider = [
54 1
			'displayName' => (string) $config->emailProvider->displayName,
55 1
		];
56 1
		foreach ($config->emailProvider->children() as $tag => $server) {
57 1
			if (!in_array($tag, ['incomingServer', 'outgoingServer'])) {
58 1
				continue;
59
			}
60 1
			foreach ($server->attributes() as $name => $value) {
61 1
				if ($name == 'type') {
62 1
					$type = (string) $value;
63 1
				}
64 1
			}
65 1
			$data = [];
66 1
			foreach ($server as $name => $value) {
67 1
				foreach ($value->children() as $tag => $val) {
68
					$data[$name][$tag] = (string) $val;
69 1
				}
70 1
				if (!isset($data[$name])) {
71 1
					$data[$name] = (string) $value;
72 1
				}
73 1
			}
74 1
			$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...
75 1
		}
76 1
		return $provider;
77
	}
78
79 2
	private function queryUrl($url) {
80
		try {
81 2
			$file = $this->clientService->newClient()->get($url);
82 1
			$config = @simplexml_load_string($file);
83 1
			if (libxml_get_last_error() !== False || !is_object($config) || !$config->emailProvider) {
84
				libxml_clear_errors();
85
				return [];
86
			}
87 1
			$provider = $this->readProviderData($config);
88 2
		} catch (Exception $e) {
89
			// ignore own not-found exception or xml parsing exceptions
90 1
			unset($e);
91 1
			$provider = [];
92
		}
93 2
		return $provider;
94
	}
95
96
	/**
97
	 * @param string $domain
98
	 * @return array
99
	 */
100 2
	public function query($domain, $tryMx = true) {
101 2
		$this->logger->debug("IsbDb: querying <$domain>");
102 2
		if (strpos($domain, '@') !== false) {
103
			// TODO: use horde mail address parsing instead
104
			list(, $domain) = explode('@', $domain);
105
		}
106
107 2
		$provider = [];
108 2
		foreach ($this->urls as $url) {
109 2
			$url = str_replace("{DOMAIN}", $domain, $url);
110 2
			$this->logger->debug("IsbDb: querying <$domain> via <$url>");
111
112 2
			$provider = $this->queryUrl($url);
113 2
			if (!empty($provider)) {
114 1
				return $provider;
115
			}
116 1
		}
117
118 1
		if ($tryMx && ($dns = dns_get_record($domain, DNS_MX))) {
119 1
			$domain = $dns[0]['target'];
120 1
			if (!($provider = $this->query($domain, false))) {
121 1
				list(, $domain) = explode('.', $domain, 2);
122 1
				$provider = $this->query($domain, false);
123 1
			}
124 1
		}
125 1
		return $provider;
126
	}
127
128
}
129