14 use Symfony\Component\HttpFoundation\JsonResponse;
15 use Symfony\Component\HttpFoundation\Request;
19 const NAME =
"change-license-bulk";
27 function __construct()
29 parent::__construct(self::NAME, array(
30 self::TITLE => _(
"Private: schedule a bulk scan from post"),
35 $this->licenseDao = $this->
getObject(
'dao.license');
36 $this->uploadDao = $this->
getObject(
'dao.upload');
43 public function handle(Request $request)
45 $uploadTreeId = $request->get(
'uploadTreeId');
46 $uploadTreeId = strpos($uploadTreeId,
',') !==
false
47 ? explode(
',', $uploadTreeId)
48 : intval($uploadTreeId);
50 if (empty($uploadTreeId)) {
51 return new JsonResponse(array(
"error" =>
'bad request'), JsonResponse::HTTP_BAD_REQUEST);
56 }
catch (Exception $ex) {
57 $errorMsg = $ex->getMessage();
58 return new JsonResponse(array(
"error" => $errorMsg), JsonResponse::HTTP_INTERNAL_SERVER_ERROR);
62 return new JsonResponse(array(
"jqid" => $jobQueueId));
73 if (is_array($uploadTreeId)) {
75 foreach ($uploadTreeId as $uploadTreePk) {
76 $jqId[] = $this->getJobQueueId($uploadTreePk, $request);
80 return $this->getJobQueueId($uploadTreeId, $request);
89 private function getJobQueueId($uploadTreeId, Request $request)
91 $uploadEntry = $this->uploadDao->getUploadEntry($uploadTreeId);
92 $uploadId = intval($uploadEntry[
'upload_fk']);
93 $userId = Auth::getUserId();
94 $groupId = Auth::getGroupId();
96 if ($uploadId <= 0 || !$this->uploadDao->isAccessible($uploadId, $groupId)) {
97 throw new Exception(
'permission denied');
100 $bulkScope = $request->get(
'bulkScope');
101 switch ($bulkScope) {
103 $uploadTreeTable = $this->uploadDao->getUploadtreeTableName($uploadId);
104 $topBounds = $this->uploadDao->getParentItemBounds($uploadId, $uploadTreeTable);
105 $uploadTreeId = $topBounds->getItemId();
109 if (!
Isdir($uploadEntry[
'ufile_mode']) &&
112 $uploadTreeId = $uploadEntry[
'parent'] ?: $uploadTreeId;
117 throw new InvalidArgumentException(
'bad scope request');
120 $refText = $request->get(
'refText');
121 $actions = $request->get(
'bulkAction');
122 $ignoreIrrelevantFiles = (intval($request->get(
'ignoreIrre')) == 1);
123 $scanFindingsOnly = boolval(intval($request->get(
'scanOnlyFindings')));
124 $delimiters = $request->get(
'delimiters');
126 $licenseRemovals = array();
127 foreach ($actions as $licenseAction) {
128 $licenseRemovals[$licenseAction[
'licenseId']] = array(($licenseAction[
'action']==
'Remove'), $licenseAction[
'comment'], $licenseAction[
'reportinfo'], $licenseAction[
'acknowledgement']);
130 $bulkId = $this->licenseDao->insertBulkLicense($userId, $groupId,
131 $uploadTreeId, $licenseRemovals, $refText, $ignoreIrrelevantFiles,
132 $delimiters, $scanFindingsOnly);
135 throw new Exception(
'cannot insert bulk reference');
139 $addToCustomPhrase = intval($request->get(
'addToCustomPhrase'));
140 if ($addToCustomPhrase == 1) {
143 $upload = $this->uploadDao->getUpload($uploadId);
144 $uploadName = $upload->getFilename();
145 $job_pk = JobAddJob($userId, $groupId, $uploadName, $uploadId);
149 $dependecies = array(array(
'name' =>
'agent_monk_bulk',
'args' => $bulkId));
151 $conflictStrategyId = intval($request->get(
'forceDecision'));
153 $jqId =
$deciderPlugin->AgentAdd($job_pk, $uploadId, $errorMsg, $dependecies, $conflictStrategyId);
155 if (!empty($errorMsg)) {
156 throw new Exception(str_replace(
'<br>',
"\n", $errorMsg));
173 $bulkDataSql =
"SELECT rf_text FROM license_ref_bulk WHERE lrb_pk = $1";
174 $this->
dbManager->prepare($bulkStmt = __METHOD__ .
".getBulkData", $bulkDataSql);
175 $bulkResult = $this->
dbManager->execute($bulkStmt, array($bulkId));
176 $bulkRow = $this->
dbManager->fetchArray($bulkResult);
177 $this->
dbManager->freeResult($bulkResult);
179 if ($bulkRow ===
false) {
180 error_log(
"Failed to fetch bulk data for lrb_pk: $bulkId");
184 $refText = $bulkRow[
'rf_text'];
185 $textMd5 = md5($refText);
187 $licensesSql =
"SELECT rf_fk, COALESCE(removing, false) as removing,
188 comment, reportinfo, acknowledgement
189 FROM license_set_bulk WHERE lrb_fk = $1";
190 $this->
dbManager->prepare($licenseStmt = __METHOD__ .
".getLicenses", $licensesSql);
191 $licensesResult = $this->
dbManager->execute($licenseStmt, array($bulkId));
194 while ($licenseRow = $this->
dbManager->fetchArray($licensesResult)) {
196 'rf_fk' => intval($licenseRow[
'rf_fk']),
197 'removing' => $licenseRow[
'removing'] ===
't' || $licenseRow[
'removing'] ===
true,
198 'comment' => $licenseRow[
'comment'] ?:
null,
199 'reportinfo' => $licenseRow[
'reportinfo'] ?:
null,
200 'acknowledgement' => $licenseRow[
'acknowledgement'] ?:
null
203 $this->
dbManager->freeResult($licensesResult);
209 $insertSql =
"INSERT INTO custom_phrase
210 (text, text_md5, acknowledgement, comments, user_fk, group_fk, is_active, created_date)
211 VALUES ($1, $2, $3, $4, $5, $6, $7, CURRENT_TIMESTAMP)
212 ON CONFLICT (text_md5) DO NOTHING
214 $params = array($refText, $textMd5,
'',
'', $userId, $groupId,
'true');
215 $this->
dbManager->prepare($insertStmt = __METHOD__ .
".insertPhrase", $insertSql);
216 $result = $this->
dbManager->execute($insertStmt, $params);
217 $row = $this->
dbManager->fetchArray($result);
220 $isNewPhrase = ($row !==
false);
222 $cpPk = $row[
'cp_pk'];
224 $checkSql =
"SELECT cp_pk FROM custom_phrase WHERE text_md5 = $1";
225 $this->
dbManager->prepare($checkStmt = __METHOD__ .
".findExisting", $checkSql);
226 $checkResult = $this->
dbManager->execute($checkStmt, array($textMd5));
227 $existingPhrase = $this->
dbManager->fetchArray($checkResult);
228 $this->
dbManager->freeResult($checkResult);
229 if ($existingPhrase ===
false) {
230 throw new Exception(
"custom_phrase insert conflicted but no row found for text_md5 $textMd5");
232 $cpPk = $existingPhrase[
'cp_pk'];
235 if (!empty($licenses)) {
236 $mapSql =
"INSERT INTO custom_phrase_license_map
237 (cp_fk, rf_fk, removing, comment, reportinfo, acknowledgement)
238 VALUES ($1, $2, $3, $4, $5, $6)
239 ON CONFLICT (cp_fk, rf_fk) DO NOTHING";
240 $this->
dbManager->prepare($mapStmt = __METHOD__ .
".insertLicenseMap", $mapSql);
242 foreach ($licenses as $license) {
243 $mapResult = $this->
dbManager->execute($mapStmt, array(
246 $license[
'removing'] ?
'true' :
'false',
248 $license[
'reportinfo'],
249 $license[
'acknowledgement']
251 $this->
dbManager->freeResult($mapResult);
256 error_log(($isNewPhrase ?
"Custom phrase imported" :
"Licenses merged into existing custom phrase")
257 .
" from bulk data. cp_pk: $cpPk, lrb_pk: $bulkId");
258 }
catch (Exception $e) {
260 error_log(
"Error importing bulk data to custom phrase: " . $e->getMessage());
scheduleBulkScan($uploadTreeId, Request $request)
importBulkDataToCustomPhrase($bulkId, $userId, $groupId)
UI plugin for DeciderJobAgent.
Contains the constants and helpers for authentication of user.
ReportCachePurgeAll()
Purge all records from the report cache.
plugin_find($pluginName)
Given the official name of a plugin, return the $Plugins object.
#define PERM_WRITE
Read-Write permission.
fo_dbManager * dbManager
fo_dbManager object