/** * Gets user image (goes out to gravitar / facebook) * * @param bool $hard_refresh * @return object */ public function getImage($hard_refresh = false) { global $f3, $db, $logger; // If an image is already set then use that if (!$hard_refresh) { if (isset($this->image) && $this->image) { return $this->image; } } // No email or id? Nothing we can do here if (!$this->email && !$this->id) { return false; } // If is a facebook account, we'll hard refresh the image if ($hard_refresh && $this->facebook_id) { $session = new FacebookSession(getFacebookAccessToken()); try { // Get information about fb user $me = (new FacebookRequest($session, 'GET', '/me?fields=picture.height(200)'))->execute()->getGraphObject(GraphUser::className())->asArray(); if (@$me['picture']) { $sql = "UPDATE user "; $sql .= "SET image = ? "; $sql .= "WHERE id = ?;"; $query = $db->prepare($sql); $sql_params = array($me['picture']->data->url, $this->id); $query->execute($sql_params); if (!$query->execute($sql_params)) { $logger->write(sprintf('Couldnt update facebook photo for user ID %s', $this->id)); } } // The Graph API returned an error } catch (FacebookRequestException $e) { echo $e->getMessage(); // Some other error occurred } catch (\Exception $e) { echo $e->getMessage(); } } // Next we'll check the database for an image, $sql = "SELECT image FROM user WHERE "; if ($this->email) { $sql .= 'email = ?'; $sql_params = array($this->email); } else { $sql .= 'id = ?'; $sql_params = array($this->id); } $query = $db->prepare($sql); $query->execute($sql_params); if ($image = $query->fetchColumn()) { return $image; } // As a last resort we'll reach out to gravatar return getUserImage($this->email); }
<?php include_once "modules/config.php"; if (!loggedIn()) { header('Location: login.php'); exit; } else { $query = $coll->findOne(array('username' => $_SESSION["username"])); $code = $_REQUEST["code"]; // get user access_token if (isset($code)) { $access_token = getFacebookAccessToken($code); setFacebookAccessToken($query['username'], $access_token); header('Location: facebook-get-user.php'); exit; } }