4 private $client_secret;
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';
15 private $endpoints = array(
19 '/profiles/:id' => 'get',
20 '/profiles/:id/schedules' => 'get',
21 '/profiles/:id/schedules/update' => 'post', // Array schedules [0][days][]=mon, [0][times][]=12:00
23 '/updates/:id' => 'get',
24 '/profiles/:id/updates/pending' => 'get',
25 '/profiles/:id/updates/sent' => 'get',
26 '/updates/:id/interactions' => 'get',
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',
36 '/links/shares' => 'get',
38 '/info/configuration' => 'get',
42 public $errors = array(
43 'invalid-endpoint' => 'The endpoint you supplied does not appear to be valid.',
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.',
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;
88 if (isset($_GET['code']) AND $_GET['code']) {
89 $this->code = $_GET['code'];
90 $this->create_access_token_url();
94 $this->retrieve_access_token();
97 function go($endpoint = '', $data = '') {
98 if (in_array($endpoint, array_keys($this->endpoints))) {
99 $done_endpoint = $endpoint;
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)) {
110 if (!$ok) return $this->error('invalid-endpoint');
113 if (!$data || !is_array($data)) $data = array();
114 $data['access_token'] = $this->access_token;
116 $method = $this->endpoints[$done_endpoint]; //get() or post()
117 return $this->$method($this->buffer_url . $endpoint . '.json', $data);
120 function store_access_token() {
121 $_SESSION['oauth']['buffer']['access_token'] = $this->access_token;
124 function retrieve_access_token() {
125 $this->access_token = $_SESSION['oauth']['buffer']['access_token'];
127 if ($this->access_token) {
132 function error($error) {
133 return (object) array('error' => $this->errors[$error]);
136 function create_access_token_url() {
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,
145 $obj = $this->post($this->access_token_url, $data);
146 $this->access_token = $obj->access_token;
148 $this->store_access_token();
151 function req($url = '', $data = '', $post = true) {
152 if (!$url) return false;
153 if (!$data || !is_array($data)) $data = array();
155 $options = array(CURLOPT_RETURNTRANSFER => true, CURLOPT_HEADER => false);
159 CURLOPT_POST => $post,
160 CURLOPT_POSTFIELDS => $data
163 $url .= '?' . http_build_query($data);
166 $ch = curl_init($url);
167 curl_setopt_array($ch, $options);
168 $rs = curl_exec($ch);
170 $code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
172 return $this->error($code);
175 return json_decode($rs);
178 function get($url = '', $data = '') {
179 return $this->req($url, $data, false);
182 function post($url = '', $data = '') {
183 return $this->req($url, $data, true);
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';
193 function set_client_id($client_id) {
194 $this->client_id = $client_id;
197 function set_client_secret($client_secret) {
198 $this->client_secret = $client_secret;
201 function set_callback_url($callback_url) {
202 $this->callback_url = $callback_url;