FOSSology  3.2.0rc1
Open Source License Compliance by Open Source Software
showjobs.php
1 <?php
2 /***********************************************************
3  Copyright (C) 2012-2014 Hewlett-Packard Development Company, L.P.
4  Copyright (C) 2015-2016 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 
21 define("TITLE_SHOWJOBS", _("Show Jobs"));
22 
28 
29 class showjobs extends FO_Plugin
30 {
32  private $showJobsDao;
34  private $uploadDao;
36  private $jobDao;
37 
38  function __construct()
39  {
40  $this->Name = "showjobs";
41  $this->Title = TITLE_SHOWJOBS;
42  $this->MenuOrder = 5;
43  $this->Dependency = array("browse");
44  $this->DBaccess = PLUGIN_DB_WRITE;
45 
46  global $container;
47  $this->showJobsDao = $container->get('dao.show_jobs');
48  $this->uploadDao = $container->get('dao.upload');
49  $this->jobDao = $container->get('dao.job');
50 
51  parent::__construct();
52  }
53 
54  function RegisterMenus()
55  {
56  menu_insert("Main::Jobs::My Recent Jobs", $this->MenuOrder - 1, $this->Name,
57  $this->MenuTarget);
58 
59  if ($_SESSION[Auth::USER_LEVEL] == PLUGIN_DB_ADMIN) {
60  $URIpart = $this->Name . Traceback_parm_keep(array(
61  "page"
62  )) . "&allusers=";
63 
64  menu_insert("Main::Jobs::All Recent Jobs", $this->MenuOrder - 2,
65  $URIpart . '1', $this->MenuTarget);
66 
67  if (GetParm("mod", PARM_STRING) == $this->Name) {
68  /* Set micro menu to select either all users or this user */
69  $allusers = GetParm("allusers", PARM_INTEGER);
70  if ($allusers == 0) {
71  $text = _("Show uploads from all users");
72  $URI = $URIpart . "1";
73  } else {
74  $text = _("Show only your own uploads");
75  $URI = $URIpart . "0";
76  }
77  menu_insert("showjobs::$text", 1, $URI, $text);
78  }
79  }
80 
81  } // RegisterMenus()
82 
88  function getUploadNameForGeekyScan($job_pk)
89  {
90  $row = $this->showJobsDao->getDataForASingleJob($job_pk);
91 
92  if (empty($row["job_upload_fk"])) {
93  return '';
94  }
95 
96  if (empty($row['jq_pk'])) {
97  return _("Job history record is no longer available");
98  }
99 
100  /* get the upload filename */
101  $uploadFileName = htmlspecialchars($this->uploadDao->getUpload($row['job_upload_fk'])->getFilename());
102  if (empty($uploadFileName)) {
103  /* upload has been deleted so try to get the job name from the original upload job record */
104  $jobName = $this->showJobsDao->getJobName($row["job_upload_fk"]);
105  $uploadFileName = "Deleted " . $jobName;
106  }
107 
108  $uploadtree_pk = -1;
109  /* Find the uploadtree_pk for this upload so that it can be used in the browse link */
110  try {
111  $uploadtree_pk = $this->uploadDao->getUploadParent($row['job_upload_fk']);
112  } catch (Exception $e) {
113  echo $e->getMessage(), "\n";
114  }
115 
116  /* upload file name link to browse */
117  $uploadNameLink = "<a title='Click to browse this upload' href='" . Traceback_uri() . "?mod=browse&upload=" . $row['job_upload_fk'] . "&item=" . $uploadtree_pk . "'>" . $uploadFileName . "</a>";
118  return $uploadNameLink;
119  } // getUploadNameForGeekyScan()
120 
121  public function Output()
122  {
123  $page = "";
124  $uploadPk = GetParm('upload', PARM_INTEGER);
125  if (empty($uploadPk)) {
126  $uploadPk = - 1;
127  } elseif ($uploadPk > 0) {
128  if (! $this->uploadDao->isEditable($uploadPk, Auth::getGroupId())) {
129  $this->vars['message'] = _("Permission Denied");
130  return;
131  }
132  }
133  $this->vars['uploadId']= $uploadPk;
134 
135  /* Process any actions */
136  $action = GetParm("action",PARM_STRING);
137  $page = GetParm('page',PARM_INTEGER) ?: 0;
138  if ($_SESSION[Auth::USER_LEVEL] >= PLUGIN_DB_WRITE && !empty($action)) {
139  $jq_pk = GetParm("jobid",PARM_INTEGER);
140  $uploadPk = GetParm("upload",PARM_INTEGER);
141 
142  if (!($uploadPk === -1 &&
143  ($_SESSION[Auth::USER_LEVEL] >= PLUGIN_DB_ADMIN ||
144  $this->jobDao->hasActionPermissionsOnJob($jq_pk, Auth::getUserId(), Auth::getGroupId()))) &&
145  !$this->uploadDao->isEditable($uploadPk, Auth::getGroupId())) {
146  $this->vars['message'] = _("Permission Denied to perform action");
147  } else {
148  $thisURL = Traceback_uri() . "?mod=" . $this->Name . "&upload=$uploadPk";
149  switch($action) {
150  case 'pause':
151  if (empty($jq_pk)) {
152  break;
153  }
154  $command = "pause $jq_pk";
155  $rv = fo_communicate_with_scheduler($command, $response_from_scheduler, $error_info);
156  if ($rv == false) {
157  $this->vars['errorInfo'] = _("Unable to pause job.") . " " . $response_from_scheduler . $error_info;
158  }
159  echo "<script type=\"text/javascript\"> window.location.replace(\"$thisURL\"); </script>";
160  break;
161 
162  case 'restart':
163  if (empty($jq_pk)) {
164  break;
165  }
166  $command = "restart $jq_pk";
167  $rv = fo_communicate_with_scheduler($command, $response_from_scheduler, $error_info);
168  if ($rv == false) {
169  $this->vars['errorInfo'] = _("Unable to restart job.") . " " . $response_from_scheduler . $error_info;
170  }
171  echo "<script type=\"text/javascript\"> window.location.replace(\"$thisURL\"); </script>";
172  break;
173 
174  case 'cancel':
175  if (empty($jq_pk)) {
176  break;
177  }
178  $Msg = "\"" . _("Killed by") . " " . $_SESSION[Auth::USER_NAME] . "\"";
179  $command = "kill $jq_pk $Msg";
180  $rv = fo_communicate_with_scheduler($command, $response_from_scheduler, $error_info);
181  if ($rv == false) {
182  $this->vars['errorInfo'] = _("Unable to cancel job.") . $response_from_scheduler . $error_info;
183  }
184  echo "<script type=\"text/javascript\"> window.location.replace(\"$thisURL\"); </script>";
185  break;
186  }
187  }
188  }
189  $job = GetParm('job', PARM_INTEGER);
190  if (! empty($job)) {
191  $this->vars['jobId'] = $job;
192  $this->vars['uploadName'] = $this->getUploadNameForGeekyScan($job);
193  } else {
194  $allusersval = GetParm("allusers", PARM_INTEGER);
195  if (! $allusersval) {
196  $allusersval = 0;
197  }
198  $this->vars['allusersval'] = $allusersval;
199  if (! $page) {
200  $page = 0;
201  }
202  $this->vars['page'] = $page;
203  $this->vars['clockTime'] = $this->getTimeToRefresh();
204  $this->vars['allusersdiv'] = menu_to_1html(
205  menu_find($this->Name, $MenuDepth), 0);
206  $this->vars['injectedFoot'] = GetParm("injectedFoot",PARM_TEXT);
207  $this->vars['message'] = GetParm("injectedMessage",PARM_TEXT);
208  }
209  }
210 
215  public function getTimeToRefresh()
216  {
217  global $SysConf;
218  return $SysConf['SYSCONFIG']['ShowJobsAutoRefresh'];
219  } /* getTimeToRefresh() */
220 
221  public function getTemplateName()
222  {
223  $job = GetParm('job', PARM_INTEGER);
224  if (empty($job)) {
225  return "ui-showjobs.html.twig";
226  } else {
227  return "ui-job-show.html.twig";
228  }
229  }
230 }
231 
232 $NewPlugin = new showjobs;
233 $NewPlugin->Initialize();
Traceback_uri()
Get the URI without query to this location.
const PARM_TEXT
Definition: common-parm.php:31
#define PLUGIN_DB_ADMIN
Plugin requires admin level permission on DB.
Definition: libfossology.h:51
menu_find($Name, &$MaxDepth, $Menu=NULL)
Given a top-level menu name, find the list of sub-menus below it and max depth of menu...
getTimeToRefresh()
getTimeToRefresh() get the refresh time from DB. time in seconds to refresh the jobs.
Definition: showjobs.php:215
GetParm($parameterName, $parameterType)
This function will retrieve the variables and check data types.
Definition: common-parm.php:57
getUploadNameForGeekyScan($job_pk)
Returns uploadname as link for geeky scan.
Definition: showjobs.php:88
menu_to_1html($Menu, $ShowRefresh=1, $ShowTraceback=0, $ShowAll=1)
Take a menu and render it as one HTML line.
fo_communicate_with_scheduler($input, &$output, &$error_msg)
Communicate with scheduler, send commands to the scheduler, then get the output.
const PARM_STRING
Definition: common-parm.php:29
#define PLUGIN_DB_WRITE
Plugin requires write permission on DB.
Definition: libfossology.h:50
const PARM_INTEGER
Definition: common-parm.php:25
This is the Plugin class. All plugins should:
Definition: FO_Plugin.php:67
menu_insert($Path, $LastOrder=0, $URI=NULL, $Title=NULL, $Target=NULL, $HTML=NULL)
Given a Path, order level for the last item, and optional plugin name, insert the menu item...
Traceback_parm_keep($List)
Create a new URI, keeping only these items.