/** * \brief This function is called when user output is * requested. This function is responsible for content. */ protected function getPathAndName() { if ($this->State != \PLUGIN_STATE_READY) { throw new Exception('Download plugin is not ready'); } global $container; /** @var DbManager $dbManager */ $dbManager = $container->get('db.manager'); if (!$dbManager->getDriver()) { throw new Exception("Missing database connection."); } $reportId = GetParm("report", PARM_INTEGER); $item = GetParm("item", PARM_INTEGER); $logJq = GetParm('log', PARM_INTEGER); if (!empty($reportId)) { $row = $dbManager->getSingleRow("SELECT * FROM reportgen WHERE job_fk = \$1", array($reportId), "reportFileName"); if ($row === false) { throw new Exception("Missing report"); } $path = $row['filepath']; $filename = basename($path); $uploadId = $row['upload_fk']; } elseif (!empty($logJq)) { $sql = "SELECT jq_log, job_upload_fk FROM jobqueue LEFT JOIN job ON job.job_pk = jobqueue.jq_job_fk WHERE jobqueue.jq_pk =\$1"; $row = $dbManager->getSingleRow($sql, array($logJq), "jqLogFileName"); if ($row === false) { throw new Exception("Missing report"); } $path = $row['jq_log']; $filename = basename($path); $uploadId = $row['job_upload_fk']; } elseif (empty($item)) { throw new Exception("Invalid item parameter"); } else { $path = RepPathItem($item); if (empty($path)) { throw new Exception("Invalid item parameter"); } $fileHandle = @fopen(RepPathItem($item), "rb"); /* note that CheckRestore() does not return. */ if (empty($fileHandle)) { $this->CheckRestore($item, $path); } $row = $dbManager->getSingleRow("SELECT ufile_name, upload_fk FROM uploadtree WHERE uploadtree_pk = \$1", array($item)); if ($row === false) { throw new Exception("Missing item"); } $filename = $row['ufile_name']; $uploadId = $row['upload_fk']; } /* @var $uploadDao UploadDao */ $uploadDao = $GLOBALS['container']->get('dao.upload'); if (!Auth::isAdmin() && !$uploadDao->isAccessible($uploadId, Auth::getGroupId())) { throw new Exception("No Permission: {$uploadId}"); } if (!file_exists($path)) { throw new Exception("File does not exist"); } if (!is_file($path)) { throw new Exception("Not a regular file"); } return array($path, $filename); }
/** * \brief Generate the view contents in HTML and sends it * to stdout. * * \param $Name - the name for this plugin. * * \note This function is intended to be called from other plugins. */ function ShowView($Fin = NULL, $BackMod = "browse", $ShowMenu = 1, $ShowHeader = 1, $ShowText = NULL, $ViewOnly = False, $DispView = True) { if ($this->State != PLUGIN_STATE_READY) { return; } $V = ""; global $Plugins; $Upload = GetParm("upload", PARM_INTEGER); if (!empty($Upload)) { $UploadPerm = GetUploadPerm($Upload); if ($UploadPerm < PERM_READ) { return; } } $Folder = GetParm("folder", PARM_INTEGER); $Show = GetParm("show", PARM_STRING); $Item = GetParm("item", PARM_INTEGER); $nomosagent_pk = GetParm("napk", PARM_INTEGER); $Page = GetParm("page", PARM_INTEGER); $bucketagent_pk = GetParm("bapk", PARM_INTEGER); if (!$Fin && (empty($Item) || empty($Upload))) { return; } if (empty($Page)) { $Page = 0; } /* Get uploadtree table name */ $uploadtree_tablename = "uploadtree"; if (!empty($Upload)) { $uploadtree_tablename = GetUploadtreeTablename($Upload); } switch (GetParm("format", PARM_STRING)) { case 'hex': $Format = 'hex'; break; case 'text': $Format = 'text'; break; case 'flow': $Format = 'flow'; break; default: /* Determine default show based on mime type */ if (empty($Item)) { $Format = 'text'; } else { $Meta = GetMimeType($Item); list($Type, $Junk) = explode("/", $Meta, 2); if ($Type == 'text') { $Format = 'flow'; } else { switch ($Meta) { case "application/octet-string": case "application/x-awk": case "application/x-csh": case "application/x-javascript": case "application/x-perl": case "application/x-shellscript": case "application/x-rpm-spec": case "application/xml": case "message/rfc822": $Format = 'flow'; break; default: $Format = 'flow'; } } } break; } /********************************** Display micro header **********************************/ if ($ShowHeader) { $Uri = Traceback_uri() . "?mod=browse"; $Opt = ""; if (!empty($Item)) { $Opt .= "&item={$Item}"; } if (!empty($Upload)) { $Opt .= "&upload={$Upload}"; } if (!empty($Folder)) { $Opt .= "&folder={$Folder}"; } if (!empty($Show)) { $Opt .= "&show={$Show}"; } /* No item */ $V .= Dir2Browse($BackMod, $Item, NULL, 1, "View", -1, '', '', $uploadtree_tablename) . "<P />\n"; } // if ShowHeader $this->SortHighlightMenu(); /*********************************** Display file contents ***********************************/ print $V; $V = ""; $openedFin = False; if (empty($Fin)) { $Fin = @fopen(RepPathItem($Item), "rb"); if ($Fin) { $openedFin = true; } if (empty($Fin)) { /* Added by vincent implement when view files which not in repository, ask user if want to reunpack*/ /** BEGIN **/ /* If this is a POST, then process the request. */ $uploadunpack = GetParm('uploadunpack', PARM_INTEGER); $uploadpk = $Upload; $flag = 0; $P =& $Plugins[plugin_find_id("ui_reunpack")]; $state = $P->CheckStatus($uploadpk, "reunpack", "ununpack"); //print "<p>$state</p>"; if ($state == 0 || $state == 2) { if (!empty($uploadunpack)) { $rc = $P->AgentAdd($uploadpk); if (empty($rc)) { /* Need to refresh the screen */ $text = _("Unpack added to job queue"); $V .= displayMessage($text); $flag = 1; $text = _("Reunpack job is running: you can see it in"); $text1 = _("jobqueue"); print "<p> <font color=red>{$text} <a href='" . Traceback_uri() . "?mod=showjobs'>{$text1}</a></font></p>"; } else { $text = _("Unpack of Upload failed"); $V .= displayMessage("{$text}: {$rc}"); } print $V; } } else { $flag = 1; $text = _("Reunpack job is running: you can see it in"); $text1 = _("jobqueue"); print "<p> <font color=red>{$text} <a href='" . Traceback_uri() . "?mod=showjobs'>{$text1}</a></font></p>"; } $text = _("File contents are not available in the repository."); print "{$text}\n"; $P =& $Plugins[plugin_find_id("ui_reunpack")]; print $P->ShowReunpackView($Item, $flag); return; } /** END **/ } rewind($Fin); $Pages = ""; $Uri = preg_replace('/&page=[0-9]*/', '', Traceback()); $HighlightMenu = ""; $HighlightMenu .= "</center>"; // some fcn left a dangling center if ($Format == 'hex') { $HighlightMenu .= $this->GetHighlightMenu(VIEW_BLOCK_HEX, $ViewOnly, $DispView); if (!empty($HighlightMenu)) { print "<center>{$HighlightMenu}</center><hr>\n"; } $PageMenu = $this->GetFileJumpMenu($Fin, $Page, VIEW_BLOCK_HEX, $Uri); $PageSize = VIEW_BLOCK_HEX * $Page; if (!empty($PageMenu)) { print "<center>{$PageMenu}</center><br>\n"; } $this->ShowHex($Fin, $PageSize, VIEW_BLOCK_HEX); if (!empty($PageMenu)) { print "<P /><center>{$PageMenu}</center><br>\n"; } } else { if ($Format == 'text') { $HighlightMenu .= $this->GetHighlightMenu(VIEW_BLOCK_TEXT, $ViewOnly, $DispView); if (!empty($HighlightMenu)) { print "<center>{$HighlightMenu}</center><hr>\n"; } $PageMenu = $this->GetFileJumpMenu($Fin, $Page, VIEW_BLOCK_TEXT, $Uri); $PageSize = VIEW_BLOCK_TEXT * $Page; if (!empty($PageMenu)) { print "<center>{$PageMenu}</center><br>\n"; } $this->ShowText($Fin, $PageSize, 0, VIEW_BLOCK_TEXT); if (!empty($PageMenu)) { print "<P /><center>{$PageMenu}</center><br>\n"; } } else { if ($Format == 'flow') { $HighlightMenu .= $this->GetHighlightMenu(VIEW_BLOCK_TEXT, $ViewOnly, $DispView); if (!empty($HighlightMenu)) { print "<center>{$HighlightMenu}</center><hr>\n"; } $PageMenu = $this->GetFileJumpMenu($Fin, $Page, VIEW_BLOCK_TEXT, $Uri); $PageSize = VIEW_BLOCK_TEXT * $Page; if (!empty($PageMenu)) { print "<center>{$PageMenu}</center><br>\n"; } if (!empty($ShowText)) { echo $ShowText, "<hr>"; } $this->ShowText($Fin, $PageSize, 1, VIEW_BLOCK_TEXT); if (!empty($PageMenu)) { print "<P /><center>{$PageMenu}</center><br>\n"; } } } } if ($openedFin) { fclose($Fin); } return; }
/** * \brief Generate the view contents in HTML * * @param resource $inputFile * @param string $BackMod * @param int $ShowMenu * @param int $ShowHeader * @param null $ShowText * @param bool $ViewOnly * @param bool $DispView * @param Highlight[] $highlightEntries * @param bool $insertBacklink * * \note This function is intended to be called from other plugins. */ function getView($inputFile = NULL, $BackMod = "browse", $ShowHeader = 1, $ShowText = NULL, $highlightEntries = array(), $insertBacklink = false, $getPageMenuInline = false) { if ($this->State != PLUGIN_STATE_READY) { $output = "Invalid plugin state: " . $this->State; return $getPageMenuInline ? array("Error", $output) : $output; } $Upload = GetParm("upload", PARM_INTEGER); if (!empty($Upload) && !$this->uploadDao->isAccessible($Upload, Auth::getGroupId())) { $output = "Access denied"; return $getPageMenuInline ? array("Error", $output) : $output; } $Item = GetParm("item", PARM_INTEGER); $Page = GetParm("page", PARM_INTEGER); $licenseId = GetParm("licenseId", PARM_INTEGER); if (!$inputFile && empty($Item)) { $output = "invalid input file"; return $getPageMenuInline ? array("Error", $output) : $output; } $uploadtree_tablename = $this->uploadDao->getUploadtreeTableName($Upload); if ($ShowHeader) { $Uri = Traceback_uri() . "?mod=browse" . Traceback_parm_keep(array('item', 'show', 'folder', 'upload')); /* No item */ $header = Dir2Browse($BackMod, $Item, NULL, $showBox = 0, "View", -1, '', '', $uploadtree_tablename); $this->vars['micromenu'] = $header; } /* Display file contents */ $output = ""; $openedFin = False; $Format = $this->microMenu->getFormatParameter($Item); if (empty($inputFile)) { $inputFile = @fopen(RepPathItem($Item), "rb"); if ($inputFile) { $openedFin = true; } if (empty($inputFile)) { $output = $this->outputWhenFileNotInRepo($Upload, $Item); return $getPageMenuInline ? array("Error", $output) : $output; } } rewind($inputFile); $Uri = preg_replace('/&page=[0-9]*/', '', Traceback()); $blockSize = $Format == 'hex' ? $this->blockSizeHex : $this->blockSizeText; if (!isset($Page) && !empty($licenseId)) { $startPos = -1; foreach ($highlightEntries as $highlightEntry) { if ($highlightEntry->getLicenseId() == $licenseId && ($startPos == -1 || $startPos > $highlightEntry->getStart())) { $startPos = $highlightEntry->getStart(); } } if ($startPos != -1) { $Page = floor($startPos / $blockSize); } } if (!empty($ShowText)) { echo $ShowText, "<hr>"; } $PageMenu = $this->GetFileJumpMenu($inputFile, $Page, $blockSize, $Uri); $PageSize = $blockSize * $Page; if (!empty($PageMenu) and !$getPageMenuInline) { $output .= "<center>{$PageMenu}</center><br>\n"; } $startAt = $PageSize; $endAt = $PageSize + $blockSize; $relevantHighlightEntries = array(); foreach ($highlightEntries as $highlightEntry) { if ($highlightEntry->getStart() < $endAt && $highlightEntry->getEnd() >= $startAt) { $relevantHighlightEntries[] = $highlightEntry; } } $this->highlightProcessor->sortHighlights($relevantHighlightEntries); $splitPositions = $this->highlightProcessor->calculateSplitPositions($relevantHighlightEntries); if ($Format == 'hex') { $output .= $this->getHex($inputFile, $PageSize, $this->blockSizeHex, $splitPositions); } else { $output .= $this->getText($inputFile, $PageSize, $Format == 'text' ? 0 : 1, $this->blockSizeText, $splitPositions, $insertBacklink); } if (!empty($PageMenu) and !$getPageMenuInline) { $output .= "<P /><center>{$PageMenu}</center><br>\n"; } if ($openedFin) { fclose($inputFile); } return $getPageMenuInline ? array($PageMenu, $output) : $output; }
/** * \brief This function is called when user output is * requested. This function is responsible for content. */ function Output() { if ($this->State != PLUGIN_STATE_READY) { return; } global $Plugins; global $PG_CONN; if (!$PG_CONN) { DBconnect(); if (!$PG_CONN) { $text = _("Missing database connection."); echo "<h2>{$text}</h2>"; return; } } $Item = GetParm("item", PARM_INTEGER); $text = _("Invalid item parameter"); if (empty($Item)) { echo "<h2>{$text}</h2>"; return; } $Filename = RepPathItem($Item); if (empty($Filename)) { echo "<h2>{$text}: {$Filename}</h2>"; return; } $Fin = @fopen(RepPathItem($Item), "rb"); /* note that CheckRestore() does not return. */ if (empty($Fin)) { $this->CheckRestore($Item, $Filename); } $sql = "SELECT ufile_name, upload_fk FROM uploadtree WHERE uploadtree_pk = {$Item} LIMIT 1;"; $result = pg_query($PG_CONN, $sql); DBCheckResult($result, $sql, __FILE__, __LINE__); $row = pg_fetch_assoc($result); if (pg_num_rows($result) != 1) { $text = _("Missing item"); echo "<h2>{$text}: {$Item}</h2>"; pg_free_result($result); return; } $Upload = $row['upload_fk']; $UploadPerm = GetUploadPerm($Upload); if ($UploadPerm < PERM_WRITE) { $text = _("No Permission"); echo "<h2>{$text}: {$Item}</h2>"; return; } $Name = $row['ufile_name']; pg_free_result($result); if (($rv = DownloadFile($Filename, $Name)) !== True) { $text = _("Download failed"); echo "<h2>{$text}</h2>{$Filename}<br>{$rv}"; } }