* Copyright (c) 2014-2015 Gear Consulting Pty Ltd 
 *
 * This program is free software: you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation, either version 3 of the License, or (at your
 * option) any later version.  Please see LICENSE.txt at the top level of
 * the source code distribution for details.
 */
function format_number_short($number, $sf)
{
  // This formats a number so that we only send back three digits plus an optional decimal point.
  // Example: 723.42 -> 723    72.34 -> 72.3    2.23 -> 2.23
  list($whole, $decimal) = explode (".", $number);
  if (strlen($whole) >= $sf || !is_numeric($decimal))
  {
    $number = $whole;
  } elseif(strlen($whole) < $sf) {
    $diff = $sf - strlen($whole);
    $number = $whole .".".substr($decimal, 0, $diff);
  }
  return $number;
}
function external_exec($command)
{
  global $debug;
  if ($debug) { echo($command."\n"); }
  $output = shell_exec($command);
  if ($debug) { echo($output."\n"); }
  return $output;
}
function shorthost($hostname, $len=12)
{
  // IP addresses should not be shortened
  if (filter_var($hostname, FILTER_VALIDATE_IP))
    return $hostname;
  
  $parts = explode(".", $hostname);
  $shorthost = $parts[0];
  $i = 1;
  while ($i < count($parts) && strlen($shorthost.'.'.$parts[$i]) < $len)
  {
    $shorthost = $shorthost.'.'.$parts[$i];
    $i++;
  }
  return ($shorthost);
}
function isCli()
{
  if (php_sapi_name() == 'cli' && empty($_SERVER['REMOTE_ADDR']))
  {
    return true;
  } else {
    return false;
  }
}
function print_error($text)
{
  global $console_color;
  if (isCli())
  {
    print $console_color->convert("%r".$text."%n\n", false);
  } else {
    echo('

 '.$text.'

 '.$text.'