*** lib/wiki/wikilib.php	Wed Nov 23 10:41:13 2005
***************
*** 301,309 ****
--- 301,321 ----
  	function wiki_attach_file($page, $name, $type, $size, $data, $comment, $user, $fhash) {
  		$comment = strip_tags($comment);
  		$now = date("U");
+ 		// If storage method is filesystem type ($fhash is defined)
+ 		// see if the record previously exists. If so, UPDATE it
+ 		// otherwise INSERT new record
+ 		if($fhash) {
+ 		$query = "select `attId` from `tiki_wiki_attachments` where `path`=?";
+ 		$existId = $this->getOne($query,array($fhash));
+ 		} else {$existId=FALSE;}
+ 		if($existId) {
+ 		$query = "update `tiki_wiki_attachments` set `page`=?,`filename`=?,`filesize`=?,`filetype`=?,`data`=?,`created`=?,`downloads`=0,`user`=?,`comment`=?,`path`=? where `attId`=?";
+ 		$result = $this->query($query,array("$page","$name", (int) $size,"$type","$data", (int) $now,"$user","$comment","$fhash","$existId"));
+ 		} else {
  		$query = "insert into `tiki_wiki_attachments`(`page`,`filename`,`filesize`,`filetype`,`data`,`created`,`downloads`,`user`,`comment`,`path`) values(?,?,?,?,?,?,0,?,?,?)";
  		$this->blob_encode($data);
  		$result = $this->query($query,array("$page","$name", (int) $size,"$type","$data", (int) $now,"$user","$comment","$fhash"));
+ 		}
  
  		global $feature_score;
  	        if ($feature_score == 'y') {
*** tiki-admin_include_wikiatt.php	Wed Nov 23 10:31:32 2005
***************
*** 38,44 ****
  	if ($_REQUEST['action'] == 'move2db') {
  		$wikilib->file_to_db($w_use_dir.$item['path'],$_REQUEST['attId']);
  	} elseif ($_REQUEST['action'] == 'move2file') {
! 		$wikilib->db_to_file($w_use_dir . md5($item['filename']),$_REQUEST['attId']);
  	}
  }
  
--- 38,44 ----
  	if ($_REQUEST['action'] == 'move2db') {
  		$wikilib->file_to_db($w_use_dir.$item['path'],$_REQUEST['attId']);
  	} elseif ($_REQUEST['action'] == 'move2file') {
! 		$wikilib->db_to_file($w_use_dir . md5($item['page'].$item['filename']),$_REQUEST['attId']);
  	}
  }
  
***************
*** 74,80 ****
  	$attachements = $wikilib->list_all_attachements();
  	for ($i=0;$i<$attachements['cant'];$i++) {
  		if (!$attachements['data'][$i]['path']) {
! 			$wikilib->db_to_file($w_use_dir. md5($attachements['data'][$i]['filename']),$attachements['data'][$i]['attId']);
  		}
  	}
  }
--- 74,80 ----
  	$attachements = $wikilib->list_all_attachements();
  	for ($i=0;$i<$attachements['cant'];$i++) {
  		if (!$attachements['data'][$i]['path']) {
! 			$wikilib->db_to_file($w_use_dir. md5($attachements['data'][$i]['page'].$attachements['data'][$i]['filename']),$attachements['data'][$i]['attId']);
  		}
  	}
  }
*** tiki-index.php	Wed Nov 23 10:31:32 2005
***************
*** 472,478 ****
  	    $data = '';
  	    $fhash='';
  	    if($w_use_db == 'n') {
! 		$fhash = md5($name = $_FILES['userfile1']['name']);    
  		$fw = fopen($w_use_dir.$fhash,"wb");
  		if(!$fw) {
  		    $smarty->assign('msg',tra('Cannot write to this file:').$fhash);
--- 472,478 ----
  	    $data = '';
  	    $fhash='';
  	    if($w_use_db == 'n') {
! 		$fhash = md5($name = $page.$_FILES['userfile1']['name']);    
  		$fw = fopen($w_use_dir.$fhash,"wb");
  		if(!$fw) {
  		    $smarty->assign('msg',tra('Cannot write to this file:').$fhash);
