FOSSology  3.2.0rc1
Open Source License Compliance by Open Source Software
common-ui.php
Go to the documentation of this file.
1 <?php
2 /***********************************************************
3  Copyright (C) 2009-2014 Hewlett-Packard Development Company, L.P.
4  Copyright (C) 2017, Siemens AG
5  Copyright (C) 2020 Robert Bosch GmbH, Dineshkumar Devarajan <Devarajan.Dineshkumar@in.bosch.com>
6 
7  This library is free software; you can redistribute it and/or
8  modify it under the terms of the GNU Lesser General Public
9  License version 2.1 as published by the Free Software Foundation.
10 
11  This library is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  Lesser General Public License for more details.
15 
16  You should have received a copy of the GNU Lesser General Public License
17  along with this library; if not, write to the Free Software Foundation, Inc.0
18  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 ***********************************************************/
21 
41 function Array2SingleSelect($KeyValArray, $SLName="unnamed", $SelectedVal= "",
42 $FirstEmpty=false, $SelElt=true, $Options="", $ReturnKey=true)
43 {
44  $str ="\n<select name='$SLName' $Options>\n";
45  if ($FirstEmpty == true) {
46  $str .= "<option value='' > </option>\n";
47  }
48  foreach ($KeyValArray as $key => $val) {
49  if ($SelElt == true) {
50  $SELECTED = ($val == $SelectedVal) ? "SELECTED" : "";
51  } else {
52  $SELECTED = ($key == $SelectedVal) ? "SELECTED" : "";
53  }
54  if ($ReturnKey == true) {
55  $str .= "<option value='$key' $SELECTED>".htmlentities($val, ENT_QUOTES)."</option>\n";
56  } else {
57  $str .= "<option value='$val' $SELECTED>".htmlentities($val, ENT_QUOTES)."</option>\n";
58  }
59  }
60  $str .= "</select>";
61  return $str;
62 }
63 
64 
75 function Fatal($msg, $filenm, $lineno)
76 {
77  echo "<hr>FATAL error, File: $filenm, Line number: $lineno<br>";
78  echo "$msg<hr>";
80  exit(1);
81 }
82 
86 function debugbacktrace()
87 {
88  echo "<pre>";
89  debug_print_backtrace();
90  echo "</pre>";
91 }
92 
98 function debugprint($val, $title)
99 {
100  echo $title, "<pre>";
101  print_r($val);
102  echo "</pre>";
103 }
104 
109 function HumanSize( $bytes )
110 {
111  foreach (array('B','KB','MB','GB','TB') as $unit) {
112  if ($bytes < 1024) {
113  return(round($bytes, 2) .' '. $unit);
114  }
115  $bytes /= 1024;
116  }
117  return(round($bytes, 2) . ' PB');
118 }
119 
127 function GetFileExt($fname)
128 {
129  $extpos = strrpos($fname, '.') + 1;
130  $extension = strtolower(substr($fname, $extpos));
131  return $extension;
132 }
133 
134 
143 function GetArrayVal($Key, $Arr)
144 {
145  if (! is_array($Arr)) {
146  return "";
147  }
148  if (array_key_exists($Key, $Arr)) {
149  return ($Arr[$Key]);
150  } else {
151  return "";
152  }
153 }
154 
160 function HostListOption()
161 {
162  global $SysConf;
163  $options = "";
164  $i = 0;
165  foreach ($SysConf['HOSTS'] as $key => $value) {
166  $options .= "<option value='$key' SELECTED> $key </option>\n";
167  $i ++;
168  }
169  if (1 == $i) {
170  return ""; // if only have one host, does not display
171  }
172  return $options;
173 }
174 
184 function DownloadString2File($text, $name, $contentType)
185 {
186  $connstat = connection_status();
187  if ($connstat != 0) {
188  return _("Lost connection.");
189  }
190 
191  $session = new Session();
192  $session->save();
193 
194  ob_end_clean();
195  header("Expires: ".gmdate("D, d M Y H:i:s", mktime(date("H")+2, date("i"), date("s"), date("m"), date("d"), date("Y")))." GMT");
196  header("Last-Modified: ".gmdate("D, d M Y H:i:s")." GMT");
197  header('Content-Description: File Transfer');
198  header("Content-Type: $contentType");
199  header("Content-Length: ".(string)(strlen($text)));
200  header("Content-Disposition: attachment; filename=\"$name\"");
201  header("Content-Transfer-Encoding: binary\n");
202 
203  echo $text;
204  if ((connection_status() == 0) and ! connection_aborted()) {
205  return true;
206  }
207  return _("Lost connection.");
208 }
209 
210 
219 function GetUploadtreeTableName($upload_pk)
220 {
221  if (! empty($upload_pk)) {
222  $upload_rec = GetSingleRec("upload", "where upload_pk='$upload_pk'");
223  if (! empty($upload_rec['uploadtree_tablename'])) {
224  return $upload_rec['uploadtree_tablename'];
225  }
226  }
227  return "uploadtree";
228 }
229 
237 function GetUploadName($upload_pk)
238 {
239  if (empty($upload_pk)) {
240  return "";
241  }
242  $upload_rec = GetSingleRec("upload", "where upload_pk='$upload_pk'");
243  $upload_filename = $upload_rec['upload_filename'];
244  if (empty($upload_filename)) {
245  return "";
246  } else {
247  return $upload_filename;
248  }
249 }
250 
258 function GetUploadID($uploadtreeid)
259 {
260  if (empty($uploadtreeid)) {
261  return "";
262  }
263  $upload_rec = GetSingleRec("uploadtree", "where uploadtree_pk=$uploadtreeid");
264  $uploadid = $upload_rec['upload_fk'];
265  if (empty($uploadid)) {
266  return "";
267  } else {
268  return $uploadid;
269  }
270 }
271 
279 function Get1stUploadtreeID($upload)
280 {
281  global $PG_CONN;
282  if (empty($upload)) {
283  return "";
284  }
285  $sql = "SELECT max(uploadtree_pk) from uploadtree where upload_fk = $upload and parent is null;";
286  $result = pg_query($PG_CONN, $sql);
287  DBCheckResult($result, $sql, __FILE__, __LINE__);
288  $row = pg_fetch_assoc($result);
289  $uploadtree_id = $row['max'];
290  pg_free_result($result);
291  return $uploadtree_id;
292 }
293 
298 function Convert2BrowserTime($server_time)
299 {
300  $server_timezone = date_default_timezone_get();
301  $browser_time = new \DateTime($server_time, new \DateTimeZone($server_timezone));
302  if (array_key_exists("timezone", $_SESSION)) {
303  $tz = $_SESSION["timezone"];
304  $browser_time->setTimeZone(new \DateTimeZone($tz));
305  }
306  $time = $browser_time->format('Y-m-d H:i:s');
307  return $time;
308 }
DownloadString2File($text, $name, $contentType)
Send a string to a user as a download file.
Definition: common-ui.php:184
Convert2BrowserTime($server_time)
Convert the server time to browser time.
Definition: common-ui.php:298
GetSingleRec($Table, $Where="")
Retrieve a single database record.
Definition: common-db.php:102
GetUploadID($uploadtreeid)
Get upload id through uploadtreeid.
Definition: common-ui.php:258
debugprint($val, $title)
Print debug message.
Definition: common-ui.php:98
HostListOption()
Get host list.
Definition: common-ui.php:160
GetFileExt($fname)
Get File Extension (text after last period)
Definition: common-ui.php:127
foreach($Options as $Option=> $OptVal) if(0==$reference_flag &&0==$nomos_flag) $PG_CONN
HumanSize($bytes)
Translate a byte number to a proper type, xxx bytes to xxx B/KB/MB/GB/TB/PB.
Definition: common-ui.php:109
DBCheckResult($result, $sql, $filenm, $lineno)
Check the postgres result for unexpected errors. If found, treat them as fatal.
Definition: common-db.php:198
GetArrayVal($Key, $Arr)
Get the value from a array(map)
Definition: common-ui.php:143
Array2SingleSelect($KeyValArray, $SLName="unnamed", $SelectedVal="", $FirstEmpty=false, $SelElt=true, $Options="", $ReturnKey=true)
Build a single choice select pulldown.
Definition: common-ui.php:41
debugbacktrace()
Debug back trace.
Definition: common-ui.php:86
GetUploadName($upload_pk)
Get Upload Name through upload id.
Definition: common-ui.php:237
Get1stUploadtreeID($upload)
Get 1st uploadtree id through upload id.
Definition: common-ui.php:279
Fatal($msg, $filenm, $lineno)
Write message to stdout and die.
Definition: common-ui.php:75
GetUploadtreeTableName($upload_pk)
Get the uploadtree table name for this upload_pk If upload_pk does not exist, return "uploadtree"...
Definition: common-ui.php:219