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