<?
////////////////////////////////////////////
// Admin Module class //
////////////////////////////////////////////
// Written by dkode@full-fx.com //
// this is to be used in conjunction with //
// mysql_db class for database calls //
////////////////////////////////////////////
class admin {
var $img_upload;
// Constructor
//function admin($host, $db, $user, $pass, $img_fname='',$img_path='',$img_url='') {
function admin() {
global $img_upload;
if ($img_upload == 1) {
global $img_fname;
global $img_path;
global $img_url;
// Set all of the image vars locally in this class
$this->img_fname = $img_fname;
$this->img_path = $img_path;
$this->img_url = $img_url;
$this->img_upload = 1;
}
else {
echo "Img upload not enabled<br>\n";
$this->img_upload = 0;
}
db_connect();
}
///////////////////////////////////////////////
// ADD Function //
///////////////////////////////////////////////
// $table : table to edit data in. //
// $form_fields : field values from the form //
///////////////////////////////////////////////
function add($table, $form_fields) {
// Enter IF img_up is true
if ($this->img_upload == 1) {
// First Upload file, If applicable
if ($form_fields[$this->img_fname] != 'none') {
// Uploads image and returns unique filename
$form_fields[$this->img_fname] = $this->img_upload();
if ($form_fields[$this->img_fname] == '') {
echo $form_fields[$this->img_fname];
echo "Invalid File format!!<br>\n";
exit;
}
}
else {
// otherwise, set img to nothing
$form_fields[$this->img_fname] = '';
}
}
// Build INSERT Query
$count = 1;
$fcount = sizeof($form_fields);
foreach ($form_fields as $key => $value) {
if ($count == $fcount) {
// END Field Names
$field_names .= "$key";
// END Data
$fields .= "'$value'";
}
else {
// Field Names
$field_names .= "$key, ";
// Data
$fields .= "'$value', ";
}
$count++;
}
$query = "INSERT INTO $table ($field_names) VALUES($fields)";
//$this->query($query);
db_query($query);
return true;
}
///////////////////////////////////////////////
// Edit Function //
///////////////////////////////////////////////
// $table : table to edit data in. //
// $form_fields : field values from the form //
// $id : id of item to edit //
///////////////////////////////////////////////
function edit($table,$form_fields,$id) {
// Enter IF img_upload is enabled
if ($this->img_upload == 1) {
// If they uploaded an image, we need to remove the old one if applicable
if ($form_fields[$this->img_fname] != 'none') {
// Get old info from db
$query = "SELECT $this->img_fname FROM $table WHERE id = '$id'";
$result = db_query($query);
$result = db_fetch_array($result);
if ($result[$this->img_fname] != '') {
// Delete large image
$full_path = $this->img_path . $result[$this->img_fname];
unlink($full_path);
// Delete thumbnail
$tmb_path = $this->img_path . "sm" . $result[$this->img_fname];
unlink($tmb_path);
}
// Do image functions to new image
$form_fields[$this->img_fname] = $this->img_upload();
}
if ($form_fields[$this->img_fname] == 'none') {
unset ($form_fields[$this->img_fname]);
}
}
// Make UPDATE query
$count = 1;
$fcount = sizeof($form_fields);
foreach ($form_fields as $name => $value) {
if ($count == $fcount) {
// End Field Names
$fields .= "$name='$value'";
}
else {
$fields .= "$name='$value', ";
}
$count++;
}
$query = "UPDATE $table SET $fields WHERE id = '$id'";
db_query($query);
return true;
}
///////////////////////////////////////////////
// Delete Function //
///////////////////////////////////////////////
// $table : table name to delete entry from //
// $id : id of entry to delete //
///////////////////////////////////////////////
function delete($table,$id) {
// Enter IF img_up is true
if ($this->img_upload == 1) {
// Get old image name from DB
$query = "SELECT $this->img_fname FROM $table WHERE id = '$id'";
$result = db_query($query);
$result = db_fetch_array($result);
// Delete large image and thumbnail
if ($result[$this->img_fname] != '') {
// Delete large image
$full_path = $this->img_path . $result[$this->img_fname];
unlink($full_path);
// Delete thumbnail
$tmb_path = $this->img_path . "sm" . $result[$this->img_fname];
unlink($tmb_path);
}
}
// Delete item from database
$query = "DELETE FROM $table WHERE id='$id'";
db_query($query);
return true;
}
///////////////////////////////////////////////
// IMAGE UPLOAD Function //
///////////////////////////////////////////////
// No Arguments //
///////////////////////////////////////////////
function img_upload() {
global $HTTP_POST_FILES;
$filename = $HTTP_POST_FILES['form_fields']['name'][$this->img_fname];
$filename_tmp = $HTTP_POST_FILES['form_fields']['tmp_name'][$this->img_fname];
$size = GetImageSize($filename_tmp);
$img_type = $size[2];
if ($img_type != 2) {
return false;
}
if ($img_unique == 1) {
// Name Images unqiuely with UNIX time
$filename_new = time() . ".jpg";
}
else {
// Keep original filename
$filename_new = $filename;
}
if (is_uploaded_file($filename_tmp)) {
$save_loc = $this->img_path . $filename_new;
// Move the Uploaded file to the correct Directory
move_uploaded_file($filename_tmp, $save_loc);
chmod($save_loc, 0755);
// Make and Save Thumbnail image
$thumb_save_loc = $this->img_path . "sm" . $filename_new;
$size = GetImageSize($save_loc);
$width = $size[0];
$height = $size[1];
$new_w = $width / 1.40;
$new_h = $height / 1.40;
$new_w = ceil($new_w);
$new_h = ceil($new_h);
$src_img = imagecreatefromjpeg($save_loc);
$dst_img = imagecreate($new_w,$new_h);
imagecopyresized($dst_img,$src_img,0,0,0,0,$new_w,$new_h,imagesx($src_img),imagesy($src_img));
imagejpeg($dst_img, "$thumb_save_loc");
return $filename_new;
}
}
}
?>
|