FOSSology  4.4.0
Open Source License Compliance by Open Source Software
AjaxBrowse.php
1 <?php
2 /*
3  SPDX-FileCopyrightText: © 2014-2015 Siemens AG
4  Author: J.Najjar, S. Weber
5 
6  SPDX-License-Identifier: GPL-2.0-only
7 */
8 
9 namespace Fossology\UI\Ajax;
10 
20 use Symfony\Component\HttpFoundation\JsonResponse;
21 use Symfony\Component\HttpFoundation\Request;
22 use Symfony\Component\HttpFoundation\Response;
23 
25 {
26  const NAME = "browse-processPost";
27 
29  private $uploadDao;
31  private $userDao;
33  private $dbManager;
35  private $dataTablesUtility;
37  private $filterParams;
39  private $userPerm;
41  private $statusTypes;
42 
43  function __construct()
44  {
45  parent::__construct(self::NAME, array(
46  self::REQUIRES_LOGIN => false,
47  self::PERMISSION => Auth::PERM_READ
48  ));
49  global $container;
50  $this->uploadDao = $container->get('dao.upload');
51  $this->userDao = $container->get('dao.user');
52  $this->dbManager = $container->get('db.manager');
53  $this->dataTablesUtility = $container->get('utils.data_tables_utility');
54  }
55 
59  protected function handle(Request $request)
60  {
61  $groupId = Auth::getGroupId();
62  $gup = $this->dbManager->getSingleRow('SELECT group_perm FROM group_user_member WHERE user_fk=$1 AND group_fk=$2',
63  array(Auth::getUserId(), $groupId), __METHOD__ . '.user_perm');
64  if (!$gup) {
65  throw new \Exception('You are assigned to wrong group.');
66  }
67  $this->userPerm = $gup['group_perm'];
68 
69  $uploadId = intval($request->get('uploadId'));
70  if ($uploadId && !$this->uploadDao->isAccessible($uploadId, $groupId)) {
71  throw new \Exception('You cannot access to this upload');
72  }
73 
74  $columnName = $request->get('columnName');
75  $statusId = intval($request->get('statusId'));
76  $value = intval($request->get('value'));
77  $moveUpload = intval($request->get("move"));
78  $beyondUpload = intval($request->get("beyond"));
79  $commentText = $request->get('commentText');
80  $direction = $request->get('direction');
81 
82  if (! empty($columnName) && ! empty($uploadId) && ! empty($value)) {
83  $uploadBrowseProxy = new UploadBrowseProxy($groupId, $this->userPerm, $this->dbManager);
84  $uploadBrowseProxy->updateTable($columnName, $uploadId, $value);
85  } else if (! empty($moveUpload) && ! empty($beyondUpload)) {
86  $uploadBrowseProxy = new UploadBrowseProxy($groupId, $this->userPerm, $this->dbManager);
87  $uploadBrowseProxy->moveUploadBeyond($moveUpload, $beyondUpload);
88  } else if (! empty($uploadId) && ! empty($direction)) {
89  $uploadBrowseProxy = new UploadBrowseProxy($groupId, $this->userPerm, $this->dbManager);
90  $uploadBrowseProxy->moveUploadToInfinity($uploadId, $direction == 'top');
91  } else if (!empty($uploadId) && !empty($commentText) && !empty($statusId)) {
92  $uploadBrowseProxy = new UploadBrowseProxy($groupId, $this->userPerm, $this->dbManager);
93  $uploadBrowseProxy->setStatusAndComment($uploadId, $statusId, $commentText);
94  } else {
95  return $this->respondFolderGetTableData($request);
96  }
97  return new Response('');
98  }
99 
100 
105  protected function respondFolderGetTableData(Request $request)
106  {
107  /* Get list of uploads in this folder */
108  list($result, $iTotalDisplayRecords, $iTotalRecords) = $this->getListOfUploadsOfFolder($request);
109 
110  $uri = Traceback_uri() . "?mod=license";
111  /* Browse-Pfile menu */
112  $menuPfile = menu_find("Browse-Pfile", $menuDepth);
113  /* Browse-Pfile menu without the compare menu item */
114  $menuPfileNoCompare = menu_remove($menuPfile, "Compare");
115 
116  $users = $this->userDao->getUserChoices();
117 
118  $statusTypesAvailable = $this->uploadDao->getStatusTypeMap();
119  if (!$this->userPerm) {
120  unset($statusTypesAvailable[4]);
121  }
122 
123  $output = array();
124  $rowCounter = 0;
125  while ($row = $this->dbManager->fetchArray($result)) {
126  if (empty($row['upload_pk']) || !$this->uploadDao->isAccessible($row['upload_pk'],Auth::getGroupId())) {
127  continue;
128  }
129  $rowCounter++;
130  $output[] = $this->showRow($row, $request, $uri, $menuPfile, $menuPfileNoCompare, $statusTypesAvailable, $users, $rowCounter);
131  }
132  $this->dbManager->freeResult($result);
133  return new JsonResponse(array(
134  'sEcho' => intval($request->get('sEcho')),
135  'aaData' => $output,
136  'iTotalRecords' => $iTotalRecords,
137  'iTotalDisplayRecords' => $iTotalDisplayRecords
138  ));
139  }
140 
141 
153  private function showRow($row,Request $request, $uri, $menuPfile, $menuPfileNoCompare, $statusTypesAvailable, $users, $rowCounter)
154  {
155  $show = $request->get('show');
156  $folder = $request->get('folder');
157 
158  $uploadId = intval($row['upload_pk']);
159  $description = htmlentities($row['upload_desc']);
160 
161  $fileName = $row['ufile_name'];
162  if (empty($fileName)) {
163  $fileName = $row['upload_filename'];
164  }
165 
166  $itemId = Isartifact($row['ufile_mode']) ? DirGetNonArtifact($row['uploadtree_pk']) : $row['uploadtree_pk'];
167  if (strlen($fileName) > 20) {
168  $splitFileName = str_split($fileName, 20);
169  $fileName = "";
170  foreach ($splitFileName as $key => $value) {
171  if (strlen($value) > 3 && $key > 0) {
172  $fileName .= "<br/>".$value;
173  } else {
174  $fileName = $fileName.$value;
175  }
176  }
177  }
178  $nameColumn = "<strong class='btn btn-sm font-weight-bold' style='margin-left:10px;font-size:11pt;'>$fileName</strong>";
179  if (IsContainer($row['ufile_mode'])) {
180  $nameColumn = "<a href='$uri&upload=$uploadId&folder=$folder&item=$itemId&show=$show'>$nameColumn</a>";
181  }
182  $nameColumn .= "<br>";
183  if (!empty($description)) {
184  $nameColumn .= "<i>$description</i><br>";
185  }
186  $Parm = "upload=$uploadId&show=$show&item=" . $row['uploadtree_pk'];
187  if (Iscontainer($row['ufile_mode'])) {
188  $nameAction = MenuRenderer::menuToActiveSelect($menuPfile, $Parm, $uploadId, $folder);
189  } else {
190  $nameAction = MenuRenderer::menuToActiveSelect($menuPfileNoCompare, $Parm, $uploadId, $folder);
191  }
192 
193  $modsUploadMulti = MenuHook::getAgentPluginNames('UploadMulti');
194  if (!empty($modsUploadMulti)) {
195  $nameColumn = '<input type="checkbox" name="uploads[]" class="browse-upload-checkbox" style="width:1.10rem;height:1.10rem;" value="'.$uploadId.'"/>'.$nameColumn;
196  }
197 
198  $dateCol = Convert2BrowserTime(substr($row['upload_ts'], 0, 19));
199  if (!$this->userPerm && 4 == $row['status_fk']) {
200  $currentStatus = $this->statusTypes[4];
201  } else {
202  $statusAction = " onchange =\"changeTableEntry(this, $uploadId,'status_fk' )\" ";
203  $currentStatus = $this->createSelect("Status" . $this->userPerm . "Of_$rowCounter", $statusTypesAvailable, $row['status_fk'], $statusAction);
204  }
205  if ($this->userPerm) {
206  $action = " onchange =\"changeTableEntry(this, $uploadId, 'assignee')\"";
207  $currentAssignee = $this->createSelectUsers("AssignedTo_$rowCounter", $users, $row['assignee'], $action );
208  } else {
209  $currentAssignee = array_key_exists($row['assignee'], $users) ? $users[$row['assignee']] : _('Unassigned');
210  }
211  $rejectableUploadId = ($this->userPerm || $row['status_fk'] < 4) ? $uploadId : 0;
212  $tripleComment = array($rejectableUploadId, $row['status_fk'], htmlspecialchars($row['status_comment']));
213 
214  $sql = "SELECT rf_pk, rf_shortname FROM license_ref lf JOIN upload_clearing_license ucl"
215  . " ON lf.rf_pk=ucl.rf_fk WHERE upload_fk=$1 AND ucl.group_fk=$2";
216  $stmt = __METHOD__.'.collectMainLicenses';
217  $mainParams = array($uploadId, Auth::getGroupId());
218  $lic = $this->dbManager->getRows($sql, $mainParams, $stmt);
219  $mainLicenses = array();
220  foreach ($lic as $mainLic) {
221  $mainLicenses[] = '<a onclick="javascript:window.open(\''.Traceback_uri()
222  ."?mod=popup-license&rf=$mainLic[rf_pk]','License text','width=600,height=400,toolbar=no,scrollbars=yes,resizable=yes');"
223  .'" href="javascript:;">'.$mainLic['rf_shortname'].'</a>'
224  ."<img onclick=\"removeMainLicense($uploadId,$mainLic[rf_pk]);\" class=\"delete\" src=\"images/space_16.png\" alt=\"\"/></img>";
225  }
226 
227  return array($nameColumn, $nameAction, $currentStatus, $tripleComment, implode(', ', $mainLicenses), $dateCol, $currentAssignee);
228  }
229 
236  private function createSelectUsers($selectElementName, $databaseMap, $selectedValue, $action = "")
237  {
238  if (array_key_exists($_SESSION['UserId'], $databaseMap)) {
239  $databaseMap[$_SESSION['UserId']] = _('-- Me --');
240  }
241  $databaseMap[1] = _('Unassigned');
242  return $this->createSelect($selectElementName,$databaseMap, $selectedValue,$action);
243  }
244 
245 
246  private function createSelect($id,$options,$select='',$action='')
247  {
248  $html = "<select class='form-control-sm' style=\"max-width:250px;\" name=\"$id\" id=\"$id\" $action class=\"ui-render-select2\">";
249  foreach ($options as $key=>$disp) {
250  $html .= '<option value="'.$key.'"';
251  if ($key == $select) {
252  $html .= ' selected';
253  }
254  $html .= ">$disp</option>";
255  }
256  $html .= '</select>';
257  return $html;
258  }
259 
260 
265  private function getListOfUploadsOfFolder(Request $request)
266  {
267  $uploadBrowseProxy = new UploadBrowseProxy(Auth::getGroupId(), $this->userPerm, $this->dbManager);
268  $params = array($request->get('folder'));
269  $partQuery = $uploadBrowseProxy->getFolderPartialQuery($params);
270 
271  $iTotalRecordsRow = $this->dbManager->getSingleRow("SELECT count(*) FROM $partQuery", $params, __METHOD__ . "count.all");
272  $iTotalRecords = $iTotalRecordsRow['count'];
273 
274  $this->filterParams = $params;
275  $filter = $this->getSearchString($request->get('sSearch'));
276  $filter .= $this->getIntegerFilter(intval($request->get('assigneeSelected')), 'assignee');
277  $filter .= $this->getIntegerFilter(intval($request->get('statusSelected')), 'status_fk');
278 
279  $iTotalDisplayRecordsRow = $this->dbManager->getSingleRow("SELECT count(*) FROM $partQuery $filter",
280  $this->filterParams, __METHOD__ . ".count.". $filter);
281  $iTotalDisplayRecords = $iTotalDisplayRecordsRow['count'];
282 
283  $orderString = $this->getOrderString();
284  $stmt = __METHOD__ . "getFolderContents" . $orderString . $filter;
285 
286  $statementString = "SELECT upload.*,upload_clearing.*,uploadtree.ufile_name,uploadtree.ufile_mode,uploadtree.uploadtree_pk"
287  . " FROM $partQuery $filter $orderString";
288  $rangedFilterParams = $this->filterParams;
289  $rangedFilterParams[] = intval($request->get('iDisplayStart'));
290  $statementString .= ' OFFSET $' . count($rangedFilterParams);
291  $rangedFilterParams[] = intval($request->get('iDisplayLength'));
292  $statementString .= ' LIMIT $' . count($rangedFilterParams);
293 
294  $this->dbManager->prepare($stmt, $statementString);
295  $result = $this->dbManager->execute($stmt, $rangedFilterParams);
296 
297  return array($result, $iTotalDisplayRecords, $iTotalRecords);
298  }
299 
300  private function getOrderString()
301  {
302  $columnNamesInDatabase = array('upload_filename', 'upload_clearing.status_fk', 'UNUSED', 'UNUSED', 'upload_clearing.assignee', 'upload_ts', 'upload_clearing.priority');
303 
304  return $this->dataTablesUtility->getSortingString($_GET, $columnNamesInDatabase);
305  }
306 
307  private function getSearchString($searchPattern)
308  {
309  if (empty($searchPattern)) {
310  return '';
311  }
312  $this->filterParams[] = "%$searchPattern%";
313  return ' AND upload_filename ilike $' . count($this->filterParams) . ' ';
314  }
315 
320  private function getIntegerFilter($var, $columnName)
321  {
322  if (empty($var)) {
323  return '';
324  }
325  $this->filterParams[] = $var;
326  return " AND $columnName=$" . count($this->filterParams) . ' ';
327  }
328 }
329 
330 register_plugin(new AjaxBrowse());
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
static getAgentPluginNames($hook='Agents')
Definition: MenuHook.php:16
static menuToActiveSelect($menu, &$parm, $uploadId="", $folderId=0)
showRow($row, Request $request, $uri, $menuPfile, $menuPfileNoCompare, $statusTypesAvailable, $users, $rowCounter)
Definition: AjaxBrowse.php:153
handle(Request $request)
Display the loaded menu and plugins.
Definition: AjaxBrowse.php:59
createSelectUsers($selectElementName, $databaseMap, $selectedValue, $action="")
Definition: AjaxBrowse.php:236
getListOfUploadsOfFolder(Request $request)
Definition: AjaxBrowse.php:265
respondFolderGetTableData(Request $request)
Definition: AjaxBrowse.php:105
getIntegerFilter($var, $columnName)
Definition: AjaxBrowse.php:320
Isartifact($mode)
Definition: common-dir.php:29
DirGetNonArtifact($UploadtreePk, $uploadtree_tablename='uploadtree')
Given an artifact directory (uploadtree_pk), return the first non-artifact directory (uploadtree_pk).
Definition: common-dir.php:158
Iscontainer($mode)
Definition: common-dir.php:38
menu_remove($Menu, $RmName)
Remove a menu object (based on an object name) from a menu list.
menu_find($Name, &$MaxDepth, $Menu=NULL)
Given a top-level menu name, find the list of sub-menus below it and max depth of menu.
Traceback_uri()
Get the URI without query to this location.
Definition: common-parm.php:97
Convert2BrowserTime($server_time)
Convert the server time to browser time.
Definition: common-ui.php:312
fo_dbManager * dbManager
fo_dbManager object
Definition: process.c:16