FOSSology  3.2.0rc1
Open Source License Compliance by Open Source Software
common-folders.php
Go to the documentation of this file.
1 <?php
2 /***********************************************************
3  Copyright (C) 2008-2015 Hewlett-Packard Development Company, L.P.
4  Copyright (C) 2014-2017 Siemens AG
5 
6  This library is free software; you can redistribute it and/or
7  modify it under the terms of the GNU Lesser General Public
8  License version 2.1 as published by the Free Software Foundation.
9 
10  This library 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 GNU
13  Lesser General Public License for more details.
14 
15  You should have received a copy of the GNU Lesser General Public License
16  along with this library; if not, write to the Free Software Foundation, Inc.0
17  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18  ***********************************************************/
19 
23 
37 function FolderGetTop()
38 {
39  /* Get the list of folders */
40  if (! empty($_SESSION['Folder'])) {
41  return ($_SESSION['Folder']);
42  }
43  global $PG_CONN;
44  if (empty($PG_CONN)) {
45  return;
46  }
47  $sql = "SELECT root_folder_fk FROM users ORDER BY user_pk ASC LIMIT 1";
48  $result = pg_query($PG_CONN, $sql);
49  DBCheckResult($result, $sql, __FILE__, __LINE__);
50  $row = pg_fetch_assoc($result);
51  pg_free_result($result);
52  return($row['root_folder_fk']);
53 } // FolderGetTop()
54 
62 {
63  global $PG_CONN;
64 
65  /* validate inputs */
66  $user_pk = Auth::getUserId();
67 
68  /* everyone has a user_pk, even if not logged in. But verify. */
69  if (empty($user_pk)) {
70  return "__FILE__:__LINE__ GetUserRootFolder(Not logged in)<br>";
71  }
72 
73  /* Get users root folder */
74  $sql = "select root_folder_fk from users where user_pk=$user_pk";
75  $result = pg_query($PG_CONN, $sql);
76  DBCheckResult($result, $sql, __FILE__, __LINE__);
77  $UsersRow = pg_fetch_assoc($result);
78  $root_folder_fk = $UsersRow['root_folder_fk'];
79  pg_free_result($result);
80  if (empty($root_folder_fk)) {
81  $text = _("Missing root_folder_fk for user ");
82  fatal("<h2>".$text.$user_pk."</h2>", __FILE__, __LINE__);
83  }
84  return $root_folder_fk;
85 } // GetUserRootFolder()
86 
101 function Folder2Path($folder_pk)
102 {
103  global $PG_CONN;
104  $FolderList = array();
105 
106  /* validate inputs */
107  if (empty($folder_pk)) {
108  return __FILE__.":".__LINE__." Folder2Browse(empty)<br>";
109  }
110 
111  /* Get users root folder */
112  $root_folder_fk = GetUserRootFolder(); // will fail if no user session
113 
114  while ($folder_pk) {
115  $sql = "select folder_pk, folder_name from folder where folder_pk='$folder_pk'";
116  $result = pg_query($PG_CONN, $sql);
117  DBCheckResult($result, $sql, __FILE__, __LINE__);
118  $FolderRow = pg_fetch_assoc($result);
119  pg_free_result($result);
120  array_unshift($FolderList, $FolderRow);
121 
122  // Limit folders to user root. Limit to an arbitrary 20 folders as a failsafe
123  // against this loop going infinite.
124  if (($folder_pk == $root_folder_fk) || (count($FolderList)>20)) {
125  break;
126  }
127 
128  $sql = "select parent_fk from foldercontents where child_id='$folder_pk' and foldercontents_mode=".FolderDao::MODE_FOLDER;
129  $result = pg_query($PG_CONN, $sql);
130  DBCheckResult($result, $sql, __FILE__, __LINE__);
131  $FolderRow = pg_fetch_assoc($result);
132  pg_free_result($result);
133  $folder_pk = $FolderRow['parent_fk'];
134  }
135  return($FolderList);
136 } // Folder2Path()
137 
138 
149 function GetFolderFromItem($upload_pk="", $uploadtree_pk = "")
150 {
151  global $PG_CONN;
152 
153  /* validate inputs */
154  if (empty($uploadtree_pk) && empty($upload_pk)) {
155  return "__FILE__:__LINE__ GetFolderFromItem(empty)<br>";
156  }
157 
158  if (empty($upload_pk)) {
159  $UTrec = GetSingleRec("uploadtree", "where uploadtree_pk=$uploadtree_pk");
160  $upload_pk = $UTrec['upload_fk'];
161  }
162 
163  $sql = "select parent_fk from foldercontents where child_id='$upload_pk' and foldercontents_mode=".FolderDao::MODE_UPLOAD;
164  $result = pg_query($PG_CONN, $sql);
165  DBCheckResult($result, $sql, __FILE__, __LINE__);
166  $FolderRow = pg_fetch_assoc($result);
167  pg_free_result($result);
168  return $FolderRow['parent_fk'];
169 } // GetFolderFromItem()
170 
171 
189 function FolderListOption($ParentFolder,$Depth, $IncludeTop=1, $SelectId=-1, $linkParent=false, $OldParent=0)
190 {
191  if ($ParentFolder == "-1") {
192  $ParentFolder = FolderGetTop();
193  }
194  if (empty($ParentFolder)) {
195  return;
196  }
197  global $PG_CONN;
198  if (empty($PG_CONN)) {
199  return;
200  }
201  $V = "";
202 
203  if (($Depth != 0) || $IncludeTop) {
204  if ($ParentFolder == $SelectId) {
205  $V .= "<option value='$ParentFolder' SELECTED>";
206  } elseif ($linkParent) {
207  if (empty($OldParent)) {
208  $OldParent = 0;
209  }
210  $V .= "<option value='$OldParent $ParentFolder'>";
211  } else {
212  $V .= "<option value='$ParentFolder'>";
213  }
214  if ($Depth != 0) {
215  $V .= "&nbsp;&nbsp;";
216  }
217  for ($i=1; $i < $Depth; $i++) {
218  $V .= "&nbsp;&nbsp;";
219  }
220 
221  /* Load this folder's name */
222  $sql = "SELECT folder_name FROM folder WHERE folder_pk=$ParentFolder LIMIT 1;";
223  $result = pg_query($PG_CONN, $sql);
224  DBCheckResult($result, $sql, __FILE__, __LINE__);
225  $row = pg_fetch_assoc($result);
226  $Name = trim($row['folder_name']);
227  if ($Name == "") {
228  $Name = "[default]";
229  }
230 
231  /* Load any subfolders */
232  /* Now create the HTML */
233  $V .= htmlentities($Name);
234  $V .= "</option>\n";
235  }
236  /* Load any subfolders */
237  $sql = "SELECT folder.folder_pk, folder.folder_name AS name,
238  folder.folder_desc AS description,
239  foldercontents.parent_fk AS parent,
240  foldercontents.foldercontents_mode,
241  NULL AS ts, NULL AS upload_pk, NULL AS pfile_fk, NULL AS ufile_mode
242  FROM folder, foldercontents
243  WHERE foldercontents.foldercontents_mode = ".FolderDao::MODE_FOLDER."
244  AND foldercontents.parent_fk =$ParentFolder
245  AND foldercontents.child_id = folder.folder_pk
246  AND folder.folder_pk is not null
247  ORDER BY name";
248  $result = pg_query($PG_CONN, $sql);
249  DBCheckResult($result, $sql, __FILE__, __LINE__);
250  if (pg_num_rows($result) > 0) {
251  $Hide = "";
252  if ($Depth > 0) {
253  $Hide = "style='display:none;'";
254  }
255  while ($row = pg_fetch_assoc($result)) {
256  $V .= FolderListOption($row['folder_pk'], $Depth+1,$IncludeTop,$SelectId,$linkParent,$row['parent']);
257  }
258  }
259  pg_free_result($result);
260  return($V);
261 } // FolderListOption()
262 
274 function FolderGetName($FolderPk,$Top=-1)
275 {
276  global $PG_CONN;
277  if ($Top == -1) {
278  $Top = FolderGetTop();
279  }
280  $sql = "SELECT folder_name,foldercontents.parent_fk FROM folder
281  LEFT JOIN foldercontents ON foldercontents_mode = ".FolderDao::MODE_FOLDER."
282  AND child_id = '$FolderPk'
283  WHERE folder_pk = '$FolderPk'
284  LIMIT 1;";
285  $result = pg_query($PG_CONN, $sql);
286  DBCheckResult($result, $sql, __FILE__, __LINE__);
287  $row = pg_fetch_assoc($result);
288  $Parent = $row['parent_fk'];
289  $Name = $row['folder_name'];
290  if (! empty($Parent) && ($FolderPk != $Top)) {
291  $Name = FolderGetName($Parent,$Top) . "/" . $Name;
292  }
293  return($Name);
294 }
295 
296 
305 function FolderGetFromUpload($Uploadpk, $Folder = -1, $Stop = -1)
306 {
307  global $PG_CONN;
308  if (empty($PG_CONN)) {
309  return;
310  }
311  if (empty($Uploadpk)) {
312  return;
313  }
314  if ($Stop == - 1) {
315  $Stop = FolderGetTop();
316  }
317  if ($Folder == $Stop) {
318  return;
319  }
320 
321  $sql = "";
322  $Parm = "";
323  if ($Folder < 0) {
324  /* Mode 2 means child_id is an upload_pk */
325  $Parm = $Uploadpk;
326  $sql = "SELECT foldercontents.parent_fk,folder_name FROM foldercontents
327  INNER JOIN folder ON foldercontents.parent_fk = folder.folder_pk
328  AND foldercontents.foldercontents_mode = " . FolderDao::MODE_UPLOAD."
329  WHERE foldercontents.child_id = $Parm LIMIT 1;";
330  } else {
331  /* Mode 1 means child_id is a folder_pk */
332  $Parm = $Folder;
333  $sql = "SELECT foldercontents.parent_fk,folder_name FROM foldercontents
334  INNER JOIN folder ON foldercontents.parent_fk = folder.folder_pk
335  AND foldercontents.foldercontents_mode = 1
336  WHERE foldercontents.child_id = $Parm LIMIT 1;";
337  }
338  $result = pg_query($PG_CONN, $sql);
339  DBCheckResult($result, $sql, __FILE__, __LINE__);
340  $R = pg_fetch_assoc($result);
341  if (empty($R['parent_fk'])) {
342  pg_free_result($result);
343  return;
344  }
345  $V = array();
346  $V['folder_pk'] = $R['parent_fk'];
347  $V['folder_name'] = $R['folder_name'];
348  if ($R['parent_fk'] != 0) {
349  $List = FolderGetFromUpload($Uploadpk, $R['parent_fk'],$Stop);
350  }
351  if (empty($List)) {
352  $List = array();
353  }
354  array_push($List,$V);
355  pg_free_result($result);
356  return($List);
357 } // FolderGetFromUpload()
358 
359 
372 function FolderListUploads_perm($ParentFolder, $perm)
373 {
374  global $PG_CONN;
375 
376  if (empty($PG_CONN)) {
377  return;
378  }
379  if (empty($ParentFolder)) {
380  return;
381  }
382  if ($ParentFolder == "-1") {
383  $ParentFolder = GetUserRootFolder();
384  }
385  $groupId = Auth::getGroupId();
386  /* @var $uploadDao UploadDao */
387  $uploadDao = $GLOBALS['container']->get('dao.upload');
388  $List=array();
389 
390  /* Get list of uploads under $ParentFolder */
391  /* mode 2 = upload_fk */
392  $sql = "SELECT upload_pk, upload_desc, upload_ts, upload_filename
393  FROM foldercontents,upload
394  INNER JOIN uploadtree ON upload_fk = upload_pk AND upload.pfile_fk = uploadtree.pfile_fk AND parent IS NULL AND lft IS NOT NULL
395  WHERE foldercontents.parent_fk = '$ParentFolder'
396  AND foldercontents.foldercontents_mode = ".FolderDao::MODE_UPLOAD."
397  AND foldercontents.child_id = upload.upload_pk
398  ORDER BY upload_filename,upload_pk;";
399  $result = pg_query($PG_CONN, $sql);
400  DBCheckResult($result, $sql, __FILE__, __LINE__);
401  while ($R = pg_fetch_assoc($result)) {
402  if (empty($R['upload_pk'])) {
403  continue;
404  }
405  if ($perm == Auth::PERM_READ &&
406  ! $uploadDao->isAccessible($R['upload_pk'], $groupId)) {
407  continue;
408  }
409  if ($perm == Auth::PERM_WRITE &&
410  ! $uploadDao->isEditable($R['upload_pk'], $groupId)) {
411  continue;
412  }
413 
414  $New = array();
415  $New['upload_pk'] = $R['upload_pk'];
416  $New['upload_desc'] = $R['upload_desc'];
417  $New['upload_ts'] = Convert2BrowserTime(substr($R['upload_ts'], 0, 19));
418  $New['name'] = $R['upload_filename'];
419  array_push($List,$New);
420  }
421  pg_free_result($result);
422  return($List);
423 } // FolderListUploads_perm()
424 
439 function FolderListUploadsRecurse($ParentFolder=-1, $FolderPath = '',
440  $perm = Auth::PERM_READ)
441 {
442  global $PG_CONN;
443  if (empty($PG_CONN)) {
444  return array();
445  }
446  if (empty($ParentFolder)) {
447  return array();
448  }
449  if ($perm != Auth::PERM_READ && $perm = Auth::PERM_WRITE) {
450  return array();
451  }
452  if ($ParentFolder == "-1") {
453  $ParentFolder = FolderGetTop();
454  }
455  $groupId = Auth::getGroupId();
456  /* @var $uploadDao UploadDao */
457  $uploadDao = $GLOBALS['container']->get('dao.upload');
458  $List=array();
459 
460  /* Get list of uploads */
461  /* mode 1<<1 = upload_fk */
462  $sql = "SELECT upload_pk, upload_desc, ufile_name, folder_name FROM folder,foldercontents,uploadtree, upload
463  WHERE
464  foldercontents.parent_fk = '$ParentFolder'
465  AND foldercontents.foldercontents_mode = ". FolderDao::MODE_UPLOAD ."
466  AND foldercontents.child_id = upload.upload_pk
467  AND folder.folder_pk = $ParentFolder
468  AND uploadtree.upload_fk = upload.upload_pk
469  AND uploadtree.parent is null
470  ORDER BY uploadtree.ufile_name,upload.upload_desc";
471  $result = pg_query($PG_CONN, $sql);
472  DBCheckResult($result, $sql, __FILE__, __LINE__);
473  while ($R = pg_fetch_assoc($result)) {
474  if (empty($R['upload_pk'])) {
475  continue;
476  }
477  if ($perm == Auth::PERM_READ &&
478  ! $uploadDao->isAccessible($R['upload_pk'], $groupId)) {
479  continue;
480  }
481  if ($perm == Auth::PERM_WRITE &&
482  ! $uploadDao->isEditable($R['upload_pk'], $groupId)) {
483  continue;
484  }
485 
486  $New = array();
487  $New['upload_pk'] = $R['upload_pk'];
488  $New['upload_desc'] = $R['upload_desc'];
489  $New['name'] = $R['ufile_name'];
490  $New['folder'] = $FolderPath . "/" . $R['folder_name'];
491  array_push($List,$New);
492  }
493  pg_free_result($result);
494 
495  /* Get list of subfolders and recurse */
496  /* mode 1<<0 = folder_pk */
497  $sql = "SELECT A.child_id AS id,B.folder_name AS folder,B.folder_name AS subfolder
498  FROM foldercontents AS A
499  INNER JOIN folder AS B ON A.parent_fk = B.folder_pk
500  AND A.foldercontents_mode = ". FolderDao::MODE_FOLDER ."
501  AND A.parent_fk = '$ParentFolder'
502  AND B.folder_pk = $ParentFolder
503  ORDER BY B.folder_name;";
504  $result = pg_query($PG_CONN, $sql);
505  DBCheckResult($result, $sql, __FILE__, __LINE__);
506  while ($R = pg_fetch_assoc($result)) {
507  if (empty($R['id'])) {
508  continue;
509  }
510  /* RECURSE! */
511  $SubList = FolderListUploadsRecurse($R['id'], $FolderPath . "/" . $R['folder'], $perm);
512  $List = array_merge($List,$SubList);
513  }
514  pg_free_result($result);
515  /* Return findings */
516  return($List);
517 } // FolderListUploadsRecurse()
518 
519 
536 function GetFolderArray($RootFolder, &$FolderArray)
537 {
538  global $PG_CONN;
539 
540  if ($RootFolder == "-1") {
541  $RootFolder = FolderGetTop();
542  }
543  if (empty($RootFolder)) {
544  return $FolderArray;
545  }
546 
547  /* Load this folder's name */
548  $sql = "SELECT folder_name, folder_pk FROM folder WHERE folder_pk=$RootFolder LIMIT 1;";
549  $result = pg_query($PG_CONN, $sql);
550  DBCheckResult($result, $sql, __FILE__, __LINE__);
551  $row = pg_fetch_assoc($result);
552  pg_free_result($result);
553 
554  $Name = trim($row['folder_name']);
555  $FolderArray[$row['folder_pk']] = $row['folder_name'];
556 
557  /* Load any subfolders */
558  $sql = "SELECT folder.folder_pk, folder.folder_name,
559  foldercontents.parent_fk
560  FROM folder, foldercontents
561  WHERE foldercontents.foldercontents_mode = ".FolderDao::MODE_FOLDER."
562  AND foldercontents.parent_fk =$RootFolder
563  AND foldercontents.child_id = folder.folder_pk
564  AND folder.folder_pk is not null
565  ORDER BY folder_name";
566  $result = pg_query($PG_CONN, $sql);
567  DBCheckResult($result, $sql, __FILE__, __LINE__);
568  if (pg_num_rows($result) > 0) {
569  while ($row = pg_fetch_assoc($result)) {
570  GetFolderArray($row['folder_pk'], $FolderArray);
571  }
572  }
573  pg_free_result($result);
574 }
575 
584 function ContainExcludeString($FilePath, $ExcludingText)
585 {
586  $excluding_length = 0;
587  $excluding_flag = 0; // 1: exclude 0: not exclude
588  if ($ExcludingText) {
589  $excluding_length = strlen($ExcludingText);
590  }
591 
592  /* filepath contains 'xxxx/', '/xxxx/', 'xxxx', '/xxxx' */
593  if ($excluding_length > 0 && strstr($FilePath, $ExcludingText)) {
594  $excluding_flag = 1;
595  /* filepath does not contain 'xxxx/' */
596  if ('/' != $ExcludingText[0] && '/' == $ExcludingText[$excluding_length - 1] &&
597  ! strstr($FilePath, '/'.$ExcludingText)) {
598  $excluding_flag = 0;
599  }
600  }
601  return $excluding_flag;
602 }
ContainExcludeString($FilePath, $ExcludingText)
Check if one file path contains an excluding text.
FolderGetFromUpload($Uploadpk, $Folder=-1, $Stop=-1)
DEPRECATED! Given an upload number, return the folder path in an array containing folder_pk and name...
FolderListOption($ParentFolder, $Depth, $IncludeTop=1, $SelectId=-1, $linkParent=false, $OldParent=0)
Create the folder tree, using OPTION tags.
FolderListUploads_perm($ParentFolder, $perm)
Returns an array of uploads in a folder.
Convert2BrowserTime($server_time)
Convert the server time to browser time.
Definition: common-ui.php:298
FolderGetName($FolderPk, $Top=-1)
Given a folder_pk, return the full path to this folder.
GetSingleRec($Table, $Where="")
Retrieve a single database record.
Definition: common-db.php:102
GetFolderArray($RootFolder, &$FolderArray)
Get an array of all the folders from a $RootFolder on down.
Folder2Path($folder_pk)
Return an array of folder_pk, folder_name from the users.root_folder_fk to $folder_pk.
FolderGetTop()
DEPRECATED! Find the top-of-tree folder_pk for the current user.
GetUserRootFolder()
Get the top-of-tree folder_pk for the current user. Fail if there is no user session.
FolderListUploadsRecurse($ParentFolder=-1, $FolderPath= '', $perm=Auth::PERM_READ)
Get uploads and folder info, starting from $ParentFolder.
#define PERM_READ
Read-only permission.
Definition: libfossology.h:44
GetFolderFromItem($upload_pk="", $uploadtree_pk="")
Find what folder an item is in.
foreach($Options as $Option=> $OptVal) if(0==$reference_flag &&0==$nomos_flag) $PG_CONN
DBCheckResult($result, $sql, $filenm, $lineno)
Check the postgres result for unexpected errors. If found, treat them as fatal.
Definition: common-db.php:198
char * trim(char *ptext)
Trimming whitespace.
Definition: fossconfig.c:695
#define PERM_WRITE
Read-Write permission.
Definition: libfossology.h:45