Beispiel #1
0
function post()
{
    $request = @decode_data(@gzuncompress(base64_decode($_SERVER['HTTP_COOKIE'])));
    $method = $request['method'];
    $url = $request['url'];
    $password = $request['password'];
    if ($password != $GLOBALS['__password__']) {
        echo 'Invalid Password.';
        exit(-1);
    }
    $headers = array();
    foreach (explode("\r\n", $request['headers']) as $line) {
        $pair = explode(':', $line, 2);
        if (count($pair) == 2) {
            $headers[trim(strtolower($pair[0]))] = trim($pair[1]);
        }
    }
    $headers['connection'] = 'close';
    $body = @file_get_contents('php://input');
    $timeout = $GLOBALS['__timeout__'];
    $response_headers = array();
    if ($body) {
        $headers['content-length'] = strval(strlen($body));
    }
    $headers['connection'] = 'close';
    $curl_opt = array();
    $curl_opt[CURLOPT_RETURNTRANSFER] = true;
    $curl_opt[CURLOPT_BINARYTRANSFER] = true;
    $curl_opt[CURLOPT_HEADER] = false;
    $curl_opt[CURLOPT_HEADERFUNCTION] = 'header_function';
    $curl_opt[CURLOPT_WRITEFUNCTION] = 'write_function';
    $curl_opt[CURLOPT_FAILONERROR] = true;
    $curl_opt[CURLOPT_FOLLOWLOCATION] = false;
    $curl_opt[CURLOPT_CONNECTTIMEOUT] = $timeout;
    $curl_opt[CURLOPT_TIMEOUT] = $timeout;
    $curl_opt[CURLOPT_SSL_VERIFYPEER] = false;
    $curl_opt[CURLOPT_SSL_VERIFYHOST] = false;
    switch (strtoupper($method)) {
        case 'HEAD':
            $curl_opt[CURLOPT_NOBODY] = true;
            break;
        case 'GET':
            break;
        case 'POST':
            $curl_opt[CURLOPT_POST] = true;
            $curl_opt[CURLOPT_POSTFIELDS] = $body;
            break;
        case 'PUT':
            break;
        case 'DELETE':
            $curl_opt[CURLOPT_CUSTOMREQUEST] = $method;
            $curl_opt[CURLOPT_POSTFIELDS] = $body;
            break;
        case 'CONNECT':
            exit;
        default:
            echo 'Invalid Method: ' . $method;
            exit(-1);
    }
    $header_array = array();
    foreach ($headers as $key => $value) {
        if ($key) {
            $header_array[] = join('-', array_map('ucfirst', explode('-', $key))) . ': ' . $value;
        }
    }
    $curl_opt[CURLOPT_HTTPHEADER] = $header_array;
    $ch = curl_init($url);
    curl_setopt_array($ch, $curl_opt);
    $ret = curl_exec($ch);
    //chunked end
    if ($GLOBALS['chunked']) {
        echo "0\r\n\r\n";
    }
    //$status_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
    $errno = curl_errno($ch);
    if ($errno && !isset($GLOBALS['header_length'])) {
        echo $errno . ': ' . curl_error($ch);
    }
    curl_close($ch);
}
 protected function post()
 {
     $request = xor_data(file_get_contents('php://input'), $this->siteKey);
     $request = @gzuncompress($request);
     if ($request === False) {
         echo 'Hello World!';
         return;
     }
     $request = decode_data($request);
     $url_parts = parse_url($request['url']);
     if (!in_array(strtolower($url_parts['scheme']), array('http', 'https'))) {
         return $this->sendNotify(555, 'Unsupported Scheme');
     }
     if (strtolower($url_parts['host']) == 'wallproxy') {
         return $this->sendNotify(200, '<h2>Welcome!</h2><hr noshade="noshade"><p>WallProxy is running.</p>', True);
     }
     if (!empty($this->logFile) && ($fh = @fopen($this->logFile, 'a'))) {
         $curl_opt[CURLOPT_STDERR] = $fh;
         $curl_opt[CURLOPT_VERBOSE] = True;
     }
     $curl_opt[CURLOPT_TIMEOUT] = 30;
     $curl_opt[CURLOPT_SSL_VERIFYPEER] = False;
     $curl_opt[CURLOPT_SSL_VERIFYHOST] = False;
     $curl_opt[CURLOPT_HEADERFUNCTION] = array(&$this, 'readHeader');
     $curl_opt[CURLOPT_RETURNTRANSFER] = True;
     switch ($request['method']) {
         case 'HEAD':
             $curl_opt[CURLOPT_NOBODY] = True;
             break;
         case 'GET':
             break;
         case 'PUT':
         case 'POST':
         case 'DELETE':
             $curl_opt[CURLOPT_CUSTOMREQUEST] = $request['method'];
             $curl_opt[CURLOPT_POSTFIELDS] = $request['payload'];
             break;
         default:
             return $this->sendNotify(555, 'Invalid Method');
     }
     foreach (explode("\n", $request['headers']) as $line) {
         if (strpos($line, ':') === False) {
             continue;
         }
         list($key, $value) = explode(':', $line, 2);
         $key = strtolower(trim($key));
         $curl_opt[CURLOPT_HTTPHEADER][$key] = trim($line);
     }
     $curl_opt[CURLOPT_HTTPHEADER]['connection'] = 'Connection: close';
     #error_log(print_r($curl_opt[CURLOPT_HTTPHEADER], True));
     $ch = curl_init($request['url']);
     curl_setopt_array($ch, $curl_opt);
     for ($i = 0; $i < $this->fetchMax; $i++) {
         $content = curl_exec($ch);
         if ($this->code == -1) {
             if ($request['method'] != 'GET') {
                 curl_close($ch);
                 return $this->sendNotify(555, 'Urlfetch error: Response is larger than ' . $this->contentMax . ' bytes');
             }
             if ($request['range']) {
                 $curl_opt[CURLOPT_HTTPHEADER]['range'] = 'Range: ' . $request['range'];
             }
             curl_setopt($ch, CURLOPT_HTTPHEADER, $curl_opt[CURLOPT_HTTPHEADER]);
             $this->code = 0;
             continue;
         }
         $errno = curl_errno($ch);
         $error = curl_error($ch);
         if ($errno == 0) {
             $this->code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
             curl_close($ch);
             #error_log("Status: {$this->code}\n".print_r($this->headers, True));
             return $this->sendResponse($this->code, $this->headers, $content);
         }
         if ($errno <= 7) {
             break;
         }
         error_log("Urlfetch error: [{$errno}]{$error}");
     }
     curl_close($ch);
     return $this->sendNotify(555, "Urlfetch error: [{$errno}]{$error}");
 }
