forked from WWBN/AVideo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplaylist.php
541 lines (491 loc) · 20.3 KB
/
playlist.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
<?php
global $global, $config, $refreshCacheFromPlaylist;
$refreshCacheFromPlaylist = false; // this is because it was creating playlists multiple times
if (!isset($global['systemRootPath'])) {
require_once '../videos/configuration.php';
}
require_once $global['systemRootPath'] . 'objects/user.php';
class PlayList extends ObjectYPT {
protected $id, $name, $users_id, $status;
static $validStatus = array('public', 'private', 'unlisted', 'favorite', 'watch_later');
static function getSearchFieldsNames() {
return array('pl.name');
}
static function getTableName() {
return 'playlists';
}
static protected function getFromDbFromName($name) {
global $global;
$sql = "SELECT * FROM " . static::getTableName() . " WHERE name = ? users_id = " . User::getId() . " LIMIT 1";
$res = sqlDAL::readSql($sql, "s", array($name));
$data = sqlDAL::fetchAssoc($res);
sqlDAL::close($res);
if ($res) {
$row = $data;
} else {
$row = false;
}
return $row;
}
function loadFromName($name) {
if (!User::isLogged()) {
return false;
}
$this->setName($name);
$row = self::getFromDbFromName($this->getName());
if (empty($row))
return false;
foreach ($row as $key => $value) {
$this->$key = $value;
}
return true;
}
/**
*
* @global type $global
* @param type $publicOnly
* @param type $userId if not present check session
* @param type $isVideoIdPresent pass the ID of the video checking
* @return boolean
*/
static function getAllFromUser($userId, $publicOnly = true, $status = false, $playlists_id = 0, $try=0) {
global $global, $config, $refreshCacheFromPlaylist;
$playlists_id = intval($playlists_id);
$formats = "";
$values = array();
$sql = "SELECT u.*, pl.* FROM " . static::getTableName() . " pl "
. " LEFT JOIN users u ON u.id = users_id WHERE 1=1 ";
if (!empty($playlists_id)) {
$sql .= " AND pl.id = '{$playlists_id}' ";
}
if (!empty($status)) {
$status = str_replace("'", "", $status);
$sql .= " AND pl.status = '{$status}' ";
} else
if ($publicOnly) {
$sql .= " AND pl.status = 'public' ";
}
if (!empty($userId)) {
$sql .= " AND users_id = ? ";
$formats .= "i";
$values[] = $userId;
}
$sql .= self::getSqlFromPost("pl.");
//echo $sql, $userId;exit;
$res = sqlDAL::readSql($sql, $formats, $values, $refreshCacheFromPlaylist);
$fullData = sqlDAL::fetchAllAssoc($res);
sqlDAL::close($res);
$rows = array();
$favorite = array();
$watch_later = array();
$favoriteCount = 0;
$watch_laterCount = 0;
if ($res != false) {
foreach ($fullData as $row) {
$row['videos'] = static::getVideosFromPlaylist($row['id']);
$row['isFavorite'] = false;
$row['isWatchLater'] = false;
if ($row['status'] === "favorite") {
$row['isFavorite'] = true;
$favoriteCount++;
$favorite = $row;
} else if ($row['status'] === "watch_later") {
$watch_laterCount++;
$row['isWatchLater'] = true;
$watch_later = $row;
} else {
$rows[] = $row;
}
}
if(!empty($userId)){
if($try==0 && ($favoriteCount>1 || $watch_laterCount > 1)){
self::fixDuplicatePlayList($userId);
$refreshCacheFromPlaylist = true;
return self::getAllFromUser($userId, $publicOnly, $status, $playlists_id, $try+1);
}
if (empty($_POST['current']) && empty($status) && $config->currentVersionGreaterThen("6.4")) {
if (empty($favorite)) {
$pl = new PlayList(0);
$pl->setName("Favorite");
$pl->setStatus("favorite");
$pl->setUsers_id($userId);
$id = $pl->save();
$refreshCacheFromPlaylist = true;
$row['id'] = $id;
$row['name'] = $pl->getName();
$row['status'] = $pl->getStatus();
$row['users_id'] = $pl->getUsers_id();
$favorite = $row;
}
if (empty($watch_later)) {
$pl = new PlayList(0);
$pl->setName("Watch Later");
$pl->setStatus("watch_later");
$pl->setUsers_id($userId);
$id = $pl->save();
$refreshCacheFromPlaylist = true;
$row['id'] = $id;
$row['name'] = $pl->getName();
$row['status'] = $pl->getStatus();
$row['users_id'] = $pl->getUsers_id();
$watch_later = $row;
}
}
}
if (!empty($favorite)) {
array_unshift($rows, $favorite);
}
if (!empty($watch_later)) {
array_unshift($rows, $watch_later);
}
} else {
die($sql . '\nError : (' . $global['mysqli']->errno . ') ' . $global['mysqli']->error);
}
return $rows;
}
static function fixDuplicatePlayList($user_id) {
if(empty($user_id)){
return false;
}
_error_log("PlayList::fixDuplicatePlayList Process user_id = {$user_id} favorite");
$sql = "SELECT * FROM playlists WHERE users_id = ? AND status = 'favorite' ORDER BY created ";
$res = sqlDAL::readSql($sql, "i", array($user_id), true);
$fullData = sqlDAL::fetchAllAssoc($res);
sqlDAL::close($res);
$rows = array();
if ($res != false) {
foreach ($fullData as $key => $row) {
if ($key === 0) {
continue;
}
if (!empty(PlayList::getVideosIDFromPlaylistLight($row['id']))) {
_error_log("PlayList::fixDuplicatePlayList favorite PlayList NOT empty {$row['id']}");
continue;
}
$sql = "DELETE FROM playlists ";
$sql .= " WHERE id = ?";
_error_log("PlayList::fixDuplicatePlayList favorite {$row['id']}");
sqlDAL::writeSql($sql, "i", array($row['id']));
}
}
_error_log("PlayList::fixDuplicatePlayList Process user_id = {$user_id} watch_later");
$sql = "SELECT * FROM playlists WHERE users_id = ? AND status = 'watch_later' ORDER BY created ";
$res = sqlDAL::readSql($sql, "i", array($user_id), true);
$fullData = sqlDAL::fetchAllAssoc($res);
sqlDAL::close($res);
$rows = array();
if ($res != false) {
foreach ($fullData as $key => $row) {
if ($key === 0) {
continue;
}
if (!empty(PlayList::getVideosIDFromPlaylistLight($row['id']))) {
_error_log("PlayList::fixDuplicatePlayList watch_later PlayList NOT empty {$row['id']}");
continue;
}
$sql = "DELETE FROM playlists ";
$sql .= " WHERE id = ?";
_error_log("PlayList::fixDuplicatePlayList watch_later {$row['id']}");
ob_flush();
sqlDAL::writeSql($sql, "i", array($row['id']));
}
}
}
static function getAllFromUserVideo($userId, $videos_id, $publicOnly = true, $status = false) {
if (empty($_SESSION['getAllFromUserVideo'][$videos_id][$userId][intval($publicOnly)][intval($status)])) {
$rows = self::getAllFromUser($userId, $publicOnly, $status);
foreach ($rows as $key => $value) {
$videos = self::getVideosIdFromPlaylist($value['id']);
$rows[$key]['isOnPlaylist'] = in_array($videos_id, $videos);
}
_session_start();
$_SESSION['getAllFromUserVideo'][$videos_id][$userId][intval($publicOnly)][intval($status)] = $rows;
} else {
$rows = $_SESSION['getAllFromUserVideo'][$videos_id][$userId][intval($publicOnly)][intval($status)];
}
return $rows;
}
static private function removeCache($videos_id) {
$close = false;
_session_start();
unset($_SESSION['getAllFromUserVideo'][$videos_id]);
}
static function getVideosIDFromPlaylistLight($playlists_id) {
global $global;
$sql = "SELECT * FROM playlists_has_videos p "
. " WHERE playlists_id = ? ";
$sort = @$_POST['sort'];
$_POST['sort'] = array();
$_POST['sort']['p.`order`'] = 'ASC';
$sql .= self::getSqlFromPost();
$_POST['sort'] = $sort;
$res = sqlDAL::readSql($sql, "i", array($playlists_id));
$fullData = sqlDAL::fetchAllAssoc($res);
sqlDAL::close($res);
$rows = array();
if ($res != false) {
foreach ($fullData as $row) {
$rows[] = $row;
}
} else {
die($sql . '\nError : (' . $global['mysqli']->errno . ') ' . $global['mysqli']->error);
}
return $rows;
}
public static function setCache($name, $value) {
parent::setCache($name, $value);
}
static function getVideosFromPlaylist($playlists_id) {
$rows = self::getCache("getVideosFromPlaylist{$playlists_id}", 0);
if (empty($rows)) {
global $global;
$sql = "SELECT *,v.created as cre, p.`order` as video_order, v.externalOptions as externalOptions "
. ", (SELECT count(id) FROM likes as l where l.videos_id = v.id AND `like` = 1 ) as likes "
. " FROM playlists_has_videos p "
. " LEFT JOIN videos as v ON videos_id = v.id "
. " LEFT JOIN users u ON u.id = v.users_id "
. " WHERE playlists_id = ? AND v.status != 'i' ";
$sort = @$_POST['sort'];
$_POST['sort'] = array();
$_POST['sort']['p.`order`'] = 'ASC';
$sql .= self::getSqlFromPost();
$_POST['sort'] = $sort;
$res = sqlDAL::readSql($sql, "i", array($playlists_id));
$fullData = sqlDAL::fetchAllAssoc($res);
sqlDAL::close($res);
$rows = array();
$SubtitleSwitcher = AVideoPlugin::loadPluginIfEnabled("SubtitleSwitcher");
if ($res != false) {
$timeLog2 = __FILE__ . " - getVideosFromPlaylist: {$playlists_id}";
TimeLogStart($timeLog2);
foreach ($fullData as $row) {
if (!empty($_GET['isChannel'])) {
$row['tags'] = Video::getTags($row['id']);
$row['pluginBtns'] = AVideoPlugin::getPlayListButtons($playlists_id);
$row['humancreate'] = humanTiming(strtotime($row['cre']));
}
TimeLogEnd($timeLog2, __LINE__);
$images = Video::getImageFromFilename($row['filename'], $row['type']);
TimeLogEnd($timeLog2, __LINE__);
$row['images'] = $images;
$row['videos'] = Video::getVideosPaths($row['filename'], true);
TimeLogEnd($timeLog2, __LINE__);
$row['progress'] = Video::getVideoPogressPercent($row['videos_id']);
TimeLogEnd($timeLog2, __LINE__);
$row['title'] = UTF8encode($row['title']);
TimeLogEnd($timeLog2, __LINE__);
$row['description'] = UTF8encode($row['description']);
TimeLogEnd($timeLog2, __LINE__);
$row['tags'] = Video::getTags($row['videos_id']);
TimeLogEnd($timeLog2, __LINE__);
if (AVideoPlugin::isEnabledByName("VideoTags")) {
$row['videoTags'] = Tags::getAllFromVideosId($row['videos_id']);
$row['videoTagsObject'] = Tags::getObjectFromVideosId($row['videos_id']);
}
TimeLogEnd($timeLog2, __LINE__);
if ($SubtitleSwitcher) {
$row['subtitles'] = getVTTTracks($row['filename'], true);
}
TimeLogEnd($timeLog2, __LINE__);
unset($row['password']);
unset($row['recoverPass']);
//unset($row['description']);
$rows[] = $row;
}
self::setCache("getVideosFromPlaylist{$playlists_id}", $rows);
} else {
die($sql . '\nError : (' . $global['mysqli']->errno . ') ' . $global['mysqli']->error);
}
} else {
$rows = object_to_array($rows);
}
return $rows;
}
static function isVideoOnFavorite($videos_id, $users_id) {
return self::isVideoOn($videos_id, $users_id, 'favorite');
}
static function isVideoOnWatchLater($videos_id, $users_id) {
return self::isVideoOn($videos_id, $users_id, 'watch_later');
}
private static function isVideoOn($videos_id, $users_id, $status) {
global $global;
$status = str_replace("'", "", $status);
$sql = "SELECT pl.id FROM " . static::getTableName() . " pl "
. " LEFT JOIN users u ON u.id = users_id "
. " LEFT JOIN playlists_has_videos p ON pl.id = playlists_id"
. " LEFT JOIN videos as v ON videos_id = v.id "
. " WHERE videos_id = ? AND pl.users_id = ? AND pl.status = '{$status}' LIMIT 1 ";
//echo $videos_id," - " ,$users_id, $sql;
$res = sqlDAL::readSql($sql, "ii", array($videos_id, $users_id));
$data = sqlDAL::fetchAssoc($res);
sqlDAL::close($res);
if ($res) {
$row = $data;
} else {
$row = false;
}
return $row;
}
static function getFavoriteIdFromUser($users_id) {
global $refreshCacheFromPlaylist;
$favorite = self::getIdFromUser($users_id, "favorite");
if (empty($favorite)) {
$pl = new PlayList(0);
$pl->setName("Favorite");
$pl->setUsers_id($users_id);
$pl->setStatus("favorite");
$pl->save();
$refreshCacheFromPlaylist = true;
$favorite = self::getIdFromUser($users_id, "favorite");
}
return $favorite;
}
static function getWatchLaterIdFromUser($users_id) {
global $refreshCacheFromPlaylist;
$watch_later = self::getIdFromUser($users_id, "watch_later");
if (empty($watch_later)) {
$pl = new PlayList(0);
$pl->setName("Watch Later");
$pl->setUsers_id($users_id);
$pl->setStatus("watch_later");
$pl->save();
$refreshCacheFromPlaylist = true;
$watch_later = self::getIdFromUser($users_id, "watch_later");
}
return $watch_later;
}
private static function getIdFromUser($users_id, $status) {
global $global;
$status = str_replace("'", "", $status);
$sql = "SELECT * FROM " . static::getTableName() . " pl WHERE"
. " users_id = ? AND pl.status = '{$status}' LIMIT 1 ";
$res = sqlDAL::readSql($sql, "i", array($users_id));
$data = sqlDAL::fetchAssoc($res);
sqlDAL::close($res);
if ($res) {
$row = $data['id'];
} else {
$row = false;
}
return $row;
}
static function getVideosIdFromPlaylist($playlists_id) {
$videosId = array();
$rows = static::getVideosIDFromPlaylistLight($playlists_id);
foreach ($rows as $value) {
$videosId[] = $value['videos_id'];
}
return $videosId;
}
static function sortVideos($videosList, $listIdOrder) {
$list = array();
foreach ($listIdOrder as $value) {
$found = false;
foreach ($videosList as $key => $value2) {
if ($value2['id'] == $value) {
$list[] = $value2;
unset($videosList[$key]);
$found = true;
}
}
if (!$found) {
$v = new Video("", "", $value);
if (empty($v->getFilename())) {
continue;
}
$list[] = array('id' => $value);
}
}
return $list;
}
public function save() {
if (!User::isLogged()) {
return false;
}
$this->clearEmptyLists();
$users_id = User::getId();
$this->setUsers_id($users_id);
$playlists_id = parent::save();
if (!empty($playlists_id)) {
self::deleteCache("getVideosFromPlaylist{$playlists_id}");
}
return $playlists_id;
}
/**
* This is just to fix errors from the update 6.4 to 6.5, where empty playlists were created before the update
* @return type
*/
private function clearEmptyLists() {
$sql = "DELETE FROM " . static::getTableName() . " WHERE status = ''";
return sqlDAL::writeSql($sql);
}
public function addVideo($videos_id, $add, $order = 0) {
global $global;
$formats = "";
$values = array();
if (empty($add) || $add === "false") {
$sql = "DELETE FROM playlists_has_videos WHERE playlists_id = ? AND videos_id = ? ";
$formats = "ii";
$values[] = $this->id;
$values[] = $videos_id;
} else {
$this->addVideo($videos_id, false);
$sql = "INSERT INTO playlists_has_videos ( playlists_id, videos_id , `order`) VALUES (?, ?, ?) ";
$formats = "iii";
$values[] = $this->id;
$values[] = $videos_id;
$values[] = $order;
}
self::deleteCache("getVideosFromPlaylist{$this->id}");
self::removeCache($videos_id);
return sqlDAL::writeSql($sql, $formats, $values);
}
public function delete() {
if (empty($this->id)) {
return false;
}
self::deleteCache("getVideosFromPlaylist{$this->id}");
global $global;
$sql = "DELETE FROM playlists WHERE id = ? ";
//echo $sql;
return sqlDAL::writeSql($sql, "i", array($this->id));
}
function getId() {
return $this->id;
}
function getName() {
return $this->name;
}
function getUsers_id() {
return $this->users_id;
}
function getStatus() {
return $this->status;
}
function setId($id) {
$this->id = $id;
}
function setName($name) {
if (strlen($name) > 45)
$name = substr($name, 0, 42) . '...';
$this->name = xss_esc($name);
//var_dump($name,$this->name);exit;
}
function setUsers_id($users_id) {
$this->users_id = $users_id;
}
function setStatus($status) {
if (!in_array($status, self::$validStatus)) {
$status = 'public';
}
$this->status = $status;
}
static function canSee($playlist_id, $users_id) {
$obj = new PlayList($playlist_id);
$status = $obj->getStatus();
if ($status !== 'public' && $status !== 'unlisted' && $users_id != $obj->getUsers_id()) {
return false;
}
return true;
}
}