private static function parse_response($raw, $info) { list($head, $body) = explode("\r\n\r\n", $raw, 2); list($head_out, $body_out) = explode("\r\n\r\n", $info['request_header'], 2); unset($info['request_header']); $headers_in = SlackHTTP::parse_headers($head, '_status'); $headers_out = SlackHTTP::parse_headers($head_out, '_request'); preg_match("/^([A-Z]+)\\s/", $headers_out['_request'], $m); $method = $m[1]; # log_notice("http", "{$method} {$url}", $end-$start); # http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.2 # http://en.wikipedia.org/wiki/List_of_HTTP_status_codes#2xx_Success (note HTTP 207 WTF) $status = $info['http_code']; if ($status < 200 || $status > 299) { return array('ok' => false, 'error' => 'http_failed', 'code' => $info['http_code'], 'method' => $method, 'url' => $info['url'], 'info' => $info, 'req_headers' => $headers_out, 'headers' => $headers_in, 'body' => $body); } return array('ok' => true, 'code' => $info['http_code'], 'method' => $method, 'url' => $info['url'], 'info' => $info, 'req_headers' => $headers_out, 'headers' => $headers_in, 'body' => $body); }
function api_call($method, $args = array()) { $team = $GLOBALS['data']->get('metadata', 'team'); $url = $GLOBALS['cfg']['slack_root'] . "api/" . $method . "?token=" . $team['token']; foreach ($args as $k => $v) { $url .= '&' . urlencode($k) . '=' . urlencode($v); } $ret = SlackHTTP::get($url); if ($ret['ok'] && $ret['code'] == '200') { return array('ok' => true, 'data' => json_decode($ret['body'], true)); } return $ret; }
} } else { echo "problem with oauth.access call"; dumper($ret); exit; } if ($_GET['debug']) { echo "debug mode:"; dumper($obj); exit; } # # fetch user info # $url = $cfg['slack_root'] . "api/auth.test?token={$token}"; $ret = SlackHTTP::get($url); if ($ret['ok'] && $ret['code'] == '200') { $obj = json_decode($ret['body'], true); } else { echo "problem with auth.test call"; dumper($ret); exit; } $info = $obj; unset($info['ok']); $info['access_token'] = $token; $info['secret'] = substr(md5(rand()), 0, 10); $cookie = $info['user_id'] . '-' . $info['secret']; $expire = time() + 365 * 24 * 60 * 60; setcookie($cfg['cookie_name'], $cookie, $expire, $cfg['cookie_path'], $cfg['cookie_domain'], isset($_SERVER["HTTPS"]), true); $data->set('users', $info['user_id'], $info);