FOSSology  4.4.0
Open Source License Compliance by Open Source Software
ui-view.php
1 <?php
2 /*
3  SPDX-FileCopyrightText: © 2008-2011 Hewlett-Packard Development Company, L.P.
4  SPDX-FileCopyrightText: © 2015 Siemens AG
5 
6  SPDX-License-Identifier: GPL-2.0-only
7 */
15 use Monolog\Logger;
16 
17 class ui_view extends FO_Plugin
18 {
19  const NAME = "view";
21  private $logger;
23  private $textRenderer;
25  private $highlightProcessor;
27  private $uploadDao;
29  protected $blockSizeHex = 8192;
31  protected $blockSizeText = 81920;
32 
33  function __construct()
34  {
35  $this->Name = self::NAME;
36  $this->Title = _("View File");
37  $this->Dependency = array("browse");
38  $this->DBaccess = PLUGIN_DB_READ;
39  $this->LoginFlag = 0;
40 
41  parent::__construct();
42 
43  if (array_key_exists('BlockSizeHex', $GLOBALS['SysConf']['SYSCONFIG'])) {
44  $this->blockSizeHex = max(64,
45  $GLOBALS['SysConf']['SYSCONFIG']['BlockSizeHex']);
46  }
47  if (array_key_exists('BlockSizeText', $GLOBALS['SysConf']['SYSCONFIG'])) {
48  $this->blockSizeText = max(64,
49  $GLOBALS['SysConf']['SYSCONFIG']['BlockSizeText']);
50  }
51 
52  global $container;
53  $this->logger = $container->get("logger");
54  $this->textRenderer = $container->get("view.text_renderer");
55  $this->highlightProcessor = $container->get("view.highlight_processor");
56  $this->uploadDao = $container->get("dao.upload");
57  }
58 
62  function RegisterMenus()
63  {
64  $tooltipText = _("View file contents");
65  menu_insert("Browse-Pfile::View", 10, $this->Name, $tooltipText);
66  // For the Browse menu, permit switching between detail and summary.
67 
68  $itemId = GetParm("item", PARM_INTEGER);
69  $textFormat = $this->microMenu->getFormatParameter($itemId);
70  $pageNumber = GetParm("page", PARM_INTEGER);
71  $this->microMenu->addFormatMenuEntries($textFormat, $pageNumber);
72 
73  $URI = Traceback_parm_keep(
74  array(
75  "show",
76  "format",
77  "page",
78  "upload",
79  "item"
80  ));
81  $menuPosition = 59;
82  $menuText = "View";
83  $tooltipText = _("View file contents");
84  $this->microMenu->insert(MicroMenu::TARGET_DEFAULT, $menuText, $menuPosition,
85  $this->Name, $this->Name . $URI, $tooltipText);
86 
87  if (GetParm("mod", PARM_STRING) != $this->Name) {
88  menu_insert("Browse::{$menuText}", - 2, $this->Name . $URI, $tooltipText);
89  menu_insert("Browse::[BREAK]", - 1);
90  }
91  } // RegisterMenus()
92 
98  function GetFileJumpMenu($Fin, $CurrPage, $PageSize, $Uri)
99  {
100  if (! $Fin) {
101  return;
102  }
103  $Stat = fstat($Fin);
104  $MaxSize = $Stat['size'];
105  $MaxPage = intval($MaxSize / $PageSize);
106  $V = "<font class='text'>";
107  $CurrSize = $CurrPage * $PageSize;
108 
109  $Pages = 0; /* How many pages are there? */
110 
111  if ($CurrPage * $PageSize >= $MaxSize) {
112  $CurrPage = 0;
113  $CurrSize = 0;
114  }
115  if ($CurrPage < 0) {
116  $CurrPage = 0;
117  }
118 
119  if ($CurrPage > 0) {
120  $text = _("First");
121  $V .= "<a href='$Uri&page=0'>[$text]</a> ";
122  $text = _("Prev");
123  $V .= "<a href='$Uri&page=" . ($CurrPage - 1) . "'>[$text]</a> ";
124  $Pages ++;
125  }
126  for ($i = $CurrPage - 5; $i <= $CurrPage + 5; $i ++) {
127  if ($i == $CurrPage) {
128  $V .= "<b>" . ($i + 1) . "</b> ";
129  } else if (($i >= 0) && ($i <= $MaxPage)) {
130  $V .= "<a href='$Uri&page=$i'>" . ($i + 1) . "</a> ";
131  }
132  }
133  if ($CurrPage < $MaxPage) {
134  $text = _("Next");
135  $V .= "<a href='$Uri&page=" . ($CurrPage + 1) . "'>[$text]</a>";
136  $text = _("Last");
137  $V .= "<a href='$Uri&page=" . (intval(($MaxSize - 1) / $PageSize)) .
138  "'>[$text]</a>";
139  $Pages ++;
140  }
141  $V .= "</font>";
142 
143  /* If there is only one page, return nothing */
144  if ($Pages == 0) {
145  return;
146  }
147  return ($V);
148  } // GetFileJumpMenu()
149 
154  function ShowText($inputFile, $startOffset, $Flowed, $outputLength = -1,
155  $splitPositions = null, $insertBacklink = false)
156  {
157  print
158  $this->getText($inputFile, $startOffset, $Flowed, $outputLength,
159  $splitPositions, $insertBacklink);
160  }
161 
165  function getText($inputFile, $startOffset, $Flowed, $outputLength = -1,
166  $splitPositions = null, $insertBacklink = false, $fromRest = false)
167  {
168  if (! ($outputLength = $this->checkAndPrepare($inputFile, $startOffset,
169  $outputLength))) {
170  return "";
171  }
172 
173  $output = "";
174  $output .= ($Flowed ? '<div class="text">' : '<div class="mono"><pre style="overflow:unset;">');
175 
176  fseek($inputFile, $startOffset, SEEK_SET);
177  $textFragment = new TextFragment($startOffset,
178  fread($inputFile, $outputLength));
179 
180  $renderedText = $this->textRenderer->renderText($textFragment,
181  $splitPositions, $insertBacklink);
182 
183  $output .= ($Flowed ? nl2br($renderedText) : $renderedText) .
184  (! $Flowed ? "</pre>" : "") . "</div>\n";
185 
186  return $fromRest ? $renderedText : $output;
187  } // ShowText()
188 
193  function ShowHex($inputFile, $startOffset = 0, $outputLength = -1,
194  $splitPositions = array())
195  {
196  print $this->getHex($inputFile, $startOffset, $outputLength, $splitPositions);
197  }
198 
203  function getHex($inputFile, $startOffset = 0, $outputLength = -1,
204  $splitPositions = array())
205  {
206  if (! ($outputLength = $this->checkAndPrepare($inputFile, $startOffset,
207  $outputLength))) {
208  return "";
209  }
210 
211  $output = "";
212  fseek($inputFile, $startOffset, SEEK_SET);
213  $textFragment = new TextFragment($startOffset,
214  fread($inputFile, $outputLength));
215 
216  $output .= "<div class='mono'>";
217 
218  $renderedText = $this->textRenderer->renderHex($textFragment,
219  $splitPositions);
220  $output .= $renderedText;
221 
222  $output .= "</div>\n";
223 
224  return $output;
225  } // ShowHex()
226 
227  private function checkAndPrepare($inputFile, $startOffset, $outputLength)
228  {
229  if (! $inputFile) {
230  return False;
231  }
232 
233  $inputFileStat = fstat($inputFile);
234  $inputFileSize = $inputFileStat['size'];
235 
236  if ($outputLength < 0) {
237  $outputLength = $inputFileSize;
238  }
239 
240  if (($startOffset < 0) || ($startOffset >= $inputFileSize)) {
241  return False;
242  }
243 
244  if ($outputLength == 0) {
245  return false;
246  }
247  return $outputLength;
248  }
249 
266  function ShowView($inputFile = null, $BackMod = "browse", $ShowMenu = 1, $ShowHeader = 1,
267  $ShowText = null, $ViewOnly = false, $DispView = true, $highlightEntries = array(),
268  $insertBacklink = false)
269  {
270  return $this->getView($inputFile, $BackMod, $ShowHeader, $ShowText,
271  $highlightEntries, $insertBacklink);
272  }
273 
288  function getView($inputFile = null, $BackMod = "browse", $ShowHeader = 1, $ShowText = null,
289  $highlightEntries = array(), $insertBacklink = false, $getPageMenuInline = false)
290  {
291  if ($this->State != PLUGIN_STATE_READY) {
292  $output = "Invalid plugin state: " . $this->State;
293  return $getPageMenuInline ? array("Error", $output) : $output;
294  }
295 
296  $Upload = GetParm("upload", PARM_INTEGER);
297  if (! empty($Upload) &&
298  ! $this->uploadDao->isAccessible($Upload, Auth::getGroupId())) {
299  $output = "Access denied";
300  return $getPageMenuInline ? array("Error", $output) : $output;
301  }
302 
303  $Item = GetParm("item", PARM_INTEGER);
304  $Page = GetParm("page", PARM_INTEGER);
305  $licenseId = GetParm("licenseId", PARM_INTEGER);
306  if (! $inputFile && empty($Item)) {
307  $output = "invalid input file";
308  return $getPageMenuInline ? array("Error", $output) : $output;
309  }
310 
311  $uploadtree_tablename = $this->uploadDao->getUploadtreeTableName($Upload);
312 
313  if ($ShowHeader) {
314  $Uri = Traceback_uri() . "?mod=browse" .
315  Traceback_parm_keep(array('item', 'show', 'folder', 'upload'));
316  /* No item */
317  $header = Dir2Browse($BackMod, $Item, null, $showBox = 0, "View", - 1, '',
318  '', $uploadtree_tablename);
319  $this->vars['micromenu'] = $header;
320  }
321 
322  /* Display file contents */
323  $output = "";
324  $openedFin = False;
325  $Format = $this->microMenu->getFormatParameter($Item);
326  if (empty($inputFile)) {
327  $inputFile = @fopen(RepPathItem($Item), "rb");
328  if ($inputFile) {
329  $openedFin = true;
330  }
331  if (empty($inputFile)) {
332  $output = $this->outputWhenFileNotInRepo($Upload, $Item);
333  return $getPageMenuInline ? array("Error", $output) : $output;
334  }
335  }
336  rewind($inputFile);
337  $Uri = preg_replace('/&page=[0-9]*/', '', Traceback());
338 
339  $blockSize = $Format == 'hex' ? $this->blockSizeHex : $this->blockSizeText;
340 
341  if (! isset($Page) && ! empty($licenseId)) {
342  $startPos = - 1;
343  foreach ($highlightEntries as $highlightEntry) {
344  if ($highlightEntry->getLicenseId() == $licenseId &&
345  ($startPos == - 1 || $startPos > $highlightEntry->getStart())) {
346  $startPos = $highlightEntry->getStart();
347  }
348  }
349  if ($startPos != - 1) {
350  $Page = floor($startPos / $blockSize);
351  }
352  }
353 
354  if (! empty($ShowText)) {
355  echo $ShowText, "<hr>";
356  }
357  $PageMenu = $this->GetFileJumpMenu($inputFile, $Page, $blockSize, $Uri);
358  $PageSize = $blockSize * $Page;
359  if (! empty($PageMenu) and ! $getPageMenuInline) {
360  $output .= "<center>$PageMenu</center><br>\n";
361  }
362 
363  $startAt = $PageSize;
364  $endAt = $PageSize + $blockSize;
365  $relevantHighlightEntries = array();
366  foreach ($highlightEntries as $highlightEntry) {
367  if ($highlightEntry->getStart() < $endAt &&
368  $highlightEntry->getEnd() >= $startAt) {
369  $relevantHighlightEntries[] = $highlightEntry;
370  }
371  }
372 
373  $this->highlightProcessor->sortHighlights($relevantHighlightEntries);
374 
375  $splitPositions = $this->highlightProcessor->calculateSplitPositions(
376  $relevantHighlightEntries);
377 
378  if ($Format == 'hex') {
379  $output .= $this->getHex($inputFile, $PageSize, $this->blockSizeHex,
380  $splitPositions);
381  } else {
382  $output .= $this->getText($inputFile, $PageSize, $Format == 'text' ? 0 : 1,
383  $this->blockSizeText, $splitPositions, $insertBacklink);
384  }
385 
386  if (! empty($PageMenu) and ! $getPageMenuInline) {
387  $output .= "<P /><center>$PageMenu</center><br>\n";
388  }
389 
390  if ($openedFin) {
391  fclose($inputFile);
392  }
393 
394  return $getPageMenuInline ? array($PageMenu, $output) : $output;
395  }
396 
397  /*
398  * Added by vincent implement when view files which not in repository, ask
399  * user if want to reunpack
400  */
401  protected function outputWhenFileNotInRepo($uploadpk, $item)
402  {
403  global $Plugins;
404  $reunpackPlugin = & $Plugins[plugin_find_id("ui_reunpack")];
405  $state = $reunpackPlugin->CheckStatus($uploadpk, "reunpack", "ununpack");
406 
407  /* If this is a POST, then process the request. */
408  $uploadunpack = GetParm('uploadunpack', PARM_INTEGER);
409  $flag = 0;
410  $output = '';
411 
412  if ($state != 0 && $state != 2) {
413  $flag = 1;
414  $text = _("Reunpack job is running: you can see it in");
415  $text1 = _("jobqueue");
416  $output .= "<p> <font color=red>$text <a href='" . Traceback_uri() .
417  "?mod=showjobs'>$text1</a></font></p>";
418  } elseif (! empty($uploadunpack)) {
419  $rc = $reunpackPlugin->AgentAdd($uploadpk);
420  if (empty($rc)) {
421  /* Need to refresh the screen */
422  $this->vars['message'] = _("Unpack added to job queue");
423  $flag = 1;
424  $text = _("Reunpack job is running: you can see it in");
425  $text1 = _("jobqueue");
426  $output .= "<p> <font color=red>$text <a href='" . Traceback_uri() .
427  "?mod=showjobs'>$text1</a></font></p>";
428  } else {
429  $text = _("Unpack of Upload failed");
430  $this->vars['message'] = "$text: $rc";
431  }
432  }
433 
434  $text = _("File contents are not available in the repository.");
435  $output .= "$text\n";
436  $output .= $reunpackPlugin->ShowReunpackView($item, $flag);
437  return $output;
438  }
439 
440  public function Output()
441  {
442  return $this->ShowView(null, "browse");
443  }
444 }
445 
446 $NewPlugin = new ui_view();
447 $NewPlugin->Initialize();
This is the Plugin class. All plugins should:
Definition: FO_Plugin.php:57
Contains the constants and helpers for authentication of user.
Definition: Auth.php:24
Definition: state.hpp:16
getText($inputFile, $startOffset, $Flowed, $outputLength=-1, $splitPositions=null, $insertBacklink=false, $fromRest=false)
Given a file handle, display "strings" of the file.
Definition: ui-view.php:165
ShowView($inputFile=null, $BackMod="browse", $ShowMenu=1, $ShowHeader=1, $ShowText=null, $ViewOnly=false, $DispView=true, $highlightEntries=array(), $insertBacklink=false)
Generate the view contents in HTML and sends it to stdout.
Definition: ui-view.php:266
getHex($inputFile, $startOffset=0, $outputLength=-1, $splitPositions=array())
Given a file handle, display a "hex dump" of the file. Output goes to stdout!
Definition: ui-view.php:203
GetFileJumpMenu($Fin, $CurrPage, $PageSize, $Uri)
Given a file handle and current page, generate the "Next" and "Prev" menu options....
Definition: ui-view.php:98
ShowText($inputFile, $startOffset, $Flowed, $outputLength=-1, $splitPositions=null, $insertBacklink=false)
Given a file handle, display "strings" of the file. Output goes to stdout!
Definition: ui-view.php:154
getView($inputFile=null, $BackMod="browse", $ShowHeader=1, $ShowText=null, $highlightEntries=array(), $insertBacklink=false, $getPageMenuInline=false)
Generate the view contents in HTML.
Definition: ui-view.php:288
RegisterMenus()
Customize submenus.
Definition: ui-view.php:62
Output()
This function is called when user output is requested. This function is responsible for content....
Definition: ui-view.php:440
ShowHex($inputFile, $startOffset=0, $outputLength=-1, $splitPositions=array())
Given a file handle, display a "hex dump" of the file. Output goes to stdout!
Definition: ui-view.php:193
__construct()
base constructor. Most plugins will just use this
Definition: ui-view.php:33
Dir2Browse($Mod, $UploadtreePk, $LinkLast=NULL, $ShowBox=1, $ShowMicro=NULL, $Enumerate=-1, $PreText='', $PostText='', $uploadtree_tablename="uploadtree")
Get an html linked string of a file browse path.
Definition: common-dir.php:263
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.
Traceback_uri()
Get the URI without query to this location.
Definition: common-parm.php:97
const PARM_INTEGER
Definition: common-parm.php:14
Traceback()
Get the URI + query to this location.
Definition: common-parm.php:89
const PARM_STRING
Definition: common-parm.php:18
GetParm($parameterName, $parameterType)
This function will retrieve the variables and check data types.
Definition: common-parm.php:46
Traceback_parm_keep($List)
Create a new URI, keeping only these items.
RepPathItem($Item, $Repo="files")
Given an uploadtree_pk, retrieve the pfile path.
Definition: common-repo.php:91
FUNCTION int max(int permGroup, int permPublic)
Get the maximum group privilege.
Definition: libfossagent.c:295
#define PLUGIN_DB_READ
Plugin requires read permission on DB.
Definition: libfossology.h:37