FOSSology  3.2.0rc1
Open Source License Compliance by Open Source Software
UploadVcsPage.php
1 <?php
2 /***********************************************************
3  Copyright (C) 2013 Hewlett-Packard Development Company, L.P.
4  Copyright (C) 2015 Siemens AG
5 
6  This program is free software; you can redistribute it and/or
7  modify it under the terms of the GNU General Public License
8  version 2 as published by the Free Software Foundation.
9 
10  This program is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  GNU General Public License for more details.
14 
15  You should have received a copy of the GNU General Public License along
16  with this program; if not, write to the Free Software Foundation, Inc.,
17  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 ***********************************************************/
19 
20 namespace Fossology\UI\Page;
21 
25 
30 {
31  const NAME = "upload_vcs";
32  const GETURL_PARAM = 'geturl';
33 
34  public function __construct()
35  {
36  parent::__construct(self::NAME, array(
37  self::TITLE => _("Upload from Version Control System"),
38  self::MENU_LIST => "Upload::From Version Control System",
39  self::DEPENDENCIES => array("agent_unpack", "showjobs"),
40  self::PERMISSION => Auth::PERM_WRITE
41  ));
42  }
43 
48  protected function handleView(Request $request, $vars)
49  {
50  $vars['vcstypeField'] = 'vcstype';
51  $vars['usernameField'] = 'username';
52  $vars['passwdField'] = 'passwd';
53  $vars['geturlField'] = self::GETURL_PARAM;
54  $vars['branchField'] = 'branch';
55  $vars['nameField'] = 'name';
56  $this->renderer->clearTemplateCache();
57  $this->renderer->clearCacheFiles();
58  return $this->render("upload_vcs.html.twig", $this->mergeWithDefault($vars));
59  }
60 
64  protected function handleUpload(Request $request)
65  {
66  global $MODDIR;
67  global $SYSCONFDIR;
68  global $Plugins;
69 
70  $folderId = intval($request->get(self::FOLDER_PARAMETER_NAME));
71  $description = stripslashes($request->get(self::DESCRIPTION_INPUT_NAME));
72  $description = $this->basicShEscaping($description);
73 
74  $getUrlThatMightIncludeSpaces = trim($request->get(self::GETURL_PARAM));
75  $getUrl = str_replace(" ", "%20", $getUrlThatMightIncludeSpaces);
76 
77  if (empty($getUrl)) {
78  return array(false, _("Empty URL") . $getUrl, $description);
79  }
80  if (preg_match("@^((http)|(https))://([[:alnum:]]+)@i", $getUrl) != 1) {
81  return array(false, _("Invalid URL") . $getUrl, $description);
82  }
83  $getUrl = $this->basicShEscaping($getUrl);
84 
85  if ($request->getSession()->get(self::UPLOAD_FORM_BUILD_PARAMETER_NAME)
86  != $request->get(self::UPLOAD_FORM_BUILD_PARAMETER_NAME)) {
87  $text = _("This seems to be a resent file.");
88  return array(false, $text, $description);
89  }
90 
91  if (empty($folderId)) {
92  $text = _("Invalid Folder.");
93  return array(false, $text, $description);
94  }
95 
96  $public = $request->get('public');
97  $publicPermission = ($public == self::PUBLIC_ALL) ? Auth::PERM_READ : Auth::PERM_NONE;
98 
99  $Name = trim($request->get('name'));
100  if (empty($Name)) {
101  $Name = basename($getUrl);
102  }
103  $ShortName = basename($Name);
104  if (empty($ShortName)) {
105  $ShortName = $Name;
106  }
107 
108  /* Create an upload record. */
109  $uploadMode = (1 << 2); // code for "it came from wget"
110  $userId = Auth::getUserId();
111  $groupId = Auth::getGroupId();
112  $uploadId = JobAddUpload($userId, $groupId, $ShortName, $getUrl,
113  $description, $uploadMode, $folderId, $publicPermission);
114  if (empty($uploadId)) {
115  $text = _("Failed to insert upload record");
116  return array(false, $text, $description);
117  }
118 
119  /* Create the job: job "wget" */
120  $jobpk = JobAddJob($userId, $groupId, "wget", $uploadId);
121  if (empty($jobpk) || ($jobpk < 0)) {
122  $text = _("Failed to insert job record");
123  return array(false, $text, $description);
124  }
125 
126  $VCSType = trim($request->get('vcstype'));
127  $VCSType = $this->basicShEscaping($VCSType);
128  $jq_args = "$uploadId - $getUrl $VCSType ";
129 
130  $Username = trim($request->get('username'));
131  $Username = $this->basicShEscaping($Username);
132  if (!empty($Username)) {
133  $jq_args .= "--username $Username ";
134  }
135 
136  $Passwd = trim($request->get('passwd'));
137  $Passwd = $this->basicShEscaping($Passwd);
138  if (!empty($Passwd)) {
139  $jq_args .= "--password $Passwd ";
140  }
141 
142  $Branch = trim(explode(' ',trim($request->get('branch')))[0]);
143  if (!empty($Branch) && strcasecmp($VCSType,'git') == 0) {
144  $jq_args .= "--single-branch --branch '$Branch'";
145  }
146 
147  $jobqueuepk = JobQueueAdd($jobpk, "wget_agent", $jq_args, NULL, NULL);
148  if (empty($jobqueuepk)) {
149  $text = _("Failed to insert task 'wget_agent' into job queue");
150  return array(false, $text, $description);
151  }
152  /* schedule agents */
153  $unpackplugin = &$Plugins[plugin_find_id("agent_unpack") ];
154  $unpackArgs = intval($request->get('scm')) == 1 ? '-I' : '';
155  $ununpack_jq_pk = $unpackplugin->AgentAdd($jobpk, $uploadId, $ErrorMsg, array("wget_agent"), $unpackArgs);
156  if ($ununpack_jq_pk < 0) {
157  return array(false, _($ErrorMsg), $description);
158  }
159 
160  $adj2nestplugin = &$Plugins[plugin_find_id("agent_adj2nest") ];
161  $adj2nest_jq_pk = $adj2nestplugin->AgentAdd($jobpk, $uploadId, $ErrorMsg, array());
162  if ($adj2nest_jq_pk < 0) {
163  return array(false, _($ErrorMsg), $description);
164  }
165 
166  AgentCheckBoxDo($jobpk, $uploadId);
167 
168  $msg = "";
170  $status = GetRunnableJobList();
171  if (empty($status)) {
172  $msg .= _("Is the scheduler running? ");
173  }
174  $Url = Traceback_uri() . "?mod=showjobs&upload=$uploadId";
175  $text = _("The upload");
176  $text1 = _("has been queued. It is");
177  $msg .= "$text $Name $text1 ";
178  $keep = "<a href='$Url'>upload #" . $uploadId . "</a>.\n";
179  return array(true, $msg.$keep, $description, $uploadId);
180  }
181 }
182 
183 register_plugin(new UploadVcsPage());
GetRunnableJobList()
Get runnable job list, the process is below:
Traceback_uri()
Get the URI without query to this location.
static getUserId()
Get the current user&#39;s id.
Definition: Auth.php:69
AgentCheckBoxDo($job_pk, $upload_pk)
Assume someone called AgentCheckBoxMake() and submitted the HTML form. Run AgentAdd() for each of the...
handleUpload(Request $request)
Process the upload request.
Upload from some Version Conntrol System using the UI.
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
handleView(Request $request, $vars)
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