summaryrefslogtreecommitdiff
path: root/includes/session.php
blob: 36c52164557d502800abb5bbe563d8035fe13943 (plain)
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
<?php

global $_PROFILE;
$_PROFILE = null;

if (isset($_COOKIE["WAVY_SESSION_TOKEN"])) {
    if (!str_contains($_COOKIE["WAVY_SESSION_TOKEN"], ".") && !str_contains($_COOKIE["WAVY_SESSION_TOKEN"], "/")) {
        if (str_starts_with($_COOKIE["WAVY_SESSION_TOKEN"], "wv_")) {
            if (file_exists($_SERVER['DOCUMENT_ROOT'] . "/includes/tokens/" . $_COOKIE["WAVY_SESSION_TOKEN"])) {
                $_PROFILE = json_decode(file_get_contents($_SERVER['DOCUMENT_ROOT'] . "/includes/tokens/" . $_COOKIE["WAVY_SESSION_TOKEN"]), true);
            }
        }
    }
}

if (!isset($_PROFILE)) {
    if (str_contains($_SERVER['HTTP_USER_AGENT'], "MistNative/")) {
        header("Location: /oauth/needs-native/");
    } else {
        header("Location: /oauth/init/");
    }

    die();
}

global $albums; global $songs;

if (!file_exists($_SERVER['DOCUMENT_ROOT'] . "/includes/users")) mkdir($_SERVER['DOCUMENT_ROOT'] . "/includes/users");
if (!file_exists($_SERVER['DOCUMENT_ROOT'] . "/includes/users/" . $_PROFILE["id"] . "-favorites.json")) file_put_contents($_SERVER['DOCUMENT_ROOT'] . "/includes/users/" . $_PROFILE["id"] . "-favorites.json", "[]");
if (!file_exists($_SERVER['DOCUMENT_ROOT'] . "/includes/users/" . $_PROFILE["id"] . "-library.json")) file_put_contents($_SERVER['DOCUMENT_ROOT'] . "/includes/users/" . $_PROFILE["id"] . "-library.json", "[]");
if (!file_exists($_SERVER['DOCUMENT_ROOT'] . "/includes/users/" . $_PROFILE["id"] . "-history.json")) file_put_contents($_SERVER['DOCUMENT_ROOT'] . "/includes/users/" . $_PROFILE["id"] . "-history.json", "[]");

$albums = json_decode(file_get_contents($_SERVER['DOCUMENT_ROOT'] . "/assets/content/albums.json"), true);
$songs = json_decode(file_get_contents($_SERVER['DOCUMENT_ROOT'] . "/assets/content/songs.json"), true);
$favorites = json_decode(file_get_contents($_SERVER['DOCUMENT_ROOT'] . "/includes/users/" . $_PROFILE["id"] . "-favorites.json"), true);
$library = json_decode(file_get_contents($_SERVER['DOCUMENT_ROOT'] . "/includes/users/" . $_PROFILE["id"] . "-library.json"), true);
$history = json_decode(file_get_contents($_SERVER['DOCUMENT_ROOT'] . "/includes/users/" . $_PROFILE["id"] . "-history.json"), true);

$albums = array_map(function ($i) {
    $i["artist"] = str_replace(";", ", ", $i["artist"]);
    return $i;
}, $albums);

