FOSSology  4.4.0
Open Source License Compliance by Open Source Software
fossjobs.php
Go to the documentation of this file.
1 <?php
2 /*
3  SPDX-FileCopyrightText: © 2008-2012 Hewlett-Packard Development Company, L.P.
4 
5  SPDX-License-Identifier: GPL-2.0-only
6 */
7 
10 
35 /**********************************************************************
36  **********************************************************************
37 SUPPORT FUNCTIONS
38 **********************************************************************
39 **********************************************************************/
48 function list_agents()
49 {
50  $agent_list = menu_find("Agents", $depth);
51  return ($agent_list);
52 }
53 
58 require_once("$MODDIR/lib/php/common-cli.php");
59 
60 global $Plugins;
61 global $PERM_NAMES;
62 global $SysConf;
63 global $PG_CONN;
64 
65 /**********************************************************************
66  **********************************************************************
67 INITIALIZE THIS INTERFACE
68 **********************************************************************
69 **********************************************************************/
70 $usage = basename($argv[0]) . " [options]
71  Options:
72  -h :: help, this message
73  -v :: verbose output
74  -a :: list available agent tasks
75  -A string :: specify agent to schedule (default is everything from -a)
76  The string can be a comma-separated list of agent tasks.
77  -u :: list available upload ids
78  -U upload :: the upload identifier for scheduling agent tasks
79  The string can be a comma-separated list of upload ids.
80  Or, use 'ALL' to specify all upload ids.
81  -D upload :: the upload identifier for scheduling delete tasks
82  The string can either be 'ALL', a string (the upload_pk),
83  or an array of upload_pk's if multiple -D's were specified.
84  --username string :: user name
85  --password string :: password
86  --groupname string :: group name
87  -c string :: Specify the directory for the system configuration
88 ";
89 //process parameters, see usage above
90 $longopts = array("username:", "password:", "groupname:");
91 $options = getopt("c:haA:P:uU:D:v", $longopts);
92 //print_r($options);
93 if (empty($options)) {
94  echo $usage;
95  exit(1);
96 }
97 if (array_key_exists("h", $options)) {
98  echo $usage;
99  exit(0);
100 }
101 
102 /**********************************************************************
103  **********************************************************************
104 PROCESS COMMAND LINE SELECTION
105 **********************************************************************
106 **********************************************************************/
107 $user = "";
108 $passwd = "";
109 $group = "";
110 if (array_key_exists("username", $options)) {
111  $user = $options["username"];
112 }
113 
114 if (array_key_exists("groupname", $options)) {
115  $group = $options["groupname"];
116 }
117 
118 if (array_key_exists("password", $options)) {
119  $passwd = $options["password"];
120 }
121 
122 account_check($user, $passwd, $group); // check username/password
123 $groupId = Auth::getGroupId();
124 
125 /* init plugins */
126 cli_Init();
127 
128 $Verbose = 0;
129 if (array_key_exists("v", $options)) {
130  $Verbose = 1;
131 }
132 
133 // Get the list of registered agents
134 $agent_list = list_agents();
135 if (empty($agent_list)) {
136  echo "ERROR! could not get list of agents\n";
137  echo "Are Plugins configured?\n";
138  exit(1);
139 }
140 
141 /* List available agents */
142 if (array_key_exists("a", $options)) {
143  if (empty($agent_list)) {
144  echo "No agents configured\n";
145  } else {
146  echo "The available agents are:\n";
147  $agent_count = count($agent_list);
148  for ($ac = 0;$ac < $agent_count;$ac++) {
149  $agent = ($agent_list[$ac]->URI);
150  if (!empty($agent)) {
151  echo " $agent\n";
152  }
153  }
154  }
155 }
156 
157 /* Hide agents that aren't related to data scans */
158 $Skip = array("agent_unpack", "agent_adj2nest", "wget_agent");
159 for ($ac=0; !empty($agent_list[$ac]->URI); $ac++) {
160  if (array_search($agent_list[$ac]->URI, $Skip) !== false) {
161  unset($agent_list[$ac]);
162  }
163 }
164 
165 /* If the user specified a list, then disable every agent not in the list */
166 $Skip = array("agent_unpack", "agent_adj2nest", "wget_agent");
167 if (array_key_exists("A", $options)) {
168  $agent_count = count($agent_list);
169  for ($ac = 0;$ac < $agent_count;$ac++) {
170  $Found = 0;
171  foreach (explode(',', $options["A"]) as $Val) {
172  if (!strcmp($Val, $agent_list[$ac]->URI)) {
173  $Found = 1;
174  }
175  }
176  if ($Found == 0) {
177  $agent_list[$ac]->URI = null;
178  }
179  }
180 }
181 
182 /* List available uploads */
183 if (array_key_exists("u", $options)) {
184  $root_folder_pk = GetUserRootFolder();
185  $FolderPath = null;
186  $FolderList = FolderListUploadsRecurse($root_folder_pk, $FolderPath, Auth::PERM_WRITE);
187 
188  print "# The following uploads are available (upload id: name)\n";
189  foreach ($FolderList as $Folder) {
190  $Label = $Folder['name'] . " (" . $Folder['upload_desc'] . ')';
191  print $Folder['upload_pk'] . ": $Label\n";
192  }
193  exit(0);
194 }
195 
196 /* @var $uploadDao UploadDao */
197 $uploadDao = $GLOBALS['container']->get('dao.upload');
198 
199 if (array_key_exists("U", $options)) {
200  /* $options['U'] can either be 'ALL', a string (the upload_pk),
201  or an array of upload_pk's if multiple -U's were specified.
202  */
203  $upload_options = $options['U'];
204  $upload_pk_array = array();
205  if ($upload_options == 'ALL') {
206  $SQL = "SELECT upload_pk,upload_desc,upload_filename FROM upload ORDER BY upload_pk;";
207  $result = pg_query($PG_CONN, $SQL);
208  DBCheckResult($result, $SQL, __FILE__, __LINE__);
209  while ($row = pg_fetch_assoc($result) && !empty($row['upload_pk'])) {
210  $upload_pk_array[] = $row['upload_pk'];
211  }
212  pg_free_result($result);
213  } else if (is_array($upload_options)) {
214  $upload_pk_array = $upload_options;
215  } else {
216  $upload_pk_array[] = $upload_options;
217  }
218 
219  /* check permissions */
220  $checked_list = array();
221  foreach ($upload_pk_array as $upload_pk) {
222  if (!$uploadDao->isEditable($upload_pk, $groupId)) {
223  print "You have no permission to queue agents for upload $upload_pk in group $groupId\n";
224  continue;
225  }
226  $checked_list[] = $upload_pk;
227  }
228  $checked_list_str = implode(",", $checked_list);
229 
231  QueueUploadsOnAgents($checked_list_str, $agent_list, $Verbose);
232 }
233 
234 if (array_key_exists("D", $options)) {
235  /* $options['D'] can either be 'ALL', a string (the upload_pk),
236  or an array of upload_pk's if multiple -D's were specified.
237  */
238  $upload_options = $options['D'];
239  $upload_pk_array = array();
240  if ($upload_options == 'ALL') {
241  $SQL = "SELECT upload_pk,upload_desc,upload_filename FROM upload ORDER BY upload_pk;";
242  $result = pg_query($PG_CONN, $SQL);
243  DBCheckResult($result, $SQL, __FILE__, __LINE__);
244  while ($row = pg_fetch_assoc($result) && !empty($row['upload_pk'])) {
245  $upload_pk_array[] = $row['upload_pk'];
246  }
247  pg_free_result($result);
248  } else if (is_array($upload_options)) {
249  $upload_pk_array = $upload_options;
250  } else {
251  $upload_pk_array[] = $upload_options;
252  }
253  /* check permissions */
254  $checked_list = array();
255  foreach ($upload_pk_array as $upload_pk) {
256  if (!$uploadDao->isEditable($upload_pk, $groupId)) {
257  print "You have no permission to delete upload " . $upload_pk . "\n";
258  continue;
259  }
260  $checked_list[] = $upload_pk;
261  }
262  $checked_list_str = implode(",", $checked_list);
263 
265  QueueUploadsOnDelagents($checked_list_str);
266 }
Contains the constants and helpers for authentication of user.
Definition: Auth.php:24
account_check(&$user, &$passwd, &$group="")
check if this account is correct
Definition: common-auth.php:75
cli_Init()
Initialize the fossology environment for CLI use. This routine loads the plugins so they can be use b...
Definition: common-cli.php:25
DBCheckResult($result, $sql, $filenm, $lineno)
Check the postgres result for unexpected errors. If found, treat them as fatal.
Definition: common-db.php:187
GetUserRootFolder()
Get the top-of-tree folder_pk for the current user. Fail if there is no user session.
FolderListUploadsRecurse($ParentFolder=-1, $FolderPath='', $perm=Auth::PERM_READ)
Get uploads and folder info, starting from $ParentFolder.
QueueUploadsOnDelagents($upload_pk_list)
Schedule delagent on upload ids.
Definition: common-job.php:333
QueueUploadsOnAgents($upload_pk_list, $agent_list, $Verbose)
Schedule agent tasks on upload ids.
Definition: common-job.php:268
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.
global $Plugins
Definition: fossjobs.php:58
list_agents()
list agents
Definition: fossjobs.php:48
#define PERM_WRITE
Read-Write permission.
Definition: libfossology.h:33
foreach($Options as $Option=> $OptVal) if(0==$reference_flag &&0==$nomos_flag) $PG_CONN