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