-
Notifications
You must be signed in to change notification settings - Fork 1
/
InstagramAPI.module
executable file
·346 lines (273 loc) · 8.95 KB
/
InstagramAPI.module
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
<?php namespace ProcessWire;
/**
* Facebook API
*
* Copyright:
*
* IDT Media - Goran Ilic & Tapio Löytty
* Web: www.i-do-this.com
* Email: [email protected]
*
*
* Authors:
*
* Goran Ilic, <[email protected]>
* Web: www.ich-mach-das.at
*
* Tapio Löytty, <[email protected]>
* Web: www.orange-media.fi
*
*
* ProcessWire 2.x
* Copyright (C) 2014 by Ryan Cramer
* Licensed under GNU/GPL v2, see LICENSE.TXT
*
* http://processwire.com
*
*/
use MetzWeb\Instagram\Instagram;
class InstagramAPI extends WireData implements Module, ConfigurableModule
{
#----------------------------------------------------------------
# Constants
#----------------------------------------------------------------
const callback = 'instagramapi-callback';
const token = 'InstagramAPI.token'; // string
#----------------------------------------------------------------
# Attributes
#----------------------------------------------------------------
protected $instance;
protected $options = [
'clientId' => '',
'clientSecret' => '',
];
#----------------------------------------------------------------
# Magic methods
#----------------------------------------------------------------
public function __construct()
{
require_once(__DIR__ . '/vendor/autoload.php');
}
#----------------------------------------------------------------
# Interface methods: WireData/Wire
#----------------------------------------------------------------
public function set($key, $value)
{
if($key == 'options' && is_array($value)) $this->options = array_merge($this->options, $value);
else if(array_key_exists($key, $this->options)) $this->options[$key] = $value;
else return parent::set($key, $value);
return $this;
}
public function get($key)
{
if(array_key_exists($key, $this->options)) return $this->options[$key];
elseif($key == 'callbackUrl') return $this->getCallbackUrl();
elseif($key == 'token') return $this->getToken();
return parent::get($key);
}
protected function ___callUnknown($method, $arguments)
{
$object = $this->getClient();
if(is_object($object)) {
$callto = array($object, $method);
if(is_callable($callto))
return call_user_func_array($callto, $arguments);
}
return parent::___callUnknown($method, $arguments);
}
#----------------------------------------------------------------
# Interface methods: Module
#----------------------------------------------------------------
public static function getModuleInfo()
{
return [
'title' => 'Instagram API',
'author' => 'IDT Media',
'summary' => 'Instagram API',
'version' => 001,
'singular' => true,
'autoload' => true,
'requires' => array('PHP>=5.6')
];
}
/**
* Run init function
*
*/
public function init() {
$class = $this->className();
$conf = $this->getModuleInfo();
$version = (int)$conf['version'];
if($this->input->name && $this->input->name == $class) {
$this->addHookBefore('ProcessModule::executeEdit', function($event) {
$token = $this->cache->get(self::token);
if($this->input->post->submit_authenticate) {
// if($token)
// $this->cache->delete(self::token);
try {
$url = $this->getLoginUrl($this->scopes ?: ['basic']);
if($url)
$this->session->redirect($url);
} catch(Exception $e) {
$event->object->error($e->getMessage());
}
}
else {
if(!$token)
$event->object->warning(__('Token is not set, please authenticate'));
}
});
}
}
/**
* Install the module
*
*/
public function ___install() {
$name = self::callback;
$class = $this->className();
$page = $this->wire('pages')->get("name=$name");
if($page->id) throw new WireException("There is already a page installed called '/$name/'");
$template = $this->wire('templates')->get($name);
if($template) throw new WireException("There is already a template installed called '$name'");
$fieldgroup = $this->wire('fieldgroups')->get($name);
if($fieldgroup) throw new WireException("There is already a fieldgroup installed called '$name'");
$fieldgroup = new Fieldgroup();
$fieldgroup->name = $name;
$title = $this->wire('fields')->get('title');
if($title) $fieldgroup->add($title);
$fieldgroup->save();
$template = new Template();
$template->name = $name;
$template->fieldgroup = $fieldgroup;
$template->save();
$this->message("Installed template $name");
$page = new Page();
$page->template = $template;
$page->parent = $this->wire('config')->adminRootPageID;
$page->name = $name;
$page->title = "InstagramAPI OAuth Callback";
$page->addStatus(Page::statusHidden);
$page->save();
$this->message("Installed page $page->path");
$basename = $name . ".php";
$src = $this->wire('config')->paths->$class . $basename;
$dst = $this->wire('config')->paths->templates . $basename;
if(@copy($src, $dst)) $this->message("Installed template file $basename");
else $this->error("Templates directory is not writable so we were unable to auto-install the $basename template file.");
}
/**
* Uninstall the module
*
*/
public function ___uninstall() {
$name = self::callback;
$this->cache->delete(self::token);
$page = $this->wire('pages')->get("template=$name, name=$name");
if($page->id) {
$this->message("Deleted page {$page->path}");
$this->pages->delete($page);
}
$template = $this->wire('templates')->get($name);
if($template) {
$this->message("Deleted template $name");
$this->templates->delete($template);
}
$fieldgroup = $this->wire('fieldgroups')->get($name);
if($fieldgroup) {
$this->message("Deleted fieldgroup $name");
$this->fieldgroups->delete($fieldgroup);
}
$templateFile = $this->wire('config')->paths->templates . $name . ".php";
if(is_file($templateFile)) {
if(@unlink($templateFile)) $this->message("Deleted template file $templateFile");
else $this->error("Unable to remove file '$templateFile', please delete it manually at your convenience.");
}
}
#----------------------------------------------------------------
# Class methods
#----------------------------------------------------------------
// See: https://github.com/cosenary/Instagram-PHP-API
public function getUserMedia($id, $limit = 0)
{
$instance = $this->getClient();
if($token = $this->getToken($id))
$instance->setAccessToken($token);
return $instance->getUserMedia($id, $limit);
}
#----------------------------------------------------------------
# Helpers
#----------------------------------------------------------------
protected function getClient($force = false)
{
if(is_object($this->instance) && !$force)
return $this->instance;
$this->instance = new Instagram([
'apiKey' => $this->clientId,
'apiSecret' => $this->clientSecret,
'apiCallback' => $this->getCallbackUrl()
]);
if($token = $this->getToken())
$this->instance->setAccessToken($token);
return $this->instance;
}
protected function getToken($id = null)
{
if($tokens = $this->cache->get(self::token))
return !is_null($id) ? $tokens[$id] : $tokens[0];
return null;
}
protected function getCallbackUrl() {
$name = self::callback;
$page = $this->pages->get("name=$name");
if($this->modules->isInstalled('LanguageSupportPageNames')) {
$default = $this->languages->getDefault();
return $page->localHttpUrl($default);
}
return $page->httpUrl;
}
#----------------------------------------------------------------
# Interface methods: ConfigurableModule
#----------------------------------------------------------------
static public function getModuleConfigInputfields(array $data)
{
$wrap = new InputfieldWrapper();
$form = wire('modules')->get('InputfieldFieldset');
$form->label = __('API Authentication');
$form->description = __('You may create Facebook application at [https://www.instagram.com/developer/](https://www.instagram.com/developer/)');
$options = [
'clientId' => __('Client ID'),
'clientSecret' => __('Client Secret'),
];
foreach($options as $name => $label) {
$f = wire('modules')->get('InputfieldText');
$f->attr('name', $name);
$f->label = $label;
$f->required = false;
$f->columnWidth = 50;
if(isset($data[$name])) $f->attr('value', $data[$name]);
$form->add($f);
}
$field = wire('modules')->get("InputfieldSubmit");
$field->attr('name', 'submit_authenticate');
$field->attr('value', __('Authenticate'));
$field->addClass('auth-button');
$form->add($field);
$wrap->add($form);
$options = [
'basic',
'relationships',
'likes',
'comments',
'public_content',
];
$scopes = wire('modules')->get('InputfieldAsmSelect');
$scopes->name = 'scopes';
$scopes->label = 'API Scopes';
foreach($options as $value)
$scopes->addOption($value);
$scopes->value = isset($data['scopes']) ? $data['scopes'] : [];
$wrap->add($scopes);
return $wrap;
}
}