-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindex.php
More file actions
288 lines (264 loc) · 11.4 KB
/
index.php
File metadata and controls
288 lines (264 loc) · 11.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
<?php
// Enforce https on production
if ($_SERVER['HTTP_X_FORWARDED_PROTO'] == "http" && $_SERVER['REMOTE_ADDR'] != '127.0.0.1') {
header("Location: https://" . $_SERVER["HTTP_HOST"] . $_SERVER["REQUEST_URI"]);
exit();
}
/**
* This sample app is provided to kickstart your experience using Facebook's
* resources for developers. This sample app provides examples of several
* key concepts, including authentication, the Graph API, and FQL (Facebook
* Query Language). Please visit the docs at 'developers.facebook.com/docs'
* to learn more about the resources available to you
*/
// Provides access to Facebook specific utilities defined in 'FBUtils.php'
require_once('FBUtils.php');
// Provides access to app specific values such as your app id and app secret.
// Defined in 'AppInfo.php'
require_once('AppInfo.php');
// This provides access to helper functions defined in 'utils.php'
require_once('utils.php');
/*****************************************************************************
*
* The content below provides examples of how to fetch Facebook data using the
* Graph API and FQL. It uses the helper functions defined in 'utils.php' to
* do so. You should change this section so that it prepares all of the
* information that you want to display to the user.
*
****************************************************************************/
// Log the user in, and get their access token
$token = FBUtils::login(AppInfo::getHome());
if ($token) {
// Fetch the viewer's basic information, using the token just provided
$basic = FBUtils::fetchFromFBGraph("me?access_token=$token");
$my_id = assertNumeric(idx($basic, 'id'));
// Fetch the basic info of the app that they are using
$app_id = AppInfo::appID();
$app_info = FBUtils::fetchFromFBGraph("$app_id?access_token=$token");
// This fetches some things that you like . 'limit=*" only returns * values.
// To see the format of the data you are retrieving, use the "Graph API
// Explorer" which is at https://developers.facebook.com/tools/explorer/
$likes = array_values(
idx(FBUtils::fetchFromFBGraph("me/likes?access_token=$token&limit=4"), 'data', null, false)
);
// This fetches 4 of your friends.
$friends = array_values(
idx(FBUtils::fetchFromFBGraph("me/friends?access_token=$token&limit=4"), 'data', null, false)
);
// And this returns 16 of your photos.
$photos = array_values(
idx($raw = FBUtils::fetchFromFBGraph("me/photos?access_token=$token&limit=16"), 'data', null, false)
);
// Here is an example of a FQL call that fetches all of your friends that are
// using this app
$app_using_friends = FBUtils::fql(
"SELECT uid, name, is_app_user, pic_square FROM user WHERE uid in (SELECT uid2 FROM friend WHERE uid1 = me()) AND is_app_user = 1",
$token
);
// This formats our home URL so that we can pass it as a web request
$encoded_home = urlencode(AppInfo::getHome());
$redirect_url = $encoded_home . 'close.php';
// These two URL's are links to dialogs that you will be able to use to share
// your app with others. Look under the documentation for dialogs at
// developers.facebook.com for more information
$send_url = "https://www.facebook.com/dialog/send?redirect_uri=$redirect_url&display=popup&app_id=$app_id&link=$encoded_home";
$post_to_wall_url = "https://www.facebook.com/dialog/feed?redirect_uri=$redirect_url&display=popup&app_id=$app_id";
} else {
// Stop running if we did not get a valid response from logging in
exit("Invalid credentials");
}
?>
<!-- This following code is responsible for rendering the HTML -->
<!-- content on the page. Here we use the information generated -->
<!-- in the above requests to display content that is personal -->
<!-- to whomever views the page. You would rewrite this content -->
<!-- with your own HTML content. Be sure that you sanitize any -->
<!-- content that you will be displaying to the user. idx() by -->
<!-- default will remove any html tags from the value being -->
<!-- and echoEntity() will echo the sanitized content. Both of -->
<!-- these functions are located and documented in 'utils.php'. -->
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<!-- We get the name of the app out of the information fetched -->
<title><?php echo(idx($app_info, 'name')) ?></title>
<link rel="stylesheet" href="stylesheets/screen.css" media="screen">
<!-- These are Open Graph tags. They add meta data to your -->
<!-- site that facebook uses when your content is shared -->
<!-- over facebook. You should fill these tags in with -->
<!-- your data. To learn more about Open Graph, visit -->
<!-- 'https://developers.facebook.com/docs/opengraph/' -->
<meta property="og:title" content=""/>
<meta property="og:type" content=""/>
<meta property="og:url" content=""/>
<meta property="og:image" content=""/>
<meta property="og:site_name" content=""/>
<?php echo('<meta property="fb:app_id" content="' . AppInfo::appID() . '" />'); ?>
<script>
function popup(pageURL, title,w,h) {
var left = (screen.width/2)-(w/2);
var top = (screen.height/2)-(h/2);
var targetWin = window.open(
pageURL,
title,
'toolbar=no, location=no, directories=no, status=no, menubar=no, scrollbars=no, resizable=no, copyhistory=no, width='+w+', height='+h+', top='+top+', left='+left
);
}
</script>
<!--[if IE]>
<script>
var tags = ['header', 'section'];
while(tags.length)
document.createElement(tags.pop());
</script>
<![endif]-->
</head>
<body>
<header class="clearfix">
<!-- By passing a valid access token here, we are able to display -->
<!-- the user's images without having to download or prepare -->
<!-- them ahead of time -->
<p id="picture" style="background-image: url(https://graph.facebook.com/me/picture?type=normal&access_token=<?php echoEntity($token) ?>)"></p>
<div>
<h1>Welcome, <strong><?php echo idx($basic, 'name'); ?></strong></h1>
<p class="tagline">
This is your app
<a href="<?php echo(idx($app_info, 'link'));?>"><?php echo(idx($app_info, 'name')); ?></a>
</p>
<div id="share-app">
<p>Share your app:</p>
<ul>
<li>
<a href="#" class="facebook-button" onclick="popup('<?php echo $post_to_wall_url ?>', 'Post to Wall', 580, 400);">
<span class="plus">Post to Wall</span>
</a>
</li>
<li>
<a href="#" class="facebook-button speech-bubble" onclick="popup('<?php echo $send_url ?>', 'Send', 580, 400);">
<span class="speech-bubble">Send to Friends</span>
</a>
</li>
</ul>
</div>
</div>
</header>
<section id="get-started">
<p>Welcome to your Facebook app, running on <span>heroku</span>!</p>
<a href="http://devcenter.heroku.com/articles/facebook" class="button">Learn How to Edit This App</a>
</section>
<section id="samples" class="clearfix">
<h1>Examples of the Facebook Graph API</h1>
<div class="list">
<h3>A few of your friends</h3>
<ul class="friends">
<?php
foreach ($friends as $friend) {
// Extract the pieces of info we need from the requests above
$id = assertNumeric(idx($friend, 'id'));
$name = idx($friend, 'name');
// Here we link each friend we display to their profile
echo('
<li>
<a href="#" onclick="window.open(\'http://www.facebook.com/' . $id . '\')">
<img src="https://graph.facebook.com/' . $id . '/picture?type=square" alt="' . $name . '">'
. $name . '
</a>
</li>');
}
?>
</ul>
</div>
<div class="list inline">
<h3>Recent photos</h3>
<ul class="photos">
<?php
foreach ($photos as $key => $photo) {
// Extract the pieces of info we need from the requests above
$src = idx($photo, 'source');
$name = idx($photo, 'name');
$id = assertNumeric(idx($photo, 'id'));
$class = ($key%4 === 0) ? ' class="first-column"' : '';
// Here we link each photo we display to it's location on Facebook
echo('
<li style="background-image: url(' . $src . ')"' . $class . '>
<a href="#" onclick="window.open(\'http://www.facebook.com/' .$id . '\')">
' . $name . '
</a>
</li>'
);
}
?>
</ul>
</div>
<div class="list">
<h3>Things you like</h3>
<ul class="things">
<?php
foreach ($likes as $like) {
// Extract the pieces of info we need from the requests above
$id = assertNumeric(idx($like, 'id'));
$item = idx($like, 'name');
// This display's the object that the user liked as a link to
// that object's page.
echo('
<li>
<a href="#" onclick="window.open(\'http://www.facebook.com/' .$id .'\')">
<img src="https://graph.facebook.com/' . $id . '/picture?type=square" alt="' . $item . '">
' . $item . '
</a>
</li>');
}
?>
</ul>
</div>
<div class="list">
<h3>Friends using this app</h3>
<ul class="friends">
<?php
foreach ($app_using_friends as $auf) {
// Extract the pieces of info we need from the requests above
$uid = assertNumeric(idx($auf, 'uid'));
$pic = idx($auf, 'pic_square');
$name = idx($auf, 'name');
echo('
<li>
<a href="#" onclick="window.open(\'http://www.facebook.com/' .$uid . '\')">
<img src="https://graph.facebook.com/' . $uid . '/picture?type=square" alt="' . $name . '">
' . $name . '
</a>
</li>');
}
?>
</ul>
</div>
</section>
<section id="guides" class="clearfix">
<h1>Learn More About Heroku & Facebook Apps</h1>
<ul>
<li>
<a href="http://www.heroku.com/" class="icon heroku">Heroku</a>
<p>Learn more about <a href="http://www.heroku.com/">Heroku</a>, or read developer docs in the Heroku <a href="http://devcenter.heroku.com/">Dev Center</a>.</p>
</li>
<li>
<a href="http://developers.facebook.com/docs/guides/web/" class="icon websites">Websites</a>
<p>
Drive growth and engagement on your site with
Facebook Login and Social Plugins.
</p>
</li>
<li>
<a href="http://developers.facebook.com/docs/guides/mobile/" class="icon mobile-apps">Mobile Apps</a>
<p>
Integrate with our core experience by building apps
that operate within Facebook.
</p>
</li>
<li>
<a href="http://developers.facebook.com/docs/guides/canvas/" class="icon apps-on-facebook">Apps on Facebook</a>
<p>Let users find and connect to their friends in mobile apps and games.</p>
</li>
</ul>
</section>
</body>
</html>