public function GenerateFormRequest($submitname = false, $submitvalue = false) { $method = $this->info["method"]; $fields = array(); $files = array(); foreach ($this->fields as $field) { if ($field["type"] == "input.file") { if (is_array($field["value"])) { $field["value"]["name"] = $field["name"]; $files[] = $field["value"]; $method = "post"; } } else { if ($field["type"] == "input.reset" || $field["type"] == "button.reset") { } else { if ($field["type"] == "input.submit" || $field["type"] == "input.image" || $field["type"] == "button.submit") { if (($submitname === false || $field["name"] === $submitname) && ($submitvalue === false || $field["value"] === $submitvalue)) { if ($submitname !== "") { if (!isset($fields[$field["name"]])) { $fields[$field["name"]] = array(); } $fields[$field["name"]][] = $field["value"]; } if ($field["type"] == "input.image") { if (!isset($fields["x"])) { $fields["x"] = array(); } $fields["x"][] = "1"; if (!isset($fields["y"])) { $fields["y"] = array(); } $fields["y"][] = "1"; } } } else { if ($field["type"] != "input.radio" && $field["type"] != "input.checkbox" || $field["checked"]) { if (!isset($fields[$field["name"]])) { $fields[$field["name"]] = array(); } $fields[$field["name"]][] = $field["value"]; } } } } } if ($method == "get") { $url = \CubicleSoft\HTTP::ExtractURL($this->info["action"]); unset($url["query"]); $url["queryvars"] = $fields; $result = array("url" => \CubicleSoft\HTTP::CondenseURL($url), "options" => array()); } else { $result = array("url" => $this->info["action"], "options" => array("postvars" => $fields, "files" => $files)); } return $result; }
public function Connect($url, $origin, $profile = "auto", $options = array(), $web = false) { $this->Disconnect(); if (class_exists("\\CubicleSoft\\CSPRNG", false) && $this->csprng === false) { $this->csprng = new \CubicleSoft\CSPRNG(); } if (isset($options["fp"]) && is_resource($options["fp"])) { $this->fp = $options["fp"]; } else { // Use WebBrowser to initiate the connection. if ($web === false) { $web = new \CubicleSoft\WebBrowser(); } // Transform URL. $url2 = \CubicleSoft\HTTP::ExtractURL($url); if ($url2["scheme"] != "ws" && $url2["scheme"] != "wss") { return array("success" => false, "error" => self::WSTranslate("\\CubicleSoft\\\\CubicleSoft\\WebSocket::Connect() only supports the 'ws' and 'wss' protocols."), "errorcode" => "protocol_check"); } $url2["scheme"] = str_replace("ws", "http", $url2["scheme"]); $url2 = \CubicleSoft\HTTP::CondenseURL($url2); // Generate correct request headers. if (!isset($options["headers"])) { $options["headers"] = array(); } $options["headers"]["Connection"] = "keep-alive, Upgrade"; if ($origin != "") { $options["headers"]["Origin"] = $origin; } $options["headers"]["Pragma"] = "no-cache"; $key = base64_encode($this->PRNGBytes(16)); $options["headers"]["Sec-WebSocket-Key"] = $key; $options["headers"]["Sec-WebSocket-Version"] = "13"; $options["headers"]["Upgrade"] = "websocket"; // No async support for connecting at this time. Async mode is enabled AFTER connecting though. unset($options["async"]); // Connect to the WebSocket. $result = $web->Process($url2, $profile, $options); if (!$result["success"]) { return $result; } if ($result["response"]["code"] != 101) { return array("success" => false, "error" => self::WSTranslate("\\CubicleSoft\\\\CubicleSoft\\WebSocket::Connect() failed to connect to the WebSocket. Server returned: %s %s", $result["response"]["code"], $result["response"]["meaning"]), "errorcode" => "incorrect_server_response"); } if (!isset($result["headers"]["Sec-Websocket-Accept"])) { return array("success" => false, "error" => self::WSTranslate("Server failed to include a 'Sec-WebSocket-Accept' header in its response to the request."), "errorcode" => "missing_server_websocket_accept_header"); } // Verify the Sec-WebSocket-Accept response. if ($result["headers"]["Sec-Websocket-Accept"][0] !== base64_encode(sha1($key . self::KEY_GUID, true))) { return array("success" => false, "error" => self::WSTranslate("The server's 'Sec-WebSocket-Accept' header is invalid."), "errorcode" => "invalid_server_websocket_accept_header"); } $this->fp = $result["fp"]; } // Enable non-blocking mode. stream_set_blocking($this->fp, 0); $this->state = self::STATE_OPEN; $this->readdata = ""; $this->readmessages = array(); $this->writedata = ""; $this->writemessages = array(); $this->lastkeepalive = time(); $this->keepalivesent = false; $this->rawrecvsize = 0; $this->rawsendsize = 0; return array("success" => true); }