FOSSology  4.4.0
Open Source License Compliance by Open Source Software
common-ui.php
Go to the documentation of this file.
1 <?php
2 /*
3  SPDX-FileCopyrightText: © 2009-2014 Hewlett-Packard Development Company, L.P.
4  SPDX-FileCopyrightText: © 2017 Siemens AG
5  SPDX-FileCopyrightText: © 2020 Robert Bosch GmbH
6  SPDX-FileCopyrightText: © Dineshkumar Devarajan <Devarajan.Dineshkumar@in.bosch.com>
7 
8  SPDX-License-Identifier: LGPL-2.1-only
9 */
10 
11 use Symfony\Component\HttpFoundation\Session\Session;
12 
32 function Array2SingleSelect($KeyValArray, $SLName="unnamed", $SelectedVal= "",
33 $FirstEmpty=false, $SelElt=true, $Options="", $ReturnKey=true)
34 {
35  $str ="\n<select name='$SLName' $Options>\n";
36  if ($FirstEmpty == true) {
37  $str .= "<option value='' > </option>\n";
38  }
39  foreach ($KeyValArray as $key => $val) {
40  if ($SelElt == true) {
41  $SELECTED = ($val == $SelectedVal) ? "SELECTED" : "";
42  } else {
43  $SELECTED = ($key == $SelectedVal) ? "SELECTED" : "";
44  }
45  if ($ReturnKey == true) {
46  $str .= "<option value='$key' $SELECTED>".htmlentities($val, ENT_QUOTES)."</option>\n";
47  } else {
48  $str .= "<option value='$val' $SELECTED>".htmlentities($val, ENT_QUOTES)."</option>\n";
49  }
50  }
51  $str .= "</select>";
52  return $str;
53 }
54 
55 
66 function Fatal($msg, $filenm, $lineno)
67 {
68  echo "<hr>FATAL error, File: $filenm, Line number: $lineno<br>";
69  echo "$msg<hr>";
71  exit(1);
72 }
73 
77 function debugbacktrace()
78 {
79  echo "<pre>";
80  debug_print_backtrace();
81  echo "</pre>";
82 }
83 
89 function debugprint($val, $title)
90 {
91  echo $title, "<pre>";
92  print_r($val);
93  echo "</pre>";
94 }
95 
100 function HumanSize( $bytes )
101 {
102  foreach (array('B','KB','MB','GB','TB') as $unit) {
103  if ($bytes < 1024) {
104  return(round($bytes, 2) .' '. $unit);
105  }
106  $bytes /= 1024;
107  }
108  return(round($bytes, 2) . ' PB');
109 }
110 
120 function HumanDuration(DateInterval $duration): string
121 {
122  $humanDuration = "";
123  if ($duration->y > 0) {
124  $humanDuration .= $duration->y . " y";
125  } elseif ($duration->m > 0) {
126  $humanDuration .= $duration->m . " m";
127  } elseif ($duration->days > 0) {
128  $humanDuration .= $duration->days . " d";
129  } else {
130  $humanDuration .= $duration->h . "h " . $duration->i . "m";
131  }
132  return $humanDuration;
133 }
134 
142 function GetFileExt($fname)
143 {
144  $extpos = strrpos($fname, '.') + 1;
145  return strtolower(substr($fname, $extpos));
146 }
147 
148 
157 function GetArrayVal($Key, $Arr)
158 {
159  if (! is_array($Arr)) {
160  return "";
161  }
162  if (array_key_exists($Key, $Arr)) {
163  return ($Arr[$Key]);
164  } else {
165  return "";
166  }
167 }
168 
174 function HostListOption()
175 {
176  global $SysConf;
177  $options = "";
178  $i = 0;
179  foreach ($SysConf['HOSTS'] as $key => $value) {
180  $options .= "<option value='$key' SELECTED> $key </option>\n";
181  $i ++;
182  }
183  if (1 == $i) {
184  return ""; // if only have one host, does not display
185  }
186  return $options;
187 }
188 
198 function DownloadString2File($text, $name, $contentType)
199 {
200  $connstat = connection_status();
201  if ($connstat != 0) {
202  return _("Lost connection.");
203  }
204 
205  $session = new Session();
206  $session->save();
207 
208  ob_end_clean();
209  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");
210  header("Last-Modified: ".gmdate("D, d M Y H:i:s")." GMT");
211  header('Content-Description: File Transfer');
212  header("Content-Type: $contentType");
213  header("Content-Length: ".(string)(strlen($text)));
214  header("Content-Disposition: attachment; filename=\"$name\"");
215  header("Content-Transfer-Encoding: binary\n");
216 
217  echo $text;
218  if ((connection_status() == 0) and ! connection_aborted()) {
219  return true;
220  }
221  return _("Lost connection.");
222 }
223 
224 
233 function GetUploadtreeTableName($upload_pk)
234 {
235  if (! empty($upload_pk)) {
236  $upload_rec = GetSingleRec("upload", "where upload_pk='$upload_pk'");
237  if (! empty($upload_rec['uploadtree_tablename'])) {
238  return $upload_rec['uploadtree_tablename'];
239  }
240  }
241  return "uploadtree";
242 }
243 
251 function GetUploadName($upload_pk)
252 {
253  if (empty($upload_pk)) {
254  return "";
255  }
256  $upload_rec = GetSingleRec("upload", "where upload_pk='$upload_pk'");
257  $upload_filename = $upload_rec['upload_filename'];
258  if (empty($upload_filename)) {
259  return "";
260  } else {
261  return $upload_filename;
262  }
263 }
264 
272 function GetUploadID($uploadtreeid)
273 {
274  if (empty($uploadtreeid)) {
275  return "";
276  }
277  $upload_rec = GetSingleRec("uploadtree", "where uploadtree_pk=$uploadtreeid");
278  $uploadid = $upload_rec['upload_fk'];
279  if (empty($uploadid)) {
280  return "";
281  } else {
282  return $uploadid;
283  }
284 }
285 
293 function Get1stUploadtreeID($upload)
294 {
295  global $PG_CONN;
296  if (empty($upload)) {
297  return "";
298  }
299  $sql = "SELECT max(uploadtree_pk) from uploadtree where upload_fk = $upload and parent is null;";
300  $result = pg_query($PG_CONN, $sql);
301  DBCheckResult($result, $sql, __FILE__, __LINE__);
302  $row = pg_fetch_assoc($result);
303  $uploadtree_id = $row['max'];
304  pg_free_result($result);
305  return $uploadtree_id;
306 }
307 
312 function Convert2BrowserTime($server_time)
313 {
314  $server_timezone = date_default_timezone_get();
315  $browser_time = new \DateTime($server_time, new \DateTimeZone($server_timezone));
316  if (array_key_exists("timezone", $_SESSION)) {
317  $tz = $_SESSION["timezone"];
318  $browser_time->setTimeZone(new \DateTimeZone($tz));
319  }
320  return $browser_time->format('Y-m-d H:i:s');
321 }
DBCheckResult($result, $sql, $filenm, $lineno)
Check the postgres result for unexpected errors. If found, treat them as fatal.
Definition: common-db.php:187
GetSingleRec($Table, $Where="")
Retrieve a single database record.
Definition: common-db.php:91
HumanSize( $bytes)
Translate a byte number to a proper type, xxx bytes to xxx B/KB/MB/GB/TB/PB.
Definition: common-ui.php:100
DownloadString2File($text, $name, $contentType)
Send a string to a user as a download file.
Definition: common-ui.php:198
debugprint($val, $title)
Print debug message.
Definition: common-ui.php:89
Array2SingleSelect($KeyValArray, $SLName="unnamed", $SelectedVal="", $FirstEmpty=false, $SelElt=true, $Options="", $ReturnKey=true)
Build a single choice select pulldown.
Definition: common-ui.php:32
GetFileExt($fname)
Get File Extension (text after last period)
Definition: common-ui.php:142
GetArrayVal($Key, $Arr)
Get the value from a array(map)
Definition: common-ui.php:157
GetUploadID($uploadtreeid)
Get upload id through uploadtreeid.
Definition: common-ui.php:272
GetUploadtreeTableName($upload_pk)
Get the uploadtree table name for this upload_pk If upload_pk does not exist, return "uploadtree".
Definition: common-ui.php:233
Fatal($msg, $filenm, $lineno)
Write message to stdout and die.
Definition: common-ui.php:66
HumanDuration(DateInterval $duration)
Convert DateInterval to Human readable format.
Definition: common-ui.php:120
HostListOption()
Get host list.
Definition: common-ui.php:174
GetUploadName($upload_pk)
Get Upload Name through upload id.
Definition: common-ui.php:251
Convert2BrowserTime($server_time)
Convert the server time to browser time.
Definition: common-ui.php:312
Get1stUploadtreeID($upload)
Get 1st uploadtree id through upload id.
Definition: common-ui.php:293
debugbacktrace()
Debug back trace.
Definition: common-ui.php:77
foreach($Options as $Option=> $OptVal) if(0==$reference_flag &&0==$nomos_flag) $PG_CONN