FOSSology  3.2.0rc1
Open Source License Compliance by Open Source Software
common-dir.php
Go to the documentation of this file.
1 <?php
2 /***********************************************************
3  Copyright (C) 2008-2014 Hewlett-Packard Development Company, L.P.
4 
5  This library is free software; you can redistribute it and/or
6  modify it under the terms of the GNU Lesser General Public
7  License version 2.1 as published by the Free Software Foundation.
8 
9  This library 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 GNU
12  Lesser General Public License for more details.
13 
14  You should have received a copy of the GNU Lesser General Public License
15  along with this library; if not, write to the Free Software Foundation, Inc.0
16  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17  ***********************************************************/
18 
25 
31 function Isdir($mode)
32 {
33  return (($mode & 1 << 18) + ($mode & 0040000) != 0);
34 }
40 function Isartifact($mode)
41 {
42  return (($mode & 1 << 28) != 0);
43 }
49 function Iscontainer($mode)
50 {
51  return (($mode & 1 << 29) != 0);
52 }
53 
61 function DirMode2String($Mode)
62 {
63  $V="";
64  if (Isartifact($Mode)) {
65  $V .= "a";
66  } else {
67  $V .= "-";
68  }
69  if (($Mode & 0120000) == 0120000) {
70  $V .= "l";
71  } else {
72  $V .= "-";
73  }
74  if (Isdir($Mode)) {
75  $V .= "d";
76  } else {
77  $V .= "-";
78  }
79 
80  if ($Mode & 0000400) {
81  $V .= "r";
82  } else {
83  $V .= "-";
84  }
85  if ($Mode & 0000200) {
86  $V .= "w";
87  } else {
88  $V .= "-";
89  }
90  if ($Mode & 0000100) {
91  if ($Mode & 0004000) {
92  $V .= "s"; /* setuid */
93  } else {
94  $V .= "x";
95  }
96  } else {
97  if ($Mode & 0004000) {
98  $V .= "S"; /* setuid */
99  } else {
100  $V .= "-";
101  }
102  }
103 
104  if ($Mode & 0000040) {
105  $V .= "r";
106  } else {
107  $V .= "-";
108  }
109  if ($Mode & 0000020) {
110  $V .= "w";
111  } else {
112  $V .= "-";
113  }
114  if ($Mode & 0000010) {
115  if ($Mode & 0002000) {
116  $V .= "s"; /* setgid */
117  } else {
118  $V .= "x";
119  }
120  } else {
121  if ($Mode & 0002000) {
122  $V .= "S"; /* setgid */
123  } else {
124  $V .= "-";
125  }
126  }
127 
128  if ($Mode & 0000004) {
129  $V .= "r";
130  } else {
131  $V .= "-";
132  }
133  if ($Mode & 0000002) {
134  $V .= "w";
135  } else {
136  $V .= "-";
137  }
138  if ($Mode & 0000001) {
139  if ($Mode & 0001000) {
140  $V .= "t"; /* sticky bit */
141  } else {
142  $V .= "x";
143  }
144  } else {
145  if ($Mode & 0001000) {
146  $V .= "T"; /* setgid */
147  } else {
148  $V .= "-";
149  }
150  }
151 
152  return($V);
153 } // DirMode2String()
154 
155 $DirGetNonArtifact_Prepared=0;
169 function DirGetNonArtifact($UploadtreePk, $uploadtree_tablename='uploadtree')
170 {
171  $Children = array();
172 
173  /* Get contents of this directory */
174  global $DirGetNonArtifact_Prepared;
175  global $container;
176  $dbManager = $container->get('db.manager');
177  if (! $DirGetNonArtifact_Prepared) {
178  $DirGetNonArtifact_Prepared=1;
179  $sql = "SELECT * FROM $uploadtree_tablename LEFT JOIN pfile ON pfile_pk = pfile_fk WHERE parent = $1";
180  $dbManager->prepare($stmt=__METHOD__.".$uploadtree_tablename",$sql);
181  $result = $dbManager->execute($stmt,array($UploadtreePk));
182  while ($child = $dbManager->fetchArray($result)) {
183  $Children[] = $child;
184  }
185  $dbManager->freeResult($result);
186  }
187  $Recurse=null;
188  foreach ($Children as $C) {
189  if (empty($C['ufile_mode'])) {
190  continue;
191  }
192  if (! Isartifact($C['ufile_mode'])) {
193  return($UploadtreePk);
194  }
195  if (($C['ufile_name'] == 'artifact.dir') ||
196  ($C['ufile_name'] == 'artifact.unpacked')) {
197  $Recurse = DirGetNonArtifact($C['uploadtree_pk'], $uploadtree_tablename);
198  }
199  }
200  if (! empty($Recurse)) {
201  return(DirGetNonArtifact($Recurse, $uploadtree_tablename));
202  }
203  return($UploadtreePk);
204 } // DirGetNonArtifact()
205 
206 
216 function _DirCmp($a,$b)
217 {
218  return(strcasecmp($a['ufile_name'],$b['ufile_name']));
219 } // _DirCmp()
220 
221 
233 function Dir2Path($uploadtree_pk, $uploadtree_tablename='uploadtree')
234 {
235  global $PG_CONN;
236 
237  $uploadtreeArray = array();
238 
239  if (empty($uploadtree_pk)) {
240  return $uploadtreeArray;
241  }
242 
243  while (! empty($uploadtree_pk)) {
244  $sql = "SELECT parent, upload_fk, ufile_mode, ufile_name, uploadtree_pk from $uploadtree_tablename where uploadtree_pk='$uploadtree_pk'";
245  $result = pg_query($PG_CONN, $sql);
246  DBCheckResult($result, $sql, __FILE__, __LINE__);
247  $Row = pg_fetch_assoc($result);
248  pg_free_result($result);
249  if (!Isartifact($Row['ufile_mode'])) {
250  array_unshift($uploadtreeArray, $Row);
251  }
252  $uploadtree_pk = $Row['parent'];
253  }
254 
255  return($uploadtreeArray);
256 } // Dir2Path()
257 
274 function Dir2Browse ($Mod, $UploadtreePk, $LinkLast=NULL,
275 $ShowBox=1, $ShowMicro=NULL, $Enumerate=-1, $PreText='', $PostText='', $uploadtree_tablename="uploadtree")
276 {
277  $V = "";
278  if ($ShowBox) {
279  $V .= "<div style='border: thin dotted gray; background-color:lightyellow'>\n";
280  }
281 
282  if ($Enumerate >= 0) {
283  $V .= "<table border=0 width='100%'><tr><td width='5%'>";
284  $V .= "<font size='+2'>" . number_format($Enumerate,0,"",",") . ":</font>";
285  $V .= "</td><td>";
286  }
287 
288  $Opt = Traceback_parm_keep(array("folder","show"));
289  $Uri = Traceback_uri() . "?mod=$Mod";
290 
291  /* Get array of upload recs for this path, in top down order.
292  This does not contain artifacts.
293  */
294  $Path = Dir2Path($UploadtreePk, $uploadtree_tablename);
295  $Last = &$Path[count($Path)-1];
296 
297  $V .= "<font class='text'>\n";
298 
299  /* Add in additional text */
300  if (! empty($PreText)) {
301  $V .= "$PreText<br>\n";
302  }
303 
304  /* Get the FOLDER list for the upload */
305  $text = _("Folder");
306  $V .= "<b>$text</b>: ";
307  if (array_key_exists(0, $Path)) {
308  $List = FolderGetFromUpload($Path[0]['upload_fk']);
309  $Uri2 = Traceback_uri() . "?mod=browse" . Traceback_parm_keep(array("show"));
310  for ($i = 0; $i < count($List); $i ++) {
311  $Folder = $List[$i]['folder_pk'];
312  $FolderName = htmlentities($List[$i]['folder_name']);
313  $V .= "<b><a href='$Uri2&folder=$Folder'>$FolderName</a></b>/ ";
314  }
315  }
316 
317  /* Print the upload, itself (on the next line since it is not a folder) */
318  if (count($Path) == - 1) {
319  $Upload = $Path[0]['upload_fk'];
320  $UploadName = htmlentities($Path[0]['ufile_name']);
321  $UploadtreePk = $Path[0]['uploadtree_pk'];
322  $V .= "<br><b><a href='$Uri2&folder=$Folder&upload=$Upload&item=$UploadtreePk'>$UploadName</a></b>";
323  } else {
324  $V .= "<br>";
325  }
326 
327  /* Show the path within the upload */
328  for ($p = 0; ! empty($Path[$p]['uploadtree_pk']); $p ++) {
329  $P = &$Path[$p];
330  if (empty($P['ufile_name'])) {
331  continue;
332  }
333  $UploadtreePk = $P['uploadtree_pk'];
334  if ($p > 0) {
335  $V .= "/";
336  }
337  if (! empty($LinkLast) || ($P != $Last)) {
338  if ($P == $Last) {
339  $Uri = Traceback_uri() . "?mod=$LinkLast";
340  }
341  $V .= "<a href='$Uri&upload=" . $P['upload_fk'] . $Opt . "&item=" . $UploadtreePk . "'>";
342  }
343 
344  if (Isdir($P['ufile_mode'])) {
345  $V .= $P['ufile_name'];
346  } else {
347  $V .= "<b>" . $P['ufile_name'] . "</b>";
348  }
349 
350  if (! empty($LinkLast) || ($P != $Last)) {
351  $V .= "</a>";
352  }
353  }
354  $V .= "</font>\n";
355 
356  if (! empty($ShowMicro)) {
357  $MenuDepth = 0; /* unused: depth of micro menu */
358  $V .= menu_to_1html(menu_find($ShowMicro,$MenuDepth),1);
359  }
360 
361  if ($Enumerate >= 0) {
362  if ($PostText) {
363  $V .= "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$PostText";
364  }
365  $V .= "</td></tr></table>";
366  }
367 
368  if ($ShowBox) {
369  $V .= "</div>\n";
370  }
371  return($V);
372 } // Dir2Browse()
373 
388 function Dir2BrowseUpload ($Mod, $UploadPk, $LinkLast=NULL, $ShowBox=1, $ShowMicro=NULL, $uploadtree_tablename='uploadtree')
389 {
390  global $PG_CONN;
391  /* Find the file associated with the upload */
392  $sql = "SELECT uploadtree_pk FROM upload INNER JOIN $uploadtree_tablename ON upload_fk = '$UploadPk' AND parent is null;";
393  $result = pg_query($PG_CONN, $sql);
394  DBCheckResult($result, $sql, __FILE__, __LINE__);
395  $row = pg_fetch_assoc($result);
396  $UploadtreePk = $row['uploadtree_pk'];
397  pg_free_result($result);
398  return(Dir2Browse($Mod,$UploadtreePk,$LinkLast,$ShowBox,$ShowMicro, -1, '','', $uploadtree_tablename));
399 } // Dir2BrowseUpload()
400 
417 function Dir2FileList (&$Listing, $IfDirPlugin, $IfFilePlugin, $Count=-1, $ShowPhrase=0)
418 {
419  $LastPfilePk = -1;
420  $V = "";
421  while (($R = pg_fetch_assoc($Listing)) && ! empty($R['uploadtree_pk'])) {
422  if (array_key_exists("licenses", $R)) {
423  $Licenses = $R["licenses"];
424  } else {
425  $Licenses = '';
426  }
427 
428  $Phrase='';
429  if ($ShowPhrase && ! empty($R['phrase_text'])) {
430  $text = _("Phrase");
431  $Phrase = "<b>$text:</b> " . htmlentities($R['phrase_text']);
432  }
433 
434  if ((IsDir($R['ufile_mode'])) || (Iscontainer($R['ufile_mode']))) {
435  $V .= "<P />\n";
436  $V .= Dir2Browse("browse",$R['uploadtree_pk'],$IfDirPlugin,1,
437  null,$Count,$Phrase, $Licenses) . "\n";
438  } else if ($R['pfile_fk'] != $LastPfilePk) {
439  $V .= "<P />\n";
440  $V .= Dir2Browse("browse",$R['uploadtree_pk'],$IfFilePlugin,1,
441  null,$Count,$Phrase, $Licenses) . "\n";
442  $LastPfilePk = $R['pfile_fk'];
443  } else {
444  $V .= "<div style='margin-left:2em;'>";
445  $V .= Dir2Browse("browse",$R['uploadtree_pk'],$IfFilePlugin,1,
446  null,$Count,$Phrase, $Licenses) . "\n";
447  $V .= "</div>";
448  }
449  $Count++;
450  }
451  return($V);
452 } // Dir2FileList()
453 
468 function UploadtreeFileList($Listing, $IfDirPlugin, $IfFilePlugin, $Count=-1, $ShowPhrase=0)
469 {
470  $LastPfilePk = -1;
471  $V = "";
472  foreach ($Listing as $R) {
473  if (array_key_exists("licenses", $R)) {
474  $Licenses = $R["licenses"];
475  } else {
476  $Licenses = '';
477  }
478 
479  $Phrase='';
480  if ($ShowPhrase && ! empty($R['phrase_text'])) {
481  $text = _("Phrase");
482  $Phrase = "<b>$text:</b> " . htmlentities($R['phrase_text']);
483  }
484 
485  $uploadtree_tablename = GetUploadtreeTableName($R['upload_fk']);
486 
487  if ((IsDir($R['ufile_mode'])) || (Iscontainer($R['ufile_mode']))) {
488  $V .= "<P />\n";
489  $V .= Dir2Browse("browse",$R['uploadtree_pk'],$IfDirPlugin,1,NULL,$Count,$Phrase,$Licenses,$uploadtree_tablename) . "\n";
490  } else if ($R['pfile_fk'] != $LastPfilePk) {
491  $V .= "<P />\n";
492  $V .= Dir2Browse("browse",$R['uploadtree_pk'],$IfFilePlugin,1,NULL,$Count,$Phrase,$Licenses,$uploadtree_tablename) . "\n";
493  $LastPfilePk = $R['pfile_fk'];
494  } else {
495  $V .= "<div style='margin-left:2em;'>";
496  $V .= Dir2Browse("browse",$R['uploadtree_pk'],$IfFilePlugin,1,NULL,$Count,$Phrase,$Licenses,$uploadtree_tablename) . "\n";
497  $V .= "</div>";
498  }
499  $Count++;
500  }
501  return($V);
502 } // UploadtreeFileList()
503 
504 
518 function GetNonArtifactChildren($uploadtree_pk, $uploadtree_tablename='uploadtree')
519 {
520  global $container;
522  $dbManager = $container->get('db.manager');
523 
524  /* Find all the children */
525  $sql = "select {$uploadtree_tablename}.*, pfile_size, pfile_mimetypefk from $uploadtree_tablename
526  left outer join pfile on (pfile_pk=pfile_fk)
527  where parent=$1 ORDER BY lft";
528  $dbManager->prepare($stmt=__METHOD__."$uploadtree_tablename",$sql);
529  $result = $dbManager->execute($stmt,array($uploadtree_pk));
530  $children = $dbManager->fetchAll($result);
531  $dbManager->freeResult($result);
532  if (count($children) == 0) {
533  return $children;
534  }
535 
536  /* Loop through each child and replace any artifacts with their
537  non artifact child. Or skip them if they are not containers.
538  */
539  $foundChildren = array();
540  foreach ($children as $key => $child) {
541  if (Isartifact($child['ufile_mode'])) {
542  if (Iscontainer($child['ufile_mode'])) {
543  unset($children[$key]);
544  $NonAChildren = GetNonArtifactChildren($child['uploadtree_pk'], $uploadtree_tablename);
545  if ($NonAChildren) {
546  $foundChildren = array_merge($foundChildren, $NonAChildren);
547  }
548  } else {
549  unset($children[$key]);
550  }
551  } else {
552  $foundChildren[$key] = $child;
553  }
554  }
555  // uasort($foundChildren, '_DirCmp');
556  return $foundChildren;
557 }
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
Traceback_uri()
Get the URI without query to this location.
Dir2FileList(&$Listing, $IfDirPlugin, $IfFilePlugin, $Count=-1, $ShowPhrase=0)
Given an array of pfiles/uploadtree, sorted by pfile, list all of the breadcrumbs for each file...
Definition: common-dir.php:417
Dir2Path($uploadtree_pk, $uploadtree_tablename='uploadtree')
Return the path (without artifacts) of an uploadtree_pk.
Definition: common-dir.php:233
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...
FolderGetFromUpload($Uploadpk, $Folder=-1, $Stop=-1)
DEPRECATED! Given an upload number, return the folder path in an array containing folder_pk and name...
DirMode2String($Mode)
Convert a file mode to string values.
Definition: common-dir.php:61
int IsDir(char *Fname)
Given a filename, is it a directory?
Definition: utils.c:330
Iscontainer($mode)
Definition: common-dir.php:49
UploadtreeFileList($Listing, $IfDirPlugin, $IfFilePlugin, $Count=-1, $ShowPhrase=0)
Given an array of pfiles/uploadtree, sorted by pfile, list all of the breadcrumbs for each file...
Definition: common-dir.php:468
DirGetNonArtifact($UploadtreePk, $uploadtree_tablename='uploadtree')
Given an artifact directory (uploadtree_pk), return the first non-artifact directory (uploadtree_pk)...
Definition: common-dir.php:169
menu_to_1html($Menu, $ShowRefresh=1, $ShowTraceback=0, $ShowAll=1)
Take a menu and render it as one HTML line.
Isdir($mode)
Definition: common-dir.php:31
foreach($Options as $Option=> $OptVal) if(0==$reference_flag &&0==$nomos_flag) $PG_CONN
_DirCmp($a, $b)
Compare function for usort() on directory items.
Definition: common-dir.php:216
DBCheckResult($result, $sql, $filenm, $lineno)
Check the postgres result for unexpected errors. If found, treat them as fatal.
Definition: common-db.php:198
Traceback_parm_keep($List)
Create a new URI, keeping only these items.
Isartifact($mode)
Definition: common-dir.php:40
Dir2BrowseUpload($Mod, $UploadPk, $LinkLast=NULL, $ShowBox=1, $ShowMicro=NULL, $uploadtree_tablename='uploadtree')
Get an html links string of a file browse path.
Definition: common-dir.php:388