Support playlist management
[infodrom/musiikki-web.git] / class / playlists.class.php
1 <?php
2
3 class Playlists {
4     const EMPTYSTRING = 'not empty';
5
6     public function getList()
7     {
8         $dir = Config::main()->get('media_dir_A') . '/playlists';
9         $list = [];
10
11         foreach (new DirectoryIterator($dir) as $fileInfo) {
12             if ($fileInfo->isDot()) continue;
13             if ($fileInfo->isDir()) continue;
14             if ($fileInfo->getExtension() != 'm3u') continue;
15
16             $lines = preg_split('/\r?\n/', file_get_contents($fileInfo->getPathname()));
17             if ($lines[0] == static::EMPTYSTRING) {
18                 $count = 0;
19             } else {
20                 $count = count($lines) - 1;
21             }
22
23             $list[] = ['name' => $fileInfo->getBasename('.m3u'), 'count' => $count];
24         }
25
26         usort($list, function($a, $b){return strcmp($a['name'], $b['name']);});
27         return $list;
28     }
29
30     public function getContents($name)
31     {
32         $dir = Config::main()->get('media_dir_A') . '/playlists';
33         $list = [];
34
35         $path = $dir . '/' . $name . '.m3u';
36         if (file_exists($path)) {
37             $content = explode("\n", file_get_contents($path));
38             foreach ($content as $path) {
39                 if (empty(trim($path))) continue;
40                 if ($path == static::EMPTYSTRING) continue;
41
42                 $fname = basename($path);
43
44                 if (($pos = strrpos($fname, '.')) > strlen($fname)-10)
45                     $fname = substr($fname, 0, $pos);
46                 $list[] = $fname;
47             }       
48         }
49
50         return $list;
51     }
52
53     public function add($name, $path)
54     {
55         $playlist = Config::main()->get('media_dir_A') . '/playlists/' . $name . '.m3u';
56         if (!file_exists($playlist)) return;
57
58         $content = explode("\n", file_get_contents($playlist));
59         $found = false;
60         foreach ($content as $line)
61             if ($line == $path)
62                 $found = true;
63
64         if (!$found)
65             $content[] = $path;
66
67         if (($f = fopen($playlist.'.new', 'w')) !== false) {
68             foreach ($content as $line) {
69                 if (empty(trim($line))) continue;
70                 if ($line == static::EMPTYSTRING) continue;
71                 fwrite($f, $line . "\n");
72             }
73             fclose($f);
74             rename($playlist.'.new', $playlist);
75         }
76         
77     }
78
79     public function create($name)
80     {
81         $playlist = Config::main()->get('media_dir_A') . '/playlists/' . $name . '.m3u';
82         if (file_exists($playlist)) return;
83
84         if (($f = fopen($playlist, 'w')) !== false) {
85             fwrite($f, static::EMPTYSTRING . "\n");
86             
87             fclose($f);
88         }
89     }
90 }