Passed
Push — master ( 5f0765...81482e )
by Marcel
02:18
created

CategoryController::getTrackCount()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 20
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 16
c 1
b 0
f 0
nc 2
nop 2
dl 0
loc 20
rs 9.7333
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` , `AA`.`id` AS `art`, `AB`.`cover` AS `cid` 
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
                if ($category === 'Album') {
161
                    $row['art'] = $this->DBController->loadArtistsToAlbum($row['id'], $row['art']);
162
                    if ($row['cid'] !== null) {
163
                        $row['cid'] = $row['id'];
164
                    } else {
165
                        $row['cid'] = '';
166
                    }
167
                }
168
                array_splice($row, 2, 1);
169
                if ($row['name'] === '0' OR $row['name'] === '') $row['name'] = $this->l10n->t('Unknown');
170
                $row['cnt'] = $this->getTrackCount($category, $row['id']);
171
                $aPlaylists[] = $row;
172
            }
173
        }
174
175
        $result = empty($aPlaylists) ? [
176
            'status' => 'nodata'
177
        ] : [
178
            'status' => 'success',
179
            'data' => $aPlaylists
180
        ];
181
        $response = new JSONResponse();
182
        $response->setData($result);
183
        return $response;
184
    }
185
186
    /**
187
     * Get the covers for the "Album Covers" view
188
     *
189
     * @NoAdminRequired
190
     * @param $category
191
     * @param $categoryId
192
     * @return JSONResponse
193
     */
194
    public function getCategoryItemCovers($category, $categoryId)
195
    {
196
        $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`');
197
198
        $aPlaylists = array();
199
        $SQL = 'SELECT  `AB`.`id` , `AB`.`name`, LOWER(`AB`.`name`) AS `lower` , `AA`.`id` AS `art`, `AB`.`cover` AS `cid`';
200
        $SQL .= ' FROM `*PREFIX*audioplayer_tracks` `AT`';
201
        $SQL .= ' LEFT JOIN `*PREFIX*audioplayer_artists` `AA` ON `AT`.`artist_id` = `AA`.`id`';
202
        $SQL .= ' LEFT JOIN `*PREFIX*audioplayer_albums` `AB` ON `AT`.`album_id` = `AB`.`id`';
203
        $SQL .= ' WHERE `AT`.`user_id` = ? ';
204
        if ($categoryId) $SQL .= 'AND ' . $whereMatching[$category] . '= ?';
205
        $SQL .= ' GROUP BY `AB`.`id` ORDER BY LOWER(`AB`.`name`) ASC';
206
207
        if (isset($SQL)) {
208
            $stmt = $this->db->prepare($SQL);
209
            if ($categoryId) {
210
                $stmt->execute(array($this->userId, $categoryId));
211
            } else {
212
                $stmt->execute(array($this->userId));
213
            }
214
            $results = $stmt->fetchAll();
215
            foreach ($results as $row) {
216
                $row['art'] = $this->DBController->loadArtistsToAlbum($row['id'], $row['art']);
217
                if ($row['cid'] !== null) {
218
                    $row['cid'] = $row['id'];
219
                } else {
220
                    $row['cid'] = '';
221
                }
222
                array_splice($row, 2, 1);
223
                if ($row['name'] === '0' OR $row['name'] === '') $row['name'] = $this->l10n->t('Unknown');
224
                $aPlaylists[] = $row;
225
            }
226
        }
227
        $result = empty($aPlaylists) ? [
228
            'status' => 'nodata'
229
        ] : [
230
            'status' => 'success',
231
            'data' => $aPlaylists
232
        ];
233
        $response = new JSONResponse();
234
        $response->setData($result);
235
        return $response;
236
    }
237
238
    /**
239
     * Get the number of tracks for a category item
240
     *
241
     * @param string $category
242
     * @param integer $categoryId
243
     * @return integer
244
     */
245
    private function getTrackCount($category, $categoryId)
