/**
  *  PUT method handler
  *
  * @param  array  parameter passing array
  * @return string  HTTP status code or false
  */
 function PUT(&$options)
 {
     global $default;
     if ($this->checkSafeMode()) {
         $this->ktwebdavLog("Entering PUT. options are " . print_r($options, true), 'info', true);
         $this->ktwebdavLog("dav_client is: " . $this->dav_client, 'info', true);
         $path = $options["path"];
         // Fix for Mac
         // Modified - 22/10/07
         // Mac adds DS_Store files when folders are added and ._filename files when files are added
         // we want to ignore them.
         if ($this->dav_client == 'MC' || $this->dav_client == 'MG') {
             // Remove filename from path
             $aPath = explode('/', $path);
             $fileName = $aPath[count($aPath) - 1];
             if (strtolower($fileName) == '.ds_store') {
                 $this->ktwebdavLog("Using a mac client. Ignore the .DS_Store files created with every folder.", 'info', true);
                 // ignore
                 return "204 No Content";
             }
             if ($fileName[0] == '.' && $fileName[1] == '_') {
                 $fileName = substr($fileName, 2);
                 $this->ktwebdavLog("Using a mac client. Ignore the ._filename files created with every file.", 'info', true);
                 // ignore
                 return "204 No Content";
             }
         }
         $res = $this->_folderOrDocument($path);
         list($iFolderID, $iDocumentID) = $res;
         if ($iDocumentID === false && $iFolderID === false) {
             // Couldn't find intermediary paths
             /*
              * RFC2518: 8.7.1 PUT for Non-Collection Resources
              *
              * 409 (Conflict) - A PUT that would result in the creation
              * of a resource without an appropriately scoped parent collection
              * MUST fail with a 409 (Conflict).
              */
             return "409 Conflict - Couldn't find intermediary paths";
         }
         $oParentFolder =& Folder::get($iFolderID);
         // Check if the user has permissions to write in this folder
         $oPerm =& KTPermission::getByName('ktcore.permissions.write');
         $oUser =& User::get($this->userID);
         if (!KTPermissionUtil::userHasPermissionOnItem($oUser, $oPerm, $oParentFolder)) {
             return "403 Forbidden - User does not have sufficient permissions";
         }
         $this->ktwebdavLog("iDocumentID is " . $iDocumentID, 'info', true);
         if (is_null($iDocumentID)) {
             // This means there is a folder with the given path
             $this->ktwebdavLog("405 Method not allowed", 'info', true);
             return "405 Method not allowed - There is a folder with the given path";
         }
         if ($iDocumentID == false) {
             $this->ktwebdavLog("iDocumentID is false", 'info', true);
         }
         if ($iDocumentID !== false) {
             // This means there is a document with the given path
             $oDocument = Document::get($iDocumentID);
             $this->ktwebdavLog("oDocument is " . print_r($oDocument, true), 'info', true);
             $this->ktwebdavLog("oDocument statusid is " . print_r($oDocument->getStatusID(), true), 'info', true);
             if ((int) $oDocument->getStatusID() != STATUS_WEBDAV && (int) $oDocument->getStatusID() != DELETED) {
                 $this->ktwebdavLog("Trying to PUT to an existing document", 'info', true);
                 if (!$this->dav_client == "MS" && !$this->dav_client == "MC") {
                     return "409 Conflict - There is a document with the given path";
                 }
             }
             // FIXME: Direct filesystem access
             $fh = $options["stream"];
             $sTempFilename = tempnam('/tmp', 'ktwebdav_dav_put');
             $ofh = fopen($sTempFilename, 'w');
             $contents = '';
             while (!feof($fh)) {
                 $contents .= fread($fh, 8192);
             }
             $fres = fwrite($ofh, $contents);
             $this->ktwebdavLog("A DELETED or CHECKEDOUT document exists. Overwriting...", 'info', true);
             $this->ktwebdavLog("Temp Filename is: " . $sTempFilename, 'info', true);
             $this->ktwebdavLog("File write result size was: " . $fres, 'info', true);
             fflush($fh);
             fclose($fh);
             fflush($ofh);
             fclose($ofh);
             $this->ktwebdavLog("Files have been flushed and closed.", 'info', true);
             $name = basename($path);
             $aFileArray = array("name" => $name, "size" => filesize($sTempFilename), "type" => false, "userID" => $this->_getUserID());
             $this->ktwebdavLog("aFileArray is " . print_r($aFileArray, true), 'info', true);
             //include_once(KT_LIB_DIR . '/filelike/fsfilelike.inc.php');
             $aOptions = array('temp_file' => $sTempFilename, 'metadata' => array(), 'novalidate' => true);
             $this->ktwebdavLog("DEBUG: overwriting file. Options: " . print_r($aOptions, true));
             $this->ktwebdavLog("DEBUG: overwriting file. Temp name: " . $sTempFilename . ' ' . print_r($sTempFilename, true));
             $this->ktwebdavLog("DEBUG: overwriting file. Name: " . $name . ' ' . print_r($name, true));
             // Modified - 25/10/07 - changed add to overwrite
             //$oDocument =& KTDocumentUtil::add($oParentFolder, $name, $oUser, $aOptions);
             $oDocument =& KTDocumentUtil::overwrite($oDocument, $name, $sTempFilename, $oUser, $aOptions);
             if (PEAR::isError($oDocument)) {
                 $this->ktwebdavLog("oDocument ERROR: " . $oDocument->getMessage(), 'info', true);
                 unlink($sTempFilename);
                 return "409 Conflict - " . $oDocument->getMessage();
             }
             $this->ktwebdavLog("oDocument is " . print_r($oDocument, true), 'info', true);
             unlink($sTempFilename);
             return "201 Created";
         }
         $options["new"] = true;
         // FIXME: Direct filesystem access
         $fh = $options["stream"];
         $sTempFilename = tempnam('/tmp', 'ktwebdav_dav_put');
         $ofh = fopen($sTempFilename, 'w');
         $contents = '';
         while (!feof($fh)) {
             $contents .= fread($fh, 8192);
         }
         $fres = fwrite($ofh, $contents);
         $this->ktwebdavLog("Content length was not 0, doing the whole thing.", 'info', true);
         $this->ktwebdavLog("Temp Filename is: " . $sTempFilename, 'info', true);
         $this->ktwebdavLog("File write result size was: " . $fres, 'info', true);
         fflush($fh);
         fclose($fh);
         fflush($ofh);
         fclose($ofh);
         $this->ktwebdavLog("Files have been flushed and closed.", 'info', true);
         $name = basename($path);
         $aFileArray = array("name" => $name, "size" => filesize($sTempFilename), "type" => false, "userID" => $this->_getUserID());
         $this->ktwebdavLog("aFileArray is " . print_r($aFileArray, true), 'info', true);
         //include_once(KT_LIB_DIR . '/filelike/fsfilelike.inc.php');
         $aOptions = array('temp_file' => $sTempFilename, 'metadata' => array(), 'novalidate' => true);
         $oDocument =& KTDocumentUtil::add($oParentFolder, $name, $oUser, $aOptions);
         if (PEAR::isError($oDocument)) {
             $this->ktwebdavLog("oDocument ERROR: " . $oDocument->getMessage(), 'info', true);
             unlink($sTempFilename);
             return "409 Conflict - " . $oDocument->getMessage();
         }
         $this->ktwebdavLog("oDocument is " . print_r($oDocument, true), 'info', true);
         unlink($sTempFilename);
         return "201 Created";
     } else {
         return "423 Locked - KTWebDAV is in SafeMode";
     }
 }