FOSSology  3.2.0rc1
Open Source License Compliance by Open Source Software
common-agents.php
Go to the documentation of this file.
1 <?php
2 /***********************************************************
3  Copyright (C) 2008-2012 Hewlett-Packard Development Company, L.P.
4  Copyright (C) 2018 Siemens AG
5 
6  This library is free software; you can redistribute it and/or
7  modify it under the terms of the GNU Lesser General Public
8  License version 2.1 as published by the Free Software Foundation.
9 
10  This library 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 GNU
13  Lesser General Public License for more details.
14 
15  You should have received a copy of the GNU Lesser General Public License
16  along with this library; if not, write to the Free Software Foundation, Inc.0
17  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18  ***********************************************************/
19 
51 function AgentCheckBoxMake($upload_pk,$SkipAgents=array(), $specified_username = "")
52 {
53 
54  global $Plugins;
55  global $PG_CONN;
56 
57  $AgentList = menu_find("Agents",$Depth);
58  $V = "";
59 
60  if (!empty($AgentList)) {
61  // get user agent preferences
62  $userName = $_SESSION['User'];
63  if (!empty($specified_username)) {
64  $userName = $specified_username;
65  }
66  $sql = "SELECT user_name, user_agent_list, default_bucketpool_fk FROM users WHERE
67  user_name='$userName';";
68  $result = pg_query($PG_CONN, $sql);
69  DBCheckResult($result, $sql, __FILE__, __LINE__);
70  $uList = pg_fetch_all($result);
71  pg_free_result($result);
72  // Ulist should never be empty, if it is, something really wrong,
73  // like the user_agent_list column is missing.
74  if (empty($uList)) {
75  $text = _("Fatal! Query Failed getting user_agent_list for user");
76  return("<h3 style='color:red'>$text $userName</h3>");
77  }
78  $list = explode(',',$uList[0]['user_agent_list']);
79  $default_bucketpool_fk = $uList[0]['default_bucketpool_fk'];
80  if (empty($default_bucketpool_fk)) {
81  $SkipAgents[] = "agent_bucket";
82  }
83 
84  foreach ($AgentList as $AgentItem) {
85  $Agent = &$Plugins[plugin_find_id($AgentItem->URI)];
86  if (empty($Agent)) {
87  continue;
88  }
89 
90  // ignore agents to skip from list
91  $FoundSkip = false;
92  foreach ($SkipAgents as $SkipAgent) {
93  if ($Agent->Name == $SkipAgent) {
94  $FoundSkip = true;
95  break;
96  }
97  }
98  if ($FoundSkip) {
99  continue;
100  }
101 
102  if ($upload_pk != -1) {
103  $rc = $Agent->AgentHasResults($upload_pk);
104  } else {
105  $rc = 0;
106  }
107  if ($rc != 1) {
108  $Name = htmlentities($Agent->Name);
109  $Desc = htmlentities($AgentItem->Name);
110 
111  // display user agent preferences
112 
113  if (in_array($Name, $list)) {
114  $Selected = " checked ";
115  } else {
116  $Selected = "";
117  }
118  $V .= "<input type='checkbox' name='Check_$Name' value='1' $Selected />$Desc<br />\n";
119  }
120  }
121  }
122  return($V);
123 } // AgentCheckBoxMake()
124 
132 function AgentCheckBoxDo($job_pk, $upload_pk)
133 {
134  $agents = checkedAgents();
135  return AgentSchedule($job_pk, $upload_pk, $agents);
136 }
137 
138 
148 function AgentSchedule($jobId, $uploadId, $agents)
149 {
150  $errorMsg = "";
151  foreach ($agents as &$agent) {
152  $rv = $agent->AgentAdd($jobId, $uploadId, $errorMsg, array());
153  if ($rv == -1) {
154  return $errorMsg;
155  }
156  }
157  return null;
158 }
159 
168 function FindDependent($UploadPk, $list=NULL)
169 {
170  /*
171  * Find the jobs that fo_notify should depend on. fo_notify is
172  * dependent on the following agents:
173  * copyright
174  * nomos
175  * package
176  * bucket
177  *
178  * Determine if the above agents are scheduled and create a list of
179  * jq_pk for each agent.
180  *
181  */
182  global $PG_CONN;
183 
184  $Depends = array();
185  /* get job list for this upload */
186 
187  // get the list of jobs for this upload
188  $sql = "SELECT job_upload_fk, job_pk, job_name FROM job WHERE " .
189  "job_upload_fk = $UploadPk order by job_pk desc;";
190  $result = pg_query($PG_CONN, $sql);
191  DBCheckResult($result, $sql, __FILE__, __LINE__);
192  $Jobs = pg_fetch_all($result);
193  pg_free_result($result);
194 
195  $jobList = array();
196  foreach ($Jobs as $Row) {
197  if ($Row['job_name'] == 'Copyright Analysis') {
198  $jobList[] = $Row['job_pk'];
199  } elseif ($Row['job_name'] == 'Bucket Analysis') {
200  $jobList[] = $Row['job_pk'];
201  } elseif ($Row['job_name'] == 'Package Agents') {
202  $jobList[] = $Row['job_pk'];
203  } elseif ($Row['job_name'] == 'Nomos License Analysis') {
204  $jobList[] = $Row['job_pk'];
205  }
206  }
207 
208  // get the jq_pk's for each job, retrun the list of jq_pk's
209  foreach ($jobList as $job) {
210  $sql = "SELECT jq_pk, jq_job_fk FROM jobqueue WHERE jq_job_fk = $job " .
211  " order by jq_pk desc;";
212  $result = pg_query($PG_CONN, $sql);
213  DBCheckResult($result, $sql, __FILE__, __LINE__);
214  $Q = pg_fetch_all($result);
215  pg_free_result($result);
216  $Depends[] = $Q[0]['jq_pk'];
217  }
218  return($Depends);
219 } // FindDependent
220 
234 function GetAgentKey($agentName, $agentDesc)
235 {
236  global $PG_CONN;
237 
238  /* get the exact agent rec requested */
239  $sqlselect = "SELECT agent_pk FROM agent WHERE agent_name ='$agentName' "
240  . "and agent_enabled='true' order by agent_ts desc limit 1";
241  $result = pg_query($PG_CONN, $sqlselect);
242  DBCheckResult($result, $sqlselect, __FILE__, __LINE__);
243 
244  if (pg_num_rows($result) == 0) {
245  /* no match, so add an agent rec */
246  $sql = "INSERT INTO agent (agent_name,agent_desc,agent_enabled) VALUES ('$agentName',E'$agentDesc',1)";
247  $result = pg_query($PG_CONN, $sqlselect);
248  DBCheckResult($result, $sql, __FILE__, __LINE__);
249 
250  /* get inserted agent_pk */
251  $result = pg_query($PG_CONN, $sqlselect);
252  DBCheckResult($result, $sqlselect, __FILE__, __LINE__);
253  }
254 
255  $row = pg_fetch_assoc($result);
256  pg_free_result($result);
257  return $row["agent_pk"];
258 
259 } // GetAgentKey
260 
261 
283 function AgentARSList($TableName, $upload_pk, $limit=1, $agent_fk=0, $ExtraWhere="")
284 {
285  global $PG_CONN;
286  global $container;
288  $agentDao = $container->get('dao.agent');
289  return $agentDao->agentARSList($TableName, $upload_pk, $limit, $agent_fk, $ExtraWhere);
290 }
291 
292 
302 function LatestAgentpk($upload_pk, $arsTableName, $arsSuccess = false)
303 {
304  $AgentRec = AgentARSList($arsTableName, $upload_pk, 1, 0, $arsSuccess);
305 
306  if (empty($AgentRec)) {
307  $Agent_pk = 0;
308  } else {
309  $Agent_pk = intval($AgentRec[0]['agent_fk']);
310  }
311  return $Agent_pk;
312 }
313 
314 
331 function AgentSelect($TableName, $upload_pk, $SLName, &$agent_pk, $extra = "")
332 {
333  global $PG_CONN;
334  /* get the agent recs */
335  $TableName .= '_ars';
336  $sql = "select agent_pk, agent_name, agent_rev from agent, $TableName where "
337  . "agent.agent_pk = $TableName.agent_fk and upload_fk = $upload_pk order by agent_rev DESC";
338  $result = pg_query($PG_CONN, $sql);
339  DBCheckResult($result, $sql, __FILE__, __LINE__);
340 
341  $NumRows = pg_num_rows($result);
342  if ($NumRows == 1) { // only one result
343  pg_free_result($result);
344  return; /* only one result */
345  }
346 
347  $select = "<select name='$SLName' id='$SLName' $extra>";
348  while ($row = pg_fetch_assoc($result)) {
349  $select .= "<option value='$row[agent_pk]'";
350 
351  if (empty($agent_pk)) {
352  $select .= " SELECTED ";
353  $agent_pk = $row["agent_pk"];
354  } else if ($agent_pk == $row['agent_pk']) {
355  $select .= " SELECTED ";
356  }
357 
358  $select .= ">$row[agent_name], v $row[agent_rev]\n";
359  }
360  $select .= "</select>";
361  pg_free_result($result);
362  return $select;
363 }
364 
365 
372 function userAgents()
373 {
374  return implode(',', array_keys(checkedAgents()));
375 }
376 
383 function checkedAgents()
384 {
385  $agentsChecked = array();
386  $agentList = listAgents();
387  foreach ($agentList as $agentName => &$agentPlugin) {
388  if (GetParm("Check_" . $agentName, PARM_INTEGER) == 1) {
389  $agentsChecked[$agentName] = &$agentPlugin;
390  }
391  }
392  unset($agentPlugin);
393 
394  return $agentsChecked;
395 }
396 
402 function listAgents()
403 {
404  $agents = array();
405 
406  $agentList = menu_find("Agents",$Depth);
407  if (!empty($agentList)) {
408  foreach ($agentList as $agentItem) {
409  /*
410  The URI below contains the agent name e.g agent_license, this is
411  not be confused with the Name attribute in the class, for example,
412  the Name attribute for agent_license is: Schedule License Analysis
413  */
414  $agentPlugin = plugin_find($agentItem->URI);
415  if (empty($agentPlugin)) {
416  continue;
417  }
418  $name = htmlentities($agentPlugin->Name);
419  $agents[$name] = $agentPlugin;
420  }
421  }
422  return $agents;
423 }
437 function CheckARS($upload_pk, $AgentName, $AgentDesc, $AgentARSTableName)
438 {
439  /* get the latest agent_pk */
440  $Latest_agent_pk = GetAgentKey($AgentName, $AgentDesc);
441 
442  /* get last agent pk with successful results */
443  $Last_successful_agent_pk = LatestAgentpk($upload_pk, $AgentARSTableName, true);
444 
445  if (! empty($Latest_agent_pk) && ! empty($Last_successful_agent_pk)) {
446  if ($Latest_agent_pk == $Last_successful_agent_pk) {
447  return 1;
448  } else {
449  return 2;
450  }
451  }
452 
453  return 0;
454 } // CheckARS()
AgentSchedule($jobId, $uploadId, $agents)
Schedule all given agents.
checkedAgents()
read the UI form and return array of user selected agents Because input comes from the user...
userAgents()
Read the UI form and format the user selected agents into a comma separated list. ...
AgentCheckBoxDo($job_pk, $upload_pk)
Assume someone called AgentCheckBoxMake() and submitted the HTML form. Run AgentAdd() for each of the...
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...
listAgents()
Search in available plugins and return all agents.
AgentCheckBoxMake($upload_pk, $SkipAgents=array(), $specified_username="")
Generate a checkbox list of available agents.
FindDependent($UploadPk, $list=NULL)
Find the jobs in the job and jobqueue table to be dependent on.
GetParm($parameterName, $parameterType)
This function will retrieve the variables and check data types.
Definition: common-parm.php:57
LatestAgentpk($upload_pk, $arsTableName, $arsSuccess=false)
Given an upload_pk, find the latest enabled agent_pk with results.
plugin_find($pluginName)
Given the official name of a plugin, return the $Plugins object.
CheckARS($upload_pk, $AgentName, $AgentDesc, $AgentARSTableName)
Check the ARS table to see if an agent has successfully scanned an upload.
AgentSelect($TableName, $upload_pk, $SLName, &$agent_pk, $extra="")
const PARM_INTEGER
Definition: common-parm.php:25
foreach($Options as $Option=> $OptVal) if(0==$reference_flag &&0==$nomos_flag) $PG_CONN
DBCheckResult($result, $sql, $filenm, $lineno)
Check the postgres result for unexpected errors. If found, treat them as fatal.
Definition: common-db.php:198
GetAgentKey($agentName, $agentDesc)
Get the latest enabled agent_pk for a given agent.