246
    {
247
        $SQL = array();
248
        $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` = ?';
249
        $SQL['Genre'] = 'SELECT COUNT(`AT`.`id`) AS `count` FROM `*PREFIX*audioplayer_tracks` `AT` WHERE  `AT`.`genre_id` = ? AND `AT`.`user_id` = ?';
250
        $SQL['Year'] = 'SELECT COUNT(`AT`.`id`) AS `count` FROM `*PREFIX*audioplayer_tracks` `AT` WHERE  `AT`.`year` = ? AND `AT`.`user_id` = ?';
251
        $SQL['Title'] = 'SELECT COUNT(`AT`.`id`) AS `count` FROM `*PREFIX*audioplayer_tracks` `AT` WHERE  `AT`.`id` > ? AND `AT`.`user_id` = ?';
252
        $SQL['Playlist'] = 'SELECT COUNT(`AP`.`track_id`) AS `count` FROM `*PREFIX*audioplayer_playlist_tracks` `AP` WHERE  `AP`.`playlist_id` = ?';
253
        $SQL['Folder'] = 'SELECT COUNT(`AT`.`id`) AS `count` FROM `*PREFIX*audioplayer_tracks` `AT` WHERE  `AT`.`folder_id` = ? AND `AT`.`user_id` = ?';
254
        $SQL['Album'] = 'SELECT COUNT(`AT`.`id`) AS `count` FROM `*PREFIX*audioplayer_tracks` `AT` WHERE  `AT`.`album_id` = ? AND `AT`.`user_id` = ?';
255
        $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` = ?';
256
257
        $stmt = $this->db->prepare($SQL[$category]);
258
        if ($category === 'Playlist') {
259
            $stmt->execute(array($categoryId));
260
        } else {
261
            $stmt->execute(array($categoryId, $this->userId));
262
        }
263
        $results = $stmt->fetch();
264
        return $results['count'];
265
    }
266
267
    /**
268
     * get the tracks for a selected category or album
269
     *
270
     * @NoAdminRequired
271
     * @param string $category
272
     * @param string $categoryId
273
     * @return JSONResponse
274
     * @throws InvalidPathException
275
     * @throws NotFoundException
276
     */
277
    public function getTracks($category, $categoryId)
278
    {
279
        if ($categoryId[0] === 'S') $category = 'Stream';
280
        if ($categoryId[0] === 'P') $category = 'Playlist';
281
        $items = $this->getTracksDetails($category, $categoryId);
282
        $headers = $this->getListViewHeaders($category);
283
284
        $result = !empty($items) ? [
285
            'status' => 'success',
286
            'data' => $items,
287
            'header' => $headers,
288
        ] : [
289
            'status' => 'nodata',
290
        ];
291
        $response = new JSONResponse();
292
        $response->setData($result);
293
        return $response;
294
    }
295
296
    /**
297
     * Get the tracks for a selected category or album
298
     *
299
     * @param string $category
300
     * @param string $categoryId
301
     * @return array
302
     * @throws InvalidPathException
303
     */
304
    private function getTracksDetails($category, $categoryId)
