1<?php
2/***********************************************************
3 Copyright (C) 2010-2015 Hewlett-Packard Development Company, L.P.
4 Copyright (C) 2015 Siemens AG
5
6 This program is free software; you can redistribute it and/or
7 modify it under the terms of the GNU General Public License
8 version 2 as published by the Free Software Foundation.
9
10 This program 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
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License along
16 with this program; if not, write to the Free Software Foundation, Inc.,
17 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
18***********************************************************/
19
20use Fossology\Lib\Auth\Auth;
21use Fossology\Lib\Dao\UploadDao;
22
23/**
24 * \file ui-list-bucket-files.php
25 * This plugin is used to: \n
26 * List files for a given bucket in a given uploadtree. \n
27 * The following are passed in: \n
28 *  bapk   bucketagent_pk \n
29 *  napk   nomosagent_pk \n
30 *  item   uploadtree_pk \n
31 *  bpk    bucket_pk \n
32 *  bp     bucketpool_pk \n
33 */
34
35/**
36 * @class list_bucket_files
37 * This plugin is used to: \n
38 * List files for a given bucket in a given uploadtree. \n
39 * The following are passed in: \n
40 *  bapk   bucketagent_pk \n
41 *  napk   nomosagent_pk \n
42 *  item   uploadtree_pk \n
43 *  bpk    bucket_pk \n
44 *  bp     bucketpool_pk \n
45 */
46class list_bucket_files extends FO_Plugin
47{
48  function __construct()
49  {
50    $this->Name       = "list_bucket_files";
51    $this->Title      = _("List Files for Bucket");
52    $this->Dependency = array("nomoslicense");
53    $this->DBaccess   = PLUGIN_DB_READ;
54    $this->LoginFlag  = 0;
55    parent::__construct();
56  }
57
58  /**
59   * @brief Customize submenus.
60   * @see FO_Plugin::RegisterMenus()
61   */
62  function RegisterMenus()
63  {
64    // micro-menu
65    $bucketagent_pk = GetParm("bapk",PARM_INTEGER);
66    $uploadtree_pk = GetParm("item",PARM_INTEGER);
67    $bucket_pk = GetParm("bpk",PARM_INTEGER);
68    $bucketpool_pk = GetParm("bp",PARM_INTEGER);
69    $nomosagent_pk = GetParm("napk",PARM_INTEGER);
70
71    $URL = $this->Name . "&bapk=$bucketagent_pk&item=$uploadtree_pk&bpk=$bucket_pk&bp=$bucketpool_pk&napk=$nomosagent_pk&page=-1";
72    $text = _("Show All Files");
73    menu_insert($this->Name."::Show All",0, $URL, $text);
74  } // RegisterMenus()
75
76  /**
77   * @brief This is called before the plugin is used.
78   *
79   * It should assume that Install() was already run one time
80   * (possibly years ago and not during this object's creation).
81   *
82   * @return boolean true on success, false on failure.
83   *  A failed initialize is not used by the system.
84   *
85   * @note This function must NOT assume that other plugins are installed.
86   * @see FO_Plugin::Initialize()
87   */
88  function Initialize()
89  {
90    $this->State=PLUGIN_STATE_VALID;
91    $this->State=PLUGIN_STATE_READY;
92
93    return(true);
94  } // Initialize()
95
96
97  /**
98   * @brief Display all the files for a bucket in this subtree.
99   * @see FO_Plugin::Output()
100   */
101  function Output()
102  {
103    if ($this->State != PLUGIN_STATE_READY) {
104      return;
105    }
106    global $PG_CONN;
107
108    /*  Input parameters */
109    $bucketagent_pk = GetParm("bapk",PARM_INTEGER);
110    $uploadtree_pk = GetParm("item",PARM_INTEGER);
111    $bucket_pk = GetParm("bpk",PARM_INTEGER);
112    $bucketpool_pk = GetParm("bp",PARM_INTEGER);
113    $nomosagent_pk = GetParm("napk",PARM_INTEGER);
114    $BinNoSrc = GetParm("bns",PARM_INTEGER);  // 1 if requesting binary with no src
115    $Excl = GetParm("excl",PARM_RAW);
116
117    if (empty($uploadtree_pk) || empty($bucket_pk) || empty($bucketpool_pk))
118    {
119      $text = _("is missing required parameters.");
120      echo $this->Name . " $text";
121      return;
122    }
123
124    /* Check upload permission */
125    $Row = GetSingleRec("uploadtree", "WHERE uploadtree_pk = $uploadtree_pk");
126    /** @var UploadDao $uploadDao */
127    $uploadDao = $GLOBALS['container']->get('dao.upload');
128    if ( !$uploadDao->isAccessible($Row['upload_fk'], Auth::getGroupId()) )
129    {
130      $text = _("Permission Denied");
131      echo "<h2>$text item 1</h2>";
132      return;
133    }
134
135    $Page = GetParm("page",PARM_INTEGER);
136    if (empty($Page)) {
137      $Page=0;
138    }
139
140    $V="";
141    $Time = time();
142    $Max = 200;
143
144    // Create cache of bucket_pk => bucket_name
145    // Since we are going to do a lot of lookups
146    $sql = "select bucket_pk, bucket_name from bucket_def where bucketpool_fk=$bucketpool_pk";
147    $result_name = pg_query($PG_CONN, $sql);
148    DBCheckResult($result_name, $sql, __FILE__, __LINE__);
149    $bucketNameCache = array();
150    while ($name_row = pg_fetch_assoc($result_name)) {
151      $bucketNameCache[$name_row['bucket_pk']] = $name_row['bucket_name'];
152    }
153    pg_free_result($result_name);
154
155    switch($this->OutputType)
156    {
157      case "XML":
158        break;
159      case "HTML":
160        // micro menus
161        $V .= menu_to_1html(menu_find($this->Name, $MenuDepth),0);
162
163        /* Get all the files under this uploadtree_pk with this bucket */
164        $V .= _("The following files are in bucket: '<b>");
165        $V .= $bucketNameCache[$bucket_pk];
166        $V .= "</b>'.\n";
167        $text = _("Display");
168        $text1 = _("excludes");
169        $text2 = _("files with these licenses");
170        if (!empty($Excl)) $V .= "<br>$text <b>$text1</b> $text2: $Excl";
171
172        $Offset = ($Page <= 0) ? 0 : $Page*$Max;
173        $PkgsOnly = false;
174
175        // Get bounds of subtree (lft, rgt) for this uploadtree_pk
176        $sql = "SELECT lft,rgt,upload_fk FROM uploadtree
177              WHERE uploadtree_pk = $uploadtree_pk";
178        $result = pg_query($PG_CONN, $sql);
179        DBCheckResult($result, $sql, __FILE__, __LINE__);
180        $row = pg_fetch_assoc($result);
181        $lft = $row["lft"];
182        $rgt = $row["rgt"];
183        $upload_pk = $row["upload_fk"];
184        pg_free_result($result);
185
186        /* Get uploadtree table */
187        $uploadtree_tablename = GetUploadtreeTableName($upload_pk);
188
189        /* If $BinNoSrc, then only list binary packages in this subtree
190         * that do not have Source packages.
191        * Else list files in the asked for bucket.
192        */
193        if ($BinNoSrc)
194        {
195        }
196        else
197        {
198          $Offset= ($Page < 0) ? 0 : $Page*$Max;
199          $limit = ($Page < 0) ? "ALL":$Max;
200          // Get all the uploadtree_pk's with this bucket (for this agent and bucketpool)
201          // in this subtree.
202          // It would be best to sort by pfile_pk, so that the duplicate pfiles are
203          // correctly indented, but pfile_pk has no meaning to the user.  So a compromise,
204          // sorting by ufile_name is used.
205          $sql = "select uploadtree.*, bucket_file.nomosagent_fk as nomosagent_fk
206               from uploadtree, bucket_file, bucket_def
207               where upload_fk=$upload_pk and uploadtree.lft between $lft and $rgt
208                 and ((ufile_mode & (3<<28)) = 0)
209                 and uploadtree.pfile_fk=bucket_file.pfile_fk
210                 and agent_fk=$bucketagent_pk
211                 and bucket_fk=$bucket_pk
212                 and bucketpool_fk=$bucketpool_pk
213                 and bucket_pk=bucket_fk
214                 and nomosagent_fk=$nomosagent_pk
215                 order by uploadtree.ufile_name
216                 limit $limit offset $Offset";
217          $fileresult = pg_query($PG_CONN, $sql);
218          DBCheckResult($fileresult, $sql, __FILE__, __LINE__);
219          $Count = pg_num_rows($fileresult);
220        }
221        $file_result_temp = pg_fetch_all($fileresult);
222        $sourted_file_result = array(); // the final file list will display
223        $max_num = $Count;
224        /** sorting by ufile_name from DB, then reorder the duplicates indented */
225        for($i = 0; $i < $max_num; $i++)
226        {
227          $row = $file_result_temp[$i];
228          if (empty($row)) continue;
229          array_push($sourted_file_result, $row);
230          for($j = $i + 1; $j < $max_num; $j++)
231          {
232            $row_next = $file_result_temp[$j];
233            if (!empty($row_next) && ($row['pfile_fk'] == $row_next['pfile_fk']))
234            {
235              array_push($sourted_file_result, $row_next);
236              $file_result_temp[$j] = null;
237            }
238          }
239        }
240
241        if ($Count < (1.25 * $Max)) $Max = $Count;
242        if ($Max < 1) $Max = 1;  // prevent div by zero in corner case of no files
243
244        /* Get the page menu */
245        if (($Count >= $Max) && ($Page >= 0))
246        {
247          $VM = "<P />\n" . MenuEndlessPage($Page,intval((($Count+$Offset)/$Max))) . "<P />\n";
248          $V .= $VM;
249        }
250        else
251        {
252          $VM = "";
253        }
254
255        // base url
256        $baseURL = "?mod=" . $this->Name . "&bapk=$bucketagent_pk&item=$uploadtree_pk&bpk=$bucket_pk&bp=$bucketpool_pk&napk=$nomosagent_pk&page=-1";
257
258        // for each uploadtree rec ($fileresult), find all the licenses in it and it's children
259        $ShowBox = 1;
260        $ShowMicro=NULL;
261        $RowNum = $Offset;
262        $Header = "";
263        $LinkLast = "list_bucket_files&bapk=$bucketagent_pk";
264
265        /* file display loop/table */
266        $V .= "<table>";
267        $text = _("File");
268        $V .= "<tr><th>$text</th><th>&nbsp";
269        $ExclArray = explode(":", $Excl);
270        $ItemNumb = 0;
271        $PrevPfile_pk = 0;
272
273        if ($Count > 0)
274        foreach ($sourted_file_result as $row)
275        {
276          // get all the licenses in this subtree (bucket uploadtree_pk)
277          $pfile_pk = $row['pfile_fk'];
278          $licstring = GetFileLicenses_string($nomosagent_pk, $row['pfile_fk'], $row['uploadtree_pk'], $uploadtree_tablename);
279          if (empty($licstring)) $licstring = '-';
280          $URLlicstring = urlencode($licstring);
281
282          /* Allow user to exclude files with this exact license list */
283          if (!empty($Excl))
284          $URL = $baseURL ."&excl=".urlencode($Excl).":".$URLlicstring;
285          else
286          $URL = $baseURL ."&excl=$URLlicstring";
287          $text = _("Exclude files with license");
288          $Header = "<a href=$URL>$text: $licstring.</a>";
289
290          $ok = !($Excl && in_array($licstring, $ExclArray));
291
292          if ($ok)
293          {
294            $nomosagent_pk = $row['nomosagent_fk'];
295            $LinkLast = "view-license&bapk=$bucketagent_pk&napk=$nomosagent_pk";
296            $V .= "<tr><td>";
297            if ($PrevPfile_pk == $pfile_pk)
298            {
299              $V .= "<div style='margin-left:2em;'>";
300            }
301            else
302            {
303              $V .= "<div>";
304            }
305            $V .= Dir2Browse("browse", $row['uploadtree_pk'], $LinkLast, $ShowBox, $ShowMicro, ++$RowNum, $Header, '', $uploadtree_tablename);
306            $V .= "</div>";
307
308            $V .= "</td>";
309            $V .= "<td>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</td>";  // spaces to seperate licenses
310
311            // show the entire license list as a single string with links to the files
312            // in this container with that license.
313            $V .= "<td>$licstring</td></tr>";
314            $V .= "<tr><td colspan=3><hr></td></tr>";  // separate files
315          }
316          $PrevPfile_pk = $pfile_pk;
317        }
318        pg_free_result($fileresult);
319        $V .= "</table>";
320        if (!empty($VM)) {
321          $V .= $VM . "\n";
322        }
323        $V .= "<hr>\n";
324        $Time = time() - $Time;
325        $text = _("Elapsed time");
326        $text1 = _("seconds");
327        $V .= "<small>$text: $Time $text1</small>\n";
328        break;
329      case "Text":
330        break;
331      default:
332        break;
333    }
334    if (!$this->OutputToStdout) {
335      return($V);
336    }
337    print($V);
338    return;
339  } // Output()
340
341}
342
343$NewPlugin = new list_bucket_files;
344$NewPlugin->Initialize();
345