FOSSology  4.4.0
Open Source License Compliance by Open Source Software
UploadUrlPage.php
1 <?php
2 /*
3  SPDX-FileCopyrightText: © 2015 Siemens AG
4 
5  SPDX-License-Identifier: GPL-2.0-only
6 */
7 
8 namespace Fossology\UI\Page;
9 
11 use Symfony\Component\HttpFoundation\Request;
12 
14 {
15  const NAME = 'upload_url';
16 
17  const NAME_PARAM = 'name';
18  const ACCEPT_PARAM = 'accept';
19  const REJECT_PARAM = 'reject';
20  const GETURL_PARAM = 'geturl';
21  const LEVEL_PARAM = 'level';
22 
23  public function __construct()
24  {
25  parent::__construct(self::NAME, array(
26  self::TITLE => _("Upload from URL"),
27  self::MENU_LIST => "Upload::From URL",
28  self::DEPENDENCIES => array("agent_unpack", "showjobs"),
29  self::PERMISSION => Auth::PERM_WRITE
30  ));
31  }
32 
33  protected function handleUpload(Request $request)
34  {
35  $folderId = intval($request->get(self::FOLDER_PARAMETER_NAME));
36  $description = stripslashes($request->get(self::DESCRIPTION_INPUT_NAME));
37  $description = $this->basicShEscaping($description);
38 
39  $getUrlThatMightIncludeSpaces = trim($request->get(self::GETURL_PARAM));
40  $getURL = str_replace(" ", "%20", $getUrlThatMightIncludeSpaces);
41 
42  if (empty($getURL)) {
43  return array(false, _("Invalid URL"), $description);
44  }
45  if (preg_match("@^((http)|(https)|(ftp))://([[:alnum:]]+)@i", $getURL) != 1) {
46  return array(false, _("Invalid URL"), $description);
47  }
48  $getURL = $this->basicShEscaping($getURL);
49 
50  $name = $request->get(self::NAME_PARAM);
51  if (empty($name)) {
52  $name = basename($getURL);
53  }
54  $shortName = basename($name);
55  if (empty($shortName)) {
56  $shortName = $name;
57  }
58 
59  /* Create an upload record. */
60  $mode = (1 << 2); // code for "it came from wget"
61  $userId = Auth::getUserId();
62  $groupId = Auth::getGroupId();
63  $setGlobal = ($request->get('globalDecisions')) ? 1 : 0;
64  $public = $request->get('public');
65  $publicPermission = ($public == self::PUBLIC_ALL) ? Auth::PERM_READ : Auth::PERM_NONE;
66 
67  $uploadId = JobAddUpload($userId, $groupId, $shortName, $getURL, $description, $mode, $folderId, $publicPermission, $setGlobal);
68  if (empty($uploadId)) {
69  $text = _("Failed to insert upload record");
70  return array(false, $text, $description);
71  }
72 
73  $level = intval($request->get(self::LEVEL_PARAM));
74  if ($level < 0) {
75  $level = 1;
76  }
77 
78  /* first trim, then get rid of whitespaces before and after each comma letter */
79  $accept = preg_replace('/\s*,\s*/', ',', trim($request->get(self::ACCEPT_PARAM)));
80  $accept = $this->basicShEscaping($accept);
81  $reject = preg_replace('/\s*,\s*/', ',', trim($request->get(self::REJECT_PARAM)));
82  $reject = $this->basicShEscaping($reject);
83 
84  /* Create the job: job "wget" */
85  $jobId = JobAddJob($userId, $groupId, "wget", $uploadId);
86  if (empty($jobId) || ($jobId < 0)) {
87  return array(false, _("Failed to insert job record"), $description);
88  }
89 
90  $jqArgs = "$uploadId - $getURL -l $level ";
91  if (! empty($accept)) {
92  $jqArgs .= "-A $accept ";
93  }
94  $jqArgs .= empty($reject) ? "-R index.html* " : "-R $reject,index.html* ";
95 
96  $jobqueueId = JobQueueAdd($jobId, "wget_agent", $jqArgs, NULL, NULL);
97  if (empty($jobqueueId)) {
98  return array(false,
99  "Failed to insert task 'wget_agent' into job queue", $description);
100  }
101 
102  $message = $this->postUploadAddJobs($request, $shortName, $uploadId, $jobId, true);
103  return array(true, $message, $description, $uploadId);
104  }
105 
106  protected function handleView(Request $request, $vars)
107  {
108  $vars['geturlField'] = self::GETURL_PARAM;
109  $vars['nameField'] = self::NAME_PARAM;
110  $vars['acceptField'] = self::ACCEPT_PARAM;
111  $vars['rejectField'] = self::REJECT_PARAM;
112  $vars['levelField'] = self::LEVEL_PARAM;
113  return $this->render("upload_url.html.twig", $this->mergeWithDefault($vars));
114  }
115 }
116 
117 register_plugin(new UploadUrlPage());
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
render($templateName, $vars=null, $headers=null)
JobQueueAdd($job_pk, $jq_type, $jq_args, $jq_runonpfile, $Depends, $host=NULL, $jq_cmd_args=NULL)
Insert a jobqueue + jobdepends records.
Definition: common-job.php:157
JobAddUpload($userId, $groupId, $job_name, $filename, $desc, $UploadMode, $folder_pk, $public_perm=Auth::PERM_NONE, $setGlobal=0)
Insert a new upload record, and update the foldercontents table.
Definition: common-job.php:56
char * trim(char *ptext)
Trimming whitespace.
Definition: fossconfig.c:690