Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
spotify
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
web
spotify
Commits
46a35ce0
Commit
46a35ce0
authored
3 years ago
by
Benoît Harrault
Browse files
Options
Downloads
Patches
Plain Diff
Improve/clean some code
parent
226daaa5
No related branches found
No related tags found
1 merge request
!4
Resolve "Improve/clean code"
Changes
5
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
public/auth.php
+3
-15
3 additions, 15 deletions
public/auth.php
public/callback.php
+2
-9
2 additions, 9 deletions
public/callback.php
public/index.php
+16
-38
16 additions, 38 deletions
public/index.php
spotify/spotify.php
+18
-0
18 additions, 0 deletions
spotify/spotify.php
template.php
+3
-1
3 additions, 1 deletion
template.php
with
42 additions
and
63 deletions
public/auth.php
+
3
−
15
View file @
46a35ce0
<?php
session_start
()
;
require
'../spotify/spotify.php'
;
require
'../vendor/autoload.php'
;
require
'../config.php'
;
$session
=
new
SpotifyWebAPI\Session
(
$CLIENT_ID
,
$CLIENT_SECRET
,
$REDIRECT_URI
);
$session
=
new
SpotifyWebAPI\Session
(
$CLIENT_ID
,
$CLIENT_SECRET
,
$REDIRECT_URI
);
$state
=
$session
->
generateState
();
$_SESSION
[
'state'
]
=
$state
;
$options
=
[
'scope'
=>
[
'playlist-read-private'
,
'playlist-modify-private'
,
'playlist-modify-public'
,
'user-read-private'
,
],
'scope'
=>
$SPOTIFY_REQUIRED_SCOPES
,
'state'
=>
$state
,
];
...
...
This diff is collapsed.
Click to expand it.
public/callback.php
+
2
−
9
View file @
46a35ce0
<?php
session_start
()
;
require
'../spotify/spotify.php'
;
require
'../vendor/autoload.php'
;
require
'../config.php'
;
$session
=
new
SpotifyWebAPI\Session
(
$CLIENT_ID
,
$CLIENT_SECRET
,
$REDIRECT_URI
);
$session
=
new
SpotifyWebAPI\Session
(
$CLIENT_ID
,
$CLIENT_SECRET
,
$REDIRECT_URI
);
$storedState
=
$_SESSION
[
'state'
];
$state
=
$_GET
[
'state'
];
...
...
This diff is collapsed.
Click to expand it.
public/index.php
+
16
−
38
View file @
46a35ce0
<?php
session_start
();
require
'../vendor/autoload.php'
;
require
'../config.php'
;
require
'../spotify/spotify.php'
;
if
(
isset
(
$_GET
[
'logout'
]))
{
$_SESSION
[
'accessToken'
]
=
''
;
$_SESSION
[
'refreshToken'
]
=
''
;
}
$accessToken
=
''
;
if
(
isset
(
$_SESSION
[
'accessToken'
]))
{
$accessToken
=
$_SESSION
[
'accessToken'
];
}
$refreshToken
=
''
;
if
(
isset
(
$_SESSION
[
'refreshToken'
]))
{
$refreshToken
=
$_SESSION
[
'refreshToken'
];
}
$_SESSION
[
'accessToken'
]
=
isset
(
$_SESSION
[
'accessToken'
])
?
$_SESSION
[
'accessToken'
]
:
''
;
$_SESSION
[
'refreshToken'
]
=
isset
(
$_SESSION
[
'refreshToken'
])
?
$_SESSION
[
'refreshToken'
]
:
''
;
if
(
!
$accessToken
)
{
if
(
!
$
_SESSION
[
'
accessToken
'
]
)
{
header
(
'Location: auth.php'
);
die
();
}
$session
=
new
SpotifyWebAPI\Session
(
$CLIENT_ID
,
$CLIENT_SECRET
,
$REDIRECT_URI
);
$session
=
new
SpotifyWebAPI\Session
(
$CLIENT_ID
,
$CLIENT_SECRET
,
$REDIRECT_URI
);
// Use previously requested tokens fetched from session
if
(
$accessToken
)
{
$session
->
setAccessToken
(
$accessToken
);
$session
->
setRefreshToken
(
$refreshToken
);
if
(
$
_SESSION
[
'
accessToken
'
]
)
{
$session
->
setAccessToken
(
$
_SESSION
[
'
accessToken
'
]
);
$session
->
setRefreshToken
(
$
_SESSION
[
'
refreshToken
'
]
);
}
else
{
// Or request a new access token
$session
->
refreshAccessToken
(
$refreshToken
);
$session
->
refreshAccessToken
(
$
_SESSION
[
'
refreshToken
'
]
);
}
$options
=
[
'scope'
=>
[
'playlist-read-private'
,
'playlist-modify-private'
,
'playlist-modify-public'
,
'user-read-private'
,
],
'scope'
=>
$SPOTIFY_REQUIRED_SCOPES
,
'auto_refresh'
=>
true
,
];
...
...
@@ -58,23 +40,19 @@ $_SESSION['refreshToken'] = $session->getRefreshToken();
$user
=
$api
->
me
();
$templateData
=
[];
$templateData
[
'user'
]
=
$user
;
$templateData
[
'userImageUrl'
]
=
''
;
if
(
$user
->
images
[
0
])
{
$templateData
[
'userImageUrl'
]
=
$user
->
images
[
0
]
->
url
;
}
$templateData
=
[
'user'
=>
$user
,
'playlists'
=>
[],
$templateData
[
'infos'
]
=
[];
$templateData
[
'errors'
]
=
[];
'infos'
=>
[],
'errors'
=>
[],
];
$selectedPlaylistId
=
''
;
if
(
isset
(
$_GET
[
'playlist'
]))
{
$selectedPlaylistId
=
$_GET
[
'playlist'
];
}
$templateData
[
'playlists'
]
=
[];
if
(
$selectedPlaylistId
)
{
$playlist
=
$api
->
getPlaylist
(
$selectedPlaylistId
);
error_log
(
'Will create new playlist from tracks in playlist '
.
$selectedPlaylistId
.
' "'
.
$playlist
->
name
.
'".'
);
...
...
This diff is collapsed.
Click to expand it.
config
.php
→
spotify/spotify
.php
+
18
−
0
View file @
46a35ce0
<?php
session_start
();
$parametersFile
=
__DIR__
.
'/parameters.php'
;
require
'../vendor/autoload.php'
;
$parametersFile
=
realpath
(
__DIR__
.
'/../parameters.php'
);
if
(
!
is_file
(
$parametersFile
))
{
die
(
'Missing parameters file: '
.
$parametersFile
);
}
require
$parametersFile
;
$SPOTIFY_REQUIRED_SCOPES
=
[
'playlist-read-private'
,
'playlist-modify-private'
,
'playlist-modify-public'
,
'user-read-private'
,
];
This diff is collapsed.
Click to expand it.
template.php
+
3
−
1
View file @
46a35ce0
...
...
@@ -16,7 +16,9 @@
<div
class=
"col-md-2"
>
<div
class=
"card"
>
<img
src=
"
<?php
echo
$templateData
[
'userImageUrl'
];
?>
"
class=
"card-img-top"
alt=
"
<?php
echo
$templateData
[
'user'
]
->
display_name
;
?>
"
>
<?php
if
(
count
(
$templateData
[
'user'
]
->
images
))
{
?>
<img
src=
"
<?php
echo
$templateData
[
'user'
]
->
images
[
0
]
->
url
;
?>
"
class=
"card-img-top"
alt=
"
<?php
echo
$templateData
[
'user'
]
->
display_name
;
?>
"
>
<?php
}
?>
<div
class=
"card-body"
>
<h5
class=
"card-title"
>
<?php
echo
$templateData
[
'user'
]
->
display_name
;
?>
</h5>
<a
href=
"
<?php
echo
$templateData
[
'user'
]
->
external_urls
->
spotify
;
?>
"
class=
"btn btn-primary"
>
View profile
</a>
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment