-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathirt-callback.php
175 lines (143 loc) · 5.45 KB
/
irt-callback.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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
<?php
require_once('irt-functions.php');
//
$irtCode = $_GET['code'];
$irtToken = $_GET['hub_challenge'];
$irtPost = file_get_contents('php://input');
//
// setting and saving the main authorization
//
if (isset($irtCode)) {
$irtPostAuthArgs = array(
'client_id' => '' . $irtOptions['clientID'] . '',
'client_secret' => '' . $irtOptions['clientSecret'] . '',
'grant_type' => 'authorization_code',
'redirect_uri' => IRT_URL . 'irt-callback.php',
'code' => '' . $irtCode . ''
);
foreach($irtPostAuthArgs as $key=>$value) {
$fields_string .= $key . '=' . $value . '&';
}
$fields_string = rtrim($fields_string,'&');
$url = 'https://api.instagram.com/oauth/access_token';
$irtPostAuth = curl_init();
curl_setopt($irtPostAuth, CURLOPT_URL, $url);
curl_setopt($irtPostAuth, CURLOPT_HEADER, false);
curl_setopt($irtPostAuth, CURLOPT_RETURNTRANSFER, true);
curl_setopt($irtPostAuth, CURLOPT_POST, true);
curl_setopt($irtPostAuth, CURLOPT_POSTFIELDS, $fields_string);
$irtPostAuthed = curl_exec($irtPostAuth);
curl_close($irtPostAuth);
//
$auth = json_decode($irtPostAuthed);
$authUser = $auth->user->id;
$authToken = $auth->access_token;
$irtAuthSave = curl_init();
curl_setopt($irtAuthSave, CURLOPT_URL, IRT_URL . 'irt-editor.php');
curl_setopt($irtAuthSave, CURLOPT_HEADER, false);
curl_setopt($irtAuthSave, CURLOPT_RETURNTRANSFER, true);
curl_setopt($irtAuthSave, CURLOPT_POST, true);
curl_setopt($irtAuthSave, CURLOPT_POSTFIELDS, 'user=' . $authUser . '&token=' . $authToken . '');
$irtAuthSaved = curl_exec($irtAuthSave);
curl_close($irtAuthSave);
//
$irtPostSubArgs = array(
'client_id' => '' . $irtOptions['clientID'] . '',
'client_secret' => '' . $irtOptions['clientSecret'] . '',
'object' => 'user',
'object_id' => '' . $authUser . '',
'aspect' => 'media',
'verify_token' => '' . $authToken . '',
'callback_url' => IRT_URL . 'irt-callback.php',
);
foreach($irtPostSubArgs as $key=>$value) {
$fields_string .= $key . '=' . $value . '&';
}
$fields_string = rtrim($fields_string,'&');
$url = 'https://api.instagram.com/v1/subscriptions/';
$irtPostSub = curl_init();
curl_setopt($irtPostSub, CURLOPT_URL, $url);
curl_setopt($irtPostSub, CURLOPT_HEADER, false);
curl_setopt($irtPostSub, CURLOPT_RETURNTRANSFER, true);
curl_setopt($irtPostSub, CURLOPT_POST, true);
curl_setopt($irtPostSub, CURLOPT_POSTFIELDS, $fields_string);
$result = curl_exec($irtPostSub);
curl_close($irtPostSub);
$resultObj = json_decode($result);
if ($resultObj->meta->code == 200) {
if ($irtOptions['emailNotify']['notify'] == 'yes') irtEmailNotifications($irtOptions['emailNotify']['address'], 'In Real-Time OAuth Success', 'token response = '. $authToken . "\r\n" . 'authorized UserID = '. $authUser . "\r\n" . 'full result = ' . $result);
echo '<html>
<head>
<style>
body {margin: 30% 0 0 0; background-color: #ECECEC; text-align: center;}
h2 {font: 23px/29px "HelveticaNeue-Light","Helvetica Neue Light","Helvetica Neue",sans-serif;}
p {font: 12px/1.4em sans-serif;}
</style>
</head>
<body>
<h2>Congrats!</h2>
<p>You\'re all done. <b>Happy Instagram-ing.</b></p>
<p>(this page will close itself in 3 seconds.)</p>
</body>
<script>
setTimeout(function() { window.close(); }, 3000);
</script>
</html>';
} else {
if ($irtOptions['emailNotify']['notify'] == 'yes') irtEmailNotifications($irtOptions['emailNotify']['address'], 'In Real-Time OAuth Failure!', 'token response = '. $authToken . "\r\n" . 'authorized UserID = '. $authUser . "\r\n" . 'full result = ' . $result);
echo '<html>
<head>
<style>
body {margin: 30% 0 0 0; background-color: #ECECEC; text-align: center;}
h2 {font: 23px/29px "HelveticaNeue-Light","Helvetica Neue Light","Helvetica Neue",sans-serif;}
p {font: 12px/1.4em sans-serif;}
</style>
</head>
<body>
<h2>Authorization Failed!</h2>
<p><b>Please check your configuration and try again.</b></p>
<p>!!! Make sure your plug-ins folder is accessible to the public and not password protected !!!</p>
</body>
</html>';
}
}
//
// the token echo for authorization
//
if (isset($irtToken)) {
echo $irtToken;
}
//
// the main callback functionality. this is the meat.
//
if ($irtCode == null && $irtToken == null) {
$irtBaseline = $irtPost;
$irtResponse = json_decode($irtPost, true);
$irtTime = $irtResponse[0]['time'];
$irtImageReq = 'https://api.instagram.com/v1/users/'. $irtOptions['userID'] .'/media/recent/?access_token='. $irtOptions['accessToken'] .'&max_timestamp='. $irtTime .'&count=8';
//
//
$irtPostGet = curl_init();
$url = $irtImageReq;
curl_setopt($irtPostGet, CURLOPT_URL, $url);
curl_setopt($irtPostGet, CURLOPT_HEADER, false);
curl_setopt($irtPostGet, CURLOPT_RETURNTRANSFER, true);
//
$irtPost = curl_exec($irtPostGet);
curl_close($irtPostGet);
//
$irtPost = json_decode($irtPost, true);
// the mail sanity check.
if ($irtOptions['emailNotify']['notify'] == 'yes') irtEmailNotifications($irtOptions['emailNotify']['address'], 'In Real-Time : sanity check', 'baseline = ' . $irtBaseline . "\r\n" . 'response data = ' . print_r($irtResponse) . "\r\n" . 'post data = '. print_r($irtPost) . "\r\n" . 'request string = ' . $irtImageReq . '');
//
if (is_dir($irtUploadFolder['relative'])) { // check to see if directory exists
irtAssetChecker($irtPost['data']);
} else { // if not, make it.
if (!mkdir($irtUploadFolder['relative'])) {
die('Failed to create folders...');
} else {
irtAssetChecker($irtPost['data']);
}
}
}
?>