Ejemplo n.º 1
0
 public static function handshake($handshake)
 {
     $headers = parse_headers($handshake);
     $ws_location = self::location_from_host($headers['Host']);
     ///TODO update handshake to comply with new SEC-* websocket changes
     return "HTTP/1.1 101 Web Socket Protocol Handshake\r\n" . "Upgrade: WebSocket\r\n" . "Connection: Upgrade\r\n" . "WebSocket-Origin: {$headers['Origin']}\r\n" . "WebSocket-Location: {$ws_location}\r\n\r\n";
 }
 function get_api_data($url, $ch)
 {
     $token = "1064~PANNjKSDEQ1mvsZFTnyraP1cLuCs24qZpBuRQKQWPFLxbRWTz3B3EvMPj8QL9x6z";
     curl_setopt($ch, CURLOPT_URL, $url);
     curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
     curl_setopt($ch, CURLOPT_VERBOSE, 1);
     //Requires to load headers
     curl_setopt($ch, CURLOPT_HEADER, 1);
     //Requires to load headers
     $headers = array('Authorization: Bearer ' . $token);
     curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
     //curl_setopt ($ch, CURLOPT_SSL_VERIFYHOST, false);
     //curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
     curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
     curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
     curl_setopt($ch, CURLOPT_CERTINFO, TRUE);
     $result = curl_exec($ch);
     if ($result === false) {
         return 'Curl error: ' . curl_error($ch);
     }
     #Parse header information from body response
     $header_size = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
     $canvas_header = substr($result, 0, $header_size);
     $body = substr($result, $header_size);
     $data = json_decode($body);
     #Parse Link Information
     $header_info = parse_headers($canvas_header);
     if (isset($header_info['Link'])) {
         $links = explode(',', $header_info['Link']);
         foreach ($links as $value) {
             if (preg_match('/^\\s*<(.*?)>;\\s*rel="(.*?)"/', $value, $match)) {
                 $links[$match[2]] = $match[1];
             }
         }
     }
     #Check for Pagination
     if (isset($links['next'])) {
         $next_data = get_api_data($links['next'], $ch);
         $data = array_merge($data, $next_data);
         return $data;
     } else {
         return $data;
     }
 }
 function get_api_data($url, $ch)
 {
     $token = "YourToken";
     curl_setopt($ch, CURLOPT_URL, $url);
     curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
     curl_setopt($ch, CURLOPT_VERBOSE, 1);
     //Requires to load headers
     curl_setopt($ch, CURLOPT_HEADER, 1);
     //Requires to load headers
     $headers = array('Authorization: Bearer ' . $token);
     curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
     $result = curl_exec($ch);
     #Parse header information from body response
     $header_size = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
     $canvas_header = substr($result, 0, $header_size);
     $body = substr($result, $header_size);
     $data = json_decode($body);
     #Parse Link Information
     $header_info = parse_headers($canvas_header);
     if (isset($header_info['Link'])) {
         $links = explode(',', $header_info['Link']);
         foreach ($links as $value) {
             if (preg_match('/^\\s*<(.*?)>;\\s*rel="(.*?)"/', $value, $match)) {
                 $links[$match[2]] = $match[1];
             }
         }
     }
     #Check for Pagination
     if (isset($links['next'])) {
         $next_data = get_api_data($links['next'], $ch);
         $data->quiz_submissions = array_merge($data->quiz_submissions, $next_data->quiz_submissions);
         $data->users = array_merge($data->users, $next_data->users);
         return $data;
     } else {
         return $data;
     }
 }
