FOSSology  3.2.0rc1
Open Source License Compliance by Open Source Software
UploadUrlPage.php
1 <?php
2 /***********************************************************
3  * Copyright (C) 2015 Siemens AG
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License
7  * version 2 as published by the Free Software Foundation.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License along
15  * with this program; if not, write to the Free Software Foundation, Inc.,
16  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17  ***********************************************************/
18 
19 namespace Fossology\UI\Page;
20 
23 
25 {
26  const NAME = 'upload_url';
27 
28  const NAME_PARAM = 'name';
29  const ACCEPT_PARAM = 'accept';
30  const REJECT_PARAM = 'reject';
31  const GETURL_PARAM = 'geturl';
32  const LEVEL_PARAM = 'level';
33 
34  public function __construct()
35  {
36  parent::__construct(self::NAME, array(
37  self::TITLE => _("Upload from URL"),
38  self::MENU_LIST => "Upload::From URL",
39  self::DEPENDENCIES => array("agent_unpack", "showjobs"),
40  self::PERMISSION => Auth::PERM_WRITE
41  ));
42  }
43 
44  protected function handleUpload(Request $request)
45  {
46  $folderId = intval($request->get(self::FOLDER_PARAMETER_NAME));
47  $description = stripslashes($request->get(self::DESCRIPTION_INPUT_NAME));
48  $description = $this->basicShEscaping($description);
49 
50  $getUrlThatMightIncludeSpaces = trim($request->get(self::GETURL_PARAM));
51  $getURL = str_replace(" ", "%20", $getUrlThatMightIncludeSpaces);
52 
53  if (empty($getURL)) {
54  return array(false, _("Invalid URL"), $description);
55  }
56  if (preg_match("@^((http)|(https)|(ftp))://([[:alnum:]]+)@i", $getURL) != 1) {
57  return array(false, _("Invalid URL"), $description);
58  }
59  $getURL = $this->basicShEscaping($getURL);
60 
61  $name = $request->get(self::NAME_PARAM);
62  if (empty($name)) {
63  $name = basename($getURL);
64  }
65  $shortName = basename($name);
66  if (empty($shortName)) {
67  $shortName = $name;
68  }
69 
70  /* Create an upload record. */
71  $mode = (1 << 2); // code for "it came from wget"
72  $userId = Auth::getUserId();
73  $groupId = Auth::getGroupId();
74  $public = $request->get('public');
75  $publicPermission = ($public == self::PUBLIC_ALL) ? Auth::PERM_READ : Auth::PERM_NONE;
76 
77  $uploadId = JobAddUpload($userId, $groupId, $shortName, $getURL, $description, $mode, $folderId, $publicPermission);
78  if (empty($uploadId)) {
79  $text = _("Failed to insert upload record");
80  return array(false, $text, $description);
81  }
82 
83  $level = intval($request->get(self::LEVEL_PARAM));
84  if ($level < 0) {
85  $level = 1;
86  }
87 
88  /* first trim, then get rid of whitespaces before and after each comma letter */
89  $accept = preg_replace('/\s*,\s*/', ',', trim($request->get(self::ACCEPT_PARAM)));
90  $accept = $this->basicShEscaping($accept);
91  $reject = preg_replace('/\s*,\s*/', ',', trim($request->get(self::REJECT_PARAM)));
92  $reject = $this->basicShEscaping($reject);
93 
94  /* Create the job: job "wget" */
95  $jobId = JobAddJob($userId, $groupId, "wget", $uploadId);
96  if (empty($jobId) || ($jobId < 0)) {
97  return array(false, _("Failed to insert job record"), $description);
98  }
99 
100  $jqArgs = "$uploadId - $getURL -l $level ";
101  if (! empty($accept)) {
102  $jqArgs .= "-A $accept ";
103  }
104  $jqArgs .= empty($reject) ? "-R index.html* " : "-R $reject,index.html* ";
105 
106  $jobqueueId = JobQueueAdd($jobId, "wget_agent", $jqArgs, NULL, NULL);
107  if (empty($jobqueueId)) {
108  return array(false,
109  "Failed to insert task 'wget_agent' into job queue", $description);
110  }
111 
112  $message = $this->postUploadAddJobs($request, $shortName, $uploadId, $jobId, true);
113  return array(true, $message, $description, $uploadId);
114  }
115 
116  protected function handleView(Request $request, $vars)
117  {
118  $vars['geturlField'] = self::GETURL_PARAM;
119  $vars['nameField'] = self::NAME_PARAM;
120  $vars['acceptField'] = self::ACCEPT_PARAM;
121  $vars['rejectField'] = self::REJECT_PARAM;
122  $vars['levelField'] = self::LEVEL_PARAM;
123  return $this->render("upload_url.html.twig", $this->mergeWithDefault($vars));
124  }
125 }
126 
127 register_plugin(new UploadUrlPage());
static getUserId()
Get the current user&#39;s id.
Definition: Auth.php:69
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:159
static getGroupId()
Get the current user&#39;s group id.
Definition: Auth.php:78
JobAddUpload($userId, $groupId, $job_name, $filename, $desc, $UploadMode, $folder_pk, $public_perm=Auth::PERM_NONE)
Insert a new upload record, and update the foldercontents table.
Definition: common-job.php:66
char * trim(char *ptext)
Trimming whitespace.
Definition: fossconfig.c:695