FOSSology  4.4.0
Open Source License Compliance by Open Source Software
common-dir.php
Go to the documentation of this file.
1 <?php
2 /*
3  SPDX-FileCopyrightText: © 2008-2014 Hewlett-Packard Development Company, L.P.
4 
5  SPDX-License-Identifier: LGPL-2.1-only
6 */
7 
14 
20 function Isdir($mode)
21 {
22  return (($mode & 1 << 18) + ($mode & 0040000) != 0);
23 }
29 function Isartifact($mode)
30 {
31  return (($mode & 1 << 28) != 0);
32 }
38 function Iscontainer($mode)
39 {
40  return (($mode & 1 << 29) != 0);
41 }
42 
50 function DirMode2String($Mode)
51 {
52  $V="";
53  if (Isartifact($Mode)) {
54  $V .= "a";
55  } else {
56  $V .= "-";
57  }
58  if (($Mode & 0120000) == 0120000) {
59  $V .= "l";
60  } else {
61  $V .= "-";
62  }
63  if (Isdir($Mode)) {
64  $V .= "d";
65  } else {
66  $V .= "-";
67  }
68 
69  if ($Mode & 0000400) {
70  $V .= "r";
71  } else {
72  $V .= "-";
73  }
74  if ($Mode & 0000200) {
75  $V .= "w";
76  } else {
77  $V .= "-";
78  }
79  if ($Mode & 0000100) {
80  if ($Mode & 0004000) {
81  $V .= "s"; /* setuid */
82  } else {
83  $V .= "x";
84  }
85  } else {
86  if ($Mode & 0004000) {
87  $V .= "S"; /* setuid */
88  } else {
89  $V .= "-";
90  }
91  }
92 
93  if ($Mode & 0000040) {
94  $V .= "r";
95  } else {
96  $V .= "-";
97  }
98  if ($Mode & 0000020) {
99  $V .= "w";
100  } else {
101  $V .= "-";
102  }
103  if ($Mode & 0000010) {
104  if ($Mode & 0002000) {
105  $V .= "s"; /* setgid */
106  } else {
107  $V .= "x";
108  }
109  } else {
110  if ($Mode & 0002000) {
111  $V .= "S"; /* setgid */
112  } else {
113  $V .= "-";
114  }
115  }
116 
117  if ($Mode & 0000004) {
118  $V .= "r";
119  } else {
120  $V .= "-";
121  }
122  if ($Mode & 0000002) {
123  $V .= "w";
124  } else {
125  $V .= "-";
126  }
127  if ($Mode & 0000001) {
128  if ($Mode & 0001000) {
129  $V .= "t"; /* sticky bit */
130  } else {
131  $V .= "x";
132  }
133  } else {
134  if ($Mode & 0001000) {
135  $V .= "T"; /* setgid */
136  } else {
137  $V .= "-";
138  }
139  }
140 
141  return($V);
142 } // DirMode2String()
143 
144 $DirGetNonArtifact_Prepared=0;
158 function DirGetNonArtifact($UploadtreePk, $uploadtree_tablename='uploadtree')
159 {
160  $Children = array();
161 
162  /* Get contents of this directory */
163  global $DirGetNonArtifact_Prepared;
164  global $container;
165  $dbManager = $container->get('db.manager');
166  if (! $DirGetNonArtifact_Prepared) {
167  $DirGetNonArtifact_Prepared=1;
168  $sql = "SELECT * FROM $uploadtree_tablename LEFT JOIN pfile ON pfile_pk = pfile_fk WHERE parent = $1";
169  $dbManager->prepare($stmt=__METHOD__.".$uploadtree_tablename",$sql);
170  $result = $dbManager->execute($stmt,array($UploadtreePk));
171  while ($child = $dbManager->fetchArray($result)) {
172  $Children[] = $child;
173  }
174  $dbManager->freeResult($result);
175  }
176  $Recurse=null;
177  foreach ($Children as $C) {
178  if (empty($C['ufile_mode'])) {
179  continue;
180  }
181  if (! Isartifact($C['ufile_mode'])) {
182  return($UploadtreePk);
183  }
184  if (($C['ufile_name'] == 'artifact.dir') ||
185  ($C['ufile_name'] == 'artifact.unpacked')) {
186  $Recurse = DirGetNonArtifact($C['uploadtree_pk'], $uploadtree_tablename);
187  }
188  }
189  if (! empty($Recurse)) {
190  return(DirGetNonArtifact($Recurse, $uploadtree_tablename));
191  }
192  return($UploadtreePk);
193 } // DirGetNonArtifact()
194 
195 
205 function _DirCmp($a,$b)
206 {
207  return(strcasecmp($a['ufile_name'],$b['ufile_name']));
208 } // _DirCmp()
209 
210 
222 function Dir2Path($uploadtree_pk, $uploadtree_tablename='uploadtree')
223 {
224  global $PG_CONN;
225 
226  $uploadtreeArray = array();
227 
228  if (empty($uploadtree_pk)) {
229  return $uploadtreeArray;
230  }
231 
232  while (! empty($uploadtree_pk)) {
233  $sql = "SELECT parent, upload_fk, ufile_mode, ufile_name, uploadtree_pk from $uploadtree_tablename where uploadtree_pk='$uploadtree_pk'";
234  $result = pg_query($PG_CONN, $sql);
235  DBCheckResult($result, $sql, __FILE__, __LINE__);
236  $Row = pg_fetch_assoc($result);
237  pg_free_result($result);
238  if (!Isartifact($Row['ufile_mode'])) {
239  array_unshift($uploadtreeArray, $Row);
240  }
241  $uploadtree_pk = $Row['parent'];
242  }
243 
244  return($uploadtreeArray);
245 } // Dir2Path()
246 
263 function Dir2Browse ($Mod, $UploadtreePk, $LinkLast=NULL,
264 $ShowBox=1, $ShowMicro=NULL, $Enumerate=-1, $PreText='', $PostText='', $uploadtree_tablename="uploadtree")
265 {
266  $V = "";
267  if ($ShowBox) {
268  $V .= "<div class='alert alert-info' style='padding:5px;'>\n";
269  }
270 
271  if ($Enumerate >= 0) {
272  $V .= "<table border=0 width='100%'><tr><td width='5%'>";
273  $V .= "<font size='+2'>" . number_format($Enumerate,0,"",",") . ":</font>";
274  $V .= "</td><td>";
275  }
276 
277  $Opt = Traceback_parm_keep(array("folder","show"));
278  $Uri = Traceback_uri() . "?mod=$Mod";
279 
280  /* Get array of upload recs for this path, in top down order.
281  This does not contain artifacts.
282  */
283  $Path = Dir2Path($UploadtreePk, $uploadtree_tablename);
284  $Last = &$Path[count($Path)-1];
285 
286  /* Add in additional text */
287  if (! empty($PreText)) {
288  $V .= "$PreText<br>\n";
289  }
290 
291  /* Get the FOLDER list for the upload */
292  $text = _("Folder");
293  $V .= "<b>$text</b>: ";
294  if (array_key_exists(0, $Path)) {
295  $List = FolderGetFromUpload($Path[0]['upload_fk']);
296  $Uri2 = Traceback_uri() . "?mod=browse" . Traceback_parm_keep(array("show"));
297  for ($i = 0; $i < count($List); $i ++) {
298  $Folder = $List[$i]['folder_pk'];
299  $FolderName = htmlentities($List[$i]['folder_name']);
300  $V .= "<b><a href='$Uri2&folder=$Folder'>$FolderName</a></b>/ ";
301  }
302  }
303 
304  /* Print the upload, itself (on the next line since it is not a folder) */
305  if (count($Path) == - 1) {
306  $Upload = $Path[0]['upload_fk'];
307  $UploadName = htmlentities($Path[0]['ufile_name']);
308  $UploadtreePk = $Path[0]['uploadtree_pk'];
309  $V .= "<br><b><a href='$Uri2&folder=$Folder&upload=$Upload&item=$UploadtreePk'>$UploadName</a></b>";
310  } else {
311  $V .= "<br>";
312  }
313 
314  /* Show the path within the upload */
315  for ($p = 0; ! empty($Path[$p]['uploadtree_pk']); $p ++) {
316  $P = &$Path[$p];
317  if (empty($P['ufile_name'])) {
318  continue;
319  }
320  $UploadtreePk = $P['uploadtree_pk'];
321  if ($p > 0) {
322  $V .= "/";
323  }
324  if (! empty($LinkLast) || ($P != $Last)) {
325  if ($P == $Last) {
326  $Uri = Traceback_uri() . "?mod=$LinkLast";
327  }
328  $V .= "<a href='$Uri&upload=" . $P['upload_fk'] . $Opt . "&item=" . $UploadtreePk . "'>";
329  }
330 
331  if (Isdir($P['ufile_mode'])) {
332  $V .= $P['ufile_name'];
333  } else {
334  $V .= "<b>" . $P['ufile_name'] . "</b>";
335  }
336 
337  if (! empty($LinkLast) || ($P != $Last)) {
338  $V .= "</a>";
339  }
340  }
341 
342  if (! empty($ShowMicro)) {
343  $MenuDepth = 0; /* unused: depth of micro menu */
344  $V .= menu_to_1html(menu_find($ShowMicro,$MenuDepth),1);
345  }
346 
347  if ($Enumerate >= 0) {
348  if ($PostText) {
349  $V .= "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$PostText";
350  }
351  $V .= "</td></tr></table>";
352  }
353 
354  if ($ShowBox) {
355  $V .= "</div>\n";
356  }
357  return($V);
358 } // Dir2Browse()
359 
374 function Dir2BrowseUpload ($Mod, $UploadPk, $LinkLast=NULL, $ShowBox=1, $ShowMicro=NULL, $uploadtree_tablename='uploadtree')
375 {
376  global $PG_CONN;
377  /* Find the file associated with the upload */
378  $sql = "SELECT uploadtree_pk FROM upload INNER JOIN $uploadtree_tablename ON upload_fk = '$UploadPk' AND parent is null;";
379  $result = pg_query($PG_CONN, $sql);
380  DBCheckResult($result, $sql, __FILE__, __LINE__);
381  $row = pg_fetch_assoc($result);
382  $UploadtreePk = $row['uploadtree_pk'];
383  pg_free_result($result);
384  return(Dir2Browse($Mod,$UploadtreePk,$LinkLast,$ShowBox,$ShowMicro, -1, '','', $uploadtree_tablename));
385 } // Dir2BrowseUpload()
386 
403 function Dir2FileList (&$Listing, $IfDirPlugin, $IfFilePlugin, $Count=-1, $ShowPhrase=0)
404 {
405  $LastPfilePk = -1;
406  $V = "";
407  while (($R = pg_fetch_assoc($Listing)) && ! empty($R['uploadtree_pk'])) {
408  if (array_key_exists("licenses", $R)) {
409  $Licenses = $R["licenses"];
410  } else {
411  $Licenses = '';
412  }
413 
414  $Phrase='';
415  if ($ShowPhrase && ! empty($R['phrase_text'])) {
416  $text = _("Phrase");
417  $Phrase = "<b>$text:</b> " . htmlentities($R['phrase_text']);
418  }
419 
420  if ((IsDir($R['ufile_mode'])) || (Iscontainer($R['ufile_mode']))) {
421  $V .= "<P />\n";
422  $V .= Dir2Browse("browse",$R['uploadtree_pk'],$IfDirPlugin,1,
423  null,$Count,$Phrase, $Licenses) . "\n";
424  } else if ($R['pfile_fk'] != $LastPfilePk) {
425  $V .= "<P />\n";
426  $V .= Dir2Browse("browse",$R['uploadtree_pk'],$IfFilePlugin,1,
427  null,$Count,$Phrase, $Licenses) . "\n";
428  $LastPfilePk = $R['pfile_fk'];
429  } else {
430  $V .= "<div style='margin-left:2em;'>";
431  $V .= Dir2Browse("browse",$R['uploadtree_pk'],$IfFilePlugin,1,
432  null,$Count,$Phrase, $Licenses) . "\n";
433  $V .= "</div>";
434  }
435  $Count++;
436  }
437  return($V);
438 } // Dir2FileList()
439 
454 function UploadtreeFileList($Listing, $IfDirPlugin, $IfFilePlugin, $Count=-1, $ShowPhrase=0)
455 {
456  $LastPfilePk = -1;
457  $V = "";
458  foreach ($Listing as $R) {
459  if (array_key_exists("licenses", $R)) {
460  $Licenses = $R["licenses"];
461  } else {
462  $Licenses = '';
463  }
464 
465  $Phrase='';
466  if ($ShowPhrase && ! empty($R['phrase_text'])) {
467  $text = _("Phrase");
468  $Phrase = "<b>$text:</b> " . htmlentities($R['phrase_text']);
469  }
470 
471  $uploadtree_tablename = GetUploadtreeTableName($R['upload_fk']);
472 
473  if ((IsDir($R['ufile_mode'])) || (Iscontainer($R['ufile_mode']))) {
474  $V .= "<P />\n";
475  $V .= Dir2Browse("browse",$R['uploadtree_pk'],$IfDirPlugin,1,NULL,$Count,$Phrase,$Licenses,$uploadtree_tablename) . "\n";
476  } else if ($R['pfile_fk'] != $LastPfilePk) {
477  $V .= "<P />\n";
478  $V .= Dir2Browse("browse",$R['uploadtree_pk'],$IfFilePlugin,1,NULL,$Count,$Phrase,$Licenses,$uploadtree_tablename) . "\n";
479  $LastPfilePk = $R['pfile_fk'];
480  } else {
481  $V .= "<div style='margin-left:2em;'>";
482  $V .= Dir2Browse("browse",$R['uploadtree_pk'],$IfFilePlugin,1,NULL,$Count,$Phrase,$Licenses,$uploadtree_tablename) . "\n";
483  $V .= "</div>";
484  }
485  $Count++;
486  }
487  return($V);
488 } // UploadtreeFileList()
489 
490 
504 function GetNonArtifactChildren($uploadtree_pk, $uploadtree_tablename='uploadtree')
505 {
506  global $container;
508  $dbManager = $container->get('db.manager');
509 
510  /* Find all the children */
511  $sql = "select {$uploadtree_tablename}.*, pfile_size, pfile_mimetypefk from $uploadtree_tablename
512  left outer join pfile on (pfile_pk=pfile_fk)
513  where parent=$1 ORDER BY lft";
514  $dbManager->prepare($stmt=__METHOD__."$uploadtree_tablename",$sql);
515  $result = $dbManager->execute($stmt,array($uploadtree_pk));
516  $children = $dbManager->fetchAll($result);
517  $dbManager->freeResult($result);
518  if (count($children) == 0) {
519  return $children;
520  }
521 
522  /* Loop through each child and replace any artifacts with their
523  non artifact child. Or skip them if they are not containers.
524  */
525  $foundChildren = array();
526  foreach ($children as $key => $child) {
527  if (Isartifact($child['ufile_mode'])) {
528  unset($children[$key]);
529  if (Iscontainer($child['ufile_mode'])) {
530  $NonAChildren = GetNonArtifactChildren($child['uploadtree_pk'], $uploadtree_tablename);
531  if ($NonAChildren) {
532  $foundChildren = array_merge($foundChildren, $NonAChildren);
533  }
534  }
535  } else {
536  $foundChildren[$key] = $child;
537  }
538  }
539  // uasort($foundChildren, '_DirCmp');
540  return $foundChildren;
541 }
DBCheckResult($result, $sql, $filenm, $lineno)
Check the postgres result for unexpected errors. If found, treat them as fatal.
Definition: common-db.php:187
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:454
_DirCmp($a, $b)
Compare function for usort() on directory items.
Definition: common-dir.php:205
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:374
Isdir($mode)
Definition: common-dir.php:20
Isartifact($mode)
Definition: common-dir.php:29
DirGetNonArtifact($UploadtreePk, $uploadtree_tablename='uploadtree')
Given an artifact directory (uploadtree_pk), return the first non-artifact directory (uploadtree_pk).
Definition: common-dir.php:158
DirMode2String($Mode)
Convert a file mode to string values.
Definition: common-dir.php:50
Iscontainer($mode)
Definition: common-dir.php:38
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:403
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
Dir2Path($uploadtree_pk, $uploadtree_tablename='uploadtree')
Return the path (without artifacts) of an uploadtree_pk.
Definition: common-dir.php:222
FolderGetFromUpload($Uploadpk, $Folder=-1, $Stop=-1)
DEPRECATED! Given an upload number, return the folder path in an array containing folder_pk and name.
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.
menu_to_1html($Menu, $ShowRefresh=1, $ShowTraceback=0, $ShowAll=1)
Take a menu and render it as one HTML line.
Traceback_uri()
Get the URI without query to this location.
Definition: common-parm.php:97
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
foreach($Options as $Option=> $OptVal) if(0==$reference_flag &&0==$nomos_flag) $PG_CONN
int IsDir(char *Fname)
Given a filename, is it a directory?
Definition: utils.c:319