Ejemplo n.º 4
0
function proxypass($url, $method, $parameters, $header_timeout_ms, $body_timeout_ms)
{
    global $user_agent, $chunk_size_kb;
    $opts = array();
    $opts['http'] = array();
    $opts['http']['method'] = $method;
    if ($user_agent == "") {
        // setting the user agent according to the user choise
        $opts['http']['user_agent'] = $_SERVER['HTTP_USER_AGENT'];
    } else {
        $opts['http']['user_agent'] = $user_agent;
    }
    if ($method == "POST" || $method == "PUT" || $method == "DELETE") {
        $opts['http']['content'] = http_build_query($parameters);
    }
    $context = stream_context_create($opts);
    $handle = @fopen($url, "rb", false, $context);
    if (!$handle) {
        @fclose($handle);
        generate_http_error("HTTP/1.1 500 Backend Server Error");
        echo ": caused by: ";
        echo $http_response_header[0];
        // echo " opening url: $url\n"; // security issue here because the password is printed in the output
        echo " opening url: " . parse_url($url, PHP_URL_SCHEME) . "://_hidden_server_" . parse_url($url, PHP_URL_PATH);
        ob_end_flush();
        flush();
        exit;
    }
    if ($header_timeout_ms > 0) {
        @stream_set_timeout($handle, 0, $header_timeout_ms * 1000);
    }
    $info = parse_headers($handle);
    if ($body_timeout_ms > 0) {
        @stream_set_timeout($handle, 0, $body_timeout_ms * 1000);
    }
    while (!feof($handle)) {
        $res = @fread($handle, $chunk_size_kb);
        // read a chunk of data from the backend server
        if ($res === FALSE) {
            //generate_http_error("HTTP/1.1 500 Backend Server Error");
            ob_end_flush();
            flush();
            exit;
        }
        $i = check_timeout($handle);
        // check if there was a timeout error
        if (!$i) {
            // good! The server's response chunk was in time with our need
            print $res;
            // send it to the client
            ob_flush();
            // be sure to send it to the client
            flush();
            // be extra sure to send it to the client :-)
        } else {
            // mhhh... :-( there was a timeout reading data
            handle_response_timeout(1);
            break;
        }
    }
    @fclose($handle);
    ob_end_flush();
}
Ejemplo n.º 5
0
     $redirect = false;
     do {
         $loc[] = $u;
         $ch = curl_init();
         curl_setopt($ch, CURLOPT_URL, $u);
         curl_setopt($ch, CURLOPT_HEADER, true);
         curl_setopt($ch, CURLOPT_NOBODY, true);
         curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
         curl_setopt($ch, CURLOPT_TIMEOUT, $max_seconds);
         $b = curl_exec($ch);
         $e = curl_errno($ch);
         curl_close($ch);
         if ($e) {
             $response['error'] = "cURL error";
         } else {
             $h = parse_headers($b);
             if ($redirect = $h['status'] == 301 || $h['status'] == 302) {
                 $u = $h['location'];
             }
         }
     } while ($redirect && !in_array($u, $loc));
     if ($redirect) {
         $response['error'] = "Redirection loop";
     } else {
         $response['url'] = $u;
     }
     echo $response['url'];
     // Might as well print URL here
 } else {
     $response['error'] = "Malformed URL";
 }
Ejemplo n.º 6
0
    if ($argc == 2) {
        // pobranie struktury wiadomosci
        $ires = connect();
        $uid = $argv[1];
        $struct = imap_fetchstructure($ires, $uid, FT_UID);
        $parts = get_parts($struct);
        print_r($parts);
        imap_close($ires);
    } else {
        if ($argc == 3) {
            // pobranie fragmentu wiadomosci
            $ires = connect();
            $uid = $argv[1];
            $section = $argv[2];
            $headers = imap_fetchmime($ires, $uid, $section, FT_UID);
            $headers = parse_headers($headers);
            var_export($headers);
            $body = imap_fetchbody($ires, $uid, $section, FT_UID);
            echo "\n" . $body . "\n";
            imap_close($ires);
        } else {
            echo "ERROR: Too many parameters\n";
        }
    }
}
function connect()
{
    $ires = imap_open("{imap.gmail.com:993/imap/ssl/readonly}INBOX", "*****@*****.**", "") or die("Connect Error: " . imap_last_error() . "\n");
    return $ires;
}
function decode($str)
Ejemplo n.º 7
0
/**
 * curl() 函数用来进行远程 http 请求
 * @param  array   $option    设置请求的参数,可最多包含下面 (array)$default 中所有的键值对
 * @param  boolean $iconvUtf8 尝试自动将文件流编码转换为 utf8, 默认位 true
 * @return string             返回请求结果,结果是字符串
 */
