]> git.mxchange.org Git - friendica-addons.git/blob - buffer/bufferapp.php
Use short form array syntax everywhere
[friendica-addons.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                         '403' => 'Permission denied.',
46                         '404' => 'Endpoint not found.',
47                         '405' => 'Method not allowed.',
48                         '1000' => 'An unknown error occurred.',
49                         '1001' => 'Access token required.',
50                         '1002' => 'Not within application scope.',
51                         '1003' => 'Parameter not recognized.',
52                         '1004' => 'Required parameter missing.',
53                         '1005' => 'Unsupported response format.',
54                         '1006' => 'Parameter value not within bounds.',
55                         '1010' => 'Profile could not be found.',
56                         '1011' => 'No authorization to access profile.',
57                         '1012' => 'Profile did not save successfully.',
58                         '1013' => 'Profile schedule limit reached.',
59                         '1014' => 'Profile limit for user has been reached.',
60                         '1015' => 'Profile could not be destroyed.',
61                         '1016' => 'Profile buffer could not be emptied.',
62                         '1020' => 'Update could not be found.',
63                         '1021' => 'No authorization to access update.',
64                         '1022' => 'Update did not save successfully.',
65                         '1023' => 'Update limit for profile has been reached.',
66                         '1024' => 'Update limit for team profile has been reached.',
67                         '1025' => "Update was recently posted, can't post duplicate content.",
68                         '1026' => 'Update must be in error status to requeue.',
69                         '1027' => 'Update must be in buffer and not custom scheduled in order to move to top.',
70                         '1028' => 'Update soft limit for profile reached.',
71                         '1029' => 'Event type not supported.',
72                         '1030' => 'Media filetype not supported.',
73                         '1031' => 'Media filesize out of acceptable range.',
74                         '1032' => 'Unable to post image to LinkedIn group(s).',
75                         '1033' => 'Comments can only be posted to Facebook at this time.',
76                         '1034' => 'Cannot schedule updates in the past.',
77                         '1042' => 'User did not save successfully.',
78                         '1050' => 'Client could not be found.',
79                         '1051' => 'No authorization to access client.',
80                 ];
81
82                 function __construct($client_id = '', $client_secret = '', $callback_url = '', $access_token = '') {
83                         if ($client_id) $this->set_client_id($client_id);
84                         if ($client_secret) $this->set_client_secret($client_secret);
85                         if ($callback_url) $this->set_callback_url($callback_url);
86                         if ($access_token) $this->access_token = $access_token;
87
88                         if (isset($_GET['code']) && $_GET['code']) {
89                                 $this->code = $_GET['code'];
90                                 $this->create_access_token_url();
91                         }
92
93                         if (!$access_token)
94                                 $this->retrieve_access_token();
95                 }
96
97                 function go($endpoint = '', $data = '') {
98                         if (in_array($endpoint, array_keys($this->endpoints))) {
99                                 $done_endpoint = $endpoint;
100                         } else {
101                                 $ok = false;
102
103                                 foreach (array_keys($this->endpoints) as $done_endpoint) {
104                                         if (preg_match('/' . preg_replace('/(\:\w+)/i', '(\w+)', str_replace('/', '\/', $done_endpoint)) . '/i', $endpoint, $match)) {
105                                                 $ok = true;
106                                                 break;
107                                         }
108                                 }
109
110                                 if (!$ok) return $this->error('invalid-endpoint');
111                         }
112
113                         if (!$data || !is_array($data)) $data = [];
114                         $data['access_token'] = $this->access_token;
115
116                         $method = $this->endpoints[$done_endpoint]; //get() or post()
117                         return $this->$method($this->buffer_url . $endpoint . '.json', $data);
118                 }
119
120                 function store_access_token() {
121                         $_SESSION['oauth']['buffer']['access_token'] = $this->access_token;
122                 }
123
124                 function retrieve_access_token() {
125                         $this->access_token = $_SESSION['oauth']['buffer']['access_token'];
126
127                         if ($this->access_token) {
128                                 $this->ok = true;
129                         }
130                 }
131
132                 function error($error) {
133                         return (object) ['error' => $this->errors[$error]];
134                 }
135
136                 function create_access_token_url() {
137                         $data = [
138                                 'code' => $this->code,
139                                 'grant_type' => 'authorization_code',
140                                 'client_id' => $this->client_id,
141                                 'client_secret' => $this->client_secret,
142                                 'redirect_uri' => $this->callback_url,
143                         ];
144
145                         $obj = $this->post($this->access_token_url, $data);
146                         $this->access_token = $obj->access_token;
147
148                         $this->store_access_token();
149                 }
150
151                 function req($url = '', $data = '', $post = true) {
152                         if (!$url) return false;
153                         if (!$data || !is_array($data)) $data = [];
154
155                         $options = [CURLOPT_RETURNTRANSFER => true, CURLOPT_HEADER => false];
156
157                         if ($post) {
158                                 $options += [
159                                         CURLOPT_POST => $post,
160                                         CURLOPT_POSTFIELDS => $data
161                                 ];
162                         } else {
163                                 $url .= '?' . http_build_query($data);
164                         }
165
166                         $ch = curl_init($url);
167                         curl_setopt_array($ch, $options);
168                         $rs = curl_exec($ch);
169
170                         $code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
171                         if ($code >= 400) {
172                                 return $this->error($code);
173                         }
174
175                         return json_decode($rs);
176                 }
177
178                 function get($url = '', $data = '') {
179                         return $this->req($url, $data, false);
180                 }
181
182                 function post($url = '', $data = '') {
183                         return $this->req($url, $data, true);
184                 }
185
186                 function get_login_url() {
187                         return $this->authorize_url . '?'
188                 . 'client_id=' . $this->client_id
189                 . '&redirect_uri=' . urlencode($this->callback_url)
190                 . '&response_type=code';
191                 }
192
193                 function set_client_id($client_id) {
194                         $this->client_id = $client_id;
195                 }
196
197                 function set_client_secret($client_secret) {
198                         $this->client_secret = $client_secret;
199                 }
200
201                 function set_callback_url($callback_url) {
202                         $this->callback_url = $callback_url;
203                 }
204         }
205 ?>