aboutsummaryrefslogtreecommitdiffstats
path: root/web/flickr.cgi
blob: 68e8fb396f5ad1bd2aa2f617aea5c90a332d463b (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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
#!/usr/bin/perl -w

# 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.4 2007-06-17 11:46:42 matthew Exp $

use strict;
require 5.8.0;

# Horrible boilerplate to set up appropriate library paths.
use FindBin;
use lib "$FindBin::Bin/../perllib";
use lib "$FindBin::Bin/../../perllib";
use LWP::Simple;
use mySociety::AuthToken;
use mySociety::DBHandle qw(dbh);
use mySociety::Email;
use mySociety::EmailUtil;

use Page;

BEGIN {
    mySociety::Config::set_file("$FindBin::Bin/../conf/general");
    mySociety::DBHandle::configure(
        Name => mySociety::Config::get('BCI_DB_NAME'),
        User => mySociety::Config::get('BCI_DB_USER'),
        Password => mySociety::Config::get('BCI_DB_PASS'),
        Host => mySociety::Config::get('BCI_DB_HOST', undef),
        Port => mySociety::Config::get('BCI_DB_PORT', undef)
    );
}

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='.$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='.$nsid;
            $result = get($url);
            my ($name) = $result =~ /<realname>(.*?)<\/realname>/;
	    $name ||= '';

            my $id = dbh()->selectrow_array("select nextval('flickr_id_seq');");
            dbh()->do("insert into flickr (id, nsid, name, email) values (?, ?, ?, ?)", {},
                $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' ],
        });

        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();
    dbh()->rollback();
}
Page::do_fastcgi(\&main);