/**
  * @return HttpClient
  */
 public function getRemoteConnexion(&$remoteSecureToken, $refreshSessId = false, $repository = null)
 {
     require_once AJXP_BIN_FOLDER . "/class.HttpClient.php";
     if ($repository != null) {
         $crtRep = $repository;
     } else {
         $crtRep = ConfService::getRepository();
     }
     $httpClient = new HttpClient($crtRep->getOption("HOST"));
     $httpClient->cookie_host = $crtRep->getOption("HOST");
     $httpClient->timeout = 10;
     if (isset($_SESSION["AJXP_REMOTE_SESSION"]) && is_array($_SESSION["AJXP_REMOTE_SESSION"])) {
         $httpClient->setCookies($_SESSION["AJXP_REMOTE_SESSION"]);
     }
     //$httpClient->setDebug(true);
     if (!isset($_SESSION["AJXP_REMOTE_SECURE_TOKEN"])) {
         $httpClient->get($crtRep->getOption("URI") . "?get_action=get_secure_token");
         $remoteSecureToken = $httpClient->getContent();
         $_SESSION["AJXP_REMOTE_SECURE_TOKEN"] = $remoteSecureToken;
     } else {
         $remoteSecureToken = $_SESSION["AJXP_REMOTE_SECURE_TOKEN"];
     }
     if (!$crtRep->getOption("USE_AUTH")) {
         return $httpClient;
     }
     $uri = "";
     if ($crtRep->getOption("AUTH_URI") != "") {
         $httpClient->setAuthorization($crtRep->getOption("AUTH_USER"), $crtRep->getOption("AUTH_PASS"));
         $uri = $crtRep->getOption("AUTH_URI") . "?secure_token={$remoteSecureToken}";
     }
     if (!isset($_SESSION["AJXP_REMOTE_SESSION"]) || !is_array($_SESSION["AJXP_REMOTE_SESSION"]) || $refreshSessId) {
         if ($uri == "") {
             $this->logDebug("Remote_fs : relog necessary");
             // Retrieve a seed!
             $httpClient->get($crtRep->getOption("URI") . "?get_action=get_seed&secure_token={$remoteSecureToken}");
             $seed = $httpClient->getContent();
             $cookies = $httpClient->getCookies();
             if (isset($cookies["AjaXplorer"])) {
                 $_SESSION["AJXP_REMOTE_SESSION"] = $cookies;
             }
             $user = $crtRep->getOption("AUTH_USER");
             $pass = $crtRep->getOption("AUTH_PASS");
             $pass = md5(md5($pass) . $seed);
             $uri = $crtRep->getOption("URI") . "?get_action=login&userid=" . $user . "&password="******"&login_seed={$seed}&secure_token={$remoteSecureToken}";
             $httpClient->get($uri);
             $content = $httpClient->getContent();
             $matches = array();
             if (preg_match_all('#.*?secure_token="(.*?)".*?#s', $content, $matches)) {
                 $remoteSecureToken = $matches[1][0];
                 $_SESSION["AJXP_REMOTE_SECURE_TOKEN"] = $remoteSecureToken;
             }
             $httpClient->setHeadersOnly(false);
         } else {
             $httpClient->setHeadersOnly(true);
             $httpClient->get($uri);
             $httpClient->setHeadersOnly(false);
         }
         $cookies = $httpClient->getCookies();
         $_SESSION["AJXP_REMOTE_SESSION"] = $httpClient->getCookies();
     } else {
         $httpClient->setCookies($_SESSION["AJXP_REMOTE_SESSION"]);
     }
     return $httpClient;
 }
