<?php
/*
Plugin Name: General Functions
Description: Functions used in the standard bootstrap CMV framework.
Author: Greg Thomas
Version: 1.0
Required System Plugin: True
*/


function displayErrors($errorsAndAlerts){

  if(!$errorsAndAlerts){ return; }
 
  ?>
    <div class="alert alert-warning" role="alert">
      <strong>Warning!</strong><br>
      <?php echo $errorsAndAlerts; ?>
    </div>
  <?php
}

function displaySuccess($success){

  if(!$success){ return; }
 
  ?>
    <div class="alert alert-success" role="alert">
      <strong>Success!</strong> <?php echo $success; ?>
    </div>
  <?php
}

function dbg($string = ""){
  showme($string);
  $traceDetails = debug_backtrace();
  $traceDetails = @$traceDetails[0];

  if($traceDetails){
    echo @"<span style=\"color:red;font-family: Helvetica;font-size: 12px;\" >You're on line {$traceDetails['line']} of file: {$traceDetails['file']}</span><br>\r\n";
  }
}


function formatDate($dateOrTime, $noDateString = '') {
  if (!$dateOrTime || $dateOrTime === '0000-00-00 00:00:00') { return L($noDateString); }
  $time  = is_numeric($dateOrTime) ? $dateOrTime : strtotime($dateOrTime);
  list($month, $day, $year, $hour, $minute) = explode(' ', date('M j Y G i', $time));
  $dateString = "$month $day, $year ";
  return $dateString;
}

function isDateSqlFormat($date){
  return (preg_match("/^[0-9]{4}-(0[1-9]|1[0-2])-(0[1-9]|[1-2][0-9]|3[0-1])$/",$date));
}

function isActive($page){


  if(basename($_SERVER['REQUEST_URI'], '?' . $_SERVER['QUERY_STRING']) == $page){
    return 'active';
  }else{
    return '';
  }

}

function general_daysUntilDate($date){
  $now = time(); // or your date as well
  $startDate = strtotime($date);
  $datediff = $startDate - $now;
  $numberOfDays = floor($datediff/(60*60*24));
  return ($numberOfDays < 0)? '0' : $numberOfDays;
}

function general_daysFromDate($date){
  $now = time(); // or your date as well
  $startDate = strtotime($date);
  $datediff = $now - $startDate;
  $numberOfDays = floor($datediff/(60*60*24));
  return ($numberOfDays < 0)? '0' : $numberOfDays;
}


function general_prettyDate($date, $noDate = "N/A"){
  if(!$date || $date == '0000-00-00'){ return $noDate; }
  return date("jS \of\ F, Y" , strtotime($date));
}

function general_getIP() {
    $ipaddress = '';
    if (getenv('HTTP_CLIENT_IP'))
        $ipaddress = getenv('HTTP_CLIENT_IP');
    else if(getenv('HTTP_X_FORWARDED_FOR'))
        $ipaddress = getenv('HTTP_X_FORWARDED_FOR');
    else if(getenv('HTTP_X_FORWARDED'))
        $ipaddress = getenv('HTTP_X_FORWARDED');
    else if(getenv('HTTP_FORWARDED_FOR'))
        $ipaddress = getenv('HTTP_FORWARDED_FOR');
    else if(getenv('HTTP_FORWARDED'))
       $ipaddress = getenv('HTTP_FORWARDED');
    else if(getenv('REMOTE_ADDR'))
        $ipaddress = getenv('REMOTE_ADDR');
    else
        $ipaddress = 'UNKNOWN';
    return $ipaddress;
}

/* 
 * From this post: http://php.net/manual/en/function.time.php#89415
 * Used to return how long ago something happened eg: 10 seconds, 3 days, 4 hours, etc.
 * $date = "2009-03-04 17:45";
 * $result = nicetime($date); // 2 days ago
*/
function nicetime($date){
    if(empty($date)) {
        return "No date provided";
    }
    
    $periods         = array("second", "minute", "hour", "day", "week", "month", "year", "decade");
    $lengths         = array("60","60","24","7","4.35","12","10");
    
    $now             = time();
    $unix_date       = strtotime($date);
    
       // check validity of date
    if(empty($unix_date)) {    
        return "Bad date";
    }

    // is it future date or past date
    if($now > $unix_date) {    
        $difference     = $now - $unix_date;
        $tense         = "ago";
        
    } else {
        $difference     = $unix_date - $now;
        $tense         = "from now";
    }
    
    for($j = 0; $difference >= $lengths[$j] && $j < count($lengths)-1; $j++) {
        $difference /= $lengths[$j];
    }
    
    $difference = round($difference);
    
    if($difference != 1) {
        $periods[$j].= "s";
    }
    
    return "$difference $periods[$j] {$tense}";
}

function general_getLocation($ipaddress) {
  $location = unserialize(file_get_contents('http://www.geoplugin.net/php.gp?ip=' . $ipaddress));
  return $location;
}