Merge pull request #1024 from hoergen/develop
[friendica-addons.git/.git] / buffer / bufferapp.php
1 <?php
2         class BufferApp {
3                 private $client_id;
4                 private $client_secret;
5                 private $code;
6                 public $access_token;
7
8                 private $callback_url;
9                 private $authorize_url = 'https://bufferapp.com/oauth2/authorize';
10                 private $access_token_url = 'https://api.bufferapp.com/1/oauth2/token.json';
11                 private $buffer_url = 'https://api.bufferapp.com/1';
12
13                 public $ok = false;
14
15                 private $endpoints = [
16                         '/user' => 'get',
17
18                         '/profiles' => 'get',
19                         '/profiles/:id' => 'get',
20                         '/profiles/:id/schedules' => 'get',
21                         '/profiles/:id/schedules/update' => 'post',     // Array schedules [0][days][]=mon, [0][times][]=12:00
22
23                         '/updates/:id' => 'get',
24                         '/profiles/:id/updates/pending' => 'get',
25                         '/profiles/:id/updates/sent' => 'get',
26                         '/updates/:id/interactions' => 'get',
27
28                         '/profiles/:id/updates/reorder' => 'post',      // Array order, int offset, bool utc
29                         '/profiles/:id/updates/shuffle' => 'post',
30                         '/updates/create' => 'post',                                                            // String text, Array profile_ids, Aool shorten, Bool now, Array media ['link'], ['description'], ['picture']
31                         '/updates/:id/update' => 'post',                                                // String text, Bool now, Array media ['link'], ['description'], ['picture'], Bool utc
32                         '/updates/:id/share' => 'post',
33                         '/updates/:id/destroy' => 'post',
34                         '/updates/:id/move_to_top' => 'post',
35
36                         '/links/shares' => 'get',
37
38                         '/info/configuration' => 'get',
39
40                 ];
41
42                 public $errors = [
43                         'invalid-endpoint' => 'The endpoint you supplied does not appear to be valid.',
44
45                         '401' => 'Unauthorized.',
46                         '403' => 'Permission denied.',
47                         '404' => 'Endpoint not found.',
48                         '405' => 'Method not allowed.',
49                         '504' => 'Gateway timeout server response timeout.',
50                         '1000' => 'An unknown error occurred.',
51                         '1001' => 'Access token required.',
52                         '1002' => 'Not within application scope.',
53                         '1003' => 'Parameter not recognized.',
54                         '1004' => 'Required parameter missing.',
55                         '1005' => 'Unsupported response format.',
56                         '1006' => 'Parameter value not within bounds.',
57                         '1010' => 'Profile could not be found.',
58                         '1011' => 'No authorization to access profile.',
59                         '1012' => 'Profile did not save successfully.',
60                         '1013' => 'Profile schedule limit reached.',
61                         '1014' => 'Profile limit for user has been reached.',
62                         '1015' => 'Profile could not be destroyed.',
63                         '1016' => 'Profile buffer could not be emptied.',
64                         '1020' => 'Update could not be found.',
65                         '1021' => 'No authorization to access update.',
66                         '1022' => 'Update did not save successfully.',
67                         '1023' => 'Update limit for profile has been reached.',
68                         '1024' => 'Update limit for team profile has been reached.',
69                         '1025' => "Update was recently posted, can't post duplicate content.",
70                         '1026' => 'Update must be in error status to requeue.',
71                         '1027' => 'Update must be in buffer and not custom scheduled in order to move to top.',
72                         '1028' => 'Update soft limit for profile reached.',
73                         '1029' => 'Event type not supported.',
74                         '1030' => 'Media filetype not supported.',
75                         '1031' => 'Media filesize out of acceptable range.',
76                         '1032' => 'Unable to post image to LinkedIn group(s).',
77                         '1033' => 'Comments can only be posted to Facebook at this time.',
78                         '1034' => 'Cannot schedule updates in the past.',
79                         '1042' => 'User did not save successfully.',
80                         '1050' => 'Client could not be found.',
81                         '1051' => 'No authorization to access client.',
82                 ];
83
84                 function __construct($client_id = '', $client_secret = '', $callback_url = '', $access_token = '') {
85                         if ($client_id) $this->set_client_id($client_id);
86                         if ($client_secret) $this->set_client_secret($client_secret);
87                         if ($callback_url) $this->set_callback_url($callback_url);
88                         if ($access_token) $this->access_token = $access_token;
89
90                         if (isset($_GET['code']) && $_GET['code']) {
91                                 $this->code = $_GET['code'];
92                                 $this->create_access_token_url();
93                         }
94
95                         if (!$access_token)
96                                 $this->retrieve_access_token();
97                 }
98
99                 function go($endpoint = '', $data = '') {
100                         if (in_array($endpoint, array_keys($this->endpoints))) {
101                                 $done_endpoint = $endpoint;
102                         } else {
103                                 $ok = false;
104
105                                 foreach (array_keys($this->endpoints) as $done_endpoint) {
106                                         if (preg_match('/' . preg_replace('/(\:\w+)/i', '(\w+)', str_replace('/', '\/', $done_endpoint)) . '/i', $endpoint, $match)) {
107                                                 $ok = true;
108                                                 break;
109                                         }
110                                 }
111
112                                 if (!$ok) return $this->error('invalid-endpoint');
113                         }
114
115                         if (!$data || !is_array($data)) $data = [];
116                         $data['access_token'] = $this->access_token;
117
118                         $method = $this->endpoints[$done_endpoint]; //get() or post()
119                         return $this->$method($this->buffer_url . $endpoint . '.json', $data);
120                 }
121
122                 function store_access_token() {
123                         $_SESSION['oauth']['buffer']['access_token'] = $this->access_token;
124                 }
125
126                 function retrieve_access_token() {
127                         $this->access_token = $_SESSION['oauth']['buffer']['access_token'];
128
129                         if ($this->access_token) {
130                                 $this->ok = true;
131                         }
132                 }
133
134                 function error($error) {
135                         return (object) ['error' => $this->errors[$error]];
136                 }
137
138                 function create_access_token_url() {
139                         $data = [
140                                 'code' => $this->code,
141                                 'grant_type' => 'authorization_code',
142                                 'client_id' => $this->client_id,
143                                 'client_secret' => $this->client_secret,
144                                 'redirect_uri' => $this->callback_url,
145                         ];
146
147                         $obj = $this->post($this->access_token_url, $data);
148                         $this->access_token = $obj->access_token;
149
150                         $this->store_access_token();
151                 }
152
153                 function req($url = '', $data = '', $post = true) {
154                         if (!$url) return false;
155                         if (!$data || !is_array($data)) $data = [];
156
157                         $options = [CURLOPT_RETURNTRANSFER => true, CURLOPT_HEADER => false];
158
159                         if ($post) {
160                                 $options += [
161                                         CURLOPT_POST => $post,
162                                         CURLOPT_POSTFIELDS => $data
163                                 ];
164                         } else {
165                                 $url .= '?' . http_build_query($data);
166                         }
167
168                         $ch = curl_init($url);
169                         curl_setopt_array($ch, $options);
170                         $rs = curl_exec($ch);
171
172                         $code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
173                         if ($code >= 400) {
174                                 return $this->error($code);
175                         }
176
177                         return json_decode($rs);
178                 }
179
180                 function get($url = '', $data = '') {
181                         return $this->req($url, $data, false);
182                 }
183
184                 function post($url = '', $data = '') {
185                         return $this->req($url, $data, true);
186                 }
187
188                 function get_login_url() {
189                         return $this->authorize_url . '?'
190                 . 'client_id=' . $this->client_id
191                 . '&redirect_uri=' . urlencode($this->callback_url)
192                 . '&response_type=code';
193                 }
194
195                 function set_client_id($client_id) {
196                         $this->client_id = $client_id;
197                 }
198
199                 function set_client_secret($client_secret) {
200                         $this->client_secret = $client_secret;
201                 }
202
203                 function set_callback_url($callback_url) {
204                         $this->callback_url = $callback_url;
205                 }
206         }
207 ?>