FOSSology  4.4.0
Open Source License Compliance by Open Source Software
common-agents.php
Go to the documentation of this file.
1 <?php
2 /*
3  SPDX-FileCopyrightText: © 2008-2012 Hewlett-Packard Development Company, L.P.
4  SPDX-FileCopyrightText: © 2018 Siemens AG
5 
6  SPDX-License-Identifier: LGPL-2.1-only
7 */
8 
40 function AgentCheckBoxMake($upload_pk,$SkipAgents=array(), $specified_username = "")
41 {
42 
43  global $Plugins;
44  global $PG_CONN;
45 
46  $AgentList = menu_find("Agents",$Depth);
47  $V = "";
48 
49  if (!empty($AgentList)) {
50  // get user agent preferences
51  $userName = $_SESSION['User'];
52  if (!empty($specified_username)) {
53  $userName = $specified_username;
54  }
55  $sql = "SELECT user_name, user_agent_list, default_bucketpool_fk FROM users WHERE
56  user_name='$userName';";
57  $result = pg_query($PG_CONN, $sql);
58  DBCheckResult($result, $sql, __FILE__, __LINE__);
59  $uList = pg_fetch_all($result);
60  pg_free_result($result);
61  // Ulist should never be empty, if it is, something really wrong,
62  // like the user_agent_list column is missing.
63  if (empty($uList)) {
64  $text = _("Fatal! Query Failed getting user_agent_list for user");
65  return("<h3 style='color:red'>$text $userName</h3>");
66  }
67  $list = explode(',',$uList[0]['user_agent_list']);
68  $default_bucketpool_fk = $uList[0]['default_bucketpool_fk'];
69  if (empty($default_bucketpool_fk)) {
70  $SkipAgents[] = "agent_bucket";
71  }
72 
73  foreach ($AgentList as $AgentItem) {
74  $Agent = &$Plugins[plugin_find_id($AgentItem->URI)];
75  if (empty($Agent)) {
76  continue;
77  }
78 
79  // ignore agents to skip from list
80  $FoundSkip = false;
81  foreach ($SkipAgents as $SkipAgent) {
82  if ($Agent->Name == $SkipAgent) {
83  $FoundSkip = true;
84  break;
85  }
86  }
87  if ($FoundSkip) {
88  continue;
89  }
90 
91  if ($upload_pk != -1) {
92  $rc = $Agent->AgentHasResults($upload_pk);
93  } else {
94  $rc = 0;
95  }
96  if ($rc != 1) {
97  $Name = htmlentities($Agent->Name);
98  $Desc = $AgentItem->Name;
99 
100  // display user agent preferences
101 
102  if (in_array($Name, $list)) {
103  $Selected = " checked ";
104  } else {
105  $Selected = "";
106  }
107  $V .= "<input type='checkbox' class='browse-upload-checkbox view-license-rc-size' name='Check_$Name' value='1' $Selected /> $Desc<br />\n";
108  }
109  }
110  }
111  return($V);
112 } // AgentCheckBoxMake()
113 
121 function AgentCheckBoxDo($job_pk, $upload_pk)
122 {
123  $agents = checkedAgents();
124  return AgentSchedule($job_pk, $upload_pk, $agents);
125 }
126 
127 
137 function AgentSchedule($jobId, $uploadId, $agents)
138 {
139  $errorMsg = "";
140  foreach ($agents as &$agent) {
141  $rv = $agent->AgentAdd($jobId, $uploadId, $errorMsg, array());
142  if ($rv == -1) {
143  return $errorMsg;
144  }
145  }
146  return null;
147 }
148 
157 function FindDependent($UploadPk, $list=NULL)
158 {
159  /*
160  * Find the jobs that fo_notify should depend on. fo_notify is
161  * dependent on the following agents:
162  * copyright
163  * nomos
164  * package
165  * bucket
166  *
167  * Determine if the above agents are scheduled and create a list of
168  * jq_pk for each agent.
169  *
170  */
171  global $PG_CONN;
172 
173  $Depends = array();
174  /* get job list for this upload */
175 
176  // get the list of jobs for this upload
177  $sql = "SELECT job_upload_fk, job_pk, job_name FROM job WHERE " .
178  "job_upload_fk = $UploadPk order by job_pk desc;";
179  $result = pg_query($PG_CONN, $sql);
180  DBCheckResult($result, $sql, __FILE__, __LINE__);
181  $Jobs = pg_fetch_all($result);
182  pg_free_result($result);
183 
184  $jobList = array();
185  foreach ($Jobs as $Row) {
186  if ($Row['job_name'] == 'Copyright Analysis') {
187  $jobList[] = $Row['job_pk'];
188  } elseif ($Row['job_name'] == 'Bucket Analysis') {
189  $jobList[] = $Row['job_pk'];
190  } elseif ($Row['job_name'] == 'Package Agents') {
191  $jobList[] = $Row['job_pk'];
192  } elseif ($Row['job_name'] == 'Nomos License Analysis') {
193  $jobList[] = $Row['job_pk'];
194  }
195  }
196 
197  // get the jq_pk's for each job, retrun the list of jq_pk's
198  foreach ($jobList as $job) {
199  $sql = "SELECT jq_pk, jq_job_fk FROM jobqueue WHERE jq_job_fk = $job " .
200  " order by jq_pk desc;";
201  $result = pg_query($PG_CONN, $sql);
202  DBCheckResult($result, $sql, __FILE__, __LINE__);
203  $Q = pg_fetch_all($result);
204  pg_free_result($result);
205  $Depends[] = $Q[0]['jq_pk'];
206  }
207  return($Depends);
208 } // FindDependent
209 
223 function GetAgentKey($agentName, $agentDesc)
224 {
225  global $PG_CONN;
226 
227  /* get the exact agent rec requested */
228  $sqlselect = "SELECT agent_pk FROM agent WHERE agent_name ='$agentName' "
229  . "and agent_enabled='true' order by agent_ts desc limit 1";
230  $result = pg_query($PG_CONN, $sqlselect);
231  DBCheckResult($result, $sqlselect, __FILE__, __LINE__);
232 
233  if (pg_num_rows($result) == 0) {
234  /* no match, so add an agent rec */
235  $sql = "INSERT INTO agent (agent_name,agent_desc,agent_enabled) VALUES ('$agentName',E'$agentDesc',1)";
236  $result = pg_query($PG_CONN, $sqlselect);
237  DBCheckResult($result, $sql, __FILE__, __LINE__);
238 
239  /* get inserted agent_pk */
240  $result = pg_query($PG_CONN, $sqlselect);
241  DBCheckResult($result, $sqlselect, __FILE__, __LINE__);
242  }
243 
244  $row = pg_fetch_assoc($result);
245  pg_free_result($result);
246  return $row["agent_pk"];
247 
248 } // GetAgentKey
249 
250 
272 function AgentARSList($TableName, $upload_pk, $limit=1, $agent_fk=0, $ExtraWhere="")
273 {
274  global $PG_CONN;
275  global $container;
277  $agentDao = $container->get('dao.agent');
278  return $agentDao->agentARSList($TableName, $upload_pk, $limit, $agent_fk, $ExtraWhere);
279 }
280 
281 
291 function LatestAgentpk($upload_pk, $arsTableName, $arsSuccess = false)
292 {
293  $AgentRec = AgentARSList($arsTableName, $upload_pk, 1, 0, $arsSuccess);
294 
295  if (empty($AgentRec)) {
296  $Agent_pk = 0;
297  } else {
298  $Agent_pk = intval($AgentRec[0]['agent_fk']);
299  }
300  return $Agent_pk;
301 }
302 
303 
320 function AgentSelect($TableName, $upload_pk, $SLName, &$agent_pk, $extra = "")
321 {
322  global $PG_CONN;
323  /* get the agent recs */
324  $TableName .= '_ars';
325  $sql = "select agent_pk, agent_name, agent_rev from agent, $TableName where "
326  . "agent.agent_pk = $TableName.agent_fk and upload_fk = $upload_pk order by agent_rev DESC";
327  $result = pg_query($PG_CONN, $sql);
328  DBCheckResult($result, $sql, __FILE__, __LINE__);
329 
330  $NumRows = pg_num_rows($result);
331  if ($NumRows == 1) { // only one result
332  pg_free_result($result);
333  return; /* only one result */
334  }
335 
336  $select = "<select name='$SLName' id='$SLName' $extra>";
337  while ($row = pg_fetch_assoc($result)) {
338  $select .= "<option value='$row[agent_pk]'";
339 
340  if (empty($agent_pk)) {
341  $select .= " SELECTED ";
342  $agent_pk = $row["agent_pk"];
343  } else if (in_array($row['agent_pk'], $agent_pk)) {
344  $select .= " SELECTED ";
345  }
346 
347  $select .= ">$row[agent_name], v $row[agent_rev]\n";
348  }
349  $select .= "</select>";
350  pg_free_result($result);
351  return $select;
352 }
353 
354 
361 function userAgents($agents=null)
362 {
363  return implode(',', array_keys(checkedAgents($agents)));
364 }
365 
372 function checkedAgents($agents=null)
373 {
374  $agentsChecked = array();
375  $agentList = listAgents();
376  foreach ($agentList as $agentName => &$agentPlugin) {
377  if (is_null($agents)) {
378  if (GetParm("Check_" . $agentName, PARM_INTEGER) == 1) {
379  $agentsChecked[$agentName] = &$agentPlugin;
380  }
381  } else {
382  if ($agents["Check_" . $agentName] == 1) {
383  $agentsChecked[$agentName] = &$agentPlugin;
384  }
385  }
386  }
387  unset($agentPlugin);
388 
389  return $agentsChecked;
390 }
391 
397 function listAgents()
398 {
399  $agents = array();
400 
401  $agentList = menu_find("Agents",$Depth);
402  if (!empty($agentList)) {
403  foreach ($agentList as $agentItem) {
404  /*
405  The URI below contains the agent name e.g agent_license, this is
406  not be confused with the Name attribute in the class, for example,
407  the Name attribute for agent_license is: Schedule License Analysis
408  */
409  $agentPlugin = plugin_find($agentItem->URI);
410  if (empty($agentPlugin)) {
411  continue;
412  }
413  $name = htmlentities($agentPlugin->Name);
414  $agents[$name] = $agentPlugin;
415  }
416  }
417  return $agents;
418 }
432 function CheckARS($upload_pk, $AgentName, $AgentDesc, $AgentARSTableName)
433 {
434  /* get the latest agent_pk */
435  $Latest_agent_pk = GetAgentKey($AgentName, $AgentDesc);
436 
437  /* get last agent pk with successful results */
438  $Last_successful_agent_pk = LatestAgentpk($upload_pk, $AgentARSTableName, true);
439 
440  if (! empty($Latest_agent_pk) && ! empty($Last_successful_agent_pk)) {
441  if ($Latest_agent_pk == $Last_successful_agent_pk) {
442  return 1;
443  } else {
444  return 2;
445  }
446  }
447 
448  return 0;
449 } // CheckARS()
AgentSchedule($jobId, $uploadId, $agents)
Schedule all given agents.
AgentCheckBoxDo($job_pk, $upload_pk)
Assume someone called AgentCheckBoxMake() and submitted the HTML form. Run AgentAdd() for each of the...
checkedAgents($agents=null)
read the UI form and return array of user selected agents Because input comes from the user,...
GetAgentKey($agentName, $agentDesc)
Get the latest enabled agent_pk for a given agent.
userAgents($agents=null)
Read the UI form and format the user selected agents into a comma separated list.
AgentCheckBoxMake($upload_pk, $SkipAgents=array(), $specified_username="")
Generate a checkbox list of available agents.
listAgents()
Search in available plugins and return all agents.
AgentSelect($TableName, $upload_pk, $SLName, &$agent_pk, $extra="")
LatestAgentpk($upload_pk, $arsTableName, $arsSuccess=false)
Given an upload_pk, find the latest enabled agent_pk with results.
FindDependent($UploadPk, $list=NULL)
Find the jobs in the job and jobqueue table to be dependent on.
CheckARS($upload_pk, $AgentName, $AgentDesc, $AgentARSTableName)
Check the ARS table to see if an agent has successfully scanned an upload.
DBCheckResult($result, $sql, $filenm, $lineno)
Check the postgres result for unexpected errors. If found, treat them as fatal.
Definition: common-db.php:187
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.
const PARM_INTEGER
Definition: common-parm.php:14
GetParm($parameterName, $parameterType)
This function will retrieve the variables and check data types.
Definition: common-parm.php:46
plugin_find($pluginName)
Given the official name of a plugin, return the $Plugins object.
foreach($Options as $Option=> $OptVal) if(0==$reference_flag &&0==$nomos_flag) $PG_CONN