From 6ac2576bde5cd3115a21535f2c1094ececd12a91 Mon Sep 17 00:00:00 2001 From: Peter Brenner Date: Mon, 11 Mar 2013 22:15:47 -0400 Subject: [PATCH] Issue #56 - Changed to pull profile picture each time the user logs in --- app/Controller/AppController.php | 42 +++++++++++++++++++++++++++++++- 1 file changed, 41 insertions(+), 1 deletion(-) diff --git a/app/Controller/AppController.php b/app/Controller/AppController.php index 70df814..d630419 100644 --- a/app/Controller/AppController.php +++ b/app/Controller/AppController.php @@ -118,7 +118,10 @@ private function _initUser() { if ($user) { - $this->_currentUser = $user; + //$this->_currentUser = $user; + // Update the user record with the new image + $this->_currentUser = $this->_updateUser($user, $facebookId); + } else { $this->_currentUser = $this->_createUser($facebookId); } @@ -184,6 +187,43 @@ private function _createUser($facebookId){ return $data; } + + /** + * Update the user record with an updated profile picture + * + * TODO: Need error trapping here badly + * + * @author khoople + * + * @param int $facebookId + * @return array + */ + private function _updateUser($user, $facebookId) { + $ch = curl_init(); + + curl_setopt($ch, CURLOPT_URL, "http://graph.facebook.com/$facebookId/picture?type=large"); + + curl_setopt($ch, CURLOPT_HEADER, false); + curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); + curl_setopt($ch, CURLOPT_NOBODY, true); + curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); + curl_exec($ch); + + $url = curl_getinfo($ch, CURLINFO_EFFECTIVE_URL); + + curl_close($ch); + + $data['User'] = array( + 'image' => $url, + 'id' => $user['User']['id'] + ); + + // Create the new user + $this->User->Save($data); + + return $this->User->findById($user['User']['id']); + + } /** * Redirect user to the page where they grant access to our facebook app.