<?php
  /**
   * Copyright 2011 Zazavi - http://www.zazavi.com
   * Zazavi is free software released under the terms of the GNU General Public License.
   * You should have received a copy of the license.  If not, see <http://www.gnu.org/licenses/>.
   */
  define("_VALID_PHP", true);
  
  require_once("../../init.php");
  if (!$user->is_Admin())
      redirect_to("../../login.php");

$filename = $_SERVER['DOCUMENT_ROOT'] . "/admin/modules/gallery/lang/" . $core->language . ".lang.php";
 
 if (file_exists($filename)) {
 require_once("lang/" . $core->language . ".lang.php");
 } else {
 require_once("lang/en.lang.php");
 }
 
  require_once("admin_class.php");
  $gal = new Gallery();
?>
<?php
  /* Update Configuration*/
  if (isset($_POST['processGallery'])):
  $gal->galid = (isset($_POST['galid'])) ? $_POST['galid'] : 0; 
  $gal->updateConfig();
  endif;
?>
<?php
  /* Delete Gallery */
  if (isset($_POST['deleteGallery']))
      : if (intval($_POST['deleteGallery']) == 0 || empty($_POST['deleteGallery']))
      : redirect_to("loader.php?do=modules&action=config&mod=galler");
  endif;
  
  $id = intval($_POST['deleteGallery']);
  $folder = getValue("folder", "mod_gallery_config", "id='" . $id . "'");
  $dirname = ZAZAVILITE . $gal->galpath . $folder;
  
  delete_directory($dirname);

  $action = $db->delete("mod_gallery_config", "id='" . $id . "'");
  $db->delete("mod_gallery_images", "gallery_id='" . $id . "'");

  $title = sanitize($_POST['galtitle']);
  
  print ($action) ? $zazavisec->writeLog(_GALLERY .' <strong>'.$title.'</strong> '._DELETED, "", "no", "module") . $core->msgOk(_GALLERY .' <strong>'.$title.'</strong> '._DELETED) : $core->msgAlert(_SYSTEM_PROCCESS);   
  endif;
?>
<?php
  /* Update Gallery Order */
  if (isset($_GET['sortgallery']) && $_GET['sortgallery'] == 1) :
      foreach ($_POST['item'] as $k => $v) :
          $p = $k + 1;
          
          $data['sorting'] = $p;
          
          $db->update("mod_gallery_images", $data, "id='" . intval($v) . "'");
      endforeach;
 endif;
?>
<?php
  /* Delete Image */
  if (isset($_POST['deleteImage'])): 
  
  list($id, $folder) = explode(":", $_POST['deleteImage']);
  
  $id = intval($id);
  $folder = sanitize($folder);
  $dirname = ZAZAVILITE . $gal->galpath . $folder;
  
  $img = getValue("thumb", "mod_gallery_images", "id='" . $id . "'");
  
  @unlink($dirname . '/'.$img);
  @unlink($dirname . '/thumbs/'.$img);

  $db->delete("mod_gallery_images", "id='" . $id . "'");
  $title = sanitize($_POST['imgtitle']);
  
  print ($db->affected()) ? $zazavisec->writeLog(_IMAGE .' <strong>'.$title.'</strong> '._DELETED, "", "no", "module") . $core->msgOk(_IMAGE .' <strong>'.$title.'</strong> '._DELETED) : $core->msgAlert(_SYSTEM_PROCCESS); 
  endif;

  /* Update Image */
  if (isset($_POST['imgid']))
      : if ((intval($_POST['imgid']) == 0))
      : redirect_to("loader.php?do=modules&action=config&mod=gallery");
  endif;
  
  $id = intval($_POST['imgid']);
  
  $data['title'] = sanitize($_POST['title']);
  $data['description'] = sanitize($_POST['desc']);
  
  $db->update("mod_gallery_images", $data, "id='" . $id . "'");
    
  print ($db->affected()) ? $zazavisec->writeLog(_IMAGE .' <strong>'.$data['title'].'</strong> '._UPDATED, "", "no", "module") . $core->msgOk(_IMAGE .' <strong>'.$data['title'].'</strong> '._UPDATED) : $core->msgAlert(_SYSTEM_PROCCESS); 
  endif;
?>
<?php
	if (isset($_POST['uploadimage'])) {
		set_time_limit(240);
		
		$id = intval($_POST['id']);
		$row = $db->first("SELECT * FROM mod_gallery_config WHERE id = '" . $id . "'");

		include(ZAZAVILITE . "lib/class_imageUpload.php");
		include(ZAZAVILITE . "lib/class_imageResize.php");
		
		$galdir = ZAZAVILITE . $gal->galpath . $row['folder'] . "/";
		
		$newName = "IMG_" . randName();
		$ext = substr($_FILES['image']['name'], strrpos($_FILES['image']['name'], '.') + 1);
		$name = $newName.".".strtolower($ext);
			
		
		$bdp = new Upload();
		$bdp->File = $_FILES['image'];
		$bdp->method = $row['method'];
		$bdp->SavePath = $galdir;
		$bdp->ThumbPath = $galdir . "thumbs/";
		$bdp->TWidth = $row['thumb_w'];
		$bdp->THeight = $row['thumb_h'];
		$bdp->NewWidth = $row['image_w'];
		$bdp->NewHeight = $row['image_h'];
		$bdp->NewName = $newName;
		$bdp->OverWrite = true;

		$err = $bdp->UploadFile();
		
		$data['gallery_id'] = $id;
		
			$data = array(
				  'gallery_id' => $id,
				  'thumb' => sanitize($name), 
				  'title' => "Img Title",
				  'description' => "Img Description"
			);
		  $db->insert("mod_gallery_images",$data);
		
		if (count($err) > 0 and is_array($err)) {
			foreach ($err as $key => $val) {
				print $val;
			}
		}

		print "OK|".$data['thumb'];
	}
?>