<?php 
/**
#	Project: PHPDISK File Storage Solution
#	This is NOT a freeware, use is subject to license terms.
#
#	Site: http://www.phpdisk.com
#
#	$Id: folders.inc.php 64 2016-07-30 14:01:18Z along $
#
#	Copyright (C) 2008-2016 PHPDisk Team. All Rights Reserved.
#
*/

if(!defined('IN_PHPDISK') || !defined('IN_MYDISK')) {
	exit('[PHPDisk] Access Denied');
}

switch($action){

	case 'folder_modify':

		$folder_id = (int)gpc('folder_id','GP',0);
		if($task == 'folder_modify'){
			form_auth(gpc('formhash','P',''),formhash());

			$folder_name = trim(gpc('folder_name','P',''));
			$folder_description = trim(gpc('folder_description','P',''));
			$ref = trim(gpc('ref','P',''));
			if(checklength($folder_name,1,100)){
				$error = true;
				$sysmsg[] = __('folder_min_max');
			}elseif(is_bad_chars($folder_name)){
				$error = true;
				$sysmsg[] = __('folder_name_bad_chars');
			}
			$num = @$db->result_first("select folder_id from {$tpf}folders where userid='$pd_uid' and folder_name='$folder_name' and folder_id<>'$folder_id'");
			if($num){
				$error = true;
				$sysmsg[] = __('folder_exists');
			}

			if(!$error){
				$ins = array(
				'folder_name' => $folder_name,
				'folder_description' => $folder_description,
				);
				$db->query_unbuffered("update {$tpf}folders set ".$db->sql_array($ins)." where folder_id='{$folder_id}' and userid='$pd_uid';");
				tb_redirect($ref,__('folder_modify_success'));
			}else{
				tb_redirect($_SERVER['HTTP_REFERER'],$sysmsg);
			}
		}else{
			$folder = $db->fetch_one_array("select folder_name,folder_description,parent_id from {$tpf}folders where folder_id='$folder_id' and userid='$pd_uid'");
			$ref = $_SERVER['HTTP_REFERER'];
			require_once template_echo('folders',$user_tpl_dir);
		}
		break;

	case 'folder_create':
		auth_check_folder();
		$group_set = $group_settings[$pd_gid];

		$folder_id = (int)gpc('folder_id','G',0);

		if($task == 'folder_create'){
			create_new_folder();
		}else{

			$ref = $_SERVER['HTTP_REFERER'];
			require_once template_echo('folders',$user_tpl_dir);
		}
		break;

	case 'folder_delete':
		$folder_id = (int)gpc('folder_id','GP',0);
		if($task =='folder_delete'){
			form_auth(gpc('formhash','P',''),formhash());

			$ref = trim(gpc('ref','P',''));

			$rs = $db->fetch_one_array("select count(*) as total from {$tpf}folders where parent_id='$folder_id' and userid='$pd_uid'");
			if($rs['total']){
				$error = true;
				$sysmsg[] = __('has_sub_folder');
			}
			unset($rs);
			if(!$error){
				$db->query_unbuffered("update {$tpf}files set in_recycle=1 where folder_id='$folder_id' and userid='$pd_uid'");
				$db->query_unbuffered("update {$tpf}folders set in_recycle=1 where folder_id='$folder_id' and userid='$pd_uid'");
				tb_redirect($ref,__('folder_delete_success'));

			}else{
				tb_redirect('back',$sysmsg);
			}
		}else{
			$rs = $db->fetch_one_array("select folder_name from {$tpf}folders where folder_id='$folder_id' and userid='$pd_uid'");
			$folder_name = $rs['folder_name'];
			$ref = $_SERVER['HTTP_REFERER'];
			require_once template_echo('folders',$user_tpl_dir);
		}
		break;

}
?>
