This repository has been archived by the owner on May 6, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
functions.php
705 lines (589 loc) · 17.3 KB
/
functions.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
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
<?php
if (!session_status()) {
session_start();
}
use AppleMusic\Album as Album;
use AppleMusic\API as api;
use AppleMusic\Artist as Artist;
use AppleMusic\DB as db;
use AppleMusic\Song as Song;
function displayAlbums($albums) {
if ($albums) {
/** @var Album $album */
foreach ($albums as $album) {
echo Album::withArray(Album::objectToArray($album))->toString("albums");
}
}
}
function displaySongs($songs, $upcoming = true) {
if ($songs) {
/** @var Song $songs */
foreach ($songs as $song) {
echo Song::withArray(Song::objectToArray($song))->toString();
}
}
}
function displaySongsNew($collections, $upcoming = true) {
if ($collections) {
foreach ($collections as $collectionId => $songs) {
/** @var Song $songs */
foreach ($songs as $song) {
$unique = $song->first && $song->last;
// print_r($song);
// if (!$unique && $song->first || $unique) {
if (!$unique && $song->first) {
echo "<div class=\"group--song\" data-id-collection=\"{$collectionId}\">";
}
echo Song::withArray(Song::objectToArray($song))->toString(true);
// if (!$unique && $song->last || $unique) {
if (!$unique && $song->last) {
echo "</div>";
}
}
}
}
}
function getThisWeekReleases($type = null, $allow_duplicates = false) {
$db = new db();
$releases = $db->getUserWeekReleases($type);
if (!$releases) {
return '';
}
$week_display = 'grid-2-row'; // 'grid'
$html_multi_row = $html_row_1 = $html_row_2 = '';
$title = 'Sorties de la semaine';
switch ($type) {
case 'albums':
$title = 'Nouveaux projets';
break;
case 'singles':
$title = 'Nouveaux singles';
break;
}
$html = '
<section class="main-header l-content-width section section--bordered ' . ($week_display === 'grid-2-row' ? 'l-multi-rows' : '') . '"
id="weekly-releases">
<div class="section-header section__nav clearfix">
<h2 class="section-title section__headline">' . $title . '</h2>
</div>
<div class="section-body l-row l-row--grid">';
$array_releases = [];
$i = 0;
$releases_decoded = json_decode($releases);
$nb_releases = count($releases_decoded);
foreach ($releases_decoded as $r) {
// avoiding duplicates + removing non explicits
$tmp_name = trim(preg_replace('/(?:\(|\[)(?:e|E)(?:xplicit|XPLICIT)(?:\)|\]) */m', '', $r->name));
$str = trim(preg_replace('/([^A-Za-z0-9]|(\s))*/', '', "{$tmp_name} {$r->artistName}"));
if (!$allow_duplicates && !empty($array_releases[$str])) {
continue;
}
$array_releases[$str] = true;
$artistId = $r->idArtist;
$album = Album::withArray(Album::objectToArray($r));
if (!isset($artists[$artistId])) {
$artists[$artistId] = [
"id" => $artistId,
"name" => $r->artistName,
"albums" => [],
"lastUpdate" => $r->lastUpdate,
"added" => $r->added,
];
}
$display = 'grid';
$album_html = $album->toString($week_display);
if ($week_display === 'grid-2-row') {
if ($i % 2 === 0 || $nb_releases === 2) {
$html_row_1 .= $album_html;
} else {
$html_row_2 .= $album_html;
}
$i++;
}
$html .= $album_html;
}
if ($week_display === 'grid-2-row') {
$html_multi_row = '
<div class="section-body l-row l-row--peek l-row--flex">
<div class="l-sub-row l-sub-row--1">' . $html_row_1 . '</div>
<div class="l-sub-row l-sub-row--2">' . $html_row_2 . '</div>
</div>
';
}
$html .= '
</div>
' . $html_multi_row . '
</section>';
return $html;
}
function getUpcomingReleases() {
$db = new db();
$releases = $db->getUserUpcomingReleases();
if (!$releases) {
return '';
}
$html = '
<section class="l-content-width section section--bordered" data-am-artist-id="968750077">
<div class="section-header section__nav clearfix">
<h2 class="section-title section__headline">À venir</h2>
</div>
<div class="section-body l-row l-row--peek">';
$array_releases = [];
foreach (json_decode($releases) as $r) {
// avoiding duplicates + removing non explicits
$str = trim(preg_replace('/([^A-Za-z0-9]|(\s))*/', '', "{$r->name} {$r->artistName}"));
if (!empty($array_releases[$str])) {
continue;
}
$array_releases[$str] = true;
$artistId = $r->idArtist;
$album = Album::withArray(Album::objectToArray($r));
if (!isset($artists[$artistId])) {
$artists[$artistId] = [
"id" => $artistId,
"name" => $r->artistName,
"albums" => [],
"lastUpdate" => $r->lastUpdate,
];
}
$display = 'row';
$html .= $album->toString('row');
}
//$html .= '<pre>'.print_r($releases, 1).'</pre>';
$html .= '
</div>
</section>';
return $html;
}
// Dernières sorties depuis actualisation, dans la BD
function getAllAlbums($display = "artists") {
$db = new db();
$releases = $db->getUserAlbums();
$artists = [];
if (!$releases) {
return json_decode($releases);
}
if ($display === "albums") {
echo '
<section class="artist l-content-width section">
<div class="section-header section__headline">
<h2 class="section-title">Tous les albums</h2>
</div>
<div class="section-body l-row">';
foreach (json_decode($releases) as $r) {
$artistId = $r->idArtist;
$album = Album::withArray(Album::objectToArray($r));
if (!isset($artists[$artistId])) {
$artists[$artistId] = [
"id" => $artistId,
"name" => $r->artistName,
"albums" => [],
"lastUpdate" => $r->lastUpdate,
];
}
$album->toString();
}
echo '
</div>
</section>';
} else {
//"artists"
foreach (json_decode($releases) as $r) {
// Artiste
$artistId = $r->idArtist;
$album = Album::withArray(Album::objectToArray($r));
if (!isset($artists[$artistId])) {
$artists[$artistId] = [
"id" => $artistId,
"name" => $r->artistName,
"albums" => [],
"lastUpdate" => $r->lastUpdate,
];
}
$artists[$artistId]["albums"][] = $album;
}
foreach ($artists as $artist) {
Artist::withNewRelease($artist)->toString();
}
}
return json_decode($releases);
}
function getAllSongs($filtrer_albums = false, $only_explicit = true, $type = null, $available = false, ?bool $compilation = null) {
global $daysInterval;
$db = new db();
$releases = $db->getUserSongs($daysInterval, null, $compilation);
$artists = [];
if ($filtrer_albums) {
$week_releases = $db->getUserWeekReleases();
$albums = $week_releases ? array_map(static function ($album) {
return $album['id'];
}, json_decode($db->getUserWeekReleases(), true)) : [];
}
$releases_array = [];
if ($releases) {
$released_data = json_decode($releases);
foreach ($released_data as $i => $r) {
$artistId = $r->idArtist;
$song = Song::withArray(Song::objectToArray($r));
$streamable = $song->isStreamable();
// $upcoming = (new DateTime()) < (new DateTime($song->getDate()));
$upcoming = (new DateTime()) < ((new DateTime($song->getDate()))->setTime(0, 0));
// type 1 : only streamable
if ($type === 1 && (!$streamable || $upcoming)) {
continue;
}
// // type 2 : only not streamable/upcoming or streamable & upcoming
if ($type === 2 && (!$upcoming || (!$available && $streamable))) {
continue;
}
if ($type === 2 && (!$upcoming || ($available && !$streamable))) {
continue;
}
if ($filtrer_albums && in_array($r->collectionId, $albums)) {
// unset($releases_array[$i]);
continue;
}
if ($only_explicit) {
$correspondances = array_keys(array_filter($releases_array, function ($element) use ($r) {
return $element->trackName === $r->trackName
&& $element->collectionName === $r->collectionName
&& $element->artistName === $r->artistName;
}));
if ($correspondances) {
$index_correspondance = $correspondances[0];
// if the explicit version is already in the array, then cool !
if ($releases_array[$index_correspondance]->explicit) {
continue;
}
// if the new one is the explicit one, then we remove the clean one
else if ($r->explicit) {
unset($releases_array[$index_correspondance]);
}
}
}
$releases_array[] = $r;
if (!isset($artists[$artistId])) {
$artists[$artistId] = [
"id" => $artistId,
"name" => $r->artistName,
"albums" => [],
"songs" => [],
"lastUpdate" => $r->lastUpdate,
];
}
$artists[$artistId]["songs"][] = $song;
}
if ($only_explicit) {
// find duplicates
// $indexes = array_keys(array_filter($res, function($element) use($item){ return $element['name'] === $item['name'] && $element['artistName'] === $item['artistName'];}))[0];
// $return[] = $res[$indexes];
}
// foreach ($artists as $artist) {
// Artist::withNewRelease($artist)->toString();
// }
if ($type !== 1) {
$releases_array = array_reverse($releases_array);
}
}
// ordering for groups
$final = [];
$rupture = null;
$collectionIndex = -1;
foreach ($releases_array as $index => $release) {
$collectionId = $release->collectionId;
$release->first = false;
$release->last = true;
if (!isset($final[$collectionId])) {
$release->first = true;
} else {
$final[$collectionId][count($final[$collectionId]) - 1]->last = false;
}
$final[$collectionId][] = $release;
}
return $final;
// print_r($final);
// [id] => 1587680893
// [collectionId] => 1587680887
// [collectionName] => Girl Next Door (Young Bombs Remix) [feat. Wiz Khalifa, DVBBS] - Single
// [trackName] => Girl Next Door (Young Bombs Remix) [feat. Wiz Khalifa, DVBBS]
// [artistName] => SK8
// [date] => 2021-09-27 00:00:00
// [artwork] => //is5-ssl.mzstatic.com/image/thumb/Music115/v4/5c/54/e2/5c54e293-4770-bf55-4749-b9289a7b2f47/source/100x100bb.jpg
// [idArtist] => 201714418
// [lastUpdate] => 2019-10-09 00:00:00
// [explicit] => 1
// [isStreamable] => 0
return $releases_array;
}
// Récupère tous les artistes
function getAllNewReleases(int | null $idArtist = null) {
$db = new db();
$releases = [];
/*$removal =*/
$db->removeOldAlbums(90, $idArtist);
$db->removeOldSongs(90, $idArtist);
$artists = $db->getUsersArtists(null, true, $idArtist);
if (!$artists) {
return $releases;
}
foreach (json_decode($artists) as $artist) {
printCli(sprintf('Getting releases for %s (%s)', $artist->name, $artist->id));
$releases[] = getArtistRelease($artist);
// break;
}
return $releases;
}
function getAllNewScrappedReleases(int | null $idArtist = null) {
$db = new db();
$releases = [];
/*$removal =*/
$artists = $db->getUsersArtists(null, true, $idArtist);
if (!$artists) {
return $releases;
}
foreach (json_decode($artists) as $artist) {
$releases[] = getArtistScrappedRelease($artist);
// break;
}
return $releases;
}
function removeOldAlbums($days = 180) {
$db = new db();
return $db->removeOldAlbums($days);
}
/**
* @param $objArtist
* @param bool $display
* @return mixed
*/
function getArtistRelease($objArtist, $display = false) {
// $db = new db;
global $nodisplay;
// Artiste
$artist = new Artist($objArtist->id);
$artist->setName($objArtist->name);
$artist->setLastUpdate($objArtist->lastUpdate);
//https://itunes.apple.com/search?term=Dinos&entity=songs&limit=200&sort=recent&country=fr
//file_put_contents(LOG_FILE, "\n\n\nFetching artist {$objArtist->name}, id {$objArtist->id}\n", FILE_APPEND);
// Recupération des albums sur l'API
$api = new api($artist->getId());
$newEntities = $api->update($artist->getLastUpdate());
$artist->setAlbums($newEntities["albums"]);
$artist->setSongs($newEntities["songs"]);
$albums = $artist->getAlbums();
$songs = $artist->getSongs();
/*if ($objArtist->id == "331066376") {
file_put_contents(LOG_FILE, "\n\n\n---> {$objArtist->name}, id:{$objArtist->id}\n", FILE_APPEND);
file_put_contents(LOG_FILE, "\nALBUMS " . json_encode($albums) . "\n", FILE_APPEND);
file_put_contents(LOG_FILE, "\nSONGS " . json_encode($songs) . "\n", FILE_APPEND);
}*/
// Mise en BD des nouveaux albums & nouvelles chansons
/** @var Album $album */
foreach ($albums as $album) {
// Ajout de l'album à la BD
$album->addAlbum($artist->getId());
// $artist->update();
echo $nodisplay ? null : $album->toString($display, $artist->getId());
}
if ($songs) {
/** @var Song $song */
foreach ($songs as $song) {
// Ajout de l'album à la BD
$song->addSong($artist->getId());
// $artist->update();
//echo $nodisplay ? null : $song->toString($display);
}
}
return ["albums" => $albums, "songs" => $songs];
}
function getArtistScrappedRelease($objArtist, $display = false) {
// $db = new db;
global $nodisplay;
// Artiste
$artist = new Artist($objArtist->id);
$artist->setName($objArtist->name);
$artist->setLastUpdate($objArtist->lastUpdate);
//https://itunes.apple.com/search?term=Dinos&entity=songs&limit=200&sort=recent&country=fr
//file_put_contents(LOG_FILE, "\n\n\nFetching artist {$objArtist->name}, id {$objArtist->id}\n", FILE_APPEND);
// Recupération des albums sur l'API
$api = new api($artist->getId());
$newEntities = $api->update($artist->getLastUpdate(), true, $objArtist->name);
return $newEntities;
}
function editLastUpdated($days = 7, $id_user = 0) {
$db = new db();
return $db->editLastUpdated($days, $id_user);
}
function logRefresh($type = "") {
$db = new db();
return $db->logRefresh($type);
}
function logMail($description, $id_user) {
$db = new db();
return $db->logMail($description, $id_user);
}
function getLastRefresh() {
$db = new db();
return $db->getLastRefresh();
}
function getNotificationsStatus() {
$db = new db();
return $db->getNotificationsStatus();
}
/**
* Renvoie une date selon le format voulu (string : Y:m:d H:i:s|timestamp)
* @param string|int $date
* @param string|int $format ("string"|"integer,int,timestamp")
* @return false|int|string
*/
function fixDate($date, $format = "string") {
$format = strtolower($format);
switch ($format) {
case "string":
if (is_numeric($date)) {
return date(DEFAULT_DATE_FORMAT, $date);
}
return $date;
case "string_time_no_sec":
if (is_numeric($date)) {
return date(DEFAULT_DATE_FORMAT_NO_SECS, $date);
}
return $date;
case "string_time":
if (is_numeric($date)) {
return date(DEFAULT_DATE_FORMAT_TIME, $date);
}
return $date;
case "integer":
case "int":
case "timestamp":
if (!is_numeric($date)) {
return strtotime($date);
}
return $date;
default:
return $date;
}
}
function fixTZDate($date) {
return str_replace("Z", " ", str_replace("T", " ", str_replace("07:00:00", "00:00:00", str_replace("08:00:00", "00:00:00", $date))));
}
/**
* @param $d
* @return mixed
*/
function getWeekDay($d) {
$weekdays = unserialize(WEEKDAYS_NAMES);
return isset($weekdays[$d]) ? $weekdays[$d] : "error";
}
/**
* @param int $m
* @param bool $short
* @return mixed
*/
function getMonth($m, $short = false) {
$monthNames = unserialize($short ? MONTHS_NAMES_SHORT : MONTHS_NAMES);
$m = intval($m);
return isset($monthNames[$m]) ?
$monthNames[$m] : (1 <= $m && $m <= 12 ? strtolower(date("F", strtotime("01-$m-2000"))) : "error");
}
/**
* Vérifie que l'utilisateur est connecté
* L'adresse cible dans un cookie sinon et on le redirige vers la page de connexion
*/
function checkConnexion() {
if (!isConnected()) {
if ($_SERVER && isset($_SERVER['HTTP_HOST']) && isset($_SERVER['REQUEST_URI'])) {
$_COOKIE["redirect"] = "http://" . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
setcookie("redirect", "http://" . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']);
}
clearAuthCookie();
header("location: login.php");
exit;
}
}
function int_greater_than_zero($var) {
return is_numeric($var) && intval($var) > 0;
}
function id_user() {
$idUser = $_SESSION["id_user"] ?? 0;
if (int_greater_than_zero($idUser)) {
return (int) $idUser;
}
if (
is_array($idUser) && isset($idUser['id'])
&& int_greater_than_zero($idUser['id'])
) {
return (int) $idUser['id'];
}
if (
is_array($idUser) && isset($idUser[0])
&& int_greater_than_zero($idUser[0])
) {
return (int) $idUser[0];
}
return 0;
}
/**
* Vérifie que l'utilisateur est connecté
* @return bool
*/
function isConnected() {
$isLoggedIn = false;
//
$idUser = id_user();
if ($idUser > 0) {
$_SESSION['id_user'] = $idUser;
return true;
}
require "login_auth.php";
return $isLoggedIn;
// return (isset($_SESSION) && !empty($_SESSION) && isset($_SESSION["id_user"]) && strlen(strval($_SESSION["id_user"])));
}
function getInitial($str) {
$tmp = explode(" ", preg_replace("/[^A-Za-z0-9$ ]/", '', $str));
if (count($tmp) === 1) {
return strtoupper($tmp[0][0]);
} else {
end($tmp);
return strtoupper($tmp[0][0] . $tmp[key($tmp)][0]);
}
}
function getArtistSVG($str) {
return '
<svg xmlns="http://www.w3.org/2000/svg" width="44" height="44" viewBox="0 0 640 640"
class="we-artwork__image artist-search-artwork-img">
<defs>
<linearGradient id="a" x1="50%" y1="0%" x2="50%" y2="100%">
<stop offset="0%" stop-color="#A5ABB8"></stop>
<stop offset="100%" stop-color="#848993"></stop>
</linearGradient>
</defs>
<rect width="100%" height="100%" fill="url(#a)"></rect>
<text x="320" y="50%" dy="0.35em" font-size="250" fill="#fff" text-anchor="middle"
font-family="SF Pro Display,Helvetica,Arial" font-weight="500">' . getInitial($str) . '
</text>
</svg>
';
}
function removeIntIndex($array) {
foreach ($array as $key => $value) {
if (is_int($key)) {
unset($array[$key]);
}
}
return $array;
}
function writeJSON($name, $content) {
$fp = fopen($name, 'w');
fwrite($fp, json_encode($content));
fclose($fp);
}
/**
* -
*
* @return bool
*/
function is_localhost() {
return in_array($_SERVER['REMOTE_ADDR'], ['127.0.0.1', '::1']);
}