FOSSology  4.4.0
Open Source License Compliance by Open Source Software
ui-download.php
1 <?php
2 /*
3  SPDX-FileCopyrightText: © 2008-2013 Hewlett-Packard Development Company, L.P.
4  SPDX-FileCopyrightText: © 2015 Siemens AG
5 
6  SPDX-License-Identifier: GPL-2.0-only
7 */
8 
12 use Monolog\Handler\BrowserConsoleHandler;
13 use Monolog\Handler\NullHandler;
14 use Monolog\Logger;
15 use Symfony\Component\HttpFoundation\BinaryFileResponse;
16 use Symfony\Component\HttpFoundation\ResponseHeaderBag;
17 
22 class ui_download extends FO_Plugin
23 {
24  var $NoHTML = 1;
25 
26  function __construct()
27  {
28  $this->Name = "download";
29  $this->Title = _("Download File");
30  $this->Dependency = array();
31  $this->DBaccess = PLUGIN_DB_WRITE;
32  parent::__construct();
33  }
34 
38  function RegisterMenus()
39  {
40  global $SysConf;
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);
45  }
46 
47  } // RegisterMenus()
48 
53  function CheckRestore($Item, $Filename)
54  {
55  global $Plugins;
56 
57  $this->NoHeader = 0;
58  header('Content-type: text/html');
59  header("Pragma: no-cache"); /* for IE cache control */
60  header('Cache-Control: no-cache, must-revalidate, maxage=1, post-check=0, pre-check=0'); /* prevent HTTP/1.1 caching */
61  header('Expires: Expires: Thu, 19 Nov 1981 08:52:00 GMT'); /* mark it as expired (value from Apache default) */
62 
63  $V = "";
64  if (($this->NoMenu == 0) && ($this->Name != "menus")) {
65  $Menu = &$Plugins[plugin_find_id("menus")];
66  } else {
67  $Menu = null;
68  }
69 
70  /* DOCTYPE is required for IE to use styles! (else: css menu breaks) */
71  $V .= '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Frameset//EN" "xhtml1-frameset.dtd">' . "\n";
72 
73  $V .= "<html>\n";
74  $V .= "<head>\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";
84  }
85  $V .= "<link rel='stylesheet' href='css/fossology.css'>\n";
86  if (!empty($Menu)) {
87  print $Menu->OutputCSS();
88  }
89  $V .= "</head>\n";
90  $V .= "<body class='text'>\n";
91  print $V;
92  if (! empty($Menu)) {
93  $Menu->Output($this->Title);
94  }
95  }
96 
97  $P = &$Plugins[plugin_find_id("view")];
98  $P->ShowView(null, "browse");
99  exit();
100  }
101 
102  function getResponse()
103  {
104  try {
105  $output = $this->getPathAndName();
106  list($Filename, $Name) = $output;
107  $response = $this->downloadFile($Filename, $Name);
108  }
109  catch(Exception $e)
110  {
111  $this->vars['content'] = $e->getMessage();
112  $response = $this->render($this->getTemplateName());
113  }
114  return $response;
115  }
116 
121  protected function getPathAndName()
122  {
123  if ($this->State != \PLUGIN_STATE_READY) {
124  throw new Exception('Download plugin is not ready');
125  }
126 
127  global $SysConf;
128  global $container;
130  $dbManager = $container->get('db.manager');
131  if (!$dbManager->getDriver()) {
132  throw new Exception("Missing database connection.");
133  }
134 
135  $reportId = GetParm("report",PARM_INTEGER);
136  $item = GetParm("item",PARM_INTEGER);
137  $logJq = GetParm('log', PARM_INTEGER);
138 
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");
143  }
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");
152  }
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");
160  } else {
161  $path = RepPathItem($item);
162  if (empty($path)) {
163  throw new Exception("Invalid item parameter");
164  }
165 
166  $fileHandle = @fopen( RepPathItem($item) ,"rb");
167  /* note that CheckRestore() does not return. */
168  if (empty($fileHandle)) {
169  $this->CheckRestore($item, $path);
170  }
171 
172  $row = $dbManager->getSingleRow("SELECT ufile_name, upload_fk FROM uploadtree WHERE uploadtree_pk = $1",array($item));
173  if ($row===false) {
174  throw new Exception("Missing item");
175  }
176  $filename = $row['ufile_name'];
177  $uploadId = $row['upload_fk'];
178  }
179 
180  /* @var $uploadDao UploadDao */
181  $uploadDao = $GLOBALS['container']->get('dao.upload');
182  if (!Auth::isAdmin() && !$uploadDao->isAccessible($uploadId, Auth::getGroupId())) {
183  throw new Exception("No Permission: $uploadId");
184  }
185  if (!file_exists($path)) {
186  throw new Exception("File does not exist");
187  }
188  if (!is_file($path)) {
189  throw new Exception("Not a regular file");
190  }
191  return array($path, $filename);
192  }
193 
200  protected function downloadFile($path, $filename)
201  {
202  global $container;
203  $session = $container->get('session');
204  $session->save();
205 
206  $filenameFallback = str_replace('%','_',$filename);
207  $filenameFallback = str_replace('/','_',$filenameFallback);
208  $filenameFallback = str_replace('\\','_',$filenameFallback);
209 
210  $response = new BinaryFileResponse($path);
211  $response->setContentDisposition(ResponseHeaderBag::DISPOSITION_ATTACHMENT, $filename, $filenameFallback);
212  $response->headers->set('Content-Type', $response->getFile()->getMimeType());
213 
214  $logger = $container->get("logger");
215  $logger->pushHandler(new NullHandler(Logger::DEBUG));
216  BrowserConsoleHandler::resetStatic();
217 
218  return $response;
219  }
220 
227  public function getDownload($path, $name)
228  {
229  return $this->downloadFile($path, $name);
230  }
231 
238  public function getReport($jobId)
239  {
240  $_GET['report'] = $jobId;
241  list($Filename, $Name) = $this->getPathAndName();
242  return $this->downloadFile($Filename, $Name);
243  }
244 }
245 
246 $NewPlugin = new ui_download();
247 $NewPlugin->Initialize();
This is the Plugin class. All plugins should:
Definition: FO_Plugin.php:57
render($templateName, $vars=null)
Definition: FO_Plugin.php:434
Contains the constants and helpers for authentication of user.
Definition: Auth.php:24
Definition: state.hpp:16
downlad file(s)
Definition: ui-download.php:23
getDownload($path, $name)
downloadFile($path, $filename)
RegisterMenus()
Customize submenus.
Definition: ui-download.php:38
__construct()
base constructor. Most plugins will just use this
Definition: ui-download.php:26
CheckRestore($Item, $Filename)
Called if there is no file. User is queried if they want to reunpack.
Definition: ui-download.php:53
getReport($jobId)
menu_insert($Path, $LastOrder=0, $URI=NULL, $Title=NULL, $Target=NULL, $HTML=NULL)
Given a Path, order level for the last item, and optional plugin name, insert the menu item.
const PARM_INTEGER
Definition: common-parm.php:14
GetParm($parameterName, $parameterType)
This function will retrieve the variables and check data types.
Definition: common-parm.php:46
RepPathItem($Item, $Repo="files")
Given an uploadtree_pk, retrieve the pfile path.
Definition: common-repo.php:91
#define PLUGIN_DB_WRITE
Plugin requires write permission on DB.
Definition: libfossology.h:38