1 |
#!/usr/bin/perl |
2 |
# |
3 |
# Facebook appliaction which use Koha's RSS search results |
4 |
# 2009-06-27 Dobrica Pavlinusic <dpavlin@rot13.org> |
5 |
# |
6 |
# Based on WWW::Facebook::API example |
7 |
# Copyright David Leadbeater 2007 <http://dgl.cx>. |
8 |
# Licensed under the same terms as Perl itself. |
9 |
|
10 |
use strict; |
11 |
use CGI; |
12 |
use WWW::Facebook::API; |
13 |
use Data::Dump qw/dump/; |
14 |
use XML::FeedPP; |
15 |
use Time::HiRes qw/time/; |
16 |
|
17 |
our ($api_key,$secret,$rss_url,$app_path); # XXX configure this in config.pl |
18 |
require 'config.pl'; |
19 |
|
20 |
# UI elements |
21 |
|
22 |
my $facebook = WWW::Facebook::API->new( |
23 |
api_key => $api_key, |
24 |
secret => $secret, |
25 |
app_path => $app_path, |
26 |
parse => 1, |
27 |
); |
28 |
|
29 |
warn dump($facebook); |
30 |
|
31 |
sub dashboard { |
32 |
print qq| |
33 |
<fb:dashboard> |
34 |
<fb:action href=/$app_path/>Search</fb:action> |
35 |
</fb:dashboard> |
36 |
|; |
37 |
''; |
38 |
} |
39 |
|
40 |
sub form { |
41 |
my $q = shift; |
42 |
my $v = $q->param('search'); |
43 |
$v = qq| value="$v"| if defined $v; |
44 |
|
45 |
print qq| |
46 |
<form method="post" action="search"> |
47 |
<input name="search" $v> |
48 |
<input type="submit"> |
49 |
</form> |
50 |
|; |
51 |
''; |
52 |
} |
53 |
|
54 |
# dispatcher |
55 |
|
56 |
our %action_map = ( |
57 |
'' => \&index_page, |
58 |
info => \&info_page, |
59 |
search => \&search_page, |
60 |
debug => \&debug_page, |
61 |
); |
62 |
|
63 |
sub main { |
64 |
|
65 |
# Should also work with FastCGI (via CGI::Fast). |
66 |
my $q = new CGI; |
67 |
print "Content-type: text/html; encoding=utf-8\r\n\r\n"; |
68 |
|
69 |
# redirect("Must be called in facebook canvas") |
70 |
# unless $facebook->canvas->in_fb_canvas($q); |
71 |
|
72 |
my $params = $facebook->canvas->validate_sig($q); |
73 |
if ( $params->{user} ) { |
74 |
|
75 |
# Canvas takes care of setting up a session for us, no need to call the |
76 |
# auth methods. |
77 |
$facebook->session_key( $params->{session_key} ); |
78 |
} |
79 |
else { |
80 |
|
81 |
# User hasn't added app (could reject/display info/whatever) |
82 |
# (Or handle later when a user is needed). |
83 |
} |
84 |
|
85 |
my ( $action, $param ) = ( $q->path_info =~ m!^/(\w+)/?(.*)! ); |
86 |
|
87 |
print dashboard; |
88 |
|
89 |
if ( my $s = $action_map{$action} ) { |
90 |
print qq|<div style='padding-left: 2em'>|; |
91 |
$s->( $q, $params ); |
92 |
print qq|</div>|; |
93 |
} |
94 |
else { |
95 |
print "Action <tt>$action</tt> unknown"; |
96 |
warn "unknown action $action"; |
97 |
} |
98 |
|
99 |
# Clear session_key (for if running under FastCGI). |
100 |
$facebook->session_key(undef); |
101 |
} |
102 |
|
103 |
sub index_page { |
104 |
my ( $q, $params ) = @_; |
105 |
|
106 |
warn dump($params); |
107 |
|
108 |
form($q); |
109 |
|
110 |
# You could do this easier by using <fb:name>, just showing some API stuff here. |
111 |
# http://wiki.developers.facebook.com/index.php/User_(FQL) |
112 |
my $name = "You"; |
113 |
if ( my $uid = $params->{user} ) { |
114 |
my $res = $facebook->fql->query( query => qq| |
115 |
select |
116 |
first_name, last_name, |
117 |
is_app_user, |
118 |
books, |
119 |
locale |
120 |
from user |
121 |
where uid=$uid |
122 |
| ); |
123 |
warn "# $uid res = ",dump(@{$res}); |
124 |
$name = "Hello $res->[0]->{first_name}, you"; |
125 |
} |
126 |
print "$name ", ( $params ? "have" : "have't" ), |
127 |
" added this application."; |
128 |
|
129 |
if ( !$params ) { |
130 |
print "<a href='", $facebook->get_add_url, |
131 |
"'>Add this application</a>."; |
132 |
} |
133 |
|
134 |
|
135 |
|
136 |
} |
137 |
|
138 |
sub info_page { |
139 |
open( my $fh, '<', 'info.html' ); |
140 |
print <$fh>; |
141 |
close($fh); |
142 |
} |
143 |
|
144 |
sub debug_page { |
145 |
my ( $q, $params ) = @_; |
146 |
print "<pre>"; |
147 |
print dump($params); |
148 |
print "</pre>"; |
149 |
} |
150 |
|
151 |
sub search_page { |
152 |
my ( $q, $params ) = @_; |
153 |
|
154 |
my $search = $q->param('search'); |
155 |
|
156 |
my $t = time(); |
157 |
|
158 |
my $feed = XML::FeedPP->new( $rss_url . $search ); |
159 |
|
160 |
my $t = time() - $t; |
161 |
|
162 |
print form($q), qq| |
163 |
<ol> |
164 |
|; |
165 |
|
166 |
foreach my $item ( $feed->get_item ) { |
167 |
print qq|<li><a href="|, $item->link, qq|">|, $item->title, qq|</a><br>|, $item->description; |
168 |
} |
169 |
|
170 |
print qq| |
171 |
</ol> |
172 |
|
173 |
<div style="color: #ccc;">search took $t sec</div> |
174 |
|; |
175 |
|
176 |
} |
177 |
|
178 |
sub redirect { |
179 |
print("Please go <a href='" |
180 |
. $facebook->get_app_url |
181 |
. "'>to facebook</a>" ); |
182 |
exit; |
183 |
} |
184 |
|
185 |
main(); |