FOSSology  4.4.0
Open Source License Compliance by Open Source Software
LicenseStdCommentDao.php
Go to the documentation of this file.
1 <?php
2 /*
3  SPDX-FileCopyrightText: © 2019 Siemens AG
4  Author: Gaurav Mishra <mishra.gaurav@siemens.com>
5 
6  SPDX-License-Identifier: GPL-2.0-only
7 */
12 namespace Fossology\Lib\Dao;
13 
17 
23 {
26  private $dbManager;
27 
28  function __construct(DbManager $dbManager)
29  {
30  $this->dbManager = $dbManager;
31  }
32 
40  public function getAllComments($skipNotSet = false)
41  {
42  $where = "";
43  if ($skipNotSet) {
44  $where = "WHERE name <> 'not-set' AND is_enabled = TRUE";
45  }
46  $sql = "SELECT lsc_pk, name, comment, is_enabled " .
47  "FROM license_std_comment $where " .
48  "ORDER BY lsc_pk ASC;";
49  return $this->dbManager->getRows($sql);
50  }
51 
59  function updateComment($commentPk, $newName, $newComment)
60  {
61  if (!Auth::isAdmin()) {
62  // Only admins can update the comments.
63  return false;
64  }
65  $this->isCommentIdValid($commentPk);
66 
67  $userFk = Auth::getUserId();
68 
69  $sql = "UPDATE license_std_comment " .
70  "SET name = $2, comment = $3, updated = NOW(), user_fk = $4 " .
71  "WHERE lsc_pk = $1 " .
72  "RETURNING 1 AS updated;";
73  $row = $this->dbManager->getSingleRow($sql,
74  [$commentPk, $newName,
75  StringOperation::replaceUnicodeControlChar($newComment), $userFk]);
76  return $row['updated'] == 1;
77  }
78 
87  function insertComment($name, $comment)
88  {
89  if (! Auth::isAdmin()) {
90  // Only admins can add comments.
91  return -1;
92  }
93 
94  $name = trim($name);
95  $comment = trim($comment);
96 
97  if (empty($name) || empty($comment)) {
98  // Cannot insert empty fields.
99  return -1;
100  }
101 
102  $userFk = Auth::getUserId();
103 
104  $params = [
105  'name' => $name,
106  'comment' => StringOperation::replaceUnicodeControlChar($comment),
107  'user_fk' => $userFk
108  ];
109  $statement = __METHOD__ . ".insertNewLicStdComment";
110  $returning = "lsc_pk";
111  $returnVal = -1;
112  try {
113  $returnVal = $this->dbManager->insertTableRow("license_std_comment",
114  $params, $statement, $returning);
115  } catch (\Exception $e) {
116  $returnVal = -2;
117  }
118  return $returnVal;
119  }
120 
131  function updateCommentFromArray($commentArray)
132  {
133  if (!Auth::isAdmin()) {
134  // Only admins can update the comments.
135  return false;
136  }
137 
138  $userFk = Auth::getUserId();
139  $updated = 0;
140 
141  foreach ($commentArray as $commentPk => $comment) {
142  if (count($comment) < 1 ||
143  (! array_key_exists("name", $comment) &&
144  ! array_key_exists("comment", $comment))) {
145  throw new \UnexpectedValueException(
146  "At least name or comment is " . "required for entry " . $commentPk);
147  }
148  $this->isCommentIdValid($commentPk);
149  $statement = __METHOD__;
150  $params = [$commentPk, $userFk];
151  $updateStatement = [];
152  if (array_key_exists("name", $comment)) {
153  $params[] = $comment["name"];
154  $updateStatement[] = "name = $" . count($params);
155  $statement .= ".name";
156  }
157  if (array_key_exists("comment", $comment)) {
158  $params[] = StringOperation::replaceUnicodeControlChar($comment["comment"]);
159  $updateStatement[] = "comment = $" . count($params);
160  $statement .= ".comment";
161  }
162  $sql = "UPDATE license_std_comment " .
163  "SET updated = NOW(), user_fk = $2, " . join(",", $updateStatement) .
164  " WHERE lsc_pk = $1 " .
165  "RETURNING 1 AS updated;";
166  $retVal = $this->dbManager->getSingleRow($sql, $params, $statement);
167  $updated += intval($retVal);
168  }
169  return $updated;
170  }
171 
177  function getComment($commentPk)
178  {
179  $this->isCommentIdValid($commentPk);
180  $sql = "SELECT comment FROM license_std_comment " . "WHERE lsc_pk = $1;";
181  $statement = __METHOD__ . ".getComment";
182 
183  $comment = $this->dbManager->getSingleRow($sql, [$commentPk], $statement);
184  $comment = $comment['comment'];
185  if (strcasecmp($comment, "null") === 0) {
186  return null;
187  }
188  return $comment;
189  }
190 
197  function toggleComment($commentPk)
198  {
199  if (! Auth::isAdmin()) {
200  // Only admins can update the comments.
201  return false;
202  }
203  $this->isCommentIdValid($commentPk);
204 
205  $userFk = Auth::getUserId();
206 
207  $sql = "UPDATE license_std_comment " .
208  "SET is_enabled = NOT is_enabled, user_fk = $2 " .
209  "WHERE lsc_pk = $1;";
210 
211  $this->dbManager->getSingleRow($sql, [$commentPk, $userFk]);
212  return true;
213  }
214 
221  private function isCommentIdValid($commentPk)
222  {
223  if (! is_int($commentPk)) {
224  throw new \UnexpectedValueException("Inavlid comment id");
225  }
226  $sql = "SELECT count(*) AS cnt FROM license_std_comment " .
227  "WHERE lsc_pk = $1;";
228 
229  $commentCount = $this->dbManager->getSingleRow($sql, [$commentPk]);
230  if ($commentCount['cnt'] < 1) {
231  // Invalid comment id
232  throw new \UnexpectedValueException("Inavlid comment id");
233  }
234  }
235 }
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 isAdmin()
Check if user is admin.
Definition: Auth.php:92
updateComment($commentPk, $newName, $newComment)
updateCommentFromArray($commentArray)
Update the comments based only on the values provided.
Fossology exception.
Definition: Exception.php:15
static replaceUnicodeControlChar($input, $replace="")
char * trim(char *ptext)
Trimming whitespace.
Definition: fossconfig.c:690
fo_dbManager * dbManager
fo_dbManager object
Definition: process.c:16