diff --git a/gsAPI.php b/gsAPI.php index 4dd16dc..b20412d 100644 --- a/gsAPI.php +++ b/gsAPI.php @@ -137,11 +137,12 @@ public function authenticateUser(gsUser $user){ } } - if (!$user->getUsername() || !$user->getToken()) { + if ((!$user->getUsername() && !$user->getEmail()) || !$user->getToken()) { return false; } - $return = self::apiCall('authenticate',array('username'=>$user->getUsername(), 'password'=>$user->getToken(), 'sessionID'=>$this->session), true); + $return = self::apiCall('authenticate',array('login'=>($user->getUsername() ? $user->getUsername() : $user->getEmail()), + 'password'=>$user->getToken(), 'sessionID'=>$this->session), true); if (isset($return['decoded']['result']['UserID']) && $return['decoded']['result']['UserID'] > 0) { $user->importUserData($return['decoded']['result']); $this->sessionUserid = $user->getUserID(); @@ -248,8 +249,8 @@ public function getUserPlaylists($limit=null){ } $return = self::apiCall('getUserPlaylists',array('sessionID'=>$this->session, 'limit'=>$limit)); - if (isset($return['decoded']['result'])) { - return $return['decoded']['result']; + if (isset($return['decoded']['result']['playlists'])) { + return $return['decoded']['result']['playlists']; } else { return false; } @@ -276,6 +277,48 @@ public static function getUserPlaylistsByUserID($userid, $limit=null){ return false; } } + + /* + * getUserLibrary + + Requirements: session + + @param integer limit, optional + */ + public function getUserLibrarySongs($limit=null){ + if (empty($this->session)){ + trigger_error(__FUNCTION__." requires a valid session. No session was found.",E_USER_ERROR); + return false; + } + + $return = self::apiCall('getUserLibrarySongs',array('sessionID'=>$this->session, 'limit'=>$limit)); + if (isset($return['decoded']['result']['songs'])) { + return $return['decoded']['result']['songs']; + } else { + return false; + } + } + + /* + * Returns a list of favorites from the user. + + TODO: Sort by newest at the top. + + Requirements: session + */ + public function getUserFavoriteSongs($limit=null){ + if (empty($this->session)){ + trigger_error(__FUNCTION__." requires a valid session. No session was found.",E_USER_ERROR); + return false; + } + + $return = self::apiCall('getUserFavoriteSongs',array('sessionID'=>$this->session, 'limit'=>$limit)); + if (isset($return['decoded']['result']['songs'])) { + return $return['decoded']['result']['songs']; + } else { + return false; + } + } /* * Adds a song to the logged-in user's favorites @@ -416,6 +459,27 @@ public static function getAlbumInfo($album){ return false; } } + + /* + * Returns songs in a playlist + + Requirements: none + Static Session + + @param integer playlistID + */ + public static function getPlaylistSongs($playlistID, $limit=null){ + if (!is_numeric($playlistID)){ + return false; + } + + $return = self::apiCall('getPlaylistSongs', array('playlistID'=>$playlistID, 'limit'=>$limit)); + if (isset($return['decoded']['result']['songs'])) { + return $return['decoded']['result']['songs']; + } else { + return false; + } + } /* * Creates a playlist under the logged in user @@ -654,27 +718,6 @@ public static function getPopularSongsMonth($limit=null){ } } - /* - * Returns a list of favorites from the user. - - TODO: Sort by newest at the top. - - Requirements: session - */ - public function getUserFavoriteSongs($limit=null){ - if (empty($this->session)){ - trigger_error(__FUNCTION__." requires a valid session. No session was found.",E_USER_ERROR); - return false; - } - - $return = self::apiCall('getUserFavoriteSongs',array('sessionID'=>$this->session, 'limit'=>$limit)); - if (isset($return['decoded']['result'])) { - return $return['decoded']['result']; - } else { - return false; - } - } - /* * Returns the Country from the IP Address it was requested from diff --git a/gsPlaylist.php b/gsPlaylist.php index 9e86fa7..8ad2322 100644 --- a/gsPlaylist.php +++ b/gsPlaylist.php @@ -115,7 +115,7 @@ private function setURL($url) { } public function getURL() { - return self:getPlaylistURLService($this->playlistid); + return self::getPlaylistURLService($this->playlistid); } public static function getPlaylistURLService($playlistID){ @@ -141,24 +141,10 @@ public function getSongs($fetch=true) { if ($this->songs || !$fetch) { return $this->songs; } - - return $this->getPlaylistSongs(); + $this->songs = parent::getPlaylistSongs($this->getPlaylistID()); + return $this->songs; } - - private function getPlaylistSongs($limit=null){ - if (!is_numeric($this->getPlaylistID())){ - return false; - } - - $return = parent::apiCall('getPlaylistSongs', array('playlistID'=>$this->getPlaylistID(), 'limit'=>$limit)); - if (isset($return['decoded']['result'])) { - $this->setSongs($return['decoded']['result']); - return $this->songs; - } else { - return false; - } - } - + public function importPlaylistData($data) { if (is_array($data)) { if (isset($data['PlaylistID'])) { diff --git a/gsUser.php b/gsUser.php index 5a2d17e..eb2ce79 100644 --- a/gsUser.php +++ b/gsUser.php @@ -44,13 +44,19 @@ function toArray() { if ($this->getLevel(false) === 0) { $array['IsPlus'] = false; $array['IsAnywhere'] = false; } if ($this->getProfile(false)) $array['Profile'] = $this->getProfile(false); if ($this->getFavorites(false) !== null) $array['Favorites'] = $this->getFavorites(false); - //if ($this->getLibrary()) $array['Library'] = $this->getLibrary(); + if ($this->getLibrary()) $array['Library'] = $this->getLibrary(false); if ($this->getPlaylists(false) !== null) $array['Playlists'] = $this->getPlaylists(false); return $array; } public function setUsername($string) { if (preg_match("/^([a-zA-Z0-9]+[\.\-_]?)+[a-zA-Z0-9]+$/",$string) === false){ + + if (strpos($string, "@") !== false) { + $this->setEmail($string); + return $string; + } + return false; } else { $this->username = $string; @@ -276,6 +282,26 @@ public function getPlaylistsFromID($fetch=true) { return null; } + public function setLibrary($array) { + if (is_array($array)) { + $this->library = $array; + return true; + } + return false; + } + + public function getLibrary($fetch=true) { + if (is_array($this->library) || !$fetch) { + return $this->library; + } + if (is_object($this->parent) && $this->getUserID(false) == $this->parent->sessionUserid && $this->parent->getSession()) { + $library = $this->parent->getUserLibrarySongs(); + $this->library = $library; + return $this->library; + } + return null; + } + public function importUserData($data) { if (is_array($data)) { if (isset($data['UserID'])) { @@ -320,6 +346,12 @@ public function importUserData($data) { } if (isset($data['Favorites'])) { $this->setFavorites($data['Favorites']); + } + if (isset($data['Playlists'])) { + $this->setPlaylists($data['Playlists']); + } + if (isset($data['Library'])) { + $this->setLibrary($data['Library']); } return true; } else { @@ -348,6 +380,10 @@ public function authenticate() { return true; } } + + public function getSession() { + return $this->parent->session; + } }