Passed
Pull Request — master (#992)
by
unknown
02:12
created

RadioMetadata::fetchStreamData()   C

Complexity

Conditions 16
Paths 13

Size

Total Lines 59
Code Lines 43

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
eloc 43
c 3
b 0
f 0
dl 0
loc 59
rs 5.5666
cc 16
nc 13
nop 3

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php declare(strict_types=1);
2
3
/**
4
 * ownCloud - Music app
5
 *
6
 * This file is licensed under the Affero General Public License version 3 or
7
 * later. See the COPYING file.
8
 *
9
 * @author Moahmed-Ismail MEJRI <[email protected]>
10
 * @copyright Moahmed-Ismail MEJRI 2022
11
 */
12
13
namespace OCA\Music\Utility;
14
15
/**
16
 * MetaData radio utility functions
17
 */
18
class RadioMetadata {
19
20
	private static function findStr($data, $str) {
21
		$ret = "";
22
		foreach ($data as $value) {
23
			$find = strstr($value, $str);
24
			if ($find !== false) {
25
				$ret = $find;
26
				break;
27
			}
28
		}
29
		return $ret;
30
	}
31
32
	private static function parseStreamUrl($url) {
33
34
		$ret = array();
35
		$parse_url = parse_url($url);
36
37
		$ret['port'] = 80;
38
		if (isset($parse_url['port'])) {
39
			$ret['port'] = $parse_url['port'];
40
		} else if ($parse_url['scheme'] == "https") {
41
			$ret['port'] = 443;
42
		}
43
44
		$ret['hostname'] = $parse_url['host'];
45
		$ret['pathname'] = $parse_url['path'];
46
47
		if (isset($parse_url['query'])) {
48
			$ret['pathname'] .= "?" . $parse_url['query'];
49
		}
50
51
		if ($parse_url['scheme'] == "https") {
52
			$ret['sockadd'] = "ssl://" . $ret['hostname'];
53
		} else {
54
			$ret['sockadd'] = $ret['hostname'];
55
		}
56
57
		return $ret;
58
	}
59
60
61
	public static function fetchUrlData($url) : array {
62
		$content = \file_get_contents($url);
63
		list($version, $status_code, $msg) = explode(' ', $http_response_header[0], 3);
64
		return [$content, $status_code, $msg];
65
	}
66
67
	public static function fetchStreamData($url, $maxattempts, $maxredirect) {
68
		$timeout = 10;
69
		$streamTitle = "";
70
		$pUrl = self::parseStreamUrl($url);
71
		if (($pUrl['sockadd']) && ($pUrl['port'])) {
72
			$fp = fsockopen($pUrl['sockadd'], $pUrl['port'], $errno, $errstr, $timeout);
73
			if ($fp != false) {
74
				$out = "GET " . $pUrl['pathname'] . " HTTP/1.1\r\n";
75
				$out .= "Host: ". $pUrl['hostname'] . "\r\n";
76
				$out .= "Accept: */*\r\n"; /* test */
77
				$out .= "User-Agent: OCMusic/1.52\r\n";
78
				$out .= "Icy-MetaData: 1\r\n";
79
				$out .= "Connection: Close\r\n\r\n";
80
				fwrite($fp, $out);
81
				stream_set_timeout($fp, $timeout);
82
83
				$header = fread($fp, 1024);
84
				$headers = explode("\n", $header);
85
86
				if (strstr($headers[0], "200 OK") !== false) {
87
					$interval = 0;
88
					$line = self::findStr($headers, "icy-metaint:");
89
					if ($line) {
90
						$interval = trim(explode(':', $line)[1]);
91
					}
92
93
					if (($interval)&&($interval<64001)) {
94
						$attempts = 0;
95
						while ($attempts < $maxattempts) {
96
							for ($j = 0; $j < $interval; $j++) {
97
								fread($fp, 1);
98
							}
99
100
							$meta_length = ord(fread($fp, 1)) * 16;
101
							if ($meta_length) {
102
								$metadatas = explode(';', fread($fp, $meta_length));
103
								$metadata = self::findStr($metadatas, "StreamTitle");
104
								if ($metadata) {
105
									$streamTitle = trim(explode('=', $metadata)[1], "'");
106
									if (strlen($streamTitle) > 256) {
107
										$streamTitle = "";
108
									}
109
									break;
110
								}
111
							}
112
							$attempts++;
113
						}
114
					}
115
				} else if (($maxredirect>0)&&(strstr($headers[0], "302 Found") !== false)) {
116
					$value = self::findStr($headers, "Location:");
117
					if ($value) {
118
						$location = trim(substr($value, 10), "\r");
119
						$streamTitle = RadioMetadata::fetchStreamData($location, $maxattempts, $maxredirect-1);
120
					}
121
				}
122
				fclose($fp);
123
			}
124
		}
125
		return $streamTitle;
126
	}
127
128
}
129