1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
use Larabros\Elogram\Client; |
4
|
|
|
|
5
|
|
|
class InstagramAccount extends DataObject |
|
|
|
|
6
|
|
|
{ |
7
|
|
|
/** |
8
|
|
|
* @config |
9
|
|
|
* @var string |
10
|
|
|
*/ |
11
|
|
|
private static $client_id; |
|
|
|
|
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* @config |
15
|
|
|
* @var string |
16
|
|
|
*/ |
17
|
|
|
private static $client_secret; |
|
|
|
|
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @config |
21
|
|
|
* @var string |
22
|
|
|
*/ |
23
|
|
|
private static $redirect_path; |
|
|
|
|
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @config |
27
|
|
|
*/ |
28
|
|
|
private static $items_per_page = 9; |
|
|
|
|
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var array |
32
|
|
|
*/ |
33
|
|
|
private static $db = [ |
|
|
|
|
34
|
|
|
'Title' => 'Varchar', |
35
|
|
|
'AccessToken' => 'Text', |
36
|
|
|
]; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @return FieldList |
40
|
|
|
*/ |
41
|
|
|
public function getCMSFields() |
42
|
|
|
{ |
43
|
|
|
$fields = parent::getCMSFields(); |
44
|
|
|
|
45
|
|
|
$fields->removeByName('AccessToken'); |
46
|
|
|
|
47
|
|
|
if ($this->AccessToken) { |
|
|
|
|
48
|
|
|
$token = json_decode($this->AccessToken); |
|
|
|
|
49
|
|
|
|
50
|
|
|
$usernameField = LiteralField::create( |
51
|
|
|
'Title', |
52
|
|
|
'<div class="field">' . |
53
|
|
|
'<label class="left">' . |
54
|
|
|
_t('Instagram.FieldLabelTitle', 'Username') . |
55
|
|
|
'</label>' . |
56
|
|
|
'<div class="middleColumn" style="padding-top:8px;">' . |
57
|
|
|
'<a ' . |
58
|
|
|
"href='https://www.instagram.com/{$token->user->username}' " . |
59
|
|
|
'title="View on Instagram" ' . |
60
|
|
|
'target="_blank">' . |
61
|
|
|
$token->user->username . |
62
|
|
|
'</a>' . |
63
|
|
|
'</div>' . |
64
|
|
|
'</div>' |
65
|
|
|
); |
66
|
|
|
} else { |
67
|
|
|
$usernameField = Textfield::create('Title', _t('Instagram.FieldLabelTitle', 'Username')); |
68
|
|
|
$usernameField->setDescription( |
69
|
|
|
_t( |
70
|
|
|
'Instagram.FieldDescriptionTitle', |
71
|
|
|
'The Instagram account you want to pull media from.' |
72
|
|
|
) |
73
|
|
|
); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
$fields->addFieldToTab('Root.Main', $usernameField); |
77
|
|
|
|
78
|
|
|
$this->extend('updateCMSFields', $fields); |
79
|
|
|
|
80
|
|
|
return $fields; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* @return FieldList |
85
|
|
|
*/ |
86
|
|
|
public function getCMSActions() |
87
|
|
|
{ |
88
|
|
|
$actions = parent::getCMSActions(); |
89
|
|
|
|
90
|
|
|
if (!$this->ID || $this->AccessToken) { |
|
|
|
|
91
|
|
|
$this->extend('updateCMSActions', $actions); |
92
|
|
|
return $actions; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
$client = self::getNewInstagramClient(); |
96
|
|
|
$loginURL = $client->getLoginUrl(); |
97
|
|
|
|
98
|
|
|
$this->setSessionOAuthState($this->getOAuthStateValueFromLoginURL($loginURL)); |
99
|
|
|
|
100
|
|
|
$actions->push( |
101
|
|
|
LiteralField::create( |
102
|
|
|
'OAuthLink', |
103
|
|
|
'<a class="ss-ui-button" href="' . $loginURL . '">' . |
104
|
|
|
_t('Instagram.ButtonLabelAuthoriseAccount', 'Authorise account') . |
105
|
|
|
'</a>' |
106
|
|
|
) |
107
|
|
|
); |
108
|
|
|
|
109
|
|
|
$this->extend('updateCMSActions', $actions); |
110
|
|
|
|
111
|
|
|
return $actions; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* @return RequiredFields |
116
|
|
|
*/ |
117
|
|
|
public function getCMSValidator() |
118
|
|
|
{ |
119
|
|
|
return new RequiredFields('Title'); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* Ensures the person to authorising the account is logged into Instagram as the correct user |
124
|
|
|
* before setting the token. |
125
|
|
|
* |
126
|
|
|
* For example if the user is logged into Instagram as 'FooUser' and they attempt to set a token |
127
|
|
|
* for the InstagramAccount record 'InstagramAccount', the token will contain data relating to |
128
|
|
|
* the 'FooUser' account. So if this happen we display a message telling the user they need to |
129
|
|
|
* log out of Instagram before they can authorise another account. |
130
|
|
|
* |
131
|
|
|
* @param string $token |
132
|
|
|
*/ |
133
|
1 |
|
public function updateAccessToken($token, $state) |
134
|
|
|
{ |
135
|
1 |
|
$newToken = json_decode($token); |
136
|
|
|
|
137
|
1 |
|
if ($state !== $this->getSessionOAuthState() || |
138
|
1 |
|
$newToken->user->username !== $this->getField('Title')) { |
139
|
1 |
|
throw new Exception('Trying to set token on wrong InstagramAccount'); |
140
|
|
|
} |
141
|
|
|
|
142
|
1 |
|
if (!$currentToken = $this->getField('AccessToken')) { |
143
|
1 |
|
$this->setField('AccessToken', $token); |
144
|
1 |
|
return; |
145
|
|
|
} |
146
|
|
|
|
147
|
1 |
|
$currentToken = json_decode($currentToken); |
148
|
|
|
|
149
|
1 |
|
if ($newToken->user->id !== $currentToken->user->id) { |
150
|
1 |
|
throw new Exception('Trying to set token on wrong InstagramAccount'); |
151
|
|
|
} |
152
|
|
|
|
153
|
1 |
|
$this->setField('AccessToken', $token); |
154
|
1 |
|
} |
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* Create a configured Instagram API interface. |
158
|
|
|
* |
159
|
|
|
* @param string $token |
160
|
|
|
* @return MetzWeb\Instagram\Instagram |
161
|
|
|
*/ |
162
|
|
|
public static function getNewInstagramClient($token = null) |
163
|
|
|
{ |
164
|
|
|
$client_id = Config::inst()->get('InstagramAccount', 'client_id'); |
165
|
|
|
$client_secret = Config::inst()->get('InstagramAccount', 'client_secret'); |
166
|
|
|
$redirect_path = Config::inst()->get('InstagramAccount', 'redirect_path'); |
167
|
|
|
|
168
|
|
|
if (!$client_id) { |
169
|
|
|
user_error( |
170
|
|
|
'Add a client_id to config (InstagramAdmin::client_id)', |
171
|
|
|
E_USER_ERROR |
172
|
|
|
); |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
if (!$client_secret) { |
176
|
|
|
user_error( |
177
|
|
|
'Add a client_secret to config (InstagramAdmin::client_secret)', |
178
|
|
|
E_USER_ERROR |
179
|
|
|
); |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
return new Client( |
183
|
|
|
$client_id, |
184
|
|
|
$client_secret, |
185
|
|
|
$token, |
186
|
|
|
Director::absoluteBaseURL() . $redirect_path |
187
|
|
|
); |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
/** |
191
|
|
|
* Gets the 'state' value from an OAuth login URL. |
192
|
|
|
* |
193
|
|
|
* @param string $loginURL |
194
|
|
|
* @return string|null |
195
|
|
|
*/ |
196
|
1 |
|
public function getOAuthStateValueFromLoginURL($loginURL = null) |
197
|
|
|
{ |
198
|
1 |
|
if (!$loginURL) { |
|
|
|
|
199
|
1 |
|
return null; |
200
|
|
|
} |
201
|
|
|
|
202
|
1 |
|
$parts = parse_url($loginURL); |
203
|
1 |
|
parse_str($parts['query'], $query); |
204
|
|
|
|
205
|
1 |
|
return array_key_exists('state', $query) |
206
|
1 |
|
? $query['state'] |
207
|
1 |
|
: null; |
208
|
|
|
} |
209
|
|
|
|
210
|
|
|
/** |
211
|
|
|
* Gets the InstsgramAccount's OAuth state from Session. |
212
|
|
|
* |
213
|
|
|
* @return string|null |
214
|
|
|
*/ |
215
|
|
|
public function getSessionOAuthState() |
216
|
|
|
{ |
217
|
|
|
$instagramAccounts = Session::get('InstagramAccounts'); |
218
|
|
|
|
219
|
|
|
if (!$this->ID || !$instagramAccounts || !array_key_exists($this->ID, $instagramAccounts)) { |
220
|
|
|
return null; |
221
|
|
|
} |
222
|
|
|
|
223
|
|
|
return $instagramAccounts[$this->ID]; |
224
|
|
|
} |
225
|
|
|
|
226
|
|
|
/** |
227
|
|
|
* Gets the authorised user's Instagram ID from the AccessToken. |
228
|
|
|
* |
229
|
|
|
* @return string|null |
230
|
|
|
*/ |
231
|
|
|
public function getInstagramID() |
232
|
|
|
{ |
233
|
|
|
if (!$token = $this->getField('AccessToken')) { |
234
|
|
|
return null; |
235
|
|
|
} |
236
|
|
|
|
237
|
|
|
return json_decode($token)->user->id; |
238
|
|
|
} |
239
|
|
|
|
240
|
|
|
/** |
241
|
|
|
* Checks if the passed ID is a valid pattern. |
242
|
|
|
* |
243
|
|
|
* @param string $mediaID |
244
|
|
|
* @return boolean |
245
|
|
|
*/ |
246
|
1 |
|
public function isValidMediaID($mediaID = null) |
247
|
|
|
{ |
248
|
1 |
|
if (!$mediaID || !$instagramID = $this->getInstagramID()) { |
|
|
|
|
249
|
1 |
|
return false; |
250
|
|
|
} |
251
|
|
|
|
252
|
1 |
|
$pattern = '/^(\d{18}|\d{19})_' . $instagramID . '$/'; |
253
|
|
|
|
254
|
1 |
|
return preg_match($pattern, $mediaID) === 1; |
255
|
|
|
} |
256
|
|
|
|
257
|
|
|
/** |
258
|
|
|
* Sets a Session variable which is used to keep track of |
259
|
|
|
* the InstagramAccount through the OAuth flow. |
260
|
|
|
* |
261
|
|
|
* @param string $state |
262
|
|
|
*/ |
263
|
|
|
private function setSessionOAuthState($state = null) |
264
|
|
|
{ |
265
|
|
|
if (!$this->ID || !$state) { |
|
|
|
|
266
|
|
|
return null; |
267
|
|
|
} |
268
|
|
|
|
269
|
|
|
$instagramAccounts = Session::get('InstagramAccounts'); |
270
|
|
|
$instagramAccounts = $instagramAccounts ? $instagramAccounts : []; |
271
|
|
|
|
272
|
|
|
$instagramAccounts[$this->ID] = $state; |
273
|
|
|
|
274
|
|
|
Session::set('InstagramAccounts', $instagramAccounts); |
|
|
|
|
275
|
|
|
} |
276
|
|
|
|
277
|
|
|
/** |
278
|
|
|
* Gets a list of media from the user's Instagram. |
279
|
|
|
* |
280
|
|
|
* @param string $maxID Return media earlier than this ID |
281
|
|
|
* @return Larabros\Elogram\Http\Response|null |
282
|
|
|
*/ |
283
|
|
|
public function getMedia($maxID = null) |
284
|
|
|
{ |
285
|
|
|
if ( |
286
|
|
|
!is_string($this->getField('AccessToken')) || |
287
|
|
|
($maxID && !$this->isValidMediaID($maxID)) |
|
|
|
|
288
|
|
|
) { |
289
|
|
|
return null; |
290
|
|
|
} |
291
|
|
|
|
292
|
|
|
$client = $this->getNewInstagramClient($this->getField('AccessToken')); |
293
|
|
|
$client->secureRequests(); |
294
|
|
|
|
295
|
|
|
return $client->users()->getMedia('self', $this->config()->items_per_page, null, $maxID); |
296
|
|
|
} |
297
|
|
|
} |
298
|
|
|
|
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.