function displayList($list, $hasAlbum = false) { global $albums; global $favorites; ?>
    <div class="list-group" style="margin-left: 10px; margin-top: 20px;" id="main-list">
        <?php foreach ($list as $id => $song): ?>
            <div data-item-track="<?= $song["title"] ?>" data-item-artist="<?= $song["artist"] ?>" id="item-<?= $id ?>" class="list-group-item track" style="display: grid; grid-template-columns: 32px 1fr max-content;">
                <div style="opacity: .5; display: flex; align-items: center; justify-content: left;"><?= $hasAlbum && $song["track"] > 0 ? $song["track"] : "" ?></div>
                <?php if (!$hasAlbum): ?>
                <div style="display: grid; grid-template-columns: 48px 1fr; grid-gap: 10px;">
                    <img src="/assets/content/<?= $id ?>.jpg" style="width: 48px; height: 48px;">
                <?php endif; ?>
                    <div style="width: <?php if (!$hasAlbum): ?>50<?php else: ?>55<?php endif; ?>vw; height: 3em; display: flex; align-items: center; justify-content: left;">
                        <?php if ($hasAlbum && $song["artist"] === $albums[$_GET["a"]]["artist"]): ?>
                            <div style="max-width: 100%;"><div style="max-width: 100%; white-space: nowrap; overflow: hidden !important; text-overflow: ellipsis;"><?= $song["title"] ?></div></div>
                        <?php else: ?>
                            <div style="max-width: 100%;"><div style="max-width: 100%; white-space: nowrap; overflow: hidden !important; text-overflow: ellipsis;"><?= $song["title"] ?></div><div style="max-width: 100%; white-space: nowrap; overflow: hidden !important; text-overflow: ellipsis; opacity: .5;"><?= $song["artist"] ?></div></div>
                        <?php endif; ?>
                    </div>
            <?php if (!$hasAlbum): ?>
                </div>
            <?php endif; ?>
                <div class="list-actions">
                    <!--<span onclick="<?= in_array($id, $favorites) ? "un" : "" ?>favoriteSong('<?= $id ?>');" class="player-btn" style="border-radius: 999px; display: inline-flex; align-items: center; justify-content: center; height: 48px; width: 48px;" id="btn-favorite-<?= $id ?>">
                        <img class="icon" alt="" src="/assets/icons/favorite-<?= in_array($id, $favorites) ? "on" : "off" ?>.svg" style="pointer-events: none; width: 32px; height: 32px;" id="btn-favorite-<?= $id ?>-icon">
                    </span>-->
                    <span onclick="window.parent.playSong('<?= $id ?>'<?php if ($hasAlbum): ?>, 'album:<?= $_GET["a"] ?>'<?php endif; ?>);" class="player-btn" style="border-radius: 999px; display: inline-flex; align-items: center; justify-content: center; height: 48px; width: 48px;" id="btn-play-<?= $id ?>">
                        <img class="icon" alt="" src="/assets/icons/play.svg" style="pointer-events: none; width: 32px; height: 32px;" id="btn-play-<?= $id ?>-icon">
                    </span>
                    <span class="dropdown">
                        <span class="player-btn" style="border-radius: 999px; display: inline-flex; align-items: center; justify-content: center; height: 48px; width: 48px;" id="btn-menu-<?= $id ?>" data-bs-toggle="dropdown">
                            <img class="icon" alt="" src="/assets/icons/menu.svg" style="pointer-events: none; width: 32px; height: 32px;" id="btn-menu-<?= $id ?>-icon">
                        </span>
                        <ul class="dropdown-menu">
                            <li><a onclick="playNext('<?= $id ?>');" class="dropdown-item" href="#"><img alt="" src="/assets/icons/playnext.svg" style="pointer-events: none; width: 24px; height: 24px; margin-right: 5px;">Play next</a></li>
                            <li><a onclick="enqueue('<?= $id ?>');" class="dropdown-item" href="#"><img alt="" src="/assets/icons/add.svg" style="pointer-events: none; width: 24px; height: 24px; margin-right: 5px;">Add to queue</a></li>
                            <li><hr class="dropdown-divider"></hr></li>
                            <li><a id="btn-favorite-<?= $id ?>" onclick="<?= in_array($id, $favorites) ? "un" : "" ?>favoriteSong('<?= $id ?>');" class="dropdown-item" href="#"><img id="btn-favorite-<?= $id ?>-icon" alt="" src="/assets/icons/favorite-<?= in_array($id, $favorites) ? "on" : "off" ?>.svg" style="pointer-events: none; width: 24px; height: 24px; margin-right: 5px;"><span id="btn-favorite-<?= $id ?>-text"><?= in_array($id, $favorites) ? "Remove from favorites" : "Add to favorites" ?></span></a></li>
                            <li><a onclick="downloadSong('<?= $id ?>');" class="dropdown-item" href="#"><img alt="" src="/assets/icons/download.svg" style="pointer-events: none; width: 24px; height: 24px; margin-right: 5px;">Download</a></li>
                            <li><a onclick="getInfo('<?= $id ?>');" class="dropdown-item" href="#"><img alt="" src="/assets/icons/info.svg" style="pointer-events: none; width: 24px; height: 24px; margin-right: 5px;">Get info</a></li>
                        </ul>
                    </span>
                </div>
            </div>
        <?php endforeach; ?>
    </div>
    <script>
        async function favoriteSong(id) {
            document.getElementById("btn-favorite-" + id + "-icon").src = "/assets/icons/favorite-on.svg";
            document.getElementById("btn-favorite-" + id + "-text").innerText = "Remove from favorites";
            document.getElementById("btn-favorite-" + id).onclick = () => {
                unfavoriteSong(id);
            }
            await fetch("/api/addFavorite.php?i=" + id);

            window.parent.redownloadFavorites();
        }

        function playNext(id) {
            if (window.parent.playlist.length === 0) {
                window.parent.playSong(id);
            } else {
                window.parent.playlist.splice(window.parent.currentPlaylistPosition, 0, id);
            }
        }

        function downloadSong(id) {
            window.parent.openModal("Download song", "download.php?i=" + id);
        }

        function getInfo(id) {
            window.parent.openModal((window.parent.songs[id]?.artist ?? "Unknown artist") + " - " + (window.parent.songs[id]?.title ?? "Unknown song"), "info.php?i=" + id);
        }

        function enqueue(id) {
            if (window.parent.playlist.length === 0) {
                window.parent.playSong(id);
            } else {
                window.parent.playlist.push(id);
            }
        }

        async function unfavoriteSong(id) {
            document.getElementById("btn-favorite-" + id + "-icon").src = "/assets/icons/favorite-off.svg";
            document.getElementById("btn-favorite-" + id + "-text").innerText = "Add to favorites";
            document.getElementById("btn-favorite-" + id).onclick = () => {
                favoriteSong(id);
            }
            await fetch("/api/removeFavorite.php?i=" + id);

            <?php if (isset($favoritesList)): ?>
            location.reload();
            <?php endif; ?>

            window.parent.redownloadFavorites();
        }
    </script>
<?php }