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