FOSSology  3.2.0rc1
Open Source License Compliance by Open Source Software
UploadHelper.php
Go to the documentation of this file.
1 <?php
26 namespace Fossology\UI\Api\Helper;
27 
37 
43 {
48  private $uploadFilePage;
49 
54  private $uploadVcsPage;
55 
60  private $uploadUrlPage;
61 
66  private $uploadSrvPage;
67 
72  const VALID_VCS_TYPES = array(
73  "git",
74  "svn"
75  );
76 
81  const VALID_UPLOAD_TYPES = array(
82  "vcs",
83  "url",
84  "server"
85  );
86 
90  public function __construct()
91  {
92  $this->uploadFilePage = new HelperToUploadFilePage();
93  $this->uploadVcsPage = new HelperToUploadVcsPage();
94  $this->uploadUrlPage = new HelperToUploadUrlPage();
95  $this->uploadSrvPage = new HelperToUploadSrvPage();
96  }
97 
112  public function createNewUpload(ServerRequestInterface $request, $folderId,
113  $fileDescription, $isPublic, $ignoreScm, $uploadType)
114  {
115  $uploadedFile = $request->getUploadedFiles();
116  $body = $request->getParsedBody();
117 
118  if (! empty($ignoreScm) && ($ignoreScm == "true" || $ignoreScm)) {
119  // If SCM should be ignored
120  $ignoreScm = 1;
121  } else {
122  $ignoreScm = 0;
123  }
124  if (empty($uploadedFile) ||
125  ! isset($uploadedFile[$this->uploadFilePage::FILE_INPUT_NAME])) {
126  if (empty($uploadType)) {
127  return array(false, "Missing 'uploadType' header",
128  "Send file with parameter " . $this->uploadFilePage::FILE_INPUT_NAME .
129  " or define 'uploadType' header with appropriate body.",
130  - 1
131  );
132  }
133  return $this->handleUpload($body, $uploadType, $folderId,
134  $fileDescription, $isPublic, $ignoreScm);
135  } else {
136  $uploadedFile = $uploadedFile[$this->uploadFilePage::FILE_INPUT_NAME];
137  return $this->createFileUpload($uploadedFile, $folderId,
138  $fileDescription, $isPublic, $ignoreScm);
139  }
140  }
141 
152  private function createFileUpload($uploadedFile, $folderId, $fileDescription,
153  $isPublic, $ignoreScm = 0)
154  {
155  $path = $uploadedFile->file;
156  $originalName = $uploadedFile->getClientFilename();
157  $originalMime = $uploadedFile->getClientMediaType();
158  $originalError = $uploadedFile->getError();
159  $symfonyFile = new \Symfony\Component\HttpFoundation\File\UploadedFile(
160  $path, $originalName, $originalMime, $originalError);
161  $symfonyRequest = new \Symfony\Component\HttpFoundation\Request();
162  $symfonySession = $GLOBALS['container']->get('session');
163  $symfonySession->set(
164  $this->uploadFilePage::UPLOAD_FORM_BUILD_PARAMETER_NAME, "restUpload");
165 
166  $symfonyRequest->request->set($this->uploadFilePage::FOLDER_PARAMETER_NAME,
167  $folderId);
168  $symfonyRequest->request->set($this->uploadFilePage::DESCRIPTION_INPUT_NAME,
169  $fileDescription);
170  $symfonyRequest->files->set($this->uploadFilePage::FILE_INPUT_NAME,
171  $symfonyFile);
172  $symfonyRequest->setSession($symfonySession);
173  $symfonyRequest->request->set(
174  $this->uploadFilePage::UPLOAD_FORM_BUILD_PARAMETER_NAME, "restUpload");
175  $symfonyRequest->request->set('public', $isPublic);
176  $symfonyRequest->request->set('scm', $ignoreScm);
177 
178  return $this->uploadFilePage->handleRequest($symfonyRequest);
179  }
180 
192  private function handleUpload($body, $uploadType, $folderId, $fileDescription,
193  $isPublic, $ignoreScm = 0)
194  {
195  $sanity = false;
196  switch ($uploadType) {
197  case "vcs":
198  $sanity = $this->sanitizeVcsData($body);
199  break;
200  case "url":
201  $sanity = $this->sanitizeUrlData($body);
202  break;
203  case "server":
204  $sanity = $this->sanitizeSrvData($body);
205  break;
206  default:
207  $message = "Invalid 'uploadType'";
208  $statusDescription = "uploadType should be any of (" .
209  implode(",", self::VALID_UPLOAD_TYPES) . ")";
210  $code = 400;
211  $sanity = array(false, $message, $statusDescription, $code);
212  }
213  if ($sanity !== true) {
214  return $sanity;
215  }
216  $uploadResponse = false;
217  switch ($uploadType) {
218  case "vcs":
219  $uploadResponse = $this->generateVcsUpload($body, $folderId,
220  $fileDescription, $isPublic, $ignoreScm);
221  break;
222  case "url":
223  $uploadResponse = $this->generateUrlUpload($body, $folderId,
224  $fileDescription, $isPublic, $ignoreScm);
225  break;
226  case "server":
227  $uploadResponse = $this->generateSrvUpload($body, $folderId,
228  $fileDescription, $isPublic, $ignoreScm);
229  break;
230  }
231  return $uploadResponse;
232  }
233 
244  private function sanitizeVcsData(&$vcsData)
245  {
246  $message = "";
247  $statusDescription = "";
248  $code = 0;
249 
250  if (! array_key_exists("vcsType", $vcsData) ||
251  ! in_array($vcsData["vcsType"], self::VALID_VCS_TYPES)) {
252  $message = "Missing vcsType";
253  $statusDescription = "vcsType should be any of (" .
254  implode(", ", self::VALID_VCS_TYPES) . ")";
255  $code = 400;
256  }
257  $vcsType = "";
258  if ($vcsData["vcsType"] == "git") {
259  $vcsType = "Git";
260  } else {
261  $vcsType = "SVN";
262  }
263 
264  if (! array_key_exists("vcsUrl", $vcsData)) {
265  $message = "Missing vcsUrl";
266  $statusDescription = "vcsUrl should be passed.";
267  $code = 400;
268  }
269 
270  if (! array_key_exists("vcsName", $vcsData)) {
271  $vcsData["vcsName"] = "";
272  }
273  if (! array_key_exists("vcsUsername", $vcsData)) {
274  $vcsData["vcsUsername"] = "";
275  }
276  if (! array_key_exists("vcsPassword", $vcsData)) {
277  $vcsData["vcsPassword"] = "";
278  }
279  if (! array_key_exists("vcsBranch", $vcsData)) {
280  $vcsData["vcsBranch"] = "";
281  }
282  $vcsData["vcsType"] = $vcsType;
283  if ($code !== 0) {
284  return array(false, $message, $statusDescription, $code);
285  } else {
286  return true;
287  }
288  }
289 
299  private function sanitizeUrlData(&$urlData)
300  {
301  $message = "";
302  $statusDescription = "";
303  $code = 0;
304 
305  if (! array_key_exists("url", $urlData)) {
306  $message = "Missing url";
307  $statusDescription = "Missing upload url from request";
308  $code = 400;
309  }
310 
311  if (! array_key_exists("name", $urlData)) {
312  $urlData["name"] = "";
313  }
314  if (! array_key_exists("accept", $urlData)) {
315  $urlData["accept"] = "";
316  }
317  if (! array_key_exists("reject", $urlData)) {
318  $urlData["reject"] = "";
319  }
320  if (! array_key_exists("maxRecursionDepth", $urlData)) {
321  $urlData["maxRecursionDepth"] = "";
322  }
323  if ($code !== 0) {
324  return array(false, $message, $statusDescription, $code);
325  } else {
326  return true;
327  }
328  }
329 
339  private function sanitizeSrvData(&$srvData)
340  {
341  $message = "";
342  $statusDescription = "";
343  $code = 0;
344 
345  if (! array_key_exists("path", $srvData)) {
346  $message = "Missing path";
347  $statusDescription = "Missing upload path from request";
348  $code = 400;
349  }
350 
351  if (! array_key_exists("name", $srvData)) {
352  $srvData["name"] = "";
353  }
354  if ($code !== 0) {
355  return array(false, $message, $statusDescription, $code);
356  } else {
357  return true;
358  }
359  }
360 
370  private function generateVcsUpload($vcsData, $folderId, $fileDescription,
371  $isPublic, $ignoreScm)
372  {
373  $vcsType = $vcsData["vcsType"];
374  $vcsUrl = $vcsData["vcsUrl"];
375  $vcsName = $vcsData["vcsName"];
376  $vcsUsername = $vcsData["vcsUsername"];
377  $vcsPasswd = $vcsData["vcsPassword"];
378  $vcsBranch = $vcsData["vcsBranch"];
379 
380  $symfonySession = $GLOBALS['container']->get('session');
381  $symfonySession->set($this->uploadVcsPage::UPLOAD_FORM_BUILD_PARAMETER_NAME,
382  "restUpload");
383 
384  $symfonyRequest = new \Symfony\Component\HttpFoundation\Request();
385  $symfonyRequest->setSession($symfonySession);
386 
387  $symfonyRequest->request->set($this->uploadVcsPage::FOLDER_PARAMETER_NAME,
388  $folderId);
389  $symfonyRequest->request->set($this->uploadVcsPage::DESCRIPTION_INPUT_NAME,
390  $fileDescription);
391  $symfonyRequest->request->set($this->uploadVcsPage::GETURL_PARAM, $vcsUrl);
392  $symfonyRequest->request->set(
393  $this->uploadVcsPage::UPLOAD_FORM_BUILD_PARAMETER_NAME, "restUpload");
394  $symfonyRequest->request->set('public', $isPublic);
395  $symfonyRequest->request->set('name', $vcsName);
396  $symfonyRequest->request->set('vcstype', $vcsType);
397  $symfonyRequest->request->set('username', $vcsUsername);
398  $symfonyRequest->request->set('passwd', $vcsPasswd);
399  $symfonyRequest->request->set('branch', $vcsBranch);
400  $symfonyRequest->request->set('scm', $ignoreScm);
401 
402  return $this->uploadVcsPage->handleRequest($symfonyRequest);
403  }
404 
414  private function generateUrlUpload($urlData, $folderName, $fileDescription,
415  $isPublic, $ignoreScm)
416  {
417  $url = $urlData["url"];
418  $name = $urlData["name"];
419  $accept = $urlData["accept"];
420  $reject = $urlData["reject"];
421  $maxRecursionDepth = $urlData["maxRecursionDepth"];
422 
423  $symfonySession = $GLOBALS['container']->get('session');
424  $symfonySession->set($this->uploadUrlPage::UPLOAD_FORM_BUILD_PARAMETER_NAME,
425  "restUpload");
426 
427  $symfonyRequest = new \Symfony\Component\HttpFoundation\Request();
428  $symfonyRequest->setSession($symfonySession);
429 
430  $symfonyRequest->request->set($this->uploadUrlPage::FOLDER_PARAMETER_NAME,
431  $folderName);
432  $symfonyRequest->request->set($this->uploadUrlPage::DESCRIPTION_INPUT_NAME,
433  $fileDescription);
434  $symfonyRequest->request->set(
435  $this->uploadUrlPage::UPLOAD_FORM_BUILD_PARAMETER_NAME, "restUpload");
436  $symfonyRequest->request->set('public', $isPublic);
437  $symfonyRequest->request->set($this->uploadUrlPage::NAME_PARAM, $name);
438  $symfonyRequest->request->set($this->uploadUrlPage::ACCEPT_PARAM, $accept);
439  $symfonyRequest->request->set($this->uploadUrlPage::REJECT_PARAM, $reject);
440  $symfonyRequest->request->set($this->uploadUrlPage::GETURL_PARAM, $url);
441  $symfonyRequest->request->set($this->uploadUrlPage::LEVEL_PARAM,
442  $maxRecursionDepth);
443  $symfonyRequest->request->set('scm', $ignoreScm);
444 
445  return $this->uploadUrlPage->handleRequest($symfonyRequest);
446  }
447 
457  private function generateSrvUpload($srvData, $folderName, $fileDescription,
458  $isPublic, $ignoreScm)
459  {
460  $path = $srvData["path"];
461  $name = $srvData["name"];
462 
463  $symfonySession = $GLOBALS['container']->get('session');
464  $symfonySession->set($this->uploadSrvPage::UPLOAD_FORM_BUILD_PARAMETER_NAME,
465  "restUpload");
466 
467  $symfonyRequest = new \Symfony\Component\HttpFoundation\Request();
468  $symfonyRequest->setSession($symfonySession);
469 
470  $symfonyRequest->request->set($this->uploadSrvPage::FOLDER_PARAMETER_NAME,
471  $folderName);
472  $symfonyRequest->request->set($this->uploadSrvPage::DESCRIPTION_INPUT_NAME,
473  $fileDescription);
474  $symfonyRequest->request->set(
475  $this->uploadSrvPage::UPLOAD_FORM_BUILD_PARAMETER_NAME, "restUpload");
476  $symfonyRequest->request->set('public', $isPublic);
477  $symfonyRequest->request->set($this->uploadSrvPage::SOURCE_FILES_FIELD,
478  $path);
479  $symfonyRequest->request->set($this->uploadSrvPage::NAME_PARAM, $name);
480  $symfonyRequest->request->set('scm', $ignoreScm);
481 
482  return $this->uploadSrvPage->handleRequest($symfonyRequest);
483  }
484 
491  public function generateUploadSummary($uploadId, $groupId)
492  {
493  global $container;
494  $restHelper = $container->get('helper.restHelper');
495  $uploadDao = $restHelper->getUploadDao();
496  $dbManager = $restHelper->getDbHelper()->getDbManager();
497  $clearingDao = $container->get('dao.clearing');
498  $copyrightDao = $container->get('dao.copyright');
499  $agentDao = $container->get('dao.agent');
500 
501  $agentName = "copyright";
502 
503  $totalClearings = $clearingDao->getTotalDecisionCount($uploadId, $groupId);
504  $clearingCount = $clearingDao->getClearingDecisionsCount($uploadId,
505  $groupId);
506  $uploadTreeTableName = $uploadDao->getUploadtreeTableName($uploadId);
507  $itemTreeBounds = $uploadDao->getParentItemBounds($uploadId,
508  $uploadTreeTableName);
509  $scanProx = new ScanJobProxy($agentDao, $uploadId);
510  $scanProx->createAgentStatus([$agentName]);
511  $agents = $scanProx->getLatestSuccessfulAgentIds();
512  $copyrightCount = 0;
513  if (array_key_exists($agentName, $agents) && ! empty($agents[$agentName])) {
514  $copyrightCount = count(
515  $copyrightDao->getAllEntriesReport($agentName, $uploadId,
516  $uploadTreeTableName, null, false, null, "C.agent_fk = " .
517  $agents[$agentName], $groupId));
518  }
519 
520  $mainLicenses = $this->getMainLicenses($dbManager, $uploadId, $groupId);
521 
522  $uiLicense = $restHelper->getPlugin("license");
523  $hist = $uiLicense->getUploadHist($itemTreeBounds);
524 
525  $summary = new UploadSummary();
526  $summary->setUploadId($uploadId);
527  $summary->setUploadName($uploadDao->getUpload($uploadId)->getFilename());
528  if ($mainLicenses !== null) {
529  $summary->setMainLicense(implode(",", $mainLicenses));
530  }
531  $summary->setUniqueLicenses($hist['uniqueLicenseCount']);
532  $summary->setTotalLicenses($hist['scannerLicenseCount']);
533  $summary->setUniqueConcludedLicenses($hist['editedUniqueLicenseCount']);
534  $summary->setTotalConcludedLicenses($hist['editedLicenseCount']);
535  $summary->setFilesToBeCleared($totalClearings - $clearingCount);
536  $summary->setFilesCleared($clearingCount);
537  $summary->setClearingStatus($uploadDao->getStatus($uploadId, $groupId));
538  $summary->setCopyrightCount($copyrightCount);
539  return $summary;
540  }
541 
549  private function getMainLicenses($dbManager, $uploadId, $groupId)
550  {
551  $sql = "SELECT rf_shortname FROM upload_clearing_license ucl, license_ref"
552  . " WHERE ucl.group_fk=$1 AND upload_fk=$2 AND ucl.rf_fk=rf_pk;";
553  $stmt = __METHOD__.'.collectMainLicenses';
554  $rows = $dbManager->getRows($sql, array($groupId, $uploadId), $stmt);
555  if (empty($rows)) {
556  return null;
557  }
558  $mainLicenses = [];
559  foreach ($rows as $row) {
560  array_push($mainLicenses, $row['rf_shortname']);
561  }
562  return $mainLicenses;
563  }
564 
573  public function getUploadLicenseList($uploadId, $agents, $printContainers)
574  {
575  global $container;
576  $restHelper = $container->get('helper.restHelper');
577  $uploadDao = $restHelper->getUploadDao();
578  $agentDao = $container->get('dao.agent');
579 
580  $uploadTreeTableName = $uploadDao->getUploadtreeTableName($uploadId);
581  $parent = $uploadDao->getParentItemBounds($uploadId, $uploadTreeTableName);
582 
583  $scanProx = new ScanJobProxy($agentDao, $uploadId);
584  $scanProx->createAgentStatus($agents);
585  $agent_ids = $scanProx->getLatestSuccessfulAgentIds();
586 
590  $licenseListObj = $restHelper->getPlugin('export-list');
591  $licenseList = $licenseListObj->createListOfLines($uploadTreeTableName,
592  $parent->getItemId(), $agent_ids, -1, true, '', !$printContainers);
593  if (array_key_exists("warn", $licenseList)) {
594  unset($licenseList["warn"]);
595  }
596  return $licenseList;
597  }
598 }
generateVcsUpload($vcsData, $folderId, $fileDescription, $isPublic, $ignoreScm)
generateSrvUpload($srvData, $folderName, $fileDescription, $isPublic, $ignoreScm)
sanitizeUrlData(&$urlData)
Check if the passed URL object is correct or not.
createFileUpload($uploadedFile, $folderId, $fileDescription, $isPublic, $ignoreScm=0)
handleUpload($body, $uploadType, $folderId, $fileDescription, $isPublic, $ignoreScm=0)
REST api helper classes.
createNewUpload(ServerRequestInterface $request, $folderId, $fileDescription, $isPublic, $ignoreScm, $uploadType)
generateUploadSummary($uploadId, $groupId)
Model class to hold Upload info.
generateUrlUpload($urlData, $folderName, $fileDescription, $isPublic, $ignoreScm)
getMainLicenses($dbManager, $uploadId, $groupId)
Handle new file uploads from Slim framework and move to FOSSology.
sanitizeVcsData(&$vcsData)
Check if the passed VCS object is correct or not.
sanitizeSrvData(&$srvData)
Check if the passed server upload object is correct or not.