4 const EMPTYSTRING = 'not empty';
6 public function getList()
8 $dir = Config::main()->get('media_dir_A') . '/playlists';
11 foreach (new DirectoryIterator($dir) as $fileInfo) {
12 if ($fileInfo->isDot()) continue;
13 if ($fileInfo->isDir()) continue;
14 if ($fileInfo->getExtension() != 'm3u') continue;
16 $lines = preg_split('/\r?\n/', file_get_contents($fileInfo->getPathname()));
17 if ($lines[0] == static::EMPTYSTRING) {
20 $count = count($lines) - 1;
23 $list[] = ['name' => $fileInfo->getBasename('.m3u'), 'count' => $count];
26 usort($list, function($a, $b){return strcmp($a['name'], $b['name']);});
30 public function getContents($name)
32 $dir = Config::main()->get('media_dir_A') . '/playlists';
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;
42 $fname = basename($path);
44 if (($pos = strrpos($fname, '.')) > strlen($fname)-10)
45 $fname = substr($fname, 0, $pos);
53 public function add($name, $path)
55 $playlist = Config::main()->get('media_dir_A') . '/playlists/' . $name . '.m3u';
56 if (!file_exists($playlist)) return;
58 $content = explode("\n", file_get_contents($playlist));
60 foreach ($content as $line)
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");
74 rename($playlist.'.new', $playlist);
79 public function create($name)
81 $playlist = Config::main()->get('media_dir_A') . '/playlists/' . $name . '.m3u';
82 if (file_exists($playlist)) return;
84 if (($f = fopen($playlist, 'w')) !== false) {
85 fwrite($f, static::EMPTYSTRING . "\n");