aboutsummaryrefslogtreecommitdiffstats
path: root/web/flickr.cgi
blob: 0ccce316cd01cfd113629ebc6f4898bfba5b130f (plain)
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
#!/usr/bin/perl -w -I../perllib

# flickr.cgi:
# Register for Flickr usage, and update photos
#
# Copyright (c) 2007 UK Citizens Online Democracy. All rights reserved.
# Email: matthew@mysociety.org. WWW: http://www.mysociety.org
#
# $Id: flickr.cgi,v 1.9 2008-10-09 14:20:54 matthew Exp $

use strict;
use Standard;
use LWP::Simple;
use URI::Escape;
use mySociety::AuthToken;
use mySociety::Email;
use mySociety::EmailUtil;
use mySociety::Random qw(random_bytes);

sub main {
    my $q = shift;
    print Page::header($q, title=>'Flickr photo upload');
    my $out = '';
    if (my $token = $q->param('token')) {
        my $email = mySociety::AuthToken::retrieve('flickr', $token);
        if ($email) {
            my $key = mySociety::Config::get('FLICKR_API');
            my $url = 'http://api.flickr.com/services/rest/?method=flickr.people.findByEmail&api_key='.$key.'&find_email=' . uri_escape($email);
            my $result = get($url);
            my ($nsid) = $result =~ /nsid="([^"]*)"/;
            $url = 'http://api.flickr.com/services/rest/?method=flickr.people.getInfo&api_key='.$key.'&user_id=' . uri_escape($nsid);
            $result = get($url);
            my ($name) = $result =~ /<realname>(.*?)<\/realname>/;
            $name ||= '';

            my $id = dbh()->selectrow_array("select nextval('partial_user_id_seq');");
            dbh()->do("insert into partial_user (id, service, nsid, name, email, phone) values (?, 'flickr', ?, ?, ?, '')", {},
                $id, $nsid, $name, $email);
            dbh()->commit();
            $out .= $q->p('Thanks for confirming your email address. Please now tag
your photos with FixMyStreet (and geo-tag them if you want/can, automatically if possible!)
for us to pick them up.');
        } else {
            $out = $q->p(_(<<EOF));
Thank you for trying to register for your Flickr photos. We seem to have a
problem ourselves though, so <a href="/contact">please let us know what went on</a>
and we'll look into it.
EOF
        }
    } elsif (my $email = $q->param('email')) {
        my $template = File::Slurp::read_file("$FindBin::Bin/../templates/emails/flickr-confirm");
        my %h = ();
        my $token = mySociety::AuthToken::store('flickr', $email);
        $h{url} = mySociety::Config::get('BASE_URL') . '/F/' . $token;

        my $body = mySociety::Email::construct_email({
            _template_ => $template,
            _parameters_ => \%h,
            To => $email,
            From => [ mySociety::Config::get('CONTACT_EMAIL'), 'FixMyStreet' ],
            'Message-ID' => sprintf('<flickr-%s-%s@mysociety.org>', time(), unpack('h*', random_bytes(5, 1))),
        });

        my $result;
        $result = mySociety::EmailUtil::send_email($body, mySociety::Config::get('CONTACT_EMAIL'), $email);
        if ($result == mySociety::EmailUtil::EMAIL_SUCCESS) {
            $out = 'Thanks, we\'ve sent you a confirmation email!';
            dbh()->commit();
        } else {
            $out = 'Sorry, something went wrong - very alpha!';
            dbh()->rollback();
        }
    } else {
        $out .= <<EOF;
<p><strong>This feature was added for HackDay London 2007, and might not be of production quality.</strong>
Please <a href="/contact">send bug reports to us</a>.</p>
<p>Using the Flickr API, FixMyStreet can utilise all the methods of uploading photos to Flickr
to report problems to your council:</p>
<ol>
<li>Register that you're going to be using Flickr here, so we know to check your photos.
<li>Upload your photo to Flickr, for example via camera phone on location
<li>Tag the photo with FixMyStreet when uploading, or afterwards
<li>Locate the problem on Flickr's map (if you have GPS, this might be done automatically :) )
<li>FixMyStreet will find the photo, and ask you to add/ check the details;
<li>The report is then sent to the council.
</ol>

<form method="post">
<p>To begin, please enter your Flickr email address, both so we know the account to watch and
so we can email you when you upload FixMyStreet photos with a link to check and confirm
the details: <input type="text" name="email" value="" size="30">
<input type="submit" value="Go">
</p></form>
EOF
    }

    print $out;
    print Page::footer($q);
}
Page::do_fastcgi(\&main);