FOSSology  4.4.0
Open Source License Compliance by Open Source Software
UploadVcsPage.php
1 <?php
2 /*
3  SPDX-FileCopyrightText: © 2013 Hewlett-Packard Development Company, L.P.
4  SPDX-FileCopyrightText: © 2015 Siemens AG
5 
6  SPDX-License-Identifier: GPL-2.0-only
7 */
8 
9 namespace Fossology\UI\Page;
10 
13 use Symfony\Component\HttpFoundation\Request;
14 
19 {
20  const NAME = "upload_vcs";
21  const GETURL_PARAM = 'geturl';
22 
23  public function __construct()
24  {
25  parent::__construct(self::NAME, array(
26  self::TITLE => _("Upload from Version Control System"),
27  self::MENU_LIST => "Upload::From Version Control System",
28  self::DEPENDENCIES => array("agent_unpack", "showjobs"),
29  self::PERMISSION => Auth::PERM_WRITE
30  ));
31  }
32 
38  protected function handleView(Request $request, $vars)
39  {
40  $vars['vcstypeField'] = 'vcstype';
41  $vars['usernameField'] = 'username';
42  $vars['passwdField'] = 'passwd';
43  $vars['geturlField'] = self::GETURL_PARAM;
44  $vars['branchField'] = 'branch';
45  $vars['nameField'] = 'name';
46  return $this->render("upload_vcs.html.twig", $this->mergeWithDefault($vars));
47  }
48 
52  protected function handleUpload(Request $request)
53  {
54  global $MODDIR;
55  global $SYSCONFDIR;
56  global $Plugins;
57 
58  $folderId = intval($request->get(self::FOLDER_PARAMETER_NAME));
59  $description = stripslashes($request->get(self::DESCRIPTION_INPUT_NAME));
60  $description = $this->basicShEscaping($description);
61 
62  $getUrlThatMightIncludeSpaces = trim($request->get(self::GETURL_PARAM));
63  $getUrl = str_replace(" ", "%20", $getUrlThatMightIncludeSpaces);
64 
65  if (empty($getUrl)) {
66  return array(false, _("Empty URL") . $getUrl, $description);
67  }
68  if (preg_match("@^((http)|(https))://([[:alnum:]]+)@i", $getUrl) != 1) {
69  return array(false, _("Invalid URL") . $getUrl, $description);
70  }
71  $getUrl = $this->basicShEscaping($getUrl);
72 
73  if ($request->getSession()->get(self::UPLOAD_FORM_BUILD_PARAMETER_NAME)
74  != $request->get(self::UPLOAD_FORM_BUILD_PARAMETER_NAME)) {
75  $text = _("This seems to be a resent file.");
76  return array(false, $text, $description);
77  }
78 
79  if (empty($folderId)) {
80  $text = _("Invalid Folder.");
81  return array(false, $text, $description);
82  }
83  $setGlobal = ($request->get('globalDecisions')) ? 1 : 0;
84 
85  $public = $request->get('public');
86  $publicPermission = ($public == self::PUBLIC_ALL) ? Auth::PERM_READ : Auth::PERM_NONE;
87 
88  $Name = trim($request->get('name'));
89  if (empty($Name)) {
90  $Name = basename($getUrl);
91  }
92  $ShortName = basename($Name);
93  if (empty($ShortName)) {
94  $ShortName = $Name;
95  }
96 
97  /* Create an upload record. */
98  $uploadMode = (1 << 2); // code for "it came from wget"
99  $userId = Auth::getUserId();
100  $groupId = Auth::getGroupId();
101  $uploadId = JobAddUpload($userId, $groupId, $ShortName, $getUrl,
102  $description, $uploadMode, $folderId, $publicPermission, $setGlobal);
103  if (empty($uploadId)) {
104  $text = _("Failed to insert upload record");
105  return array(false, $text, $description);
106  }
107 
108  /* Create the job: job "wget" */
109  $jobpk = JobAddJob($userId, $groupId, "wget", $uploadId);
110  if (empty($jobpk) || ($jobpk < 0)) {
111  $text = _("Failed to insert job record");
112  return array(false, $text, $description);
113  }
114 
115  $VCSType = trim($request->get('vcstype'));
116  $VCSType = $this->basicShEscaping($VCSType);
117  $jq_args = "$uploadId - $getUrl $VCSType ";
118 
119  $Username = trim($request->get('username'));
120  $Username = $this->basicShEscaping($Username);
121  if (!empty($Username)) {
122  $jq_args .= "--username $Username ";
123  }
124 
125  $Passwd = trim($request->get('passwd'));
126  $Passwd = $this->basicShEscaping($Passwd);
127  if (!empty($Passwd)) {
128  $jq_args .= "--password $Passwd ";
129  }
130 
131  $Branch = trim(explode(' ',trim($request->get('branch')))[0]);
132  if (!empty($Branch) && strcasecmp($VCSType,'git') == 0) {
133  $jq_args .= "--single-branch --branch '$Branch'";
134  }
135 
136  $jobqueuepk = JobQueueAdd($jobpk, "wget_agent", $jq_args, NULL, NULL);
137  if (empty($jobqueuepk)) {
138  $text = _("Failed to insert task 'wget_agent' into job queue");
139  return array(false, $text, $description);
140  }
141  /* schedule agents */
142  $unpackplugin = &$Plugins[plugin_find_id("agent_unpack") ];
143  $unpackArgs = intval($request->get('scm')) == 1 ? '-I' : '';
144  $ununpack_jq_pk = $unpackplugin->AgentAdd($jobpk, $uploadId, $ErrorMsg, array("wget_agent"), $unpackArgs);
145  if ($ununpack_jq_pk < 0) {
146  return array(false, _($ErrorMsg), $description);
147  }
148 
149  $adj2nestplugin = &$Plugins[plugin_find_id("agent_adj2nest") ];
150  $adj2nest_jq_pk = $adj2nestplugin->AgentAdd($jobpk, $uploadId, $ErrorMsg, array());
151  if ($adj2nest_jq_pk < 0) {
152  return array(false, _($ErrorMsg), $description);
153  }
154 
155  $message = $this->postUploadAddJobs($request, $Name, $uploadId, $jobpk);
156  return array(true, $message, $description, $uploadId);
157  }
158 }
159 
160 register_plugin(new UploadVcsPage());
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)
Upload from some Version Conntrol System using the UI.
handleView(Request $request, $vars)
handleUpload(Request $request)
Process the upload request.
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