Passed
Push — master ( 9133f3...d53efc )
by Marcel
05:42 queued 03:57
created

CategoryController::getTracks()   A

Complexity

Conditions 4
Paths 8

Size

Total Lines 15
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 4
eloc 10
c 2
b 0
f 0
nc 8
nop 2
dl 0
loc 15
rs 9.9332
1
<?php
2
/**
3
 * Audio Player
4
 *
5
 * This file is licensed under the Affero General Public License version 3 or
6
 * later. See the LICENSE.md file.
7
 *
8
 * @author Marcel Scherello <[email protected]>
9
 * @copyright 2016-2019 Marcel Scherello
10
 */
11
12
namespace OCA\audioplayer\Controller;
13
14
use OCP\AppFramework\Controller;
15
use OCP\AppFramework\Http\JSONResponse;
16
use OCP\Files\InvalidPathException;
17
use OCP\IRequest;
18
use OCP\IL10N;
19
use OCP\IDbConnection;
20
use OCP\ITagManager;
21
use OCP\Files\IRootFolder;
22
use OCP\ILogger;
23
use \OCP\Files\NotFoundException;
24
25
/**
26
 * Controller class for main page.
27
 */
28
class CategoryController extends Controller
29
{
30
31
    private $userId;
32
    private $l10n;
33
    private $db;
34
    private $tagger;
35
    private $tagManager;
36
    private $rootFolder;
37
    private $logger;
38
    private $DBController;
39
40
    public function __construct(
41
        $appName,
42
        IRequest $request,
43
        $userId,
44
        IL10N $l10n,
45
        IDBConnection $db,
46
        ITagManager $tagManager,
47
        IRootFolder $rootFolder,
48
        ILogger $logger,
49
        DbController $DBController
50
    )
51
    {
52
        parent::__construct($appName, $request);
53
        $this->userId = $userId;
54
        $this->l10n = $l10n;
55
        $this->db = $db;
56
        $this->tagManager = $tagManager;
57
        $this->tagger = null;
58
        $this->rootFolder = $rootFolder;
59
        $this->logger = $logger;
60
        $this->DBController = $DBController;
61
    }
62
63
    /**
64
     * Get the items for the selected category
65
     *
66
     * @NoAdminRequired
67
     * @param $category
68
     * @return JSONResponse
69
     */
70
    public function getCategoryItems($category)
71
    {
72
        $SQL = null;
73
        $aPlaylists = array();
74
        if ($category === 'Artist') {
75
            $SQL = 'SELECT  DISTINCT(`AT`.`artist_id`) AS `id`, `AA`.`name`, LOWER(`AA`.`name`) AS `lower` 
76
						FROM `*PREFIX*audioplayer_tracks` `AT`
77
						JOIN `*PREFIX*audioplayer_artists` `AA`
78
						ON `AA`.`id` = `AT`.`artist_id`
79
			 			WHERE  `AT`.`user_id` = ?
80
			 			ORDER BY LOWER(`AA`.`name`) ASC
81
			 			';
82
        } elseif ($category === 'Genre') {
83
            $SQL = 'SELECT  `id`, `name`, LOWER(`name`) AS `lower` 
84
						FROM `*PREFIX*audioplayer_genre`
85
			 			WHERE  `user_id` = ?
86
			 			ORDER BY LOWER(`name`) ASC
87
			 			';
88
        } elseif ($category === 'Year') {
89
            $SQL = 'SELECT DISTINCT(`year`) AS `id` ,`year` AS `name`  
90
						FROM `*PREFIX*audioplayer_tracks`
91
			 			WHERE  `user_id` = ?
92
			 			ORDER BY `id` ASC
93
			 			';
94
        } elseif ($category === 'Title') {
95
            $SQL = "SELECT distinct('0') as `id` ,'" . $this->l10n->t('All Titles') . "' as `name`  
96
						FROM `*PREFIX*audioplayer_tracks`
97
			 			WHERE  `user_id` = ?
98
			 			";
99
        } elseif ($category === 'Playlist') {
100
            $aPlaylists[] = array('id' => 'X1', 'name' => $this->l10n->t('Favorites'));
101
            $aPlaylists[] = array('id' => 'X2', 'name' => $this->l10n->t('Recently Added'));
102
            $aPlaylists[] = array('id' => 'X3', 'name' => $this->l10n->t('Recently Played'));
103
            $aPlaylists[] = array('id' => 'X4', 'name' => $this->l10n->t('Most Played'));
104
            $aPlaylists[] = array('id' => '', 'name' => '');
105
106
            // Stream files are shown directly
107
            $SQL = 'SELECT  `file_id` AS `id`, `title` AS `name`, LOWER(`title`) AS `lower` 
108
						FROM `*PREFIX*audioplayer_streams`
109
			 			WHERE  `user_id` = ?
110
			 			ORDER BY LOWER(`title`) ASC
111
			 			';
112
            $stmt = $this->db->prepare($SQL);
113
            $stmt->execute(array($this->userId));
114
            $results = $stmt->fetchAll();
115
            foreach ($results as $row) {
116
                array_splice($row, 2, 1);
117
                $row['id'] = 'S' . $row['id'];
118
                $aPlaylists[] = $row;
119
            }
120
            $aPlaylists[] = array('id' => '', 'name' => '');
121
122
            // regular playlists are selected
123
            $SQL = 'SELECT  `id`,`name`, LOWER(`name`) AS `lower` 
124
						FROM `*PREFIX*audioplayer_playlists`
125
			 			WHERE  `user_id` = ?
126
			 			ORDER BY LOWER(`name`) ASC
127
			 			';
128
129
        } elseif ($category === 'Folder') {
130
            $SQL = 'SELECT  DISTINCT(`FC`.`fileid`) AS `id`, `FC`.`name`, LOWER(`FC`.`name`) AS `lower` 
131
						FROM `*PREFIX*audioplayer_tracks` `AT`
132
						JOIN `*PREFIX*filecache` `FC`
133
						ON `FC`.`fileid` = `AT`.`folder_id`
134
			 			WHERE `AT`.`user_id` = ?
135
			 			ORDER BY LOWER(`FC`.`name`) ASC
136
			 			';
137
        } elseif ($category === 'Album') {
138
            $SQL = 'SELECT  `AB`.`id` , `AB`.`name`, LOWER(`AB`.`name`) AS `lower`
139
						FROM `*PREFIX*audioplayer_albums` `AB`
140
						LEFT JOIN `*PREFIX*audioplayer_artists` `AA` 
141
						ON `AB`.`artist_id` = `AA`.`id`
142
			 			WHERE `AB`.`user_id` = ?
143
			 			ORDER BY LOWER(`AB`.`name`) ASC
144
			 			';
145
        } elseif ($category === 'Album Artist') {
146
            $SQL = 'SELECT  DISTINCT(`AB`.`artist_id`) AS `id`, `AA`.`name`, LOWER(`AA`.`name`) AS `lower` 
147
						FROM `*PREFIX*audioplayer_albums` `AB`
148
						JOIN `*PREFIX*audioplayer_artists` `AA`
149
						ON `AB`.`artist_id` = `AA`.`id`
150
			 			WHERE `AB`.`user_id` = ?
151
			 			ORDER BY LOWER(`AA`.`name`) ASC
152
			 			';
153
        }
154
155
        if (isset($SQL)) {
156
            $stmt = $this->db->prepare($SQL);
157
            $stmt->execute(array($this->userId));
158
            $results = $stmt->fetchAll();
159
            foreach ($results as $row) {
160
                array_splice($row, 2, 1);
161
                if ($row['name'] === '0' OR $row['name'] === '') $row['name'] = $this->l10n->t('Unknown');
162
                $row['cnt'] = $this->getTrackCount($category, $row['id']);
163
                $aPlaylists[] = $row;
164
            }
165
        }
166
167
        $result = empty($aPlaylists) ? [
168
            'status' => 'nodata'
169
        ] : [
170
            'status' => 'success',
171
            'data' => $aPlaylists
172
        ];
173
        return new JSONResponse($result);
174
    }
175
176
    /**
177
     * Get the covers for the "Album Covers" view
178
     *
179
     * @NoAdminRequired
180
     * @param $category
181
     * @param $categoryId
182
     * @return JSONResponse
183
     */
184
    public function getCategoryItemCovers($category, $categoryId)
185
    {
186
        $whereMatching = array('Artist' => '`AT`.`artist_id`', 'Genre' => '`AT`.`genre_id`', 'Album' => '`AB`.`id`', 'Album Artist' => '`AB`.`artist_id`', 'Year' => '`AT`.`year`', 'Folder' => '`AT`.`folder_id`');
187
188
        $aPlaylists = array();
189
        $SQL = 'SELECT  `AB`.`id` , `AB`.`name`, LOWER(`AB`.`name`) AS `lower` , `AA`.`id` AS `art`, (CASE  WHEN `AB`.`cover` IS NOT NULL THEN `AB`.`id` ELSE NULL END) AS `cid`';
190
        $SQL .= ' FROM `*PREFIX*audioplayer_tracks` `AT`';
191
        $SQL .= ' LEFT JOIN `*PREFIX*audioplayer_albums` `AB` ON `AB`.`id` = `AT`.`album_id`';
192
        $SQL .= ' LEFT JOIN `*PREFIX*audioplayer_artists` `AA` ON `AA`.`id` = `AB`.`artist_id`';
193
        $SQL .= ' WHERE `AT`.`user_id` = ? ';
194
        if ($categoryId) $SQL .= 'AND ' . $whereMatching[$category] . '= ?';
195
        $SQL .= ' GROUP BY `AB`.`id`, `AA`.`id`, `AB`.`name` ORDER BY LOWER(`AB`.`name`) ASC';
196
197
        if (isset($SQL)) {
198
            $stmt = $this->db->prepare($SQL);
199
            if ($categoryId) {
200
                $stmt->execute(array($this->userId, $categoryId));
201
            } else {
202
                $stmt->execute(array($this->userId));
203
            }
204
            $results = $stmt->fetchAll();
205
            foreach ($results as $row) {
206
                $row['art'] = $this->DBController->loadArtistsToAlbum($row['id'], $row['art']);
207
                array_splice($row, 2, 1);
208
                if ($row['name'] === '0' OR $row['name'] === '') $row['name'] = $this->l10n->t('Unknown');
209
                $aPlaylists[] = $row;
210
            }
211
        }
212
        $result = empty($aPlaylists) ? [
213
            'status' => 'nodata'
214
        ] : [
215
            'status' => 'success',
216
            'data' => $aPlaylists
217
        ];
218
        return new JSONResponse($result);
219
    }
220
221
    /**
222
     * Get the number of tracks for a category item
223
     *
224
     * @param string $category
225
     * @param integer $categoryId
226
     * @return integer
227
     */
228
    private function getTrackCount($category, $categoryId)
229
    {
230
        $SQL = array();
231
        $SQL['Artist'] = 'SELECT COUNT(`AT`.`id`) AS `count` FROM `*PREFIX*audioplayer_tracks` `AT` LEFT JOIN `*PREFIX*audioplayer_artists` `AA` ON `AT`.`artist_id` = `AA`.`id` WHERE  `AT`.`artist_id` = ? AND `AT`.`user_id` = ?';
232
        $SQL['Genre'] = 'SELECT COUNT(`AT`.`id`) AS `count` FROM `*PREFIX*audioplayer_tracks` `AT` WHERE  `AT`.`genre_id` = ? AND `AT`.`user_id` = ?';
233
        $SQL['Year'] = 'SELECT COUNT(`AT`.`id`) AS `count` FROM `*PREFIX*audioplayer_tracks` `AT` WHERE  `AT`.`year` = ? AND `AT`.`user_id` = ?';
234
        $SQL['Title'] = 'SELECT COUNT(`AT`.`id`) AS `count` FROM `*PREFIX*audioplayer_tracks` `AT` WHERE  `AT`.`id` > ? AND `AT`.`user_id` = ?';
235
        $SQL['Playlist'] = 'SELECT COUNT(`AP`.`track_id`) AS `count` FROM `*PREFIX*audioplayer_playlist_tracks` `AP` WHERE  `AP`.`playlist_id` = ?';
236
        $SQL['Folder'] = 'SELECT COUNT(`AT`.`id`) AS `count` FROM `*PREFIX*audioplayer_tracks` `AT` WHERE  `AT`.`folder_id` = ? AND `AT`.`user_id` = ?';
237
        $SQL['Album'] = 'SELECT COUNT(`AT`.`id`) AS `count` FROM `*PREFIX*audioplayer_tracks` `AT` WHERE  `AT`.`album_id` = ? AND `AT`.`user_id` = ?';
238
        $SQL['Album Artist'] = 'SELECT COUNT(`AT`.`id`) AS `count` FROM `*PREFIX*audioplayer_albums` `AB` JOIN `*PREFIX*audioplayer_tracks` `AT` ON `AB`.`id` = `AT`.`album_id` WHERE  `AB`.`artist_id` = ? AND `AB`.`user_id` = ?';
239
240
        $stmt = $this->db->prepare($SQL[$category]);
241
        if ($category === 'Playlist') {
242
            $stmt->execute(array($categoryId));
243
        } else {
244
            $stmt->execute(array($categoryId, $this->userId));
245
        }
246
        $results = $stmt->fetch();
247
        return $results['count'];
248
    }
249
250
    /**
251
     * get the tracks for a selected category or album
252
     *
253
     * @NoAdminRequired
254
     * @param string $category
255
     * @param string $categoryId
256
     * @return JSONResponse
257
     * @throws InvalidPathException
258
     * @throws NotFoundException
259
     */
260
    public function getTracks($category, $categoryId)
261
    {
262
        if ($categoryId[0] === 'S') $category = 'Stream';
263
        if ($categoryId[0] === 'P') $category = 'Playlist';
264
        $items = $this->getTracksDetails($category, $categoryId);
265
        $headers = $this->getListViewHeaders($category);
266
267
        $result = !empty($items) ? [
268
            'status' => 'success',
269
            'data' => $items,
270
            'header' => $headers,
271
        ] : [
272
            'status' => 'nodata',
273
        ];
274
        return new JSONResponse($result);
275
    }
276
277
    /**
278
     * Get the tracks for a selected category or album
279
     *
280
     * @param string $category
281
     * @param string $categoryId
282
     * @return array
283
     * @throws InvalidPathException
284
     */
285
    private function getTracksDetails($category, $categoryId)
286
    {
287
        $SQL = null;
288
        $favorite = false;
289
        $aTracks = array();
290
        $SQL_select = 'SELECT  `AT`.`id`, `AT`.`title`  AS `cl1`, `AA`.`name` AS `cl2`, `AB`.`name` AS `cl3`, `AT`.`length` AS `len`, `AT`.`file_id` AS `fid`, `AT`.`mimetype` AS `mim`, (CASE  WHEN `AB`.`cover` IS NOT NULL THEN `AB`.`id` ELSE NULL END) AS `cid`, LOWER(`AB`.`name`) AS `lower`';
291
        $SQL_from = ' FROM `*PREFIX*audioplayer_tracks` `AT`
292
					LEFT JOIN `*PREFIX*audioplayer_artists` `AA` ON `AT`.`artist_id` = `AA`.`id`
293
					LEFT JOIN `*PREFIX*audioplayer_albums` `AB` ON `AT`.`album_id` = `AB`.`id`';
294
        $SQL_order = ' ORDER BY LOWER(`AB`.`name`) ASC, `AT`.`disc` ASC, `AT`.`number` ASC';
295
296
        if ($category === 'Artist') {
297
            $SQL_select = 'SELECT  `AT`.`id`, `AT`.`title`  AS `cl1`, `AB`.`name` AS `cl2`, `AT`.`year` AS `cl3`, `AT`.`length` AS `len`, `AT`.`file_id` AS `fid`, `AT`.`mimetype` AS `mim`, (CASE  WHEN `AB`.`cover` IS NOT NULL THEN `AB`.`id` ELSE NULL END) AS `cid`, LOWER(`AB`.`name`) AS `lower`';
298
            $SQL = $SQL_select . $SQL_from .
299
                'WHERE  `AT`.`artist_id` = ? AND `AT`.`user_id` = ?' .
300
                $SQL_order;
301
        } elseif ($category === 'Genre') {
302
            $SQL = $SQL_select . $SQL_from .
303
                'WHERE `AT`.`genre_id` = ? AND `AT`.`user_id` = ?' .
304
                $SQL_order;
305
        } elseif ($category === 'Year') {
306
            $SQL = $SQL_select . $SQL_from .
307
                'WHERE `AT`.`year` = ? AND `AT`.`user_id` = ?' .
308
                $SQL_order;
309
        } elseif ($category === 'Title') {
310
            $SQL = $SQL_select . $SQL_from .
311
                'WHERE `AT`.`id` > ? AND `AT`.`user_id` = ?' .
312
                $SQL_order;
313
        } elseif ($category === 'Playlist' AND $categoryId === 'X1') { // Favorites
314
            $SQL = 'SELECT  `AT`.`id` , `AT`.`title`  AS `cl1`,`AA`.`name` AS `cl2`, `AB`.`name` AS `cl3`,`AT`.`length` AS `len`, `AT`.`file_id` AS `fid`, `AT`.`mimetype` AS `mim`, (CASE  WHEN `AB`.`cover` IS NOT NULL THEN `AB`.`id` ELSE NULL END) AS `cid`, LOWER(`AT`.`title`) AS `lower`' .
315
                $SQL_from .
316
                'WHERE `AT`.`id` <> ? AND `AT`.`user_id` = ?' .
317
                ' ORDER BY LOWER(`AT`.`title`) ASC';
318
            $categoryId = 0; //overwrite to integer for PostgreSQL
319
            $favorite = true;
320
        } elseif ($category === 'Playlist' AND $categoryId === 'X2') { // Recently Added
321
            $SQL = $SQL_select . $SQL_from .
322
                'WHERE `AT`.`id` <> ? AND `AT`.`user_id` = ? 
323
			 		ORDER BY `AT`.`file_id` DESC
324
			 		Limit 100';
325
            $categoryId = 0;
326
        } elseif ($category === 'Playlist' AND $categoryId === 'X3') { // Recently Played
327
            $SQL = $SQL_select . $SQL_from .
328
                'LEFT JOIN `*PREFIX*audioplayer_stats` `AS` ON `AT`.`id` = `AS`.`track_id`
329
			 		WHERE `AS`.`id` <> ? AND `AT`.`user_id` = ? 
330
			 		ORDER BY `AS`.`playtime` DESC
331
			 		Limit 50';
332
            $categoryId = 0;
333
        } elseif ($category === 'Playlist' AND $categoryId === 'X4') { // Most Played
334
            $SQL = $SQL_select . $SQL_from .
335
                'LEFT JOIN `*PREFIX*audioplayer_stats` `AS` ON `AT`.`id` = `AS`.`track_id`
336
			 		WHERE `AS`.`id` <> ? AND `AT`.`user_id` = ? 
337
			 		ORDER BY `AS`.`playcount` DESC
338
			 		Limit 25';
339
            $categoryId = 0;
340
        } elseif ($category === 'Playlist') {
341
            $SQL = $SQL_select . ' , `AP`.`sortorder`' .
342
                'FROM `*PREFIX*audioplayer_playlist_tracks` `AP` 
343
					LEFT JOIN `*PREFIX*audioplayer_tracks` `AT` ON `AP`.`track_id` = `AT`.`id`
344
					LEFT JOIN `*PREFIX*audioplayer_artists` `AA` ON `AT`.`artist_id` = `AA`.`id`
345
					LEFT JOIN `*PREFIX*audioplayer_albums` `AB` ON `AT`.`album_id` = `AB`.`id`
346
			 		WHERE  `AP`.`playlist_id` = ?
347
					AND `AT`.`user_id` = ? 
348
			 		ORDER BY `AP`.`sortorder` ASC';
349
        } elseif ($category === 'Stream') {
350
            $aTracks = $this->StreamParser(intval(substr($categoryId, 1)));
351
            return $aTracks;
352
353
        } elseif ($category === 'Folder') {
354
            $SQL = $SQL_select . $SQL_from .
355
                'WHERE `AT`.`folder_id` = ? AND `AT`.`user_id` = ?' .
356
                $SQL_order;
357
        } elseif ($category === 'Album') {
358
            $SQL_select = 'SELECT  `AT`.`id`, `AT`.`title` AS `cl1`, `AA`.`name` AS `cl2`, `AT`.`length` AS `len`, `AT`.`disc` AS `dsc`, `AT`.`file_id` AS `fid`, `AT`.`mimetype` AS `mim`, (CASE  WHEN `AB`.`cover` IS NOT NULL THEN `AB`.`id` ELSE NULL END) AS `cid`, LOWER(`AT`.`title`) AS `lower`,`AT`.`number`  AS `num`';
359
            $SQL = $SQL_select . $SQL_from .
360
                'WHERE `AB`.`id` = ? AND `AB`.`user_id` = ?' .
361
                ' ORDER BY `AT`.`disc` ASC, `AT`.`number` ASC';
362
        } elseif ($category === 'Album Artist') {
363
            $SQL = $SQL_select . $SQL_from .
364
                'WHERE  `AB`.`artist_id` = ? AND `AT`.`user_id` = ?' .
365
                $SQL_order;
366
        }
367
368
        $this->tagger = $this->tagManager->load('files');
369
        $favorites = $this->tagger->getFavorites();
370
371
        $stmt = $this->db->prepare($SQL);
372
        $stmt->execute(array($categoryId, $this->userId));
373
        $results = $stmt->fetchAll();
374
        foreach ($results as $row) {
375
            if ($category === 'Album') {
376
                $row['cl3'] = $row['dsc'] . '-' . $row['num'];
377
            }
378
            array_splice($row, 8, 1);
379
            //$nodes = $this->rootFolder->getUserFolder($this->userId)->getById($row['fid']);
380
            //$file = array_shift($nodes);
381
            //if ($file === null) {
382
            //    $this->logger->debug('removed/unshared file found => remove '.$row['fid'], array('app' => 'audioplayer'));
383
            //    $this->DBController->deleteFromDB($row['fid'], $this->userId);
384
            //    continue;
385
            //}
386
            if (is_array($favorites) AND in_array($row['fid'], $favorites)) {
387
                $row['fav'] = 't';
388
            }
389
390
            if ($favorite AND is_array($favorites) AND !in_array($row['fid'], $favorites)) {
391
                //special handling for Favorites smart playlist;
392
                //do not display anything that is NOT a fav
393
            } else {
394
                array_splice($row, 5, 1);
395
                $aTracks[] = $row;
396
            }
397
        }
398
        return $aTracks;
399
    }
400
401
    /**
402
     * Extract steam urls from playlist files
403
     *
404
     * @param integer $fileId
405
     * @return array
406
     * @throws InvalidPathException
407
     */
408
    private function StreamParser($fileId)
409
    {
410
        $tracks = array();
411
        $x = 0;
412
        $title = null;
413
        $userView = $this->rootFolder->getUserFolder($this->userId);
414
        //$this->logger->debug('removed/unshared file found => remove '.$row['fid'], array('app' => 'audioplayer'));
415
416
        $streamfile = $userView->getById($fileId);
417
        $file_type = $streamfile[0]->getMimetype();
418
        $file_content = $streamfile[0]->getContent();
0 ignored issues
show
Bug introduced by
The method getContent() does not exist on OCP\Files\Node. It seems like you code against a sub-type of OCP\Files\Node such as OCP\Files\File. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

418
        /** @scrutinizer ignore-call */ 
419
        $file_content = $streamfile[0]->getContent();
Loading history...
419
420
        if ($file_type === 'audio/x-scpls') {
421
            $stream_data = parse_ini_string($file_content, true, INI_SCANNER_RAW);
422
            $stream_rows = isset($stream_data['playlist']['NumberOfEntries']) ? $stream_data['playlist']['NumberOfEntries'] : $stream_data['playlist']['numberofentries'];
423
            for ($i = 1; $i <= $stream_rows; $i++) {
424
                $title = $stream_data['playlist']['Title' . $i];
425
                $file = $stream_data['playlist']['File' . $i];
426
                preg_match_all('#\bhttps?://[^,\s()<>]+(?:\([\w\d]+\)|([^,[:punct:]\s]|/))#', $file, $matches);
427
428
                if ($matches[0]) {
429
                    $row = array();
430
                    $row['id'] = $fileId . $i;
431
                    $row['cl1'] = $matches[0][0];
432
                    $row['cl2'] = '';
433
                    $row['cl3'] = '';
434
                    $row['len'] = '';
435
                    $row['mim'] = $file_type;
436
                    $row['cid'] = '';
437
                    $row['lin'] = $matches[0][0];
438
                    if ($title) $row['cl1'] = $title;
439
                    $tracks[] = $row;
440
                }
441
            }
442
        } else {
443
            foreach (preg_split("/((\r?\n)|(\r\n?))/", $file_content) as $line) {
444
                if (empty($line)) continue;
445
                if (substr($line, 0, 8) === '#EXTINF:') {
446
                    $extinf = explode(',', substr($line, 8));
447
                    $title = $extinf[1];
448
                }
449
                preg_match_all('#\bhttps?://[^,\s()<>]+(?:\([\w\d]+\)|([^,[:punct:]\s]|/))#', $line, $matches);
450
451
                if ($matches[0]) {
452
                    $x++;
453
                    $row = array();
454
                    $row['id'] = $fileId . $x;
455
                    $row['cl1'] = $matches[0][0];
456
                    $row['cl2'] = '';
457
                    $row['cl3'] = '';
458
                    $row['len'] = '';
459
                    $row['mim'] = $file_type;
460
                    $row['cid'] = '';
461
                    $row['lin'] = $matches[0][0];
462
                    if ($title) $row['cl1'] = $title;
463
                    $title = null;
464
                    $tracks[] = $row;
465
                } elseif (preg_match('/^[^"<>|:]*$/',$line)) {
466
                    $path = explode('/', $streamfile[0]->getInternalPath());
467
                    array_shift($path);
468
                    array_pop($path);
469
                    array_push($path, $line);
470
                    $path = implode('/', $path);
471
                    $x++;
472
473
                    try {
474
                        $fileId = $this->rootFolder->getUserFolder($this->userId)->get($path)->getId();
475
                        $track = $this->DBController->getTrackInfo(null,$fileId);
476
477
                        $row = array();
478
                        $row['id'] = $track['id'];
479
                        $row['cl1'] = $track['Title'];
480
                        $row['cl2'] = $track['Artist'];
481
                        $row['cl3'] = $track['Album'];
482
                        $row['len'] = $track['Length'];
483
                        $row['mim'] = $track['MIME type'];
484
                        $row['cid'] = '';
485
                        $row['lin'] = $track['id'];
486
                        $row['fav'] = $track['fav'];
487
                        if ($title) $row['cl1'] = $title;
488
                        $tracks[] = $row;
489
                        $title = null;
490
                    } catch (NotFoundException $e) {
491
                        //File is not known in the filecache and will be ignored;
492
                    }
493
                }
494
            }
495
        }
496
        return $tracks;
497
    }
498
499
    /**
500
     * Get selection dependend headers for the list view
501
     *
502
     * @param string $category
503
     * @return array
504
     */
505
    private function getListViewHeaders($category)
506
    {
507
        if ($category === 'Artist') {
508
            return ['col1' => $this->l10n->t('Title'), 'col2' => $this->l10n->t('Album'), 'col3' => $this->l10n->t('Year'), 'col4' => $this->l10n->t('Length')];
509
        } elseif ($category === 'Album') {
510
            return ['col1' => $this->l10n->t('Title'), 'col2' => $this->l10n->t('Artist'), 'col3' => $this->l10n->t('Disc') . '-' . $this->l10n->t('Track'), 'col4' => $this->l10n->t('Length')];
511
        } elseif ($category === 'x_Stream') { //temporary disabled; need to separate streams and playlists
512
            return ['col1' => $this->l10n->t('URL'), 'col2' => $this->l10n->t(''), 'col3' => $this->l10n->t(''), 'col4' => $this->l10n->t('')];
513
        } else {
514
            return ['col1' => $this->l10n->t('Title'), 'col2' => $this->l10n->t('Artist'), 'col3' => $this->l10n->t('Album'), 'col4' => $this->l10n->t('Length')];
515
        }
516
    }
517
}