MediaStreamer /rest/api.php

Language PHP Lines 206
MD5 Hash 769e231dfc3026f253fe9bb1793bfb4f
Repository https://github.com/cyprieng/MediaStreamer.git View Raw File
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
<?php
	header ("Content-Type:text/xml");
	require_once('../global/init.php');
	require_once(ROOT_PATH.MODELE_PATH.'login.php');
	require_once(ROOT_PATH.MODELE_PATH.'settings.php');
	require_once(ROOT_PATH.MODELE_PATH.'home.php');
	require_once(ROOT_PATH.MODELE_PATH.'podcast.php');
	require_once(ROOT_PATH.MODELE_PATH.'scrobbler.php');
	require_once(ROOT_PATH.MODELE_PATH.'getid3/getid3.php');


	//On vérifit si tout les paramètres sont transmis
	$missing_parameter = '<?xml version="1.0" encoding="UTF-8"?><subsonic-response xmlns="http://subsonic.org/restapi" status="failed" version="1.1.0"><error code="10" message="Required parameter is missing"/></subsonic-response>';
	if(empty($_GET['u']) || empty($_GET['p']) || empty($_GET['v']) || empty($_GET['c'])){exit($missing_parameter);}

	//Si le pass est en hexa, on le décode
	function hextostr($hex){
		$str='';
		for ($i=0; $i < strlen($hex)-1; $i+=2){
			$str .= chr(hexdec($hex[$i].$hex[$i+1]));
		}
		return $str;
	}
	if(preg_match('#^enc:#', $_GET['p'])){
		$passToStr = hextostr($_GET['p']);

		$pass = '';
		for($i = 0;$i <= strlen($passToStr)-1;$i++){
			if(preg_match('/^[a-z0-9_-]{1}$/', $passToStr[$i])){
				$pass .= $passToStr[$i];
			}
		}
	}
	else{$pass = $_GET['p'];}

	//On test le login
	$login = testLogin($_GET['u'], sha1($_GET['u'].$pass));
	if(!$login){exit('<?xml version="1.0" encoding="UTF-8"?><subsonic-response xmlns="http://subsonic.org/restapi" status="failed" version="1.1.0"><error code="40" message="Wrong username or password"/></subsonic-response>');}

	//On récupère les infos sur l'user
	$user = getUserArray();
	foreach($user as $key => $us){
		if($us['name'] == $_GET['u']){$user = $user[$key];break;}
	}

	$api = $_GET['api'];

	switch($api){ //cf Subsonic API
		case 'ping':
			echo '<?xml version="1.0" encoding="UTF-8"?><subsonic-response xmlns="http://subsonic.org/restapi" status="ok" version="1.1.1"></subsonic-response>';
		break;

		case 'getLicense':
			echo '<subsonic-response xmlns="http://subsonic.org/restapi" status="ok" version="1.8.0">
<license valid="true" email="demo@subsonic.org" key="27608bf44d8dceed3a90c1b608dc4bea" date="2009-02-26T12:24:19" />
</subsonic-response>';
		break;

		case 'getMusicFolders':
			echo '<?xml version="1.0" encoding="UTF-8"?><subsonic-response xmlns="http://subsonic.org/restapi" status="ok" version="1.1.1"><musicFolders><musicFolder id="1" name="Music"/></musicFolders></subsonic-response>';
		break;

		case 'getIndexes':
			$music_library = getArborescence($user['music_folder'], explode(', ', MUSIC_EXTENSION));
			unset($music_library[0]); //On supprime le nom du dossier racine
			$library_xml = '<artist name="Podcast" id="0" />'; $i=1;

			foreach($music_library as $arbo){ //On ajoute tous les dossiers
				$library_xml .= '<artist name="'.substr(strrchr($arbo[0],'/'),1).'" id="'.$i.'" />';
				$i++;
			}

			echo '<?xml version="1.0" encoding="UTF-8"?><subsonic-response xmlns="http://subsonic.org/restapi" status="ok" version="1.1.1"><indexes lastModified="'.time().'">'.$library_xml.'</indexes></subsonic-response>';
		break;

		case 'getMusicDirectory':
			if(empty($_GET['id']) && $_GET['id'] != 0){exit($missing_parameter);}

			$library = getArborescence($user['music_folder'], explode(', ', MUSIC_EXTENSION));

			if(preg_match('#^0#', $_GET['id'])){ //Podcast
				$podcast_library = getPodcast(explode(',', $user['podcast']));
				$library_xml = '';$i=0;

				if($_GET['id'] == '0'){ //Racine podcast
					$name = 'Podcast';
					foreach($podcast_library as $podcast){ //On boucle les podcasts
						$library_xml .= '<child id="'.$_GET['id'].'c'.$i.'" parent="'.$_GET['id'].'" title="'.substr(strrchr($podcast[0],'/'),1).'" isDir="true" />';
						$i++;
					}
				}
				else{
					$id = explode('c', $_GET['id']);
					$id = $id[1];

					$podcast_library = $podcast_library[$id]; //On récupère le podcast voulu
					$name = substr(strrchr($podcast_library[0],'/'),1);
					unset($podcast_library[0]); //On supprime le nom

					foreach($podcast_library as $podcast){ //On parcours les item
						$library_xml .= '<child id="'.$_GET['id'].'c'.$i.'" parent="'.$_GET['id'].'" title="'.preg_replace('#"#', "''", Trim($podcast[1])).'" isDir="false" album="'.$name.'" artist="'.$name.'" track="'.$i.'" type="podcast"/>';
						$i++;
					}
				}
				echo '<?xml version="1.0" encoding="UTF-8"?><subsonic-response xmlns="http://subsonic.org/restapi" status="ok" version="1.4.0"><directory id="'.$_GET['id'].'" name="'.$name.'">'.$library_xml.'</directory></subsonic-response>';
				exit('');

			}
			else if(!isset($library[$_GET['id']])){ //Musique sous-dossier
				$id = explode('c', $_GET['id']);
				
				$music_library = $library;
				
				for($i=1;$i<=count($id);$i++){ //On parcour les dossier définit dans $id
					$music_library = $music_library[$id[$i-1]];
				}
			}
			else{ //Musique dans racine
				$music_library = $library[$_GET['id']];
			}

			$artistName = substr(strrchr($music_library[0],'/'),1);
			unset($music_library[0]); //On supprime le nom de l'artiste
			$library_xml = '';$i=1;

			foreach($music_library as $arbo){ //On parcours le dossier
				if(is_array($arbo) && is_dir($arbo[0])){ //Dossier
					$library_xml .= '<child id="'.$_GET['id'].'c'.$i.'" parent="'.$_GET['id'].'" title="'.substr(strrchr($arbo[0],'/'),1).'" artist="'.$artistName.'" isDir="true" />';
				}
				else{ //Fichier
					//On récupère les tag de la musique
					$getID3 = new getID3;
					$tag = $getID3->analyze($arbo);
					$tag['bitrate'] = floor($tag['bitrate'] / 1000);
					$tag['tags_html']['id3v1']['album'][0] = ($tag['tags_html']['id3v1']['album'][0] != '') ? $tag['tags_html']['id3v1']['album'][0] : 'unknow';
					$tag['tags_html']['id3v1']['artist'][0] = ($tag['tags_html']['id3v1']['artist'][0] != '') ? $tag['tags_html']['id3v1']['artist'][0] : 'unknow';
					$tag['tags_html']['id3v1']['year'][0] = ($tag['tags_html']['id3v1']['year'][0] != '') ? $tag['tags_html']['id3v1']['year'][0] : '1970';
					$tag['tags_html']['id3v1']['genre'][0] = ($tag['tags_html']['id3v1']['genre'][0] != '') ? $tag['tags_html']['id3v1']['genre'][0] : 'unknow';

					$library_xml .= '<child id="'.$_GET['id'].'c'.$i.'" parent="'.$_GET['id'].'" title="'.substr(strrchr($arbo,'/'),1).'" isDir="false" album="'.$tag['tags_html']['id3v1']['album'][0].'" artist="'.$tag['tags_html']['id3v1']['artist'][0].'" track="'.$i.'" year="'.$tag['tags_html']['id3v1']['year'][0].'" genre="'.$tag['tags_html']['id3v1']['genre'][0].'" size="'.$tag['filesize'].'" contentType="'.$tag['mime_type'].'" suffix="'.$tag['fileformat'].'" duration="'.floor($tag['playtime_seconds']).'" bitRate="'.$tag['bitrate'].'" path="'.$arbo.'"/>';
				}
				$i++;
			}

			echo preg_replace('#&#', '&amp;', '<?xml version="1.0" encoding="UTF-8"?><subsonic-response xmlns="http://subsonic.org/restapi" status="ok" version="1.4.0"><directory id="'.$_GET['id'].'" name="'.$artistName.'">'.$library_xml.'</directory></subsonic-response>');
		break;

		case 'stream':
			if(empty($_GET['id'])){exit($missing_parameter);} //On vérifit les paramètres

			$library = getArborescence($user['music_folder'], explode(', ', MUSIC_EXTENSION));

			if(preg_match('#^0#', $_GET['id'])){ //Podcast
				//On récupère le podcast
				$podcast_library = getPodcast(explode(',', $user['podcast']));
				$id = explode('c', $_GET['id']);
				$music_library = $podcast_library[$id[1]][$id[2]+1][0][0];

			}
			else if(!isset($library[$_GET['id']])){ //Musique dans sous dossier
				$id = explode('c', $_GET['id']);
				
				$music_library = $library;
				
				for($i=1;$i<=count($id);$i++){ //On parcour les dossier définit dans $id
					$music_library = $music_library[$id[$i-1]];
				}
			}
			else{ //Musique dans dossier racine
				$music_library = $library[$_GET['id']];
			}

			//On définit les variables pour media.php
			$_GET['file'] = $music_library;
			$_GET['p'] = sha1($_GET['u'].$pass);

			scrobble($music_library, $user['lastfm_session']); //On scrobble
			require(ROOT_PATH.MODELE_PATH.'media.php');
		break;

		case 'scrobble':
			if(empty($_GET['id'])){exit($missing_parameter);}

			$library = getArborescence($user['music_folder'], explode(', ', MUSIC_EXTENSION));

			if(!isset($library[$_GET['id']])){ //Musique dns sous-dossier
				$id = explode('c', $_GET['id']);
				
				$music_library = $library;
				
				for($i=1;$i<=count($id);$i++){ //On parcour les dossier définit dans $id
					$music_library = $music_library[$id[$i-1]];
				}
			}
			else{ //Musique dans dossier racine
				$music_library = $library[$_GET['id']];
			}

			scrobble($music_library, $user['lastfm_session']); //On scrobble
			echo '<?xml version="1.0" encoding="UTF-8"?><subsonic-response xmlns="http://subsonic.org/restapi" status="ok" version="1.8.0"></subsonic-response>';
		break;

		default:
			echo '<?xml version="1.0" encoding="UTF-8"?><subsonic-response xmlns="http://subsonic.org/restapi" status="failed" version="1.1.0"><error code="70" message="The requested data was not found"/></subsonic-response>';
	}
?>
Back to Top