openrat-cms

OpenRat Content Management System
git clone http://git.code.weiherhei.de/openrat-cms.git
Log | Files | Refs | README

Dav.class.php (2739B)


      1 <?php
      2 // OpenRat Content Management System
      3 // Copyright (C) 2002-2012 Jan Dankert, cms@jandankert.de
      4 //
      5 // This program is free software; you can redistribute it and/or
      6 // modify it under the terms of the GNU General Public License
      7 // as published by the Free Software Foundation; either version 2
      8 // of the License, or (at your option) any later version.
      9 //
     10 // This program is distributed in the hope that it will be useful,
     11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
     12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     13 // GNU General Public License for more details.
     14 //
     15 // You should have received a copy of the GNU General Public License
     16 // along with this program; if not, write to the Free Software
     17 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
     18 namespace cms\generator\target;
     19 
     20 use logger\Logger;
     21 use util\exception\PublisherException;
     22 use util\exception\UIException;
     23 use util\Url;
     24 
     25 
     26 /**
     27  * Publishing a file over WebDAV.
     28  *
     29  * @author Jan Dankert
     30  */
     31 class Dav extends BaseTarget
     32 {
     33 	/**
     34 	 * @var false|resource
     35 	 */
     36 	//private $socket;
     37 
     38 	public function checkConnection()
     39 	{
     40 		$dest = $this->url->path;
     41 
     42 		$content  = "HEAD /$dest HTTP/1.1\r\n";
     43 		$content .= "Host: ".$this->url->host."\r\n";
     44 		$content .= "Connection: Close\r\n";
     45 		$content .= "\r\n";
     46 
     47 		fwrite($this->createSocket(), $content );
     48 	}
     49 
     50 
     51 	public function put($source, $dest, $time)
     52 	{
     53 		$dest = $this->url->path . '/' . $dest;
     54 
     55 		$this->mkdirs( dirname($dest) ); // Try MKCOL
     56 
     57 		$content  = "PUT $dest HTTP/1.1\r\n";
     58 		$content .= "Host: ".$this->url->host."\r\n";
     59 		$content .= "Content-Length: ".filesize($source)."\r\n";
     60 		$content .= "Connection: Close\r\n";
     61 		$content .= "\r\n";
     62 
     63 		fwrite($this->createSocket(), $content.file_get_contents($source));
     64 	}
     65 
     66 
     67 	/**
     68 	 * resursive make the directory on DAV server.
     69 	 *
     70 	 * @param String path
     71 	 */
     72 	private function mkdirs($strPath)
     73 	{
     74 		$pStrPath = dirname($strPath);
     75 		if   ( $pStrPath && $pStrPath != '.' && $pStrPath != '/' )
     76 			$this->mkdirs($pStrPath);
     77 
     78 		$this->mkcol( $strPath );
     79 	}
     80 
     81 
     82 	public function mkcol( $dir ) {
     83 		$content = "MKCOL $dir HTTP/1.1\r\n";
     84 		$content .= "Host: ".$this->url->host."\r\n";
     85 		$content .= "Connection: Close\r\n";
     86 		$content .= "\r\n";
     87 		fwrite($this->createSocket(), $content);
     88 	}
     89 
     90 	public function close()
     91 	{
     92 		//fclose($this->socket);
     93 	}
     94 
     95 	public function open()
     96 	{
     97 		$this->checkConnection();
     98 	}
     99 
    100 	/**
    101 	 * @return false|resource
    102 	 */
    103 	protected function createSocket()
    104 	{
    105 		$socket = fsockopen($this->url->host, empty($this->url->port) ? 80 : $this->url->port, $errno, $errstr, 5);
    106 
    107 		if(!$socket)
    108 			throw new PublisherException("cannot connect to DAV server: $errno -> $errstr");
    109 
    110 		return $socket;
    111 
    112 	}
    113 }