helpers.php (1914B)
1 <?php 2 // This file is in the public domain. 3 4 /** 5 * If '$arr[$idx]' exists, return it. Otherwise 6 * set '$arr[$idx]' to '$default' and then return it 7 * as well. 8 */ 9 function &pull(&$arr, $idx, $default) { 10 if (!isset($arr[$idx])) { 11 $arr[$idx] = $default; 12 } 13 return $arr[$idx]; 14 } 15 16 /** 17 * Concatenates '$base' and '$path'. Typically used 18 * to add the path (represented by '$path') to a base URL 19 * (represented by '$base'). 20 */ 21 function url_join($base, $path) { 22 // Please note that this simplistic way of joining URLs is 23 // to avoiding using pecl_http (painful installation!) and 24 // to NOT bloat too much tutorial code. 25 return $base . $path; 26 } 27 28 /** 29 * Construct a URL having the host where the script is 30 * running as the base URL, and concatenating '$path' to 31 * it. NOTE: $path must have a leading '/'. 32 */ 33 function url_rel($path){ 34 // Make sure 'REQUEST_SCHEME' is http/https, as in some setups it may 35 // be "HTTP/1.1". 36 return $_SERVER['REQUEST_SCHEME'].'://'.$_SERVER['HTTP_HOST'].$path; 37 } 38 39 /** 40 * Take date in Taler format, as "/Date(timestamp)/", and 41 * convert it in a human-readable string. 42 */ 43 function get_pretty_date($taler_date){ 44 $match = array(); 45 $timestamp = preg_match('/\/Date\(([0-9]+)\)\//', $taler_date, $match); 46 $date = new DateTime(); 47 $date->setTimestamp(intval($match[1])); 48 return $date->format('d/M/Y H:i:s'); 49 } 50 51 /** 52 * Takes amount object in Taler format, and converts it 53 * in a human-readable string. NOTE: Taler amounts objects 54 * come from JSON of the form: 55 * '{"value" x, "fraction": y, "currency": "CUR"}' 56 */ 57 function get_amount($taler_amount){ 58 $PRECISION = 100000000; 59 $fraction = $taler_amount->fraction / $PRECISION; 60 $number = $taler_amount->value + $fraction; 61 return sprintf("%s %s", $number, $taler_amount->currency); 62 } 63 64 ?>