96be27c0114ba8c5bd75cf79c909b5e120fa7f68
[noxon-gateway.git] / src / mediatomb.php
1 <?php
2 require_once 'Services/MediaTomb.php';
3
4 function handleMediatomb($action, $fullPath, $prefix)
5 {
6     global $mediatomb;
7
8     extract($mediatomb);
9     try {
10         $smt = new Services_MediaTomb($user, $pass, $host, $port);
11         if ($action == 'browse') {
12             mediatombBrowse($smt, $fullPath, $prefix);
13         } else if ($action == 'single') {
14             mediatombSingle($smt, $fullPath, $prefix);
15         }
16     } catch (Exception $e) {
17         sendMessage('Mediatomb error: ' . $e->getMessage());
18         return;
19     }
20 }
21
22 function mediatombBrowse(Services_MediaTomb $smt, $fullPath, $prefix)
23 {
24     global $mediatomb;
25
26     $path = substr($fullPath, strlen($prefix));
27     $container = $smt->getContainerByPath($path);
28     $listItems = array();
29
30     $it = $container->getItemIterator(false);
31     $it->rewind();
32     $hasFiles = $it->valid();
33     if ($hasFiles && is_array($mediatomb['singleFileDirectories'])) {
34         $enableSingle = false;
35         foreach ($mediatomb['singleFileDirectories'] as $dir) {
36             if (substr($fullPath, 0, strlen($dir)) == $dir) {
37                 $enableSingle = true;
38             }
39         }
40         if ($enableSingle) {
41             $listItems[] = getDirItem(
42                 'Einzeln',
43                 pathEncode('.mt-single/' . $path)
44             );
45         }
46     }
47
48     foreach ($container->getContainers() as $subContainer) {
49         $listItems[] = getDirItem(
50             $subContainer->title,
51             pathEncode($fullPath . $subContainer->title) . '/'
52         );
53     }
54
55     foreach ($container->getItemIterator(false) as $item) {
56         mediatombAddFile($listItems, $item);
57     }
58
59     sendListItems($listItems, buildPreviousItem($fullPath));
60 }
61
62 function mediatombAddFile(&$listItems, $item)
63 {
64     global $host1;
65
66     $di = $item->getDetailedItem();
67     $itemUrl = $item->url;
68     if (!clientSupportsType($di->mimetype)) {
69         //client wants transcoded file
70         //noxon iRadio cube does not want to play .ogg files
71         if (isset($GLOBALS['cacheDir']) && $GLOBALS['cacheDir'] != '') {
72             $itemUrl = $host1 . 'transcode-cache.php'
73                 . '?url=' . urlencode($itemUrl);
74         } else {
75             $itemUrl = $host1 . 'transcode-nocache.php'
76                 . '?url=' . urlencode($itemUrl);
77         }
78     }
79     $listItems[] = getEpisodeItem(
80         $item->title,
81         $itemUrl,
82         '',
83         'MP3'
84     );
85 }
86
87 function clientSupportsType($mimetype)
88 {
89     if ($mimetype === 'audio/mpeg') {
90         return true;
91     }
92     $ip = $_SERVER['REMOTE_ADDR'];
93     if (isset($GLOBALS['clientSupport'][$ip][$mimetype])
94         && $GLOBALS['clientSupport'][$ip][$mimetype] === true
95     ) {
96         return true;
97     }
98     return false;
99 }
100
101 /**
102  * Single file mode - shows directories that only have a single file in them.
103  * Each audio file gets its own virtual directory, containing only the
104  * audio file itself.
105  *
106  * Useful children who want to listen a single story before sleeping,
107  * but the noxon's auto switch-off timer is not exactly at the end of
108  * the story. So the next story starts already, and the kid complains
109  * that it wanted to listen to that as well...
110  */
111 function mediatombSingle(Services_MediaTomb $smt, $fullPath, $prefix)
112 {
113     $path = substr($fullPath, strlen($prefix));
114
115     $parts = explode('/', $path);
116     $fileMode = false;
117     if (substr(end($parts), 0, 5) == 'file-') {
118         $fileMode = true;
119         $fileTitle = substr(end($parts), 5);
120         $path = substr($path, 0, -strlen($fileTitle) - 5);
121     }
122
123     $container = $smt->getContainerByPath($path);
124     $listItems = array();
125
126     $previous = null;
127     if ($fileMode) {
128         //show single file to play
129         $previous = buildPreviousItem(pathEncode($fullPath));
130         $item = $smt->getSingleItem($container, $fileTitle, false);
131         mediatombAddFile($listItems, $item);
132     } else {
133         $previous = buildPreviousItem(pathEncode('internetradio/' . $path . '/dummy'));
134
135         //browse directory
136         foreach ($container->getItemIterator(false) as $item) {
137             $listItems[] = getDirItem(
138                 $item->title,
139                 pathEncode($fullPath . 'file-' . $item->title)
140             );
141         }
142     }
143
144     sendListItems($listItems, $previous);
145 }
146 ?>