Skip to content
This repository has been archived by the owner on Nov 25, 2020. It is now read-only.

Commit

Permalink
Use http_class instead of HttpClient to enable SSL remote download. Fix
Browse files Browse the repository at this point in the history
  • Loading branch information
cdujeu committed Nov 11, 2014
1 parent 666c6ad commit 7dbd887
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 104 deletions.
153 changes: 76 additions & 77 deletions core/src/plugins/uploader.http/class.HttpDownloader.php
Expand Up @@ -40,10 +40,12 @@ public function switchAction($action, $httpVars, $fileVars)
$streamData = $plugin->detectStreamWrapper(true);
$dir = AJXP_Utils::decodeSecureMagic($httpVars["dir"]);
$destStreamURL = $streamData["protocol"]."://".$repository->getId().$dir."/";
$dlURL = null;
if (isSet($httpVars["file"])) {
$parts = parse_url($httpVars["file"]);
$getPath = $parts["path"];
$basename = basename($getPath);
$dlURL = $httpVars["file"];
}
if (isSet($httpVars["dlfile"])) {
$dlFile = $streamData["protocol"]."://".$repository->getId().AJXP_Utils::decodeSecureMagic($httpVars["dlfile"]);
Expand All @@ -52,6 +54,7 @@ public function switchAction($action, $httpVars, $fileVars)
$parts = parse_url($realFile);
$getPath = $parts["path"];
$basename = basename($getPath);
$dlURL = $realFile;
}

switch ($action) {
Expand All @@ -69,92 +72,89 @@ public function switchAction($action, $httpVars, $fileVars)
exit();
}

require_once AJXP_BIN_FOLDER."/class.HttpClient.php";
$mess = ConfService::getMessages();
require_once(AJXP_BIN_FOLDER."/http_class/http_class.php");
session_write_close();

$client = new HttpClient($parts["host"]);
$httpClient = new http_class();
$arguments = array();
$httpClient->GetRequestArguments($httpVars["file"], $arguments);
$err = $httpClient->Open($arguments);
$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);
}

$this->logDebug("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"]);
$this->logDebug("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);

if (empty($err)) {
$err = $httpClient->SendRequest($arguments);
$httpClient->follow_redirect = true;

$client->redirect_count = 0;
$client->setHeadersOnly(false);

$destStream = fopen($tmpFilename, "w");
if ($destStream !== false) {
$pidHiddenFileName = $destStreamURL.".".$basename.".pid";
if (is_file($pidHiddenFileName)) {
$pid = file_get_contents($pidHiddenFileName);
@unlink($pidHiddenFileName);
}
if (empty($err)) {

$httpClient->ReadReplyHeaders($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"]);
$this->logDebug("Should download $totalSize bytes!");
}
if ($totalSize != -1) {
$node = new AJXP_Node($destStreamURL.$basename);
AJXP_Controller::applyHook("node.before_create", array($node, $totalSize));
}

$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);

// NOW READ RESPONSE
$destStream = fopen($tmpFilename, "w");
while (true) {
$body = "";
$error = $httpClient->ReadReplyBody($body, 1000);
if($error != "" || strlen($body) == 0) break;
fwrite($destStream, $body, strlen($body));
}
fclose($destStream);
rename($tmpFilename, $filename);
unlink($hiddenFilename);
}
$httpClient->Close();

$client->writeContentToStream($destStream);
$client->get($getPath, $qData);
fclose($destStream);
if (isset($dlFile) && isSet($httpVars["delete_dlfile"]) && is_file($dlFile)) {
AJXP_Controller::applyHook("node.before_path_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();

}
rename($tmpFilename, $filename);
unlink($hiddenFilename);
if (isset($dlFile) && isSet($httpVars["delete_dlfile"]) && is_file($dlFile)) {
AJXP_Controller::applyHook("node.before_path_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"]);
Expand All @@ -164,7 +164,7 @@ public function switchAction($action, $httpVars, $fileVars)
} else {
echo "stop";
}
exit;

break;
case "stop_dl":
$newName = "__".str_replace(".dlpart", ".ser", $basename);
Expand All @@ -182,13 +182,12 @@ public function switchAction($action, $httpVars, $fileVars)
} else {
echo 'failed';
}
exit();
break;
default:
break;
}

return true;
return false;

}

Expand Down
28 changes: 1 addition & 27 deletions core/src/plugins/uploader.http/manifest.xml
Expand Up @@ -11,35 +11,9 @@
<registry_contributions>
<extensions>
<extension mime="download" messageId="httpdownloader.17" icon="httpdownloader_images/ICON_SIZE/kget_list.png"/>
<extension mime="dlpart" messageId="httpdownloader.18" icon="httpdownloader_images/ICON_SIZE/make.png"/>
<extension mime="dlpart" messageId="httpdownloader.18" icon="exe.png"/>
</extensions>
<actions>
<!--
<action name="upload">
<subMenu>
<dynamicBuilder><![CDATA[
var context = window.builderContext;
context.builderMenuItems = $A([]);
context.builderMenuItems.push({
name:MessageHash['httpdownloader.13'],
alt:MessageHash['httpdownloader.13'],
isDefault : true,
image:resolveImageSource('httpdownloader_images/ICON_SIZE/computer_upload.png', '', 22),
icon_class:'icon-upload',
callback:function(e){this.apply();}.bind(context)
} );
context.builderMenuItems.push({
name:MessageHash['httpdownloader.14'],
alt:MessageHash['httpdownloader.2'],
isDefault : false,
image:resolveImageSource('httpdownloader_images/ICON_SIZE/external_download.png', '', 22),
icon_class:'icon-cloud-upload',
callback:function(e){ajaxplorer.actionBar.fireAction('external_download');}.bind(context)
} );
]]></dynamicBuilder>
</subMenu>
</action>
-->
<action name="external_download">
<gui text="httpdownloader.1" title="httpdownloader.2" src="httpdownloader_images/ICON_SIZE/external_download.png" accessKey="httpdownloader.3" hasAccessKey="true">
<context selection="false" dir="true" recycle="hidden"
Expand Down

0 comments on commit 7dbd887

Please sign in to comment.