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);
188 $checkSql =
"SELECT cp_pk FROM custom_phrase WHERE text_md5 = $1";
189 $this->
dbManager->prepare($checkStmt = __METHOD__ .
".checkDuplicate", $checkSql);
190 $checkResult = $this->
dbManager->execute($checkStmt, array($textMd5));
191 $existingPhrase = $this->
dbManager->fetchArray($checkResult);
192 $this->
dbManager->freeResult($checkResult);
194 if ($existingPhrase !==
false) {
196 error_log(
"Custom phrase with MD5 hash $textMd5 already exists. Skipping insertion.");
201 $licensesSql =
"SELECT rf_fk, COALESCE(removing, false) as removing FROM license_set_bulk
203 $this->
dbManager->prepare($licenseStmt = __METHOD__ .
".getLicenses", $licensesSql);
204 $licensesResult = $this->
dbManager->execute($licenseStmt, array($bulkId));
207 while ($licenseRow = $this->
dbManager->fetchArray($licensesResult)) {
209 'rf_fk' => intval($licenseRow[
'rf_fk']),
210 'removing' => $licenseRow[
'removing'] ===
't' || $licenseRow[
'removing'] ===
true
213 $this->
dbManager->freeResult($licensesResult);
219 $insertSql =
"INSERT INTO custom_phrase
220 (text, text_md5, acknowledgement, comments, user_fk, group_fk, is_active, created_date)
221 VALUES ($1, $2, $3, $4, $5, $6, $7, CURRENT_TIMESTAMP) RETURNING cp_pk";
222 $params = array($refText, $textMd5,
'',
'', $userId, $groupId,
'true');
223 $this->
dbManager->prepare($insertStmt = __METHOD__ .
".insertPhrase", $insertSql);
224 $result = $this->
dbManager->execute($insertStmt, $params);
225 $row = $this->
dbManager->fetchArray($result);
227 if ($row ===
false) {
229 throw new Exception(
'Failed to insert custom phrase');
232 $cpPk = $row[
'cp_pk'];
236 if (!empty($licenses)) {
237 $mapSql =
"INSERT INTO custom_phrase_license_map (cp_fk, rf_fk, removing) VALUES ($1, $2, $3)";
238 $this->
dbManager->prepare($mapStmt = __METHOD__ .
".insertLicenseMap", $mapSql);
240 foreach ($licenses as $license) {
241 $mapResult = $this->
dbManager->execute($mapStmt, array($cpPk, $license[
'rf_fk'], $license[
'removing'] ?
'true' :
'false'));
242 $this->
dbManager->freeResult($mapResult);
247 error_log(
"Custom phrase imported successfully from bulk data. cp_pk: $cpPk, lrb_pk: $bulkId");
248 }
catch (Exception $e) {
250 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