305
    {
306
        $SQL = null;
307
        $favorite = false;
308
        $aTracks = array();
309
        $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`, `AB`.`id` AS `cid`, `AB`.`cover`, LOWER(`AB`.`name`) AS `lower`';
310
        $SQL_from = ' FROM `*PREFIX*audioplayer_tracks` `AT`
311
					LEFT JOIN `*PREFIX*audioplayer_artists` `AA` ON `AT`.`artist_id` = `AA`.`id`
312
					LEFT JOIN `*PREFIX*audioplayer_albums` `AB` ON `AT`.`album_id` = `AB`.`id`';
313
        $SQL_order = ' ORDER BY LOWER(`AB`.`name`) ASC, `AT`.`disc` ASC, `AT`.`number` ASC';
314
315
        if ($category === 'Artist') {
316
            $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`, `AB`.`id` AS `cid`, `AB`.`cover`, LOWER(`AB`.`name`) AS `lower`';
317
            $SQL = $SQL_select . $SQL_from .
318
                'WHERE  `AT`.`artist_id` = ? AND `AT`.`user_id` = ?' .
319
                $SQL_order;
320
        } elseif ($category === 'Genre') {
321
            $SQL = $SQL_select . $SQL_from .
322
                'WHERE `AT`.`genre_id` = ? AND `AT`.`user_id` = ?' .
323
                $SQL_order;
324
        } elseif ($category === 'Year') {
325
            $SQL = $SQL_select . $SQL_from .
326
                'WHERE `AT`.`year` = ? AND `AT`.`user_id` = ?' .
327
                $SQL_order;
328
        } elseif ($category === 'Title') {
329
            $SQL = $SQL_select . $SQL_from .
330
                'WHERE `AT`.`id` > ? AND `AT`.`user_id` = ?' .
331
                $SQL_order;
332
        } elseif ($category === 'Playlist' AND $categoryId === 'X1') { // Favorites
333
            $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`, `AB`.`id` AS `cid`, `AB`.`cover`, LOWER(`AT`.`title`) AS `lower`' .
334
                $SQL_from .
335
                'WHERE `AT`.`id` <> ? AND `AT`.`user_id` = ?' .
336
                ' ORDER BY LOWER(`AT`.`title`) ASC';
337
            $categoryId = 0; //overwrite to integer for PostgreSQL
338
            $favorite = true;
339
        } elseif ($category === 'Playlist' AND $categoryId === 'X2') { // Recently Added
340
            $SQL = $SQL_select . $SQL_from .
341
                'WHERE `AT`.`id` <> ? AND `AT`.`user_id` = ? 
342
			 		ORDER BY `AT`.`file_id` DESC
343
			 		Limit 100';
344
            $categoryId = 0;
345
        } elseif ($category === 'Playlist' AND $categoryId === 'X3') { // Recently Played
346
            $SQL = $SQL_select . $SQL_from .
347
                'LEFT JOIN `*PREFIX*audioplayer_stats` `AS` ON `AT`.`id` = `AS`.`track_id`
348
			 		WHERE `AS`.`id` <> ? AND `AT`.`user_id` = ? 
349
			 		ORDER BY `AS`.`playtime` DESC
350
			 		Limit 50';
351
            $categoryId = 0;
352
        } elseif ($category === 'Playlist' AND $categoryId === 'X4') { // Most Played
353
            $SQL = $SQL_select . $SQL_from .
354
                'LEFT JOIN `*PREFIX*audioplayer_stats` `AS` ON `AT`.`id` = `AS`.`track_id`
355
			 		WHERE `AS`.`id` <> ? AND `AT`.`user_id` = ? 
356
			 		ORDER BY `AS`.`playcount` DESC
357
			 		Limit 25';
358
            $categoryId = 0;
359
        } elseif ($category === 'Playlist') {
360
            $SQL = $SQL_select . ' , `AP`.`sortorder`' .
361
                'FROM `*PREFIX*audioplayer_playlist_tracks` `AP` 
362
					LEFT JOIN `*PREFIX*audioplayer_tracks` `AT` ON `AP`.`track_id` = `AT`.`id`
363
					LEFT JOIN `*PREFIX*audioplayer_artists` `AA` ON `AT`.`artist_id` = `AA`.`id`
364
					LEFT JOIN `*PREFIX*audioplayer_albums` `AB` ON `AT`.`album_id` = `AB`.`id`
365
			 		WHERE  `AP`.`playlist_id` = ?
366
					AND `AT`.`user_id` = ? 
367
			 		ORDER BY `AP`.`sortorder` ASC';
368
        } elseif ($category === 'Stream') {
369
            $aTracks = $this->StreamParser(intval(substr($categoryId, 1)));
370
            return $aTracks;
371
372
        } elseif ($category === 'Folder') {
373
            $SQL = $SQL_select . $SQL_from .
374
                'WHERE `AT`.`folder_id` = ? AND `AT`.`user_id` = ?' .
375
                $SQL_order;
376
        } elseif ($category === 'Album') {
377
            $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`, `AB`.`id` AS `cid`, `AB`.`cover`, LOWER(`AT`.`title`) AS `lower`,`AT`.`number`  AS `num`';
378
            $SQL = $SQL_select . $SQL_from .
379
                'WHERE `AB`.`id` = ? AND `AB`.`user_id` = ?' .
380
                ' ORDER BY `AT`.`disc` ASC, `AT`.`number` ASC';
381
        } elseif ($category === 'Album Artist') {
382
            $SQL = $SQL_select . $SQL_from .
383
                'WHERE  `AB`.`artist_id` = ? AND `AT`.`user_id` = ?' .
384
                $SQL_order;
385
        }
