<?php

function clean($val){
   global $con;
   $val = nl2br($val);
   $val = strip_tags($val, '<a><b><i><br><p><h1><h2><h3><b><i><u><ol><ul><li><img>');
   $val = mysqli_real_escape_string($con, $val);
   return $val;
}

function br2nl($val){
   return preg_replace('/(<br \/>)+/i', "", $val);
}

function query($sql){
   global $con;
   return mysqli_query($con, $sql);
}

function fetch_record($sql){
   global $con;
   $q = mysqli_query($con, $sql);
   $f = mysqli_fetch_array($q);
   return $f[0];
}

function fetch_records($sql){
   global $con;
   $q = mysqli_query($con, $sql);
   $f = mysqli_fetch_array($q);
   return $f;
}

function fetch_rows($sql){
   global $con;
   $q = mysqli_query($con, $sql);
   return mysqli_num_rows($q);
}

function fetch_id($table, $id){
   return fetch_records("SELECT * FROM $table WHERE id = '{$id}' LIMIT 1");
}

function fetch_elements($sql, $inc, $x = 'x', $n = 1){
   global $con;
   $q1 = mysqli_query($con, $sql);
   if(mysqli_num_rows($q1) > 0){
      while($f1 = mysqli_fetch_array($q1)){
         include(DIR.$inc);
      }
   }else{
      echo $x != 'x' ? "<div class='GLOB-Empty'>There are no records</div>" : null;
   }
}

function timestamp2day($timestamp){
   return date("d M o", $timestamp);
}

function time_elapsed($timestamp){

   global $t;

   $elapsed    = $t - $timestamp;
   $minutes    = round($elapsed / 60 );
   $hours      = round($elapsed / 3600);
   $days       = round($elapsed / 86400 );
   $weeks      = round($elapsed / 604800);
   $months     = round($elapsed / 2600640 );
   $years      = round($elapsed / 31207680 );

   switch(true){

      case $elapsed <= 60:
      $result = 'just now';
      break;

      case $elapsed > 60 && $elapsed <= 3600:
      $result = $minutes . ' minutes ago';
      break;

      case $elapsed > 3600 && $elapsed <= 86400:
      $result = $hours . ' hours ago';
      break;

      case $elapsed > 86400 && $elapsed <= 604800:
      $result = $days . ' days ago';
      break;

      case $elapsed > 604800 && $elapsed <= 2600640:
      $result = $weeks . ' weeks ago';
      break;

      case $elapsed > 2600640 && $elapsed <= 31207680:
      $result = $months . ' months ago';
      break;

      case $elapsed > 31207680:
      $result = $years . ' years ago';
      break;

      default:
      $result = $minutes . ' minutes ago';
      break;

   }

   return $timestamp != 0 ? $result : 'No Records';

}

function comma_number($number){
   return number_format($number);
}

function decimal_number($number){
   return number_format($number, 2);
}

function short_number($number){

   switch(true){

      case $number >= 1 && $number <= 999:
      $result = $number;
      break;

      case $number >= 1000 && $number <= 999999:
      $result = number_format($number / 1000, 1).'K';
      break;

      case $number >= 1000000 && $number <= 999999999:
      $result = number_format($number / 1000000, 2).'M';
      break;

      case $number >= 1000000000 && $number <= 999999999999 :
      $result = number_format($number / 1000000000, 2).'B';
      break;

      default:
      $result = $number;
      break;

   }

   return $result;

}

function logged_in($x){
   global $$x;
   if(!isset($$x)){

      $_SESSION['messages'][] = 'You cannot access this page';
      header('Location: '.URL);
      exit;

   }
}

function current_page($page){
   echo $page == basename($_SERVER['PHP_SELF']) ? 'x' : '';
}

function current_section($x){
   global $section;
   echo $x == $section ? 'x' : '';
}

function to_url($string){
   $string = trim($string);
   $string = strtolower($string);
   $string = preg_replace("/[^a-z0-9_\s-]/", "", $string);
   $string = preg_replace("/[\s-]+/", " ", $string);
   $string = preg_replace("/[\s_]/", "-", $string);
   return $string;
}

function ex_messages($errors){

   $errors = implode('. ', $errors);
   $message = array('message' => $errors);
   
   echo json_encode($message);

}

function ex_redirect($url){

   $redirect = array('redirect' => $url);
   
   echo json_encode($redirect);

}

function check_file($files, $max_size, $image_width, $image_height){

   $errors = array();

   $pname = $files['name'];
   $psize = $files['size'];
   $ptype = $files['type'];
   $ptmpn = $files['tmp_name'];

   if(!empty($pname)){

      $psize = $psize / 1000;

      if($psize > $max_size){
         $errors[] = "You cannot upload a file more than {$max_size}KB";
      }

      switch($ptype){

         case 'image/jpeg':
         $image = imagecreatefromjpeg($ptmpn);
         break;

         case 'image/png':
         $image = imagecreatefrompng($ptmpn);
         break;

         default:
         $errors[] = "You must select a png or jpg file format";
         break;


      }
   
      $width = imagesx($image);
      $height = imagesy($image);
   
      if($width != $image_width){
         $errors[] = "Image width must be {$image_width}px. You uploaded: {$width}px";
      }
   
      if($height != $image_height){
         $errors[] = "Image width must be {$image_height}px. You uploaded: {$height}px";
      }

      $errors = implode('. ', $errors);

      return $errors;
   
   }

}

function upload_file($files, $location, $id, $table){

   switch($files['type']){

         case 'image/jpeg':
         $filename = $id.'.jpg';
         break;

         case 'image/png':
         $filename = $id.'.png';
         break;

         default:
         $filename = $id.'.jpg';
         break;


      }

   if(!empty($files['name'])){
         
      if(move_uploaded_file($files['tmp_name'], '../images/' . $location . '/' . $filename)){
         query("UPDATE $table SET image = '{$filename}' WHERE id = '{$id}' LIMIT 1");
      }
      
   }

}

function messages(){
   if(isset($_SESSION['messages'])){
      echo "<div class='Messages' id='Messages'>";
         echo implode(".<br />", $_SESSION['messages']);
         echo "<br /><span class='T' id='closeMessages'>[Close Window]</span>";
      echo "</div>";
      unset($_SESSION['messages']);
   }
}

function email_user($email_to, $email_subject, $message){

   global $set;

   $headers  = 'MIME-Version: 1.0' . "\r\n";
   $headers .= 'Content-Type: text/html; charset=UTF-8' . "\r\n";
   $headers .= 'From: '.$set['website_name'].' <'.$set['email'].'>' . "\r\n";

   return mail($email_to, $email_subject, $message, $headers);

}

?>
