1 |
#!/usr/bin/perl |
2 |
# Copyright David Leadbeater 2007 <http://dgl.cx>. |
3 |
# Licensed under the same terms as Perl itself. |
4 |
|
5 |
# A simple example of using WWW::Facebook::API within a facebook canvas. |
6 |
# You will need to change the api_key, secret and app_path below to match your |
7 |
# account. To get an api_key and secret, go to |
8 |
# http://www.facebook.com/developers/editapp.php?new, choose "Use FBML" and |
9 |
# enter a unique name for the canvas which you should put into app_path. |
10 |
|
11 |
use strict; |
12 |
use CGI; |
13 |
use WWW::Facebook::API; |
14 |
use Data::Dump qw/dump/; |
15 |
use XML::FeedPP; |
16 |
|
17 |
our ($api_key,$secret,$rss_url); # XXX configure this in config.pl |
18 |
require 'config.pl'; |
19 |
|
20 |
my $facebook = WWW::Facebook::API->new( |
21 |
api_key => $api_key, |
22 |
secret => $secret, |
23 |
app_path => "ffzg-koha", |
24 |
parse => 1, |
25 |
); |
26 |
|
27 |
our %action_map = ( |
28 |
'' => \&index_page, |
29 |
info => \&info_page, |
30 |
search => \&search_page, |
31 |
); |
32 |
|
33 |
sub main { |
34 |
|
35 |
# Should also work with FastCGI (via CGI::Fast). |
36 |
my $q = new CGI; |
37 |
print "Content-type: text/html; encoding=utf-8\r\n\r\n"; |
38 |
|
39 |
# redirect("Must be called in facebook canvas") |
40 |
# unless $facebook->canvas->in_fb_canvas($q); |
41 |
|
42 |
my $params = $facebook->canvas->validate_sig($q); |
43 |
if ( $params->{user} ) { |
44 |
|
45 |
# Canvas takes care of setting up a session for us, no need to call the |
46 |
# auth methods. |
47 |
$facebook->session_key( $params->{session_key} ); |
48 |
} |
49 |
else { |
50 |
|
51 |
# User hasn't added app (could reject/display info/whatever) |
52 |
# (Or handle later when a user is needed). |
53 |
} |
54 |
|
55 |
my ( $action, $param ) = ( $q->path_info =~ m!^/(\w+)/?(.*)! ); |
56 |
if ( my $s = $action_map{$action} ) { |
57 |
$s->( $q, $params ); |
58 |
} |
59 |
else { |
60 |
div_error("Action unknown"); |
61 |
} |
62 |
|
63 |
# Clear session_key (for if running under FastCGI). |
64 |
$facebook->session_key(undef); |
65 |
} |
66 |
|
67 |
sub index_page { |
68 |
my ( $q, $params ) = @_; |
69 |
|
70 |
print "<fb:header/><div style='padding: 20px'>"; |
71 |
|
72 |
# You could do this easier by using <fb:name>, just showing some API stuff here. |
73 |
my $name = "You"; |
74 |
if ($params) { |
75 |
my $res = |
76 |
$facebook->fql->query( query => |
77 |
"SELECT first_name FROM user WHERE uid=$params->{canvas_user}" ); |
78 |
$name = "Hi $res->[0]->{first_name}, you"; |
79 |
} |
80 |
print "$name ", ( $params ? "have" : "have't" ), |
81 |
" added this application. Some <a href='info'>info</a>."; |
82 |
if ( !$params ) { |
83 |
print "<a href='", $facebook->get_add_url, |
84 |
"'>Add this application</a>."; |
85 |
} |
86 |
print "</div>"; |
87 |
|
88 |
print qq{ |
89 |
|
90 |
<form method="post" action="search"> |
91 |
<input name="search"> |
92 |
<input type="submit"> |
93 |
</form> |
94 |
|
95 |
}; |
96 |
|
97 |
|
98 |
} |
99 |
|
100 |
sub info_page { |
101 |
my ( $q, $params ) = @_; |
102 |
|
103 |
print "<fb:header/><div style='padding: 20px'><pre>"; |
104 |
print dump($params); |
105 |
print "</pre></div>"; |
106 |
} |
107 |
|
108 |
sub search_page { |
109 |
my ( $q, $params ) = @_; |
110 |
|
111 |
my $search = $q->param('search'); |
112 |
|
113 |
print qq| |
114 |
<fb:header/> |
115 |
<div style='padding: 20px'> |
116 |
Search results for <em>$search</em> |
117 |
<ol> |
118 |
|; |
119 |
|
120 |
my $feed = XML::FeedPP->new( $rss_url . $search ); |
121 |
|
122 |
foreach my $item ( $feed->get_item ) { |
123 |
print qq|<li><a href="|, $item->link, qq|">|, $item->title, qq|</a><br>|, $item->description; |
124 |
} |
125 |
|
126 |
print qq| |
127 |
</ol> |
128 |
</div> |
129 |
|; |
130 |
|
131 |
} |
132 |
|
133 |
sub redirect { |
134 |
div_error("Please go <a href='" |
135 |
. $facebook->get_app_url |
136 |
. "'>to facebook</a>" ); |
137 |
exit; |
138 |
} |
139 |
|
140 |
sub div_error { |
141 |
print "<div style='padding: 20px'>", join( "", @_ ), "</div>"; |
142 |
} |
143 |
|
144 |
main(); |