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 FROM license_candidate WHERE 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  $this->bool2checkbox($dbManager->booleanFromDb($row['marydone']))
95  );
96  if ($canEdit) {
97  $link = Traceback_uri() . '?mod=' . Traceback_parm() . '&rf=' . $row['rf_pk'];
98  $edit = '<a href="' . $link . '"><img border="0" src="images/button_edit.png"></a>';
99  array_unshift($aData,$edit);
100  }
101  $aaData[] = $aData;
102  }
103  $dbManager->freeResult($res);
104  return $aaData;
105  }
106 
107 
108  private function getDataRow($groupId, $licId)
109  {
110  if ($licId == -1) {
111  return array('rf_pk' => -1, 'rf_shortname' => '');
112  }
113  $sql = "SELECT rf_pk,rf_spdx_id,rf_shortname,rf_fullname,rf_text,rf_url," .
114  "rf_notes,rf_lastmodified,rf_user_fk_modified,rf_user_fk_created," .
115  "rf_creationdate,marydone,rf_risk FROM license_candidate " .
116  "WHERE group_fk=$1 AND rf_pk=$2";
117  /* @var $dbManager DbManager */
118  $dbManager = $this->getObject('db.manager');
119  $row = $dbManager->getSingleRow($sql, array($groupId, $licId), __METHOD__);
120  if (false !== $row) {
121  $row['marydone'] = $dbManager->booleanFromDb($row['marydone']);
122  $row['rf_lastmodified'] = Convert2BrowserTime($row['rf_lastmodified']);
123  $row['rf_creationdate'] = Convert2BrowserTime($row['rf_creationdate']);
124  $userDao = $this->getObject('dao.user');
125  $username = $userDao->getUserByPk($row['rf_user_fk_created']);
126  $row['rf_user_fk_created'] = $username['user_name'];
127  $username = $userDao->getUserByPk($row['rf_user_fk_modified']);
128  $row['rf_user_fk_modified'] = $username['user_name'];
129  }
130  return $row;
131  }
132 
133 
134  private function bool2checkbox($bool)
135  {
136  $check = $bool ? ' checked="checked"' : '';
137  return '<input type="checkbox"' . $check . ' disabled="disabled"/>';
138  }
139 
147  private function saveInput(Request $request, $oldRow, $userId)
148  {
149  $spdxLicenses = new SpdxLicenses();
150 
151  $spdxId = $request->get('spdx_id');
152  $shortname = $request->get('shortname');
153  $fullname = $request->get('fullname');
154  $rfText = $request->get('rf_text');
155  $url = $request->get('url');
156  $marydone = $request->get('marydone');
157  $note = $request->get('note');
158  $riskLvl = intval($request->get('risk'));
159  $lastmodified = date(DATE_ATOM);
160  $userIdcreated = $userId;
161  $userIdmodified = $userId;
162 
163  if (empty($shortname) || empty($fullname) || empty($rfText)) {
164  throw new \Exception('missing shortname (or) fullname (or) reference text');
165  }
166 
167  /* @var $licenseDao LicenseDao */
168  $licenseDao = $this->getObject('dao.license');
169  $ok = ($oldRow['rf_shortname'] == $shortname);
170  if (!$ok) {
171  $ok = $licenseDao->isNewLicense($shortname, Auth::getGroupId());
172  }
173  if (!$ok) {
174  throw new \Exception('shortname already in use');
175  }
176  if ($oldRow['rf_pk'] == -1) {
177  $oldRow['rf_pk'] = $licenseDao->insertUploadLicense($shortname, $rfText, Auth::getGroupId(), $userId);
178  }
179 
180  if (! empty($spdxId) &&
181  strstr(strtolower($spdxId), strtolower(LicenseRef::SPDXREF_PREFIX)) === false) {
182  if (! $spdxLicenses->validate($spdxId)) {
183  $spdxId = LicenseRef::convertToSpdxId($spdxId, null);
184  }
185  } elseif (empty($spdxId)) {
186  $spdxId = null;
187  }
188  if (! empty($spdxId)) {
189  $spdxId = LicenseRef::replaceSpaces($spdxId);
190  }
191 
192  $licenseDao->updateCandidate($oldRow['rf_pk'], $shortname, $fullname,
193  $rfText, $url, $note, $lastmodified, $userIdmodified, !empty($marydone),
194  $riskLvl, $spdxId);
195  return $this->getDataRow(Auth::getGroupId(), $oldRow['rf_pk']);
196  }
197 }
198 
199 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