12 use Monolog\Handler\BrowserConsoleHandler;
13 use Monolog\Handler\NullHandler;
15 use Symfony\Component\HttpFoundation\BinaryFileResponse;
16 use Symfony\Component\HttpFoundation\ResponseHeaderBag;
28 $this->Name =
"download";
29 $this->Title = _(
"Download File");
30 $this->Dependency = array();
32 parent::__construct();
41 $text = _(
"Download this file");
42 if (array_key_exists(Auth::USER_LEVEL, $_SESSION) &&
43 $_SESSION[Auth::USER_LEVEL] >= $SysConf[
'SYSCONFIG'][
'SourceCodeDownloadRights']) {
44 menu_insert(
"Browse-Pfile::Download",0,$this->Name,$text);
58 header(
'Content-type: text/html');
59 header(
"Pragma: no-cache");
60 header(
'Cache-Control: no-cache, must-revalidate, maxage=1, post-check=0, pre-check=0');
61 header(
'Expires: Expires: Thu, 19 Nov 1981 08:52:00 GMT');
64 if (($this->NoMenu == 0) && ($this->Name !=
"menus")) {
65 $Menu = &$Plugins[plugin_find_id(
"menus")];
71 $V .=
'<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Frameset//EN" "xhtml1-frameset.dtd">' .
"\n";
75 $V .=
"<meta name='description' content='The study of Open Source'>\n";
76 if ($this->NoHeader == 0) {
82 if (!empty($this->Title)) {
83 $V .=
"<title>" . htmlentities($this->Title) .
"</title>\n";
85 $V .=
"<link rel='stylesheet' href='css/fossology.css'>\n";
87 print $Menu->OutputCSS();
90 $V .=
"<body class='text'>\n";
93 $Menu->Output($this->Title);
97 $P = &$Plugins[plugin_find_id(
"view")];
98 $P->ShowView(
null,
"browse");
105 $output = $this->getPathAndName();
106 list($Filename,
$Name) = $output;
111 $this->vars[
'content'] = $e->getMessage();
112 $response = $this->
render($this->getTemplateName());
121 protected function getPathAndName()
123 if ($this->
State != \PLUGIN_STATE_READY) {
124 throw new Exception(
'Download plugin is not ready');
130 $dbManager = $container->get(
'db.manager');
131 if (!$dbManager->getDriver()) {
132 throw new Exception(
"Missing database connection.");
139 if (!empty($reportId)) {
140 $row = $dbManager->getSingleRow(
"SELECT * FROM reportgen WHERE job_fk = $1", array($reportId),
"reportFileName");
141 if ($row ===
false) {
142 throw new Exception(
"Missing report");
144 $path = $row[
'filepath'];
145 $filename = basename($path);
146 $uploadId = $row[
'upload_fk'];
147 } elseif (!empty($logJq)) {
148 $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";
149 $row = $dbManager->getSingleRow($sql, array($logJq),
"jqLogFileName");
150 if ($row ===
false) {
151 throw new Exception(
"Missing report");
153 $path = $row[
'jq_log'];
154 $filename = basename($path);
155 $uploadId = $row[
'job_upload_fk'];
156 } elseif (empty($item)) {
157 throw new Exception(
"Invalid item parameter");
158 } elseif ($_SESSION[Auth::USER_LEVEL] < $SysConf[
'SYSCONFIG'][
'SourceCodeDownloadRights']) {
159 throw new Exception(
"User permissions not sufficient for source code download");
163 throw new Exception(
"Invalid item parameter");
168 if (empty($fileHandle)) {
172 $row = $dbManager->getSingleRow(
"SELECT ufile_name, upload_fk FROM uploadtree WHERE uploadtree_pk = $1",array($item));
174 throw new Exception(
"Missing item");
176 $filename = $row[
'ufile_name'];
177 $uploadId = $row[
'upload_fk'];
181 $uploadDao = $GLOBALS[
'container']->get(
'dao.upload');
182 if (!Auth::isAdmin() && !$uploadDao->isAccessible($uploadId, Auth::getGroupId())) {
183 throw new Exception(
"No Permission: $uploadId");
185 if (!file_exists($path)) {
186 throw new Exception(
"File does not exist");
188 if (!is_file($path)) {
189 throw new Exception(
"Not a regular file");
191 return array($path, $filename);
203 $session = $container->get(
'session');
206 $filenameFallback = str_replace(
'%',
'_',$filename);
207 $filenameFallback = str_replace(
'/',
'_',$filenameFallback);
208 $filenameFallback = str_replace(
'\\',
'_',$filenameFallback);
210 $response =
new BinaryFileResponse($path);
211 $response->setContentDisposition(ResponseHeaderBag::DISPOSITION_ATTACHMENT, $filename, $filenameFallback);
212 $response->headers->set(
'Content-Type', $response->getFile()->getMimeType());
214 $logger = $container->get(
"logger");
215 $logger->pushHandler(
new NullHandler(Logger::DEBUG));
216 BrowserConsoleHandler::resetStatic();
240 $_GET[
'report'] = $jobId;
241 list($Filename,
$Name) = $this->getPathAndName();
247 $NewPlugin->Initialize();
This is the Plugin class. All plugins should:
render($templateName, $vars=null)
Contains the constants and helpers for authentication of user.
getDownload($path, $name)
downloadFile($path, $filename)
RegisterMenus()
Customize submenus.
__construct()
base constructor. Most plugins will just use this
CheckRestore($Item, $Filename)
Called if there is no file. User is queried if they want to reunpack.
GetParm($parameterName, $parameterType)
This function will retrieve the variables and check data types.
RepPathItem($Item, $Repo="files")
Given an uploadtree_pk, retrieve the pfile path.
#define PLUGIN_DB_WRITE
Plugin requires write permission on DB.