Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- //error_reporting(E_ALL);
- //ini_set('display_errors','On');
- //ini_set('display_errors', '1');
- require '../../vendor/autoload.php';
- use Facebook\FacebookSession;
- use Facebook\FacebookRedirectLoginHelper;
- use Facebook\FacebookCanvasLoginHelper;
- use Facebook\FacebookRequest;
- use Facebook\FacebookResponse;
- use Facebook\FacebookSDKException;
- use Facebook\FacebookRequestException;
- use Facebook\FacebookAuthorizationException;
- use Facebook\GraphUser;
- use Facebook\GraphObject;
- use Facebook\Entities\AccessToken;
- use Facebook\HttpClients\FacebookCurlHttpClient;
- use Facebook\HttpClients\FacebookHttpable;
- $fetchUserData = new FetchUserData();
- class FetchUserData
- {
- //holds the users uid
- public $id;
- public $selectionUID;
- //holds a reference to the db connection
- public $connection;
- //the output object to send back to flash
- public $outputObject;
- //a reference to thd facebook object
- public $facebook;
- public $session;
- public $currentUserObject;
- public function __construct()
- {
- //include the database connection
- include 'database.php';
- //initialize the output object
- $this->outputObject = new stdClass();
- //include the php sdk
- //include the facebook sdk library
- // require_once('facebook.php');
- //
- // //instantiate a new Facebook Object, you must replace your APP_ID with your real apps app id. do the same for the secret.
- // $this->facebook = new Facebook(array(
- // 'appId' => '123',
- // 'secret' => '123',
- // 'cookie' => true));
- FacebookSession::setDefaultApplication('123', '123');
- $helper = new FacebookCanvasLoginHelper();
- try {
- $this->session = $helper->getSession();
- } catch(FacebookRequestException $ex) {
- // When Facebook returns an error
- $this->outputObject->loggedIn = "facebook error";
- } catch(\Exception $ex) {
- // When validation fails or other local issues
- $this->outputObject->loggedIn = "validation failed";
- }
- if ($this->session) {
- // Logged in
- $this->outputObject->loggedIn = "true";
- /* make the API call */
- $request = new FacebookRequest(
- $this->session,
- 'GET',
- '/me/?fields=id,first_name,last_name,picture'
- );
- }
- $response1 = $request->execute();
- //$graphObject = $response->getGraphObject();
- /* handle the result */
- //$this->outputObject->ricky1 = json_encode(var_dump($response));
- //$this->outputObject->ricky2 = json_encode(var_dump($graphObject));
- $this->currentUserObject = json_decode($response1->getRawResponse());
- //$this->outputObject->ricky2 = json_encode($jsonObject);
- //$user_profile = (new FacebookRequest($this->session, 'GET', '/me'))->execute()->getGraphObject(GraphUser::className());
- //$this->outputObject->name = $user_profile->getId();
- $this->id = mysqli_real_escape_string($connection, $this->currentUserObject->id);
- //attempt to get the current user
- // $this->id = mysqli_real_escape_string($connection, $this->facebook->getUser());
- //store a reference to the db connection in this object
- $this->connection = $connection;
- //get the passed in selection UID
- $this->selectionUID = $this->getSelectionUID();
- //only if a selection uid was received continue
- if($this->selectionUID != NULL)
- {
- $this->retrieveUsersDataFromDatabase($this->selectionUID);
- $this->fetchDrawings();
- }
- echo json_encode($this->outputObject);
- }
- public function fetchDrawings()
- {
- //craft up a query
- $query = "SELECT * FROM drawings WHERE victimUID = '" . $this->selectionUID . "'";
- //execute the query
- $result = mysqli_query($this->connection, $query);
- //create an output array
- $outputArray = array();
- while($row = mysqli_fetch_assoc($result))
- {
- array_push($outputArray, $row);
- }
- //loop through the output array to get the first_name, last_name, pictureURL from facebook graph api
- for($i = 0; $i < count($outputArray); $i++)
- {
- $id = mysqli_real_escape_string($this->connection, $outputArray[$i]['artistUID']);
- if($id == $this->id)
- {
- $id = "me";
- $jsonObject = $this->currentUserObject;
- } else
- {
- /* make the API call */
- $request = new FacebookRequest(
- $this->session,
- 'GET',
- '/' . $id . '/?fields=id,first_name,last_name,picture'
- );
- $response1 = $request->execute();
- //$graphObject = $response->getGraphObject();
- /* handle the result */
- //$this->outputObject->ricky1 = json_encode(var_dump($response));
- //$this->outputObject->ricky2 = json_encode(var_dump($graphObject));
- $jsonObject = json_decode($response1->getRawResponse());
- }
- //$this->outputObject->ricky2 = json_encode($jsonObject);
- //$user_profile = (new FacebookRequest($this->session, 'GET', '/me'))->execute()->getGraphObject(GraphUser::className());
- //$this->outputObject->name = $user_profile->getId();
- //$this->id = mysqli_real_escape_string($connection, $jsonObject->id);
- //connect to the graph api to fetch the users first_name, last_name, and picture
- //$user = $this->facebook->api($id . "/?fields=id,first_name,last_name,picture,gender,timezone,link");
- $outputArray[$i]['uid'] = $jsonObject->id;
- $outputArray[$i]['first_name'] = $jsonObject->first_name;
- $outputArray[$i]['last_name'] = $jsonObject->last_name;
- $outputArray[$i]['pictureURL'] = $jsonObject->picture->data->url;
- //$outputArray[$i]['gender'] = $jsonObject->gender;
- // $outputArray[$i]['timezone'] = $jsonObject->timezone;
- // $outputArray[$i]['link'] = $jsonObject->link;
- //connect to the database to get the users faveorite colors
- //craft up a query
- $query = "SELECT favColor1,favColor2,motd2 FROM Users WHERE uid='" . $id . "' LIMIT 1";
- //$this->outputObject->query = $query;
- //execute the query
- $result = mysqli_query($this->connection, $query);
- while($row = mysqli_fetch_assoc($result))
- {
- //$this->outputObject->testing = $row['favColor1'] . " " . $row['favColor2'];
- $outputArray[$i]['favColor1'] = $row['favColor1'];
- $outputArray[$i]['favColor2'] = $row['favColor2'];
- $outputArray[$i]['motd2'] = $row['motd2'];
- }
- }
- //store a reference to the array in the output object
- $this->outputObject->drawingsData = $outputArray;
- }
- public function retrieveUsersDataFromDatabase($uid)
- {
- //prepare a query
- $query = "SELECT * FROM Users WHERE uid in (" . $uid . ") LIMIT 1;";
- //execute the query
- $result = mysqli_query($this->connection, $query);
- while($row = mysqli_fetch_assoc($result))
- {
- $this->outputObject->userData = $row;
- }
- if($uid == $this->id)
- {
- $uid = "me";
- $jsonObject = $this->currentUserObject;
- } else
- {
- /* make the API call */
- $request = new FacebookRequest(
- $this->session,
- 'GET',
- '/' . $uid . '/?fields=id,first_name,last_name,picture'
- );
- $response1 = $request->execute();
- //$graphObject = $response->getGraphObject();
- /* handle the result */
- //$this->outputObject->ricky1 = json_encode(var_dump($response));
- //$this->outputObject->ricky2 = json_encode(var_dump($graphObject));
- $jsonObject = json_decode($response1->getRawResponse());
- }
- //$this->outputObject->ricky2 = json_encode($jsonObject);
- //$user_profile = (new FacebookRequest($this->session, 'GET', '/me'))->execute()->getGraphObject(GraphUser::className());
- //$this->outputObject->name = $user_profile->getId();
- //$this->id = mysqli_real_escape_string($connection, $jsonObject->id);
- //fetch the users pictureURL from facebook graph api
- //$user = $this->facebook->api("/" . $uid . "?fields=id,first_name,last_name,picture,gender,timezone,link");
- $this->outputObject->userData['uid'] = $jsonObject->id;
- $this->outputObject->userData['first_name'] = $jsonObject->first_name;
- $this->outputObject->userData['last_name'] = $jsonObject->last_name;
- $this->outputObject->userData['pictureURL'] = $jsonObject->picture->data->url;
- //$this->outputObject->userData['gender'] = $jsonObject->gender;
- // $this->outputObject->userData['timezone'] = $jsonObject->timezone;
- // $this->outputObject->userData['link'] = $user['link'];
- }
- public function getSelectionUID()
- {
- if(isset($_POST['selected_uid']))
- {
- return mysqli_real_escape_string($this->connection, $_POST['selected_uid']);
- } else
- {
- return NULL;
- }
- }
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement