forked from jmas/uniav
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.php
123 lines (104 loc) · 3.78 KB
/
index.php
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
<?php
require 'vendor/autoload.php';
use Aws\S3\S3Client;
use Aws\S3\Exception\S3Exception;
class Uniav
{
public $email;
private $config = array();
public function __construct()
{
if (! file_exists('./config.php')) {
die('Create config.php to run app.');
}
$this->config = require('./config.php');
// 1
if (isset($_GET['e']) && filter_var(base64_decode($_GET['e']), FILTER_VALIDATE_EMAIL)) {
$this->email = base64_decode($_GET['e']);
} else {
header("HTTP/1.0 500 Internal Server Error");
die('Required parameter missing.');
}
}
static public function create()
{
return new self;
}
public function run()
{
$emailFormatted = strtolower(trim($this->email));
$emailHash = md5($emailFormatted);
// 2.a
if (! file_exists('./data/' . $emailHash . '.dat')) {
// 3.a
if ($image = $this->getFacebookPhoto()) {
// 5.a
if ($imageUrl = $this->uploadToAws($image, $emailHash . '.jpg')) {
//6
$data = $emailFormatted . "\n";
$data .= $imageUrl . "\n";
file_put_contents('./data/' . $emailHash . '.dat', $data);
header("Location:" . $imageUrl);
} else { //7.b
$this->redirectGravatar();
}
} else { //7.b
$this->redirectGravatar();
}
} else {
// 7.a
$data = file('./data/' . $emailHash . '.dat');
header("Location:" . $data[1]);
}
}
public function getCurl($url)
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.1.2) Gecko/20090729 Firefox/3.5.2 GTB5');
$result = curl_exec($ch);
curl_close($ch);
return $result;
}
public function getFacebookPhoto()
{
$url = 'http://www.facebook.com/search.php?init=s:email&q=' . strtolower(trim($this->email)) . '&type=users';
$output = $this->getCurl($url);
if (substr_count($output, 'detailedsearch_result') === 1) {
preg_match("/<a.?href=['\"](.+?facebook.com.+?)['\"].+?>/i", $output, $match);
$id = substr( $match[1], strrpos( $match[1], '/' ) +1);
return $this->getCurl('https://graph.facebook.com/'. $id .'/picture/?type=large');
} else {
return false;
}
}
public function uploadToAws($image, $keyname)
{
$s3 = S3Client::factory(array(
'key' => $this->config['awsKey'],
'secret' => $this->config['awsSecret'],
));
try {
$result = $s3->getCommand('PutObject')
->set('Bucket', $this->config['awsBucket'])
->set('Key', $keyname)
->set('Body', $image)
->set('ACL', 'public-read')
->set('ContentType', 'image/jpeg')
->getResult();
return $result['ObjectURL'];
} catch (S3Exception $e) {
echo $e->getMessage();
}
}
public function redirectGravatar()
{
header("Location:http://www.gravatar.com/avatar/" . md5(strtolower(trim($this->email))) . "?d=mm&s=400");
}
}
Uniav::create()->run();