public function switchAction($action, $httpVars, $postProcessData)
 {
     if (!isset($this->actions[$action])) {
         return false;
     }
     $repository = ConfService::getRepository();
     if (!$repository->detectStreamWrapper(false)) {
         return false;
     }
     $plugin = AJXP_PluginsService::findPlugin("access", $repository->getAccessType());
     $streamData = $plugin->detectStreamWrapper(true);
     $destStreamURL = $streamData["protocol"] . "://" . $repository->getId() . "/";
     if ($action == "audio_proxy") {
         $selection = new UserSelection($repository, $httpVars);
         // Backward compat
         $file = $selection->getUniqueFile();
         if (!file_exists($destStreamURL . $file) && strpos($httpVars["file"], "base64encoded:") === false) {
             // May be a backward compatibility problem, try to base64decode the filepath
             $file = AJXP_Utils::decodeSecureMagic(base64_decode($httpVars["file"]));
             if (!file_exists($destStreamURL . $file)) {
                 throw new Exception("Cannot find file!");
             }
         }
         $cType = "audio/" . array_pop(explode(".", $file));
         $localName = basename($file);
         $node = new AJXP_Node($destStreamURL . $file);
         if (method_exists($node->getDriver(), "filesystemFileSize")) {
             $size = $node->getDriver()->filesystemFileSize($node->getUrl());
         } else {
             $size = filesize($node->getUrl());
         }
         header("Content-Type: " . $cType . "; name=\"" . $localName . "\"");
         header("Content-Length: " . $size);
         $stream = fopen("php://output", "a");
         call_user_func(array($streamData["classname"], "copyFileInStream"), $destStreamURL . $file, $stream);
         fflush($stream);
         fclose($stream);
         AJXP_Controller::applyHook("node.read", array($node));
         $this->logInfo('Preview', 'Read content of ' . $node->getUrl());
         //exit(1);
     } else {
         if ($action == "ls") {
             if (!isset($httpVars["playlist"])) {
                 // This should not happen anyway, because of the applyCondition.
                 AJXP_Controller::passProcessDataThrough($postProcessData);
                 return;
             }
             // We transform the XML into XSPF
             $xmlString = $postProcessData["ob_output"];
             $xmlDoc = new DOMDocument();
             $xmlDoc->loadXML($xmlString);
             $xElement = $xmlDoc->documentElement;
             header("Content-Type:application/xspf+xml;charset=UTF-8");
             print '<?xml version="1.0" encoding="UTF-8"?>';
             print '<playlist version="1" xmlns="http://xspf.org/ns/0/">';
             print "<trackList>";
             foreach ($xElement->childNodes as $child) {
                 $isFile = $child->getAttribute("is_file") == "true";
                 $label = $child->getAttribute("text");
                 $ar = explode(".", $label);
                 $ext = strtolower(end($ar));
                 if (!$isFile || $ext != "mp3") {
                     continue;
                 }
                 print "<track><location>" . AJXP_SERVER_ACCESS . "?secure_token=" . AuthService::getSecureToken() . "&get_action=audio_proxy&file=" . base64_encode($child->getAttribute("filename")) . "</location><title>" . $label . "</title></track>";
             }
             print "</trackList>";
             AJXP_XMLWriter::close("playlist");
         }
     }
 }
 public function switchAction($action, $httpVars, $fileVars)
 {
     //$this->logInfo("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 . "/";
     $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"]);
         $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);
         $dlURL = $realFile;
     }
     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 . "/http_class/http_class.php";
             session_write_close();
             $httpClient = new http_class();
             $arguments = array();
             $httpClient->GetRequestArguments($httpVars["file"], $arguments);
             $err = $httpClient->Open($arguments);
             $collectHeaders = array("ajxp-last-redirection" => "", "content-disposition" => "", "content-length" => "");
             if (empty($err)) {
                 $err = $httpClient->SendRequest($arguments);
                 $httpClient->follow_redirect = true;
                 $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();
                 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));
                 }
                 $mess = ConfService::getMessages();
                 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();
             }
             break;
         case "update_dl_data":
             $file = AJXP_Utils::decodeSecureMagic($httpVars["file"]);
             header("text/plain");
             if (is_file($destStreamURL . $file)) {
                 $node = new AJXP_Node($destStreamURL . $file);
                 if (method_exists($node->getDriver(), "filesystemFileSize")) {
                     $filesize = $node->getDriver()->filesystemFileSize($node->getUrl());
                 } else {
                     $filesize = filesize($node->getUrl());
                 }
                 echo $filesize;
             } else {
                 echo "stop";
             }
             break;
         case "stop_dl":
             $newName = "__" . str_replace(".dlpart", ".ser", $basename);
             $hiddenFilename = $destStreamURL . $newName;
             $data = @unserialize(@file_get_contents($hiddenFilename));
             header("text/plain");
             $this->logDebug("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';
             }
             break;
         default:
             break;
     }
     return false;
 }
 public function switchAction($action, $httpVars, $filesVars)
 {
     if (!isset($this->actions[$action])) {
         return false;
     }
     $repository = ConfService::getRepositoryById($httpVars["repository_id"]);
     if (!$repository->detectStreamWrapper(true)) {
         return false;
     }
     if (AuthService::usersEnabled()) {
         $loggedUser = AuthService::getLoggedUser();
         if ($loggedUser === null && ConfService::getCoreConf("ALLOW_GUEST_BROWSING", "auth")) {
             AuthService::logUser("guest", null);
             $loggedUser = AuthService::getLoggedUser();
         }
         if (!$loggedUser->canSwitchTo($repository->getId())) {
             echo "You do not have permissions to access this resource";
             return false;
         }
     }
     $streamData = $repository->streamData;
     $destStreamURL = $streamData["protocol"] . "://" . $repository->getId();
     $selection = new UserSelection($repository, $httpVars);
     if ($action == "open_file") {
         $file = $selection->getUniqueFile();
         if (!file_exists($destStreamURL . $file)) {
             echo "File does not exist";
             return false;
         }
         $node = new AJXP_Node($destStreamURL . $file);
         if (method_exists($node->getDriver(), "filesystemFileSize")) {
             $filesize = $node->getDriver()->filesystemFileSize($node->getUrl());
         } else {
             $filesize = filesize($node->getUrl());
         }
         $fp = fopen($destStreamURL . $file, "rb");
         $fileMime = "application/octet-stream";
         //Get mimetype with fileinfo PECL extension
         if (class_exists("finfo")) {
             $finfo = new finfo(FILEINFO_MIME);
             $fileMime = $finfo->buffer(fread($fp, 100));
         }
         //Get mimetype with (deprecated) mime_content_type
         if (strpos($fileMime, "application/octet-stream") === 0 && function_exists("mime_content_type")) {
             $fileMime = @mime_content_type($fp);
         }
         //Guess mimetype based on file extension
         if (strpos($fileMime, "application/octet-stream") === 0) {
             $fileExt = substr(strrchr(basename($file), '.'), 1);
             if (empty($fileExt)) {
                 $fileMime = "application/octet-stream";
             } else {
                 $regex = "/^([\\w\\+\\-\\.\\/]+)\\s+(\\w+\\s)*({$fileExt}\\s)/i";
                 $lines = file($this->getBaseDir() . "/resources/other/mime.types");
                 foreach ($lines as $line) {
                     if (substr($line, 0, 1) == '#') {
                         continue;
                     }
                     // skip comments
                     $line = rtrim($line) . " ";
                     if (!preg_match($regex, $line, $matches)) {
                         continue;
                     }
                     // no match to the extension
                     $fileMime = $matches[1];
                 }
             }
         }
         fclose($fp);
         // If still no mimetype, give up and serve application/octet-stream
         if (empty($fileMime)) {
             $fileMime = "application/octet-stream";
         }
         //Send headers
         HTMLWriter::generateInlineHeaders(basename($file), $filesize, $fileMime);
         $class = $streamData["classname"];
         $stream = fopen("php://output", "a");
         call_user_func(array($streamData["classname"], "copyFileInStream"), $destStreamURL . $file, $stream);
         fflush($stream);
         fclose($stream);
         $node = new AJXP_Node($destStreamURL . $file);
         AJXP_Controller::applyHook("node.read", array($node));
         $this->logInfo('Download', 'Read content of ' . $node->getUrl());
     }
 }
 public function switchAction($action, $httpVars, $filesVars)
 {
     if (!isset($this->actions[$action])) {
         return false;
     }
     $repository = ConfService::getRepository();
     if (!$repository->detectStreamWrapper(true)) {
         return false;
     }
     $streamData = $repository->streamData;
     $destStreamURL = $streamData["protocol"] . "://" . $repository->getId();
     $selection = new UserSelection($repository, $httpVars);
     if ($action == "read_video_data") {
         $this->logDebug("Reading video");
         $file = $selection->getUniqueFile();
         $node = new AJXP_Node($destStreamURL . $file);
         session_write_close();
         if (method_exists($node->getDriver(), "filesystemFileSize")) {
             $filesize = $node->getDriver()->filesystemFileSize($node->getUrl());
         } else {
             $filesize = filesize($node->getUrl());
         }
         $filename = $destStreamURL . $file;
         //$fp = fopen($destStreamURL.$file, "r");
         if (preg_match("/\\.ogv\$/", $file)) {
             header("Content-Type: video/ogg; name=\"" . basename($file) . "\"");
         } else {
             if (preg_match("/\\.mp4\$/", $file)) {
                 header("Content-Type: video/mp4; name=\"" . basename($file) . "\"");
             } else {
                 if (preg_match("/\\.m4v\$/", $file)) {
                     header("Content-Type: video/x-m4v; name=\"" . basename($file) . "\"");
                 } else {
                     if (preg_match("/\\.webm\$/", $file)) {
                         header("Content-Type: video/webm; name=\"" . basename($file) . "\"");
                     }
                 }
             }
         }
         if (isset($_SERVER['HTTP_RANGE']) && $filesize != 0) {
             $this->logDebug("Http range", array($_SERVER['HTTP_RANGE']));
             // multiple ranges, which can become pretty complex, so ignore it for now
             $ranges = explode('=', $_SERVER['HTTP_RANGE']);
             $offsets = explode('-', $ranges[1]);
             $offset = floatval($offsets[0]);
             if ($offset == 0) {
                 $this->logInfo('Preview', 'Streaming content of ' . $file);
             }
             $length = floatval($offsets[1]) - $offset;
             if (!$length) {
                 $length = $filesize - $offset;
             }
             if ($length + $offset > $filesize || $length < 0) {
                 $length = $filesize - $offset;
             }
             header('HTTP/1.1 206 Partial Content');
             header('Content-Range: bytes ' . $offset . '-' . ($offset + $length - 1) . '/' . $filesize);
             header('Accept-Ranges:bytes');
             header("Content-Length: " . $length);
             $file = fopen($filename, 'rb');
             if (!is_resource($file)) {
                 throw new Exception("Cannot open file {$file}!");
             }
             fseek($file, 0);
             $relOffset = $offset;
             while ($relOffset > 2000000000.0) {
                 // seek to the requested offset, this is 0 if it's not a partial content request
                 fseek($file, 2000000000, SEEK_CUR);
                 $relOffset -= 2000000000;
                 // This works because we never overcome the PHP 32 bit limit
             }
             fseek($file, $relOffset, SEEK_CUR);
             while (ob_get_level()) {
                 ob_end_flush();
             }
             $readSize = 0.0;
             while (!feof($file) && $readSize < $length && connection_status() == 0) {
                 echo fread($file, 2048);
                 $readSize += 2048.0;
                 flush();
             }
             fclose($file);
         } else {
             $this->logInfo('Preview', 'Streaming content of ' . $file);
             $fp = fopen($filename, "rb");
             header("Content-Length: " . $filesize);
             header("Content-Range: bytes 0-" . ($filesize - 1) . "/" . $filesize . ";");
             header('Cache-Control: public');
             $class = $streamData["classname"];
             $stream = fopen("php://output", "a");
             call_user_func(array($streamData["classname"], "copyFileInStream"), $destStreamURL . $file, $stream);
             fflush($stream);
             fclose($stream);
         }
         AJXP_Controller::applyHook("node.read", array($node));
     } else {
         if ($action == "get_sess_id") {
             HTMLWriter::charsetHeader("text/plain");
             print session_id();
         }
     }
 }