Passed
Push — master ( 02eff1...3c036d )
by Rakesh
01:52
created

fb-callback.php (2 issues)

Labels
Severity
1
<?php
2
if(!session_id()) {
3
  session_start();
4
}
5
require_once 'lib\Facebook\autoload.php';
6
7
  $appId='362540437809242';
8
  $appSecret='538cd04f971479ff14dc409df2fbcf3b';
9
$fb = new Facebook\Facebook([
10
  'app_id' => $appId, // variable with My Facebook App ID
11
  'app_secret' => $appSecret,
12
  'default_graph_version' => 'v3.2',
13
  ]);
14
$helper = $fb->getRedirectLoginHelper();
15
try {
16
  $accessToken = $helper->getAccessToken();
17
} catch(Facebook\Exceptions\FacebookResponseException $e) {
18
  // When Graph returns an error
19
  echo 'Graph returned an error: ' . $e->getMessage();
20
  exit;
21
} catch(Facebook\Exceptions\FacebookSDKException $e) {
22
  // When validation fails or other local issues
23
  echo 'Facebook SDK returned an error: ' . $e->getMessage();
24
  exit;
25
}
26
if (! isset($accessToken)) {
27
  
28
  if ($helper->getError()) {
29
    header('HTTP/1.0 401 Unauthorized');
30
    echo "Error: " . $helper->getError() . "\n";
31
    echo "Error Code: " . $helper->getErrorCode() . "\n";
32
    echo "Error Reason: " . $helper->getErrorReason() . "\n";
33
    echo "Error Description: " . $helper->getErrorDescription() . "\n";
34
  } else {
35
    header('HTTP/1.0 400 Bad Request');
36
    echo 'Bad request';
37
  }
38
  exit;
39
}
40
// Logged in
41
//echo '<h3>Access Token</h3>';
42
//($accessToken->getValue());
43
// The OAuth 2.0 client handler helps us manage access tokens
44
$oAuth2Client = $fb->getOAuth2Client();
45
// Get the access token metadata from /debug_token
46
$tokenMetadata = $oAuth2Client->debugToken($accessToken);
47
echo '<h3>Facebook Photos Challenge</h3>';
48
//var_dump($tokenMetadata);
49
// Validation (these will throw FacebookSDKException's when they fail)
50
$tokenMetadata->validateAppId('362540437809242'); // My Facebook App ID
51
// If you know the user ID this access token belongs to, you can validate it here
52
//$tokenMetadata->validateUserId('123');
53
$tokenMetadata->validateExpiration();
54
if (! $accessToken->isLongLived()) {
55
  // Exchanges a short-lived access token for a long-lived one
56
  try {
57
    $accessToken = $oAuth2Client->getLongLivedAccessToken($accessToken);
58
  } catch (Facebook\Exceptions\FacebookSDKException $e) {
59
    echo "<p>Error getting long-lived access token: " . $e->getMessage() . "</p>\n\n";
60
    exit;
61
  }
62
  echo '<h3>Long-lived</h3>';
63
  var_dump($accessToken->getValue());
64
}
65
$_SESSION['fb_access_token'] = (string) $accessToken;
66
try {
67
  // Returns a `Facebook\FacebookResponse` object
68
  $response = $fb->get('/me?fields=id,name', $accessToken);
69
} catch(Facebook\Exceptions\FacebookResponseException $e) {
70
  echo 'Graph returned an error: ' . $e->getMessage();
71
  exit;
72
} catch(Facebook\Exceptions\FacebookSDKException $e) {
73
  echo 'Facebook SDK returned an error: ' . $e->getMessage();
74
  exit;
75
}
76
$user = $response->getGraphUser();
77
78
echo 'Welcome, ' . $user['name'];
79
echo '<a href="logout.php"  >Logout</a>';
80
81
// Get photo albums of Facebook page using Facebook Graph API
82
$fields = "id,name,description,link,cover_photo,count";
83
$fb_page_id = $user['id'];
84
$graphAlbLink = "https://graph.facebook.com/v3.2/{$fb_page_id}/albums?fields={$fields}&access_token={$accessToken}";
85
86
$jsonData = file_get_contents($graphAlbLink);
87
$fbAlbumObj = json_decode($jsonData, true, 512, JSON_BIGINT_AS_STRING);
88
89
// Facebook albums content
90
$fbAlbumData = $fbAlbumObj['data'];
91
92
// Render all photo albums
93
echo "<br/><br/>";
94
foreach($fbAlbumData as $data){
95
  $id = isset($data['id'])?$data['id']:'';
96
  $name = isset($data['name'])?$data['name']:'';
97
  $description = isset($data['description'])?$data['description']:'';
98
  $link = isset($data['link'])?$data['link']:'';
99
  $cover_photo_id = isset($data['cover_photo']['id'])?$data['cover_photo']['id']:'';
100
  $count = isset($data['count'])?$data['count']:'';
101
  
102
  $pictureLink = "fb-callback.php?album_id={$id}&album_name={$name}";
103
  
104
105
  echo "<a href='{$pictureLink}'>";
106
  $cover_photo_id = (!empty($cover_photo_id ))?$cover_photo_id : 123456;
107
  echo "<img width=100px height=100px src='https://graph.facebook.com/v3.2/{$cover_photo_id}/picture?access_token={$accessToken}' alt=''>";
108
  echo "</a>";
109
  echo "<p>{$name}</p>";
110
111
  $photoCount = ($count > 1)?$count. 'Photos':$count. 'Photo';
112
  
113
  echo "<p><span style='color:#888;'>{$photoCount} / <a href='{$link}' target='_blank'>View on Facebook</a></span></p>";
114
  echo "<p>{$description}</p>";
115
}
116
117
$album_id = isset($_GET['album_id'])?$_GET['album_id']:header('Location: fb-callback.php');
0 ignored issues
show
Are you sure the usage of header('Location: fb-callback.php') is correct as it seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
118
$album_name = isset($_GET['album_name'])?$_GET['album_name']:header('Location: fb-callback.php');
0 ignored issues
show
Are you sure the usage of header('Location: fb-callback.php') is correct as it seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
119
120
// Get photos of Facebook page album using Facebook Graph API
121
$graphPhoLink = "https://graph.facebook.com/v3.2/{$album_id}/photos?fields=source,images,name&access_token={$accessToken}";
122
$jsonData = file_get_contents($graphPhoLink);
123
$fbPhotoObj = json_decode($jsonData, true, 512, JSON_BIGINT_AS_STRING);
124
125
// Facebook photos content
126
$fbPhotoData = $fbPhotoObj['data'];
127
128
echo "<h2>".$album_name."</h2>";
129
130
// Render all photos   
131
if (is_array($fbPhotoData) || is_object($fbPhotoData))
132
{  
133
  foreach($fbPhotoData as $data)
134
  {
135
      $imageData = end($data['images']);
136
      $imgSource = isset($imageData['source'])?$imageData['source']:'';
137
      $name = isset($data['name'])?$data['name']:'';
138
139
      echo "<div class='item'>";
140
      echo "<img src='{$imgSource}' alt=''>";
141
      echo "<p>{$name}</p>";
142
      echo "</div>";
143
  }
144
}
145
146
147
echo'</div>';
148
149
echo "<div class='slideshow-container'>";
150
151
// Render all photos 
152
  if (is_array($fbPhotoData) || is_object($fbPhotoData))
153
  {   
154
      foreach($fbPhotoData as $data){
155
      $imageData = end($data['images']);
156
      $imgSource = isset($imageData['source'])?$imageData['source']:'';
157
      $name = isset($data['name'])?$data['name']:'';
158
159
      echo "<div class='mySlides fade'>";
160
      echo "<img src='{$imgSource}' alt='' style='width:100%'>";
161
      echo "<div class='text'>{$name}</div>";
162
      echo "</div>";
163
  }
164
}
165
166
?>
167
<br>
168
<html><head>
169
<link rel="stylesheet" type="text/css" href="lib\CSS\slider.css"></head><body>
170
<div style="text-align:center">
171
<?php
172
if (is_array($fbPhotoData) || is_object($fbPhotoData))
173
{ 
174
  foreach($fbPhotoData as $data){
175
    echo "<span class='dot'></span>";
176
 }
177
}
178
?>
179
</div>
180
</div>
181
</body></html>
182
183