<?php

/*++++++++++++++++++++++++++++++++++++++++

  Script: Maian Gallery v2.0
  Written by: David Ian Bennett
  E-Mail: support@maianscriptworld.co.uk
  Website: http://www.maianscriptworld.co.uk

  ++++++++++++++++++++++++++++++++++++++++
  
  This File: class_images.inc.php
  Description: Images Class

  ++++++++++++++++++++++++++++++++++++++++*/


class images extends genericOptions {

var $prefix;

// Approve/reject comment..
function process_comment($id,$reject=false)
{
  if ($reject) {
    mysql_query("DELETE FROM ".$this->prefix."comments
                 WHERE id   = '{$id}'
                 LIMIT 1
                 ") or die(mysql_error());
  } else {
    mysql_query("UPDATE ".$this->prefix."comments SET
                 is_active    = '1',
                 is_approved  = '0'
                 WHERE id     = '{$id}'
                 LIMIT 1
                 ") or die(mysql_error());
  }
}

// Update comments..
function update_comment($DATA)
{
  // Use callback function to prepare data for importing..
  $DATA = array_map(array($this,'safe_import'),$DATA);
  
  mysql_query("UPDATE ".$this->prefix."comments SET
               name      = '{$DATA['name']}',
               email     = '{$DATA['email']}',
               comments  = '{$DATA['comments']}'
               WHERE id  = '".(isset($DATA['comid']) ? $DATA['comid'] : $DATA['cid'])."'
               LIMIT 1
               ") or die(mysql_error());
}

// Delete comment..
function delete_comment($id)
{
  mysql_query("DELETE FROM ".$this->prefix."comments
               WHERE id  = '{$id}'
               LIMIT 1
               ") or die(mysql_error());
}

// Delete images..
function delete_images($DATA,$folder)
{
  for ($i=0; $i<count($DATA['img']); $i++)
  {
    $q_img = mysql_query("SELECT * FROM ".$this->prefix."images 
                          WHERE id = '{$DATA['img'][$i]}'
                          LIMIT 1
                          ") or die(mysql_error());
    $IMG = mysql_fetch_object($q_img);                      
                          
    // Remove image...
    if (file_exists($folder.'/'.$IMG->imagepath) && is_writeable($folder)) {
      @unlink($folder.'/'.$IMG->imagepath);
    } 
    // Remove thumbnail...
    if (file_exists($folder.'/'.$IMG->thumbpath) && is_writeable($folder)) {
      @unlink($folder.'/'.$IMG->thumbpath);
    }                  
    // Remove file
    mysql_query("DELETE FROM ".$this->prefix."images 
                 WHERE id = '{$IMG->id}'
                 LIMIT 1
                 ") or die(mysql_error());
  }
}

// Update image..
function update_image_sql($DATA)
{
  // Use callback function to prepare data for importing..
  $DATA = array_map(array($this,'safe_import'),$DATA);
  
  mysql_query("UPDATE ".$this->prefix."images SET
               name         = '{$DATA['name']}',
               cat          = '{$DATA['cat']}',
               comments     = '{$DATA['comments']}',
               keywords     = '{$DATA['keywords']}',
               description  = '{$DATA['description']}',
               visits       = '{$DATA['visits']}',
               details      = '".(isset($DATA['details']) ? $DATA['details'] : '')."',
               sale_item    = '".(isset($DATA['sale_item']) ? $DATA['sale_item'] : '')."',
               enabled      = '".(isset($DATA['enabled']) ? $DATA['enabled'] : '1')."'
               WHERE id     = '{$DATA['edit']}'
               LIMIT 1
               ") or die(mysql_error());
}

// Update image sale options..
function update_sale_options($DATA)
{
  // Use callback function to prepare data for importing..
  $DATA = array_map(array($this,'safe_import'),$DATA);
  
  mysql_query("UPDATE ".$this->prefix."images SET
               details    = '{$DATA['details']}',
               sale_item  = '{$DATA['sale']}'
               WHERE cat  = '{$DATA['cat']}'
               ") or die(mysql_error());
               
  return mysql_affected_rows();             
}

// Delete temporary file..
function delete_temp_file($temp)
{
  if (file_exists($temp)) {
    unlink($temp);
  }
}

// Add info to database..
function insert_image_sql($DATA,$increment,$tpath,$tsize,$ipath)
{
  $query = mysql_query("INSERT INTO ".$this->prefix."images (
                        name,
                        addDate,
                        cat,
                        comments,
                        keywords,
                        description,
                        isize,
                        tsize,
                        imagepath,
                        thumbpath,
                        visits,
                        details,
                        sale_item,
                        enabled
                        ) VALUES (
                        '".$this->safe_import($DATA['name'][$increment])."',
                        '".date("Y-m-d")."',
                        '{$DATA['cat'][$increment]}',
                        '".$this->safe_import($DATA['comments'][$increment])."',
                        '".$this->safe_import($DATA['keywords'][$increment])."',
                        '".$this->safe_import($DATA['description'][$increment])."',
                        '{$DATA['isize'][$increment]}',
                        '{$tsize}',
                        '{$ipath}',
                        '{$tpath}',
                        '0',
                        '".(isset($DATA['item_details'][$increment]) ? $this->safe_import($DATA['item_details'][$increment]) : '')."',
                        '".(isset($DATA['sale_item_'.$increment]) ? $DATA['sale_item_'.$increment] : '')."',
                        '1'
                        )");
                        
  return ($query ? true : false);                      
}

// Create thumbnail image..
function create_thumbnail($folder,$path,$width,$height,$t_folder,$t_path)
{
  if (!function_exists('imagecreatefromjpeg')) {
    die('You do not have the GD library instaled or enabled!! Contact your host!');
  }
  
  // Get file extension..
  $ext = strtolower(strrchr($path, "."));

  // Check file and make copy..
  if (file_exists($folder.$path)) {
    $img = '';

    // Is file a jpeg..
    if ($ext=='.jpg' || $ext=='.jpeg' && function_exists('imagecreatefromjpeg')) {
      $img = imagecreatefromjpeg($folder.$path);
    }
    // Is this file a .png..
    if ($ext==".png" && function_exists('imagecreatefrompng')) {
      $img = imagecreatefrompng($folder.$path);
    }
    // Is this file a .gif..
    if ($ext=='.gif' && function_exists('imagecreatefromgif')) {
      $img = imagecreatefromgif($folder.$path);
    }
  }
  
  // Create thumbnail..
  if ($img) {
    $img_width   = imagesx($img);
    $img_height  = imagesy($img);
    $scale       = min($width/$img_width, $height/$img_height);

    // Maintain aspect ration..
    // If image is smaller than resize, no resize..
    if ($scale<1) {
      $new_width   = floor($scale * $img_width);
      $new_height  = floor($scale * $img_height);
      $tmp_img     = imagecreatetruecolor($new_width,$new_height);

      imagecopyresampled($tmp_img,$img,0,0,0,0,$new_width,$new_height,$img_width,$img_height);
    }
    // Create jpeg thumbnail..
    if ($ext=='.jpg' || $ext=='.jpeg') {
      imagejpeg($tmp_img, $t_folder.$t_path);
    }
    // Create png thumbnail..
    if ($ext=='.png') {
      imagepng($tmp_img, $t_folder.$t_path);
    }
    // Create gif thumbnail..
    if ($ext=='.gif') {
      imagepng($tmp_img, $t_folder.$t_path);
    }
    
    // Destroy temp files..
    imagedestroy($img);
    imagedestroy($tmp_img);
  }

  // Does thumbnail exist..
  if (file_exists($t_folder.$t_path)) {
    return true;
  } else {
    return false;
  }
}

// Move image to new directory..
function move_to_new_directory($folder,$old,$folder2,$new)
{
  // Move file to gallery directory..
  if (file_exists($folder.$old)) {
    rename($folder.$old, $folder2.$new);
  }

  // Check file has been moved..
  if (file_exists($folder2.$new)) {
    if (file_exists($folder.$old)) {
      unlink($folder.$old);
    }  
    return true;
  } else {
    return false;
  }
}

// Upload thumbnail
function upload_thumbnail($file,$temp,$folder)
{
  // Check if temp file was uploaded, folder is present and folder is writeable..
  if (is_uploaded_file($temp) && is_dir($folder) && is_writeable($folder)) {
    // Upload file..
    move_uploaded_file($temp, $folder.$file);
    // Not all servers support chmod, so suppress error with @..
    @chmod($folder.$file,0644);

    // Does uploaded file exist..
    if (file_exists($folder.$file)) {
      // Delete temporary file if it exists..
      $this->delete_temp_file($temp);
      return true;
    } else {
      return false;
    }
  } else {
    return false;
  }
}

// Rename file upload..
function filter_file_path($file,$cat,$name)
{
  // Get extension of original file..
  $ext  = strtolower(strrchr($file, "."));
  
  // Return new file name..
  return $name.$this->getNextID().'-'.$cat.$ext;
}

// Get next id for image...
function getNextID()
{
  $q_img = mysql_query("SELECT id FROM ".$this->prefix."images ORDER BY id DESC LIMIT 1") or die(mysql_error());
  $IMG = mysql_fetch_object($q_img);
  
  return (mysql_num_rows($q_img)==0 ? '1' : ($IMG->id+1));
}

}

?>
