f5c49777687035e1c343832ae2160cadd3177127
[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     addPreviousItem($listItems, $fullPath);
30
31     $it = $container->getItemIterator(false);
32     $it->rewind();
33     $hasFiles = $it->valid();
34     if ($hasFiles && is_array($mediatomb['singleFileDirectories'])) {
35         $enableSingle = false;
36         foreach ($mediatomb['singleFileDirectories'] as $dir) {
37             if (substr($fullPath, 0, strlen($dir)) == $dir) {
38                 $enableSingle = true;
39             }
40         }
41         if ($enableSingle) {
42             $listItems[] = getDirItem(
43                 'Einzeln',
44                 '.mt-single/' . $path
45             );
46         }
47     }
48
49     foreach ($container->getContainers() as $subContainer) {
50         $listItems[] = getDirItem(
51             $subContainer->title,
52             pathEncode($fullPath . $subContainer->title) . '/'
53         );
54     }
55
56     foreach ($container->getItemIterator(false) as $item) {
57         mediatombAddFile($listItems, $item);
58     }
59
60     sendListItems($listItems);
61 }
62
63 function mediatombAddFile(&$listItems, $item)
64 {
65     global $host1;
66
67     $di = $item->getDetailedItem();
68     $itemUrl = $item->url;
69     if ($di->mimetype !== 'audio/mpeg') {
70         //noxon iRadio cube does not want to play .ogg files
71         $itemUrl = $host1 . 'transcode-nocache.php'
72             . '?url=' . urlencode($itemUrl);
73     }
74     $listItems[] = getEpisodeItem(
75         $item->title,
76         $itemUrl,
77         '',
78         'MP3'
79     );
80 }
81
82 /**
83  * Single file mode - shows directories that only have a single file in them.
84  * Each audio file gets its own virtual directory, containing only the
85  * audio file itself.
86  *
87  * Useful children who want to listen a single story before sleeping,
88  * but the noxon's auto switch-off timer is not exactly at the end of
89  * the story. So the next story starts already, and the kid complains
90  * that it wanted to listen to that as well...
91  */
92 function mediatombSingle(Services_MediaTomb $smt, $fullPath, $prefix)
93 {
94     $path = substr($fullPath, strlen($prefix));
95
96     $parts = explode('/', $path);
97     $fileMode = false;
98     if (substr(end($parts), 0, 5) == 'file-') {
99         $fileMode = true;
100         $fileTitle = substr(end($parts), 5);
101         $path = substr($path, 0, -strlen($fileTitle) - 5);
102     }
103
104     $container = $smt->getContainerByPath($path);
105     $listItems = array();
106
107     if ($fileMode) {
108         //show single file to play
109         addPreviousItem($listItems, $fullPath);
110         $item = $smt->getSingleItem($container, $fileTitle, false);
111         mediatombAddFile($listItems, $item);
112     } else {
113         addPreviousItem($listItems, 'internetradio/' . $path . '/dummy');
114
115         //browse directory
116         foreach ($container->getItemIterator(false) as $item) {
117             $listItems[] = getDirItem(
118                 $item->title,
119                 $fullPath . 'file-' . $item->title
120             );
121         }
122     }
123
124     sendListItems($listItems);
125 }
126 ?>