]> git.mxchange.org Git - friendica-addons.git/blob - jappixmini/jappix/php/music-search.php
Merge branch '3.6-release'
[friendica-addons.git] / jappixmini / jappix / php / music-search.php
1 <?php
2
3 /*
4
5 Jappix - An open social platform
6 This is the Jappix music search script
7
8 -------------------------------------------------
9
10 License: AGPL
11 Author: Vanaryon
12 Last revision: 15/01/12
13
14 */
15
16 // PHP base
17 define('JAPPIX_BASE', '..');
18
19 // Get the needed files
20 require_once('./functions.php');
21 require_once('./read-main.php');
22 require_once('./read-hosts.php');
23
24 // Optimize the page rendering
25 hideErrors();
26 compressThis();
27
28 // Not allowed for a special node
29 if(isStatic() || isUpload())
30         exit;
31
32 // If valid data was sent
33 if((isset($_GET['searchquery']) && !empty($_GET['searchquery'])) && (isset($_GET['location']) && !empty($_GET['location']))) {
34         // Set a XML header
35         header('Content-Type: text/xml; charset=utf-8');
36         
37         // Get the values
38         $searchquery = $_GET['searchquery'];
39         $location = $_GET['location'];
40         
41         // Jamendo search?
42         if($location == 'jamendo')
43                 exit(read_url('http://api.jamendo.com/get2/name+id+duration+url/track/xml/?searchquery='.urlencode($searchquery).'&order=searchweight_desc'));
44         
45         // Local music search
46         $xml = '<data>';
47         $searchquery = strtolower($searchquery);
48         
49         // Escape the regex special characters
50         $searchquery = escapeRegex($searchquery);
51         
52         // Search in the directory
53         $repertory = '../store/music/';
54         $scan = scandir($repertory);
55         
56         foreach($scan as $current) {
57                 // This file match our query!
58                 if(is_file($repertory.$current) && $current && preg_match('/(^|\s|\[)('.$searchquery.')(.+)?(\.(og(g|a)|mp3|wav))$/i', strtolower($current))) {
59                         // Get the basic informations
60                         $title = preg_replace('/^(.+)(\.)(og(g|a)|mp3|wav)$/i', '$1', $current);
61                         $url = $location.'store/music/'.$current;
62                         $ext = getFileExt($current);
63                         $id = md5($url);
64                         
65                         // Get the MIME type
66                         if($ext == 'mp3')
67                                 $type = 'audio/mpeg';
68                         else if($ext == 'wav')
69                                 $type = 'audio/x-wav';
70                         else
71                                 $type = 'audio/ogg';
72                         
73                         // Get the advanced informations
74                         $locked_title = $title;
75                         $artist = '';
76                         $source = '';
77                         
78                         $title_regex = '/^(([^-]+) - )?([^\[]+)( \[(.+))?$/i';
79                         $artist_regex = '/^(.+) - (.+)$/i';
80                         $source_regex = '/^(.+) \[(.+)\]$/i';
81                         
82                         if(preg_match($title_regex, $locked_title))
83                                 $title = preg_replace($title_regex, '$3', $locked_title);
84                         
85                         if(preg_match($artist_regex, $locked_title))
86                                 $artist = preg_replace($artist_regex, '$1', $locked_title);
87                         
88                         if(preg_match($source_regex, $locked_title))
89                                 $source = preg_replace($source_regex, '$2', $locked_title);
90                         
91                         // Generate the XML
92                         $xml .= '<data><track><name>'.htmlspecialchars($title).'</name><artist>'.htmlspecialchars($artist).'</artist><source>'.htmlspecialchars($source).'</source><id>'.htmlspecialchars($id).'</id><url>'.htmlspecialchars($url).'</url><type>'.$type.'</type></track></data>';
93                 }
94         }
95         
96         // End
97         $xml .= '</data>';
98         
99         // Return the path to the file
100         exit($xml);
101 }
102
103 ?>