FOSSology  4.4.0
Open Source License Compliance by Open Source Software
AdviceLicense.php
1 <?php
2 /*
3  SPDX-FileCopyrightText: © 2014-2015 Siemens AG
4 
5  SPDX-License-Identifier: GPL-2.0-only
6 */
7 
8 namespace Fossology\UI\Page;
9 
10 use Composer\Spdx\SpdxLicenses;
17 use Symfony\Component\HttpFoundation\Request;
18 use Symfony\Component\HttpFoundation\Response;
19 
21 {
22  const NAME = "advice_license";
23 
24  function __construct()
25  {
26  parent::__construct(self::NAME, array(
27  self::TITLE => "Candidate Licenses",
28  self::MENU_LIST => "Organize::Licenses",
29  self::REQUIRES_LOGIN => true
30  ));
31  }
32 
38  protected function handle(Request $request)
39  {
40  $rf = intval($request->get('rf'));
41  $userId = Auth::getUserId();
42  $groupId = Auth::getGroupId();
44  $userDao = $this->getObject('dao.user');
45  $username = $userDao->getUserByPk($userId);
46  $canEdit = $userDao->isAdvisorOrAdmin($userId, $groupId);
47  if (empty($rf) || ! $canEdit) {
48  $vars = array(
49  'aaData' => json_encode($this->getArrayArrayData($groupId, $canEdit)),
50  'canEdit' => $canEdit
51  );
52  return $this->render('advice_license.html.twig', $this->mergeWithDefault($vars));
53  }
54 
55  $vars = $this->getDataRow($groupId, $rf);
56  if ($vars === false) {
57  return $this->flushContent( _('invalid license candidate'));
58  }
59 
60  if ($request->get('save')) {
61  try {
62  $vars = $this->saveInput($request, $vars, $userId);
63  $vars['message'] = 'Successfully updated.';
64  } catch (\Exception $e) {
65  $vars = array('rf_spdx_id' => $request->get('spdx_id'),
66  'rf_shortname' => $request->get('shortname'),
67  'rf_fullname' => $request->get('fullname'),
68  'rf_text' => $request->get('rf_text'),
69  'rf_url' => $request->get('url'),
70  'rf_notes' => $request->get('note'),
71  'rf_risk' => intval($request->get('risk'))
72  );
73  $vars['message'] = $e->getMessage();
74  }
75  }
76 
77  return $this->render('advice_license-edit.html.twig', $this->mergeWithDefault($vars));
78  }
79 
80 
81  private function getArrayArrayData($groupId,$canEdit)
82  {
83  $sql = "SELECT rf_pk,rf_spdx_id,rf_shortname,rf_fullname,rf_text,rf_url,rf_notes,marydone,user_name FROM license_candidate lc INNER JOIN users ON lc.rf_user_fk_created=users.user_pk WHERE lc.group_fk=$1";
85  $dbManager = $this->getObject('db.manager');
86  $dbManager->prepare($stmt = __METHOD__, $sql);
87  $res = $dbManager->execute($stmt, array($groupId));
88  $aaData = array();
89  while ($row = $dbManager->fetchArray($res)) {
90  $aData = array(htmlentities($row['rf_spdx_id']),
91  htmlentities($row['rf_shortname']), htmlentities($row['rf_fullname']),
92  '<div style="overflow-y:scroll;max-height:150px;margin:0;">' . nl2br(htmlentities($row['rf_text'])) . '</div>',
93  htmlentities($row['rf_url']),
94  htmlentities($row['user_name']),
95  $this->bool2checkbox($dbManager->booleanFromDb($row['marydone']))
96  );
97  if ($canEdit) {
98  $link = Traceback_uri() . '?mod=' . Traceback_parm() . '&rf=' . $row['rf_pk'];
99  $edit = '<a href="' . $link . '"><img border="0" src="images/button_edit.png"></a>';
100  array_unshift($aData,$edit);
101  }
102  $aaData[] = $aData;
103  }
104  $dbManager->freeResult($res);
105  return $aaData;
106  }
107 
108 
109  private function getDataRow($groupId, $licId)
110  {
111  if ($licId == -1) {
112  return array('rf_pk' => -1, 'rf_shortname' => '');
113  }
114  $sql = "SELECT rf_pk,rf_spdx_id,rf_shortname,rf_fullname,rf_text,rf_url," .
115  "rf_notes,rf_lastmodified,rf_user_fk_modified,rf_user_fk_created," .
116  "rf_creationdate,marydone,rf_risk FROM license_candidate " .
117  "WHERE group_fk=$1 AND rf_pk=$2";
118  /* @var $dbManager DbManager */
119  $dbManager = $this->getObject('db.manager');
120  $row = $dbManager->getSingleRow($sql, array($groupId, $licId), __METHOD__);
121  if (false !== $row) {
122  $row['marydone'] = $dbManager->booleanFromDb($row['marydone']);
123  $row['rf_lastmodified'] = Convert2BrowserTime($row['rf_lastmodified']);
124  $row['rf_creationdate'] = Convert2BrowserTime($row['rf_creationdate']);
125  $userDao = $this->getObject('dao.user');
126  $username = $userDao->getUserByPk($row['rf_user_fk_created']);
127  $row['rf_user_fk_created'] = $username['user_name'];
128  $username = $userDao->getUserByPk($row['rf_user_fk_modified']);
129  $row['rf_user_fk_modified'] = $username['user_name'];
130  }
131  return $row;
132  }
133 
134 
135  private function bool2checkbox($bool)
136  {
137  $check = $bool ? ' checked="checked"' : '';
138  return '<input type="checkbox"' . $check . ' disabled="disabled"/>';
139  }
140 
148  private function saveInput(Request $request, $oldRow, $userId)
149  {
150  $spdxLicenses = new SpdxLicenses();
151 
152  $spdxId = $request->get('spdx_id');
153  $shortname = $request->get('shortname');
154  $fullname = $request->get('fullname');
155  $rfText = $request->get('rf_text');
156  $url = $request->get('url');
157  $marydone = $request->get('marydone');
158  $note = $request->get('note');
159  $riskLvl = intval($request->get('risk'));
160  $lastmodified = date(DATE_ATOM);
161  $userIdcreated = $userId;
162  $userIdmodified = $userId;
163 
164  if (empty($shortname) || empty($fullname) || empty($rfText)) {
165  throw new \Exception('missing shortname (or) fullname (or) reference text');
166  }
167 
168  /* @var $licenseDao LicenseDao */
169  $licenseDao = $this->getObject('dao.license');
170  $ok = ($oldRow['rf_shortname'] == $shortname);
171  if (!$ok) {
172  $ok = $licenseDao->isNewLicense($shortname, Auth::getGroupId());
173  }
174  if (!$ok) {
175  throw new \Exception('shortname already in use');
176  }
177  if ($oldRow['rf_pk'] == -1) {
178  $oldRow['rf_pk'] = $licenseDao->insertUploadLicense($shortname, $rfText, Auth::getGroupId(), $userId);
179  }
180 
181  if (! empty($spdxId) &&
182  strstr(strtolower($spdxId), strtolower(LicenseRef::SPDXREF_PREFIX)) === false) {
183  if (! $spdxLicenses->validate($spdxId)) {
184  $spdxId = LicenseRef::convertToSpdxId($spdxId, null);
185  }
186  } elseif (empty($spdxId)) {
187  $spdxId = null;
188  }
189  if (! empty($spdxId)) {
190  $spdxId = LicenseRef::replaceSpaces($spdxId);
191  }
192 
193  $licenseDao->updateCandidate($oldRow['rf_pk'], $shortname, $fullname,
194  $rfText, $url, $note, $lastmodified, $userIdmodified, !empty($marydone),
195  $riskLvl, $spdxId);
196  return $this->getDataRow(Auth::getGroupId(), $oldRow['rf_pk']);
197  }
198 }
199 
200 register_plugin(new AdviceLicense());
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 convertToSpdxId($shortname, $spdxId)
Given a license's shortname and spdx id, give out spdx id to use in reports.
Definition: LicenseRef.php:106
static replaceSpaces($licenseName)
Definition: LicenseRef.php:132
render($templateName, $vars=null, $headers=null)
saveInput(Request $request, $oldRow, $userId)
Traceback_uri()
Get the URI without query to this location.
Definition: common-parm.php:97
Traceback_parm($ShowMod=1)
Get the URI query to this location.
Convert2BrowserTime($server_time)
Convert the server time to browser time.
Definition: common-ui.php:312