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