FOSSology  3.2.0rc1
Open Source License Compliance by Open Source Software
ui-download.php
1 <?php
2 /***********************************************************
3  Copyright (C) 2008-2013 Hewlett-Packard Development Company, L.P.
4  Copyright (C) 2015 Siemens AG
5 
6  This program is free software; you can redistribute it and/or
7  modify it under the terms of the GNU General Public License
8  version 2 as published by the Free Software Foundation.
9 
10  This program is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  GNU General Public License for more details.
14 
15  You should have received a copy of the GNU General Public License along
16  with this program; if not, write to the Free Software Foundation, Inc.,
17  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18  ***********************************************************/
19 
25 use Monolog\Logger;
28 
33 class ui_download extends FO_Plugin
34 {
35  var $NoHTML = 1;
36 
37  function __construct()
38  {
39  $this->Name = "download";
40  $this->Title = _("Download File");
41  $this->Dependency = array();
42  $this->DBaccess = PLUGIN_DB_WRITE;
43  parent::__construct();
44  }
45 
49  function RegisterMenus()
50  {
51  global $SysConf;
52  $text = _("Download this file");
53  if ($_SESSION[Auth::USER_LEVEL] >= $SysConf['SYSCONFIG']['SourceCodeDownloadRights']) {
54  menu_insert("Browse-Pfile::Download",0,$this->Name,$text);
55  }
56 
57  } // RegisterMenus()
58 
63  function CheckRestore($Item, $Filename)
64  {
65  global $Plugins;
66 
67  $this->NoHeader = 0;
68  header('Content-type: text/html');
69  header("Pragma: no-cache"); /* for IE cache control */
70  header('Cache-Control: no-cache, must-revalidate, maxage=1, post-check=0, pre-check=0'); /* prevent HTTP/1.1 caching */
71  header('Expires: Expires: Thu, 19 Nov 1981 08:52:00 GMT'); /* mark it as expired (value from Apache default) */
72 
73  $V = "";
74  if (($this->NoMenu == 0) && ($this->Name != "menus")) {
75  $Menu = &$Plugins[plugin_find_id("menus")];
76  } else {
77  $Menu = null;
78  }
79 
80  /* DOCTYPE is required for IE to use styles! (else: css menu breaks) */
81  $V .= '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Frameset//EN" "xhtml1-frameset.dtd">' . "\n";
82 
83  $V .= "<html>\n";
84  $V .= "<head>\n";
85  $V .= "<meta name='description' content='The study of Open Source'>\n";
86  if ($this->NoHeader == 0) {
92  if (!empty($this->Title)) {
93  $V .= "<title>" . htmlentities($this->Title) . "</title>\n";
94  }
95  $V .= "<link rel='stylesheet' href='css/fossology.css'>\n";
96  if (!empty($Menu)) {
97  print $Menu->OutputCSS();
98  }
99  $V .= "</head>\n";
100  $V .= "<body class='text'>\n";
101  print $V;
102  if (! empty($Menu)) {
103  $Menu->Output($this->Title);
104  }
105  }
106 
107  $P = &$Plugins[plugin_find_id("view")];
108  $P->ShowView(null, "browse");
109  exit();
110  }
111 
112  function getResponse()
113  {
114  try {
115  $output = $this->getPathAndName();
116  list($Filename, $Name) = $output;
117  $response = $this->downloadFile($Filename, $Name);
118  }
119  catch(Exception $e)
120  {
121  $this->vars['content'] = $e->getMessage();
122  $response = $this->render($this->getTemplateName());
123  }
124  return $response;
125  }
126 
131  protected function getPathAndName()
132  {
133  if ($this->State != \PLUGIN_STATE_READY) {
134  throw new Exception('Download plugin is not ready');
135  }
136 
137  global $SysConf;
138  global $container;
140  $dbManager = $container->get('db.manager');
141  if (!$dbManager->getDriver()) {
142  throw new Exception("Missing database connection.");
143  }
144 
145  $reportId = GetParm("report",PARM_INTEGER);
146  $item = GetParm("item",PARM_INTEGER);
147  $logJq = GetParm('log', PARM_INTEGER);
148 
149  if (!empty($reportId)) {
150  $row = $dbManager->getSingleRow("SELECT * FROM reportgen WHERE job_fk = $1", array($reportId), "reportFileName");
151  if ($row === false) {
152  throw new Exception("Missing report");
153  }
154  $path = $row['filepath'];
155  $filename = basename($path);
156  $uploadId = $row['upload_fk'];
157  } elseif (!empty($logJq)) {
158  $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";
159  $row = $dbManager->getSingleRow($sql, array($logJq), "jqLogFileName");
160  if ($row === false) {
161  throw new Exception("Missing report");
162  }
163  $path = $row['jq_log'];
164  $filename = basename($path);
165  $uploadId = $row['job_upload_fk'];
166  } elseif (empty($item)) {
167  throw new Exception("Invalid item parameter");
168  } elseif ($_SESSION[Auth::USER_LEVEL] < $SysConf['SYSCONFIG']['SourceCodeDownloadRights']) {
169  throw new Exception("User permissions not sufficient for source code download");
170  } else {
171  $path = RepPathItem($item);
172  if (empty($path)) {
173  throw new Exception("Invalid item parameter");
174  }
175 
176  $fileHandle = @fopen( RepPathItem($item) ,"rb");
177  /* note that CheckRestore() does not return. */
178  if (empty($fileHandle)) {
179  $this->CheckRestore($item, $path);
180  }
181 
182  $row = $dbManager->getSingleRow("SELECT ufile_name, upload_fk FROM uploadtree WHERE uploadtree_pk = $1",array($item));
183  if ($row===false) {
184  throw new Exception("Missing item");
185  }
186  $filename = $row['ufile_name'];
187  $uploadId = $row['upload_fk'];
188  }
189 
190  /* @var $uploadDao UploadDao */
191  $uploadDao = $GLOBALS['container']->get('dao.upload');
192  if (!Auth::isAdmin() && !$uploadDao->isAccessible($uploadId, Auth::getGroupId())) {
193  throw new Exception("No Permission: $uploadId");
194  }
195  if (!file_exists($path)) {
196  throw new Exception("File does not exist");
197  }
198  if (!is_file($path)) {
199  throw new Exception("Not a regular file");
200  }
201  return array($path, $filename);
202  }
203 
210  protected function downloadFile($path, $filename)
211  {
212  global $container;
213  $session = $container->get('session');
214  $session->save();
215 
216  $filenameFallback = str_replace('%','_',$filename);
217  $filenameFallback = str_replace('/','_',$filenameFallback);
218  $filenameFallback = str_replace('\\','_',$filenameFallback);
219 
220  $response = new BinaryFileResponse($path);
221  $response->setContentDisposition(ResponseHeaderBag::DISPOSITION_ATTACHMENT, $filename, $filenameFallback);
222  if (pathinfo($filename, PATHINFO_EXTENSION) == 'docx') {
223  $response->headers->set('Content-Type', ''); // otherwise mineType would be zip
224  }
225 
226  $logger = $container->get("logger");
227  $logger->pushHandler(new NullHandler(Logger::DEBUG));
228  BrowserConsoleHandler::resetStatic();
229 
230  return $response;
231  }
232 
239  public function getReport($jobId)
240  {
241  $_GET['report'] = $jobId;
242  list($Filename, $Name) = $this->getPathAndName();
243  return $this->downloadFile($Filename, $Name);
244  }
245 }
246 
247 $NewPlugin = new ui_download();
248 $NewPlugin->Initialize();
RegisterMenus()
Customize submenus.
Definition: ui-download.php:49
downloadFile($path, $filename)
getReport($jobId)
CheckRestore($Item, $Filename)
Called if there is no file. User is queried if they want to reunpack.
Definition: ui-download.php:63
Definition: state.hpp:26
GetParm($parameterName, $parameterType)
This function will retrieve the variables and check data types.
Definition: common-parm.php:57
RepPathItem($Item, $Repo="files")
Given an uploadtree_pk, retrieve the pfile path.
#define PLUGIN_DB_WRITE
Plugin requires write permission on DB.
Definition: libfossology.h:50
const PARM_INTEGER
Definition: common-parm.php:25
This is the Plugin class. All plugins should:
Definition: FO_Plugin.php:67
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...
downlad file(s)
Definition: ui-download.php:33
render($templateName, $vars=null)
Definition: FO_Plugin.php:442