/**
  * replace maildomain in input file
  * 
  * @param string $filename
  * @return string filename
  */
 public static function replaceEmailDomainInFile($filename)
 {
     $maildomain = self::getPrimaryMailDomain();
     $tempPath = Tinebase_TempFile::getTempPath();
     $contents = file_get_contents($filename);
     $contents = preg_replace('/tine20.org/', $maildomain, $contents);
     file_put_contents($tempPath, $contents);
     return $tempPath;
 }
 /**
  * replace maildomain in input file
  * 
  * @param string $filename
  * @return string filename
  */
 public static function replaceEmailDomainInFile($filename)
 {
     $config = TestServer::getInstance()->getConfig();
     $maildomain = $config->maildomain ? $config->maildomain : 'tine20.org';
     $tempPath = Tinebase_TempFile::getTempPath();
     $contents = file_get_contents($filename);
     $contents = preg_replace('/tine20.org/', $maildomain, $contents);
     file_put_contents($tempPath, $contents);
     return $tempPath;
 }
 /**
  * handle chunked upload
  * 
  * @param string $name Name of the file
  * @param resource $data payload, passed as a readable stream resource.
  * @throws \Sabre\DAV\Exception\BadRequest
  * @return boolean|Tinebase_Model_TempFile
  */
 public static function handleOwnCloudChunkedFileUpload($name, $data)
 {
     if (!isset($_SERVER['CONTENT_LENGTH'])) {
         throw new \Sabre\DAV\Exception\BadRequest('CONTENT_LENGTH header missing!');
     }
     $matched = preg_match('/(?P<name>.*)-chunking-(?P<tempId>\\d+)-(?P<totalCount>\\d+)-(?P<chunkId>\\d+)/', $name, $chunkInfo);
     if (!$matched) {
         throw new \Sabre\DAV\Exception\BadRequest('bad filename provided: ' . $name);
     }
     // copy chunk to temp file
     $path = Tinebase_TempFile::getTempPath();
     $tempfileHandle = fopen($path, "w");
     if (!$tempfileHandle) {
         throw new \Sabre\DAV\Exception\BadRequest('Could not open tempfile while uploading! ');
     }
     do {
         stream_copy_to_stream($data, $tempfileHandle);
     } while (!feof($data));
     $stat = fstat($tempfileHandle);
     if ($_SERVER['CONTENT_LENGTH'] != $stat['size']) {
         throw new \Sabre\DAV\Exception\BadRequest('uploaded part incomplete! expected size of: ' . $_SERVER['CONTENT_LENGTH'] . ' got: ' . $stat['size']);
     }
     fclose($tempfileHandle);
     $tempFileName = sha1(Tinebase_Core::getUser()->accountId . $chunkInfo['name'] . $chunkInfo['tempId']);
     Tinebase_TempFile::getInstance()->createTempFile($path, $tempFileName, $chunkInfo['chunkId'] + 1);
     // check if the client sent all chunks
     $uploadedChunks = Tinebase_TempFile::getInstance()->search(new Tinebase_Model_TempFileFilter(array(array('field' => 'name', 'operator' => 'equals', 'value' => $tempFileName))), new Tinebase_Model_Pagination(array('sort' => 'type', 'dir' => 'ASC')));
     if ($uploadedChunks->count() != $chunkInfo['totalCount']) {
         return false;
     }
     // combine all chunks to one file
     $joinedFile = Tinebase_TempFile::getInstance()->joinTempFiles($uploadedChunks);
     $joinedFile->name = $chunkInfo['name'];
     foreach ($uploadedChunks as $chunk) {
         unlink($chunk->path);
     }
     return $joinedFile;
 }