<?
/* trash management class :) */

class Trash {

	var $db;
	var $currentUser = array();
	var $errorCode;

	function trash() {
		global $settings, $sys;
		//$this->qty = $this->countObjects();
		if ($settings->config['trashpath']) {
			$this->trashroot = $settings->config['trashpath'];
		} else {
			$this->trashroot = gluePath($sys['root'], "/system/data/trash/");
		}
	}
	
	
	function add2trash($path) {
		global $sys, $fm, $auth, $comments,$permissions, $db, $settings, $img, $thumb, $share, $public, $log;

		$max_id = $db->GetOne("SELECT max(id) as max_id FROM ".$settings->config['trash_tablename']."");
		$id = $max_id+1;


		$fromFilename = $fm->basename($path);
		$tempfilename = $id;
		$trashpath = gluePath($this->trashroot, $auth->currentUserInfo['username'], $tempfilename);
		$fullpath = gluePath($permissions['path'], $path);


		
		
		if ($fm->move($fullpath, $trashpath)) {
			$comments->move($fullpath, $trashpath);
			$log->act("0", "delete", $fullpath);
			if ($img->isImage($fromFilename)) {
				$thumb->clearCache($fullpath);
			}
			
			/* remove sharing and public info*/
			/* aici ar trebui sa le stearga pe toate recursiv */
			$share->removeByPath($path);
			//$public->unPublish($path);
			
			
			$rs = $db->query("SELECT * FROM ".$settings->config['trash_tablename']." WHERE id = ''");
			$data['uid'] = $auth->currentUserInfo['id'];
			$data['path'] = $path;
			$data['date'] = time();
			$updateSQL = $db->GetInsertSQL($rs, $data);
			$db->query($updateSQL);
			$real_id = $db->Insert_ID();
			/*  */
			if ($real_id != $id) {
				@$fm->rename($trashpath, $real_id);
			}
			return true;
		} else {
			echo $fm->errorMsg;
			//echo $fm->errorMsg;
			//echo "<br>";
			//echo $trashfolder;
			$this->errorCode = 2;
			return false;
		}
	}


	function restore($id) {
		global $sys, $auth, $fm, $comments, $permissions, $db, $settings;

		$objectInfo = $this->getObjectInfo($id);
		if ($objectInfo) {
			$restorepath = gluePath($permissions['path'], $objectInfo['path']);
			$trashpath = gluePath($this->trashroot, $auth->currentUserInfo['username'], $objectInfo['id']);

			if (!is_dir($fm->dirname($restorepath))) {
				$fm->createPath($fm->dirname($restorepath));
			}

			if (@$fm->move($trashpath, $restorepath)) {
				//delete record from db
				$db->query("DELETE FROM ".$settings->config['trash_tablename']." WHERE uid='".$auth->currentUserInfo['id']."' and id='".$id."'");
				$comments->move($trashpath, $restorepath);
				return array("result"=>true, "filename"=>basename($objectInfo['path']), "restorepath"=>$restorepath);
			} else {
				if (!file_exists($trashpath)) {
					$errMsg = "<br><span class=\"nowrap\">File not available on file system.</span>";
				}
				if (file_exists($restorepath)) {
					$errMsg = "<br>\"".$restorepath."\" already exists.";
				}
				
				
				return array("result"=>false, "filename"=>basename($objectInfo['path']), "restorepath"=>$restorepath, "errorMsg"=>$errMsg);
			}
		} else {
			return array("result"=>false, "filename"=>basename($objectInfo['path']), "restorepath"=>$restorepath, "errorMsg"=>"<br>File not available on file system.");
		}
	}

	function getObjectInfo($id) {
		global $db, $auth, $settings;
		return $db->GetRow("SELECT * FROM ".$settings->config['trash_tablename']." WHERE uid='".$auth->currentUserInfo['id']."' and id='".$id."'",false);
	}
	
	function delete($id) {
		global $sys, $fm, $comments, $db, $auth, $settings;
		$objectInfo = $this->getObjectInfo($id);
		if ($objectInfo) {
			$trashpath = gluePath($this->trashroot, $auth->currentUserInfo['username'], $objectInfo['id']);
			$dir = is_dir($trashpath);
			$file = is_file($trashpath);
			$ok = false;
			
			if ($dir || $file) {
				$ok = $fm->delete($trashpath);
			} else {
				$ok = true;
			}
			
			if ($ok) {
				//delete record from db
				$db->query("DELETE FROM ".$settings->config['trash_tablename']." WHERE uid='".$auth->currentUserInfo['id']."' and id='".$id."'");
				return array("result"=>true, "filename"=>$objectInfo['path'], "dir"=>$dir);
			} else {
				return array("result"=>false, "filename"=>$objectInfo['path'], "dir"=>$dir);
			}
		} else {
			return array("result"=>false, "filename"=>$objectInfo['path'], "dir"=>$dir);
		}
	}


	function emptyTrash($uid) {
		global $sys,$fm,$db, $settings;
		$rs = $db->query("SELECT * FROM ".$settings->config['trash_tablename']." WHERE uid='".$uid."'");
		while (!$rs->EOF) {
			$delresult = $this->delete($rs->fields['id']);
			if (!$delresult['result']) {
				$this->errorMsg .= "<span class=\"nowrap\">Failed to delete trash record \"".$rs->fields['id']."\" (".$rs->fields['path'].").</span>";
				return false;
			}
			$rs->MoveNext();
		}
		$this->folderCleanUp();
		return true;
	}

	function folderCleanUp() {
		global $sys, $fm, $auth;
		$trashfolder = gluePath($this->trashroot, $auth->currentUserInfo['username']);
		$files = $fm->getDirList($trashfolder, array());
		foreach ($files as $file){
			$fm->delete($file);
		}
	}
	
	function countObjects() {
		global $db, $auth, $settings;
		return $db->GetOne("SELECT count(id) AS total FROM ".$settings->config['trash_tablename']." where uid='".$auth->currentUserInfo['id']."'", false);
	}

}
?>