386
387
        $this->tagger = $this->tagManager->load('files');
388
        $favorites = $this->tagger->getFavorites();
389
390
        $stmt = $this->db->prepare($SQL);
391
        $stmt->execute(array($categoryId, $this->userId));
392
        $results = $stmt->fetchAll();
393
        foreach ($results as $row) {
394
            if ($row['cover'] === null) {
395
                $row['cid'] = '';
396
            }
397
            if ($category === 'Album') {
398
                $row['cl3'] = $row['dsc'] . '-' . $row['num'];
399
            }
400
            array_splice($row, 8, 3);
401
            $nodes = $this->rootFolder->getUserFolder($this->userId)->getById($row['fid']);
402
            $file = array_shift($nodes);
403
            if ($file === null) {
404
                $this->logger->debug('removed/unshared file found => remove '.$row['fid'], array('app' => 'audioplayer'));
405
                $this->DBController->deleteFromDB($row['fid'], $this->userId);
406
                continue;
407
            }
408
            $path = $file->getPath();
409
            $segments = explode('/', trim($path, '/'), 3);
410
            $row['lin'] = rawurlencode($segments[2]);
411
            if (is_array($favorites) AND in_array($row['fid'], $favorites)) {
412
                $row['fav'] = 't';
413
            }
414
415
            if ($favorite AND is_array($favorites) AND !in_array($row['fid'], $favorites)) {
416
                //special handling for Favorites smart playlist;
417
                //do not display anything that is NOT a fav
418
            } else {
419
                array_splice($row, 5, 1);
420
                $aTracks[] = $row;
421
            }
422
        }
423
        return $aTracks;
424
    }
425
426
    /**
427
     * Extract steam urls from playlist files
428
     *
429
     * @param integer $fileId
430
     * @return array
431
     * @throws InvalidPathException
432
     */
433
    private function StreamParser($fileId)