function curl($option = array(), $iconvUtf8 = true)
{
    /* 定义默认的参数 */
    $default = array('url' => '', 'method' => 'get', 'data' => '', 'cookie' => '', 'referer' => '', 'userAgent' => '', 'requestHeaders' => array(), 'responseHeaders' => true, 'sessionCookie' => false, 'noBody' => false, 'followLocation' => false, 'maxRedirs' => 2, 'autoReferer' => true, 'sslVerify' => false, 'proxy' => '', 'clientIp' => '', 'timeout' => 30, 'username' => '', 'password' => '', 'charset' => '');
    $option = array_merge($default, $option);
    $ch = curl_init();
    if (strtolower($option['method']) == 'post') {
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $option['data']);
    } else {
        if ($option['data']) {
            $option['url'] .= (strpos('?', $option['url']) ? '&' : '?') . (is_array($option['data']) ? http_build_query($option['data']) : $option['data']);
        }
    }
    if (is_assoc($option['requestHeaders'])) {
        foreach ($option['requestHeaders'] as $key => $value) {
            array_push($option['requestHeaders'], "{$key}: {$value}");
            unset($option['requestHeaders'][$key]);
        }
    }
    curl_setopt($ch, CURLOPT_URL, $option['url']);
    curl_setopt($ch, CURLOPT_COOKIESESSION, $option['sessionCookie']);
    curl_setopt($ch, CURLOPT_HEADER, $option['responseHeaders']);
    curl_setopt($ch, CURLINFO_HEADER_OUT, true);
    curl_setopt($ch, CURLOPT_NOBODY, $option['noBody']);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, $option['followLocation']);
    curl_setopt($ch, CURLOPT_MAXREDIRS, $option['maxRedirs']);
    curl_setopt($ch, CURLOPT_AUTOREFERER, $option['autoReferer']);
    curl_setopt($ch, CURLOPT_TIMEOUT, $option['timeout']);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
    if ($option['sslVerify']) {
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 2);
    } else {
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    }
    if ($option['cookie']) {
        curl_setopt($ch, CURLOPT_COOKIE, $option['cookie']);
    }
    if ($option['referer']) {
        curl_setopt($ch, CURLOPT_REFERER, $option['referer']);
    }
    if ($option['userAgent']) {
        curl_setopt($ch, CURLOPT_USERAGENT, $option['userAgent']);
    }
    if ($option['proxy']) {
        curl_setopt($ch, CURLOPT_PROXY, $option['proxy']);
    }
    if ($option['clientIp']) {
        array_push($option['requestHeaders'], 'CLIENT-IP: ' . $option['clientIp'], 'X-FORWARDED-FOR: ' . $option['clientIp']);
    }
    if (!empty($option['requestHeaders'])) {
        curl_setopt($ch, CURLOPT_HTTPHEADER, $option['requestHeaders']);
    }
    if ($option['username']) {
        curl_setopt($ch, CURLOPT_USERPWD, $option['username'] . ':' . $option['password']);
    }
    $data = curl_exec($ch);
    if (curl_errno($ch)) {
        return curl_error($ch);
    }
    curl_info(curl_getinfo($ch));
    curl_close($ch);
    if (!$option['charset'] && $iconvUtf8) {
        if (preg_match('/text\\/html;[\\s]*charset=(.*)/i', curl_info('content_type'), $matches)) {
            $option['charset'] = $matches[1];
        } else {
            if (preg_match('/<meta[\\s]*.*charset=["]*(.*)["][\\s]*.*>/i', $data, $matches)) {
                $option['charset'] = $matches[1];
            } else {
                if (preg_match('/<\\?xml[\\s]*.*encoding=["]*(.*)["][\\s]*.*>/i', $data, $matches)) {
                    $option['charset'] = $matches[1];
                }
            }
        }
    }
    if ($option['charset']) {
        $data = iconv($option['charset'], 'UTF-8', $data);
    }
    curl_info('response_headers', parse_headers(substr($data, 0, curl_info('header_size'))));
    curl_info('request_headers', parse_headers(curl_info('request_header')));
    curl_info('request_header', null);
    return unicode_decode(substr($data, curl_info('header_size')));
}