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 ProcessClientRequestHeaders($request, $headers, $id) { if (!isset($this->clients[$id])) { return false; } $client = $this->clients[$id]; $client->request = $request; $client->headers = array(); foreach ($headers as $key => $vals) { $client->headers[$key] = $vals[count($vals) - 1]; } if (isset($client->headers["Content-Type"])) { $client->contenttype = \CubicleSoft\HTTP::ExtractHeader($client->headers["Content-Type"]); } if (isset($client->headers["Host"])) { $client->headers["Host"] = preg_replace('/[^a-z0-9.:\\[\\]_-]/', "", strtolower($client->headers["Host"])); } $client->url = ($this->ssl ? "https" : "http") . "://" . (isset($client->headers["Host"]) ? $client->headers["Host"] : "localhost") . $request["path"]; // Process cookies. $client->cookievars = array(); $client->requestvars = array(); if (isset($client->headers["Cookie"])) { $cookies = explode(";", $client->headers["Cookie"]); foreach ($cookies as $cookie) { $pos = strpos($cookie, "="); if ($pos === false) { $name = $cookie; $val = ""; } else { $name = substr($cookie, 0, $pos); $val = urldecode(trim(substr($cookie, $pos + 1))); } $name = urldecode(trim($name)); $this->AddClientRecvHeader($id, $name, $val); if (substr($name, -2) !== "[]") { $client->cookievars[$name] = $val; } else { $name = substr($name, 0, -2); if (!isset($client->cookievars[$name]) || !is_array($client->cookievars[$name])) { $client->cookievars[$name] = array(); } $client->cookievars[$name][] = $val; } } } // Process query string. $url = \CubicleSoft\HTTP::ExtractURL($client->url); foreach ($url["queryvars"] as $name => $vals) { foreach ($vals as $val) { $this->AddClientRecvHeader($id, $name, $val); } } return true; }
public function InteractiveLogin() { echo self::DO_Translate("***************\n"); echo self::DO_Translate("Starting interactive login for DigitalOcean.\n\n"); echo self::DO_Translate("During the next few minutes, you will be asked to sign into your DigitalOcean account here on the command-line and then approve this application to have access to perform actions on your behalf within your DigitalOcean account. You may press Ctrl+C at any time to terminate this application.\n\n"); echo self::DO_Translate("Every web request made from this point on will be dumped to your console and take the form of \"Retrieving '[URL being retrieved]'...\".\n"); echo self::DO_Translate("***************\n\n"); $html = new \CubicleSoft\simple_html_dom(); $web = new \CubicleSoft\WebBrowser(array("httpopts" => array("pre_retrievewebpage_callback" => array($this, "InteractiveLogin__HandleRequest")))); $filteropts = \CubicleSoft\TagFilter::GetHTMLOptions(); $this->accesstokens["refreshtoken"] = false; $this->accesstokens["bearertoken"] = false; $this->accesstokens["bearerexpirets"] = -1; $result = array("url" => $this->GetLoginURL(), "options" => array()); do { $result["options"]["sslopts"] = self::InitSSLOpts(array()); $result2 = $web->Process($result["url"], "auto", $result["options"]); if (!$result2["success"]) { if ($this->accesstokens["refreshtoken"] === false) { return $result2; } echo self::DO_Translate("***************\n"); echo self::DO_Translate("Interactive login completed successfully. Resuming where you left off.\n"); echo self::DO_Translate("***************\n\n"); return array("success" => true); } else { if ($result2["response"]["code"] != 200) { return array("success" => false, "error" => self::DO_Translate("Expected a 200 response from DigitalOcean. Received '%s'.", $result2["response"]["line"]), "errorcode" => "unexpected_digitalocean_response", "info" => $result2); } else { $body = \CubicleSoft\TagFilter::Run($result2["body"], $filteropts); $html->load($body); echo "-----------------------\n\n"; $title = $html->find('title', 0); if ($title) { echo trim($title->plaintext) . "\n\n"; } $h1 = $html->find('h1', 0); if ($h1) { echo trim($h1->plaintext) . "\n\n"; } $h2 = $html->find('h2', 0); if ($h2) { echo trim($h1->plaintext) . "\n\n"; } $error = $html->find('.errors', 0); if ($error) { echo trim(preg_replace('/\\s+/', " ", $error->plaintext)) . "\n\n"; } $forms = $web->ExtractForms($result2["url"], $body); foreach ($forms as $num => $form) { if ($form->info["action"] === "https://cloud.digitalocean.com/login/refresh") { unset($forms[$num]); } } if (!count($forms)) { $url = \CubicleSoft\HTTP::ExtractURL($result2["url"]); if ($url["host"] === "cloud.digitalocean.com" && $url["path"] === "/v1/oauth/authorize") { // Construct a fake form. Might be a touch fragile. // Find the window.currentUser Javascript object. $user = false; $preauth = false; $lines = explode("\n", $body); foreach ($lines as $line) { $line = trim($line); if (preg_match('/window\\.currentUser\\s*=\\s*(\\{.*\\})/', $line, $matches)) { $user = json_decode($matches[1], true); } if ($preauth === false && substr($line, 0, 14) === "window.preAuth") { $preauth = true; } else { if (substr($line, 0, 5) === "name:" || substr($line, 0, 12) === "description:" || substr($line, 0, 4) === "url:") { echo $line . "\n\n"; } } } if ($user !== false) { $html2 = "<body>"; $html2 .= "<form action=\"/oauth/authorize\" method=\"post\">"; $html2 .= "<input type=\"hidden\" name=\"" . htmlspecialchars($html->find('meta[name=csrf-param]', 0)->content) . "\" value=\"" . htmlspecialchars($html->find('meta[name=csrf-token]', 0)->content) . "\">"; foreach ($url["queryvars"] as $key => $vals) { $html2 .= "<input type=\"hidden\" name=\"" . htmlspecialchars($key) . "\" value=\"" . htmlspecialchars($vals[0]) . "\">"; } $html2 .= "<input type=\"hidden\" name=\"context_id\" value=\"" . htmlspecialchars($user["current_context_id"]) . "\">"; $html2 .= "<input type=\"submit\" name=\"cancel\" value=\"Cancel\">"; $html2 .= "<input type=\"submit\" value=\"Authorize application\">"; $html2 .= "</form>"; $html2 .= "</body>"; $forms = $web->ExtractForms($result2["url"], $html2); } } } else { $text = $html->find('p'); if ($text) { foreach ($text as $text2) { echo trim(preg_replace('/\\s+/', " ", $text2->plaintext)) . "\n\n"; } } } $result = $web->InteractiveFormFill($forms); if ($result === false) { return array("success" => false, "error" => self::DO_Translate("Expected at least one form to exist. Received none."), "errorcode" => "invalid_digitalocean_response", "info" => $result2); } } } } while (1); }
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); }