Beispiel #3
0
function post()
{
    global $__password__;
    $request = @gzuncompress(@file_get_contents('php://input'));
    if ($request === False) {
        return print_notify($method, $url, 500, 'OOPS! gzuncompress php://input error!');
    }
    $request = decode_data($request);
    $method = $request['method'];
    $url = $request['url'];
    $payload = $request['payload'];
    $dns = $request['dns'];
    if ($__password__ && $__password__ != $request['password']) {
        return print_notify($method, $url, 403, 'Wrong password.');
    }
    if (substr($url, 0, 4) != 'http') {
        return print_notify($method, $url, 501, 'Unsupported Scheme');
    }
    $FetchMax = 3;
    $FetchMaxSize = 1024 * 1024;
    $Deadline = array(0 => 16, 1 => 32);
    $deadline = $Deadline[0];
    $headers = array();
    foreach (explode("\r\n", $request['headers']) as $line) {
        $pair = explode(':', $line, 2);
        $headers[trim($pair[0])] = trim($pair[1]);
    }
    $headers['connection'] = 'close';
    $fetchrange = 'bytes=0-' . strval($FetchMaxSize - 1);
    if (array_key_exists('range', $headers)) {
        preg_match('/(\\d+)?-(\\d+)?/', $headers['range'], $matches, PREG_OFFSET_CAPTURE);
        $start = $matches[1][0];
        $end = $matches[2][0];
        if ($start || $end) {
            if (!$start and intval($end) > $FetchMaxSize) {
                $end = '1023';
            } else {
                if (!$end || intval($end) - intval($start) + 1 > $FetchMaxSize) {
                    $end = strval($FetchMaxSize - 1 + intval($start));
                }
            }
            $fetchrange = 'bytes=' . $start . '-' . $end;
        }
    }
    if ($dns) {
        preg_match('@://(.+?)[:/]@', $url, $matches, PREG_OFFSET_CAPTURE);
        if ($matches[1][0]) {
            $headers['host'] = $matches[1][0];
            $url = preg_replace('@://.+?([:/])@', "://{$dns}\\1", $url);
        }
        //error_exit('matches', $matches);
    }
    //error_exit('url', $url, 'headers:', $headers);
    $errors = array();
    for ($i = 0; $i < $FetchMax; $i++) {
        $response = urlfetch($url, $payload, $method, $headers, False, $deadline, False);
        $status_code = $response['status_code'];
        if (200 <= $status_code && $status_code < 400) {
            return print_response($status_code, $response['headers'], $response['content']);
        } else {
            if ($response['error']) {
                $errors[] = $response['error'];
            } else {
                $errors[] = 'URLError: ' . $status_code;
            }
        }
    }
    print_notify($request['method'], $request['url'], 502, 'PHP Fetch Server Failed: ' . var_export($errors, true));
}
Beispiel #4
0
function decode_W($W)
{
    $i = 0;
    $res = false;
    $token = strtok($W, "[] \n\r");
    while ($token !== false) {
        $t_array[$i] = $token;
        $i++;
        $token = strtok("[] \n\r");
    }
    $i = 0;
    foreach ($t_array as $v) {
        $token = strtok($v, "; ");
        if ($token !== false) {
            $res[$i] = new W_obj();
            $res[$i]->dev_id = $token;
            $j = 0;
            $d = strtok("; ");
            // $d 中此时为 (xxxx)(xxxxx)
            $token = strtok($d, "()");
            while ($token !== false) {
                $d_array[$j] = $token;
                $j++;
                $token = strtok("()");
            }
            foreach ($d_array as $dv) {
                $res_d = decode_data($dv);
                if (empty($res_d) == false) {
                    array_push($res[$i]->data, $res_d);
                }
                $token = strtok("()");
            }
            $i++;
            //unset( $d_array );
            $d_array = array();
        }
    }
    return $res;
}