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);
 }
Beispiel #2
0
 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);
 }