FOSSology  3.2.0rc1
Open Source License Compliance by Open Source Software
demomod.php
Go to the documentation of this file.
1 <?php
2 /***********************************************************
3  Copyright (C) 2013 Hewlett-Packard Development Company, L.P.
4 
5  This program is free software; you can redistribute it and/or
6  modify it under the terms of the GNU General Public License
7  version 2 as published by the Free Software Foundation.
8 
9  This program is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  GNU General Public License for more details.
13 
14  You should have received a copy of the GNU General Public License along
15  with this program; if not, write to the Free Software Foundation, Inc.,
16  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 ***********************************************************/
18 
20 
26 define("TITLE_UI_DEMOMOD", _("Demomod View"));
27 
32 class ui_demomod extends FO_Plugin
33 {
34  var $Name = "demomod";
35  var $Title = TITLE_UI_DEMOMOD;
36  var $Dependency = array("browse");
39 
44  function Install()
45  {
46  global $PG_CONN;
47 
48  if (!$PG_CONN) {
49  return(1);
50  }
51 
52  return(0);
53  } // Install()
54 
58  function RegisterMenus()
59  {
60  // For all other menus, permit coming back here.
61  $URI = $this->Name . Traceback_parm_keep(array("upload","item"));
62  $MenuName = "Demomod View";
63 
64  $Item = GetParm("item",PARM_INTEGER);
65  $Upload = GetParm("upload",PARM_INTEGER);
66  if (!empty($Item))
67  {
68  $text = _("Demomod data");
69  menu_insert("Browse::$MenuName",100,$URI,$text);
70  menu_insert("View::$MenuName",100,$URI,$text);
71  }
72  } // RegisterMenus()
73 
74 
86  function Initialize()
87  {
88  if ($this->State != PLUGIN_STATE_INVALID) { return(1); // don't re-run
89  }
90 
91  return($this->State == PLUGIN_STATE_VALID);
92  } // Initialize()
93 
94 
101  function ShowData($upload_pk, $uploadtree_pk)
102  {
103  global $PG_CONN;
104 
105  /* Check the demomod_ars table to see if we have any data */
106  $sql = "select ars_pk from demomod_ars where upload_fk=$upload_pk and ars_success=true";
107  $result = pg_query($PG_CONN, $sql);
108  DBCheckResult($result, $sql, __FILE__, __LINE__);
109  $rows = pg_num_rows($result);
110  pg_free_result($result);
111 
112  if ($rows == 0) { return _("There is no demomod data for this upload. Use Jobs > Schedule Agent.");
113  }
114 
115  /* Get the scan result */
116  /* First we need the pfile_pk */
117  $sql = "select pfile_fk from $this->uploadtree_tablename where uploadtree_pk=$uploadtree_pk and upload_fk=$upload_pk";
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  $pfile_fk = $row['pfile_fk'];
125  pg_free_result($result);
126 
127  /* Now we can get the scan result */
128  $sql = "select firstbytes from demomod where pfile_fk=$pfile_fk";
129  $result = pg_query($PG_CONN, $sql);
130  DBCheckResult($result, $sql, __FILE__, __LINE__);
131  $rows = pg_num_rows($result);
132  if ($rows == 0) { return _("Internal consistency error. Failed: $sql");
133  }
134  $row = pg_fetch_assoc($result);
135  $firstbytes = $row['firstbytes'];
136  pg_free_result($result);
137 
138  $text = _("The first bytes of this file are: ");
139  return ($text . $firstbytes);
140  }
141 
142 
146  function Output()
147  {
148  $uTime = microtime(true);
149  if ($this->State != PLUGIN_STATE_READY) {
150  return(0);
151  }
152  $V="";
153 
154  $Upload = GetParm("upload",PARM_INTEGER);
155  /* @var $uploadDao UploadDao */
156  $uploadDao = $GLOBALS['container']->get('dao.upload');
157  if (!$uploadDao->isAccessible($Upload, Fossology\Lib\Auth\Auth::getGroupId()))
158  {
159  $text = _("Permission Denied");
160  return "<h2>$text</h2>";
161  }
162 
163  $Item = GetParm("item",PARM_INTEGER);
164  $updcache = GetParm("updcache",PARM_INTEGER);
165 
166  /* Remove "updcache" from the GET args.
167  * This way all the url's based on the input args won't be
168  * polluted with updcache
169  * Use Traceback_parm_keep to ensure that all parameters are in order */
170  $CacheKey = "?mod=" . $this->Name . Traceback_parm_keep(array("upload","item","agent"));
171  if ($updcache)
172  {
173  $_SERVER['REQUEST_URI'] = preg_replace("/&updcache=[0-9]*/","",$_SERVER['REQUEST_URI']);
174  unset($_GET['updcache']);
175  $V = ReportCachePurgeByKey($CacheKey);
176  }
177  else
178  {
179  $V = ReportCacheGet($CacheKey);
180  }
181 
183 
184  if (empty($V) ) // no cache exists
185  {
186  switch($this->OutputType)
187  {
188  case "XML":
189  break;
190  case "HTML":
191  $V .= "<font class='text'>\n";
192 
193  /************************/
194  /* Show the folder path */
195  /************************/
196  $V .= Dir2Browse($this->Name,$Item,NULL,1,"Browse", -1, '', '', $this->uploadtree_tablename) . "<P />\n";
197 
198  if (!empty($Upload))
199  {
200  $Uri = preg_replace("/&item=([0-9]*)/","",Traceback());
201  $V .= js_url();
202  $V .= $this->ShowData($Upload, $Item);
203  }
204  $V .= "</font>\n";
205  $V .= "<p>\n";
206  break;
207  case "Text":
208  break;
209  default:
210  }
211 
212  $Cached = false;
213  }
214  else {
215  $Cached = true;
216  }
217 
218  if (!$this->OutputToStdout) {
219  return($V);
220  }
221  print "$V";
222  $Time = microtime(true) - $uTime; // convert usecs to secs
223  $text = _("Elapsed time: %.2f seconds");
224  printf( "<small>$text</small>", $Time);
225 
226  if ($Cached)
227  {
228  $text = _("cached");
229  $text1 = _("Update");
230  echo " <i>$text</i> <a href=\"$_SERVER[REQUEST_URI]&updcache=1\"> $text1 </a>";
231  }
232  else
233  {
234  /* Cache Report if this took longer than 1/2 second*/
235  if ($Time > 0.5) { ReportCachePut($CacheKey, $V);
236  }
237  }
238  return;
239  }
240 
241 }
242 
243 $NewPlugin = new ui_demomod;
244 $NewPlugin->Initialize();
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:421
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:274
$DBaccess
DB access required.
Definition: demomod.php:37
$Title
Page title.
Definition: demomod.php:35
ReportCachePut($CacheKey, $CacheValue)
This function is used to write a record to the report cache. If the record already exists...
$uploadtree_tablename
Upload tree table to use.
Definition: demomod.php:38
ShowData($upload_pk, $uploadtree_pk)
Display the demomod data.
Definition: demomod.php:101
browse an upload and display the demomod data (first bytes of the file)
Definition: demomod.php:32
#define PLUGIN_DB_READ
Plugin requires read permission on DB.
Definition: libfossology.h:49
ReportCachePurgeByKey($CacheKey)
Purge from the report cache the record with $CacheKey.
Definition: state.hpp:26
RegisterMenus()
Customize submenus.
Definition: demomod.php:58
GetParm($parameterName, $parameterType)
This function will retrieve the variables and check data types.
Definition: common-parm.php:57
$Dependency
Dependecy for plugin.
Definition: demomod.php:36
const PARM_INTEGER
Definition: common-parm.php:25
Namespace used by reuser agent.
char * uploadtree_tablename
upload.uploadtree_tablename
Definition: adj2nest.c:112
ReportCacheGet($CacheKey)
This function is used by Output() to see if the requested report is in the report cache...
This is the Plugin class. All plugins should:
Definition: FO_Plugin.php:67
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...
foreach($Options as $Option=> $OptVal) if(0==$reference_flag &&0==$nomos_flag) $PG_CONN
js_url()
Load a new url.
DBCheckResult($result, $sql, $filenm, $lineno)
Check the postgres result for unexpected errors. If found, treat them as fatal.
Definition: common-db.php:198
Install()
Only used during installation.
Definition: demomod.php:44
Output()
This function returns the scheduler status.
Definition: demomod.php:146
$Name
Mod name.
Definition: demomod.php:34
Traceback_parm_keep($List)
Create a new URI, keeping only these items.
Traceback()
Get the URI + query to this location.
Initialize()
This is called before the plugin is used.
Definition: demomod.php:86