FOSSology  4.4.0
Open Source License Compliance by Open Source Software
demomod.php
Go to the documentation of this file.
1 <?php
2 /*
3  SPDX-FileCopyrightText: © 2013 Hewlett-Packard Development Company, L.P.
4 
5  SPDX-License-Identifier: GPL-2.0-only
6 */
7 
9 
15 define("TITLE_UI_DEMOMOD", _("Demomod View"));
16 
21 class ui_demomod extends FO_Plugin
22 {
23  var $Name = "demomod";
24  var $Title = TITLE_UI_DEMOMOD;
25  var $Dependency = array("browse");
28 
33  function Install()
34  {
35  global $PG_CONN;
36 
37  if (!$PG_CONN) {
38  return(1);
39  }
40 
41  return(0);
42  } // Install()
43 
47  function RegisterMenus()
48  {
49  // For all other menus, permit coming back here.
50  $URI = $this->Name . Traceback_parm_keep(array("upload","item"));
51  $MenuName = "Demomod View";
52 
53  $Item = GetParm("item",PARM_INTEGER);
54  $Upload = GetParm("upload",PARM_INTEGER);
55  if (!empty($Item))
56  {
57  $text = _("Demomod data");
58  menu_insert("Browse::$MenuName",100,$URI,$text);
59  menu_insert("View::$MenuName",100,$URI,$text);
60  }
61  } // RegisterMenus()
62 
63 
75  function Initialize()
76  {
77  if ($this->State != PLUGIN_STATE_INVALID) { return(1); // don't re-run
78  }
79 
80  return($this->State == PLUGIN_STATE_VALID);
81  } // Initialize()
82 
83 
90  function ShowData($upload_pk, $uploadtree_pk)
91  {
92  global $PG_CONN;
93 
94  /* Check the demomod_ars table to see if we have any data */
95  $sql = "select ars_pk from demomod_ars where upload_fk=$upload_pk and ars_success=true";
96  $result = pg_query($PG_CONN, $sql);
97  DBCheckResult($result, $sql, __FILE__, __LINE__);
98  $rows = pg_num_rows($result);
99  pg_free_result($result);
100 
101  if ($rows == 0) { return _("There is no demomod data for this upload. Use Jobs > Schedule Agent.");
102  }
103 
104  /* Get the scan result */
105  /* First we need the pfile_pk */
106  $sql = "select pfile_fk from $this->uploadtree_tablename where uploadtree_pk=$uploadtree_pk and upload_fk=$upload_pk";
107  $result = pg_query($PG_CONN, $sql);
108  DBCheckResult($result, $sql, __FILE__, __LINE__);
109  $rows = pg_num_rows($result);
110  if ($rows == 0) { return _("Internal consistency error. Failed: $sql");
111  }
112  $row = pg_fetch_assoc($result);
113  $pfile_fk = $row['pfile_fk'];
114  pg_free_result($result);
115 
116  /* Now we can get the scan result */
117  $sql = "select firstbytes from demomod where pfile_fk=$pfile_fk";
118  $result = pg_query($PG_CONN, $sql);
119  DBCheckResult($result, $sql, __FILE__, __LINE__);
120  $rows = pg_num_rows($result);
121  if ($rows == 0) { return _("Internal consistency error. Failed: $sql");
122  }
123  $row = pg_fetch_assoc($result);
124  $firstbytes = $row['firstbytes'];
125  pg_free_result($result);
126 
127  $text = _("The first bytes of this file are: ");
128  return ($text . $firstbytes);
129  }
130 
131 
135  function Output()
136  {
137  $uTime = microtime(true);
138  if ($this->State != PLUGIN_STATE_READY) {
139  return(0);
140  }
141  $V="";
142 
143  $Upload = GetParm("upload",PARM_INTEGER);
144  /* @var $uploadDao UploadDao */
145  $uploadDao = $GLOBALS['container']->get('dao.upload');
146  if (!$uploadDao->isAccessible($Upload, Fossology\Lib\Auth\Auth::getGroupId()))
147  {
148  $text = _("Permission Denied");
149  return "<h2>$text</h2>";
150  }
151 
152  $Item = GetParm("item",PARM_INTEGER);
153  $updcache = GetParm("updcache",PARM_INTEGER);
154 
155  /* Remove "updcache" from the GET args.
156  * This way all the url's based on the input args won't be
157  * polluted with updcache
158  * Use Traceback_parm_keep to ensure that all parameters are in order */
159  $CacheKey = "?mod=" . $this->Name . Traceback_parm_keep(array("upload","item","agent"));
160  if ($updcache)
161  {
162  $_SERVER['REQUEST_URI'] = preg_replace("/&updcache=[0-9]*/","",$_SERVER['REQUEST_URI']);
163  unset($_GET['updcache']);
164  $V = ReportCachePurgeByKey($CacheKey);
165  }
166  else
167  {
168  $V = ReportCacheGet($CacheKey);
169  }
170 
172 
173  if (empty($V) ) // no cache exists
174  {
175  switch($this->OutputType)
176  {
177  case "XML":
178  break;
179  case "HTML":
180  $V .= "<font class='text'>\n";
181 
182  /************************/
183  /* Show the folder path */
184  /************************/
185  $V .= Dir2Browse($this->Name,$Item,NULL,1,"Browse", -1, '', '', $this->uploadtree_tablename) . "<P />\n";
186 
187  if (!empty($Upload))
188  {
189  $Uri = preg_replace("/&item=([0-9]*)/","",Traceback());
190  $V .= js_url();
191  $V .= $this->ShowData($Upload, $Item);
192  }
193  $V .= "</font>\n";
194  $V .= "<p>\n";
195  break;
196  case "Text":
197  break;
198  default:
199  }
200 
201  $Cached = false;
202  }
203  else {
204  $Cached = true;
205  }
206 
207  if (!$this->OutputToStdout) {
208  return($V);
209  }
210  print "$V";
211  $Time = microtime(true) - $uTime; // convert usecs to secs
212  $text = _("Elapsed time: %.2f seconds");
213  printf( "<small>$text</small>", $Time);
214 
215  if ($Cached)
216  {
217  $text = _("cached");
218  $text1 = _("Update");
219  echo " <i>$text</i> <a href=\"$_SERVER[REQUEST_URI]&updcache=1\"> $text1 </a>";
220  }
221  else
222  {
223  /* Cache Report if this took longer than 1/2 second*/
224  if ($Time > 0.5) { ReportCachePut($CacheKey, $V);
225  }
226  }
227  return;
228  }
229 
230 }
231 
232 $NewPlugin = new ui_demomod;
233 $NewPlugin->Initialize();
char * uploadtree_tablename
upload.uploadtree_tablename
Definition: adj2nest.c:100
This is the Plugin class. All plugins should:
Definition: FO_Plugin.php:57
Definition: state.hpp:16
browse an upload and display the demomod data (first bytes of the file)
Definition: demomod.php:22
$uploadtree_tablename
Upload tree table to use.
Definition: demomod.php:27
$Dependency
Dependecy for plugin.
Definition: demomod.php:25
$Title
Page title.
Definition: demomod.php:24
Install()
Only used during installation.
Definition: demomod.php:33
ShowData($upload_pk, $uploadtree_pk)
Display the demomod data.
Definition: demomod.php:90
RegisterMenus()
Customize submenus.
Definition: demomod.php:47
$DBaccess
DB access required.
Definition: demomod.php:26
Output()
This function returns the scheduler status.
Definition: demomod.php:135
$Name
Mod name.
Definition: demomod.php:23
Initialize()
This is called before the plugin is used.
Definition: demomod.php:75
js_url()
Load a new url.
ReportCacheGet($CacheKey)
This function is used by Output() to see if the requested report is in the report cache.
ReportCachePut($CacheKey, $CacheValue)
This function is used to write a record to the report cache. If the record already exists,...
ReportCachePurgeByKey($CacheKey)
Purge from the report cache the record with $CacheKey.
DBCheckResult($result, $sql, $filenm, $lineno)
Check the postgres result for unexpected errors. If found, treat them as fatal.
Definition: common-db.php:187
Dir2Browse($Mod, $UploadtreePk, $LinkLast=NULL, $ShowBox=1, $ShowMicro=NULL, $Enumerate=-1, $PreText='', $PostText='', $uploadtree_tablename="uploadtree")
Get an html linked string of a file browse path.
Definition: common-dir.php:263
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.
const PARM_INTEGER
Definition: common-parm.php:14
Traceback()
Get the URI + query to this location.
Definition: common-parm.php:89
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.
FUNCTION char * GetUploadtreeTableName(PGconn *pgConn, int upload_pk)
Get the uploadtree table name for this upload_pk If upload_pk does not exist, return "uploadtree".
Definition: libfossagent.c:414
#define PLUGIN_DB_READ
Plugin requires read permission on DB.
Definition: libfossology.h:37
foreach($Options as $Option=> $OptVal) if(0==$reference_flag &&0==$nomos_flag) $PG_CONN