FOSSology  4.4.0
Open Source License Compliance by Open Source Software
Xpview.php
1 <?php
2 /*
3  SPDX-FileCopyrightText: © 2014-2015 Siemens AG
4  Author: Johannes Najjar
5 
6  SPDX-License-Identifier: GPL-2.0-only
7 */
8 
14 
24 use Symfony\Component\HttpFoundation\RedirectResponse;
25 use Symfony\Component\HttpFoundation\Request;
26 use Symfony\Component\HttpFoundation\Response;
27 use ui_view;
28 
33 class Xpview extends DefaultPlugin
34 {
36  protected $optionName;
38  protected $ajaxAction;
40  protected $modBack;
42  protected $uploadDao;
44  protected $copyrightDao;
46  protected $agentDao;
48  protected $highlightRenderer;
50  protected $decisionTypes;
52  protected $decisionTableName;
54  protected $tableName;
56  protected $agentName;
58  protected $highlightTypeToStringMap;
60  protected $typeToHighlightTypeMap;
62  protected $skipOption;
64  protected $xptext;
65 
66  function __construct($name, $params)
67  {
68  $mergedParams = array_merge($params, array(self::DEPENDENCIES=>array("browse", "view"),
69  self::PERMISSION=> Auth::PERM_READ));
70 
71  parent::__construct($name, $mergedParams);
72  $this->agentName = $this->tableName;
73 
74  $this->uploadDao = $this->getObject('dao.upload');
75  $this->copyrightDao = $this->getObject('dao.copyright');
76  $this->agentDao = $this->getObject('dao.agent');
77  $this->highlightRenderer = $this->getObject('view.highlight_renderer');
78  $this->decisionTypes = $this->getObject('decision.types');
79  }
80 
85  protected function handle(Request $request)
86  {
87  $vars = array();
88  $uploadId = intval($request->get('upload'));
89  $uploadTreeId = intval($request->get('item'));
90  if (empty($uploadTreeId) || empty($uploadId)) {
91  $text = _("Empty Input");
92  $vars['message']= "<h2>$text</h2>";
93  return $this->responseBad($vars);
94  }
95 
96  if (!$this->uploadDao->isAccessible($uploadId, Auth::getGroupId())) {
97  $text = _("Permission Denied");
98  $vars['message']= "<h2>$text</h2>";
99  return $this->responseBad();
100  }
101 
102  $uploadTreeTableName = $this->uploadDao->getUploadtreeTableName($uploadId);
103  $uploadEntry = $this->uploadDao->getUploadEntry($uploadTreeId, $uploadTreeTableName);
104  if (Isdir($uploadEntry['ufile_mode']) || Iscontainer($uploadEntry['ufile_mode'])) {
105  $parent = $this->uploadDao->getUploadParent($uploadEntry['upload_fk']);
106  if (!isset($parent)) {
107  return $this->responseBad();
108  }
109 
110  $uploadTree = $this->uploadDao->getNextItem($uploadEntry['upload_fk'], $parent);
111  if ($uploadTree === UploadDao::NOT_FOUND) {
112  return $this->responseBad();
113  }
114  $uploadTreeId = $uploadTree->getId();
115 
116  return new RedirectResponse(Traceback_uri() . '?mod=' . $this->getName() . Traceback_parm_keep(array('show','upload')) . "&item=$uploadTreeId");
117  }
118  if (empty($uploadTreeId)) {
119  return $this->responseBad('No item selected.');
120  }
121 
122  $copyrightDecisionMap = $this->decisionTypes->getMap();
123  $vars['micromenu'] = Dir2Browse($this->modBack, $uploadTreeId, NULL, $showBox = 0, "View", -1, '', '', $uploadTreeTableName);
124 
125  $lastItem = GetParm("lastItem", PARM_INTEGER);
126  $changed= GetParm("changedSomething", PARM_STRING);
127  $userId = Auth::getUserId();
128  if (!empty($lastItem) && $changed =="true") {
129  $lastUploadEntry = $this->uploadDao->getUploadEntry($lastItem, $uploadTreeTableName);
130  $clearingType = GetParm('clearingTypes', PARM_INTEGER);
131  $description = trim(GetParm('description', PARM_STRING));
132  $textFinding = trim(GetParm('textFinding', PARM_STRING));
133  $comment = trim(GetParm('comment', PARM_STRING));
134  $decision_pk = GetParm('decision_pk', PARM_INTEGER);
135  if (empty($clearingType) || empty($textFinding)) {
136  if (empty($clearingType)) {
137  $text = _("The clearing type cannot be empty. " .
138  "Please choose a value and submit again.");
139  } else {
140  $text = _("The text finding cannot be empty. " .
141  "Please enter a text and submit again.");
142  }
143  $vars['message']= "<strong>$text</strong>";
144  } else {
145  $this->copyrightDao->saveDecision($this->decisionTableName,
146  $lastUploadEntry['pfile_fk'], $userId , $clearingType, $description,
147  $textFinding, $comment, $decision_pk);
148  }
149  }
150 
151  $scanJobProxy = new ScanJobProxy($this->agentDao, $uploadId);
152  if ($this->agentName == "copyright") {
153  $scanJobProxy->createAgentStatus(array($this->agentName, 'reso'));
154  } else {
155  $scanJobProxy->createAgentStatus(array($this->agentName));
156  }
157  $selectedScanners = $scanJobProxy->getLatestSuccessfulAgentIds();
158  $highlights = array();
159  if (array_key_exists('reso', $selectedScanners)) {
160  $latestXpAgentId[] = $selectedScanners['reso'];
161  }
162  if (array_key_exists($this->agentName, $selectedScanners)) {
163  $latestXpAgentId[] = $selectedScanners[$this->agentName];
164  $highlights = $this->copyrightDao->getHighlights($uploadTreeId, $this->tableName, $latestXpAgentId, $this->typeToHighlightTypeMap);
165  }
166 
167  if (count($highlights) < 1) {
168  $vars['message'] = _("No ").$this->tableName ._(" data is available for this file.");
169  }
170 
171  /* @var $view ui_view */
172  $view = plugin_find("view");
173  $theView = $view->getView(null, null, $showHeader=0, "", $highlights, false, true);
174  list($pageMenu, $textView) = $theView;
175 
176  $decisions = $this->copyrightDao->getDecisions($this->decisionTableName, $uploadEntry['pfile_fk']);
177 
178  $vars['agentName'] = $this->agentName;
179  $vars['decisions'] = $decisions;
180  $vars['decisionsTable'] = $this->decisionTableName;
181  $vars['itemId'] = $uploadTreeId;
182  $vars['uploadId'] = $uploadId;
183  $vars['pfile'] = $uploadEntry['pfile_fk'];
184  $vars['pageMenu'] = $pageMenu;
185  $vars['textView'] = $textView;
186  $vars['legendBox'] = $this->legendBox();
187  $vars['uri'] = Traceback_uri() . "?mod=" . $this->Name;
188  $vars['optionName'] = $this->optionName;
189  $vars['formName'] = "CopyRightForm";
190  $vars['ajaxAction'] = $this->ajaxAction;
191  $vars['skipOption'] =$this->skipOption;
192  $vars['clearingTypes'] = $copyrightDecisionMap;
193  $vars['xptext'] = $this->xptext;
194 
195  $agentId = strval($request->get("agent"));
196  $vars = array_merge($vars,$this->additionalVars($uploadId, $uploadTreeId, $agentId));
197  return $this->render('ui-cp-view.html.twig',$this->mergeWithDefault($vars));
198  }
199 
208  protected function additionalVars($uploadId, $uploadTreeId, $agentId)
209  {
210  return array();
211  }
212 
217  protected function mergeWithDefault($vars)
218  {
219  $allVars = array_merge($this->getDefaultVars(), $vars);
220  $allVars['styles'] .= "<link rel='stylesheet' href='css/highlights.css'>\n";
221  return $allVars;
222  }
223 
229  private function responseBad($vars=array())
230  {
231  $vars['content'] = 'This upload contains no files!<br><a href="' . Traceback_uri() . '?mod=browse">Go back to browse view</a>';
232  return $this->render("include/base.html.twig",$vars);
233  }
234 
239  function legendBox()
240  {
241  $output = _("file text");
242  foreach ($this->highlightTypeToStringMap as $colorKey => $txt) {
243  $output .= '<br/>' . $this->highlightRenderer->createStartSpan($colorKey, $txt) . $txt . '</span>';
244  }
245  return $output;
246  }
247 
252  function RegisterMenus()
253  {
254  $tooltipText = _("Copyright/Email/Url/Author");
255  menu_insert("Browse-Pfile::Copyright/Email/Url", 0, 'copyright-view', $tooltipText);
256 
257  $itemId = GetParm("item", PARM_INTEGER);
258  $textFormat = $this->microMenu->getFormatParameter($itemId);
259  $pageNumber = GetParm("page", PARM_INTEGER);
260  $this->microMenu->addFormatMenuEntries($textFormat, $pageNumber);
261 
262  // For all other menus, permit coming back here.
263  $uploadId = GetParm("upload", PARM_INTEGER);
264  if (!empty($itemId) && !empty($uploadId)) {
265  $menuText = "Copyright/Email/Url/Author";
266  $menuPosition = 57;
267  $tooltipText = _("View Copyright/Email/Url/Author info");
268  $URI = $this->getName() . Traceback_parm_keep(array("show", "format", "page", "upload", "item"));
269  $this->microMenu->insert(MicroMenu::TARGET_DEFAULT, $menuText, $menuPosition, $this->getName(), $URI, $tooltipText);
270  }
271  $licId = GetParm("lic", PARM_INTEGER);
272  if (!empty($licId)) {
273  $this->NoMenu = 1;
274  }
275  }
276 }
responseBad($vars=array())
Call on bad uploads.
Definition: Xpview.php:229
legendBox()
Create legend box.
Definition: Xpview.php:239
RegisterMenus()
Customize submenus.
Definition: Xpview.php:252
additionalVars($uploadId, $uploadTreeId, $agentId)
Get additional variables for a give item.
Definition: Xpview.php:208
Contains the constants and helpers for authentication of user.
Definition: Auth.php:24
static getUserId()
Get the current user's id.
Definition: Auth.php:68
static getGroupId()
Get the current user's group id.
Definition: Auth.php:80
render($templateName, $vars=null, $headers=null)
Isdir($mode)
Definition: common-dir.php:20
Iscontainer($mode)
Definition: common-dir.php:38
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
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.
plugin_find($pluginName)
Given the official name of a plugin, return the $Plugins object.
char * trim(char *ptext)
Trimming whitespace.
Definition: fossconfig.c:690
Namespace for Copyright agent's UI components.