function drupal_remote_auth($host, $uri, $login, $pass, $formId = "")
{
    $client = new HttpClient($host);
    $client->setHandleRedirects(false);
    $res = $client->get($uri);
    $content = $client->getContent();
    $xmlDoc = DOMDocument::loadHTML($content);
    $xPath = new DOMXPath($xmlDoc);
    if ($formId == "") {
        $formId = "user-login-form";
    }
    $nodes = $xPath->query('//form[@id="' . $formId . '"]');
    if (!$nodes->length) {
        return "";
    }
    $form = $nodes->item(0);
    $postUri = $form->getAttribute("action");
    $hiddens = $xPath->query('//input[@type="hidden"]', $form);
    AJXP_Logger::debug("Carry on Drupal hiddens " . $hiddens->length);
    $postData = array("name" => $login, "pass" => $pass, "Submit" => "Log in");
    foreach ($hiddens as $hiddenNode) {
        $postData[$hiddenNode->getAttribute("name")] = $hiddenNode->getAttribute("value");
    }
    $client->setHandleRedirects(false);
    $client->setHeadersOnly(true);
    $client->setCookies(extractResponseCookies($client));
    $res2 = $client->post($postUri, $postData);
    $newCookies = extractResponseCookies($client);
    if (isset($newCookies["AjaXplorer"])) {
        return $newCookies["AjaXplorer"];
    }
    return "";
}
 /**
  * @return HttpClient
  */
 function getRemoteConnexion(&$remoteSessionId, $refreshSessId = false)
 {
     require_once INSTALL_PATH . "/server/classes/class.HttpClient.php";
     $crtRep = ConfService::getRepository();
     $httpClient = new HttpClient($crtRep->getOption("HOST"));
     $httpClient->cookie_host = $crtRep->getOption("HOST");
     $httpClient->timeout = 50;
     //$httpClient->setDebug(true);
     if ($crtRep->getOption("AUTH_URI") != "") {
         $httpClient->setAuthorization($crtRep->getOption("AUTH_NAME"), $crtRep->getOption("AUTH_PASS"));
     }
     if (!isset($_SESSION["AJXP_REMOTE_SESSION"]) || $refreshSessId) {
         $httpClient->setHeadersOnly(true);
         $httpClient->get($crtRep->getOption("AUTH_URI"));
         $httpClient->setHeadersOnly(false);
         $cookies = $httpClient->getCookies();
         if (isset($cookies["PHPSESSID"])) {
             $_SESSION["AJXP_REMOTE_SESSION"] = $cookies["PHPSESSID"];
             $remoteSessionId = $cookies["PHPSESSID"];
         }
     } else {
         $remoteSessionId = $_SESSION["AJXP_REMOTE_SESSION"];
         $httpClient->setCookies(array("PHPSESSID" => $remoteSessionId));
     }
     return $httpClient;
 }
 /**
  * Initialize and return the HttpClient
  *
  * @return HttpClient
  */
 protected function createHttpClient()
 {
     require_once INSTALL_PATH . "/server/classes/class.HttpClient.php";
     $httpClient = new HttpClient($this->host);
     $httpClient->cookie_host = $this->host;
     $httpClient->timeout = 50;
     AJXP_Logger::debug("Creating Http client", array());
     //$httpClient->setDebug(true);
     if (!$this->use_auth) {
         return $httpClient;
     }
     $uri = "";
     if ($this->auth_path != "") {
         $httpClient->setAuthorization($this->user, $this->password);
         $uri = $this->auth_path;
     }
     if (!isset($_SESSION["AJXP_REMOTE_SESSION"])) {
         if ($uri == "") {
             // Retrieve a seed!
             $httpClient->get($this->path . "?get_action=get_seed");
             $seed = $httpClient->getContent();
             $user = $this->user;
             $pass = $this->password;
             $pass = md5(md5($pass) . $seed);
             $uri = $this->path . "?get_action=login&userid=" . $user . "&password="******"&login_seed={$seed}";
         }
         $httpClient->setHeadersOnly(true);
         $httpClient->get($uri);
         $httpClient->setHeadersOnly(false);
         $cookies = $httpClient->getCookies();
         if (isset($cookies["AjaXplorer"])) {
             $_SESSION["AJXP_REMOTE_SESSION"] = $cookies["AjaXplorer"];
             $remoteSessionId = $cookies["AjaXplorer"];
         }
     } else {
         $remoteSessionId = $_SESSION["AJXP_REMOTE_SESSION"];
         $httpClient->setCookies(array("AjaXplorer" => $remoteSessionId));
     }
     AJXP_Logger::debug("Http Client created", array());
     return $httpClient;
 }
 public function switchAction($action, $httpVars, $fileVars)
 {
     //AJXP_Logger::logAction("DL file", $httpVars);
     $repository = ConfService::getRepository();
     if (!$repository->detectStreamWrapper(false)) {
         return false;
     }
     $plugin = AJXP_PluginsService::findPlugin("access", $repository->getAccessType());
     $streamData = $plugin->detectStreamWrapper(true);
     $dir = AJXP_Utils::decodeSecureMagic($httpVars["dir"]);
     $destStreamURL = $streamData["protocol"] . "://" . $repository->getId() . $dir . "/";
     if (isset($httpVars["file"])) {
         $parts = parse_url($httpVars["file"]);
         $getPath = $parts["path"];
         $basename = basename($getPath);
     }
     if (isset($httpVars["dlfile"])) {
         $dlFile = $streamData["protocol"] . "://" . $repository->getId() . AJXP_Utils::decodeSecureMagic($httpVars["dlfile"]);
         $realFile = file_get_contents($dlFile);
         if (empty($realFile)) {
             throw new Exception("cannot find file {$dlFile} for download");
         }
         $parts = parse_url($realFile);
         $getPath = $parts["path"];
         $basename = basename($getPath);
     }
     switch ($action) {
         case "external_download":
             if (!ConfService::currentContextIsCommandLine() && ConfService::backgroundActionsSupported()) {
                 $unixProcess = AJXP_Controller::applyActionInBackground($repository->getId(), "external_download", $httpVars);
                 if ($unixProcess !== null) {
                     @file_put_contents($destStreamURL . "." . $basename . ".pid", $unixProcess->getPid());
                 }
                 AJXP_XMLWriter::header();
                 AJXP_XMLWriter::triggerBgAction("reload_node", array(), "Triggering DL ", true, 2);
                 AJXP_XMLWriter::close();
                 session_write_close();
                 exit;
             }
             require_once AJXP_BIN_FOLDER . "/class.HttpClient.php";
             $mess = ConfService::getMessages();
             session_write_close();
             $client = new HttpClient($parts["host"]);
             $collectHeaders = array("ajxp-last-redirection" => "", "content-disposition" => "", "content-length" => "");
             $client->setHeadersOnly(true, $collectHeaders);
             $client->setMaxRedirects(8);
             $client->setDebug(false);
             $client->get($getPath);
             $pidHiddenFileName = $destStreamURL . "." . $basename . ".pid";
             if (is_file($pidHiddenFileName)) {
                 $pid = file_get_contents($pidHiddenFileName);
                 @unlink($pidHiddenFileName);
             }
             AJXP_Logger::debug("COLLECTED HEADERS", $client->collectHeaders);
             $collectHeaders = $client->collectHeaders;
             $totalSize = -1;
             if (!empty($collectHeaders["content-disposition"]) && strstr($collectHeaders["content-disposition"], "filename") !== false) {
                 $ar = explode("filename=", $collectHeaders["content-disposition"]);
                 $basename = trim(array_pop($ar));
                 $basename = str_replace("\"", "", $basename);
                 // Remove quotes
             }
             if (!empty($collectHeaders["content-length"])) {
                 $totalSize = intval($collectHeaders["content-length"]);
                 AJXP_Logger::debug("Should download {$totalSize} bytes!");
             }
             if ($totalSize != -1) {
                 $node = new AJXP_Node($destStreamURL . $basename);
                 AJXP_Controller::applyHook("node.before_create", array($node, $totalSize));
             }
             $qData = false;
             if (!empty($collectHeaders["ajxp-last-redirection"])) {
                 $newParsed = parse_url($collectHeaders["ajxp-last-redirection"]);
                 $client->host = $newParsed["host"];
                 $getPath = $newParsed["path"];
                 if (isset($newParsed["query"])) {
                     $qData = parse_url($newParsed["query"]);
                 }
             }
             $tmpFilename = $destStreamURL . $basename . ".dlpart";
             $hiddenFilename = $destStreamURL . "__" . $basename . ".ser";
             $filename = $destStreamURL . $basename;
             $dlData = array("sourceUrl" => $getPath, "totalSize" => $totalSize);
             if (isset($pid)) {
                 $dlData["pid"] = $pid;
             }
             //file_put_contents($hiddenFilename, serialize($dlData));
             $fpHid = fopen($hiddenFilename, "w");
             fputs($fpHid, serialize($dlData));
             fclose($fpHid);
             $client->redirect_count = 0;
             $client->setHeadersOnly(false);
             $destStream = fopen($tmpFilename, "w");
             if ($destStream !== false) {
                 $client->writeContentToStream($destStream);
                 $client->get($getPath, $qData);
                 fclose($destStream);
             }
             rename($tmpFilename, $filename);
             unlink($hiddenFilename);
             if (isset($dlFile) && isset($httpVars["delete_dlfile"]) && is_file($dlFile)) {
                 AJXP_Controller::applyHook("node.before_change", array(new AJXP_Node($dlFile)));
                 unlink($dlFile);
                 AJXP_Controller::applyHook("node.change", array(new AJXP_Node($dlFile), null, false));
             }
             AJXP_Controller::applyHook("node.change", array(null, new AJXP_Node($filename), false));
             AJXP_XMLWriter::header();
             AJXP_XMLWriter::triggerBgAction("reload_node", array(), $mess["httpdownloader.8"]);
             AJXP_XMLWriter::close();
             exit;
             break;
         case "update_dl_data":
             $file = AJXP_Utils::decodeSecureMagic($httpVars["file"]);
             header("text/plain");
             if (is_file($destStreamURL . $file)) {
                 echo filesize($destStreamURL . $file);
             } else {
                 echo "stop";
             }
             exit;
             break;
         case "stop_dl":
             $newName = "__" . str_replace(".dlpart", ".ser", $basename);
             $hiddenFilename = $destStreamURL . $newName;
             $data = @unserialize(@file_get_contents($hiddenFilename));
             header("text/plain");
             AJXP_Logger::debug("Getting {$hiddenFilename}", $data);
             if (isset($data["pid"])) {
                 $process = new UnixProcess();
                 $process->setPid($data["pid"]);
                 $process->stop();
                 unlink($hiddenFilename);
                 unlink($destStreamURL . $basename);
                 echo 'stop';
             } else {
                 echo 'failed';
             }
             exit;
             break;
         default:
             break;
     }
     return true;
 }
 /**
  * @return HttpClient
  */
 function getRemoteConnexion(&$remoteSessionId, $refreshSessId = false)
 {
     require_once INSTALL_PATH . "/server/classes/class.HttpClient.php";
     $crtRep = ConfService::getRepository();
     $httpClient = new HttpClient($crtRep->getOption("HOST"));
     $httpClient->cookie_host = $crtRep->getOption("HOST");
     $httpClient->timeout = 10;
     //$httpClient->setDebug(true);
     if (!$crtRep->getOption("USE_AUTH")) {
         return $httpClient;
     }
     $uri = "";
     if ($crtRep->getOption("AUTH_URI") != "") {
         $httpClient->setAuthorization($crtRep->getOption("AUTH_USER"), $crtRep->getOption("AUTH_PASS"));
         $uri = $crtRep->getOption("AUTH_URI");
     }
     if (!isset($_SESSION["AJXP_REMOTE_SESSION"]) || $refreshSessId) {
         if ($uri == "") {
             // Retrieve a seed!
             $httpClient->get($crtRep->getOption("URI") . "?get_action=get_seed");
             $seed = $httpClient->getContent();
             $user = $crtRep->getOption("AUTH_USER");
             $pass = $crtRep->getOption("AUTH_PASS");
             $pass = md5(md5($pass) . $seed);
             $uri = $crtRep->getOption("URI") . "?get_action=login&userid=" . $user . "&password="******"&login_seed={$seed}";
         }
         $httpClient->setHeadersOnly(true);
         $httpClient->get($uri);
         $httpClient->setHeadersOnly(false);
         $cookies = $httpClient->getCookies();
         if (isset($cookies["AjaXplorer"])) {
             $_SESSION["AJXP_REMOTE_SESSION"] = $cookies["AjaXplorer"];
             $remoteSessionId = $cookies["AjaXplorer"];
         }
     } else {
         $remoteSessionId = $_SESSION["AJXP_REMOTE_SESSION"];
         $httpClient->setCookies(array("AjaXplorer" => $remoteSessionId));
     }
     return $httpClient;
 }