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

RadioMetadata::fetchUrlData()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 3
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 1
nc 1
nop 1
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
	public static function fetchUrlData($url) : array {
21
		$content = \file_get_contents($url);
22
		list($version, $status_code, $msg) = explode(' ', $http_response_header[0], 3);
23
		return [$content, $status_code, $msg];
24
	}
25
26
	public static function fetchStreamData($url, $maxattempts, $maxredirect) {
27
		$parse_url = parse_url($url);
28
		$timeout = 10;
29
		$port = 80;
30
		if (isset($parse_url['port'])) {
31
			$port = $parse_url['port'];
32
		} else if ($parse_url['scheme'] == "https") {
33
			$port = 443;
34
		}
35
		$hostname = $parse_url['host'];
36
		$pathname = $parse_url['path'];
37
		if (isset($parse_url['query'])) {
38
			$pathname .= "?" . $parse_url['query'];
39
		}
40
		if ($parse_url['scheme'] == "https") {
41
			$sockadd = "ssl://" . $hostname;
42
		} else {
43
			$sockadd = $hostname;
44
		}
45
		$streamTitle = "";
46
		if (($sockadd)&&($port)) {
47
			$fp = fsockopen($sockadd, $port, $errno, $errstr, $timeout);
48
			if ($fp != false) {
49
				$out = "GET " . $pathname . " HTTP/1.1\r\n";
50
				$out .= "Host: ". $hostname . "\r\n";
51
				$out .= "Accept: */*\r\n"; /* test */
52
				$out .= "User-Agent: OCMusic/1.52\r\n";
53
				$out .= "Icy-MetaData: 1\r\n";
54
				$out .= "Connection: Close\r\n\r\n";
55
				fwrite($fp, $out);
56
				stream_set_timeout($fp, $timeout);
57
58
				$header = fread($fp, 1024);
59
				$headers = array();
0 ignored issues
show
Unused Code introduced by
The assignment to $headers is dead and can be removed.
Loading history...
60
				$headers = explode("\n", $header);
61
62
				if (strstr($headers[0], "200 OK") !== false) {
63
					$interval = 0;
64
					foreach ($headers as $value) {
65
						if (strstr($value, "icy-metaint:") !== false) {
66
							$val = explode(':', $value);
67
							$interval = trim($val[1]);
68
							break;
69
						}
70
					}
71
72
					if (($interval)&&($interval<64001)) {
73
						$attempts = 0;
74
						while ($attempts < $maxattempts) {
75
							for ($j = 0; $j < $interval; $j++) {
76
								fread($fp, 1);
77
							}
78
79
							$meta_length = ord(fread($fp, 1)) * 16;
80
							if ($meta_length) {
81
								$metadatas = explode(';', fread($fp, $meta_length));
82
                        					foreach ($metadatas as $metadata) {
83
			        		                        if (strstr($metadata, "StreamTitle") !== false) {
84
										$streamTitle = trim(explode('=', $metadata)[1], "'");
85
										if (strlen($streamTitle) > 120) {
86
											$streamTitle = "";
87
										}
88
										break;
89
                                					}
90
								}
91
							}
92
							$attempts++;
93
						}
94
					}
95
				} else if (($maxredirect>0)&&(strstr($headers[0], "302 Found") !== false)) {
96
					foreach ($headers as $value) {
97
						$val = strstr($value, "Location:");
98
						if ($val) {
99
							$location = trim(substr($val, 10), "\r");
100
							$streamTitle = RadioMetadata::fetchStreamData($location, $maxattempts, $maxredirect-1);
101
							break;
102
						}
103
					}
104
				}
105
				fclose($fp);
106
			}
107
		}
108
		return $streamTitle;
109
	}
110
111
}
112