434
    {
435
        $tracks = array();
436
        $x = 0;
437
        $title = null;
438
        $userView = $this->rootFolder->getUserFolder($this->userId);
439
        //$this->logger->debug('removed/unshared file found => remove '.$row['fid'], array('app' => 'audioplayer'));
440
441
        $streamfile = $userView->getById($fileId);
442
        $file_type = $streamfile[0]->getMimetype();
443
        $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

443
        /** @scrutinizer ignore-call */ 
444
        $file_content = $streamfile[0]->getContent();
Loading history...
444
445
        if ($file_type === 'audio/x-scpls') {
446
            $stream_data = parse_ini_string($file_content, true, INI_SCANNER_RAW);
447
            $stream_rows = isset($stream_data['playlist']['NumberOfEntries']) ? $stream_data['playlist']['NumberOfEntries'] : $stream_data['playlist']['numberofentries'];
448
            for ($i = 1; $i <= $stream_rows; $i++) {
449
                $title = $stream_data['playlist']['Title' . $i];
450
                $file = $stream_data['playlist']['File' . $i];
451
                preg_match_all('#\bhttps?://[^,\s()<>]+(?:\([\w\d]+\)|([^,[:punct:]\s]|/))#', $file, $matches);
452
453
                if ($matches[0]) {
454
                    $row = array();
455
                    $row['id'] = $fileId . $i;
456
                    $row['fid'] = $fileId . $i;
457
                    $row['cl1'] = $matches[0][0];
458
                    $row['cl2'] = '';
459
                    $row['cl3'] = '';
460
                    $row['len'] = '';
461
                    $row['mim'] = $file_type;
462
                    $row['cid'] = '';
463
                    $row['lin'] = $matches[0][0];
464
                    $row['fav'] = 'f';
465
                    if ($title) $row['cl1'] = $title;
466
                    $tracks[] = $row;
467
                }
468
            }
469
        } else {
470
            foreach (preg_split("/((\r?\n)|(\r\n?))/", $file_content) as $line) {
471
                if (empty($line)) continue;
472
                if (substr($line, 0, 8) === '#EXTINF:') {
473
                    $extinf = explode(',', substr($line, 8));
474
                    $title = $extinf[1];
475
                }
476
                preg_match_all('#\bhttps?://[^,\s()<>]+(?:\([\w\d]+\)|([^,[:punct:]\s]|/))#', $line, $matches);
477
478
                if ($matches[0]) {
479
                    $x++;
480
                    $row = array();
481
                    $row['id'] = $fileId . $x;
482
                    $row['fid'] = $fileId . $x;
483
                    $row['cl1'] = $matches[0][0];
484
                    $row['cl2'] = '';
485
                    $row['cl3'] = '';
486
                    $row['len'] = '';
487
                    $row['mim'] = $file_type;
488
                    $row['cid'] = '';
489
                    $row['lin'] = $matches[0][0];
490
                    $row['fav'] = 'f';
491
                    if ($title) $row['cl1'] = $title;
492
                    $title = null;
493
                    $tracks[] = $row;
494
                } elseif (preg_match('/^[^"<>|:]*$/',$line)) {
495
                    $path = explode('/', $streamfile[0]->getInternalPath());
496
                    array_shift($path);
497
                    array_pop($path);
498
                    array_push($path, $line);
499
                    $path = implode('/', $path);
500
                    $x++;
501
502
                    try {
503
                        $fileId = $this->rootFolder->getUserFolder($this->userId)->get($path)->getId();
504
                        $track = $this->DBController->getTrackInfo(null,$fileId);
505
506
                        $row = array();
507
                        $row['id'] = $track['id'];
508
                        $row['fid'] = $track['file_id'];
509
                        $row['cl1'] = $track['Title'];
510
                        $row['cl2'] = $track['Artist'];
511
                        $row['cl3'] = $track['Album'];
512
                        $row['len'] = $track['Length'];
513
                        $row['mim'] = $track['MIME type'];
514
                        $row['cid'] = '';
515
                        $row['lin'] = rawurlencode($path);
516
                        $row['fav'] = $track['fav'];
517
                        if ($title) $row['cl1'] = $title;
518
                        $tracks[] = $row;
519
                        $title = null;
520
                    } catch (NotFoundException $e) {
521
                        //File is not known in the filecache and will be ignored;
522
                    }
523
                }
524
            }
525
        }
526
        return $tracks;
527
    }
528
529
    /**
530
     * Get selection dependend headers for the list view
531
     *
532
     * @param string $category
533
     * @return array
534
     */
535
    private function getListViewHeaders($category)
536
    {
537
        if ($category === 'Artist') {
538
            return ['col1' => $this->l10n->t('Title'), 'col2' => $this->l10n->t('Album'), 'col3' => $this->l10n->t('Year'), 'col4' => $this->l10n->t('Length')];
539
        } elseif ($category === 'Album') {
540
            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')];
541
        } elseif ($category === 'x_Stream') { //temporary disabled; need to separate streams and playlists
542
            return ['col1' => $this->l10n->t('URL'), 'col2' => $this->l10n->t(''), 'col3' => $this->l10n->t(''), 'col4' => $this->l10n->t('')];
543
        } else {
544
            return ['col1' => $this->l10n->t('Title'), 'col2' => $this->l10n->t('Artist'), 'col3' => $this->l10n->t('Album'), 'col4' => $this->l10n->t('Length')];
545
        }
546
    }
547
}