FOSSology  4.4.0
Open Source License Compliance by Open Source Software
/home/runner/work/fossology/fossology/src/www/ui/ui-picker.php

get a a path to a file

Parameters
$File1uploadtree_pk- pk of file1
$FolderList- folder path for the file (or folder).
$DirectoryList- directory path to the file. May be empty.

$FolderList array example:
[0] => Array
[folder_pk] => 1
[folder_name] => Software Repository
[1] => Array
[folder_pk] => 5
[folder_name] => cpio

$DirectoryList array example:
[0] => Array
[uploadtree_pk] => 897121
[parent] =>
[upload_fk] => 11
[pfile_fk] => 691036
[ufile_mode] => 536904704
[lft] => 1
[rgt] => 1048
[ufile_name] => cpio-2.10-9.el6.src.rpm

Returns
string which is a linked path to a file. The path includes folders and files. This is the stuff in the yellow box
<?php
/*
SPDX-FileCopyrightText: © 2010-2013 Hewlett-Packard Development Company, L.P.
SPDX-License-Identifier: GPL-2.0-only
*/
function picker_name_cmp($rowa, $rowb)
{
return (strnatcasecmp($rowa['name'], $rowb['name']));
}
function picker_ufile_name_cmp($rowa, $rowb)
{
return (strnatcasecmp($rowa['ufile_name'], $rowb['ufile_name']));
}
class ui_picker extends FO_Plugin
{
var $HighlightColor = '#4bfe78';
private $uploadDao;
function __construct()
{
$this->Name = "picker";
$this->Title = _("File Picker");
$this->Dependency = array("browse","view");
$this->DBaccess = PLUGIN_DB_READ;
$this->LoginFlag = 0;
parent::__construct();
$this->uploadDao = $GLOBALS['container']->get('dao.upload');
}
function Install()
{
global $PG_CONN;
if (empty($PG_CONN)) {
return(1);
} /* No DB */
/* create it if it doesn't exist */
return(0);
} // Install()
function RegisterMenus()
{
$text = _("Compare this file to another.");
menu_insert("Browse-Pfile::Compare",0,$this->Name,$text);
return 0;
} // RegisterMenus()
function Initialize()
{
if ($this->State != PLUGIN_STATE_INVALID) {
return (1);
} // don't re-run
if ($this->Name !== "") {
// Name must be defined
global $Plugins;
$this->State = PLUGIN_STATE_VALID;
$Plugins[] = $this;
}
return($this->State == PLUGIN_STATE_VALID);
} // Initialize()
function Create_file_picker()
{
global $PG_CONN;
/* If table exists, then we are done */
$sql = "SELECT typlen FROM pg_type where typname='file_picker' limit 1";
$result = pg_query($PG_CONN, $sql);
DBCheckResult($result, $sql, __FILE__, __LINE__);
if (pg_num_rows($result) > 0) {
pg_free_result($result);
return 0;
}
pg_free_result($result);
/* Create table */
$sql = "CREATE TABLE file_picker (
file_picker_pk serial NOT NULL PRIMARY KEY,
user_fk integer NOT NULL,
uploadtree_fk1 integer NOT NULL,
uploadtree_fk2 integer NOT NULL,
last_access_date date NOT NULL
);
ALTER TABLE ONLY file_picker
ADD CONSTRAINT file_picker_user_fk_key UNIQUE (user_fk, uploadtree_fk1, uploadtree_fk2);";
$result = pg_query($PG_CONN, $sql);
DBCheckResult($result, $sql, __FILE__, __LINE__);
pg_free_result($result);
}
function HTMLFileList($File1uploadtree_pk, $Children, $FolderContents)
{
global $PG_CONN;
global $Plugins;
$OutBuf=""; // return values for file listing
$Uri = Traceback_uri() . "?mod=$this->Name";
$OutBuf .= "<table style='text-align:left;' >";
if (!empty($FolderContents)) {
usort($FolderContents, 'picker_name_cmp');
/* write subfolders */
foreach ($FolderContents as $Folder) {
if ($Folder && array_key_exists('folder_pk', $Folder)) {
$folder_pk = $Folder['folder_pk'];
$folder_name = htmlentities($Folder['name']);
$OutBuf .= "<tr>";
$OutBuf .= "<td></td>";
$OutBuf .= "<td>";
$OutBuf .= "<a href='$Uri&folder=$folder_pk&item=$File1uploadtree_pk'><b>$folder_name</b></a>/";
$OutBuf .= "</td></tr>";
} else if ($Folder && array_key_exists('uploadtree_pk', $Folder)) {
$bitem = $Folder['uploadtree_pk'];
$upload_filename = htmlentities($Folder['name']);
$OutBuf .= "<tr>";
$OutBuf .= "<td>";
$text = _("Select");
$Options = "id=filepick2 onclick='AppJump($bitem)')";
$OutBuf .= "<button type='button' $Options> $text </button>\n";
$OutBuf .= "</td>";
$OutBuf .= "<td>";
$OutBuf .= "<a href='$Uri&bitem=$bitem&item=$File1uploadtree_pk'><b>$upload_filename</b></a>/";
$OutBuf .= "</td></tr>";
}
}
} else {
if (empty($Children)) {
$text = _("No children to compare");
$OutBuf .= "<tr><td colspan=2>$text</td></tr>";
} else {
usort($Children, 'picker_ufile_name_cmp');
foreach ($Children as $Child) {
if (empty($Child)) {
continue;
}
$OutBuf .= "<tr>";
$IsDir = Isdir($Child['ufile_mode']);
$IsContainer = Iscontainer($Child['ufile_mode']);
$LinkUri = $Uri . "&bitem=$Child[uploadtree_pk]&item=$File1uploadtree_pk";
$OutBuf .= "<td>";
$text = _("Select");
$Options = "id=filepick onclick='AppJump($Child[uploadtree_pk])')";
$OutBuf .= "<button type='button' $Options> $text </button>\n";
$OutBuf .= "</td>";
$OutBuf .= "<td>";
if ($IsContainer) {
$OutBuf .= "<a href='$LinkUri'> $Child[ufile_name]</a>";
} else {
$OutBuf .= $Child['ufile_name'];
}
if ($IsDir) {
$OutBuf .= "/";
}
$OutBuf .= "</td>";
$OutBuf .= "</tr>";
}
}
}
$OutBuf .= "</table>";
return($OutBuf);
} // HTMLFileList()
function HTMLPath($File1uploadtree_pk, $FolderList, $DirectoryList)
{
if (empty($FolderList)) {
return "__FILE__ __LINE__ No folder list specified";
}
$OutBuf = "";
$Uri2 = Traceback_uri() . "?mod=$this->Name";
/* Box decorations */
$OutBuf .= "<div class='alert alert-info' style='padding:5px;'>\n";
/* write the FolderList */
$text = _("Folder");
$OutBuf .= "<b>$text</b>: ";
foreach ($FolderList as $Folder) {
$folder_pk = $Folder['folder_pk'];
$folder_name = htmlentities($Folder['folder_name']);
$OutBuf .= "<a href='$Uri2&folder=$folder_pk&item=$File1uploadtree_pk'><b>$folder_name</b></a>/";
}
/* write the DirectoryList */
if (! empty($DirectoryList)) {
$OutBuf .= "<br>";
$First = true; /* If $First is true, directory path starts a new line */
/* Show the path within the upload */
foreach ($DirectoryList as $uploadtree_rec) {
if (! $First) {
$OutBuf .= "/ ";
}
$href = "$Uri2&bitem=$uploadtree_rec[uploadtree_pk]&item=$File1uploadtree_pk";
$OutBuf .= "<a href='$href'>";
if (!$First && Iscontainer($uploadtree_rec['ufile_mode'])) {
$OutBuf .= "<br>&nbsp;&nbsp;";
}
$OutBuf .= "<b>" . $uploadtree_rec['ufile_name'] . "</b>";
$OutBuf .= "</a>";
$First = false;
}
}
$OutBuf .= "</div>\n"; // box
return($OutBuf);
} // HTMLPath()
function HistoryPick($uploadtree_pk, &$rtncount)
{
global $PG_CONN;
$PickerRows = array();
/* select possible item2's from pick history for this user */
$user_pk = $_SESSION['UserId'];
if (empty($user_pk)) {
return $PickerRows;
}
$sql = "select file_picker_pk, uploadtree_fk1, uploadtree_fk2 from file_picker
where user_fk= '$user_pk' and ($uploadtree_pk=uploadtree_fk1 or $uploadtree_pk=uploadtree_fk2)";
$result = pg_query($PG_CONN, $sql);
DBCheckResult($result, $sql, __FILE__, __LINE__);
$rtncount = pg_num_rows($result);
if ($rtncount > 0) {
$PickerRows = pg_fetch_all($result);
pg_free_result($result);
} else {
/* No rows in history for this item and user */
pg_free_result($result);
return "";
}
/* reformat $PickHistRecs for select list */
$PickSelectArray = array();
foreach ($PickerRows as $PickRec) {
if ($PickRec['uploadtree_fk1'] == $uploadtree_pk) {
$item2 = $PickRec["uploadtree_fk2"];
} else {
$item2 = $PickRec["uploadtree_fk1"];
}
$PathArray = Dir2Path($item2, 'uploadtree');
$Path = $this->Uploadtree2PathStr($PathArray);
$PickSelectArray[$item2] = $Path;
}
$Options = "id=HistoryPick onchange='AppJump(this.value)')";
$SelectList = Array2SingleSelect($PickSelectArray, "HistoryPick", "", true,
true, $Options);
return $SelectList;
} /* End HistoryPick() */
function SuggestionsPick($FileName, $uploadtree_pk, &$rtncount)
{
global $PG_CONN;
/* find the root of $FileName. Thats the beginning alpha part. */
$BaseFN = basename($FileName);
$delims= "/-.0123456789 \t\n\r\0\0xb";
$NameRoot = ltrim($BaseFN, $delims);
$NameRoot = strtok($NameRoot, $delims);
/* Only make suggestions with matching file extensions */
$ext = GetFileExt($FileName);
$tail = ".$ext";
if (empty($NameRoot)) {
return "";
}
/* find non artifact containers with names similar to $FileName */
$sql = "select uploadtree_pk from uploadtree
where ((ufile_mode & (1<<29))!=0) AND ((ufile_mode & (1<<28))=0)
and (ufile_name like '$NameRoot%$tail')
and (uploadtree_pk != '$uploadtree_pk') limit 100";
$result = pg_query($PG_CONN, $sql);
DBCheckResult($result, $sql, __FILE__, __LINE__);
$SuggestionsArray = array();
while ($row = pg_fetch_assoc($result)) {
$PathArray = Dir2Path($row['uploadtree_pk'], 'uploadtree');
$SuggestionsArray[$row['uploadtree_pk']] = $this->Uploadtree2PathStr($PathArray);
}
pg_free_result($result);
$rtncount = count($SuggestionsArray);
if ($rtncount == 0) {
return "";
}
/* Order the select list by the beginning of the path */
natsort($SuggestionsArray);
$Options = "id=SuggestPick onchange='AppJump(this.value)')";
$SelectList = Array2SingleSelect($SuggestionsArray, "SuggestionsPick", "",
true, true, $Options);
return $SelectList;
} /* End SuggestionsPick */
function BrowsePick($uploadtree_pk, $inBrowseuploadtree_pk, $infolder_pk, $PathArray)
{
$OutBuf = "";
if (empty($inBrowseuploadtree_pk)) {
$Browseuploadtree_pk = $uploadtree_pk;
} else {
$Browseuploadtree_pk = $inBrowseuploadtree_pk;
}
if (empty($infolder_pk)) {
$folder_pk = GetFolderFromItem("", $Browseuploadtree_pk);
} else {
$folder_pk = $infolder_pk;
}
// Get list of folders that this $Browseuploadtree_pk is in
$FolderList = Folder2Path($folder_pk);
// If you aren't browsing folders,
// Get list of directories that this $Browseuploadtree_pk is in
if (empty($infolder_pk)) {
$DirectoryList = Dir2Path($Browseuploadtree_pk, 'uploadtree');
} else {
$DirectoryList = '';
}
// Get HTML for folder/directory list.
// This is the stuff in the yellow bar.
$OutBuf .= $this->HTMLPath($uploadtree_pk, $FolderList, $DirectoryList);
/*
* Get list of folders in this folder
* That is, $DirectoryList is empty
*/
if (empty($infolder_pk)) {
$FolderContents = array();
$Children = GetNonArtifactChildren($Browseuploadtree_pk);
} else {
$Children = array();
$FolderContents = $this->GetFolderContents($folder_pk);
}
$OutBuf .= $this->HTMLFileList($uploadtree_pk, $Children, $FolderContents);
return $OutBuf;
} /* End BrowsePick */
function GetFolderContents($folder_pk)
{
global $PG_CONN;
$FolderContents = array();
$Uri2 = Traceback_uri() . "?mod=$this->Name";
/* Display all the folders in this folder_pk */
$sql = "select * from foldercontents where parent_fk='$folder_pk'";
$FCresult = pg_query($PG_CONN, $sql);
DBCheckResult($FCresult, $sql, __FILE__, __LINE__);
/* Display folder contents */
while ($FCrow = pg_fetch_assoc($FCresult)) {
switch ($FCrow['foldercontents_mode']) {
case 1: /******* child is folder *******/
$sql = "select folder_pk, folder_name as name from folder where folder_pk=$FCrow[child_id]";
$FolderResult = pg_query($PG_CONN, $sql);
DBCheckResult($FolderResult, $sql, __FILE__, __LINE__);
$FolderRow = pg_fetch_assoc($FolderResult);
pg_free_result($FolderResult);
$FolderContents[] = $FolderRow;
break;
case 2: /******* child is upload *******/
$sql = "select upload_pk, upload_filename as name from upload"
. " where upload_pk=$FCrow[child_id] and ((upload_mode & (1<<5))!=0)";
$UpResult = pg_query($PG_CONN, $sql);
DBCheckResult($UpResult, $sql, __FILE__, __LINE__);
$NumRows = pg_num_rows($UpResult);
if ($NumRows) {
$UpRow = pg_fetch_assoc($UpResult);
pg_free_result($UpResult);
} else {
pg_free_result($UpResult);
break;
}
/* get top level uploadtree_pk for this upload_pk */
$sql = "select uploadtree_pk from uploadtree where upload_fk=$FCrow[child_id] and parent is null";
$UtreeResult = pg_query($PG_CONN, $sql);
DBCheckResult($UtreeResult, $sql, __FILE__, __LINE__);
$UtreeRow = pg_fetch_assoc($UtreeResult);
pg_free_result($UtreeResult);
$UpRow['uploadtree_pk'] = $UtreeRow['uploadtree_pk'];
$FolderContents[] = $UpRow;
break;
case 4: /******* child_id is uploadtree_pk (unused) *******/
default:
}
}
pg_free_result($FCresult);
return $FolderContents;
} /* End GetFolderContents */
function HTMLout($RtnMod, $uploadtree_pk, $Browseuploadtree_pk, $folder_pk, $PathArray)
{
$OutBuf = '';
$uri = Traceback_uri() . "?mod=$this->Name";
$OutBuf .= "<script language='javascript'>\n";
$OutBuf .= "function AppJump(val) {";
$OutBuf .= "var rtnmodelt = document.getElementById('apick');";
$OutBuf .= "var rtnmod = rtnmodelt.value;";
$OutBuf .= "var uri = '$uri' + '&rtnmod=' + rtnmod + '&item=' + $uploadtree_pk + '&item2=' + val;";
$OutBuf .= "window.location.assign(uri);";
$OutBuf .= "}";
$OutBuf .= "</script>\n";
/* Explain what the picker is for */
$OutBuf .= "The purpose of the picker is to permit people to positively pick a pair of paths.";
$OutBuf .= "<br>Path pairs are used by reports that do file comparisons and "
. "differences between files (like isos, packages, directories, etc.).";
$OutBuf .= "<hr>";
/* Print file 1 so people know what they are comparing to */
$OutBuf .= "<div style=background-color:lavender>";
$OutBuf .= "<center><table style='border:5px groove red'>";
$OutBuf .= "<tr><td><b>File 1: </b></td><td>&nbsp;&nbsp;</td><td>";
$PathStr = $this->Uploadtree2PathStr($PathArray);
$OutBuf .= "$PathStr";
$OutBuf .= "</td></tr>";
$OutBuf .= "</table></center>";
$text = _("Choose the program to run after you select the second file.");
$OutBuf .= "<b>$text</b><br>";
$OutBuf .= $this->ApplicationPick("PickRtnApp", $RtnMod, "will run after chosing a file");
$OutBuf .= "</div>";
$OutBuf .= "<br>";
/* Display the history pick, if there is a history for this user. */
$HistPick = $this->HistoryPick($uploadtree_pk, $rtncount);
if (! empty($HistPick)) {
$text = _("Select from your pick history");
$OutBuf .= "<h3>$text ($rtncount):</h3>";
$OutBuf .= "$HistPick";
}
/* Browse window */
$text = _("Browse");
$OutBuf .= "<hr><h3>$text:</h3>";
/* Folder/directory bar */
$OutBuf .= $this->BrowsePick($uploadtree_pk, $Browseuploadtree_pk, $folder_pk, $PathArray);
return $OutBuf;
}
function Output()
{
global $PG_CONN;
if ($this->State != PLUGIN_STATE_READY) {
return(0);
}
$RtnMod = GetParm("rtnmod",PARM_TEXT);
$uploadtree_pk = GetParm("item", PARM_INTEGER);
if (! $uploadtree_pk) {
return "<h2>Unidentified item 1</h2>";
}
$uploadtree_pk2 = GetParm("item2",PARM_INTEGER);
$folder_pk = GetParm("folder",PARM_INTEGER);
$user_pk = Auth::getUserId();
/* Item to start Browse window on */
$Browseuploadtree_pk = GetParm("bitem",PARM_INTEGER);
/* Check item1 and item2 upload permissions */
$Item1Row = GetSingleRec("uploadtree", "WHERE uploadtree_pk = $uploadtree_pk");
if (! $this->uploadDao->isAccessible($Item1Row['upload_fk'],
Auth::getGroupId())) {
$text = _("Permission Denied");
return "<h2>$text item 1</h2>";
}
if (! empty($uploadtree_pk2)) {
$Item2Row = GetSingleRec("uploadtree", "WHERE uploadtree_pk = $uploadtree_pk2");
if (! $this->uploadDao->isAccessible($Item2Row['upload_fk'], Auth::getGroupId())) {
$text = _("Permission Denied");
return "<h2>$text item 2</h2>";
}
}
if (!empty($user_pk) && !empty($RtnMod) && !empty($uploadtree_pk) && !empty($uploadtree_pk2)) {
$sql = "insert into file_picker (user_fk, uploadtree_fk1, uploadtree_fk2, last_access_date)
values($user_pk, $uploadtree_pk, $uploadtree_pk2, now())";
// ignore errors (most probably a duplicate key)
@$result = pg_query($PG_CONN, $sql);
// Redirect to diff module
$uri = Traceback_uri() . "?mod=$RtnMod&item1=$uploadtree_pk&item2=$uploadtree_pk2";
echo "<script type='text/javascript'> window.location.assign('$uri');</script>";
exit();
}
$OutBuf = "";
if ($this->OutputType == 'HTML') {
if (empty($uploadtree_pk)) {
$OutBuf = "<h2>Picker URL is missing the first comparison file.</h2>";
} else {
$PathArray = Dir2Path($uploadtree_pk, 'uploadtree');
$OutBuf .= $this->HTMLout($RtnMod, $uploadtree_pk, $Browseuploadtree_pk, $folder_pk, $PathArray);
}
}
return $OutBuf;
}
private function Uploadtree2PathStr ($PathArray)
{
$Path = "";
if (count($PathArray)) {
foreach ($PathArray as $PathRow) {
$Path .= "/" . $PathRow['ufile_name'];
}
}
return $Path;
}
protected function ApplicationPick($SLName, $SelectedVal, $label)
{
/* select the apps that are registered to accept item1, item2 pairs.
* At this time (pre 2.x) we don't know enough about the plugins
* to know if they can take a pair. Till then, the list is
* hardcoded.
*/
$AppList = array("nomosdiff" => "License Difference",
"bucketsdiff" => "Bucket Difference");
$Options = "id=apick";
$SelectList = Array2SingleSelect($AppList, $SLName, $SelectedVal,
false, true, $Options);
$StrOut = "$SelectList $label";
return $StrOut;
}
}
$NewPlugin = new ui_picker;
$NewPlugin->Initialize();
This is the Plugin class. All plugins should:
Definition: FO_Plugin.php:57
Contains the constants and helpers for authentication of user.
Definition: Auth.php:24
Definition: state.hpp:16
ApplicationPick($SLName, $SelectedVal, $label)
Generate html to pick the application that will be called after the items are identified.
Definition: ui-picker.php:728
Install()
Create and configure database tables.
Definition: ui-picker.php:57
__construct()
base constructor. Most plugins will just use this
Definition: ui-picker.php:42
SuggestionsPick($FileName, $uploadtree_pk, &$rtncount)
Search the whole repository for containers with names similar to $FileName (based on the beggining te...
Definition: ui-picker.php:369
Output()
The Picker page.
Definition: ui-picker.php:624
HTMLout($RtnMod, $uploadtree_pk, $Browseuploadtree_pk, $folder_pk, $PathArray)
the html format out info
Definition: ui-picker.php:551
Uploadtree2PathStr($PathArray)
Get string representation of uploadtree path. Use Dir2Path to get $PathArray.
Definition: ui-picker.php:704
RegisterMenus()
Customize submenus.
Definition: ui-picker.php:73
HTMLFileList($File1uploadtree_pk, $Children, $FolderContents)
Given an $File1uploadtree_pk, $Children are non artifact children of $File1uploadtree_pk.
Definition: ui-picker.php:146
HistoryPick($uploadtree_pk, &$rtncount)
pick history
Definition: ui-picker.php:316
BrowsePick($uploadtree_pk, $inBrowseuploadtree_pk, $infolder_pk, $PathArray)
file browser
Definition: ui-picker.php:421
Initialize()
This is called before the plugin is used.
Definition: ui-picker.php:90
Create_file_picker()
Create file_picker table.
Definition: ui-picker.php:108
DBCheckResult($result, $sql, $filenm, $lineno)
Check the postgres result for unexpected errors. If found, treat them as fatal.
Definition: common-db.php:187
GetSingleRec($Table, $Where="")
Retrieve a single database record.
Definition: common-db.php:91
Isdir($mode)
Definition: common-dir.php:20
Iscontainer($mode)
Definition: common-dir.php:38
Dir2Path($uploadtree_pk, $uploadtree_tablename='uploadtree')
Return the path (without artifacts) of an uploadtree_pk.
Definition: common-dir.php:222
GetFolderFromItem($upload_pk="", $uploadtree_pk="")
Find what folder an item is in.
Folder2Path($folder_pk)
Return an array of folder_pk, folder_name from the users.root_folder_fk to $folder_pk.
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.
Traceback_uri()
Get the URI without query to this location.
Definition: common-parm.php:97
const PARM_TEXT
Definition: common-parm.php:20
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
Array2SingleSelect($KeyValArray, $SLName="unnamed", $SelectedVal="", $FirstEmpty=false, $SelElt=true, $Options="", $ReturnKey=true)
Build a single choice select pulldown.
Definition: common-ui.php:32
GetFileExt($fname)
Get File Extension (text after last period)
Definition: common-ui.php:142
#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
picker_ufile_name_cmp($rowa, $rowb)
Sort filenames.
Definition: ui-picker.php:30
picker_name_cmp($rowa, $rowb)
Sort folder and upload names.
Definition: ui-picker.php:21